org.el 526 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.09
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.09"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-default-extensions'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global id's for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " mouse: Additional mouse support" org-mouse)
  155. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  156. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  157. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  158. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  159. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  160. (const :tag "C eval: Include command output as text" org-eval)
  161. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  162. (const :tag "C id: Global id's for identifying entries" org-id)
  163. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  164. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  165. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  166. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  167. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  168. (const :tag "C registry: A registry for Org links" org-registry)
  169. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  170. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  171. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  172. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  173. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  174. (defgroup org-startup nil
  175. "Options concerning startup of Org-mode."
  176. :tag "Org Startup"
  177. :group 'org)
  178. (defcustom org-startup-folded t
  179. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  180. This can also be configured on a per-file basis by adding one of
  181. the following lines anywhere in the buffer:
  182. #+STARTUP: fold
  183. #+STARTUP: nofold
  184. #+STARTUP: content"
  185. :group 'org-startup
  186. :type '(choice
  187. (const :tag "nofold: show all" nil)
  188. (const :tag "fold: overview" t)
  189. (const :tag "content: all headlines" content)))
  190. (defcustom org-startup-truncated t
  191. "Non-nil means, entering Org-mode will set `truncate-lines'.
  192. This is useful since some lines containing links can be very long and
  193. uninteresting. Also tables look terrible when wrapped."
  194. :group 'org-startup
  195. :type 'boolean)
  196. (defcustom org-startup-align-all-tables nil
  197. "Non-nil means, align all tables when visiting a file.
  198. This is useful when the column width in tables is forced with <N> cookies
  199. in table fields. Such tables will look correct only after the first re-align.
  200. This can also be configured on a per-file basis by adding one of
  201. the following lines anywhere in the buffer:
  202. #+STARTUP: align
  203. #+STARTUP: noalign"
  204. :group 'org-startup
  205. :type 'boolean)
  206. (defcustom org-insert-mode-line-in-empty-file nil
  207. "Non-nil means insert the first line setting Org-mode in empty files.
  208. When the function `org-mode' is called interactively in an empty file, this
  209. normally means that the file name does not automatically trigger Org-mode.
  210. To ensure that the file will always be in Org-mode in the future, a
  211. line enforcing Org-mode will be inserted into the buffer, if this option
  212. has been set."
  213. :group 'org-startup
  214. :type 'boolean)
  215. (defcustom org-replace-disputed-keys nil
  216. "Non-nil means use alternative key bindings for some keys.
  217. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  218. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  219. If you want to use Org-mode together with one of these other modes,
  220. or more generally if you would like to move some Org-mode commands to
  221. other keys, set this variable and configure the keys with the variable
  222. `org-disputed-keys'.
  223. This option is only relevant at load-time of Org-mode, and must be set
  224. *before* org.el is loaded. Changing it requires a restart of Emacs to
  225. become effective."
  226. :group 'org-startup
  227. :type 'boolean)
  228. (defcustom org-use-extra-keys nil
  229. "Non-nil means use extra key sequence definitions for certain
  230. commands. This happens automatically if you run XEmacs or if
  231. window-system is nil. This variable lets you do the same
  232. manually. You must set it before loading org.
  233. Example: on Carbon Emacs 22 running graphically, with an external
  234. keyboard on a Powerbook, the default way of setting M-left might
  235. not work for either Alt or ESC. Setting this variable will make
  236. it work for ESC."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (if (fboundp 'defvaralias)
  240. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  241. (defcustom org-disputed-keys
  242. '(([(shift up)] . [(meta p)])
  243. ([(shift down)] . [(meta n)])
  244. ([(shift left)] . [(meta -)])
  245. ([(shift right)] . [(meta +)])
  246. ([(control shift right)] . [(meta shift +)])
  247. ([(control shift left)] . [(meta shift -)]))
  248. "Keys for which Org-mode and other modes compete.
  249. This is an alist, cars are the default keys, second element specifies
  250. the alternative to use when `org-replace-disputed-keys' is t.
  251. Keys can be specified in any syntax supported by `define-key'.
  252. The value of this option takes effect only at Org-mode's startup,
  253. therefore you'll have to restart Emacs to apply it after changing."
  254. :group 'org-startup
  255. :type 'alist)
  256. (defun org-key (key)
  257. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  258. Or return the original if not disputed."
  259. (if org-replace-disputed-keys
  260. (let* ((nkey (key-description key))
  261. (x (org-find-if (lambda (x)
  262. (equal (key-description (car x)) nkey))
  263. org-disputed-keys)))
  264. (if x (cdr x) key))
  265. key))
  266. (defun org-find-if (predicate seq)
  267. (catch 'exit
  268. (while seq
  269. (if (funcall predicate (car seq))
  270. (throw 'exit (car seq))
  271. (pop seq)))))
  272. (defun org-defkey (keymap key def)
  273. "Define a key, possibly translated, as returned by `org-key'."
  274. (define-key keymap (org-key key) def))
  275. (defcustom org-ellipsis nil
  276. "The ellipsis to use in the Org-mode outline.
  277. When nil, just use the standard three dots. When a string, use that instead,
  278. When a face, use the standart 3 dots, but with the specified face.
  279. The change affects only Org-mode (which will then use its own display table).
  280. Changing this requires executing `M-x org-mode' in a buffer to become
  281. effective."
  282. :group 'org-startup
  283. :type '(choice (const :tag "Default" nil)
  284. (face :tag "Face" :value org-warning)
  285. (string :tag "String" :value "...#")))
  286. (defvar org-display-table nil
  287. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  288. (defgroup org-keywords nil
  289. "Keywords in Org-mode."
  290. :tag "Org Keywords"
  291. :group 'org)
  292. (defcustom org-deadline-string "DEADLINE:"
  293. "String to mark deadline entries.
  294. A deadline is this string, followed by a time stamp. Should be a word,
  295. terminated by a colon. You can insert a schedule keyword and
  296. a timestamp with \\[org-deadline].
  297. Changes become only effective after restarting Emacs."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-scheduled-string "SCHEDULED:"
  301. "String to mark scheduled TODO entries.
  302. A schedule is this string, followed by a time stamp. Should be a word,
  303. terminated by a colon. You can insert a schedule keyword and
  304. a timestamp with \\[org-schedule].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-closed-string "CLOSED:"
  309. "String used as the prefix for timestamps logging closing a TODO entry."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-clock-string "CLOCK:"
  313. "String used as prefix for timestamps clocking work hours on an item."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-comment-string "COMMENT"
  317. "Entries starting with this keyword will never be exported.
  318. An entry can be toggled between COMMENT and normal with
  319. \\[org-toggle-comment].
  320. Changes become only effective after restarting Emacs."
  321. :group 'org-keywords
  322. :type 'string)
  323. (defcustom org-quote-string "QUOTE"
  324. "Entries starting with this keyword will be exported in fixed-width font.
  325. Quoting applies only to the text in the entry following the headline, and does
  326. not extend beyond the next headline, even if that is lower level.
  327. An entry can be toggled between QUOTE and normal with
  328. \\[org-toggle-fixed-width-section]."
  329. :group 'org-keywords
  330. :type 'string)
  331. (defconst org-repeat-re
  332. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  333. "Regular expression for specifying repeated events.
  334. After a match, group 1 contains the repeat expression.")
  335. (defgroup org-structure nil
  336. "Options concerning the general structure of Org-mode files."
  337. :tag "Org Structure"
  338. :group 'org)
  339. (defgroup org-reveal-location nil
  340. "Options about how to make context of a location visible."
  341. :tag "Org Reveal Location"
  342. :group 'org-structure)
  343. (defconst org-context-choice
  344. '(choice
  345. (const :tag "Always" t)
  346. (const :tag "Never" nil)
  347. (repeat :greedy t :tag "Individual contexts"
  348. (cons
  349. (choice :tag "Context"
  350. (const agenda)
  351. (const org-goto)
  352. (const occur-tree)
  353. (const tags-tree)
  354. (const link-search)
  355. (const mark-goto)
  356. (const bookmark-jump)
  357. (const isearch)
  358. (const default))
  359. (boolean))))
  360. "Contexts for the reveal options.")
  361. (defcustom org-show-hierarchy-above '((default . t))
  362. "Non-nil means, show full hierarchy when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the hierarchy of headings
  365. above the exposed location is shown.
  366. Turning this off for example for sparse trees makes them very compact.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. Valid contexts are
  369. agenda when exposing an entry from the agenda
  370. org-goto when using the command `org-goto' on key C-c C-j
  371. occur-tree when using the command `org-occur' on key C-c /
  372. tags-tree when constructing a sparse tree based on tags matches
  373. link-search when exposing search matches associated with a link
  374. mark-goto when exposing the jump goal of a mark
  375. bookmark-jump when exposing a bookmark location
  376. isearch when exiting from an incremental search
  377. default default for all contexts not set explicitly"
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-following-heading '((default . nil))
  381. "Non-nil means, show following heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the heading following the
  384. match is shown.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-siblings '((default . nil) (isearch t))
  393. "Non-nil means, show all sibling heading when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the sibling of the current entry
  396. heading are all made visible. If `org-show-hierarchy-above' is t,
  397. the same happens on each level of the hierarchy above the current entry.
  398. By default this is on for the isearch context, off for all other contexts.
  399. Turning this off for example for sparse trees makes them very compact,
  400. but makes it harder to edit the location of the match. In such a case,
  401. use the command \\[org-reveal] to show more context.
  402. Instead of t, this can also be an alist specifying this option for different
  403. contexts. See `org-show-hierarchy-above' for valid contexts."
  404. :group 'org-reveal-location
  405. :type org-context-choice)
  406. (defcustom org-show-entry-below '((default . nil))
  407. "Non-nil means, show the entry below a headline when revealing a location.
  408. Org-mode often shows locations in an org-mode file which might have
  409. been invisible before. When this is set, the text below the headline that is
  410. exposed is also shown.
  411. By default this is off for all contexts.
  412. Instead of t, this can also be an alist specifying this option for different
  413. contexts. See `org-show-hierarchy-above' for valid contexts."
  414. :group 'org-reveal-location
  415. :type org-context-choice)
  416. (defcustom org-indirect-buffer-display 'other-window
  417. "How should indirect tree buffers be displayed?
  418. This applies to indirect buffers created with the commands
  419. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  420. Valid values are:
  421. current-window Display in the current window
  422. other-window Just display in another window.
  423. dedicated-frame Create one new frame, and re-use it each time.
  424. new-frame Make a new frame each time. Note that in this case
  425. previously-made indirect buffers are kept, and you need to
  426. kill these buffers yourself."
  427. :group 'org-structure
  428. :group 'org-agenda-windows
  429. :type '(choice
  430. (const :tag "In current window" current-window)
  431. (const :tag "In current frame, other window" other-window)
  432. (const :tag "Each time a new frame" new-frame)
  433. (const :tag "One dedicated frame" dedicated-frame)))
  434. (defgroup org-cycle nil
  435. "Options concerning visibility cycling in Org-mode."
  436. :tag "Org Cycle"
  437. :group 'org-structure)
  438. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  439. "Names of drawers. Drawers are not opened by cycling on the headline above.
  440. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  441. this:
  442. :DRAWERNAME:
  443. .....
  444. :END:
  445. The drawer \"PROPERTIES\" is special for capturing properties through
  446. the property API.
  447. Drawers can be defined on the per-file basis with a line like:
  448. #+DRAWERS: HIDDEN STATE PROPERTIES"
  449. :group 'org-structure
  450. :type '(repeat (string :tag "Drawer Name")))
  451. (defcustom org-cycle-global-at-bob nil
  452. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  453. This makes it possible to do global cycling without having to use S-TAB or
  454. C-u TAB. For this special case to work, the first line of the buffer
  455. must not be a headline - it may be empty ot some other text. When used in
  456. this way, `org-cycle-hook' is disables temporarily, to make sure the
  457. cursor stays at the beginning of the buffer.
  458. When this option is nil, don't do anything special at the beginning
  459. of the buffer."
  460. :group 'org-cycle
  461. :type 'boolean)
  462. (defcustom org-cycle-emulate-tab t
  463. "Where should `org-cycle' emulate TAB.
  464. nil Never
  465. white Only in completely white lines
  466. whitestart Only at the beginning of lines, before the first non-white char
  467. t Everywhere except in headlines
  468. exc-hl-bol Everywhere except at the start of a headline
  469. If TAB is used in a place where it does not emulate TAB, the current subtree
  470. visibility is cycled."
  471. :group 'org-cycle
  472. :type '(choice (const :tag "Never" nil)
  473. (const :tag "Only in completely white lines" white)
  474. (const :tag "Before first char in a line" whitestart)
  475. (const :tag "Everywhere except in headlines" t)
  476. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  477. ))
  478. (defcustom org-cycle-separator-lines 2
  479. "Number of empty lines needed to keep an empty line between collapsed trees.
  480. If you leave an empty line between the end of a subtree and the following
  481. headline, this empty line is hidden when the subtree is folded.
  482. Org-mode will leave (exactly) one empty line visible if the number of
  483. empty lines is equal or larger to the number given in this variable.
  484. So the default 2 means, at least 2 empty lines after the end of a subtree
  485. are needed to produce free space between a collapsed subtree and the
  486. following headline.
  487. Special case: when 0, never leave empty lines in collapsed view."
  488. :group 'org-cycle
  489. :type 'integer)
  490. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  491. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  492. org-cycle-hide-drawers
  493. org-cycle-show-empty-lines
  494. org-optimize-window-after-visibility-change)
  495. "Hook that is run after `org-cycle' has changed the buffer visibility.
  496. The function(s) in this hook must accept a single argument which indicates
  497. the new state that was set by the most recent `org-cycle' command. The
  498. argument is a symbol. After a global state change, it can have the values
  499. `overview', `content', or `all'. After a local state change, it can have
  500. the values `folded', `children', or `subtree'."
  501. :group 'org-cycle
  502. :type 'hook)
  503. (defgroup org-edit-structure nil
  504. "Options concerning structure editing in Org-mode."
  505. :tag "Org Edit Structure"
  506. :group 'org-structure)
  507. (defcustom org-odd-levels-only nil
  508. "Non-nil means, skip even levels and only use odd levels for the outline.
  509. This has the effect that two stars are being added/taken away in
  510. promotion/demotion commands. It also influences how levels are
  511. handled by the exporters.
  512. Changing it requires restart of `font-lock-mode' to become effective
  513. for fontification also in regions already fontified.
  514. You may also set this on a per-file basis by adding one of the following
  515. lines to the buffer:
  516. #+STARTUP: odd
  517. #+STARTUP: oddeven"
  518. :group 'org-edit-structure
  519. :group 'org-font-lock
  520. :type 'boolean)
  521. (defcustom org-adapt-indentation t
  522. "Non-nil means, adapt indentation when promoting and demoting.
  523. When this is set and the *entire* text in an entry is indented, the
  524. indentation is increased by one space in a demotion command, and
  525. decreased by one in a promotion command. If any line in the entry
  526. body starts at column 0, indentation is not changed at all."
  527. :group 'org-edit-structure
  528. :type 'boolean)
  529. (defcustom org-special-ctrl-a/e nil
  530. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  531. When t, `C-a' will bring back the cursor to the beginning of the
  532. headline text, i.e. after the stars and after a possible TODO keyword.
  533. In an item, this will be the position after the bullet.
  534. When the cursor is already at that position, another `C-a' will bring
  535. it to the beginning of the line.
  536. `C-e' will jump to the end of the headline, ignoring the presence of tags
  537. in the headline. A second `C-e' will then jump to the true end of the
  538. line, after any tags.
  539. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  540. and only a directly following, identical keypress will bring the cursor
  541. to the special positions."
  542. :group 'org-edit-structure
  543. :type '(choice
  544. (const :tag "off" nil)
  545. (const :tag "after bullet first" t)
  546. (const :tag "border first" reversed)))
  547. (if (fboundp 'defvaralias)
  548. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  549. (defcustom org-special-ctrl-k nil
  550. "Non-nil means `C-k' will behave specially in headlines.
  551. When nil, `C-k' will call the default `kill-line' command.
  552. When t, the following will happen while the cursor is in the headline:
  553. - When the cursor is at the beginning of a headline, kill the entire
  554. line and possible the folded subtree below the line.
  555. - When in the middle of the headline text, kill the headline up to the tags.
  556. - When after the headline text, kill the tags."
  557. :group 'org-edit-structure
  558. :type 'boolean)
  559. (defcustom org-yank-folded-subtrees t
  560. "Non-nil means, when yanking subtrees, fold them.
  561. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  562. it starts with a heading and all other headings in it are either children
  563. or siblings, then fold all the subtrees."
  564. :group 'org-edit-structure
  565. :type 'boolean)
  566. (defcustom org-M-RET-may-split-line '((default . t))
  567. "Non-nil means, M-RET will split the line at the cursor position.
  568. When nil, it will go to the end of the line before making a
  569. new line.
  570. You may also set this option in a different way for different
  571. contexts. Valid contexts are:
  572. headline when creating a new headline
  573. item when creating a new item
  574. table in a table field
  575. default the value to be used for all contexts not explicitly
  576. customized"
  577. :group 'org-structure
  578. :group 'org-table
  579. :type '(choice
  580. (const :tag "Always" t)
  581. (const :tag "Never" nil)
  582. (repeat :greedy t :tag "Individual contexts"
  583. (cons
  584. (choice :tag "Context"
  585. (const headline)
  586. (const item)
  587. (const table)
  588. (const default))
  589. (boolean)))))
  590. (defcustom org-insert-heading-respect-content nil
  591. "Non-nil means, insert new headings after the current subtree.
  592. When nil, the new heading is created directly after the current line.
  593. The commands \\[org-insert-heading-respect-content] and
  594. \\[org-insert-todo-heading-respect-content] turn this variable on
  595. for the duration of the command."
  596. :group 'org-structure
  597. :type 'boolean)
  598. (defcustom org-blank-before-new-entry '((heading . nil)
  599. (plain-list-item . nil))
  600. "Should `org-insert-heading' leave a blank line before new heading/item?
  601. The value is an alist, with `heading' and `plain-list-item' as car,
  602. and a boolean flag as cdr."
  603. :group 'org-edit-structure
  604. :type '(list
  605. (cons (const heading) (boolean))
  606. (cons (const plain-list-item) (boolean))))
  607. (defcustom org-insert-heading-hook nil
  608. "Hook being run after inserting a new heading."
  609. :group 'org-edit-structure
  610. :type 'hook)
  611. (defcustom org-enable-fixed-width-editor t
  612. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  613. This currently only means, they are never auto-wrapped.
  614. When nil, such lines will be treated like ordinary lines.
  615. See also the QUOTE keyword."
  616. :group 'org-edit-structure
  617. :type 'boolean)
  618. (defcustom org-edit-src-region-extra nil
  619. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  620. For examples see the function `org-edit-src-find-region-and-lang'.
  621. The regular expression identifying the begin marker should end with a newline,
  622. and the regexp marking the end line should start with a newline, to make sure
  623. there are kept outside the narrowed region."
  624. :group 'org-edit-structure
  625. :type '(repeat
  626. (list
  627. (regexp :tag "begin regexp")
  628. (regexp :tag "end regexp")
  629. (choice :tag "language"
  630. (string :tag "specify")
  631. (integer :tag "from match group")
  632. (const :tag "from `lang' element")
  633. (const :tag "from `style' element")))))
  634. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  635. "The mode that should be used to edit fixed-width regions.
  636. These are the regions where each line starts with a colon."
  637. :group 'org-edit-structure
  638. :type '(choice
  639. (const artist-mode)
  640. (const picture-mode)
  641. (const fundamental-mode)
  642. (function :tag "Other (specify)")))
  643. (defcustom org-goto-auto-isearch t
  644. "Non-nil means, typing characters in org-goto starts incremental search."
  645. :group 'org-edit-structure
  646. :type 'boolean)
  647. (defgroup org-sparse-trees nil
  648. "Options concerning sparse trees in Org-mode."
  649. :tag "Org Sparse Trees"
  650. :group 'org-structure)
  651. (defcustom org-highlight-sparse-tree-matches t
  652. "Non-nil means, highlight all matches that define a sparse tree.
  653. The highlights will automatically disappear the next time the buffer is
  654. changed by an edit command."
  655. :group 'org-sparse-trees
  656. :type 'boolean)
  657. (defcustom org-remove-highlights-with-change t
  658. "Non-nil means, any change to the buffer will remove temporary highlights.
  659. Such highlights are created by `org-occur' and `org-clock-display'.
  660. When nil, `C-c C-c needs to be used to get rid of the highlights.
  661. The highlights created by `org-preview-latex-fragment' always need
  662. `C-c C-c' to be removed."
  663. :group 'org-sparse-trees
  664. :group 'org-time
  665. :type 'boolean)
  666. (defcustom org-occur-hook '(org-first-headline-recenter)
  667. "Hook that is run after `org-occur' has constructed a sparse tree.
  668. This can be used to recenter the window to show as much of the structure
  669. as possible."
  670. :group 'org-sparse-trees
  671. :type 'hook)
  672. (defgroup org-imenu-and-speedbar nil
  673. "Options concerning imenu and speedbar in Org-mode."
  674. :tag "Org Imenu and Speedbar"
  675. :group 'org-structure)
  676. (defcustom org-imenu-depth 2
  677. "The maximum level for Imenu access to Org-mode headlines.
  678. This also applied for speedbar access."
  679. :group 'org-imenu-and-speedbar
  680. :type 'number)
  681. (defgroup org-table nil
  682. "Options concerning tables in Org-mode."
  683. :tag "Org Table"
  684. :group 'org)
  685. (defcustom org-enable-table-editor 'optimized
  686. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  687. When nil, such lines will be treated like ordinary lines.
  688. When equal to the symbol `optimized', the table editor will be optimized to
  689. do the following:
  690. - Automatic overwrite mode in front of whitespace in table fields.
  691. This makes the structure of the table stay in tact as long as the edited
  692. field does not exceed the column width.
  693. - Minimize the number of realigns. Normally, the table is aligned each time
  694. TAB or RET are pressed to move to another field. With optimization this
  695. happens only if changes to a field might have changed the column width.
  696. Optimization requires replacing the functions `self-insert-command',
  697. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  698. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  699. very good at guessing when a re-align will be necessary, but you can always
  700. force one with \\[org-ctrl-c-ctrl-c].
  701. If you would like to use the optimized version in Org-mode, but the
  702. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  703. This variable can be used to turn on and off the table editor during a session,
  704. but in order to toggle optimization, a restart is required.
  705. See also the variable `org-table-auto-blank-field'."
  706. :group 'org-table
  707. :type '(choice
  708. (const :tag "off" nil)
  709. (const :tag "on" t)
  710. (const :tag "on, optimized" optimized)))
  711. (defcustom org-table-tab-recognizes-table.el t
  712. "Non-nil means, TAB will automatically notice a table.el table.
  713. When it sees such a table, it moves point into it and - if necessary -
  714. calls `table-recognize-table'."
  715. :group 'org-table-editing
  716. :type 'boolean)
  717. (defgroup org-link nil
  718. "Options concerning links in Org-mode."
  719. :tag "Org Link"
  720. :group 'org)
  721. (defvar org-link-abbrev-alist-local nil
  722. "Buffer-local version of `org-link-abbrev-alist', which see.
  723. The value of this is taken from the #+LINK lines.")
  724. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  725. (defcustom org-link-abbrev-alist nil
  726. "Alist of link abbreviations.
  727. The car of each element is a string, to be replaced at the start of a link.
  728. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  729. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  730. [[linkkey:tag][description]]
  731. If REPLACE is a string, the tag will simply be appended to create the link.
  732. If the string contains \"%s\", the tag will be inserted there.
  733. REPLACE may also be a function that will be called with the tag as the
  734. only argument to create the link, which should be returned as a string.
  735. See the manual for examples."
  736. :group 'org-link
  737. :type 'alist)
  738. (defcustom org-descriptive-links t
  739. "Non-nil means, hide link part and only show description of bracket links.
  740. Bracket links are like [[link][descritpion]]. This variable sets the initial
  741. state in new org-mode buffers. The setting can then be toggled on a
  742. per-buffer basis from the Org->Hyperlinks menu."
  743. :group 'org-link
  744. :type 'boolean)
  745. (defcustom org-link-file-path-type 'adaptive
  746. "How the path name in file links should be stored.
  747. Valid values are:
  748. relative Relative to the current directory, i.e. the directory of the file
  749. into which the link is being inserted.
  750. absolute Absolute path, if possible with ~ for home directory.
  751. noabbrev Absolute path, no abbreviation of home directory.
  752. adaptive Use relative path for files in the current directory and sub-
  753. directories of it. For other files, use an absolute path."
  754. :group 'org-link
  755. :type '(choice
  756. (const relative)
  757. (const absolute)
  758. (const noabbrev)
  759. (const adaptive)))
  760. (defcustom org-activate-links '(bracket angle plain radio tag date)
  761. "Types of links that should be activated in Org-mode files.
  762. This is a list of symbols, each leading to the activation of a certain link
  763. type. In principle, it does not hurt to turn on most link types - there may
  764. be a small gain when turning off unused link types. The types are:
  765. bracket The recommended [[link][description]] or [[link]] links with hiding.
  766. angular Links in angular brackes that may contain whitespace like
  767. <bbdb:Carsten Dominik>.
  768. plain Plain links in normal text, no whitespace, like http://google.com.
  769. radio Text that is matched by a radio target, see manual for details.
  770. tag Tag settings in a headline (link to tag search).
  771. date Time stamps (link to calendar).
  772. Changing this variable requires a restart of Emacs to become effective."
  773. :group 'org-link
  774. :type '(set (const :tag "Double bracket links (new style)" bracket)
  775. (const :tag "Angular bracket links (old style)" angular)
  776. (const :tag "Plain text links" plain)
  777. (const :tag "Radio target matches" radio)
  778. (const :tag "Tags" tag)
  779. (const :tag "Timestamps" date)))
  780. (defcustom org-make-link-description-function nil
  781. "Function to use to generate link descriptions from links. If
  782. nil the link location will be used. This function must take two
  783. parameters; the first is the link and the second the description
  784. org-insert-link has generated, and should return the description
  785. to use."
  786. :group 'org-link
  787. :type 'function)
  788. (defgroup org-link-store nil
  789. "Options concerning storing links in Org-mode."
  790. :tag "Org Store Link"
  791. :group 'org-link)
  792. (defcustom org-email-link-description-format "Email %c: %.30s"
  793. "Format of the description part of a link to an email or usenet message.
  794. The following %-excapes will be replaced by corresponding information:
  795. %F full \"From\" field
  796. %f name, taken from \"From\" field, address if no name
  797. %T full \"To\" field
  798. %t first name in \"To\" field, address if no name
  799. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  800. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  801. %s subject
  802. %m message-id.
  803. You may use normal field width specification between the % and the letter.
  804. This is for example useful to limit the length of the subject.
  805. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  806. :group 'org-link-store
  807. :type 'string)
  808. (defcustom org-from-is-user-regexp
  809. (let (r1 r2)
  810. (when (and user-mail-address (not (string= user-mail-address "")))
  811. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  812. (when (and user-full-name (not (string= user-full-name "")))
  813. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  814. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  815. "Regexp mached against the \"From:\" header of an email or usenet message.
  816. It should match if the message is from the user him/herself."
  817. :group 'org-link-store
  818. :type 'regexp)
  819. (defcustom org-context-in-file-links t
  820. "Non-nil means, file links from `org-store-link' contain context.
  821. A search string will be added to the file name with :: as separator and
  822. used to find the context when the link is activated by the command
  823. `org-open-at-point'.
  824. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  825. negates this setting for the duration of the command."
  826. :group 'org-link-store
  827. :type 'boolean)
  828. (defcustom org-keep-stored-link-after-insertion nil
  829. "Non-nil means, keep link in list for entire session.
  830. The command `org-store-link' adds a link pointing to the current
  831. location to an internal list. These links accumulate during a session.
  832. The command `org-insert-link' can be used to insert links into any
  833. Org-mode file (offering completion for all stored links). When this
  834. option is nil, every link which has been inserted once using \\[org-insert-link]
  835. will be removed from the list, to make completing the unused links
  836. more efficient."
  837. :group 'org-link-store
  838. :type 'boolean)
  839. (defgroup org-link-follow nil
  840. "Options concerning following links in Org-mode."
  841. :tag "Org Follow Link"
  842. :group 'org-link)
  843. (defcustom org-follow-link-hook nil
  844. "Hook that is run after a link has been followed."
  845. :group 'org-link-follow
  846. :type 'hook)
  847. (defcustom org-tab-follows-link nil
  848. "Non-nil means, on links TAB will follow the link.
  849. Needs to be set before org.el is loaded."
  850. :group 'org-link-follow
  851. :type 'boolean)
  852. (defcustom org-return-follows-link nil
  853. "Non-nil means, on links RET will follow the link.
  854. Needs to be set before org.el is loaded."
  855. :group 'org-link-follow
  856. :type 'boolean)
  857. (defcustom org-mouse-1-follows-link
  858. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  859. "Non-nil means, mouse-1 on a link will follow the link.
  860. A longer mouse click will still set point. Does not work on XEmacs.
  861. Needs to be set before org.el is loaded."
  862. :group 'org-link-follow
  863. :type 'boolean)
  864. (defcustom org-mark-ring-length 4
  865. "Number of different positions to be recorded in the ring
  866. Changing this requires a restart of Emacs to work correctly."
  867. :group 'org-link-follow
  868. :type 'interger)
  869. (defcustom org-link-frame-setup
  870. '((vm . vm-visit-folder-other-frame)
  871. (gnus . gnus-other-frame)
  872. (file . find-file-other-window))
  873. "Setup the frame configuration for following links.
  874. When following a link with Emacs, it may often be useful to display
  875. this link in another window or frame. This variable can be used to
  876. set this up for the different types of links.
  877. For VM, use any of
  878. `vm-visit-folder'
  879. `vm-visit-folder-other-frame'
  880. For Gnus, use any of
  881. `gnus'
  882. `gnus-other-frame'
  883. For FILE, use any of
  884. `find-file'
  885. `find-file-other-window'
  886. `find-file-other-frame'
  887. For the calendar, use the variable `calendar-setup'.
  888. For BBDB, it is currently only possible to display the matches in
  889. another window."
  890. :group 'org-link-follow
  891. :type '(list
  892. (cons (const vm)
  893. (choice
  894. (const vm-visit-folder)
  895. (const vm-visit-folder-other-window)
  896. (const vm-visit-folder-other-frame)))
  897. (cons (const gnus)
  898. (choice
  899. (const gnus)
  900. (const gnus-other-frame)))
  901. (cons (const file)
  902. (choice
  903. (const find-file)
  904. (const find-file-other-window)
  905. (const find-file-other-frame)))))
  906. (defcustom org-display-internal-link-with-indirect-buffer nil
  907. "Non-nil means, use indirect buffer to display infile links.
  908. Activating internal links (from one location in a file to another location
  909. in the same file) normally just jumps to the location. When the link is
  910. activated with a C-u prefix (or with mouse-3), the link is displayed in
  911. another window. When this option is set, the other window actually displays
  912. an indirect buffer clone of the current buffer, to avoid any visibility
  913. changes to the current buffer."
  914. :group 'org-link-follow
  915. :type 'boolean)
  916. (defcustom org-open-non-existing-files nil
  917. "Non-nil means, `org-open-file' will open non-existing files.
  918. When nil, an error will be generated."
  919. :group 'org-link-follow
  920. :type 'boolean)
  921. (defcustom org-open-directory-means-index-dot-org nil
  922. "Non-nil means, a link to a directory really means to index.org.
  923. When nil, following a directory link will run dired or open a finder/explorer
  924. window on that directory."
  925. :group 'org-link-follow
  926. :type 'boolean)
  927. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  928. "Function and arguments to call for following mailto links.
  929. This is a list with the first element being a lisp function, and the
  930. remaining elements being arguments to the function. In string arguments,
  931. %a will be replaced by the address, and %s will be replaced by the subject
  932. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  933. :group 'org-link-follow
  934. :type '(choice
  935. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  936. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  937. (const :tag "message-mail" (message-mail "%a" "%s"))
  938. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  939. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  940. "Non-nil means, ask for confirmation before executing shell links.
  941. Shell links can be dangerous: just think about a link
  942. [[shell:rm -rf ~/*][Google Search]]
  943. This link would show up in your Org-mode document as \"Google Search\",
  944. but really it would remove your entire home directory.
  945. Therefore we advise against setting this variable to nil.
  946. Just change it to `y-or-n-p' of you want to confirm with a
  947. single keystroke rather than having to type \"yes\"."
  948. :group 'org-link-follow
  949. :type '(choice
  950. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  951. (const :tag "with y-or-n (faster)" y-or-n-p)
  952. (const :tag "no confirmation (dangerous)" nil)))
  953. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  954. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  955. Elisp links can be dangerous: just think about a link
  956. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  957. This link would show up in your Org-mode document as \"Google Search\",
  958. but really it would remove your entire home directory.
  959. Therefore we advise against setting this variable to nil.
  960. Just change it to `y-or-n-p' of you want to confirm with a
  961. single keystroke rather than having to type \"yes\"."
  962. :group 'org-link-follow
  963. :type '(choice
  964. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  965. (const :tag "with y-or-n (faster)" y-or-n-p)
  966. (const :tag "no confirmation (dangerous)" nil)))
  967. (defconst org-file-apps-defaults-gnu
  968. '((remote . emacs)
  969. (t . mailcap))
  970. "Default file applications on a UNIX or GNU/Linux system.
  971. See `org-file-apps'.")
  972. (defconst org-file-apps-defaults-macosx
  973. '((remote . emacs)
  974. (t . "open %s")
  975. ("ps.gz" . "gv %s")
  976. ("eps.gz" . "gv %s")
  977. ("dvi" . "xdvi %s")
  978. ("fig" . "xfig %s"))
  979. "Default file applications on a MacOS X system.
  980. The system \"open\" is known as a default, but we use X11 applications
  981. for some files for which the OS does not have a good default.
  982. See `org-file-apps'.")
  983. (defconst org-file-apps-defaults-windowsnt
  984. (list
  985. '(remote . emacs)
  986. (cons t
  987. (list (if (featurep 'xemacs)
  988. 'mswindows-shell-execute
  989. 'w32-shell-execute)
  990. "open" 'file)))
  991. "Default file applications on a Windows NT system.
  992. The system \"open\" is used for most files.
  993. See `org-file-apps'.")
  994. (defcustom org-file-apps
  995. '(
  996. ("txt" . emacs)
  997. ("tex" . emacs)
  998. ("ltx" . emacs)
  999. ("org" . emacs)
  1000. ("el" . emacs)
  1001. ("bib" . emacs)
  1002. (auto-mode . emacs)
  1003. )
  1004. "External applications for opening `file:path' items in a document.
  1005. Org-mode uses system defaults for different file types, but
  1006. you can use this variable to set the application for a given file
  1007. extension. The entries in this list are cons cells where the car identifies
  1008. files and the cdr the corresponding command. Possible values for the
  1009. file identifier are
  1010. \"ext\" A string identifying an extension
  1011. `directory' Matches a directory
  1012. `remote' Matches a remote file, accessible through tramp or efs.
  1013. Remote files most likely should be visited through Emacs
  1014. because external applications cannot handle such paths.
  1015. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1016. so all files Emacs knows how to handle. Useing this with
  1017. command `emacs' will open most files in Emacs.
  1018. t Default for files not matched by any of the other options.
  1019. Possible values for the command are:
  1020. `emacs' The file will be visited by the current Emacs process.
  1021. `default' Use the default application for this file type.
  1022. This can be used to overrule an unwanted seting in the
  1023. system-specific variable.
  1024. string A command to be executed by a shell; %s will be replaced
  1025. by the path to the file.
  1026. sexp A Lisp form which will be evaluated. The file path will
  1027. be available in the Lisp variable `file'.
  1028. For more examples, see the system specific constants
  1029. `org-file-apps-defaults-macosx'
  1030. `org-file-apps-defaults-windowsnt'
  1031. `org-file-apps-defaults-gnu'."
  1032. :group 'org-link-follow
  1033. :type '(repeat
  1034. (cons (choice :value ""
  1035. (string :tag "Extension")
  1036. (const :tag "Default for unrecognized files" t)
  1037. (const :tag "Remote file" remote)
  1038. (const :tag "Links to a directory" directory)
  1039. (const :tag "Any files that have Emacs modes"
  1040. auto-mode))
  1041. (choice :value ""
  1042. (const :tag "Visit with Emacs" emacs)
  1043. (const :tag "Use system default" default)
  1044. (string :tag "Command")
  1045. (sexp :tag "Lisp form")))))
  1046. (defgroup org-refile nil
  1047. "Options concerning refiling entries in Org-mode."
  1048. :tag "Org Remember"
  1049. :group 'org)
  1050. (defcustom org-directory "~/org"
  1051. "Directory with org files.
  1052. This directory will be used as default to prompt for org files.
  1053. Used by the hooks for remember.el."
  1054. :group 'org-refile
  1055. :group 'org-remember
  1056. :type 'directory)
  1057. (defcustom org-default-notes-file "~/.notes"
  1058. "Default target for storing notes.
  1059. Used by the hooks for remember.el. This can be a string, or nil to mean
  1060. the value of `remember-data-file'.
  1061. You can set this on a per-template basis with the variable
  1062. `org-remember-templates'."
  1063. :group 'org-refile
  1064. :group 'org-remember
  1065. :type '(choice
  1066. (const :tag "Default from remember-data-file" nil)
  1067. file))
  1068. (defcustom org-goto-interface 'outline
  1069. "The default interface to be used for `org-goto'.
  1070. Allowed vaues are:
  1071. outline The interface shows an outline of the relevant file
  1072. and the correct heading is found by moving through
  1073. the outline or by searching with incremental search.
  1074. outline-path-completion Headlines in the current buffer are offered via
  1075. completion."
  1076. :group 'org-refile
  1077. :type '(choice
  1078. (const :tag "Outline" outline)
  1079. (const :tag "Outline-path-completion" outline-path-completion)))
  1080. (defcustom org-reverse-note-order nil
  1081. "Non-nil means, store new notes at the beginning of a file or entry.
  1082. When nil, new notes will be filed to the end of a file or entry.
  1083. This can also be a list with cons cells of regular expressions that
  1084. are matched against file names, and values."
  1085. :group 'org-remember
  1086. :type '(choice
  1087. (const :tag "Reverse always" t)
  1088. (const :tag "Reverse never" nil)
  1089. (repeat :tag "By file name regexp"
  1090. (cons regexp boolean))))
  1091. (defcustom org-refile-targets nil
  1092. "Targets for refiling entries with \\[org-refile].
  1093. This is list of cons cells. Each cell contains:
  1094. - a specification of the files to be considered, either a list of files,
  1095. or a symbol whose function or variable value will be used to retrieve
  1096. a file name or a list of file names. Nil means, refile to a different
  1097. heading in the current buffer.
  1098. - A specification of how to find candidate refile targets. This may be
  1099. any of
  1100. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1101. This tag has to be present in all target headlines, inheritance will
  1102. not be considered.
  1103. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1104. todo keyword.
  1105. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1106. headlines that are refiling targets.
  1107. - a cons cell (:level . N). Any headline of level N is considered a target.
  1108. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1109. When this variable is nil, all top-level headlines in the current buffer
  1110. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1111. :group 'org-remember
  1112. :type '(repeat
  1113. (cons
  1114. (choice :value org-agenda-files
  1115. (const :tag "All agenda files" org-agenda-files)
  1116. (const :tag "Current buffer" nil)
  1117. (function) (variable) (file))
  1118. (choice :tag "Identify target headline by"
  1119. (cons :tag "Specific tag" (const :tag) (string))
  1120. (cons :tag "TODO keyword" (const :todo) (string))
  1121. (cons :tag "Regular expression" (const :regexp) (regexp))
  1122. (cons :tag "Level number" (const :level) (integer))
  1123. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1124. (defcustom org-refile-use-outline-path nil
  1125. "Non-nil means, provide refile targets as paths.
  1126. So a level 3 headline will be available as level1/level2/level3.
  1127. When the value is `file', also include the file name (without directory)
  1128. into the path. When `full-file-path', include the full file path."
  1129. :group 'org-remember
  1130. :type '(choice
  1131. (const :tag "Not" nil)
  1132. (const :tag "Yes" t)
  1133. (const :tag "Start with file name" file)
  1134. (const :tag "Start with full file path" full-file-path)))
  1135. (defgroup org-todo nil
  1136. "Options concerning TODO items in Org-mode."
  1137. :tag "Org TODO"
  1138. :group 'org)
  1139. (defgroup org-progress nil
  1140. "Options concerning Progress logging in Org-mode."
  1141. :tag "Org Progress"
  1142. :group 'org-time)
  1143. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1144. "List of TODO entry keyword sequences and their interpretation.
  1145. \\<org-mode-map>This is a list of sequences.
  1146. Each sequence starts with a symbol, either `sequence' or `type',
  1147. indicating if the keywords should be interpreted as a sequence of
  1148. action steps, or as different types of TODO items. The first
  1149. keywords are states requiring action - these states will select a headline
  1150. for inclusion into the global TODO list Org-mode produces. If one of
  1151. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1152. signify that no further action is necessary. If \"|\" is not found,
  1153. the last keyword is treated as the only DONE state of the sequence.
  1154. The command \\[org-todo] cycles an entry through these states, and one
  1155. additional state where no keyword is present. For details about this
  1156. cycling, see the manual.
  1157. TODO keywords and interpretation can also be set on a per-file basis with
  1158. the special #+SEQ_TODO and #+TYP_TODO lines.
  1159. Each keyword can optionally specify a character for fast state selection
  1160. \(in combination with the variable `org-use-fast-todo-selection')
  1161. and specifiers for state change logging, using the same syntax
  1162. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1163. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1164. indicates to record a time stamp each time this state is selected.
  1165. Each keyword may also specify if a timestamp or a note should be
  1166. recorded when entering or leaving the state, by adding additional
  1167. characters in the parenthesis after the keyword. This looks like this:
  1168. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1169. record only the time of the state change. With X and Y being either
  1170. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1171. Y when leaving the state if and only if the *target* state does not
  1172. define X. You may omit any of the fast-selection key or X or /Y,
  1173. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1174. For backward compatibility, this variable may also be just a list
  1175. of keywords - in this case the interptetation (sequence or type) will be
  1176. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1177. :group 'org-todo
  1178. :group 'org-keywords
  1179. :type '(choice
  1180. (repeat :tag "Old syntax, just keywords"
  1181. (string :tag "Keyword"))
  1182. (repeat :tag "New syntax"
  1183. (cons
  1184. (choice
  1185. :tag "Interpretation"
  1186. (const :tag "Sequence (cycling hits every state)" sequence)
  1187. (const :tag "Type (cycling directly to DONE)" type))
  1188. (repeat
  1189. (string :tag "Keyword"))))))
  1190. (defvar org-todo-keywords-1 nil
  1191. "All TODO and DONE keywords active in a buffer.")
  1192. (make-variable-buffer-local 'org-todo-keywords-1)
  1193. (defvar org-todo-keywords-for-agenda nil)
  1194. (defvar org-done-keywords-for-agenda nil)
  1195. (defvar org-todo-keyword-alist-for-agenda nil)
  1196. (defvar org-tag-alist-for-agenda nil)
  1197. (defvar org-agenda-contributing-files nil)
  1198. (defvar org-not-done-keywords nil)
  1199. (make-variable-buffer-local 'org-not-done-keywords)
  1200. (defvar org-done-keywords nil)
  1201. (make-variable-buffer-local 'org-done-keywords)
  1202. (defvar org-todo-heads nil)
  1203. (make-variable-buffer-local 'org-todo-heads)
  1204. (defvar org-todo-sets nil)
  1205. (make-variable-buffer-local 'org-todo-sets)
  1206. (defvar org-todo-log-states nil)
  1207. (make-variable-buffer-local 'org-todo-log-states)
  1208. (defvar org-todo-kwd-alist nil)
  1209. (make-variable-buffer-local 'org-todo-kwd-alist)
  1210. (defvar org-todo-key-alist nil)
  1211. (make-variable-buffer-local 'org-todo-key-alist)
  1212. (defvar org-todo-key-trigger nil)
  1213. (make-variable-buffer-local 'org-todo-key-trigger)
  1214. (defcustom org-todo-interpretation 'sequence
  1215. "Controls how TODO keywords are interpreted.
  1216. This variable is in principle obsolete and is only used for
  1217. backward compatibility, if the interpretation of todo keywords is
  1218. not given already in `org-todo-keywords'. See that variable for
  1219. more information."
  1220. :group 'org-todo
  1221. :group 'org-keywords
  1222. :type '(choice (const sequence)
  1223. (const type)))
  1224. (defcustom org-use-fast-todo-selection 'prefix
  1225. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1226. This variable describes if and under what circumstances the cycling
  1227. mechanism for TODO keywords will be replaced by a single-key, direct
  1228. selection scheme.
  1229. When nil, fast selection is never used.
  1230. When the symbol `prefix', it will be used when `org-todo' is called with
  1231. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1232. in an agenda buffer.
  1233. When t, fast selection is used by default. In this case, the prefix
  1234. argument forces cycling instead.
  1235. In all cases, the special interface is only used if access keys have actually
  1236. been assigned by the user, i.e. if keywords in the configuration are followed
  1237. by a letter in parenthesis, like TODO(t)."
  1238. :group 'org-todo
  1239. :type '(choice
  1240. (const :tag "Never" nil)
  1241. (const :tag "By default" t)
  1242. (const :tag "Only with C-u C-c C-t" prefix)))
  1243. (defcustom org-provide-todo-statistics t
  1244. "Non-nil means, update todo statistics after insert and toggle.
  1245. When this is set, todo statistics is updated in the parent of the current
  1246. entry each time a todo state is changed."
  1247. :group 'org-todo
  1248. :type 'boolean)
  1249. (defcustom org-after-todo-state-change-hook nil
  1250. "Hook which is run after the state of a TODO item was changed.
  1251. The new state (a string with a TODO keyword, or nil) is available in the
  1252. Lisp variable `state'."
  1253. :group 'org-todo
  1254. :type 'hook)
  1255. (defcustom org-log-done nil
  1256. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1257. When equal to the list (done), also prompt for a closing note.
  1258. This can also be configured on a per-file basis by adding one of
  1259. the following lines anywhere in the buffer:
  1260. #+STARTUP: logdone
  1261. #+STARTUP: lognotedone
  1262. #+STARTUP: nologdone"
  1263. :group 'org-todo
  1264. :group 'org-progress
  1265. :type '(choice
  1266. (const :tag "No logging" nil)
  1267. (const :tag "Record CLOSED timestamp" time)
  1268. (const :tag "Record CLOSED timestamp with closing note." note)))
  1269. ;; Normalize old uses of org-log-done.
  1270. (cond
  1271. ((eq org-log-done t) (setq org-log-done 'time))
  1272. ((and (listp org-log-done) (memq 'done org-log-done))
  1273. (setq org-log-done 'note)))
  1274. (defcustom org-log-note-clock-out nil
  1275. "Non-nil means, record a note when clocking out of an item.
  1276. This can also be configured on a per-file basis by adding one of
  1277. the following lines anywhere in the buffer:
  1278. #+STARTUP: lognoteclock-out
  1279. #+STARTUP: nolognoteclock-out"
  1280. :group 'org-todo
  1281. :group 'org-progress
  1282. :type 'boolean)
  1283. (defcustom org-log-done-with-time t
  1284. "Non-nil means, the CLOSED time stamp will contain date and time.
  1285. When nil, only the date will be recorded."
  1286. :group 'org-progress
  1287. :type 'boolean)
  1288. (defcustom org-log-note-headings
  1289. '((done . "CLOSING NOTE %t")
  1290. (state . "State %-12s %t")
  1291. (note . "Note taken on %t")
  1292. (clock-out . ""))
  1293. "Headings for notes added to entries.
  1294. The value is an alist, with the car being a symbol indicating the note
  1295. context, and the cdr is the heading to be used. The heading may also be the
  1296. empty string.
  1297. %t in the heading will be replaced by a time stamp.
  1298. %s will be replaced by the new TODO state, in double quotes.
  1299. %u will be replaced by the user name.
  1300. %U will be replaced by the full user name."
  1301. :group 'org-todo
  1302. :group 'org-progress
  1303. :type '(list :greedy t
  1304. (cons (const :tag "Heading when closing an item" done) string)
  1305. (cons (const :tag
  1306. "Heading when changing todo state (todo sequence only)"
  1307. state) string)
  1308. (cons (const :tag "Heading when just taking a note" note) string)
  1309. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1310. (unless (assq 'note org-log-note-headings)
  1311. (push '(note . "%t") org-log-note-headings))
  1312. (defcustom org-log-states-order-reversed t
  1313. "Non-nil means, the latest state change note will be directly after heading.
  1314. When nil, the notes will be orderer according to time."
  1315. :group 'org-todo
  1316. :group 'org-progress
  1317. :type 'boolean)
  1318. (defcustom org-log-repeat 'time
  1319. "Non-nil means, record moving through the DONE state when triggering repeat.
  1320. An auto-repeating tasks is immediately switched back to TODO when marked
  1321. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1322. the TODO keyword definition, or recording a closing note by setting
  1323. `org-log-done', there will be no record of the task moving through DONE.
  1324. This variable forces taking a note anyway. Possible values are:
  1325. nil Don't force a record
  1326. time Record a time stamp
  1327. note Record a note
  1328. This option can also be set with on a per-file-basis with
  1329. #+STARTUP: logrepeat
  1330. #+STARTUP: lognoterepeat
  1331. #+STARTUP: nologrepeat
  1332. You can have local logging settings for a subtree by setting the LOGGING
  1333. property to one or more of these keywords."
  1334. :group 'org-todo
  1335. :group 'org-progress
  1336. :type '(choice
  1337. (const :tag "Don't force a record" nil)
  1338. (const :tag "Force recording the DONE state" time)
  1339. (const :tag "Force recording a note with the DONE state" note)))
  1340. (defgroup org-priorities nil
  1341. "Priorities in Org-mode."
  1342. :tag "Org Priorities"
  1343. :group 'org-todo)
  1344. (defcustom org-highest-priority ?A
  1345. "The highest priority of TODO items. A character like ?A, ?B etc.
  1346. Must have a smaller ASCII number than `org-lowest-priority'."
  1347. :group 'org-priorities
  1348. :type 'character)
  1349. (defcustom org-lowest-priority ?C
  1350. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1351. Must have a larger ASCII number than `org-highest-priority'."
  1352. :group 'org-priorities
  1353. :type 'character)
  1354. (defcustom org-default-priority ?B
  1355. "The default priority of TODO items.
  1356. This is the priority an item get if no explicit priority is given."
  1357. :group 'org-priorities
  1358. :type 'character)
  1359. (defcustom org-priority-start-cycle-with-default t
  1360. "Non-nil means, start with default priority when starting to cycle.
  1361. When this is nil, the first step in the cycle will be (depending on the
  1362. command used) one higher or lower that the default priority."
  1363. :group 'org-priorities
  1364. :type 'boolean)
  1365. (defgroup org-time nil
  1366. "Options concerning time stamps and deadlines in Org-mode."
  1367. :tag "Org Time"
  1368. :group 'org)
  1369. (defcustom org-insert-labeled-timestamps-at-point nil
  1370. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1371. When nil, these labeled time stamps are forces into the second line of an
  1372. entry, just after the headline. When scheduling from the global TODO list,
  1373. the time stamp will always be forced into the second line."
  1374. :group 'org-time
  1375. :type 'boolean)
  1376. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1377. "Formats for `format-time-string' which are used for time stamps.
  1378. It is not recommended to change this constant.")
  1379. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1380. "Number of minutes to round time stamps to.
  1381. These are two values, the first applies when first creating a time stamp.
  1382. The second applies when changing it with the commands `S-up' and `S-down'.
  1383. When changing the time stamp, this means that it will change in steps
  1384. of N minutes, as given by the second value.
  1385. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1386. numbers should be factors of 60, so for example 5, 10, 15.
  1387. When this is larger than 1, you can still force an exact time-stamp by using
  1388. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1389. and by using a prefix arg to `S-up/down' to specify the exact number
  1390. of minutes to shift."
  1391. :group 'org-time
  1392. :get '(lambda (var) ; Make sure all entries have 5 elements
  1393. (if (integerp (default-value var))
  1394. (list (default-value var) 5)
  1395. (default-value var)))
  1396. :type '(list
  1397. (integer :tag "when inserting times")
  1398. (integer :tag "when modifying times")))
  1399. ;; Normalize old customizations of this variable.
  1400. (when (integerp org-time-stamp-rounding-minutes)
  1401. (setq org-time-stamp-rounding-minutes
  1402. (list org-time-stamp-rounding-minutes
  1403. org-time-stamp-rounding-minutes)))
  1404. (defcustom org-display-custom-times nil
  1405. "Non-nil means, overlay custom formats over all time stamps.
  1406. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1407. To turn this on on a per-file basis, insert anywhere in the file:
  1408. #+STARTUP: customtime"
  1409. :group 'org-time
  1410. :set 'set-default
  1411. :type 'sexp)
  1412. (make-variable-buffer-local 'org-display-custom-times)
  1413. (defcustom org-time-stamp-custom-formats
  1414. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1415. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1416. These are overlayed over the default ISO format if the variable
  1417. `org-display-custom-times' is set. Time like %H:%M should be at the
  1418. end of the second format."
  1419. :group 'org-time
  1420. :type 'sexp)
  1421. (defun org-time-stamp-format (&optional long inactive)
  1422. "Get the right format for a time string."
  1423. (let ((f (if long (cdr org-time-stamp-formats)
  1424. (car org-time-stamp-formats))))
  1425. (if inactive
  1426. (concat "[" (substring f 1 -1) "]")
  1427. f)))
  1428. (defcustom org-time-clocksum-format "%d:%02d"
  1429. "The format string used when creating CLOCKSUM lines, or when
  1430. org-mode generates a time duration."
  1431. :group 'org-time
  1432. :type 'string)
  1433. (defcustom org-deadline-warning-days 14
  1434. "No. of days before expiration during which a deadline becomes active.
  1435. This variable governs the display in sparse trees and in the agenda.
  1436. When 0 or negative, it means use this number (the absolute value of it)
  1437. even if a deadline has a different individual lead time specified."
  1438. :group 'org-time
  1439. :group 'org-agenda-daily/weekly
  1440. :type 'number)
  1441. (defcustom org-read-date-prefer-future t
  1442. "Non-nil means, assume future for incomplete date input from user.
  1443. This affects the following situations:
  1444. 1. The user gives a day, but no month.
  1445. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1446. read this as the third of *next* month. However, if you enter \"17\",
  1447. it will be considered as *this* month.
  1448. 2. The user gives a month but not a year.
  1449. For example, if it is april and you enter \"feb 2\", this will be read
  1450. as feb 2, *next* year. \"May 5\", however, will be this year.
  1451. Currently this does not work for ISO week specifications.
  1452. When this option is nil, the current month and year will always be used
  1453. as defaults."
  1454. :group 'org-time
  1455. :type 'boolean)
  1456. (defcustom org-read-date-display-live t
  1457. "Non-nil means, display current interpretation of date prompt live.
  1458. This display will be in an overlay, in the minibuffer."
  1459. :group 'org-time
  1460. :type 'boolean)
  1461. (defcustom org-read-date-popup-calendar t
  1462. "Non-nil means, pop up a calendar when prompting for a date.
  1463. In the calendar, the date can be selected with mouse-1. However, the
  1464. minibuffer will also be active, and you can simply enter the date as well.
  1465. When nil, only the minibuffer will be available."
  1466. :group 'org-time
  1467. :type 'boolean)
  1468. (if (fboundp 'defvaralias)
  1469. (defvaralias 'org-popup-calendar-for-date-prompt
  1470. 'org-read-date-popup-calendar))
  1471. (defcustom org-extend-today-until 0
  1472. "The hour when your day really ends. Must be an integer.
  1473. This has influence for the following applications:
  1474. - When switching the agenda to \"today\". It it is still earlier than
  1475. the time given here, the day recognized as TODAY is actually yesterday.
  1476. - When a date is read from the user and it is still before the time given
  1477. here, the current date and time will be assumed to be yesterday, 23:59.
  1478. Also, timestamps inserted in remember templates follow this rule.
  1479. IMPORTANT: This is a feature whose implementation is and likely will
  1480. remain incomplete. Really, it is only here because past midnight seems to
  1481. ne the favorite working time of John Wiegley :-)"
  1482. :group 'org-time
  1483. :type 'number)
  1484. (defcustom org-edit-timestamp-down-means-later nil
  1485. "Non-nil means, S-down will increase the time in a time stamp.
  1486. When nil, S-up will increase."
  1487. :group 'org-time
  1488. :type 'boolean)
  1489. (defcustom org-calendar-follow-timestamp-change t
  1490. "Non-nil means, make the calendar window follow timestamp changes.
  1491. When a timestamp is modified and the calendar window is visible, it will be
  1492. moved to the new date."
  1493. :group 'org-time
  1494. :type 'boolean)
  1495. (defgroup org-tags nil
  1496. "Options concerning tags in Org-mode."
  1497. :tag "Org Tags"
  1498. :group 'org)
  1499. (defcustom org-tag-alist nil
  1500. "List of tags allowed in Org-mode files.
  1501. When this list is nil, Org-mode will base TAG input on what is already in the
  1502. buffer.
  1503. The value of this variable is an alist, the car of each entry must be a
  1504. keyword as a string, the cdr may be a character that is used to select
  1505. that tag through the fast-tag-selection interface.
  1506. See the manual for details."
  1507. :group 'org-tags
  1508. :type '(repeat
  1509. (choice
  1510. (cons (string :tag "Tag name")
  1511. (character :tag "Access char"))
  1512. (const :tag "Start radio group" (:startgroup))
  1513. (const :tag "End radio group" (:endgroup)))))
  1514. (defvar org-file-tags nil
  1515. "List of tags that can be inherited by all entries in the file.
  1516. The tags will be inherited if the variable `org-use-tag-inheritance'
  1517. says they should be.
  1518. This variable is populated from #+TAG lines.")
  1519. (defcustom org-use-fast-tag-selection 'auto
  1520. "Non-nil means, use fast tag selection scheme.
  1521. This is a special interface to select and deselect tags with single keys.
  1522. When nil, fast selection is never used.
  1523. When the symbol `auto', fast selection is used if and only if selection
  1524. characters for tags have been configured, either through the variable
  1525. `org-tag-alist' or through a #+TAGS line in the buffer.
  1526. When t, fast selection is always used and selection keys are assigned
  1527. automatically if necessary."
  1528. :group 'org-tags
  1529. :type '(choice
  1530. (const :tag "Always" t)
  1531. (const :tag "Never" nil)
  1532. (const :tag "When selection characters are configured" 'auto)))
  1533. (defcustom org-fast-tag-selection-single-key nil
  1534. "Non-nil means, fast tag selection exits after first change.
  1535. When nil, you have to press RET to exit it.
  1536. During fast tag selection, you can toggle this flag with `C-c'.
  1537. This variable can also have the value `expert'. In this case, the window
  1538. displaying the tags menu is not even shown, until you press C-c again."
  1539. :group 'org-tags
  1540. :type '(choice
  1541. (const :tag "No" nil)
  1542. (const :tag "Yes" t)
  1543. (const :tag "Expert" expert)))
  1544. (defvar org-fast-tag-selection-include-todo nil
  1545. "Non-nil means, fast tags selection interface will also offer TODO states.
  1546. This is an undocumented feature, you should not rely on it.")
  1547. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1548. "The column to which tags should be indented in a headline.
  1549. If this number is positive, it specifies the column. If it is negative,
  1550. it means that the tags should be flushright to that column. For example,
  1551. -80 works well for a normal 80 character screen."
  1552. :group 'org-tags
  1553. :type 'integer)
  1554. (defcustom org-auto-align-tags t
  1555. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1556. These operations change the length of a headline and therefore shift
  1557. the tags around. With this options turned on, after each such operation
  1558. the tags are again aligned to `org-tags-column'."
  1559. :group 'org-tags
  1560. :type 'boolean)
  1561. (defcustom org-use-tag-inheritance t
  1562. "Non-nil means, tags in levels apply also for sublevels.
  1563. When nil, only the tags directly given in a specific line apply there.
  1564. If this option is t, a match early-on in a tree can lead to a large
  1565. number of matches in the subtree. If you only want to see the first
  1566. match in a tree during a search, check out the variable
  1567. `org-tags-match-list-sublevels'.
  1568. This may also be a list of tags that should be inherited, or a regexp that
  1569. matches tags that should be inherited."
  1570. :group 'org-tags
  1571. :type '(choice
  1572. (const :tag "Not" nil)
  1573. (const :tag "Always" t)
  1574. (repeat :tag "Specific tags" (string :tag "Tag"))
  1575. (regexp :tag "Tags matched by regexp")))
  1576. (defun org-tag-inherit-p (tag)
  1577. "Check if TAG is one that should be inherited."
  1578. (cond
  1579. ((eq org-use-tag-inheritance t) t)
  1580. ((not org-use-tag-inheritance) nil)
  1581. ((stringp org-use-tag-inheritance)
  1582. (string-match org-use-tag-inheritance tag))
  1583. ((listp org-use-tag-inheritance)
  1584. (member tag org-use-tag-inheritance))
  1585. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1586. (defcustom org-tags-match-list-sublevels t
  1587. "Non-nil means list also sublevels of headlines matching tag search.
  1588. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1589. the sublevels of a headline matching a tag search often also match
  1590. the same search. Listing all of them can create very long lists.
  1591. Setting this variable to nil causes subtrees of a match to be skipped.
  1592. This option is off by default, because inheritance in on. If you turn
  1593. inheritance off, you very likely want to turn this option on.
  1594. As a special case, if the tag search is restricted to TODO items, the
  1595. value of this variable is ignored and sublevels are always checked, to
  1596. make sure all corresponding TODO items find their way into the list."
  1597. :group 'org-tags
  1598. :type 'boolean)
  1599. (defvar org-tags-history nil
  1600. "History of minibuffer reads for tags.")
  1601. (defvar org-last-tags-completion-table nil
  1602. "The last used completion table for tags.")
  1603. (defvar org-after-tags-change-hook nil
  1604. "Hook that is run after the tags in a line have changed.")
  1605. (defgroup org-properties nil
  1606. "Options concerning properties in Org-mode."
  1607. :tag "Org Properties"
  1608. :group 'org)
  1609. (defcustom org-property-format "%-10s %s"
  1610. "How property key/value pairs should be formatted by `indent-line'.
  1611. When `indent-line' hits a property definition, it will format the line
  1612. according to this format, mainly to make sure that the values are
  1613. lined-up with respect to each other."
  1614. :group 'org-properties
  1615. :type 'string)
  1616. (defcustom org-use-property-inheritance nil
  1617. "Non-nil means, properties apply also for sublevels.
  1618. This setting is chiefly used during property searches. Turning it on can
  1619. cause significant overhead when doing a search, which is why it is not
  1620. on by default.
  1621. When nil, only the properties directly given in the current entry count.
  1622. When t, every property is inherited. The value may also be a list of
  1623. properties that should have inheritance, or a regular expression matching
  1624. properties that should be inherited.
  1625. However, note that some special properties use inheritance under special
  1626. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1627. and the properties ending in \"_ALL\" when they are used as descriptor
  1628. for valid values of a property.
  1629. Note for programmers:
  1630. When querying an entry with `org-entry-get', you can control if inheritance
  1631. should be used. By default, `org-entry-get' looks only at the local
  1632. properties. You can request inheritance by setting the inherit argument
  1633. to t (to force inheritance) or to `selective' (to respect the setting
  1634. in this variable)."
  1635. :group 'org-properties
  1636. :type '(choice
  1637. (const :tag "Not" nil)
  1638. (const :tag "Always" t)
  1639. (repeat :tag "Specific properties" (string :tag "Property"))
  1640. (regexp :tag "Properties matched by regexp")))
  1641. (defun org-property-inherit-p (property)
  1642. "Check if PROPERTY is one that should be inherited."
  1643. (cond
  1644. ((eq org-use-property-inheritance t) t)
  1645. ((not org-use-property-inheritance) nil)
  1646. ((stringp org-use-property-inheritance)
  1647. (string-match org-use-property-inheritance property))
  1648. ((listp org-use-property-inheritance)
  1649. (member property org-use-property-inheritance))
  1650. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1651. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1652. "The default column format, if no other format has been defined.
  1653. This variable can be set on the per-file basis by inserting a line
  1654. #+COLUMNS: %25ITEM ....."
  1655. :group 'org-properties
  1656. :type 'string)
  1657. (defcustom org-columns-ellipses ".."
  1658. "The ellipses to be used when a field in column view is truncated.
  1659. When this is the empty string, as many characters as possible are shown,
  1660. but then there will be no visual indication that the field has been truncated.
  1661. When this is a string of length N, the last N characters of a truncated
  1662. field are replaced by this string. If the column is narrower than the
  1663. ellipses string, only part of the ellipses string will be shown."
  1664. :group 'org-properties
  1665. :type 'string)
  1666. (defcustom org-columns-modify-value-for-display-function nil
  1667. "Function that modifies values for display in column view.
  1668. For example, it can be used to cut out a certain part from a time stamp.
  1669. The function must take 2 argments:
  1670. column-title The tite of the column (*not* the property name)
  1671. value The value that should be modified.
  1672. The function should return the value that should be displayed,
  1673. or nil if the normal value should be used."
  1674. :group 'org-properties
  1675. :type 'function)
  1676. (defcustom org-effort-property "Effort"
  1677. "The property that is being used to keep track of effort estimates.
  1678. Effort estimates given in this property need to have the format H:MM."
  1679. :group 'org-properties
  1680. :group 'org-progress
  1681. :type '(string :tag "Property"))
  1682. (defconst org-global-properties-fixed
  1683. '(("VISIBILITY_ALL" . "folded children content all"))
  1684. "List of property/value pairs that can be inherited by any entry.
  1685. These are fixed values, for the preset properties.")
  1686. (defcustom org-global-properties nil
  1687. "List of property/value pairs that can be inherited by any entry.
  1688. You can set buffer-local values for this by adding lines like
  1689. #+PROPERTY: NAME VALUE"
  1690. :group 'org-properties
  1691. :type '(repeat
  1692. (cons (string :tag "Property")
  1693. (string :tag "Value"))))
  1694. (defvar org-file-properties nil
  1695. "List of property/value pairs that can be inherited by any entry.
  1696. Valid for the current buffer.
  1697. This variable is populated from #+PROPERTY lines.")
  1698. (make-variable-buffer-local 'org-file-properties)
  1699. (defgroup org-agenda nil
  1700. "Options concerning agenda views in Org-mode."
  1701. :tag "Org Agenda"
  1702. :group 'org)
  1703. (defvar org-category nil
  1704. "Variable used by org files to set a category for agenda display.
  1705. Such files should use a file variable to set it, for example
  1706. # -*- mode: org; org-category: \"ELisp\"
  1707. or contain a special line
  1708. #+CATEGORY: ELisp
  1709. If the file does not specify a category, then file's base name
  1710. is used instead.")
  1711. (make-variable-buffer-local 'org-category)
  1712. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1713. (defcustom org-agenda-files nil
  1714. "The files to be used for agenda display.
  1715. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1716. \\[org-remove-file]. You can also use customize to edit the list.
  1717. If an entry is a directory, all files in that directory that are matched by
  1718. `org-agenda-file-regexp' will be part of the file list.
  1719. If the value of the variable is not a list but a single file name, then
  1720. the list of agenda files is actually stored and maintained in that file, one
  1721. agenda file per line."
  1722. :group 'org-agenda
  1723. :type '(choice
  1724. (repeat :tag "List of files and directories" file)
  1725. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1726. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1727. "Regular expression to match files for `org-agenda-files'.
  1728. If any element in the list in that variable contains a directory instead
  1729. of a normal file, all files in that directory that are matched by this
  1730. regular expression will be included."
  1731. :group 'org-agenda
  1732. :type 'regexp)
  1733. (defcustom org-agenda-text-search-extra-files nil
  1734. "List of extra files to be searched by text search commands.
  1735. These files will be search in addition to the agenda files by the
  1736. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1737. Note that these files will only be searched for text search commands,
  1738. not for the other agenda views like todo lists, tag searches or the weekly
  1739. agenda. This variable is intended to list notes and possibly archive files
  1740. that should also be searched by these two commands.
  1741. In fact, if the first element in the list is the symbol `agenda-archives',
  1742. than all archive files of all agenda files will be added to the search
  1743. scope."
  1744. :group 'org-agenda
  1745. :type '(set :greedy t
  1746. (const :tag "Agenda Archives" agenda-archives)
  1747. (repeat :inline t (file))))
  1748. (if (fboundp 'defvaralias)
  1749. (defvaralias 'org-agenda-multi-occur-extra-files
  1750. 'org-agenda-text-search-extra-files))
  1751. (defcustom org-agenda-skip-unavailable-files nil
  1752. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1753. A nil value means to remove them, after a query, from the list."
  1754. :group 'org-agenda
  1755. :type 'boolean)
  1756. (defcustom org-calendar-to-agenda-key [?c]
  1757. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1758. The command `org-calendar-goto-agenda' will be bound to this key. The
  1759. default is the character `c' because then `c' can be used to switch back and
  1760. forth between agenda and calendar."
  1761. :group 'org-agenda
  1762. :type 'sexp)
  1763. (defcustom org-calendar-agenda-action-key [?k]
  1764. "The key to be installed in `calendar-mode-map' for agenda-action.
  1765. The command `org-agenda-action' will be bound to this key. The
  1766. default is the character `k' because we use the same key in the agenda."
  1767. :group 'org-agenda
  1768. :type 'sexp)
  1769. (eval-after-load "calendar"
  1770. '(progn
  1771. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1772. 'org-calendar-goto-agenda)
  1773. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1774. 'org-agenda-action)))
  1775. (defgroup org-latex nil
  1776. "Options for embedding LaTeX code into Org-mode."
  1777. :tag "Org LaTeX"
  1778. :group 'org)
  1779. (defcustom org-format-latex-options
  1780. '(:foreground default :background default :scale 1.0
  1781. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1782. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1783. "Options for creating images from LaTeX fragments.
  1784. This is a property list with the following properties:
  1785. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1786. `default' means use the foreground of the default face.
  1787. :background the background color, or \"Transparent\".
  1788. `default' means use the background of the default face.
  1789. :scale a scaling factor for the size of the images.
  1790. :html-foreground, :html-background, :html-scale
  1791. the same numbers for HTML export.
  1792. :matchers a list indicating which matchers should be used to
  1793. find LaTeX fragments. Valid members of this list are:
  1794. \"begin\" find environments
  1795. \"$\" find math expressions surrounded by $...$
  1796. \"$$\" find math expressions surrounded by $$....$$
  1797. \"\\(\" find math expressions surrounded by \\(...\\)
  1798. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1799. :group 'org-latex
  1800. :type 'plist)
  1801. (defcustom org-format-latex-header "\\documentclass{article}
  1802. \\usepackage{fullpage} % do not remove
  1803. \\usepackage{amssymb}
  1804. \\usepackage[usenames]{color}
  1805. \\usepackage{amsmath}
  1806. \\usepackage{latexsym}
  1807. \\usepackage[mathscr]{eucal}
  1808. \\pagestyle{empty} % do not remove"
  1809. "The document header used for processing LaTeX fragments."
  1810. :group 'org-latex
  1811. :type 'string)
  1812. (defgroup org-font-lock nil
  1813. "Font-lock settings for highlighting in Org-mode."
  1814. :tag "Org Font Lock"
  1815. :group 'org)
  1816. (defcustom org-level-color-stars-only nil
  1817. "Non-nil means fontify only the stars in each headline.
  1818. When nil, the entire headline is fontified.
  1819. Changing it requires restart of `font-lock-mode' to become effective
  1820. also in regions already fontified."
  1821. :group 'org-font-lock
  1822. :type 'boolean)
  1823. (defcustom org-hide-leading-stars nil
  1824. "Non-nil means, hide the first N-1 stars in a headline.
  1825. This works by using the face `org-hide' for these stars. This
  1826. face is white for a light background, and black for a dark
  1827. background. You may have to customize the face `org-hide' to
  1828. make this work.
  1829. Changing it requires restart of `font-lock-mode' to become effective
  1830. also in regions already fontified.
  1831. You may also set this on a per-file basis by adding one of the following
  1832. lines to the buffer:
  1833. #+STARTUP: hidestars
  1834. #+STARTUP: showstars"
  1835. :group 'org-font-lock
  1836. :type 'boolean)
  1837. (defcustom org-fontify-done-headline nil
  1838. "Non-nil means, change the face of a headline if it is marked DONE.
  1839. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1840. When this is non-nil, the headline after the keyword is set to the
  1841. `org-headline-done' as an additional indication."
  1842. :group 'org-font-lock
  1843. :type 'boolean)
  1844. (defcustom org-fontify-emphasized-text t
  1845. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1846. Changing this variable requires a restart of Emacs to take effect."
  1847. :group 'org-font-lock
  1848. :type 'boolean)
  1849. (defcustom org-highlight-latex-fragments-and-specials nil
  1850. "Non-nil means, fontify what is treated specially by the exporters."
  1851. :group 'org-font-lock
  1852. :type 'boolean)
  1853. (defcustom org-hide-emphasis-markers nil
  1854. "Non-nil mean font-lock should hide the emphasis marker characters."
  1855. :group 'org-font-lock
  1856. :type 'boolean)
  1857. (defvar org-emph-re nil
  1858. "Regular expression for matching emphasis.")
  1859. (defvar org-verbatim-re nil
  1860. "Regular expression for matching verbatim text.")
  1861. (defvar org-emphasis-regexp-components) ; defined just below
  1862. (defvar org-emphasis-alist) ; defined just below
  1863. (defun org-set-emph-re (var val)
  1864. "Set variable and compute the emphasis regular expression."
  1865. (set var val)
  1866. (when (and (boundp 'org-emphasis-alist)
  1867. (boundp 'org-emphasis-regexp-components)
  1868. org-emphasis-alist org-emphasis-regexp-components)
  1869. (let* ((e org-emphasis-regexp-components)
  1870. (pre (car e))
  1871. (post (nth 1 e))
  1872. (border (nth 2 e))
  1873. (body (nth 3 e))
  1874. (nl (nth 4 e))
  1875. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1876. (body1 (concat body "*?"))
  1877. (markers (mapconcat 'car org-emphasis-alist ""))
  1878. (vmarkers (mapconcat
  1879. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1880. org-emphasis-alist "")))
  1881. ;; make sure special characters appear at the right position in the class
  1882. (if (string-match "\\^" markers)
  1883. (setq markers (concat (replace-match "" t t markers) "^")))
  1884. (if (string-match "-" markers)
  1885. (setq markers (concat (replace-match "" t t markers) "-")))
  1886. (if (string-match "\\^" vmarkers)
  1887. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1888. (if (string-match "-" vmarkers)
  1889. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1890. (if (> nl 0)
  1891. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1892. (int-to-string nl) "\\}")))
  1893. ;; Make the regexp
  1894. (setq org-emph-re
  1895. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1896. "\\("
  1897. "\\([" markers "]\\)"
  1898. "\\("
  1899. "[^" border "]\\|"
  1900. "[^" border (if (and nil stacked) markers) "]"
  1901. body1
  1902. "[^" border (if (and nil stacked) markers) "]"
  1903. "\\)"
  1904. "\\3\\)"
  1905. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1906. (setq org-verbatim-re
  1907. (concat "\\([" pre "]\\|^\\)"
  1908. "\\("
  1909. "\\([" vmarkers "]\\)"
  1910. "\\("
  1911. "[^" border "]\\|"
  1912. "[^" border "]"
  1913. body1
  1914. "[^" border "]"
  1915. "\\)"
  1916. "\\3\\)"
  1917. "\\([" post "]\\|$\\)")))))
  1918. (defcustom org-emphasis-regexp-components
  1919. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1920. "Components used to build the regular expression for emphasis.
  1921. This is a list with 6 entries. Terminology: In an emphasis string
  1922. like \" *strong word* \", we call the initial space PREMATCH, the final
  1923. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1924. and \"trong wor\" is the body. The different components in this variable
  1925. specify what is allowed/forbidden in each part:
  1926. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1927. post Chars allowed as postmatch. End of line will be allowed too.
  1928. border The chars *forbidden* as border characters.
  1929. body-regexp A regexp like \".\" to match a body character. Don't use
  1930. non-shy groups here, and don't allow newline here.
  1931. newline The maximum number of newlines allowed in an emphasis exp.
  1932. Use customize to modify this, or restart Emacs after changing it."
  1933. :group 'org-font-lock
  1934. :set 'org-set-emph-re
  1935. :type '(list
  1936. (sexp :tag "Allowed chars in pre ")
  1937. (sexp :tag "Allowed chars in post ")
  1938. (sexp :tag "Forbidden chars in border ")
  1939. (sexp :tag "Regexp for body ")
  1940. (integer :tag "number of newlines allowed")
  1941. (option (boolean :tag "Please ignore this button"))))
  1942. (defcustom org-emphasis-alist
  1943. `(("*" bold "<b>" "</b>")
  1944. ("/" italic "<i>" "</i>")
  1945. ("_" underline "<u>" "</u>")
  1946. ("=" org-code "<code>" "</code>" verbatim)
  1947. ("~" org-verbatim "" "" verbatim)
  1948. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1949. "<del>" "</del>")
  1950. )
  1951. "Special syntax for emphasized text.
  1952. Text starting and ending with a special character will be emphasized, for
  1953. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1954. characters, the face to be used by font-lock for highlighting in Org-mode
  1955. Emacs buffers, and the HTML tags to be used for this.
  1956. Use customize to modify this, or restart Emacs after changing it."
  1957. :group 'org-font-lock
  1958. :set 'org-set-emph-re
  1959. :type '(repeat
  1960. (list
  1961. (string :tag "Marker character")
  1962. (choice
  1963. (face :tag "Font-lock-face")
  1964. (plist :tag "Face property list"))
  1965. (string :tag "HTML start tag")
  1966. (string :tag "HTML end tag")
  1967. (option (const verbatim)))))
  1968. ;;; Miscellaneous options
  1969. (defgroup org-completion nil
  1970. "Completion in Org-mode."
  1971. :tag "Org Completion"
  1972. :group 'org)
  1973. (defcustom org-completion-fallback-command 'hippie-expand
  1974. "The expansion command called by \\[org-complete] in normal context.
  1975. Normal means, no org-mode-specific context."
  1976. :group 'org-completion
  1977. :type 'function)
  1978. ;;; Functions and variables from ther packages
  1979. ;; Declared here to avoid compiler warnings
  1980. ;; XEmacs only
  1981. (defvar outline-mode-menu-heading)
  1982. (defvar outline-mode-menu-show)
  1983. (defvar outline-mode-menu-hide)
  1984. (defvar zmacs-regions) ; XEmacs regions
  1985. ;; Emacs only
  1986. (defvar mark-active)
  1987. ;; Various packages
  1988. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1989. (declare-function calendar-forward-day "cal-move" (arg))
  1990. (declare-function calendar-goto-date "cal-move" (date))
  1991. (declare-function calendar-goto-today "cal-move" ())
  1992. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1993. (defvar calc-embedded-close-formula)
  1994. (defvar calc-embedded-open-formula)
  1995. (declare-function cdlatex-tab "ext:cdlatex" ())
  1996. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1997. (defvar font-lock-unfontify-region-function)
  1998. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1999. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2000. (defvar iswitchb-temp-buflist)
  2001. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2002. (declare-function org-agenda-skip "org-agenda" ())
  2003. (declare-function org-format-agenda-item "org-agenda"
  2004. (extra txt &optional category tags dotime noprefix remove-re))
  2005. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2006. (declare-function org-agenda-change-all-lines "org-agenda"
  2007. (newhead hdmarker &optional fixface))
  2008. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2009. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2010. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2011. (beg end))
  2012. (declare-function parse-time-string "parse-time" (string))
  2013. (declare-function remember "remember" (&optional initial))
  2014. (declare-function remember-buffer-desc "remember" ())
  2015. (declare-function remember-finalize "remember" ())
  2016. (defvar remember-save-after-remembering)
  2017. (defvar remember-data-file)
  2018. (defvar remember-register)
  2019. (defvar remember-buffer)
  2020. (defvar remember-handler-functions)
  2021. (defvar remember-annotation-functions)
  2022. (defvar texmathp-why)
  2023. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2024. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2025. (defvar w3m-current-url)
  2026. (defvar w3m-current-title)
  2027. (defvar org-latex-regexps)
  2028. ;;; Autoload and prepare some org modules
  2029. ;; Some table stuff that needs to be defined here, because it is used
  2030. ;; by the functions setting up org-mode or checking for table context.
  2031. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2032. "Detects an org-type or table-type table.")
  2033. (defconst org-table-line-regexp "^[ \t]*|"
  2034. "Detects an org-type table line.")
  2035. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2036. "Detects an org-type table line.")
  2037. (defconst org-table-hline-regexp "^[ \t]*|-"
  2038. "Detects an org-type table hline.")
  2039. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2040. "Detects a table-type table hline.")
  2041. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2042. "Searching from within a table (any type) this finds the first line
  2043. outside the table.")
  2044. ;; Autoload the functions in org-table.el that are needed by functions here.
  2045. (eval-and-compile
  2046. (org-autoload "org-table"
  2047. '(org-table-align org-table-begin org-table-blank-field
  2048. org-table-convert org-table-convert-region org-table-copy-down
  2049. org-table-copy-region org-table-create
  2050. org-table-create-or-convert-from-region
  2051. org-table-create-with-table.el org-table-current-dline
  2052. org-table-cut-region org-table-delete-column org-table-edit-field
  2053. org-table-edit-formulas org-table-end org-table-eval-formula
  2054. org-table-export org-table-field-info
  2055. org-table-get-stored-formulas org-table-goto-column
  2056. org-table-hline-and-move org-table-import org-table-insert-column
  2057. org-table-insert-hline org-table-insert-row org-table-iterate
  2058. org-table-justify-field-maybe org-table-kill-row
  2059. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2060. org-table-move-column org-table-move-column-left
  2061. org-table-move-column-right org-table-move-row
  2062. org-table-move-row-down org-table-move-row-up
  2063. org-table-next-field org-table-next-row org-table-paste-rectangle
  2064. org-table-previous-field org-table-recalculate
  2065. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2066. org-table-toggle-coordinate-overlays
  2067. org-table-toggle-formula-debugger org-table-wrap-region
  2068. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2069. (defun org-at-table-p (&optional table-type)
  2070. "Return t if the cursor is inside an org-type table.
  2071. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2072. (if org-enable-table-editor
  2073. (save-excursion
  2074. (beginning-of-line 1)
  2075. (looking-at (if table-type org-table-any-line-regexp
  2076. org-table-line-regexp)))
  2077. nil))
  2078. (defsubst org-table-p () (org-at-table-p))
  2079. (defun org-at-table.el-p ()
  2080. "Return t if and only if we are at a table.el table."
  2081. (and (org-at-table-p 'any)
  2082. (save-excursion
  2083. (goto-char (org-table-begin 'any))
  2084. (looking-at org-table1-hline-regexp))))
  2085. (defun org-table-recognize-table.el ()
  2086. "If there is a table.el table nearby, recognize it and move into it."
  2087. (if org-table-tab-recognizes-table.el
  2088. (if (org-at-table.el-p)
  2089. (progn
  2090. (beginning-of-line 1)
  2091. (if (looking-at org-table-dataline-regexp)
  2092. nil
  2093. (if (looking-at org-table1-hline-regexp)
  2094. (progn
  2095. (beginning-of-line 2)
  2096. (if (looking-at org-table-any-border-regexp)
  2097. (beginning-of-line -1)))))
  2098. (if (re-search-forward "|" (org-table-end t) t)
  2099. (progn
  2100. (require 'table)
  2101. (if (table--at-cell-p (point))
  2102. t
  2103. (message "recognizing table.el table...")
  2104. (table-recognize-table)
  2105. (message "recognizing table.el table...done")))
  2106. (error "This should not happen..."))
  2107. t)
  2108. nil)
  2109. nil))
  2110. (defun org-at-table-hline-p ()
  2111. "Return t if the cursor is inside a hline in a table."
  2112. (if org-enable-table-editor
  2113. (save-excursion
  2114. (beginning-of-line 1)
  2115. (looking-at org-table-hline-regexp))
  2116. nil))
  2117. (defvar org-table-clean-did-remove-column nil)
  2118. (defun org-table-map-tables (function)
  2119. "Apply FUNCTION to the start of all tables in the buffer."
  2120. (save-excursion
  2121. (save-restriction
  2122. (widen)
  2123. (goto-char (point-min))
  2124. (while (re-search-forward org-table-any-line-regexp nil t)
  2125. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2126. (beginning-of-line 1)
  2127. (if (looking-at org-table-line-regexp)
  2128. (save-excursion (funcall function)))
  2129. (re-search-forward org-table-any-border-regexp nil 1))))
  2130. (message "Mapping tables: done"))
  2131. ;; Declare and autoload functions from org-exp.el
  2132. (declare-function org-default-export-plist "org-exp")
  2133. (declare-function org-infile-export-plist "org-exp")
  2134. (declare-function org-get-current-options "org-exp")
  2135. (eval-and-compile
  2136. (org-autoload "org-exp"
  2137. '(org-export org-export-as-ascii org-export-visible
  2138. org-insert-export-options-template org-export-as-html-and-open
  2139. org-export-as-html-batch org-export-as-html-to-buffer
  2140. org-replace-region-by-html org-export-region-as-html
  2141. org-export-as-html org-export-icalendar-this-file
  2142. org-export-icalendar-all-agenda-files
  2143. org-table-clean-before-export
  2144. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2145. ;; Declare and autoload functions from org-agenda.el
  2146. (eval-and-compile
  2147. (org-autoload "org-agenda"
  2148. '(org-agenda org-agenda-list org-search-view
  2149. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2150. org-diary org-agenda-to-appt)))
  2151. ;; Autoload org-remember
  2152. (eval-and-compile
  2153. (org-autoload "org-remember"
  2154. '(org-remember-insinuate org-remember-annotation
  2155. org-remember-apply-template org-remember org-remember-handler)))
  2156. ;; Autoload org-clock.el
  2157. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2158. (beg end))
  2159. (declare-function org-update-mode-line "org-clock" ())
  2160. (defvar org-clock-start-time)
  2161. (defvar org-clock-marker (make-marker)
  2162. "Marker recording the last clock-in.")
  2163. (eval-and-compile
  2164. (org-autoload
  2165. "org-clock"
  2166. '(org-clock-in org-clock-out org-clock-cancel
  2167. org-clock-goto org-clock-sum org-clock-display
  2168. org-remove-clock-overlays org-clock-report
  2169. org-clocktable-shift org-dblock-write:clocktable
  2170. org-get-clocktable)))
  2171. (defun org-clock-update-time-maybe ()
  2172. "If this is a CLOCK line, update it and return t.
  2173. Otherwise, return nil."
  2174. (interactive)
  2175. (save-excursion
  2176. (beginning-of-line 1)
  2177. (skip-chars-forward " \t")
  2178. (when (looking-at org-clock-string)
  2179. (let ((re (concat "[ \t]*" org-clock-string
  2180. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2181. "\\([ \t]*=>.*\\)?\\)?"))
  2182. ts te h m s)
  2183. (cond
  2184. ((not (looking-at re))
  2185. nil)
  2186. ((not (match-end 2))
  2187. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2188. (> org-clock-marker (point))
  2189. (<= org-clock-marker (point-at-eol)))
  2190. ;; The clock is running here
  2191. (setq org-clock-start-time
  2192. (apply 'encode-time
  2193. (org-parse-time-string (match-string 1))))
  2194. (org-update-mode-line)))
  2195. (t
  2196. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2197. (end-of-line 1)
  2198. (setq ts (match-string 1)
  2199. te (match-string 3))
  2200. (setq s (- (time-to-seconds
  2201. (apply 'encode-time (org-parse-time-string te)))
  2202. (time-to-seconds
  2203. (apply 'encode-time (org-parse-time-string ts))))
  2204. h (floor (/ s 3600))
  2205. s (- s (* 3600 h))
  2206. m (floor (/ s 60))
  2207. s (- s (* 60 s)))
  2208. (insert " => " (format "%2d:%02d" h m))
  2209. t))))))
  2210. (defun org-check-running-clock ()
  2211. "Check if the current buffer contains the running clock.
  2212. If yes, offer to stop it and to save the buffer with the changes."
  2213. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2214. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2215. (buffer-name))))
  2216. (org-clock-out)
  2217. (when (y-or-n-p "Save changed buffer?")
  2218. (save-buffer))))
  2219. (defun org-clocktable-try-shift (dir n)
  2220. "Check if this line starts a clock table, if yes, shift the time block."
  2221. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2222. (org-clocktable-shift dir n)))
  2223. ;; Autoload archiving code
  2224. ;; The stuff that is needed for cycling and tags has to be defined here.
  2225. (defgroup org-archive nil
  2226. "Options concerning archiving in Org-mode."
  2227. :tag "Org Archive"
  2228. :group 'org-structure)
  2229. (defcustom org-archive-location "%s_archive::"
  2230. "The location where subtrees should be archived.
  2231. Otherwise, the value of this variable is a string, consisting of two
  2232. parts, separated by a double-colon.
  2233. The first part is a file name - when omitted, archiving happens in the same
  2234. file. %s will be replaced by the current file name (without directory part).
  2235. Archiving to a different file is useful to keep archived entries from
  2236. contributing to the Org-mode Agenda.
  2237. The part after the double colon is a headline. The archived entries will be
  2238. filed under that headline. When omitted, the subtrees are simply filed away
  2239. at the end of the file, as top-level entries.
  2240. Here are a few examples:
  2241. \"%s_archive::\"
  2242. If the current file is Projects.org, archive in file
  2243. Projects.org_archive, as top-level trees. This is the default.
  2244. \"::* Archived Tasks\"
  2245. Archive in the current file, under the top-level headline
  2246. \"* Archived Tasks\".
  2247. \"~/org/archive.org::\"
  2248. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2249. \"basement::** Finished Tasks\"
  2250. Archive in file ./basement (relative path), as level 3 trees
  2251. below the level 2 heading \"** Finished Tasks\".
  2252. You may set this option on a per-file basis by adding to the buffer a
  2253. line like
  2254. #+ARCHIVE: basement::** Finished Tasks
  2255. You may also define it locally for a subtree by setting an ARCHIVE property
  2256. in the entry. If such a property is found in an entry, or anywhere up
  2257. the hierarchy, it will be used."
  2258. :group 'org-archive
  2259. :type 'string)
  2260. (defcustom org-archive-tag "ARCHIVE"
  2261. "The tag that marks a subtree as archived.
  2262. An archived subtree does not open during visibility cycling, and does
  2263. not contribute to the agenda listings.
  2264. After changing this, font-lock must be restarted in the relevant buffers to
  2265. get the proper fontification."
  2266. :group 'org-archive
  2267. :group 'org-keywords
  2268. :type 'string)
  2269. (defcustom org-agenda-skip-archived-trees t
  2270. "Non-nil means, the agenda will skip any items located in archived trees.
  2271. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2272. variable is no longer recommended, you should leave it at the value t.
  2273. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2274. :group 'org-archive
  2275. :group 'org-agenda-skip
  2276. :type 'boolean)
  2277. (defcustom org-cycle-open-archived-trees nil
  2278. "Non-nil means, `org-cycle' will open archived trees.
  2279. An archived tree is a tree marked with the tag ARCHIVE.
  2280. When nil, archived trees will stay folded. You can still open them with
  2281. normal outline commands like `show-all', but not with the cycling commands."
  2282. :group 'org-archive
  2283. :group 'org-cycle
  2284. :type 'boolean)
  2285. (defcustom org-sparse-tree-open-archived-trees nil
  2286. "Non-nil means sparse tree construction shows matches in archived trees.
  2287. When nil, matches in these trees are highlighted, but the trees are kept in
  2288. collapsed state."
  2289. :group 'org-archive
  2290. :group 'org-sparse-trees
  2291. :type 'boolean)
  2292. (defun org-cycle-hide-archived-subtrees (state)
  2293. "Re-hide all archived subtrees after a visibility state change."
  2294. (when (and (not org-cycle-open-archived-trees)
  2295. (not (memq state '(overview folded))))
  2296. (save-excursion
  2297. (let* ((globalp (memq state '(contents all)))
  2298. (beg (if globalp (point-min) (point)))
  2299. (end (if globalp (point-max) (org-end-of-subtree t))))
  2300. (org-hide-archived-subtrees beg end)
  2301. (goto-char beg)
  2302. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2303. (message "%s" (substitute-command-keys
  2304. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2305. (defun org-force-cycle-archived ()
  2306. "Cycle subtree even if it is archived."
  2307. (interactive)
  2308. (setq this-command 'org-cycle)
  2309. (let ((org-cycle-open-archived-trees t))
  2310. (call-interactively 'org-cycle)))
  2311. (defun org-hide-archived-subtrees (beg end)
  2312. "Re-hide all archived subtrees after a visibility state change."
  2313. (save-excursion
  2314. (let* ((re (concat ":" org-archive-tag ":")))
  2315. (goto-char beg)
  2316. (while (re-search-forward re end t)
  2317. (and (org-on-heading-p) (hide-subtree))
  2318. (org-end-of-subtree t)))))
  2319. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2320. (eval-and-compile
  2321. (org-autoload "org-archive"
  2322. '(org-add-archive-files org-archive-subtree
  2323. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2324. ;; Autoload Column View Code
  2325. (declare-function org-columns-number-to-string "org-colview")
  2326. (declare-function org-columns-get-format-and-top-level "org-colview")
  2327. (declare-function org-columns-compute "org-colview")
  2328. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2329. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2330. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2331. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2332. ;; Autoload ID code
  2333. (org-autoload "org-id"
  2334. '(org-id-get-create org-id-new org-id-copy org-id-get
  2335. org-id-get-with-outline-path-completion
  2336. org-id-get-with-outline-drilling
  2337. org-id-goto org-id-find))
  2338. ;;; Variables for pre-computed regular expressions, all buffer local
  2339. (defvar org-drawer-regexp nil
  2340. "Matches first line of a hidden block.")
  2341. (make-variable-buffer-local 'org-drawer-regexp)
  2342. (defvar org-todo-regexp nil
  2343. "Matches any of the TODO state keywords.")
  2344. (make-variable-buffer-local 'org-todo-regexp)
  2345. (defvar org-not-done-regexp nil
  2346. "Matches any of the TODO state keywords except the last one.")
  2347. (make-variable-buffer-local 'org-not-done-regexp)
  2348. (defvar org-todo-line-regexp nil
  2349. "Matches a headline and puts TODO state into group 2 if present.")
  2350. (make-variable-buffer-local 'org-todo-line-regexp)
  2351. (defvar org-complex-heading-regexp nil
  2352. "Matches a headline and puts everything into groups:
  2353. group 1: the stars
  2354. group 2: The todo keyword, maybe
  2355. group 3: Priority cookie
  2356. group 4: True headline
  2357. group 5: Tags")
  2358. (make-variable-buffer-local 'org-complex-heading-regexp)
  2359. (defvar org-todo-line-tags-regexp nil
  2360. "Matches a headline and puts TODO state into group 2 if present.
  2361. Also put tags into group 4 if tags are present.")
  2362. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2363. (defvar org-nl-done-regexp nil
  2364. "Matches newline followed by a headline with the DONE keyword.")
  2365. (make-variable-buffer-local 'org-nl-done-regexp)
  2366. (defvar org-looking-at-done-regexp nil
  2367. "Matches the DONE keyword a point.")
  2368. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2369. (defvar org-ds-keyword-length 12
  2370. "Maximum length of the Deadline and SCHEDULED keywords.")
  2371. (make-variable-buffer-local 'org-ds-keyword-length)
  2372. (defvar org-deadline-regexp nil
  2373. "Matches the DEADLINE keyword.")
  2374. (make-variable-buffer-local 'org-deadline-regexp)
  2375. (defvar org-deadline-time-regexp nil
  2376. "Matches the DEADLINE keyword together with a time stamp.")
  2377. (make-variable-buffer-local 'org-deadline-time-regexp)
  2378. (defvar org-deadline-line-regexp nil
  2379. "Matches the DEADLINE keyword and the rest of the line.")
  2380. (make-variable-buffer-local 'org-deadline-line-regexp)
  2381. (defvar org-scheduled-regexp nil
  2382. "Matches the SCHEDULED keyword.")
  2383. (make-variable-buffer-local 'org-scheduled-regexp)
  2384. (defvar org-scheduled-time-regexp nil
  2385. "Matches the SCHEDULED keyword together with a time stamp.")
  2386. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2387. (defvar org-closed-time-regexp nil
  2388. "Matches the CLOSED keyword together with a time stamp.")
  2389. (make-variable-buffer-local 'org-closed-time-regexp)
  2390. (defvar org-keyword-time-regexp nil
  2391. "Matches any of the 4 keywords, together with the time stamp.")
  2392. (make-variable-buffer-local 'org-keyword-time-regexp)
  2393. (defvar org-keyword-time-not-clock-regexp nil
  2394. "Matches any of the 3 keywords, together with the time stamp.")
  2395. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2396. (defvar org-maybe-keyword-time-regexp nil
  2397. "Matches a timestamp, possibly preceeded by a keyword.")
  2398. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2399. (defvar org-planning-or-clock-line-re nil
  2400. "Matches a line with planning or clock info.")
  2401. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2402. (defconst org-plain-time-of-day-regexp
  2403. (concat
  2404. "\\(\\<[012]?[0-9]"
  2405. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2406. "\\(--?"
  2407. "\\(\\<[012]?[0-9]"
  2408. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2409. "\\)?")
  2410. "Regular expression to match a plain time or time range.
  2411. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2412. groups carry important information:
  2413. 0 the full match
  2414. 1 the first time, range or not
  2415. 8 the second time, if it is a range.")
  2416. (defconst org-plain-time-extension-regexp
  2417. (concat
  2418. "\\(\\<[012]?[0-9]"
  2419. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2420. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2421. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2422. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2423. groups carry important information:
  2424. 0 the full match
  2425. 7 hours of duration
  2426. 9 minutes of duration")
  2427. (defconst org-stamp-time-of-day-regexp
  2428. (concat
  2429. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2430. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2431. "\\(--?"
  2432. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2433. "Regular expression to match a timestamp time or time range.
  2434. After a match, the following groups carry important information:
  2435. 0 the full match
  2436. 1 date plus weekday, for backreferencing to make sure both times on same day
  2437. 2 the first time, range or not
  2438. 4 the second time, if it is a range.")
  2439. (defconst org-startup-options
  2440. '(("fold" org-startup-folded t)
  2441. ("overview" org-startup-folded t)
  2442. ("nofold" org-startup-folded nil)
  2443. ("showall" org-startup-folded nil)
  2444. ("content" org-startup-folded content)
  2445. ("hidestars" org-hide-leading-stars t)
  2446. ("showstars" org-hide-leading-stars nil)
  2447. ("odd" org-odd-levels-only t)
  2448. ("oddeven" org-odd-levels-only nil)
  2449. ("align" org-startup-align-all-tables t)
  2450. ("noalign" org-startup-align-all-tables nil)
  2451. ("customtime" org-display-custom-times t)
  2452. ("logdone" org-log-done time)
  2453. ("lognotedone" org-log-done note)
  2454. ("nologdone" org-log-done nil)
  2455. ("lognoteclock-out" org-log-note-clock-out t)
  2456. ("nolognoteclock-out" org-log-note-clock-out nil)
  2457. ("logrepeat" org-log-repeat state)
  2458. ("lognoterepeat" org-log-repeat note)
  2459. ("nologrepeat" org-log-repeat nil)
  2460. ("constcgs" constants-unit-system cgs)
  2461. ("constSI" constants-unit-system SI))
  2462. "Variable associated with STARTUP options for org-mode.
  2463. Each element is a list of three items: The startup options as written
  2464. in the #+STARTUP line, the corresponding variable, and the value to
  2465. set this variable to if the option is found. An optional forth element PUSH
  2466. means to push this value onto the list in the variable.")
  2467. (defun org-set-regexps-and-options ()
  2468. "Precompute regular expressions for current buffer."
  2469. (when (org-mode-p)
  2470. (org-set-local 'org-todo-kwd-alist nil)
  2471. (org-set-local 'org-todo-key-alist nil)
  2472. (org-set-local 'org-todo-key-trigger nil)
  2473. (org-set-local 'org-todo-keywords-1 nil)
  2474. (org-set-local 'org-done-keywords nil)
  2475. (org-set-local 'org-todo-heads nil)
  2476. (org-set-local 'org-todo-sets nil)
  2477. (org-set-local 'org-todo-log-states nil)
  2478. (org-set-local 'org-file-properties nil)
  2479. (org-set-local 'org-file-tags nil)
  2480. (let ((re (org-make-options-regexp
  2481. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2482. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2483. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2484. (splitre "[ \t]+")
  2485. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2486. tail sep kws1 prio props ftags drawers
  2487. ext-setup-or-nil setup-contents (start 0))
  2488. (save-excursion
  2489. (save-restriction
  2490. (widen)
  2491. (goto-char (point-min))
  2492. (while (or (and ext-setup-or-nil
  2493. (string-match re ext-setup-or-nil start)
  2494. (setq start (match-end 0)))
  2495. (and (setq ext-setup-or-nil nil start 0)
  2496. (re-search-forward re nil t)))
  2497. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2498. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2499. (cond
  2500. ((equal key "CATEGORY")
  2501. (if (string-match "[ \t]+$" value)
  2502. (setq value (replace-match "" t t value)))
  2503. (setq cat value))
  2504. ((member key '("SEQ_TODO" "TODO"))
  2505. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2506. ((equal key "TYP_TODO")
  2507. (push (cons 'type (org-split-string value splitre)) kwds))
  2508. ((equal key "TAGS")
  2509. (setq tags (append tags (org-split-string value splitre))))
  2510. ((equal key "COLUMNS")
  2511. (org-set-local 'org-columns-default-format value))
  2512. ((equal key "LINK")
  2513. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2514. (push (cons (match-string 1 value)
  2515. (org-trim (match-string 2 value)))
  2516. links)))
  2517. ((equal key "PRIORITIES")
  2518. (setq prio (org-split-string value " +")))
  2519. ((equal key "PROPERTY")
  2520. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2521. (push (cons (match-string 1 value) (match-string 2 value))
  2522. props)))
  2523. ((equal key "FILETAGS")
  2524. (when (string-match "\\S-" value)
  2525. (setq ftags
  2526. (append
  2527. ftags
  2528. (apply 'append
  2529. (mapcar (lambda (x) (org-split-string x ":"))
  2530. (org-split-string value)))))))
  2531. ((equal key "DRAWERS")
  2532. (setq drawers (org-split-string value splitre)))
  2533. ((equal key "CONSTANTS")
  2534. (setq const (append const (org-split-string value splitre))))
  2535. ((equal key "STARTUP")
  2536. (let ((opts (org-split-string value splitre))
  2537. l var val)
  2538. (while (setq l (pop opts))
  2539. (when (setq l (assoc l org-startup-options))
  2540. (setq var (nth 1 l) val (nth 2 l))
  2541. (if (not (nth 3 l))
  2542. (set (make-local-variable var) val)
  2543. (if (not (listp (symbol-value var)))
  2544. (set (make-local-variable var) nil))
  2545. (set (make-local-variable var) (symbol-value var))
  2546. (add-to-list var val))))))
  2547. ((equal key "ARCHIVE")
  2548. (string-match " *$" value)
  2549. (setq arch (replace-match "" t t value))
  2550. (remove-text-properties 0 (length arch)
  2551. '(face t fontified t) arch))
  2552. ((equal key "SETUPFILE")
  2553. (setq setup-contents (org-file-contents
  2554. (expand-file-name
  2555. (org-remove-double-quotes value))
  2556. 'noerror))
  2557. (if (not ext-setup-or-nil)
  2558. (setq ext-setup-or-nil setup-contents start 0)
  2559. (setq ext-setup-or-nil
  2560. (concat (substring ext-setup-or-nil 0 start)
  2561. "\n" setup-contents "\n"
  2562. (substring ext-setup-or-nil start)))))
  2563. ))))
  2564. (when cat
  2565. (org-set-local 'org-category (intern cat))
  2566. (push (cons "CATEGORY" cat) props))
  2567. (when prio
  2568. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2569. (setq prio (mapcar 'string-to-char prio))
  2570. (org-set-local 'org-highest-priority (nth 0 prio))
  2571. (org-set-local 'org-lowest-priority (nth 1 prio))
  2572. (org-set-local 'org-default-priority (nth 2 prio)))
  2573. (and props (org-set-local 'org-file-properties (nreverse props)))
  2574. (and ftags (org-set-local 'org-file-tags ftags))
  2575. (and drawers (org-set-local 'org-drawers drawers))
  2576. (and arch (org-set-local 'org-archive-location arch))
  2577. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2578. ;; Process the TODO keywords
  2579. (unless kwds
  2580. ;; Use the global values as if they had been given locally.
  2581. (setq kwds (default-value 'org-todo-keywords))
  2582. (if (stringp (car kwds))
  2583. (setq kwds (list (cons org-todo-interpretation
  2584. (default-value 'org-todo-keywords)))))
  2585. (setq kwds (reverse kwds)))
  2586. (setq kwds (nreverse kwds))
  2587. (let (inter kws kw)
  2588. (while (setq kws (pop kwds))
  2589. (setq inter (pop kws) sep (member "|" kws)
  2590. kws0 (delete "|" (copy-sequence kws))
  2591. kwsa nil
  2592. kws1 (mapcar
  2593. (lambda (x)
  2594. ;; 1 2
  2595. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2596. (progn
  2597. (setq kw (match-string 1 x)
  2598. key (and (match-end 2) (match-string 2 x))
  2599. log (org-extract-log-state-settings x))
  2600. (push (cons kw (and key (string-to-char key))) kwsa)
  2601. (and log (push log org-todo-log-states))
  2602. kw)
  2603. (error "Invalid TODO keyword %s" x)))
  2604. kws0)
  2605. kwsa (if kwsa (append '((:startgroup))
  2606. (nreverse kwsa)
  2607. '((:endgroup))))
  2608. hw (car kws1)
  2609. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2610. tail (list inter hw (car dws) (org-last dws)))
  2611. (add-to-list 'org-todo-heads hw 'append)
  2612. (push kws1 org-todo-sets)
  2613. (setq org-done-keywords (append org-done-keywords dws nil))
  2614. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2615. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2616. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2617. (setq org-todo-sets (nreverse org-todo-sets)
  2618. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2619. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2620. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2621. ;; Process the constants
  2622. (when const
  2623. (let (e cst)
  2624. (while (setq e (pop const))
  2625. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2626. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2627. (setq org-table-formula-constants-local cst)))
  2628. ;; Process the tags.
  2629. (when tags
  2630. (let (e tgs)
  2631. (while (setq e (pop tags))
  2632. (cond
  2633. ((equal e "{") (push '(:startgroup) tgs))
  2634. ((equal e "}") (push '(:endgroup) tgs))
  2635. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2636. (push (cons (match-string 1 e)
  2637. (string-to-char (match-string 2 e)))
  2638. tgs))
  2639. (t (push (list e) tgs))))
  2640. (org-set-local 'org-tag-alist nil)
  2641. (while (setq e (pop tgs))
  2642. (or (and (stringp (car e))
  2643. (assoc (car e) org-tag-alist))
  2644. (push e org-tag-alist)))))
  2645. ;; Compute the regular expressions and other local variables
  2646. (if (not org-done-keywords)
  2647. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2648. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2649. (length org-scheduled-string)
  2650. (length org-clock-string)
  2651. (length org-closed-string)))
  2652. org-drawer-regexp
  2653. (concat "^[ \t]*:\\("
  2654. (mapconcat 'regexp-quote org-drawers "\\|")
  2655. "\\):[ \t]*$")
  2656. org-not-done-keywords
  2657. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2658. org-todo-regexp
  2659. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2660. "\\|") "\\)\\>")
  2661. org-not-done-regexp
  2662. (concat "\\<\\("
  2663. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2664. "\\)\\>")
  2665. org-todo-line-regexp
  2666. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2667. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2668. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2669. org-complex-heading-regexp
  2670. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2671. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2672. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2673. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2674. org-nl-done-regexp
  2675. (concat "\n\\*+[ \t]+"
  2676. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2677. "\\)" "\\>")
  2678. org-todo-line-tags-regexp
  2679. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2680. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2681. (org-re
  2682. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2683. org-looking-at-done-regexp
  2684. (concat "^" "\\(?:"
  2685. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2686. "\\>")
  2687. org-deadline-regexp (concat "\\<" org-deadline-string)
  2688. org-deadline-time-regexp
  2689. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2690. org-deadline-line-regexp
  2691. (concat "\\<\\(" org-deadline-string "\\).*")
  2692. org-scheduled-regexp
  2693. (concat "\\<" org-scheduled-string)
  2694. org-scheduled-time-regexp
  2695. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2696. org-closed-time-regexp
  2697. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2698. org-keyword-time-regexp
  2699. (concat "\\<\\(" org-scheduled-string
  2700. "\\|" org-deadline-string
  2701. "\\|" org-closed-string
  2702. "\\|" org-clock-string "\\)"
  2703. " *[[<]\\([^]>]+\\)[]>]")
  2704. org-keyword-time-not-clock-regexp
  2705. (concat "\\<\\(" org-scheduled-string
  2706. "\\|" org-deadline-string
  2707. "\\|" org-closed-string
  2708. "\\)"
  2709. " *[[<]\\([^]>]+\\)[]>]")
  2710. org-maybe-keyword-time-regexp
  2711. (concat "\\(\\<\\(" org-scheduled-string
  2712. "\\|" org-deadline-string
  2713. "\\|" org-closed-string
  2714. "\\|" org-clock-string "\\)\\)?"
  2715. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2716. org-planning-or-clock-line-re
  2717. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2718. "\\|" org-deadline-string
  2719. "\\|" org-closed-string "\\|" org-clock-string
  2720. "\\)\\>\\)")
  2721. )
  2722. (org-compute-latex-and-specials-regexp)
  2723. (org-set-font-lock-defaults))))
  2724. (defun org-file-contents (file &optional noerror)
  2725. "Return the contents of FILE, as a string."
  2726. (if (or (not file)
  2727. (not (file-readable-p file)))
  2728. (if noerror
  2729. (progn
  2730. (message "Cannot read file %s" file)
  2731. (ding) (sit-for 2)
  2732. "")
  2733. (error "Cannot read file %s" file))
  2734. (with-temp-buffer
  2735. (insert-file-contents file)
  2736. (buffer-string))))
  2737. (defun org-extract-log-state-settings (x)
  2738. "Extract the log state setting from a TODO keyword string.
  2739. This will extract info from a string like \"WAIT(w@/!)\"."
  2740. (let (kw key log1 log2)
  2741. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2742. (setq kw (match-string 1 x)
  2743. key (and (match-end 2) (match-string 2 x))
  2744. log1 (and (match-end 3) (match-string 3 x))
  2745. log2 (and (match-end 4) (match-string 4 x)))
  2746. (and (or log1 log2)
  2747. (list kw
  2748. (and log1 (if (equal log1 "!") 'time 'note))
  2749. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2750. (defun org-remove-keyword-keys (list)
  2751. "Remove a pair of parenthesis at the end of each string in LIST."
  2752. (mapcar (lambda (x)
  2753. (if (string-match "(.*)$" x)
  2754. (substring x 0 (match-beginning 0))
  2755. x))
  2756. list))
  2757. ;; FIXME: this could be done much better, using second characters etc.
  2758. (defun org-assign-fast-keys (alist)
  2759. "Assign fast keys to a keyword-key alist.
  2760. Respect keys that are already there."
  2761. (let (new e k c c1 c2 (char ?a))
  2762. (while (setq e (pop alist))
  2763. (cond
  2764. ((equal e '(:startgroup)) (push e new))
  2765. ((equal e '(:endgroup)) (push e new))
  2766. (t
  2767. (setq k (car e) c2 nil)
  2768. (if (cdr e)
  2769. (setq c (cdr e))
  2770. ;; automatically assign a character.
  2771. (setq c1 (string-to-char
  2772. (downcase (substring
  2773. k (if (= (string-to-char k) ?@) 1 0)))))
  2774. (if (or (rassoc c1 new) (rassoc c1 alist))
  2775. (while (or (rassoc char new) (rassoc char alist))
  2776. (setq char (1+ char)))
  2777. (setq c2 c1))
  2778. (setq c (or c2 char)))
  2779. (push (cons k c) new))))
  2780. (nreverse new)))
  2781. ;;; Some variables used in various places
  2782. (defvar org-window-configuration nil
  2783. "Used in various places to store a window configuration.")
  2784. (defvar org-finish-function nil
  2785. "Function to be called when `C-c C-c' is used.
  2786. This is for getting out of special buffers like remember.")
  2787. ;; FIXME: Occasionally check by commenting these, to make sure
  2788. ;; no other functions uses these, forgetting to let-bind them.
  2789. (defvar entry)
  2790. (defvar state)
  2791. (defvar last-state)
  2792. (defvar date)
  2793. (defvar description)
  2794. ;; Defined somewhere in this file, but used before definition.
  2795. (defvar org-html-entities)
  2796. (defvar org-struct-menu)
  2797. (defvar org-org-menu)
  2798. (defvar org-tbl-menu)
  2799. (defvar org-agenda-keymap)
  2800. ;;;; Define the Org-mode
  2801. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2802. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  2803. ;; We use a before-change function to check if a table might need
  2804. ;; an update.
  2805. (defvar org-table-may-need-update t
  2806. "Indicates that a table might need an update.
  2807. This variable is set by `org-before-change-function'.
  2808. `org-table-align' sets it back to nil.")
  2809. (defun org-before-change-function (beg end)
  2810. "Every change indicates that a table might need an update."
  2811. (setq org-table-may-need-update t))
  2812. (defvar org-mode-map)
  2813. (defvar org-mode-hook nil)
  2814. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2815. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2816. (defvar org-table-buffer-is-an nil)
  2817. (defconst org-outline-regexp "\\*+ ")
  2818. ;;;###autoload
  2819. (define-derived-mode org-mode outline-mode "Org"
  2820. "Outline-based notes management and organizer, alias
  2821. \"Carsten's outline-mode for keeping track of everything.\"
  2822. Org-mode develops organizational tasks around a NOTES file which
  2823. contains information about projects as plain text. Org-mode is
  2824. implemented on top of outline-mode, which is ideal to keep the content
  2825. of large files well structured. It supports ToDo items, deadlines and
  2826. time stamps, which magically appear in the diary listing of the Emacs
  2827. calendar. Tables are easily created with a built-in table editor.
  2828. Plain text URL-like links connect to websites, emails (VM), Usenet
  2829. messages (Gnus), BBDB entries, and any files related to the project.
  2830. For printing and sharing of notes, an Org-mode file (or a part of it)
  2831. can be exported as a structured ASCII or HTML file.
  2832. The following commands are available:
  2833. \\{org-mode-map}"
  2834. ;; Get rid of Outline menus, they are not needed
  2835. ;; Need to do this here because define-derived-mode sets up
  2836. ;; the keymap so late. Still, it is a waste to call this each time
  2837. ;; we switch another buffer into org-mode.
  2838. (if (featurep 'xemacs)
  2839. (when (boundp 'outline-mode-menu-heading)
  2840. ;; Assume this is Greg's port, it used easymenu
  2841. (easy-menu-remove outline-mode-menu-heading)
  2842. (easy-menu-remove outline-mode-menu-show)
  2843. (easy-menu-remove outline-mode-menu-hide))
  2844. (define-key org-mode-map [menu-bar headings] 'undefined)
  2845. (define-key org-mode-map [menu-bar hide] 'undefined)
  2846. (define-key org-mode-map [menu-bar show] 'undefined))
  2847. (org-load-modules-maybe)
  2848. (easy-menu-add org-org-menu)
  2849. (easy-menu-add org-tbl-menu)
  2850. (org-install-agenda-files-menu)
  2851. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2852. (org-add-to-invisibility-spec '(org-cwidth))
  2853. (when (featurep 'xemacs)
  2854. (org-set-local 'line-move-ignore-invisible t))
  2855. (org-set-local 'outline-regexp org-outline-regexp)
  2856. (org-set-local 'outline-level 'org-outline-level)
  2857. (when (and org-ellipsis
  2858. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2859. (fboundp 'make-glyph-code))
  2860. (unless org-display-table
  2861. (setq org-display-table (make-display-table)))
  2862. (set-display-table-slot
  2863. org-display-table 4
  2864. (vconcat (mapcar
  2865. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2866. org-ellipsis)))
  2867. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2868. (setq buffer-display-table org-display-table))
  2869. (org-set-regexps-and-options)
  2870. ;; Calc embedded
  2871. (org-set-local 'calc-embedded-open-mode "# ")
  2872. (modify-syntax-entry ?# "<")
  2873. (modify-syntax-entry ?@ "w")
  2874. (if org-startup-truncated (setq truncate-lines t))
  2875. (org-set-local 'font-lock-unfontify-region-function
  2876. 'org-unfontify-region)
  2877. ;; Activate before-change-function
  2878. (org-set-local 'org-table-may-need-update t)
  2879. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2880. 'local)
  2881. ;; Check for running clock before killing a buffer
  2882. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2883. ;; Paragraphs and auto-filling
  2884. (org-set-autofill-regexps)
  2885. (setq indent-line-function 'org-indent-line-function)
  2886. (org-update-radio-target-regexp)
  2887. ;; Comment characters
  2888. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2889. (org-set-local 'comment-padding " ")
  2890. ;; Align options lines
  2891. (org-set-local
  2892. 'align-mode-rules-list
  2893. '((org-in-buffer-settings
  2894. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2895. (modes . '(org-mode)))))
  2896. ;; Imenu
  2897. (org-set-local 'imenu-create-index-function
  2898. 'org-imenu-get-tree)
  2899. ;; Make isearch reveal context
  2900. (if (or (featurep 'xemacs)
  2901. (not (boundp 'outline-isearch-open-invisible-function)))
  2902. ;; Emacs 21 and XEmacs make use of the hook
  2903. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2904. ;; Emacs 22 deals with this through a special variable
  2905. (org-set-local 'outline-isearch-open-invisible-function
  2906. (lambda (&rest ignore) (org-show-context 'isearch))))
  2907. ;; If empty file that did not turn on org-mode automatically, make it to.
  2908. (if (and org-insert-mode-line-in-empty-file
  2909. (interactive-p)
  2910. (= (point-min) (point-max)))
  2911. (insert "# -*- mode: org -*-\n\n"))
  2912. (unless org-inhibit-startup
  2913. (when org-startup-align-all-tables
  2914. (let ((bmp (buffer-modified-p)))
  2915. (org-table-map-tables 'org-table-align)
  2916. (set-buffer-modified-p bmp)))
  2917. (org-set-startup-visibility)))
  2918. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2919. (defun org-current-time ()
  2920. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2921. (if (> (car org-time-stamp-rounding-minutes) 1)
  2922. (let ((r (car org-time-stamp-rounding-minutes))
  2923. (time (decode-time)))
  2924. (apply 'encode-time
  2925. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2926. (nthcdr 2 time))))
  2927. (current-time)))
  2928. ;;;; Font-Lock stuff, including the activators
  2929. (defvar org-mouse-map (make-sparse-keymap))
  2930. (org-defkey org-mouse-map
  2931. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2932. (org-defkey org-mouse-map
  2933. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2934. (when org-mouse-1-follows-link
  2935. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2936. (when org-tab-follows-link
  2937. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2938. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2939. (when org-return-follows-link
  2940. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2941. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2942. (require 'font-lock)
  2943. (defconst org-non-link-chars "]\t\n\r<>")
  2944. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2945. "shell" "elisp"))
  2946. (defvar org-link-types-re nil
  2947. "Matches a link that has a url-like prefix like \"http:\"")
  2948. (defvar org-link-re-with-space nil
  2949. "Matches a link with spaces, optional angular brackets around it.")
  2950. (defvar org-link-re-with-space2 nil
  2951. "Matches a link with spaces, optional angular brackets around it.")
  2952. (defvar org-angle-link-re nil
  2953. "Matches link with angular brackets, spaces are allowed.")
  2954. (defvar org-plain-link-re nil
  2955. "Matches plain link, without spaces.")
  2956. (defvar org-bracket-link-regexp nil
  2957. "Matches a link in double brackets.")
  2958. (defvar org-bracket-link-analytic-regexp nil
  2959. "Regular expression used to analyze links.
  2960. Here is what the match groups contain after a match:
  2961. 1: http:
  2962. 2: http
  2963. 3: path
  2964. 4: [desc]
  2965. 5: desc")
  2966. (defvar org-any-link-re nil
  2967. "Regular expression matching any link.")
  2968. (defun org-make-link-regexps ()
  2969. "Update the link regular expressions.
  2970. This should be called after the variable `org-link-types' has changed."
  2971. (setq org-link-types-re
  2972. (concat
  2973. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2974. org-link-re-with-space
  2975. (concat
  2976. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2977. "\\([^" org-non-link-chars " ]"
  2978. "[^" org-non-link-chars "]*"
  2979. "[^" org-non-link-chars " ]\\)>?")
  2980. org-link-re-with-space2
  2981. (concat
  2982. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2983. "\\([^" org-non-link-chars " ]"
  2984. "[^]\t\n\r]*"
  2985. "[^" org-non-link-chars " ]\\)>?")
  2986. org-angle-link-re
  2987. (concat
  2988. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2989. "\\([^" org-non-link-chars " ]"
  2990. "[^" org-non-link-chars "]*"
  2991. "\\)>")
  2992. org-plain-link-re
  2993. (concat
  2994. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2995. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2996. org-bracket-link-regexp
  2997. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2998. org-bracket-link-analytic-regexp
  2999. (concat
  3000. "\\[\\["
  3001. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3002. "\\([^]]+\\)"
  3003. "\\]"
  3004. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3005. "\\]")
  3006. org-any-link-re
  3007. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3008. org-angle-link-re "\\)\\|\\("
  3009. org-plain-link-re "\\)")))
  3010. (org-make-link-regexps)
  3011. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3012. "Regular expression for fast time stamp matching.")
  3013. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3014. "Regular expression for fast time stamp matching.")
  3015. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3016. "Regular expression matching time strings for analysis.
  3017. This one does not require the space after the date, so it can be used
  3018. on a string that terminates immediately after the date.")
  3019. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3020. "Regular expression matching time strings for analysis.")
  3021. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3022. "Regular expression matching time stamps, with groups.")
  3023. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3024. "Regular expression matching time stamps (also [..]), with groups.")
  3025. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3026. "Regular expression matching a time stamp range.")
  3027. (defconst org-tr-regexp-both
  3028. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3029. "Regular expression matching a time stamp range.")
  3030. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3031. org-ts-regexp "\\)?")
  3032. "Regular expression matching a time stamp or time stamp range.")
  3033. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3034. org-ts-regexp-both "\\)?")
  3035. "Regular expression matching a time stamp or time stamp range.
  3036. The time stamps may be either active or inactive.")
  3037. (defvar org-emph-face nil)
  3038. (defun org-do-emphasis-faces (limit)
  3039. "Run through the buffer and add overlays to links."
  3040. (let (rtn)
  3041. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3042. (if (not (= (char-after (match-beginning 3))
  3043. (char-after (match-beginning 4))))
  3044. (progn
  3045. (setq rtn t)
  3046. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3047. 'face
  3048. (nth 1 (assoc (match-string 3)
  3049. org-emphasis-alist)))
  3050. (add-text-properties (match-beginning 2) (match-end 2)
  3051. '(font-lock-multiline t))
  3052. (when org-hide-emphasis-markers
  3053. (add-text-properties (match-end 4) (match-beginning 5)
  3054. '(invisible org-link))
  3055. (add-text-properties (match-beginning 3) (match-end 3)
  3056. '(invisible org-link)))))
  3057. (backward-char 1))
  3058. rtn))
  3059. (defun org-emphasize (&optional char)
  3060. "Insert or change an emphasis, i.e. a font like bold or italic.
  3061. If there is an active region, change that region to a new emphasis.
  3062. If there is no region, just insert the marker characters and position
  3063. the cursor between them.
  3064. CHAR should be either the marker character, or the first character of the
  3065. HTML tag associated with that emphasis. If CHAR is a space, the means
  3066. to remove the emphasis of the selected region.
  3067. If char is not given (for example in an interactive call) it
  3068. will be prompted for."
  3069. (interactive)
  3070. (let ((eal org-emphasis-alist) e det
  3071. (erc org-emphasis-regexp-components)
  3072. (prompt "")
  3073. (string "") beg end move tag c s)
  3074. (if (org-region-active-p)
  3075. (setq beg (region-beginning) end (region-end)
  3076. string (buffer-substring beg end))
  3077. (setq move t))
  3078. (while (setq e (pop eal))
  3079. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3080. c (aref tag 0))
  3081. (push (cons c (string-to-char (car e))) det)
  3082. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3083. (substring tag 1)))))
  3084. (unless char
  3085. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3086. (setq char (read-char-exclusive)))
  3087. (setq char (or (cdr (assoc char det)) char))
  3088. (if (equal char ?\ )
  3089. (setq s "" move nil)
  3090. (unless (assoc (char-to-string char) org-emphasis-alist)
  3091. (error "No such emphasis marker: \"%c\"" char))
  3092. (setq s (char-to-string char)))
  3093. (while (and (> (length string) 1)
  3094. (equal (substring string 0 1) (substring string -1))
  3095. (assoc (substring string 0 1) org-emphasis-alist))
  3096. (setq string (substring string 1 -1)))
  3097. (setq string (concat s string s))
  3098. (if beg (delete-region beg end))
  3099. (unless (or (bolp)
  3100. (string-match (concat "[" (nth 0 erc) "\n]")
  3101. (char-to-string (char-before (point)))))
  3102. (insert " "))
  3103. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3104. (char-to-string (char-after (point))))
  3105. (insert " ") (backward-char 1))
  3106. (insert string)
  3107. (and move (backward-char 1))))
  3108. (defconst org-nonsticky-props
  3109. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3110. (defun org-activate-plain-links (limit)
  3111. "Run through the buffer and add overlays to links."
  3112. (catch 'exit
  3113. (let (f)
  3114. (while (re-search-forward org-plain-link-re limit t)
  3115. (setq f (get-text-property (match-beginning 0) 'face))
  3116. (if (or (eq f 'org-tag)
  3117. (and (listp f) (memq 'org-tag f)))
  3118. nil
  3119. (add-text-properties (match-beginning 0) (match-end 0)
  3120. (list 'mouse-face 'highlight
  3121. 'rear-nonsticky org-nonsticky-props
  3122. 'keymap org-mouse-map
  3123. ))
  3124. (throw 'exit t))))))
  3125. (defun org-activate-code (limit)
  3126. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3127. (progn
  3128. (remove-text-properties (match-beginning 0) (match-end 0)
  3129. '(display t invisible t intangible t))
  3130. t)))
  3131. (defun org-activate-angle-links (limit)
  3132. "Run through the buffer and add overlays to links."
  3133. (if (re-search-forward org-angle-link-re limit t)
  3134. (progn
  3135. (add-text-properties (match-beginning 0) (match-end 0)
  3136. (list 'mouse-face 'highlight
  3137. 'rear-nonsticky org-nonsticky-props
  3138. 'keymap org-mouse-map
  3139. ))
  3140. t)))
  3141. (defun org-activate-bracket-links (limit)
  3142. "Run through the buffer and add overlays to bracketed links."
  3143. (if (re-search-forward org-bracket-link-regexp limit t)
  3144. (let* ((help (concat "LINK: "
  3145. (org-match-string-no-properties 1)))
  3146. ;; FIXME: above we should remove the escapes.
  3147. ;; but that requires another match, protecting match data,
  3148. ;; a lot of overhead for font-lock.
  3149. (ip (org-maybe-intangible
  3150. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3151. 'keymap org-mouse-map 'mouse-face 'highlight
  3152. 'font-lock-multiline t 'help-echo help)))
  3153. (vp (list 'rear-nonsticky org-nonsticky-props
  3154. 'keymap org-mouse-map 'mouse-face 'highlight
  3155. ' font-lock-multiline t 'help-echo help)))
  3156. ;; We need to remove the invisible property here. Table narrowing
  3157. ;; may have made some of this invisible.
  3158. (remove-text-properties (match-beginning 0) (match-end 0)
  3159. '(invisible nil))
  3160. (if (match-end 3)
  3161. (progn
  3162. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3163. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3164. (add-text-properties (match-end 3) (match-end 0) ip))
  3165. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3166. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3167. (add-text-properties (match-end 1) (match-end 0) ip))
  3168. t)))
  3169. (defun org-activate-dates (limit)
  3170. "Run through the buffer and add overlays to dates."
  3171. (if (re-search-forward org-tsr-regexp-both limit t)
  3172. (progn
  3173. (add-text-properties (match-beginning 0) (match-end 0)
  3174. (list 'mouse-face 'highlight
  3175. 'rear-nonsticky org-nonsticky-props
  3176. 'keymap org-mouse-map))
  3177. (when org-display-custom-times
  3178. (if (match-end 3)
  3179. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3180. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3181. t)))
  3182. (defvar org-target-link-regexp nil
  3183. "Regular expression matching radio targets in plain text.")
  3184. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3185. "Regular expression matching a link target.")
  3186. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3187. "Regular expression matching a radio target.")
  3188. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3189. "Regular expression matching any target.")
  3190. (defun org-activate-target-links (limit)
  3191. "Run through the buffer and add overlays to target matches."
  3192. (when org-target-link-regexp
  3193. (let ((case-fold-search t))
  3194. (if (re-search-forward org-target-link-regexp limit t)
  3195. (progn
  3196. (add-text-properties (match-beginning 0) (match-end 0)
  3197. (list 'mouse-face 'highlight
  3198. 'rear-nonsticky org-nonsticky-props
  3199. 'keymap org-mouse-map
  3200. 'help-echo "Radio target link"
  3201. 'org-linked-text t))
  3202. t)))))
  3203. (defun org-update-radio-target-regexp ()
  3204. "Find all radio targets in this file and update the regular expression."
  3205. (interactive)
  3206. (when (memq 'radio org-activate-links)
  3207. (setq org-target-link-regexp
  3208. (org-make-target-link-regexp (org-all-targets 'radio)))
  3209. (org-restart-font-lock)))
  3210. (defun org-hide-wide-columns (limit)
  3211. (let (s e)
  3212. (setq s (text-property-any (point) (or limit (point-max))
  3213. 'org-cwidth t))
  3214. (when s
  3215. (setq e (next-single-property-change s 'org-cwidth))
  3216. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3217. (goto-char e)
  3218. t)))
  3219. (defvar org-latex-and-specials-regexp nil
  3220. "Regular expression for highlighting export special stuff.")
  3221. (defvar org-match-substring-regexp)
  3222. (defvar org-match-substring-with-braces-regexp)
  3223. (defvar org-export-html-special-string-regexps)
  3224. (defun org-compute-latex-and-specials-regexp ()
  3225. "Compute regular expression for stuff treated specially by exporters."
  3226. (if (not org-highlight-latex-fragments-and-specials)
  3227. (org-set-local 'org-latex-and-specials-regexp nil)
  3228. (require 'org-exp)
  3229. (let*
  3230. ((matchers (plist-get org-format-latex-options :matchers))
  3231. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3232. org-latex-regexps)))
  3233. (options (org-combine-plists (org-default-export-plist)
  3234. (org-infile-export-plist)))
  3235. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3236. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3237. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3238. (org-export-html-expand (plist-get options :expand-quoted-html))
  3239. (org-export-with-special-strings (plist-get options :special-strings))
  3240. (re-sub
  3241. (cond
  3242. ((equal org-export-with-sub-superscripts '{})
  3243. (list org-match-substring-with-braces-regexp))
  3244. (org-export-with-sub-superscripts
  3245. (list org-match-substring-regexp))
  3246. (t nil)))
  3247. (re-latex
  3248. (if org-export-with-LaTeX-fragments
  3249. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3250. (re-macros
  3251. (if org-export-with-TeX-macros
  3252. (list (concat "\\\\"
  3253. (regexp-opt
  3254. (append (mapcar 'car org-html-entities)
  3255. (if (boundp 'org-latex-entities)
  3256. org-latex-entities nil))
  3257. 'words))) ; FIXME
  3258. ))
  3259. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3260. (re-special (if org-export-with-special-strings
  3261. (mapcar (lambda (x) (car x))
  3262. org-export-html-special-string-regexps)))
  3263. (re-rest
  3264. (delq nil
  3265. (list
  3266. (if org-export-html-expand "@<[^>\n]+>")
  3267. ))))
  3268. (org-set-local
  3269. 'org-latex-and-specials-regexp
  3270. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3271. re-rest) "\\|")))))
  3272. (defun org-do-latex-and-special-faces (limit)
  3273. "Run through the buffer and add overlays to links."
  3274. (when org-latex-and-specials-regexp
  3275. (let (rtn d)
  3276. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3277. limit t))
  3278. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3279. 'face))
  3280. '(org-code org-verbatim underline)))
  3281. (progn
  3282. (setq rtn t
  3283. d (cond ((member (char-after (1+ (match-beginning 0)))
  3284. '(?_ ?^)) 1)
  3285. (t 0)))
  3286. (font-lock-prepend-text-property
  3287. (+ d (match-beginning 0)) (match-end 0)
  3288. 'face 'org-latex-and-export-specials)
  3289. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3290. '(font-lock-multiline t)))))
  3291. rtn)))
  3292. (defun org-restart-font-lock ()
  3293. "Restart font-lock-mode, to force refontification."
  3294. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3295. (font-lock-mode -1)
  3296. (font-lock-mode 1)))
  3297. (defun org-all-targets (&optional radio)
  3298. "Return a list of all targets in this file.
  3299. With optional argument RADIO, only find radio targets."
  3300. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3301. rtn)
  3302. (save-excursion
  3303. (goto-char (point-min))
  3304. (while (re-search-forward re nil t)
  3305. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3306. rtn)))
  3307. (defun org-make-target-link-regexp (targets)
  3308. "Make regular expression matching all strings in TARGETS.
  3309. The regular expression finds the targets also if there is a line break
  3310. between words."
  3311. (and targets
  3312. (concat
  3313. "\\<\\("
  3314. (mapconcat
  3315. (lambda (x)
  3316. (while (string-match " +" x)
  3317. (setq x (replace-match "\\s-+" t t x)))
  3318. x)
  3319. targets
  3320. "\\|")
  3321. "\\)\\>")))
  3322. (defun org-activate-tags (limit)
  3323. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3324. (progn
  3325. (add-text-properties (match-beginning 1) (match-end 1)
  3326. (list 'mouse-face 'highlight
  3327. 'rear-nonsticky org-nonsticky-props
  3328. 'keymap org-mouse-map))
  3329. t)))
  3330. (defun org-outline-level ()
  3331. (save-excursion
  3332. (looking-at outline-regexp)
  3333. (if (match-beginning 1)
  3334. (+ (org-get-string-indentation (match-string 1)) 1000)
  3335. (1- (- (match-end 0) (match-beginning 0))))))
  3336. (defvar org-font-lock-keywords nil)
  3337. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3338. "Regular expression matching a property line.")
  3339. (defvar org-font-lock-hook nil
  3340. "Functions to be called for special font lock stuff.")
  3341. (defun org-font-lock-hook (limit)
  3342. (run-hook-with-args 'org-font-lock-hook limit))
  3343. (defun org-set-font-lock-defaults ()
  3344. (let* ((em org-fontify-emphasized-text)
  3345. (lk org-activate-links)
  3346. (org-font-lock-extra-keywords
  3347. (list
  3348. ;; Call the hook
  3349. '(org-font-lock-hook)
  3350. ;; Headlines
  3351. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3352. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3353. ;; Table lines
  3354. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3355. (1 'org-table t))
  3356. ;; Table internals
  3357. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3358. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3359. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3360. ;; Drawers
  3361. (list org-drawer-regexp '(0 'org-special-keyword t))
  3362. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3363. ;; Properties
  3364. (list org-property-re
  3365. '(1 'org-special-keyword t)
  3366. '(3 'org-property-value t))
  3367. (if org-format-transports-properties-p
  3368. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3369. ;; Links
  3370. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3371. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3372. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3373. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3374. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3375. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3376. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3377. '(org-hide-wide-columns (0 nil append))
  3378. ;; TODO lines
  3379. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3380. '(1 (org-get-todo-face 1) t))
  3381. ;; DONE
  3382. (if org-fontify-done-headline
  3383. (list (concat "^[*]+ +\\<\\("
  3384. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3385. "\\)\\(.*\\)")
  3386. '(2 'org-headline-done t))
  3387. nil)
  3388. ;; Priorities
  3389. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3390. ;; Special keywords
  3391. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3392. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3393. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3394. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3395. ;; Emphasis
  3396. (if em
  3397. (if (featurep 'xemacs)
  3398. '(org-do-emphasis-faces (0 nil append))
  3399. '(org-do-emphasis-faces)))
  3400. ;; Checkboxes
  3401. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3402. 2 'bold prepend)
  3403. (if org-provide-checkbox-statistics
  3404. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3405. (0 (org-get-checkbox-statistics-face) t)))
  3406. ;; Description list items
  3407. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3408. 2 'bold prepend)
  3409. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3410. '(1 'org-archived prepend))
  3411. ;; Specials
  3412. '(org-do-latex-and-special-faces)
  3413. ;; Code
  3414. '(org-activate-code (1 'org-code t))
  3415. ;; COMMENT
  3416. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3417. "\\|" org-quote-string "\\)\\>")
  3418. '(1 'org-special-keyword t))
  3419. '("^#.*" (0 'font-lock-comment-face t))
  3420. )))
  3421. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3422. ;; Now set the full font-lock-keywords
  3423. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3424. (org-set-local 'font-lock-defaults
  3425. '(org-font-lock-keywords t nil nil backward-paragraph))
  3426. (kill-local-variable 'font-lock-keywords) nil))
  3427. (defvar org-m nil)
  3428. (defvar org-l nil)
  3429. (defvar org-f nil)
  3430. (defun org-get-level-face (n)
  3431. "Get the right face for match N in font-lock matching of healdines."
  3432. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3433. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3434. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3435. (cond
  3436. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3437. ((eq n 2) org-f)
  3438. (t (if org-level-color-stars-only nil org-f))))
  3439. (defun org-get-todo-face (kwd)
  3440. "Get the right face for a TODO keyword KWD.
  3441. If KWD is a number, get the corresponding match group."
  3442. (if (numberp kwd) (setq kwd (match-string kwd)))
  3443. (or (cdr (assoc kwd org-todo-keyword-faces))
  3444. (and (member kwd org-done-keywords) 'org-done)
  3445. 'org-todo))
  3446. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3447. "Remove fontification and activation overlays from links."
  3448. (font-lock-default-unfontify-region beg end)
  3449. (let* ((buffer-undo-list t)
  3450. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3451. (inhibit-modification-hooks t)
  3452. deactivate-mark buffer-file-name buffer-file-truename)
  3453. (remove-text-properties beg end
  3454. '(mouse-face t keymap t org-linked-text t
  3455. invisible t intangible t))))
  3456. ;;;; Visibility cycling, including org-goto and indirect buffer
  3457. ;;; Cycling
  3458. (defvar org-cycle-global-status nil)
  3459. (make-variable-buffer-local 'org-cycle-global-status)
  3460. (defvar org-cycle-subtree-status nil)
  3461. (make-variable-buffer-local 'org-cycle-subtree-status)
  3462. ;;;###autoload
  3463. (defun org-cycle (&optional arg)
  3464. "Visibility cycling for Org-mode.
  3465. - When this function is called with a prefix argument, rotate the entire
  3466. buffer through 3 states (global cycling)
  3467. 1. OVERVIEW: Show only top-level headlines.
  3468. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3469. 3. SHOW ALL: Show everything.
  3470. When called with two C-u C-u prefixes, switch to the startup visibility,
  3471. determined by the variable `org-startup-folded', and by any VISIBILITY
  3472. properties in the buffer.
  3473. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3474. including drawers.
  3475. - When point is at the beginning of a headline, rotate the subtree started
  3476. by this line through 3 different states (local cycling)
  3477. 1. FOLDED: Only the main headline is shown.
  3478. 2. CHILDREN: The main headline and the direct children are shown.
  3479. From this state, you can move to one of the children
  3480. and zoom in further.
  3481. 3. SUBTREE: Show the entire subtree, including body text.
  3482. - When there is a numeric prefix, go up to a heading with level ARG, do
  3483. a `show-subtree' and return to the previous cursor position. If ARG
  3484. is negative, go up that many levels.
  3485. - When point is not at the beginning of a headline, execute the global
  3486. binding for TAB, which is re-indenting the line. See the option
  3487. `org-cycle-emulate-tab' for details.
  3488. - Special case: if point is at the beginning of the buffer and there is
  3489. no headline in line 1, this function will act as if called with prefix arg.
  3490. But only if also the variable `org-cycle-global-at-bob' is t."
  3491. (interactive "P")
  3492. (org-load-modules-maybe)
  3493. (let* ((outline-regexp
  3494. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3495. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3496. outline-regexp))
  3497. (bob-special (and org-cycle-global-at-bob (bobp)
  3498. (not (looking-at outline-regexp))))
  3499. (org-cycle-hook
  3500. (if bob-special
  3501. (delq 'org-optimize-window-after-visibility-change
  3502. (copy-sequence org-cycle-hook))
  3503. org-cycle-hook))
  3504. (pos (point)))
  3505. (if (or bob-special (equal arg '(4)))
  3506. ;; special case: use global cycling
  3507. (setq arg t))
  3508. (cond
  3509. ((equal arg '(16))
  3510. (org-set-startup-visibility)
  3511. (message "Startup visibility, plus VISIBILITY properties"))
  3512. ((equal arg '(64))
  3513. (show-all)
  3514. (message "Entire buffer visible, including drawers"))
  3515. ((org-at-table-p 'any)
  3516. ;; Enter the table or move to the next field in the table
  3517. (or (org-table-recognize-table.el)
  3518. (progn
  3519. (if arg (org-table-edit-field t)
  3520. (org-table-justify-field-maybe)
  3521. (call-interactively 'org-table-next-field)))))
  3522. ((eq arg t) ;; Global cycling
  3523. (cond
  3524. ((and (eq last-command this-command)
  3525. (eq org-cycle-global-status 'overview))
  3526. ;; We just created the overview - now do table of contents
  3527. ;; This can be slow in very large buffers, so indicate action
  3528. (message "CONTENTS...")
  3529. (org-content)
  3530. (message "CONTENTS...done")
  3531. (setq org-cycle-global-status 'contents)
  3532. (run-hook-with-args 'org-cycle-hook 'contents))
  3533. ((and (eq last-command this-command)
  3534. (eq org-cycle-global-status 'contents))
  3535. ;; We just showed the table of contents - now show everything
  3536. (show-all)
  3537. (message "SHOW ALL")
  3538. (setq org-cycle-global-status 'all)
  3539. (run-hook-with-args 'org-cycle-hook 'all))
  3540. (t
  3541. ;; Default action: go to overview
  3542. (org-overview)
  3543. (message "OVERVIEW")
  3544. (setq org-cycle-global-status 'overview)
  3545. (run-hook-with-args 'org-cycle-hook 'overview))))
  3546. ((and org-drawers org-drawer-regexp
  3547. (save-excursion
  3548. (beginning-of-line 1)
  3549. (looking-at org-drawer-regexp)))
  3550. ;; Toggle block visibility
  3551. (org-flag-drawer
  3552. (not (get-char-property (match-end 0) 'invisible))))
  3553. ((integerp arg)
  3554. ;; Show-subtree, ARG levels up from here.
  3555. (save-excursion
  3556. (org-back-to-heading)
  3557. (outline-up-heading (if (< arg 0) (- arg)
  3558. (- (funcall outline-level) arg)))
  3559. (org-show-subtree)))
  3560. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3561. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3562. ;; At a heading: rotate between three different views
  3563. (org-back-to-heading)
  3564. (let ((goal-column 0) eoh eol eos)
  3565. ;; First, some boundaries
  3566. (save-excursion
  3567. (org-back-to-heading)
  3568. (save-excursion
  3569. (beginning-of-line 2)
  3570. (while (and (not (eobp)) ;; this is like `next-line'
  3571. (get-char-property (1- (point)) 'invisible))
  3572. (beginning-of-line 2)) (setq eol (point)))
  3573. (outline-end-of-heading) (setq eoh (point))
  3574. (org-end-of-subtree t)
  3575. (unless (eobp)
  3576. (skip-chars-forward " \t\n")
  3577. (beginning-of-line 1) ; in case this is an item
  3578. )
  3579. (setq eos (1- (point))))
  3580. ;; Find out what to do next and set `this-command'
  3581. (cond
  3582. ((= eos eoh)
  3583. ;; Nothing is hidden behind this heading
  3584. (message "EMPTY ENTRY")
  3585. (setq org-cycle-subtree-status nil)
  3586. (save-excursion
  3587. (goto-char eos)
  3588. (outline-next-heading)
  3589. (if (org-invisible-p) (org-flag-heading nil))))
  3590. ((or (>= eol eos)
  3591. (not (string-match "\\S-" (buffer-substring eol eos))))
  3592. ;; Entire subtree is hidden in one line: open it
  3593. (org-show-entry)
  3594. (show-children)
  3595. (message "CHILDREN")
  3596. (save-excursion
  3597. (goto-char eos)
  3598. (outline-next-heading)
  3599. (if (org-invisible-p) (org-flag-heading nil)))
  3600. (setq org-cycle-subtree-status 'children)
  3601. (run-hook-with-args 'org-cycle-hook 'children))
  3602. ((and (eq last-command this-command)
  3603. (eq org-cycle-subtree-status 'children))
  3604. ;; We just showed the children, now show everything.
  3605. (org-show-subtree)
  3606. (message "SUBTREE")
  3607. (setq org-cycle-subtree-status 'subtree)
  3608. (run-hook-with-args 'org-cycle-hook 'subtree))
  3609. (t
  3610. ;; Default action: hide the subtree.
  3611. (hide-subtree)
  3612. (message "FOLDED")
  3613. (setq org-cycle-subtree-status 'folded)
  3614. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3615. ;; TAB emulation and template completion
  3616. (buffer-read-only (org-back-to-heading))
  3617. ((org-try-structure-completion))
  3618. ((org-try-cdlatex-tab))
  3619. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3620. (or (not (bolp))
  3621. (not (looking-at outline-regexp))))
  3622. (call-interactively (global-key-binding "\t")))
  3623. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3624. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3625. (or (and (eq org-cycle-emulate-tab 'white)
  3626. (= (match-end 0) (point-at-eol)))
  3627. (and (eq org-cycle-emulate-tab 'whitestart)
  3628. (>= (match-end 0) pos))))
  3629. t
  3630. (eq org-cycle-emulate-tab t))
  3631. (call-interactively (global-key-binding "\t")))
  3632. (t (save-excursion
  3633. (org-back-to-heading)
  3634. (org-cycle))))))
  3635. ;;;###autoload
  3636. (defun org-global-cycle (&optional arg)
  3637. "Cycle the global visibility. For details see `org-cycle'.
  3638. With C-u prefix arg, switch to startup visibility.
  3639. With a numeric prefix, show all headlines up to that level."
  3640. (interactive "P")
  3641. (let ((org-cycle-include-plain-lists
  3642. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3643. (cond
  3644. ((integerp arg)
  3645. (show-all)
  3646. (hide-sublevels arg)
  3647. (setq org-cycle-global-status 'contents))
  3648. ((equal arg '(4))
  3649. (org-set-startup-visibility)
  3650. (message "Startup visibility, plus VISIBILITY properties."))
  3651. (t
  3652. (org-cycle '(4))))))
  3653. (defun org-set-startup-visibility ()
  3654. "Set the visibility required by startup options and properties."
  3655. (cond
  3656. ((eq org-startup-folded t)
  3657. (org-cycle '(4)))
  3658. ((eq org-startup-folded 'content)
  3659. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3660. (org-cycle '(4)) (org-cycle '(4)))))
  3661. (org-set-visibility-according-to-property 'no-cleanup)
  3662. (org-cycle-hide-archived-subtrees 'all)
  3663. (org-cycle-hide-drawers 'all)
  3664. (org-cycle-show-empty-lines 'all))
  3665. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3666. "Switch subtree visibilities according to :VISIBILITY: property."
  3667. (interactive)
  3668. (let (state)
  3669. (save-excursion
  3670. (goto-char (point-min))
  3671. (while (re-search-forward
  3672. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3673. nil t)
  3674. (setq state (match-string 1))
  3675. (save-excursion
  3676. (org-back-to-heading t)
  3677. (hide-subtree)
  3678. (org-reveal)
  3679. (cond
  3680. ((equal state '("fold" "folded"))
  3681. (hide-subtree))
  3682. ((equal state "children")
  3683. (org-show-hidden-entry)
  3684. (show-children))
  3685. ((equal state "content")
  3686. (save-excursion
  3687. (save-restriction
  3688. (org-narrow-to-subtree)
  3689. (org-content))))
  3690. ((member state '("all" "showall"))
  3691. (show-subtree)))))
  3692. (unless no-cleanup
  3693. (org-cycle-hide-archived-subtrees 'all)
  3694. (org-cycle-hide-drawers 'all)
  3695. (org-cycle-show-empty-lines 'all)))))
  3696. (defun org-overview ()
  3697. "Switch to overview mode, shoing only top-level headlines.
  3698. Really, this shows all headlines with level equal or greater than the level
  3699. of the first headline in the buffer. This is important, because if the
  3700. first headline is not level one, then (hide-sublevels 1) gives confusing
  3701. results."
  3702. (interactive)
  3703. (let ((level (save-excursion
  3704. (goto-char (point-min))
  3705. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3706. (progn
  3707. (goto-char (match-beginning 0))
  3708. (funcall outline-level))))))
  3709. (and level (hide-sublevels level))))
  3710. (defun org-content (&optional arg)
  3711. "Show all headlines in the buffer, like a table of contents.
  3712. With numerical argument N, show content up to level N."
  3713. (interactive "P")
  3714. (save-excursion
  3715. ;; Visit all headings and show their offspring
  3716. (and (integerp arg) (org-overview))
  3717. (goto-char (point-max))
  3718. (catch 'exit
  3719. (while (and (progn (condition-case nil
  3720. (outline-previous-visible-heading 1)
  3721. (error (goto-char (point-min))))
  3722. t)
  3723. (looking-at outline-regexp))
  3724. (if (integerp arg)
  3725. (show-children (1- arg))
  3726. (show-branches))
  3727. (if (bobp) (throw 'exit nil))))))
  3728. (defun org-optimize-window-after-visibility-change (state)
  3729. "Adjust the window after a change in outline visibility.
  3730. This function is the default value of the hook `org-cycle-hook'."
  3731. (when (get-buffer-window (current-buffer))
  3732. (cond
  3733. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3734. ; ((eq state 'overview) (org-beginning-of-line))
  3735. ((eq state 'content) nil)
  3736. ((eq state 'all) nil)
  3737. ((eq state 'folded) nil)
  3738. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3739. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3740. (defun org-compact-display-after-subtree-move ()
  3741. (let (beg end)
  3742. (save-excursion
  3743. (if (org-up-heading-safe)
  3744. (progn
  3745. (hide-subtree)
  3746. (show-entry)
  3747. (show-children)
  3748. (org-cycle-show-empty-lines 'children)
  3749. (org-cycle-hide-drawers 'children))
  3750. (org-overview)))))
  3751. (defun org-cycle-show-empty-lines (state)
  3752. "Show empty lines above all visible headlines.
  3753. The region to be covered depends on STATE when called through
  3754. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3755. entire buffer covered. Note that an empty line is only shown if there
  3756. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3757. (when (> org-cycle-separator-lines 0)
  3758. (save-excursion
  3759. (let* ((n org-cycle-separator-lines)
  3760. (re (cond
  3761. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3762. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3763. (t (let ((ns (number-to-string (- n 2))))
  3764. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3765. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3766. beg end)
  3767. (cond
  3768. ((memq state '(overview contents t))
  3769. (setq beg (point-min) end (point-max)))
  3770. ((memq state '(children folded))
  3771. (setq beg (point) end (progn (org-end-of-subtree t t)
  3772. (beginning-of-line 2)
  3773. (point)))))
  3774. (when beg
  3775. (goto-char beg)
  3776. (while (re-search-forward re end t)
  3777. (if (not (get-char-property (match-end 1) 'invisible))
  3778. (outline-flag-region
  3779. (match-beginning 1) (match-end 1) nil)))))))
  3780. ;; Never hide empty lines at the end of the file.
  3781. (save-excursion
  3782. (goto-char (point-max))
  3783. (outline-previous-heading)
  3784. (outline-end-of-heading)
  3785. (if (and (looking-at "[ \t\n]+")
  3786. (= (match-end 0) (point-max)))
  3787. (outline-flag-region (point) (match-end 0) nil))))
  3788. (defun org-show-empty-lines-in-parent ()
  3789. "Move to the parent and re-show empty lines before visible headlines."
  3790. (save-excursion
  3791. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3792. (org-cycle-show-empty-lines context))))
  3793. (defun org-cycle-hide-drawers (state)
  3794. "Re-hide all drawers after a visibility state change."
  3795. (when (and (org-mode-p)
  3796. (not (memq state '(overview folded))))
  3797. (save-excursion
  3798. (let* ((globalp (memq state '(contents all)))
  3799. (beg (if globalp (point-min) (point)))
  3800. (end (if globalp (point-max) (org-end-of-subtree t))))
  3801. (goto-char beg)
  3802. (while (re-search-forward org-drawer-regexp end t)
  3803. (org-flag-drawer t))))))
  3804. (defun org-flag-drawer (flag)
  3805. (save-excursion
  3806. (beginning-of-line 1)
  3807. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3808. (let ((b (match-end 0))
  3809. (outline-regexp org-outline-regexp))
  3810. (if (re-search-forward
  3811. "^[ \t]*:END:"
  3812. (save-excursion (outline-next-heading) (point)) t)
  3813. (outline-flag-region b (point-at-eol) flag)
  3814. (error ":END: line missing"))))))
  3815. (defun org-subtree-end-visible-p ()
  3816. "Is the end of the current subtree visible?"
  3817. (pos-visible-in-window-p
  3818. (save-excursion (org-end-of-subtree t) (point))))
  3819. (defun org-first-headline-recenter (&optional N)
  3820. "Move cursor to the first headline and recenter the headline.
  3821. Optional argument N means, put the headline into the Nth line of the window."
  3822. (goto-char (point-min))
  3823. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3824. (beginning-of-line)
  3825. (recenter (prefix-numeric-value N))))
  3826. ;;; Org-goto
  3827. (defvar org-goto-window-configuration nil)
  3828. (defvar org-goto-marker nil)
  3829. (defvar org-goto-map
  3830. (let ((map (make-sparse-keymap)))
  3831. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3832. (while (setq cmd (pop cmds))
  3833. (substitute-key-definition cmd cmd map global-map)))
  3834. (suppress-keymap map)
  3835. (org-defkey map "\C-m" 'org-goto-ret)
  3836. (org-defkey map [(return)] 'org-goto-ret)
  3837. (org-defkey map [(left)] 'org-goto-left)
  3838. (org-defkey map [(right)] 'org-goto-right)
  3839. (org-defkey map [(control ?g)] 'org-goto-quit)
  3840. (org-defkey map "\C-i" 'org-cycle)
  3841. (org-defkey map [(tab)] 'org-cycle)
  3842. (org-defkey map [(down)] 'outline-next-visible-heading)
  3843. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3844. (if org-goto-auto-isearch
  3845. (if (fboundp 'define-key-after)
  3846. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3847. nil)
  3848. (org-defkey map "q" 'org-goto-quit)
  3849. (org-defkey map "n" 'outline-next-visible-heading)
  3850. (org-defkey map "p" 'outline-previous-visible-heading)
  3851. (org-defkey map "f" 'outline-forward-same-level)
  3852. (org-defkey map "b" 'outline-backward-same-level)
  3853. (org-defkey map "u" 'outline-up-heading))
  3854. (org-defkey map "/" 'org-occur)
  3855. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3856. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3857. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3858. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3859. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3860. map))
  3861. (defconst org-goto-help
  3862. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3863. RET=jump to location [Q]uit and return to previous location
  3864. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3865. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3866. ;; FIXME: Docstring doe not mention both interfaces
  3867. (defun org-goto (&optional alternative-interface)
  3868. "Look up a different location in the current file, keeping current visibility.
  3869. When you want look-up or go to a different location in a document, the
  3870. fastest way is often to fold the entire buffer and then dive into the tree.
  3871. This method has the disadvantage, that the previous location will be folded,
  3872. which may not be what you want.
  3873. This command works around this by showing a copy of the current buffer
  3874. in an indirect buffer, in overview mode. You can dive into the tree in
  3875. that copy, use org-occur and incremental search to find a location.
  3876. When pressing RET or `Q', the command returns to the original buffer in
  3877. which the visibility is still unchanged. After RET is will also jump to
  3878. the location selected in the indirect buffer and expose the
  3879. the headline hierarchy above."
  3880. (interactive "P")
  3881. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3882. (org-refile-use-outline-path t)
  3883. (interface
  3884. (if (not alternative-interface)
  3885. org-goto-interface
  3886. (if (eq org-goto-interface 'outline)
  3887. 'outline-path-completion
  3888. 'outline)))
  3889. (org-goto-start-pos (point))
  3890. (selected-point
  3891. (if (eq interface 'outline)
  3892. (car (org-get-location (current-buffer) org-goto-help))
  3893. (nth 3 (org-refile-get-location "Goto: ")))))
  3894. (if selected-point
  3895. (progn
  3896. (org-mark-ring-push org-goto-start-pos)
  3897. (goto-char selected-point)
  3898. (if (or (org-invisible-p) (org-invisible-p2))
  3899. (org-show-context 'org-goto)))
  3900. (message "Quit"))))
  3901. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3902. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3903. (defvar org-goto-local-auto-isearch-map) ; defined below
  3904. (defun org-get-location (buf help)
  3905. "Let the user select a location in the Org-mode buffer BUF.
  3906. This function uses a recursive edit. It returns the selected position
  3907. or nil."
  3908. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3909. (isearch-hide-immediately nil)
  3910. (isearch-search-fun-function
  3911. (lambda () 'org-goto-local-search-headings))
  3912. (org-goto-selected-point org-goto-exit-command))
  3913. (save-excursion
  3914. (save-window-excursion
  3915. (delete-other-windows)
  3916. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3917. (switch-to-buffer
  3918. (condition-case nil
  3919. (make-indirect-buffer (current-buffer) "*org-goto*")
  3920. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3921. (with-output-to-temp-buffer "*Help*"
  3922. (princ help))
  3923. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3924. (setq buffer-read-only nil)
  3925. (let ((org-startup-truncated t)
  3926. (org-startup-folded nil)
  3927. (org-startup-align-all-tables nil))
  3928. (org-mode)
  3929. (org-overview))
  3930. (setq buffer-read-only t)
  3931. (if (and (boundp 'org-goto-start-pos)
  3932. (integer-or-marker-p org-goto-start-pos))
  3933. (let ((org-show-hierarchy-above t)
  3934. (org-show-siblings t)
  3935. (org-show-following-heading t))
  3936. (goto-char org-goto-start-pos)
  3937. (and (org-invisible-p) (org-show-context)))
  3938. (goto-char (point-min)))
  3939. (org-beginning-of-line)
  3940. (message "Select location and press RET")
  3941. (use-local-map org-goto-map)
  3942. (recursive-edit)
  3943. ))
  3944. (kill-buffer "*org-goto*")
  3945. (cons org-goto-selected-point org-goto-exit-command)))
  3946. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3947. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3948. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3949. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3950. (defun org-goto-local-search-headings (string bound noerror)
  3951. "Search and make sure that any matches are in headlines."
  3952. (catch 'return
  3953. (while (if isearch-forward
  3954. (search-forward string bound noerror)
  3955. (search-backward string bound noerror))
  3956. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3957. (and (member :headline context)
  3958. (not (member :tags context))))
  3959. (throw 'return (point))))))
  3960. (defun org-goto-local-auto-isearch ()
  3961. "Start isearch."
  3962. (interactive)
  3963. (goto-char (point-min))
  3964. (let ((keys (this-command-keys)))
  3965. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3966. (isearch-mode t)
  3967. (isearch-process-search-char (string-to-char keys)))))
  3968. (defun org-goto-ret (&optional arg)
  3969. "Finish `org-goto' by going to the new location."
  3970. (interactive "P")
  3971. (setq org-goto-selected-point (point)
  3972. org-goto-exit-command 'return)
  3973. (throw 'exit nil))
  3974. (defun org-goto-left ()
  3975. "Finish `org-goto' by going to the new location."
  3976. (interactive)
  3977. (if (org-on-heading-p)
  3978. (progn
  3979. (beginning-of-line 1)
  3980. (setq org-goto-selected-point (point)
  3981. org-goto-exit-command 'left)
  3982. (throw 'exit nil))
  3983. (error "Not on a heading")))
  3984. (defun org-goto-right ()
  3985. "Finish `org-goto' by going to the new location."
  3986. (interactive)
  3987. (if (org-on-heading-p)
  3988. (progn
  3989. (setq org-goto-selected-point (point)
  3990. org-goto-exit-command 'right)
  3991. (throw 'exit nil))
  3992. (error "Not on a heading")))
  3993. (defun org-goto-quit ()
  3994. "Finish `org-goto' without cursor motion."
  3995. (interactive)
  3996. (setq org-goto-selected-point nil)
  3997. (setq org-goto-exit-command 'quit)
  3998. (throw 'exit nil))
  3999. ;;; Indirect buffer display of subtrees
  4000. (defvar org-indirect-dedicated-frame nil
  4001. "This is the frame being used for indirect tree display.")
  4002. (defvar org-last-indirect-buffer nil)
  4003. (defun org-tree-to-indirect-buffer (&optional arg)
  4004. "Create indirect buffer and narrow it to current subtree.
  4005. With numerical prefix ARG, go up to this level and then take that tree.
  4006. If ARG is negative, go up that many levels.
  4007. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4008. indirect buffer previously made with this command, to avoid proliferation of
  4009. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4010. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4011. is kept so that you can work with several indirect buffers at the same time.
  4012. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4013. requests that a new frame be made for the new buffer, so that the dedicated
  4014. frame is not changed."
  4015. (interactive "P")
  4016. (let ((cbuf (current-buffer))
  4017. (cwin (selected-window))
  4018. (pos (point))
  4019. beg end level heading ibuf)
  4020. (save-excursion
  4021. (org-back-to-heading t)
  4022. (when (numberp arg)
  4023. (setq level (org-outline-level))
  4024. (if (< arg 0) (setq arg (+ level arg)))
  4025. (while (> (setq level (org-outline-level)) arg)
  4026. (outline-up-heading 1 t)))
  4027. (setq beg (point)
  4028. heading (org-get-heading))
  4029. (org-end-of-subtree t) (setq end (point)))
  4030. (if (and (buffer-live-p org-last-indirect-buffer)
  4031. (not (eq org-indirect-buffer-display 'new-frame))
  4032. (not arg))
  4033. (kill-buffer org-last-indirect-buffer))
  4034. (setq ibuf (org-get-indirect-buffer cbuf)
  4035. org-last-indirect-buffer ibuf)
  4036. (cond
  4037. ((or (eq org-indirect-buffer-display 'new-frame)
  4038. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4039. (select-frame (make-frame))
  4040. (delete-other-windows)
  4041. (switch-to-buffer ibuf)
  4042. (org-set-frame-title heading))
  4043. ((eq org-indirect-buffer-display 'dedicated-frame)
  4044. (raise-frame
  4045. (select-frame (or (and org-indirect-dedicated-frame
  4046. (frame-live-p org-indirect-dedicated-frame)
  4047. org-indirect-dedicated-frame)
  4048. (setq org-indirect-dedicated-frame (make-frame)))))
  4049. (delete-other-windows)
  4050. (switch-to-buffer ibuf)
  4051. (org-set-frame-title (concat "Indirect: " heading)))
  4052. ((eq org-indirect-buffer-display 'current-window)
  4053. (switch-to-buffer ibuf))
  4054. ((eq org-indirect-buffer-display 'other-window)
  4055. (pop-to-buffer ibuf))
  4056. (t (error "Invalid value.")))
  4057. (if (featurep 'xemacs)
  4058. (save-excursion (org-mode) (turn-on-font-lock)))
  4059. (narrow-to-region beg end)
  4060. (show-all)
  4061. (goto-char pos)
  4062. (and (window-live-p cwin) (select-window cwin))))
  4063. (defun org-get-indirect-buffer (&optional buffer)
  4064. (setq buffer (or buffer (current-buffer)))
  4065. (let ((n 1) (base (buffer-name buffer)) bname)
  4066. (while (buffer-live-p
  4067. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4068. (setq n (1+ n)))
  4069. (condition-case nil
  4070. (make-indirect-buffer buffer bname 'clone)
  4071. (error (make-indirect-buffer buffer bname)))))
  4072. (defun org-set-frame-title (title)
  4073. "Set the title of the current frame to the string TITLE."
  4074. ;; FIXME: how to name a single frame in XEmacs???
  4075. (unless (featurep 'xemacs)
  4076. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4077. ;;;; Structure editing
  4078. ;;; Inserting headlines
  4079. (defun org-insert-heading (&optional force-heading)
  4080. "Insert a new heading or item with same depth at point.
  4081. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4082. If point is at the beginning of a headline, insert a sibling before the
  4083. current headline. If point is not at the beginning, do not split the line,
  4084. but create the new hedline after the current line."
  4085. (interactive "P")
  4086. (if (= (buffer-size) 0)
  4087. (insert "\n* ")
  4088. (when (or force-heading (not (org-insert-item)))
  4089. (let* ((head (save-excursion
  4090. (condition-case nil
  4091. (progn
  4092. (org-back-to-heading)
  4093. (match-string 0))
  4094. (error "*"))))
  4095. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4096. pos)
  4097. (cond
  4098. ((and (org-on-heading-p) (bolp)
  4099. (or (bobp)
  4100. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4101. ;; insert before the current line
  4102. (open-line (if blank 2 1)))
  4103. ((and (bolp)
  4104. (or (bobp)
  4105. (save-excursion
  4106. (backward-char 1) (not (org-invisible-p)))))
  4107. ;; insert right here
  4108. nil)
  4109. (t
  4110. ;; in the middle of the line
  4111. (org-show-entry)
  4112. (let ((split
  4113. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4114. tags pos)
  4115. (cond
  4116. (org-insert-heading-respect-content
  4117. (org-end-of-subtree nil t)
  4118. (open-line 1))
  4119. ((org-on-heading-p)
  4120. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4121. (setq tags (and (match-end 2) (match-string 2)))
  4122. (and (match-end 1)
  4123. (delete-region (match-beginning 1) (match-end 1)))
  4124. (setq pos (point-at-bol))
  4125. (or split (end-of-line 1))
  4126. (delete-horizontal-space)
  4127. (newline (if blank 2 1))
  4128. (when tags
  4129. (save-excursion
  4130. (goto-char pos)
  4131. (end-of-line 1)
  4132. (insert " " tags)
  4133. (org-set-tags nil 'align))))
  4134. (t
  4135. (or split (end-of-line 1))
  4136. (newline (if blank 2 1)))))))
  4137. (insert head) (just-one-space)
  4138. (setq pos (point))
  4139. (end-of-line 1)
  4140. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4141. (run-hooks 'org-insert-heading-hook)))))
  4142. (defun org-get-heading (&optional no-tags)
  4143. "Return the heading of the current entry, without the stars."
  4144. (save-excursion
  4145. (org-back-to-heading t)
  4146. (if (looking-at
  4147. (if no-tags
  4148. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4149. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4150. (match-string 1) "")))
  4151. (defun org-insert-heading-after-current ()
  4152. "Insert a new heading with same level as current, after current subtree."
  4153. (interactive)
  4154. (org-back-to-heading)
  4155. (org-insert-heading)
  4156. (org-move-subtree-down)
  4157. (end-of-line 1))
  4158. (defun org-insert-heading-respect-content ()
  4159. (interactive)
  4160. (let ((org-insert-heading-respect-content t))
  4161. (call-interactively 'org-insert-heading)))
  4162. (defun org-insert-todo-heading-respect-content ()
  4163. (interactive)
  4164. (let ((org-insert-heading-respect-content t))
  4165. (call-interactively 'org-insert-todo-todo-heading)))
  4166. (defun org-insert-todo-heading (arg)
  4167. "Insert a new heading with the same level and TODO state as current heading.
  4168. If the heading has no TODO state, or if the state is DONE, use the first
  4169. state (TODO by default). Also with prefix arg, force first state."
  4170. (interactive "P")
  4171. (when (not (org-insert-item 'checkbox))
  4172. (org-insert-heading)
  4173. (save-excursion
  4174. (org-back-to-heading)
  4175. (outline-previous-heading)
  4176. (looking-at org-todo-line-regexp))
  4177. (if (or arg
  4178. (not (match-beginning 2))
  4179. (member (match-string 2) org-done-keywords))
  4180. (insert (car org-todo-keywords-1) " ")
  4181. (insert (match-string 2) " "))
  4182. (when org-provide-todo-statistics
  4183. (org-update-parent-todo-statistics))))
  4184. (defun org-insert-subheading (arg)
  4185. "Insert a new subheading and demote it.
  4186. Works for outline headings and for plain lists alike."
  4187. (interactive "P")
  4188. (org-insert-heading arg)
  4189. (cond
  4190. ((org-on-heading-p) (org-do-demote))
  4191. ((org-at-item-p) (org-indent-item 1))))
  4192. (defun org-insert-todo-subheading (arg)
  4193. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4194. Works for outline headings and for plain lists alike."
  4195. (interactive "P")
  4196. (org-insert-todo-heading arg)
  4197. (cond
  4198. ((org-on-heading-p) (org-do-demote))
  4199. ((org-at-item-p) (org-indent-item 1))))
  4200. ;;; Promotion and Demotion
  4201. (defun org-promote-subtree ()
  4202. "Promote the entire subtree.
  4203. See also `org-promote'."
  4204. (interactive)
  4205. (save-excursion
  4206. (org-map-tree 'org-promote))
  4207. (org-fix-position-after-promote))
  4208. (defun org-demote-subtree ()
  4209. "Demote the entire subtree. See `org-demote'.
  4210. See also `org-promote'."
  4211. (interactive)
  4212. (save-excursion
  4213. (org-map-tree 'org-demote))
  4214. (org-fix-position-after-promote))
  4215. (defun org-do-promote ()
  4216. "Promote the current heading higher up the tree.
  4217. If the region is active in `transient-mark-mode', promote all headings
  4218. in the region."
  4219. (interactive)
  4220. (save-excursion
  4221. (if (org-region-active-p)
  4222. (org-map-region 'org-promote (region-beginning) (region-end))
  4223. (org-promote)))
  4224. (org-fix-position-after-promote))
  4225. (defun org-do-demote ()
  4226. "Demote the current heading lower down the tree.
  4227. If the region is active in `transient-mark-mode', demote all headings
  4228. in the region."
  4229. (interactive)
  4230. (save-excursion
  4231. (if (org-region-active-p)
  4232. (org-map-region 'org-demote (region-beginning) (region-end))
  4233. (org-demote)))
  4234. (org-fix-position-after-promote))
  4235. (defun org-fix-position-after-promote ()
  4236. "Make sure that after pro/demotion cursor position is right."
  4237. (let ((pos (point)))
  4238. (when (save-excursion
  4239. (beginning-of-line 1)
  4240. (looking-at org-todo-line-regexp)
  4241. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4242. (cond ((eobp) (insert " "))
  4243. ((eolp) (insert " "))
  4244. ((equal (char-after) ?\ ) (forward-char 1))))))
  4245. (defun org-reduced-level (l)
  4246. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4247. (defun org-get-valid-level (level &optional change)
  4248. "Rectify a level change under the influence of `org-odd-levels-only'
  4249. LEVEL is a current level, CHANGE is by how much the level should be
  4250. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4251. even level numbers will become the next higher odd number."
  4252. (if org-odd-levels-only
  4253. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4254. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4255. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4256. (max 1 (+ level change))))
  4257. (if (boundp 'define-obsolete-function-alias)
  4258. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4259. (define-obsolete-function-alias 'org-get-legal-level
  4260. 'org-get-valid-level)
  4261. (define-obsolete-function-alias 'org-get-legal-level
  4262. 'org-get-valid-level "23.1")))
  4263. (defun org-promote ()
  4264. "Promote the current heading higher up the tree.
  4265. If the region is active in `transient-mark-mode', promote all headings
  4266. in the region."
  4267. (org-back-to-heading t)
  4268. (let* ((level (save-match-data (funcall outline-level)))
  4269. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4270. (diff (abs (- level (length up-head) -1))))
  4271. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4272. (replace-match up-head nil t)
  4273. ;; Fixup tag positioning
  4274. (and org-auto-align-tags (org-set-tags nil t))
  4275. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4276. (defun org-demote ()
  4277. "Demote the current heading lower down the tree.
  4278. If the region is active in `transient-mark-mode', demote all headings
  4279. in the region."
  4280. (org-back-to-heading t)
  4281. (let* ((level (save-match-data (funcall outline-level)))
  4282. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4283. (diff (abs (- level (length down-head) -1))))
  4284. (replace-match down-head nil t)
  4285. ;; Fixup tag positioning
  4286. (and org-auto-align-tags (org-set-tags nil t))
  4287. (if org-adapt-indentation (org-fixup-indentation diff))))
  4288. (defun org-map-tree (fun)
  4289. "Call FUN for every heading underneath the current one."
  4290. (org-back-to-heading)
  4291. (let ((level (funcall outline-level)))
  4292. (save-excursion
  4293. (funcall fun)
  4294. (while (and (progn
  4295. (outline-next-heading)
  4296. (> (funcall outline-level) level))
  4297. (not (eobp)))
  4298. (funcall fun)))))
  4299. (defun org-map-region (fun beg end)
  4300. "Call FUN for every heading between BEG and END."
  4301. (let ((org-ignore-region t))
  4302. (save-excursion
  4303. (setq end (copy-marker end))
  4304. (goto-char beg)
  4305. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4306. (< (point) end))
  4307. (funcall fun))
  4308. (while (and (progn
  4309. (outline-next-heading)
  4310. (< (point) end))
  4311. (not (eobp)))
  4312. (funcall fun)))))
  4313. (defun org-fixup-indentation (diff)
  4314. "Change the indentation in the current entry by DIFF
  4315. However, if any line in the current entry has no indentation, or if it
  4316. would end up with no indentation after the change, nothing at all is done."
  4317. (save-excursion
  4318. (let ((end (save-excursion (outline-next-heading)
  4319. (point-marker)))
  4320. (prohibit (if (> diff 0)
  4321. "^\\S-"
  4322. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4323. col)
  4324. (unless (save-excursion (end-of-line 1)
  4325. (re-search-forward prohibit end t))
  4326. (while (and (< (point) end)
  4327. (re-search-forward "^[ \t]+" end t))
  4328. (goto-char (match-end 0))
  4329. (setq col (current-column))
  4330. (if (< diff 0) (replace-match ""))
  4331. (indent-to (+ diff col))))
  4332. (move-marker end nil))))
  4333. (defun org-convert-to-odd-levels ()
  4334. "Convert an org-mode file with all levels allowed to one with odd levels.
  4335. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4336. level 5 etc."
  4337. (interactive)
  4338. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4339. (let ((org-odd-levels-only nil) n)
  4340. (save-excursion
  4341. (goto-char (point-min))
  4342. (while (re-search-forward "^\\*\\*+ " nil t)
  4343. (setq n (- (length (match-string 0)) 2))
  4344. (while (>= (setq n (1- n)) 0)
  4345. (org-demote))
  4346. (end-of-line 1))))))
  4347. (defun org-convert-to-oddeven-levels ()
  4348. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4349. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4350. section with an even level, conversion would destroy the structure of the file. An error
  4351. is signaled in this case."
  4352. (interactive)
  4353. (goto-char (point-min))
  4354. ;; First check if there are no even levels
  4355. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4356. (org-show-context t)
  4357. (error "Not all levels are odd in this file. Conversion not possible."))
  4358. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4359. (let ((org-odd-levels-only nil) n)
  4360. (save-excursion
  4361. (goto-char (point-min))
  4362. (while (re-search-forward "^\\*\\*+ " nil t)
  4363. (setq n (/ (1- (length (match-string 0))) 2))
  4364. (while (>= (setq n (1- n)) 0)
  4365. (org-promote))
  4366. (end-of-line 1))))))
  4367. (defun org-tr-level (n)
  4368. "Make N odd if required."
  4369. (if org-odd-levels-only (1+ (/ n 2)) n))
  4370. ;;; Vertical tree motion, cutting and pasting of subtrees
  4371. (defun org-move-subtree-up (&optional arg)
  4372. "Move the current subtree up past ARG headlines of the same level."
  4373. (interactive "p")
  4374. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4375. (defun org-move-subtree-down (&optional arg)
  4376. "Move the current subtree down past ARG headlines of the same level."
  4377. (interactive "p")
  4378. (setq arg (prefix-numeric-value arg))
  4379. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4380. 'outline-get-last-sibling))
  4381. (ins-point (make-marker))
  4382. (cnt (abs arg))
  4383. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4384. ;; Select the tree
  4385. (org-back-to-heading)
  4386. (setq beg0 (point))
  4387. (save-excursion
  4388. (setq ne-beg (org-back-over-empty-lines))
  4389. (setq beg (point)))
  4390. (save-match-data
  4391. (save-excursion (outline-end-of-heading)
  4392. (setq folded (org-invisible-p)))
  4393. (outline-end-of-subtree))
  4394. (outline-next-heading)
  4395. (setq ne-end (org-back-over-empty-lines))
  4396. (setq end (point))
  4397. (goto-char beg0)
  4398. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4399. ;; include less whitespace
  4400. (save-excursion
  4401. (goto-char beg)
  4402. (forward-line (- ne-beg ne-end))
  4403. (setq beg (point))))
  4404. ;; Find insertion point, with error handling
  4405. (while (> cnt 0)
  4406. (or (and (funcall movfunc) (looking-at outline-regexp))
  4407. (progn (goto-char beg0)
  4408. (error "Cannot move past superior level or buffer limit")))
  4409. (setq cnt (1- cnt)))
  4410. (if (> arg 0)
  4411. ;; Moving forward - still need to move over subtree
  4412. (progn (org-end-of-subtree t t)
  4413. (save-excursion
  4414. (org-back-over-empty-lines)
  4415. (or (bolp) (newline)))))
  4416. (setq ne-ins (org-back-over-empty-lines))
  4417. (move-marker ins-point (point))
  4418. (setq txt (buffer-substring beg end))
  4419. (org-save-markers-in-region beg end)
  4420. (delete-region beg end)
  4421. (outline-flag-region (1- beg) beg nil)
  4422. (outline-flag-region (1- (point)) (point) nil)
  4423. (let ((bbb (point)))
  4424. (insert-before-markers txt)
  4425. (org-reinstall-markers-in-region bbb)
  4426. (move-marker ins-point bbb))
  4427. (or (bolp) (insert "\n"))
  4428. (setq ins-end (point))
  4429. (goto-char ins-point)
  4430. (org-skip-whitespace)
  4431. (when (and (< arg 0)
  4432. (org-first-sibling-p)
  4433. (> ne-ins ne-beg))
  4434. ;; Move whitespace back to beginning
  4435. (save-excursion
  4436. (goto-char ins-end)
  4437. (let ((kill-whole-line t))
  4438. (kill-line (- ne-ins ne-beg)) (point)))
  4439. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4440. (move-marker ins-point nil)
  4441. (org-compact-display-after-subtree-move)
  4442. (org-show-empty-lines-in-parent)
  4443. (unless folded
  4444. (org-show-entry)
  4445. (show-children)
  4446. (org-cycle-hide-drawers 'children))))
  4447. (defvar org-subtree-clip ""
  4448. "Clipboard for cut and paste of subtrees.
  4449. This is actually only a copy of the kill, because we use the normal kill
  4450. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4451. (defvar org-subtree-clip-folded nil
  4452. "Was the last copied subtree folded?
  4453. This is used to fold the tree back after pasting.")
  4454. (defun org-cut-subtree (&optional n)
  4455. "Cut the current subtree into the clipboard.
  4456. With prefix arg N, cut this many sequential subtrees.
  4457. This is a short-hand for marking the subtree and then cutting it."
  4458. (interactive "p")
  4459. (org-copy-subtree n 'cut))
  4460. (defun org-copy-subtree (&optional n cut force-store-markers)
  4461. "Cut the current subtree into the clipboard.
  4462. With prefix arg N, cut this many sequential subtrees.
  4463. This is a short-hand for marking the subtree and then copying it.
  4464. If CUT is non-nil, actually cut the subtree.
  4465. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4466. of some markers in the region, even if CUT is non-nil. This is
  4467. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4468. (interactive "p")
  4469. (let (beg end folded (beg0 (point)))
  4470. (if (interactive-p)
  4471. (org-back-to-heading nil) ; take what looks like a subtree
  4472. (org-back-to-heading t)) ; take what is really there
  4473. (org-back-over-empty-lines)
  4474. (setq beg (point))
  4475. (skip-chars-forward " \t\r\n")
  4476. (save-match-data
  4477. (save-excursion (outline-end-of-heading)
  4478. (setq folded (org-invisible-p)))
  4479. (condition-case nil
  4480. (outline-forward-same-level (1- n))
  4481. (error nil))
  4482. (org-end-of-subtree t t))
  4483. (org-back-over-empty-lines)
  4484. (setq end (point))
  4485. (goto-char beg0)
  4486. (when (> end beg)
  4487. (setq org-subtree-clip-folded folded)
  4488. (when (or cut force-store-markers)
  4489. (org-save-markers-in-region beg end))
  4490. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4491. (setq org-subtree-clip (current-kill 0))
  4492. (message "%s: Subtree(s) with %d characters"
  4493. (if cut "Cut" "Copied")
  4494. (length org-subtree-clip)))))
  4495. (defun org-paste-subtree (&optional level tree)
  4496. "Paste the clipboard as a subtree, with modification of headline level.
  4497. The entire subtree is promoted or demoted in order to match a new headline
  4498. level. By default, the new level is derived from the visible headings
  4499. before and after the insertion point, and taken to be the inferior headline
  4500. level of the two. So if the previous visible heading is level 3 and the
  4501. next is level 4 (or vice versa), level 4 will be used for insertion.
  4502. This makes sure that the subtree remains an independent subtree and does
  4503. not swallow low level entries.
  4504. You can also force a different level, either by using a numeric prefix
  4505. argument, or by inserting the heading marker by hand. For example, if the
  4506. cursor is after \"*****\", then the tree will be shifted to level 5.
  4507. If you want to insert the tree as is, just use \\[yank].
  4508. If optional TREE is given, use this text instead of the kill ring."
  4509. (interactive "P")
  4510. (unless (org-kill-is-subtree-p tree)
  4511. (error "%s"
  4512. (substitute-command-keys
  4513. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4514. (let* ((visp (not (org-invisible-p)))
  4515. (txt (or tree (and kill-ring (current-kill 0))))
  4516. (^re (concat "^\\(" outline-regexp "\\)"))
  4517. (re (concat "\\(" outline-regexp "\\)"))
  4518. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4519. (old-level (if (string-match ^re txt)
  4520. (- (match-end 0) (match-beginning 0) 1)
  4521. -1))
  4522. (force-level (cond (level (prefix-numeric-value level))
  4523. ((string-match
  4524. ^re_ (buffer-substring (point-at-bol) (point)))
  4525. (- (match-end 1) (match-beginning 1)))
  4526. (t nil)))
  4527. (previous-level (save-excursion
  4528. (condition-case nil
  4529. (progn
  4530. (outline-previous-visible-heading 1)
  4531. (if (looking-at re)
  4532. (- (match-end 0) (match-beginning 0) 1)
  4533. 1))
  4534. (error 1))))
  4535. (next-level (save-excursion
  4536. (condition-case nil
  4537. (progn
  4538. (or (looking-at outline-regexp)
  4539. (outline-next-visible-heading 1))
  4540. (if (looking-at re)
  4541. (- (match-end 0) (match-beginning 0) 1)
  4542. 1))
  4543. (error 1))))
  4544. (new-level (or force-level (max previous-level next-level)))
  4545. (shift (if (or (= old-level -1)
  4546. (= new-level -1)
  4547. (= old-level new-level))
  4548. 0
  4549. (- new-level old-level)))
  4550. (delta (if (> shift 0) -1 1))
  4551. (func (if (> shift 0) 'org-demote 'org-promote))
  4552. (org-odd-levels-only nil)
  4553. beg end)
  4554. ;; Remove the forced level indicator
  4555. (if force-level
  4556. (delete-region (point-at-bol) (point)))
  4557. ;; Paste
  4558. (beginning-of-line 1)
  4559. (org-back-over-empty-lines)
  4560. (setq beg (point))
  4561. (insert-before-markers txt)
  4562. (unless (string-match "\n\\'" txt) (insert "\n"))
  4563. (org-reinstall-markers-in-region beg)
  4564. (setq end (point))
  4565. (goto-char beg)
  4566. (skip-chars-forward " \t\n\r")
  4567. (setq beg (point))
  4568. (if (and (org-invisible-p) visp)
  4569. (save-excursion (outline-show-heading)))
  4570. ;; Shift if necessary
  4571. (unless (= shift 0)
  4572. (save-restriction
  4573. (narrow-to-region beg end)
  4574. (while (not (= shift 0))
  4575. (org-map-region func (point-min) (point-max))
  4576. (setq shift (+ delta shift)))
  4577. (goto-char (point-min))))
  4578. (when (interactive-p)
  4579. (message "Clipboard pasted as level %d subtree" new-level))
  4580. (if (and kill-ring
  4581. (eq org-subtree-clip (current-kill 0))
  4582. org-subtree-clip-folded)
  4583. ;; The tree was folded before it was killed/copied
  4584. (hide-subtree))))
  4585. (defun org-kill-is-subtree-p (&optional txt)
  4586. "Check if the current kill is an outline subtree, or a set of trees.
  4587. Returns nil if kill does not start with a headline, or if the first
  4588. headline level is not the largest headline level in the tree.
  4589. So this will actually accept several entries of equal levels as well,
  4590. which is OK for `org-paste-subtree'.
  4591. If optional TXT is given, check this string instead of the current kill."
  4592. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4593. (start-level (and kill
  4594. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4595. org-outline-regexp "\\)")
  4596. kill)
  4597. (- (match-end 2) (match-beginning 2) 1)))
  4598. (re (concat "^" org-outline-regexp))
  4599. (start (1+ (or (match-beginning 2) -1))))
  4600. (if (not start-level)
  4601. (progn
  4602. nil) ;; does not even start with a heading
  4603. (catch 'exit
  4604. (while (setq start (string-match re kill (1+ start)))
  4605. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4606. (throw 'exit nil)))
  4607. t))))
  4608. (defvar org-markers-to-move nil
  4609. "Markers that should be moved with a cut-and-paste operation.
  4610. Those markers are stored together with their positions relative to
  4611. the start of the region.")
  4612. (defun org-save-markers-in-region (beg end)
  4613. "Check markers in region.
  4614. If these markers are between BEG and END, record their position relative
  4615. to BEG, so that after moving the block of text, we can put the markers back
  4616. into place.
  4617. This function gets called just before an entry or tree gets cut from the
  4618. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4619. called immediately, to move the markers with the entries."
  4620. (setq org-markers-to-move nil)
  4621. (when (featurep 'org-clock)
  4622. (org-clock-save-markers-for-cut-and-paste beg end))
  4623. (when (featurep 'org-agenda)
  4624. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4625. (defun org-check-and-save-marker (marker beg end)
  4626. "Check if MARKER is between BEG and END.
  4627. If yes, remember the marker and the distance to BEG."
  4628. (when (and (marker-buffer marker)
  4629. (equal (marker-buffer marker) (current-buffer)))
  4630. (if (and (>= marker beg) (< marker end))
  4631. (push (cons marker (- marker beg)) org-markers-to-move))))
  4632. (defun org-reinstall-markers-in-region (beg)
  4633. "Move all remembered markers to their position relative to BEG."
  4634. (mapc (lambda (x)
  4635. (move-marker (car x) (+ beg (cdr x))))
  4636. org-markers-to-move)
  4637. (setq org-markers-to-move nil))
  4638. (defun org-narrow-to-subtree ()
  4639. "Narrow buffer to the current subtree."
  4640. (interactive)
  4641. (save-excursion
  4642. (save-match-data
  4643. (narrow-to-region
  4644. (progn (org-back-to-heading) (point))
  4645. (progn (org-end-of-subtree t) (point))))))
  4646. ;;; Outline Sorting
  4647. (defun org-sort (with-case)
  4648. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4649. Optional argument WITH-CASE means sort case-sensitively."
  4650. (interactive "P")
  4651. (if (org-at-table-p)
  4652. (org-call-with-arg 'org-table-sort-lines with-case)
  4653. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4654. (defun org-sort-remove-invisible (s)
  4655. (remove-text-properties 0 (length s) org-rm-props s)
  4656. (while (string-match org-bracket-link-regexp s)
  4657. (setq s (replace-match (if (match-end 2)
  4658. (match-string 3 s)
  4659. (match-string 1 s)) t t s)))
  4660. s)
  4661. (defvar org-priority-regexp) ; defined later in the file
  4662. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4663. "Sort entries on a certain level of an outline tree.
  4664. If there is an active region, the entries in the region are sorted.
  4665. Else, if the cursor is before the first entry, sort the top-level items.
  4666. Else, the children of the entry at point are sorted.
  4667. Sorting can be alphabetically, numerically, and by date/time as given by
  4668. the first time stamp in the entry. The command prompts for the sorting
  4669. type unless it has been given to the function through the SORTING-TYPE
  4670. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4671. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4672. called with point at the beginning of the record. It must return either
  4673. a string or a number that should serve as the sorting key for that record.
  4674. Comparing entries ignores case by default. However, with an optional argument
  4675. WITH-CASE, the sorting considers case as well."
  4676. (interactive "P")
  4677. (let ((case-func (if with-case 'identity 'downcase))
  4678. start beg end stars re re2
  4679. txt what tmp plain-list-p)
  4680. ;; Find beginning and end of region to sort
  4681. (cond
  4682. ((org-region-active-p)
  4683. ;; we will sort the region
  4684. (setq end (region-end)
  4685. what "region")
  4686. (goto-char (region-beginning))
  4687. (if (not (org-on-heading-p)) (outline-next-heading))
  4688. (setq start (point)))
  4689. ((org-at-item-p)
  4690. ;; we will sort this plain list
  4691. (org-beginning-of-item-list) (setq start (point))
  4692. (org-end-of-item-list) (setq end (point))
  4693. (goto-char start)
  4694. (setq plain-list-p t
  4695. what "plain list"))
  4696. ((or (org-on-heading-p)
  4697. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4698. ;; we will sort the children of the current headline
  4699. (org-back-to-heading)
  4700. (setq start (point)
  4701. end (progn (org-end-of-subtree t t)
  4702. (org-back-over-empty-lines)
  4703. (point))
  4704. what "children")
  4705. (goto-char start)
  4706. (show-subtree)
  4707. (outline-next-heading))
  4708. (t
  4709. ;; we will sort the top-level entries in this file
  4710. (goto-char (point-min))
  4711. (or (org-on-heading-p) (outline-next-heading))
  4712. (setq start (point) end (point-max) what "top-level")
  4713. (goto-char start)
  4714. (show-all)))
  4715. (setq beg (point))
  4716. (if (>= beg end) (error "Nothing to sort"))
  4717. (unless plain-list-p
  4718. (looking-at "\\(\\*+\\)")
  4719. (setq stars (match-string 1)
  4720. re (concat "^" (regexp-quote stars) " +")
  4721. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4722. txt (buffer-substring beg end))
  4723. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4724. (if (and (not (equal stars "*")) (string-match re2 txt))
  4725. (error "Region to sort contains a level above the first entry")))
  4726. (unless sorting-type
  4727. (message
  4728. (if plain-list-p
  4729. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4730. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  4731. what)
  4732. (setq sorting-type (read-char-exclusive))
  4733. (and (= (downcase sorting-type) ?f)
  4734. (setq getkey-func
  4735. (completing-read "Sort using function: "
  4736. obarray 'fboundp t nil nil))
  4737. (setq getkey-func (intern getkey-func)))
  4738. (and (= (downcase sorting-type) ?r)
  4739. (setq property
  4740. (completing-read "Property: "
  4741. (mapcar 'list (org-buffer-property-keys t))
  4742. nil t))))
  4743. (message "Sorting entries...")
  4744. (save-restriction
  4745. (narrow-to-region start end)
  4746. (let ((dcst (downcase sorting-type))
  4747. (now (current-time)))
  4748. (sort-subr
  4749. (/= dcst sorting-type)
  4750. ;; This function moves to the beginning character of the "record" to
  4751. ;; be sorted.
  4752. (if plain-list-p
  4753. (lambda nil
  4754. (if (org-at-item-p) t (goto-char (point-max))))
  4755. (lambda nil
  4756. (if (re-search-forward re nil t)
  4757. (goto-char (match-beginning 0))
  4758. (goto-char (point-max)))))
  4759. ;; This function moves to the last character of the "record" being
  4760. ;; sorted.
  4761. (if plain-list-p
  4762. 'org-end-of-item
  4763. (lambda nil
  4764. (save-match-data
  4765. (condition-case nil
  4766. (outline-forward-same-level 1)
  4767. (error
  4768. (goto-char (point-max)))))))
  4769. ;; This function returns the value that gets sorted against.
  4770. (if plain-list-p
  4771. (lambda nil
  4772. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4773. (cond
  4774. ((= dcst ?n)
  4775. (string-to-number (buffer-substring (match-end 0)
  4776. (point-at-eol))))
  4777. ((= dcst ?a)
  4778. (buffer-substring (match-end 0) (point-at-eol)))
  4779. ((= dcst ?t)
  4780. (if (re-search-forward org-ts-regexp
  4781. (point-at-eol) t)
  4782. (org-time-string-to-time (match-string 0))
  4783. now))
  4784. ((= dcst ?f)
  4785. (if getkey-func
  4786. (progn
  4787. (setq tmp (funcall getkey-func))
  4788. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4789. tmp)
  4790. (error "Invalid key function `%s'" getkey-func)))
  4791. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4792. (lambda nil
  4793. (cond
  4794. ((= dcst ?n)
  4795. (if (looking-at org-complex-heading-regexp)
  4796. (string-to-number (match-string 4))
  4797. nil))
  4798. ((= dcst ?a)
  4799. (if (looking-at org-complex-heading-regexp)
  4800. (funcall case-func (match-string 4))
  4801. nil))
  4802. ((= dcst ?t)
  4803. (if (re-search-forward org-ts-regexp
  4804. (save-excursion
  4805. (forward-line 2)
  4806. (point)) t)
  4807. (org-time-string-to-time (match-string 0))
  4808. now))
  4809. ((= dcst ?p)
  4810. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4811. (string-to-char (match-string 2))
  4812. org-default-priority))
  4813. ((= dcst ?r)
  4814. (or (org-entry-get nil property) ""))
  4815. ((= dcst ?o)
  4816. (if (looking-at org-complex-heading-regexp)
  4817. (- 9999 (length (member (match-string 2)
  4818. org-todo-keywords-1)))))
  4819. ((= dcst ?f)
  4820. (if getkey-func
  4821. (progn
  4822. (setq tmp (funcall getkey-func))
  4823. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4824. tmp)
  4825. (error "Invalid key function `%s'" getkey-func)))
  4826. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4827. nil
  4828. (cond
  4829. ((= dcst ?a) 'string<)
  4830. ((= dcst ?t) 'time-less-p)
  4831. (t nil)))))
  4832. (message "Sorting entries...done")))
  4833. (defun org-do-sort (table what &optional with-case sorting-type)
  4834. "Sort TABLE of WHAT according to SORTING-TYPE.
  4835. The user will be prompted for the SORTING-TYPE if the call to this
  4836. function does not specify it. WHAT is only for the prompt, to indicate
  4837. what is being sorted. The sorting key will be extracted from
  4838. the car of the elements of the table.
  4839. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4840. (unless sorting-type
  4841. (message
  4842. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4843. what)
  4844. (setq sorting-type (read-char-exclusive)))
  4845. (let ((dcst (downcase sorting-type))
  4846. extractfun comparefun)
  4847. ;; Define the appropriate functions
  4848. (cond
  4849. ((= dcst ?n)
  4850. (setq extractfun 'string-to-number
  4851. comparefun (if (= dcst sorting-type) '< '>)))
  4852. ((= dcst ?a)
  4853. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4854. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4855. comparefun (if (= dcst sorting-type)
  4856. 'string<
  4857. (lambda (a b) (and (not (string< a b))
  4858. (not (string= a b)))))))
  4859. ((= dcst ?t)
  4860. (setq extractfun
  4861. (lambda (x)
  4862. (if (string-match org-ts-regexp x)
  4863. (time-to-seconds
  4864. (org-time-string-to-time (match-string 0 x)))
  4865. 0))
  4866. comparefun (if (= dcst sorting-type) '< '>)))
  4867. (t (error "Invalid sorting type `%c'" sorting-type)))
  4868. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4869. table)
  4870. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4871. ;;; Editing source examples
  4872. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4873. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4874. (defvar org-edit-src-force-single-line nil)
  4875. (defvar org-edit-src-from-org-mode nil)
  4876. (defvar org-edit-src-picture nil)
  4877. (define-minor-mode org-exit-edit-mode
  4878. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4879. (defun org-edit-src-code ()
  4880. "Edit the source code example at point.
  4881. An indirect buffer is created, and that buffer is then narrowed to the
  4882. example at point and switched to the correct language mode. When done,
  4883. exit by killing the buffer with \\[org-edit-src-exit]."
  4884. (interactive)
  4885. (let ((line (org-current-line))
  4886. (case-fold-search t)
  4887. (msg (substitute-command-keys
  4888. "Edit, then exit with C-c ' (C-c and single quote)"))
  4889. (info (org-edit-src-find-region-and-lang))
  4890. (org-mode-p (eq major-mode 'org-mode))
  4891. beg end lang lang-f single)
  4892. (if (not info)
  4893. nil
  4894. (setq beg (nth 0 info)
  4895. end (nth 1 info)
  4896. lang (nth 2 info)
  4897. single (nth 3 info)
  4898. lang-f (intern (concat lang "-mode")))
  4899. (unless (functionp lang-f)
  4900. (error "No such language mode: %s" lang-f))
  4901. (goto-line line)
  4902. (if (get-buffer "*Org Edit Src Example*")
  4903. (kill-buffer "*Org Edit Src Example*"))
  4904. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4905. "*Org Edit Src Example*"))
  4906. (narrow-to-region beg end)
  4907. (remove-text-properties beg end '(display nil invisible nil
  4908. intangible nil))
  4909. (let ((org-inhibit-startup t))
  4910. (funcall lang-f))
  4911. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4912. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4913. (when org-mode-p
  4914. (goto-char (point-min))
  4915. (while (re-search-forward "^," nil t)
  4916. (replace-match "")))
  4917. (goto-line line)
  4918. (org-exit-edit-mode)
  4919. (org-set-local 'header-line-format msg)
  4920. (message "%s" msg)
  4921. t)))
  4922. (defun org-edit-fixed-width-region ()
  4923. "Edit the fixed-width ascii drawing at point.
  4924. This must be a region where each line starts with ca colon followed by
  4925. a space character.
  4926. An indirect buffer is created, and that buffer is then narrowed to the
  4927. example at point and switched to artist-mode. When done,
  4928. exit by killing the buffer with \\[org-edit-src-exit]."
  4929. (interactive)
  4930. (let ((line (org-current-line))
  4931. (case-fold-search t)
  4932. (msg (substitute-command-keys
  4933. "Edit, then exit with C-c ' (C-c and single quote)"))
  4934. (org-mode-p (eq major-mode 'org-mode))
  4935. beg end lang lang-f)
  4936. (beginning-of-line 1)
  4937. (if (looking-at "[ \t]*[^:\n \t]")
  4938. nil
  4939. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  4940. (setq beg (point) end (match-end 0))
  4941. (save-excursion
  4942. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  4943. (setq beg (point-at-bol 2))
  4944. (setq beg (point))))
  4945. (save-excursion
  4946. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  4947. (setq end (match-beginning 0))
  4948. (setq end (point))))
  4949. (goto-line line)
  4950. (if (get-buffer "*Org Edit Picture*")
  4951. (kill-buffer "*Org Edit Picture*"))
  4952. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4953. "*Org Edit Picture*"))
  4954. (narrow-to-region beg end)
  4955. (remove-text-properties beg end '(display nil invisible nil
  4956. intangible nil))
  4957. (when (fboundp 'font-lock-unfontify-region)
  4958. (font-lock-unfontify-region (point-min) (point-max)))
  4959. (cond
  4960. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  4961. (fundamental-mode)
  4962. (artist-mode 1))
  4963. (t (funcall org-edit-fixed-width-region-mode)))
  4964. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  4965. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4966. (set (make-local-variable 'org-edit-src-picture) t)
  4967. (goto-char (point-min))
  4968. (while (re-search-forward "^[ \t]*: " nil t)
  4969. (replace-match ""))
  4970. (goto-line line)
  4971. (org-exit-edit-mode)
  4972. (org-set-local 'header-line-format msg)
  4973. (message "%s" msg)
  4974. t))))
  4975. (defun org-edit-src-find-region-and-lang ()
  4976. "Find the region and language for a local edit.
  4977. Return a list with beginning and end of the region, a string representing
  4978. the language, a switch telling of the content should be in a single line."
  4979. (let ((re-list
  4980. (append
  4981. org-edit-src-region-extra
  4982. '(
  4983. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  4984. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  4985. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  4986. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  4987. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  4988. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  4989. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  4990. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  4991. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  4992. ("^#\\+html:" "\n" "html" single-line)
  4993. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  4994. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  4995. ("^#\\+latex:" "\n" "latex" single-line)
  4996. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  4997. ("^#\\+ascii:" "\n" "ascii" single-line)
  4998. )))
  4999. (pos (point))
  5000. re re1 re2 single beg end lang)
  5001. (catch 'exit
  5002. (while (setq entry (pop re-list))
  5003. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5004. single (nth 3 entry))
  5005. (save-excursion
  5006. (if (or (looking-at re1)
  5007. (re-search-backward re1 nil t))
  5008. (progn
  5009. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5010. (if (and (re-search-forward re2 nil t)
  5011. (>= (match-end 0) pos))
  5012. (throw 'exit (list beg (match-beginning 0) lang single))))
  5013. (if (or (looking-at re2)
  5014. (re-search-forward re2 nil t))
  5015. (progn
  5016. (setq end (match-beginning 0))
  5017. (if (and (re-search-backward re1 nil t)
  5018. (<= (match-beginning 0) pos))
  5019. (throw 'exit
  5020. (list (match-end 0) end
  5021. (org-edit-src-get-lang lang) single)))))))))))
  5022. (defun org-edit-src-get-lang (lang)
  5023. "Extract the src language."
  5024. (let ((m (match-string 0)))
  5025. (cond
  5026. ((stringp lang) lang)
  5027. ((integerp lang) (match-string lang))
  5028. ((and (eq lang 'lang)
  5029. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5030. (match-string 1 m))
  5031. ((and (eq lang 'style)
  5032. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5033. (match-string 1 m))
  5034. (t "fundamental"))))
  5035. (defun org-edit-src-exit ()
  5036. "Exit special edit and protect problematic lines."
  5037. (interactive)
  5038. (unless (buffer-base-buffer (current-buffer))
  5039. (error "This is not an indirect buffer, something is wrong..."))
  5040. (unless (> (point-min) 1)
  5041. (error "This buffer is not narrowed, something is wrong..."))
  5042. (goto-char (point-min))
  5043. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5044. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5045. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5046. (goto-char (point-min))
  5047. (while (re-search-forward "\n" nil t)
  5048. (replace-match " "))
  5049. (goto-char (point-min))
  5050. (if (looking-at "\\s-*") (replace-match " "))
  5051. (if (re-search-forward "\\s-+\\'" nil t)
  5052. (replace-match "")))
  5053. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5054. (goto-char (point-min))
  5055. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5056. (replace-match ",\\1"))
  5057. (when font-lock-mode
  5058. (font-lock-unfontify-region (point-min) (point-max)))
  5059. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5060. (when (org-bound-and-true-p org-edit-src-picture)
  5061. (goto-char (point-min))
  5062. (while (re-search-forward "^" nil t)
  5063. (replace-match ": "))
  5064. (when font-lock-mode
  5065. (font-lock-unfontify-region (point-min) (point-max)))
  5066. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5067. (kill-buffer (current-buffer))
  5068. (and (org-mode-p) (org-restart-font-lock)))
  5069. ;;; The orgstruct minor mode
  5070. ;; Define a minor mode which can be used in other modes in order to
  5071. ;; integrate the org-mode structure editing commands.
  5072. ;; This is really a hack, because the org-mode structure commands use
  5073. ;; keys which normally belong to the major mode. Here is how it
  5074. ;; works: The minor mode defines all the keys necessary to operate the
  5075. ;; structure commands, but wraps the commands into a function which
  5076. ;; tests if the cursor is currently at a headline or a plain list
  5077. ;; item. If that is the case, the structure command is used,
  5078. ;; temporarily setting many Org-mode variables like regular
  5079. ;; expressions for filling etc. However, when any of those keys is
  5080. ;; used at a different location, function uses `key-binding' to look
  5081. ;; up if the key has an associated command in another currently active
  5082. ;; keymap (minor modes, major mode, global), and executes that
  5083. ;; command. There might be problems if any of the keys is otherwise
  5084. ;; used as a prefix key.
  5085. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5086. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5087. ;; addresses this by checking explicitly for both bindings.
  5088. (defvar orgstruct-mode-map (make-sparse-keymap)
  5089. "Keymap for the minor `orgstruct-mode'.")
  5090. (defvar org-local-vars nil
  5091. "List of local variables, for use by `orgstruct-mode'")
  5092. ;;;###autoload
  5093. (define-minor-mode orgstruct-mode
  5094. "Toggle the minor more `orgstruct-mode'.
  5095. This mode is for using Org-mode structure commands in other modes.
  5096. The following key behave as if Org-mode was active, if the cursor
  5097. is on a headline, or on a plain list item (both in the definition
  5098. of Org-mode).
  5099. M-up Move entry/item up
  5100. M-down Move entry/item down
  5101. M-left Promote
  5102. M-right Demote
  5103. M-S-up Move entry/item up
  5104. M-S-down Move entry/item down
  5105. M-S-left Promote subtree
  5106. M-S-right Demote subtree
  5107. M-q Fill paragraph and items like in Org-mode
  5108. C-c ^ Sort entries
  5109. C-c - Cycle list bullet
  5110. TAB Cycle item visibility
  5111. M-RET Insert new heading/item
  5112. S-M-RET Insert new TODO heading / Chekbox item
  5113. C-c C-c Set tags / toggle checkbox"
  5114. nil " OrgStruct" nil
  5115. (org-load-modules-maybe)
  5116. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5117. ;;;###autoload
  5118. (defun turn-on-orgstruct ()
  5119. "Unconditionally turn on `orgstruct-mode'."
  5120. (orgstruct-mode 1))
  5121. ;;;###autoload
  5122. (defun turn-on-orgstruct++ ()
  5123. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5124. In addition to setting orgstruct-mode, this also exports all indentation and
  5125. autofilling variables from org-mode into the buffer. Note that turning
  5126. off orgstruct-mode will *not* remove these additional settings."
  5127. (orgstruct-mode 1)
  5128. (let (var val)
  5129. (mapc
  5130. (lambda (x)
  5131. (when (string-match
  5132. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5133. (symbol-name (car x)))
  5134. (setq var (car x) val (nth 1 x))
  5135. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5136. org-local-vars)))
  5137. (defun orgstruct-error ()
  5138. "Error when there is no default binding for a structure key."
  5139. (interactive)
  5140. (error "This key has no function outside structure elements"))
  5141. (defun orgstruct-setup ()
  5142. "Setup orgstruct keymaps."
  5143. (let ((nfunc 0)
  5144. (bindings
  5145. (list
  5146. '([(meta up)] org-metaup)
  5147. '([(meta down)] org-metadown)
  5148. '([(meta left)] org-metaleft)
  5149. '([(meta right)] org-metaright)
  5150. '([(meta shift up)] org-shiftmetaup)
  5151. '([(meta shift down)] org-shiftmetadown)
  5152. '([(meta shift left)] org-shiftmetaleft)
  5153. '([(meta shift right)] org-shiftmetaright)
  5154. '([(shift up)] org-shiftup)
  5155. '([(shift down)] org-shiftdown)
  5156. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5157. '("\M-q" fill-paragraph)
  5158. '("\C-c^" org-sort)
  5159. '("\C-c-" org-cycle-list-bullet)))
  5160. elt key fun cmd)
  5161. (while (setq elt (pop bindings))
  5162. (setq nfunc (1+ nfunc))
  5163. (setq key (org-key (car elt))
  5164. fun (nth 1 elt)
  5165. cmd (orgstruct-make-binding fun nfunc key))
  5166. (org-defkey orgstruct-mode-map key cmd))
  5167. ;; Special treatment needed for TAB and RET
  5168. (org-defkey orgstruct-mode-map [(tab)]
  5169. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5170. (org-defkey orgstruct-mode-map "\C-i"
  5171. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5172. (org-defkey orgstruct-mode-map "\M-\C-m"
  5173. (orgstruct-make-binding 'org-insert-heading 105
  5174. "\M-\C-m" [(meta return)]))
  5175. (org-defkey orgstruct-mode-map [(meta return)]
  5176. (orgstruct-make-binding 'org-insert-heading 106
  5177. [(meta return)] "\M-\C-m"))
  5178. (org-defkey orgstruct-mode-map [(shift meta return)]
  5179. (orgstruct-make-binding 'org-insert-todo-heading 107
  5180. [(meta return)] "\M-\C-m"))
  5181. (unless org-local-vars
  5182. (setq org-local-vars (org-get-local-variables)))
  5183. t))
  5184. (defun orgstruct-make-binding (fun n &rest keys)
  5185. "Create a function for binding in the structure minor mode.
  5186. FUN is the command to call inside a table. N is used to create a unique
  5187. command name. KEYS are keys that should be checked in for a command
  5188. to execute outside of tables."
  5189. (eval
  5190. (list 'defun
  5191. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5192. '(arg)
  5193. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5194. "Outside of structure, run the binding of `"
  5195. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5196. "'.")
  5197. '(interactive "p")
  5198. (list 'if
  5199. '(org-context-p 'headline 'item)
  5200. (list 'org-run-like-in-org-mode (list 'quote fun))
  5201. (list 'let '(orgstruct-mode)
  5202. (list 'call-interactively
  5203. (append '(or)
  5204. (mapcar (lambda (k)
  5205. (list 'key-binding k))
  5206. keys)
  5207. '('orgstruct-error))))))))
  5208. (defun org-context-p (&rest contexts)
  5209. "Check if local context is any of CONTEXTS.
  5210. Possible values in the list of contexts are `table', `headline', and `item'."
  5211. (let ((pos (point)))
  5212. (goto-char (point-at-bol))
  5213. (prog1 (or (and (memq 'table contexts)
  5214. (looking-at "[ \t]*|"))
  5215. (and (memq 'headline contexts)
  5216. ;;????????? (looking-at "\\*+"))
  5217. (looking-at outline-regexp))
  5218. (and (memq 'item contexts)
  5219. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5220. (goto-char pos))))
  5221. (defun org-get-local-variables ()
  5222. "Return a list of all local variables in an org-mode buffer."
  5223. (let (varlist)
  5224. (with-current-buffer (get-buffer-create "*Org tmp*")
  5225. (erase-buffer)
  5226. (org-mode)
  5227. (setq varlist (buffer-local-variables)))
  5228. (kill-buffer "*Org tmp*")
  5229. (delq nil
  5230. (mapcar
  5231. (lambda (x)
  5232. (setq x
  5233. (if (symbolp x)
  5234. (list x)
  5235. (list (car x) (list 'quote (cdr x)))))
  5236. (if (string-match
  5237. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5238. (symbol-name (car x)))
  5239. x nil))
  5240. varlist))))
  5241. ;;;###autoload
  5242. (defun org-run-like-in-org-mode (cmd)
  5243. (org-load-modules-maybe)
  5244. (unless org-local-vars
  5245. (setq org-local-vars (org-get-local-variables)))
  5246. (eval (list 'let org-local-vars
  5247. (list 'call-interactively (list 'quote cmd)))))
  5248. ;;;; Archiving
  5249. (defun org-get-category (&optional pos)
  5250. "Get the category applying to position POS."
  5251. (get-text-property (or pos (point)) 'org-category))
  5252. (defun org-refresh-category-properties ()
  5253. "Refresh category text properties in the buffer."
  5254. (let ((def-cat (cond
  5255. ((null org-category)
  5256. (if buffer-file-name
  5257. (file-name-sans-extension
  5258. (file-name-nondirectory buffer-file-name))
  5259. "???"))
  5260. ((symbolp org-category) (symbol-name org-category))
  5261. (t org-category)))
  5262. beg end cat pos optionp)
  5263. (org-unmodified
  5264. (save-excursion
  5265. (save-restriction
  5266. (widen)
  5267. (goto-char (point-min))
  5268. (put-text-property (point) (point-max) 'org-category def-cat)
  5269. (while (re-search-forward
  5270. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5271. (setq pos (match-end 0)
  5272. optionp (equal (char-after (match-beginning 0)) ?#)
  5273. cat (org-trim (match-string 2)))
  5274. (if optionp
  5275. (setq beg (point-at-bol) end (point-max))
  5276. (org-back-to-heading t)
  5277. (setq beg (point) end (org-end-of-subtree t t)))
  5278. (put-text-property beg end 'org-category cat)
  5279. (goto-char pos)))))))
  5280. ;;;; Link Stuff
  5281. ;;; Link abbreviations
  5282. (defun org-link-expand-abbrev (link)
  5283. "Apply replacements as defined in `org-link-abbrev-alist."
  5284. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5285. (let* ((key (match-string 1 link))
  5286. (as (or (assoc key org-link-abbrev-alist-local)
  5287. (assoc key org-link-abbrev-alist)))
  5288. (tag (and (match-end 2) (match-string 3 link)))
  5289. rpl)
  5290. (if (not as)
  5291. link
  5292. (setq rpl (cdr as))
  5293. (cond
  5294. ((symbolp rpl) (funcall rpl tag))
  5295. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5296. (t (concat rpl tag)))))
  5297. link))
  5298. ;;; Storing and inserting links
  5299. (defvar org-insert-link-history nil
  5300. "Minibuffer history for links inserted with `org-insert-link'.")
  5301. (defvar org-stored-links nil
  5302. "Contains the links stored with `org-store-link'.")
  5303. (defvar org-store-link-plist nil
  5304. "Plist with info about the most recently link created with `org-store-link'.")
  5305. (defvar org-link-protocols nil
  5306. "Link protocols added to Org-mode using `org-add-link-type'.")
  5307. (defvar org-store-link-functions nil
  5308. "List of functions that are called to create and store a link.
  5309. Each function will be called in turn until one returns a non-nil
  5310. value. Each function should check if it is responsible for creating
  5311. this link (for example by looking at the major mode).
  5312. If not, it must exit and return nil.
  5313. If yes, it should return a non-nil value after a calling
  5314. `org-store-link-props' with a list of properties and values.
  5315. Special properties are:
  5316. :type The link prefix. like \"http\". This must be given.
  5317. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5318. This is obligatory as well.
  5319. :description Optional default description for the second pair
  5320. of brackets in an Org-mode link. The user can still change
  5321. this when inserting this link into an Org-mode buffer.
  5322. In addition to these, any additional properties can be specified
  5323. and then used in remember templates.")
  5324. (defun org-add-link-type (type &optional follow export)
  5325. "Add TYPE to the list of `org-link-types'.
  5326. Re-compute all regular expressions depending on `org-link-types'
  5327. FOLLOW and EXPORT are two functions.
  5328. FOLLOW should take the link path as the single argument and do whatever
  5329. is necessary to follow the link, for example find a file or display
  5330. a mail message.
  5331. EXPORT should format the link path for export to one of the export formats.
  5332. It should be a function accepting three arguments:
  5333. path the path of the link, the text after the prefix (like \"http:\")
  5334. desc the description of the link, if any, nil if there was no descripton
  5335. format the export format, a symbol like `html' or `latex'.
  5336. The function may use the FORMAT information to return different values
  5337. depending on the format. The return value will be put literally into
  5338. the exported file.
  5339. Org-mode has a built-in default for exporting links. If you are happy with
  5340. this default, there is no need to define an export function for the link
  5341. type. For a simple example of an export function, see `org-bbdb.el'."
  5342. (add-to-list 'org-link-types type t)
  5343. (org-make-link-regexps)
  5344. (if (assoc type org-link-protocols)
  5345. (setcdr (assoc type org-link-protocols) (list follow export))
  5346. (push (list type follow export) org-link-protocols)))
  5347. ;;;###autoload
  5348. (defun org-store-link (arg)
  5349. "\\<org-mode-map>Store an org-link to the current location.
  5350. This link is added to `org-stored-links' and can later be inserted
  5351. into an org-buffer with \\[org-insert-link].
  5352. For some link types, a prefix arg is interpreted:
  5353. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5354. For file links, arg negates `org-context-in-file-links'."
  5355. (interactive "P")
  5356. (org-load-modules-maybe)
  5357. (setq org-store-link-plist nil) ; reset
  5358. (let (link cpltxt desc description search txt)
  5359. (cond
  5360. ((run-hook-with-args-until-success 'org-store-link-functions)
  5361. (setq link (plist-get org-store-link-plist :link)
  5362. desc (or (plist-get org-store-link-plist :description) link)))
  5363. ((eq major-mode 'calendar-mode)
  5364. (let ((cd (calendar-cursor-to-date)))
  5365. (setq link
  5366. (format-time-string
  5367. (car org-time-stamp-formats)
  5368. (apply 'encode-time
  5369. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5370. nil nil nil))))
  5371. (org-store-link-props :type "calendar" :date cd)))
  5372. ((eq major-mode 'w3-mode)
  5373. (setq cpltxt (url-view-url t)
  5374. link (org-make-link cpltxt))
  5375. (org-store-link-props :type "w3" :url (url-view-url t)))
  5376. ((eq major-mode 'w3m-mode)
  5377. (setq cpltxt (or w3m-current-title w3m-current-url)
  5378. link (org-make-link w3m-current-url))
  5379. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5380. ((setq search (run-hook-with-args-until-success
  5381. 'org-create-file-search-functions))
  5382. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5383. "::" search))
  5384. (setq cpltxt (or description link)))
  5385. ((eq major-mode 'image-mode)
  5386. (setq cpltxt (concat "file:"
  5387. (abbreviate-file-name buffer-file-name))
  5388. link (org-make-link cpltxt))
  5389. (org-store-link-props :type "image" :file buffer-file-name))
  5390. ((eq major-mode 'dired-mode)
  5391. ;; link to the file in the current line
  5392. (setq cpltxt (concat "file:"
  5393. (abbreviate-file-name
  5394. (expand-file-name
  5395. (dired-get-filename nil t))))
  5396. link (org-make-link cpltxt)))
  5397. ((and buffer-file-name (org-mode-p))
  5398. ;; Just link to current headline
  5399. (setq cpltxt (concat "file:"
  5400. (abbreviate-file-name buffer-file-name)))
  5401. ;; Add a context search string
  5402. (when (org-xor org-context-in-file-links arg)
  5403. ;; Check if we are on a target
  5404. (if (org-in-regexp "<<\\(.*?\\)>>")
  5405. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5406. (setq txt (cond
  5407. ((org-on-heading-p) nil)
  5408. ((org-region-active-p)
  5409. (buffer-substring (region-beginning) (region-end)))
  5410. (t nil)))
  5411. (when (or (null txt) (string-match "\\S-" txt))
  5412. (setq cpltxt
  5413. (concat cpltxt "::"
  5414. (condition-case nil
  5415. (org-make-org-heading-search-string txt)
  5416. (error "")))
  5417. desc "NONE"))))
  5418. (if (string-match "::\\'" cpltxt)
  5419. (setq cpltxt (substring cpltxt 0 -2)))
  5420. (setq link (org-make-link cpltxt)))
  5421. ((buffer-file-name (buffer-base-buffer))
  5422. ;; Just link to this file here.
  5423. (setq cpltxt (concat "file:"
  5424. (abbreviate-file-name
  5425. (buffer-file-name (buffer-base-buffer)))))
  5426. ;; Add a context string
  5427. (when (org-xor org-context-in-file-links arg)
  5428. (setq txt (if (org-region-active-p)
  5429. (buffer-substring (region-beginning) (region-end))
  5430. (buffer-substring (point-at-bol) (point-at-eol))))
  5431. ;; Only use search option if there is some text.
  5432. (when (string-match "\\S-" txt)
  5433. (setq cpltxt
  5434. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5435. desc "NONE")))
  5436. (setq link (org-make-link cpltxt)))
  5437. ((interactive-p)
  5438. (error "Cannot link to a buffer which is not visiting a file"))
  5439. (t (setq link nil)))
  5440. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5441. (setq link (or link cpltxt)
  5442. desc (or desc cpltxt))
  5443. (if (equal desc "NONE") (setq desc nil))
  5444. (if (and (interactive-p) link)
  5445. (progn
  5446. (setq org-stored-links
  5447. (cons (list link desc) org-stored-links))
  5448. (message "Stored: %s" (or desc link)))
  5449. (and link (org-make-link-string link desc)))))
  5450. (defun org-store-link-props (&rest plist)
  5451. "Store link properties, extract names and addresses."
  5452. (let (x adr)
  5453. (when (setq x (plist-get plist :from))
  5454. (setq adr (mail-extract-address-components x))
  5455. (plist-put plist :fromname (car adr))
  5456. (plist-put plist :fromaddress (nth 1 adr)))
  5457. (when (setq x (plist-get plist :to))
  5458. (setq adr (mail-extract-address-components x))
  5459. (plist-put plist :toname (car adr))
  5460. (plist-put plist :toaddress (nth 1 adr))))
  5461. (let ((from (plist-get plist :from))
  5462. (to (plist-get plist :to)))
  5463. (when (and from to org-from-is-user-regexp)
  5464. (plist-put plist :fromto
  5465. (if (string-match org-from-is-user-regexp from)
  5466. (concat "to %t")
  5467. (concat "from %f")))))
  5468. (setq org-store-link-plist plist))
  5469. (defun org-add-link-props (&rest plist)
  5470. "Add these properties to the link property list."
  5471. (let (key value)
  5472. (while plist
  5473. (setq key (pop plist) value (pop plist))
  5474. (setq org-store-link-plist
  5475. (plist-put org-store-link-plist key value)))))
  5476. (defun org-email-link-description (&optional fmt)
  5477. "Return the description part of an email link.
  5478. This takes information from `org-store-link-plist' and formats it
  5479. according to FMT (default from `org-email-link-description-format')."
  5480. (setq fmt (or fmt org-email-link-description-format))
  5481. (let* ((p org-store-link-plist)
  5482. (to (plist-get p :toaddress))
  5483. (from (plist-get p :fromaddress))
  5484. (table
  5485. (list
  5486. (cons "%c" (plist-get p :fromto))
  5487. (cons "%F" (plist-get p :from))
  5488. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5489. (cons "%T" (plist-get p :to))
  5490. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5491. (cons "%s" (plist-get p :subject))
  5492. (cons "%m" (plist-get p :message-id)))))
  5493. (when (string-match "%c" fmt)
  5494. ;; Check if the user wrote this message
  5495. (if (and org-from-is-user-regexp from to
  5496. (save-match-data (string-match org-from-is-user-regexp from)))
  5497. (setq fmt (replace-match "to %t" t t fmt))
  5498. (setq fmt (replace-match "from %f" t t fmt))))
  5499. (org-replace-escapes fmt table)))
  5500. (defun org-make-org-heading-search-string (&optional string heading)
  5501. "Make search string for STRING or current headline."
  5502. (interactive)
  5503. (let ((s (or string (org-get-heading))))
  5504. (unless (and string (not heading))
  5505. ;; We are using a headline, clean up garbage in there.
  5506. (if (string-match org-todo-regexp s)
  5507. (setq s (replace-match "" t t s)))
  5508. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5509. (setq s (replace-match "" t t s)))
  5510. (setq s (org-trim s))
  5511. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5512. org-comment-string "\\)") s)
  5513. (setq s (replace-match "" t t s)))
  5514. (while (string-match org-ts-regexp s)
  5515. (setq s (replace-match "" t t s))))
  5516. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5517. (setq s (replace-match " " t t s)))
  5518. (or string (setq s (concat "*" s))) ; Add * for headlines
  5519. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5520. (defun org-make-link (&rest strings)
  5521. "Concatenate STRINGS."
  5522. (apply 'concat strings))
  5523. (defun org-make-link-string (link &optional description)
  5524. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5525. (unless (string-match "\\S-" link)
  5526. (error "Empty link"))
  5527. (when (stringp description)
  5528. ;; Remove brackets from the description, they are fatal.
  5529. (while (string-match "\\[" description)
  5530. (setq description (replace-match "{" t t description)))
  5531. (while (string-match "\\]" description)
  5532. (setq description (replace-match "}" t t description))))
  5533. (when (equal (org-link-escape link) description)
  5534. ;; No description needed, it is identical
  5535. (setq description nil))
  5536. (when (and (not description)
  5537. (not (equal link (org-link-escape link))))
  5538. (setq description (org-extract-attributes link)))
  5539. (concat "[[" (org-link-escape link) "]"
  5540. (if description (concat "[" description "]") "")
  5541. "]"))
  5542. (defconst org-link-escape-chars
  5543. '((?\ . "%20")
  5544. (?\[ . "%5B")
  5545. (?\] . "%5D")
  5546. (?\340 . "%E0") ; `a
  5547. (?\342 . "%E2") ; ^a
  5548. (?\347 . "%E7") ; ,c
  5549. (?\350 . "%E8") ; `e
  5550. (?\351 . "%E9") ; 'e
  5551. (?\352 . "%EA") ; ^e
  5552. (?\356 . "%EE") ; ^i
  5553. (?\364 . "%F4") ; ^o
  5554. (?\371 . "%F9") ; `u
  5555. (?\373 . "%FB") ; ^u
  5556. (?\; . "%3B")
  5557. (?? . "%3F")
  5558. (?= . "%3D")
  5559. (?+ . "%2B")
  5560. )
  5561. "Association list of escapes for some characters problematic in links.
  5562. This is the list that is used for internal purposes.")
  5563. (defconst org-link-escape-chars-browser
  5564. '((?\ . "%20")) ; 32 for the SPC char
  5565. "Association list of escapes for some characters problematic in links.
  5566. This is the list that is used before handing over to the browser.")
  5567. (defun org-link-escape (text &optional table)
  5568. "Escape charaters in TEXT that are problematic for links."
  5569. (setq table (or table org-link-escape-chars))
  5570. (when text
  5571. (let ((re (mapconcat (lambda (x) (regexp-quote
  5572. (char-to-string (car x))))
  5573. table "\\|")))
  5574. (while (string-match re text)
  5575. (setq text
  5576. (replace-match
  5577. (cdr (assoc (string-to-char (match-string 0 text))
  5578. table))
  5579. t t text)))
  5580. text)))
  5581. (defun org-link-unescape (text &optional table)
  5582. "Reverse the action of `org-link-escape'."
  5583. (setq table (or table org-link-escape-chars))
  5584. (when text
  5585. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5586. table "\\|")))
  5587. (while (string-match re text)
  5588. (setq text
  5589. (replace-match
  5590. (char-to-string (car (rassoc (match-string 0 text) table)))
  5591. t t text)))
  5592. text)))
  5593. (defun org-xor (a b)
  5594. "Exclusive or."
  5595. (if a (not b) b))
  5596. (defun org-get-header (header)
  5597. "Find a header field in the current buffer."
  5598. (save-excursion
  5599. (goto-char (point-min))
  5600. (let ((case-fold-search t) s)
  5601. (cond
  5602. ((eq header 'from)
  5603. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5604. (setq s (match-string 1)))
  5605. (while (string-match "\"" s)
  5606. (setq s (replace-match "" t t s)))
  5607. (if (string-match "[<(].*" s)
  5608. (setq s (replace-match "" t t s))))
  5609. ((eq header 'message-id)
  5610. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5611. (setq s (match-string 1))))
  5612. ((eq header 'subject)
  5613. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5614. (setq s (match-string 1)))))
  5615. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5616. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5617. s)))
  5618. (defun org-fixup-message-id-for-http (s)
  5619. "Replace special characters in a message id, so it can be used in an http query."
  5620. (while (string-match "<" s)
  5621. (setq s (replace-match "%3C" t t s)))
  5622. (while (string-match ">" s)
  5623. (setq s (replace-match "%3E" t t s)))
  5624. (while (string-match "@" s)
  5625. (setq s (replace-match "%40" t t s)))
  5626. s)
  5627. ;;;###autoload
  5628. (defun org-insert-link-global ()
  5629. "Insert a link like Org-mode does.
  5630. This command can be called in any mode to insert a link in Org-mode syntax."
  5631. (interactive)
  5632. (org-load-modules-maybe)
  5633. (org-run-like-in-org-mode 'org-insert-link))
  5634. (defun org-insert-link (&optional complete-file link-location)
  5635. "Insert a link. At the prompt, enter the link.
  5636. Completion can be used to select a link previously stored with
  5637. `org-store-link'. When the empty string is entered (i.e. if you just
  5638. press RET at the prompt), the link defaults to the most recently
  5639. stored link. As SPC triggers completion in the minibuffer, you need to
  5640. use M-SPC or C-q SPC to force the insertion of a space character.
  5641. You will also be prompted for a description, and if one is given, it will
  5642. be displayed in the buffer instead of the link.
  5643. If there is already a link at point, this command will allow you to edit link
  5644. and description parts.
  5645. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5646. be selected using completion. The path to the file will be relative to the
  5647. current directory if the file is in the current directory or a subdirectory.
  5648. Otherwise, the link will be the absolute path as completed in the minibuffer
  5649. \(i.e. normally ~/path/to/file).
  5650. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5651. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5652. of `org-keep-stored-link-after-insertion'.
  5653. If `org-make-link-description-function' is non-nil, this function will be
  5654. called with the link target, and the result will be the default
  5655. link description.
  5656. If the LINK-LOCATION parameter is non-nil, this value will be
  5657. used as the link location instead of reading one interactively."
  5658. (interactive "P")
  5659. (let* ((wcf (current-window-configuration))
  5660. (region (if (org-region-active-p)
  5661. (buffer-substring (region-beginning) (region-end))))
  5662. (remove (and region (list (region-beginning) (region-end))))
  5663. (desc region)
  5664. tmphist ; byte-compile incorrectly complains about this
  5665. (link link-location)
  5666. entry file)
  5667. (cond
  5668. (link-location) ; specified by arg, just use it.
  5669. ((org-in-regexp org-bracket-link-regexp 1)
  5670. ;; We do have a link at point, and we are going to edit it.
  5671. (setq remove (list (match-beginning 0) (match-end 0)))
  5672. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5673. (setq link (read-string "Link: "
  5674. (org-link-unescape
  5675. (org-match-string-no-properties 1)))))
  5676. ((or (org-in-regexp org-angle-link-re)
  5677. (org-in-regexp org-plain-link-re))
  5678. ;; Convert to bracket link
  5679. (setq remove (list (match-beginning 0) (match-end 0))
  5680. link (read-string "Link: "
  5681. (org-remove-angle-brackets (match-string 0)))))
  5682. ((equal complete-file '(4))
  5683. ;; Completing read for file names.
  5684. (setq file (read-file-name "File: "))
  5685. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5686. (pwd1 (file-name-as-directory (abbreviate-file-name
  5687. (expand-file-name ".")))))
  5688. (cond
  5689. ((equal complete-file '(16))
  5690. (setq link (org-make-link
  5691. "file:"
  5692. (abbreviate-file-name (expand-file-name file)))))
  5693. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5694. (setq link (org-make-link "file:" (match-string 1 file))))
  5695. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5696. (expand-file-name file))
  5697. (setq link (org-make-link
  5698. "file:" (match-string 1 (expand-file-name file)))))
  5699. (t (setq link (org-make-link "file:" file))))))
  5700. (t
  5701. ;; Read link, with completion for stored links.
  5702. (with-output-to-temp-buffer "*Org Links*"
  5703. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5704. (when org-stored-links
  5705. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5706. (princ (mapconcat
  5707. (lambda (x)
  5708. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5709. (reverse org-stored-links) "\n"))))
  5710. (let ((cw (selected-window)))
  5711. (select-window (get-buffer-window "*Org Links*"))
  5712. (shrink-window-if-larger-than-buffer)
  5713. (setq truncate-lines t)
  5714. (select-window cw))
  5715. ;; Fake a link history, containing the stored links.
  5716. (setq tmphist (append (mapcar 'car org-stored-links)
  5717. org-insert-link-history))
  5718. (unwind-protect
  5719. (setq link (org-completing-read
  5720. "Link: "
  5721. (append
  5722. (mapcar (lambda (x) (list (concat (car x) ":")))
  5723. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5724. (mapcar (lambda (x) (list (concat x ":")))
  5725. org-link-types))
  5726. nil nil nil
  5727. 'tmphist
  5728. (or (car (car org-stored-links)))))
  5729. (set-window-configuration wcf)
  5730. (kill-buffer "*Org Links*"))
  5731. (setq entry (assoc link org-stored-links))
  5732. (or entry (push link org-insert-link-history))
  5733. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5734. (not org-keep-stored-link-after-insertion))
  5735. (setq org-stored-links (delq (assoc link org-stored-links)
  5736. org-stored-links)))
  5737. (setq desc (or desc (nth 1 entry)))))
  5738. (if (string-match org-plain-link-re link)
  5739. ;; URL-like link, normalize the use of angular brackets.
  5740. (setq link (org-make-link (org-remove-angle-brackets link))))
  5741. ;; Check if we are linking to the current file with a search option
  5742. ;; If yes, simplify the link by using only the search option.
  5743. (when (and buffer-file-name
  5744. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5745. (let* ((path (match-string 1 link))
  5746. (case-fold-search nil)
  5747. (search (match-string 2 link)))
  5748. (save-match-data
  5749. (if (equal (file-truename buffer-file-name) (file-truename path))
  5750. ;; We are linking to this same file, with a search option
  5751. (setq link search)))))
  5752. ;; Check if we can/should use a relative path. If yes, simplify the link
  5753. (when (string-match "\\<file:\\(.*\\)" link)
  5754. (let* ((path (match-string 1 link))
  5755. (origpath path)
  5756. (case-fold-search nil))
  5757. (cond
  5758. ((eq org-link-file-path-type 'absolute)
  5759. (setq path (abbreviate-file-name (expand-file-name path))))
  5760. ((eq org-link-file-path-type 'noabbrev)
  5761. (setq path (expand-file-name path)))
  5762. ((eq org-link-file-path-type 'relative)
  5763. (setq path (file-relative-name path)))
  5764. (t
  5765. (save-match-data
  5766. (if (string-match (concat "^" (regexp-quote
  5767. (file-name-as-directory
  5768. (expand-file-name "."))))
  5769. (expand-file-name path))
  5770. ;; We are linking a file with relative path name.
  5771. (setq path (substring (expand-file-name path)
  5772. (match-end 0)))))))
  5773. (setq link (concat "file:" path))
  5774. (if (equal desc origpath)
  5775. (setq desc path))))
  5776. (if org-make-link-description-function
  5777. (setq desc (funcall org-make-link-description-function link desc)))
  5778. (setq desc (read-string "Description: " desc))
  5779. (unless (string-match "\\S-" desc) (setq desc nil))
  5780. (if remove (apply 'delete-region remove))
  5781. (insert (org-make-link-string link desc))))
  5782. (defun org-completing-read (&rest args)
  5783. (let ((minibuffer-local-completion-map
  5784. (copy-keymap minibuffer-local-completion-map)))
  5785. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5786. (apply 'completing-read args)))
  5787. (defun org-extract-attributes (s)
  5788. "Extract the attributes cookie from a string and set as text property."
  5789. (let (a attr (start 0) key value)
  5790. (save-match-data
  5791. (when (string-match "{{\\([^}]+\\)}}$" s)
  5792. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5793. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5794. (setq key (match-string 1 a) value (match-string 2 a)
  5795. start (match-end 0)
  5796. attr (plist-put attr (intern key) value))))
  5797. (org-add-props s nil 'org-attributes attr))
  5798. s))
  5799. (defun org-attributes-to-string (plist)
  5800. "Format a property list into an HTML attribute list."
  5801. (let ((s "") key value)
  5802. (while plist
  5803. (setq key (pop plist) value (pop plist))
  5804. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5805. s))
  5806. ;;; Opening/following a link
  5807. (defvar org-link-search-failed nil)
  5808. (defun org-next-link ()
  5809. "Move forward to the next link.
  5810. If the link is in hidden text, expose it."
  5811. (interactive)
  5812. (when (and org-link-search-failed (eq this-command last-command))
  5813. (goto-char (point-min))
  5814. (message "Link search wrapped back to beginning of buffer"))
  5815. (setq org-link-search-failed nil)
  5816. (let* ((pos (point))
  5817. (ct (org-context))
  5818. (a (assoc :link ct)))
  5819. (if a (goto-char (nth 2 a)))
  5820. (if (re-search-forward org-any-link-re nil t)
  5821. (progn
  5822. (goto-char (match-beginning 0))
  5823. (if (org-invisible-p) (org-show-context)))
  5824. (goto-char pos)
  5825. (setq org-link-search-failed t)
  5826. (error "No further link found"))))
  5827. (defun org-previous-link ()
  5828. "Move backward to the previous link.
  5829. If the link is in hidden text, expose it."
  5830. (interactive)
  5831. (when (and org-link-search-failed (eq this-command last-command))
  5832. (goto-char (point-max))
  5833. (message "Link search wrapped back to end of buffer"))
  5834. (setq org-link-search-failed nil)
  5835. (let* ((pos (point))
  5836. (ct (org-context))
  5837. (a (assoc :link ct)))
  5838. (if a (goto-char (nth 1 a)))
  5839. (if (re-search-backward org-any-link-re nil t)
  5840. (progn
  5841. (goto-char (match-beginning 0))
  5842. (if (org-invisible-p) (org-show-context)))
  5843. (goto-char pos)
  5844. (setq org-link-search-failed t)
  5845. (error "No further link found"))))
  5846. (defun org-find-file-at-mouse (ev)
  5847. "Open file link or URL at mouse."
  5848. (interactive "e")
  5849. (mouse-set-point ev)
  5850. (org-open-at-point 'in-emacs))
  5851. (defun org-open-at-mouse (ev)
  5852. "Open file link or URL at mouse."
  5853. (interactive "e")
  5854. (mouse-set-point ev)
  5855. (org-open-at-point))
  5856. (defvar org-window-config-before-follow-link nil
  5857. "The window configuration before following a link.
  5858. This is saved in case the need arises to restore it.")
  5859. (defvar org-open-link-marker (make-marker)
  5860. "Marker pointing to the location where `org-open-at-point; was called.")
  5861. ;;;###autoload
  5862. (defun org-open-at-point-global ()
  5863. "Follow a link like Org-mode does.
  5864. This command can be called in any mode to follow a link that has
  5865. Org-mode syntax."
  5866. (interactive)
  5867. (org-run-like-in-org-mode 'org-open-at-point))
  5868. ;;;###autoload
  5869. (defun org-open-link-from-string (s &optional arg)
  5870. "Open a link in the string S, as if it was in Org-mode."
  5871. (interactive "sLink: \nP")
  5872. (with-temp-buffer
  5873. (let ((org-inhibit-startup t))
  5874. (org-mode)
  5875. (insert s)
  5876. (goto-char (point-min))
  5877. (org-open-at-point arg))))
  5878. (defun org-open-at-point (&optional in-emacs)
  5879. "Open link at or after point.
  5880. If there is no link at point, this function will search forward up to
  5881. the end of the current subtree.
  5882. Normally, files will be opened by an appropriate application. If the
  5883. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  5884. (interactive "P")
  5885. (org-load-modules-maybe)
  5886. (move-marker org-open-link-marker (point))
  5887. (setq org-window-config-before-follow-link (current-window-configuration))
  5888. (org-remove-occur-highlights nil nil t)
  5889. (if (org-at-timestamp-p t)
  5890. (org-follow-timestamp-link)
  5891. (let (type path link line search (pos (point)))
  5892. (catch 'match
  5893. (save-excursion
  5894. (skip-chars-forward "^]\n\r")
  5895. (when (org-in-regexp org-bracket-link-regexp)
  5896. (setq link (org-extract-attributes
  5897. (org-link-unescape (org-match-string-no-properties 1))))
  5898. (while (string-match " *\n *" link)
  5899. (setq link (replace-match " " t t link)))
  5900. (setq link (org-link-expand-abbrev link))
  5901. (cond
  5902. ((or (file-name-absolute-p link)
  5903. (string-match "^\\.\\.?/" link))
  5904. (setq type "file" path link))
  5905. ((string-match org-link-re-with-space2 link)
  5906. (setq type (match-string 1 link) path (match-string 2 link)))
  5907. (t (setq type "thisfile" path link)))
  5908. (throw 'match t)))
  5909. (when (get-text-property (point) 'org-linked-text)
  5910. (setq type "thisfile"
  5911. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  5912. (1+ (point)) (point))
  5913. path (buffer-substring
  5914. (previous-single-property-change pos 'org-linked-text)
  5915. (next-single-property-change pos 'org-linked-text)))
  5916. (throw 'match t))
  5917. (save-excursion
  5918. (when (or (org-in-regexp org-angle-link-re)
  5919. (org-in-regexp org-plain-link-re))
  5920. (setq type (match-string 1) path (match-string 2))
  5921. (throw 'match t)))
  5922. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  5923. (setq type "tree-match"
  5924. path (match-string 1))
  5925. (throw 'match t))
  5926. (save-excursion
  5927. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  5928. (setq type "tags"
  5929. path (match-string 1))
  5930. (while (string-match ":" path)
  5931. (setq path (replace-match "+" t t path)))
  5932. (throw 'match t))))
  5933. (unless path
  5934. (error "No link found"))
  5935. ;; Remove any trailing spaces in path
  5936. (if (string-match " +\\'" path)
  5937. (setq path (replace-match "" t t path)))
  5938. (cond
  5939. ((assoc type org-link-protocols)
  5940. (funcall (nth 1 (assoc type org-link-protocols)) path))
  5941. ((equal type "mailto")
  5942. (let ((cmd (car org-link-mailto-program))
  5943. (args (cdr org-link-mailto-program)) args1
  5944. (address path) (subject "") a)
  5945. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  5946. (setq address (match-string 1 path)
  5947. subject (org-link-escape (match-string 2 path))))
  5948. (while args
  5949. (cond
  5950. ((not (stringp (car args))) (push (pop args) args1))
  5951. (t (setq a (pop args))
  5952. (if (string-match "%a" a)
  5953. (setq a (replace-match address t t a)))
  5954. (if (string-match "%s" a)
  5955. (setq a (replace-match subject t t a)))
  5956. (push a args1))))
  5957. (apply cmd (nreverse args1))))
  5958. ((member type '("http" "https" "ftp" "news"))
  5959. (browse-url (concat type ":" (org-link-escape
  5960. path org-link-escape-chars-browser))))
  5961. ((member type '("message"))
  5962. (browse-url (concat type ":" path)))
  5963. ((string= type "tags")
  5964. (org-tags-view in-emacs path))
  5965. ((string= type "thisfile")
  5966. (if in-emacs
  5967. (switch-to-buffer-other-window
  5968. (org-get-buffer-for-internal-link (current-buffer)))
  5969. (org-mark-ring-push))
  5970. (let ((cmd `(org-link-search
  5971. ,path
  5972. ,(cond ((equal in-emacs '(4)) 'occur)
  5973. ((equal in-emacs '(16)) 'org-occur)
  5974. (t nil))
  5975. ,pos)))
  5976. (condition-case nil (eval cmd)
  5977. (error (progn (widen) (eval cmd))))))
  5978. ((string= type "tree-match")
  5979. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  5980. ((string= type "file")
  5981. (if (string-match "::\\([0-9]+\\)\\'" path)
  5982. (setq line (string-to-number (match-string 1 path))
  5983. path (substring path 0 (match-beginning 0)))
  5984. (if (string-match "::\\(.+\\)\\'" path)
  5985. (setq search (match-string 1 path)
  5986. path (substring path 0 (match-beginning 0)))))
  5987. (if (string-match "[*?{]" (file-name-nondirectory path))
  5988. (dired path)
  5989. (org-open-file path in-emacs line search)))
  5990. ((string= type "news")
  5991. (require 'org-gnus)
  5992. (org-gnus-follow-link path))
  5993. ((string= type "shell")
  5994. (let ((cmd path))
  5995. (if (or (not org-confirm-shell-link-function)
  5996. (funcall org-confirm-shell-link-function
  5997. (format "Execute \"%s\" in shell? "
  5998. (org-add-props cmd nil
  5999. 'face 'org-warning))))
  6000. (progn
  6001. (message "Executing %s" cmd)
  6002. (shell-command cmd))
  6003. (error "Abort"))))
  6004. ((string= type "elisp")
  6005. (let ((cmd path))
  6006. (if (or (not org-confirm-elisp-link-function)
  6007. (funcall org-confirm-elisp-link-function
  6008. (format "Execute \"%s\" as elisp? "
  6009. (org-add-props cmd nil
  6010. 'face 'org-warning))))
  6011. (message "%s => %s" cmd (eval (read cmd)))
  6012. (error "Abort"))))
  6013. (t
  6014. (browse-url-at-point)))))
  6015. (move-marker org-open-link-marker nil)
  6016. (run-hook-with-args 'org-follow-link-hook))
  6017. ;;;; Time estimates
  6018. (defun org-get-effort (&optional pom)
  6019. "Get the effort estimate for the current entry."
  6020. (org-entry-get pom org-effort-property))
  6021. ;;; File search
  6022. (defvar org-create-file-search-functions nil
  6023. "List of functions to construct the right search string for a file link.
  6024. These functions are called in turn with point at the location to
  6025. which the link should point.
  6026. A function in the hook should first test if it would like to
  6027. handle this file type, for example by checking the major-mode or
  6028. the file extension. If it decides not to handle this file, it
  6029. should just return nil to give other functions a chance. If it
  6030. does handle the file, it must return the search string to be used
  6031. when following the link. The search string will be part of the
  6032. file link, given after a double colon, and `org-open-at-point'
  6033. will automatically search for it. If special measures must be
  6034. taken to make the search successful, another function should be
  6035. added to the companion hook `org-execute-file-search-functions',
  6036. which see.
  6037. A function in this hook may also use `setq' to set the variable
  6038. `description' to provide a suggestion for the descriptive text to
  6039. be used for this link when it gets inserted into an Org-mode
  6040. buffer with \\[org-insert-link].")
  6041. (defvar org-execute-file-search-functions nil
  6042. "List of functions to execute a file search triggered by a link.
  6043. Functions added to this hook must accept a single argument, the
  6044. search string that was part of the file link, the part after the
  6045. double colon. The function must first check if it would like to
  6046. handle this search, for example by checking the major-mode or the
  6047. file extension. If it decides not to handle this search, it
  6048. should just return nil to give other functions a chance. If it
  6049. does handle the search, it must return a non-nil value to keep
  6050. other functions from trying.
  6051. Each function can access the current prefix argument through the
  6052. variable `current-prefix-argument'. Note that a single prefix is
  6053. used to force opening a link in Emacs, so it may be good to only
  6054. use a numeric or double prefix to guide the search function.
  6055. In case this is needed, a function in this hook can also restore
  6056. the window configuration before `org-open-at-point' was called using:
  6057. (set-window-configuration org-window-config-before-follow-link)")
  6058. (defun org-link-search (s &optional type avoid-pos)
  6059. "Search for a link search option.
  6060. If S is surrounded by forward slashes, it is interpreted as a
  6061. regular expression. In org-mode files, this will create an `org-occur'
  6062. sparse tree. In ordinary files, `occur' will be used to list matches.
  6063. If the current buffer is in `dired-mode', grep will be used to search
  6064. in all files. If AVOID-POS is given, ignore matches near that position."
  6065. (let ((case-fold-search t)
  6066. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6067. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6068. (append '(("") (" ") ("\t") ("\n"))
  6069. org-emphasis-alist)
  6070. "\\|") "\\)"))
  6071. (pos (point))
  6072. (pre nil) (post nil)
  6073. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6074. (cond
  6075. ;; First check if there are any special
  6076. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6077. ;; Now try the builtin stuff
  6078. ((save-excursion
  6079. (goto-char (point-min))
  6080. (and
  6081. (re-search-forward
  6082. (concat "<<" (regexp-quote s0) ">>") nil t)
  6083. (setq type 'dedicated
  6084. pos (match-beginning 0))))
  6085. ;; There is an exact target for this
  6086. (goto-char pos))
  6087. ((string-match "^/\\(.*\\)/$" s)
  6088. ;; A regular expression
  6089. (cond
  6090. ((org-mode-p)
  6091. (org-occur (match-string 1 s)))
  6092. ;;((eq major-mode 'dired-mode)
  6093. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6094. (t (org-do-occur (match-string 1 s)))))
  6095. (t
  6096. ;; A normal search strings
  6097. (when (equal (string-to-char s) ?*)
  6098. ;; Anchor on headlines, post may include tags.
  6099. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6100. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6101. s (substring s 1)))
  6102. (remove-text-properties
  6103. 0 (length s)
  6104. '(face nil mouse-face nil keymap nil fontified nil) s)
  6105. ;; Make a series of regular expressions to find a match
  6106. (setq words (org-split-string s "[ \n\r\t]+")
  6107. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6108. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6109. "\\)" markers)
  6110. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6111. re2a (concat "[ \t\r\n]" re2a_)
  6112. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6113. re4 (concat "[^a-zA-Z_]" re4_)
  6114. re1 (concat pre re2 post)
  6115. re3 (concat pre (if pre re4_ re4) post)
  6116. re5 (concat pre ".*" re4)
  6117. re2 (concat pre re2)
  6118. re2a (concat pre (if pre re2a_ re2a))
  6119. re4 (concat pre (if pre re4_ re4))
  6120. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6121. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6122. re5 "\\)"
  6123. ))
  6124. (cond
  6125. ((eq type 'org-occur) (org-occur reall))
  6126. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6127. (t (goto-char (point-min))
  6128. (setq type 'fuzzy)
  6129. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6130. (org-search-not-self 1 re1 nil t)
  6131. (org-search-not-self 1 re2 nil t)
  6132. (org-search-not-self 1 re2a nil t)
  6133. (org-search-not-self 1 re3 nil t)
  6134. (org-search-not-self 1 re4 nil t)
  6135. (org-search-not-self 1 re5 nil t)
  6136. )
  6137. (goto-char (match-beginning 1))
  6138. (goto-char pos)
  6139. (error "No match")))))
  6140. (t
  6141. ;; Normal string-search
  6142. (goto-char (point-min))
  6143. (if (search-forward s nil t)
  6144. (goto-char (match-beginning 0))
  6145. (error "No match"))))
  6146. (and (org-mode-p) (org-show-context 'link-search))
  6147. type))
  6148. (defun org-search-not-self (group &rest args)
  6149. "Execute `re-search-forward', but only accept matches that do not
  6150. enclose the position of `org-open-link-marker'."
  6151. (let ((m org-open-link-marker))
  6152. (catch 'exit
  6153. (while (apply 're-search-forward args)
  6154. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6155. (goto-char (match-end group))
  6156. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6157. (> (match-beginning 0) (marker-position m))
  6158. (< (match-end 0) (marker-position m)))
  6159. (save-match-data
  6160. (or (not (org-in-regexp
  6161. org-bracket-link-analytic-regexp 1))
  6162. (not (match-end 4)) ; no description
  6163. (and (<= (match-beginning 4) (point))
  6164. (>= (match-end 4) (point))))))
  6165. (throw 'exit (point))))))))
  6166. (defun org-get-buffer-for-internal-link (buffer)
  6167. "Return a buffer to be used for displaying the link target of internal links."
  6168. (cond
  6169. ((not org-display-internal-link-with-indirect-buffer)
  6170. buffer)
  6171. ((string-match "(Clone)$" (buffer-name buffer))
  6172. (message "Buffer is already a clone, not making another one")
  6173. ;; we also do not modify visibility in this case
  6174. buffer)
  6175. (t ; make a new indirect buffer for displaying the link
  6176. (let* ((bn (buffer-name buffer))
  6177. (ibn (concat bn "(Clone)"))
  6178. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6179. (with-current-buffer ib (org-overview))
  6180. ib))))
  6181. (defun org-do-occur (regexp &optional cleanup)
  6182. "Call the Emacs command `occur'.
  6183. If CLEANUP is non-nil, remove the printout of the regular expression
  6184. in the *Occur* buffer. This is useful if the regex is long and not useful
  6185. to read."
  6186. (occur regexp)
  6187. (when cleanup
  6188. (let ((cwin (selected-window)) win beg end)
  6189. (when (setq win (get-buffer-window "*Occur*"))
  6190. (select-window win))
  6191. (goto-char (point-min))
  6192. (when (re-search-forward "match[a-z]+" nil t)
  6193. (setq beg (match-end 0))
  6194. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6195. (setq end (1- (match-beginning 0)))))
  6196. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6197. (goto-char (point-min))
  6198. (select-window cwin))))
  6199. ;;; The mark ring for links jumps
  6200. (defvar org-mark-ring nil
  6201. "Mark ring for positions before jumps in Org-mode.")
  6202. (defvar org-mark-ring-last-goto nil
  6203. "Last position in the mark ring used to go back.")
  6204. ;; Fill and close the ring
  6205. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6206. (loop for i from 1 to org-mark-ring-length do
  6207. (push (make-marker) org-mark-ring))
  6208. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6209. org-mark-ring)
  6210. (defun org-mark-ring-push (&optional pos buffer)
  6211. "Put the current position or POS into the mark ring and rotate it."
  6212. (interactive)
  6213. (setq pos (or pos (point)))
  6214. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6215. (move-marker (car org-mark-ring)
  6216. (or pos (point))
  6217. (or buffer (current-buffer)))
  6218. (message "%s"
  6219. (substitute-command-keys
  6220. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6221. (defun org-mark-ring-goto (&optional n)
  6222. "Jump to the previous position in the mark ring.
  6223. With prefix arg N, jump back that many stored positions. When
  6224. called several times in succession, walk through the entire ring.
  6225. Org-mode commands jumping to a different position in the current file,
  6226. or to another Org-mode file, automatically push the old position
  6227. onto the ring."
  6228. (interactive "p")
  6229. (let (p m)
  6230. (if (eq last-command this-command)
  6231. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6232. (setq p org-mark-ring))
  6233. (setq org-mark-ring-last-goto p)
  6234. (setq m (car p))
  6235. (switch-to-buffer (marker-buffer m))
  6236. (goto-char m)
  6237. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6238. (defun org-remove-angle-brackets (s)
  6239. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6240. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6241. s)
  6242. (defun org-add-angle-brackets (s)
  6243. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6244. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6245. s)
  6246. (defun org-remove-double-quotes (s)
  6247. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6248. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6249. s)
  6250. ;;; Following specific links
  6251. (defun org-follow-timestamp-link ()
  6252. (cond
  6253. ((org-at-date-range-p t)
  6254. (let ((org-agenda-start-on-weekday)
  6255. (t1 (match-string 1))
  6256. (t2 (match-string 2)))
  6257. (setq t1 (time-to-days (org-time-string-to-time t1))
  6258. t2 (time-to-days (org-time-string-to-time t2)))
  6259. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6260. ((org-at-timestamp-p t)
  6261. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6262. (substring (match-string 1) 0 10)))
  6263. 1))
  6264. (t (error "This should not happen"))))
  6265. ;;; Following file links
  6266. (defvar org-wait nil)
  6267. (defun org-open-file (path &optional in-emacs line search)
  6268. "Open the file at PATH.
  6269. First, this expands any special file name abbreviations. Then the
  6270. configuration variable `org-file-apps' is checked if it contains an
  6271. entry for this file type, and if yes, the corresponding command is launched.
  6272. If no application is found, Emacs simply visits the file.
  6273. With optional argument IN-EMACS, Emacs will visit the file.
  6274. Optional LINE specifies a line to go to, optional SEARCH a string to
  6275. search for. If LINE or SEARCH is given, the file will always be
  6276. opened in Emacs.
  6277. If the file does not exist, an error is thrown."
  6278. (setq in-emacs (or in-emacs line search))
  6279. (let* ((file (if (equal path "")
  6280. buffer-file-name
  6281. (substitute-in-file-name (expand-file-name path))))
  6282. (apps (append org-file-apps (org-default-apps)))
  6283. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6284. (dirp (if remp nil (file-directory-p file)))
  6285. (file (if (and dirp org-open-directory-means-index-dot-org)
  6286. (concat (file-name-as-directory file) "index.org")
  6287. file))
  6288. (a-m-a-p (assq 'auto-mode apps))
  6289. (dfile (downcase file))
  6290. (old-buffer (current-buffer))
  6291. (old-pos (point))
  6292. (old-mode major-mode)
  6293. ext cmd)
  6294. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6295. (setq ext (match-string 1 dfile))
  6296. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6297. (setq ext (match-string 1 dfile))))
  6298. (if in-emacs
  6299. (setq cmd 'emacs)
  6300. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6301. (and dirp (cdr (assoc 'directory apps)))
  6302. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6303. 'string-match)
  6304. (cdr (assoc ext apps))
  6305. (cdr (assoc t apps)))))
  6306. (when (eq cmd 'default)
  6307. (setq cmd (cdr (assoc t apps))))
  6308. (when (eq cmd 'mailcap)
  6309. (require 'mailcap)
  6310. (mailcap-parse-mailcaps)
  6311. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6312. (command (mailcap-mime-info mime-type)))
  6313. (if (stringp command)
  6314. (setq cmd command)
  6315. (setq cmd 'emacs))))
  6316. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6317. (not (file-exists-p file))
  6318. (not org-open-non-existing-files))
  6319. (error "No such file: %s" file))
  6320. (cond
  6321. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6322. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6323. (while (string-match "['\"]%s['\"]" cmd)
  6324. (setq cmd (replace-match "%s" t t cmd)))
  6325. (while (string-match "%s" cmd)
  6326. (setq cmd (replace-match
  6327. (save-match-data
  6328. (shell-quote-argument
  6329. (convert-standard-filename file)))
  6330. t t cmd)))
  6331. (save-window-excursion
  6332. (start-process-shell-command cmd nil cmd)
  6333. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6334. ))
  6335. ((or (stringp cmd)
  6336. (eq cmd 'emacs))
  6337. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6338. (widen)
  6339. (if line (goto-line line)
  6340. (if search (org-link-search search))))
  6341. ((consp cmd)
  6342. (let ((file (convert-standard-filename file)))
  6343. (eval cmd)))
  6344. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6345. (and (org-mode-p) (eq old-mode 'org-mode)
  6346. (or (not (equal old-buffer (current-buffer)))
  6347. (not (equal old-pos (point))))
  6348. (org-mark-ring-push old-pos old-buffer))))
  6349. (defun org-default-apps ()
  6350. "Return the default applications for this operating system."
  6351. (cond
  6352. ((eq system-type 'darwin)
  6353. org-file-apps-defaults-macosx)
  6354. ((eq system-type 'windows-nt)
  6355. org-file-apps-defaults-windowsnt)
  6356. (t org-file-apps-defaults-gnu)))
  6357. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6358. "Convert extensions to regular expressions in the cars of LIST.
  6359. Also, weed out any non-string entries, because the return value is used
  6360. only for regexp matching.
  6361. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6362. point to the symbol `emacs', indicating that the file should
  6363. be opened in Emacs."
  6364. (append
  6365. (delq nil
  6366. (mapcar (lambda (x)
  6367. (if (not (stringp (car x)))
  6368. nil
  6369. (if (string-match "\\W" (car x))
  6370. x
  6371. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6372. list))
  6373. (if add-auto-mode
  6374. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6375. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6376. (defun org-file-remote-p (file)
  6377. "Test whether FILE specifies a location on a remote system.
  6378. Return non-nil if the location is indeed remote.
  6379. For example, the filename \"/user@host:/foo\" specifies a location
  6380. on the system \"/user@host:\"."
  6381. (cond ((fboundp 'file-remote-p)
  6382. (file-remote-p file))
  6383. ((fboundp 'tramp-handle-file-remote-p)
  6384. (tramp-handle-file-remote-p file))
  6385. ((and (boundp 'ange-ftp-name-format)
  6386. (string-match (car ange-ftp-name-format) file))
  6387. t)
  6388. (t nil)))
  6389. ;;;; Refiling
  6390. (defun org-get-org-file ()
  6391. "Read a filename, with default directory `org-directory'."
  6392. (let ((default (or org-default-notes-file remember-data-file)))
  6393. (read-file-name (format "File name [%s]: " default)
  6394. (file-name-as-directory org-directory)
  6395. default)))
  6396. (defun org-notes-order-reversed-p ()
  6397. "Check if the current file should receive notes in reversed order."
  6398. (cond
  6399. ((not org-reverse-note-order) nil)
  6400. ((eq t org-reverse-note-order) t)
  6401. ((not (listp org-reverse-note-order)) nil)
  6402. (t (catch 'exit
  6403. (let ((all org-reverse-note-order)
  6404. entry)
  6405. (while (setq entry (pop all))
  6406. (if (string-match (car entry) buffer-file-name)
  6407. (throw 'exit (cdr entry))))
  6408. nil)))))
  6409. (defvar org-refile-target-table nil
  6410. "The list of refile targets, created by `org-refile'.")
  6411. (defvar org-agenda-new-buffers nil
  6412. "Buffers created to visit agenda files.")
  6413. (defun org-get-refile-targets (&optional default-buffer)
  6414. "Produce a table with refile targets."
  6415. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6416. targets txt re files f desc descre)
  6417. (with-current-buffer (or default-buffer (current-buffer))
  6418. (while (setq entry (pop entries))
  6419. (setq files (car entry) desc (cdr entry))
  6420. (cond
  6421. ((null files) (setq files (list (current-buffer))))
  6422. ((eq files 'org-agenda-files)
  6423. (setq files (org-agenda-files 'unrestricted)))
  6424. ((and (symbolp files) (fboundp files))
  6425. (setq files (funcall files)))
  6426. ((and (symbolp files) (boundp files))
  6427. (setq files (symbol-value files))))
  6428. (if (stringp files) (setq files (list files)))
  6429. (cond
  6430. ((eq (car desc) :tag)
  6431. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6432. ((eq (car desc) :todo)
  6433. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6434. ((eq (car desc) :regexp)
  6435. (setq descre (cdr desc)))
  6436. ((eq (car desc) :level)
  6437. (setq descre (concat "^\\*\\{" (number-to-string
  6438. (if org-odd-levels-only
  6439. (1- (* 2 (cdr desc)))
  6440. (cdr desc)))
  6441. "\\}[ \t]")))
  6442. ((eq (car desc) :maxlevel)
  6443. (setq descre (concat "^\\*\\{1," (number-to-string
  6444. (if org-odd-levels-only
  6445. (1- (* 2 (cdr desc)))
  6446. (cdr desc)))
  6447. "\\}[ \t]")))
  6448. (t (error "Bad refiling target description %s" desc)))
  6449. (while (setq f (pop files))
  6450. (save-excursion
  6451. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6452. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6453. (save-excursion
  6454. (save-restriction
  6455. (widen)
  6456. (goto-char (point-min))
  6457. (while (re-search-forward descre nil t)
  6458. (goto-char (point-at-bol))
  6459. (when (looking-at org-complex-heading-regexp)
  6460. (setq txt (org-link-display-format (match-string 4))
  6461. re (concat "^" (regexp-quote
  6462. (buffer-substring (match-beginning 1)
  6463. (match-end 4)))))
  6464. (if (match-end 5) (setq re (concat re "[ \t]+"
  6465. (regexp-quote
  6466. (match-string 5)))))
  6467. (setq re (concat re "[ \t]*$"))
  6468. (when org-refile-use-outline-path
  6469. (setq txt (mapconcat 'org-protect-slash
  6470. (append
  6471. (if (eq org-refile-use-outline-path 'file)
  6472. (list (file-name-nondirectory
  6473. (buffer-file-name (buffer-base-buffer))))
  6474. (if (eq org-refile-use-outline-path 'full-file-path)
  6475. (list (buffer-file-name (buffer-base-buffer)))))
  6476. (org-get-outline-path)
  6477. (list txt))
  6478. "/")))
  6479. (push (list txt f re (point)) targets))
  6480. (goto-char (point-at-eol))))))))
  6481. (nreverse targets))))
  6482. (defun org-protect-slash (s)
  6483. (while (string-match "/" s)
  6484. (setq s (replace-match "\\" t t s)))
  6485. s)
  6486. (defun org-get-outline-path ()
  6487. "Return the outline path to the current entry, as a list."
  6488. (let (rtn)
  6489. (save-excursion
  6490. (while (org-up-heading-safe)
  6491. (when (looking-at org-complex-heading-regexp)
  6492. (push (org-match-string-no-properties 4) rtn)))
  6493. rtn)))
  6494. (defvar org-refile-history nil
  6495. "History for refiling operations.")
  6496. (defun org-refile (&optional goto default-buffer)
  6497. "Move the entry at point to another heading.
  6498. The list of target headings is compiled using the information in
  6499. `org-refile-targets', which see. This list is created before each use
  6500. and will therefore always be up-to-date.
  6501. At the target location, the entry is filed as a subitem of the target heading.
  6502. Depending on `org-reverse-note-order', the new subitem will either be the
  6503. first of the last subitem.
  6504. With prefix arg GOTO, the command will only visit the target location,
  6505. not actually move anything.
  6506. With a double prefix `C-u C-u', go to the location where the last refiling
  6507. operation has put the subtree."
  6508. (interactive "P")
  6509. (let* ((cbuf (current-buffer))
  6510. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6511. pos it nbuf file re level reversed)
  6512. (if (equal goto '(16))
  6513. (org-refile-goto-last-stored)
  6514. (when (setq it (org-refile-get-location
  6515. (if goto "Goto: " "Refile to: ") default-buffer))
  6516. (setq file (nth 1 it)
  6517. re (nth 2 it)
  6518. pos (nth 3 it))
  6519. (setq nbuf (or (find-buffer-visiting file)
  6520. (find-file-noselect file)))
  6521. (if goto
  6522. (progn
  6523. (switch-to-buffer nbuf)
  6524. (goto-char pos)
  6525. (org-show-context 'org-goto))
  6526. (org-copy-subtree 1 nil t)
  6527. (save-excursion
  6528. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6529. (find-file-noselect file))))
  6530. (setq reversed (org-notes-order-reversed-p))
  6531. (save-excursion
  6532. (save-restriction
  6533. (widen)
  6534. (goto-char pos)
  6535. (looking-at outline-regexp)
  6536. (setq level (org-get-valid-level (funcall outline-level) 1))
  6537. (goto-char
  6538. (if reversed
  6539. (or (outline-next-heading) (point-max))
  6540. (or (save-excursion (outline-get-next-sibling))
  6541. (org-end-of-subtree t t)
  6542. (point-max))))
  6543. (if (not (bolp)) (newline))
  6544. (bookmark-set "org-refile-last-stored")
  6545. (org-paste-subtree level))))
  6546. (org-cut-subtree)
  6547. (setq org-markers-to-move nil)
  6548. (message "Entry refiled to \"%s\"" (car it)))))))
  6549. (defun org-refile-goto-last-stored ()
  6550. "Go to the location where the last refile was stored."
  6551. (interactive)
  6552. (bookmark-jump "org-refile-last-stored")
  6553. (message "This is the location of the last refile"))
  6554. (defun org-refile-get-location (&optional prompt default-buffer)
  6555. "Prompt the user for a refile location, using PROMPT."
  6556. (let ((org-refile-targets org-refile-targets)
  6557. (org-refile-use-outline-path org-refile-use-outline-path))
  6558. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6559. (unless org-refile-target-table
  6560. (error "No refile targets"))
  6561. (let* ((cbuf (current-buffer))
  6562. (cfunc (if org-refile-use-outline-path
  6563. 'org-olpath-completing-read
  6564. 'completing-read))
  6565. (extra (if org-refile-use-outline-path "/" ""))
  6566. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6567. (fname (and filename (file-truename filename)))
  6568. (tbl (mapcar
  6569. (lambda (x)
  6570. (if (not (equal fname (file-truename (nth 1 x))))
  6571. (cons (concat (car x) extra " ("
  6572. (file-name-nondirectory (nth 1 x)) ")")
  6573. (cdr x))
  6574. (cons (concat (car x) extra) (cdr x))))
  6575. org-refile-target-table))
  6576. (completion-ignore-case t))
  6577. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6578. tbl)))
  6579. (defun org-olpath-completing-read (prompt collection &rest args)
  6580. "Read an outline path like a file name."
  6581. (let ((thetable collection))
  6582. (apply
  6583. 'completing-read prompt
  6584. (lambda (string predicate &optional flag)
  6585. (let (rtn r s f (l (length string)))
  6586. (cond
  6587. ((eq flag nil)
  6588. ;; try completion
  6589. (try-completion string thetable))
  6590. ((eq flag t)
  6591. ;; all-completions
  6592. (setq rtn (all-completions string thetable predicate))
  6593. (mapcar
  6594. (lambda (x)
  6595. (setq r (substring x l))
  6596. (if (string-match " ([^)]*)$" x)
  6597. (setq f (match-string 0 x))
  6598. (setq f ""))
  6599. (if (string-match "/" r)
  6600. (concat string (substring r 0 (match-end 0)) f)
  6601. x))
  6602. rtn))
  6603. ((eq flag 'lambda)
  6604. ;; exact match?
  6605. (assoc string thetable)))
  6606. ))
  6607. args)))
  6608. ;;;; Dynamic blocks
  6609. (defun org-find-dblock (name)
  6610. "Find the first dynamic block with name NAME in the buffer.
  6611. If not found, stay at current position and return nil."
  6612. (let (pos)
  6613. (save-excursion
  6614. (goto-char (point-min))
  6615. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6616. nil t)
  6617. (match-beginning 0))))
  6618. (if pos (goto-char pos))
  6619. pos))
  6620. (defconst org-dblock-start-re
  6621. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6622. "Matches the startline of a dynamic block, with parameters.")
  6623. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6624. "Matches the end of a dyhamic block.")
  6625. (defun org-create-dblock (plist)
  6626. "Create a dynamic block section, with parameters taken from PLIST.
  6627. PLIST must containe a :name entry which is used as name of the block."
  6628. (unless (bolp) (newline))
  6629. (let ((name (plist-get plist :name)))
  6630. (insert "#+BEGIN: " name)
  6631. (while plist
  6632. (if (eq (car plist) :name)
  6633. (setq plist (cddr plist))
  6634. (insert " " (prin1-to-string (pop plist)))))
  6635. (insert "\n\n#+END:\n")
  6636. (beginning-of-line -2)))
  6637. (defun org-prepare-dblock ()
  6638. "Prepare dynamic block for refresh.
  6639. This empties the block, puts the cursor at the insert position and returns
  6640. the property list including an extra property :name with the block name."
  6641. (unless (looking-at org-dblock-start-re)
  6642. (error "Not at a dynamic block"))
  6643. (let* ((begdel (1+ (match-end 0)))
  6644. (name (org-no-properties (match-string 1)))
  6645. (params (append (list :name name)
  6646. (read (concat "(" (match-string 3) ")")))))
  6647. (unless (re-search-forward org-dblock-end-re nil t)
  6648. (error "Dynamic block not terminated"))
  6649. (setq params
  6650. (append params
  6651. (list :content (buffer-substring
  6652. begdel (match-beginning 0)))))
  6653. (delete-region begdel (match-beginning 0))
  6654. (goto-char begdel)
  6655. (open-line 1)
  6656. params))
  6657. (defun org-map-dblocks (&optional command)
  6658. "Apply COMMAND to all dynamic blocks in the current buffer.
  6659. If COMMAND is not given, use `org-update-dblock'."
  6660. (let ((cmd (or command 'org-update-dblock))
  6661. pos)
  6662. (save-excursion
  6663. (goto-char (point-min))
  6664. (while (re-search-forward org-dblock-start-re nil t)
  6665. (goto-char (setq pos (match-beginning 0)))
  6666. (condition-case nil
  6667. (funcall cmd)
  6668. (error (message "Error during update of dynamic block")))
  6669. (goto-char pos)
  6670. (unless (re-search-forward org-dblock-end-re nil t)
  6671. (error "Dynamic block not terminated"))))))
  6672. (defun org-dblock-update (&optional arg)
  6673. "User command for updating dynamic blocks.
  6674. Update the dynamic block at point. With prefix ARG, update all dynamic
  6675. blocks in the buffer."
  6676. (interactive "P")
  6677. (if arg
  6678. (org-update-all-dblocks)
  6679. (or (looking-at org-dblock-start-re)
  6680. (org-beginning-of-dblock))
  6681. (org-update-dblock)))
  6682. (defun org-update-dblock ()
  6683. "Update the dynamic block at point
  6684. This means to empty the block, parse for parameters and then call
  6685. the correct writing function."
  6686. (save-window-excursion
  6687. (let* ((pos (point))
  6688. (line (org-current-line))
  6689. (params (org-prepare-dblock))
  6690. (name (plist-get params :name))
  6691. (cmd (intern (concat "org-dblock-write:" name))))
  6692. (message "Updating dynamic block `%s' at line %d..." name line)
  6693. (funcall cmd params)
  6694. (message "Updating dynamic block `%s' at line %d...done" name line)
  6695. (goto-char pos))))
  6696. (defun org-beginning-of-dblock ()
  6697. "Find the beginning of the dynamic block at point.
  6698. Error if there is no scuh block at point."
  6699. (let ((pos (point))
  6700. beg)
  6701. (end-of-line 1)
  6702. (if (and (re-search-backward org-dblock-start-re nil t)
  6703. (setq beg (match-beginning 0))
  6704. (re-search-forward org-dblock-end-re nil t)
  6705. (> (match-end 0) pos))
  6706. (goto-char beg)
  6707. (goto-char pos)
  6708. (error "Not in a dynamic block"))))
  6709. (defun org-update-all-dblocks ()
  6710. "Update all dynamic blocks in the buffer.
  6711. This function can be used in a hook."
  6712. (when (org-mode-p)
  6713. (org-map-dblocks 'org-update-dblock)))
  6714. ;;;; Completion
  6715. (defconst org-additional-option-like-keywords
  6716. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6717. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6718. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6719. "BEGIN_QUOTE" "END_QUOTE"
  6720. "BEGIN_VERSE" "END_VERSE"
  6721. "BEGIN_SRC" "END_SRC"))
  6722. (defcustom org-structure-template-alist
  6723. '(
  6724. ("s" "#+begin_src ?\n\n#+end_src"
  6725. "<src lang=\"?\">\n\n</src>")
  6726. ("e" "#+begin_example\n?\n#+end_example"
  6727. "<example>\n?\n</example>")
  6728. ("q" "#+begin_quote\n?\n#+end_quote"
  6729. "<quote>\n?\n</quote>")
  6730. ("v" "#+begin_verse\n?\n#+end_verse"
  6731. "<verse>\n?\n/verse>")
  6732. ("l" "#+begin_latex\n?\n#+end_latex"
  6733. "<literal style=\"latex\">\n?\n</literal>")
  6734. ("L" "#+latex: "
  6735. "<literal style=\"latex\">?</literal>")
  6736. ("h" "#+begin_html\n?\n#+end_html"
  6737. "<literal style=\"html\">\n?\n</literal>")
  6738. ("H" "#+html: "
  6739. "<literal style=\"html\">?</literal>")
  6740. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6741. ("A" "#+ascii: ")
  6742. ("i" "#+include %file ?"
  6743. "<include file=%file markup=\"?\">")
  6744. )
  6745. "Structure completion elements.
  6746. This is a list of abbreviation keys and values. The value gets inserted
  6747. it you type @samp{.} followed by the key and then the completion key,
  6748. usually `M-TAB'. %file will be replaced by a file name after prompting
  6749. for the file uning completion.
  6750. There are two templates for each key, the first uses the original Org syntax,
  6751. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6752. the default when the /org-mtags.el/ module has been loaded. See also the
  6753. variable `org-mtags-prefere-muse-templates'.
  6754. This is an experimental feature, it is undecided if it is going to stay in."
  6755. :group 'org-completion
  6756. :type '(repeat
  6757. (string :tag "Key")
  6758. (string :tag "Template")
  6759. (string :tag "Muse Template")))
  6760. (defun org-try-structure-completion ()
  6761. "Try to complete a structure template before point.
  6762. This looks for strings like \"<e\" on an otherwise empty line and
  6763. expands them."
  6764. (let ((l (buffer-substring (point-at-bol) (point)))
  6765. a)
  6766. (when (and (looking-at "[ \t]*$")
  6767. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  6768. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  6769. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  6770. (match-beginning 1)) a)
  6771. t)))
  6772. (defun org-complete-expand-structure-template (start cell)
  6773. "Expand a structure template."
  6774. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  6775. (rpl (nth (if musep 2 1) cell)))
  6776. (delete-region start (point))
  6777. (when (string-match "\\`#\\+" rpl)
  6778. (cond
  6779. ((bolp))
  6780. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6781. (delete-region (point-at-bol) (point)))
  6782. (t (newline))))
  6783. (setq start (point))
  6784. (if (string-match "%file" rpl)
  6785. (setq rpl (replace-match
  6786. (concat
  6787. "\""
  6788. (save-match-data
  6789. (abbreviate-file-name (read-file-name "Include file: ")))
  6790. "\"")
  6791. t t rpl)))
  6792. (insert rpl)
  6793. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6794. (defun org-complete (&optional arg)
  6795. "Perform completion on word at point.
  6796. At the beginning of a headline, this completes TODO keywords as given in
  6797. `org-todo-keywords'.
  6798. If the current word is preceded by a backslash, completes the TeX symbols
  6799. that are supported for HTML support.
  6800. If the current word is preceded by \"#+\", completes special words for
  6801. setting file options.
  6802. In the line after \"#+STARTUP:, complete valid keywords.\"
  6803. At all other locations, this simply calls the value of
  6804. `org-completion-fallback-command'."
  6805. (interactive "P")
  6806. (org-without-partial-completion
  6807. (catch 'exit
  6808. (let* ((a nil)
  6809. (end (point))
  6810. (beg1 (save-excursion
  6811. (skip-chars-backward (org-re "[:alnum:]_@"))
  6812. (point)))
  6813. (beg (save-excursion
  6814. (skip-chars-backward "a-zA-Z0-9_:$")
  6815. (point)))
  6816. (confirm (lambda (x) (stringp (car x))))
  6817. (searchhead (equal (char-before beg) ?*))
  6818. (struct
  6819. (when (and (member (char-before beg1) '(?. ?<))
  6820. (setq a (assoc (buffer-substring beg1 (point))
  6821. org-structure-template-alist)))
  6822. (org-complete-expand-structure-template (1- beg1) a)
  6823. (throw 'exit t)))
  6824. (tag (and (equal (char-before beg1) ?:)
  6825. (equal (char-after (point-at-bol)) ?*)))
  6826. (prop (and (equal (char-before beg1) ?:)
  6827. (not (equal (char-after (point-at-bol)) ?*))))
  6828. (texp (equal (char-before beg) ?\\))
  6829. (link (equal (char-before beg) ?\[))
  6830. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6831. beg)
  6832. "#+"))
  6833. (startup (string-match "^#\\+STARTUP:.*"
  6834. (buffer-substring (point-at-bol) (point))))
  6835. (completion-ignore-case opt)
  6836. (type nil)
  6837. (tbl nil)
  6838. (table (cond
  6839. (opt
  6840. (setq type :opt)
  6841. (require 'org-exp)
  6842. (append
  6843. (mapcar
  6844. (lambda (x)
  6845. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6846. (cons (match-string 2 x) (match-string 1 x)))
  6847. (org-split-string (org-get-current-options) "\n"))
  6848. (mapcar 'list org-additional-option-like-keywords)))
  6849. (startup
  6850. (setq type :startup)
  6851. org-startup-options)
  6852. (link (append org-link-abbrev-alist-local
  6853. org-link-abbrev-alist))
  6854. (texp
  6855. (setq type :tex)
  6856. org-html-entities)
  6857. ((string-match "\\`\\*+[ \t]+\\'"
  6858. (buffer-substring (point-at-bol) beg))
  6859. (setq type :todo)
  6860. (mapcar 'list org-todo-keywords-1))
  6861. (searchhead
  6862. (setq type :searchhead)
  6863. (save-excursion
  6864. (goto-char (point-min))
  6865. (while (re-search-forward org-todo-line-regexp nil t)
  6866. (push (list
  6867. (org-make-org-heading-search-string
  6868. (match-string 3) t))
  6869. tbl)))
  6870. tbl)
  6871. (tag (setq type :tag beg beg1)
  6872. (or org-tag-alist (org-get-buffer-tags)))
  6873. (prop (setq type :prop beg beg1)
  6874. (mapcar 'list (org-buffer-property-keys nil t t)))
  6875. (t (progn
  6876. (call-interactively org-completion-fallback-command)
  6877. (throw 'exit nil)))))
  6878. (pattern (buffer-substring-no-properties beg end))
  6879. (completion (try-completion pattern table confirm)))
  6880. (cond ((eq completion t)
  6881. (if (not (assoc (upcase pattern) table))
  6882. (message "Already complete")
  6883. (if (and (equal type :opt)
  6884. (not (member (car (assoc (upcase pattern) table))
  6885. org-additional-option-like-keywords)))
  6886. (insert (substring (cdr (assoc (upcase pattern) table))
  6887. (length pattern)))
  6888. (if (memq type '(:tag :prop)) (insert ":")))))
  6889. ((null completion)
  6890. (message "Can't find completion for \"%s\"" pattern)
  6891. (ding))
  6892. ((not (string= pattern completion))
  6893. (delete-region beg end)
  6894. (if (string-match " +$" completion)
  6895. (setq completion (replace-match "" t t completion)))
  6896. (insert completion)
  6897. (if (get-buffer-window "*Completions*")
  6898. (delete-window (get-buffer-window "*Completions*")))
  6899. (if (assoc completion table)
  6900. (if (eq type :todo) (insert " ")
  6901. (if (memq type '(:tag :prop)) (insert ":"))))
  6902. (if (and (equal type :opt) (assoc completion table))
  6903. (message "%s" (substitute-command-keys
  6904. "Press \\[org-complete] again to insert example settings"))))
  6905. (t
  6906. (message "Making completion list...")
  6907. (let ((list (sort (all-completions pattern table confirm)
  6908. 'string<)))
  6909. (with-output-to-temp-buffer "*Completions*"
  6910. (condition-case nil
  6911. ;; Protection needed for XEmacs and emacs 21
  6912. (display-completion-list list pattern)
  6913. (error (display-completion-list list)))))
  6914. (message "Making completion list...%s" "done")))))))
  6915. ;;;; TODO, DEADLINE, Comments
  6916. (defun org-toggle-comment ()
  6917. "Change the COMMENT state of an entry."
  6918. (interactive)
  6919. (save-excursion
  6920. (org-back-to-heading)
  6921. (let (case-fold-search)
  6922. (if (looking-at (concat outline-regexp
  6923. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6924. (replace-match "" t t nil 1)
  6925. (if (looking-at outline-regexp)
  6926. (progn
  6927. (goto-char (match-end 0))
  6928. (insert org-comment-string " ")))))))
  6929. (defvar org-last-todo-state-is-todo nil
  6930. "This is non-nil when the last TODO state change led to a TODO state.
  6931. If the last change removed the TODO tag or switched to DONE, then
  6932. this is nil.")
  6933. (defvar org-setting-tags nil) ; dynamically skiped
  6934. (defun org-parse-local-options (string var)
  6935. "Parse STRING for startup setting relevant for variable VAR."
  6936. (let ((rtn (symbol-value var))
  6937. e opts)
  6938. (save-match-data
  6939. (if (or (not string) (not (string-match "\\S-" string)))
  6940. rtn
  6941. (setq opts (delq nil (mapcar (lambda (x)
  6942. (setq e (assoc x org-startup-options))
  6943. (if (eq (nth 1 e) var) e nil))
  6944. (org-split-string string "[ \t]+"))))
  6945. (if (not opts)
  6946. rtn
  6947. (setq rtn nil)
  6948. (while (setq e (pop opts))
  6949. (if (not (nth 3 e))
  6950. (setq rtn (nth 2 e))
  6951. (if (not (listp rtn)) (setq rtn nil))
  6952. (push (nth 2 e) rtn)))
  6953. rtn)))))
  6954. (defvar org-blocker-hook nil
  6955. "Hook for functions that are allowed to block a state change.
  6956. Each function gets as its single argument a property list, see
  6957. `org-trigger-hook' for more information about this list.
  6958. If any of the functions in this hook returns nil, the state change
  6959. is blocked.")
  6960. (defvar org-trigger-hook nil
  6961. "Hook for functions that are triggered by a state change.
  6962. Each function gets as its single argument a property list with at least
  6963. the following elements:
  6964. (:type type-of-change :position pos-at-entry-start
  6965. :from old-state :to new-state)
  6966. Depending on the type, more properties may be present.
  6967. This mechanism is currently implemented for:
  6968. TODO state changes
  6969. ------------------
  6970. :type todo-state-change
  6971. :from previous state (keyword as a string), or nil
  6972. :to new state (keyword as a string), or nil")
  6973. (defun org-todo (&optional arg)
  6974. "Change the TODO state of an item.
  6975. The state of an item is given by a keyword at the start of the heading,
  6976. like
  6977. *** TODO Write paper
  6978. *** DONE Call mom
  6979. The different keywords are specified in the variable `org-todo-keywords'.
  6980. By default the available states are \"TODO\" and \"DONE\".
  6981. So for this example: when the item starts with TODO, it is changed to DONE.
  6982. When it starts with DONE, the DONE is removed. And when neither TODO nor
  6983. DONE are present, add TODO at the beginning of the heading.
  6984. With C-u prefix arg, use completion to determine the new state.
  6985. With numeric prefix arg, switch to that state.
  6986. For calling through lisp, arg is also interpreted in the following way:
  6987. 'none -> empty state
  6988. \"\"(empty string) -> switch to empty state
  6989. 'done -> switch to DONE
  6990. 'nextset -> switch to the next set of keywords
  6991. 'previousset -> switch to the previous set of keywords
  6992. \"WAITING\" -> switch to the specified keyword, but only if it
  6993. really is a member of `org-todo-keywords'."
  6994. (interactive "P")
  6995. (save-excursion
  6996. (catch 'exit
  6997. (org-back-to-heading)
  6998. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  6999. (or (looking-at (concat " +" org-todo-regexp " *"))
  7000. (looking-at " *"))
  7001. (let* ((match-data (match-data))
  7002. (startpos (point-at-bol))
  7003. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7004. (org-log-done org-log-done)
  7005. (org-log-repeat org-log-repeat)
  7006. (org-todo-log-states org-todo-log-states)
  7007. (this (match-string 1))
  7008. (hl-pos (match-beginning 0))
  7009. (head (org-get-todo-sequence-head this))
  7010. (ass (assoc head org-todo-kwd-alist))
  7011. (interpret (nth 1 ass))
  7012. (done-word (nth 3 ass))
  7013. (final-done-word (nth 4 ass))
  7014. (last-state (or this ""))
  7015. (completion-ignore-case t)
  7016. (member (member this org-todo-keywords-1))
  7017. (tail (cdr member))
  7018. (state (cond
  7019. ((and org-todo-key-trigger
  7020. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7021. (and (not arg) org-use-fast-todo-selection
  7022. (not (eq org-use-fast-todo-selection 'prefix)))))
  7023. ;; Use fast selection
  7024. (org-fast-todo-selection))
  7025. ((and (equal arg '(4))
  7026. (or (not org-use-fast-todo-selection)
  7027. (not org-todo-key-trigger)))
  7028. ;; Read a state with completion
  7029. (completing-read "State: " (mapcar (lambda(x) (list x))
  7030. org-todo-keywords-1)
  7031. nil t))
  7032. ((eq arg 'right)
  7033. (if this
  7034. (if tail (car tail) nil)
  7035. (car org-todo-keywords-1)))
  7036. ((eq arg 'left)
  7037. (if (equal member org-todo-keywords-1)
  7038. nil
  7039. (if this
  7040. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7041. org-todo-keywords-1)
  7042. (org-last org-todo-keywords-1))))
  7043. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7044. (setq arg nil))) ; hack to fall back to cycling
  7045. (arg
  7046. ;; user or caller requests a specific state
  7047. (cond
  7048. ((equal arg "") nil)
  7049. ((eq arg 'none) nil)
  7050. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7051. ((eq arg 'nextset)
  7052. (or (car (cdr (member head org-todo-heads)))
  7053. (car org-todo-heads)))
  7054. ((eq arg 'previousset)
  7055. (let ((org-todo-heads (reverse org-todo-heads)))
  7056. (or (car (cdr (member head org-todo-heads)))
  7057. (car org-todo-heads))))
  7058. ((car (member arg org-todo-keywords-1)))
  7059. ((nth (1- (prefix-numeric-value arg))
  7060. org-todo-keywords-1))))
  7061. ((null member) (or head (car org-todo-keywords-1)))
  7062. ((equal this final-done-word) nil) ;; -> make empty
  7063. ((null tail) nil) ;; -> first entry
  7064. ((eq interpret 'sequence)
  7065. (car tail))
  7066. ((memq interpret '(type priority))
  7067. (if (eq this-command last-command)
  7068. (car tail)
  7069. (if (> (length tail) 0)
  7070. (or done-word (car org-done-keywords))
  7071. nil)))
  7072. (t nil)))
  7073. (next (if state (concat " " state " ") " "))
  7074. (change-plist (list :type 'todo-state-change :from this :to state
  7075. :position startpos))
  7076. dolog now-done-p)
  7077. (when org-blocker-hook
  7078. (unless (save-excursion
  7079. (save-match-data
  7080. (run-hook-with-args-until-failure
  7081. 'org-blocker-hook change-plist)))
  7082. (if (interactive-p)
  7083. (error "TODO state change from %s to %s blocked" this state)
  7084. ;; fail silently
  7085. (message "TODO state change from %s to %s blocked" this state)
  7086. (throw 'exit nil))))
  7087. (store-match-data match-data)
  7088. (replace-match next t t)
  7089. (unless (pos-visible-in-window-p hl-pos)
  7090. (message "TODO state changed to %s" (org-trim next)))
  7091. (unless head
  7092. (setq head (org-get-todo-sequence-head state)
  7093. ass (assoc head org-todo-kwd-alist)
  7094. interpret (nth 1 ass)
  7095. done-word (nth 3 ass)
  7096. final-done-word (nth 4 ass)))
  7097. (when (memq arg '(nextset previousset))
  7098. (message "Keyword-Set %d/%d: %s"
  7099. (- (length org-todo-sets) -1
  7100. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7101. (length org-todo-sets)
  7102. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7103. (setq org-last-todo-state-is-todo
  7104. (not (member state org-done-keywords)))
  7105. (setq now-done-p (and (member state org-done-keywords)
  7106. (not (member this org-done-keywords))))
  7107. (and logging (org-local-logging logging))
  7108. (when (and (or org-todo-log-states org-log-done)
  7109. (not (memq arg '(nextset previousset))))
  7110. ;; we need to look at recording a time and note
  7111. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7112. (nth 2 (assoc this org-todo-log-states))))
  7113. (when (and state
  7114. (member state org-not-done-keywords)
  7115. (not (member this org-not-done-keywords)))
  7116. ;; This is now a todo state and was not one before
  7117. ;; If there was a CLOSED time stamp, get rid of it.
  7118. (org-add-planning-info nil nil 'closed))
  7119. (when (and now-done-p org-log-done)
  7120. ;; It is now done, and it was not done before
  7121. (org-add-planning-info 'closed (org-current-time))
  7122. (if (and (not dolog) (eq 'note org-log-done))
  7123. (org-add-log-setup 'done state 'findpos 'note)))
  7124. (when (and state dolog)
  7125. ;; This is a non-nil state, and we need to log it
  7126. (org-add-log-setup 'state state 'findpos dolog)))
  7127. ;; Fixup tag positioning
  7128. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7129. (when org-provide-todo-statistics
  7130. (org-update-parent-todo-statistics))
  7131. (run-hooks 'org-after-todo-state-change-hook)
  7132. (if (and arg (not (member state org-done-keywords)))
  7133. (setq head (org-get-todo-sequence-head state)))
  7134. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7135. ;; Do we need to trigger a repeat?
  7136. (when now-done-p (org-auto-repeat-maybe state))
  7137. ;; Fixup cursor location if close to the keyword
  7138. (if (and (outline-on-heading-p)
  7139. (not (bolp))
  7140. (save-excursion (beginning-of-line 1)
  7141. (looking-at org-todo-line-regexp))
  7142. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7143. (progn
  7144. (goto-char (or (match-end 2) (match-end 1)))
  7145. (just-one-space)))
  7146. (when org-trigger-hook
  7147. (save-excursion
  7148. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7149. (defun org-update-parent-todo-statistics ()
  7150. "Update any statistics cookie in the parent of the current headline."
  7151. (interactive)
  7152. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7153. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7154. (catch 'exit
  7155. (save-excursion
  7156. (setq level (org-up-heading-safe))
  7157. (unless (and level
  7158. (re-search-forward box-re (point-at-eol) t))
  7159. (throw 'exit nil))
  7160. (setq is-percent (match-end 2))
  7161. (save-match-data
  7162. (unless (outline-next-heading) (throw 'exit nil))
  7163. (while (looking-at org-todo-line-regexp)
  7164. (setq kwd (match-string 2))
  7165. (and kwd (setq cnt-all (1+ cnt-all)))
  7166. (and (member kwd org-done-keywords)
  7167. (setq cnt-done (1+ cnt-done)))
  7168. (condition-case nil
  7169. (org-forward-same-level 1)
  7170. (error (end-of-line 1)))))
  7171. (replace-match
  7172. (if is-percent
  7173. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7174. (format "[%d/%d]" cnt-done cnt-all)))
  7175. (run-hook-with-args 'org-after-todo-statistics-hook
  7176. cnt-done (- cnt-all cnt-done))))))
  7177. (defvar org-after-todo-statistics-hook nil
  7178. "Hook that is called after a TODO statistics cookie has been updated.
  7179. Each function is called with two arguments: the number of not-done entries
  7180. and the number of done entries.
  7181. For example, the following function, when added to this hook, will switch
  7182. an entry to DONE when all children are done, and back to TODO when new
  7183. entries are set to a TODO status. Note that this hook is only called
  7184. when there is a statistics cookie in the headline!
  7185. (defun org-summary-todo (n-done n-not-done)
  7186. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7187. (let (org-log-done org-log-states) ; turn off logging
  7188. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7189. ")
  7190. (defun org-local-logging (value)
  7191. "Get logging settings from a property VALUE."
  7192. (let* (words w a)
  7193. ;; directly set the variables, they are already local.
  7194. (setq org-log-done nil
  7195. org-log-repeat nil
  7196. org-todo-log-states nil)
  7197. (setq words (org-split-string value))
  7198. (while (setq w (pop words))
  7199. (cond
  7200. ((setq a (assoc w org-startup-options))
  7201. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7202. (set (nth 1 a) (nth 2 a))))
  7203. ((setq a (org-extract-log-state-settings w))
  7204. (and (member (car a) org-todo-keywords-1)
  7205. (push a org-todo-log-states)))))))
  7206. (defun org-get-todo-sequence-head (kwd)
  7207. "Return the head of the TODO sequence to which KWD belongs.
  7208. If KWD is not set, check if there is a text property remembering the
  7209. right sequence."
  7210. (let (p)
  7211. (cond
  7212. ((not kwd)
  7213. (or (get-text-property (point-at-bol) 'org-todo-head)
  7214. (progn
  7215. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7216. nil (point-at-eol)))
  7217. (get-text-property p 'org-todo-head))))
  7218. ((not (member kwd org-todo-keywords-1))
  7219. (car org-todo-keywords-1))
  7220. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7221. (defun org-fast-todo-selection ()
  7222. "Fast TODO keyword selection with single keys.
  7223. Returns the new TODO keyword, or nil if no state change should occur."
  7224. (let* ((fulltable org-todo-key-alist)
  7225. (done-keywords org-done-keywords) ;; needed for the faces.
  7226. (maxlen (apply 'max (mapcar
  7227. (lambda (x)
  7228. (if (stringp (car x)) (string-width (car x)) 0))
  7229. fulltable)))
  7230. (expert nil)
  7231. (fwidth (+ maxlen 3 1 3))
  7232. (ncol (/ (- (window-width) 4) fwidth))
  7233. tg cnt e c tbl
  7234. groups ingroup)
  7235. (save-window-excursion
  7236. (if expert
  7237. (set-buffer (get-buffer-create " *Org todo*"))
  7238. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7239. (erase-buffer)
  7240. (org-set-local 'org-done-keywords done-keywords)
  7241. (setq tbl fulltable cnt 0)
  7242. (while (setq e (pop tbl))
  7243. (cond
  7244. ((equal e '(:startgroup))
  7245. (push '() groups) (setq ingroup t)
  7246. (when (not (= cnt 0))
  7247. (setq cnt 0)
  7248. (insert "\n"))
  7249. (insert "{ "))
  7250. ((equal e '(:endgroup))
  7251. (setq ingroup nil cnt 0)
  7252. (insert "}\n"))
  7253. (t
  7254. (setq tg (car e) c (cdr e))
  7255. (if ingroup (push tg (car groups)))
  7256. (setq tg (org-add-props tg nil 'face
  7257. (org-get-todo-face tg)))
  7258. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7259. (insert "[" c "] " tg (make-string
  7260. (- fwidth 4 (length tg)) ?\ ))
  7261. (when (= (setq cnt (1+ cnt)) ncol)
  7262. (insert "\n")
  7263. (if ingroup (insert " "))
  7264. (setq cnt 0)))))
  7265. (insert "\n")
  7266. (goto-char (point-min))
  7267. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7268. (fit-window-to-buffer))
  7269. (message "[a-z..]:Set [SPC]:clear")
  7270. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7271. (cond
  7272. ((or (= c ?\C-g)
  7273. (and (= c ?q) (not (rassoc c fulltable))))
  7274. (setq quit-flag t))
  7275. ((= c ?\ ) nil)
  7276. ((setq e (rassoc c fulltable) tg (car e))
  7277. tg)
  7278. (t (setq quit-flag t))))))
  7279. (defun org-entry-is-todo-p ()
  7280. (member (org-get-todo-state) org-not-done-keywords))
  7281. (defun org-entry-is-done-p ()
  7282. (member (org-get-todo-state) org-done-keywords))
  7283. (defun org-get-todo-state ()
  7284. (save-excursion
  7285. (org-back-to-heading t)
  7286. (and (looking-at org-todo-line-regexp)
  7287. (match-end 2)
  7288. (match-string 2))))
  7289. (defun org-at-date-range-p (&optional inactive-ok)
  7290. "Is the cursor inside a date range?"
  7291. (interactive)
  7292. (save-excursion
  7293. (catch 'exit
  7294. (let ((pos (point)))
  7295. (skip-chars-backward "^[<\r\n")
  7296. (skip-chars-backward "<[")
  7297. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7298. (>= (match-end 0) pos)
  7299. (throw 'exit t))
  7300. (skip-chars-backward "^<[\r\n")
  7301. (skip-chars-backward "<[")
  7302. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7303. (>= (match-end 0) pos)
  7304. (throw 'exit t)))
  7305. nil)))
  7306. (defun org-get-repeat ()
  7307. "Check if there is a deadline/schedule with repeater in this entry."
  7308. (save-match-data
  7309. (save-excursion
  7310. (org-back-to-heading t)
  7311. (if (re-search-forward
  7312. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7313. (match-string 1)))))
  7314. (defvar org-last-changed-timestamp)
  7315. (defvar org-last-inserted-timestamp)
  7316. (defvar org-log-post-message)
  7317. (defvar org-log-note-purpose)
  7318. (defvar org-log-note-how)
  7319. (defvar org-log-note-extra)
  7320. (defun org-auto-repeat-maybe (done-word)
  7321. "Check if the current headline contains a repeated deadline/schedule.
  7322. If yes, set TODO state back to what it was and change the base date
  7323. of repeating deadline/scheduled time stamps to new date.
  7324. This function is run automatically after each state change to a DONE state."
  7325. ;; last-state is dynamically scoped into this function
  7326. (let* ((repeat (org-get-repeat))
  7327. (aa (assoc last-state org-todo-kwd-alist))
  7328. (interpret (nth 1 aa))
  7329. (head (nth 2 aa))
  7330. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7331. (msg "Entry repeats: ")
  7332. (org-log-done nil)
  7333. (org-todo-log-states nil)
  7334. (nshiftmax 10) (nshift 0)
  7335. re type n what ts mb0 time)
  7336. (when repeat
  7337. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7338. (org-todo (if (eq interpret 'type) last-state head))
  7339. (when org-log-repeat
  7340. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7341. (memq 'org-add-log-note post-command-hook))
  7342. ;; OK, we are already setup for some record
  7343. (if (eq org-log-repeat 'note)
  7344. ;; make sure we take a note, not only a time stamp
  7345. (setq org-log-note-how 'note))
  7346. ;; Set up for taking a record
  7347. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7348. 'findpos org-log-repeat)))
  7349. (org-back-to-heading t)
  7350. (org-add-planning-info nil nil 'closed)
  7351. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7352. org-deadline-time-regexp "\\)\\|\\("
  7353. org-ts-regexp "\\)"))
  7354. (while (re-search-forward
  7355. re (save-excursion (outline-next-heading) (point)) t)
  7356. (setq type (if (match-end 1) org-scheduled-string
  7357. (if (match-end 3) org-deadline-string "Plain:"))
  7358. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7359. mb0 (match-beginning 0))
  7360. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7361. (setq n (string-to-number (match-string 2 ts))
  7362. what (match-string 3 ts))
  7363. (if (equal what "w") (setq n (* n 7) what "d"))
  7364. ;; Preparation, see if we need to modify the start date for the change
  7365. (when (match-end 1)
  7366. (setq time (save-match-data (org-time-string-to-time ts)))
  7367. (cond
  7368. ((equal (match-string 1 ts) ".")
  7369. ;; Shift starting date to today
  7370. (org-timestamp-change
  7371. (- (time-to-days (current-time)) (time-to-days time))
  7372. 'day))
  7373. ((equal (match-string 1 ts) "+")
  7374. (while (or (= nshift 0)
  7375. (<= (time-to-days time) (time-to-days (current-time))))
  7376. (when (= (incf nshift) nshiftmax)
  7377. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7378. (error "Abort")))
  7379. (org-timestamp-change n (cdr (assoc what whata)))
  7380. (org-at-timestamp-p t)
  7381. (setq ts (match-string 1))
  7382. (setq time (save-match-data (org-time-string-to-time ts))))
  7383. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7384. ;; rematch, so that we have everything in place for the real shift
  7385. (org-at-timestamp-p t)
  7386. (setq ts (match-string 1))
  7387. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7388. (org-timestamp-change n (cdr (assoc what whata)))
  7389. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7390. (setq org-log-post-message msg)
  7391. (message "%s" msg))))
  7392. (defun org-show-todo-tree (arg)
  7393. "Make a compact tree which shows all headlines marked with TODO.
  7394. The tree will show the lines where the regexp matches, and all higher
  7395. headlines above the match.
  7396. With a \\[universal-argument] prefix, also show the DONE entries.
  7397. With a numeric prefix N, construct a sparse tree for the Nth element
  7398. of `org-todo-keywords-1'."
  7399. (interactive "P")
  7400. (let ((case-fold-search nil)
  7401. (kwd-re
  7402. (cond ((null arg) org-not-done-regexp)
  7403. ((equal arg '(4))
  7404. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7405. (mapcar 'list org-todo-keywords-1))))
  7406. (concat "\\("
  7407. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7408. "\\)\\>")))
  7409. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7410. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7411. org-todo-keywords-1)))
  7412. (t (error "Invalid prefix argument: %s" arg)))))
  7413. (message "%d TODO entries found"
  7414. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7415. (defun org-deadline (&optional remove time)
  7416. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7417. With argument REMOVE, remove any deadline from the item.
  7418. When TIME is set, it should be an internal time specification, and the
  7419. scheduling will use the corresponding date."
  7420. (interactive "P")
  7421. (if remove
  7422. (progn
  7423. (org-remove-timestamp-with-keyword org-deadline-string)
  7424. (message "Item no longer has a deadline."))
  7425. (if (org-get-repeat)
  7426. (error "Cannot change deadline on task with repeater, please do that by hand")
  7427. (org-add-planning-info 'deadline time 'closed)
  7428. (message "Deadline on %s" org-last-inserted-timestamp))))
  7429. (defun org-schedule (&optional remove time)
  7430. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7431. With argument REMOVE, remove any scheduling date from the item.
  7432. When TIME is set, it should be an internal time specification, and the
  7433. scheduling will use the corresponding date."
  7434. (interactive "P")
  7435. (if remove
  7436. (progn
  7437. (org-remove-timestamp-with-keyword org-scheduled-string)
  7438. (message "Item is no longer scheduled."))
  7439. (if (org-get-repeat)
  7440. (error "Cannot reschedule task with repeater, please do that by hand")
  7441. (org-add-planning-info 'scheduled time 'closed)
  7442. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7443. (defun org-remove-timestamp-with-keyword (keyword)
  7444. "Remove all time stamps with KEYWORD in the current entry."
  7445. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7446. beg)
  7447. (save-excursion
  7448. (org-back-to-heading t)
  7449. (setq beg (point))
  7450. (org-end-of-subtree t t)
  7451. (while (re-search-backward re beg t)
  7452. (replace-match "")
  7453. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7454. (equal (char-before) ?\ ))
  7455. (backward-delete-char 1)
  7456. (if (string-match "^[ \t]*$" (buffer-substring
  7457. (point-at-bol) (point-at-eol)))
  7458. (delete-region (point-at-bol)
  7459. (min (point-max) (1+ (point-at-eol))))))))))
  7460. (defun org-add-planning-info (what &optional time &rest remove)
  7461. "Insert new timestamp with keyword in the line directly after the headline.
  7462. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7463. If non is given, the user is prompted for a date.
  7464. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7465. be removed."
  7466. (interactive)
  7467. (let (org-time-was-given org-end-time-was-given ts
  7468. end default-time default-input)
  7469. (when (and (not time) (memq what '(scheduled deadline)))
  7470. ;; Try to get a default date/time from existing timestamp
  7471. (save-excursion
  7472. (org-back-to-heading t)
  7473. (setq end (save-excursion (outline-next-heading) (point)))
  7474. (when (re-search-forward (if (eq what 'scheduled)
  7475. org-scheduled-time-regexp
  7476. org-deadline-time-regexp)
  7477. end t)
  7478. (setq ts (match-string 1)
  7479. default-time
  7480. (apply 'encode-time (org-parse-time-string ts))
  7481. default-input (and ts (org-get-compact-tod ts))))))
  7482. (when what
  7483. ;; If necessary, get the time from the user
  7484. (setq time (or time (org-read-date nil 'to-time nil nil
  7485. default-time default-input))))
  7486. (when (and org-insert-labeled-timestamps-at-point
  7487. (member what '(scheduled deadline)))
  7488. (insert
  7489. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7490. (org-insert-time-stamp time org-time-was-given
  7491. nil nil nil (list org-end-time-was-given))
  7492. (setq what nil))
  7493. (save-excursion
  7494. (save-restriction
  7495. (let (col list elt ts buffer-invisibility-spec)
  7496. (org-back-to-heading t)
  7497. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7498. (goto-char (match-end 1))
  7499. (setq col (current-column))
  7500. (goto-char (match-end 0))
  7501. (if (eobp) (insert "\n") (forward-char 1))
  7502. (if (and (not (looking-at outline-regexp))
  7503. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7504. "[^\r\n]*"))
  7505. (not (equal (match-string 1) org-clock-string)))
  7506. (narrow-to-region (match-beginning 0) (match-end 0))
  7507. (insert-before-markers "\n")
  7508. (backward-char 1)
  7509. (narrow-to-region (point) (point))
  7510. (and org-adapt-indentation (org-indent-to-column col)))
  7511. ;; Check if we have to remove something.
  7512. (setq list (cons what remove))
  7513. (while list
  7514. (setq elt (pop list))
  7515. (goto-char (point-min))
  7516. (when (or (and (eq elt 'scheduled)
  7517. (re-search-forward org-scheduled-time-regexp nil t))
  7518. (and (eq elt 'deadline)
  7519. (re-search-forward org-deadline-time-regexp nil t))
  7520. (and (eq elt 'closed)
  7521. (re-search-forward org-closed-time-regexp nil t)))
  7522. (replace-match "")
  7523. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7524. (if (looking-at " +") (replace-match ""))))
  7525. (goto-char (point-max))
  7526. (when what
  7527. (insert
  7528. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7529. (cond ((eq what 'scheduled) org-scheduled-string)
  7530. ((eq what 'deadline) org-deadline-string)
  7531. ((eq what 'closed) org-closed-string))
  7532. " ")
  7533. (setq ts (org-insert-time-stamp
  7534. time
  7535. (or org-time-was-given
  7536. (and (eq what 'closed) org-log-done-with-time))
  7537. (eq what 'closed)
  7538. nil nil (list org-end-time-was-given)))
  7539. (end-of-line 1))
  7540. (goto-char (point-min))
  7541. (widen)
  7542. (if (and (looking-at "[ \t]+\n")
  7543. (equal (char-before) ?\n))
  7544. (delete-region (1- (point)) (point-at-eol)))
  7545. ts)))))
  7546. (defvar org-log-note-marker (make-marker))
  7547. (defvar org-log-note-purpose nil)
  7548. (defvar org-log-note-state nil)
  7549. (defvar org-log-note-how nil)
  7550. (defvar org-log-note-extra nil)
  7551. (defvar org-log-note-window-configuration nil)
  7552. (defvar org-log-note-return-to (make-marker))
  7553. (defvar org-log-post-message nil
  7554. "Message to be displayed after a log note has been stored.
  7555. The auto-repeater uses this.")
  7556. (defun org-add-note ()
  7557. "Add a note to the current entry.
  7558. This is done in the same way as adding a state change note."
  7559. (interactive)
  7560. (org-add-log-setup 'note nil 'findpos nil))
  7561. (defvar org-property-end-re)
  7562. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7563. "Set up the post command hook to take a note.
  7564. If this is about to TODO state change, the new state is expected in STATE.
  7565. When FINDPOS is non-nil, find the correct position for the note in
  7566. the current entry. If not, assume that it can be inserted at point.
  7567. HOW is an indicator what kind of note should be created.
  7568. EXTRA is additional text that will be inserted into the notes buffer."
  7569. (save-restriction
  7570. (save-excursion
  7571. (when findpos
  7572. (org-back-to-heading t)
  7573. (narrow-to-region (point) (save-excursion
  7574. (outline-next-heading) (point)))
  7575. (while (re-search-forward
  7576. (concat "\\(" org-drawer-regexp "\\|" org-property-end-re "\\)")
  7577. (point-max) t) (forward-line))
  7578. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7579. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7580. "[^\r\n]*\\)?"))
  7581. (goto-char (match-end 0))
  7582. (unless org-log-states-order-reversed
  7583. (and (= (char-after) ?\n) (forward-char 1))
  7584. (org-skip-over-state-notes)
  7585. (skip-chars-backward " \t\n\r")))
  7586. (move-marker org-log-note-marker (point))
  7587. (setq org-log-note-purpose purpose
  7588. org-log-note-state state
  7589. org-log-note-how how
  7590. org-log-note-extra extra)
  7591. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7592. (defun org-skip-over-state-notes ()
  7593. "Skip past the list of State notes in an entry."
  7594. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7595. (while (looking-at "[ \t]*- State")
  7596. (condition-case nil
  7597. (org-next-item)
  7598. (error (org-end-of-item)))))
  7599. (defun org-add-log-note (&optional purpose)
  7600. "Pop up a window for taking a note, and add this note later at point."
  7601. (remove-hook 'post-command-hook 'org-add-log-note)
  7602. (setq org-log-note-window-configuration (current-window-configuration))
  7603. (delete-other-windows)
  7604. (move-marker org-log-note-return-to (point))
  7605. (switch-to-buffer (marker-buffer org-log-note-marker))
  7606. (goto-char org-log-note-marker)
  7607. (org-switch-to-buffer-other-window "*Org Note*")
  7608. (erase-buffer)
  7609. (if (memq org-log-note-how '(time state))
  7610. (org-store-log-note)
  7611. (let ((org-inhibit-startup t)) (org-mode))
  7612. (insert (format "# Insert note for %s.
  7613. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7614. (cond
  7615. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7616. ((eq org-log-note-purpose 'done) "closed todo item")
  7617. ((eq org-log-note-purpose 'state)
  7618. (format "state change to \"%s\"" org-log-note-state))
  7619. ((eq org-log-note-purpose 'note)
  7620. "this entry")
  7621. (t (error "This should not happen")))))
  7622. (if org-log-note-extra (insert org-log-note-extra))
  7623. (org-set-local 'org-finish-function 'org-store-log-note)))
  7624. (defvar org-note-abort nil) ; dynamically scoped
  7625. (defun org-store-log-note ()
  7626. "Finish taking a log note, and insert it to where it belongs."
  7627. (let ((txt (buffer-string))
  7628. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7629. lines ind)
  7630. (kill-buffer (current-buffer))
  7631. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7632. (setq txt (replace-match "" t t txt)))
  7633. (if (string-match "\\s-+\\'" txt)
  7634. (setq txt (replace-match "" t t txt)))
  7635. (setq lines (org-split-string txt "\n"))
  7636. (when (and note (string-match "\\S-" note))
  7637. (setq note
  7638. (org-replace-escapes
  7639. note
  7640. (list (cons "%u" (user-login-name))
  7641. (cons "%U" user-full-name)
  7642. (cons "%t" (format-time-string
  7643. (org-time-stamp-format 'long 'inactive)
  7644. (current-time)))
  7645. (cons "%s" (if org-log-note-state
  7646. (concat "\"" org-log-note-state "\"")
  7647. "")))))
  7648. (if lines (setq note (concat note " \\\\")))
  7649. (push note lines))
  7650. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7651. (when lines
  7652. (save-excursion
  7653. (set-buffer (marker-buffer org-log-note-marker))
  7654. (save-excursion
  7655. (goto-char org-log-note-marker)
  7656. (move-marker org-log-note-marker nil)
  7657. (end-of-line 1)
  7658. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7659. (indent-relative nil)
  7660. (insert "- " (pop lines))
  7661. (org-indent-line-function)
  7662. (beginning-of-line 1)
  7663. (looking-at "[ \t]*")
  7664. (setq ind (concat (match-string 0) " "))
  7665. (end-of-line 1)
  7666. (while lines (insert "\n" ind (pop lines)))))))
  7667. (set-window-configuration org-log-note-window-configuration)
  7668. (with-current-buffer (marker-buffer org-log-note-return-to)
  7669. (goto-char org-log-note-return-to))
  7670. (move-marker org-log-note-return-to nil)
  7671. (and org-log-post-message (message "%s" org-log-post-message)))
  7672. (defun org-sparse-tree (&optional arg)
  7673. "Create a sparse tree, prompt for the details.
  7674. This command can create sparse trees. You first need to select the type
  7675. of match used to create the tree:
  7676. t Show entries with a specific TODO keyword.
  7677. T Show entries selected by a tags match.
  7678. p Enter a property name and its value (both with completion on existing
  7679. names/values) and show entries with that property.
  7680. r Show entries matching a regular expression
  7681. d Show deadlines due within `org-deadline-warning-days'."
  7682. (interactive "P")
  7683. (let (ans kwd value)
  7684. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7685. (setq ans (read-char-exclusive))
  7686. (cond
  7687. ((equal ans ?d)
  7688. (call-interactively 'org-check-deadlines))
  7689. ((equal ans ?b)
  7690. (call-interactively 'org-check-before-date))
  7691. ((equal ans ?t)
  7692. (org-show-todo-tree '(4)))
  7693. ((equal ans ?T)
  7694. (call-interactively 'org-tags-sparse-tree))
  7695. ((member ans '(?p ?P))
  7696. (setq kwd (completing-read "Property: "
  7697. (mapcar 'list (org-buffer-property-keys))))
  7698. (setq value (completing-read "Value: "
  7699. (mapcar 'list (org-property-values kwd))))
  7700. (unless (string-match "\\`{.*}\\'" value)
  7701. (setq value (concat "\"" value "\"")))
  7702. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7703. ((member ans '(?r ?R ?/))
  7704. (call-interactively 'org-occur))
  7705. (t (error "No such sparse tree command \"%c\"" ans)))))
  7706. (defvar org-occur-highlights nil
  7707. "List of overlays used for occur matches.")
  7708. (make-variable-buffer-local 'org-occur-highlights)
  7709. (defvar org-occur-parameters nil
  7710. "Parameters of the active org-occur calls.
  7711. This is a list, each call to org-occur pushes as cons cell,
  7712. containing the regular expression and the callback, onto the list.
  7713. The list can contain several entries if `org-occur' has been called
  7714. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7715. will only contain one set of parameters. When the highlights are
  7716. removed (for example with `C-c C-c', or with the next edit (depending
  7717. on `org-remove-highlights-with-change'), this variable is emptied
  7718. as well.")
  7719. (make-variable-buffer-local 'org-occur-parameters)
  7720. (defun org-occur (regexp &optional keep-previous callback)
  7721. "Make a compact tree which shows all matches of REGEXP.
  7722. The tree will show the lines where the regexp matches, and all higher
  7723. headlines above the match. It will also show the heading after the match,
  7724. to make sure editing the matching entry is easy.
  7725. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7726. call to `org-occur' will be kept, to allow stacking of calls to this
  7727. command.
  7728. If CALLBACK is non-nil, it is a function which is called to confirm
  7729. that the match should indeed be shown."
  7730. (interactive "sRegexp: \nP")
  7731. (unless keep-previous
  7732. (org-remove-occur-highlights nil nil t))
  7733. (push (cons regexp callback) org-occur-parameters)
  7734. (let ((cnt 0))
  7735. (save-excursion
  7736. (goto-char (point-min))
  7737. (if (or (not keep-previous) ; do not want to keep
  7738. (not org-occur-highlights)) ; no previous matches
  7739. ;; hide everything
  7740. (org-overview))
  7741. (while (re-search-forward regexp nil t)
  7742. (when (or (not callback)
  7743. (save-match-data (funcall callback)))
  7744. (setq cnt (1+ cnt))
  7745. (when org-highlight-sparse-tree-matches
  7746. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7747. (org-show-context 'occur-tree))))
  7748. (when org-remove-highlights-with-change
  7749. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7750. nil 'local))
  7751. (unless org-sparse-tree-open-archived-trees
  7752. (org-hide-archived-subtrees (point-min) (point-max)))
  7753. (run-hooks 'org-occur-hook)
  7754. (if (interactive-p)
  7755. (message "%d match(es) for regexp %s" cnt regexp))
  7756. cnt))
  7757. (defun org-show-context (&optional key)
  7758. "Make sure point and context and visible.
  7759. How much context is shown depends upon the variables
  7760. `org-show-hierarchy-above', `org-show-following-heading'. and
  7761. `org-show-siblings'."
  7762. (let ((heading-p (org-on-heading-p t))
  7763. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7764. (following-p (org-get-alist-option org-show-following-heading key))
  7765. (entry-p (org-get-alist-option org-show-entry-below key))
  7766. (siblings-p (org-get-alist-option org-show-siblings key)))
  7767. (catch 'exit
  7768. ;; Show heading or entry text
  7769. (if (and heading-p (not entry-p))
  7770. (org-flag-heading nil) ; only show the heading
  7771. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7772. (org-show-hidden-entry))) ; show entire entry
  7773. (when following-p
  7774. ;; Show next sibling, or heading below text
  7775. (save-excursion
  7776. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7777. (org-flag-heading nil))))
  7778. (when siblings-p (org-show-siblings))
  7779. (when hierarchy-p
  7780. ;; show all higher headings, possibly with siblings
  7781. (save-excursion
  7782. (while (and (condition-case nil
  7783. (progn (org-up-heading-all 1) t)
  7784. (error nil))
  7785. (not (bobp)))
  7786. (org-flag-heading nil)
  7787. (when siblings-p (org-show-siblings))))))))
  7788. (defun org-reveal (&optional siblings)
  7789. "Show current entry, hierarchy above it, and the following headline.
  7790. This can be used to show a consistent set of context around locations
  7791. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7792. not t for the search context.
  7793. With optional argument SIBLINGS, on each level of the hierarchy all
  7794. siblings are shown. This repairs the tree structure to what it would
  7795. look like when opened with hierarchical calls to `org-cycle'."
  7796. (interactive "P")
  7797. (let ((org-show-hierarchy-above t)
  7798. (org-show-following-heading t)
  7799. (org-show-siblings (if siblings t org-show-siblings)))
  7800. (org-show-context nil)))
  7801. (defun org-highlight-new-match (beg end)
  7802. "Highlight from BEG to END and mark the highlight is an occur headline."
  7803. (let ((ov (org-make-overlay beg end)))
  7804. (org-overlay-put ov 'face 'secondary-selection)
  7805. (push ov org-occur-highlights)))
  7806. (defun org-remove-occur-highlights (&optional beg end noremove)
  7807. "Remove the occur highlights from the buffer.
  7808. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7809. from the `before-change-functions' in the current buffer."
  7810. (interactive)
  7811. (unless org-inhibit-highlight-removal
  7812. (mapc 'org-delete-overlay org-occur-highlights)
  7813. (setq org-occur-highlights nil)
  7814. (setq org-occur-parameters nil)
  7815. (unless noremove
  7816. (remove-hook 'before-change-functions
  7817. 'org-remove-occur-highlights 'local))))
  7818. ;;;; Priorities
  7819. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7820. "Regular expression matching the priority indicator.")
  7821. (defvar org-remove-priority-next-time nil)
  7822. (defun org-priority-up ()
  7823. "Increase the priority of the current item."
  7824. (interactive)
  7825. (org-priority 'up))
  7826. (defun org-priority-down ()
  7827. "Decrease the priority of the current item."
  7828. (interactive)
  7829. (org-priority 'down))
  7830. (defun org-priority (&optional action)
  7831. "Change the priority of an item by ARG.
  7832. ACTION can be `set', `up', `down', or a character."
  7833. (interactive)
  7834. (setq action (or action 'set))
  7835. (let (current new news have remove)
  7836. (save-excursion
  7837. (org-back-to-heading)
  7838. (if (looking-at org-priority-regexp)
  7839. (setq current (string-to-char (match-string 2))
  7840. have t)
  7841. (setq current org-default-priority))
  7842. (cond
  7843. ((or (eq action 'set)
  7844. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7845. (if (not (eq action 'set))
  7846. (setq new action)
  7847. (message "Priority %c-%c, SPC to remove: "
  7848. org-highest-priority org-lowest-priority)
  7849. (setq new (read-char-exclusive)))
  7850. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7851. (= (upcase org-lowest-priority) org-lowest-priority))
  7852. (setq new (upcase new)))
  7853. (cond ((equal new ?\ ) (setq remove t))
  7854. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7855. (error "Priority must be between `%c' and `%c'"
  7856. org-highest-priority org-lowest-priority))))
  7857. ((eq action 'up)
  7858. (if (and (not have) (eq last-command this-command))
  7859. (setq new org-lowest-priority)
  7860. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7861. org-default-priority (1- current)))))
  7862. ((eq action 'down)
  7863. (if (and (not have) (eq last-command this-command))
  7864. (setq new org-highest-priority)
  7865. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7866. org-default-priority (1+ current)))))
  7867. (t (error "Invalid action")))
  7868. (if (or (< (upcase new) org-highest-priority)
  7869. (> (upcase new) org-lowest-priority))
  7870. (setq remove t))
  7871. (setq news (format "%c" new))
  7872. (if have
  7873. (if remove
  7874. (replace-match "" t t nil 1)
  7875. (replace-match news t t nil 2))
  7876. (if remove
  7877. (error "No priority cookie found in line")
  7878. (looking-at org-todo-line-regexp)
  7879. (if (match-end 2)
  7880. (progn
  7881. (goto-char (match-end 2))
  7882. (insert " [#" news "]"))
  7883. (goto-char (match-beginning 3))
  7884. (insert "[#" news "] ")))))
  7885. (org-preserve-lc (org-set-tags nil 'align))
  7886. (if remove
  7887. (message "Priority removed")
  7888. (message "Priority of current item set to %s" news))))
  7889. (defun org-get-priority (s)
  7890. "Find priority cookie and return priority."
  7891. (save-match-data
  7892. (if (not (string-match org-priority-regexp s))
  7893. (* 1000 (- org-lowest-priority org-default-priority))
  7894. (* 1000 (- org-lowest-priority
  7895. (string-to-char (match-string 2 s)))))))
  7896. ;;;; Tags
  7897. (defvar org-agenda-archives-mode)
  7898. (defun org-scan-tags (action matcher &optional todo-only)
  7899. "Scan headline tags with inheritance and produce output ACTION.
  7900. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  7901. or `agenda' to produce an entry list for an agenda view. It can also be
  7902. a Lisp form or a function that should be called at each matched headline, in
  7903. this case the return value is a list of all return values from these calls.
  7904. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  7905. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  7906. only lines with a TODO keyword are included in the output."
  7907. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7908. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7909. (org-re
  7910. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7911. (props (list 'face 'default
  7912. 'done-face 'org-done
  7913. 'undone-face 'default
  7914. 'mouse-face 'highlight
  7915. 'org-not-done-regexp org-not-done-regexp
  7916. 'org-todo-regexp org-todo-regexp
  7917. 'keymap org-agenda-keymap
  7918. 'help-echo
  7919. (format "mouse-2 or RET jump to org file %s"
  7920. (abbreviate-file-name
  7921. (or (buffer-file-name (buffer-base-buffer))
  7922. (buffer-name (buffer-base-buffer)))))))
  7923. (case-fold-search nil)
  7924. lspos tags tags-list
  7925. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  7926. (llast 0) rtn rtn1 level category i txt
  7927. todo marker entry priority)
  7928. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  7929. (setq action (list 'lambda nil action)))
  7930. (save-excursion
  7931. (goto-char (point-min))
  7932. (when (eq action 'sparse-tree)
  7933. (org-overview)
  7934. (org-remove-occur-highlights))
  7935. (while (re-search-forward re nil t)
  7936. (catch :skip
  7937. (setq todo (if (match-end 1) (match-string 2))
  7938. tags (if (match-end 4) (match-string 4)))
  7939. (goto-char (setq lspos (1+ (match-beginning 0))))
  7940. (setq level (org-reduced-level (funcall outline-level))
  7941. category (org-get-category))
  7942. (setq i llast llast level)
  7943. ;; remove tag lists from same and sublevels
  7944. (while (>= i level)
  7945. (when (setq entry (assoc i tags-alist))
  7946. (setq tags-alist (delete entry tags-alist)))
  7947. (setq i (1- i)))
  7948. ;; add the next tags
  7949. (when tags
  7950. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7951. tags-alist
  7952. (cons (cons level tags) tags-alist)))
  7953. ;; compile tags for current headline
  7954. (setq tags-list
  7955. (if org-use-tag-inheritance
  7956. (apply 'append (mapcar 'cdr tags-alist))
  7957. tags))
  7958. (when (and tags org-use-tag-inheritance
  7959. (not (eq t org-use-tag-inheritance)))
  7960. ;; selective inheritance, remove uninherited ones
  7961. (setcdr (car tags-alist)
  7962. (org-remove-uniherited-tags (cdar tags-alist))))
  7963. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  7964. (let ((case-fold-search t)) (eval matcher))
  7965. (or
  7966. (not (member org-archive-tag tags-list))
  7967. ;; we have an archive tag, should we use this anyway?
  7968. (or (not org-agenda-skip-archived-trees)
  7969. (and (eq action 'agenda) org-agenda-archives-mode))))
  7970. (unless (eq action 'sparse-tree) (org-agenda-skip))
  7971. ;; select this headline
  7972. (cond
  7973. ((eq action 'sparse-tree)
  7974. (and org-highlight-sparse-tree-matches
  7975. (org-get-heading) (match-end 0)
  7976. (org-highlight-new-match
  7977. (match-beginning 0) (match-beginning 1)))
  7978. (org-show-context 'tags-tree))
  7979. ((eq action 'agenda)
  7980. (setq txt (org-format-agenda-item
  7981. ""
  7982. (concat
  7983. (if org-tags-match-list-sublevels
  7984. (make-string (1- level) ?.) "")
  7985. (org-get-heading))
  7986. category tags-list)
  7987. priority (org-get-priority txt))
  7988. (goto-char lspos)
  7989. (setq marker (org-agenda-new-marker))
  7990. (org-add-props txt props
  7991. 'org-marker marker 'org-hd-marker marker 'org-category category
  7992. 'priority priority 'type "tagsmatch")
  7993. (push txt rtn))
  7994. ((functionp action)
  7995. (save-excursion
  7996. (setq rtn1 (funcall action))
  7997. (push rtn1 rtn))
  7998. (goto-char (point-at-eol)))
  7999. (t (error "Invalid action")))
  8000. ;; if we are to skip sublevels, jump to end of subtree
  8001. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8002. (when (and (eq action 'sparse-tree)
  8003. (not org-sparse-tree-open-archived-trees))
  8004. (org-hide-archived-subtrees (point-min) (point-max)))
  8005. (nreverse rtn)))
  8006. (defun org-remove-uniherited-tags (tags)
  8007. "Remove all tags that are not inherited from the list TAGS."
  8008. (cond
  8009. ((eq org-use-tag-inheritance t) tags)
  8010. ((not org-use-tag-inheritance) nil)
  8011. ((stringp org-use-tag-inheritance)
  8012. (delq nil (mapcar
  8013. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8014. tags)))
  8015. ((listp org-use-tag-inheritance)
  8016. (delq nil (mapcar
  8017. (lambda (x) (if (member x org-use-tag-inheritance) x nil))
  8018. tags)))))
  8019. (defvar todo-only) ;; dynamically scoped
  8020. (defun org-tags-sparse-tree (&optional todo-only match)
  8021. "Create a sparse tree according to tags string MATCH.
  8022. MATCH can contain positive and negative selection of tags, like
  8023. \"+WORK+URGENT-WITHBOSS\".
  8024. If optional argument TODO_ONLY is non-nil, only select lines that are
  8025. also TODO lines."
  8026. (interactive "P")
  8027. (org-prepare-agenda-buffers (list (current-buffer)))
  8028. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8029. (defvar org-cached-props nil)
  8030. (defun org-cached-entry-get (pom property)
  8031. (if (or (eq t org-use-property-inheritance)
  8032. (and (stringp org-use-property-inheritance)
  8033. (string-match org-use-property-inheritance property))
  8034. (and (listp org-use-property-inheritance)
  8035. (member property org-use-property-inheritance)))
  8036. ;; Caching is not possible, check it directly
  8037. (org-entry-get pom property 'inherit)
  8038. ;; Get all properties, so that we can do complicated checks easily
  8039. (cdr (assoc property (or org-cached-props
  8040. (setq org-cached-props
  8041. (org-entry-properties pom)))))))
  8042. (defun org-global-tags-completion-table (&optional files)
  8043. "Return the list of all tags in all agenda buffer/files."
  8044. (save-excursion
  8045. (org-uniquify
  8046. (delq nil
  8047. (apply 'append
  8048. (mapcar
  8049. (lambda (file)
  8050. (set-buffer (find-file-noselect file))
  8051. (append (org-get-buffer-tags)
  8052. (mapcar (lambda (x) (if (stringp (car-safe x))
  8053. (list (car-safe x)) nil))
  8054. org-tag-alist)))
  8055. (if (and files (car files))
  8056. files
  8057. (org-agenda-files))))))))
  8058. (defun org-make-tags-matcher (match)
  8059. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8060. ;; todo-only is scoped dynamically into this function, and the function
  8061. ;; may change it it the matcher asksk for it.
  8062. (unless match
  8063. ;; Get a new match request, with completion
  8064. (let ((org-last-tags-completion-table
  8065. (org-global-tags-completion-table)))
  8066. (setq match (completing-read
  8067. "Match: " 'org-tags-completion-function nil nil nil
  8068. 'org-tags-history))))
  8069. ;; Parse the string and create a lisp form
  8070. (let ((match0 match)
  8071. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8072. minus tag mm
  8073. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8074. orterms term orlist re-p str-p level-p level-op time-p
  8075. prop-p pn pv po cat-p gv)
  8076. (if (string-match "/+" match)
  8077. ;; match contains also a todo-matching request
  8078. (progn
  8079. (setq tagsmatch (substring match 0 (match-beginning 0))
  8080. todomatch (substring match (match-end 0)))
  8081. (if (string-match "^!" todomatch)
  8082. (setq todo-only t todomatch (substring todomatch 1)))
  8083. (if (string-match "^\\s-*$" todomatch)
  8084. (setq todomatch nil)))
  8085. ;; only matching tags
  8086. (setq tagsmatch match todomatch nil))
  8087. ;; Make the tags matcher
  8088. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8089. (setq tagsmatcher t)
  8090. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8091. (while (setq term (pop orterms))
  8092. (while (and (equal (substring term -1) "\\") orterms)
  8093. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8094. (while (string-match re term)
  8095. (setq minus (and (match-end 1)
  8096. (equal (match-string 1 term) "-"))
  8097. tag (match-string 2 term)
  8098. re-p (equal (string-to-char tag) ?{)
  8099. level-p (match-end 4)
  8100. prop-p (match-end 5)
  8101. mm (cond
  8102. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8103. (level-p
  8104. (setq level-op (org-op-to-function (match-string 3 term)))
  8105. `(,level-op level ,(string-to-number
  8106. (match-string 4 term))))
  8107. (prop-p
  8108. (setq pn (match-string 5 term)
  8109. po (match-string 6 term)
  8110. pv (match-string 7 term)
  8111. cat-p (equal pn "CATEGORY")
  8112. re-p (equal (string-to-char pv) ?{)
  8113. str-p (equal (string-to-char pv) ?\")
  8114. time-p (save-match-data (string-match "^\"<.*>\"$" pv))
  8115. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8116. (if time-p (setq pv (org-matcher-time pv)))
  8117. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8118. (if (equal pn "CATEGORY")
  8119. (setq gv '(get-text-property (point) 'org-category))
  8120. (setq gv `(org-cached-entry-get nil ,pn)))
  8121. (if re-p
  8122. (if (eq po 'org<>)
  8123. `(not (string-match ,pv (or ,gv "")))
  8124. `(string-match ,pv (or ,gv "")))
  8125. (if str-p
  8126. `(,po (or ,gv "") ,pv)
  8127. `(,po (string-to-number (or ,gv ""))
  8128. ,(string-to-number pv) ))))
  8129. (t `(member ,(downcase tag) tags-list)))
  8130. mm (if minus (list 'not mm) mm)
  8131. term (substring term (match-end 0)))
  8132. (push mm tagsmatcher))
  8133. (push (if (> (length tagsmatcher) 1)
  8134. (cons 'and tagsmatcher)
  8135. (car tagsmatcher))
  8136. orlist)
  8137. (setq tagsmatcher nil))
  8138. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8139. (setq tagsmatcher
  8140. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8141. ;; Make the todo matcher
  8142. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8143. (setq todomatcher t)
  8144. (setq orterms (org-split-string todomatch "|") orlist nil)
  8145. (while (setq term (pop orterms))
  8146. (while (string-match re term)
  8147. (setq minus (and (match-end 1)
  8148. (equal (match-string 1 term) "-"))
  8149. kwd (match-string 2 term)
  8150. re-p (equal (string-to-char kwd) ?{)
  8151. term (substring term (match-end 0))
  8152. mm (if re-p
  8153. `(string-match ,(substring kwd 1 -1) todo)
  8154. (list 'equal 'todo kwd))
  8155. mm (if minus (list 'not mm) mm))
  8156. (push mm todomatcher))
  8157. (push (if (> (length todomatcher) 1)
  8158. (cons 'and todomatcher)
  8159. (car todomatcher))
  8160. orlist)
  8161. (setq todomatcher nil))
  8162. (setq todomatcher (if (> (length orlist) 1)
  8163. (cons 'or orlist) (car orlist))))
  8164. ;; Return the string and lisp forms of the matcher
  8165. (setq matcher (if todomatcher
  8166. (list 'and tagsmatcher todomatcher)
  8167. tagsmatcher))
  8168. (cons match0 matcher)))
  8169. (defun org-op-to-function (op &optional stringp)
  8170. "Turn an operator into the appropriate function."
  8171. (setq op
  8172. (cond
  8173. ((equal op "<" ) '(< string< org-time<))
  8174. ((equal op ">" ) '(> org-string> org-time>))
  8175. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8176. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8177. ((member op '("=" "==")) '(= string= org-time=))
  8178. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8179. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8180. (defun org<> (a b) (not (= a b)))
  8181. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8182. (defun org-string>= (a b) (not (string< a b)))
  8183. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8184. (defun org-string<> (a b) (not (string= a b)))
  8185. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8186. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8187. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8188. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8189. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8190. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8191. (defun org-2ft (s)
  8192. "Convert S to a floating point time.
  8193. If S is already a number, just return it. If it is a string, parse
  8194. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8195. (cond
  8196. ((numberp s) s)
  8197. ((stringp s)
  8198. (condition-case nil
  8199. (float-time (apply 'encode-time (org-parse-time-string s)))
  8200. (error 0.)))
  8201. (t 0.)))
  8202. (defun org-matcher-time (s)
  8203. (cond
  8204. ((equal s "<now>") (float-time))
  8205. ((equal s "<today>")
  8206. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8207. (t (org-2ft s))))
  8208. (defun org-match-any-p (re list)
  8209. "Does re match any element of list?"
  8210. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8211. (delq nil list))
  8212. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8213. (defvar org-tags-overlay (org-make-overlay 1 1))
  8214. (org-detach-overlay org-tags-overlay)
  8215. (defun org-get-local-tags-at (&optional pos)
  8216. "Get a list of tags defined in the current headline."
  8217. (org-get-tags-at pos 'local))
  8218. (defun org-get-local-tags ()
  8219. "Get a list of tags defined in the current headline."
  8220. (org-get-tags-at nil 'local))
  8221. (defun org-get-tags-at (&optional pos local)
  8222. "Get a list of all headline tags applicable at POS.
  8223. POS defaults to point. If tags are inherited, the list contains
  8224. the targets in the same sequence as the headlines appear, i.e.
  8225. the tags of the current headline come last.
  8226. When LOCAL is non-nil, only return tags from the current headline,
  8227. ignore inherited ones."
  8228. (interactive)
  8229. (let (tags ltags lastpos parent)
  8230. (save-excursion
  8231. (save-restriction
  8232. (widen)
  8233. (goto-char (or pos (point)))
  8234. (save-match-data
  8235. (catch 'done
  8236. (condition-case nil
  8237. (progn
  8238. (org-back-to-heading t)
  8239. (while (not (equal lastpos (point)))
  8240. (setq lastpos (point))
  8241. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8242. (setq ltags (org-split-string
  8243. (org-match-string-no-properties 1) ":"))
  8244. (setq tags (append
  8245. (if parent
  8246. (org-remove-uniherited-tags ltags)
  8247. ltags)
  8248. tags)))
  8249. (or org-use-tag-inheritance (throw 'done t))
  8250. (if local (throw 'done t))
  8251. (org-up-heading-all 1)
  8252. (setq parent t)))
  8253. (error nil)))))
  8254. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8255. (defun org-toggle-tag (tag &optional onoff)
  8256. "Toggle the tag TAG for the current line.
  8257. If ONOFF is `on' or `off', don't toggle but set to this state."
  8258. (unless (org-on-heading-p t) (error "Not on headling"))
  8259. (let (res current)
  8260. (save-excursion
  8261. (beginning-of-line)
  8262. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8263. (point-at-eol) t)
  8264. (progn
  8265. (setq current (match-string 1))
  8266. (replace-match ""))
  8267. (setq current ""))
  8268. (setq current (nreverse (org-split-string current ":")))
  8269. (cond
  8270. ((eq onoff 'on)
  8271. (setq res t)
  8272. (or (member tag current) (push tag current)))
  8273. ((eq onoff 'off)
  8274. (or (not (member tag current)) (setq current (delete tag current))))
  8275. (t (if (member tag current)
  8276. (setq current (delete tag current))
  8277. (setq res t)
  8278. (push tag current))))
  8279. (end-of-line 1)
  8280. (if current
  8281. (progn
  8282. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8283. (org-set-tags nil t))
  8284. (delete-horizontal-space))
  8285. (run-hooks 'org-after-tags-change-hook))
  8286. res))
  8287. (defun org-align-tags-here (to-col)
  8288. ;; Assumes that this is a headline
  8289. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8290. (beginning-of-line 1)
  8291. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8292. (< pos (match-beginning 2)))
  8293. (progn
  8294. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8295. (goto-char (match-beginning 1))
  8296. (insert " ")
  8297. (delete-region (point) (1+ (match-beginning 2)))
  8298. (setq ncol (max (1+ (current-column))
  8299. (1+ col)
  8300. (if (> to-col 0)
  8301. to-col
  8302. (- (abs to-col) tags-l))))
  8303. (setq p (point))
  8304. (insert (make-string (- ncol (current-column)) ?\ ))
  8305. (setq ncol (current-column))
  8306. (when indent-tabs-mode (tabify p (point-at-eol)))
  8307. (org-move-to-column (min ncol col) t))
  8308. (goto-char pos))))
  8309. (defun org-set-tags (&optional arg just-align)
  8310. "Set the tags for the current headline.
  8311. With prefix ARG, realign all tags in headings in the current buffer."
  8312. (interactive "P")
  8313. (let* ((re (concat "^" outline-regexp))
  8314. (current (org-get-tags-string))
  8315. (col (current-column))
  8316. (org-setting-tags t)
  8317. table current-tags inherited-tags ; computed below when needed
  8318. tags p0 c0 c1 rpl)
  8319. (if arg
  8320. (save-excursion
  8321. (goto-char (point-min))
  8322. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8323. (while (re-search-forward re nil t)
  8324. (org-set-tags nil t)
  8325. (end-of-line 1)))
  8326. (message "All tags realigned to column %d" org-tags-column))
  8327. (if just-align
  8328. (setq tags current)
  8329. ;; Get a new set of tags from the user
  8330. (save-excursion
  8331. (setq table (or org-tag-alist (org-get-buffer-tags))
  8332. org-last-tags-completion-table table
  8333. current-tags (org-split-string current ":")
  8334. inherited-tags (nreverse
  8335. (nthcdr (length current-tags)
  8336. (nreverse (org-get-tags-at))))
  8337. tags
  8338. (if (or (eq t org-use-fast-tag-selection)
  8339. (and org-use-fast-tag-selection
  8340. (delq nil (mapcar 'cdr table))))
  8341. (org-fast-tag-selection
  8342. current-tags inherited-tags table
  8343. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8344. (let ((org-add-colon-after-tag-completion t))
  8345. (org-trim
  8346. (org-without-partial-completion
  8347. (completing-read "Tags: " 'org-tags-completion-function
  8348. nil nil current 'org-tags-history)))))))
  8349. (while (string-match "[-+&]+" tags)
  8350. ;; No boolean logic, just a list
  8351. (setq tags (replace-match ":" t t tags))))
  8352. (if (string-match "\\`[\t ]*\\'" tags)
  8353. (setq tags "")
  8354. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8355. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8356. ;; Insert new tags at the correct column
  8357. (beginning-of-line 1)
  8358. (cond
  8359. ((and (equal current "") (equal tags "")))
  8360. ((re-search-forward
  8361. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8362. (point-at-eol) t)
  8363. (if (equal tags "")
  8364. (setq rpl "")
  8365. (goto-char (match-beginning 0))
  8366. (setq c0 (current-column) p0 (point)
  8367. c1 (max (1+ c0) (if (> org-tags-column 0)
  8368. org-tags-column
  8369. (- (- org-tags-column) (length tags))))
  8370. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8371. (replace-match rpl t t)
  8372. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8373. tags)
  8374. (t (error "Tags alignment failed")))
  8375. (org-move-to-column col)
  8376. (unless just-align
  8377. (run-hooks 'org-after-tags-change-hook)))))
  8378. (defun org-change-tag-in-region (beg end tag off)
  8379. "Add or remove TAG for each entry in the region.
  8380. This works in the agenda, and also in an org-mode buffer."
  8381. (interactive
  8382. (list (region-beginning) (region-end)
  8383. (let ((org-last-tags-completion-table
  8384. (if (org-mode-p)
  8385. (org-get-buffer-tags)
  8386. (org-global-tags-completion-table))))
  8387. (completing-read
  8388. "Tag: " 'org-tags-completion-function nil nil nil
  8389. 'org-tags-history))
  8390. (progn
  8391. (message "[s]et or [r]emove? ")
  8392. (equal (read-char-exclusive) ?r))))
  8393. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8394. (let ((agendap (equal major-mode 'org-agenda-mode))
  8395. l1 l2 m buf pos newhead (cnt 0))
  8396. (goto-char end)
  8397. (setq l2 (1- (org-current-line)))
  8398. (goto-char beg)
  8399. (setq l1 (org-current-line))
  8400. (loop for l from l1 to l2 do
  8401. (goto-line l)
  8402. (setq m (get-text-property (point) 'org-hd-marker))
  8403. (when (or (and (org-mode-p) (org-on-heading-p))
  8404. (and agendap m))
  8405. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8406. pos (if agendap m (point)))
  8407. (with-current-buffer buf
  8408. (save-excursion
  8409. (save-restriction
  8410. (goto-char pos)
  8411. (setq cnt (1+ cnt))
  8412. (org-toggle-tag tag (if off 'off 'on))
  8413. (setq newhead (org-get-heading)))))
  8414. (and agendap (org-agenda-change-all-lines newhead m))))
  8415. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8416. (defun org-tags-completion-function (string predicate &optional flag)
  8417. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8418. (confirm (lambda (x) (stringp (car x)))))
  8419. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8420. (setq s1 (match-string 1 string)
  8421. s2 (match-string 2 string))
  8422. (setq s1 "" s2 string))
  8423. (cond
  8424. ((eq flag nil)
  8425. ;; try completion
  8426. (setq rtn (try-completion s2 ctable confirm))
  8427. (if (stringp rtn)
  8428. (setq rtn
  8429. (concat s1 s2 (substring rtn (length s2))
  8430. (if (and org-add-colon-after-tag-completion
  8431. (assoc rtn ctable))
  8432. ":" ""))))
  8433. rtn)
  8434. ((eq flag t)
  8435. ;; all-completions
  8436. (all-completions s2 ctable confirm)
  8437. )
  8438. ((eq flag 'lambda)
  8439. ;; exact match?
  8440. (assoc s2 ctable)))
  8441. ))
  8442. (defun org-fast-tag-insert (kwd tags face &optional end)
  8443. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8444. (insert (format "%-12s" (concat kwd ":"))
  8445. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8446. (or end "")))
  8447. (defun org-fast-tag-show-exit (flag)
  8448. (save-excursion
  8449. (goto-line 3)
  8450. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8451. (replace-match ""))
  8452. (when flag
  8453. (end-of-line 1)
  8454. (org-move-to-column (- (window-width) 19) t)
  8455. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8456. (defun org-set-current-tags-overlay (current prefix)
  8457. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8458. (if (featurep 'xemacs)
  8459. (org-overlay-display org-tags-overlay (concat prefix s)
  8460. 'secondary-selection)
  8461. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8462. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8463. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8464. "Fast tag selection with single keys.
  8465. CURRENT is the current list of tags in the headline, INHERITED is the
  8466. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8467. possibly with grouping information. TODO-TABLE is a similar table with
  8468. TODO keywords, should these have keys assigned to them.
  8469. If the keys are nil, a-z are automatically assigned.
  8470. Returns the new tags string, or nil to not change the current settings."
  8471. (let* ((fulltable (append table todo-table))
  8472. (maxlen (apply 'max (mapcar
  8473. (lambda (x)
  8474. (if (stringp (car x)) (string-width (car x)) 0))
  8475. fulltable)))
  8476. (buf (current-buffer))
  8477. (expert (eq org-fast-tag-selection-single-key 'expert))
  8478. (buffer-tags nil)
  8479. (fwidth (+ maxlen 3 1 3))
  8480. (ncol (/ (- (window-width) 4) fwidth))
  8481. (i-face 'org-done)
  8482. (c-face 'org-todo)
  8483. tg cnt e c char c1 c2 ntable tbl rtn
  8484. ov-start ov-end ov-prefix
  8485. (exit-after-next org-fast-tag-selection-single-key)
  8486. (done-keywords org-done-keywords)
  8487. groups ingroup)
  8488. (save-excursion
  8489. (beginning-of-line 1)
  8490. (if (looking-at
  8491. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8492. (setq ov-start (match-beginning 1)
  8493. ov-end (match-end 1)
  8494. ov-prefix "")
  8495. (setq ov-start (1- (point-at-eol))
  8496. ov-end (1+ ov-start))
  8497. (skip-chars-forward "^\n\r")
  8498. (setq ov-prefix
  8499. (concat
  8500. (buffer-substring (1- (point)) (point))
  8501. (if (> (current-column) org-tags-column)
  8502. " "
  8503. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8504. (org-move-overlay org-tags-overlay ov-start ov-end)
  8505. (save-window-excursion
  8506. (if expert
  8507. (set-buffer (get-buffer-create " *Org tags*"))
  8508. (delete-other-windows)
  8509. (split-window-vertically)
  8510. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8511. (erase-buffer)
  8512. (org-set-local 'org-done-keywords done-keywords)
  8513. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8514. (org-fast-tag-insert "Current" current c-face "\n\n")
  8515. (org-fast-tag-show-exit exit-after-next)
  8516. (org-set-current-tags-overlay current ov-prefix)
  8517. (setq tbl fulltable char ?a cnt 0)
  8518. (while (setq e (pop tbl))
  8519. (cond
  8520. ((equal e '(:startgroup))
  8521. (push '() groups) (setq ingroup t)
  8522. (when (not (= cnt 0))
  8523. (setq cnt 0)
  8524. (insert "\n"))
  8525. (insert "{ "))
  8526. ((equal e '(:endgroup))
  8527. (setq ingroup nil cnt 0)
  8528. (insert "}\n"))
  8529. (t
  8530. (setq tg (car e) c2 nil)
  8531. (if (cdr e)
  8532. (setq c (cdr e))
  8533. ;; automatically assign a character.
  8534. (setq c1 (string-to-char
  8535. (downcase (substring
  8536. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8537. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8538. (while (or (rassoc char ntable) (rassoc char table))
  8539. (setq char (1+ char)))
  8540. (setq c2 c1))
  8541. (setq c (or c2 char)))
  8542. (if ingroup (push tg (car groups)))
  8543. (setq tg (org-add-props tg nil 'face
  8544. (cond
  8545. ((not (assoc tg table))
  8546. (org-get-todo-face tg))
  8547. ((member tg current) c-face)
  8548. ((member tg inherited) i-face)
  8549. (t nil))))
  8550. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8551. (insert "[" c "] " tg (make-string
  8552. (- fwidth 4 (length tg)) ?\ ))
  8553. (push (cons tg c) ntable)
  8554. (when (= (setq cnt (1+ cnt)) ncol)
  8555. (insert "\n")
  8556. (if ingroup (insert " "))
  8557. (setq cnt 0)))))
  8558. (setq ntable (nreverse ntable))
  8559. (insert "\n")
  8560. (goto-char (point-min))
  8561. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8562. (fit-window-to-buffer))
  8563. (setq rtn
  8564. (catch 'exit
  8565. (while t
  8566. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8567. (if groups " [!] no groups" " [!]groups")
  8568. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8569. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8570. (cond
  8571. ((= c ?\r) (throw 'exit t))
  8572. ((= c ?!)
  8573. (setq groups (not groups))
  8574. (goto-char (point-min))
  8575. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8576. ((= c ?\C-c)
  8577. (if (not expert)
  8578. (org-fast-tag-show-exit
  8579. (setq exit-after-next (not exit-after-next)))
  8580. (setq expert nil)
  8581. (delete-other-windows)
  8582. (split-window-vertically)
  8583. (org-switch-to-buffer-other-window " *Org tags*")
  8584. (and (fboundp 'fit-window-to-buffer)
  8585. (fit-window-to-buffer))))
  8586. ((or (= c ?\C-g)
  8587. (and (= c ?q) (not (rassoc c ntable))))
  8588. (org-detach-overlay org-tags-overlay)
  8589. (setq quit-flag t))
  8590. ((= c ?\ )
  8591. (setq current nil)
  8592. (if exit-after-next (setq exit-after-next 'now)))
  8593. ((= c ?\t)
  8594. (condition-case nil
  8595. (setq tg (completing-read
  8596. "Tag: "
  8597. (or buffer-tags
  8598. (with-current-buffer buf
  8599. (org-get-buffer-tags)))))
  8600. (quit (setq tg "")))
  8601. (when (string-match "\\S-" tg)
  8602. (add-to-list 'buffer-tags (list tg))
  8603. (if (member tg current)
  8604. (setq current (delete tg current))
  8605. (push tg current)))
  8606. (if exit-after-next (setq exit-after-next 'now)))
  8607. ((setq e (rassoc c todo-table) tg (car e))
  8608. (with-current-buffer buf
  8609. (save-excursion (org-todo tg)))
  8610. (if exit-after-next (setq exit-after-next 'now)))
  8611. ((setq e (rassoc c ntable) tg (car e))
  8612. (if (member tg current)
  8613. (setq current (delete tg current))
  8614. (loop for g in groups do
  8615. (if (member tg g)
  8616. (mapc (lambda (x)
  8617. (setq current (delete x current)))
  8618. g)))
  8619. (push tg current))
  8620. (if exit-after-next (setq exit-after-next 'now))))
  8621. ;; Create a sorted list
  8622. (setq current
  8623. (sort current
  8624. (lambda (a b)
  8625. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8626. (if (eq exit-after-next 'now) (throw 'exit t))
  8627. (goto-char (point-min))
  8628. (beginning-of-line 2)
  8629. (delete-region (point) (point-at-eol))
  8630. (org-fast-tag-insert "Current" current c-face)
  8631. (org-set-current-tags-overlay current ov-prefix)
  8632. (while (re-search-forward
  8633. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8634. (setq tg (match-string 1))
  8635. (add-text-properties
  8636. (match-beginning 1) (match-end 1)
  8637. (list 'face
  8638. (cond
  8639. ((member tg current) c-face)
  8640. ((member tg inherited) i-face)
  8641. (t (get-text-property (match-beginning 1) 'face))))))
  8642. (goto-char (point-min)))))
  8643. (org-detach-overlay org-tags-overlay)
  8644. (if rtn
  8645. (mapconcat 'identity current ":")
  8646. nil))))
  8647. (defun org-get-tags-string ()
  8648. "Get the TAGS string in the current headline."
  8649. (unless (org-on-heading-p t)
  8650. (error "Not on a heading"))
  8651. (save-excursion
  8652. (beginning-of-line 1)
  8653. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8654. (org-match-string-no-properties 1)
  8655. "")))
  8656. (defun org-get-tags ()
  8657. "Get the list of tags specified in the current headline."
  8658. (org-split-string (org-get-tags-string) ":"))
  8659. (defun org-get-buffer-tags ()
  8660. "Get a table of all tags used in the buffer, for completion."
  8661. (let (tags)
  8662. (save-excursion
  8663. (goto-char (point-min))
  8664. (while (re-search-forward
  8665. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8666. (when (equal (char-after (point-at-bol 0)) ?*)
  8667. (mapc (lambda (x) (add-to-list 'tags x))
  8668. (org-split-string (org-match-string-no-properties 1) ":")))))
  8669. (mapcar 'list tags)))
  8670. ;;;; The mapping API
  8671. ;;;###autoload
  8672. (defun org-map-entries (func &optional match scope &rest skip)
  8673. "Call FUNC at each headline selected by MATCH in SCOPE.
  8674. FUNC is a function or a lisp form. The function will be called without
  8675. arguments, with the cursor positioned at the beginning of the headline.
  8676. The return values of all calls to the function will be collected and
  8677. returned as a list.
  8678. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8679. Only headlines that are matched by this query will be considered during
  8680. the iteration. When MATCH is nil or t, all headlines will be
  8681. visited by the iteration.
  8682. SCOPE determines the scope of this command. It can be any of:
  8683. nil The current buffer, respecting the restriction if any
  8684. tree The subtree started with the entry at point
  8685. file The current buffer, without restriction
  8686. file-with-archives
  8687. The current buffer, and any archives associated with it
  8688. agenda All agenda files
  8689. agenda-with-archives
  8690. All agenda files with any archive files associated with them
  8691. \(file1 file2 ...)
  8692. If this is a list, all files in the list will be scanned
  8693. The remaining args are treated as settings for the skipping facilities of
  8694. the scanner. The following items can be given here:
  8695. archive skip trees with the archive tag.
  8696. comment skip trees with the COMMENT keyword
  8697. function or Emacs Lisp form:
  8698. will be used as value for `org-agenda-skip-function', so whenever
  8699. the the function returns t, FUNC will not be called for that
  8700. entry and search will continue from the point where the
  8701. function leaves it."
  8702. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8703. (org-agenda-skip-archived-trees (memq 'archive skip))
  8704. (org-agenda-skip-comment-trees (memq 'comment skip))
  8705. (org-agenda-skip-function
  8706. (car (org-delete-all '(comment archive) skip)))
  8707. (org-tags-match-list-sublevels t)
  8708. matcher pos file
  8709. org-todo-keywords-for-agenda
  8710. org-done-keywords-for-agenda
  8711. org-todo-keyword-alist-for-agenda
  8712. org-tag-alist-for-agenda)
  8713. (cond
  8714. ((eq match t) (setq matcher t))
  8715. ((eq match nil) (setq matcher t))
  8716. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  8717. (when (eq scope 'tree)
  8718. (org-back-to-heading t)
  8719. (org-narrow-to-subtree)
  8720. (setq scope nil))
  8721. (if (not scope)
  8722. (progn
  8723. (org-prepare-agenda-buffers
  8724. (list (buffer-file-name (current-buffer))))
  8725. (org-scan-tags func matcher))
  8726. ;; Get the right scope
  8727. (setq pos (point))
  8728. (cond
  8729. ((and scope (listp scope) (symbolp (car scope)))
  8730. (setq scope (eval scope)))
  8731. ((eq scope 'agenda)
  8732. (setq scope (org-agenda-files t)))
  8733. ((eq scope 'agenda-with-archives)
  8734. (setq scope (org-agenda-files t))
  8735. (setq scope (org-add-archive-files scope)))
  8736. ((eq scope 'file)
  8737. (setq scope (list (buffer-file-name))))
  8738. ((eq scope 'file-with-archives)
  8739. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  8740. (org-prepare-agenda-buffers scope)
  8741. (while (setq file (pop scope))
  8742. (with-current-buffer (org-find-base-buffer-visiting file)
  8743. (save-excursion
  8744. (save-restriction
  8745. (widen)
  8746. (goto-char (point-min))
  8747. (org-scan-tags func matcher))))))))
  8748. ;;;; Properties
  8749. ;;; Setting and retrieving properties
  8750. (defconst org-special-properties
  8751. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8752. "TIMESTAMP" "TIMESTAMP_IA")
  8753. "The special properties valid in Org-mode.
  8754. These are properties that are not defined in the property drawer,
  8755. but in some other way.")
  8756. (defconst org-default-properties
  8757. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8758. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8759. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8760. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8761. "Some properties that are used by Org-mode for various purposes.
  8762. Being in this list makes sure that they are offered for completion.")
  8763. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8764. "Regular expression matching the first line of a property drawer.")
  8765. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8766. "Regular expression matching the first line of a property drawer.")
  8767. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  8768. "Regular expression matching the first line of a property drawer.")
  8769. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  8770. "Regular expression matching the first line of a property drawer.")
  8771. (defconst org-property-drawer-re
  8772. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  8773. org-property-end-re "\\)\n?")
  8774. "Matches an entire property drawer.")
  8775. (defconst org-clock-drawer-re
  8776. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  8777. org-property-end-re "\\)\n?")
  8778. "Matches an entire clock drawer.")
  8779. (defun org-property-action ()
  8780. "Do an action on properties."
  8781. (interactive)
  8782. (let (c)
  8783. (org-at-property-p)
  8784. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8785. (setq c (read-char-exclusive))
  8786. (cond
  8787. ((equal c ?s)
  8788. (call-interactively 'org-set-property))
  8789. ((equal c ?d)
  8790. (call-interactively 'org-delete-property))
  8791. ((equal c ?D)
  8792. (call-interactively 'org-delete-property-globally))
  8793. ((equal c ?c)
  8794. (call-interactively 'org-compute-property-at-point))
  8795. (t (error "No such property action %c" c)))))
  8796. (defun org-at-property-p ()
  8797. "Is the cursor in a property line?"
  8798. ;; FIXME: Does not check if we are actually in the drawer.
  8799. ;; FIXME: also returns true on any drawers.....
  8800. ;; This is used by C-c C-c for property action.
  8801. (save-excursion
  8802. (beginning-of-line 1)
  8803. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8804. (defun org-get-property-block (&optional beg end force)
  8805. "Return the (beg . end) range of the body of the property drawer.
  8806. BEG and END can be beginning and end of subtree, if not given
  8807. they will be found.
  8808. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8809. (catch 'exit
  8810. (save-excursion
  8811. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8812. (end (or end (progn (outline-next-heading) (point)))))
  8813. (goto-char beg)
  8814. (if (re-search-forward org-property-start-re end t)
  8815. (setq beg (1+ (match-end 0)))
  8816. (if force
  8817. (save-excursion
  8818. (org-insert-property-drawer)
  8819. (setq end (progn (outline-next-heading) (point))))
  8820. (throw 'exit nil))
  8821. (goto-char beg)
  8822. (if (re-search-forward org-property-start-re end t)
  8823. (setq beg (1+ (match-end 0)))))
  8824. (if (re-search-forward org-property-end-re end t)
  8825. (setq end (match-beginning 0))
  8826. (or force (throw 'exit nil))
  8827. (goto-char beg)
  8828. (setq end beg)
  8829. (org-indent-line-function)
  8830. (insert ":END:\n"))
  8831. (cons beg end)))))
  8832. (defun org-entry-properties (&optional pom which)
  8833. "Get all properties of the entry at point-or-marker POM.
  8834. This includes the TODO keyword, the tags, time strings for deadline,
  8835. scheduled, and clocking, and any additional properties defined in the
  8836. entry. The return value is an alist, keys may occur multiple times
  8837. if the property key was used several times.
  8838. POM may also be nil, in which case the current entry is used.
  8839. If WHICH is nil or `all', get all properties. If WHICH is
  8840. `special' or `standard', only get that subclass."
  8841. (setq which (or which 'all))
  8842. (org-with-point-at pom
  8843. (let ((clockstr (substring org-clock-string 0 -1))
  8844. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8845. beg end range props sum-props key value string clocksum)
  8846. (save-excursion
  8847. (when (condition-case nil (org-back-to-heading t) (error nil))
  8848. (setq beg (point))
  8849. (setq sum-props (get-text-property (point) 'org-summaries))
  8850. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8851. (outline-next-heading)
  8852. (setq end (point))
  8853. (when (memq which '(all special))
  8854. ;; Get the special properties, like TODO and tags
  8855. (goto-char beg)
  8856. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8857. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8858. (when (looking-at org-priority-regexp)
  8859. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8860. (when (and (setq value (org-get-tags-string))
  8861. (string-match "\\S-" value))
  8862. (push (cons "TAGS" value) props))
  8863. (when (setq value (org-get-tags-at))
  8864. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8865. props))
  8866. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8867. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8868. string (if (equal key clockstr)
  8869. (org-no-properties
  8870. (org-trim
  8871. (buffer-substring
  8872. (match-beginning 3) (goto-char (point-at-eol)))))
  8873. (substring (org-match-string-no-properties 3) 1 -1)))
  8874. (unless key
  8875. (if (= (char-after (match-beginning 3)) ?\[)
  8876. (setq key "TIMESTAMP_IA")
  8877. (setq key "TIMESTAMP")))
  8878. (when (or (equal key clockstr) (not (assoc key props)))
  8879. (push (cons key string) props)))
  8880. )
  8881. (when (memq which '(all standard))
  8882. ;; Get the standard properties, like :PORP: ...
  8883. (setq range (org-get-property-block beg end))
  8884. (when range
  8885. (goto-char (car range))
  8886. (while (re-search-forward
  8887. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8888. (cdr range) t)
  8889. (setq key (org-match-string-no-properties 1)
  8890. value (org-trim (or (org-match-string-no-properties 2) "")))
  8891. (unless (member key excluded)
  8892. (push (cons key (or value "")) props)))))
  8893. (if clocksum
  8894. (push (cons "CLOCKSUM"
  8895. (org-columns-number-to-string (/ (float clocksum) 60.)
  8896. 'add_times))
  8897. props))
  8898. (append sum-props (nreverse props)))))))
  8899. (defun org-entry-get (pom property &optional inherit)
  8900. "Get value of PROPERTY for entry at point-or-marker POM.
  8901. If INHERIT is non-nil and the entry does not have the property,
  8902. then also check higher levels of the hierarchy.
  8903. If INHERIT is the symbol `selective', use inheritance only if the setting
  8904. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8905. If the property is present but empty, the return value is the empty string.
  8906. If the property is not present at all, nil is returned."
  8907. (org-with-point-at pom
  8908. (if (and inherit (if (eq inherit 'selective)
  8909. (org-property-inherit-p property)
  8910. t))
  8911. (org-entry-get-with-inheritance property)
  8912. (if (member property org-special-properties)
  8913. ;; We need a special property. Use brute force, get all properties.
  8914. (cdr (assoc property (org-entry-properties nil 'special)))
  8915. (let ((range (org-get-property-block)))
  8916. (if (and range
  8917. (goto-char (car range))
  8918. (re-search-forward
  8919. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8920. (cdr range) t))
  8921. ;; Found the property, return it.
  8922. (if (match-end 1)
  8923. (org-match-string-no-properties 1)
  8924. "")))))))
  8925. (defun org-property-or-variable-value (var &optional inherit)
  8926. "Check if there is a property fixing the value of VAR.
  8927. If yes, return this value. If not, return the current value of the variable."
  8928. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8929. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8930. (read prop)
  8931. (symbol-value var))))
  8932. (defun org-entry-delete (pom property)
  8933. "Delete the property PROPERTY from entry at point-or-marker POM."
  8934. (org-with-point-at pom
  8935. (if (member property org-special-properties)
  8936. nil ; cannot delete these properties.
  8937. (let ((range (org-get-property-block)))
  8938. (if (and range
  8939. (goto-char (car range))
  8940. (re-search-forward
  8941. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8942. (cdr range) t))
  8943. (progn
  8944. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8945. t)
  8946. nil)))))
  8947. ;; Multi-values properties are properties that contain multiple values
  8948. ;; These values are assumed to be single words, separated by whitespace.
  8949. (defun org-entry-add-to-multivalued-property (pom property value)
  8950. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8951. (let* ((old (org-entry-get pom property))
  8952. (values (and old (org-split-string old "[ \t]"))))
  8953. (setq value (org-entry-protect-space value))
  8954. (unless (member value values)
  8955. (setq values (cons value values))
  8956. (org-entry-put pom property
  8957. (mapconcat 'identity values " ")))))
  8958. (defun org-entry-remove-from-multivalued-property (pom property value)
  8959. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8960. (let* ((old (org-entry-get pom property))
  8961. (values (and old (org-split-string old "[ \t]"))))
  8962. (setq value (org-entry-protect-space value))
  8963. (when (member value values)
  8964. (setq values (delete value values))
  8965. (org-entry-put pom property
  8966. (mapconcat 'identity values " ")))))
  8967. (defun org-entry-member-in-multivalued-property (pom property value)
  8968. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8969. (let* ((old (org-entry-get pom property))
  8970. (values (and old (org-split-string old "[ \t]"))))
  8971. (setq value (org-entry-protect-space value))
  8972. (member value values)))
  8973. (defun org-entry-get-multivalued-property (pom property)
  8974. "Return a list of values in a multivalued property."
  8975. (let* ((value (org-entry-get pom property))
  8976. (values (and value (org-split-string value "[ \t]"))))
  8977. (mapcar 'org-entry-restore-space values)))
  8978. (defun org-entry-put-multivalued-property (pom property &rest values)
  8979. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  8980. VALUES should be a list of strings. Spaces will be protected."
  8981. (org-entry-put pom property
  8982. (mapconcat 'org-entry-protect-space values " "))
  8983. (let* ((value (org-entry-get pom property))
  8984. (values (and value (org-split-string value "[ \t]"))))
  8985. (mapcar 'org-entry-restore-space values)))
  8986. (defun org-entry-protect-space (s)
  8987. "Protect spaces and newline in string S."
  8988. (while (string-match " " s)
  8989. (setq s (replace-match "%20" t t s)))
  8990. (while (string-match "\n" s)
  8991. (setq s (replace-match "%0A" t t s)))
  8992. s)
  8993. (defun org-entry-restore-space (s)
  8994. "Restore spaces and newline in string S."
  8995. (while (string-match "%20" s)
  8996. (setq s (replace-match " " t t s)))
  8997. (while (string-match "%0A" s)
  8998. (setq s (replace-match "\n" t t s)))
  8999. s)
  9000. (defvar org-entry-property-inherited-from (make-marker)
  9001. "Marker pointing to the entry from where a proerty was inherited.
  9002. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9003. location of the entry where the inheriance search matched. If there was
  9004. no match, the marker will point nowhere.
  9005. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9006. is set.")
  9007. (defun org-entry-get-with-inheritance (property)
  9008. "Get entry property, and search higher levels if not present."
  9009. (move-marker org-entry-property-inherited-from nil)
  9010. (let (tmp)
  9011. (save-excursion
  9012. (save-restriction
  9013. (widen)
  9014. (catch 'ex
  9015. (while t
  9016. (when (setq tmp (org-entry-get nil property))
  9017. (org-back-to-heading t)
  9018. (move-marker org-entry-property-inherited-from (point))
  9019. (throw 'ex tmp))
  9020. (or (org-up-heading-safe) (throw 'ex nil)))))
  9021. (or tmp
  9022. (cdr (assoc property org-file-properties))
  9023. (cdr (assoc property org-global-properties))
  9024. (cdr (assoc property org-global-properties-fixed))))))
  9025. (defun org-entry-put (pom property value)
  9026. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9027. (org-with-point-at pom
  9028. (org-back-to-heading t)
  9029. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9030. range)
  9031. (cond
  9032. ((equal property "TODO")
  9033. (when (and (stringp value) (string-match "\\S-" value)
  9034. (not (member value org-todo-keywords-1)))
  9035. (error "\"%s\" is not a valid TODO state" value))
  9036. (if (or (not value)
  9037. (not (string-match "\\S-" value)))
  9038. (setq value 'none))
  9039. (org-todo value)
  9040. (org-set-tags nil 'align))
  9041. ((equal property "PRIORITY")
  9042. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9043. (string-to-char value) ?\ ))
  9044. (org-set-tags nil 'align))
  9045. ((equal property "SCHEDULED")
  9046. (if (re-search-forward org-scheduled-time-regexp end t)
  9047. (cond
  9048. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9049. ((eq value 'later) (org-timestamp-change 1 'day))
  9050. (t (call-interactively 'org-schedule)))
  9051. (call-interactively 'org-schedule)))
  9052. ((equal property "DEADLINE")
  9053. (if (re-search-forward org-deadline-time-regexp end t)
  9054. (cond
  9055. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9056. ((eq value 'later) (org-timestamp-change 1 'day))
  9057. (t (call-interactively 'org-deadline)))
  9058. (call-interactively 'org-deadline)))
  9059. ((member property org-special-properties)
  9060. (error "The %s property can not yet be set with `org-entry-put'"
  9061. property))
  9062. (t ; a non-special property
  9063. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9064. (setq range (org-get-property-block beg end 'force))
  9065. (goto-char (car range))
  9066. (if (re-search-forward
  9067. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9068. (progn
  9069. (delete-region (match-beginning 1) (match-end 1))
  9070. (goto-char (match-beginning 1)))
  9071. (goto-char (cdr range))
  9072. (insert "\n")
  9073. (backward-char 1)
  9074. (org-indent-line-function)
  9075. (insert ":" property ":"))
  9076. (and value (insert " " value))
  9077. (org-indent-line-function)))))))
  9078. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9079. "Get all property keys in the current buffer.
  9080. With INCLUDE-SPECIALS, also list the special properties that relect things
  9081. like tags and TODO state.
  9082. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9083. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9084. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9085. formats in the current buffer."
  9086. (let (rtn range cfmt cols s p)
  9087. (save-excursion
  9088. (save-restriction
  9089. (widen)
  9090. (goto-char (point-min))
  9091. (while (re-search-forward org-property-start-re nil t)
  9092. (setq range (org-get-property-block))
  9093. (goto-char (car range))
  9094. (while (re-search-forward
  9095. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9096. (cdr range) t)
  9097. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9098. (outline-next-heading))))
  9099. (when include-specials
  9100. (setq rtn (append org-special-properties rtn)))
  9101. (when include-defaults
  9102. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9103. (when include-columns
  9104. (save-excursion
  9105. (save-restriction
  9106. (widen)
  9107. (goto-char (point-min))
  9108. (while (re-search-forward
  9109. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9110. nil t)
  9111. (setq cfmt (match-string 2) s 0)
  9112. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9113. cfmt s)
  9114. (setq s (match-end 0)
  9115. p (match-string 1 cfmt))
  9116. (unless (or (equal p "ITEM")
  9117. (member p org-special-properties))
  9118. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9119. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9120. (defun org-property-values (key)
  9121. "Return a list of all values of property KEY."
  9122. (save-excursion
  9123. (save-restriction
  9124. (widen)
  9125. (goto-char (point-min))
  9126. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9127. values)
  9128. (while (re-search-forward re nil t)
  9129. (add-to-list 'values (org-trim (match-string 1))))
  9130. (delete "" values)))))
  9131. (defun org-insert-property-drawer ()
  9132. "Insert a property drawer into the current entry."
  9133. (interactive)
  9134. (org-back-to-heading t)
  9135. (looking-at outline-regexp)
  9136. (let ((indent (- (match-end 0)(match-beginning 0)))
  9137. (beg (point))
  9138. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9139. end hiddenp)
  9140. (outline-next-heading)
  9141. (setq end (point))
  9142. (goto-char beg)
  9143. (while (re-search-forward re end t))
  9144. (setq hiddenp (org-invisible-p))
  9145. (end-of-line 1)
  9146. (and (equal (char-after) ?\n) (forward-char 1))
  9147. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9148. (beginning-of-line 2))
  9149. (org-skip-over-state-notes)
  9150. (skip-chars-backward " \t\n\r")
  9151. (if (eq (char-before) ?*) (forward-char 1))
  9152. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9153. (beginning-of-line 0)
  9154. (org-indent-to-column indent)
  9155. (beginning-of-line 2)
  9156. (org-indent-to-column indent)
  9157. (beginning-of-line 0)
  9158. (if hiddenp
  9159. (save-excursion
  9160. (org-back-to-heading t)
  9161. (hide-entry))
  9162. (org-flag-drawer t))))
  9163. (defun org-set-property (property value)
  9164. "In the current entry, set PROPERTY to VALUE.
  9165. When called interactively, this will prompt for a property name, offering
  9166. completion on existing and default properties. And then it will prompt
  9167. for a value, offering competion either on allowed values (via an inherited
  9168. xxx_ALL property) or on existing values in other instances of this property
  9169. in the current file."
  9170. (interactive
  9171. (let* ((completion-ignore-case t)
  9172. (keys (org-buffer-property-keys nil t t))
  9173. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9174. (prop (if (member prop0 keys)
  9175. prop0
  9176. (or (cdr (assoc (downcase prop0)
  9177. (mapcar (lambda (x) (cons (downcase x) x))
  9178. keys)))
  9179. prop0)))
  9180. (cur (org-entry-get nil prop))
  9181. (allowed (org-property-get-allowed-values nil prop 'table))
  9182. (existing (mapcar 'list (org-property-values prop)))
  9183. (val (if allowed
  9184. (org-completing-read "Value: " allowed nil 'req-match)
  9185. (org-completing-read
  9186. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9187. (concat "[" cur "]") "")
  9188. ": ")
  9189. existing nil nil "" nil cur))))
  9190. (list prop (if (equal val "") cur val))))
  9191. (unless (equal (org-entry-get nil property) value)
  9192. (org-entry-put nil property value)))
  9193. (defun org-delete-property (property)
  9194. "In the current entry, delete PROPERTY."
  9195. (interactive
  9196. (let* ((completion-ignore-case t)
  9197. (prop (completing-read
  9198. "Property: " (org-entry-properties nil 'standard))))
  9199. (list prop)))
  9200. (message "Property %s %s" property
  9201. (if (org-entry-delete nil property)
  9202. "deleted"
  9203. "was not present in the entry")))
  9204. (defun org-delete-property-globally (property)
  9205. "Remove PROPERTY globally, from all entries."
  9206. (interactive
  9207. (let* ((completion-ignore-case t)
  9208. (prop (completing-read
  9209. "Globally remove property: "
  9210. (mapcar 'list (org-buffer-property-keys)))))
  9211. (list prop)))
  9212. (save-excursion
  9213. (save-restriction
  9214. (widen)
  9215. (goto-char (point-min))
  9216. (let ((cnt 0))
  9217. (while (re-search-forward
  9218. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9219. nil t)
  9220. (setq cnt (1+ cnt))
  9221. (replace-match ""))
  9222. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9223. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9224. (defun org-compute-property-at-point ()
  9225. "Compute the property at point.
  9226. This looks for an enclosing column format, extracts the operator and
  9227. then applies it to the proerty in the column format's scope."
  9228. (interactive)
  9229. (unless (org-at-property-p)
  9230. (error "Not at a property"))
  9231. (let ((prop (org-match-string-no-properties 2)))
  9232. (org-columns-get-format-and-top-level)
  9233. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9234. (error "No operator defined for property %s" prop))
  9235. (org-columns-compute prop)))
  9236. (defun org-property-get-allowed-values (pom property &optional table)
  9237. "Get allowed values for the property PROPERTY.
  9238. When TABLE is non-nil, return an alist that can directly be used for
  9239. completion."
  9240. (let (vals)
  9241. (cond
  9242. ((equal property "TODO")
  9243. (setq vals (org-with-point-at pom
  9244. (append org-todo-keywords-1 '("")))))
  9245. ((equal property "PRIORITY")
  9246. (let ((n org-lowest-priority))
  9247. (while (>= n org-highest-priority)
  9248. (push (char-to-string n) vals)
  9249. (setq n (1- n)))))
  9250. ((member property org-special-properties))
  9251. (t
  9252. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9253. (when (and vals (string-match "\\S-" vals))
  9254. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9255. (setq vals (mapcar (lambda (x)
  9256. (cond ((stringp x) x)
  9257. ((numberp x) (number-to-string x))
  9258. ((symbolp x) (symbol-name x))
  9259. (t "???")))
  9260. vals)))))
  9261. (if table (mapcar 'list vals) vals)))
  9262. (defun org-property-previous-allowed-value (&optional previous)
  9263. "Switch to the next allowed value for this property."
  9264. (interactive)
  9265. (org-property-next-allowed-value t))
  9266. (defun org-property-next-allowed-value (&optional previous)
  9267. "Switch to the next allowed value for this property."
  9268. (interactive)
  9269. (unless (org-at-property-p)
  9270. (error "Not at a property"))
  9271. (let* ((key (match-string 2))
  9272. (value (match-string 3))
  9273. (allowed (or (org-property-get-allowed-values (point) key)
  9274. (and (member value '("[ ]" "[-]" "[X]"))
  9275. '("[ ]" "[X]"))))
  9276. nval)
  9277. (unless allowed
  9278. (error "Allowed values for this property have not been defined"))
  9279. (if previous (setq allowed (reverse allowed)))
  9280. (if (member value allowed)
  9281. (setq nval (car (cdr (member value allowed)))))
  9282. (setq nval (or nval (car allowed)))
  9283. (if (equal nval value)
  9284. (error "Only one allowed value for this property"))
  9285. (org-at-property-p)
  9286. (replace-match (concat " :" key ": " nval) t t)
  9287. (org-indent-line-function)
  9288. (beginning-of-line 1)
  9289. (skip-chars-forward " \t")))
  9290. (defun org-find-entry-with-id (ident)
  9291. "Locate the entry that contains the ID property with exact value IDENT.
  9292. IDENT can be a string, a symbol or a number, this function will search for
  9293. the string representation of it.
  9294. Return the position where this entry starts, or nil if there is no such entry."
  9295. (let ((id (cond
  9296. ((stringp ident) ident)
  9297. ((symbol-name ident) (symbol-name ident))
  9298. ((numberp ident) (number-to-string ident))
  9299. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9300. (case-fold-search nil))
  9301. (save-excursion
  9302. (save-restriction
  9303. (widen)
  9304. (goto-char (point-min))
  9305. (when (re-search-forward
  9306. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9307. nil t)
  9308. (org-back-to-heading)
  9309. (point))))))
  9310. ;;;; Timestamps
  9311. (defvar org-last-changed-timestamp nil)
  9312. (defvar org-last-inserted-timestamp nil
  9313. "The last time stamp inserted with `org-insert-time-stamp'.")
  9314. (defvar org-time-was-given) ; dynamically scoped parameter
  9315. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9316. (defvar org-ts-what) ; dynamically scoped parameter
  9317. (defun org-time-stamp (arg &optional inactive)
  9318. "Prompt for a date/time and insert a time stamp.
  9319. If the user specifies a time like HH:MM, or if this command is called
  9320. with a prefix argument, the time stamp will contain date and time.
  9321. Otherwise, only the date will be included. All parts of a date not
  9322. specified by the user will be filled in from the current date/time.
  9323. So if you press just return without typing anything, the time stamp
  9324. will represent the current date/time. If there is already a timestamp
  9325. at the cursor, it will be modified."
  9326. (interactive "P")
  9327. (let* ((ts nil)
  9328. (default-time
  9329. ;; Default time is either today, or, when entering a range,
  9330. ;; the range start.
  9331. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9332. (save-excursion
  9333. (re-search-backward
  9334. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9335. (- (point) 20) t)))
  9336. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9337. (current-time)))
  9338. (default-input (and ts (org-get-compact-tod ts)))
  9339. org-time-was-given org-end-time-was-given time)
  9340. (cond
  9341. ((and (org-at-timestamp-p t)
  9342. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9343. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9344. (insert "--")
  9345. (setq time (let ((this-command this-command))
  9346. (org-read-date arg 'totime nil nil
  9347. default-time default-input)))
  9348. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9349. ((org-at-timestamp-p t)
  9350. (setq time (let ((this-command this-command))
  9351. (org-read-date arg 'totime nil nil default-time default-input)))
  9352. (when (org-at-timestamp-p t) ; just to get the match data
  9353. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9354. (replace-match "")
  9355. (setq org-last-changed-timestamp
  9356. (org-insert-time-stamp
  9357. time (or org-time-was-given arg)
  9358. inactive nil nil (list org-end-time-was-given))))
  9359. (message "Timestamp updated"))
  9360. (t
  9361. (setq time (let ((this-command this-command))
  9362. (org-read-date arg 'totime nil nil default-time default-input)))
  9363. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9364. nil nil (list org-end-time-was-given))))))
  9365. ;; FIXME: can we use this for something else, like computing time differences?
  9366. (defun org-get-compact-tod (s)
  9367. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9368. (let* ((t1 (match-string 1 s))
  9369. (h1 (string-to-number (match-string 2 s)))
  9370. (m1 (string-to-number (match-string 3 s)))
  9371. (t2 (and (match-end 4) (match-string 5 s)))
  9372. (h2 (and t2 (string-to-number (match-string 6 s))))
  9373. (m2 (and t2 (string-to-number (match-string 7 s))))
  9374. dh dm)
  9375. (if (not t2)
  9376. t1
  9377. (setq dh (- h2 h1) dm (- m2 m1))
  9378. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9379. (concat t1 "+" (number-to-string dh)
  9380. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9381. (defun org-time-stamp-inactive (&optional arg)
  9382. "Insert an inactive time stamp.
  9383. An inactive time stamp is enclosed in square brackets instead of angle
  9384. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9385. does not link to the calendar and cannot be changed with the S-cursor keys.
  9386. So these are more for recording a certain time/date."
  9387. (interactive "P")
  9388. (org-time-stamp arg 'inactive))
  9389. (defvar org-date-ovl (org-make-overlay 1 1))
  9390. (org-overlay-put org-date-ovl 'face 'org-warning)
  9391. (org-detach-overlay org-date-ovl)
  9392. (defvar org-ans1) ; dynamically scoped parameter
  9393. (defvar org-ans2) ; dynamically scoped parameter
  9394. (defvar org-plain-time-of-day-regexp) ; defined below
  9395. (defvar org-overriding-default-time nil) ; dynamically scoped
  9396. (defvar org-read-date-overlay nil)
  9397. (defvar org-dcst nil) ; dynamically scoped
  9398. (defun org-read-date (&optional with-time to-time from-string prompt
  9399. default-time default-input)
  9400. "Read a date, possibly a time, and make things smooth for the user.
  9401. The prompt will suggest to enter an ISO date, but you can also enter anything
  9402. which will at least partially be understood by `parse-time-string'.
  9403. Unrecognized parts of the date will default to the current day, month, year,
  9404. hour and minute. If this command is called to replace a timestamp at point,
  9405. of to enter the second timestamp of a range, the default time is taken from the
  9406. existing stamp. For example,
  9407. 3-2-5 --> 2003-02-05
  9408. feb 15 --> currentyear-02-15
  9409. sep 12 9 --> 2009-09-12
  9410. 12:45 --> today 12:45
  9411. 22 sept 0:34 --> currentyear-09-22 0:34
  9412. 12 --> currentyear-currentmonth-12
  9413. Fri --> nearest Friday (today or later)
  9414. etc.
  9415. Furthermore you can specify a relative date by giving, as the *first* thing
  9416. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9417. change in days weeks, months, years.
  9418. With a single plus or minus, the date is relative to today. With a double
  9419. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9420. +4d --> four days from today
  9421. +4 --> same as above
  9422. +2w --> two weeks from today
  9423. ++5 --> five days from default date
  9424. The function understands only English month and weekday abbreviations,
  9425. but this can be configured with the variables `parse-time-months' and
  9426. `parse-time-weekdays'.
  9427. While prompting, a calendar is popped up - you can also select the
  9428. date with the mouse (button 1). The calendar shows a period of three
  9429. months. To scroll it to other months, use the keys `>' and `<'.
  9430. If you don't like the calendar, turn it off with
  9431. \(setq org-read-date-popup-calendar nil)
  9432. With optional argument TO-TIME, the date will immediately be converted
  9433. to an internal time.
  9434. With an optional argument WITH-TIME, the prompt will suggest to also
  9435. insert a time. Note that when WITH-TIME is not set, you can still
  9436. enter a time, and this function will inform the calling routine about
  9437. this change. The calling routine may then choose to change the format
  9438. used to insert the time stamp into the buffer to include the time.
  9439. With optional argument FROM-STRING, read from this string instead from
  9440. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9441. the time/date that is used for everything that is not specified by the
  9442. user."
  9443. (require 'parse-time)
  9444. (let* ((org-time-stamp-rounding-minutes
  9445. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9446. (org-dcst org-display-custom-times)
  9447. (ct (org-current-time))
  9448. (def (or org-overriding-default-time default-time ct))
  9449. (defdecode (decode-time def))
  9450. (dummy (progn
  9451. (when (< (nth 2 defdecode) org-extend-today-until)
  9452. (setcar (nthcdr 2 defdecode) -1)
  9453. (setcar (nthcdr 1 defdecode) 59)
  9454. (setq def (apply 'encode-time defdecode)
  9455. defdecode (decode-time def)))))
  9456. (calendar-move-hook nil)
  9457. (calendar-view-diary-initially-flag nil)
  9458. (view-diary-entries-initially nil)
  9459. (calendar-view-holidays-initially-flag nil)
  9460. (view-calendar-holidays-initially nil)
  9461. (timestr (format-time-string
  9462. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9463. (prompt (concat (if prompt (concat prompt " ") "")
  9464. (format "Date+time [%s]: " timestr)))
  9465. ans (org-ans0 "") org-ans1 org-ans2 final)
  9466. (cond
  9467. (from-string (setq ans from-string))
  9468. (org-read-date-popup-calendar
  9469. (save-excursion
  9470. (save-window-excursion
  9471. (calendar)
  9472. (calendar-forward-day (- (time-to-days def)
  9473. (calendar-absolute-from-gregorian
  9474. (calendar-current-date))))
  9475. (org-eval-in-calendar nil t)
  9476. (let* ((old-map (current-local-map))
  9477. (map (copy-keymap calendar-mode-map))
  9478. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9479. (org-defkey map (kbd "RET") 'org-calendar-select)
  9480. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9481. 'org-calendar-select-mouse)
  9482. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9483. 'org-calendar-select-mouse)
  9484. (org-defkey minibuffer-local-map [(meta shift left)]
  9485. (lambda () (interactive)
  9486. (org-eval-in-calendar '(calendar-backward-month 1))))
  9487. (org-defkey minibuffer-local-map [(meta shift right)]
  9488. (lambda () (interactive)
  9489. (org-eval-in-calendar '(calendar-forward-month 1))))
  9490. (org-defkey minibuffer-local-map [(meta shift up)]
  9491. (lambda () (interactive)
  9492. (org-eval-in-calendar '(calendar-backward-year 1))))
  9493. (org-defkey minibuffer-local-map [(meta shift down)]
  9494. (lambda () (interactive)
  9495. (org-eval-in-calendar '(calendar-forward-year 1))))
  9496. (org-defkey minibuffer-local-map [(shift up)]
  9497. (lambda () (interactive)
  9498. (org-eval-in-calendar '(calendar-backward-week 1))))
  9499. (org-defkey minibuffer-local-map [(shift down)]
  9500. (lambda () (interactive)
  9501. (org-eval-in-calendar '(calendar-forward-week 1))))
  9502. (org-defkey minibuffer-local-map [(shift left)]
  9503. (lambda () (interactive)
  9504. (org-eval-in-calendar '(calendar-backward-day 1))))
  9505. (org-defkey minibuffer-local-map [(shift right)]
  9506. (lambda () (interactive)
  9507. (org-eval-in-calendar '(calendar-forward-day 1))))
  9508. (org-defkey minibuffer-local-map ">"
  9509. (lambda () (interactive)
  9510. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9511. (org-defkey minibuffer-local-map "<"
  9512. (lambda () (interactive)
  9513. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9514. (unwind-protect
  9515. (progn
  9516. (use-local-map map)
  9517. (add-hook 'post-command-hook 'org-read-date-display)
  9518. (setq org-ans0 (read-string prompt default-input nil nil))
  9519. ;; org-ans0: from prompt
  9520. ;; org-ans1: from mouse click
  9521. ;; org-ans2: from calendar motion
  9522. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9523. (remove-hook 'post-command-hook 'org-read-date-display)
  9524. (use-local-map old-map)
  9525. (when org-read-date-overlay
  9526. (org-delete-overlay org-read-date-overlay)
  9527. (setq org-read-date-overlay nil)))))))
  9528. (t ; Naked prompt only
  9529. (unwind-protect
  9530. (setq ans (read-string prompt default-input nil timestr))
  9531. (when org-read-date-overlay
  9532. (org-delete-overlay org-read-date-overlay)
  9533. (setq org-read-date-overlay nil)))))
  9534. (setq final (org-read-date-analyze ans def defdecode))
  9535. (if to-time
  9536. (apply 'encode-time final)
  9537. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9538. (format "%04d-%02d-%02d %02d:%02d"
  9539. (nth 5 final) (nth 4 final) (nth 3 final)
  9540. (nth 2 final) (nth 1 final))
  9541. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9542. (defvar def)
  9543. (defvar defdecode)
  9544. (defvar with-time)
  9545. (defun org-read-date-display ()
  9546. "Display the currrent date prompt interpretation in the minibuffer."
  9547. (when org-read-date-display-live
  9548. (when org-read-date-overlay
  9549. (org-delete-overlay org-read-date-overlay))
  9550. (let ((p (point)))
  9551. (end-of-line 1)
  9552. (while (not (equal (buffer-substring
  9553. (max (point-min) (- (point) 4)) (point))
  9554. " "))
  9555. (insert " "))
  9556. (goto-char p))
  9557. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9558. " " (or org-ans1 org-ans2)))
  9559. (org-end-time-was-given nil)
  9560. (f (org-read-date-analyze ans def defdecode))
  9561. (fmts (if org-dcst
  9562. org-time-stamp-custom-formats
  9563. org-time-stamp-formats))
  9564. (fmt (if (or with-time
  9565. (and (boundp 'org-time-was-given) org-time-was-given))
  9566. (cdr fmts)
  9567. (car fmts)))
  9568. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9569. (when (and org-end-time-was-given
  9570. (string-match org-plain-time-of-day-regexp txt))
  9571. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9572. org-end-time-was-given
  9573. (substring txt (match-end 0)))))
  9574. (setq org-read-date-overlay
  9575. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9576. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9577. (defun org-read-date-analyze (ans def defdecode)
  9578. "Analyze the combined answer of the date prompt."
  9579. ;; FIXME: cleanup and comment
  9580. (let (delta deltan deltaw deltadef year month day
  9581. hour minute second wday pm h2 m2 tl wday1
  9582. iso-year iso-weekday iso-week iso-year iso-date)
  9583. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9584. (setq ans "+0"))
  9585. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9586. (setq ans (replace-match "" t t ans)
  9587. deltan (car delta)
  9588. deltaw (nth 1 delta)
  9589. deltadef (nth 2 delta)))
  9590. ;; Check if there is an iso week date in there
  9591. ;; If yes, sore the info and ostpone interpreting it until the rest
  9592. ;; of the parsing is done
  9593. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9594. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9595. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9596. iso-week (string-to-number (match-string 2 ans)))
  9597. (setq ans (replace-match "" t t ans)))
  9598. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9599. (when (string-match
  9600. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9601. (setq year (if (match-end 2)
  9602. (string-to-number (match-string 2 ans))
  9603. (string-to-number (format-time-string "%Y")))
  9604. month (string-to-number (match-string 3 ans))
  9605. day (string-to-number (match-string 4 ans)))
  9606. (if (< year 100) (setq year (+ 2000 year)))
  9607. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9608. t nil ans)))
  9609. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9610. ;; If there is a time with am/pm, and *no* time without it, we convert
  9611. ;; so that matching will be successful.
  9612. (loop for i from 1 to 2 do ; twice, for end time as well
  9613. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9614. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9615. (setq hour (string-to-number (match-string 1 ans))
  9616. minute (if (match-end 3)
  9617. (string-to-number (match-string 3 ans))
  9618. 0)
  9619. pm (equal ?p
  9620. (string-to-char (downcase (match-string 4 ans)))))
  9621. (if (and (= hour 12) (not pm))
  9622. (setq hour 0)
  9623. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9624. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9625. t t ans))))
  9626. ;; Check if a time range is given as a duration
  9627. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9628. (setq hour (string-to-number (match-string 1 ans))
  9629. h2 (+ hour (string-to-number (match-string 3 ans)))
  9630. minute (string-to-number (match-string 2 ans))
  9631. m2 (+ minute (if (match-end 5) (string-to-number
  9632. (match-string 5 ans))0)))
  9633. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9634. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9635. t t ans)))
  9636. ;; Check if there is a time range
  9637. (when (boundp 'org-end-time-was-given)
  9638. (setq org-time-was-given nil)
  9639. (when (and (string-match org-plain-time-of-day-regexp ans)
  9640. (match-end 8))
  9641. (setq org-end-time-was-given (match-string 8 ans))
  9642. (setq ans (concat (substring ans 0 (match-beginning 7))
  9643. (substring ans (match-end 7))))))
  9644. (setq tl (parse-time-string ans)
  9645. day (or (nth 3 tl) (nth 3 defdecode))
  9646. month (or (nth 4 tl)
  9647. (if (and org-read-date-prefer-future
  9648. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9649. (1+ (nth 4 defdecode))
  9650. (nth 4 defdecode)))
  9651. year (or (nth 5 tl)
  9652. (if (and org-read-date-prefer-future
  9653. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9654. (1+ (nth 5 defdecode))
  9655. (nth 5 defdecode)))
  9656. hour (or (nth 2 tl) (nth 2 defdecode))
  9657. minute (or (nth 1 tl) (nth 1 defdecode))
  9658. second (or (nth 0 tl) 0)
  9659. wday (nth 6 tl))
  9660. ;; Special date definitions below
  9661. (cond
  9662. (iso-week
  9663. ;; There was an iso week
  9664. (setq year (or iso-year year)
  9665. day (or iso-weekday wday 1)
  9666. wday nil ; to make sure that the trigger below does not match
  9667. iso-date (calendar-gregorian-from-absolute
  9668. (calendar-absolute-from-iso
  9669. (list iso-week day year))))
  9670. ; FIXME: Should we also push ISO weeks into the future?
  9671. ; (when (and org-read-date-prefer-future
  9672. ; (not iso-year)
  9673. ; (< (calendar-absolute-from-gregorian iso-date)
  9674. ; (time-to-days (current-time))))
  9675. ; (setq year (1+ year)
  9676. ; iso-date (calendar-gregorian-from-absolute
  9677. ; (calendar-absolute-from-iso
  9678. ; (list iso-week day year)))))
  9679. (setq month (car iso-date)
  9680. year (nth 2 iso-date)
  9681. day (nth 1 iso-date)))
  9682. (deltan
  9683. (unless deltadef
  9684. (let ((now (decode-time (current-time))))
  9685. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9686. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9687. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9688. ((equal deltaw "m") (setq month (+ month deltan)))
  9689. ((equal deltaw "y") (setq year (+ year deltan)))))
  9690. ((and wday (not (nth 3 tl)))
  9691. ;; Weekday was given, but no day, so pick that day in the week
  9692. ;; on or after the derived date.
  9693. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9694. (unless (equal wday wday1)
  9695. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9696. (if (and (boundp 'org-time-was-given)
  9697. (nth 2 tl))
  9698. (setq org-time-was-given t))
  9699. (if (< year 100) (setq year (+ 2000 year)))
  9700. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9701. (list second minute hour day month year)))
  9702. (defvar parse-time-weekdays)
  9703. (defun org-read-date-get-relative (s today default)
  9704. "Check string S for special relative date string.
  9705. TODAY and DEFAULT are internal times, for today and for a default.
  9706. Return shift list (N what def-flag)
  9707. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9708. N is the number of WHATs to shift.
  9709. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9710. the DEFAULT date rather than TODAY."
  9711. (when (and
  9712. (string-match
  9713. (concat
  9714. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9715. "\\([0-9]+\\)?"
  9716. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9717. "\\([ \t]\\|$\\)") s)
  9718. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9719. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9720. (string-to-char (substring (match-string 1 s) -1))
  9721. ?+))
  9722. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9723. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9724. (what (if (match-end 3) (match-string 3 s) "d"))
  9725. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9726. (date (if rel default today))
  9727. (wday (nth 6 (decode-time date)))
  9728. delta)
  9729. (if wday1
  9730. (progn
  9731. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9732. (if (= dir ?-) (setq delta (- delta 7)))
  9733. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9734. (list delta "d" rel))
  9735. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9736. (defun org-eval-in-calendar (form &optional keepdate)
  9737. "Eval FORM in the calendar window and return to current window.
  9738. Also, store the cursor date in variable org-ans2."
  9739. (let ((sw (selected-window)))
  9740. (select-window (get-buffer-window "*Calendar*"))
  9741. (eval form)
  9742. (when (and (not keepdate) (calendar-cursor-to-date))
  9743. (let* ((date (calendar-cursor-to-date))
  9744. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9745. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9746. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9747. (select-window sw)))
  9748. (defun org-calendar-select ()
  9749. "Return to `org-read-date' with the date currently selected.
  9750. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9751. (interactive)
  9752. (when (calendar-cursor-to-date)
  9753. (let* ((date (calendar-cursor-to-date))
  9754. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9755. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9756. (if (active-minibuffer-window) (exit-minibuffer))))
  9757. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9758. "Insert a date stamp for the date given by the internal TIME.
  9759. WITH-HM means, use the stamp format that includes the time of the day.
  9760. INACTIVE means use square brackets instead of angular ones, so that the
  9761. stamp will not contribute to the agenda.
  9762. PRE and POST are optional strings to be inserted before and after the
  9763. stamp.
  9764. The command returns the inserted time stamp."
  9765. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9766. stamp)
  9767. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9768. (insert-before-markers (or pre ""))
  9769. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9770. (when (listp extra)
  9771. (setq extra (car extra))
  9772. (if (and (stringp extra)
  9773. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9774. (setq extra (format "-%02d:%02d"
  9775. (string-to-number (match-string 1 extra))
  9776. (string-to-number (match-string 2 extra))))
  9777. (setq extra nil)))
  9778. (when extra
  9779. (backward-char 1)
  9780. (insert-before-markers extra)
  9781. (forward-char 1))
  9782. (insert-before-markers (or post ""))
  9783. (setq org-last-inserted-timestamp stamp)))
  9784. (defun org-toggle-time-stamp-overlays ()
  9785. "Toggle the use of custom time stamp formats."
  9786. (interactive)
  9787. (setq org-display-custom-times (not org-display-custom-times))
  9788. (unless org-display-custom-times
  9789. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9790. (while (setq p (next-single-property-change p 'display))
  9791. (if (and (get-text-property p 'display)
  9792. (eq (get-text-property p 'face) 'org-date))
  9793. (remove-text-properties
  9794. p (setq p (next-single-property-change p 'display))
  9795. '(display t))))
  9796. (set-buffer-modified-p bmp)))
  9797. (if (featurep 'xemacs)
  9798. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9799. (org-restart-font-lock)
  9800. (setq org-table-may-need-update t)
  9801. (if org-display-custom-times
  9802. (message "Time stamps are overlayed with custom format")
  9803. (message "Time stamp overlays removed")))
  9804. (defun org-display-custom-time (beg end)
  9805. "Overlay modified time stamp format over timestamp between BEG and END."
  9806. (let* ((ts (buffer-substring beg end))
  9807. t1 w1 with-hm tf time str w2 (off 0))
  9808. (save-match-data
  9809. (setq t1 (org-parse-time-string ts t))
  9810. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9811. (setq off (- (match-end 0) (match-beginning 0)))))
  9812. (setq end (- end off))
  9813. (setq w1 (- end beg)
  9814. with-hm (and (nth 1 t1) (nth 2 t1))
  9815. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9816. time (org-fix-decoded-time t1)
  9817. str (org-add-props
  9818. (format-time-string
  9819. (substring tf 1 -1) (apply 'encode-time time))
  9820. nil 'mouse-face 'highlight)
  9821. w2 (length str))
  9822. (if (not (= w2 w1))
  9823. (add-text-properties (1+ beg) (+ 2 beg)
  9824. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9825. (if (featurep 'xemacs)
  9826. (progn
  9827. (put-text-property beg end 'invisible t)
  9828. (put-text-property beg end 'end-glyph (make-glyph str)))
  9829. (put-text-property beg end 'display str))))
  9830. (defun org-translate-time (string)
  9831. "Translate all timestamps in STRING to custom format.
  9832. But do this only if the variable `org-display-custom-times' is set."
  9833. (when org-display-custom-times
  9834. (save-match-data
  9835. (let* ((start 0)
  9836. (re org-ts-regexp-both)
  9837. t1 with-hm inactive tf time str beg end)
  9838. (while (setq start (string-match re string start))
  9839. (setq beg (match-beginning 0)
  9840. end (match-end 0)
  9841. t1 (save-match-data
  9842. (org-parse-time-string (substring string beg end) t))
  9843. with-hm (and (nth 1 t1) (nth 2 t1))
  9844. inactive (equal (substring string beg (1+ beg)) "[")
  9845. tf (funcall (if with-hm 'cdr 'car)
  9846. org-time-stamp-custom-formats)
  9847. time (org-fix-decoded-time t1)
  9848. str (format-time-string
  9849. (concat
  9850. (if inactive "[" "<") (substring tf 1 -1)
  9851. (if inactive "]" ">"))
  9852. (apply 'encode-time time))
  9853. string (replace-match str t t string)
  9854. start (+ start (length str)))))))
  9855. string)
  9856. (defun org-fix-decoded-time (time)
  9857. "Set 0 instead of nil for the first 6 elements of time.
  9858. Don't touch the rest."
  9859. (let ((n 0))
  9860. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9861. (defun org-days-to-time (timestamp-string)
  9862. "Difference between TIMESTAMP-STRING and now in days."
  9863. (- (time-to-days (org-time-string-to-time timestamp-string))
  9864. (time-to-days (current-time))))
  9865. (defun org-deadline-close (timestamp-string &optional ndays)
  9866. "Is the time in TIMESTAMP-STRING close to the current date?"
  9867. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9868. (and (< (org-days-to-time timestamp-string) ndays)
  9869. (not (org-entry-is-done-p))))
  9870. (defun org-get-wdays (ts)
  9871. "Get the deadline lead time appropriate for timestring TS."
  9872. (cond
  9873. ((<= org-deadline-warning-days 0)
  9874. ;; 0 or negative, enforce this value no matter what
  9875. (- org-deadline-warning-days))
  9876. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9877. ;; lead time is specified.
  9878. (floor (* (string-to-number (match-string 1 ts))
  9879. (cdr (assoc (match-string 2 ts)
  9880. '(("d" . 1) ("w" . 7)
  9881. ("m" . 30.4) ("y" . 365.25)))))))
  9882. ;; go for the default.
  9883. (t org-deadline-warning-days)))
  9884. (defun org-calendar-select-mouse (ev)
  9885. "Return to `org-read-date' with the date currently selected.
  9886. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9887. (interactive "e")
  9888. (mouse-set-point ev)
  9889. (when (calendar-cursor-to-date)
  9890. (let* ((date (calendar-cursor-to-date))
  9891. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9892. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9893. (if (active-minibuffer-window) (exit-minibuffer))))
  9894. (defun org-check-deadlines (ndays)
  9895. "Check if there are any deadlines due or past due.
  9896. A deadline is considered due if it happens within `org-deadline-warning-days'
  9897. days from today's date. If the deadline appears in an entry marked DONE,
  9898. it is not shown. The prefix arg NDAYS can be used to test that many
  9899. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9900. (interactive "P")
  9901. (let* ((org-warn-days
  9902. (cond
  9903. ((equal ndays '(4)) 100000)
  9904. (ndays (prefix-numeric-value ndays))
  9905. (t (abs org-deadline-warning-days))))
  9906. (case-fold-search nil)
  9907. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9908. (callback
  9909. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9910. (message "%d deadlines past-due or due within %d days"
  9911. (org-occur regexp nil callback)
  9912. org-warn-days)))
  9913. (defun org-check-before-date (date)
  9914. "Check if there are deadlines or scheduled entries before DATE."
  9915. (interactive (list (org-read-date)))
  9916. (let ((case-fold-search nil)
  9917. (regexp (concat "\\<\\(" org-deadline-string
  9918. "\\|" org-scheduled-string
  9919. "\\) *<\\([^>]+\\)>"))
  9920. (callback
  9921. (lambda () (time-less-p
  9922. (org-time-string-to-time (match-string 2))
  9923. (org-time-string-to-time date)))))
  9924. (message "%d entries before %s"
  9925. (org-occur regexp nil callback) date)))
  9926. (defun org-evaluate-time-range (&optional to-buffer)
  9927. "Evaluate a time range by computing the difference between start and end.
  9928. Normally the result is just printed in the echo area, but with prefix arg
  9929. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9930. If the time range is actually in a table, the result is inserted into the
  9931. next column.
  9932. For time difference computation, a year is assumed to be exactly 365
  9933. days in order to avoid rounding problems."
  9934. (interactive "P")
  9935. (or
  9936. (org-clock-update-time-maybe)
  9937. (save-excursion
  9938. (unless (org-at-date-range-p t)
  9939. (goto-char (point-at-bol))
  9940. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9941. (if (not (org-at-date-range-p t))
  9942. (error "Not at a time-stamp range, and none found in current line")))
  9943. (let* ((ts1 (match-string 1))
  9944. (ts2 (match-string 2))
  9945. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9946. (match-end (match-end 0))
  9947. (time1 (org-time-string-to-time ts1))
  9948. (time2 (org-time-string-to-time ts2))
  9949. (t1 (time-to-seconds time1))
  9950. (t2 (time-to-seconds time2))
  9951. (diff (abs (- t2 t1)))
  9952. (negative (< (- t2 t1) 0))
  9953. ;; (ys (floor (* 365 24 60 60)))
  9954. (ds (* 24 60 60))
  9955. (hs (* 60 60))
  9956. (fy "%dy %dd %02d:%02d")
  9957. (fy1 "%dy %dd")
  9958. (fd "%dd %02d:%02d")
  9959. (fd1 "%dd")
  9960. (fh "%02d:%02d")
  9961. y d h m align)
  9962. (if havetime
  9963. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9964. y 0
  9965. d (floor (/ diff ds)) diff (mod diff ds)
  9966. h (floor (/ diff hs)) diff (mod diff hs)
  9967. m (floor (/ diff 60)))
  9968. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9969. y 0
  9970. d (floor (+ (/ diff ds) 0.5))
  9971. h 0 m 0))
  9972. (if (not to-buffer)
  9973. (message "%s" (org-make-tdiff-string y d h m))
  9974. (if (org-at-table-p)
  9975. (progn
  9976. (goto-char match-end)
  9977. (setq align t)
  9978. (and (looking-at " *|") (goto-char (match-end 0))))
  9979. (goto-char match-end))
  9980. (if (looking-at
  9981. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9982. (replace-match ""))
  9983. (if negative (insert " -"))
  9984. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9985. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9986. (insert " " (format fh h m))))
  9987. (if align (org-table-align))
  9988. (message "Time difference inserted")))))
  9989. (defun org-make-tdiff-string (y d h m)
  9990. (let ((fmt "")
  9991. (l nil))
  9992. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9993. l (push y l)))
  9994. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9995. l (push d l)))
  9996. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9997. l (push h l)))
  9998. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9999. l (push m l)))
  10000. (apply 'format fmt (nreverse l))))
  10001. (defun org-time-string-to-time (s)
  10002. (apply 'encode-time (org-parse-time-string s)))
  10003. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10004. "Convert a time stamp to an absolute day number.
  10005. If there is a specifyer for a cyclic time stamp, get the closest date to
  10006. DAYNR.
  10007. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10008. (cond
  10009. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10010. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10011. daynr
  10012. (+ daynr 1000)))
  10013. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10014. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10015. (time-to-days (current-time))) (match-string 0 s)
  10016. prefer show-all))
  10017. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10018. (defun org-days-to-iso-week (days)
  10019. "Return the iso week number."
  10020. (require 'cal-iso)
  10021. (car (calendar-iso-from-absolute days)))
  10022. (defun org-small-year-to-year (year)
  10023. "Convert 2-digit years into 4-digit years.
  10024. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10025. The year 2000 cannot be abbreviated. Any year lager than 99
  10026. is retrned unchanged."
  10027. (if (< year 38)
  10028. (setq year (+ 2000 year))
  10029. (if (< year 100)
  10030. (setq year (+ 1900 year))))
  10031. year)
  10032. (defun org-time-from-absolute (d)
  10033. "Return the time corresponding to date D.
  10034. D may be an absolute day number, or a calendar-type list (month day year)."
  10035. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10036. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10037. (defun org-calendar-holiday ()
  10038. "List of holidays, for Diary display in Org-mode."
  10039. (require 'holidays)
  10040. (let ((hl (funcall
  10041. (if (fboundp 'calendar-check-holidays)
  10042. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10043. (if hl (mapconcat 'identity hl "; "))))
  10044. (defun org-diary-sexp-entry (sexp entry date)
  10045. "Process a SEXP diary ENTRY for DATE."
  10046. (require 'diary-lib)
  10047. (let ((result (if calendar-debug-sexp
  10048. (let ((stack-trace-on-error t))
  10049. (eval (car (read-from-string sexp))))
  10050. (condition-case nil
  10051. (eval (car (read-from-string sexp)))
  10052. (error
  10053. (beep)
  10054. (message "Bad sexp at line %d in %s: %s"
  10055. (org-current-line)
  10056. (buffer-file-name) sexp)
  10057. (sleep-for 2))))))
  10058. (cond ((stringp result) result)
  10059. ((and (consp result)
  10060. (stringp (cdr result))) (cdr result))
  10061. (result entry)
  10062. (t nil))))
  10063. (defun org-diary-to-ical-string (frombuf)
  10064. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10065. This uses the icalendar.el library."
  10066. (let* ((tmpdir (if (featurep 'xemacs)
  10067. (temp-directory)
  10068. temporary-file-directory))
  10069. (tmpfile (make-temp-name
  10070. (expand-file-name "orgics" tmpdir)))
  10071. buf rtn b e)
  10072. (save-excursion
  10073. (set-buffer frombuf)
  10074. (icalendar-export-region (point-min) (point-max) tmpfile)
  10075. (setq buf (find-buffer-visiting tmpfile))
  10076. (set-buffer buf)
  10077. (goto-char (point-min))
  10078. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10079. (setq b (match-beginning 0)))
  10080. (goto-char (point-max))
  10081. (if (re-search-backward "^END:VEVENT" nil t)
  10082. (setq e (match-end 0)))
  10083. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10084. (kill-buffer buf)
  10085. (delete-file tmpfile)
  10086. rtn))
  10087. (defun org-closest-date (start current change prefer show-all)
  10088. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10089. When PREFER is `past' return a date that is either CURRENT or past.
  10090. When PREFER is `future', return a date that is either CURRENT or future.
  10091. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10092. ;; Make the proper lists from the dates
  10093. (catch 'exit
  10094. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10095. dn dw sday cday n1 n2
  10096. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10097. (setq start (org-date-to-gregorian start)
  10098. current (org-date-to-gregorian
  10099. (if show-all
  10100. current
  10101. (time-to-days (current-time))))
  10102. sday (calendar-absolute-from-gregorian start)
  10103. cday (calendar-absolute-from-gregorian current))
  10104. (if (<= cday sday) (throw 'exit sday))
  10105. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10106. (setq dn (string-to-number (match-string 1 change))
  10107. dw (cdr (assoc (match-string 2 change) a1)))
  10108. (error "Invalid change specifyer: %s" change))
  10109. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10110. (cond
  10111. ((eq dw 'day)
  10112. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10113. n2 (+ n1 dn)))
  10114. ((eq dw 'year)
  10115. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10116. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10117. (setq date1 (list m d y1)
  10118. n1 (calendar-absolute-from-gregorian date1)
  10119. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10120. n2 (calendar-absolute-from-gregorian date2)))
  10121. ((eq dw 'month)
  10122. ;; approx number of month between the two dates
  10123. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10124. ;; How often does dn fit in there?
  10125. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10126. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10127. m (+ m nm)
  10128. ny (floor (/ m 12))
  10129. y (+ y ny)
  10130. m (- m (* ny 12)))
  10131. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10132. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10133. (setq m2 (+ m dn) y2 y)
  10134. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10135. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10136. (while (<= n2 cday)
  10137. (setq n1 n2 m m2 y y2)
  10138. (setq m2 (+ m dn) y2 y)
  10139. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10140. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10141. (if show-all
  10142. (cond
  10143. ((eq prefer 'past) n1)
  10144. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10145. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10146. (cond
  10147. ((eq prefer 'past) n1)
  10148. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10149. (t (if (= cday n1) n1 n2)))))))
  10150. (defun org-date-to-gregorian (date)
  10151. "Turn any specification of DATE into a gregorian date for the calendar."
  10152. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10153. ((and (listp date) (= (length date) 3)) date)
  10154. ((stringp date)
  10155. (setq date (org-parse-time-string date))
  10156. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10157. ((listp date)
  10158. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10159. (defun org-parse-time-string (s &optional nodefault)
  10160. "Parse the standard Org-mode time string.
  10161. This should be a lot faster than the normal `parse-time-string'.
  10162. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10163. hour and minute fields will be nil if not given."
  10164. (if (string-match org-ts-regexp0 s)
  10165. (list 0
  10166. (if (or (match-beginning 8) (not nodefault))
  10167. (string-to-number (or (match-string 8 s) "0")))
  10168. (if (or (match-beginning 7) (not nodefault))
  10169. (string-to-number (or (match-string 7 s) "0")))
  10170. (string-to-number (match-string 4 s))
  10171. (string-to-number (match-string 3 s))
  10172. (string-to-number (match-string 2 s))
  10173. nil nil nil)
  10174. (make-list 9 0)))
  10175. (defun org-timestamp-up (&optional arg)
  10176. "Increase the date item at the cursor by one.
  10177. If the cursor is on the year, change the year. If it is on the month or
  10178. the day, change that.
  10179. With prefix ARG, change by that many units."
  10180. (interactive "p")
  10181. (org-timestamp-change (prefix-numeric-value arg)))
  10182. (defun org-timestamp-down (&optional arg)
  10183. "Decrease the date item at the cursor by one.
  10184. If the cursor is on the year, change the year. If it is on the month or
  10185. the day, change that.
  10186. With prefix ARG, change by that many units."
  10187. (interactive "p")
  10188. (org-timestamp-change (- (prefix-numeric-value arg))))
  10189. (defun org-timestamp-up-day (&optional arg)
  10190. "Increase the date in the time stamp by one day.
  10191. With prefix ARG, change that many days."
  10192. (interactive "p")
  10193. (if (and (not (org-at-timestamp-p t))
  10194. (org-on-heading-p))
  10195. (org-todo 'up)
  10196. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10197. (defun org-timestamp-down-day (&optional arg)
  10198. "Decrease the date in the time stamp by one day.
  10199. With prefix ARG, change that many days."
  10200. (interactive "p")
  10201. (if (and (not (org-at-timestamp-p t))
  10202. (org-on-heading-p))
  10203. (org-todo 'down)
  10204. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10205. (defun org-at-timestamp-p (&optional inactive-ok)
  10206. "Determine if the cursor is in or at a timestamp."
  10207. (interactive)
  10208. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10209. (pos (point))
  10210. (ans (or (looking-at tsr)
  10211. (save-excursion
  10212. (skip-chars-backward "^[<\n\r\t")
  10213. (if (> (point) (point-min)) (backward-char 1))
  10214. (and (looking-at tsr)
  10215. (> (- (match-end 0) pos) -1))))))
  10216. (and ans
  10217. (boundp 'org-ts-what)
  10218. (setq org-ts-what
  10219. (cond
  10220. ((= pos (match-beginning 0)) 'bracket)
  10221. ((= pos (1- (match-end 0))) 'bracket)
  10222. ((org-pos-in-match-range pos 2) 'year)
  10223. ((org-pos-in-match-range pos 3) 'month)
  10224. ((org-pos-in-match-range pos 7) 'hour)
  10225. ((org-pos-in-match-range pos 8) 'minute)
  10226. ((or (org-pos-in-match-range pos 4)
  10227. (org-pos-in-match-range pos 5)) 'day)
  10228. ((and (> pos (or (match-end 8) (match-end 5)))
  10229. (< pos (match-end 0)))
  10230. (- pos (or (match-end 8) (match-end 5))))
  10231. (t 'day))))
  10232. ans))
  10233. (defun org-toggle-timestamp-type ()
  10234. "Toggle the type (<active> or [inactive]) of a time stamp."
  10235. (interactive)
  10236. (when (org-at-timestamp-p t)
  10237. (save-excursion
  10238. (goto-char (match-beginning 0))
  10239. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10240. (goto-char (1- (match-end 0)))
  10241. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10242. (message "Timestamp is now %sactive"
  10243. (if (equal (char-before) ?>) "in" ""))))
  10244. (defun org-timestamp-change (n &optional what)
  10245. "Change the date in the time stamp at point.
  10246. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10247. `year', `minute', `second'. If WHAT is not given, the cursor position
  10248. in the timestamp determines what will be changed."
  10249. (let ((pos (point))
  10250. with-hm inactive
  10251. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10252. org-ts-what
  10253. extra rem
  10254. ts time time0)
  10255. (if (not (org-at-timestamp-p t))
  10256. (error "Not at a timestamp"))
  10257. (if (and (not what) (eq org-ts-what 'bracket))
  10258. (org-toggle-timestamp-type)
  10259. (if (and (not what) (not (eq org-ts-what 'day))
  10260. org-display-custom-times
  10261. (get-text-property (point) 'display)
  10262. (not (get-text-property (1- (point)) 'display)))
  10263. (setq org-ts-what 'day))
  10264. (setq org-ts-what (or what org-ts-what)
  10265. inactive (= (char-after (match-beginning 0)) ?\[)
  10266. ts (match-string 0))
  10267. (replace-match "")
  10268. (if (string-match
  10269. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10270. ts)
  10271. (setq extra (match-string 1 ts)))
  10272. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10273. (setq with-hm t))
  10274. (setq time0 (org-parse-time-string ts))
  10275. (when (and (eq org-ts-what 'minute)
  10276. (eq current-prefix-arg nil))
  10277. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10278. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10279. (setcar (cdr time0) (+ (nth 1 time0)
  10280. (if (> n 0) (- rem) (- dm rem))))))
  10281. (setq time
  10282. (encode-time (or (car time0) 0)
  10283. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10284. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10285. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10286. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10287. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10288. (nthcdr 6 time0)))
  10289. (when (integerp org-ts-what)
  10290. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10291. (if (eq what 'calendar)
  10292. (let ((cal-date (org-get-date-from-calendar)))
  10293. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10294. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10295. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10296. (setcar time0 (or (car time0) 0))
  10297. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10298. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10299. (setq time (apply 'encode-time time0))))
  10300. (setq org-last-changed-timestamp
  10301. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10302. (org-clock-update-time-maybe)
  10303. (goto-char pos)
  10304. ;; Try to recenter the calendar window, if any
  10305. (if (and org-calendar-follow-timestamp-change
  10306. (get-buffer-window "*Calendar*" t)
  10307. (memq org-ts-what '(day month year)))
  10308. (org-recenter-calendar (time-to-days time))))))
  10309. (defun org-modify-ts-extra (s pos n dm)
  10310. "Change the different parts of the lead-time and repeat fields in timestamp."
  10311. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10312. ng h m new rem)
  10313. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10314. (cond
  10315. ((or (org-pos-in-match-range pos 2)
  10316. (org-pos-in-match-range pos 3))
  10317. (setq m (string-to-number (match-string 3 s))
  10318. h (string-to-number (match-string 2 s)))
  10319. (if (org-pos-in-match-range pos 2)
  10320. (setq h (+ h n))
  10321. (setq n (* dm (org-no-warnings (signum n))))
  10322. (when (not (= 0 (setq rem (% m dm))))
  10323. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10324. (setq m (+ m n)))
  10325. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10326. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10327. (setq h (min 24 (max 0 h)))
  10328. (setq ng 1 new (format "-%02d:%02d" h m)))
  10329. ((org-pos-in-match-range pos 6)
  10330. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10331. ((org-pos-in-match-range pos 5)
  10332. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10333. ((org-pos-in-match-range pos 9)
  10334. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10335. ((org-pos-in-match-range pos 8)
  10336. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10337. (when ng
  10338. (setq s (concat
  10339. (substring s 0 (match-beginning ng))
  10340. new
  10341. (substring s (match-end ng))))))
  10342. s))
  10343. (defun org-recenter-calendar (date)
  10344. "If the calendar is visible, recenter it to DATE."
  10345. (let* ((win (selected-window))
  10346. (cwin (get-buffer-window "*Calendar*" t))
  10347. (calendar-move-hook nil))
  10348. (when cwin
  10349. (select-window cwin)
  10350. (calendar-goto-date (if (listp date) date
  10351. (calendar-gregorian-from-absolute date)))
  10352. (select-window win))))
  10353. (defun org-goto-calendar (&optional arg)
  10354. "Go to the Emacs calendar at the current date.
  10355. If there is a time stamp in the current line, go to that date.
  10356. A prefix ARG can be used to force the current date."
  10357. (interactive "P")
  10358. (let ((tsr org-ts-regexp) diff
  10359. (calendar-move-hook nil)
  10360. (calendar-view-holidays-initially-flag nil)
  10361. (view-calendar-holidays-initially nil)
  10362. (calendar-view-diary-initially-flag nil)
  10363. (view-diary-entries-initially nil))
  10364. (if (or (org-at-timestamp-p)
  10365. (save-excursion
  10366. (beginning-of-line 1)
  10367. (looking-at (concat ".*" tsr))))
  10368. (let ((d1 (time-to-days (current-time)))
  10369. (d2 (time-to-days
  10370. (org-time-string-to-time (match-string 1)))))
  10371. (setq diff (- d2 d1))))
  10372. (calendar)
  10373. (calendar-goto-today)
  10374. (if (and diff (not arg)) (calendar-forward-day diff))))
  10375. (defun org-get-date-from-calendar ()
  10376. "Return a list (month day year) of date at point in calendar."
  10377. (with-current-buffer "*Calendar*"
  10378. (save-match-data
  10379. (calendar-cursor-to-date))))
  10380. (defun org-date-from-calendar ()
  10381. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10382. If there is already a time stamp at the cursor position, update it."
  10383. (interactive)
  10384. (if (org-at-timestamp-p t)
  10385. (org-timestamp-change 0 'calendar)
  10386. (let ((cal-date (org-get-date-from-calendar)))
  10387. (org-insert-time-stamp
  10388. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10389. (defun org-minutes-to-hh:mm-string (m)
  10390. "Compute H:MM from a number of minutes."
  10391. (let ((h (/ m 60)))
  10392. (setq m (- m (* 60 h)))
  10393. (format org-time-clocksum-format h m)))
  10394. (defun org-hh:mm-string-to-minutes (s)
  10395. "Convert a string H:MM to a number of minutes."
  10396. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10397. (+ (* (string-to-number (match-string 1 s)) 60)
  10398. (string-to-number (match-string 2 s)))
  10399. 0))
  10400. ;;;; Agenda files
  10401. ;;;###autoload
  10402. (defun org-iswitchb (&optional arg)
  10403. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10404. With a prefix argument, restrict available to files.
  10405. With two prefix arguments, restrict available buffers to agenda files.
  10406. Due to some yet unresolved reason, the global function
  10407. `iswitchb-mode' needs to be active for this function to work."
  10408. (interactive "P")
  10409. (require 'iswitchb)
  10410. (let ((enabled iswitchb-mode) blist)
  10411. (or enabled (iswitchb-mode 1))
  10412. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10413. ((equal arg '(16)) (org-buffer-list 'agenda))
  10414. (t (org-buffer-list))))
  10415. (unwind-protect
  10416. (let ((iswitchb-make-buflist-hook
  10417. (lambda ()
  10418. (setq iswitchb-temp-buflist
  10419. (mapcar 'buffer-name blist)))))
  10420. (switch-to-buffer
  10421. (iswitchb-read-buffer
  10422. "Switch-to: " nil t))
  10423. (or enabled (iswitchb-mode -1))))))
  10424. (defun org-buffer-list (&optional predicate exclude-tmp)
  10425. "Return a list of Org buffers.
  10426. PREDICATE can be `export', `files' or `agenda'.
  10427. export restrict the list to Export buffers.
  10428. files restrict the list to buffers visiting Org files.
  10429. agenda restrict the list to buffers visiting agenda files.
  10430. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10431. (let* ((bfn nil)
  10432. (agenda-files (and (eq predicate 'agenda)
  10433. (mapcar 'file-truename (org-agenda-files t))))
  10434. (filter
  10435. (cond
  10436. ((eq predicate 'files)
  10437. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10438. ((eq predicate 'export)
  10439. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10440. ((eq predicate 'agenda)
  10441. (lambda (b)
  10442. (with-current-buffer b
  10443. (and (eq major-mode 'org-mode)
  10444. (setq bfn (buffer-file-name b))
  10445. (member (file-truename bfn) agenda-files)))))
  10446. (t (lambda (b) (with-current-buffer b
  10447. (or (eq major-mode 'org-mode)
  10448. (string-match "\*Org .*Export"
  10449. (buffer-name b)))))))))
  10450. (delq nil
  10451. (mapcar
  10452. (lambda(b)
  10453. (if (and (funcall filter b)
  10454. (or (not exclude-tmp)
  10455. (not (string-match "tmp" (buffer-name b)))))
  10456. b
  10457. nil))
  10458. (buffer-list)))))
  10459. (defun org-agenda-files (&optional unrestricted archives)
  10460. "Get the list of agenda files.
  10461. Optional UNRESTRICTED means return the full list even if a restriction
  10462. is currently in place.
  10463. When ARCHIVES is t, include all archive files hat are really being
  10464. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10465. `org-agenda-archives-mode' is t."
  10466. (let ((files
  10467. (cond
  10468. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10469. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10470. ((listp org-agenda-files) org-agenda-files)
  10471. (t (error "Invalid value of `org-agenda-files'")))))
  10472. (setq files (apply 'append
  10473. (mapcar (lambda (f)
  10474. (if (file-directory-p f)
  10475. (directory-files
  10476. f t org-agenda-file-regexp)
  10477. (list f)))
  10478. files)))
  10479. (when org-agenda-skip-unavailable-files
  10480. (setq files (delq nil
  10481. (mapcar (function
  10482. (lambda (file)
  10483. (and (file-readable-p file) file)))
  10484. files))))
  10485. (when (or (eq archives t)
  10486. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10487. (setq files (org-add-archive-files files)))
  10488. files))
  10489. (defun org-edit-agenda-file-list ()
  10490. "Edit the list of agenda files.
  10491. Depending on setup, this either uses customize to edit the variable
  10492. `org-agenda-files', or it visits the file that is holding the list. In the
  10493. latter case, the buffer is set up in a way that saving it automatically kills
  10494. the buffer and restores the previous window configuration."
  10495. (interactive)
  10496. (if (stringp org-agenda-files)
  10497. (let ((cw (current-window-configuration)))
  10498. (find-file org-agenda-files)
  10499. (org-set-local 'org-window-configuration cw)
  10500. (org-add-hook 'after-save-hook
  10501. (lambda ()
  10502. (set-window-configuration
  10503. (prog1 org-window-configuration
  10504. (kill-buffer (current-buffer))))
  10505. (org-install-agenda-files-menu)
  10506. (message "New agenda file list installed"))
  10507. nil 'local)
  10508. (message "%s" (substitute-command-keys
  10509. "Edit list and finish with \\[save-buffer]")))
  10510. (customize-variable 'org-agenda-files)))
  10511. (defun org-store-new-agenda-file-list (list)
  10512. "Set new value for the agenda file list and save it correcly."
  10513. (if (stringp org-agenda-files)
  10514. (let ((f org-agenda-files) b)
  10515. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10516. (with-temp-file f
  10517. (insert (mapconcat 'identity list "\n") "\n")))
  10518. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10519. (setq org-agenda-files list)
  10520. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10521. (defun org-read-agenda-file-list ()
  10522. "Read the list of agenda files from a file."
  10523. (when (file-directory-p org-agenda-files)
  10524. (error "`org-agenda-files' cannot be a single directory"))
  10525. (when (stringp org-agenda-files)
  10526. (with-temp-buffer
  10527. (insert-file-contents org-agenda-files)
  10528. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10529. ;;;###autoload
  10530. (defun org-cycle-agenda-files ()
  10531. "Cycle through the files in `org-agenda-files'.
  10532. If the current buffer visits an agenda file, find the next one in the list.
  10533. If the current buffer does not, find the first agenda file."
  10534. (interactive)
  10535. (let* ((fs (org-agenda-files t))
  10536. (files (append fs (list (car fs))))
  10537. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10538. file)
  10539. (unless files (error "No agenda files"))
  10540. (catch 'exit
  10541. (while (setq file (pop files))
  10542. (if (equal (file-truename file) tcf)
  10543. (when (car files)
  10544. (find-file (car files))
  10545. (throw 'exit t))))
  10546. (find-file (car fs)))
  10547. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10548. (defun org-agenda-file-to-front (&optional to-end)
  10549. "Move/add the current file to the top of the agenda file list.
  10550. If the file is not present in the list, it is added to the front. If it is
  10551. present, it is moved there. With optional argument TO-END, add/move to the
  10552. end of the list."
  10553. (interactive "P")
  10554. (let ((org-agenda-skip-unavailable-files nil)
  10555. (file-alist (mapcar (lambda (x)
  10556. (cons (file-truename x) x))
  10557. (org-agenda-files t)))
  10558. (ctf (file-truename buffer-file-name))
  10559. x had)
  10560. (setq x (assoc ctf file-alist) had x)
  10561. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10562. (if to-end
  10563. (setq file-alist (append (delq x file-alist) (list x)))
  10564. (setq file-alist (cons x (delq x file-alist))))
  10565. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10566. (org-install-agenda-files-menu)
  10567. (message "File %s to %s of agenda file list"
  10568. (if had "moved" "added") (if to-end "end" "front"))))
  10569. (defun org-remove-file (&optional file)
  10570. "Remove current file from the list of files in variable `org-agenda-files'.
  10571. These are the files which are being checked for agenda entries.
  10572. Optional argument FILE means, use this file instead of the current."
  10573. (interactive)
  10574. (let* ((org-agenda-skip-unavailable-files nil)
  10575. (file (or file buffer-file-name))
  10576. (true-file (file-truename file))
  10577. (afile (abbreviate-file-name file))
  10578. (files (delq nil (mapcar
  10579. (lambda (x)
  10580. (if (equal true-file
  10581. (file-truename x))
  10582. nil x))
  10583. (org-agenda-files t)))))
  10584. (if (not (= (length files) (length (org-agenda-files t))))
  10585. (progn
  10586. (org-store-new-agenda-file-list files)
  10587. (org-install-agenda-files-menu)
  10588. (message "Removed file: %s" afile))
  10589. (message "File was not in list: %s (not removed)" afile))))
  10590. (defun org-file-menu-entry (file)
  10591. (vector file (list 'find-file file) t))
  10592. (defun org-check-agenda-file (file)
  10593. "Make sure FILE exists. If not, ask user what to do."
  10594. (when (not (file-exists-p file))
  10595. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10596. (abbreviate-file-name file))
  10597. (let ((r (downcase (read-char-exclusive))))
  10598. (cond
  10599. ((equal r ?r)
  10600. (org-remove-file file)
  10601. (throw 'nextfile t))
  10602. (t (error "Abort"))))))
  10603. (defun org-get-agenda-file-buffer (file)
  10604. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10605. it to the list of buffers which might be released later."
  10606. (let ((buf (org-find-base-buffer-visiting file)))
  10607. (if buf
  10608. buf ; just return it
  10609. ;; Make a new buffer and remember it
  10610. (setq buf (find-file-noselect file))
  10611. (if buf (push buf org-agenda-new-buffers))
  10612. buf)))
  10613. (defun org-release-buffers (blist)
  10614. "Release all buffers in list, asking the user for confirmation when needed.
  10615. When a buffer is unmodified, it is just killed. When modified, it is saved
  10616. \(if the user agrees) and then killed."
  10617. (let (buf file)
  10618. (while (setq buf (pop blist))
  10619. (setq file (buffer-file-name buf))
  10620. (when (and (buffer-modified-p buf)
  10621. file
  10622. (y-or-n-p (format "Save file %s? " file)))
  10623. (with-current-buffer buf (save-buffer)))
  10624. (kill-buffer buf))))
  10625. (defun org-prepare-agenda-buffers (files)
  10626. "Create buffers for all agenda files, protect archived trees and comments."
  10627. (interactive)
  10628. (let ((pa '(:org-archived t))
  10629. (pc '(:org-comment t))
  10630. (pall '(:org-archived t :org-comment t))
  10631. (inhibit-read-only t)
  10632. (rea (concat ":" org-archive-tag ":"))
  10633. bmp file re)
  10634. (save-excursion
  10635. (save-restriction
  10636. (while (setq file (pop files))
  10637. (if (bufferp file)
  10638. (set-buffer file)
  10639. (org-check-agenda-file file)
  10640. (set-buffer (org-get-agenda-file-buffer file)))
  10641. (widen)
  10642. (setq bmp (buffer-modified-p))
  10643. (org-refresh-category-properties)
  10644. (setq org-todo-keywords-for-agenda
  10645. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10646. (setq org-done-keywords-for-agenda
  10647. (append org-done-keywords-for-agenda org-done-keywords))
  10648. (setq org-todo-keyword-alist-for-agenda
  10649. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10650. (setq org-tag-alist-for-agenda
  10651. (append org-tag-alist-for-agenda org-tag-alist))
  10652. (save-excursion
  10653. (remove-text-properties (point-min) (point-max) pall)
  10654. (when org-agenda-skip-archived-trees
  10655. (goto-char (point-min))
  10656. (while (re-search-forward rea nil t)
  10657. (if (org-on-heading-p t)
  10658. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10659. (goto-char (point-min))
  10660. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10661. (while (re-search-forward re nil t)
  10662. (add-text-properties
  10663. (match-beginning 0) (org-end-of-subtree t) pc)))
  10664. (set-buffer-modified-p bmp))))
  10665. (setq org-todo-keyword-alist-for-agenda
  10666. (org-uniquify org-todo-keyword-alist-for-agenda)
  10667. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10668. ;;;; Embedded LaTeX
  10669. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10670. "Keymap for the minor `org-cdlatex-mode'.")
  10671. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10672. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10673. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10674. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10675. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10676. (defvar org-cdlatex-texmathp-advice-is-done nil
  10677. "Flag remembering if we have applied the advice to texmathp already.")
  10678. (define-minor-mode org-cdlatex-mode
  10679. "Toggle the minor `org-cdlatex-mode'.
  10680. This mode supports entering LaTeX environment and math in LaTeX fragments
  10681. in Org-mode.
  10682. \\{org-cdlatex-mode-map}"
  10683. nil " OCDL" nil
  10684. (when org-cdlatex-mode (require 'cdlatex))
  10685. (unless org-cdlatex-texmathp-advice-is-done
  10686. (setq org-cdlatex-texmathp-advice-is-done t)
  10687. (defadvice texmathp (around org-math-always-on activate)
  10688. "Always return t in org-mode buffers.
  10689. This is because we want to insert math symbols without dollars even outside
  10690. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10691. en embedded LaTeX fragement, let texmathp do its job.
  10692. \\[org-cdlatex-mode-map]"
  10693. (interactive)
  10694. (let (p)
  10695. (cond
  10696. ((not (org-mode-p)) ad-do-it)
  10697. ((eq this-command 'cdlatex-math-symbol)
  10698. (setq ad-return-value t
  10699. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10700. (t
  10701. (let ((p (org-inside-LaTeX-fragment-p)))
  10702. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10703. (setq ad-return-value t
  10704. texmathp-why '("Org-mode embedded math" . 0))
  10705. (if p ad-do-it)))))))))
  10706. (defun turn-on-org-cdlatex ()
  10707. "Unconditionally turn on `org-cdlatex-mode'."
  10708. (org-cdlatex-mode 1))
  10709. (defun org-inside-LaTeX-fragment-p ()
  10710. "Test if point is inside a LaTeX fragment.
  10711. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10712. sequence appearing also before point.
  10713. Even though the matchers for math are configurable, this function assumes
  10714. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10715. delimiters are skipped when they have been removed by customization.
  10716. The return value is nil, or a cons cell with the delimiter and
  10717. and the position of this delimiter.
  10718. This function does a reasonably good job, but can locally be fooled by
  10719. for example currency specifications. For example it will assume being in
  10720. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10721. fragments that are properly closed, but during editing, we have to live
  10722. with the uncertainty caused by missing closing delimiters. This function
  10723. looks only before point, not after."
  10724. (catch 'exit
  10725. (let ((pos (point))
  10726. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10727. (lim (progn
  10728. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10729. (point)))
  10730. dd-on str (start 0) m re)
  10731. (goto-char pos)
  10732. (when dodollar
  10733. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10734. re (nth 1 (assoc "$" org-latex-regexps)))
  10735. (while (string-match re str start)
  10736. (cond
  10737. ((= (match-end 0) (length str))
  10738. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10739. ((= (match-end 0) (- (length str) 5))
  10740. (throw 'exit nil))
  10741. (t (setq start (match-end 0))))))
  10742. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10743. (goto-char pos)
  10744. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10745. (and (match-beginning 2) (throw 'exit nil))
  10746. ;; count $$
  10747. (while (re-search-backward "\\$\\$" lim t)
  10748. (setq dd-on (not dd-on)))
  10749. (goto-char pos)
  10750. (if dd-on (cons "$$" m))))))
  10751. (defun org-try-cdlatex-tab ()
  10752. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10753. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10754. - inside a LaTeX fragment, or
  10755. - after the first word in a line, where an abbreviation expansion could
  10756. insert a LaTeX environment."
  10757. (when org-cdlatex-mode
  10758. (cond
  10759. ((save-excursion
  10760. (skip-chars-backward "a-zA-Z0-9*")
  10761. (skip-chars-backward " \t")
  10762. (bolp))
  10763. (cdlatex-tab) t)
  10764. ((org-inside-LaTeX-fragment-p)
  10765. (cdlatex-tab) t)
  10766. (t nil))))
  10767. (defun org-cdlatex-underscore-caret (&optional arg)
  10768. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10769. Revert to the normal definition outside of these fragments."
  10770. (interactive "P")
  10771. (if (org-inside-LaTeX-fragment-p)
  10772. (call-interactively 'cdlatex-sub-superscript)
  10773. (let (org-cdlatex-mode)
  10774. (call-interactively (key-binding (vector last-input-event))))))
  10775. (defun org-cdlatex-math-modify (&optional arg)
  10776. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10777. Revert to the normal definition outside of these fragments."
  10778. (interactive "P")
  10779. (if (org-inside-LaTeX-fragment-p)
  10780. (call-interactively 'cdlatex-math-modify)
  10781. (let (org-cdlatex-mode)
  10782. (call-interactively (key-binding (vector last-input-event))))))
  10783. (defvar org-latex-fragment-image-overlays nil
  10784. "List of overlays carrying the images of latex fragments.")
  10785. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10786. (defun org-remove-latex-fragment-image-overlays ()
  10787. "Remove all overlays with LaTeX fragment images in current buffer."
  10788. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10789. (setq org-latex-fragment-image-overlays nil))
  10790. (defun org-preview-latex-fragment (&optional subtree)
  10791. "Preview the LaTeX fragment at point, or all locally or globally.
  10792. If the cursor is in a LaTeX fragment, create the image and overlay
  10793. it over the source code. If there is no fragment at point, display
  10794. all fragments in the current text, from one headline to the next. With
  10795. prefix SUBTREE, display all fragments in the current subtree. With a
  10796. double prefix `C-u C-u', or when the cursor is before the first headline,
  10797. display all fragments in the buffer.
  10798. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10799. (interactive "P")
  10800. (org-remove-latex-fragment-image-overlays)
  10801. (save-excursion
  10802. (save-restriction
  10803. (let (beg end at msg)
  10804. (cond
  10805. ((or (equal subtree '(16))
  10806. (not (save-excursion
  10807. (re-search-backward (concat "^" outline-regexp) nil t))))
  10808. (setq beg (point-min) end (point-max)
  10809. msg "Creating images for buffer...%s"))
  10810. ((equal subtree '(4))
  10811. (org-back-to-heading)
  10812. (setq beg (point) end (org-end-of-subtree t)
  10813. msg "Creating images for subtree...%s"))
  10814. (t
  10815. (if (setq at (org-inside-LaTeX-fragment-p))
  10816. (goto-char (max (point-min) (- (cdr at) 2)))
  10817. (org-back-to-heading))
  10818. (setq beg (point) end (progn (outline-next-heading) (point))
  10819. msg (if at "Creating image...%s"
  10820. "Creating images for entry...%s"))))
  10821. (message msg "")
  10822. (narrow-to-region beg end)
  10823. (goto-char beg)
  10824. (org-format-latex
  10825. (concat "ltxpng/" (file-name-sans-extension
  10826. (file-name-nondirectory
  10827. buffer-file-name)))
  10828. default-directory 'overlays msg at 'forbuffer)
  10829. (message msg "done. Use `C-c C-c' to remove images.")))))
  10830. (defvar org-latex-regexps
  10831. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10832. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10833. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10834. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10835. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10836. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10837. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10838. "Regular expressions for matching embedded LaTeX.")
  10839. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10840. "Replace LaTeX fragments with links to an image, and produce images."
  10841. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10842. (let* ((prefixnodir (file-name-nondirectory prefix))
  10843. (absprefix (expand-file-name prefix dir))
  10844. (todir (file-name-directory absprefix))
  10845. (opt org-format-latex-options)
  10846. (matchers (plist-get opt :matchers))
  10847. (re-list org-latex-regexps)
  10848. (cnt 0) txt link beg end re e checkdir
  10849. m n block linkfile movefile ov)
  10850. ;; Check if there are old images files with this prefix, and remove them
  10851. (when (file-directory-p todir)
  10852. (mapc 'delete-file
  10853. (directory-files
  10854. todir 'full
  10855. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10856. ;; Check the different regular expressions
  10857. (while (setq e (pop re-list))
  10858. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10859. block (if (nth 3 e) "\n\n" ""))
  10860. (when (member m matchers)
  10861. (goto-char (point-min))
  10862. (while (re-search-forward re nil t)
  10863. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10864. (setq txt (match-string n)
  10865. beg (match-beginning n) end (match-end n)
  10866. cnt (1+ cnt)
  10867. linkfile (format "%s_%04d.png" prefix cnt)
  10868. movefile (format "%s_%04d.png" absprefix cnt)
  10869. link (concat block "[[file:" linkfile "]]" block))
  10870. (if msg (message msg cnt))
  10871. (goto-char beg)
  10872. (unless checkdir ; make sure the directory exists
  10873. (setq checkdir t)
  10874. (or (file-directory-p todir) (make-directory todir)))
  10875. (org-create-formula-image
  10876. txt movefile opt forbuffer)
  10877. (if overlays
  10878. (progn
  10879. (setq ov (org-make-overlay beg end))
  10880. (if (featurep 'xemacs)
  10881. (progn
  10882. (org-overlay-put ov 'invisible t)
  10883. (org-overlay-put
  10884. ov 'end-glyph
  10885. (make-glyph (vector 'png :file movefile))))
  10886. (org-overlay-put
  10887. ov 'display
  10888. (list 'image :type 'png :file movefile :ascent 'center)))
  10889. (push ov org-latex-fragment-image-overlays)
  10890. (goto-char end))
  10891. (delete-region beg end)
  10892. (insert link))))))))
  10893. ;; This function borrows from Ganesh Swami's latex2png.el
  10894. (defun org-create-formula-image (string tofile options buffer)
  10895. (let* ((tmpdir (if (featurep 'xemacs)
  10896. (temp-directory)
  10897. temporary-file-directory))
  10898. (texfilebase (make-temp-name
  10899. (expand-file-name "orgtex" tmpdir)))
  10900. (texfile (concat texfilebase ".tex"))
  10901. (dvifile (concat texfilebase ".dvi"))
  10902. (pngfile (concat texfilebase ".png"))
  10903. (fnh (if (featurep 'xemacs)
  10904. (font-height (get-face-font 'default))
  10905. (face-attribute 'default :height nil)))
  10906. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10907. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10908. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10909. "Black"))
  10910. (bg (or (plist-get options (if buffer :background :html-background))
  10911. "Transparent")))
  10912. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10913. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10914. (with-temp-file texfile
  10915. (insert org-format-latex-header
  10916. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10917. (let ((dir default-directory))
  10918. (condition-case nil
  10919. (progn
  10920. (cd tmpdir)
  10921. (call-process "latex" nil nil nil texfile))
  10922. (error nil))
  10923. (cd dir))
  10924. (if (not (file-exists-p dvifile))
  10925. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10926. (condition-case nil
  10927. (call-process "dvipng" nil nil nil
  10928. "-E" "-fg" fg "-bg" bg
  10929. "-D" dpi
  10930. ;;"-x" scale "-y" scale
  10931. "-T" "tight"
  10932. "-o" pngfile
  10933. dvifile)
  10934. (error nil))
  10935. (if (not (file-exists-p pngfile))
  10936. (progn (message "Failed to create png file from %s" texfile) nil)
  10937. ;; Use the requested file name and clean up
  10938. (copy-file pngfile tofile 'replace)
  10939. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10940. (delete-file (concat texfilebase e)))
  10941. pngfile))))
  10942. (defun org-dvipng-color (attr)
  10943. "Return an rgb color specification for dvipng."
  10944. (apply 'format "rgb %s %s %s"
  10945. (mapcar 'org-normalize-color
  10946. (color-values (face-attribute 'default attr nil)))))
  10947. (defun org-normalize-color (value)
  10948. "Return string to be used as color value for an RGB component."
  10949. (format "%g" (/ value 65535.0)))
  10950. ;;;; Key bindings
  10951. ;; Make `C-c C-x' a prefix key
  10952. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10953. ;; TAB key with modifiers
  10954. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10955. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10956. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10957. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10958. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10959. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10960. ;; The following line is necessary under Suse GNU/Linux
  10961. (unless (featurep 'xemacs)
  10962. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10963. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10964. (define-key org-mode-map [backtab] 'org-shifttab)
  10965. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10966. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10967. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10968. ;; Cursor keys with modifiers
  10969. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10970. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10971. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10972. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10973. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10974. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10975. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10976. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10977. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10978. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10979. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10980. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10981. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10982. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10983. ;;; Extra keys for tty access.
  10984. ;; We only set them when really needed because otherwise the
  10985. ;; menus don't show the simple keys
  10986. (when (or org-use-extra-keys
  10987. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10988. (not window-system))
  10989. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10990. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10991. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10992. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10993. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10994. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10995. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10996. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10997. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10998. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10999. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11000. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11001. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11002. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11003. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11004. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11005. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11006. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11007. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11008. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11009. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11010. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11011. ;; All the other keys
  11012. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11013. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11014. (if (boundp 'narrow-map)
  11015. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11016. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11017. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11018. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11019. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11020. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11021. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11022. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11023. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11024. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11025. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11026. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11027. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11028. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11029. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11030. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11031. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11032. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11033. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11034. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11035. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11036. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11037. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11038. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11039. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11040. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11041. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11042. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11043. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11044. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11045. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11046. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11047. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11048. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11049. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11050. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11051. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11052. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11053. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11054. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11055. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11056. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11057. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11058. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11059. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11060. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11061. (org-defkey org-mode-map "\C-m" 'org-return)
  11062. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11063. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11064. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11065. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11066. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11067. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11068. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11069. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11070. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11071. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11072. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11073. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11074. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11075. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11076. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11077. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11078. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11079. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11080. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11081. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11082. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11083. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11084. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11085. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11086. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11087. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11088. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11089. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11090. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11091. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11092. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11093. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11094. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11095. (when (featurep 'xemacs)
  11096. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11097. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11098. (defun org-self-insert-command (N)
  11099. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11100. If the cursor is in a table looking at whitespace, the whitespace is
  11101. overwritten, and the table is not marked as requiring realignment."
  11102. (interactive "p")
  11103. (if (and (org-table-p)
  11104. (progn
  11105. ;; check if we blank the field, and if that triggers align
  11106. (and (featurep 'org-table) org-table-auto-blank-field
  11107. (member last-command
  11108. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11109. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11110. ;; got extra space, this field does not determine column width
  11111. (let (org-table-may-need-update) (org-table-blank-field))
  11112. ;; no extra space, this field may determine column width
  11113. (org-table-blank-field)))
  11114. t)
  11115. (eq N 1)
  11116. (looking-at "[^|\n]* |"))
  11117. (let (org-table-may-need-update)
  11118. (goto-char (1- (match-end 0)))
  11119. (delete-backward-char 1)
  11120. (goto-char (match-beginning 0))
  11121. (self-insert-command N))
  11122. (setq org-table-may-need-update t)
  11123. (self-insert-command N)
  11124. (org-fix-tags-on-the-fly)))
  11125. (defun org-fix-tags-on-the-fly ()
  11126. (when (and (equal (char-after (point-at-bol)) ?*)
  11127. (org-on-heading-p))
  11128. (org-align-tags-here org-tags-column)))
  11129. (defun org-delete-backward-char (N)
  11130. "Like `delete-backward-char', insert whitespace at field end in tables.
  11131. When deleting backwards, in tables this function will insert whitespace in
  11132. front of the next \"|\" separator, to keep the table aligned. The table will
  11133. still be marked for re-alignment if the field did fill the entire column,
  11134. because, in this case the deletion might narrow the column."
  11135. (interactive "p")
  11136. (if (and (org-table-p)
  11137. (eq N 1)
  11138. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11139. (looking-at ".*?|"))
  11140. (let ((pos (point))
  11141. (noalign (looking-at "[^|\n\r]* |"))
  11142. (c org-table-may-need-update))
  11143. (backward-delete-char N)
  11144. (skip-chars-forward "^|")
  11145. (insert " ")
  11146. (goto-char (1- pos))
  11147. ;; noalign: if there were two spaces at the end, this field
  11148. ;; does not determine the width of the column.
  11149. (if noalign (setq org-table-may-need-update c)))
  11150. (backward-delete-char N)
  11151. (org-fix-tags-on-the-fly)))
  11152. (defun org-delete-char (N)
  11153. "Like `delete-char', but insert whitespace at field end in tables.
  11154. When deleting characters, in tables this function will insert whitespace in
  11155. front of the next \"|\" separator, to keep the table aligned. The table will
  11156. still be marked for re-alignment if the field did fill the entire column,
  11157. because, in this case the deletion might narrow the column."
  11158. (interactive "p")
  11159. (if (and (org-table-p)
  11160. (not (bolp))
  11161. (not (= (char-after) ?|))
  11162. (eq N 1))
  11163. (if (looking-at ".*?|")
  11164. (let ((pos (point))
  11165. (noalign (looking-at "[^|\n\r]* |"))
  11166. (c org-table-may-need-update))
  11167. (replace-match (concat
  11168. (substring (match-string 0) 1 -1)
  11169. " |"))
  11170. (goto-char pos)
  11171. ;; noalign: if there were two spaces at the end, this field
  11172. ;; does not determine the width of the column.
  11173. (if noalign (setq org-table-may-need-update c)))
  11174. (delete-char N))
  11175. (delete-char N)
  11176. (org-fix-tags-on-the-fly)))
  11177. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11178. (put 'org-self-insert-command 'delete-selection t)
  11179. (put 'orgtbl-self-insert-command 'delete-selection t)
  11180. (put 'org-delete-char 'delete-selection 'supersede)
  11181. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11182. ;; Make `flyspell-mode' delay after some commands
  11183. (put 'org-self-insert-command 'flyspell-delayed t)
  11184. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11185. (put 'org-delete-char 'flyspell-delayed t)
  11186. (put 'org-delete-backward-char 'flyspell-delayed t)
  11187. ;; Make pabbrev-mode expand after org-mode commands
  11188. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11189. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11190. ;; How to do this: Measure non-white length of current string
  11191. ;; If equal to column width, we should realign.
  11192. (defun org-remap (map &rest commands)
  11193. "In MAP, remap the functions given in COMMANDS.
  11194. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11195. (let (new old)
  11196. (while commands
  11197. (setq old (pop commands) new (pop commands))
  11198. (if (fboundp 'command-remapping)
  11199. (org-defkey map (vector 'remap old) new)
  11200. (substitute-key-definition old new map global-map)))))
  11201. (when (eq org-enable-table-editor 'optimized)
  11202. ;; If the user wants maximum table support, we need to hijack
  11203. ;; some standard editing functions
  11204. (org-remap org-mode-map
  11205. 'self-insert-command 'org-self-insert-command
  11206. 'delete-char 'org-delete-char
  11207. 'delete-backward-char 'org-delete-backward-char)
  11208. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11209. (defun org-shiftcursor-error ()
  11210. "Throw an error because Shift-Cursor command was applied in wrong context."
  11211. (error "This command is active in special context like tables, headlines or timestamps"))
  11212. (defun org-shifttab (&optional arg)
  11213. "Global visibility cycling or move to previous table field.
  11214. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11215. on context.
  11216. See the individual commands for more information."
  11217. (interactive "P")
  11218. (cond
  11219. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11220. ((integerp arg)
  11221. (message "Content view to level: %d" arg)
  11222. (org-content (prefix-numeric-value arg))
  11223. (setq org-cycle-global-status 'overview))
  11224. (t (call-interactively 'org-global-cycle))))
  11225. (defun org-shiftmetaleft ()
  11226. "Promote subtree or delete table column.
  11227. Calls `org-promote-subtree', `org-outdent-item',
  11228. or `org-table-delete-column', depending on context.
  11229. See the individual commands for more information."
  11230. (interactive)
  11231. (cond
  11232. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11233. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11234. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11235. (t (org-shiftcursor-error))))
  11236. (defun org-shiftmetaright ()
  11237. "Demote subtree or insert table column.
  11238. Calls `org-demote-subtree', `org-indent-item',
  11239. or `org-table-insert-column', depending on context.
  11240. See the individual commands for more information."
  11241. (interactive)
  11242. (cond
  11243. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11244. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11245. ((org-at-item-p) (call-interactively 'org-indent-item))
  11246. (t (org-shiftcursor-error))))
  11247. (defun org-shiftmetaup (&optional arg)
  11248. "Move subtree up or kill table row.
  11249. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11250. `org-move-item-up' depending on context. See the individual commands
  11251. for more information."
  11252. (interactive "P")
  11253. (cond
  11254. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11255. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11256. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11257. (t (org-shiftcursor-error))))
  11258. (defun org-shiftmetadown (&optional arg)
  11259. "Move subtree down or insert table row.
  11260. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11261. `org-move-item-down', depending on context. See the individual
  11262. commands for more information."
  11263. (interactive "P")
  11264. (cond
  11265. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11266. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11267. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11268. (t (org-shiftcursor-error))))
  11269. (defun org-metaleft (&optional arg)
  11270. "Promote heading or move table column to left.
  11271. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11272. With no specific context, calls the Emacs default `backward-word'.
  11273. See the individual commands for more information."
  11274. (interactive "P")
  11275. (cond
  11276. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11277. ((or (org-on-heading-p) (org-region-active-p))
  11278. (call-interactively 'org-do-promote))
  11279. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11280. (t (call-interactively 'backward-word))))
  11281. (defun org-metaright (&optional arg)
  11282. "Demote subtree or move table column to right.
  11283. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11284. With no specific context, calls the Emacs default `forward-word'.
  11285. See the individual commands for more information."
  11286. (interactive "P")
  11287. (cond
  11288. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11289. ((or (org-on-heading-p) (org-region-active-p))
  11290. (call-interactively 'org-do-demote))
  11291. ((org-at-item-p) (call-interactively 'org-indent-item))
  11292. (t (call-interactively 'forward-word))))
  11293. (defun org-metaup (&optional arg)
  11294. "Move subtree up or move table row up.
  11295. Calls `org-move-subtree-up' or `org-table-move-row' or
  11296. `org-move-item-up', depending on context. See the individual commands
  11297. for more information."
  11298. (interactive "P")
  11299. (cond
  11300. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11301. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11302. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11303. (t (transpose-lines 1) (beginning-of-line -1))))
  11304. (defun org-metadown (&optional arg)
  11305. "Move subtree down or move table row down.
  11306. Calls `org-move-subtree-down' or `org-table-move-row' or
  11307. `org-move-item-down', depending on context. See the individual
  11308. commands for more information."
  11309. (interactive "P")
  11310. (cond
  11311. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11312. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11313. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11314. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11315. (defun org-shiftup (&optional arg)
  11316. "Increase item in timestamp or increase priority of current headline.
  11317. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11318. depending on context. See the individual commands for more information."
  11319. (interactive "P")
  11320. (cond
  11321. ((org-at-timestamp-p t)
  11322. (call-interactively (if org-edit-timestamp-down-means-later
  11323. 'org-timestamp-down 'org-timestamp-up)))
  11324. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11325. ((org-at-item-p) (call-interactively 'org-previous-item))
  11326. ((org-clocktable-try-shift 'up arg))
  11327. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11328. (defun org-shiftdown (&optional arg)
  11329. "Decrease item in timestamp or decrease priority of current headline.
  11330. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11331. depending on context. See the individual commands for more information."
  11332. (interactive "P")
  11333. (cond
  11334. ((org-at-timestamp-p t)
  11335. (call-interactively (if org-edit-timestamp-down-means-later
  11336. 'org-timestamp-up 'org-timestamp-down)))
  11337. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11338. ((org-clocktable-try-shift 'down arg))
  11339. (t (call-interactively 'org-next-item))))
  11340. (defun org-shiftright (&optional arg)
  11341. "Next TODO keyword or timestamp one day later, depending on context."
  11342. (interactive "P")
  11343. (cond
  11344. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11345. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11346. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11347. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11348. ((org-clocktable-try-shift 'right arg))
  11349. (t (org-shiftcursor-error))))
  11350. (defun org-shiftleft (&optional arg)
  11351. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11352. (interactive "P")
  11353. (cond
  11354. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11355. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11356. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11357. ((org-at-property-p)
  11358. (call-interactively 'org-property-previous-allowed-value))
  11359. ((org-clocktable-try-shift 'left arg))
  11360. (t (org-shiftcursor-error))))
  11361. (defun org-shiftcontrolright ()
  11362. "Switch to next TODO set."
  11363. (interactive)
  11364. (cond
  11365. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11366. (t (org-shiftcursor-error))))
  11367. (defun org-shiftcontrolleft ()
  11368. "Switch to previous TODO set."
  11369. (interactive)
  11370. (cond
  11371. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11372. (t (org-shiftcursor-error))))
  11373. (defun org-ctrl-c-ret ()
  11374. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11375. (interactive)
  11376. (cond
  11377. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11378. (t (call-interactively 'org-insert-heading))))
  11379. (defun org-copy-special ()
  11380. "Copy region in table or copy current subtree.
  11381. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11382. See the individual commands for more information."
  11383. (interactive)
  11384. (call-interactively
  11385. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11386. (defun org-cut-special ()
  11387. "Cut region in table or cut current subtree.
  11388. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11389. See the individual commands for more information."
  11390. (interactive)
  11391. (call-interactively
  11392. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11393. (defun org-paste-special (arg)
  11394. "Paste rectangular region into table, or past subtree relative to level.
  11395. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11396. See the individual commands for more information."
  11397. (interactive "P")
  11398. (if (org-at-table-p)
  11399. (org-table-paste-rectangle)
  11400. (org-paste-subtree arg)))
  11401. (defun org-edit-special ()
  11402. "Call a special editor for the stuff at point.
  11403. When at a table, call the formula editor with `org-table-edit-formulas'.
  11404. When at the first line of an src example, call `org-edit-src-code'.
  11405. When in an #+include line, visit the include file. Otherwise call
  11406. `ffap' to visit the file at point."
  11407. (interactive)
  11408. (cond
  11409. ((org-at-table-p)
  11410. (call-interactively 'org-table-edit-formulas))
  11411. ((save-excursion
  11412. (beginning-of-line 1)
  11413. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11414. (find-file (org-trim (match-string 1))))
  11415. ((org-edit-src-code))
  11416. ((org-edit-fixed-width-region))
  11417. (t (call-interactively 'ffap))))
  11418. (defun org-ctrl-c-ctrl-c (&optional arg)
  11419. "Set tags in headline, or update according to changed information at point.
  11420. This command does many different things, depending on context:
  11421. - If the cursor is in a headline, prompt for tags and insert them
  11422. into the current line, aligned to `org-tags-column'. When called
  11423. with prefix arg, realign all tags in the current buffer.
  11424. - If the cursor is in one of the special #+KEYWORD lines, this
  11425. triggers scanning the buffer for these lines and updating the
  11426. information.
  11427. - If the cursor is inside a table, realign the table. This command
  11428. works even if the automatic table editor has been turned off.
  11429. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11430. the entire table.
  11431. - If the cursor is a the beginning of a dynamic block, update it.
  11432. - If the cursor is inside a table created by the table.el package,
  11433. activate that table.
  11434. - If the current buffer is a remember buffer, close note and file it.
  11435. with a prefix argument, file it without further interaction to the default
  11436. location.
  11437. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11438. links in this buffer.
  11439. - If the cursor is on a numbered item in a plain list, renumber the
  11440. ordered list.
  11441. - If the cursor is on a checkbox, toggle it."
  11442. (interactive "P")
  11443. (let ((org-enable-table-editor t))
  11444. (cond
  11445. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11446. org-occur-highlights
  11447. org-latex-fragment-image-overlays)
  11448. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11449. (org-remove-occur-highlights)
  11450. (org-remove-latex-fragment-image-overlays)
  11451. (message "Temporary highlights/overlays removed from current buffer"))
  11452. ((and (local-variable-p 'org-finish-function (current-buffer))
  11453. (fboundp org-finish-function))
  11454. (funcall org-finish-function))
  11455. ((org-at-property-p)
  11456. (call-interactively 'org-property-action))
  11457. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11458. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11459. ((org-at-table.el-p)
  11460. (require 'table)
  11461. (beginning-of-line 1)
  11462. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11463. (call-interactively 'table-recognize-table))
  11464. ((org-at-table-p)
  11465. (org-table-maybe-eval-formula)
  11466. (if arg
  11467. (call-interactively 'org-table-recalculate)
  11468. (org-table-maybe-recalculate-line))
  11469. (call-interactively 'org-table-align))
  11470. ((org-at-item-checkbox-p)
  11471. (call-interactively 'org-toggle-checkbox))
  11472. ((org-at-item-p)
  11473. (call-interactively 'org-maybe-renumber-ordered-list))
  11474. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11475. ;; Dynamic block
  11476. (beginning-of-line 1)
  11477. (save-excursion (org-update-dblock)))
  11478. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11479. (cond
  11480. ((equal (match-string 1) "TBLFM")
  11481. ;; Recalculate the table before this line
  11482. (save-excursion
  11483. (beginning-of-line 1)
  11484. (skip-chars-backward " \r\n\t")
  11485. (if (org-at-table-p)
  11486. (org-call-with-arg 'org-table-recalculate t))))
  11487. (t
  11488. ; (org-set-regexps-and-options)
  11489. ; (org-restart-font-lock)
  11490. (let ((org-inhibit-startup t)) (org-mode-restart))
  11491. (message "Local setup has been refreshed"))))
  11492. (t (error "C-c C-c can do nothing useful at this location.")))))
  11493. (defun org-mode-restart ()
  11494. "Restart Org-mode, to scan again for special lines.
  11495. Also updates the keyword regular expressions."
  11496. (interactive)
  11497. (org-mode)
  11498. (message "Org-mode restarted"))
  11499. (defun org-kill-note-or-show-branches ()
  11500. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11501. (interactive)
  11502. (if (not org-finish-function)
  11503. (call-interactively 'show-branches)
  11504. (let ((org-note-abort t))
  11505. (funcall org-finish-function))))
  11506. (defun org-return (&optional indent)
  11507. "Goto next table row or insert a newline.
  11508. Calls `org-table-next-row' or `newline', depending on context.
  11509. See the individual commands for more information."
  11510. (interactive)
  11511. (cond
  11512. ((bobp) (if indent (newline-and-indent) (newline)))
  11513. ((and (org-at-heading-p)
  11514. (looking-at
  11515. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11516. (org-show-entry)
  11517. (end-of-line 1)
  11518. (newline))
  11519. ((org-at-table-p)
  11520. (org-table-justify-field-maybe)
  11521. (call-interactively 'org-table-next-row))
  11522. (t (if indent (newline-and-indent) (newline)))))
  11523. (defun org-return-indent ()
  11524. "Goto next table row or insert a newline and indent.
  11525. Calls `org-table-next-row' or `newline-and-indent', depending on
  11526. context. See the individual commands for more information."
  11527. (interactive)
  11528. (org-return t))
  11529. (defun org-ctrl-c-star ()
  11530. "Compute table, or change heading status of lines.
  11531. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11532. depending on context. This will also turn a plain list item or a normal
  11533. line into a subheading."
  11534. (interactive)
  11535. (cond
  11536. ((org-at-table-p)
  11537. (call-interactively 'org-table-recalculate))
  11538. ((org-region-active-p)
  11539. ;; Convert all lines in region to list items
  11540. (call-interactively 'org-toggle-region-headings))
  11541. ((org-on-heading-p)
  11542. (org-toggle-region-headings (point-at-bol)
  11543. (min (1+ (point-at-eol)) (point-max))))
  11544. ((org-at-item-p)
  11545. ;; Convert to heading
  11546. (let ((level (save-match-data
  11547. (save-excursion
  11548. (condition-case nil
  11549. (progn
  11550. (org-back-to-heading t)
  11551. (funcall outline-level))
  11552. (error 0))))))
  11553. (replace-match
  11554. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11555. (t (org-toggle-region-headings (point-at-bol)
  11556. (min (1+ (point-at-eol)) (point-max))))))
  11557. (defun org-ctrl-c-minus ()
  11558. "Insert separator line in table or modify bullet status of line.
  11559. Also turns a plain line or a region of lines into list items.
  11560. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11561. `org-cycle-list-bullet', depending on context."
  11562. (interactive)
  11563. (cond
  11564. ((org-at-table-p)
  11565. (call-interactively 'org-table-insert-hline))
  11566. ((org-on-heading-p)
  11567. ;; Convert to item
  11568. (save-excursion
  11569. (beginning-of-line 1)
  11570. (if (looking-at "\\*+ ")
  11571. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11572. ((org-region-active-p)
  11573. ;; Convert all lines in region to list items
  11574. (call-interactively 'org-toggle-region-items))
  11575. ((org-in-item-p)
  11576. (call-interactively 'org-cycle-list-bullet))
  11577. (t (org-toggle-region-items (point-at-bol)
  11578. (min (1+ (point-at-eol)) (point-max))))))
  11579. (defun org-toggle-region-items (beg end)
  11580. "Convert all lines in region to list items.
  11581. If the first line is already an item, convert all list items in the region
  11582. to normal lines."
  11583. (interactive "r")
  11584. (let (l2 l)
  11585. (save-excursion
  11586. (goto-char end)
  11587. (setq l2 (org-current-line))
  11588. (goto-char beg)
  11589. (beginning-of-line 1)
  11590. (setq l (1- (org-current-line)))
  11591. (if (org-at-item-p)
  11592. ;; We already have items, de-itemize
  11593. (while (< (setq l (1+ l)) l2)
  11594. (when (org-at-item-p)
  11595. (goto-char (match-beginning 2))
  11596. (delete-region (match-beginning 2) (match-end 2))
  11597. (and (looking-at "[ \t]+") (replace-match "")))
  11598. (beginning-of-line 2))
  11599. (while (< (setq l (1+ l)) l2)
  11600. (unless (org-at-item-p)
  11601. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11602. (replace-match "\\1- \\2")))
  11603. (beginning-of-line 2))))))
  11604. (defun org-toggle-region-headings (beg end)
  11605. "Convert all lines in region to list items.
  11606. If the first line is already an item, convert all list items in the region
  11607. to normal lines."
  11608. (interactive "r")
  11609. (let (l2 l)
  11610. (save-excursion
  11611. (goto-char end)
  11612. (setq l2 (org-current-line))
  11613. (goto-char beg)
  11614. (beginning-of-line 1)
  11615. (setq l (1- (org-current-line)))
  11616. (if (org-on-heading-p)
  11617. ;; We already have headlines, de-star them
  11618. (while (< (setq l (1+ l)) l2)
  11619. (when (org-on-heading-p t)
  11620. (and (looking-at outline-regexp) (replace-match "")))
  11621. (beginning-of-line 2))
  11622. (let* ((stars (save-excursion
  11623. (re-search-backward org-complex-heading-regexp nil t)
  11624. (or (match-string 1) "*")))
  11625. (add-stars (if org-odd-levels-only "**" "*"))
  11626. (rpl (concat stars add-stars " \\2")))
  11627. (while (< (setq l (1+ l)) l2)
  11628. (unless (org-on-heading-p)
  11629. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11630. (replace-match rpl)))
  11631. (beginning-of-line 2)))))))
  11632. (defun org-meta-return (&optional arg)
  11633. "Insert a new heading or wrap a region in a table.
  11634. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11635. See the individual commands for more information."
  11636. (interactive "P")
  11637. (cond
  11638. ((org-at-table-p)
  11639. (call-interactively 'org-table-wrap-region))
  11640. (t (call-interactively 'org-insert-heading))))
  11641. ;;; Menu entries
  11642. ;; Define the Org-mode menus
  11643. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11644. '("Tbl"
  11645. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11646. ["Next Field" org-cycle (org-at-table-p)]
  11647. ["Previous Field" org-shifttab (org-at-table-p)]
  11648. ["Next Row" org-return (org-at-table-p)]
  11649. "--"
  11650. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11651. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11652. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11653. "--"
  11654. ("Column"
  11655. ["Move Column Left" org-metaleft (org-at-table-p)]
  11656. ["Move Column Right" org-metaright (org-at-table-p)]
  11657. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11658. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11659. ("Row"
  11660. ["Move Row Up" org-metaup (org-at-table-p)]
  11661. ["Move Row Down" org-metadown (org-at-table-p)]
  11662. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11663. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11664. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11665. "--"
  11666. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11667. ("Rectangle"
  11668. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11669. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11670. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11671. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11672. "--"
  11673. ("Calculate"
  11674. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11675. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11676. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11677. "--"
  11678. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11679. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11680. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11681. "--"
  11682. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11683. "--"
  11684. ["Sum Column/Rectangle" org-table-sum
  11685. (or (org-at-table-p) (org-region-active-p))]
  11686. ["Which Column?" org-table-current-column (org-at-table-p)])
  11687. ["Debug Formulas"
  11688. org-table-toggle-formula-debugger
  11689. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11690. ["Show Col/Row Numbers"
  11691. org-table-toggle-coordinate-overlays
  11692. :style toggle
  11693. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11694. "--"
  11695. ["Create" org-table-create (and (not (org-at-table-p))
  11696. org-enable-table-editor)]
  11697. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11698. ["Import from File" org-table-import (not (org-at-table-p))]
  11699. ["Export to File" org-table-export (org-at-table-p)]
  11700. "--"
  11701. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11702. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11703. '("Org"
  11704. ("Show/Hide"
  11705. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11706. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11707. ["Sparse Tree..." org-sparse-tree t]
  11708. ["Reveal Context" org-reveal t]
  11709. ["Show All" show-all t]
  11710. "--"
  11711. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11712. "--"
  11713. ["New Heading" org-insert-heading t]
  11714. ("Navigate Headings"
  11715. ["Up" outline-up-heading t]
  11716. ["Next" outline-next-visible-heading t]
  11717. ["Previous" outline-previous-visible-heading t]
  11718. ["Next Same Level" outline-forward-same-level t]
  11719. ["Previous Same Level" outline-backward-same-level t]
  11720. "--"
  11721. ["Jump" org-goto t])
  11722. ("Edit Structure"
  11723. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11724. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11725. "--"
  11726. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11727. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11728. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11729. "--"
  11730. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11731. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11732. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11733. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11734. "--"
  11735. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11736. "--"
  11737. ["Convert to odd levels" org-convert-to-odd-levels t]
  11738. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11739. ("Editing"
  11740. ["Emphasis..." org-emphasize t]
  11741. ["Edit Source Example" org-edit-special t])
  11742. ("Archive"
  11743. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11744. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11745. ; :active t :keys "C-u C-c C-x C-a"]
  11746. ["Sparse trees open ARCHIVE trees"
  11747. (setq org-sparse-tree-open-archived-trees
  11748. (not org-sparse-tree-open-archived-trees))
  11749. :style toggle :selected org-sparse-tree-open-archived-trees]
  11750. ["Cycling opens ARCHIVE trees"
  11751. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11752. :style toggle :selected org-cycle-open-archived-trees]
  11753. "--"
  11754. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  11755. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11756. ; ["Check and Move Children" (org-archive-subtree '(4))
  11757. ; :active t :keys "C-u C-c C-x C-s"]
  11758. )
  11759. "--"
  11760. ("TODO Lists"
  11761. ["TODO/DONE/-" org-todo t]
  11762. ("Select keyword"
  11763. ["Next keyword" org-shiftright (org-on-heading-p)]
  11764. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11765. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11766. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11767. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11768. ["Show TODO Tree" org-show-todo-tree t]
  11769. ["Global TODO list" org-todo-list t]
  11770. "--"
  11771. ["Set Priority" org-priority t]
  11772. ["Priority Up" org-shiftup t]
  11773. ["Priority Down" org-shiftdown t])
  11774. ("TAGS and Properties"
  11775. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11776. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11777. "--"
  11778. ["Set property" 'org-set-property t]
  11779. ["Column view of properties" org-columns t]
  11780. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11781. ("Dates and Scheduling"
  11782. ["Timestamp" org-time-stamp t]
  11783. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11784. ("Change Date"
  11785. ["1 Day Later" org-shiftright t]
  11786. ["1 Day Earlier" org-shiftleft t]
  11787. ["1 ... Later" org-shiftup t]
  11788. ["1 ... Earlier" org-shiftdown t])
  11789. ["Compute Time Range" org-evaluate-time-range t]
  11790. ["Schedule Item" org-schedule t]
  11791. ["Deadline" org-deadline t]
  11792. "--"
  11793. ["Custom time format" org-toggle-time-stamp-overlays
  11794. :style radio :selected org-display-custom-times]
  11795. "--"
  11796. ["Goto Calendar" org-goto-calendar t]
  11797. ["Date from Calendar" org-date-from-calendar t])
  11798. ("Logging work"
  11799. ["Clock in" org-clock-in t]
  11800. ["Clock out" org-clock-out t]
  11801. ["Clock cancel" org-clock-cancel t]
  11802. ["Goto running clock" org-clock-goto t]
  11803. ["Display times" org-clock-display t]
  11804. ["Create clock table" org-clock-report t]
  11805. "--"
  11806. ["Record DONE time"
  11807. (progn (setq org-log-done (not org-log-done))
  11808. (message "Switching to %s will %s record a timestamp"
  11809. (car org-done-keywords)
  11810. (if org-log-done "automatically" "not")))
  11811. :style toggle :selected org-log-done])
  11812. "--"
  11813. ["Agenda Command..." org-agenda t]
  11814. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11815. ("File List for Agenda")
  11816. ("Special views current file"
  11817. ["TODO Tree" org-show-todo-tree t]
  11818. ["Check Deadlines" org-check-deadlines t]
  11819. ["Timeline" org-timeline t]
  11820. ["Tags Tree" org-tags-sparse-tree t])
  11821. "--"
  11822. ("Hyperlinks"
  11823. ["Store Link (Global)" org-store-link t]
  11824. ["Insert Link" org-insert-link t]
  11825. ["Follow Link" org-open-at-point t]
  11826. "--"
  11827. ["Next link" org-next-link t]
  11828. ["Previous link" org-previous-link t]
  11829. "--"
  11830. ["Descriptive Links"
  11831. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11832. :style radio
  11833. :selected (member '(org-link) buffer-invisibility-spec)]
  11834. ["Literal Links"
  11835. (progn
  11836. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11837. :style radio
  11838. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11839. "--"
  11840. ["Export/Publish..." org-export t]
  11841. ("LaTeX"
  11842. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11843. :selected org-cdlatex-mode]
  11844. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11845. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11846. ["Modify math symbol" org-cdlatex-math-modify
  11847. (org-inside-LaTeX-fragment-p)]
  11848. ["Export LaTeX fragments as images"
  11849. (if (featurep 'org-exp)
  11850. (setq org-export-with-LaTeX-fragments
  11851. (not org-export-with-LaTeX-fragments))
  11852. (require 'org-exp))
  11853. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11854. org-export-with-LaTeX-fragments)])
  11855. "--"
  11856. ("Documentation"
  11857. ["Show Version" org-version t]
  11858. ["Info Documentation" org-info t])
  11859. ("Customize"
  11860. ["Browse Org Group" org-customize t]
  11861. "--"
  11862. ["Expand This Menu" org-create-customize-menu
  11863. (fboundp 'customize-menu-create)])
  11864. "--"
  11865. ["Refresh setup" org-mode-restart t]
  11866. ))
  11867. (defun org-info (&optional node)
  11868. "Read documentation for Org-mode in the info system.
  11869. With optional NODE, go directly to that node."
  11870. (interactive)
  11871. (info (format "(org)%s" (or node ""))))
  11872. (defun org-install-agenda-files-menu ()
  11873. (let ((bl (buffer-list)))
  11874. (save-excursion
  11875. (while bl
  11876. (set-buffer (pop bl))
  11877. (if (org-mode-p) (setq bl nil)))
  11878. (when (org-mode-p)
  11879. (easy-menu-change
  11880. '("Org") "File List for Agenda"
  11881. (append
  11882. (list
  11883. ["Edit File List" (org-edit-agenda-file-list) t]
  11884. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11885. ["Remove Current File from List" org-remove-file t]
  11886. ["Cycle through agenda files" org-cycle-agenda-files t]
  11887. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11888. "--")
  11889. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11890. ;;;; Documentation
  11891. ;;;###autoload
  11892. (defun org-require-autoloaded-modules ()
  11893. (interactive)
  11894. (mapc 'require
  11895. '(org-agenda org-archive org-clock org-colview
  11896. org-exp org-id org-export-latex org-publish
  11897. org-remember org-table)))
  11898. ;;;###autoload
  11899. (defun org-customize ()
  11900. "Call the customize function with org as argument."
  11901. (interactive)
  11902. (org-load-modules-maybe)
  11903. (org-require-autoloaded-modules)
  11904. (customize-browse 'org))
  11905. (defun org-create-customize-menu ()
  11906. "Create a full customization menu for Org-mode, insert it into the menu."
  11907. (interactive)
  11908. (org-load-modules-maybe)
  11909. (org-require-autoloaded-modules)
  11910. (if (fboundp 'customize-menu-create)
  11911. (progn
  11912. (easy-menu-change
  11913. '("Org") "Customize"
  11914. `(["Browse Org group" org-customize t]
  11915. "--"
  11916. ,(customize-menu-create 'org)
  11917. ["Set" Custom-set t]
  11918. ["Save" Custom-save t]
  11919. ["Reset to Current" Custom-reset-current t]
  11920. ["Reset to Saved" Custom-reset-saved t]
  11921. ["Reset to Standard Settings" Custom-reset-standard t]))
  11922. (message "\"Org\"-menu now contains full customization menu"))
  11923. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11924. ;;;; Miscellaneous stuff
  11925. ;;; Generally useful functions
  11926. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11927. "Display the given MESSAGE as a warning."
  11928. (if (fboundp 'display-warning)
  11929. (display-warning 'org message
  11930. (if (featurep 'xemacs)
  11931. 'warning
  11932. :warning))
  11933. (let ((buf (get-buffer-create "*Org warnings*")))
  11934. (with-current-buffer buf
  11935. (goto-char (point-max))
  11936. (insert "Warning (Org): " message)
  11937. (unless (bolp)
  11938. (newline)))
  11939. (display-buffer buf)
  11940. (sit-for 0))))
  11941. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  11942. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  11943. (if (and marker (marker-buffer marker)
  11944. (buffer-live-p (marker-buffer marker)))
  11945. (progn
  11946. (switch-to-buffer (marker-buffer marker))
  11947. (if (or (> marker (point-max)) (< marker (point-min)))
  11948. (widen))
  11949. (goto-char marker))
  11950. (if bookmark
  11951. (bookmark-jump bookmark)
  11952. (error "Cannot find location"))))
  11953. (defun org-quote-csv-field (s)
  11954. "Quote field for inclusion in CSV material."
  11955. (if (string-match "[\",]" s)
  11956. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  11957. s))
  11958. (defun org-plist-delete (plist property)
  11959. "Delete PROPERTY from PLIST.
  11960. This is in contrast to merely setting it to 0."
  11961. (let (p)
  11962. (while plist
  11963. (if (not (eq property (car plist)))
  11964. (setq p (plist-put p (car plist) (nth 1 plist))))
  11965. (setq plist (cddr plist)))
  11966. p))
  11967. (defun org-force-self-insert (N)
  11968. "Needed to enforce self-insert under remapping."
  11969. (interactive "p")
  11970. (self-insert-command N))
  11971. (defun org-string-width (s)
  11972. "Compute width of string, ignoring invisible characters.
  11973. This ignores character with invisibility property `org-link', and also
  11974. characters with property `org-cwidth', because these will become invisible
  11975. upon the next fontification round."
  11976. (let (b l)
  11977. (when (or (eq t buffer-invisibility-spec)
  11978. (assq 'org-link buffer-invisibility-spec))
  11979. (while (setq b (text-property-any 0 (length s)
  11980. 'invisible 'org-link s))
  11981. (setq s (concat (substring s 0 b)
  11982. (substring s (or (next-single-property-change
  11983. b 'invisible s) (length s)))))))
  11984. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11985. (setq s (concat (substring s 0 b)
  11986. (substring s (or (next-single-property-change
  11987. b 'org-cwidth s) (length s))))))
  11988. (setq l (string-width s) b -1)
  11989. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11990. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11991. l))
  11992. (defun org-get-indentation (&optional line)
  11993. "Get the indentation of the current line, interpreting tabs.
  11994. When LINE is given, assume it represents a line and compute its indentation."
  11995. (if line
  11996. (if (string-match "^ *" (org-remove-tabs line))
  11997. (match-end 0))
  11998. (save-excursion
  11999. (beginning-of-line 1)
  12000. (skip-chars-forward " \t")
  12001. (current-column))))
  12002. (defun org-remove-tabs (s &optional width)
  12003. "Replace tabulators in S with spaces.
  12004. Assumes that s is a single line, starting in column 0."
  12005. (setq width (or width tab-width))
  12006. (while (string-match "\t" s)
  12007. (setq s (replace-match
  12008. (make-string
  12009. (- (* width (/ (+ (match-beginning 0) width) width))
  12010. (match-beginning 0)) ?\ )
  12011. t t s)))
  12012. s)
  12013. (defun org-fix-indentation (line ind)
  12014. "Fix indentation in LINE.
  12015. IND is a cons cell with target and minimum indentation.
  12016. If the current indenation in LINE is smaller than the minimum,
  12017. leave it alone. If it is larger than ind, set it to the target."
  12018. (let* ((l (org-remove-tabs line))
  12019. (i (org-get-indentation l))
  12020. (i1 (car ind)) (i2 (cdr ind)))
  12021. (if (>= i i2) (setq l (substring line i2)))
  12022. (if (> i1 0)
  12023. (concat (make-string i1 ?\ ) l)
  12024. l)))
  12025. (defun org-base-buffer (buffer)
  12026. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12027. (if (not buffer)
  12028. buffer
  12029. (or (buffer-base-buffer buffer)
  12030. buffer)))
  12031. (defun org-trim (s)
  12032. "Remove whitespace at beginning and end of string."
  12033. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12034. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12035. s)
  12036. (defun org-wrap (string &optional width lines)
  12037. "Wrap string to either a number of lines, or a width in characters.
  12038. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12039. that costs. If there is a word longer than WIDTH, the text is actually
  12040. wrapped to the length of that word.
  12041. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12042. many lines, whatever width that takes.
  12043. The return value is a list of lines, without newlines at the end."
  12044. (let* ((words (org-split-string string "[ \t\n]+"))
  12045. (maxword (apply 'max (mapcar 'org-string-width words)))
  12046. w ll)
  12047. (cond (width
  12048. (org-do-wrap words (max maxword width)))
  12049. (lines
  12050. (setq w maxword)
  12051. (setq ll (org-do-wrap words maxword))
  12052. (if (<= (length ll) lines)
  12053. ll
  12054. (setq ll words)
  12055. (while (> (length ll) lines)
  12056. (setq w (1+ w))
  12057. (setq ll (org-do-wrap words w)))
  12058. ll))
  12059. (t (error "Cannot wrap this")))))
  12060. (defun org-do-wrap (words width)
  12061. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12062. (let (lines line)
  12063. (while words
  12064. (setq line (pop words))
  12065. (while (and words (< (+ (length line) (length (car words))) width))
  12066. (setq line (concat line " " (pop words))))
  12067. (setq lines (push line lines)))
  12068. (nreverse lines)))
  12069. (defun org-split-string (string &optional separators)
  12070. "Splits STRING into substrings at SEPARATORS.
  12071. No empty strings are returned if there are matches at the beginning
  12072. and end of string."
  12073. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12074. (start 0)
  12075. notfirst
  12076. (list nil))
  12077. (while (and (string-match rexp string
  12078. (if (and notfirst
  12079. (= start (match-beginning 0))
  12080. (< start (length string)))
  12081. (1+ start) start))
  12082. (< (match-beginning 0) (length string)))
  12083. (setq notfirst t)
  12084. (or (eq (match-beginning 0) 0)
  12085. (and (eq (match-beginning 0) (match-end 0))
  12086. (eq (match-beginning 0) start))
  12087. (setq list
  12088. (cons (substring string start (match-beginning 0))
  12089. list)))
  12090. (setq start (match-end 0)))
  12091. (or (eq start (length string))
  12092. (setq list
  12093. (cons (substring string start)
  12094. list)))
  12095. (nreverse list)))
  12096. (defun org-context ()
  12097. "Return a list of contexts of the current cursor position.
  12098. If several contexts apply, all are returned.
  12099. Each context entry is a list with a symbol naming the context, and
  12100. two positions indicating start and end of the context. Possible
  12101. contexts are:
  12102. :headline anywhere in a headline
  12103. :headline-stars on the leading stars in a headline
  12104. :todo-keyword on a TODO keyword (including DONE) in a headline
  12105. :tags on the TAGS in a headline
  12106. :priority on the priority cookie in a headline
  12107. :item on the first line of a plain list item
  12108. :item-bullet on the bullet/number of a plain list item
  12109. :checkbox on the checkbox in a plain list item
  12110. :table in an org-mode table
  12111. :table-special on a special filed in a table
  12112. :table-table in a table.el table
  12113. :link on a hyperlink
  12114. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12115. :target on a <<target>>
  12116. :radio-target on a <<<radio-target>>>
  12117. :latex-fragment on a LaTeX fragment
  12118. :latex-preview on a LaTeX fragment with overlayed preview image
  12119. This function expects the position to be visible because it uses font-lock
  12120. faces as a help to recognize the following contexts: :table-special, :link,
  12121. and :keyword."
  12122. (let* ((f (get-text-property (point) 'face))
  12123. (faces (if (listp f) f (list f)))
  12124. (p (point)) clist o)
  12125. ;; First the large context
  12126. (cond
  12127. ((org-on-heading-p t)
  12128. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12129. (when (progn
  12130. (beginning-of-line 1)
  12131. (looking-at org-todo-line-tags-regexp))
  12132. (push (org-point-in-group p 1 :headline-stars) clist)
  12133. (push (org-point-in-group p 2 :todo-keyword) clist)
  12134. (push (org-point-in-group p 4 :tags) clist))
  12135. (goto-char p)
  12136. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12137. (if (looking-at "\\[#[A-Z0-9]\\]")
  12138. (push (org-point-in-group p 0 :priority) clist)))
  12139. ((org-at-item-p)
  12140. (push (org-point-in-group p 2 :item-bullet) clist)
  12141. (push (list :item (point-at-bol)
  12142. (save-excursion (org-end-of-item) (point)))
  12143. clist)
  12144. (and (org-at-item-checkbox-p)
  12145. (push (org-point-in-group p 0 :checkbox) clist)))
  12146. ((org-at-table-p)
  12147. (push (list :table (org-table-begin) (org-table-end)) clist)
  12148. (if (memq 'org-formula faces)
  12149. (push (list :table-special
  12150. (previous-single-property-change p 'face)
  12151. (next-single-property-change p 'face)) clist)))
  12152. ((org-at-table-p 'any)
  12153. (push (list :table-table) clist)))
  12154. (goto-char p)
  12155. ;; Now the small context
  12156. (cond
  12157. ((org-at-timestamp-p)
  12158. (push (org-point-in-group p 0 :timestamp) clist))
  12159. ((memq 'org-link faces)
  12160. (push (list :link
  12161. (previous-single-property-change p 'face)
  12162. (next-single-property-change p 'face)) clist))
  12163. ((memq 'org-special-keyword faces)
  12164. (push (list :keyword
  12165. (previous-single-property-change p 'face)
  12166. (next-single-property-change p 'face)) clist))
  12167. ((org-on-target-p)
  12168. (push (org-point-in-group p 0 :target) clist)
  12169. (goto-char (1- (match-beginning 0)))
  12170. (if (looking-at org-radio-target-regexp)
  12171. (push (org-point-in-group p 0 :radio-target) clist))
  12172. (goto-char p))
  12173. ((setq o (car (delq nil
  12174. (mapcar
  12175. (lambda (x)
  12176. (if (memq x org-latex-fragment-image-overlays) x))
  12177. (org-overlays-at (point))))))
  12178. (push (list :latex-fragment
  12179. (org-overlay-start o) (org-overlay-end o)) clist)
  12180. (push (list :latex-preview
  12181. (org-overlay-start o) (org-overlay-end o)) clist))
  12182. ((org-inside-LaTeX-fragment-p)
  12183. ;; FIXME: positions wrong.
  12184. (push (list :latex-fragment (point) (point)) clist)))
  12185. (setq clist (nreverse (delq nil clist)))
  12186. clist))
  12187. ;; FIXME: Compare with at-regexp-p Do we need both?
  12188. (defun org-in-regexp (re &optional nlines visually)
  12189. "Check if point is inside a match of regexp.
  12190. Normally only the current line is checked, but you can include NLINES extra
  12191. lines both before and after point into the search.
  12192. If VISUALLY is set, require that the cursor is not after the match but
  12193. really on, so that the block visually is on the match."
  12194. (catch 'exit
  12195. (let ((pos (point))
  12196. (eol (point-at-eol (+ 1 (or nlines 0))))
  12197. (inc (if visually 1 0)))
  12198. (save-excursion
  12199. (beginning-of-line (- 1 (or nlines 0)))
  12200. (while (re-search-forward re eol t)
  12201. (if (and (<= (match-beginning 0) pos)
  12202. (>= (+ inc (match-end 0)) pos))
  12203. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12204. (defun org-at-regexp-p (regexp)
  12205. "Is point inside a match of REGEXP in the current line?"
  12206. (catch 'exit
  12207. (save-excursion
  12208. (let ((pos (point)) (end (point-at-eol)))
  12209. (beginning-of-line 1)
  12210. (while (re-search-forward regexp end t)
  12211. (if (and (<= (match-beginning 0) pos)
  12212. (>= (match-end 0) pos))
  12213. (throw 'exit t)))
  12214. nil))))
  12215. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12216. "Call `multi-occur' with buffers for all agenda files."
  12217. (interactive "sOrg-files matching: \np")
  12218. (let* ((files (org-agenda-files))
  12219. (tnames (mapcar 'file-truename files))
  12220. (extra org-agenda-text-search-extra-files)
  12221. f)
  12222. (when (eq (car extra) 'agenda-archives)
  12223. (setq extra (cdr extra))
  12224. (setq files (org-add-archive-files files)))
  12225. (while (setq f (pop extra))
  12226. (unless (member (file-truename f) tnames)
  12227. (add-to-list 'files f 'append)
  12228. (add-to-list 'tnames (file-truename f) 'append)))
  12229. (multi-occur
  12230. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12231. regexp)))
  12232. (if (boundp 'occur-mode-find-occurrence-hook)
  12233. ;; Emacs 23
  12234. (add-hook 'occur-mode-find-occurrence-hook
  12235. (lambda ()
  12236. (when (org-mode-p)
  12237. (org-reveal))))
  12238. ;; Emacs 22
  12239. (defadvice occur-mode-goto-occurrence
  12240. (after org-occur-reveal activate)
  12241. (and (org-mode-p) (org-reveal)))
  12242. (defadvice occur-mode-goto-occurrence-other-window
  12243. (after org-occur-reveal activate)
  12244. (and (org-mode-p) (org-reveal)))
  12245. (defadvice occur-mode-display-occurrence
  12246. (after org-occur-reveal activate)
  12247. (when (org-mode-p)
  12248. (let ((pos (occur-mode-find-occurrence)))
  12249. (with-current-buffer (marker-buffer pos)
  12250. (save-excursion
  12251. (goto-char pos)
  12252. (org-reveal)))))))
  12253. (defun org-uniquify (list)
  12254. "Remove duplicate elements from LIST."
  12255. (let (res)
  12256. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12257. res))
  12258. (defun org-delete-all (elts list)
  12259. "Remove all elements in ELTS from LIST."
  12260. (while elts
  12261. (setq list (delete (pop elts) list)))
  12262. list)
  12263. (defun org-back-over-empty-lines ()
  12264. "Move backwards over witespace, to the beginning of the first empty line.
  12265. Returns the number of empty lines passed."
  12266. (let ((pos (point)))
  12267. (skip-chars-backward " \t\n\r")
  12268. (beginning-of-line 2)
  12269. (goto-char (min (point) pos))
  12270. (count-lines (point) pos)))
  12271. (defun org-skip-whitespace ()
  12272. (skip-chars-forward " \t\n\r"))
  12273. (defun org-point-in-group (point group &optional context)
  12274. "Check if POINT is in match-group GROUP.
  12275. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12276. match. If the match group does ot exist or point is not inside it,
  12277. return nil."
  12278. (and (match-beginning group)
  12279. (>= point (match-beginning group))
  12280. (<= point (match-end group))
  12281. (if context
  12282. (list context (match-beginning group) (match-end group))
  12283. t)))
  12284. (defun org-switch-to-buffer-other-window (&rest args)
  12285. "Switch to buffer in a second window on the current frame.
  12286. In particular, do not allow pop-up frames."
  12287. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12288. special-display-function)
  12289. (apply 'switch-to-buffer-other-window args)))
  12290. (defun org-combine-plists (&rest plists)
  12291. "Create a single property list from all plists in PLISTS.
  12292. The process starts by copying the first list, and then setting properties
  12293. from the other lists. Settings in the last list are the most significant
  12294. ones and overrule settings in the other lists."
  12295. (let ((rtn (copy-sequence (pop plists)))
  12296. p v ls)
  12297. (while plists
  12298. (setq ls (pop plists))
  12299. (while ls
  12300. (setq p (pop ls) v (pop ls))
  12301. (setq rtn (plist-put rtn p v))))
  12302. rtn))
  12303. (defun org-move-line-down (arg)
  12304. "Move the current line down. With prefix argument, move it past ARG lines."
  12305. (interactive "p")
  12306. (let ((col (current-column))
  12307. beg end pos)
  12308. (beginning-of-line 1) (setq beg (point))
  12309. (beginning-of-line 2) (setq end (point))
  12310. (beginning-of-line (+ 1 arg))
  12311. (setq pos (move-marker (make-marker) (point)))
  12312. (insert (delete-and-extract-region beg end))
  12313. (goto-char pos)
  12314. (org-move-to-column col)))
  12315. (defun org-move-line-up (arg)
  12316. "Move the current line up. With prefix argument, move it past ARG lines."
  12317. (interactive "p")
  12318. (let ((col (current-column))
  12319. beg end pos)
  12320. (beginning-of-line 1) (setq beg (point))
  12321. (beginning-of-line 2) (setq end (point))
  12322. (beginning-of-line (- arg))
  12323. (setq pos (move-marker (make-marker) (point)))
  12324. (insert (delete-and-extract-region beg end))
  12325. (goto-char pos)
  12326. (org-move-to-column col)))
  12327. (defun org-replace-escapes (string table)
  12328. "Replace %-escapes in STRING with values in TABLE.
  12329. TABLE is an association list with keys like \"%a\" and string values.
  12330. The sequences in STRING may contain normal field width and padding information,
  12331. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12332. so values can contain further %-escapes if they are define later in TABLE."
  12333. (let ((case-fold-search nil)
  12334. e re rpl)
  12335. (while (setq e (pop table))
  12336. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12337. (while (string-match re string)
  12338. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12339. (cdr e)))
  12340. (setq string (replace-match rpl t t string))))
  12341. string))
  12342. (defun org-sublist (list start end)
  12343. "Return a section of LIST, from START to END.
  12344. Counting starts at 1."
  12345. (let (rtn (c start))
  12346. (setq list (nthcdr (1- start) list))
  12347. (while (and list (<= c end))
  12348. (push (pop list) rtn)
  12349. (setq c (1+ c)))
  12350. (nreverse rtn)))
  12351. (defun org-find-base-buffer-visiting (file)
  12352. "Like `find-buffer-visiting' but alway return the base buffer and
  12353. not an indirect buffer."
  12354. (let ((buf (find-buffer-visiting file)))
  12355. (if buf
  12356. (or (buffer-base-buffer buf) buf)
  12357. nil)))
  12358. (defun org-image-file-name-regexp ()
  12359. "Return regexp matching the file names of images."
  12360. (if (fboundp 'image-file-name-regexp)
  12361. (image-file-name-regexp)
  12362. (let ((image-file-name-extensions
  12363. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12364. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12365. (concat "\\."
  12366. (regexp-opt (nconc (mapcar 'upcase
  12367. image-file-name-extensions)
  12368. image-file-name-extensions)
  12369. t)
  12370. "\\'"))))
  12371. (defun org-file-image-p (file)
  12372. "Return non-nil if FILE is an image."
  12373. (save-match-data
  12374. (string-match (org-image-file-name-regexp) file)))
  12375. (defun org-get-cursor-date ()
  12376. "Return the date at cursor in as a time.
  12377. This works in the calendar and in the agenda, anywhere else it just
  12378. returns the current time."
  12379. (let (date day defd)
  12380. (cond
  12381. ((eq major-mode 'calendar-mode)
  12382. (setq date (calendar-cursor-to-date)
  12383. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12384. ((eq major-mode 'org-agenda-mode)
  12385. (setq day (get-text-property (point) 'day))
  12386. (if day
  12387. (setq date (calendar-gregorian-from-absolute day)
  12388. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12389. (nth 2 date))))))
  12390. (or defd (current-time))))
  12391. (defvar org-agenda-action-marker (make-marker)
  12392. "Marker pointing to the entry for the next agenda action.")
  12393. (defun org-mark-entry-for-agenda-action ()
  12394. "Mark the current entry as target of an agenda action.
  12395. Agenda actions are actions executed from the agenda with the key `k',
  12396. which make use of the date at the cursor."
  12397. (interactive)
  12398. (move-marker org-agenda-action-marker
  12399. (save-excursion (org-back-to-heading t) (point))
  12400. (current-buffer))
  12401. (message
  12402. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12403. ;;; Paragraph filling stuff.
  12404. ;; We want this to be just right, so use the full arsenal.
  12405. (defun org-indent-line-function ()
  12406. "Indent line like previous, but further if previous was headline or item."
  12407. (interactive)
  12408. (let* ((pos (point))
  12409. (itemp (org-at-item-p))
  12410. column bpos bcol tpos tcol bullet btype bullet-type)
  12411. ;; Find the previous relevant line
  12412. (beginning-of-line 1)
  12413. (cond
  12414. ((looking-at "#") (setq column 0))
  12415. ((looking-at "\\*+ ") (setq column 0))
  12416. (t
  12417. (beginning-of-line 0)
  12418. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12419. (beginning-of-line 0))
  12420. (cond
  12421. ((looking-at "\\*+[ \t]+")
  12422. (if (not org-adapt-indentation)
  12423. (setq column 0)
  12424. (goto-char (match-end 0))
  12425. (setq column (current-column))))
  12426. ((org-in-item-p)
  12427. (org-beginning-of-item)
  12428. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12429. (setq bpos (match-beginning 1) tpos (match-end 0)
  12430. bcol (progn (goto-char bpos) (current-column))
  12431. tcol (progn (goto-char tpos) (current-column))
  12432. bullet (match-string 1)
  12433. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12434. (if (> tcol (+ bcol org-description-max-indent))
  12435. (setq tcol (+ bcol 5)))
  12436. (if (not itemp)
  12437. (setq column tcol)
  12438. (goto-char pos)
  12439. (beginning-of-line 1)
  12440. (if (looking-at "\\S-")
  12441. (progn
  12442. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12443. (setq bullet (match-string 1)
  12444. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12445. (setq column (if (equal btype bullet-type) bcol tcol)))
  12446. (setq column (org-get-indentation)))))
  12447. (t (setq column (org-get-indentation))))))
  12448. (goto-char pos)
  12449. (if (<= (current-column) (current-indentation))
  12450. (org-indent-line-to column)
  12451. (save-excursion (org-indent-line-to column)))
  12452. (setq column (current-column))
  12453. (beginning-of-line 1)
  12454. (if (looking-at
  12455. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12456. (replace-match (concat "\\1" (format org-property-format
  12457. (match-string 2) (match-string 3)))
  12458. t nil))
  12459. (org-move-to-column column)))
  12460. (defun org-set-autofill-regexps ()
  12461. (interactive)
  12462. ;; In the paragraph separator we include headlines, because filling
  12463. ;; text in a line directly attached to a headline would otherwise
  12464. ;; fill the headline as well.
  12465. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12466. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12467. ;; The paragraph starter includes hand-formatted lists.
  12468. (org-set-local 'paragraph-start
  12469. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12470. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12471. ;; But only if the user has not turned off tables or fixed-width regions
  12472. (org-set-local
  12473. 'auto-fill-inhibit-regexp
  12474. (concat "\\*+ \\|#\\+"
  12475. "\\|[ \t]*" org-keyword-time-regexp
  12476. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12477. (concat
  12478. "\\|[ \t]*["
  12479. (if org-enable-table-editor "|" "")
  12480. (if org-enable-fixed-width-editor ":" "")
  12481. "]"))))
  12482. ;; We use our own fill-paragraph function, to make sure that tables
  12483. ;; and fixed-width regions are not wrapped. That function will pass
  12484. ;; through to `fill-paragraph' when appropriate.
  12485. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12486. ; Adaptive filling: To get full control, first make sure that
  12487. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12488. (org-set-local 'adaptive-fill-regexp "\000")
  12489. (org-set-local 'adaptive-fill-function
  12490. 'org-adaptive-fill-function)
  12491. (org-set-local
  12492. 'align-mode-rules-list
  12493. '((org-in-buffer-settings
  12494. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12495. (modes . '(org-mode))))))
  12496. (defun org-fill-paragraph (&optional justify)
  12497. "Re-align a table, pass through to fill-paragraph if no table."
  12498. (let ((table-p (org-at-table-p))
  12499. (table.el-p (org-at-table.el-p)))
  12500. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12501. (save-excursion (goto-char (point-at-bol))
  12502. (looking-at outline-regexp)))
  12503. t) ; skip headlines
  12504. (table.el-p t) ; skip table.el tables
  12505. (table-p (org-table-align) t) ; align org-mode tables
  12506. (t nil)))) ; call paragraph-fill
  12507. ;; For reference, this is the default value of adaptive-fill-regexp
  12508. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12509. (defun org-adaptive-fill-function ()
  12510. "Return a fill prefix for org-mode files.
  12511. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12512. work correctly."
  12513. (cond ((looking-at "#[ \t]+")
  12514. (match-string 0))
  12515. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12516. (save-excursion
  12517. (if (> (match-end 1) (+ (match-beginning 1)
  12518. org-description-max-indent))
  12519. (goto-char (+ (match-beginning 1) 5))
  12520. (goto-char (match-end 0)))
  12521. (make-string (current-column) ?\ )))
  12522. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12523. (save-excursion
  12524. (goto-char (match-end 0))
  12525. (make-string (current-column) ?\ )))
  12526. (t nil)))
  12527. ;;; Other stuff.
  12528. (defun org-toggle-fixed-width-section (arg)
  12529. "Toggle the fixed-width export.
  12530. If there is no active region, the QUOTE keyword at the current headline is
  12531. inserted or removed. When present, it causes the text between this headline
  12532. and the next to be exported as fixed-width text, and unmodified.
  12533. If there is an active region, this command adds or removes a colon as the
  12534. first character of this line. If the first character of a line is a colon,
  12535. this line is also exported in fixed-width font."
  12536. (interactive "P")
  12537. (let* ((cc 0)
  12538. (regionp (org-region-active-p))
  12539. (beg (if regionp (region-beginning) (point)))
  12540. (end (if regionp (region-end)))
  12541. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12542. (case-fold-search nil)
  12543. (re "[ \t]*\\(:\\)")
  12544. off)
  12545. (if regionp
  12546. (save-excursion
  12547. (goto-char beg)
  12548. (setq cc (current-column))
  12549. (beginning-of-line 1)
  12550. (setq off (looking-at re))
  12551. (while (> nlines 0)
  12552. (setq nlines (1- nlines))
  12553. (beginning-of-line 1)
  12554. (cond
  12555. (arg
  12556. (org-move-to-column cc t)
  12557. (insert ":\n")
  12558. (forward-line -1))
  12559. ((and off (looking-at re))
  12560. (replace-match "" t t nil 1))
  12561. ((not off) (org-move-to-column cc t) (insert ":")))
  12562. (forward-line 1)))
  12563. (save-excursion
  12564. (org-back-to-heading)
  12565. (if (looking-at (concat outline-regexp
  12566. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12567. (replace-match "" t t nil 1)
  12568. (if (looking-at outline-regexp)
  12569. (progn
  12570. (goto-char (match-end 0))
  12571. (insert org-quote-string " "))))))))
  12572. ;;;; Functions extending outline functionality
  12573. (defun org-beginning-of-line (&optional arg)
  12574. "Go to the beginning of the current line. If that is invisible, continue
  12575. to a visible line beginning. This makes the function of C-a more intuitive.
  12576. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12577. first attempt, and only move to after the tags when the cursor is already
  12578. beyond the end of the headline."
  12579. (interactive "P")
  12580. (let ((pos (point)) refpos)
  12581. (beginning-of-line 1)
  12582. (if (bobp)
  12583. nil
  12584. (backward-char 1)
  12585. (if (org-invisible-p)
  12586. (while (and (not (bobp)) (org-invisible-p))
  12587. (backward-char 1)
  12588. (beginning-of-line 1))
  12589. (forward-char 1)))
  12590. (when org-special-ctrl-a/e
  12591. (cond
  12592. ((and (looking-at org-complex-heading-regexp)
  12593. (= (char-after (match-end 1)) ?\ ))
  12594. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12595. (point-at-eol)))
  12596. (goto-char
  12597. (if (eq org-special-ctrl-a/e t)
  12598. (cond ((> pos refpos) refpos)
  12599. ((= pos (point)) refpos)
  12600. (t (point)))
  12601. (cond ((> pos (point)) (point))
  12602. ((not (eq last-command this-command)) (point))
  12603. (t refpos)))))
  12604. ((org-at-item-p)
  12605. (goto-char
  12606. (if (eq org-special-ctrl-a/e t)
  12607. (cond ((> pos (match-end 4)) (match-end 4))
  12608. ((= pos (point)) (match-end 4))
  12609. (t (point)))
  12610. (cond ((> pos (point)) (point))
  12611. ((not (eq last-command this-command)) (point))
  12612. (t (match-end 4))))))))
  12613. (org-no-warnings
  12614. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12615. (defun org-end-of-line (&optional arg)
  12616. "Go to the end of the line.
  12617. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12618. first attempt, and only move to after the tags when the cursor is already
  12619. beyond the end of the headline."
  12620. (interactive "P")
  12621. (if (or (not org-special-ctrl-a/e)
  12622. (not (org-on-heading-p)))
  12623. (end-of-line arg)
  12624. (let ((pos (point)))
  12625. (beginning-of-line 1)
  12626. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12627. (if (eq org-special-ctrl-a/e t)
  12628. (if (or (< pos (match-beginning 1))
  12629. (= pos (match-end 0)))
  12630. (goto-char (match-beginning 1))
  12631. (goto-char (match-end 0)))
  12632. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12633. (goto-char (match-end 0))
  12634. (goto-char (match-beginning 1))))
  12635. (end-of-line arg))))
  12636. (org-no-warnings
  12637. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12638. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12639. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12640. (defun org-kill-line (&optional arg)
  12641. "Kill line, to tags or end of line."
  12642. (interactive "P")
  12643. (cond
  12644. ((or (not org-special-ctrl-k)
  12645. (bolp)
  12646. (not (org-on-heading-p)))
  12647. (call-interactively 'kill-line))
  12648. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12649. (kill-region (point) (match-beginning 1))
  12650. (org-set-tags nil t))
  12651. (t (kill-region (point) (point-at-eol)))))
  12652. (define-key org-mode-map "\C-k" 'org-kill-line)
  12653. (defun org-yank ()
  12654. "Yank, and if the yanked text is a single subtree, fold it.
  12655. In fact, if the yanked text is a sequence of subtrees, fold all of them."
  12656. (interactive)
  12657. (if org-yank-folded-subtrees
  12658. (let ((beg (point)) end)
  12659. (call-interactively 'yank)
  12660. (setq end (point))
  12661. (goto-char beg)
  12662. (when (and (bolp)
  12663. (org-kill-is-subtree-p))
  12664. (or (looking-at outline-regexp)
  12665. (re-search-forward (concat "^" outline-regexp) end t))
  12666. (while (and (< (point) end) (looking-at outline-regexp))
  12667. (hide-subtree)
  12668. (org-cycle-show-empty-lines 'folded)
  12669. (condition-case nil
  12670. (outline-forward-same-level 1)
  12671. (error (goto-char end)))))
  12672. (goto-char end)
  12673. (skip-chars-forward " \t\n\r"))
  12674. (call-interactively 'yank)))
  12675. (define-key org-mode-map "\C-y" 'org-yank)
  12676. (defun org-invisible-p ()
  12677. "Check if point is at a character currently not visible."
  12678. ;; Early versions of noutline don't have `outline-invisible-p'.
  12679. (if (fboundp 'outline-invisible-p)
  12680. (outline-invisible-p)
  12681. (get-char-property (point) 'invisible)))
  12682. (defun org-invisible-p2 ()
  12683. "Check if point is at a character currently not visible."
  12684. (save-excursion
  12685. (if (and (eolp) (not (bobp))) (backward-char 1))
  12686. ;; Early versions of noutline don't have `outline-invisible-p'.
  12687. (if (fboundp 'outline-invisible-p)
  12688. (outline-invisible-p)
  12689. (get-char-property (point) 'invisible))))
  12690. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12691. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12692. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12693. (defun org-at-heading-or-item-p ()
  12694. (or (org-on-heading-p) (org-at-item-p)))
  12695. (defun org-on-target-p ()
  12696. (or (org-in-regexp org-radio-target-regexp)
  12697. (org-in-regexp org-target-regexp)))
  12698. (defun org-up-heading-all (arg)
  12699. "Move to the heading line of which the present line is a subheading.
  12700. This function considers both visible and invisible heading lines.
  12701. With argument, move up ARG levels."
  12702. (if (fboundp 'outline-up-heading-all)
  12703. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12704. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12705. (defun org-up-heading-safe ()
  12706. "Move to the heading line of which the present line is a subheading.
  12707. This version will not throw an error. It will return the level of the
  12708. headline found, or nil if no higher level is found."
  12709. (let ((pos (point)) start-level level
  12710. (re (concat "^" outline-regexp)))
  12711. (catch 'exit
  12712. (outline-back-to-heading t)
  12713. (setq start-level (funcall outline-level))
  12714. (if (equal start-level 1) (throw 'exit nil))
  12715. (while (re-search-backward re nil t)
  12716. (setq level (funcall outline-level))
  12717. (if (< level start-level) (throw 'exit level)))
  12718. nil)))
  12719. (defun org-first-sibling-p ()
  12720. "Is this heading the first child of its parents?"
  12721. (interactive)
  12722. (let ((re (concat "^" outline-regexp))
  12723. level l)
  12724. (unless (org-at-heading-p t)
  12725. (error "Not at a heading"))
  12726. (setq level (funcall outline-level))
  12727. (save-excursion
  12728. (if (not (re-search-backward re nil t))
  12729. t
  12730. (setq l (funcall outline-level))
  12731. (< l level)))))
  12732. (defun org-goto-sibling (&optional previous)
  12733. "Goto the next sibling, even if it is invisible.
  12734. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12735. when a sibling was found. When none is found, return nil and don't
  12736. move point."
  12737. (let ((fun (if previous 're-search-backward 're-search-forward))
  12738. (pos (point))
  12739. (re (concat "^" outline-regexp))
  12740. level l)
  12741. (when (condition-case nil (org-back-to-heading t) (error nil))
  12742. (setq level (funcall outline-level))
  12743. (catch 'exit
  12744. (or previous (forward-char 1))
  12745. (while (funcall fun re nil t)
  12746. (setq l (funcall outline-level))
  12747. (when (< l level) (goto-char pos) (throw 'exit nil))
  12748. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12749. (goto-char pos)
  12750. nil))))
  12751. (defun org-show-siblings ()
  12752. "Show all siblings of the current headline."
  12753. (save-excursion
  12754. (while (org-goto-sibling) (org-flag-heading nil)))
  12755. (save-excursion
  12756. (while (org-goto-sibling 'previous)
  12757. (org-flag-heading nil))))
  12758. (defun org-show-hidden-entry ()
  12759. "Show an entry where even the heading is hidden."
  12760. (save-excursion
  12761. (org-show-entry)))
  12762. (defun org-flag-heading (flag &optional entry)
  12763. "Flag the current heading. FLAG non-nil means make invisible.
  12764. When ENTRY is non-nil, show the entire entry."
  12765. (save-excursion
  12766. (org-back-to-heading t)
  12767. ;; Check if we should show the entire entry
  12768. (if entry
  12769. (progn
  12770. (org-show-entry)
  12771. (save-excursion
  12772. (and (outline-next-heading)
  12773. (org-flag-heading nil))))
  12774. (outline-flag-region (max (point-min) (1- (point)))
  12775. (save-excursion (outline-end-of-heading) (point))
  12776. flag))))
  12777. (defun org-forward-same-level (arg)
  12778. "Move forward to the ARG'th subheading at same level as this one.
  12779. Stop at the first and last subheadings of a superior heading.
  12780. This is like outline-forward-same-level, but invisible headings are ok."
  12781. (interactive "p")
  12782. (outline-back-to-heading t)
  12783. (while (> arg 0)
  12784. (let ((point-to-move-to (save-excursion
  12785. (org-get-next-sibling))))
  12786. (if point-to-move-to
  12787. (progn
  12788. (goto-char point-to-move-to)
  12789. (setq arg (1- arg)))
  12790. (progn
  12791. (setq arg 0)
  12792. (error "No following same-level heading"))))))
  12793. (defun org-get-next-sibling ()
  12794. "Move to next heading of the same level, and return point.
  12795. If there is no such heading, return nil.
  12796. This is like outline-next-sibling, but invisible headings are ok."
  12797. (let ((level (funcall outline-level)))
  12798. (outline-next-heading)
  12799. (while (and (not (eobp)) (> (funcall outline-level) level))
  12800. (outline-next-heading))
  12801. (if (or (eobp) (< (funcall outline-level) level))
  12802. nil
  12803. (point))))
  12804. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12805. ;; This is an exact copy of the original function, but it uses
  12806. ;; `org-back-to-heading', to make it work also in invisible
  12807. ;; trees. And is uses an invisible-OK argument.
  12808. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12809. (org-back-to-heading invisible-OK)
  12810. (let ((first t)
  12811. (level (funcall outline-level)))
  12812. (while (and (not (eobp))
  12813. (or first (> (funcall outline-level) level)))
  12814. (setq first nil)
  12815. (outline-next-heading))
  12816. (unless to-heading
  12817. (if (memq (preceding-char) '(?\n ?\^M))
  12818. (progn
  12819. ;; Go to end of line before heading
  12820. (forward-char -1)
  12821. (if (memq (preceding-char) '(?\n ?\^M))
  12822. ;; leave blank line before heading
  12823. (forward-char -1))))))
  12824. (point))
  12825. (defun org-show-subtree ()
  12826. "Show everything after this heading at deeper levels."
  12827. (outline-flag-region
  12828. (point)
  12829. (save-excursion
  12830. (outline-end-of-subtree) (outline-next-heading) (point))
  12831. nil))
  12832. (defun org-show-entry ()
  12833. "Show the body directly following this heading.
  12834. Show the heading too, if it is currently invisible."
  12835. (interactive)
  12836. (save-excursion
  12837. (condition-case nil
  12838. (progn
  12839. (org-back-to-heading t)
  12840. (outline-flag-region
  12841. (max (point-min) (1- (point)))
  12842. (save-excursion
  12843. (re-search-forward
  12844. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12845. (or (match-beginning 1) (point-max)))
  12846. nil))
  12847. (error nil))))
  12848. (defun org-make-options-regexp (kwds)
  12849. "Make a regular expression for keyword lines."
  12850. (concat
  12851. "^"
  12852. "#?[ \t]*\\+\\("
  12853. (mapconcat 'regexp-quote kwds "\\|")
  12854. "\\):[ \t]*"
  12855. "\\(.+\\)"))
  12856. ;; Make isearch reveal the necessary context
  12857. (defun org-isearch-end ()
  12858. "Reveal context after isearch exits."
  12859. (when isearch-success ; only if search was successful
  12860. (if (featurep 'xemacs)
  12861. ;; Under XEmacs, the hook is run in the correct place,
  12862. ;; we directly show the context.
  12863. (org-show-context 'isearch)
  12864. ;; In Emacs the hook runs *before* restoring the overlays.
  12865. ;; So we have to use a one-time post-command-hook to do this.
  12866. ;; (Emacs 22 has a special variable, see function `org-mode')
  12867. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12868. isearch-mode-end-hook-quit)
  12869. ;; Only when the isearch was not quitted.
  12870. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12871. 'append 'local)))))
  12872. (defun org-isearch-post-command ()
  12873. "Remove self from hook, and show context."
  12874. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12875. (org-show-context 'isearch))
  12876. ;;;; Integration with and fixes for other packages
  12877. ;;; Imenu support
  12878. (defvar org-imenu-markers nil
  12879. "All markers currently used by Imenu.")
  12880. (make-variable-buffer-local 'org-imenu-markers)
  12881. (defun org-imenu-new-marker (&optional pos)
  12882. "Return a new marker for use by Imenu, and remember the marker."
  12883. (let ((m (make-marker)))
  12884. (move-marker m (or pos (point)))
  12885. (push m org-imenu-markers)
  12886. m))
  12887. (defun org-imenu-get-tree ()
  12888. "Produce the index for Imenu."
  12889. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12890. (setq org-imenu-markers nil)
  12891. (let* ((n org-imenu-depth)
  12892. (re (concat "^" outline-regexp))
  12893. (subs (make-vector (1+ n) nil))
  12894. (last-level 0)
  12895. m tree level head)
  12896. (save-excursion
  12897. (save-restriction
  12898. (widen)
  12899. (goto-char (point-max))
  12900. (while (re-search-backward re nil t)
  12901. (setq level (org-reduced-level (funcall outline-level)))
  12902. (when (<= level n)
  12903. (looking-at org-complex-heading-regexp)
  12904. (setq head (org-link-display-format
  12905. (org-match-string-no-properties 4))
  12906. m (org-imenu-new-marker))
  12907. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12908. (if (>= level last-level)
  12909. (push (cons head m) (aref subs level))
  12910. (push (cons head (aref subs (1+ level))) (aref subs level))
  12911. (loop for i from (1+ level) to n do (aset subs i nil)))
  12912. (setq last-level level)))))
  12913. (aref subs 1)))
  12914. (eval-after-load "imenu"
  12915. '(progn
  12916. (add-hook 'imenu-after-jump-hook
  12917. (lambda ()
  12918. (if (eq major-mode 'org-mode)
  12919. (org-show-context 'org-goto))))))
  12920. (defun org-link-display-format (link)
  12921. "Replace a link with either the description, or the link target
  12922. if no description is present"
  12923. (save-match-data
  12924. (if (string-match org-bracket-link-analytic-regexp link)
  12925. (replace-match (or (match-string 5 link)
  12926. (concat (match-string 1 link)
  12927. (match-string 3 link)))
  12928. nil nil link)
  12929. link)))
  12930. ;; Speedbar support
  12931. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12932. "Overlay marking the agenda restriction line in speedbar.")
  12933. (org-overlay-put org-speedbar-restriction-lock-overlay
  12934. 'face 'org-agenda-restriction-lock)
  12935. (org-overlay-put org-speedbar-restriction-lock-overlay
  12936. 'help-echo "Agendas are currently limited to this item.")
  12937. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12938. (defun org-speedbar-set-agenda-restriction ()
  12939. "Restrict future agenda commands to the location at point in speedbar.
  12940. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12941. (interactive)
  12942. (require 'org-agenda)
  12943. (let (p m tp np dir txt w)
  12944. (cond
  12945. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12946. 'org-imenu t))
  12947. (setq m (get-text-property p 'org-imenu-marker))
  12948. (save-excursion
  12949. (save-restriction
  12950. (set-buffer (marker-buffer m))
  12951. (goto-char m)
  12952. (org-agenda-set-restriction-lock 'subtree))))
  12953. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12954. 'speedbar-function 'speedbar-find-file))
  12955. (setq tp (previous-single-property-change
  12956. (1+ p) 'speedbar-function)
  12957. np (next-single-property-change
  12958. tp 'speedbar-function)
  12959. dir (speedbar-line-directory)
  12960. txt (buffer-substring-no-properties (or tp (point-min))
  12961. (or np (point-max))))
  12962. (save-excursion
  12963. (save-restriction
  12964. (set-buffer (find-file-noselect
  12965. (let ((default-directory dir))
  12966. (expand-file-name txt))))
  12967. (unless (org-mode-p)
  12968. (error "Cannot restrict to non-Org-mode file"))
  12969. (org-agenda-set-restriction-lock 'file))))
  12970. (t (error "Don't know how to restrict Org-mode's agenda")))
  12971. (org-move-overlay org-speedbar-restriction-lock-overlay
  12972. (point-at-bol) (point-at-eol))
  12973. (setq current-prefix-arg nil)
  12974. (org-agenda-maybe-redo)))
  12975. (eval-after-load "speedbar"
  12976. '(progn
  12977. (speedbar-add-supported-extension ".org")
  12978. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12979. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12980. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12981. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12982. (add-hook 'speedbar-visiting-tag-hook
  12983. (lambda () (org-show-context 'org-goto)))))
  12984. ;;; Fixes and Hacks for problems with other packages
  12985. ;; Make flyspell not check words in links, to not mess up our keymap
  12986. (defun org-mode-flyspell-verify ()
  12987. "Don't let flyspell put overlays at active buttons."
  12988. (not (get-text-property (point) 'keymap)))
  12989. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12990. (eval-after-load "bookmark"
  12991. '(if (boundp 'bookmark-after-jump-hook)
  12992. ;; We can use the hook
  12993. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12994. ;; Hook not available, use advice
  12995. (defadvice bookmark-jump (after org-make-visible activate)
  12996. "Make the position visible."
  12997. (org-bookmark-jump-unhide))))
  12998. (defun org-bookmark-jump-unhide ()
  12999. "Unhide the current position, to show the bookmark location."
  13000. (and (org-mode-p)
  13001. (or (org-invisible-p)
  13002. (save-excursion (goto-char (max (point-min) (1- (point))))
  13003. (org-invisible-p)))
  13004. (org-show-context 'bookmark-jump)))
  13005. ;; Make session.el ignore our circular variable
  13006. (eval-after-load "session"
  13007. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13008. ;;;; Experimental code
  13009. (defun org-closed-in-range ()
  13010. "Sparse tree of items closed in a certain time range.
  13011. Still experimental, may disappear in the future."
  13012. (interactive)
  13013. ;; Get the time interval from the user.
  13014. (let* ((time1 (time-to-seconds
  13015. (org-read-date nil 'to-time nil "Starting date: ")))
  13016. (time2 (time-to-seconds
  13017. (org-read-date nil 'to-time nil "End date:")))
  13018. ;; callback function
  13019. (callback (lambda ()
  13020. (let ((time
  13021. (time-to-seconds
  13022. (apply 'encode-time
  13023. (org-parse-time-string
  13024. (match-string 1))))))
  13025. ;; check if time in interval
  13026. (and (>= time time1) (<= time time2))))))
  13027. ;; make tree, check each match with the callback
  13028. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13029. ;;;; Finish up
  13030. (provide 'org)
  13031. (run-hooks 'org-load-hook)
  13032. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13033. ;;; org.el ends here