org.el 528 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283
  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.09a
  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.09a"
  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. (auto-mode . emacs)
  997. ("\\.x?html?\\'" . default)
  998. ("\\.pdf\\'" . default)
  999. )
  1000. "External applications for opening `file:path' items in a document.
  1001. Org-mode uses system defaults for different file types, but
  1002. you can use this variable to set the application for a given file
  1003. extension. The entries in this list are cons cells where the car identifies
  1004. files and the cdr the corresponding command. Possible values for the
  1005. file identifier are
  1006. \"regex\" Regular expression matched against the file name. For backward
  1007. compatibility, this can also be a string with only alphanumeric
  1008. characters, which is then interpreted as an extension.
  1009. `directory' Matches a directory
  1010. `remote' Matches a remote file, accessible through tramp or efs.
  1011. Remote files most likely should be visited through Emacs
  1012. because external applications cannot handle such paths.
  1013. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1014. so all files Emacs knows how to handle. Useing this with
  1015. command `emacs' will open most files in Emacs. Beware that this
  1016. will also open html files insite Emacs, unless you add
  1017. (\"html\" . default) to the list as well.
  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, which is the
  1022. association for t in the list, most likely in the system-specific
  1023. part.
  1024. This can be used to overrule an unwanted seting in the
  1025. system-specific variable.
  1026. string A command to be executed by a shell; %s will be replaced
  1027. by the path to the file.
  1028. sexp A Lisp form which will be evaluated. The file path will
  1029. be available in the Lisp variable `file'.
  1030. For more examples, see the system specific constants
  1031. `org-file-apps-defaults-macosx'
  1032. `org-file-apps-defaults-windowsnt'
  1033. `org-file-apps-defaults-gnu'."
  1034. :group 'org-link-follow
  1035. :type '(repeat
  1036. (cons (choice :value ""
  1037. (string :tag "Extension")
  1038. (const :tag "Default for unrecognized files" t)
  1039. (const :tag "Remote file" remote)
  1040. (const :tag "Links to a directory" directory)
  1041. (const :tag "Any files that have Emacs modes"
  1042. auto-mode))
  1043. (choice :value ""
  1044. (const :tag "Visit with Emacs" emacs)
  1045. (const :tag "Use system default" default)
  1046. (string :tag "Command")
  1047. (sexp :tag "Lisp form")))))
  1048. (defgroup org-refile nil
  1049. "Options concerning refiling entries in Org-mode."
  1050. :tag "Org Remember"
  1051. :group 'org)
  1052. (defcustom org-directory "~/org"
  1053. "Directory with org files.
  1054. This directory will be used as default to prompt for org files.
  1055. Used by the hooks for remember.el."
  1056. :group 'org-refile
  1057. :group 'org-remember
  1058. :type 'directory)
  1059. (defcustom org-default-notes-file "~/.notes"
  1060. "Default target for storing notes.
  1061. Used by the hooks for remember.el. This can be a string, or nil to mean
  1062. the value of `remember-data-file'.
  1063. You can set this on a per-template basis with the variable
  1064. `org-remember-templates'."
  1065. :group 'org-refile
  1066. :group 'org-remember
  1067. :type '(choice
  1068. (const :tag "Default from remember-data-file" nil)
  1069. file))
  1070. (defcustom org-goto-interface 'outline
  1071. "The default interface to be used for `org-goto'.
  1072. Allowed vaues are:
  1073. outline The interface shows an outline of the relevant file
  1074. and the correct heading is found by moving through
  1075. the outline or by searching with incremental search.
  1076. outline-path-completion Headlines in the current buffer are offered via
  1077. completion."
  1078. :group 'org-refile
  1079. :type '(choice
  1080. (const :tag "Outline" outline)
  1081. (const :tag "Outline-path-completion" outline-path-completion)))
  1082. (defcustom org-reverse-note-order nil
  1083. "Non-nil means, store new notes at the beginning of a file or entry.
  1084. When nil, new notes will be filed to the end of a file or entry.
  1085. This can also be a list with cons cells of regular expressions that
  1086. are matched against file names, and values."
  1087. :group 'org-remember
  1088. :type '(choice
  1089. (const :tag "Reverse always" t)
  1090. (const :tag "Reverse never" nil)
  1091. (repeat :tag "By file name regexp"
  1092. (cons regexp boolean))))
  1093. (defcustom org-refile-targets nil
  1094. "Targets for refiling entries with \\[org-refile].
  1095. This is list of cons cells. Each cell contains:
  1096. - a specification of the files to be considered, either a list of files,
  1097. or a symbol whose function or variable value will be used to retrieve
  1098. a file name or a list of file names. Nil means, refile to a different
  1099. heading in the current buffer.
  1100. - A specification of how to find candidate refile targets. This may be
  1101. any of
  1102. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1103. This tag has to be present in all target headlines, inheritance will
  1104. not be considered.
  1105. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1106. todo keyword.
  1107. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1108. headlines that are refiling targets.
  1109. - a cons cell (:level . N). Any headline of level N is considered a target.
  1110. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1111. When this variable is nil, all top-level headlines in the current buffer
  1112. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1113. :group 'org-remember
  1114. :type '(repeat
  1115. (cons
  1116. (choice :value org-agenda-files
  1117. (const :tag "All agenda files" org-agenda-files)
  1118. (const :tag "Current buffer" nil)
  1119. (function) (variable) (file))
  1120. (choice :tag "Identify target headline by"
  1121. (cons :tag "Specific tag" (const :tag) (string))
  1122. (cons :tag "TODO keyword" (const :todo) (string))
  1123. (cons :tag "Regular expression" (const :regexp) (regexp))
  1124. (cons :tag "Level number" (const :level) (integer))
  1125. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1126. (defcustom org-refile-use-outline-path nil
  1127. "Non-nil means, provide refile targets as paths.
  1128. So a level 3 headline will be available as level1/level2/level3.
  1129. When the value is `file', also include the file name (without directory)
  1130. into the path. When `full-file-path', include the full file path."
  1131. :group 'org-remember
  1132. :type '(choice
  1133. (const :tag "Not" nil)
  1134. (const :tag "Yes" t)
  1135. (const :tag "Start with file name" file)
  1136. (const :tag "Start with full file path" full-file-path)))
  1137. (defgroup org-todo nil
  1138. "Options concerning TODO items in Org-mode."
  1139. :tag "Org TODO"
  1140. :group 'org)
  1141. (defgroup org-progress nil
  1142. "Options concerning Progress logging in Org-mode."
  1143. :tag "Org Progress"
  1144. :group 'org-time)
  1145. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1146. "List of TODO entry keyword sequences and their interpretation.
  1147. \\<org-mode-map>This is a list of sequences.
  1148. Each sequence starts with a symbol, either `sequence' or `type',
  1149. indicating if the keywords should be interpreted as a sequence of
  1150. action steps, or as different types of TODO items. The first
  1151. keywords are states requiring action - these states will select a headline
  1152. for inclusion into the global TODO list Org-mode produces. If one of
  1153. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1154. signify that no further action is necessary. If \"|\" is not found,
  1155. the last keyword is treated as the only DONE state of the sequence.
  1156. The command \\[org-todo] cycles an entry through these states, and one
  1157. additional state where no keyword is present. For details about this
  1158. cycling, see the manual.
  1159. TODO keywords and interpretation can also be set on a per-file basis with
  1160. the special #+SEQ_TODO and #+TYP_TODO lines.
  1161. Each keyword can optionally specify a character for fast state selection
  1162. \(in combination with the variable `org-use-fast-todo-selection')
  1163. and specifiers for state change logging, using the same syntax
  1164. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1165. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1166. indicates to record a time stamp each time this state is selected.
  1167. Each keyword may also specify if a timestamp or a note should be
  1168. recorded when entering or leaving the state, by adding additional
  1169. characters in the parenthesis after the keyword. This looks like this:
  1170. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1171. record only the time of the state change. With X and Y being either
  1172. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1173. Y when leaving the state if and only if the *target* state does not
  1174. define X. You may omit any of the fast-selection key or X or /Y,
  1175. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1176. For backward compatibility, this variable may also be just a list
  1177. of keywords - in this case the interptetation (sequence or type) will be
  1178. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1179. :group 'org-todo
  1180. :group 'org-keywords
  1181. :type '(choice
  1182. (repeat :tag "Old syntax, just keywords"
  1183. (string :tag "Keyword"))
  1184. (repeat :tag "New syntax"
  1185. (cons
  1186. (choice
  1187. :tag "Interpretation"
  1188. (const :tag "Sequence (cycling hits every state)" sequence)
  1189. (const :tag "Type (cycling directly to DONE)" type))
  1190. (repeat
  1191. (string :tag "Keyword"))))))
  1192. (defvar org-todo-keywords-1 nil
  1193. "All TODO and DONE keywords active in a buffer.")
  1194. (make-variable-buffer-local 'org-todo-keywords-1)
  1195. (defvar org-todo-keywords-for-agenda nil)
  1196. (defvar org-done-keywords-for-agenda nil)
  1197. (defvar org-todo-keyword-alist-for-agenda nil)
  1198. (defvar org-tag-alist-for-agenda nil)
  1199. (defvar org-agenda-contributing-files nil)
  1200. (defvar org-not-done-keywords nil)
  1201. (make-variable-buffer-local 'org-not-done-keywords)
  1202. (defvar org-done-keywords nil)
  1203. (make-variable-buffer-local 'org-done-keywords)
  1204. (defvar org-todo-heads nil)
  1205. (make-variable-buffer-local 'org-todo-heads)
  1206. (defvar org-todo-sets nil)
  1207. (make-variable-buffer-local 'org-todo-sets)
  1208. (defvar org-todo-log-states nil)
  1209. (make-variable-buffer-local 'org-todo-log-states)
  1210. (defvar org-todo-kwd-alist nil)
  1211. (make-variable-buffer-local 'org-todo-kwd-alist)
  1212. (defvar org-todo-key-alist nil)
  1213. (make-variable-buffer-local 'org-todo-key-alist)
  1214. (defvar org-todo-key-trigger nil)
  1215. (make-variable-buffer-local 'org-todo-key-trigger)
  1216. (defcustom org-todo-interpretation 'sequence
  1217. "Controls how TODO keywords are interpreted.
  1218. This variable is in principle obsolete and is only used for
  1219. backward compatibility, if the interpretation of todo keywords is
  1220. not given already in `org-todo-keywords'. See that variable for
  1221. more information."
  1222. :group 'org-todo
  1223. :group 'org-keywords
  1224. :type '(choice (const sequence)
  1225. (const type)))
  1226. (defcustom org-use-fast-todo-selection 'prefix
  1227. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1228. This variable describes if and under what circumstances the cycling
  1229. mechanism for TODO keywords will be replaced by a single-key, direct
  1230. selection scheme.
  1231. When nil, fast selection is never used.
  1232. When the symbol `prefix', it will be used when `org-todo' is called with
  1233. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1234. in an agenda buffer.
  1235. When t, fast selection is used by default. In this case, the prefix
  1236. argument forces cycling instead.
  1237. In all cases, the special interface is only used if access keys have actually
  1238. been assigned by the user, i.e. if keywords in the configuration are followed
  1239. by a letter in parenthesis, like TODO(t)."
  1240. :group 'org-todo
  1241. :type '(choice
  1242. (const :tag "Never" nil)
  1243. (const :tag "By default" t)
  1244. (const :tag "Only with C-u C-c C-t" prefix)))
  1245. (defcustom org-provide-todo-statistics t
  1246. "Non-nil means, update todo statistics after insert and toggle.
  1247. When this is set, todo statistics is updated in the parent of the current
  1248. entry each time a todo state is changed."
  1249. :group 'org-todo
  1250. :type 'boolean)
  1251. (defcustom org-after-todo-state-change-hook nil
  1252. "Hook which is run after the state of a TODO item was changed.
  1253. The new state (a string with a TODO keyword, or nil) is available in the
  1254. Lisp variable `state'."
  1255. :group 'org-todo
  1256. :type 'hook)
  1257. (defcustom org-todo-state-tags-triggers nil
  1258. "Tag changes that should be triggered by TODO state changes.
  1259. This is a list. Each entry is
  1260. (state-change (tag . flag) .......)
  1261. State-change can be a string with a state, and empty string to indicate the
  1262. state that has no TODO keyword, or it can be one of the symbols `todo'
  1263. or `done', meaning any not-done or done state, respectively."
  1264. :group 'org-todo
  1265. :group 'org-tags
  1266. :type '(repeat
  1267. (cons (choice :tag "When changing to"
  1268. (const :tag "Not-done state" todo)
  1269. (const :tag "Done state" done)
  1270. (string :tag "State"))
  1271. (repeat
  1272. (cons :tag "Tag action"
  1273. (string :tag "Tag")
  1274. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1275. (defcustom org-log-done nil
  1276. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1277. When equal to the list (done), also prompt for a closing note.
  1278. This can also be configured on a per-file basis by adding one of
  1279. the following lines anywhere in the buffer:
  1280. #+STARTUP: logdone
  1281. #+STARTUP: lognotedone
  1282. #+STARTUP: nologdone"
  1283. :group 'org-todo
  1284. :group 'org-progress
  1285. :type '(choice
  1286. (const :tag "No logging" nil)
  1287. (const :tag "Record CLOSED timestamp" time)
  1288. (const :tag "Record CLOSED timestamp with closing note." note)))
  1289. ;; Normalize old uses of org-log-done.
  1290. (cond
  1291. ((eq org-log-done t) (setq org-log-done 'time))
  1292. ((and (listp org-log-done) (memq 'done org-log-done))
  1293. (setq org-log-done 'note)))
  1294. (defcustom org-log-note-clock-out nil
  1295. "Non-nil means, record a note when clocking out of an item.
  1296. This can also be configured on a per-file basis by adding one of
  1297. the following lines anywhere in the buffer:
  1298. #+STARTUP: lognoteclock-out
  1299. #+STARTUP: nolognoteclock-out"
  1300. :group 'org-todo
  1301. :group 'org-progress
  1302. :type 'boolean)
  1303. (defcustom org-log-done-with-time t
  1304. "Non-nil means, the CLOSED time stamp will contain date and time.
  1305. When nil, only the date will be recorded."
  1306. :group 'org-progress
  1307. :type 'boolean)
  1308. (defcustom org-log-note-headings
  1309. '((done . "CLOSING NOTE %t")
  1310. (state . "State %-12s %t")
  1311. (note . "Note taken on %t")
  1312. (clock-out . ""))
  1313. "Headings for notes added to entries.
  1314. The value is an alist, with the car being a symbol indicating the note
  1315. context, and the cdr is the heading to be used. The heading may also be the
  1316. empty string.
  1317. %t in the heading will be replaced by a time stamp.
  1318. %s will be replaced by the new TODO state, in double quotes.
  1319. %u will be replaced by the user name.
  1320. %U will be replaced by the full user name."
  1321. :group 'org-todo
  1322. :group 'org-progress
  1323. :type '(list :greedy t
  1324. (cons (const :tag "Heading when closing an item" done) string)
  1325. (cons (const :tag
  1326. "Heading when changing todo state (todo sequence only)"
  1327. state) string)
  1328. (cons (const :tag "Heading when just taking a note" note) string)
  1329. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1330. (unless (assq 'note org-log-note-headings)
  1331. (push '(note . "%t") org-log-note-headings))
  1332. (defcustom org-log-state-notes-insert-after-drawers nil
  1333. "Non-nil means, insert state change notes after any drawers in entry.
  1334. When nil, insert them right after the heading and perhaps the line
  1335. with deadline/scheduling if present."
  1336. :group 'org-todo
  1337. :group 'org-progress
  1338. :type 'boolean)
  1339. (defcustom org-log-states-order-reversed t
  1340. "Non-nil means, the latest state change note will be directly after heading.
  1341. When nil, the notes will be orderer according to time."
  1342. :group 'org-todo
  1343. :group 'org-progress
  1344. :type 'boolean)
  1345. (defcustom org-log-repeat 'time
  1346. "Non-nil means, record moving through the DONE state when triggering repeat.
  1347. An auto-repeating tasks is immediately switched back to TODO when marked
  1348. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1349. the TODO keyword definition, or recording a closing note by setting
  1350. `org-log-done', there will be no record of the task moving through DONE.
  1351. This variable forces taking a note anyway. Possible values are:
  1352. nil Don't force a record
  1353. time Record a time stamp
  1354. note Record a note
  1355. This option can also be set with on a per-file-basis with
  1356. #+STARTUP: logrepeat
  1357. #+STARTUP: lognoterepeat
  1358. #+STARTUP: nologrepeat
  1359. You can have local logging settings for a subtree by setting the LOGGING
  1360. property to one or more of these keywords."
  1361. :group 'org-todo
  1362. :group 'org-progress
  1363. :type '(choice
  1364. (const :tag "Don't force a record" nil)
  1365. (const :tag "Force recording the DONE state" time)
  1366. (const :tag "Force recording a note with the DONE state" note)))
  1367. (defgroup org-priorities nil
  1368. "Priorities in Org-mode."
  1369. :tag "Org Priorities"
  1370. :group 'org-todo)
  1371. (defcustom org-highest-priority ?A
  1372. "The highest priority of TODO items. A character like ?A, ?B etc.
  1373. Must have a smaller ASCII number than `org-lowest-priority'."
  1374. :group 'org-priorities
  1375. :type 'character)
  1376. (defcustom org-lowest-priority ?C
  1377. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1378. Must have a larger ASCII number than `org-highest-priority'."
  1379. :group 'org-priorities
  1380. :type 'character)
  1381. (defcustom org-default-priority ?B
  1382. "The default priority of TODO items.
  1383. This is the priority an item get if no explicit priority is given."
  1384. :group 'org-priorities
  1385. :type 'character)
  1386. (defcustom org-priority-start-cycle-with-default t
  1387. "Non-nil means, start with default priority when starting to cycle.
  1388. When this is nil, the first step in the cycle will be (depending on the
  1389. command used) one higher or lower that the default priority."
  1390. :group 'org-priorities
  1391. :type 'boolean)
  1392. (defgroup org-time nil
  1393. "Options concerning time stamps and deadlines in Org-mode."
  1394. :tag "Org Time"
  1395. :group 'org)
  1396. (defcustom org-insert-labeled-timestamps-at-point nil
  1397. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1398. When nil, these labeled time stamps are forces into the second line of an
  1399. entry, just after the headline. When scheduling from the global TODO list,
  1400. the time stamp will always be forced into the second line."
  1401. :group 'org-time
  1402. :type 'boolean)
  1403. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1404. "Formats for `format-time-string' which are used for time stamps.
  1405. It is not recommended to change this constant.")
  1406. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1407. "Number of minutes to round time stamps to.
  1408. These are two values, the first applies when first creating a time stamp.
  1409. The second applies when changing it with the commands `S-up' and `S-down'.
  1410. When changing the time stamp, this means that it will change in steps
  1411. of N minutes, as given by the second value.
  1412. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1413. numbers should be factors of 60, so for example 5, 10, 15.
  1414. When this is larger than 1, you can still force an exact time-stamp by using
  1415. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1416. and by using a prefix arg to `S-up/down' to specify the exact number
  1417. of minutes to shift."
  1418. :group 'org-time
  1419. :get '(lambda (var) ; Make sure all entries have 5 elements
  1420. (if (integerp (default-value var))
  1421. (list (default-value var) 5)
  1422. (default-value var)))
  1423. :type '(list
  1424. (integer :tag "when inserting times")
  1425. (integer :tag "when modifying times")))
  1426. ;; Normalize old customizations of this variable.
  1427. (when (integerp org-time-stamp-rounding-minutes)
  1428. (setq org-time-stamp-rounding-minutes
  1429. (list org-time-stamp-rounding-minutes
  1430. org-time-stamp-rounding-minutes)))
  1431. (defcustom org-display-custom-times nil
  1432. "Non-nil means, overlay custom formats over all time stamps.
  1433. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1434. To turn this on on a per-file basis, insert anywhere in the file:
  1435. #+STARTUP: customtime"
  1436. :group 'org-time
  1437. :set 'set-default
  1438. :type 'sexp)
  1439. (make-variable-buffer-local 'org-display-custom-times)
  1440. (defcustom org-time-stamp-custom-formats
  1441. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1442. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1443. These are overlayed over the default ISO format if the variable
  1444. `org-display-custom-times' is set. Time like %H:%M should be at the
  1445. end of the second format."
  1446. :group 'org-time
  1447. :type 'sexp)
  1448. (defun org-time-stamp-format (&optional long inactive)
  1449. "Get the right format for a time string."
  1450. (let ((f (if long (cdr org-time-stamp-formats)
  1451. (car org-time-stamp-formats))))
  1452. (if inactive
  1453. (concat "[" (substring f 1 -1) "]")
  1454. f)))
  1455. (defcustom org-time-clocksum-format "%d:%02d"
  1456. "The format string used when creating CLOCKSUM lines, or when
  1457. org-mode generates a time duration."
  1458. :group 'org-time
  1459. :type 'string)
  1460. (defcustom org-deadline-warning-days 14
  1461. "No. of days before expiration during which a deadline becomes active.
  1462. This variable governs the display in sparse trees and in the agenda.
  1463. When 0 or negative, it means use this number (the absolute value of it)
  1464. even if a deadline has a different individual lead time specified."
  1465. :group 'org-time
  1466. :group 'org-agenda-daily/weekly
  1467. :type 'number)
  1468. (defcustom org-read-date-prefer-future t
  1469. "Non-nil means, assume future for incomplete date input from user.
  1470. This affects the following situations:
  1471. 1. The user gives a day, but no month.
  1472. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1473. read this as the third of *next* month. However, if you enter \"17\",
  1474. it will be considered as *this* month.
  1475. 2. The user gives a month but not a year.
  1476. For example, if it is april and you enter \"feb 2\", this will be read
  1477. as feb 2, *next* year. \"May 5\", however, will be this year.
  1478. Currently this does not work for ISO week specifications.
  1479. When this option is nil, the current month and year will always be used
  1480. as defaults."
  1481. :group 'org-time
  1482. :type 'boolean)
  1483. (defcustom org-read-date-display-live t
  1484. "Non-nil means, display current interpretation of date prompt live.
  1485. This display will be in an overlay, in the minibuffer."
  1486. :group 'org-time
  1487. :type 'boolean)
  1488. (defcustom org-read-date-popup-calendar t
  1489. "Non-nil means, pop up a calendar when prompting for a date.
  1490. In the calendar, the date can be selected with mouse-1. However, the
  1491. minibuffer will also be active, and you can simply enter the date as well.
  1492. When nil, only the minibuffer will be available."
  1493. :group 'org-time
  1494. :type 'boolean)
  1495. (if (fboundp 'defvaralias)
  1496. (defvaralias 'org-popup-calendar-for-date-prompt
  1497. 'org-read-date-popup-calendar))
  1498. (defcustom org-extend-today-until 0
  1499. "The hour when your day really ends. Must be an integer.
  1500. This has influence for the following applications:
  1501. - When switching the agenda to \"today\". It it is still earlier than
  1502. the time given here, the day recognized as TODAY is actually yesterday.
  1503. - When a date is read from the user and it is still before the time given
  1504. here, the current date and time will be assumed to be yesterday, 23:59.
  1505. Also, timestamps inserted in remember templates follow this rule.
  1506. IMPORTANT: This is a feature whose implementation is and likely will
  1507. remain incomplete. Really, it is only here because past midnight seems to
  1508. ne the favorite working time of John Wiegley :-)"
  1509. :group 'org-time
  1510. :type 'number)
  1511. (defcustom org-edit-timestamp-down-means-later nil
  1512. "Non-nil means, S-down will increase the time in a time stamp.
  1513. When nil, S-up will increase."
  1514. :group 'org-time
  1515. :type 'boolean)
  1516. (defcustom org-calendar-follow-timestamp-change t
  1517. "Non-nil means, make the calendar window follow timestamp changes.
  1518. When a timestamp is modified and the calendar window is visible, it will be
  1519. moved to the new date."
  1520. :group 'org-time
  1521. :type 'boolean)
  1522. (defgroup org-tags nil
  1523. "Options concerning tags in Org-mode."
  1524. :tag "Org Tags"
  1525. :group 'org)
  1526. (defcustom org-tag-alist nil
  1527. "List of tags allowed in Org-mode files.
  1528. When this list is nil, Org-mode will base TAG input on what is already in the
  1529. buffer.
  1530. The value of this variable is an alist, the car of each entry must be a
  1531. keyword as a string, the cdr may be a character that is used to select
  1532. that tag through the fast-tag-selection interface.
  1533. See the manual for details."
  1534. :group 'org-tags
  1535. :type '(repeat
  1536. (choice
  1537. (cons (string :tag "Tag name")
  1538. (character :tag "Access char"))
  1539. (const :tag "Start radio group" (:startgroup))
  1540. (const :tag "End radio group" (:endgroup)))))
  1541. (defvar org-file-tags nil
  1542. "List of tags that can be inherited by all entries in the file.
  1543. The tags will be inherited if the variable `org-use-tag-inheritance'
  1544. says they should be.
  1545. This variable is populated from #+TAG lines.")
  1546. (defcustom org-use-fast-tag-selection 'auto
  1547. "Non-nil means, use fast tag selection scheme.
  1548. This is a special interface to select and deselect tags with single keys.
  1549. When nil, fast selection is never used.
  1550. When the symbol `auto', fast selection is used if and only if selection
  1551. characters for tags have been configured, either through the variable
  1552. `org-tag-alist' or through a #+TAGS line in the buffer.
  1553. When t, fast selection is always used and selection keys are assigned
  1554. automatically if necessary."
  1555. :group 'org-tags
  1556. :type '(choice
  1557. (const :tag "Always" t)
  1558. (const :tag "Never" nil)
  1559. (const :tag "When selection characters are configured" 'auto)))
  1560. (defcustom org-fast-tag-selection-single-key nil
  1561. "Non-nil means, fast tag selection exits after first change.
  1562. When nil, you have to press RET to exit it.
  1563. During fast tag selection, you can toggle this flag with `C-c'.
  1564. This variable can also have the value `expert'. In this case, the window
  1565. displaying the tags menu is not even shown, until you press C-c again."
  1566. :group 'org-tags
  1567. :type '(choice
  1568. (const :tag "No" nil)
  1569. (const :tag "Yes" t)
  1570. (const :tag "Expert" expert)))
  1571. (defvar org-fast-tag-selection-include-todo nil
  1572. "Non-nil means, fast tags selection interface will also offer TODO states.
  1573. This is an undocumented feature, you should not rely on it.")
  1574. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1575. "The column to which tags should be indented in a headline.
  1576. If this number is positive, it specifies the column. If it is negative,
  1577. it means that the tags should be flushright to that column. For example,
  1578. -80 works well for a normal 80 character screen."
  1579. :group 'org-tags
  1580. :type 'integer)
  1581. (defcustom org-auto-align-tags t
  1582. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1583. These operations change the length of a headline and therefore shift
  1584. the tags around. With this options turned on, after each such operation
  1585. the tags are again aligned to `org-tags-column'."
  1586. :group 'org-tags
  1587. :type 'boolean)
  1588. (defcustom org-use-tag-inheritance t
  1589. "Non-nil means, tags in levels apply also for sublevels.
  1590. When nil, only the tags directly given in a specific line apply there.
  1591. If this option is t, a match early-on in a tree can lead to a large
  1592. number of matches in the subtree. If you only want to see the first
  1593. match in a tree during a search, check out the variable
  1594. `org-tags-match-list-sublevels'.
  1595. This may also be a list of tags that should be inherited, or a regexp that
  1596. matches tags that should be inherited."
  1597. :group 'org-tags
  1598. :type '(choice
  1599. (const :tag "Not" nil)
  1600. (const :tag "Always" t)
  1601. (repeat :tag "Specific tags" (string :tag "Tag"))
  1602. (regexp :tag "Tags matched by regexp")))
  1603. (defun org-tag-inherit-p (tag)
  1604. "Check if TAG is one that should be inherited."
  1605. (cond
  1606. ((eq org-use-tag-inheritance t) t)
  1607. ((not org-use-tag-inheritance) nil)
  1608. ((stringp org-use-tag-inheritance)
  1609. (string-match org-use-tag-inheritance tag))
  1610. ((listp org-use-tag-inheritance)
  1611. (member tag org-use-tag-inheritance))
  1612. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1613. (defcustom org-tags-match-list-sublevels t
  1614. "Non-nil means list also sublevels of headlines matching tag search.
  1615. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1616. the sublevels of a headline matching a tag search often also match
  1617. the same search. Listing all of them can create very long lists.
  1618. Setting this variable to nil causes subtrees of a match to be skipped.
  1619. This option is off by default, because inheritance in on. If you turn
  1620. inheritance off, you very likely want to turn this option on.
  1621. As a special case, if the tag search is restricted to TODO items, the
  1622. value of this variable is ignored and sublevels are always checked, to
  1623. make sure all corresponding TODO items find their way into the list."
  1624. :group 'org-tags
  1625. :type 'boolean)
  1626. (defvar org-tags-history nil
  1627. "History of minibuffer reads for tags.")
  1628. (defvar org-last-tags-completion-table nil
  1629. "The last used completion table for tags.")
  1630. (defvar org-after-tags-change-hook nil
  1631. "Hook that is run after the tags in a line have changed.")
  1632. (defgroup org-properties nil
  1633. "Options concerning properties in Org-mode."
  1634. :tag "Org Properties"
  1635. :group 'org)
  1636. (defcustom org-property-format "%-10s %s"
  1637. "How property key/value pairs should be formatted by `indent-line'.
  1638. When `indent-line' hits a property definition, it will format the line
  1639. according to this format, mainly to make sure that the values are
  1640. lined-up with respect to each other."
  1641. :group 'org-properties
  1642. :type 'string)
  1643. (defcustom org-use-property-inheritance nil
  1644. "Non-nil means, properties apply also for sublevels.
  1645. This setting is chiefly used during property searches. Turning it on can
  1646. cause significant overhead when doing a search, which is why it is not
  1647. on by default.
  1648. When nil, only the properties directly given in the current entry count.
  1649. When t, every property is inherited. The value may also be a list of
  1650. properties that should have inheritance, or a regular expression matching
  1651. properties that should be inherited.
  1652. However, note that some special properties use inheritance under special
  1653. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1654. and the properties ending in \"_ALL\" when they are used as descriptor
  1655. for valid values of a property.
  1656. Note for programmers:
  1657. When querying an entry with `org-entry-get', you can control if inheritance
  1658. should be used. By default, `org-entry-get' looks only at the local
  1659. properties. You can request inheritance by setting the inherit argument
  1660. to t (to force inheritance) or to `selective' (to respect the setting
  1661. in this variable)."
  1662. :group 'org-properties
  1663. :type '(choice
  1664. (const :tag "Not" nil)
  1665. (const :tag "Always" t)
  1666. (repeat :tag "Specific properties" (string :tag "Property"))
  1667. (regexp :tag "Properties matched by regexp")))
  1668. (defun org-property-inherit-p (property)
  1669. "Check if PROPERTY is one that should be inherited."
  1670. (cond
  1671. ((eq org-use-property-inheritance t) t)
  1672. ((not org-use-property-inheritance) nil)
  1673. ((stringp org-use-property-inheritance)
  1674. (string-match org-use-property-inheritance property))
  1675. ((listp org-use-property-inheritance)
  1676. (member property org-use-property-inheritance))
  1677. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1678. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1679. "The default column format, if no other format has been defined.
  1680. This variable can be set on the per-file basis by inserting a line
  1681. #+COLUMNS: %25ITEM ....."
  1682. :group 'org-properties
  1683. :type 'string)
  1684. (defcustom org-columns-ellipses ".."
  1685. "The ellipses to be used when a field in column view is truncated.
  1686. When this is the empty string, as many characters as possible are shown,
  1687. but then there will be no visual indication that the field has been truncated.
  1688. When this is a string of length N, the last N characters of a truncated
  1689. field are replaced by this string. If the column is narrower than the
  1690. ellipses string, only part of the ellipses string will be shown."
  1691. :group 'org-properties
  1692. :type 'string)
  1693. (defcustom org-columns-modify-value-for-display-function nil
  1694. "Function that modifies values for display in column view.
  1695. For example, it can be used to cut out a certain part from a time stamp.
  1696. The function must take 2 argments:
  1697. column-title The tite of the column (*not* the property name)
  1698. value The value that should be modified.
  1699. The function should return the value that should be displayed,
  1700. or nil if the normal value should be used."
  1701. :group 'org-properties
  1702. :type 'function)
  1703. (defcustom org-effort-property "Effort"
  1704. "The property that is being used to keep track of effort estimates.
  1705. Effort estimates given in this property need to have the format H:MM."
  1706. :group 'org-properties
  1707. :group 'org-progress
  1708. :type '(string :tag "Property"))
  1709. (defconst org-global-properties-fixed
  1710. '(("VISIBILITY_ALL" . "folded children content all"))
  1711. "List of property/value pairs that can be inherited by any entry.
  1712. These are fixed values, for the preset properties.")
  1713. (defcustom org-global-properties nil
  1714. "List of property/value pairs that can be inherited by any entry.
  1715. You can set buffer-local values for this by adding lines like
  1716. #+PROPERTY: NAME VALUE"
  1717. :group 'org-properties
  1718. :type '(repeat
  1719. (cons (string :tag "Property")
  1720. (string :tag "Value"))))
  1721. (defvar org-file-properties nil
  1722. "List of property/value pairs that can be inherited by any entry.
  1723. Valid for the current buffer.
  1724. This variable is populated from #+PROPERTY lines.")
  1725. (make-variable-buffer-local 'org-file-properties)
  1726. (defgroup org-agenda nil
  1727. "Options concerning agenda views in Org-mode."
  1728. :tag "Org Agenda"
  1729. :group 'org)
  1730. (defvar org-category nil
  1731. "Variable used by org files to set a category for agenda display.
  1732. Such files should use a file variable to set it, for example
  1733. # -*- mode: org; org-category: \"ELisp\"
  1734. or contain a special line
  1735. #+CATEGORY: ELisp
  1736. If the file does not specify a category, then file's base name
  1737. is used instead.")
  1738. (make-variable-buffer-local 'org-category)
  1739. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1740. (defcustom org-agenda-files nil
  1741. "The files to be used for agenda display.
  1742. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1743. \\[org-remove-file]. You can also use customize to edit the list.
  1744. If an entry is a directory, all files in that directory that are matched by
  1745. `org-agenda-file-regexp' will be part of the file list.
  1746. If the value of the variable is not a list but a single file name, then
  1747. the list of agenda files is actually stored and maintained in that file, one
  1748. agenda file per line."
  1749. :group 'org-agenda
  1750. :type '(choice
  1751. (repeat :tag "List of files and directories" file)
  1752. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1753. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1754. "Regular expression to match files for `org-agenda-files'.
  1755. If any element in the list in that variable contains a directory instead
  1756. of a normal file, all files in that directory that are matched by this
  1757. regular expression will be included."
  1758. :group 'org-agenda
  1759. :type 'regexp)
  1760. (defcustom org-agenda-text-search-extra-files nil
  1761. "List of extra files to be searched by text search commands.
  1762. These files will be search in addition to the agenda files by the
  1763. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1764. Note that these files will only be searched for text search commands,
  1765. not for the other agenda views like todo lists, tag searches or the weekly
  1766. agenda. This variable is intended to list notes and possibly archive files
  1767. that should also be searched by these two commands.
  1768. In fact, if the first element in the list is the symbol `agenda-archives',
  1769. than all archive files of all agenda files will be added to the search
  1770. scope."
  1771. :group 'org-agenda
  1772. :type '(set :greedy t
  1773. (const :tag "Agenda Archives" agenda-archives)
  1774. (repeat :inline t (file))))
  1775. (if (fboundp 'defvaralias)
  1776. (defvaralias 'org-agenda-multi-occur-extra-files
  1777. 'org-agenda-text-search-extra-files))
  1778. (defcustom org-agenda-skip-unavailable-files nil
  1779. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1780. A nil value means to remove them, after a query, from the list."
  1781. :group 'org-agenda
  1782. :type 'boolean)
  1783. (defcustom org-calendar-to-agenda-key [?c]
  1784. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1785. The command `org-calendar-goto-agenda' will be bound to this key. The
  1786. default is the character `c' because then `c' can be used to switch back and
  1787. forth between agenda and calendar."
  1788. :group 'org-agenda
  1789. :type 'sexp)
  1790. (defcustom org-calendar-agenda-action-key [?k]
  1791. "The key to be installed in `calendar-mode-map' for agenda-action.
  1792. The command `org-agenda-action' will be bound to this key. The
  1793. default is the character `k' because we use the same key in the agenda."
  1794. :group 'org-agenda
  1795. :type 'sexp)
  1796. (eval-after-load "calendar"
  1797. '(progn
  1798. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1799. 'org-calendar-goto-agenda)
  1800. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1801. 'org-agenda-action)))
  1802. (defgroup org-latex nil
  1803. "Options for embedding LaTeX code into Org-mode."
  1804. :tag "Org LaTeX"
  1805. :group 'org)
  1806. (defcustom org-format-latex-options
  1807. '(:foreground default :background default :scale 1.0
  1808. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1809. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1810. "Options for creating images from LaTeX fragments.
  1811. This is a property list with the following properties:
  1812. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1813. `default' means use the foreground of the default face.
  1814. :background the background color, or \"Transparent\".
  1815. `default' means use the background of the default face.
  1816. :scale a scaling factor for the size of the images.
  1817. :html-foreground, :html-background, :html-scale
  1818. the same numbers for HTML export.
  1819. :matchers a list indicating which matchers should be used to
  1820. find LaTeX fragments. Valid members of this list are:
  1821. \"begin\" find environments
  1822. \"$\" find math expressions surrounded by $...$
  1823. \"$$\" find math expressions surrounded by $$....$$
  1824. \"\\(\" find math expressions surrounded by \\(...\\)
  1825. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1826. :group 'org-latex
  1827. :type 'plist)
  1828. (defcustom org-format-latex-header "\\documentclass{article}
  1829. \\usepackage{fullpage} % do not remove
  1830. \\usepackage{amssymb}
  1831. \\usepackage[usenames]{color}
  1832. \\usepackage{amsmath}
  1833. \\usepackage{latexsym}
  1834. \\usepackage[mathscr]{eucal}
  1835. \\pagestyle{empty} % do not remove"
  1836. "The document header used for processing LaTeX fragments."
  1837. :group 'org-latex
  1838. :type 'string)
  1839. (defgroup org-font-lock nil
  1840. "Font-lock settings for highlighting in Org-mode."
  1841. :tag "Org Font Lock"
  1842. :group 'org)
  1843. (defcustom org-level-color-stars-only nil
  1844. "Non-nil means fontify only the stars in each headline.
  1845. When nil, the entire headline is fontified.
  1846. Changing it requires restart of `font-lock-mode' to become effective
  1847. also in regions already fontified."
  1848. :group 'org-font-lock
  1849. :type 'boolean)
  1850. (defcustom org-hide-leading-stars nil
  1851. "Non-nil means, hide the first N-1 stars in a headline.
  1852. This works by using the face `org-hide' for these stars. This
  1853. face is white for a light background, and black for a dark
  1854. background. You may have to customize the face `org-hide' to
  1855. make this work.
  1856. Changing it requires restart of `font-lock-mode' to become effective
  1857. also in regions already fontified.
  1858. You may also set this on a per-file basis by adding one of the following
  1859. lines to the buffer:
  1860. #+STARTUP: hidestars
  1861. #+STARTUP: showstars"
  1862. :group 'org-font-lock
  1863. :type 'boolean)
  1864. (defcustom org-fontify-done-headline nil
  1865. "Non-nil means, change the face of a headline if it is marked DONE.
  1866. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1867. When this is non-nil, the headline after the keyword is set to the
  1868. `org-headline-done' as an additional indication."
  1869. :group 'org-font-lock
  1870. :type 'boolean)
  1871. (defcustom org-fontify-emphasized-text t
  1872. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1873. Changing this variable requires a restart of Emacs to take effect."
  1874. :group 'org-font-lock
  1875. :type 'boolean)
  1876. (defcustom org-highlight-latex-fragments-and-specials nil
  1877. "Non-nil means, fontify what is treated specially by the exporters."
  1878. :group 'org-font-lock
  1879. :type 'boolean)
  1880. (defcustom org-hide-emphasis-markers nil
  1881. "Non-nil mean font-lock should hide the emphasis marker characters."
  1882. :group 'org-font-lock
  1883. :type 'boolean)
  1884. (defvar org-emph-re nil
  1885. "Regular expression for matching emphasis.")
  1886. (defvar org-verbatim-re nil
  1887. "Regular expression for matching verbatim text.")
  1888. (defvar org-emphasis-regexp-components) ; defined just below
  1889. (defvar org-emphasis-alist) ; defined just below
  1890. (defun org-set-emph-re (var val)
  1891. "Set variable and compute the emphasis regular expression."
  1892. (set var val)
  1893. (when (and (boundp 'org-emphasis-alist)
  1894. (boundp 'org-emphasis-regexp-components)
  1895. org-emphasis-alist org-emphasis-regexp-components)
  1896. (let* ((e org-emphasis-regexp-components)
  1897. (pre (car e))
  1898. (post (nth 1 e))
  1899. (border (nth 2 e))
  1900. (body (nth 3 e))
  1901. (nl (nth 4 e))
  1902. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1903. (body1 (concat body "*?"))
  1904. (markers (mapconcat 'car org-emphasis-alist ""))
  1905. (vmarkers (mapconcat
  1906. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1907. org-emphasis-alist "")))
  1908. ;; make sure special characters appear at the right position in the class
  1909. (if (string-match "\\^" markers)
  1910. (setq markers (concat (replace-match "" t t markers) "^")))
  1911. (if (string-match "-" markers)
  1912. (setq markers (concat (replace-match "" t t markers) "-")))
  1913. (if (string-match "\\^" vmarkers)
  1914. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1915. (if (string-match "-" vmarkers)
  1916. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1917. (if (> nl 0)
  1918. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1919. (int-to-string nl) "\\}")))
  1920. ;; Make the regexp
  1921. (setq org-emph-re
  1922. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1923. "\\("
  1924. "\\([" markers "]\\)"
  1925. "\\("
  1926. "[^" border "]\\|"
  1927. "[^" border (if (and nil stacked) markers) "]"
  1928. body1
  1929. "[^" border (if (and nil stacked) markers) "]"
  1930. "\\)"
  1931. "\\3\\)"
  1932. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1933. (setq org-verbatim-re
  1934. (concat "\\([" pre "]\\|^\\)"
  1935. "\\("
  1936. "\\([" vmarkers "]\\)"
  1937. "\\("
  1938. "[^" border "]\\|"
  1939. "[^" border "]"
  1940. body1
  1941. "[^" border "]"
  1942. "\\)"
  1943. "\\3\\)"
  1944. "\\([" post "]\\|$\\)")))))
  1945. (defcustom org-emphasis-regexp-components
  1946. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1947. "Components used to build the regular expression for emphasis.
  1948. This is a list with 6 entries. Terminology: In an emphasis string
  1949. like \" *strong word* \", we call the initial space PREMATCH, the final
  1950. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1951. and \"trong wor\" is the body. The different components in this variable
  1952. specify what is allowed/forbidden in each part:
  1953. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1954. post Chars allowed as postmatch. End of line will be allowed too.
  1955. border The chars *forbidden* as border characters.
  1956. body-regexp A regexp like \".\" to match a body character. Don't use
  1957. non-shy groups here, and don't allow newline here.
  1958. newline The maximum number of newlines allowed in an emphasis exp.
  1959. Use customize to modify this, or restart Emacs after changing it."
  1960. :group 'org-font-lock
  1961. :set 'org-set-emph-re
  1962. :type '(list
  1963. (sexp :tag "Allowed chars in pre ")
  1964. (sexp :tag "Allowed chars in post ")
  1965. (sexp :tag "Forbidden chars in border ")
  1966. (sexp :tag "Regexp for body ")
  1967. (integer :tag "number of newlines allowed")
  1968. (option (boolean :tag "Please ignore this button"))))
  1969. (defcustom org-emphasis-alist
  1970. `(("*" bold "<b>" "</b>")
  1971. ("/" italic "<i>" "</i>")
  1972. ("_" underline "<u>" "</u>")
  1973. ("=" org-code "<code>" "</code>" verbatim)
  1974. ("~" org-verbatim "" "" verbatim)
  1975. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1976. "<del>" "</del>")
  1977. )
  1978. "Special syntax for emphasized text.
  1979. Text starting and ending with a special character will be emphasized, for
  1980. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1981. characters, the face to be used by font-lock for highlighting in Org-mode
  1982. Emacs buffers, and the HTML tags to be used for this.
  1983. Use customize to modify this, or restart Emacs after changing it."
  1984. :group 'org-font-lock
  1985. :set 'org-set-emph-re
  1986. :type '(repeat
  1987. (list
  1988. (string :tag "Marker character")
  1989. (choice
  1990. (face :tag "Font-lock-face")
  1991. (plist :tag "Face property list"))
  1992. (string :tag "HTML start tag")
  1993. (string :tag "HTML end tag")
  1994. (option (const verbatim)))))
  1995. ;;; Miscellaneous options
  1996. (defgroup org-completion nil
  1997. "Completion in Org-mode."
  1998. :tag "Org Completion"
  1999. :group 'org)
  2000. (defcustom org-completion-fallback-command 'hippie-expand
  2001. "The expansion command called by \\[org-complete] in normal context.
  2002. Normal means, no org-mode-specific context."
  2003. :group 'org-completion
  2004. :type 'function)
  2005. ;;; Functions and variables from ther packages
  2006. ;; Declared here to avoid compiler warnings
  2007. ;; XEmacs only
  2008. (defvar outline-mode-menu-heading)
  2009. (defvar outline-mode-menu-show)
  2010. (defvar outline-mode-menu-hide)
  2011. (defvar zmacs-regions) ; XEmacs regions
  2012. ;; Emacs only
  2013. (defvar mark-active)
  2014. ;; Various packages
  2015. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2016. (declare-function calendar-forward-day "cal-move" (arg))
  2017. (declare-function calendar-goto-date "cal-move" (date))
  2018. (declare-function calendar-goto-today "cal-move" ())
  2019. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2020. (defvar calc-embedded-close-formula)
  2021. (defvar calc-embedded-open-formula)
  2022. (declare-function cdlatex-tab "ext:cdlatex" ())
  2023. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2024. (defvar font-lock-unfontify-region-function)
  2025. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2026. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2027. (defvar iswitchb-temp-buflist)
  2028. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2029. (declare-function org-agenda-skip "org-agenda" ())
  2030. (declare-function org-format-agenda-item "org-agenda"
  2031. (extra txt &optional category tags dotime noprefix remove-re))
  2032. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2033. (declare-function org-agenda-change-all-lines "org-agenda"
  2034. (newhead hdmarker &optional fixface))
  2035. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2036. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2037. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2038. (beg end))
  2039. (declare-function parse-time-string "parse-time" (string))
  2040. (declare-function remember "remember" (&optional initial))
  2041. (declare-function remember-buffer-desc "remember" ())
  2042. (declare-function remember-finalize "remember" ())
  2043. (defvar remember-save-after-remembering)
  2044. (defvar remember-data-file)
  2045. (defvar remember-register)
  2046. (defvar remember-buffer)
  2047. (defvar remember-handler-functions)
  2048. (defvar remember-annotation-functions)
  2049. (defvar texmathp-why)
  2050. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2051. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2052. (defvar w3m-current-url)
  2053. (defvar w3m-current-title)
  2054. (defvar org-latex-regexps)
  2055. ;;; Autoload and prepare some org modules
  2056. ;; Some table stuff that needs to be defined here, because it is used
  2057. ;; by the functions setting up org-mode or checking for table context.
  2058. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2059. "Detects an org-type or table-type table.")
  2060. (defconst org-table-line-regexp "^[ \t]*|"
  2061. "Detects an org-type table line.")
  2062. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2063. "Detects an org-type table line.")
  2064. (defconst org-table-hline-regexp "^[ \t]*|-"
  2065. "Detects an org-type table hline.")
  2066. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2067. "Detects a table-type table hline.")
  2068. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2069. "Searching from within a table (any type) this finds the first line
  2070. outside the table.")
  2071. ;; Autoload the functions in org-table.el that are needed by functions here.
  2072. (eval-and-compile
  2073. (org-autoload "org-table"
  2074. '(org-table-align org-table-begin org-table-blank-field
  2075. org-table-convert org-table-convert-region org-table-copy-down
  2076. org-table-copy-region org-table-create
  2077. org-table-create-or-convert-from-region
  2078. org-table-create-with-table.el org-table-current-dline
  2079. org-table-cut-region org-table-delete-column org-table-edit-field
  2080. org-table-edit-formulas org-table-end org-table-eval-formula
  2081. org-table-export org-table-field-info
  2082. org-table-get-stored-formulas org-table-goto-column
  2083. org-table-hline-and-move org-table-import org-table-insert-column
  2084. org-table-insert-hline org-table-insert-row org-table-iterate
  2085. org-table-justify-field-maybe org-table-kill-row
  2086. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2087. org-table-move-column org-table-move-column-left
  2088. org-table-move-column-right org-table-move-row
  2089. org-table-move-row-down org-table-move-row-up
  2090. org-table-next-field org-table-next-row org-table-paste-rectangle
  2091. org-table-previous-field org-table-recalculate
  2092. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2093. org-table-toggle-coordinate-overlays
  2094. org-table-toggle-formula-debugger org-table-wrap-region
  2095. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2096. (defun org-at-table-p (&optional table-type)
  2097. "Return t if the cursor is inside an org-type table.
  2098. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2099. (if org-enable-table-editor
  2100. (save-excursion
  2101. (beginning-of-line 1)
  2102. (looking-at (if table-type org-table-any-line-regexp
  2103. org-table-line-regexp)))
  2104. nil))
  2105. (defsubst org-table-p () (org-at-table-p))
  2106. (defun org-at-table.el-p ()
  2107. "Return t if and only if we are at a table.el table."
  2108. (and (org-at-table-p 'any)
  2109. (save-excursion
  2110. (goto-char (org-table-begin 'any))
  2111. (looking-at org-table1-hline-regexp))))
  2112. (defun org-table-recognize-table.el ()
  2113. "If there is a table.el table nearby, recognize it and move into it."
  2114. (if org-table-tab-recognizes-table.el
  2115. (if (org-at-table.el-p)
  2116. (progn
  2117. (beginning-of-line 1)
  2118. (if (looking-at org-table-dataline-regexp)
  2119. nil
  2120. (if (looking-at org-table1-hline-regexp)
  2121. (progn
  2122. (beginning-of-line 2)
  2123. (if (looking-at org-table-any-border-regexp)
  2124. (beginning-of-line -1)))))
  2125. (if (re-search-forward "|" (org-table-end t) t)
  2126. (progn
  2127. (require 'table)
  2128. (if (table--at-cell-p (point))
  2129. t
  2130. (message "recognizing table.el table...")
  2131. (table-recognize-table)
  2132. (message "recognizing table.el table...done")))
  2133. (error "This should not happen..."))
  2134. t)
  2135. nil)
  2136. nil))
  2137. (defun org-at-table-hline-p ()
  2138. "Return t if the cursor is inside a hline in a table."
  2139. (if org-enable-table-editor
  2140. (save-excursion
  2141. (beginning-of-line 1)
  2142. (looking-at org-table-hline-regexp))
  2143. nil))
  2144. (defvar org-table-clean-did-remove-column nil)
  2145. (defun org-table-map-tables (function)
  2146. "Apply FUNCTION to the start of all tables in the buffer."
  2147. (save-excursion
  2148. (save-restriction
  2149. (widen)
  2150. (goto-char (point-min))
  2151. (while (re-search-forward org-table-any-line-regexp nil t)
  2152. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2153. (beginning-of-line 1)
  2154. (if (looking-at org-table-line-regexp)
  2155. (save-excursion (funcall function)))
  2156. (re-search-forward org-table-any-border-regexp nil 1))))
  2157. (message "Mapping tables: done"))
  2158. ;; Declare and autoload functions from org-exp.el
  2159. (declare-function org-default-export-plist "org-exp")
  2160. (declare-function org-infile-export-plist "org-exp")
  2161. (declare-function org-get-current-options "org-exp")
  2162. (eval-and-compile
  2163. (org-autoload "org-exp"
  2164. '(org-export org-export-as-ascii org-export-visible
  2165. org-insert-export-options-template org-export-as-html-and-open
  2166. org-export-as-html-batch org-export-as-html-to-buffer
  2167. org-replace-region-by-html org-export-region-as-html
  2168. org-export-as-html org-export-icalendar-this-file
  2169. org-export-icalendar-all-agenda-files
  2170. org-table-clean-before-export
  2171. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2172. ;; Declare and autoload functions from org-agenda.el
  2173. (eval-and-compile
  2174. (org-autoload "org-agenda"
  2175. '(org-agenda org-agenda-list org-search-view
  2176. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2177. org-diary org-agenda-to-appt)))
  2178. ;; Autoload org-remember
  2179. (eval-and-compile
  2180. (org-autoload "org-remember"
  2181. '(org-remember-insinuate org-remember-annotation
  2182. org-remember-apply-template org-remember org-remember-handler)))
  2183. ;; Autoload org-clock.el
  2184. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2185. (beg end))
  2186. (declare-function org-update-mode-line "org-clock" ())
  2187. (defvar org-clock-start-time)
  2188. (defvar org-clock-marker (make-marker)
  2189. "Marker recording the last clock-in.")
  2190. (eval-and-compile
  2191. (org-autoload
  2192. "org-clock"
  2193. '(org-clock-in org-clock-out org-clock-cancel
  2194. org-clock-goto org-clock-sum org-clock-display
  2195. org-remove-clock-overlays org-clock-report
  2196. org-clocktable-shift org-dblock-write:clocktable
  2197. org-get-clocktable)))
  2198. (defun org-clock-update-time-maybe ()
  2199. "If this is a CLOCK line, update it and return t.
  2200. Otherwise, return nil."
  2201. (interactive)
  2202. (save-excursion
  2203. (beginning-of-line 1)
  2204. (skip-chars-forward " \t")
  2205. (when (looking-at org-clock-string)
  2206. (let ((re (concat "[ \t]*" org-clock-string
  2207. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2208. "\\([ \t]*=>.*\\)?\\)?"))
  2209. ts te h m s)
  2210. (cond
  2211. ((not (looking-at re))
  2212. nil)
  2213. ((not (match-end 2))
  2214. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2215. (> org-clock-marker (point))
  2216. (<= org-clock-marker (point-at-eol)))
  2217. ;; The clock is running here
  2218. (setq org-clock-start-time
  2219. (apply 'encode-time
  2220. (org-parse-time-string (match-string 1))))
  2221. (org-update-mode-line)))
  2222. (t
  2223. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2224. (end-of-line 1)
  2225. (setq ts (match-string 1)
  2226. te (match-string 3))
  2227. (setq s (- (time-to-seconds
  2228. (apply 'encode-time (org-parse-time-string te)))
  2229. (time-to-seconds
  2230. (apply 'encode-time (org-parse-time-string ts))))
  2231. h (floor (/ s 3600))
  2232. s (- s (* 3600 h))
  2233. m (floor (/ s 60))
  2234. s (- s (* 60 s)))
  2235. (insert " => " (format "%2d:%02d" h m))
  2236. t))))))
  2237. (defun org-check-running-clock ()
  2238. "Check if the current buffer contains the running clock.
  2239. If yes, offer to stop it and to save the buffer with the changes."
  2240. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2241. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2242. (buffer-name))))
  2243. (org-clock-out)
  2244. (when (y-or-n-p "Save changed buffer?")
  2245. (save-buffer))))
  2246. (defun org-clocktable-try-shift (dir n)
  2247. "Check if this line starts a clock table, if yes, shift the time block."
  2248. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2249. (org-clocktable-shift dir n)))
  2250. ;; Autoload archiving code
  2251. ;; The stuff that is needed for cycling and tags has to be defined here.
  2252. (defgroup org-archive nil
  2253. "Options concerning archiving in Org-mode."
  2254. :tag "Org Archive"
  2255. :group 'org-structure)
  2256. (defcustom org-archive-location "%s_archive::"
  2257. "The location where subtrees should be archived.
  2258. Otherwise, the value of this variable is a string, consisting of two
  2259. parts, separated by a double-colon.
  2260. The first part is a file name - when omitted, archiving happens in the same
  2261. file. %s will be replaced by the current file name (without directory part).
  2262. Archiving to a different file is useful to keep archived entries from
  2263. contributing to the Org-mode Agenda.
  2264. The part after the double colon is a headline. The archived entries will be
  2265. filed under that headline. When omitted, the subtrees are simply filed away
  2266. at the end of the file, as top-level entries.
  2267. Here are a few examples:
  2268. \"%s_archive::\"
  2269. If the current file is Projects.org, archive in file
  2270. Projects.org_archive, as top-level trees. This is the default.
  2271. \"::* Archived Tasks\"
  2272. Archive in the current file, under the top-level headline
  2273. \"* Archived Tasks\".
  2274. \"~/org/archive.org::\"
  2275. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2276. \"basement::** Finished Tasks\"
  2277. Archive in file ./basement (relative path), as level 3 trees
  2278. below the level 2 heading \"** Finished Tasks\".
  2279. You may set this option on a per-file basis by adding to the buffer a
  2280. line like
  2281. #+ARCHIVE: basement::** Finished Tasks
  2282. You may also define it locally for a subtree by setting an ARCHIVE property
  2283. in the entry. If such a property is found in an entry, or anywhere up
  2284. the hierarchy, it will be used."
  2285. :group 'org-archive
  2286. :type 'string)
  2287. (defcustom org-archive-tag "ARCHIVE"
  2288. "The tag that marks a subtree as archived.
  2289. An archived subtree does not open during visibility cycling, and does
  2290. not contribute to the agenda listings.
  2291. After changing this, font-lock must be restarted in the relevant buffers to
  2292. get the proper fontification."
  2293. :group 'org-archive
  2294. :group 'org-keywords
  2295. :type 'string)
  2296. (defcustom org-agenda-skip-archived-trees t
  2297. "Non-nil means, the agenda will skip any items located in archived trees.
  2298. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2299. variable is no longer recommended, you should leave it at the value t.
  2300. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2301. :group 'org-archive
  2302. :group 'org-agenda-skip
  2303. :type 'boolean)
  2304. (defcustom org-cycle-open-archived-trees nil
  2305. "Non-nil means, `org-cycle' will open archived trees.
  2306. An archived tree is a tree marked with the tag ARCHIVE.
  2307. When nil, archived trees will stay folded. You can still open them with
  2308. normal outline commands like `show-all', but not with the cycling commands."
  2309. :group 'org-archive
  2310. :group 'org-cycle
  2311. :type 'boolean)
  2312. (defcustom org-sparse-tree-open-archived-trees nil
  2313. "Non-nil means sparse tree construction shows matches in archived trees.
  2314. When nil, matches in these trees are highlighted, but the trees are kept in
  2315. collapsed state."
  2316. :group 'org-archive
  2317. :group 'org-sparse-trees
  2318. :type 'boolean)
  2319. (defun org-cycle-hide-archived-subtrees (state)
  2320. "Re-hide all archived subtrees after a visibility state change."
  2321. (when (and (not org-cycle-open-archived-trees)
  2322. (not (memq state '(overview folded))))
  2323. (save-excursion
  2324. (let* ((globalp (memq state '(contents all)))
  2325. (beg (if globalp (point-min) (point)))
  2326. (end (if globalp (point-max) (org-end-of-subtree t))))
  2327. (org-hide-archived-subtrees beg end)
  2328. (goto-char beg)
  2329. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2330. (message "%s" (substitute-command-keys
  2331. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2332. (defun org-force-cycle-archived ()
  2333. "Cycle subtree even if it is archived."
  2334. (interactive)
  2335. (setq this-command 'org-cycle)
  2336. (let ((org-cycle-open-archived-trees t))
  2337. (call-interactively 'org-cycle)))
  2338. (defun org-hide-archived-subtrees (beg end)
  2339. "Re-hide all archived subtrees after a visibility state change."
  2340. (save-excursion
  2341. (let* ((re (concat ":" org-archive-tag ":")))
  2342. (goto-char beg)
  2343. (while (re-search-forward re end t)
  2344. (and (org-on-heading-p) (hide-subtree))
  2345. (org-end-of-subtree t)))))
  2346. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2347. (eval-and-compile
  2348. (org-autoload "org-archive"
  2349. '(org-add-archive-files org-archive-subtree
  2350. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2351. ;; Autoload Column View Code
  2352. (declare-function org-columns-number-to-string "org-colview")
  2353. (declare-function org-columns-get-format-and-top-level "org-colview")
  2354. (declare-function org-columns-compute "org-colview")
  2355. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2356. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2357. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2358. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2359. ;; Autoload ID code
  2360. (org-autoload "org-id"
  2361. '(org-id-get-create org-id-new org-id-copy org-id-get
  2362. org-id-get-with-outline-path-completion
  2363. org-id-get-with-outline-drilling
  2364. org-id-goto org-id-find))
  2365. ;;; Variables for pre-computed regular expressions, all buffer local
  2366. (defvar org-drawer-regexp nil
  2367. "Matches first line of a hidden block.")
  2368. (make-variable-buffer-local 'org-drawer-regexp)
  2369. (defvar org-todo-regexp nil
  2370. "Matches any of the TODO state keywords.")
  2371. (make-variable-buffer-local 'org-todo-regexp)
  2372. (defvar org-not-done-regexp nil
  2373. "Matches any of the TODO state keywords except the last one.")
  2374. (make-variable-buffer-local 'org-not-done-regexp)
  2375. (defvar org-todo-line-regexp nil
  2376. "Matches a headline and puts TODO state into group 2 if present.")
  2377. (make-variable-buffer-local 'org-todo-line-regexp)
  2378. (defvar org-complex-heading-regexp nil
  2379. "Matches a headline and puts everything into groups:
  2380. group 1: the stars
  2381. group 2: The todo keyword, maybe
  2382. group 3: Priority cookie
  2383. group 4: True headline
  2384. group 5: Tags")
  2385. (make-variable-buffer-local 'org-complex-heading-regexp)
  2386. (defvar org-todo-line-tags-regexp nil
  2387. "Matches a headline and puts TODO state into group 2 if present.
  2388. Also put tags into group 4 if tags are present.")
  2389. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2390. (defvar org-nl-done-regexp nil
  2391. "Matches newline followed by a headline with the DONE keyword.")
  2392. (make-variable-buffer-local 'org-nl-done-regexp)
  2393. (defvar org-looking-at-done-regexp nil
  2394. "Matches the DONE keyword a point.")
  2395. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2396. (defvar org-ds-keyword-length 12
  2397. "Maximum length of the Deadline and SCHEDULED keywords.")
  2398. (make-variable-buffer-local 'org-ds-keyword-length)
  2399. (defvar org-deadline-regexp nil
  2400. "Matches the DEADLINE keyword.")
  2401. (make-variable-buffer-local 'org-deadline-regexp)
  2402. (defvar org-deadline-time-regexp nil
  2403. "Matches the DEADLINE keyword together with a time stamp.")
  2404. (make-variable-buffer-local 'org-deadline-time-regexp)
  2405. (defvar org-deadline-line-regexp nil
  2406. "Matches the DEADLINE keyword and the rest of the line.")
  2407. (make-variable-buffer-local 'org-deadline-line-regexp)
  2408. (defvar org-scheduled-regexp nil
  2409. "Matches the SCHEDULED keyword.")
  2410. (make-variable-buffer-local 'org-scheduled-regexp)
  2411. (defvar org-scheduled-time-regexp nil
  2412. "Matches the SCHEDULED keyword together with a time stamp.")
  2413. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2414. (defvar org-closed-time-regexp nil
  2415. "Matches the CLOSED keyword together with a time stamp.")
  2416. (make-variable-buffer-local 'org-closed-time-regexp)
  2417. (defvar org-keyword-time-regexp nil
  2418. "Matches any of the 4 keywords, together with the time stamp.")
  2419. (make-variable-buffer-local 'org-keyword-time-regexp)
  2420. (defvar org-keyword-time-not-clock-regexp nil
  2421. "Matches any of the 3 keywords, together with the time stamp.")
  2422. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2423. (defvar org-maybe-keyword-time-regexp nil
  2424. "Matches a timestamp, possibly preceeded by a keyword.")
  2425. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2426. (defvar org-planning-or-clock-line-re nil
  2427. "Matches a line with planning or clock info.")
  2428. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2429. (defconst org-plain-time-of-day-regexp
  2430. (concat
  2431. "\\(\\<[012]?[0-9]"
  2432. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2433. "\\(--?"
  2434. "\\(\\<[012]?[0-9]"
  2435. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2436. "\\)?")
  2437. "Regular expression to match a plain time or time range.
  2438. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2439. groups carry important information:
  2440. 0 the full match
  2441. 1 the first time, range or not
  2442. 8 the second time, if it is a range.")
  2443. (defconst org-plain-time-extension-regexp
  2444. (concat
  2445. "\\(\\<[012]?[0-9]"
  2446. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2447. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2448. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2449. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2450. groups carry important information:
  2451. 0 the full match
  2452. 7 hours of duration
  2453. 9 minutes of duration")
  2454. (defconst org-stamp-time-of-day-regexp
  2455. (concat
  2456. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2457. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2458. "\\(--?"
  2459. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2460. "Regular expression to match a timestamp time or time range.
  2461. After a match, the following groups carry important information:
  2462. 0 the full match
  2463. 1 date plus weekday, for backreferencing to make sure both times on same day
  2464. 2 the first time, range or not
  2465. 4 the second time, if it is a range.")
  2466. (defconst org-startup-options
  2467. '(("fold" org-startup-folded t)
  2468. ("overview" org-startup-folded t)
  2469. ("nofold" org-startup-folded nil)
  2470. ("showall" org-startup-folded nil)
  2471. ("content" org-startup-folded content)
  2472. ("hidestars" org-hide-leading-stars t)
  2473. ("showstars" org-hide-leading-stars nil)
  2474. ("odd" org-odd-levels-only t)
  2475. ("oddeven" org-odd-levels-only nil)
  2476. ("align" org-startup-align-all-tables t)
  2477. ("noalign" org-startup-align-all-tables nil)
  2478. ("customtime" org-display-custom-times t)
  2479. ("logdone" org-log-done time)
  2480. ("lognotedone" org-log-done note)
  2481. ("nologdone" org-log-done nil)
  2482. ("lognoteclock-out" org-log-note-clock-out t)
  2483. ("nolognoteclock-out" org-log-note-clock-out nil)
  2484. ("logrepeat" org-log-repeat state)
  2485. ("lognoterepeat" org-log-repeat note)
  2486. ("nologrepeat" org-log-repeat nil)
  2487. ("constcgs" constants-unit-system cgs)
  2488. ("constSI" constants-unit-system SI))
  2489. "Variable associated with STARTUP options for org-mode.
  2490. Each element is a list of three items: The startup options as written
  2491. in the #+STARTUP line, the corresponding variable, and the value to
  2492. set this variable to if the option is found. An optional forth element PUSH
  2493. means to push this value onto the list in the variable.")
  2494. (defun org-set-regexps-and-options ()
  2495. "Precompute regular expressions for current buffer."
  2496. (when (org-mode-p)
  2497. (org-set-local 'org-todo-kwd-alist nil)
  2498. (org-set-local 'org-todo-key-alist nil)
  2499. (org-set-local 'org-todo-key-trigger nil)
  2500. (org-set-local 'org-todo-keywords-1 nil)
  2501. (org-set-local 'org-done-keywords nil)
  2502. (org-set-local 'org-todo-heads nil)
  2503. (org-set-local 'org-todo-sets nil)
  2504. (org-set-local 'org-todo-log-states nil)
  2505. (org-set-local 'org-file-properties nil)
  2506. (org-set-local 'org-file-tags nil)
  2507. (let ((re (org-make-options-regexp
  2508. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2509. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2510. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2511. (splitre "[ \t]+")
  2512. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2513. tail sep kws1 prio props ftags drawers
  2514. ext-setup-or-nil setup-contents (start 0))
  2515. (save-excursion
  2516. (save-restriction
  2517. (widen)
  2518. (goto-char (point-min))
  2519. (while (or (and ext-setup-or-nil
  2520. (string-match re ext-setup-or-nil start)
  2521. (setq start (match-end 0)))
  2522. (and (setq ext-setup-or-nil nil start 0)
  2523. (re-search-forward re nil t)))
  2524. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2525. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2526. (cond
  2527. ((equal key "CATEGORY")
  2528. (if (string-match "[ \t]+$" value)
  2529. (setq value (replace-match "" t t value)))
  2530. (setq cat value))
  2531. ((member key '("SEQ_TODO" "TODO"))
  2532. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2533. ((equal key "TYP_TODO")
  2534. (push (cons 'type (org-split-string value splitre)) kwds))
  2535. ((equal key "TAGS")
  2536. (setq tags (append tags (org-split-string value splitre))))
  2537. ((equal key "COLUMNS")
  2538. (org-set-local 'org-columns-default-format value))
  2539. ((equal key "LINK")
  2540. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2541. (push (cons (match-string 1 value)
  2542. (org-trim (match-string 2 value)))
  2543. links)))
  2544. ((equal key "PRIORITIES")
  2545. (setq prio (org-split-string value " +")))
  2546. ((equal key "PROPERTY")
  2547. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2548. (push (cons (match-string 1 value) (match-string 2 value))
  2549. props)))
  2550. ((equal key "FILETAGS")
  2551. (when (string-match "\\S-" value)
  2552. (setq ftags
  2553. (append
  2554. ftags
  2555. (apply 'append
  2556. (mapcar (lambda (x) (org-split-string x ":"))
  2557. (org-split-string value)))))))
  2558. ((equal key "DRAWERS")
  2559. (setq drawers (org-split-string value splitre)))
  2560. ((equal key "CONSTANTS")
  2561. (setq const (append const (org-split-string value splitre))))
  2562. ((equal key "STARTUP")
  2563. (let ((opts (org-split-string value splitre))
  2564. l var val)
  2565. (while (setq l (pop opts))
  2566. (when (setq l (assoc l org-startup-options))
  2567. (setq var (nth 1 l) val (nth 2 l))
  2568. (if (not (nth 3 l))
  2569. (set (make-local-variable var) val)
  2570. (if (not (listp (symbol-value var)))
  2571. (set (make-local-variable var) nil))
  2572. (set (make-local-variable var) (symbol-value var))
  2573. (add-to-list var val))))))
  2574. ((equal key "ARCHIVE")
  2575. (string-match " *$" value)
  2576. (setq arch (replace-match "" t t value))
  2577. (remove-text-properties 0 (length arch)
  2578. '(face t fontified t) arch))
  2579. ((equal key "SETUPFILE")
  2580. (setq setup-contents (org-file-contents
  2581. (expand-file-name
  2582. (org-remove-double-quotes value))
  2583. 'noerror))
  2584. (if (not ext-setup-or-nil)
  2585. (setq ext-setup-or-nil setup-contents start 0)
  2586. (setq ext-setup-or-nil
  2587. (concat (substring ext-setup-or-nil 0 start)
  2588. "\n" setup-contents "\n"
  2589. (substring ext-setup-or-nil start)))))
  2590. ))))
  2591. (when cat
  2592. (org-set-local 'org-category (intern cat))
  2593. (push (cons "CATEGORY" cat) props))
  2594. (when prio
  2595. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2596. (setq prio (mapcar 'string-to-char prio))
  2597. (org-set-local 'org-highest-priority (nth 0 prio))
  2598. (org-set-local 'org-lowest-priority (nth 1 prio))
  2599. (org-set-local 'org-default-priority (nth 2 prio)))
  2600. (and props (org-set-local 'org-file-properties (nreverse props)))
  2601. (and ftags (org-set-local 'org-file-tags ftags))
  2602. (and drawers (org-set-local 'org-drawers drawers))
  2603. (and arch (org-set-local 'org-archive-location arch))
  2604. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2605. ;; Process the TODO keywords
  2606. (unless kwds
  2607. ;; Use the global values as if they had been given locally.
  2608. (setq kwds (default-value 'org-todo-keywords))
  2609. (if (stringp (car kwds))
  2610. (setq kwds (list (cons org-todo-interpretation
  2611. (default-value 'org-todo-keywords)))))
  2612. (setq kwds (reverse kwds)))
  2613. (setq kwds (nreverse kwds))
  2614. (let (inter kws kw)
  2615. (while (setq kws (pop kwds))
  2616. (setq inter (pop kws) sep (member "|" kws)
  2617. kws0 (delete "|" (copy-sequence kws))
  2618. kwsa nil
  2619. kws1 (mapcar
  2620. (lambda (x)
  2621. ;; 1 2
  2622. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2623. (progn
  2624. (setq kw (match-string 1 x)
  2625. key (and (match-end 2) (match-string 2 x))
  2626. log (org-extract-log-state-settings x))
  2627. (push (cons kw (and key (string-to-char key))) kwsa)
  2628. (and log (push log org-todo-log-states))
  2629. kw)
  2630. (error "Invalid TODO keyword %s" x)))
  2631. kws0)
  2632. kwsa (if kwsa (append '((:startgroup))
  2633. (nreverse kwsa)
  2634. '((:endgroup))))
  2635. hw (car kws1)
  2636. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2637. tail (list inter hw (car dws) (org-last dws)))
  2638. (add-to-list 'org-todo-heads hw 'append)
  2639. (push kws1 org-todo-sets)
  2640. (setq org-done-keywords (append org-done-keywords dws nil))
  2641. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2642. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2643. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2644. (setq org-todo-sets (nreverse org-todo-sets)
  2645. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2646. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2647. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2648. ;; Process the constants
  2649. (when const
  2650. (let (e cst)
  2651. (while (setq e (pop const))
  2652. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2653. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2654. (setq org-table-formula-constants-local cst)))
  2655. ;; Process the tags.
  2656. (when tags
  2657. (let (e tgs)
  2658. (while (setq e (pop tags))
  2659. (cond
  2660. ((equal e "{") (push '(:startgroup) tgs))
  2661. ((equal e "}") (push '(:endgroup) tgs))
  2662. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2663. (push (cons (match-string 1 e)
  2664. (string-to-char (match-string 2 e)))
  2665. tgs))
  2666. (t (push (list e) tgs))))
  2667. (org-set-local 'org-tag-alist nil)
  2668. (while (setq e (pop tgs))
  2669. (or (and (stringp (car e))
  2670. (assoc (car e) org-tag-alist))
  2671. (push e org-tag-alist)))))
  2672. ;; Compute the regular expressions and other local variables
  2673. (if (not org-done-keywords)
  2674. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2675. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2676. (length org-scheduled-string)
  2677. (length org-clock-string)
  2678. (length org-closed-string)))
  2679. org-drawer-regexp
  2680. (concat "^[ \t]*:\\("
  2681. (mapconcat 'regexp-quote org-drawers "\\|")
  2682. "\\):[ \t]*$")
  2683. org-not-done-keywords
  2684. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2685. org-todo-regexp
  2686. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2687. "\\|") "\\)\\>")
  2688. org-not-done-regexp
  2689. (concat "\\<\\("
  2690. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2691. "\\)\\>")
  2692. org-todo-line-regexp
  2693. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2694. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2695. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2696. org-complex-heading-regexp
  2697. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2698. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2699. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2700. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2701. org-nl-done-regexp
  2702. (concat "\n\\*+[ \t]+"
  2703. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2704. "\\)" "\\>")
  2705. org-todo-line-tags-regexp
  2706. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2707. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2708. (org-re
  2709. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2710. org-looking-at-done-regexp
  2711. (concat "^" "\\(?:"
  2712. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2713. "\\>")
  2714. org-deadline-regexp (concat "\\<" org-deadline-string)
  2715. org-deadline-time-regexp
  2716. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2717. org-deadline-line-regexp
  2718. (concat "\\<\\(" org-deadline-string "\\).*")
  2719. org-scheduled-regexp
  2720. (concat "\\<" org-scheduled-string)
  2721. org-scheduled-time-regexp
  2722. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2723. org-closed-time-regexp
  2724. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2725. org-keyword-time-regexp
  2726. (concat "\\<\\(" org-scheduled-string
  2727. "\\|" org-deadline-string
  2728. "\\|" org-closed-string
  2729. "\\|" org-clock-string "\\)"
  2730. " *[[<]\\([^]>]+\\)[]>]")
  2731. org-keyword-time-not-clock-regexp
  2732. (concat "\\<\\(" org-scheduled-string
  2733. "\\|" org-deadline-string
  2734. "\\|" org-closed-string
  2735. "\\)"
  2736. " *[[<]\\([^]>]+\\)[]>]")
  2737. org-maybe-keyword-time-regexp
  2738. (concat "\\(\\<\\(" org-scheduled-string
  2739. "\\|" org-deadline-string
  2740. "\\|" org-closed-string
  2741. "\\|" org-clock-string "\\)\\)?"
  2742. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2743. org-planning-or-clock-line-re
  2744. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2745. "\\|" org-deadline-string
  2746. "\\|" org-closed-string "\\|" org-clock-string
  2747. "\\)\\>\\)")
  2748. )
  2749. (org-compute-latex-and-specials-regexp)
  2750. (org-set-font-lock-defaults))))
  2751. (defun org-file-contents (file &optional noerror)
  2752. "Return the contents of FILE, as a string."
  2753. (if (or (not file)
  2754. (not (file-readable-p file)))
  2755. (if noerror
  2756. (progn
  2757. (message "Cannot read file %s" file)
  2758. (ding) (sit-for 2)
  2759. "")
  2760. (error "Cannot read file %s" file))
  2761. (with-temp-buffer
  2762. (insert-file-contents file)
  2763. (buffer-string))))
  2764. (defun org-extract-log-state-settings (x)
  2765. "Extract the log state setting from a TODO keyword string.
  2766. This will extract info from a string like \"WAIT(w@/!)\"."
  2767. (let (kw key log1 log2)
  2768. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2769. (setq kw (match-string 1 x)
  2770. key (and (match-end 2) (match-string 2 x))
  2771. log1 (and (match-end 3) (match-string 3 x))
  2772. log2 (and (match-end 4) (match-string 4 x)))
  2773. (and (or log1 log2)
  2774. (list kw
  2775. (and log1 (if (equal log1 "!") 'time 'note))
  2776. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2777. (defun org-remove-keyword-keys (list)
  2778. "Remove a pair of parenthesis at the end of each string in LIST."
  2779. (mapcar (lambda (x)
  2780. (if (string-match "(.*)$" x)
  2781. (substring x 0 (match-beginning 0))
  2782. x))
  2783. list))
  2784. ;; FIXME: this could be done much better, using second characters etc.
  2785. (defun org-assign-fast-keys (alist)
  2786. "Assign fast keys to a keyword-key alist.
  2787. Respect keys that are already there."
  2788. (let (new e k c c1 c2 (char ?a))
  2789. (while (setq e (pop alist))
  2790. (cond
  2791. ((equal e '(:startgroup)) (push e new))
  2792. ((equal e '(:endgroup)) (push e new))
  2793. (t
  2794. (setq k (car e) c2 nil)
  2795. (if (cdr e)
  2796. (setq c (cdr e))
  2797. ;; automatically assign a character.
  2798. (setq c1 (string-to-char
  2799. (downcase (substring
  2800. k (if (= (string-to-char k) ?@) 1 0)))))
  2801. (if (or (rassoc c1 new) (rassoc c1 alist))
  2802. (while (or (rassoc char new) (rassoc char alist))
  2803. (setq char (1+ char)))
  2804. (setq c2 c1))
  2805. (setq c (or c2 char)))
  2806. (push (cons k c) new))))
  2807. (nreverse new)))
  2808. ;;; Some variables used in various places
  2809. (defvar org-window-configuration nil
  2810. "Used in various places to store a window configuration.")
  2811. (defvar org-finish-function nil
  2812. "Function to be called when `C-c C-c' is used.
  2813. This is for getting out of special buffers like remember.")
  2814. ;; FIXME: Occasionally check by commenting these, to make sure
  2815. ;; no other functions uses these, forgetting to let-bind them.
  2816. (defvar entry)
  2817. (defvar state)
  2818. (defvar last-state)
  2819. (defvar date)
  2820. (defvar description)
  2821. ;; Defined somewhere in this file, but used before definition.
  2822. (defvar org-html-entities)
  2823. (defvar org-struct-menu)
  2824. (defvar org-org-menu)
  2825. (defvar org-tbl-menu)
  2826. (defvar org-agenda-keymap)
  2827. ;;;; Define the Org-mode
  2828. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2829. (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."))
  2830. ;; We use a before-change function to check if a table might need
  2831. ;; an update.
  2832. (defvar org-table-may-need-update t
  2833. "Indicates that a table might need an update.
  2834. This variable is set by `org-before-change-function'.
  2835. `org-table-align' sets it back to nil.")
  2836. (defun org-before-change-function (beg end)
  2837. "Every change indicates that a table might need an update."
  2838. (setq org-table-may-need-update t))
  2839. (defvar org-mode-map)
  2840. (defvar org-mode-hook nil)
  2841. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2842. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2843. (defvar org-table-buffer-is-an nil)
  2844. (defconst org-outline-regexp "\\*+ ")
  2845. ;;;###autoload
  2846. (define-derived-mode org-mode outline-mode "Org"
  2847. "Outline-based notes management and organizer, alias
  2848. \"Carsten's outline-mode for keeping track of everything.\"
  2849. Org-mode develops organizational tasks around a NOTES file which
  2850. contains information about projects as plain text. Org-mode is
  2851. implemented on top of outline-mode, which is ideal to keep the content
  2852. of large files well structured. It supports ToDo items, deadlines and
  2853. time stamps, which magically appear in the diary listing of the Emacs
  2854. calendar. Tables are easily created with a built-in table editor.
  2855. Plain text URL-like links connect to websites, emails (VM), Usenet
  2856. messages (Gnus), BBDB entries, and any files related to the project.
  2857. For printing and sharing of notes, an Org-mode file (or a part of it)
  2858. can be exported as a structured ASCII or HTML file.
  2859. The following commands are available:
  2860. \\{org-mode-map}"
  2861. ;; Get rid of Outline menus, they are not needed
  2862. ;; Need to do this here because define-derived-mode sets up
  2863. ;; the keymap so late. Still, it is a waste to call this each time
  2864. ;; we switch another buffer into org-mode.
  2865. (if (featurep 'xemacs)
  2866. (when (boundp 'outline-mode-menu-heading)
  2867. ;; Assume this is Greg's port, it used easymenu
  2868. (easy-menu-remove outline-mode-menu-heading)
  2869. (easy-menu-remove outline-mode-menu-show)
  2870. (easy-menu-remove outline-mode-menu-hide))
  2871. (define-key org-mode-map [menu-bar headings] 'undefined)
  2872. (define-key org-mode-map [menu-bar hide] 'undefined)
  2873. (define-key org-mode-map [menu-bar show] 'undefined))
  2874. (org-load-modules-maybe)
  2875. (easy-menu-add org-org-menu)
  2876. (easy-menu-add org-tbl-menu)
  2877. (org-install-agenda-files-menu)
  2878. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2879. (org-add-to-invisibility-spec '(org-cwidth))
  2880. (when (featurep 'xemacs)
  2881. (org-set-local 'line-move-ignore-invisible t))
  2882. (org-set-local 'outline-regexp org-outline-regexp)
  2883. (org-set-local 'outline-level 'org-outline-level)
  2884. (when (and org-ellipsis
  2885. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2886. (fboundp 'make-glyph-code))
  2887. (unless org-display-table
  2888. (setq org-display-table (make-display-table)))
  2889. (set-display-table-slot
  2890. org-display-table 4
  2891. (vconcat (mapcar
  2892. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2893. org-ellipsis)))
  2894. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2895. (setq buffer-display-table org-display-table))
  2896. (org-set-regexps-and-options)
  2897. ;; Calc embedded
  2898. (org-set-local 'calc-embedded-open-mode "# ")
  2899. (modify-syntax-entry ?# "<")
  2900. (modify-syntax-entry ?@ "w")
  2901. (if org-startup-truncated (setq truncate-lines t))
  2902. (org-set-local 'font-lock-unfontify-region-function
  2903. 'org-unfontify-region)
  2904. ;; Activate before-change-function
  2905. (org-set-local 'org-table-may-need-update t)
  2906. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2907. 'local)
  2908. ;; Check for running clock before killing a buffer
  2909. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2910. ;; Paragraphs and auto-filling
  2911. (org-set-autofill-regexps)
  2912. (setq indent-line-function 'org-indent-line-function)
  2913. (org-update-radio-target-regexp)
  2914. ;; Comment characters
  2915. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2916. (org-set-local 'comment-padding " ")
  2917. ;; Align options lines
  2918. (org-set-local
  2919. 'align-mode-rules-list
  2920. '((org-in-buffer-settings
  2921. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2922. (modes . '(org-mode)))))
  2923. ;; Imenu
  2924. (org-set-local 'imenu-create-index-function
  2925. 'org-imenu-get-tree)
  2926. ;; Make isearch reveal context
  2927. (if (or (featurep 'xemacs)
  2928. (not (boundp 'outline-isearch-open-invisible-function)))
  2929. ;; Emacs 21 and XEmacs make use of the hook
  2930. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2931. ;; Emacs 22 deals with this through a special variable
  2932. (org-set-local 'outline-isearch-open-invisible-function
  2933. (lambda (&rest ignore) (org-show-context 'isearch))))
  2934. ;; If empty file that did not turn on org-mode automatically, make it to.
  2935. (if (and org-insert-mode-line-in-empty-file
  2936. (interactive-p)
  2937. (= (point-min) (point-max)))
  2938. (insert "# -*- mode: org -*-\n\n"))
  2939. (unless org-inhibit-startup
  2940. (when org-startup-align-all-tables
  2941. (let ((bmp (buffer-modified-p)))
  2942. (org-table-map-tables 'org-table-align)
  2943. (set-buffer-modified-p bmp)))
  2944. (org-set-startup-visibility)))
  2945. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2946. (defun org-current-time ()
  2947. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2948. (if (> (car org-time-stamp-rounding-minutes) 1)
  2949. (let ((r (car org-time-stamp-rounding-minutes))
  2950. (time (decode-time)))
  2951. (apply 'encode-time
  2952. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2953. (nthcdr 2 time))))
  2954. (current-time)))
  2955. ;;;; Font-Lock stuff, including the activators
  2956. (defvar org-mouse-map (make-sparse-keymap))
  2957. (org-defkey org-mouse-map
  2958. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2959. (org-defkey org-mouse-map
  2960. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2961. (when org-mouse-1-follows-link
  2962. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2963. (when org-tab-follows-link
  2964. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2965. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2966. (when org-return-follows-link
  2967. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2968. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2969. (require 'font-lock)
  2970. (defconst org-non-link-chars "]\t\n\r<>")
  2971. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2972. "shell" "elisp"))
  2973. (defvar org-link-types-re nil
  2974. "Matches a link that has a url-like prefix like \"http:\"")
  2975. (defvar org-link-re-with-space nil
  2976. "Matches a link with spaces, optional angular brackets around it.")
  2977. (defvar org-link-re-with-space2 nil
  2978. "Matches a link with spaces, optional angular brackets around it.")
  2979. (defvar org-angle-link-re nil
  2980. "Matches link with angular brackets, spaces are allowed.")
  2981. (defvar org-plain-link-re nil
  2982. "Matches plain link, without spaces.")
  2983. (defvar org-bracket-link-regexp nil
  2984. "Matches a link in double brackets.")
  2985. (defvar org-bracket-link-analytic-regexp nil
  2986. "Regular expression used to analyze links.
  2987. Here is what the match groups contain after a match:
  2988. 1: http:
  2989. 2: http
  2990. 3: path
  2991. 4: [desc]
  2992. 5: desc")
  2993. (defvar org-any-link-re nil
  2994. "Regular expression matching any link.")
  2995. (defun org-make-link-regexps ()
  2996. "Update the link regular expressions.
  2997. This should be called after the variable `org-link-types' has changed."
  2998. (setq org-link-types-re
  2999. (concat
  3000. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3001. org-link-re-with-space
  3002. (concat
  3003. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3004. "\\([^" org-non-link-chars " ]"
  3005. "[^" org-non-link-chars "]*"
  3006. "[^" org-non-link-chars " ]\\)>?")
  3007. org-link-re-with-space2
  3008. (concat
  3009. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3010. "\\([^" org-non-link-chars " ]"
  3011. "[^]\t\n\r]*"
  3012. "[^" org-non-link-chars " ]\\)>?")
  3013. org-angle-link-re
  3014. (concat
  3015. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3016. "\\([^" org-non-link-chars " ]"
  3017. "[^" org-non-link-chars "]*"
  3018. "\\)>")
  3019. org-plain-link-re
  3020. (concat
  3021. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3022. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3023. org-bracket-link-regexp
  3024. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3025. org-bracket-link-analytic-regexp
  3026. (concat
  3027. "\\[\\["
  3028. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3029. "\\([^]]+\\)"
  3030. "\\]"
  3031. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3032. "\\]")
  3033. org-any-link-re
  3034. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3035. org-angle-link-re "\\)\\|\\("
  3036. org-plain-link-re "\\)")))
  3037. (org-make-link-regexps)
  3038. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3039. "Regular expression for fast time stamp matching.")
  3040. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3041. "Regular expression for fast time stamp matching.")
  3042. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3043. "Regular expression matching time strings for analysis.
  3044. This one does not require the space after the date, so it can be used
  3045. on a string that terminates immediately after the date.")
  3046. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3047. "Regular expression matching time strings for analysis.")
  3048. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3049. "Regular expression matching time stamps, with groups.")
  3050. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3051. "Regular expression matching time stamps (also [..]), with groups.")
  3052. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3053. "Regular expression matching a time stamp range.")
  3054. (defconst org-tr-regexp-both
  3055. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3056. "Regular expression matching a time stamp range.")
  3057. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3058. org-ts-regexp "\\)?")
  3059. "Regular expression matching a time stamp or time stamp range.")
  3060. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3061. org-ts-regexp-both "\\)?")
  3062. "Regular expression matching a time stamp or time stamp range.
  3063. The time stamps may be either active or inactive.")
  3064. (defvar org-emph-face nil)
  3065. (defun org-do-emphasis-faces (limit)
  3066. "Run through the buffer and add overlays to links."
  3067. (let (rtn)
  3068. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3069. (if (not (= (char-after (match-beginning 3))
  3070. (char-after (match-beginning 4))))
  3071. (progn
  3072. (setq rtn t)
  3073. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3074. 'face
  3075. (nth 1 (assoc (match-string 3)
  3076. org-emphasis-alist)))
  3077. (add-text-properties (match-beginning 2) (match-end 2)
  3078. '(font-lock-multiline t))
  3079. (when org-hide-emphasis-markers
  3080. (add-text-properties (match-end 4) (match-beginning 5)
  3081. '(invisible org-link))
  3082. (add-text-properties (match-beginning 3) (match-end 3)
  3083. '(invisible org-link)))))
  3084. (backward-char 1))
  3085. rtn))
  3086. (defun org-emphasize (&optional char)
  3087. "Insert or change an emphasis, i.e. a font like bold or italic.
  3088. If there is an active region, change that region to a new emphasis.
  3089. If there is no region, just insert the marker characters and position
  3090. the cursor between them.
  3091. CHAR should be either the marker character, or the first character of the
  3092. HTML tag associated with that emphasis. If CHAR is a space, the means
  3093. to remove the emphasis of the selected region.
  3094. If char is not given (for example in an interactive call) it
  3095. will be prompted for."
  3096. (interactive)
  3097. (let ((eal org-emphasis-alist) e det
  3098. (erc org-emphasis-regexp-components)
  3099. (prompt "")
  3100. (string "") beg end move tag c s)
  3101. (if (org-region-active-p)
  3102. (setq beg (region-beginning) end (region-end)
  3103. string (buffer-substring beg end))
  3104. (setq move t))
  3105. (while (setq e (pop eal))
  3106. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3107. c (aref tag 0))
  3108. (push (cons c (string-to-char (car e))) det)
  3109. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3110. (substring tag 1)))))
  3111. (unless char
  3112. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3113. (setq char (read-char-exclusive)))
  3114. (setq char (or (cdr (assoc char det)) char))
  3115. (if (equal char ?\ )
  3116. (setq s "" move nil)
  3117. (unless (assoc (char-to-string char) org-emphasis-alist)
  3118. (error "No such emphasis marker: \"%c\"" char))
  3119. (setq s (char-to-string char)))
  3120. (while (and (> (length string) 1)
  3121. (equal (substring string 0 1) (substring string -1))
  3122. (assoc (substring string 0 1) org-emphasis-alist))
  3123. (setq string (substring string 1 -1)))
  3124. (setq string (concat s string s))
  3125. (if beg (delete-region beg end))
  3126. (unless (or (bolp)
  3127. (string-match (concat "[" (nth 0 erc) "\n]")
  3128. (char-to-string (char-before (point)))))
  3129. (insert " "))
  3130. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3131. (char-to-string (char-after (point))))
  3132. (insert " ") (backward-char 1))
  3133. (insert string)
  3134. (and move (backward-char 1))))
  3135. (defconst org-nonsticky-props
  3136. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3137. (defun org-activate-plain-links (limit)
  3138. "Run through the buffer and add overlays to links."
  3139. (catch 'exit
  3140. (let (f)
  3141. (while (re-search-forward org-plain-link-re limit t)
  3142. (setq f (get-text-property (match-beginning 0) 'face))
  3143. (if (or (eq f 'org-tag)
  3144. (and (listp f) (memq 'org-tag f)))
  3145. nil
  3146. (add-text-properties (match-beginning 0) (match-end 0)
  3147. (list 'mouse-face 'highlight
  3148. 'rear-nonsticky org-nonsticky-props
  3149. 'keymap org-mouse-map
  3150. ))
  3151. (throw 'exit t))))))
  3152. (defun org-activate-code (limit)
  3153. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3154. (progn
  3155. (remove-text-properties (match-beginning 0) (match-end 0)
  3156. '(display t invisible t intangible t))
  3157. t)))
  3158. (defun org-activate-angle-links (limit)
  3159. "Run through the buffer and add overlays to links."
  3160. (if (re-search-forward org-angle-link-re limit t)
  3161. (progn
  3162. (add-text-properties (match-beginning 0) (match-end 0)
  3163. (list 'mouse-face 'highlight
  3164. 'rear-nonsticky org-nonsticky-props
  3165. 'keymap org-mouse-map
  3166. ))
  3167. t)))
  3168. (defun org-activate-bracket-links (limit)
  3169. "Run through the buffer and add overlays to bracketed links."
  3170. (if (re-search-forward org-bracket-link-regexp limit t)
  3171. (let* ((help (concat "LINK: "
  3172. (org-match-string-no-properties 1)))
  3173. ;; FIXME: above we should remove the escapes.
  3174. ;; but that requires another match, protecting match data,
  3175. ;; a lot of overhead for font-lock.
  3176. (ip (org-maybe-intangible
  3177. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3178. 'keymap org-mouse-map 'mouse-face 'highlight
  3179. 'font-lock-multiline t 'help-echo help)))
  3180. (vp (list 'rear-nonsticky org-nonsticky-props
  3181. 'keymap org-mouse-map 'mouse-face 'highlight
  3182. ' font-lock-multiline t 'help-echo help)))
  3183. ;; We need to remove the invisible property here. Table narrowing
  3184. ;; may have made some of this invisible.
  3185. (remove-text-properties (match-beginning 0) (match-end 0)
  3186. '(invisible nil))
  3187. (if (match-end 3)
  3188. (progn
  3189. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3190. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3191. (add-text-properties (match-end 3) (match-end 0) ip))
  3192. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3193. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3194. (add-text-properties (match-end 1) (match-end 0) ip))
  3195. t)))
  3196. (defun org-activate-dates (limit)
  3197. "Run through the buffer and add overlays to dates."
  3198. (if (re-search-forward org-tsr-regexp-both limit t)
  3199. (progn
  3200. (add-text-properties (match-beginning 0) (match-end 0)
  3201. (list 'mouse-face 'highlight
  3202. 'rear-nonsticky org-nonsticky-props
  3203. 'keymap org-mouse-map))
  3204. (when org-display-custom-times
  3205. (if (match-end 3)
  3206. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3207. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3208. t)))
  3209. (defvar org-target-link-regexp nil
  3210. "Regular expression matching radio targets in plain text.")
  3211. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3212. "Regular expression matching a link target.")
  3213. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3214. "Regular expression matching a radio target.")
  3215. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3216. "Regular expression matching any target.")
  3217. (defun org-activate-target-links (limit)
  3218. "Run through the buffer and add overlays to target matches."
  3219. (when org-target-link-regexp
  3220. (let ((case-fold-search t))
  3221. (if (re-search-forward org-target-link-regexp limit t)
  3222. (progn
  3223. (add-text-properties (match-beginning 0) (match-end 0)
  3224. (list 'mouse-face 'highlight
  3225. 'rear-nonsticky org-nonsticky-props
  3226. 'keymap org-mouse-map
  3227. 'help-echo "Radio target link"
  3228. 'org-linked-text t))
  3229. t)))))
  3230. (defun org-update-radio-target-regexp ()
  3231. "Find all radio targets in this file and update the regular expression."
  3232. (interactive)
  3233. (when (memq 'radio org-activate-links)
  3234. (setq org-target-link-regexp
  3235. (org-make-target-link-regexp (org-all-targets 'radio)))
  3236. (org-restart-font-lock)))
  3237. (defun org-hide-wide-columns (limit)
  3238. (let (s e)
  3239. (setq s (text-property-any (point) (or limit (point-max))
  3240. 'org-cwidth t))
  3241. (when s
  3242. (setq e (next-single-property-change s 'org-cwidth))
  3243. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3244. (goto-char e)
  3245. t)))
  3246. (defvar org-latex-and-specials-regexp nil
  3247. "Regular expression for highlighting export special stuff.")
  3248. (defvar org-match-substring-regexp)
  3249. (defvar org-match-substring-with-braces-regexp)
  3250. (defvar org-export-html-special-string-regexps)
  3251. (defun org-compute-latex-and-specials-regexp ()
  3252. "Compute regular expression for stuff treated specially by exporters."
  3253. (if (not org-highlight-latex-fragments-and-specials)
  3254. (org-set-local 'org-latex-and-specials-regexp nil)
  3255. (require 'org-exp)
  3256. (let*
  3257. ((matchers (plist-get org-format-latex-options :matchers))
  3258. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3259. org-latex-regexps)))
  3260. (options (org-combine-plists (org-default-export-plist)
  3261. (org-infile-export-plist)))
  3262. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3263. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3264. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3265. (org-export-html-expand (plist-get options :expand-quoted-html))
  3266. (org-export-with-special-strings (plist-get options :special-strings))
  3267. (re-sub
  3268. (cond
  3269. ((equal org-export-with-sub-superscripts '{})
  3270. (list org-match-substring-with-braces-regexp))
  3271. (org-export-with-sub-superscripts
  3272. (list org-match-substring-regexp))
  3273. (t nil)))
  3274. (re-latex
  3275. (if org-export-with-LaTeX-fragments
  3276. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3277. (re-macros
  3278. (if org-export-with-TeX-macros
  3279. (list (concat "\\\\"
  3280. (regexp-opt
  3281. (append (mapcar 'car org-html-entities)
  3282. (if (boundp 'org-latex-entities)
  3283. org-latex-entities nil))
  3284. 'words))) ; FIXME
  3285. ))
  3286. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3287. (re-special (if org-export-with-special-strings
  3288. (mapcar (lambda (x) (car x))
  3289. org-export-html-special-string-regexps)))
  3290. (re-rest
  3291. (delq nil
  3292. (list
  3293. (if org-export-html-expand "@<[^>\n]+>")
  3294. ))))
  3295. (org-set-local
  3296. 'org-latex-and-specials-regexp
  3297. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3298. re-rest) "\\|")))))
  3299. (defun org-do-latex-and-special-faces (limit)
  3300. "Run through the buffer and add overlays to links."
  3301. (when org-latex-and-specials-regexp
  3302. (let (rtn d)
  3303. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3304. limit t))
  3305. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3306. 'face))
  3307. '(org-code org-verbatim underline)))
  3308. (progn
  3309. (setq rtn t
  3310. d (cond ((member (char-after (1+ (match-beginning 0)))
  3311. '(?_ ?^)) 1)
  3312. (t 0)))
  3313. (font-lock-prepend-text-property
  3314. (+ d (match-beginning 0)) (match-end 0)
  3315. 'face 'org-latex-and-export-specials)
  3316. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3317. '(font-lock-multiline t)))))
  3318. rtn)))
  3319. (defun org-restart-font-lock ()
  3320. "Restart font-lock-mode, to force refontification."
  3321. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3322. (font-lock-mode -1)
  3323. (font-lock-mode 1)))
  3324. (defun org-all-targets (&optional radio)
  3325. "Return a list of all targets in this file.
  3326. With optional argument RADIO, only find radio targets."
  3327. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3328. rtn)
  3329. (save-excursion
  3330. (goto-char (point-min))
  3331. (while (re-search-forward re nil t)
  3332. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3333. rtn)))
  3334. (defun org-make-target-link-regexp (targets)
  3335. "Make regular expression matching all strings in TARGETS.
  3336. The regular expression finds the targets also if there is a line break
  3337. between words."
  3338. (and targets
  3339. (concat
  3340. "\\<\\("
  3341. (mapconcat
  3342. (lambda (x)
  3343. (while (string-match " +" x)
  3344. (setq x (replace-match "\\s-+" t t x)))
  3345. x)
  3346. targets
  3347. "\\|")
  3348. "\\)\\>")))
  3349. (defun org-activate-tags (limit)
  3350. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3351. (progn
  3352. (add-text-properties (match-beginning 1) (match-end 1)
  3353. (list 'mouse-face 'highlight
  3354. 'rear-nonsticky org-nonsticky-props
  3355. 'keymap org-mouse-map))
  3356. t)))
  3357. (defun org-outline-level ()
  3358. (save-excursion
  3359. (looking-at outline-regexp)
  3360. (if (match-beginning 1)
  3361. (+ (org-get-string-indentation (match-string 1)) 1000)
  3362. (1- (- (match-end 0) (match-beginning 0))))))
  3363. (defvar org-font-lock-keywords nil)
  3364. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3365. "Regular expression matching a property line.")
  3366. (defvar org-font-lock-hook nil
  3367. "Functions to be called for special font lock stuff.")
  3368. (defun org-font-lock-hook (limit)
  3369. (run-hook-with-args 'org-font-lock-hook limit))
  3370. (defun org-set-font-lock-defaults ()
  3371. (let* ((em org-fontify-emphasized-text)
  3372. (lk org-activate-links)
  3373. (org-font-lock-extra-keywords
  3374. (list
  3375. ;; Call the hook
  3376. '(org-font-lock-hook)
  3377. ;; Headlines
  3378. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3379. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3380. ;; Table lines
  3381. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3382. (1 'org-table t))
  3383. ;; Table internals
  3384. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3385. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3386. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3387. ;; Drawers
  3388. (list org-drawer-regexp '(0 'org-special-keyword t))
  3389. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3390. ;; Properties
  3391. (list org-property-re
  3392. '(1 'org-special-keyword t)
  3393. '(3 'org-property-value t))
  3394. (if org-format-transports-properties-p
  3395. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3396. ;; Links
  3397. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3398. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3399. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3400. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3401. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3402. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3403. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3404. '(org-hide-wide-columns (0 nil append))
  3405. ;; TODO lines
  3406. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3407. '(1 (org-get-todo-face 1) t))
  3408. ;; DONE
  3409. (if org-fontify-done-headline
  3410. (list (concat "^[*]+ +\\<\\("
  3411. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3412. "\\)\\(.*\\)")
  3413. '(2 'org-headline-done t))
  3414. nil)
  3415. ;; Priorities
  3416. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3417. ;; Special keywords
  3418. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3419. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3420. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3421. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3422. ;; Emphasis
  3423. (if em
  3424. (if (featurep 'xemacs)
  3425. '(org-do-emphasis-faces (0 nil append))
  3426. '(org-do-emphasis-faces)))
  3427. ;; Checkboxes
  3428. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3429. 2 'bold prepend)
  3430. (if org-provide-checkbox-statistics
  3431. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3432. (0 (org-get-checkbox-statistics-face) t)))
  3433. ;; Description list items
  3434. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3435. 2 'bold prepend)
  3436. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3437. '(1 'org-archived prepend))
  3438. ;; Specials
  3439. '(org-do-latex-and-special-faces)
  3440. ;; Code
  3441. '(org-activate-code (1 'org-code t))
  3442. ;; COMMENT
  3443. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3444. "\\|" org-quote-string "\\)\\>")
  3445. '(1 'org-special-keyword t))
  3446. '("^#.*" (0 'font-lock-comment-face t))
  3447. )))
  3448. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3449. ;; Now set the full font-lock-keywords
  3450. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3451. (org-set-local 'font-lock-defaults
  3452. '(org-font-lock-keywords t nil nil backward-paragraph))
  3453. (kill-local-variable 'font-lock-keywords) nil))
  3454. (defvar org-m nil)
  3455. (defvar org-l nil)
  3456. (defvar org-f nil)
  3457. (defun org-get-level-face (n)
  3458. "Get the right face for match N in font-lock matching of healdines."
  3459. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3460. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3461. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3462. (cond
  3463. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3464. ((eq n 2) org-f)
  3465. (t (if org-level-color-stars-only nil org-f))))
  3466. (defun org-get-todo-face (kwd)
  3467. "Get the right face for a TODO keyword KWD.
  3468. If KWD is a number, get the corresponding match group."
  3469. (if (numberp kwd) (setq kwd (match-string kwd)))
  3470. (or (cdr (assoc kwd org-todo-keyword-faces))
  3471. (and (member kwd org-done-keywords) 'org-done)
  3472. 'org-todo))
  3473. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3474. "Remove fontification and activation overlays from links."
  3475. (font-lock-default-unfontify-region beg end)
  3476. (let* ((buffer-undo-list t)
  3477. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3478. (inhibit-modification-hooks t)
  3479. deactivate-mark buffer-file-name buffer-file-truename)
  3480. (remove-text-properties beg end
  3481. '(mouse-face t keymap t org-linked-text t
  3482. invisible t intangible t))))
  3483. ;;;; Visibility cycling, including org-goto and indirect buffer
  3484. ;;; Cycling
  3485. (defvar org-cycle-global-status nil)
  3486. (make-variable-buffer-local 'org-cycle-global-status)
  3487. (defvar org-cycle-subtree-status nil)
  3488. (make-variable-buffer-local 'org-cycle-subtree-status)
  3489. ;;;###autoload
  3490. (defun org-cycle (&optional arg)
  3491. "Visibility cycling for Org-mode.
  3492. - When this function is called with a prefix argument, rotate the entire
  3493. buffer through 3 states (global cycling)
  3494. 1. OVERVIEW: Show only top-level headlines.
  3495. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3496. 3. SHOW ALL: Show everything.
  3497. When called with two C-u C-u prefixes, switch to the startup visibility,
  3498. determined by the variable `org-startup-folded', and by any VISIBILITY
  3499. properties in the buffer.
  3500. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3501. including drawers.
  3502. - When point is at the beginning of a headline, rotate the subtree started
  3503. by this line through 3 different states (local cycling)
  3504. 1. FOLDED: Only the main headline is shown.
  3505. 2. CHILDREN: The main headline and the direct children are shown.
  3506. From this state, you can move to one of the children
  3507. and zoom in further.
  3508. 3. SUBTREE: Show the entire subtree, including body text.
  3509. - When there is a numeric prefix, go up to a heading with level ARG, do
  3510. a `show-subtree' and return to the previous cursor position. If ARG
  3511. is negative, go up that many levels.
  3512. - When point is not at the beginning of a headline, execute the global
  3513. binding for TAB, which is re-indenting the line. See the option
  3514. `org-cycle-emulate-tab' for details.
  3515. - Special case: if point is at the beginning of the buffer and there is
  3516. no headline in line 1, this function will act as if called with prefix arg.
  3517. But only if also the variable `org-cycle-global-at-bob' is t."
  3518. (interactive "P")
  3519. (org-load-modules-maybe)
  3520. (let* ((outline-regexp
  3521. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3522. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3523. outline-regexp))
  3524. (bob-special (and org-cycle-global-at-bob (bobp)
  3525. (not (looking-at outline-regexp))))
  3526. (org-cycle-hook
  3527. (if bob-special
  3528. (delq 'org-optimize-window-after-visibility-change
  3529. (copy-sequence org-cycle-hook))
  3530. org-cycle-hook))
  3531. (pos (point)))
  3532. (if (or bob-special (equal arg '(4)))
  3533. ;; special case: use global cycling
  3534. (setq arg t))
  3535. (cond
  3536. ((equal arg '(16))
  3537. (org-set-startup-visibility)
  3538. (message "Startup visibility, plus VISIBILITY properties"))
  3539. ((equal arg '(64))
  3540. (show-all)
  3541. (message "Entire buffer visible, including drawers"))
  3542. ((org-at-table-p 'any)
  3543. ;; Enter the table or move to the next field in the table
  3544. (or (org-table-recognize-table.el)
  3545. (progn
  3546. (if arg (org-table-edit-field t)
  3547. (org-table-justify-field-maybe)
  3548. (call-interactively 'org-table-next-field)))))
  3549. ((eq arg t) ;; Global cycling
  3550. (cond
  3551. ((and (eq last-command this-command)
  3552. (eq org-cycle-global-status 'overview))
  3553. ;; We just created the overview - now do table of contents
  3554. ;; This can be slow in very large buffers, so indicate action
  3555. (message "CONTENTS...")
  3556. (org-content)
  3557. (message "CONTENTS...done")
  3558. (setq org-cycle-global-status 'contents)
  3559. (run-hook-with-args 'org-cycle-hook 'contents))
  3560. ((and (eq last-command this-command)
  3561. (eq org-cycle-global-status 'contents))
  3562. ;; We just showed the table of contents - now show everything
  3563. (show-all)
  3564. (message "SHOW ALL")
  3565. (setq org-cycle-global-status 'all)
  3566. (run-hook-with-args 'org-cycle-hook 'all))
  3567. (t
  3568. ;; Default action: go to overview
  3569. (org-overview)
  3570. (message "OVERVIEW")
  3571. (setq org-cycle-global-status 'overview)
  3572. (run-hook-with-args 'org-cycle-hook 'overview))))
  3573. ((and org-drawers org-drawer-regexp
  3574. (save-excursion
  3575. (beginning-of-line 1)
  3576. (looking-at org-drawer-regexp)))
  3577. ;; Toggle block visibility
  3578. (org-flag-drawer
  3579. (not (get-char-property (match-end 0) 'invisible))))
  3580. ((integerp arg)
  3581. ;; Show-subtree, ARG levels up from here.
  3582. (save-excursion
  3583. (org-back-to-heading)
  3584. (outline-up-heading (if (< arg 0) (- arg)
  3585. (- (funcall outline-level) arg)))
  3586. (org-show-subtree)))
  3587. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3588. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3589. ;; At a heading: rotate between three different views
  3590. (org-back-to-heading)
  3591. (let ((goal-column 0) eoh eol eos)
  3592. ;; First, some boundaries
  3593. (save-excursion
  3594. (org-back-to-heading)
  3595. (save-excursion
  3596. (beginning-of-line 2)
  3597. (while (and (not (eobp)) ;; this is like `next-line'
  3598. (get-char-property (1- (point)) 'invisible))
  3599. (beginning-of-line 2)) (setq eol (point)))
  3600. (outline-end-of-heading) (setq eoh (point))
  3601. (org-end-of-subtree t)
  3602. (unless (eobp)
  3603. (skip-chars-forward " \t\n")
  3604. (beginning-of-line 1) ; in case this is an item
  3605. )
  3606. (setq eos (1- (point))))
  3607. ;; Find out what to do next and set `this-command'
  3608. (cond
  3609. ((= eos eoh)
  3610. ;; Nothing is hidden behind this heading
  3611. (message "EMPTY ENTRY")
  3612. (setq org-cycle-subtree-status nil)
  3613. (save-excursion
  3614. (goto-char eos)
  3615. (outline-next-heading)
  3616. (if (org-invisible-p) (org-flag-heading nil))))
  3617. ((or (>= eol eos)
  3618. (not (string-match "\\S-" (buffer-substring eol eos))))
  3619. ;; Entire subtree is hidden in one line: open it
  3620. (org-show-entry)
  3621. (show-children)
  3622. (message "CHILDREN")
  3623. (save-excursion
  3624. (goto-char eos)
  3625. (outline-next-heading)
  3626. (if (org-invisible-p) (org-flag-heading nil)))
  3627. (setq org-cycle-subtree-status 'children)
  3628. (run-hook-with-args 'org-cycle-hook 'children))
  3629. ((and (eq last-command this-command)
  3630. (eq org-cycle-subtree-status 'children))
  3631. ;; We just showed the children, now show everything.
  3632. (org-show-subtree)
  3633. (message "SUBTREE")
  3634. (setq org-cycle-subtree-status 'subtree)
  3635. (run-hook-with-args 'org-cycle-hook 'subtree))
  3636. (t
  3637. ;; Default action: hide the subtree.
  3638. (hide-subtree)
  3639. (message "FOLDED")
  3640. (setq org-cycle-subtree-status 'folded)
  3641. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3642. ;; TAB emulation and template completion
  3643. (buffer-read-only (org-back-to-heading))
  3644. ((org-try-structure-completion))
  3645. ((org-try-cdlatex-tab))
  3646. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3647. (or (not (bolp))
  3648. (not (looking-at outline-regexp))))
  3649. (call-interactively (global-key-binding "\t")))
  3650. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3651. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3652. (or (and (eq org-cycle-emulate-tab 'white)
  3653. (= (match-end 0) (point-at-eol)))
  3654. (and (eq org-cycle-emulate-tab 'whitestart)
  3655. (>= (match-end 0) pos))))
  3656. t
  3657. (eq org-cycle-emulate-tab t))
  3658. (call-interactively (global-key-binding "\t")))
  3659. (t (save-excursion
  3660. (org-back-to-heading)
  3661. (org-cycle))))))
  3662. ;;;###autoload
  3663. (defun org-global-cycle (&optional arg)
  3664. "Cycle the global visibility. For details see `org-cycle'.
  3665. With C-u prefix arg, switch to startup visibility.
  3666. With a numeric prefix, show all headlines up to that level."
  3667. (interactive "P")
  3668. (let ((org-cycle-include-plain-lists
  3669. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3670. (cond
  3671. ((integerp arg)
  3672. (show-all)
  3673. (hide-sublevels arg)
  3674. (setq org-cycle-global-status 'contents))
  3675. ((equal arg '(4))
  3676. (org-set-startup-visibility)
  3677. (message "Startup visibility, plus VISIBILITY properties."))
  3678. (t
  3679. (org-cycle '(4))))))
  3680. (defun org-set-startup-visibility ()
  3681. "Set the visibility required by startup options and properties."
  3682. (cond
  3683. ((eq org-startup-folded t)
  3684. (org-cycle '(4)))
  3685. ((eq org-startup-folded 'content)
  3686. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3687. (org-cycle '(4)) (org-cycle '(4)))))
  3688. (org-set-visibility-according-to-property 'no-cleanup)
  3689. (org-cycle-hide-archived-subtrees 'all)
  3690. (org-cycle-hide-drawers 'all)
  3691. (org-cycle-show-empty-lines 'all))
  3692. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3693. "Switch subtree visibilities according to :VISIBILITY: property."
  3694. (interactive)
  3695. (let (state)
  3696. (save-excursion
  3697. (goto-char (point-min))
  3698. (while (re-search-forward
  3699. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3700. nil t)
  3701. (setq state (match-string 1))
  3702. (save-excursion
  3703. (org-back-to-heading t)
  3704. (hide-subtree)
  3705. (org-reveal)
  3706. (cond
  3707. ((equal state '("fold" "folded"))
  3708. (hide-subtree))
  3709. ((equal state "children")
  3710. (org-show-hidden-entry)
  3711. (show-children))
  3712. ((equal state "content")
  3713. (save-excursion
  3714. (save-restriction
  3715. (org-narrow-to-subtree)
  3716. (org-content))))
  3717. ((member state '("all" "showall"))
  3718. (show-subtree)))))
  3719. (unless no-cleanup
  3720. (org-cycle-hide-archived-subtrees 'all)
  3721. (org-cycle-hide-drawers 'all)
  3722. (org-cycle-show-empty-lines 'all)))))
  3723. (defun org-overview ()
  3724. "Switch to overview mode, shoing only top-level headlines.
  3725. Really, this shows all headlines with level equal or greater than the level
  3726. of the first headline in the buffer. This is important, because if the
  3727. first headline is not level one, then (hide-sublevels 1) gives confusing
  3728. results."
  3729. (interactive)
  3730. (let ((level (save-excursion
  3731. (goto-char (point-min))
  3732. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3733. (progn
  3734. (goto-char (match-beginning 0))
  3735. (funcall outline-level))))))
  3736. (and level (hide-sublevels level))))
  3737. (defun org-content (&optional arg)
  3738. "Show all headlines in the buffer, like a table of contents.
  3739. With numerical argument N, show content up to level N."
  3740. (interactive "P")
  3741. (save-excursion
  3742. ;; Visit all headings and show their offspring
  3743. (and (integerp arg) (org-overview))
  3744. (goto-char (point-max))
  3745. (catch 'exit
  3746. (while (and (progn (condition-case nil
  3747. (outline-previous-visible-heading 1)
  3748. (error (goto-char (point-min))))
  3749. t)
  3750. (looking-at outline-regexp))
  3751. (if (integerp arg)
  3752. (show-children (1- arg))
  3753. (show-branches))
  3754. (if (bobp) (throw 'exit nil))))))
  3755. (defun org-optimize-window-after-visibility-change (state)
  3756. "Adjust the window after a change in outline visibility.
  3757. This function is the default value of the hook `org-cycle-hook'."
  3758. (when (get-buffer-window (current-buffer))
  3759. (cond
  3760. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3761. ; ((eq state 'overview) (org-beginning-of-line))
  3762. ((eq state 'content) nil)
  3763. ((eq state 'all) nil)
  3764. ((eq state 'folded) nil)
  3765. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3766. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3767. (defun org-compact-display-after-subtree-move ()
  3768. (let (beg end)
  3769. (save-excursion
  3770. (if (org-up-heading-safe)
  3771. (progn
  3772. (hide-subtree)
  3773. (show-entry)
  3774. (show-children)
  3775. (org-cycle-show-empty-lines 'children)
  3776. (org-cycle-hide-drawers 'children))
  3777. (org-overview)))))
  3778. (defun org-cycle-show-empty-lines (state)
  3779. "Show empty lines above all visible headlines.
  3780. The region to be covered depends on STATE when called through
  3781. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3782. entire buffer covered. Note that an empty line is only shown if there
  3783. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3784. (when (> org-cycle-separator-lines 0)
  3785. (save-excursion
  3786. (let* ((n org-cycle-separator-lines)
  3787. (re (cond
  3788. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3789. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3790. (t (let ((ns (number-to-string (- n 2))))
  3791. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3792. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3793. beg end)
  3794. (cond
  3795. ((memq state '(overview contents t))
  3796. (setq beg (point-min) end (point-max)))
  3797. ((memq state '(children folded))
  3798. (setq beg (point) end (progn (org-end-of-subtree t t)
  3799. (beginning-of-line 2)
  3800. (point)))))
  3801. (when beg
  3802. (goto-char beg)
  3803. (while (re-search-forward re end t)
  3804. (if (not (get-char-property (match-end 1) 'invisible))
  3805. (outline-flag-region
  3806. (match-beginning 1) (match-end 1) nil)))))))
  3807. ;; Never hide empty lines at the end of the file.
  3808. (save-excursion
  3809. (goto-char (point-max))
  3810. (outline-previous-heading)
  3811. (outline-end-of-heading)
  3812. (if (and (looking-at "[ \t\n]+")
  3813. (= (match-end 0) (point-max)))
  3814. (outline-flag-region (point) (match-end 0) nil))))
  3815. (defun org-show-empty-lines-in-parent ()
  3816. "Move to the parent and re-show empty lines before visible headlines."
  3817. (save-excursion
  3818. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3819. (org-cycle-show-empty-lines context))))
  3820. (defun org-cycle-hide-drawers (state)
  3821. "Re-hide all drawers after a visibility state change."
  3822. (when (and (org-mode-p)
  3823. (not (memq state '(overview folded))))
  3824. (save-excursion
  3825. (let* ((globalp (memq state '(contents all)))
  3826. (beg (if globalp (point-min) (point)))
  3827. (end (if globalp (point-max) (org-end-of-subtree t))))
  3828. (goto-char beg)
  3829. (while (re-search-forward org-drawer-regexp end t)
  3830. (org-flag-drawer t))))))
  3831. (defun org-flag-drawer (flag)
  3832. (save-excursion
  3833. (beginning-of-line 1)
  3834. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3835. (let ((b (match-end 0))
  3836. (outline-regexp org-outline-regexp))
  3837. (if (re-search-forward
  3838. "^[ \t]*:END:"
  3839. (save-excursion (outline-next-heading) (point)) t)
  3840. (outline-flag-region b (point-at-eol) flag)
  3841. (error ":END: line missing"))))))
  3842. (defun org-subtree-end-visible-p ()
  3843. "Is the end of the current subtree visible?"
  3844. (pos-visible-in-window-p
  3845. (save-excursion (org-end-of-subtree t) (point))))
  3846. (defun org-first-headline-recenter (&optional N)
  3847. "Move cursor to the first headline and recenter the headline.
  3848. Optional argument N means, put the headline into the Nth line of the window."
  3849. (goto-char (point-min))
  3850. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3851. (beginning-of-line)
  3852. (recenter (prefix-numeric-value N))))
  3853. ;;; Org-goto
  3854. (defvar org-goto-window-configuration nil)
  3855. (defvar org-goto-marker nil)
  3856. (defvar org-goto-map
  3857. (let ((map (make-sparse-keymap)))
  3858. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3859. (while (setq cmd (pop cmds))
  3860. (substitute-key-definition cmd cmd map global-map)))
  3861. (suppress-keymap map)
  3862. (org-defkey map "\C-m" 'org-goto-ret)
  3863. (org-defkey map [(return)] 'org-goto-ret)
  3864. (org-defkey map [(left)] 'org-goto-left)
  3865. (org-defkey map [(right)] 'org-goto-right)
  3866. (org-defkey map [(control ?g)] 'org-goto-quit)
  3867. (org-defkey map "\C-i" 'org-cycle)
  3868. (org-defkey map [(tab)] 'org-cycle)
  3869. (org-defkey map [(down)] 'outline-next-visible-heading)
  3870. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3871. (if org-goto-auto-isearch
  3872. (if (fboundp 'define-key-after)
  3873. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3874. nil)
  3875. (org-defkey map "q" 'org-goto-quit)
  3876. (org-defkey map "n" 'outline-next-visible-heading)
  3877. (org-defkey map "p" 'outline-previous-visible-heading)
  3878. (org-defkey map "f" 'outline-forward-same-level)
  3879. (org-defkey map "b" 'outline-backward-same-level)
  3880. (org-defkey map "u" 'outline-up-heading))
  3881. (org-defkey map "/" 'org-occur)
  3882. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3883. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3884. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3885. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3886. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3887. map))
  3888. (defconst org-goto-help
  3889. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3890. RET=jump to location [Q]uit and return to previous location
  3891. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3892. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3893. ;; FIXME: Docstring doe not mention both interfaces
  3894. (defun org-goto (&optional alternative-interface)
  3895. "Look up a different location in the current file, keeping current visibility.
  3896. When you want look-up or go to a different location in a document, the
  3897. fastest way is often to fold the entire buffer and then dive into the tree.
  3898. This method has the disadvantage, that the previous location will be folded,
  3899. which may not be what you want.
  3900. This command works around this by showing a copy of the current buffer
  3901. in an indirect buffer, in overview mode. You can dive into the tree in
  3902. that copy, use org-occur and incremental search to find a location.
  3903. When pressing RET or `Q', the command returns to the original buffer in
  3904. which the visibility is still unchanged. After RET is will also jump to
  3905. the location selected in the indirect buffer and expose the
  3906. the headline hierarchy above."
  3907. (interactive "P")
  3908. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3909. (org-refile-use-outline-path t)
  3910. (interface
  3911. (if (not alternative-interface)
  3912. org-goto-interface
  3913. (if (eq org-goto-interface 'outline)
  3914. 'outline-path-completion
  3915. 'outline)))
  3916. (org-goto-start-pos (point))
  3917. (selected-point
  3918. (if (eq interface 'outline)
  3919. (car (org-get-location (current-buffer) org-goto-help))
  3920. (nth 3 (org-refile-get-location "Goto: ")))))
  3921. (if selected-point
  3922. (progn
  3923. (org-mark-ring-push org-goto-start-pos)
  3924. (goto-char selected-point)
  3925. (if (or (org-invisible-p) (org-invisible-p2))
  3926. (org-show-context 'org-goto)))
  3927. (message "Quit"))))
  3928. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3929. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3930. (defvar org-goto-local-auto-isearch-map) ; defined below
  3931. (defun org-get-location (buf help)
  3932. "Let the user select a location in the Org-mode buffer BUF.
  3933. This function uses a recursive edit. It returns the selected position
  3934. or nil."
  3935. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3936. (isearch-hide-immediately nil)
  3937. (isearch-search-fun-function
  3938. (lambda () 'org-goto-local-search-headings))
  3939. (org-goto-selected-point org-goto-exit-command))
  3940. (save-excursion
  3941. (save-window-excursion
  3942. (delete-other-windows)
  3943. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3944. (switch-to-buffer
  3945. (condition-case nil
  3946. (make-indirect-buffer (current-buffer) "*org-goto*")
  3947. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3948. (with-output-to-temp-buffer "*Help*"
  3949. (princ help))
  3950. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3951. (setq buffer-read-only nil)
  3952. (let ((org-startup-truncated t)
  3953. (org-startup-folded nil)
  3954. (org-startup-align-all-tables nil))
  3955. (org-mode)
  3956. (org-overview))
  3957. (setq buffer-read-only t)
  3958. (if (and (boundp 'org-goto-start-pos)
  3959. (integer-or-marker-p org-goto-start-pos))
  3960. (let ((org-show-hierarchy-above t)
  3961. (org-show-siblings t)
  3962. (org-show-following-heading t))
  3963. (goto-char org-goto-start-pos)
  3964. (and (org-invisible-p) (org-show-context)))
  3965. (goto-char (point-min)))
  3966. (org-beginning-of-line)
  3967. (message "Select location and press RET")
  3968. (use-local-map org-goto-map)
  3969. (recursive-edit)
  3970. ))
  3971. (kill-buffer "*org-goto*")
  3972. (cons org-goto-selected-point org-goto-exit-command)))
  3973. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3974. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3975. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3976. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3977. (defun org-goto-local-search-headings (string bound noerror)
  3978. "Search and make sure that any matches are in headlines."
  3979. (catch 'return
  3980. (while (if isearch-forward
  3981. (search-forward string bound noerror)
  3982. (search-backward string bound noerror))
  3983. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3984. (and (member :headline context)
  3985. (not (member :tags context))))
  3986. (throw 'return (point))))))
  3987. (defun org-goto-local-auto-isearch ()
  3988. "Start isearch."
  3989. (interactive)
  3990. (goto-char (point-min))
  3991. (let ((keys (this-command-keys)))
  3992. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3993. (isearch-mode t)
  3994. (isearch-process-search-char (string-to-char keys)))))
  3995. (defun org-goto-ret (&optional arg)
  3996. "Finish `org-goto' by going to the new location."
  3997. (interactive "P")
  3998. (setq org-goto-selected-point (point)
  3999. org-goto-exit-command 'return)
  4000. (throw 'exit nil))
  4001. (defun org-goto-left ()
  4002. "Finish `org-goto' by going to the new location."
  4003. (interactive)
  4004. (if (org-on-heading-p)
  4005. (progn
  4006. (beginning-of-line 1)
  4007. (setq org-goto-selected-point (point)
  4008. org-goto-exit-command 'left)
  4009. (throw 'exit nil))
  4010. (error "Not on a heading")))
  4011. (defun org-goto-right ()
  4012. "Finish `org-goto' by going to the new location."
  4013. (interactive)
  4014. (if (org-on-heading-p)
  4015. (progn
  4016. (setq org-goto-selected-point (point)
  4017. org-goto-exit-command 'right)
  4018. (throw 'exit nil))
  4019. (error "Not on a heading")))
  4020. (defun org-goto-quit ()
  4021. "Finish `org-goto' without cursor motion."
  4022. (interactive)
  4023. (setq org-goto-selected-point nil)
  4024. (setq org-goto-exit-command 'quit)
  4025. (throw 'exit nil))
  4026. ;;; Indirect buffer display of subtrees
  4027. (defvar org-indirect-dedicated-frame nil
  4028. "This is the frame being used for indirect tree display.")
  4029. (defvar org-last-indirect-buffer nil)
  4030. (defun org-tree-to-indirect-buffer (&optional arg)
  4031. "Create indirect buffer and narrow it to current subtree.
  4032. With numerical prefix ARG, go up to this level and then take that tree.
  4033. If ARG is negative, go up that many levels.
  4034. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4035. indirect buffer previously made with this command, to avoid proliferation of
  4036. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4037. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4038. is kept so that you can work with several indirect buffers at the same time.
  4039. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4040. requests that a new frame be made for the new buffer, so that the dedicated
  4041. frame is not changed."
  4042. (interactive "P")
  4043. (let ((cbuf (current-buffer))
  4044. (cwin (selected-window))
  4045. (pos (point))
  4046. beg end level heading ibuf)
  4047. (save-excursion
  4048. (org-back-to-heading t)
  4049. (when (numberp arg)
  4050. (setq level (org-outline-level))
  4051. (if (< arg 0) (setq arg (+ level arg)))
  4052. (while (> (setq level (org-outline-level)) arg)
  4053. (outline-up-heading 1 t)))
  4054. (setq beg (point)
  4055. heading (org-get-heading))
  4056. (org-end-of-subtree t) (setq end (point)))
  4057. (if (and (buffer-live-p org-last-indirect-buffer)
  4058. (not (eq org-indirect-buffer-display 'new-frame))
  4059. (not arg))
  4060. (kill-buffer org-last-indirect-buffer))
  4061. (setq ibuf (org-get-indirect-buffer cbuf)
  4062. org-last-indirect-buffer ibuf)
  4063. (cond
  4064. ((or (eq org-indirect-buffer-display 'new-frame)
  4065. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4066. (select-frame (make-frame))
  4067. (delete-other-windows)
  4068. (switch-to-buffer ibuf)
  4069. (org-set-frame-title heading))
  4070. ((eq org-indirect-buffer-display 'dedicated-frame)
  4071. (raise-frame
  4072. (select-frame (or (and org-indirect-dedicated-frame
  4073. (frame-live-p org-indirect-dedicated-frame)
  4074. org-indirect-dedicated-frame)
  4075. (setq org-indirect-dedicated-frame (make-frame)))))
  4076. (delete-other-windows)
  4077. (switch-to-buffer ibuf)
  4078. (org-set-frame-title (concat "Indirect: " heading)))
  4079. ((eq org-indirect-buffer-display 'current-window)
  4080. (switch-to-buffer ibuf))
  4081. ((eq org-indirect-buffer-display 'other-window)
  4082. (pop-to-buffer ibuf))
  4083. (t (error "Invalid value.")))
  4084. (if (featurep 'xemacs)
  4085. (save-excursion (org-mode) (turn-on-font-lock)))
  4086. (narrow-to-region beg end)
  4087. (show-all)
  4088. (goto-char pos)
  4089. (and (window-live-p cwin) (select-window cwin))))
  4090. (defun org-get-indirect-buffer (&optional buffer)
  4091. (setq buffer (or buffer (current-buffer)))
  4092. (let ((n 1) (base (buffer-name buffer)) bname)
  4093. (while (buffer-live-p
  4094. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4095. (setq n (1+ n)))
  4096. (condition-case nil
  4097. (make-indirect-buffer buffer bname 'clone)
  4098. (error (make-indirect-buffer buffer bname)))))
  4099. (defun org-set-frame-title (title)
  4100. "Set the title of the current frame to the string TITLE."
  4101. ;; FIXME: how to name a single frame in XEmacs???
  4102. (unless (featurep 'xemacs)
  4103. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4104. ;;;; Structure editing
  4105. ;;; Inserting headlines
  4106. (defun org-insert-heading (&optional force-heading)
  4107. "Insert a new heading or item with same depth at point.
  4108. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4109. If point is at the beginning of a headline, insert a sibling before the
  4110. current headline. If point is not at the beginning, do not split the line,
  4111. but create the new hedline after the current line."
  4112. (interactive "P")
  4113. (if (= (buffer-size) 0)
  4114. (insert "\n* ")
  4115. (when (or force-heading (not (org-insert-item)))
  4116. (let* ((head (save-excursion
  4117. (condition-case nil
  4118. (progn
  4119. (org-back-to-heading)
  4120. (match-string 0))
  4121. (error "*"))))
  4122. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4123. pos)
  4124. (cond
  4125. ((and (org-on-heading-p) (bolp)
  4126. (or (bobp)
  4127. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4128. ;; insert before the current line
  4129. (open-line (if blank 2 1)))
  4130. ((and (bolp)
  4131. (or (bobp)
  4132. (save-excursion
  4133. (backward-char 1) (not (org-invisible-p)))))
  4134. ;; insert right here
  4135. nil)
  4136. (t
  4137. ;; in the middle of the line
  4138. (org-show-entry)
  4139. (let ((split
  4140. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4141. tags pos)
  4142. (cond
  4143. (org-insert-heading-respect-content
  4144. (org-end-of-subtree nil t)
  4145. (open-line 1))
  4146. ((org-on-heading-p)
  4147. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4148. (setq tags (and (match-end 2) (match-string 2)))
  4149. (and (match-end 1)
  4150. (delete-region (match-beginning 1) (match-end 1)))
  4151. (setq pos (point-at-bol))
  4152. (or split (end-of-line 1))
  4153. (delete-horizontal-space)
  4154. (newline (if blank 2 1))
  4155. (when tags
  4156. (save-excursion
  4157. (goto-char pos)
  4158. (end-of-line 1)
  4159. (insert " " tags)
  4160. (org-set-tags nil 'align))))
  4161. (t
  4162. (or split (end-of-line 1))
  4163. (newline (if blank 2 1)))))))
  4164. (insert head) (just-one-space)
  4165. (setq pos (point))
  4166. (end-of-line 1)
  4167. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4168. (run-hooks 'org-insert-heading-hook)))))
  4169. (defun org-get-heading (&optional no-tags)
  4170. "Return the heading of the current entry, without the stars."
  4171. (save-excursion
  4172. (org-back-to-heading t)
  4173. (if (looking-at
  4174. (if no-tags
  4175. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4176. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4177. (match-string 1) "")))
  4178. (defun org-insert-heading-after-current ()
  4179. "Insert a new heading with same level as current, after current subtree."
  4180. (interactive)
  4181. (org-back-to-heading)
  4182. (org-insert-heading)
  4183. (org-move-subtree-down)
  4184. (end-of-line 1))
  4185. (defun org-insert-heading-respect-content ()
  4186. (interactive)
  4187. (let ((org-insert-heading-respect-content t))
  4188. (call-interactively 'org-insert-heading)))
  4189. (defun org-insert-todo-heading-respect-content ()
  4190. (interactive)
  4191. (let ((org-insert-heading-respect-content t))
  4192. (call-interactively 'org-insert-todo-todo-heading)))
  4193. (defun org-insert-todo-heading (arg)
  4194. "Insert a new heading with the same level and TODO state as current heading.
  4195. If the heading has no TODO state, or if the state is DONE, use the first
  4196. state (TODO by default). Also with prefix arg, force first state."
  4197. (interactive "P")
  4198. (when (not (org-insert-item 'checkbox))
  4199. (org-insert-heading)
  4200. (save-excursion
  4201. (org-back-to-heading)
  4202. (outline-previous-heading)
  4203. (looking-at org-todo-line-regexp))
  4204. (if (or arg
  4205. (not (match-beginning 2))
  4206. (member (match-string 2) org-done-keywords))
  4207. (insert (car org-todo-keywords-1) " ")
  4208. (insert (match-string 2) " "))
  4209. (when org-provide-todo-statistics
  4210. (org-update-parent-todo-statistics))))
  4211. (defun org-insert-subheading (arg)
  4212. "Insert a new subheading and demote it.
  4213. Works for outline headings and for plain lists alike."
  4214. (interactive "P")
  4215. (org-insert-heading arg)
  4216. (cond
  4217. ((org-on-heading-p) (org-do-demote))
  4218. ((org-at-item-p) (org-indent-item 1))))
  4219. (defun org-insert-todo-subheading (arg)
  4220. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4221. Works for outline headings and for plain lists alike."
  4222. (interactive "P")
  4223. (org-insert-todo-heading arg)
  4224. (cond
  4225. ((org-on-heading-p) (org-do-demote))
  4226. ((org-at-item-p) (org-indent-item 1))))
  4227. ;;; Promotion and Demotion
  4228. (defun org-promote-subtree ()
  4229. "Promote the entire subtree.
  4230. See also `org-promote'."
  4231. (interactive)
  4232. (save-excursion
  4233. (org-map-tree 'org-promote))
  4234. (org-fix-position-after-promote))
  4235. (defun org-demote-subtree ()
  4236. "Demote the entire subtree. See `org-demote'.
  4237. See also `org-promote'."
  4238. (interactive)
  4239. (save-excursion
  4240. (org-map-tree 'org-demote))
  4241. (org-fix-position-after-promote))
  4242. (defun org-do-promote ()
  4243. "Promote the current heading higher up the tree.
  4244. If the region is active in `transient-mark-mode', promote all headings
  4245. in the region."
  4246. (interactive)
  4247. (save-excursion
  4248. (if (org-region-active-p)
  4249. (org-map-region 'org-promote (region-beginning) (region-end))
  4250. (org-promote)))
  4251. (org-fix-position-after-promote))
  4252. (defun org-do-demote ()
  4253. "Demote the current heading lower down the tree.
  4254. If the region is active in `transient-mark-mode', demote all headings
  4255. in the region."
  4256. (interactive)
  4257. (save-excursion
  4258. (if (org-region-active-p)
  4259. (org-map-region 'org-demote (region-beginning) (region-end))
  4260. (org-demote)))
  4261. (org-fix-position-after-promote))
  4262. (defun org-fix-position-after-promote ()
  4263. "Make sure that after pro/demotion cursor position is right."
  4264. (let ((pos (point)))
  4265. (when (save-excursion
  4266. (beginning-of-line 1)
  4267. (looking-at org-todo-line-regexp)
  4268. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4269. (cond ((eobp) (insert " "))
  4270. ((eolp) (insert " "))
  4271. ((equal (char-after) ?\ ) (forward-char 1))))))
  4272. (defun org-reduced-level (l)
  4273. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4274. (defun org-get-valid-level (level &optional change)
  4275. "Rectify a level change under the influence of `org-odd-levels-only'
  4276. LEVEL is a current level, CHANGE is by how much the level should be
  4277. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4278. even level numbers will become the next higher odd number."
  4279. (if org-odd-levels-only
  4280. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4281. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4282. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4283. (max 1 (+ level change))))
  4284. (if (boundp 'define-obsolete-function-alias)
  4285. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4286. (define-obsolete-function-alias 'org-get-legal-level
  4287. 'org-get-valid-level)
  4288. (define-obsolete-function-alias 'org-get-legal-level
  4289. 'org-get-valid-level "23.1")))
  4290. (defun org-promote ()
  4291. "Promote the current heading higher up the tree.
  4292. If the region is active in `transient-mark-mode', promote all headings
  4293. in the region."
  4294. (org-back-to-heading t)
  4295. (let* ((level (save-match-data (funcall outline-level)))
  4296. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4297. (diff (abs (- level (length up-head) -1))))
  4298. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4299. (replace-match up-head nil t)
  4300. ;; Fixup tag positioning
  4301. (and org-auto-align-tags (org-set-tags nil t))
  4302. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4303. (defun org-demote ()
  4304. "Demote the current heading lower down the tree.
  4305. If the region is active in `transient-mark-mode', demote all headings
  4306. in the region."
  4307. (org-back-to-heading t)
  4308. (let* ((level (save-match-data (funcall outline-level)))
  4309. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4310. (diff (abs (- level (length down-head) -1))))
  4311. (replace-match down-head nil t)
  4312. ;; Fixup tag positioning
  4313. (and org-auto-align-tags (org-set-tags nil t))
  4314. (if org-adapt-indentation (org-fixup-indentation diff))))
  4315. (defun org-map-tree (fun)
  4316. "Call FUN for every heading underneath the current one."
  4317. (org-back-to-heading)
  4318. (let ((level (funcall outline-level)))
  4319. (save-excursion
  4320. (funcall fun)
  4321. (while (and (progn
  4322. (outline-next-heading)
  4323. (> (funcall outline-level) level))
  4324. (not (eobp)))
  4325. (funcall fun)))))
  4326. (defun org-map-region (fun beg end)
  4327. "Call FUN for every heading between BEG and END."
  4328. (let ((org-ignore-region t))
  4329. (save-excursion
  4330. (setq end (copy-marker end))
  4331. (goto-char beg)
  4332. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4333. (< (point) end))
  4334. (funcall fun))
  4335. (while (and (progn
  4336. (outline-next-heading)
  4337. (< (point) end))
  4338. (not (eobp)))
  4339. (funcall fun)))))
  4340. (defun org-fixup-indentation (diff)
  4341. "Change the indentation in the current entry by DIFF
  4342. However, if any line in the current entry has no indentation, or if it
  4343. would end up with no indentation after the change, nothing at all is done."
  4344. (save-excursion
  4345. (let ((end (save-excursion (outline-next-heading)
  4346. (point-marker)))
  4347. (prohibit (if (> diff 0)
  4348. "^\\S-"
  4349. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4350. col)
  4351. (unless (save-excursion (end-of-line 1)
  4352. (re-search-forward prohibit end t))
  4353. (while (and (< (point) end)
  4354. (re-search-forward "^[ \t]+" end t))
  4355. (goto-char (match-end 0))
  4356. (setq col (current-column))
  4357. (if (< diff 0) (replace-match ""))
  4358. (indent-to (+ diff col))))
  4359. (move-marker end nil))))
  4360. (defun org-convert-to-odd-levels ()
  4361. "Convert an org-mode file with all levels allowed to one with odd levels.
  4362. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4363. level 5 etc."
  4364. (interactive)
  4365. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4366. (let ((org-odd-levels-only nil) n)
  4367. (save-excursion
  4368. (goto-char (point-min))
  4369. (while (re-search-forward "^\\*\\*+ " nil t)
  4370. (setq n (- (length (match-string 0)) 2))
  4371. (while (>= (setq n (1- n)) 0)
  4372. (org-demote))
  4373. (end-of-line 1))))))
  4374. (defun org-convert-to-oddeven-levels ()
  4375. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4376. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4377. section with an even level, conversion would destroy the structure of the file. An error
  4378. is signaled in this case."
  4379. (interactive)
  4380. (goto-char (point-min))
  4381. ;; First check if there are no even levels
  4382. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4383. (org-show-context t)
  4384. (error "Not all levels are odd in this file. Conversion not possible."))
  4385. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4386. (let ((org-odd-levels-only nil) n)
  4387. (save-excursion
  4388. (goto-char (point-min))
  4389. (while (re-search-forward "^\\*\\*+ " nil t)
  4390. (setq n (/ (1- (length (match-string 0))) 2))
  4391. (while (>= (setq n (1- n)) 0)
  4392. (org-promote))
  4393. (end-of-line 1))))))
  4394. (defun org-tr-level (n)
  4395. "Make N odd if required."
  4396. (if org-odd-levels-only (1+ (/ n 2)) n))
  4397. ;;; Vertical tree motion, cutting and pasting of subtrees
  4398. (defun org-move-subtree-up (&optional arg)
  4399. "Move the current subtree up past ARG headlines of the same level."
  4400. (interactive "p")
  4401. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4402. (defun org-move-subtree-down (&optional arg)
  4403. "Move the current subtree down past ARG headlines of the same level."
  4404. (interactive "p")
  4405. (setq arg (prefix-numeric-value arg))
  4406. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4407. 'outline-get-last-sibling))
  4408. (ins-point (make-marker))
  4409. (cnt (abs arg))
  4410. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4411. ;; Select the tree
  4412. (org-back-to-heading)
  4413. (setq beg0 (point))
  4414. (save-excursion
  4415. (setq ne-beg (org-back-over-empty-lines))
  4416. (setq beg (point)))
  4417. (save-match-data
  4418. (save-excursion (outline-end-of-heading)
  4419. (setq folded (org-invisible-p)))
  4420. (outline-end-of-subtree))
  4421. (outline-next-heading)
  4422. (setq ne-end (org-back-over-empty-lines))
  4423. (setq end (point))
  4424. (goto-char beg0)
  4425. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4426. ;; include less whitespace
  4427. (save-excursion
  4428. (goto-char beg)
  4429. (forward-line (- ne-beg ne-end))
  4430. (setq beg (point))))
  4431. ;; Find insertion point, with error handling
  4432. (while (> cnt 0)
  4433. (or (and (funcall movfunc) (looking-at outline-regexp))
  4434. (progn (goto-char beg0)
  4435. (error "Cannot move past superior level or buffer limit")))
  4436. (setq cnt (1- cnt)))
  4437. (if (> arg 0)
  4438. ;; Moving forward - still need to move over subtree
  4439. (progn (org-end-of-subtree t t)
  4440. (save-excursion
  4441. (org-back-over-empty-lines)
  4442. (or (bolp) (newline)))))
  4443. (setq ne-ins (org-back-over-empty-lines))
  4444. (move-marker ins-point (point))
  4445. (setq txt (buffer-substring beg end))
  4446. (org-save-markers-in-region beg end)
  4447. (delete-region beg end)
  4448. (outline-flag-region (1- beg) beg nil)
  4449. (outline-flag-region (1- (point)) (point) nil)
  4450. (let ((bbb (point)))
  4451. (insert-before-markers txt)
  4452. (org-reinstall-markers-in-region bbb)
  4453. (move-marker ins-point bbb))
  4454. (or (bolp) (insert "\n"))
  4455. (setq ins-end (point))
  4456. (goto-char ins-point)
  4457. (org-skip-whitespace)
  4458. (when (and (< arg 0)
  4459. (org-first-sibling-p)
  4460. (> ne-ins ne-beg))
  4461. ;; Move whitespace back to beginning
  4462. (save-excursion
  4463. (goto-char ins-end)
  4464. (let ((kill-whole-line t))
  4465. (kill-line (- ne-ins ne-beg)) (point)))
  4466. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4467. (move-marker ins-point nil)
  4468. (org-compact-display-after-subtree-move)
  4469. (org-show-empty-lines-in-parent)
  4470. (unless folded
  4471. (org-show-entry)
  4472. (show-children)
  4473. (org-cycle-hide-drawers 'children))))
  4474. (defvar org-subtree-clip ""
  4475. "Clipboard for cut and paste of subtrees.
  4476. This is actually only a copy of the kill, because we use the normal kill
  4477. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4478. (defvar org-subtree-clip-folded nil
  4479. "Was the last copied subtree folded?
  4480. This is used to fold the tree back after pasting.")
  4481. (defun org-cut-subtree (&optional n)
  4482. "Cut the current subtree into the clipboard.
  4483. With prefix arg N, cut this many sequential subtrees.
  4484. This is a short-hand for marking the subtree and then cutting it."
  4485. (interactive "p")
  4486. (org-copy-subtree n 'cut))
  4487. (defun org-copy-subtree (&optional n cut force-store-markers)
  4488. "Cut the current subtree into the clipboard.
  4489. With prefix arg N, cut this many sequential subtrees.
  4490. This is a short-hand for marking the subtree and then copying it.
  4491. If CUT is non-nil, actually cut the subtree.
  4492. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4493. of some markers in the region, even if CUT is non-nil. This is
  4494. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4495. (interactive "p")
  4496. (let (beg end folded (beg0 (point)))
  4497. (if (interactive-p)
  4498. (org-back-to-heading nil) ; take what looks like a subtree
  4499. (org-back-to-heading t)) ; take what is really there
  4500. (org-back-over-empty-lines)
  4501. (setq beg (point))
  4502. (skip-chars-forward " \t\r\n")
  4503. (save-match-data
  4504. (save-excursion (outline-end-of-heading)
  4505. (setq folded (org-invisible-p)))
  4506. (condition-case nil
  4507. (outline-forward-same-level (1- n))
  4508. (error nil))
  4509. (org-end-of-subtree t t))
  4510. (org-back-over-empty-lines)
  4511. (setq end (point))
  4512. (goto-char beg0)
  4513. (when (> end beg)
  4514. (setq org-subtree-clip-folded folded)
  4515. (when (or cut force-store-markers)
  4516. (org-save-markers-in-region beg end))
  4517. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4518. (setq org-subtree-clip (current-kill 0))
  4519. (message "%s: Subtree(s) with %d characters"
  4520. (if cut "Cut" "Copied")
  4521. (length org-subtree-clip)))))
  4522. (defun org-paste-subtree (&optional level tree)
  4523. "Paste the clipboard as a subtree, with modification of headline level.
  4524. The entire subtree is promoted or demoted in order to match a new headline
  4525. level. By default, the new level is derived from the visible headings
  4526. before and after the insertion point, and taken to be the inferior headline
  4527. level of the two. So if the previous visible heading is level 3 and the
  4528. next is level 4 (or vice versa), level 4 will be used for insertion.
  4529. This makes sure that the subtree remains an independent subtree and does
  4530. not swallow low level entries.
  4531. You can also force a different level, either by using a numeric prefix
  4532. argument, or by inserting the heading marker by hand. For example, if the
  4533. cursor is after \"*****\", then the tree will be shifted to level 5.
  4534. If you want to insert the tree as is, just use \\[yank].
  4535. If optional TREE is given, use this text instead of the kill ring."
  4536. (interactive "P")
  4537. (unless (org-kill-is-subtree-p tree)
  4538. (error "%s"
  4539. (substitute-command-keys
  4540. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4541. (let* ((visp (not (org-invisible-p)))
  4542. (txt (or tree (and kill-ring (current-kill 0))))
  4543. (^re (concat "^\\(" outline-regexp "\\)"))
  4544. (re (concat "\\(" outline-regexp "\\)"))
  4545. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4546. (old-level (if (string-match ^re txt)
  4547. (- (match-end 0) (match-beginning 0) 1)
  4548. -1))
  4549. (force-level (cond (level (prefix-numeric-value level))
  4550. ((string-match
  4551. ^re_ (buffer-substring (point-at-bol) (point)))
  4552. (- (match-end 1) (match-beginning 1)))
  4553. (t nil)))
  4554. (previous-level (save-excursion
  4555. (condition-case nil
  4556. (progn
  4557. (outline-previous-visible-heading 1)
  4558. (if (looking-at re)
  4559. (- (match-end 0) (match-beginning 0) 1)
  4560. 1))
  4561. (error 1))))
  4562. (next-level (save-excursion
  4563. (condition-case nil
  4564. (progn
  4565. (or (looking-at outline-regexp)
  4566. (outline-next-visible-heading 1))
  4567. (if (looking-at re)
  4568. (- (match-end 0) (match-beginning 0) 1)
  4569. 1))
  4570. (error 1))))
  4571. (new-level (or force-level (max previous-level next-level)))
  4572. (shift (if (or (= old-level -1)
  4573. (= new-level -1)
  4574. (= old-level new-level))
  4575. 0
  4576. (- new-level old-level)))
  4577. (delta (if (> shift 0) -1 1))
  4578. (func (if (> shift 0) 'org-demote 'org-promote))
  4579. (org-odd-levels-only nil)
  4580. beg end)
  4581. ;; Remove the forced level indicator
  4582. (if force-level
  4583. (delete-region (point-at-bol) (point)))
  4584. ;; Paste
  4585. (beginning-of-line 1)
  4586. (org-back-over-empty-lines)
  4587. (setq beg (point))
  4588. (insert-before-markers txt)
  4589. (unless (string-match "\n\\'" txt) (insert "\n"))
  4590. (org-reinstall-markers-in-region beg)
  4591. (setq end (point))
  4592. (goto-char beg)
  4593. (skip-chars-forward " \t\n\r")
  4594. (setq beg (point))
  4595. (if (and (org-invisible-p) visp)
  4596. (save-excursion (outline-show-heading)))
  4597. ;; Shift if necessary
  4598. (unless (= shift 0)
  4599. (save-restriction
  4600. (narrow-to-region beg end)
  4601. (while (not (= shift 0))
  4602. (org-map-region func (point-min) (point-max))
  4603. (setq shift (+ delta shift)))
  4604. (goto-char (point-min))))
  4605. (when (interactive-p)
  4606. (message "Clipboard pasted as level %d subtree" new-level))
  4607. (if (and kill-ring
  4608. (eq org-subtree-clip (current-kill 0))
  4609. org-subtree-clip-folded)
  4610. ;; The tree was folded before it was killed/copied
  4611. (hide-subtree))))
  4612. (defun org-kill-is-subtree-p (&optional txt)
  4613. "Check if the current kill is an outline subtree, or a set of trees.
  4614. Returns nil if kill does not start with a headline, or if the first
  4615. headline level is not the largest headline level in the tree.
  4616. So this will actually accept several entries of equal levels as well,
  4617. which is OK for `org-paste-subtree'.
  4618. If optional TXT is given, check this string instead of the current kill."
  4619. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4620. (start-level (and kill
  4621. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4622. org-outline-regexp "\\)")
  4623. kill)
  4624. (- (match-end 2) (match-beginning 2) 1)))
  4625. (re (concat "^" org-outline-regexp))
  4626. (start (1+ (or (match-beginning 2) -1))))
  4627. (if (not start-level)
  4628. (progn
  4629. nil) ;; does not even start with a heading
  4630. (catch 'exit
  4631. (while (setq start (string-match re kill (1+ start)))
  4632. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4633. (throw 'exit nil)))
  4634. t))))
  4635. (defvar org-markers-to-move nil
  4636. "Markers that should be moved with a cut-and-paste operation.
  4637. Those markers are stored together with their positions relative to
  4638. the start of the region.")
  4639. (defun org-save-markers-in-region (beg end)
  4640. "Check markers in region.
  4641. If these markers are between BEG and END, record their position relative
  4642. to BEG, so that after moving the block of text, we can put the markers back
  4643. into place.
  4644. This function gets called just before an entry or tree gets cut from the
  4645. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4646. called immediately, to move the markers with the entries."
  4647. (setq org-markers-to-move nil)
  4648. (when (featurep 'org-clock)
  4649. (org-clock-save-markers-for-cut-and-paste beg end))
  4650. (when (featurep 'org-agenda)
  4651. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4652. (defun org-check-and-save-marker (marker beg end)
  4653. "Check if MARKER is between BEG and END.
  4654. If yes, remember the marker and the distance to BEG."
  4655. (when (and (marker-buffer marker)
  4656. (equal (marker-buffer marker) (current-buffer)))
  4657. (if (and (>= marker beg) (< marker end))
  4658. (push (cons marker (- marker beg)) org-markers-to-move))))
  4659. (defun org-reinstall-markers-in-region (beg)
  4660. "Move all remembered markers to their position relative to BEG."
  4661. (mapc (lambda (x)
  4662. (move-marker (car x) (+ beg (cdr x))))
  4663. org-markers-to-move)
  4664. (setq org-markers-to-move nil))
  4665. (defun org-narrow-to-subtree ()
  4666. "Narrow buffer to the current subtree."
  4667. (interactive)
  4668. (save-excursion
  4669. (save-match-data
  4670. (narrow-to-region
  4671. (progn (org-back-to-heading) (point))
  4672. (progn (org-end-of-subtree t) (point))))))
  4673. ;;; Outline Sorting
  4674. (defun org-sort (with-case)
  4675. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4676. Optional argument WITH-CASE means sort case-sensitively."
  4677. (interactive "P")
  4678. (if (org-at-table-p)
  4679. (org-call-with-arg 'org-table-sort-lines with-case)
  4680. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4681. (defun org-sort-remove-invisible (s)
  4682. (remove-text-properties 0 (length s) org-rm-props s)
  4683. (while (string-match org-bracket-link-regexp s)
  4684. (setq s (replace-match (if (match-end 2)
  4685. (match-string 3 s)
  4686. (match-string 1 s)) t t s)))
  4687. s)
  4688. (defvar org-priority-regexp) ; defined later in the file
  4689. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4690. "Sort entries on a certain level of an outline tree.
  4691. If there is an active region, the entries in the region are sorted.
  4692. Else, if the cursor is before the first entry, sort the top-level items.
  4693. Else, the children of the entry at point are sorted.
  4694. Sorting can be alphabetically, numerically, and by date/time as given by
  4695. the first time stamp in the entry. The command prompts for the sorting
  4696. type unless it has been given to the function through the SORTING-TYPE
  4697. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4698. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4699. called with point at the beginning of the record. It must return either
  4700. a string or a number that should serve as the sorting key for that record.
  4701. Comparing entries ignores case by default. However, with an optional argument
  4702. WITH-CASE, the sorting considers case as well."
  4703. (interactive "P")
  4704. (let ((case-func (if with-case 'identity 'downcase))
  4705. start beg end stars re re2
  4706. txt what tmp plain-list-p)
  4707. ;; Find beginning and end of region to sort
  4708. (cond
  4709. ((org-region-active-p)
  4710. ;; we will sort the region
  4711. (setq end (region-end)
  4712. what "region")
  4713. (goto-char (region-beginning))
  4714. (if (not (org-on-heading-p)) (outline-next-heading))
  4715. (setq start (point)))
  4716. ((org-at-item-p)
  4717. ;; we will sort this plain list
  4718. (org-beginning-of-item-list) (setq start (point))
  4719. (org-end-of-item-list) (setq end (point))
  4720. (goto-char start)
  4721. (setq plain-list-p t
  4722. what "plain list"))
  4723. ((or (org-on-heading-p)
  4724. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4725. ;; we will sort the children of the current headline
  4726. (org-back-to-heading)
  4727. (setq start (point)
  4728. end (progn (org-end-of-subtree t t)
  4729. (org-back-over-empty-lines)
  4730. (point))
  4731. what "children")
  4732. (goto-char start)
  4733. (show-subtree)
  4734. (outline-next-heading))
  4735. (t
  4736. ;; we will sort the top-level entries in this file
  4737. (goto-char (point-min))
  4738. (or (org-on-heading-p) (outline-next-heading))
  4739. (setq start (point) end (point-max) what "top-level")
  4740. (goto-char start)
  4741. (show-all)))
  4742. (setq beg (point))
  4743. (if (>= beg end) (error "Nothing to sort"))
  4744. (unless plain-list-p
  4745. (looking-at "\\(\\*+\\)")
  4746. (setq stars (match-string 1)
  4747. re (concat "^" (regexp-quote stars) " +")
  4748. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4749. txt (buffer-substring beg end))
  4750. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4751. (if (and (not (equal stars "*")) (string-match re2 txt))
  4752. (error "Region to sort contains a level above the first entry")))
  4753. (unless sorting-type
  4754. (message
  4755. (if plain-list-p
  4756. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4757. "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:")
  4758. what)
  4759. (setq sorting-type (read-char-exclusive))
  4760. (and (= (downcase sorting-type) ?f)
  4761. (setq getkey-func
  4762. (completing-read "Sort using function: "
  4763. obarray 'fboundp t nil nil))
  4764. (setq getkey-func (intern getkey-func)))
  4765. (and (= (downcase sorting-type) ?r)
  4766. (setq property
  4767. (completing-read "Property: "
  4768. (mapcar 'list (org-buffer-property-keys t))
  4769. nil t))))
  4770. (message "Sorting entries...")
  4771. (save-restriction
  4772. (narrow-to-region start end)
  4773. (let ((dcst (downcase sorting-type))
  4774. (now (current-time)))
  4775. (sort-subr
  4776. (/= dcst sorting-type)
  4777. ;; This function moves to the beginning character of the "record" to
  4778. ;; be sorted.
  4779. (if plain-list-p
  4780. (lambda nil
  4781. (if (org-at-item-p) t (goto-char (point-max))))
  4782. (lambda nil
  4783. (if (re-search-forward re nil t)
  4784. (goto-char (match-beginning 0))
  4785. (goto-char (point-max)))))
  4786. ;; This function moves to the last character of the "record" being
  4787. ;; sorted.
  4788. (if plain-list-p
  4789. 'org-end-of-item
  4790. (lambda nil
  4791. (save-match-data
  4792. (condition-case nil
  4793. (outline-forward-same-level 1)
  4794. (error
  4795. (goto-char (point-max)))))))
  4796. ;; This function returns the value that gets sorted against.
  4797. (if plain-list-p
  4798. (lambda nil
  4799. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4800. (cond
  4801. ((= dcst ?n)
  4802. (string-to-number (buffer-substring (match-end 0)
  4803. (point-at-eol))))
  4804. ((= dcst ?a)
  4805. (buffer-substring (match-end 0) (point-at-eol)))
  4806. ((= dcst ?t)
  4807. (if (re-search-forward org-ts-regexp
  4808. (point-at-eol) t)
  4809. (org-time-string-to-time (match-string 0))
  4810. now))
  4811. ((= dcst ?f)
  4812. (if getkey-func
  4813. (progn
  4814. (setq tmp (funcall getkey-func))
  4815. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4816. tmp)
  4817. (error "Invalid key function `%s'" getkey-func)))
  4818. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4819. (lambda nil
  4820. (cond
  4821. ((= dcst ?n)
  4822. (if (looking-at org-complex-heading-regexp)
  4823. (string-to-number (match-string 4))
  4824. nil))
  4825. ((= dcst ?a)
  4826. (if (looking-at org-complex-heading-regexp)
  4827. (funcall case-func (match-string 4))
  4828. nil))
  4829. ((= dcst ?t)
  4830. (if (re-search-forward org-ts-regexp
  4831. (save-excursion
  4832. (forward-line 2)
  4833. (point)) t)
  4834. (org-time-string-to-time (match-string 0))
  4835. now))
  4836. ((= dcst ?p)
  4837. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4838. (string-to-char (match-string 2))
  4839. org-default-priority))
  4840. ((= dcst ?r)
  4841. (or (org-entry-get nil property) ""))
  4842. ((= dcst ?o)
  4843. (if (looking-at org-complex-heading-regexp)
  4844. (- 9999 (length (member (match-string 2)
  4845. org-todo-keywords-1)))))
  4846. ((= dcst ?f)
  4847. (if getkey-func
  4848. (progn
  4849. (setq tmp (funcall getkey-func))
  4850. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4851. tmp)
  4852. (error "Invalid key function `%s'" getkey-func)))
  4853. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4854. nil
  4855. (cond
  4856. ((= dcst ?a) 'string<)
  4857. ((= dcst ?t) 'time-less-p)
  4858. (t nil)))))
  4859. (message "Sorting entries...done")))
  4860. (defun org-do-sort (table what &optional with-case sorting-type)
  4861. "Sort TABLE of WHAT according to SORTING-TYPE.
  4862. The user will be prompted for the SORTING-TYPE if the call to this
  4863. function does not specify it. WHAT is only for the prompt, to indicate
  4864. what is being sorted. The sorting key will be extracted from
  4865. the car of the elements of the table.
  4866. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4867. (unless sorting-type
  4868. (message
  4869. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4870. what)
  4871. (setq sorting-type (read-char-exclusive)))
  4872. (let ((dcst (downcase sorting-type))
  4873. extractfun comparefun)
  4874. ;; Define the appropriate functions
  4875. (cond
  4876. ((= dcst ?n)
  4877. (setq extractfun 'string-to-number
  4878. comparefun (if (= dcst sorting-type) '< '>)))
  4879. ((= dcst ?a)
  4880. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4881. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4882. comparefun (if (= dcst sorting-type)
  4883. 'string<
  4884. (lambda (a b) (and (not (string< a b))
  4885. (not (string= a b)))))))
  4886. ((= dcst ?t)
  4887. (setq extractfun
  4888. (lambda (x)
  4889. (if (string-match org-ts-regexp x)
  4890. (time-to-seconds
  4891. (org-time-string-to-time (match-string 0 x)))
  4892. 0))
  4893. comparefun (if (= dcst sorting-type) '< '>)))
  4894. (t (error "Invalid sorting type `%c'" sorting-type)))
  4895. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4896. table)
  4897. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4898. ;;; Editing source examples
  4899. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4900. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4901. (defvar org-edit-src-force-single-line nil)
  4902. (defvar org-edit-src-from-org-mode nil)
  4903. (defvar org-edit-src-picture nil)
  4904. (define-minor-mode org-exit-edit-mode
  4905. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4906. (defun org-edit-src-code ()
  4907. "Edit the source code example at point.
  4908. An indirect buffer is created, and that buffer is then narrowed to the
  4909. example at point and switched to the correct language mode. When done,
  4910. exit by killing the buffer with \\[org-edit-src-exit]."
  4911. (interactive)
  4912. (let ((line (org-current-line))
  4913. (case-fold-search t)
  4914. (msg (substitute-command-keys
  4915. "Edit, then exit with C-c ' (C-c and single quote)"))
  4916. (info (org-edit-src-find-region-and-lang))
  4917. (org-mode-p (eq major-mode 'org-mode))
  4918. beg end lang lang-f single)
  4919. (if (not info)
  4920. nil
  4921. (setq beg (nth 0 info)
  4922. end (nth 1 info)
  4923. lang (nth 2 info)
  4924. single (nth 3 info)
  4925. lang-f (intern (concat lang "-mode")))
  4926. (unless (functionp lang-f)
  4927. (error "No such language mode: %s" lang-f))
  4928. (goto-line line)
  4929. (if (get-buffer "*Org Edit Src Example*")
  4930. (kill-buffer "*Org Edit Src Example*"))
  4931. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4932. "*Org Edit Src Example*"))
  4933. (narrow-to-region beg end)
  4934. (remove-text-properties beg end '(display nil invisible nil
  4935. intangible nil))
  4936. (let ((org-inhibit-startup t))
  4937. (funcall lang-f))
  4938. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4939. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4940. (when org-mode-p
  4941. (goto-char (point-min))
  4942. (while (re-search-forward "^," nil t)
  4943. (replace-match "")))
  4944. (goto-line line)
  4945. (org-exit-edit-mode)
  4946. (org-set-local 'header-line-format msg)
  4947. (message "%s" msg)
  4948. t)))
  4949. (defun org-edit-fixed-width-region ()
  4950. "Edit the fixed-width ascii drawing at point.
  4951. This must be a region where each line starts with ca colon followed by
  4952. a space character.
  4953. An indirect buffer is created, and that buffer is then narrowed to the
  4954. example at point and switched to artist-mode. When done,
  4955. exit by killing the buffer with \\[org-edit-src-exit]."
  4956. (interactive)
  4957. (let ((line (org-current-line))
  4958. (case-fold-search t)
  4959. (msg (substitute-command-keys
  4960. "Edit, then exit with C-c ' (C-c and single quote)"))
  4961. (org-mode-p (eq major-mode 'org-mode))
  4962. beg end lang lang-f)
  4963. (beginning-of-line 1)
  4964. (if (looking-at "[ \t]*[^:\n \t]")
  4965. nil
  4966. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  4967. (setq beg (point) end (match-end 0))
  4968. (save-excursion
  4969. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  4970. (setq beg (point-at-bol 2))
  4971. (setq beg (point))))
  4972. (save-excursion
  4973. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  4974. (setq end (1- (match-beginning 0)))
  4975. (setq end (point))))
  4976. (goto-line line)
  4977. (if (get-buffer "*Org Edit Picture*")
  4978. (kill-buffer "*Org Edit Picture*"))
  4979. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4980. "*Org Edit Picture*"))
  4981. (narrow-to-region beg end)
  4982. (remove-text-properties beg end '(display nil invisible nil
  4983. intangible nil))
  4984. (when (fboundp 'font-lock-unfontify-region)
  4985. (font-lock-unfontify-region (point-min) (point-max)))
  4986. (cond
  4987. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  4988. (fundamental-mode)
  4989. (artist-mode 1))
  4990. (t (funcall org-edit-fixed-width-region-mode)))
  4991. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  4992. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4993. (set (make-local-variable 'org-edit-src-picture) t)
  4994. (goto-char (point-min))
  4995. (while (re-search-forward "^[ \t]*: " nil t)
  4996. (replace-match ""))
  4997. (goto-line line)
  4998. (org-exit-edit-mode)
  4999. (org-set-local 'header-line-format msg)
  5000. (message "%s" msg)
  5001. t))))
  5002. (defun org-edit-src-find-region-and-lang ()
  5003. "Find the region and language for a local edit.
  5004. Return a list with beginning and end of the region, a string representing
  5005. the language, a switch telling of the content should be in a single line."
  5006. (let ((re-list
  5007. (append
  5008. org-edit-src-region-extra
  5009. '(
  5010. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5011. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5012. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5013. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5014. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5015. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5016. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5017. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5018. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5019. ("^#\\+html:" "\n" "html" single-line)
  5020. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5021. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5022. ("^#\\+latex:" "\n" "latex" single-line)
  5023. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5024. ("^#\\+ascii:" "\n" "ascii" single-line)
  5025. )))
  5026. (pos (point))
  5027. re re1 re2 single beg end lang)
  5028. (catch 'exit
  5029. (while (setq entry (pop re-list))
  5030. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5031. single (nth 3 entry))
  5032. (save-excursion
  5033. (if (or (looking-at re1)
  5034. (re-search-backward re1 nil t))
  5035. (progn
  5036. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5037. (if (and (re-search-forward re2 nil t)
  5038. (>= (match-end 0) pos))
  5039. (throw 'exit (list beg (match-beginning 0) lang single))))
  5040. (if (or (looking-at re2)
  5041. (re-search-forward re2 nil t))
  5042. (progn
  5043. (setq end (match-beginning 0))
  5044. (if (and (re-search-backward re1 nil t)
  5045. (<= (match-beginning 0) pos))
  5046. (throw 'exit
  5047. (list (match-end 0) end
  5048. (org-edit-src-get-lang lang) single)))))))))))
  5049. (defun org-edit-src-get-lang (lang)
  5050. "Extract the src language."
  5051. (let ((m (match-string 0)))
  5052. (cond
  5053. ((stringp lang) lang)
  5054. ((integerp lang) (match-string lang))
  5055. ((and (eq lang 'lang)
  5056. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5057. (match-string 1 m))
  5058. ((and (eq lang 'style)
  5059. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5060. (match-string 1 m))
  5061. (t "fundamental"))))
  5062. (defun org-edit-src-exit ()
  5063. "Exit special edit and protect problematic lines."
  5064. (interactive)
  5065. (unless (buffer-base-buffer (current-buffer))
  5066. (error "This is not an indirect buffer, something is wrong..."))
  5067. (unless (> (point-min) 1)
  5068. (error "This buffer is not narrowed, something is wrong..."))
  5069. (goto-char (point-min))
  5070. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5071. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5072. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5073. (goto-char (point-min))
  5074. (while (re-search-forward "\n" nil t)
  5075. (replace-match " "))
  5076. (goto-char (point-min))
  5077. (if (looking-at "\\s-*") (replace-match " "))
  5078. (if (re-search-forward "\\s-+\\'" nil t)
  5079. (replace-match "")))
  5080. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5081. (goto-char (point-min))
  5082. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5083. (replace-match ",\\1"))
  5084. (when font-lock-mode
  5085. (font-lock-unfontify-region (point-min) (point-max)))
  5086. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5087. (when (org-bound-and-true-p org-edit-src-picture)
  5088. (goto-char (point-min))
  5089. (while (re-search-forward "^" nil t)
  5090. (replace-match ": "))
  5091. (when font-lock-mode
  5092. (font-lock-unfontify-region (point-min) (point-max)))
  5093. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5094. (kill-buffer (current-buffer))
  5095. (and (org-mode-p) (org-restart-font-lock)))
  5096. ;;; The orgstruct minor mode
  5097. ;; Define a minor mode which can be used in other modes in order to
  5098. ;; integrate the org-mode structure editing commands.
  5099. ;; This is really a hack, because the org-mode structure commands use
  5100. ;; keys which normally belong to the major mode. Here is how it
  5101. ;; works: The minor mode defines all the keys necessary to operate the
  5102. ;; structure commands, but wraps the commands into a function which
  5103. ;; tests if the cursor is currently at a headline or a plain list
  5104. ;; item. If that is the case, the structure command is used,
  5105. ;; temporarily setting many Org-mode variables like regular
  5106. ;; expressions for filling etc. However, when any of those keys is
  5107. ;; used at a different location, function uses `key-binding' to look
  5108. ;; up if the key has an associated command in another currently active
  5109. ;; keymap (minor modes, major mode, global), and executes that
  5110. ;; command. There might be problems if any of the keys is otherwise
  5111. ;; used as a prefix key.
  5112. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5113. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5114. ;; addresses this by checking explicitly for both bindings.
  5115. (defvar orgstruct-mode-map (make-sparse-keymap)
  5116. "Keymap for the minor `orgstruct-mode'.")
  5117. (defvar org-local-vars nil
  5118. "List of local variables, for use by `orgstruct-mode'")
  5119. ;;;###autoload
  5120. (define-minor-mode orgstruct-mode
  5121. "Toggle the minor more `orgstruct-mode'.
  5122. This mode is for using Org-mode structure commands in other modes.
  5123. The following key behave as if Org-mode was active, if the cursor
  5124. is on a headline, or on a plain list item (both in the definition
  5125. of Org-mode).
  5126. M-up Move entry/item up
  5127. M-down Move entry/item down
  5128. M-left Promote
  5129. M-right Demote
  5130. M-S-up Move entry/item up
  5131. M-S-down Move entry/item down
  5132. M-S-left Promote subtree
  5133. M-S-right Demote subtree
  5134. M-q Fill paragraph and items like in Org-mode
  5135. C-c ^ Sort entries
  5136. C-c - Cycle list bullet
  5137. TAB Cycle item visibility
  5138. M-RET Insert new heading/item
  5139. S-M-RET Insert new TODO heading / Chekbox item
  5140. C-c C-c Set tags / toggle checkbox"
  5141. nil " OrgStruct" nil
  5142. (org-load-modules-maybe)
  5143. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5144. ;;;###autoload
  5145. (defun turn-on-orgstruct ()
  5146. "Unconditionally turn on `orgstruct-mode'."
  5147. (orgstruct-mode 1))
  5148. ;;;###autoload
  5149. (defun turn-on-orgstruct++ ()
  5150. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5151. In addition to setting orgstruct-mode, this also exports all indentation and
  5152. autofilling variables from org-mode into the buffer. Note that turning
  5153. off orgstruct-mode will *not* remove these additional settings."
  5154. (orgstruct-mode 1)
  5155. (let (var val)
  5156. (mapc
  5157. (lambda (x)
  5158. (when (string-match
  5159. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5160. (symbol-name (car x)))
  5161. (setq var (car x) val (nth 1 x))
  5162. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5163. org-local-vars)))
  5164. (defun orgstruct-error ()
  5165. "Error when there is no default binding for a structure key."
  5166. (interactive)
  5167. (error "This key has no function outside structure elements"))
  5168. (defun orgstruct-setup ()
  5169. "Setup orgstruct keymaps."
  5170. (let ((nfunc 0)
  5171. (bindings
  5172. (list
  5173. '([(meta up)] org-metaup)
  5174. '([(meta down)] org-metadown)
  5175. '([(meta left)] org-metaleft)
  5176. '([(meta right)] org-metaright)
  5177. '([(meta shift up)] org-shiftmetaup)
  5178. '([(meta shift down)] org-shiftmetadown)
  5179. '([(meta shift left)] org-shiftmetaleft)
  5180. '([(meta shift right)] org-shiftmetaright)
  5181. '([(shift up)] org-shiftup)
  5182. '([(shift down)] org-shiftdown)
  5183. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5184. '("\M-q" fill-paragraph)
  5185. '("\C-c^" org-sort)
  5186. '("\C-c-" org-cycle-list-bullet)))
  5187. elt key fun cmd)
  5188. (while (setq elt (pop bindings))
  5189. (setq nfunc (1+ nfunc))
  5190. (setq key (org-key (car elt))
  5191. fun (nth 1 elt)
  5192. cmd (orgstruct-make-binding fun nfunc key))
  5193. (org-defkey orgstruct-mode-map key cmd))
  5194. ;; Special treatment needed for TAB and RET
  5195. (org-defkey orgstruct-mode-map [(tab)]
  5196. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5197. (org-defkey orgstruct-mode-map "\C-i"
  5198. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5199. (org-defkey orgstruct-mode-map "\M-\C-m"
  5200. (orgstruct-make-binding 'org-insert-heading 105
  5201. "\M-\C-m" [(meta return)]))
  5202. (org-defkey orgstruct-mode-map [(meta return)]
  5203. (orgstruct-make-binding 'org-insert-heading 106
  5204. [(meta return)] "\M-\C-m"))
  5205. (org-defkey orgstruct-mode-map [(shift meta return)]
  5206. (orgstruct-make-binding 'org-insert-todo-heading 107
  5207. [(meta return)] "\M-\C-m"))
  5208. (unless org-local-vars
  5209. (setq org-local-vars (org-get-local-variables)))
  5210. t))
  5211. (defun orgstruct-make-binding (fun n &rest keys)
  5212. "Create a function for binding in the structure minor mode.
  5213. FUN is the command to call inside a table. N is used to create a unique
  5214. command name. KEYS are keys that should be checked in for a command
  5215. to execute outside of tables."
  5216. (eval
  5217. (list 'defun
  5218. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5219. '(arg)
  5220. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5221. "Outside of structure, run the binding of `"
  5222. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5223. "'.")
  5224. '(interactive "p")
  5225. (list 'if
  5226. '(org-context-p 'headline 'item)
  5227. (list 'org-run-like-in-org-mode (list 'quote fun))
  5228. (list 'let '(orgstruct-mode)
  5229. (list 'call-interactively
  5230. (append '(or)
  5231. (mapcar (lambda (k)
  5232. (list 'key-binding k))
  5233. keys)
  5234. '('orgstruct-error))))))))
  5235. (defun org-context-p (&rest contexts)
  5236. "Check if local context is any of CONTEXTS.
  5237. Possible values in the list of contexts are `table', `headline', and `item'."
  5238. (let ((pos (point)))
  5239. (goto-char (point-at-bol))
  5240. (prog1 (or (and (memq 'table contexts)
  5241. (looking-at "[ \t]*|"))
  5242. (and (memq 'headline contexts)
  5243. ;;????????? (looking-at "\\*+"))
  5244. (looking-at outline-regexp))
  5245. (and (memq 'item contexts)
  5246. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5247. (goto-char pos))))
  5248. (defun org-get-local-variables ()
  5249. "Return a list of all local variables in an org-mode buffer."
  5250. (let (varlist)
  5251. (with-current-buffer (get-buffer-create "*Org tmp*")
  5252. (erase-buffer)
  5253. (org-mode)
  5254. (setq varlist (buffer-local-variables)))
  5255. (kill-buffer "*Org tmp*")
  5256. (delq nil
  5257. (mapcar
  5258. (lambda (x)
  5259. (setq x
  5260. (if (symbolp x)
  5261. (list x)
  5262. (list (car x) (list 'quote (cdr x)))))
  5263. (if (string-match
  5264. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5265. (symbol-name (car x)))
  5266. x nil))
  5267. varlist))))
  5268. ;;;###autoload
  5269. (defun org-run-like-in-org-mode (cmd)
  5270. (org-load-modules-maybe)
  5271. (unless org-local-vars
  5272. (setq org-local-vars (org-get-local-variables)))
  5273. (eval (list 'let org-local-vars
  5274. (list 'call-interactively (list 'quote cmd)))))
  5275. ;;;; Archiving
  5276. (defun org-get-category (&optional pos)
  5277. "Get the category applying to position POS."
  5278. (get-text-property (or pos (point)) 'org-category))
  5279. (defun org-refresh-category-properties ()
  5280. "Refresh category text properties in the buffer."
  5281. (let ((def-cat (cond
  5282. ((null org-category)
  5283. (if buffer-file-name
  5284. (file-name-sans-extension
  5285. (file-name-nondirectory buffer-file-name))
  5286. "???"))
  5287. ((symbolp org-category) (symbol-name org-category))
  5288. (t org-category)))
  5289. beg end cat pos optionp)
  5290. (org-unmodified
  5291. (save-excursion
  5292. (save-restriction
  5293. (widen)
  5294. (goto-char (point-min))
  5295. (put-text-property (point) (point-max) 'org-category def-cat)
  5296. (while (re-search-forward
  5297. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5298. (setq pos (match-end 0)
  5299. optionp (equal (char-after (match-beginning 0)) ?#)
  5300. cat (org-trim (match-string 2)))
  5301. (if optionp
  5302. (setq beg (point-at-bol) end (point-max))
  5303. (org-back-to-heading t)
  5304. (setq beg (point) end (org-end-of-subtree t t)))
  5305. (put-text-property beg end 'org-category cat)
  5306. (goto-char pos)))))))
  5307. ;;;; Link Stuff
  5308. ;;; Link abbreviations
  5309. (defun org-link-expand-abbrev (link)
  5310. "Apply replacements as defined in `org-link-abbrev-alist."
  5311. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5312. (let* ((key (match-string 1 link))
  5313. (as (or (assoc key org-link-abbrev-alist-local)
  5314. (assoc key org-link-abbrev-alist)))
  5315. (tag (and (match-end 2) (match-string 3 link)))
  5316. rpl)
  5317. (if (not as)
  5318. link
  5319. (setq rpl (cdr as))
  5320. (cond
  5321. ((symbolp rpl) (funcall rpl tag))
  5322. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5323. (t (concat rpl tag)))))
  5324. link))
  5325. ;;; Storing and inserting links
  5326. (defvar org-insert-link-history nil
  5327. "Minibuffer history for links inserted with `org-insert-link'.")
  5328. (defvar org-stored-links nil
  5329. "Contains the links stored with `org-store-link'.")
  5330. (defvar org-store-link-plist nil
  5331. "Plist with info about the most recently link created with `org-store-link'.")
  5332. (defvar org-link-protocols nil
  5333. "Link protocols added to Org-mode using `org-add-link-type'.")
  5334. (defvar org-store-link-functions nil
  5335. "List of functions that are called to create and store a link.
  5336. Each function will be called in turn until one returns a non-nil
  5337. value. Each function should check if it is responsible for creating
  5338. this link (for example by looking at the major mode).
  5339. If not, it must exit and return nil.
  5340. If yes, it should return a non-nil value after a calling
  5341. `org-store-link-props' with a list of properties and values.
  5342. Special properties are:
  5343. :type The link prefix. like \"http\". This must be given.
  5344. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5345. This is obligatory as well.
  5346. :description Optional default description for the second pair
  5347. of brackets in an Org-mode link. The user can still change
  5348. this when inserting this link into an Org-mode buffer.
  5349. In addition to these, any additional properties can be specified
  5350. and then used in remember templates.")
  5351. (defun org-add-link-type (type &optional follow export)
  5352. "Add TYPE to the list of `org-link-types'.
  5353. Re-compute all regular expressions depending on `org-link-types'
  5354. FOLLOW and EXPORT are two functions.
  5355. FOLLOW should take the link path as the single argument and do whatever
  5356. is necessary to follow the link, for example find a file or display
  5357. a mail message.
  5358. EXPORT should format the link path for export to one of the export formats.
  5359. It should be a function accepting three arguments:
  5360. path the path of the link, the text after the prefix (like \"http:\")
  5361. desc the description of the link, if any, nil if there was no descripton
  5362. format the export format, a symbol like `html' or `latex'.
  5363. The function may use the FORMAT information to return different values
  5364. depending on the format. The return value will be put literally into
  5365. the exported file.
  5366. Org-mode has a built-in default for exporting links. If you are happy with
  5367. this default, there is no need to define an export function for the link
  5368. type. For a simple example of an export function, see `org-bbdb.el'."
  5369. (add-to-list 'org-link-types type t)
  5370. (org-make-link-regexps)
  5371. (if (assoc type org-link-protocols)
  5372. (setcdr (assoc type org-link-protocols) (list follow export))
  5373. (push (list type follow export) org-link-protocols)))
  5374. ;;;###autoload
  5375. (defun org-store-link (arg)
  5376. "\\<org-mode-map>Store an org-link to the current location.
  5377. This link is added to `org-stored-links' and can later be inserted
  5378. into an org-buffer with \\[org-insert-link].
  5379. For some link types, a prefix arg is interpreted:
  5380. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5381. For file links, arg negates `org-context-in-file-links'."
  5382. (interactive "P")
  5383. (org-load-modules-maybe)
  5384. (setq org-store-link-plist nil) ; reset
  5385. (let (link cpltxt desc description search txt)
  5386. (cond
  5387. ((run-hook-with-args-until-success 'org-store-link-functions)
  5388. (setq link (plist-get org-store-link-plist :link)
  5389. desc (or (plist-get org-store-link-plist :description) link)))
  5390. ((eq major-mode 'calendar-mode)
  5391. (let ((cd (calendar-cursor-to-date)))
  5392. (setq link
  5393. (format-time-string
  5394. (car org-time-stamp-formats)
  5395. (apply 'encode-time
  5396. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5397. nil nil nil))))
  5398. (org-store-link-props :type "calendar" :date cd)))
  5399. ((eq major-mode 'w3-mode)
  5400. (setq cpltxt (url-view-url t)
  5401. link (org-make-link cpltxt))
  5402. (org-store-link-props :type "w3" :url (url-view-url t)))
  5403. ((eq major-mode 'w3m-mode)
  5404. (setq cpltxt (or w3m-current-title w3m-current-url)
  5405. link (org-make-link w3m-current-url))
  5406. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5407. ((setq search (run-hook-with-args-until-success
  5408. 'org-create-file-search-functions))
  5409. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5410. "::" search))
  5411. (setq cpltxt (or description link)))
  5412. ((eq major-mode 'image-mode)
  5413. (setq cpltxt (concat "file:"
  5414. (abbreviate-file-name buffer-file-name))
  5415. link (org-make-link cpltxt))
  5416. (org-store-link-props :type "image" :file buffer-file-name))
  5417. ((eq major-mode 'dired-mode)
  5418. ;; link to the file in the current line
  5419. (setq cpltxt (concat "file:"
  5420. (abbreviate-file-name
  5421. (expand-file-name
  5422. (dired-get-filename nil t))))
  5423. link (org-make-link cpltxt)))
  5424. ((and buffer-file-name (org-mode-p))
  5425. ;; Just link to current headline
  5426. (setq cpltxt (concat "file:"
  5427. (abbreviate-file-name buffer-file-name)))
  5428. ;; Add a context search string
  5429. (when (org-xor org-context-in-file-links arg)
  5430. ;; Check if we are on a target
  5431. (if (org-in-regexp "<<\\(.*?\\)>>")
  5432. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5433. (setq txt (cond
  5434. ((org-on-heading-p) nil)
  5435. ((org-region-active-p)
  5436. (buffer-substring (region-beginning) (region-end)))
  5437. (t nil)))
  5438. (when (or (null txt) (string-match "\\S-" txt))
  5439. (setq cpltxt
  5440. (concat cpltxt "::"
  5441. (condition-case nil
  5442. (org-make-org-heading-search-string txt)
  5443. (error "")))
  5444. desc "NONE"))))
  5445. (if (string-match "::\\'" cpltxt)
  5446. (setq cpltxt (substring cpltxt 0 -2)))
  5447. (setq link (org-make-link cpltxt)))
  5448. ((buffer-file-name (buffer-base-buffer))
  5449. ;; Just link to this file here.
  5450. (setq cpltxt (concat "file:"
  5451. (abbreviate-file-name
  5452. (buffer-file-name (buffer-base-buffer)))))
  5453. ;; Add a context string
  5454. (when (org-xor org-context-in-file-links arg)
  5455. (setq txt (if (org-region-active-p)
  5456. (buffer-substring (region-beginning) (region-end))
  5457. (buffer-substring (point-at-bol) (point-at-eol))))
  5458. ;; Only use search option if there is some text.
  5459. (when (string-match "\\S-" txt)
  5460. (setq cpltxt
  5461. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5462. desc "NONE")))
  5463. (setq link (org-make-link cpltxt)))
  5464. ((interactive-p)
  5465. (error "Cannot link to a buffer which is not visiting a file"))
  5466. (t (setq link nil)))
  5467. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5468. (setq link (or link cpltxt)
  5469. desc (or desc cpltxt))
  5470. (if (equal desc "NONE") (setq desc nil))
  5471. (if (and (interactive-p) link)
  5472. (progn
  5473. (setq org-stored-links
  5474. (cons (list link desc) org-stored-links))
  5475. (message "Stored: %s" (or desc link)))
  5476. (and link (org-make-link-string link desc)))))
  5477. (defun org-store-link-props (&rest plist)
  5478. "Store link properties, extract names and addresses."
  5479. (let (x adr)
  5480. (when (setq x (plist-get plist :from))
  5481. (setq adr (mail-extract-address-components x))
  5482. (plist-put plist :fromname (car adr))
  5483. (plist-put plist :fromaddress (nth 1 adr)))
  5484. (when (setq x (plist-get plist :to))
  5485. (setq adr (mail-extract-address-components x))
  5486. (plist-put plist :toname (car adr))
  5487. (plist-put plist :toaddress (nth 1 adr))))
  5488. (let ((from (plist-get plist :from))
  5489. (to (plist-get plist :to)))
  5490. (when (and from to org-from-is-user-regexp)
  5491. (plist-put plist :fromto
  5492. (if (string-match org-from-is-user-regexp from)
  5493. (concat "to %t")
  5494. (concat "from %f")))))
  5495. (setq org-store-link-plist plist))
  5496. (defun org-add-link-props (&rest plist)
  5497. "Add these properties to the link property list."
  5498. (let (key value)
  5499. (while plist
  5500. (setq key (pop plist) value (pop plist))
  5501. (setq org-store-link-plist
  5502. (plist-put org-store-link-plist key value)))))
  5503. (defun org-email-link-description (&optional fmt)
  5504. "Return the description part of an email link.
  5505. This takes information from `org-store-link-plist' and formats it
  5506. according to FMT (default from `org-email-link-description-format')."
  5507. (setq fmt (or fmt org-email-link-description-format))
  5508. (let* ((p org-store-link-plist)
  5509. (to (plist-get p :toaddress))
  5510. (from (plist-get p :fromaddress))
  5511. (table
  5512. (list
  5513. (cons "%c" (plist-get p :fromto))
  5514. (cons "%F" (plist-get p :from))
  5515. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5516. (cons "%T" (plist-get p :to))
  5517. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5518. (cons "%s" (plist-get p :subject))
  5519. (cons "%m" (plist-get p :message-id)))))
  5520. (when (string-match "%c" fmt)
  5521. ;; Check if the user wrote this message
  5522. (if (and org-from-is-user-regexp from to
  5523. (save-match-data (string-match org-from-is-user-regexp from)))
  5524. (setq fmt (replace-match "to %t" t t fmt))
  5525. (setq fmt (replace-match "from %f" t t fmt))))
  5526. (org-replace-escapes fmt table)))
  5527. (defun org-make-org-heading-search-string (&optional string heading)
  5528. "Make search string for STRING or current headline."
  5529. (interactive)
  5530. (let ((s (or string (org-get-heading))))
  5531. (unless (and string (not heading))
  5532. ;; We are using a headline, clean up garbage in there.
  5533. (if (string-match org-todo-regexp s)
  5534. (setq s (replace-match "" t t s)))
  5535. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5536. (setq s (replace-match "" t t s)))
  5537. (setq s (org-trim s))
  5538. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5539. org-comment-string "\\)") s)
  5540. (setq s (replace-match "" t t s)))
  5541. (while (string-match org-ts-regexp s)
  5542. (setq s (replace-match "" t t s))))
  5543. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5544. (setq s (replace-match " " t t s)))
  5545. (or string (setq s (concat "*" s))) ; Add * for headlines
  5546. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5547. (defun org-make-link (&rest strings)
  5548. "Concatenate STRINGS."
  5549. (apply 'concat strings))
  5550. (defun org-make-link-string (link &optional description)
  5551. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5552. (unless (string-match "\\S-" link)
  5553. (error "Empty link"))
  5554. (when (stringp description)
  5555. ;; Remove brackets from the description, they are fatal.
  5556. (while (string-match "\\[" description)
  5557. (setq description (replace-match "{" t t description)))
  5558. (while (string-match "\\]" description)
  5559. (setq description (replace-match "}" t t description))))
  5560. (when (equal (org-link-escape link) description)
  5561. ;; No description needed, it is identical
  5562. (setq description nil))
  5563. (when (and (not description)
  5564. (not (equal link (org-link-escape link))))
  5565. (setq description (org-extract-attributes link)))
  5566. (concat "[[" (org-link-escape link) "]"
  5567. (if description (concat "[" description "]") "")
  5568. "]"))
  5569. (defconst org-link-escape-chars
  5570. '((?\ . "%20")
  5571. (?\[ . "%5B")
  5572. (?\] . "%5D")
  5573. (?\340 . "%E0") ; `a
  5574. (?\342 . "%E2") ; ^a
  5575. (?\347 . "%E7") ; ,c
  5576. (?\350 . "%E8") ; `e
  5577. (?\351 . "%E9") ; 'e
  5578. (?\352 . "%EA") ; ^e
  5579. (?\356 . "%EE") ; ^i
  5580. (?\364 . "%F4") ; ^o
  5581. (?\371 . "%F9") ; `u
  5582. (?\373 . "%FB") ; ^u
  5583. (?\; . "%3B")
  5584. (?? . "%3F")
  5585. (?= . "%3D")
  5586. (?+ . "%2B")
  5587. )
  5588. "Association list of escapes for some characters problematic in links.
  5589. This is the list that is used for internal purposes.")
  5590. (defconst org-link-escape-chars-browser
  5591. '((?\ . "%20")) ; 32 for the SPC char
  5592. "Association list of escapes for some characters problematic in links.
  5593. This is the list that is used before handing over to the browser.")
  5594. (defun org-link-escape (text &optional table)
  5595. "Escape charaters in TEXT that are problematic for links."
  5596. (setq table (or table org-link-escape-chars))
  5597. (when text
  5598. (let ((re (mapconcat (lambda (x) (regexp-quote
  5599. (char-to-string (car x))))
  5600. table "\\|")))
  5601. (while (string-match re text)
  5602. (setq text
  5603. (replace-match
  5604. (cdr (assoc (string-to-char (match-string 0 text))
  5605. table))
  5606. t t text)))
  5607. text)))
  5608. (defun org-link-unescape (text &optional table)
  5609. "Reverse the action of `org-link-escape'."
  5610. (setq table (or table org-link-escape-chars))
  5611. (when text
  5612. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5613. table "\\|")))
  5614. (while (string-match re text)
  5615. (setq text
  5616. (replace-match
  5617. (char-to-string (car (rassoc (match-string 0 text) table)))
  5618. t t text)))
  5619. text)))
  5620. (defun org-xor (a b)
  5621. "Exclusive or."
  5622. (if a (not b) b))
  5623. (defun org-get-header (header)
  5624. "Find a header field in the current buffer."
  5625. (save-excursion
  5626. (goto-char (point-min))
  5627. (let ((case-fold-search t) s)
  5628. (cond
  5629. ((eq header 'from)
  5630. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5631. (setq s (match-string 1)))
  5632. (while (string-match "\"" s)
  5633. (setq s (replace-match "" t t s)))
  5634. (if (string-match "[<(].*" s)
  5635. (setq s (replace-match "" t t s))))
  5636. ((eq header 'message-id)
  5637. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5638. (setq s (match-string 1))))
  5639. ((eq header 'subject)
  5640. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5641. (setq s (match-string 1)))))
  5642. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5643. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5644. s)))
  5645. (defun org-fixup-message-id-for-http (s)
  5646. "Replace special characters in a message id, so it can be used in an http query."
  5647. (while (string-match "<" s)
  5648. (setq s (replace-match "%3C" t t s)))
  5649. (while (string-match ">" s)
  5650. (setq s (replace-match "%3E" t t s)))
  5651. (while (string-match "@" s)
  5652. (setq s (replace-match "%40" t t s)))
  5653. s)
  5654. ;;;###autoload
  5655. (defun org-insert-link-global ()
  5656. "Insert a link like Org-mode does.
  5657. This command can be called in any mode to insert a link in Org-mode syntax."
  5658. (interactive)
  5659. (org-load-modules-maybe)
  5660. (org-run-like-in-org-mode 'org-insert-link))
  5661. (defun org-insert-link (&optional complete-file link-location)
  5662. "Insert a link. At the prompt, enter the link.
  5663. Completion can be used to select a link previously stored with
  5664. `org-store-link'. When the empty string is entered (i.e. if you just
  5665. press RET at the prompt), the link defaults to the most recently
  5666. stored link. As SPC triggers completion in the minibuffer, you need to
  5667. use M-SPC or C-q SPC to force the insertion of a space character.
  5668. You will also be prompted for a description, and if one is given, it will
  5669. be displayed in the buffer instead of the link.
  5670. If there is already a link at point, this command will allow you to edit link
  5671. and description parts.
  5672. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5673. be selected using completion. The path to the file will be relative to the
  5674. current directory if the file is in the current directory or a subdirectory.
  5675. Otherwise, the link will be the absolute path as completed in the minibuffer
  5676. \(i.e. normally ~/path/to/file).
  5677. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5678. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5679. of `org-keep-stored-link-after-insertion'.
  5680. If `org-make-link-description-function' is non-nil, this function will be
  5681. called with the link target, and the result will be the default
  5682. link description.
  5683. If the LINK-LOCATION parameter is non-nil, this value will be
  5684. used as the link location instead of reading one interactively."
  5685. (interactive "P")
  5686. (let* ((wcf (current-window-configuration))
  5687. (region (if (org-region-active-p)
  5688. (buffer-substring (region-beginning) (region-end))))
  5689. (remove (and region (list (region-beginning) (region-end))))
  5690. (desc region)
  5691. tmphist ; byte-compile incorrectly complains about this
  5692. (link link-location)
  5693. entry file)
  5694. (cond
  5695. (link-location) ; specified by arg, just use it.
  5696. ((org-in-regexp org-bracket-link-regexp 1)
  5697. ;; We do have a link at point, and we are going to edit it.
  5698. (setq remove (list (match-beginning 0) (match-end 0)))
  5699. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5700. (setq link (read-string "Link: "
  5701. (org-link-unescape
  5702. (org-match-string-no-properties 1)))))
  5703. ((or (org-in-regexp org-angle-link-re)
  5704. (org-in-regexp org-plain-link-re))
  5705. ;; Convert to bracket link
  5706. (setq remove (list (match-beginning 0) (match-end 0))
  5707. link (read-string "Link: "
  5708. (org-remove-angle-brackets (match-string 0)))))
  5709. ((equal complete-file '(4))
  5710. ;; Completing read for file names.
  5711. (setq file (read-file-name "File: "))
  5712. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5713. (pwd1 (file-name-as-directory (abbreviate-file-name
  5714. (expand-file-name ".")))))
  5715. (cond
  5716. ((equal complete-file '(16))
  5717. (setq link (org-make-link
  5718. "file:"
  5719. (abbreviate-file-name (expand-file-name file)))))
  5720. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5721. (setq link (org-make-link "file:" (match-string 1 file))))
  5722. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5723. (expand-file-name file))
  5724. (setq link (org-make-link
  5725. "file:" (match-string 1 (expand-file-name file)))))
  5726. (t (setq link (org-make-link "file:" file))))))
  5727. (t
  5728. ;; Read link, with completion for stored links.
  5729. (with-output-to-temp-buffer "*Org Links*"
  5730. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5731. (when org-stored-links
  5732. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5733. (princ (mapconcat
  5734. (lambda (x)
  5735. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5736. (reverse org-stored-links) "\n"))))
  5737. (let ((cw (selected-window)))
  5738. (select-window (get-buffer-window "*Org Links*"))
  5739. (shrink-window-if-larger-than-buffer)
  5740. (setq truncate-lines t)
  5741. (select-window cw))
  5742. ;; Fake a link history, containing the stored links.
  5743. (setq tmphist (append (mapcar 'car org-stored-links)
  5744. org-insert-link-history))
  5745. (unwind-protect
  5746. (setq link (org-completing-read
  5747. "Link: "
  5748. (append
  5749. (mapcar (lambda (x) (list (concat (car x) ":")))
  5750. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5751. (mapcar (lambda (x) (list (concat x ":")))
  5752. org-link-types))
  5753. nil nil nil
  5754. 'tmphist
  5755. (or (car (car org-stored-links)))))
  5756. (set-window-configuration wcf)
  5757. (kill-buffer "*Org Links*"))
  5758. (setq entry (assoc link org-stored-links))
  5759. (or entry (push link org-insert-link-history))
  5760. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5761. (not org-keep-stored-link-after-insertion))
  5762. (setq org-stored-links (delq (assoc link org-stored-links)
  5763. org-stored-links)))
  5764. (setq desc (or desc (nth 1 entry)))))
  5765. (if (string-match org-plain-link-re link)
  5766. ;; URL-like link, normalize the use of angular brackets.
  5767. (setq link (org-make-link (org-remove-angle-brackets link))))
  5768. ;; Check if we are linking to the current file with a search option
  5769. ;; If yes, simplify the link by using only the search option.
  5770. (when (and buffer-file-name
  5771. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5772. (let* ((path (match-string 1 link))
  5773. (case-fold-search nil)
  5774. (search (match-string 2 link)))
  5775. (save-match-data
  5776. (if (equal (file-truename buffer-file-name) (file-truename path))
  5777. ;; We are linking to this same file, with a search option
  5778. (setq link search)))))
  5779. ;; Check if we can/should use a relative path. If yes, simplify the link
  5780. (when (string-match "\\<file:\\(.*\\)" link)
  5781. (let* ((path (match-string 1 link))
  5782. (origpath path)
  5783. (case-fold-search nil))
  5784. (cond
  5785. ((eq org-link-file-path-type 'absolute)
  5786. (setq path (abbreviate-file-name (expand-file-name path))))
  5787. ((eq org-link-file-path-type 'noabbrev)
  5788. (setq path (expand-file-name path)))
  5789. ((eq org-link-file-path-type 'relative)
  5790. (setq path (file-relative-name path)))
  5791. (t
  5792. (save-match-data
  5793. (if (string-match (concat "^" (regexp-quote
  5794. (file-name-as-directory
  5795. (expand-file-name "."))))
  5796. (expand-file-name path))
  5797. ;; We are linking a file with relative path name.
  5798. (setq path (substring (expand-file-name path)
  5799. (match-end 0)))))))
  5800. (setq link (concat "file:" path))
  5801. (if (equal desc origpath)
  5802. (setq desc path))))
  5803. (if org-make-link-description-function
  5804. (setq desc (funcall org-make-link-description-function link desc)))
  5805. (setq desc (read-string "Description: " desc))
  5806. (unless (string-match "\\S-" desc) (setq desc nil))
  5807. (if remove (apply 'delete-region remove))
  5808. (insert (org-make-link-string link desc))))
  5809. (defun org-completing-read (&rest args)
  5810. (let ((minibuffer-local-completion-map
  5811. (copy-keymap minibuffer-local-completion-map)))
  5812. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5813. (apply 'completing-read args)))
  5814. (defun org-extract-attributes (s)
  5815. "Extract the attributes cookie from a string and set as text property."
  5816. (let (a attr (start 0) key value)
  5817. (save-match-data
  5818. (when (string-match "{{\\([^}]+\\)}}$" s)
  5819. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5820. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5821. (setq key (match-string 1 a) value (match-string 2 a)
  5822. start (match-end 0)
  5823. attr (plist-put attr (intern key) value))))
  5824. (org-add-props s nil 'org-attributes attr))
  5825. s))
  5826. (defun org-attributes-to-string (plist)
  5827. "Format a property list into an HTML attribute list."
  5828. (let ((s "") key value)
  5829. (while plist
  5830. (setq key (pop plist) value (pop plist))
  5831. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5832. s))
  5833. ;;; Opening/following a link
  5834. (defvar org-link-search-failed nil)
  5835. (defun org-next-link ()
  5836. "Move forward to the next link.
  5837. If the link is in hidden text, expose it."
  5838. (interactive)
  5839. (when (and org-link-search-failed (eq this-command last-command))
  5840. (goto-char (point-min))
  5841. (message "Link search wrapped back to beginning of buffer"))
  5842. (setq org-link-search-failed nil)
  5843. (let* ((pos (point))
  5844. (ct (org-context))
  5845. (a (assoc :link ct)))
  5846. (if a (goto-char (nth 2 a)))
  5847. (if (re-search-forward org-any-link-re nil t)
  5848. (progn
  5849. (goto-char (match-beginning 0))
  5850. (if (org-invisible-p) (org-show-context)))
  5851. (goto-char pos)
  5852. (setq org-link-search-failed t)
  5853. (error "No further link found"))))
  5854. (defun org-previous-link ()
  5855. "Move backward to the previous link.
  5856. If the link is in hidden text, expose it."
  5857. (interactive)
  5858. (when (and org-link-search-failed (eq this-command last-command))
  5859. (goto-char (point-max))
  5860. (message "Link search wrapped back to end of buffer"))
  5861. (setq org-link-search-failed nil)
  5862. (let* ((pos (point))
  5863. (ct (org-context))
  5864. (a (assoc :link ct)))
  5865. (if a (goto-char (nth 1 a)))
  5866. (if (re-search-backward org-any-link-re nil t)
  5867. (progn
  5868. (goto-char (match-beginning 0))
  5869. (if (org-invisible-p) (org-show-context)))
  5870. (goto-char pos)
  5871. (setq org-link-search-failed t)
  5872. (error "No further link found"))))
  5873. (defun org-find-file-at-mouse (ev)
  5874. "Open file link or URL at mouse."
  5875. (interactive "e")
  5876. (mouse-set-point ev)
  5877. (org-open-at-point 'in-emacs))
  5878. (defun org-open-at-mouse (ev)
  5879. "Open file link or URL at mouse."
  5880. (interactive "e")
  5881. (mouse-set-point ev)
  5882. (org-open-at-point))
  5883. (defvar org-window-config-before-follow-link nil
  5884. "The window configuration before following a link.
  5885. This is saved in case the need arises to restore it.")
  5886. (defvar org-open-link-marker (make-marker)
  5887. "Marker pointing to the location where `org-open-at-point; was called.")
  5888. ;;;###autoload
  5889. (defun org-open-at-point-global ()
  5890. "Follow a link like Org-mode does.
  5891. This command can be called in any mode to follow a link that has
  5892. Org-mode syntax."
  5893. (interactive)
  5894. (org-run-like-in-org-mode 'org-open-at-point))
  5895. ;;;###autoload
  5896. (defun org-open-link-from-string (s &optional arg)
  5897. "Open a link in the string S, as if it was in Org-mode."
  5898. (interactive "sLink: \nP")
  5899. (with-temp-buffer
  5900. (let ((org-inhibit-startup t))
  5901. (org-mode)
  5902. (insert s)
  5903. (goto-char (point-min))
  5904. (org-open-at-point arg))))
  5905. (defun org-open-at-point (&optional in-emacs)
  5906. "Open link at or after point.
  5907. If there is no link at point, this function will search forward up to
  5908. the end of the current subtree.
  5909. Normally, files will be opened by an appropriate application. If the
  5910. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  5911. (interactive "P")
  5912. (org-load-modules-maybe)
  5913. (move-marker org-open-link-marker (point))
  5914. (setq org-window-config-before-follow-link (current-window-configuration))
  5915. (org-remove-occur-highlights nil nil t)
  5916. (if (org-at-timestamp-p t)
  5917. (org-follow-timestamp-link)
  5918. (let (type path link line search (pos (point)))
  5919. (catch 'match
  5920. (save-excursion
  5921. (skip-chars-forward "^]\n\r")
  5922. (when (org-in-regexp org-bracket-link-regexp)
  5923. (setq link (org-extract-attributes
  5924. (org-link-unescape (org-match-string-no-properties 1))))
  5925. (while (string-match " *\n *" link)
  5926. (setq link (replace-match " " t t link)))
  5927. (setq link (org-link-expand-abbrev link))
  5928. (cond
  5929. ((or (file-name-absolute-p link)
  5930. (string-match "^\\.\\.?/" link))
  5931. (setq type "file" path link))
  5932. ((string-match org-link-re-with-space2 link)
  5933. (setq type (match-string 1 link) path (match-string 2 link)))
  5934. (t (setq type "thisfile" path link)))
  5935. (throw 'match t)))
  5936. (when (get-text-property (point) 'org-linked-text)
  5937. (setq type "thisfile"
  5938. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  5939. (1+ (point)) (point))
  5940. path (buffer-substring
  5941. (previous-single-property-change pos 'org-linked-text)
  5942. (next-single-property-change pos 'org-linked-text)))
  5943. (throw 'match t))
  5944. (save-excursion
  5945. (when (or (org-in-regexp org-angle-link-re)
  5946. (org-in-regexp org-plain-link-re))
  5947. (setq type (match-string 1) path (match-string 2))
  5948. (throw 'match t)))
  5949. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  5950. (setq type "tree-match"
  5951. path (match-string 1))
  5952. (throw 'match t))
  5953. (save-excursion
  5954. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  5955. (setq type "tags"
  5956. path (match-string 1))
  5957. (while (string-match ":" path)
  5958. (setq path (replace-match "+" t t path)))
  5959. (throw 'match t))))
  5960. (unless path
  5961. (error "No link found"))
  5962. ;; Remove any trailing spaces in path
  5963. (if (string-match " +\\'" path)
  5964. (setq path (replace-match "" t t path)))
  5965. (cond
  5966. ((assoc type org-link-protocols)
  5967. (funcall (nth 1 (assoc type org-link-protocols)) path))
  5968. ((equal type "mailto")
  5969. (let ((cmd (car org-link-mailto-program))
  5970. (args (cdr org-link-mailto-program)) args1
  5971. (address path) (subject "") a)
  5972. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  5973. (setq address (match-string 1 path)
  5974. subject (org-link-escape (match-string 2 path))))
  5975. (while args
  5976. (cond
  5977. ((not (stringp (car args))) (push (pop args) args1))
  5978. (t (setq a (pop args))
  5979. (if (string-match "%a" a)
  5980. (setq a (replace-match address t t a)))
  5981. (if (string-match "%s" a)
  5982. (setq a (replace-match subject t t a)))
  5983. (push a args1))))
  5984. (apply cmd (nreverse args1))))
  5985. ((member type '("http" "https" "ftp" "news"))
  5986. (browse-url (concat type ":" (org-link-escape
  5987. path org-link-escape-chars-browser))))
  5988. ((member type '("message"))
  5989. (browse-url (concat type ":" path)))
  5990. ((string= type "tags")
  5991. (org-tags-view in-emacs path))
  5992. ((string= type "thisfile")
  5993. (if in-emacs
  5994. (switch-to-buffer-other-window
  5995. (org-get-buffer-for-internal-link (current-buffer)))
  5996. (org-mark-ring-push))
  5997. (let ((cmd `(org-link-search
  5998. ,path
  5999. ,(cond ((equal in-emacs '(4)) 'occur)
  6000. ((equal in-emacs '(16)) 'org-occur)
  6001. (t nil))
  6002. ,pos)))
  6003. (condition-case nil (eval cmd)
  6004. (error (progn (widen) (eval cmd))))))
  6005. ((string= type "tree-match")
  6006. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6007. ((string= type "file")
  6008. (if (string-match "::\\([0-9]+\\)\\'" path)
  6009. (setq line (string-to-number (match-string 1 path))
  6010. path (substring path 0 (match-beginning 0)))
  6011. (if (string-match "::\\(.+\\)\\'" path)
  6012. (setq search (match-string 1 path)
  6013. path (substring path 0 (match-beginning 0)))))
  6014. (if (string-match "[*?{]" (file-name-nondirectory path))
  6015. (dired path)
  6016. (org-open-file path in-emacs line search)))
  6017. ((string= type "news")
  6018. (require 'org-gnus)
  6019. (org-gnus-follow-link path))
  6020. ((string= type "shell")
  6021. (let ((cmd path))
  6022. (if (or (not org-confirm-shell-link-function)
  6023. (funcall org-confirm-shell-link-function
  6024. (format "Execute \"%s\" in shell? "
  6025. (org-add-props cmd nil
  6026. 'face 'org-warning))))
  6027. (progn
  6028. (message "Executing %s" cmd)
  6029. (shell-command cmd))
  6030. (error "Abort"))))
  6031. ((string= type "elisp")
  6032. (let ((cmd path))
  6033. (if (or (not org-confirm-elisp-link-function)
  6034. (funcall org-confirm-elisp-link-function
  6035. (format "Execute \"%s\" as elisp? "
  6036. (org-add-props cmd nil
  6037. 'face 'org-warning))))
  6038. (message "%s => %s" cmd (eval (read cmd)))
  6039. (error "Abort"))))
  6040. (t
  6041. (browse-url-at-point)))))
  6042. (move-marker org-open-link-marker nil)
  6043. (run-hook-with-args 'org-follow-link-hook))
  6044. ;;;; Time estimates
  6045. (defun org-get-effort (&optional pom)
  6046. "Get the effort estimate for the current entry."
  6047. (org-entry-get pom org-effort-property))
  6048. ;;; File search
  6049. (defvar org-create-file-search-functions nil
  6050. "List of functions to construct the right search string for a file link.
  6051. These functions are called in turn with point at the location to
  6052. which the link should point.
  6053. A function in the hook should first test if it would like to
  6054. handle this file type, for example by checking the major-mode or
  6055. the file extension. If it decides not to handle this file, it
  6056. should just return nil to give other functions a chance. If it
  6057. does handle the file, it must return the search string to be used
  6058. when following the link. The search string will be part of the
  6059. file link, given after a double colon, and `org-open-at-point'
  6060. will automatically search for it. If special measures must be
  6061. taken to make the search successful, another function should be
  6062. added to the companion hook `org-execute-file-search-functions',
  6063. which see.
  6064. A function in this hook may also use `setq' to set the variable
  6065. `description' to provide a suggestion for the descriptive text to
  6066. be used for this link when it gets inserted into an Org-mode
  6067. buffer with \\[org-insert-link].")
  6068. (defvar org-execute-file-search-functions nil
  6069. "List of functions to execute a file search triggered by a link.
  6070. Functions added to this hook must accept a single argument, the
  6071. search string that was part of the file link, the part after the
  6072. double colon. The function must first check if it would like to
  6073. handle this search, for example by checking the major-mode or the
  6074. file extension. If it decides not to handle this search, it
  6075. should just return nil to give other functions a chance. If it
  6076. does handle the search, it must return a non-nil value to keep
  6077. other functions from trying.
  6078. Each function can access the current prefix argument through the
  6079. variable `current-prefix-argument'. Note that a single prefix is
  6080. used to force opening a link in Emacs, so it may be good to only
  6081. use a numeric or double prefix to guide the search function.
  6082. In case this is needed, a function in this hook can also restore
  6083. the window configuration before `org-open-at-point' was called using:
  6084. (set-window-configuration org-window-config-before-follow-link)")
  6085. (defun org-link-search (s &optional type avoid-pos)
  6086. "Search for a link search option.
  6087. If S is surrounded by forward slashes, it is interpreted as a
  6088. regular expression. In org-mode files, this will create an `org-occur'
  6089. sparse tree. In ordinary files, `occur' will be used to list matches.
  6090. If the current buffer is in `dired-mode', grep will be used to search
  6091. in all files. If AVOID-POS is given, ignore matches near that position."
  6092. (let ((case-fold-search t)
  6093. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6094. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6095. (append '(("") (" ") ("\t") ("\n"))
  6096. org-emphasis-alist)
  6097. "\\|") "\\)"))
  6098. (pos (point))
  6099. (pre nil) (post nil)
  6100. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6101. (cond
  6102. ;; First check if there are any special
  6103. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6104. ;; Now try the builtin stuff
  6105. ((save-excursion
  6106. (goto-char (point-min))
  6107. (and
  6108. (re-search-forward
  6109. (concat "<<" (regexp-quote s0) ">>") nil t)
  6110. (setq type 'dedicated
  6111. pos (match-beginning 0))))
  6112. ;; There is an exact target for this
  6113. (goto-char pos))
  6114. ((string-match "^/\\(.*\\)/$" s)
  6115. ;; A regular expression
  6116. (cond
  6117. ((org-mode-p)
  6118. (org-occur (match-string 1 s)))
  6119. ;;((eq major-mode 'dired-mode)
  6120. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6121. (t (org-do-occur (match-string 1 s)))))
  6122. (t
  6123. ;; A normal search strings
  6124. (when (equal (string-to-char s) ?*)
  6125. ;; Anchor on headlines, post may include tags.
  6126. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6127. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6128. s (substring s 1)))
  6129. (remove-text-properties
  6130. 0 (length s)
  6131. '(face nil mouse-face nil keymap nil fontified nil) s)
  6132. ;; Make a series of regular expressions to find a match
  6133. (setq words (org-split-string s "[ \n\r\t]+")
  6134. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6135. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6136. "\\)" markers)
  6137. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6138. re2a (concat "[ \t\r\n]" re2a_)
  6139. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6140. re4 (concat "[^a-zA-Z_]" re4_)
  6141. re1 (concat pre re2 post)
  6142. re3 (concat pre (if pre re4_ re4) post)
  6143. re5 (concat pre ".*" re4)
  6144. re2 (concat pre re2)
  6145. re2a (concat pre (if pre re2a_ re2a))
  6146. re4 (concat pre (if pre re4_ re4))
  6147. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6148. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6149. re5 "\\)"
  6150. ))
  6151. (cond
  6152. ((eq type 'org-occur) (org-occur reall))
  6153. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6154. (t (goto-char (point-min))
  6155. (setq type 'fuzzy)
  6156. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6157. (org-search-not-self 1 re1 nil t)
  6158. (org-search-not-self 1 re2 nil t)
  6159. (org-search-not-self 1 re2a nil t)
  6160. (org-search-not-self 1 re3 nil t)
  6161. (org-search-not-self 1 re4 nil t)
  6162. (org-search-not-self 1 re5 nil t)
  6163. )
  6164. (goto-char (match-beginning 1))
  6165. (goto-char pos)
  6166. (error "No match")))))
  6167. (t
  6168. ;; Normal string-search
  6169. (goto-char (point-min))
  6170. (if (search-forward s nil t)
  6171. (goto-char (match-beginning 0))
  6172. (error "No match"))))
  6173. (and (org-mode-p) (org-show-context 'link-search))
  6174. type))
  6175. (defun org-search-not-self (group &rest args)
  6176. "Execute `re-search-forward', but only accept matches that do not
  6177. enclose the position of `org-open-link-marker'."
  6178. (let ((m org-open-link-marker))
  6179. (catch 'exit
  6180. (while (apply 're-search-forward args)
  6181. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6182. (goto-char (match-end group))
  6183. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6184. (> (match-beginning 0) (marker-position m))
  6185. (< (match-end 0) (marker-position m)))
  6186. (save-match-data
  6187. (or (not (org-in-regexp
  6188. org-bracket-link-analytic-regexp 1))
  6189. (not (match-end 4)) ; no description
  6190. (and (<= (match-beginning 4) (point))
  6191. (>= (match-end 4) (point))))))
  6192. (throw 'exit (point))))))))
  6193. (defun org-get-buffer-for-internal-link (buffer)
  6194. "Return a buffer to be used for displaying the link target of internal links."
  6195. (cond
  6196. ((not org-display-internal-link-with-indirect-buffer)
  6197. buffer)
  6198. ((string-match "(Clone)$" (buffer-name buffer))
  6199. (message "Buffer is already a clone, not making another one")
  6200. ;; we also do not modify visibility in this case
  6201. buffer)
  6202. (t ; make a new indirect buffer for displaying the link
  6203. (let* ((bn (buffer-name buffer))
  6204. (ibn (concat bn "(Clone)"))
  6205. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6206. (with-current-buffer ib (org-overview))
  6207. ib))))
  6208. (defun org-do-occur (regexp &optional cleanup)
  6209. "Call the Emacs command `occur'.
  6210. If CLEANUP is non-nil, remove the printout of the regular expression
  6211. in the *Occur* buffer. This is useful if the regex is long and not useful
  6212. to read."
  6213. (occur regexp)
  6214. (when cleanup
  6215. (let ((cwin (selected-window)) win beg end)
  6216. (when (setq win (get-buffer-window "*Occur*"))
  6217. (select-window win))
  6218. (goto-char (point-min))
  6219. (when (re-search-forward "match[a-z]+" nil t)
  6220. (setq beg (match-end 0))
  6221. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6222. (setq end (1- (match-beginning 0)))))
  6223. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6224. (goto-char (point-min))
  6225. (select-window cwin))))
  6226. ;;; The mark ring for links jumps
  6227. (defvar org-mark-ring nil
  6228. "Mark ring for positions before jumps in Org-mode.")
  6229. (defvar org-mark-ring-last-goto nil
  6230. "Last position in the mark ring used to go back.")
  6231. ;; Fill and close the ring
  6232. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6233. (loop for i from 1 to org-mark-ring-length do
  6234. (push (make-marker) org-mark-ring))
  6235. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6236. org-mark-ring)
  6237. (defun org-mark-ring-push (&optional pos buffer)
  6238. "Put the current position or POS into the mark ring and rotate it."
  6239. (interactive)
  6240. (setq pos (or pos (point)))
  6241. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6242. (move-marker (car org-mark-ring)
  6243. (or pos (point))
  6244. (or buffer (current-buffer)))
  6245. (message "%s"
  6246. (substitute-command-keys
  6247. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6248. (defun org-mark-ring-goto (&optional n)
  6249. "Jump to the previous position in the mark ring.
  6250. With prefix arg N, jump back that many stored positions. When
  6251. called several times in succession, walk through the entire ring.
  6252. Org-mode commands jumping to a different position in the current file,
  6253. or to another Org-mode file, automatically push the old position
  6254. onto the ring."
  6255. (interactive "p")
  6256. (let (p m)
  6257. (if (eq last-command this-command)
  6258. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6259. (setq p org-mark-ring))
  6260. (setq org-mark-ring-last-goto p)
  6261. (setq m (car p))
  6262. (switch-to-buffer (marker-buffer m))
  6263. (goto-char m)
  6264. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6265. (defun org-remove-angle-brackets (s)
  6266. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6267. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6268. s)
  6269. (defun org-add-angle-brackets (s)
  6270. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6271. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6272. s)
  6273. (defun org-remove-double-quotes (s)
  6274. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6275. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6276. s)
  6277. ;;; Following specific links
  6278. (defun org-follow-timestamp-link ()
  6279. (cond
  6280. ((org-at-date-range-p t)
  6281. (let ((org-agenda-start-on-weekday)
  6282. (t1 (match-string 1))
  6283. (t2 (match-string 2)))
  6284. (setq t1 (time-to-days (org-time-string-to-time t1))
  6285. t2 (time-to-days (org-time-string-to-time t2)))
  6286. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6287. ((org-at-timestamp-p t)
  6288. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6289. (substring (match-string 1) 0 10)))
  6290. 1))
  6291. (t (error "This should not happen"))))
  6292. ;;; Following file links
  6293. (defvar org-wait nil)
  6294. (defun org-open-file (path &optional in-emacs line search)
  6295. "Open the file at PATH.
  6296. First, this expands any special file name abbreviations. Then the
  6297. configuration variable `org-file-apps' is checked if it contains an
  6298. entry for this file type, and if yes, the corresponding command is launched.
  6299. If no application is found, Emacs simply visits the file.
  6300. With optional argument IN-EMACS, Emacs will visit the file.
  6301. Optional LINE specifies a line to go to, optional SEARCH a string to
  6302. search for. If LINE or SEARCH is given, the file will always be
  6303. opened in Emacs.
  6304. If the file does not exist, an error is thrown."
  6305. (setq in-emacs (or in-emacs line search))
  6306. (let* ((file (if (equal path "")
  6307. buffer-file-name
  6308. (substitute-in-file-name (expand-file-name path))))
  6309. (apps (append org-file-apps (org-default-apps)))
  6310. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6311. (dirp (if remp nil (file-directory-p file)))
  6312. (file (if (and dirp org-open-directory-means-index-dot-org)
  6313. (concat (file-name-as-directory file) "index.org")
  6314. file))
  6315. (a-m-a-p (assq 'auto-mode apps))
  6316. (dfile (downcase file))
  6317. (old-buffer (current-buffer))
  6318. (old-pos (point))
  6319. (old-mode major-mode)
  6320. ext cmd)
  6321. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6322. (setq ext (match-string 1 dfile))
  6323. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6324. (setq ext (match-string 1 dfile))))
  6325. (if in-emacs
  6326. (setq cmd 'emacs)
  6327. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6328. (and dirp (cdr (assoc 'directory apps)))
  6329. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6330. 'string-match)
  6331. (cdr (assoc ext apps))
  6332. (cdr (assoc t apps)))))
  6333. (when (eq cmd 'default)
  6334. (setq cmd (cdr (assoc t apps))))
  6335. (when (eq cmd 'mailcap)
  6336. (require 'mailcap)
  6337. (mailcap-parse-mailcaps)
  6338. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6339. (command (mailcap-mime-info mime-type)))
  6340. (if (stringp command)
  6341. (setq cmd command)
  6342. (setq cmd 'emacs))))
  6343. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6344. (not (file-exists-p file))
  6345. (not org-open-non-existing-files))
  6346. (error "No such file: %s" file))
  6347. (cond
  6348. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6349. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6350. (while (string-match "['\"]%s['\"]" cmd)
  6351. (setq cmd (replace-match "%s" t t cmd)))
  6352. (while (string-match "%s" cmd)
  6353. (setq cmd (replace-match
  6354. (save-match-data
  6355. (shell-quote-argument
  6356. (convert-standard-filename file)))
  6357. t t cmd)))
  6358. (save-window-excursion
  6359. (start-process-shell-command cmd nil cmd)
  6360. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6361. ))
  6362. ((or (stringp cmd)
  6363. (eq cmd 'emacs))
  6364. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6365. (widen)
  6366. (if line (goto-line line)
  6367. (if search (org-link-search search))))
  6368. ((consp cmd)
  6369. (let ((file (convert-standard-filename file)))
  6370. (eval cmd)))
  6371. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6372. (and (org-mode-p) (eq old-mode 'org-mode)
  6373. (or (not (equal old-buffer (current-buffer)))
  6374. (not (equal old-pos (point))))
  6375. (org-mark-ring-push old-pos old-buffer))))
  6376. (defun org-default-apps ()
  6377. "Return the default applications for this operating system."
  6378. (cond
  6379. ((eq system-type 'darwin)
  6380. org-file-apps-defaults-macosx)
  6381. ((eq system-type 'windows-nt)
  6382. org-file-apps-defaults-windowsnt)
  6383. (t org-file-apps-defaults-gnu)))
  6384. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6385. "Convert extensions to regular expressions in the cars of LIST.
  6386. Also, weed out any non-string entries, because the return value is used
  6387. only for regexp matching.
  6388. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6389. point to the symbol `emacs', indicating that the file should
  6390. be opened in Emacs."
  6391. (append
  6392. (delq nil
  6393. (mapcar (lambda (x)
  6394. (if (not (stringp (car x)))
  6395. nil
  6396. (if (string-match "\\W" (car x))
  6397. x
  6398. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6399. list))
  6400. (if add-auto-mode
  6401. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6402. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6403. (defun org-file-remote-p (file)
  6404. "Test whether FILE specifies a location on a remote system.
  6405. Return non-nil if the location is indeed remote.
  6406. For example, the filename \"/user@host:/foo\" specifies a location
  6407. on the system \"/user@host:\"."
  6408. (cond ((fboundp 'file-remote-p)
  6409. (file-remote-p file))
  6410. ((fboundp 'tramp-handle-file-remote-p)
  6411. (tramp-handle-file-remote-p file))
  6412. ((and (boundp 'ange-ftp-name-format)
  6413. (string-match (car ange-ftp-name-format) file))
  6414. t)
  6415. (t nil)))
  6416. ;;;; Refiling
  6417. (defun org-get-org-file ()
  6418. "Read a filename, with default directory `org-directory'."
  6419. (let ((default (or org-default-notes-file remember-data-file)))
  6420. (read-file-name (format "File name [%s]: " default)
  6421. (file-name-as-directory org-directory)
  6422. default)))
  6423. (defun org-notes-order-reversed-p ()
  6424. "Check if the current file should receive notes in reversed order."
  6425. (cond
  6426. ((not org-reverse-note-order) nil)
  6427. ((eq t org-reverse-note-order) t)
  6428. ((not (listp org-reverse-note-order)) nil)
  6429. (t (catch 'exit
  6430. (let ((all org-reverse-note-order)
  6431. entry)
  6432. (while (setq entry (pop all))
  6433. (if (string-match (car entry) buffer-file-name)
  6434. (throw 'exit (cdr entry))))
  6435. nil)))))
  6436. (defvar org-refile-target-table nil
  6437. "The list of refile targets, created by `org-refile'.")
  6438. (defvar org-agenda-new-buffers nil
  6439. "Buffers created to visit agenda files.")
  6440. (defun org-get-refile-targets (&optional default-buffer)
  6441. "Produce a table with refile targets."
  6442. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6443. targets txt re files f desc descre)
  6444. (with-current-buffer (or default-buffer (current-buffer))
  6445. (while (setq entry (pop entries))
  6446. (setq files (car entry) desc (cdr entry))
  6447. (cond
  6448. ((null files) (setq files (list (current-buffer))))
  6449. ((eq files 'org-agenda-files)
  6450. (setq files (org-agenda-files 'unrestricted)))
  6451. ((and (symbolp files) (fboundp files))
  6452. (setq files (funcall files)))
  6453. ((and (symbolp files) (boundp files))
  6454. (setq files (symbol-value files))))
  6455. (if (stringp files) (setq files (list files)))
  6456. (cond
  6457. ((eq (car desc) :tag)
  6458. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6459. ((eq (car desc) :todo)
  6460. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6461. ((eq (car desc) :regexp)
  6462. (setq descre (cdr desc)))
  6463. ((eq (car desc) :level)
  6464. (setq descre (concat "^\\*\\{" (number-to-string
  6465. (if org-odd-levels-only
  6466. (1- (* 2 (cdr desc)))
  6467. (cdr desc)))
  6468. "\\}[ \t]")))
  6469. ((eq (car desc) :maxlevel)
  6470. (setq descre (concat "^\\*\\{1," (number-to-string
  6471. (if org-odd-levels-only
  6472. (1- (* 2 (cdr desc)))
  6473. (cdr desc)))
  6474. "\\}[ \t]")))
  6475. (t (error "Bad refiling target description %s" desc)))
  6476. (while (setq f (pop files))
  6477. (save-excursion
  6478. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6479. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6480. (save-excursion
  6481. (save-restriction
  6482. (widen)
  6483. (goto-char (point-min))
  6484. (while (re-search-forward descre nil t)
  6485. (goto-char (point-at-bol))
  6486. (when (looking-at org-complex-heading-regexp)
  6487. (setq txt (org-link-display-format (match-string 4))
  6488. re (concat "^" (regexp-quote
  6489. (buffer-substring (match-beginning 1)
  6490. (match-end 4)))))
  6491. (if (match-end 5) (setq re (concat re "[ \t]+"
  6492. (regexp-quote
  6493. (match-string 5)))))
  6494. (setq re (concat re "[ \t]*$"))
  6495. (when org-refile-use-outline-path
  6496. (setq txt (mapconcat 'org-protect-slash
  6497. (append
  6498. (if (eq org-refile-use-outline-path 'file)
  6499. (list (file-name-nondirectory
  6500. (buffer-file-name (buffer-base-buffer))))
  6501. (if (eq org-refile-use-outline-path 'full-file-path)
  6502. (list (buffer-file-name (buffer-base-buffer)))))
  6503. (org-get-outline-path)
  6504. (list txt))
  6505. "/")))
  6506. (push (list txt f re (point)) targets))
  6507. (goto-char (point-at-eol))))))))
  6508. (nreverse targets))))
  6509. (defun org-protect-slash (s)
  6510. (while (string-match "/" s)
  6511. (setq s (replace-match "\\" t t s)))
  6512. s)
  6513. (defun org-get-outline-path ()
  6514. "Return the outline path to the current entry, as a list."
  6515. (let (rtn)
  6516. (save-excursion
  6517. (while (org-up-heading-safe)
  6518. (when (looking-at org-complex-heading-regexp)
  6519. (push (org-match-string-no-properties 4) rtn)))
  6520. rtn)))
  6521. (defvar org-refile-history nil
  6522. "History for refiling operations.")
  6523. (defun org-refile (&optional goto default-buffer)
  6524. "Move the entry at point to another heading.
  6525. The list of target headings is compiled using the information in
  6526. `org-refile-targets', which see. This list is created before each use
  6527. and will therefore always be up-to-date.
  6528. At the target location, the entry is filed as a subitem of the target heading.
  6529. Depending on `org-reverse-note-order', the new subitem will either be the
  6530. first of the last subitem.
  6531. With prefix arg GOTO, the command will only visit the target location,
  6532. not actually move anything.
  6533. With a double prefix `C-u C-u', go to the location where the last refiling
  6534. operation has put the subtree."
  6535. (interactive "P")
  6536. (let* ((cbuf (current-buffer))
  6537. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6538. pos it nbuf file re level reversed)
  6539. (if (equal goto '(16))
  6540. (org-refile-goto-last-stored)
  6541. (when (setq it (org-refile-get-location
  6542. (if goto "Goto: " "Refile to: ") default-buffer))
  6543. (setq file (nth 1 it)
  6544. re (nth 2 it)
  6545. pos (nth 3 it))
  6546. (setq nbuf (or (find-buffer-visiting file)
  6547. (find-file-noselect file)))
  6548. (if goto
  6549. (progn
  6550. (switch-to-buffer nbuf)
  6551. (goto-char pos)
  6552. (org-show-context 'org-goto))
  6553. (org-copy-subtree 1 nil t)
  6554. (save-excursion
  6555. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6556. (find-file-noselect file))))
  6557. (setq reversed (org-notes-order-reversed-p))
  6558. (save-excursion
  6559. (save-restriction
  6560. (widen)
  6561. (goto-char pos)
  6562. (looking-at outline-regexp)
  6563. (setq level (org-get-valid-level (funcall outline-level) 1))
  6564. (goto-char
  6565. (if reversed
  6566. (or (outline-next-heading) (point-max))
  6567. (or (save-excursion (outline-get-next-sibling))
  6568. (org-end-of-subtree t t)
  6569. (point-max))))
  6570. (if (not (bolp)) (newline))
  6571. (bookmark-set "org-refile-last-stored")
  6572. (org-paste-subtree level))))
  6573. (org-cut-subtree)
  6574. (setq org-markers-to-move nil)
  6575. (message "Entry refiled to \"%s\"" (car it)))))))
  6576. (defun org-refile-goto-last-stored ()
  6577. "Go to the location where the last refile was stored."
  6578. (interactive)
  6579. (bookmark-jump "org-refile-last-stored")
  6580. (message "This is the location of the last refile"))
  6581. (defun org-refile-get-location (&optional prompt default-buffer)
  6582. "Prompt the user for a refile location, using PROMPT."
  6583. (let ((org-refile-targets org-refile-targets)
  6584. (org-refile-use-outline-path org-refile-use-outline-path))
  6585. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6586. (unless org-refile-target-table
  6587. (error "No refile targets"))
  6588. (let* ((cbuf (current-buffer))
  6589. (cfunc (if org-refile-use-outline-path
  6590. 'org-olpath-completing-read
  6591. 'completing-read))
  6592. (extra (if org-refile-use-outline-path "/" ""))
  6593. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6594. (fname (and filename (file-truename filename)))
  6595. (tbl (mapcar
  6596. (lambda (x)
  6597. (if (not (equal fname (file-truename (nth 1 x))))
  6598. (cons (concat (car x) extra " ("
  6599. (file-name-nondirectory (nth 1 x)) ")")
  6600. (cdr x))
  6601. (cons (concat (car x) extra) (cdr x))))
  6602. org-refile-target-table))
  6603. (completion-ignore-case t))
  6604. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6605. tbl)))
  6606. (defun org-olpath-completing-read (prompt collection &rest args)
  6607. "Read an outline path like a file name."
  6608. (let ((thetable collection))
  6609. (apply
  6610. 'completing-read prompt
  6611. (lambda (string predicate &optional flag)
  6612. (let (rtn r s f (l (length string)))
  6613. (cond
  6614. ((eq flag nil)
  6615. ;; try completion
  6616. (try-completion string thetable))
  6617. ((eq flag t)
  6618. ;; all-completions
  6619. (setq rtn (all-completions string thetable predicate))
  6620. (mapcar
  6621. (lambda (x)
  6622. (setq r (substring x l))
  6623. (if (string-match " ([^)]*)$" x)
  6624. (setq f (match-string 0 x))
  6625. (setq f ""))
  6626. (if (string-match "/" r)
  6627. (concat string (substring r 0 (match-end 0)) f)
  6628. x))
  6629. rtn))
  6630. ((eq flag 'lambda)
  6631. ;; exact match?
  6632. (assoc string thetable)))
  6633. ))
  6634. args)))
  6635. ;;;; Dynamic blocks
  6636. (defun org-find-dblock (name)
  6637. "Find the first dynamic block with name NAME in the buffer.
  6638. If not found, stay at current position and return nil."
  6639. (let (pos)
  6640. (save-excursion
  6641. (goto-char (point-min))
  6642. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6643. nil t)
  6644. (match-beginning 0))))
  6645. (if pos (goto-char pos))
  6646. pos))
  6647. (defconst org-dblock-start-re
  6648. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6649. "Matches the startline of a dynamic block, with parameters.")
  6650. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6651. "Matches the end of a dyhamic block.")
  6652. (defun org-create-dblock (plist)
  6653. "Create a dynamic block section, with parameters taken from PLIST.
  6654. PLIST must containe a :name entry which is used as name of the block."
  6655. (unless (bolp) (newline))
  6656. (let ((name (plist-get plist :name)))
  6657. (insert "#+BEGIN: " name)
  6658. (while plist
  6659. (if (eq (car plist) :name)
  6660. (setq plist (cddr plist))
  6661. (insert " " (prin1-to-string (pop plist)))))
  6662. (insert "\n\n#+END:\n")
  6663. (beginning-of-line -2)))
  6664. (defun org-prepare-dblock ()
  6665. "Prepare dynamic block for refresh.
  6666. This empties the block, puts the cursor at the insert position and returns
  6667. the property list including an extra property :name with the block name."
  6668. (unless (looking-at org-dblock-start-re)
  6669. (error "Not at a dynamic block"))
  6670. (let* ((begdel (1+ (match-end 0)))
  6671. (name (org-no-properties (match-string 1)))
  6672. (params (append (list :name name)
  6673. (read (concat "(" (match-string 3) ")")))))
  6674. (unless (re-search-forward org-dblock-end-re nil t)
  6675. (error "Dynamic block not terminated"))
  6676. (setq params
  6677. (append params
  6678. (list :content (buffer-substring
  6679. begdel (match-beginning 0)))))
  6680. (delete-region begdel (match-beginning 0))
  6681. (goto-char begdel)
  6682. (open-line 1)
  6683. params))
  6684. (defun org-map-dblocks (&optional command)
  6685. "Apply COMMAND to all dynamic blocks in the current buffer.
  6686. If COMMAND is not given, use `org-update-dblock'."
  6687. (let ((cmd (or command 'org-update-dblock))
  6688. pos)
  6689. (save-excursion
  6690. (goto-char (point-min))
  6691. (while (re-search-forward org-dblock-start-re nil t)
  6692. (goto-char (setq pos (match-beginning 0)))
  6693. (condition-case nil
  6694. (funcall cmd)
  6695. (error (message "Error during update of dynamic block")))
  6696. (goto-char pos)
  6697. (unless (re-search-forward org-dblock-end-re nil t)
  6698. (error "Dynamic block not terminated"))))))
  6699. (defun org-dblock-update (&optional arg)
  6700. "User command for updating dynamic blocks.
  6701. Update the dynamic block at point. With prefix ARG, update all dynamic
  6702. blocks in the buffer."
  6703. (interactive "P")
  6704. (if arg
  6705. (org-update-all-dblocks)
  6706. (or (looking-at org-dblock-start-re)
  6707. (org-beginning-of-dblock))
  6708. (org-update-dblock)))
  6709. (defun org-update-dblock ()
  6710. "Update the dynamic block at point
  6711. This means to empty the block, parse for parameters and then call
  6712. the correct writing function."
  6713. (save-window-excursion
  6714. (let* ((pos (point))
  6715. (line (org-current-line))
  6716. (params (org-prepare-dblock))
  6717. (name (plist-get params :name))
  6718. (cmd (intern (concat "org-dblock-write:" name))))
  6719. (message "Updating dynamic block `%s' at line %d..." name line)
  6720. (funcall cmd params)
  6721. (message "Updating dynamic block `%s' at line %d...done" name line)
  6722. (goto-char pos))))
  6723. (defun org-beginning-of-dblock ()
  6724. "Find the beginning of the dynamic block at point.
  6725. Error if there is no scuh block at point."
  6726. (let ((pos (point))
  6727. beg)
  6728. (end-of-line 1)
  6729. (if (and (re-search-backward org-dblock-start-re nil t)
  6730. (setq beg (match-beginning 0))
  6731. (re-search-forward org-dblock-end-re nil t)
  6732. (> (match-end 0) pos))
  6733. (goto-char beg)
  6734. (goto-char pos)
  6735. (error "Not in a dynamic block"))))
  6736. (defun org-update-all-dblocks ()
  6737. "Update all dynamic blocks in the buffer.
  6738. This function can be used in a hook."
  6739. (when (org-mode-p)
  6740. (org-map-dblocks 'org-update-dblock)))
  6741. ;;;; Completion
  6742. (defconst org-additional-option-like-keywords
  6743. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6744. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6745. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6746. "BEGIN_QUOTE" "END_QUOTE"
  6747. "BEGIN_VERSE" "END_VERSE"
  6748. "BEGIN_SRC" "END_SRC"))
  6749. (defcustom org-structure-template-alist
  6750. '(
  6751. ("s" "#+begin_src ?\n\n#+end_src"
  6752. "<src lang=\"?\">\n\n</src>")
  6753. ("e" "#+begin_example\n?\n#+end_example"
  6754. "<example>\n?\n</example>")
  6755. ("q" "#+begin_quote\n?\n#+end_quote"
  6756. "<quote>\n?\n</quote>")
  6757. ("v" "#+begin_verse\n?\n#+end_verse"
  6758. "<verse>\n?\n/verse>")
  6759. ("l" "#+begin_latex\n?\n#+end_latex"
  6760. "<literal style=\"latex\">\n?\n</literal>")
  6761. ("L" "#+latex: "
  6762. "<literal style=\"latex\">?</literal>")
  6763. ("h" "#+begin_html\n?\n#+end_html"
  6764. "<literal style=\"html\">\n?\n</literal>")
  6765. ("H" "#+html: "
  6766. "<literal style=\"html\">?</literal>")
  6767. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6768. ("A" "#+ascii: ")
  6769. ("i" "#+include %file ?"
  6770. "<include file=%file markup=\"?\">")
  6771. )
  6772. "Structure completion elements.
  6773. This is a list of abbreviation keys and values. The value gets inserted
  6774. it you type @samp{.} followed by the key and then the completion key,
  6775. usually `M-TAB'. %file will be replaced by a file name after prompting
  6776. for the file uning completion.
  6777. There are two templates for each key, the first uses the original Org syntax,
  6778. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6779. the default when the /org-mtags.el/ module has been loaded. See also the
  6780. variable `org-mtags-prefere-muse-templates'.
  6781. This is an experimental feature, it is undecided if it is going to stay in."
  6782. :group 'org-completion
  6783. :type '(repeat
  6784. (string :tag "Key")
  6785. (string :tag "Template")
  6786. (string :tag "Muse Template")))
  6787. (defun org-try-structure-completion ()
  6788. "Try to complete a structure template before point.
  6789. This looks for strings like \"<e\" on an otherwise empty line and
  6790. expands them."
  6791. (let ((l (buffer-substring (point-at-bol) (point)))
  6792. a)
  6793. (when (and (looking-at "[ \t]*$")
  6794. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  6795. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  6796. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  6797. (match-beginning 1)) a)
  6798. t)))
  6799. (defun org-complete-expand-structure-template (start cell)
  6800. "Expand a structure template."
  6801. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  6802. (rpl (nth (if musep 2 1) cell)))
  6803. (delete-region start (point))
  6804. (when (string-match "\\`#\\+" rpl)
  6805. (cond
  6806. ((bolp))
  6807. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6808. (delete-region (point-at-bol) (point)))
  6809. (t (newline))))
  6810. (setq start (point))
  6811. (if (string-match "%file" rpl)
  6812. (setq rpl (replace-match
  6813. (concat
  6814. "\""
  6815. (save-match-data
  6816. (abbreviate-file-name (read-file-name "Include file: ")))
  6817. "\"")
  6818. t t rpl)))
  6819. (insert rpl)
  6820. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6821. (defun org-complete (&optional arg)
  6822. "Perform completion on word at point.
  6823. At the beginning of a headline, this completes TODO keywords as given in
  6824. `org-todo-keywords'.
  6825. If the current word is preceded by a backslash, completes the TeX symbols
  6826. that are supported for HTML support.
  6827. If the current word is preceded by \"#+\", completes special words for
  6828. setting file options.
  6829. In the line after \"#+STARTUP:, complete valid keywords.\"
  6830. At all other locations, this simply calls the value of
  6831. `org-completion-fallback-command'."
  6832. (interactive "P")
  6833. (org-without-partial-completion
  6834. (catch 'exit
  6835. (let* ((a nil)
  6836. (end (point))
  6837. (beg1 (save-excursion
  6838. (skip-chars-backward (org-re "[:alnum:]_@"))
  6839. (point)))
  6840. (beg (save-excursion
  6841. (skip-chars-backward "a-zA-Z0-9_:$")
  6842. (point)))
  6843. (confirm (lambda (x) (stringp (car x))))
  6844. (searchhead (equal (char-before beg) ?*))
  6845. (struct
  6846. (when (and (member (char-before beg1) '(?. ?<))
  6847. (setq a (assoc (buffer-substring beg1 (point))
  6848. org-structure-template-alist)))
  6849. (org-complete-expand-structure-template (1- beg1) a)
  6850. (throw 'exit t)))
  6851. (tag (and (equal (char-before beg1) ?:)
  6852. (equal (char-after (point-at-bol)) ?*)))
  6853. (prop (and (equal (char-before beg1) ?:)
  6854. (not (equal (char-after (point-at-bol)) ?*))))
  6855. (texp (equal (char-before beg) ?\\))
  6856. (link (equal (char-before beg) ?\[))
  6857. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6858. beg)
  6859. "#+"))
  6860. (startup (string-match "^#\\+STARTUP:.*"
  6861. (buffer-substring (point-at-bol) (point))))
  6862. (completion-ignore-case opt)
  6863. (type nil)
  6864. (tbl nil)
  6865. (table (cond
  6866. (opt
  6867. (setq type :opt)
  6868. (require 'org-exp)
  6869. (append
  6870. (mapcar
  6871. (lambda (x)
  6872. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6873. (cons (match-string 2 x) (match-string 1 x)))
  6874. (org-split-string (org-get-current-options) "\n"))
  6875. (mapcar 'list org-additional-option-like-keywords)))
  6876. (startup
  6877. (setq type :startup)
  6878. org-startup-options)
  6879. (link (append org-link-abbrev-alist-local
  6880. org-link-abbrev-alist))
  6881. (texp
  6882. (setq type :tex)
  6883. org-html-entities)
  6884. ((string-match "\\`\\*+[ \t]+\\'"
  6885. (buffer-substring (point-at-bol) beg))
  6886. (setq type :todo)
  6887. (mapcar 'list org-todo-keywords-1))
  6888. (searchhead
  6889. (setq type :searchhead)
  6890. (save-excursion
  6891. (goto-char (point-min))
  6892. (while (re-search-forward org-todo-line-regexp nil t)
  6893. (push (list
  6894. (org-make-org-heading-search-string
  6895. (match-string 3) t))
  6896. tbl)))
  6897. tbl)
  6898. (tag (setq type :tag beg beg1)
  6899. (or org-tag-alist (org-get-buffer-tags)))
  6900. (prop (setq type :prop beg beg1)
  6901. (mapcar 'list (org-buffer-property-keys nil t t)))
  6902. (t (progn
  6903. (call-interactively org-completion-fallback-command)
  6904. (throw 'exit nil)))))
  6905. (pattern (buffer-substring-no-properties beg end))
  6906. (completion (try-completion pattern table confirm)))
  6907. (cond ((eq completion t)
  6908. (if (not (assoc (upcase pattern) table))
  6909. (message "Already complete")
  6910. (if (and (equal type :opt)
  6911. (not (member (car (assoc (upcase pattern) table))
  6912. org-additional-option-like-keywords)))
  6913. (insert (substring (cdr (assoc (upcase pattern) table))
  6914. (length pattern)))
  6915. (if (memq type '(:tag :prop)) (insert ":")))))
  6916. ((null completion)
  6917. (message "Can't find completion for \"%s\"" pattern)
  6918. (ding))
  6919. ((not (string= pattern completion))
  6920. (delete-region beg end)
  6921. (if (string-match " +$" completion)
  6922. (setq completion (replace-match "" t t completion)))
  6923. (insert completion)
  6924. (if (get-buffer-window "*Completions*")
  6925. (delete-window (get-buffer-window "*Completions*")))
  6926. (if (assoc completion table)
  6927. (if (eq type :todo) (insert " ")
  6928. (if (memq type '(:tag :prop)) (insert ":"))))
  6929. (if (and (equal type :opt) (assoc completion table))
  6930. (message "%s" (substitute-command-keys
  6931. "Press \\[org-complete] again to insert example settings"))))
  6932. (t
  6933. (message "Making completion list...")
  6934. (let ((list (sort (all-completions pattern table confirm)
  6935. 'string<)))
  6936. (with-output-to-temp-buffer "*Completions*"
  6937. (condition-case nil
  6938. ;; Protection needed for XEmacs and emacs 21
  6939. (display-completion-list list pattern)
  6940. (error (display-completion-list list)))))
  6941. (message "Making completion list...%s" "done")))))))
  6942. ;;;; TODO, DEADLINE, Comments
  6943. (defun org-toggle-comment ()
  6944. "Change the COMMENT state of an entry."
  6945. (interactive)
  6946. (save-excursion
  6947. (org-back-to-heading)
  6948. (let (case-fold-search)
  6949. (if (looking-at (concat outline-regexp
  6950. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6951. (replace-match "" t t nil 1)
  6952. (if (looking-at outline-regexp)
  6953. (progn
  6954. (goto-char (match-end 0))
  6955. (insert org-comment-string " ")))))))
  6956. (defvar org-last-todo-state-is-todo nil
  6957. "This is non-nil when the last TODO state change led to a TODO state.
  6958. If the last change removed the TODO tag or switched to DONE, then
  6959. this is nil.")
  6960. (defvar org-setting-tags nil) ; dynamically skiped
  6961. (defun org-parse-local-options (string var)
  6962. "Parse STRING for startup setting relevant for variable VAR."
  6963. (let ((rtn (symbol-value var))
  6964. e opts)
  6965. (save-match-data
  6966. (if (or (not string) (not (string-match "\\S-" string)))
  6967. rtn
  6968. (setq opts (delq nil (mapcar (lambda (x)
  6969. (setq e (assoc x org-startup-options))
  6970. (if (eq (nth 1 e) var) e nil))
  6971. (org-split-string string "[ \t]+"))))
  6972. (if (not opts)
  6973. rtn
  6974. (setq rtn nil)
  6975. (while (setq e (pop opts))
  6976. (if (not (nth 3 e))
  6977. (setq rtn (nth 2 e))
  6978. (if (not (listp rtn)) (setq rtn nil))
  6979. (push (nth 2 e) rtn)))
  6980. rtn)))))
  6981. (defvar org-blocker-hook nil
  6982. "Hook for functions that are allowed to block a state change.
  6983. Each function gets as its single argument a property list, see
  6984. `org-trigger-hook' for more information about this list.
  6985. If any of the functions in this hook returns nil, the state change
  6986. is blocked.")
  6987. (defvar org-trigger-hook nil
  6988. "Hook for functions that are triggered by a state change.
  6989. Each function gets as its single argument a property list with at least
  6990. the following elements:
  6991. (:type type-of-change :position pos-at-entry-start
  6992. :from old-state :to new-state)
  6993. Depending on the type, more properties may be present.
  6994. This mechanism is currently implemented for:
  6995. TODO state changes
  6996. ------------------
  6997. :type todo-state-change
  6998. :from previous state (keyword as a string), or nil
  6999. :to new state (keyword as a string), or nil")
  7000. (defun org-todo (&optional arg)
  7001. "Change the TODO state of an item.
  7002. The state of an item is given by a keyword at the start of the heading,
  7003. like
  7004. *** TODO Write paper
  7005. *** DONE Call mom
  7006. The different keywords are specified in the variable `org-todo-keywords'.
  7007. By default the available states are \"TODO\" and \"DONE\".
  7008. So for this example: when the item starts with TODO, it is changed to DONE.
  7009. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7010. DONE are present, add TODO at the beginning of the heading.
  7011. With C-u prefix arg, use completion to determine the new state.
  7012. With numeric prefix arg, switch to that state.
  7013. For calling through lisp, arg is also interpreted in the following way:
  7014. 'none -> empty state
  7015. \"\"(empty string) -> switch to empty state
  7016. 'done -> switch to DONE
  7017. 'nextset -> switch to the next set of keywords
  7018. 'previousset -> switch to the previous set of keywords
  7019. \"WAITING\" -> switch to the specified keyword, but only if it
  7020. really is a member of `org-todo-keywords'."
  7021. (interactive "P")
  7022. (save-excursion
  7023. (catch 'exit
  7024. (org-back-to-heading)
  7025. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7026. (or (looking-at (concat " +" org-todo-regexp " *"))
  7027. (looking-at " *"))
  7028. (let* ((match-data (match-data))
  7029. (startpos (point-at-bol))
  7030. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7031. (org-log-done org-log-done)
  7032. (org-log-repeat org-log-repeat)
  7033. (org-todo-log-states org-todo-log-states)
  7034. (this (match-string 1))
  7035. (hl-pos (match-beginning 0))
  7036. (head (org-get-todo-sequence-head this))
  7037. (ass (assoc head org-todo-kwd-alist))
  7038. (interpret (nth 1 ass))
  7039. (done-word (nth 3 ass))
  7040. (final-done-word (nth 4 ass))
  7041. (last-state (or this ""))
  7042. (completion-ignore-case t)
  7043. (member (member this org-todo-keywords-1))
  7044. (tail (cdr member))
  7045. (state (cond
  7046. ((and org-todo-key-trigger
  7047. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7048. (and (not arg) org-use-fast-todo-selection
  7049. (not (eq org-use-fast-todo-selection 'prefix)))))
  7050. ;; Use fast selection
  7051. (org-fast-todo-selection))
  7052. ((and (equal arg '(4))
  7053. (or (not org-use-fast-todo-selection)
  7054. (not org-todo-key-trigger)))
  7055. ;; Read a state with completion
  7056. (completing-read "State: " (mapcar (lambda(x) (list x))
  7057. org-todo-keywords-1)
  7058. nil t))
  7059. ((eq arg 'right)
  7060. (if this
  7061. (if tail (car tail) nil)
  7062. (car org-todo-keywords-1)))
  7063. ((eq arg 'left)
  7064. (if (equal member org-todo-keywords-1)
  7065. nil
  7066. (if this
  7067. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7068. org-todo-keywords-1)
  7069. (org-last org-todo-keywords-1))))
  7070. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7071. (setq arg nil))) ; hack to fall back to cycling
  7072. (arg
  7073. ;; user or caller requests a specific state
  7074. (cond
  7075. ((equal arg "") nil)
  7076. ((eq arg 'none) nil)
  7077. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7078. ((eq arg 'nextset)
  7079. (or (car (cdr (member head org-todo-heads)))
  7080. (car org-todo-heads)))
  7081. ((eq arg 'previousset)
  7082. (let ((org-todo-heads (reverse org-todo-heads)))
  7083. (or (car (cdr (member head org-todo-heads)))
  7084. (car org-todo-heads))))
  7085. ((car (member arg org-todo-keywords-1)))
  7086. ((nth (1- (prefix-numeric-value arg))
  7087. org-todo-keywords-1))))
  7088. ((null member) (or head (car org-todo-keywords-1)))
  7089. ((equal this final-done-word) nil) ;; -> make empty
  7090. ((null tail) nil) ;; -> first entry
  7091. ((eq interpret 'sequence)
  7092. (car tail))
  7093. ((memq interpret '(type priority))
  7094. (if (eq this-command last-command)
  7095. (car tail)
  7096. (if (> (length tail) 0)
  7097. (or done-word (car org-done-keywords))
  7098. nil)))
  7099. (t nil)))
  7100. (next (if state (concat " " state " ") " "))
  7101. (change-plist (list :type 'todo-state-change :from this :to state
  7102. :position startpos))
  7103. dolog now-done-p)
  7104. (when org-blocker-hook
  7105. (unless (save-excursion
  7106. (save-match-data
  7107. (run-hook-with-args-until-failure
  7108. 'org-blocker-hook change-plist)))
  7109. (if (interactive-p)
  7110. (error "TODO state change from %s to %s blocked" this state)
  7111. ;; fail silently
  7112. (message "TODO state change from %s to %s blocked" this state)
  7113. (throw 'exit nil))))
  7114. (store-match-data match-data)
  7115. (replace-match next t t)
  7116. (unless (pos-visible-in-window-p hl-pos)
  7117. (message "TODO state changed to %s" (org-trim next)))
  7118. (unless head
  7119. (setq head (org-get-todo-sequence-head state)
  7120. ass (assoc head org-todo-kwd-alist)
  7121. interpret (nth 1 ass)
  7122. done-word (nth 3 ass)
  7123. final-done-word (nth 4 ass)))
  7124. (when (memq arg '(nextset previousset))
  7125. (message "Keyword-Set %d/%d: %s"
  7126. (- (length org-todo-sets) -1
  7127. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7128. (length org-todo-sets)
  7129. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7130. (setq org-last-todo-state-is-todo
  7131. (not (member state org-done-keywords)))
  7132. (setq now-done-p (and (member state org-done-keywords)
  7133. (not (member this org-done-keywords))))
  7134. (and logging (org-local-logging logging))
  7135. (when (and (or org-todo-log-states org-log-done)
  7136. (not (memq arg '(nextset previousset))))
  7137. ;; we need to look at recording a time and note
  7138. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7139. (nth 2 (assoc this org-todo-log-states))))
  7140. (when (and state
  7141. (member state org-not-done-keywords)
  7142. (not (member this org-not-done-keywords)))
  7143. ;; This is now a todo state and was not one before
  7144. ;; If there was a CLOSED time stamp, get rid of it.
  7145. (org-add-planning-info nil nil 'closed))
  7146. (when (and now-done-p org-log-done)
  7147. ;; It is now done, and it was not done before
  7148. (org-add-planning-info 'closed (org-current-time))
  7149. (if (and (not dolog) (eq 'note org-log-done))
  7150. (org-add-log-setup 'done state 'findpos 'note)))
  7151. (when (and state dolog)
  7152. ;; This is a non-nil state, and we need to log it
  7153. (org-add-log-setup 'state state 'findpos dolog)))
  7154. ;; Fixup tag positioning
  7155. (org-todo-trigger-tag-changes state)
  7156. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7157. (when org-provide-todo-statistics
  7158. (org-update-parent-todo-statistics))
  7159. (run-hooks 'org-after-todo-state-change-hook)
  7160. (if (and arg (not (member state org-done-keywords)))
  7161. (setq head (org-get-todo-sequence-head state)))
  7162. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7163. ;; Do we need to trigger a repeat?
  7164. (when now-done-p (org-auto-repeat-maybe state))
  7165. ;; Fixup cursor location if close to the keyword
  7166. (if (and (outline-on-heading-p)
  7167. (not (bolp))
  7168. (save-excursion (beginning-of-line 1)
  7169. (looking-at org-todo-line-regexp))
  7170. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7171. (progn
  7172. (goto-char (or (match-end 2) (match-end 1)))
  7173. (just-one-space)))
  7174. (when org-trigger-hook
  7175. (save-excursion
  7176. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7177. (defun org-update-parent-todo-statistics ()
  7178. "Update any statistics cookie in the parent of the current headline."
  7179. (interactive)
  7180. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7181. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7182. (catch 'exit
  7183. (save-excursion
  7184. (setq level (org-up-heading-safe))
  7185. (unless (and level
  7186. (re-search-forward box-re (point-at-eol) t))
  7187. (throw 'exit nil))
  7188. (setq is-percent (match-end 2))
  7189. (save-match-data
  7190. (unless (outline-next-heading) (throw 'exit nil))
  7191. (while (looking-at org-todo-line-regexp)
  7192. (setq kwd (match-string 2))
  7193. (and kwd (setq cnt-all (1+ cnt-all)))
  7194. (and (member kwd org-done-keywords)
  7195. (setq cnt-done (1+ cnt-done)))
  7196. (condition-case nil
  7197. (org-forward-same-level 1)
  7198. (error (end-of-line 1)))))
  7199. (replace-match
  7200. (if is-percent
  7201. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7202. (format "[%d/%d]" cnt-done cnt-all)))
  7203. (run-hook-with-args 'org-after-todo-statistics-hook
  7204. cnt-done (- cnt-all cnt-done))))))
  7205. (defvar org-after-todo-statistics-hook nil
  7206. "Hook that is called after a TODO statistics cookie has been updated.
  7207. Each function is called with two arguments: the number of not-done entries
  7208. and the number of done entries.
  7209. For example, the following function, when added to this hook, will switch
  7210. an entry to DONE when all children are done, and back to TODO when new
  7211. entries are set to a TODO status. Note that this hook is only called
  7212. when there is a statistics cookie in the headline!
  7213. (defun org-summary-todo (n-done n-not-done)
  7214. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7215. (let (org-log-done org-log-states) ; turn off logging
  7216. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7217. ")
  7218. (defun org-todo-trigger-tag-changes (state)
  7219. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7220. (let ((l org-todo-state-tags-triggers)
  7221. changes)
  7222. (when (or (not state) (equal state ""))
  7223. (setq changes (append changes (cdr (assoc "" l)))))
  7224. (when (and (stringp state) (> (length state) 0))
  7225. (setq changes (append changes (cdr (assoc state l)))))
  7226. (when (member state org-not-done-keywords)
  7227. (setq changes (append changes (cdr (assoc 'todo l)))))
  7228. (when (member state org-done-keywords)
  7229. (setq changes (append changes (cdr (assoc 'done l)))))
  7230. (dolist (c changes)
  7231. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7232. (defun org-local-logging (value)
  7233. "Get logging settings from a property VALUE."
  7234. (let* (words w a)
  7235. ;; directly set the variables, they are already local.
  7236. (setq org-log-done nil
  7237. org-log-repeat nil
  7238. org-todo-log-states nil)
  7239. (setq words (org-split-string value))
  7240. (while (setq w (pop words))
  7241. (cond
  7242. ((setq a (assoc w org-startup-options))
  7243. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7244. (set (nth 1 a) (nth 2 a))))
  7245. ((setq a (org-extract-log-state-settings w))
  7246. (and (member (car a) org-todo-keywords-1)
  7247. (push a org-todo-log-states)))))))
  7248. (defun org-get-todo-sequence-head (kwd)
  7249. "Return the head of the TODO sequence to which KWD belongs.
  7250. If KWD is not set, check if there is a text property remembering the
  7251. right sequence."
  7252. (let (p)
  7253. (cond
  7254. ((not kwd)
  7255. (or (get-text-property (point-at-bol) 'org-todo-head)
  7256. (progn
  7257. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7258. nil (point-at-eol)))
  7259. (get-text-property p 'org-todo-head))))
  7260. ((not (member kwd org-todo-keywords-1))
  7261. (car org-todo-keywords-1))
  7262. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7263. (defun org-fast-todo-selection ()
  7264. "Fast TODO keyword selection with single keys.
  7265. Returns the new TODO keyword, or nil if no state change should occur."
  7266. (let* ((fulltable org-todo-key-alist)
  7267. (done-keywords org-done-keywords) ;; needed for the faces.
  7268. (maxlen (apply 'max (mapcar
  7269. (lambda (x)
  7270. (if (stringp (car x)) (string-width (car x)) 0))
  7271. fulltable)))
  7272. (expert nil)
  7273. (fwidth (+ maxlen 3 1 3))
  7274. (ncol (/ (- (window-width) 4) fwidth))
  7275. tg cnt e c tbl
  7276. groups ingroup)
  7277. (save-window-excursion
  7278. (if expert
  7279. (set-buffer (get-buffer-create " *Org todo*"))
  7280. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7281. (erase-buffer)
  7282. (org-set-local 'org-done-keywords done-keywords)
  7283. (setq tbl fulltable cnt 0)
  7284. (while (setq e (pop tbl))
  7285. (cond
  7286. ((equal e '(:startgroup))
  7287. (push '() groups) (setq ingroup t)
  7288. (when (not (= cnt 0))
  7289. (setq cnt 0)
  7290. (insert "\n"))
  7291. (insert "{ "))
  7292. ((equal e '(:endgroup))
  7293. (setq ingroup nil cnt 0)
  7294. (insert "}\n"))
  7295. (t
  7296. (setq tg (car e) c (cdr e))
  7297. (if ingroup (push tg (car groups)))
  7298. (setq tg (org-add-props tg nil 'face
  7299. (org-get-todo-face tg)))
  7300. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7301. (insert "[" c "] " tg (make-string
  7302. (- fwidth 4 (length tg)) ?\ ))
  7303. (when (= (setq cnt (1+ cnt)) ncol)
  7304. (insert "\n")
  7305. (if ingroup (insert " "))
  7306. (setq cnt 0)))))
  7307. (insert "\n")
  7308. (goto-char (point-min))
  7309. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7310. (fit-window-to-buffer))
  7311. (message "[a-z..]:Set [SPC]:clear")
  7312. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7313. (cond
  7314. ((or (= c ?\C-g)
  7315. (and (= c ?q) (not (rassoc c fulltable))))
  7316. (setq quit-flag t))
  7317. ((= c ?\ ) nil)
  7318. ((setq e (rassoc c fulltable) tg (car e))
  7319. tg)
  7320. (t (setq quit-flag t))))))
  7321. (defun org-entry-is-todo-p ()
  7322. (member (org-get-todo-state) org-not-done-keywords))
  7323. (defun org-entry-is-done-p ()
  7324. (member (org-get-todo-state) org-done-keywords))
  7325. (defun org-get-todo-state ()
  7326. (save-excursion
  7327. (org-back-to-heading t)
  7328. (and (looking-at org-todo-line-regexp)
  7329. (match-end 2)
  7330. (match-string 2))))
  7331. (defun org-at-date-range-p (&optional inactive-ok)
  7332. "Is the cursor inside a date range?"
  7333. (interactive)
  7334. (save-excursion
  7335. (catch 'exit
  7336. (let ((pos (point)))
  7337. (skip-chars-backward "^[<\r\n")
  7338. (skip-chars-backward "<[")
  7339. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7340. (>= (match-end 0) pos)
  7341. (throw 'exit t))
  7342. (skip-chars-backward "^<[\r\n")
  7343. (skip-chars-backward "<[")
  7344. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7345. (>= (match-end 0) pos)
  7346. (throw 'exit t)))
  7347. nil)))
  7348. (defun org-get-repeat ()
  7349. "Check if there is a deadline/schedule with repeater in this entry."
  7350. (save-match-data
  7351. (save-excursion
  7352. (org-back-to-heading t)
  7353. (if (re-search-forward
  7354. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7355. (match-string 1)))))
  7356. (defvar org-last-changed-timestamp)
  7357. (defvar org-last-inserted-timestamp)
  7358. (defvar org-log-post-message)
  7359. (defvar org-log-note-purpose)
  7360. (defvar org-log-note-how)
  7361. (defvar org-log-note-extra)
  7362. (defun org-auto-repeat-maybe (done-word)
  7363. "Check if the current headline contains a repeated deadline/schedule.
  7364. If yes, set TODO state back to what it was and change the base date
  7365. of repeating deadline/scheduled time stamps to new date.
  7366. This function is run automatically after each state change to a DONE state."
  7367. ;; last-state is dynamically scoped into this function
  7368. (let* ((repeat (org-get-repeat))
  7369. (aa (assoc last-state org-todo-kwd-alist))
  7370. (interpret (nth 1 aa))
  7371. (head (nth 2 aa))
  7372. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7373. (msg "Entry repeats: ")
  7374. (org-log-done nil)
  7375. (org-todo-log-states nil)
  7376. (nshiftmax 10) (nshift 0)
  7377. re type n what ts mb0 time)
  7378. (when repeat
  7379. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7380. (org-todo (if (eq interpret 'type) last-state head))
  7381. (when org-log-repeat
  7382. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7383. (memq 'org-add-log-note post-command-hook))
  7384. ;; OK, we are already setup for some record
  7385. (if (eq org-log-repeat 'note)
  7386. ;; make sure we take a note, not only a time stamp
  7387. (setq org-log-note-how 'note))
  7388. ;; Set up for taking a record
  7389. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7390. 'findpos org-log-repeat)))
  7391. (org-back-to-heading t)
  7392. (org-add-planning-info nil nil 'closed)
  7393. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7394. org-deadline-time-regexp "\\)\\|\\("
  7395. org-ts-regexp "\\)"))
  7396. (while (re-search-forward
  7397. re (save-excursion (outline-next-heading) (point)) t)
  7398. (setq type (if (match-end 1) org-scheduled-string
  7399. (if (match-end 3) org-deadline-string "Plain:"))
  7400. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7401. mb0 (match-beginning 0))
  7402. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7403. (setq n (string-to-number (match-string 2 ts))
  7404. what (match-string 3 ts))
  7405. (if (equal what "w") (setq n (* n 7) what "d"))
  7406. ;; Preparation, see if we need to modify the start date for the change
  7407. (when (match-end 1)
  7408. (setq time (save-match-data (org-time-string-to-time ts)))
  7409. (cond
  7410. ((equal (match-string 1 ts) ".")
  7411. ;; Shift starting date to today
  7412. (org-timestamp-change
  7413. (- (time-to-days (current-time)) (time-to-days time))
  7414. 'day))
  7415. ((equal (match-string 1 ts) "+")
  7416. (while (or (= nshift 0)
  7417. (<= (time-to-days time) (time-to-days (current-time))))
  7418. (when (= (incf nshift) nshiftmax)
  7419. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7420. (error "Abort")))
  7421. (org-timestamp-change n (cdr (assoc what whata)))
  7422. (org-at-timestamp-p t)
  7423. (setq ts (match-string 1))
  7424. (setq time (save-match-data (org-time-string-to-time ts))))
  7425. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7426. ;; rematch, so that we have everything in place for the real shift
  7427. (org-at-timestamp-p t)
  7428. (setq ts (match-string 1))
  7429. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7430. (org-timestamp-change n (cdr (assoc what whata)))
  7431. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7432. (setq org-log-post-message msg)
  7433. (message "%s" msg))))
  7434. (defun org-show-todo-tree (arg)
  7435. "Make a compact tree which shows all headlines marked with TODO.
  7436. The tree will show the lines where the regexp matches, and all higher
  7437. headlines above the match.
  7438. With a \\[universal-argument] prefix, also show the DONE entries.
  7439. With a numeric prefix N, construct a sparse tree for the Nth element
  7440. of `org-todo-keywords-1'."
  7441. (interactive "P")
  7442. (let ((case-fold-search nil)
  7443. (kwd-re
  7444. (cond ((null arg) org-not-done-regexp)
  7445. ((equal arg '(4))
  7446. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7447. (mapcar 'list org-todo-keywords-1))))
  7448. (concat "\\("
  7449. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7450. "\\)\\>")))
  7451. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7452. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7453. org-todo-keywords-1)))
  7454. (t (error "Invalid prefix argument: %s" arg)))))
  7455. (message "%d TODO entries found"
  7456. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7457. (defun org-deadline (&optional remove time)
  7458. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7459. With argument REMOVE, remove any deadline from the item.
  7460. When TIME is set, it should be an internal time specification, and the
  7461. scheduling will use the corresponding date."
  7462. (interactive "P")
  7463. (if remove
  7464. (progn
  7465. (org-remove-timestamp-with-keyword org-deadline-string)
  7466. (message "Item no longer has a deadline."))
  7467. (if (org-get-repeat)
  7468. (error "Cannot change deadline on task with repeater, please do that by hand")
  7469. (org-add-planning-info 'deadline time 'closed)
  7470. (message "Deadline on %s" org-last-inserted-timestamp))))
  7471. (defun org-schedule (&optional remove time)
  7472. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7473. With argument REMOVE, remove any scheduling date from the item.
  7474. When TIME is set, it should be an internal time specification, and the
  7475. scheduling will use the corresponding date."
  7476. (interactive "P")
  7477. (if remove
  7478. (progn
  7479. (org-remove-timestamp-with-keyword org-scheduled-string)
  7480. (message "Item is no longer scheduled."))
  7481. (if (org-get-repeat)
  7482. (error "Cannot reschedule task with repeater, please do that by hand")
  7483. (org-add-planning-info 'scheduled time 'closed)
  7484. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7485. (defun org-remove-timestamp-with-keyword (keyword)
  7486. "Remove all time stamps with KEYWORD in the current entry."
  7487. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7488. beg)
  7489. (save-excursion
  7490. (org-back-to-heading t)
  7491. (setq beg (point))
  7492. (org-end-of-subtree t t)
  7493. (while (re-search-backward re beg t)
  7494. (replace-match "")
  7495. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7496. (equal (char-before) ?\ ))
  7497. (backward-delete-char 1)
  7498. (if (string-match "^[ \t]*$" (buffer-substring
  7499. (point-at-bol) (point-at-eol)))
  7500. (delete-region (point-at-bol)
  7501. (min (point-max) (1+ (point-at-eol))))))))))
  7502. (defun org-add-planning-info (what &optional time &rest remove)
  7503. "Insert new timestamp with keyword in the line directly after the headline.
  7504. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7505. If non is given, the user is prompted for a date.
  7506. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7507. be removed."
  7508. (interactive)
  7509. (let (org-time-was-given org-end-time-was-given ts
  7510. end default-time default-input)
  7511. (when (and (not time) (memq what '(scheduled deadline)))
  7512. ;; Try to get a default date/time from existing timestamp
  7513. (save-excursion
  7514. (org-back-to-heading t)
  7515. (setq end (save-excursion (outline-next-heading) (point)))
  7516. (when (re-search-forward (if (eq what 'scheduled)
  7517. org-scheduled-time-regexp
  7518. org-deadline-time-regexp)
  7519. end t)
  7520. (setq ts (match-string 1)
  7521. default-time
  7522. (apply 'encode-time (org-parse-time-string ts))
  7523. default-input (and ts (org-get-compact-tod ts))))))
  7524. (when what
  7525. ;; If necessary, get the time from the user
  7526. (setq time (or time (org-read-date nil 'to-time nil nil
  7527. default-time default-input))))
  7528. (when (and org-insert-labeled-timestamps-at-point
  7529. (member what '(scheduled deadline)))
  7530. (insert
  7531. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7532. (org-insert-time-stamp time org-time-was-given
  7533. nil nil nil (list org-end-time-was-given))
  7534. (setq what nil))
  7535. (save-excursion
  7536. (save-restriction
  7537. (let (col list elt ts buffer-invisibility-spec)
  7538. (org-back-to-heading t)
  7539. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7540. (goto-char (match-end 1))
  7541. (setq col (current-column))
  7542. (goto-char (match-end 0))
  7543. (if (eobp) (insert "\n") (forward-char 1))
  7544. (if (and (not (looking-at outline-regexp))
  7545. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7546. "[^\r\n]*"))
  7547. (not (equal (match-string 1) org-clock-string)))
  7548. (narrow-to-region (match-beginning 0) (match-end 0))
  7549. (insert-before-markers "\n")
  7550. (backward-char 1)
  7551. (narrow-to-region (point) (point))
  7552. (and org-adapt-indentation (org-indent-to-column col)))
  7553. ;; Check if we have to remove something.
  7554. (setq list (cons what remove))
  7555. (while list
  7556. (setq elt (pop list))
  7557. (goto-char (point-min))
  7558. (when (or (and (eq elt 'scheduled)
  7559. (re-search-forward org-scheduled-time-regexp nil t))
  7560. (and (eq elt 'deadline)
  7561. (re-search-forward org-deadline-time-regexp nil t))
  7562. (and (eq elt 'closed)
  7563. (re-search-forward org-closed-time-regexp nil t)))
  7564. (replace-match "")
  7565. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7566. (if (looking-at " +") (replace-match ""))))
  7567. (goto-char (point-max))
  7568. (when what
  7569. (insert
  7570. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7571. (cond ((eq what 'scheduled) org-scheduled-string)
  7572. ((eq what 'deadline) org-deadline-string)
  7573. ((eq what 'closed) org-closed-string))
  7574. " ")
  7575. (setq ts (org-insert-time-stamp
  7576. time
  7577. (or org-time-was-given
  7578. (and (eq what 'closed) org-log-done-with-time))
  7579. (eq what 'closed)
  7580. nil nil (list org-end-time-was-given)))
  7581. (end-of-line 1))
  7582. (goto-char (point-min))
  7583. (widen)
  7584. (if (and (looking-at "[ \t]+\n")
  7585. (equal (char-before) ?\n))
  7586. (delete-region (1- (point)) (point-at-eol)))
  7587. ts)))))
  7588. (defvar org-log-note-marker (make-marker))
  7589. (defvar org-log-note-purpose nil)
  7590. (defvar org-log-note-state nil)
  7591. (defvar org-log-note-how nil)
  7592. (defvar org-log-note-extra nil)
  7593. (defvar org-log-note-window-configuration nil)
  7594. (defvar org-log-note-return-to (make-marker))
  7595. (defvar org-log-post-message nil
  7596. "Message to be displayed after a log note has been stored.
  7597. The auto-repeater uses this.")
  7598. (defun org-add-note ()
  7599. "Add a note to the current entry.
  7600. This is done in the same way as adding a state change note."
  7601. (interactive)
  7602. (org-add-log-setup 'note nil 'findpos nil))
  7603. (defvar org-property-end-re)
  7604. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7605. "Set up the post command hook to take a note.
  7606. If this is about to TODO state change, the new state is expected in STATE.
  7607. When FINDPOS is non-nil, find the correct position for the note in
  7608. the current entry. If not, assume that it can be inserted at point.
  7609. HOW is an indicator what kind of note should be created.
  7610. EXTRA is additional text that will be inserted into the notes buffer."
  7611. (save-restriction
  7612. (save-excursion
  7613. (when findpos
  7614. (org-back-to-heading t)
  7615. (narrow-to-region (point) (save-excursion
  7616. (outline-next-heading) (point)))
  7617. (when org-log-state-notes-insert-after-drawers
  7618. (while (re-search-forward
  7619. (concat "\\(" org-drawer-regexp
  7620. "\\|" org-property-end-re "\\)")
  7621. (point-max) t) (forward-line)))
  7622. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7623. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7624. "[^\r\n]*\\)?"))
  7625. (goto-char (match-end 0))
  7626. (unless org-log-states-order-reversed
  7627. (and (= (char-after) ?\n) (forward-char 1))
  7628. (org-skip-over-state-notes)
  7629. (skip-chars-backward " \t\n\r")))
  7630. (move-marker org-log-note-marker (point))
  7631. (setq org-log-note-purpose purpose
  7632. org-log-note-state state
  7633. org-log-note-how how
  7634. org-log-note-extra extra)
  7635. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7636. (defun org-skip-over-state-notes ()
  7637. "Skip past the list of State notes in an entry."
  7638. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7639. (while (looking-at "[ \t]*- State")
  7640. (condition-case nil
  7641. (org-next-item)
  7642. (error (org-end-of-item)))))
  7643. (defun org-add-log-note (&optional purpose)
  7644. "Pop up a window for taking a note, and add this note later at point."
  7645. (remove-hook 'post-command-hook 'org-add-log-note)
  7646. (setq org-log-note-window-configuration (current-window-configuration))
  7647. (delete-other-windows)
  7648. (move-marker org-log-note-return-to (point))
  7649. (switch-to-buffer (marker-buffer org-log-note-marker))
  7650. (goto-char org-log-note-marker)
  7651. (org-switch-to-buffer-other-window "*Org Note*")
  7652. (erase-buffer)
  7653. (if (memq org-log-note-how '(time state))
  7654. (org-store-log-note)
  7655. (let ((org-inhibit-startup t)) (org-mode))
  7656. (insert (format "# Insert note for %s.
  7657. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7658. (cond
  7659. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7660. ((eq org-log-note-purpose 'done) "closed todo item")
  7661. ((eq org-log-note-purpose 'state)
  7662. (format "state change to \"%s\"" org-log-note-state))
  7663. ((eq org-log-note-purpose 'note)
  7664. "this entry")
  7665. (t (error "This should not happen")))))
  7666. (if org-log-note-extra (insert org-log-note-extra))
  7667. (org-set-local 'org-finish-function 'org-store-log-note)))
  7668. (defvar org-note-abort nil) ; dynamically scoped
  7669. (defun org-store-log-note ()
  7670. "Finish taking a log note, and insert it to where it belongs."
  7671. (let ((txt (buffer-string))
  7672. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7673. lines ind)
  7674. (kill-buffer (current-buffer))
  7675. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7676. (setq txt (replace-match "" t t txt)))
  7677. (if (string-match "\\s-+\\'" txt)
  7678. (setq txt (replace-match "" t t txt)))
  7679. (setq lines (org-split-string txt "\n"))
  7680. (when (and note (string-match "\\S-" note))
  7681. (setq note
  7682. (org-replace-escapes
  7683. note
  7684. (list (cons "%u" (user-login-name))
  7685. (cons "%U" user-full-name)
  7686. (cons "%t" (format-time-string
  7687. (org-time-stamp-format 'long 'inactive)
  7688. (current-time)))
  7689. (cons "%s" (if org-log-note-state
  7690. (concat "\"" org-log-note-state "\"")
  7691. "")))))
  7692. (if lines (setq note (concat note " \\\\")))
  7693. (push note lines))
  7694. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7695. (when lines
  7696. (save-excursion
  7697. (set-buffer (marker-buffer org-log-note-marker))
  7698. (save-excursion
  7699. (goto-char org-log-note-marker)
  7700. (move-marker org-log-note-marker nil)
  7701. (end-of-line 1)
  7702. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7703. (indent-relative nil)
  7704. (insert "- " (pop lines))
  7705. (org-indent-line-function)
  7706. (beginning-of-line 1)
  7707. (looking-at "[ \t]*")
  7708. (setq ind (concat (match-string 0) " "))
  7709. (end-of-line 1)
  7710. (while lines (insert "\n" ind (pop lines)))))))
  7711. (set-window-configuration org-log-note-window-configuration)
  7712. (with-current-buffer (marker-buffer org-log-note-return-to)
  7713. (goto-char org-log-note-return-to))
  7714. (move-marker org-log-note-return-to nil)
  7715. (and org-log-post-message (message "%s" org-log-post-message)))
  7716. (defun org-sparse-tree (&optional arg)
  7717. "Create a sparse tree, prompt for the details.
  7718. This command can create sparse trees. You first need to select the type
  7719. of match used to create the tree:
  7720. t Show entries with a specific TODO keyword.
  7721. T Show entries selected by a tags match.
  7722. p Enter a property name and its value (both with completion on existing
  7723. names/values) and show entries with that property.
  7724. r Show entries matching a regular expression
  7725. d Show deadlines due within `org-deadline-warning-days'."
  7726. (interactive "P")
  7727. (let (ans kwd value)
  7728. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7729. (setq ans (read-char-exclusive))
  7730. (cond
  7731. ((equal ans ?d)
  7732. (call-interactively 'org-check-deadlines))
  7733. ((equal ans ?b)
  7734. (call-interactively 'org-check-before-date))
  7735. ((equal ans ?t)
  7736. (org-show-todo-tree '(4)))
  7737. ((equal ans ?T)
  7738. (call-interactively 'org-tags-sparse-tree))
  7739. ((member ans '(?p ?P))
  7740. (setq kwd (completing-read "Property: "
  7741. (mapcar 'list (org-buffer-property-keys))))
  7742. (setq value (completing-read "Value: "
  7743. (mapcar 'list (org-property-values kwd))))
  7744. (unless (string-match "\\`{.*}\\'" value)
  7745. (setq value (concat "\"" value "\"")))
  7746. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7747. ((member ans '(?r ?R ?/))
  7748. (call-interactively 'org-occur))
  7749. (t (error "No such sparse tree command \"%c\"" ans)))))
  7750. (defvar org-occur-highlights nil
  7751. "List of overlays used for occur matches.")
  7752. (make-variable-buffer-local 'org-occur-highlights)
  7753. (defvar org-occur-parameters nil
  7754. "Parameters of the active org-occur calls.
  7755. This is a list, each call to org-occur pushes as cons cell,
  7756. containing the regular expression and the callback, onto the list.
  7757. The list can contain several entries if `org-occur' has been called
  7758. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7759. will only contain one set of parameters. When the highlights are
  7760. removed (for example with `C-c C-c', or with the next edit (depending
  7761. on `org-remove-highlights-with-change'), this variable is emptied
  7762. as well.")
  7763. (make-variable-buffer-local 'org-occur-parameters)
  7764. (defun org-occur (regexp &optional keep-previous callback)
  7765. "Make a compact tree which shows all matches of REGEXP.
  7766. The tree will show the lines where the regexp matches, and all higher
  7767. headlines above the match. It will also show the heading after the match,
  7768. to make sure editing the matching entry is easy.
  7769. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7770. call to `org-occur' will be kept, to allow stacking of calls to this
  7771. command.
  7772. If CALLBACK is non-nil, it is a function which is called to confirm
  7773. that the match should indeed be shown."
  7774. (interactive "sRegexp: \nP")
  7775. (unless keep-previous
  7776. (org-remove-occur-highlights nil nil t))
  7777. (push (cons regexp callback) org-occur-parameters)
  7778. (let ((cnt 0))
  7779. (save-excursion
  7780. (goto-char (point-min))
  7781. (if (or (not keep-previous) ; do not want to keep
  7782. (not org-occur-highlights)) ; no previous matches
  7783. ;; hide everything
  7784. (org-overview))
  7785. (while (re-search-forward regexp nil t)
  7786. (when (or (not callback)
  7787. (save-match-data (funcall callback)))
  7788. (setq cnt (1+ cnt))
  7789. (when org-highlight-sparse-tree-matches
  7790. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7791. (org-show-context 'occur-tree))))
  7792. (when org-remove-highlights-with-change
  7793. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7794. nil 'local))
  7795. (unless org-sparse-tree-open-archived-trees
  7796. (org-hide-archived-subtrees (point-min) (point-max)))
  7797. (run-hooks 'org-occur-hook)
  7798. (if (interactive-p)
  7799. (message "%d match(es) for regexp %s" cnt regexp))
  7800. cnt))
  7801. (defun org-show-context (&optional key)
  7802. "Make sure point and context and visible.
  7803. How much context is shown depends upon the variables
  7804. `org-show-hierarchy-above', `org-show-following-heading'. and
  7805. `org-show-siblings'."
  7806. (let ((heading-p (org-on-heading-p t))
  7807. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7808. (following-p (org-get-alist-option org-show-following-heading key))
  7809. (entry-p (org-get-alist-option org-show-entry-below key))
  7810. (siblings-p (org-get-alist-option org-show-siblings key)))
  7811. (catch 'exit
  7812. ;; Show heading or entry text
  7813. (if (and heading-p (not entry-p))
  7814. (org-flag-heading nil) ; only show the heading
  7815. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7816. (org-show-hidden-entry))) ; show entire entry
  7817. (when following-p
  7818. ;; Show next sibling, or heading below text
  7819. (save-excursion
  7820. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7821. (org-flag-heading nil))))
  7822. (when siblings-p (org-show-siblings))
  7823. (when hierarchy-p
  7824. ;; show all higher headings, possibly with siblings
  7825. (save-excursion
  7826. (while (and (condition-case nil
  7827. (progn (org-up-heading-all 1) t)
  7828. (error nil))
  7829. (not (bobp)))
  7830. (org-flag-heading nil)
  7831. (when siblings-p (org-show-siblings))))))))
  7832. (defun org-reveal (&optional siblings)
  7833. "Show current entry, hierarchy above it, and the following headline.
  7834. This can be used to show a consistent set of context around locations
  7835. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7836. not t for the search context.
  7837. With optional argument SIBLINGS, on each level of the hierarchy all
  7838. siblings are shown. This repairs the tree structure to what it would
  7839. look like when opened with hierarchical calls to `org-cycle'."
  7840. (interactive "P")
  7841. (let ((org-show-hierarchy-above t)
  7842. (org-show-following-heading t)
  7843. (org-show-siblings (if siblings t org-show-siblings)))
  7844. (org-show-context nil)))
  7845. (defun org-highlight-new-match (beg end)
  7846. "Highlight from BEG to END and mark the highlight is an occur headline."
  7847. (let ((ov (org-make-overlay beg end)))
  7848. (org-overlay-put ov 'face 'secondary-selection)
  7849. (push ov org-occur-highlights)))
  7850. (defun org-remove-occur-highlights (&optional beg end noremove)
  7851. "Remove the occur highlights from the buffer.
  7852. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7853. from the `before-change-functions' in the current buffer."
  7854. (interactive)
  7855. (unless org-inhibit-highlight-removal
  7856. (mapc 'org-delete-overlay org-occur-highlights)
  7857. (setq org-occur-highlights nil)
  7858. (setq org-occur-parameters nil)
  7859. (unless noremove
  7860. (remove-hook 'before-change-functions
  7861. 'org-remove-occur-highlights 'local))))
  7862. ;;;; Priorities
  7863. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7864. "Regular expression matching the priority indicator.")
  7865. (defvar org-remove-priority-next-time nil)
  7866. (defun org-priority-up ()
  7867. "Increase the priority of the current item."
  7868. (interactive)
  7869. (org-priority 'up))
  7870. (defun org-priority-down ()
  7871. "Decrease the priority of the current item."
  7872. (interactive)
  7873. (org-priority 'down))
  7874. (defun org-priority (&optional action)
  7875. "Change the priority of an item by ARG.
  7876. ACTION can be `set', `up', `down', or a character."
  7877. (interactive)
  7878. (setq action (or action 'set))
  7879. (let (current new news have remove)
  7880. (save-excursion
  7881. (org-back-to-heading)
  7882. (if (looking-at org-priority-regexp)
  7883. (setq current (string-to-char (match-string 2))
  7884. have t)
  7885. (setq current org-default-priority))
  7886. (cond
  7887. ((or (eq action 'set)
  7888. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7889. (if (not (eq action 'set))
  7890. (setq new action)
  7891. (message "Priority %c-%c, SPC to remove: "
  7892. org-highest-priority org-lowest-priority)
  7893. (setq new (read-char-exclusive)))
  7894. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7895. (= (upcase org-lowest-priority) org-lowest-priority))
  7896. (setq new (upcase new)))
  7897. (cond ((equal new ?\ ) (setq remove t))
  7898. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7899. (error "Priority must be between `%c' and `%c'"
  7900. org-highest-priority org-lowest-priority))))
  7901. ((eq action 'up)
  7902. (if (and (not have) (eq last-command this-command))
  7903. (setq new org-lowest-priority)
  7904. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7905. org-default-priority (1- current)))))
  7906. ((eq action 'down)
  7907. (if (and (not have) (eq last-command this-command))
  7908. (setq new org-highest-priority)
  7909. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7910. org-default-priority (1+ current)))))
  7911. (t (error "Invalid action")))
  7912. (if (or (< (upcase new) org-highest-priority)
  7913. (> (upcase new) org-lowest-priority))
  7914. (setq remove t))
  7915. (setq news (format "%c" new))
  7916. (if have
  7917. (if remove
  7918. (replace-match "" t t nil 1)
  7919. (replace-match news t t nil 2))
  7920. (if remove
  7921. (error "No priority cookie found in line")
  7922. (looking-at org-todo-line-regexp)
  7923. (if (match-end 2)
  7924. (progn
  7925. (goto-char (match-end 2))
  7926. (insert " [#" news "]"))
  7927. (goto-char (match-beginning 3))
  7928. (insert "[#" news "] ")))))
  7929. (org-preserve-lc (org-set-tags nil 'align))
  7930. (if remove
  7931. (message "Priority removed")
  7932. (message "Priority of current item set to %s" news))))
  7933. (defun org-get-priority (s)
  7934. "Find priority cookie and return priority."
  7935. (save-match-data
  7936. (if (not (string-match org-priority-regexp s))
  7937. (* 1000 (- org-lowest-priority org-default-priority))
  7938. (* 1000 (- org-lowest-priority
  7939. (string-to-char (match-string 2 s)))))))
  7940. ;;;; Tags
  7941. (defvar org-agenda-archives-mode)
  7942. (defun org-scan-tags (action matcher &optional todo-only)
  7943. "Scan headline tags with inheritance and produce output ACTION.
  7944. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  7945. or `agenda' to produce an entry list for an agenda view. It can also be
  7946. a Lisp form or a function that should be called at each matched headline, in
  7947. this case the return value is a list of all return values from these calls.
  7948. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  7949. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  7950. only lines with a TODO keyword are included in the output."
  7951. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7952. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7953. (org-re
  7954. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7955. (props (list 'face 'default
  7956. 'done-face 'org-done
  7957. 'undone-face 'default
  7958. 'mouse-face 'highlight
  7959. 'org-not-done-regexp org-not-done-regexp
  7960. 'org-todo-regexp org-todo-regexp
  7961. 'keymap org-agenda-keymap
  7962. 'help-echo
  7963. (format "mouse-2 or RET jump to org file %s"
  7964. (abbreviate-file-name
  7965. (or (buffer-file-name (buffer-base-buffer))
  7966. (buffer-name (buffer-base-buffer)))))))
  7967. (case-fold-search nil)
  7968. lspos tags tags-list
  7969. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  7970. (llast 0) rtn rtn1 level category i txt
  7971. todo marker entry priority)
  7972. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  7973. (setq action (list 'lambda nil action)))
  7974. (save-excursion
  7975. (goto-char (point-min))
  7976. (when (eq action 'sparse-tree)
  7977. (org-overview)
  7978. (org-remove-occur-highlights))
  7979. (while (re-search-forward re nil t)
  7980. (catch :skip
  7981. (setq todo (if (match-end 1) (match-string 2))
  7982. tags (if (match-end 4) (match-string 4)))
  7983. (goto-char (setq lspos (1+ (match-beginning 0))))
  7984. (setq level (org-reduced-level (funcall outline-level))
  7985. category (org-get-category))
  7986. (setq i llast llast level)
  7987. ;; remove tag lists from same and sublevels
  7988. (while (>= i level)
  7989. (when (setq entry (assoc i tags-alist))
  7990. (setq tags-alist (delete entry tags-alist)))
  7991. (setq i (1- i)))
  7992. ;; add the next tags
  7993. (when tags
  7994. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7995. tags-alist
  7996. (cons (cons level tags) tags-alist)))
  7997. ;; compile tags for current headline
  7998. (setq tags-list
  7999. (if org-use-tag-inheritance
  8000. (apply 'append (mapcar 'cdr tags-alist))
  8001. tags))
  8002. (when (and tags org-use-tag-inheritance
  8003. (not (eq t org-use-tag-inheritance)))
  8004. ;; selective inheritance, remove uninherited ones
  8005. (setcdr (car tags-alist)
  8006. (org-remove-uniherited-tags (cdar tags-alist))))
  8007. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8008. (let ((case-fold-search t)) (eval matcher))
  8009. (or
  8010. (not (member org-archive-tag tags-list))
  8011. ;; we have an archive tag, should we use this anyway?
  8012. (or (not org-agenda-skip-archived-trees)
  8013. (and (eq action 'agenda) org-agenda-archives-mode))))
  8014. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8015. ;; select this headline
  8016. (cond
  8017. ((eq action 'sparse-tree)
  8018. (and org-highlight-sparse-tree-matches
  8019. (org-get-heading) (match-end 0)
  8020. (org-highlight-new-match
  8021. (match-beginning 0) (match-beginning 1)))
  8022. (org-show-context 'tags-tree))
  8023. ((eq action 'agenda)
  8024. (setq txt (org-format-agenda-item
  8025. ""
  8026. (concat
  8027. (if org-tags-match-list-sublevels
  8028. (make-string (1- level) ?.) "")
  8029. (org-get-heading))
  8030. category tags-list)
  8031. priority (org-get-priority txt))
  8032. (goto-char lspos)
  8033. (setq marker (org-agenda-new-marker))
  8034. (org-add-props txt props
  8035. 'org-marker marker 'org-hd-marker marker 'org-category category
  8036. 'priority priority 'type "tagsmatch")
  8037. (push txt rtn))
  8038. ((functionp action)
  8039. (save-excursion
  8040. (setq rtn1 (funcall action))
  8041. (push rtn1 rtn))
  8042. (goto-char (point-at-eol)))
  8043. (t (error "Invalid action")))
  8044. ;; if we are to skip sublevels, jump to end of subtree
  8045. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8046. (when (and (eq action 'sparse-tree)
  8047. (not org-sparse-tree-open-archived-trees))
  8048. (org-hide-archived-subtrees (point-min) (point-max)))
  8049. (nreverse rtn)))
  8050. (defun org-remove-uniherited-tags (tags)
  8051. "Remove all tags that are not inherited from the list TAGS."
  8052. (cond
  8053. ((eq org-use-tag-inheritance t) tags)
  8054. ((not org-use-tag-inheritance) nil)
  8055. ((stringp org-use-tag-inheritance)
  8056. (delq nil (mapcar
  8057. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8058. tags)))
  8059. ((listp org-use-tag-inheritance)
  8060. (delq nil (mapcar
  8061. (lambda (x) (if (member x org-use-tag-inheritance) x nil))
  8062. tags)))))
  8063. (defvar todo-only) ;; dynamically scoped
  8064. (defun org-tags-sparse-tree (&optional todo-only match)
  8065. "Create a sparse tree according to tags string MATCH.
  8066. MATCH can contain positive and negative selection of tags, like
  8067. \"+WORK+URGENT-WITHBOSS\".
  8068. If optional argument TODO_ONLY is non-nil, only select lines that are
  8069. also TODO lines."
  8070. (interactive "P")
  8071. (org-prepare-agenda-buffers (list (current-buffer)))
  8072. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8073. (defvar org-cached-props nil)
  8074. (defun org-cached-entry-get (pom property)
  8075. (if (or (eq t org-use-property-inheritance)
  8076. (and (stringp org-use-property-inheritance)
  8077. (string-match org-use-property-inheritance property))
  8078. (and (listp org-use-property-inheritance)
  8079. (member property org-use-property-inheritance)))
  8080. ;; Caching is not possible, check it directly
  8081. (org-entry-get pom property 'inherit)
  8082. ;; Get all properties, so that we can do complicated checks easily
  8083. (cdr (assoc property (or org-cached-props
  8084. (setq org-cached-props
  8085. (org-entry-properties pom)))))))
  8086. (defun org-global-tags-completion-table (&optional files)
  8087. "Return the list of all tags in all agenda buffer/files."
  8088. (save-excursion
  8089. (org-uniquify
  8090. (delq nil
  8091. (apply 'append
  8092. (mapcar
  8093. (lambda (file)
  8094. (set-buffer (find-file-noselect file))
  8095. (append (org-get-buffer-tags)
  8096. (mapcar (lambda (x) (if (stringp (car-safe x))
  8097. (list (car-safe x)) nil))
  8098. org-tag-alist)))
  8099. (if (and files (car files))
  8100. files
  8101. (org-agenda-files))))))))
  8102. (defun org-make-tags-matcher (match)
  8103. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8104. ;; todo-only is scoped dynamically into this function, and the function
  8105. ;; may change it it the matcher asksk for it.
  8106. (unless match
  8107. ;; Get a new match request, with completion
  8108. (let ((org-last-tags-completion-table
  8109. (org-global-tags-completion-table)))
  8110. (setq match (completing-read
  8111. "Match: " 'org-tags-completion-function nil nil nil
  8112. 'org-tags-history))))
  8113. ;; Parse the string and create a lisp form
  8114. (let ((match0 match)
  8115. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8116. minus tag mm
  8117. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8118. orterms term orlist re-p str-p level-p level-op time-p
  8119. prop-p pn pv po cat-p gv)
  8120. (if (string-match "/+" match)
  8121. ;; match contains also a todo-matching request
  8122. (progn
  8123. (setq tagsmatch (substring match 0 (match-beginning 0))
  8124. todomatch (substring match (match-end 0)))
  8125. (if (string-match "^!" todomatch)
  8126. (setq todo-only t todomatch (substring todomatch 1)))
  8127. (if (string-match "^\\s-*$" todomatch)
  8128. (setq todomatch nil)))
  8129. ;; only matching tags
  8130. (setq tagsmatch match todomatch nil))
  8131. ;; Make the tags matcher
  8132. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8133. (setq tagsmatcher t)
  8134. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8135. (while (setq term (pop orterms))
  8136. (while (and (equal (substring term -1) "\\") orterms)
  8137. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8138. (while (string-match re term)
  8139. (setq minus (and (match-end 1)
  8140. (equal (match-string 1 term) "-"))
  8141. tag (match-string 2 term)
  8142. re-p (equal (string-to-char tag) ?{)
  8143. level-p (match-end 4)
  8144. prop-p (match-end 5)
  8145. mm (cond
  8146. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8147. (level-p
  8148. (setq level-op (org-op-to-function (match-string 3 term)))
  8149. `(,level-op level ,(string-to-number
  8150. (match-string 4 term))))
  8151. (prop-p
  8152. (setq pn (match-string 5 term)
  8153. po (match-string 6 term)
  8154. pv (match-string 7 term)
  8155. cat-p (equal pn "CATEGORY")
  8156. re-p (equal (string-to-char pv) ?{)
  8157. str-p (equal (string-to-char pv) ?\")
  8158. time-p (save-match-data (string-match "^\"<.*>\"$" pv))
  8159. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8160. (if time-p (setq pv (org-matcher-time pv)))
  8161. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8162. (if (equal pn "CATEGORY")
  8163. (setq gv '(get-text-property (point) 'org-category))
  8164. (setq gv `(org-cached-entry-get nil ,pn)))
  8165. (if re-p
  8166. (if (eq po 'org<>)
  8167. `(not (string-match ,pv (or ,gv "")))
  8168. `(string-match ,pv (or ,gv "")))
  8169. (if str-p
  8170. `(,po (or ,gv "") ,pv)
  8171. `(,po (string-to-number (or ,gv ""))
  8172. ,(string-to-number pv) ))))
  8173. (t `(member ,(downcase tag) tags-list)))
  8174. mm (if minus (list 'not mm) mm)
  8175. term (substring term (match-end 0)))
  8176. (push mm tagsmatcher))
  8177. (push (if (> (length tagsmatcher) 1)
  8178. (cons 'and tagsmatcher)
  8179. (car tagsmatcher))
  8180. orlist)
  8181. (setq tagsmatcher nil))
  8182. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8183. (setq tagsmatcher
  8184. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8185. ;; Make the todo matcher
  8186. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8187. (setq todomatcher t)
  8188. (setq orterms (org-split-string todomatch "|") orlist nil)
  8189. (while (setq term (pop orterms))
  8190. (while (string-match re term)
  8191. (setq minus (and (match-end 1)
  8192. (equal (match-string 1 term) "-"))
  8193. kwd (match-string 2 term)
  8194. re-p (equal (string-to-char kwd) ?{)
  8195. term (substring term (match-end 0))
  8196. mm (if re-p
  8197. `(string-match ,(substring kwd 1 -1) todo)
  8198. (list 'equal 'todo kwd))
  8199. mm (if minus (list 'not mm) mm))
  8200. (push mm todomatcher))
  8201. (push (if (> (length todomatcher) 1)
  8202. (cons 'and todomatcher)
  8203. (car todomatcher))
  8204. orlist)
  8205. (setq todomatcher nil))
  8206. (setq todomatcher (if (> (length orlist) 1)
  8207. (cons 'or orlist) (car orlist))))
  8208. ;; Return the string and lisp forms of the matcher
  8209. (setq matcher (if todomatcher
  8210. (list 'and tagsmatcher todomatcher)
  8211. tagsmatcher))
  8212. (cons match0 matcher)))
  8213. (defun org-op-to-function (op &optional stringp)
  8214. "Turn an operator into the appropriate function."
  8215. (setq op
  8216. (cond
  8217. ((equal op "<" ) '(< string< org-time<))
  8218. ((equal op ">" ) '(> org-string> org-time>))
  8219. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8220. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8221. ((member op '("=" "==")) '(= string= org-time=))
  8222. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8223. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8224. (defun org<> (a b) (not (= a b)))
  8225. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8226. (defun org-string>= (a b) (not (string< a b)))
  8227. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8228. (defun org-string<> (a b) (not (string= a b)))
  8229. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8230. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8231. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8232. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8233. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8234. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8235. (defun org-2ft (s)
  8236. "Convert S to a floating point time.
  8237. If S is already a number, just return it. If it is a string, parse
  8238. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8239. (cond
  8240. ((numberp s) s)
  8241. ((stringp s)
  8242. (condition-case nil
  8243. (float-time (apply 'encode-time (org-parse-time-string s)))
  8244. (error 0.)))
  8245. (t 0.)))
  8246. (defun org-matcher-time (s)
  8247. (cond
  8248. ((equal s "<now>") (float-time))
  8249. ((equal s "<today>")
  8250. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8251. (t (org-2ft s))))
  8252. (defun org-match-any-p (re list)
  8253. "Does re match any element of list?"
  8254. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8255. (delq nil list))
  8256. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8257. (defvar org-tags-overlay (org-make-overlay 1 1))
  8258. (org-detach-overlay org-tags-overlay)
  8259. (defun org-get-local-tags-at (&optional pos)
  8260. "Get a list of tags defined in the current headline."
  8261. (org-get-tags-at pos 'local))
  8262. (defun org-get-local-tags ()
  8263. "Get a list of tags defined in the current headline."
  8264. (org-get-tags-at nil 'local))
  8265. (defun org-get-tags-at (&optional pos local)
  8266. "Get a list of all headline tags applicable at POS.
  8267. POS defaults to point. If tags are inherited, the list contains
  8268. the targets in the same sequence as the headlines appear, i.e.
  8269. the tags of the current headline come last.
  8270. When LOCAL is non-nil, only return tags from the current headline,
  8271. ignore inherited ones."
  8272. (interactive)
  8273. (let (tags ltags lastpos parent)
  8274. (save-excursion
  8275. (save-restriction
  8276. (widen)
  8277. (goto-char (or pos (point)))
  8278. (save-match-data
  8279. (catch 'done
  8280. (condition-case nil
  8281. (progn
  8282. (org-back-to-heading t)
  8283. (while (not (equal lastpos (point)))
  8284. (setq lastpos (point))
  8285. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8286. (setq ltags (org-split-string
  8287. (org-match-string-no-properties 1) ":"))
  8288. (setq tags (append
  8289. (if parent
  8290. (org-remove-uniherited-tags ltags)
  8291. ltags)
  8292. tags)))
  8293. (or org-use-tag-inheritance (throw 'done t))
  8294. (if local (throw 'done t))
  8295. (org-up-heading-all 1)
  8296. (setq parent t)))
  8297. (error nil)))))
  8298. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8299. (defun org-toggle-tag (tag &optional onoff)
  8300. "Toggle the tag TAG for the current line.
  8301. If ONOFF is `on' or `off', don't toggle but set to this state."
  8302. (unless (org-on-heading-p t) (error "Not on headling"))
  8303. (let (res current)
  8304. (save-excursion
  8305. (beginning-of-line)
  8306. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8307. (point-at-eol) t)
  8308. (progn
  8309. (setq current (match-string 1))
  8310. (replace-match ""))
  8311. (setq current ""))
  8312. (setq current (nreverse (org-split-string current ":")))
  8313. (cond
  8314. ((eq onoff 'on)
  8315. (setq res t)
  8316. (or (member tag current) (push tag current)))
  8317. ((eq onoff 'off)
  8318. (or (not (member tag current)) (setq current (delete tag current))))
  8319. (t (if (member tag current)
  8320. (setq current (delete tag current))
  8321. (setq res t)
  8322. (push tag current))))
  8323. (end-of-line 1)
  8324. (if current
  8325. (progn
  8326. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8327. (org-set-tags nil t))
  8328. (delete-horizontal-space))
  8329. (run-hooks 'org-after-tags-change-hook))
  8330. res))
  8331. (defun org-align-tags-here (to-col)
  8332. ;; Assumes that this is a headline
  8333. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8334. (beginning-of-line 1)
  8335. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8336. (< pos (match-beginning 2)))
  8337. (progn
  8338. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8339. (goto-char (match-beginning 1))
  8340. (insert " ")
  8341. (delete-region (point) (1+ (match-beginning 2)))
  8342. (setq ncol (max (1+ (current-column))
  8343. (1+ col)
  8344. (if (> to-col 0)
  8345. to-col
  8346. (- (abs to-col) tags-l))))
  8347. (setq p (point))
  8348. (insert (make-string (- ncol (current-column)) ?\ ))
  8349. (setq ncol (current-column))
  8350. (when indent-tabs-mode (tabify p (point-at-eol)))
  8351. (org-move-to-column (min ncol col) t))
  8352. (goto-char pos))))
  8353. (defun org-set-tags-command (&optional arg just-align)
  8354. "Call the set-tags command for the current entry."
  8355. (interactive "P")
  8356. (if (org-on-heading-p)
  8357. (org-set-tags arg just-align)
  8358. (save-excursion
  8359. (org-back-to-heading t)
  8360. (org-set-tags arg just-align))))
  8361. (defun org-set-tags (&optional arg just-align)
  8362. "Set the tags for the current headline.
  8363. With prefix ARG, realign all tags in headings in the current buffer."
  8364. (interactive "P")
  8365. (let* ((re (concat "^" outline-regexp))
  8366. (current (org-get-tags-string))
  8367. (col (current-column))
  8368. (org-setting-tags t)
  8369. table current-tags inherited-tags ; computed below when needed
  8370. tags p0 c0 c1 rpl)
  8371. (if arg
  8372. (save-excursion
  8373. (goto-char (point-min))
  8374. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8375. (while (re-search-forward re nil t)
  8376. (org-set-tags nil t)
  8377. (end-of-line 1)))
  8378. (message "All tags realigned to column %d" org-tags-column))
  8379. (if just-align
  8380. (setq tags current)
  8381. ;; Get a new set of tags from the user
  8382. (save-excursion
  8383. (setq table (or org-tag-alist (org-get-buffer-tags))
  8384. org-last-tags-completion-table table
  8385. current-tags (org-split-string current ":")
  8386. inherited-tags (nreverse
  8387. (nthcdr (length current-tags)
  8388. (nreverse (org-get-tags-at))))
  8389. tags
  8390. (if (or (eq t org-use-fast-tag-selection)
  8391. (and org-use-fast-tag-selection
  8392. (delq nil (mapcar 'cdr table))))
  8393. (org-fast-tag-selection
  8394. current-tags inherited-tags table
  8395. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8396. (let ((org-add-colon-after-tag-completion t))
  8397. (org-trim
  8398. (org-without-partial-completion
  8399. (completing-read "Tags: " 'org-tags-completion-function
  8400. nil nil current 'org-tags-history)))))))
  8401. (while (string-match "[-+&]+" tags)
  8402. ;; No boolean logic, just a list
  8403. (setq tags (replace-match ":" t t tags))))
  8404. (if (string-match "\\`[\t ]*\\'" tags)
  8405. (setq tags "")
  8406. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8407. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8408. ;; Insert new tags at the correct column
  8409. (beginning-of-line 1)
  8410. (cond
  8411. ((and (equal current "") (equal tags "")))
  8412. ((re-search-forward
  8413. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8414. (point-at-eol) t)
  8415. (if (equal tags "")
  8416. (setq rpl "")
  8417. (goto-char (match-beginning 0))
  8418. (setq c0 (current-column) p0 (point)
  8419. c1 (max (1+ c0) (if (> org-tags-column 0)
  8420. org-tags-column
  8421. (- (- org-tags-column) (length tags))))
  8422. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8423. (replace-match rpl t t)
  8424. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8425. tags)
  8426. (t (error "Tags alignment failed")))
  8427. (org-move-to-column col)
  8428. (unless just-align
  8429. (run-hooks 'org-after-tags-change-hook)))))
  8430. (defun org-change-tag-in-region (beg end tag off)
  8431. "Add or remove TAG for each entry in the region.
  8432. This works in the agenda, and also in an org-mode buffer."
  8433. (interactive
  8434. (list (region-beginning) (region-end)
  8435. (let ((org-last-tags-completion-table
  8436. (if (org-mode-p)
  8437. (org-get-buffer-tags)
  8438. (org-global-tags-completion-table))))
  8439. (completing-read
  8440. "Tag: " 'org-tags-completion-function nil nil nil
  8441. 'org-tags-history))
  8442. (progn
  8443. (message "[s]et or [r]emove? ")
  8444. (equal (read-char-exclusive) ?r))))
  8445. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8446. (let ((agendap (equal major-mode 'org-agenda-mode))
  8447. l1 l2 m buf pos newhead (cnt 0))
  8448. (goto-char end)
  8449. (setq l2 (1- (org-current-line)))
  8450. (goto-char beg)
  8451. (setq l1 (org-current-line))
  8452. (loop for l from l1 to l2 do
  8453. (goto-line l)
  8454. (setq m (get-text-property (point) 'org-hd-marker))
  8455. (when (or (and (org-mode-p) (org-on-heading-p))
  8456. (and agendap m))
  8457. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8458. pos (if agendap m (point)))
  8459. (with-current-buffer buf
  8460. (save-excursion
  8461. (save-restriction
  8462. (goto-char pos)
  8463. (setq cnt (1+ cnt))
  8464. (org-toggle-tag tag (if off 'off 'on))
  8465. (setq newhead (org-get-heading)))))
  8466. (and agendap (org-agenda-change-all-lines newhead m))))
  8467. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8468. (defun org-tags-completion-function (string predicate &optional flag)
  8469. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8470. (confirm (lambda (x) (stringp (car x)))))
  8471. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8472. (setq s1 (match-string 1 string)
  8473. s2 (match-string 2 string))
  8474. (setq s1 "" s2 string))
  8475. (cond
  8476. ((eq flag nil)
  8477. ;; try completion
  8478. (setq rtn (try-completion s2 ctable confirm))
  8479. (if (stringp rtn)
  8480. (setq rtn
  8481. (concat s1 s2 (substring rtn (length s2))
  8482. (if (and org-add-colon-after-tag-completion
  8483. (assoc rtn ctable))
  8484. ":" ""))))
  8485. rtn)
  8486. ((eq flag t)
  8487. ;; all-completions
  8488. (all-completions s2 ctable confirm)
  8489. )
  8490. ((eq flag 'lambda)
  8491. ;; exact match?
  8492. (assoc s2 ctable)))
  8493. ))
  8494. (defun org-fast-tag-insert (kwd tags face &optional end)
  8495. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8496. (insert (format "%-12s" (concat kwd ":"))
  8497. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8498. (or end "")))
  8499. (defun org-fast-tag-show-exit (flag)
  8500. (save-excursion
  8501. (goto-line 3)
  8502. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8503. (replace-match ""))
  8504. (when flag
  8505. (end-of-line 1)
  8506. (org-move-to-column (- (window-width) 19) t)
  8507. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8508. (defun org-set-current-tags-overlay (current prefix)
  8509. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8510. (if (featurep 'xemacs)
  8511. (org-overlay-display org-tags-overlay (concat prefix s)
  8512. 'secondary-selection)
  8513. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8514. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8515. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8516. "Fast tag selection with single keys.
  8517. CURRENT is the current list of tags in the headline, INHERITED is the
  8518. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8519. possibly with grouping information. TODO-TABLE is a similar table with
  8520. TODO keywords, should these have keys assigned to them.
  8521. If the keys are nil, a-z are automatically assigned.
  8522. Returns the new tags string, or nil to not change the current settings."
  8523. (let* ((fulltable (append table todo-table))
  8524. (maxlen (apply 'max (mapcar
  8525. (lambda (x)
  8526. (if (stringp (car x)) (string-width (car x)) 0))
  8527. fulltable)))
  8528. (buf (current-buffer))
  8529. (expert (eq org-fast-tag-selection-single-key 'expert))
  8530. (buffer-tags nil)
  8531. (fwidth (+ maxlen 3 1 3))
  8532. (ncol (/ (- (window-width) 4) fwidth))
  8533. (i-face 'org-done)
  8534. (c-face 'org-todo)
  8535. tg cnt e c char c1 c2 ntable tbl rtn
  8536. ov-start ov-end ov-prefix
  8537. (exit-after-next org-fast-tag-selection-single-key)
  8538. (done-keywords org-done-keywords)
  8539. groups ingroup)
  8540. (save-excursion
  8541. (beginning-of-line 1)
  8542. (if (looking-at
  8543. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8544. (setq ov-start (match-beginning 1)
  8545. ov-end (match-end 1)
  8546. ov-prefix "")
  8547. (setq ov-start (1- (point-at-eol))
  8548. ov-end (1+ ov-start))
  8549. (skip-chars-forward "^\n\r")
  8550. (setq ov-prefix
  8551. (concat
  8552. (buffer-substring (1- (point)) (point))
  8553. (if (> (current-column) org-tags-column)
  8554. " "
  8555. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8556. (org-move-overlay org-tags-overlay ov-start ov-end)
  8557. (save-window-excursion
  8558. (if expert
  8559. (set-buffer (get-buffer-create " *Org tags*"))
  8560. (delete-other-windows)
  8561. (split-window-vertically)
  8562. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8563. (erase-buffer)
  8564. (org-set-local 'org-done-keywords done-keywords)
  8565. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8566. (org-fast-tag-insert "Current" current c-face "\n\n")
  8567. (org-fast-tag-show-exit exit-after-next)
  8568. (org-set-current-tags-overlay current ov-prefix)
  8569. (setq tbl fulltable char ?a cnt 0)
  8570. (while (setq e (pop tbl))
  8571. (cond
  8572. ((equal e '(:startgroup))
  8573. (push '() groups) (setq ingroup t)
  8574. (when (not (= cnt 0))
  8575. (setq cnt 0)
  8576. (insert "\n"))
  8577. (insert "{ "))
  8578. ((equal e '(:endgroup))
  8579. (setq ingroup nil cnt 0)
  8580. (insert "}\n"))
  8581. (t
  8582. (setq tg (car e) c2 nil)
  8583. (if (cdr e)
  8584. (setq c (cdr e))
  8585. ;; automatically assign a character.
  8586. (setq c1 (string-to-char
  8587. (downcase (substring
  8588. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8589. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8590. (while (or (rassoc char ntable) (rassoc char table))
  8591. (setq char (1+ char)))
  8592. (setq c2 c1))
  8593. (setq c (or c2 char)))
  8594. (if ingroup (push tg (car groups)))
  8595. (setq tg (org-add-props tg nil 'face
  8596. (cond
  8597. ((not (assoc tg table))
  8598. (org-get-todo-face tg))
  8599. ((member tg current) c-face)
  8600. ((member tg inherited) i-face)
  8601. (t nil))))
  8602. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8603. (insert "[" c "] " tg (make-string
  8604. (- fwidth 4 (length tg)) ?\ ))
  8605. (push (cons tg c) ntable)
  8606. (when (= (setq cnt (1+ cnt)) ncol)
  8607. (insert "\n")
  8608. (if ingroup (insert " "))
  8609. (setq cnt 0)))))
  8610. (setq ntable (nreverse ntable))
  8611. (insert "\n")
  8612. (goto-char (point-min))
  8613. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8614. (fit-window-to-buffer))
  8615. (setq rtn
  8616. (catch 'exit
  8617. (while t
  8618. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8619. (if groups " [!] no groups" " [!]groups")
  8620. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8621. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8622. (cond
  8623. ((= c ?\r) (throw 'exit t))
  8624. ((= c ?!)
  8625. (setq groups (not groups))
  8626. (goto-char (point-min))
  8627. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8628. ((= c ?\C-c)
  8629. (if (not expert)
  8630. (org-fast-tag-show-exit
  8631. (setq exit-after-next (not exit-after-next)))
  8632. (setq expert nil)
  8633. (delete-other-windows)
  8634. (split-window-vertically)
  8635. (org-switch-to-buffer-other-window " *Org tags*")
  8636. (and (fboundp 'fit-window-to-buffer)
  8637. (fit-window-to-buffer))))
  8638. ((or (= c ?\C-g)
  8639. (and (= c ?q) (not (rassoc c ntable))))
  8640. (org-detach-overlay org-tags-overlay)
  8641. (setq quit-flag t))
  8642. ((= c ?\ )
  8643. (setq current nil)
  8644. (if exit-after-next (setq exit-after-next 'now)))
  8645. ((= c ?\t)
  8646. (condition-case nil
  8647. (setq tg (completing-read
  8648. "Tag: "
  8649. (or buffer-tags
  8650. (with-current-buffer buf
  8651. (org-get-buffer-tags)))))
  8652. (quit (setq tg "")))
  8653. (when (string-match "\\S-" tg)
  8654. (add-to-list 'buffer-tags (list tg))
  8655. (if (member tg current)
  8656. (setq current (delete tg current))
  8657. (push tg current)))
  8658. (if exit-after-next (setq exit-after-next 'now)))
  8659. ((setq e (rassoc c todo-table) tg (car e))
  8660. (with-current-buffer buf
  8661. (save-excursion (org-todo tg)))
  8662. (if exit-after-next (setq exit-after-next 'now)))
  8663. ((setq e (rassoc c ntable) tg (car e))
  8664. (if (member tg current)
  8665. (setq current (delete tg current))
  8666. (loop for g in groups do
  8667. (if (member tg g)
  8668. (mapc (lambda (x)
  8669. (setq current (delete x current)))
  8670. g)))
  8671. (push tg current))
  8672. (if exit-after-next (setq exit-after-next 'now))))
  8673. ;; Create a sorted list
  8674. (setq current
  8675. (sort current
  8676. (lambda (a b)
  8677. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8678. (if (eq exit-after-next 'now) (throw 'exit t))
  8679. (goto-char (point-min))
  8680. (beginning-of-line 2)
  8681. (delete-region (point) (point-at-eol))
  8682. (org-fast-tag-insert "Current" current c-face)
  8683. (org-set-current-tags-overlay current ov-prefix)
  8684. (while (re-search-forward
  8685. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8686. (setq tg (match-string 1))
  8687. (add-text-properties
  8688. (match-beginning 1) (match-end 1)
  8689. (list 'face
  8690. (cond
  8691. ((member tg current) c-face)
  8692. ((member tg inherited) i-face)
  8693. (t (get-text-property (match-beginning 1) 'face))))))
  8694. (goto-char (point-min)))))
  8695. (org-detach-overlay org-tags-overlay)
  8696. (if rtn
  8697. (mapconcat 'identity current ":")
  8698. nil))))
  8699. (defun org-get-tags-string ()
  8700. "Get the TAGS string in the current headline."
  8701. (unless (org-on-heading-p t)
  8702. (error "Not on a heading"))
  8703. (save-excursion
  8704. (beginning-of-line 1)
  8705. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8706. (org-match-string-no-properties 1)
  8707. "")))
  8708. (defun org-get-tags ()
  8709. "Get the list of tags specified in the current headline."
  8710. (org-split-string (org-get-tags-string) ":"))
  8711. (defun org-get-buffer-tags ()
  8712. "Get a table of all tags used in the buffer, for completion."
  8713. (let (tags)
  8714. (save-excursion
  8715. (goto-char (point-min))
  8716. (while (re-search-forward
  8717. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8718. (when (equal (char-after (point-at-bol 0)) ?*)
  8719. (mapc (lambda (x) (add-to-list 'tags x))
  8720. (org-split-string (org-match-string-no-properties 1) ":")))))
  8721. (mapcar 'list tags)))
  8722. ;;;; The mapping API
  8723. ;;;###autoload
  8724. (defun org-map-entries (func &optional match scope &rest skip)
  8725. "Call FUNC at each headline selected by MATCH in SCOPE.
  8726. FUNC is a function or a lisp form. The function will be called without
  8727. arguments, with the cursor positioned at the beginning of the headline.
  8728. The return values of all calls to the function will be collected and
  8729. returned as a list.
  8730. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8731. Only headlines that are matched by this query will be considered during
  8732. the iteration. When MATCH is nil or t, all headlines will be
  8733. visited by the iteration.
  8734. SCOPE determines the scope of this command. It can be any of:
  8735. nil The current buffer, respecting the restriction if any
  8736. tree The subtree started with the entry at point
  8737. file The current buffer, without restriction
  8738. file-with-archives
  8739. The current buffer, and any archives associated with it
  8740. agenda All agenda files
  8741. agenda-with-archives
  8742. All agenda files with any archive files associated with them
  8743. \(file1 file2 ...)
  8744. If this is a list, all files in the list will be scanned
  8745. The remaining args are treated as settings for the skipping facilities of
  8746. the scanner. The following items can be given here:
  8747. archive skip trees with the archive tag.
  8748. comment skip trees with the COMMENT keyword
  8749. function or Emacs Lisp form:
  8750. will be used as value for `org-agenda-skip-function', so whenever
  8751. the the function returns t, FUNC will not be called for that
  8752. entry and search will continue from the point where the
  8753. function leaves it."
  8754. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8755. (org-agenda-skip-archived-trees (memq 'archive skip))
  8756. (org-agenda-skip-comment-trees (memq 'comment skip))
  8757. (org-agenda-skip-function
  8758. (car (org-delete-all '(comment archive) skip)))
  8759. (org-tags-match-list-sublevels t)
  8760. matcher pos file
  8761. org-todo-keywords-for-agenda
  8762. org-done-keywords-for-agenda
  8763. org-todo-keyword-alist-for-agenda
  8764. org-tag-alist-for-agenda)
  8765. (cond
  8766. ((eq match t) (setq matcher t))
  8767. ((eq match nil) (setq matcher t))
  8768. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  8769. (when (eq scope 'tree)
  8770. (org-back-to-heading t)
  8771. (org-narrow-to-subtree)
  8772. (setq scope nil))
  8773. (if (not scope)
  8774. (progn
  8775. (org-prepare-agenda-buffers
  8776. (list (buffer-file-name (current-buffer))))
  8777. (org-scan-tags func matcher))
  8778. ;; Get the right scope
  8779. (setq pos (point))
  8780. (cond
  8781. ((and scope (listp scope) (symbolp (car scope)))
  8782. (setq scope (eval scope)))
  8783. ((eq scope 'agenda)
  8784. (setq scope (org-agenda-files t)))
  8785. ((eq scope 'agenda-with-archives)
  8786. (setq scope (org-agenda-files t))
  8787. (setq scope (org-add-archive-files scope)))
  8788. ((eq scope 'file)
  8789. (setq scope (list (buffer-file-name))))
  8790. ((eq scope 'file-with-archives)
  8791. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  8792. (org-prepare-agenda-buffers scope)
  8793. (while (setq file (pop scope))
  8794. (with-current-buffer (org-find-base-buffer-visiting file)
  8795. (save-excursion
  8796. (save-restriction
  8797. (widen)
  8798. (goto-char (point-min))
  8799. (org-scan-tags func matcher))))))))
  8800. ;;;; Properties
  8801. ;;; Setting and retrieving properties
  8802. (defconst org-special-properties
  8803. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8804. "TIMESTAMP" "TIMESTAMP_IA")
  8805. "The special properties valid in Org-mode.
  8806. These are properties that are not defined in the property drawer,
  8807. but in some other way.")
  8808. (defconst org-default-properties
  8809. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8810. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8811. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8812. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8813. "Some properties that are used by Org-mode for various purposes.
  8814. Being in this list makes sure that they are offered for completion.")
  8815. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8816. "Regular expression matching the first line of a property drawer.")
  8817. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8818. "Regular expression matching the first line of a property drawer.")
  8819. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  8820. "Regular expression matching the first line of a property drawer.")
  8821. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  8822. "Regular expression matching the first line of a property drawer.")
  8823. (defconst org-property-drawer-re
  8824. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  8825. org-property-end-re "\\)\n?")
  8826. "Matches an entire property drawer.")
  8827. (defconst org-clock-drawer-re
  8828. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  8829. org-property-end-re "\\)\n?")
  8830. "Matches an entire clock drawer.")
  8831. (defun org-property-action ()
  8832. "Do an action on properties."
  8833. (interactive)
  8834. (let (c)
  8835. (org-at-property-p)
  8836. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8837. (setq c (read-char-exclusive))
  8838. (cond
  8839. ((equal c ?s)
  8840. (call-interactively 'org-set-property))
  8841. ((equal c ?d)
  8842. (call-interactively 'org-delete-property))
  8843. ((equal c ?D)
  8844. (call-interactively 'org-delete-property-globally))
  8845. ((equal c ?c)
  8846. (call-interactively 'org-compute-property-at-point))
  8847. (t (error "No such property action %c" c)))))
  8848. (defun org-at-property-p ()
  8849. "Is the cursor in a property line?"
  8850. ;; FIXME: Does not check if we are actually in the drawer.
  8851. ;; FIXME: also returns true on any drawers.....
  8852. ;; This is used by C-c C-c for property action.
  8853. (save-excursion
  8854. (beginning-of-line 1)
  8855. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8856. (defun org-get-property-block (&optional beg end force)
  8857. "Return the (beg . end) range of the body of the property drawer.
  8858. BEG and END can be beginning and end of subtree, if not given
  8859. they will be found.
  8860. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8861. (catch 'exit
  8862. (save-excursion
  8863. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8864. (end (or end (progn (outline-next-heading) (point)))))
  8865. (goto-char beg)
  8866. (if (re-search-forward org-property-start-re end t)
  8867. (setq beg (1+ (match-end 0)))
  8868. (if force
  8869. (save-excursion
  8870. (org-insert-property-drawer)
  8871. (setq end (progn (outline-next-heading) (point))))
  8872. (throw 'exit nil))
  8873. (goto-char beg)
  8874. (if (re-search-forward org-property-start-re end t)
  8875. (setq beg (1+ (match-end 0)))))
  8876. (if (re-search-forward org-property-end-re end t)
  8877. (setq end (match-beginning 0))
  8878. (or force (throw 'exit nil))
  8879. (goto-char beg)
  8880. (setq end beg)
  8881. (org-indent-line-function)
  8882. (insert ":END:\n"))
  8883. (cons beg end)))))
  8884. (defun org-entry-properties (&optional pom which)
  8885. "Get all properties of the entry at point-or-marker POM.
  8886. This includes the TODO keyword, the tags, time strings for deadline,
  8887. scheduled, and clocking, and any additional properties defined in the
  8888. entry. The return value is an alist, keys may occur multiple times
  8889. if the property key was used several times.
  8890. POM may also be nil, in which case the current entry is used.
  8891. If WHICH is nil or `all', get all properties. If WHICH is
  8892. `special' or `standard', only get that subclass."
  8893. (setq which (or which 'all))
  8894. (org-with-point-at pom
  8895. (let ((clockstr (substring org-clock-string 0 -1))
  8896. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8897. beg end range props sum-props key value string clocksum)
  8898. (save-excursion
  8899. (when (condition-case nil (org-back-to-heading t) (error nil))
  8900. (setq beg (point))
  8901. (setq sum-props (get-text-property (point) 'org-summaries))
  8902. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8903. (outline-next-heading)
  8904. (setq end (point))
  8905. (when (memq which '(all special))
  8906. ;; Get the special properties, like TODO and tags
  8907. (goto-char beg)
  8908. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8909. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8910. (when (looking-at org-priority-regexp)
  8911. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8912. (when (and (setq value (org-get-tags-string))
  8913. (string-match "\\S-" value))
  8914. (push (cons "TAGS" value) props))
  8915. (when (setq value (org-get-tags-at))
  8916. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8917. props))
  8918. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8919. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8920. string (if (equal key clockstr)
  8921. (org-no-properties
  8922. (org-trim
  8923. (buffer-substring
  8924. (match-beginning 3) (goto-char (point-at-eol)))))
  8925. (substring (org-match-string-no-properties 3) 1 -1)))
  8926. (unless key
  8927. (if (= (char-after (match-beginning 3)) ?\[)
  8928. (setq key "TIMESTAMP_IA")
  8929. (setq key "TIMESTAMP")))
  8930. (when (or (equal key clockstr) (not (assoc key props)))
  8931. (push (cons key string) props)))
  8932. )
  8933. (when (memq which '(all standard))
  8934. ;; Get the standard properties, like :PORP: ...
  8935. (setq range (org-get-property-block beg end))
  8936. (when range
  8937. (goto-char (car range))
  8938. (while (re-search-forward
  8939. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8940. (cdr range) t)
  8941. (setq key (org-match-string-no-properties 1)
  8942. value (org-trim (or (org-match-string-no-properties 2) "")))
  8943. (unless (member key excluded)
  8944. (push (cons key (or value "")) props)))))
  8945. (if clocksum
  8946. (push (cons "CLOCKSUM"
  8947. (org-columns-number-to-string (/ (float clocksum) 60.)
  8948. 'add_times))
  8949. props))
  8950. (append sum-props (nreverse props)))))))
  8951. (defun org-entry-get (pom property &optional inherit)
  8952. "Get value of PROPERTY for entry at point-or-marker POM.
  8953. If INHERIT is non-nil and the entry does not have the property,
  8954. then also check higher levels of the hierarchy.
  8955. If INHERIT is the symbol `selective', use inheritance only if the setting
  8956. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8957. If the property is present but empty, the return value is the empty string.
  8958. If the property is not present at all, nil is returned."
  8959. (org-with-point-at pom
  8960. (if (and inherit (if (eq inherit 'selective)
  8961. (org-property-inherit-p property)
  8962. t))
  8963. (org-entry-get-with-inheritance property)
  8964. (if (member property org-special-properties)
  8965. ;; We need a special property. Use brute force, get all properties.
  8966. (cdr (assoc property (org-entry-properties nil 'special)))
  8967. (let ((range (org-get-property-block)))
  8968. (if (and range
  8969. (goto-char (car range))
  8970. (re-search-forward
  8971. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8972. (cdr range) t))
  8973. ;; Found the property, return it.
  8974. (if (match-end 1)
  8975. (org-match-string-no-properties 1)
  8976. "")))))))
  8977. (defun org-property-or-variable-value (var &optional inherit)
  8978. "Check if there is a property fixing the value of VAR.
  8979. If yes, return this value. If not, return the current value of the variable."
  8980. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8981. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8982. (read prop)
  8983. (symbol-value var))))
  8984. (defun org-entry-delete (pom property)
  8985. "Delete the property PROPERTY from entry at point-or-marker POM."
  8986. (org-with-point-at pom
  8987. (if (member property org-special-properties)
  8988. nil ; cannot delete these properties.
  8989. (let ((range (org-get-property-block)))
  8990. (if (and range
  8991. (goto-char (car range))
  8992. (re-search-forward
  8993. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8994. (cdr range) t))
  8995. (progn
  8996. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8997. t)
  8998. nil)))))
  8999. ;; Multi-values properties are properties that contain multiple values
  9000. ;; These values are assumed to be single words, separated by whitespace.
  9001. (defun org-entry-add-to-multivalued-property (pom property value)
  9002. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9003. (let* ((old (org-entry-get pom property))
  9004. (values (and old (org-split-string old "[ \t]"))))
  9005. (setq value (org-entry-protect-space value))
  9006. (unless (member value values)
  9007. (setq values (cons value values))
  9008. (org-entry-put pom property
  9009. (mapconcat 'identity values " ")))))
  9010. (defun org-entry-remove-from-multivalued-property (pom property value)
  9011. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9012. (let* ((old (org-entry-get pom property))
  9013. (values (and old (org-split-string old "[ \t]"))))
  9014. (setq value (org-entry-protect-space value))
  9015. (when (member value values)
  9016. (setq values (delete value values))
  9017. (org-entry-put pom property
  9018. (mapconcat 'identity values " ")))))
  9019. (defun org-entry-member-in-multivalued-property (pom property value)
  9020. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9021. (let* ((old (org-entry-get pom property))
  9022. (values (and old (org-split-string old "[ \t]"))))
  9023. (setq value (org-entry-protect-space value))
  9024. (member value values)))
  9025. (defun org-entry-get-multivalued-property (pom property)
  9026. "Return a list of values in a multivalued property."
  9027. (let* ((value (org-entry-get pom property))
  9028. (values (and value (org-split-string value "[ \t]"))))
  9029. (mapcar 'org-entry-restore-space values)))
  9030. (defun org-entry-put-multivalued-property (pom property &rest values)
  9031. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9032. VALUES should be a list of strings. Spaces will be protected."
  9033. (org-entry-put pom property
  9034. (mapconcat 'org-entry-protect-space values " "))
  9035. (let* ((value (org-entry-get pom property))
  9036. (values (and value (org-split-string value "[ \t]"))))
  9037. (mapcar 'org-entry-restore-space values)))
  9038. (defun org-entry-protect-space (s)
  9039. "Protect spaces and newline in string S."
  9040. (while (string-match " " s)
  9041. (setq s (replace-match "%20" t t s)))
  9042. (while (string-match "\n" s)
  9043. (setq s (replace-match "%0A" t t s)))
  9044. s)
  9045. (defun org-entry-restore-space (s)
  9046. "Restore spaces and newline in string S."
  9047. (while (string-match "%20" s)
  9048. (setq s (replace-match " " t t s)))
  9049. (while (string-match "%0A" s)
  9050. (setq s (replace-match "\n" t t s)))
  9051. s)
  9052. (defvar org-entry-property-inherited-from (make-marker)
  9053. "Marker pointing to the entry from where a proerty was inherited.
  9054. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9055. location of the entry where the inheriance search matched. If there was
  9056. no match, the marker will point nowhere.
  9057. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9058. is set.")
  9059. (defun org-entry-get-with-inheritance (property)
  9060. "Get entry property, and search higher levels if not present."
  9061. (move-marker org-entry-property-inherited-from nil)
  9062. (let (tmp)
  9063. (save-excursion
  9064. (save-restriction
  9065. (widen)
  9066. (catch 'ex
  9067. (while t
  9068. (when (setq tmp (org-entry-get nil property))
  9069. (org-back-to-heading t)
  9070. (move-marker org-entry-property-inherited-from (point))
  9071. (throw 'ex tmp))
  9072. (or (org-up-heading-safe) (throw 'ex nil)))))
  9073. (or tmp
  9074. (cdr (assoc property org-file-properties))
  9075. (cdr (assoc property org-global-properties))
  9076. (cdr (assoc property org-global-properties-fixed))))))
  9077. (defun org-entry-put (pom property value)
  9078. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9079. (org-with-point-at pom
  9080. (org-back-to-heading t)
  9081. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9082. range)
  9083. (cond
  9084. ((equal property "TODO")
  9085. (when (and (stringp value) (string-match "\\S-" value)
  9086. (not (member value org-todo-keywords-1)))
  9087. (error "\"%s\" is not a valid TODO state" value))
  9088. (if (or (not value)
  9089. (not (string-match "\\S-" value)))
  9090. (setq value 'none))
  9091. (org-todo value)
  9092. (org-set-tags nil 'align))
  9093. ((equal property "PRIORITY")
  9094. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9095. (string-to-char value) ?\ ))
  9096. (org-set-tags nil 'align))
  9097. ((equal property "SCHEDULED")
  9098. (if (re-search-forward org-scheduled-time-regexp end t)
  9099. (cond
  9100. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9101. ((eq value 'later) (org-timestamp-change 1 'day))
  9102. (t (call-interactively 'org-schedule)))
  9103. (call-interactively 'org-schedule)))
  9104. ((equal property "DEADLINE")
  9105. (if (re-search-forward org-deadline-time-regexp end t)
  9106. (cond
  9107. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9108. ((eq value 'later) (org-timestamp-change 1 'day))
  9109. (t (call-interactively 'org-deadline)))
  9110. (call-interactively 'org-deadline)))
  9111. ((member property org-special-properties)
  9112. (error "The %s property can not yet be set with `org-entry-put'"
  9113. property))
  9114. (t ; a non-special property
  9115. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9116. (setq range (org-get-property-block beg end 'force))
  9117. (goto-char (car range))
  9118. (if (re-search-forward
  9119. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9120. (progn
  9121. (delete-region (match-beginning 1) (match-end 1))
  9122. (goto-char (match-beginning 1)))
  9123. (goto-char (cdr range))
  9124. (insert "\n")
  9125. (backward-char 1)
  9126. (org-indent-line-function)
  9127. (insert ":" property ":"))
  9128. (and value (insert " " value))
  9129. (org-indent-line-function)))))))
  9130. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9131. "Get all property keys in the current buffer.
  9132. With INCLUDE-SPECIALS, also list the special properties that relect things
  9133. like tags and TODO state.
  9134. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9135. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9136. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9137. formats in the current buffer."
  9138. (let (rtn range cfmt cols s p)
  9139. (save-excursion
  9140. (save-restriction
  9141. (widen)
  9142. (goto-char (point-min))
  9143. (while (re-search-forward org-property-start-re nil t)
  9144. (setq range (org-get-property-block))
  9145. (goto-char (car range))
  9146. (while (re-search-forward
  9147. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9148. (cdr range) t)
  9149. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9150. (outline-next-heading))))
  9151. (when include-specials
  9152. (setq rtn (append org-special-properties rtn)))
  9153. (when include-defaults
  9154. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9155. (when include-columns
  9156. (save-excursion
  9157. (save-restriction
  9158. (widen)
  9159. (goto-char (point-min))
  9160. (while (re-search-forward
  9161. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9162. nil t)
  9163. (setq cfmt (match-string 2) s 0)
  9164. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9165. cfmt s)
  9166. (setq s (match-end 0)
  9167. p (match-string 1 cfmt))
  9168. (unless (or (equal p "ITEM")
  9169. (member p org-special-properties))
  9170. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9171. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9172. (defun org-property-values (key)
  9173. "Return a list of all values of property KEY."
  9174. (save-excursion
  9175. (save-restriction
  9176. (widen)
  9177. (goto-char (point-min))
  9178. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9179. values)
  9180. (while (re-search-forward re nil t)
  9181. (add-to-list 'values (org-trim (match-string 1))))
  9182. (delete "" values)))))
  9183. (defun org-insert-property-drawer ()
  9184. "Insert a property drawer into the current entry."
  9185. (interactive)
  9186. (org-back-to-heading t)
  9187. (looking-at outline-regexp)
  9188. (let ((indent (- (match-end 0)(match-beginning 0)))
  9189. (beg (point))
  9190. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9191. end hiddenp)
  9192. (outline-next-heading)
  9193. (setq end (point))
  9194. (goto-char beg)
  9195. (while (re-search-forward re end t))
  9196. (setq hiddenp (org-invisible-p))
  9197. (end-of-line 1)
  9198. (and (equal (char-after) ?\n) (forward-char 1))
  9199. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9200. (beginning-of-line 2))
  9201. (org-skip-over-state-notes)
  9202. (skip-chars-backward " \t\n\r")
  9203. (if (eq (char-before) ?*) (forward-char 1))
  9204. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9205. (beginning-of-line 0)
  9206. (org-indent-to-column indent)
  9207. (beginning-of-line 2)
  9208. (org-indent-to-column indent)
  9209. (beginning-of-line 0)
  9210. (if hiddenp
  9211. (save-excursion
  9212. (org-back-to-heading t)
  9213. (hide-entry))
  9214. (org-flag-drawer t))))
  9215. (defun org-set-property (property value)
  9216. "In the current entry, set PROPERTY to VALUE.
  9217. When called interactively, this will prompt for a property name, offering
  9218. completion on existing and default properties. And then it will prompt
  9219. for a value, offering competion either on allowed values (via an inherited
  9220. xxx_ALL property) or on existing values in other instances of this property
  9221. in the current file."
  9222. (interactive
  9223. (let* ((completion-ignore-case t)
  9224. (keys (org-buffer-property-keys nil t t))
  9225. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9226. (prop (if (member prop0 keys)
  9227. prop0
  9228. (or (cdr (assoc (downcase prop0)
  9229. (mapcar (lambda (x) (cons (downcase x) x))
  9230. keys)))
  9231. prop0)))
  9232. (cur (org-entry-get nil prop))
  9233. (allowed (org-property-get-allowed-values nil prop 'table))
  9234. (existing (mapcar 'list (org-property-values prop)))
  9235. (val (if allowed
  9236. (org-completing-read "Value: " allowed nil 'req-match)
  9237. (org-completing-read
  9238. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9239. (concat "[" cur "]") "")
  9240. ": ")
  9241. existing nil nil "" nil cur))))
  9242. (list prop (if (equal val "") cur val))))
  9243. (unless (equal (org-entry-get nil property) value)
  9244. (org-entry-put nil property value)))
  9245. (defun org-delete-property (property)
  9246. "In the current entry, delete PROPERTY."
  9247. (interactive
  9248. (let* ((completion-ignore-case t)
  9249. (prop (completing-read
  9250. "Property: " (org-entry-properties nil 'standard))))
  9251. (list prop)))
  9252. (message "Property %s %s" property
  9253. (if (org-entry-delete nil property)
  9254. "deleted"
  9255. "was not present in the entry")))
  9256. (defun org-delete-property-globally (property)
  9257. "Remove PROPERTY globally, from all entries."
  9258. (interactive
  9259. (let* ((completion-ignore-case t)
  9260. (prop (completing-read
  9261. "Globally remove property: "
  9262. (mapcar 'list (org-buffer-property-keys)))))
  9263. (list prop)))
  9264. (save-excursion
  9265. (save-restriction
  9266. (widen)
  9267. (goto-char (point-min))
  9268. (let ((cnt 0))
  9269. (while (re-search-forward
  9270. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9271. nil t)
  9272. (setq cnt (1+ cnt))
  9273. (replace-match ""))
  9274. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9275. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9276. (defun org-compute-property-at-point ()
  9277. "Compute the property at point.
  9278. This looks for an enclosing column format, extracts the operator and
  9279. then applies it to the proerty in the column format's scope."
  9280. (interactive)
  9281. (unless (org-at-property-p)
  9282. (error "Not at a property"))
  9283. (let ((prop (org-match-string-no-properties 2)))
  9284. (org-columns-get-format-and-top-level)
  9285. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9286. (error "No operator defined for property %s" prop))
  9287. (org-columns-compute prop)))
  9288. (defun org-property-get-allowed-values (pom property &optional table)
  9289. "Get allowed values for the property PROPERTY.
  9290. When TABLE is non-nil, return an alist that can directly be used for
  9291. completion."
  9292. (let (vals)
  9293. (cond
  9294. ((equal property "TODO")
  9295. (setq vals (org-with-point-at pom
  9296. (append org-todo-keywords-1 '("")))))
  9297. ((equal property "PRIORITY")
  9298. (let ((n org-lowest-priority))
  9299. (while (>= n org-highest-priority)
  9300. (push (char-to-string n) vals)
  9301. (setq n (1- n)))))
  9302. ((member property org-special-properties))
  9303. (t
  9304. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9305. (when (and vals (string-match "\\S-" vals))
  9306. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9307. (setq vals (mapcar (lambda (x)
  9308. (cond ((stringp x) x)
  9309. ((numberp x) (number-to-string x))
  9310. ((symbolp x) (symbol-name x))
  9311. (t "???")))
  9312. vals)))))
  9313. (if table (mapcar 'list vals) vals)))
  9314. (defun org-property-previous-allowed-value (&optional previous)
  9315. "Switch to the next allowed value for this property."
  9316. (interactive)
  9317. (org-property-next-allowed-value t))
  9318. (defun org-property-next-allowed-value (&optional previous)
  9319. "Switch to the next allowed value for this property."
  9320. (interactive)
  9321. (unless (org-at-property-p)
  9322. (error "Not at a property"))
  9323. (let* ((key (match-string 2))
  9324. (value (match-string 3))
  9325. (allowed (or (org-property-get-allowed-values (point) key)
  9326. (and (member value '("[ ]" "[-]" "[X]"))
  9327. '("[ ]" "[X]"))))
  9328. nval)
  9329. (unless allowed
  9330. (error "Allowed values for this property have not been defined"))
  9331. (if previous (setq allowed (reverse allowed)))
  9332. (if (member value allowed)
  9333. (setq nval (car (cdr (member value allowed)))))
  9334. (setq nval (or nval (car allowed)))
  9335. (if (equal nval value)
  9336. (error "Only one allowed value for this property"))
  9337. (org-at-property-p)
  9338. (replace-match (concat " :" key ": " nval) t t)
  9339. (org-indent-line-function)
  9340. (beginning-of-line 1)
  9341. (skip-chars-forward " \t")))
  9342. (defun org-find-entry-with-id (ident)
  9343. "Locate the entry that contains the ID property with exact value IDENT.
  9344. IDENT can be a string, a symbol or a number, this function will search for
  9345. the string representation of it.
  9346. Return the position where this entry starts, or nil if there is no such entry."
  9347. (let ((id (cond
  9348. ((stringp ident) ident)
  9349. ((symbol-name ident) (symbol-name ident))
  9350. ((numberp ident) (number-to-string ident))
  9351. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9352. (case-fold-search nil))
  9353. (save-excursion
  9354. (save-restriction
  9355. (widen)
  9356. (goto-char (point-min))
  9357. (when (re-search-forward
  9358. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9359. nil t)
  9360. (org-back-to-heading)
  9361. (point))))))
  9362. ;;;; Timestamps
  9363. (defvar org-last-changed-timestamp nil)
  9364. (defvar org-last-inserted-timestamp nil
  9365. "The last time stamp inserted with `org-insert-time-stamp'.")
  9366. (defvar org-time-was-given) ; dynamically scoped parameter
  9367. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9368. (defvar org-ts-what) ; dynamically scoped parameter
  9369. (defun org-time-stamp (arg &optional inactive)
  9370. "Prompt for a date/time and insert a time stamp.
  9371. If the user specifies a time like HH:MM, or if this command is called
  9372. with a prefix argument, the time stamp will contain date and time.
  9373. Otherwise, only the date will be included. All parts of a date not
  9374. specified by the user will be filled in from the current date/time.
  9375. So if you press just return without typing anything, the time stamp
  9376. will represent the current date/time. If there is already a timestamp
  9377. at the cursor, it will be modified."
  9378. (interactive "P")
  9379. (let* ((ts nil)
  9380. (default-time
  9381. ;; Default time is either today, or, when entering a range,
  9382. ;; the range start.
  9383. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9384. (save-excursion
  9385. (re-search-backward
  9386. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9387. (- (point) 20) t)))
  9388. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9389. (current-time)))
  9390. (default-input (and ts (org-get-compact-tod ts)))
  9391. org-time-was-given org-end-time-was-given time)
  9392. (cond
  9393. ((and (org-at-timestamp-p t)
  9394. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9395. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9396. (insert "--")
  9397. (setq time (let ((this-command this-command))
  9398. (org-read-date arg 'totime nil nil
  9399. default-time default-input)))
  9400. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9401. ((org-at-timestamp-p t)
  9402. (setq time (let ((this-command this-command))
  9403. (org-read-date arg 'totime nil nil default-time default-input)))
  9404. (when (org-at-timestamp-p t) ; just to get the match data
  9405. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9406. (replace-match "")
  9407. (setq org-last-changed-timestamp
  9408. (org-insert-time-stamp
  9409. time (or org-time-was-given arg)
  9410. inactive nil nil (list org-end-time-was-given))))
  9411. (message "Timestamp updated"))
  9412. (t
  9413. (setq time (let ((this-command this-command))
  9414. (org-read-date arg 'totime nil nil default-time default-input)))
  9415. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9416. nil nil (list org-end-time-was-given))))))
  9417. ;; FIXME: can we use this for something else, like computing time differences?
  9418. (defun org-get-compact-tod (s)
  9419. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9420. (let* ((t1 (match-string 1 s))
  9421. (h1 (string-to-number (match-string 2 s)))
  9422. (m1 (string-to-number (match-string 3 s)))
  9423. (t2 (and (match-end 4) (match-string 5 s)))
  9424. (h2 (and t2 (string-to-number (match-string 6 s))))
  9425. (m2 (and t2 (string-to-number (match-string 7 s))))
  9426. dh dm)
  9427. (if (not t2)
  9428. t1
  9429. (setq dh (- h2 h1) dm (- m2 m1))
  9430. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9431. (concat t1 "+" (number-to-string dh)
  9432. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9433. (defun org-time-stamp-inactive (&optional arg)
  9434. "Insert an inactive time stamp.
  9435. An inactive time stamp is enclosed in square brackets instead of angle
  9436. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9437. does not link to the calendar and cannot be changed with the S-cursor keys.
  9438. So these are more for recording a certain time/date."
  9439. (interactive "P")
  9440. (org-time-stamp arg 'inactive))
  9441. (defvar org-date-ovl (org-make-overlay 1 1))
  9442. (org-overlay-put org-date-ovl 'face 'org-warning)
  9443. (org-detach-overlay org-date-ovl)
  9444. (defvar org-ans1) ; dynamically scoped parameter
  9445. (defvar org-ans2) ; dynamically scoped parameter
  9446. (defvar org-plain-time-of-day-regexp) ; defined below
  9447. (defvar org-overriding-default-time nil) ; dynamically scoped
  9448. (defvar org-read-date-overlay nil)
  9449. (defvar org-dcst nil) ; dynamically scoped
  9450. (defun org-read-date (&optional with-time to-time from-string prompt
  9451. default-time default-input)
  9452. "Read a date, possibly a time, and make things smooth for the user.
  9453. The prompt will suggest to enter an ISO date, but you can also enter anything
  9454. which will at least partially be understood by `parse-time-string'.
  9455. Unrecognized parts of the date will default to the current day, month, year,
  9456. hour and minute. If this command is called to replace a timestamp at point,
  9457. of to enter the second timestamp of a range, the default time is taken from the
  9458. existing stamp. For example,
  9459. 3-2-5 --> 2003-02-05
  9460. feb 15 --> currentyear-02-15
  9461. sep 12 9 --> 2009-09-12
  9462. 12:45 --> today 12:45
  9463. 22 sept 0:34 --> currentyear-09-22 0:34
  9464. 12 --> currentyear-currentmonth-12
  9465. Fri --> nearest Friday (today or later)
  9466. etc.
  9467. Furthermore you can specify a relative date by giving, as the *first* thing
  9468. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9469. change in days weeks, months, years.
  9470. With a single plus or minus, the date is relative to today. With a double
  9471. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9472. +4d --> four days from today
  9473. +4 --> same as above
  9474. +2w --> two weeks from today
  9475. ++5 --> five days from default date
  9476. The function understands only English month and weekday abbreviations,
  9477. but this can be configured with the variables `parse-time-months' and
  9478. `parse-time-weekdays'.
  9479. While prompting, a calendar is popped up - you can also select the
  9480. date with the mouse (button 1). The calendar shows a period of three
  9481. months. To scroll it to other months, use the keys `>' and `<'.
  9482. If you don't like the calendar, turn it off with
  9483. \(setq org-read-date-popup-calendar nil)
  9484. With optional argument TO-TIME, the date will immediately be converted
  9485. to an internal time.
  9486. With an optional argument WITH-TIME, the prompt will suggest to also
  9487. insert a time. Note that when WITH-TIME is not set, you can still
  9488. enter a time, and this function will inform the calling routine about
  9489. this change. The calling routine may then choose to change the format
  9490. used to insert the time stamp into the buffer to include the time.
  9491. With optional argument FROM-STRING, read from this string instead from
  9492. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9493. the time/date that is used for everything that is not specified by the
  9494. user."
  9495. (require 'parse-time)
  9496. (let* ((org-time-stamp-rounding-minutes
  9497. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9498. (org-dcst org-display-custom-times)
  9499. (ct (org-current-time))
  9500. (def (or org-overriding-default-time default-time ct))
  9501. (defdecode (decode-time def))
  9502. (dummy (progn
  9503. (when (< (nth 2 defdecode) org-extend-today-until)
  9504. (setcar (nthcdr 2 defdecode) -1)
  9505. (setcar (nthcdr 1 defdecode) 59)
  9506. (setq def (apply 'encode-time defdecode)
  9507. defdecode (decode-time def)))))
  9508. (calendar-move-hook nil)
  9509. (calendar-view-diary-initially-flag nil)
  9510. (view-diary-entries-initially nil)
  9511. (calendar-view-holidays-initially-flag nil)
  9512. (view-calendar-holidays-initially nil)
  9513. (timestr (format-time-string
  9514. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9515. (prompt (concat (if prompt (concat prompt " ") "")
  9516. (format "Date+time [%s]: " timestr)))
  9517. ans (org-ans0 "") org-ans1 org-ans2 final)
  9518. (cond
  9519. (from-string (setq ans from-string))
  9520. (org-read-date-popup-calendar
  9521. (save-excursion
  9522. (save-window-excursion
  9523. (calendar)
  9524. (calendar-forward-day (- (time-to-days def)
  9525. (calendar-absolute-from-gregorian
  9526. (calendar-current-date))))
  9527. (org-eval-in-calendar nil t)
  9528. (let* ((old-map (current-local-map))
  9529. (map (copy-keymap calendar-mode-map))
  9530. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9531. (org-defkey map (kbd "RET") 'org-calendar-select)
  9532. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9533. 'org-calendar-select-mouse)
  9534. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9535. 'org-calendar-select-mouse)
  9536. (org-defkey minibuffer-local-map [(meta shift left)]
  9537. (lambda () (interactive)
  9538. (org-eval-in-calendar '(calendar-backward-month 1))))
  9539. (org-defkey minibuffer-local-map [(meta shift right)]
  9540. (lambda () (interactive)
  9541. (org-eval-in-calendar '(calendar-forward-month 1))))
  9542. (org-defkey minibuffer-local-map [(meta shift up)]
  9543. (lambda () (interactive)
  9544. (org-eval-in-calendar '(calendar-backward-year 1))))
  9545. (org-defkey minibuffer-local-map [(meta shift down)]
  9546. (lambda () (interactive)
  9547. (org-eval-in-calendar '(calendar-forward-year 1))))
  9548. (org-defkey minibuffer-local-map [(shift up)]
  9549. (lambda () (interactive)
  9550. (org-eval-in-calendar '(calendar-backward-week 1))))
  9551. (org-defkey minibuffer-local-map [(shift down)]
  9552. (lambda () (interactive)
  9553. (org-eval-in-calendar '(calendar-forward-week 1))))
  9554. (org-defkey minibuffer-local-map [(shift left)]
  9555. (lambda () (interactive)
  9556. (org-eval-in-calendar '(calendar-backward-day 1))))
  9557. (org-defkey minibuffer-local-map [(shift right)]
  9558. (lambda () (interactive)
  9559. (org-eval-in-calendar '(calendar-forward-day 1))))
  9560. (org-defkey minibuffer-local-map ">"
  9561. (lambda () (interactive)
  9562. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9563. (org-defkey minibuffer-local-map "<"
  9564. (lambda () (interactive)
  9565. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9566. (unwind-protect
  9567. (progn
  9568. (use-local-map map)
  9569. (add-hook 'post-command-hook 'org-read-date-display)
  9570. (setq org-ans0 (read-string prompt default-input nil nil))
  9571. ;; org-ans0: from prompt
  9572. ;; org-ans1: from mouse click
  9573. ;; org-ans2: from calendar motion
  9574. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9575. (remove-hook 'post-command-hook 'org-read-date-display)
  9576. (use-local-map old-map)
  9577. (when org-read-date-overlay
  9578. (org-delete-overlay org-read-date-overlay)
  9579. (setq org-read-date-overlay nil)))))))
  9580. (t ; Naked prompt only
  9581. (unwind-protect
  9582. (setq ans (read-string prompt default-input nil timestr))
  9583. (when org-read-date-overlay
  9584. (org-delete-overlay org-read-date-overlay)
  9585. (setq org-read-date-overlay nil)))))
  9586. (setq final (org-read-date-analyze ans def defdecode))
  9587. (if to-time
  9588. (apply 'encode-time final)
  9589. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9590. (format "%04d-%02d-%02d %02d:%02d"
  9591. (nth 5 final) (nth 4 final) (nth 3 final)
  9592. (nth 2 final) (nth 1 final))
  9593. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9594. (defvar def)
  9595. (defvar defdecode)
  9596. (defvar with-time)
  9597. (defun org-read-date-display ()
  9598. "Display the currrent date prompt interpretation in the minibuffer."
  9599. (when org-read-date-display-live
  9600. (when org-read-date-overlay
  9601. (org-delete-overlay org-read-date-overlay))
  9602. (let ((p (point)))
  9603. (end-of-line 1)
  9604. (while (not (equal (buffer-substring
  9605. (max (point-min) (- (point) 4)) (point))
  9606. " "))
  9607. (insert " "))
  9608. (goto-char p))
  9609. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9610. " " (or org-ans1 org-ans2)))
  9611. (org-end-time-was-given nil)
  9612. (f (org-read-date-analyze ans def defdecode))
  9613. (fmts (if org-dcst
  9614. org-time-stamp-custom-formats
  9615. org-time-stamp-formats))
  9616. (fmt (if (or with-time
  9617. (and (boundp 'org-time-was-given) org-time-was-given))
  9618. (cdr fmts)
  9619. (car fmts)))
  9620. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9621. (when (and org-end-time-was-given
  9622. (string-match org-plain-time-of-day-regexp txt))
  9623. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9624. org-end-time-was-given
  9625. (substring txt (match-end 0)))))
  9626. (setq org-read-date-overlay
  9627. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9628. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9629. (defun org-read-date-analyze (ans def defdecode)
  9630. "Analyze the combined answer of the date prompt."
  9631. ;; FIXME: cleanup and comment
  9632. (let (delta deltan deltaw deltadef year month day
  9633. hour minute second wday pm h2 m2 tl wday1
  9634. iso-year iso-weekday iso-week iso-year iso-date)
  9635. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9636. (setq ans "+0"))
  9637. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9638. (setq ans (replace-match "" t t ans)
  9639. deltan (car delta)
  9640. deltaw (nth 1 delta)
  9641. deltadef (nth 2 delta)))
  9642. ;; Check if there is an iso week date in there
  9643. ;; If yes, sore the info and ostpone interpreting it until the rest
  9644. ;; of the parsing is done
  9645. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9646. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9647. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9648. iso-week (string-to-number (match-string 2 ans)))
  9649. (setq ans (replace-match "" t t ans)))
  9650. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9651. (when (string-match
  9652. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9653. (setq year (if (match-end 2)
  9654. (string-to-number (match-string 2 ans))
  9655. (string-to-number (format-time-string "%Y")))
  9656. month (string-to-number (match-string 3 ans))
  9657. day (string-to-number (match-string 4 ans)))
  9658. (if (< year 100) (setq year (+ 2000 year)))
  9659. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9660. t nil ans)))
  9661. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9662. ;; If there is a time with am/pm, and *no* time without it, we convert
  9663. ;; so that matching will be successful.
  9664. (loop for i from 1 to 2 do ; twice, for end time as well
  9665. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9666. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9667. (setq hour (string-to-number (match-string 1 ans))
  9668. minute (if (match-end 3)
  9669. (string-to-number (match-string 3 ans))
  9670. 0)
  9671. pm (equal ?p
  9672. (string-to-char (downcase (match-string 4 ans)))))
  9673. (if (and (= hour 12) (not pm))
  9674. (setq hour 0)
  9675. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9676. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9677. t t ans))))
  9678. ;; Check if a time range is given as a duration
  9679. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9680. (setq hour (string-to-number (match-string 1 ans))
  9681. h2 (+ hour (string-to-number (match-string 3 ans)))
  9682. minute (string-to-number (match-string 2 ans))
  9683. m2 (+ minute (if (match-end 5) (string-to-number
  9684. (match-string 5 ans))0)))
  9685. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9686. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9687. t t ans)))
  9688. ;; Check if there is a time range
  9689. (when (boundp 'org-end-time-was-given)
  9690. (setq org-time-was-given nil)
  9691. (when (and (string-match org-plain-time-of-day-regexp ans)
  9692. (match-end 8))
  9693. (setq org-end-time-was-given (match-string 8 ans))
  9694. (setq ans (concat (substring ans 0 (match-beginning 7))
  9695. (substring ans (match-end 7))))))
  9696. (setq tl (parse-time-string ans)
  9697. day (or (nth 3 tl) (nth 3 defdecode))
  9698. month (or (nth 4 tl)
  9699. (if (and org-read-date-prefer-future
  9700. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9701. (1+ (nth 4 defdecode))
  9702. (nth 4 defdecode)))
  9703. year (or (nth 5 tl)
  9704. (if (and org-read-date-prefer-future
  9705. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9706. (1+ (nth 5 defdecode))
  9707. (nth 5 defdecode)))
  9708. hour (or (nth 2 tl) (nth 2 defdecode))
  9709. minute (or (nth 1 tl) (nth 1 defdecode))
  9710. second (or (nth 0 tl) 0)
  9711. wday (nth 6 tl))
  9712. ;; Special date definitions below
  9713. (cond
  9714. (iso-week
  9715. ;; There was an iso week
  9716. (setq year (or iso-year year)
  9717. day (or iso-weekday wday 1)
  9718. wday nil ; to make sure that the trigger below does not match
  9719. iso-date (calendar-gregorian-from-absolute
  9720. (calendar-absolute-from-iso
  9721. (list iso-week day year))))
  9722. ; FIXME: Should we also push ISO weeks into the future?
  9723. ; (when (and org-read-date-prefer-future
  9724. ; (not iso-year)
  9725. ; (< (calendar-absolute-from-gregorian iso-date)
  9726. ; (time-to-days (current-time))))
  9727. ; (setq year (1+ year)
  9728. ; iso-date (calendar-gregorian-from-absolute
  9729. ; (calendar-absolute-from-iso
  9730. ; (list iso-week day year)))))
  9731. (setq month (car iso-date)
  9732. year (nth 2 iso-date)
  9733. day (nth 1 iso-date)))
  9734. (deltan
  9735. (unless deltadef
  9736. (let ((now (decode-time (current-time))))
  9737. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9738. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9739. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9740. ((equal deltaw "m") (setq month (+ month deltan)))
  9741. ((equal deltaw "y") (setq year (+ year deltan)))))
  9742. ((and wday (not (nth 3 tl)))
  9743. ;; Weekday was given, but no day, so pick that day in the week
  9744. ;; on or after the derived date.
  9745. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9746. (unless (equal wday wday1)
  9747. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9748. (if (and (boundp 'org-time-was-given)
  9749. (nth 2 tl))
  9750. (setq org-time-was-given t))
  9751. (if (< year 100) (setq year (+ 2000 year)))
  9752. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9753. (list second minute hour day month year)))
  9754. (defvar parse-time-weekdays)
  9755. (defun org-read-date-get-relative (s today default)
  9756. "Check string S for special relative date string.
  9757. TODAY and DEFAULT are internal times, for today and for a default.
  9758. Return shift list (N what def-flag)
  9759. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9760. N is the number of WHATs to shift.
  9761. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9762. the DEFAULT date rather than TODAY."
  9763. (when (and
  9764. (string-match
  9765. (concat
  9766. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9767. "\\([0-9]+\\)?"
  9768. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9769. "\\([ \t]\\|$\\)") s)
  9770. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9771. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9772. (string-to-char (substring (match-string 1 s) -1))
  9773. ?+))
  9774. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9775. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9776. (what (if (match-end 3) (match-string 3 s) "d"))
  9777. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9778. (date (if rel default today))
  9779. (wday (nth 6 (decode-time date)))
  9780. delta)
  9781. (if wday1
  9782. (progn
  9783. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9784. (if (= dir ?-) (setq delta (- delta 7)))
  9785. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9786. (list delta "d" rel))
  9787. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9788. (defun org-eval-in-calendar (form &optional keepdate)
  9789. "Eval FORM in the calendar window and return to current window.
  9790. Also, store the cursor date in variable org-ans2."
  9791. (let ((sw (selected-window)))
  9792. (select-window (get-buffer-window "*Calendar*"))
  9793. (eval form)
  9794. (when (and (not keepdate) (calendar-cursor-to-date))
  9795. (let* ((date (calendar-cursor-to-date))
  9796. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9797. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9798. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9799. (select-window sw)))
  9800. (defun org-calendar-select ()
  9801. "Return to `org-read-date' with the date currently selected.
  9802. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9803. (interactive)
  9804. (when (calendar-cursor-to-date)
  9805. (let* ((date (calendar-cursor-to-date))
  9806. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9807. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9808. (if (active-minibuffer-window) (exit-minibuffer))))
  9809. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9810. "Insert a date stamp for the date given by the internal TIME.
  9811. WITH-HM means, use the stamp format that includes the time of the day.
  9812. INACTIVE means use square brackets instead of angular ones, so that the
  9813. stamp will not contribute to the agenda.
  9814. PRE and POST are optional strings to be inserted before and after the
  9815. stamp.
  9816. The command returns the inserted time stamp."
  9817. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9818. stamp)
  9819. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9820. (insert-before-markers (or pre ""))
  9821. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9822. (when (listp extra)
  9823. (setq extra (car extra))
  9824. (if (and (stringp extra)
  9825. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9826. (setq extra (format "-%02d:%02d"
  9827. (string-to-number (match-string 1 extra))
  9828. (string-to-number (match-string 2 extra))))
  9829. (setq extra nil)))
  9830. (when extra
  9831. (backward-char 1)
  9832. (insert-before-markers extra)
  9833. (forward-char 1))
  9834. (insert-before-markers (or post ""))
  9835. (setq org-last-inserted-timestamp stamp)))
  9836. (defun org-toggle-time-stamp-overlays ()
  9837. "Toggle the use of custom time stamp formats."
  9838. (interactive)
  9839. (setq org-display-custom-times (not org-display-custom-times))
  9840. (unless org-display-custom-times
  9841. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9842. (while (setq p (next-single-property-change p 'display))
  9843. (if (and (get-text-property p 'display)
  9844. (eq (get-text-property p 'face) 'org-date))
  9845. (remove-text-properties
  9846. p (setq p (next-single-property-change p 'display))
  9847. '(display t))))
  9848. (set-buffer-modified-p bmp)))
  9849. (if (featurep 'xemacs)
  9850. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9851. (org-restart-font-lock)
  9852. (setq org-table-may-need-update t)
  9853. (if org-display-custom-times
  9854. (message "Time stamps are overlayed with custom format")
  9855. (message "Time stamp overlays removed")))
  9856. (defun org-display-custom-time (beg end)
  9857. "Overlay modified time stamp format over timestamp between BEG and END."
  9858. (let* ((ts (buffer-substring beg end))
  9859. t1 w1 with-hm tf time str w2 (off 0))
  9860. (save-match-data
  9861. (setq t1 (org-parse-time-string ts t))
  9862. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9863. (setq off (- (match-end 0) (match-beginning 0)))))
  9864. (setq end (- end off))
  9865. (setq w1 (- end beg)
  9866. with-hm (and (nth 1 t1) (nth 2 t1))
  9867. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9868. time (org-fix-decoded-time t1)
  9869. str (org-add-props
  9870. (format-time-string
  9871. (substring tf 1 -1) (apply 'encode-time time))
  9872. nil 'mouse-face 'highlight)
  9873. w2 (length str))
  9874. (if (not (= w2 w1))
  9875. (add-text-properties (1+ beg) (+ 2 beg)
  9876. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9877. (if (featurep 'xemacs)
  9878. (progn
  9879. (put-text-property beg end 'invisible t)
  9880. (put-text-property beg end 'end-glyph (make-glyph str)))
  9881. (put-text-property beg end 'display str))))
  9882. (defun org-translate-time (string)
  9883. "Translate all timestamps in STRING to custom format.
  9884. But do this only if the variable `org-display-custom-times' is set."
  9885. (when org-display-custom-times
  9886. (save-match-data
  9887. (let* ((start 0)
  9888. (re org-ts-regexp-both)
  9889. t1 with-hm inactive tf time str beg end)
  9890. (while (setq start (string-match re string start))
  9891. (setq beg (match-beginning 0)
  9892. end (match-end 0)
  9893. t1 (save-match-data
  9894. (org-parse-time-string (substring string beg end) t))
  9895. with-hm (and (nth 1 t1) (nth 2 t1))
  9896. inactive (equal (substring string beg (1+ beg)) "[")
  9897. tf (funcall (if with-hm 'cdr 'car)
  9898. org-time-stamp-custom-formats)
  9899. time (org-fix-decoded-time t1)
  9900. str (format-time-string
  9901. (concat
  9902. (if inactive "[" "<") (substring tf 1 -1)
  9903. (if inactive "]" ">"))
  9904. (apply 'encode-time time))
  9905. string (replace-match str t t string)
  9906. start (+ start (length str)))))))
  9907. string)
  9908. (defun org-fix-decoded-time (time)
  9909. "Set 0 instead of nil for the first 6 elements of time.
  9910. Don't touch the rest."
  9911. (let ((n 0))
  9912. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9913. (defun org-days-to-time (timestamp-string)
  9914. "Difference between TIMESTAMP-STRING and now in days."
  9915. (- (time-to-days (org-time-string-to-time timestamp-string))
  9916. (time-to-days (current-time))))
  9917. (defun org-deadline-close (timestamp-string &optional ndays)
  9918. "Is the time in TIMESTAMP-STRING close to the current date?"
  9919. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9920. (and (< (org-days-to-time timestamp-string) ndays)
  9921. (not (org-entry-is-done-p))))
  9922. (defun org-get-wdays (ts)
  9923. "Get the deadline lead time appropriate for timestring TS."
  9924. (cond
  9925. ((<= org-deadline-warning-days 0)
  9926. ;; 0 or negative, enforce this value no matter what
  9927. (- org-deadline-warning-days))
  9928. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9929. ;; lead time is specified.
  9930. (floor (* (string-to-number (match-string 1 ts))
  9931. (cdr (assoc (match-string 2 ts)
  9932. '(("d" . 1) ("w" . 7)
  9933. ("m" . 30.4) ("y" . 365.25)))))))
  9934. ;; go for the default.
  9935. (t org-deadline-warning-days)))
  9936. (defun org-calendar-select-mouse (ev)
  9937. "Return to `org-read-date' with the date currently selected.
  9938. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9939. (interactive "e")
  9940. (mouse-set-point ev)
  9941. (when (calendar-cursor-to-date)
  9942. (let* ((date (calendar-cursor-to-date))
  9943. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9944. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9945. (if (active-minibuffer-window) (exit-minibuffer))))
  9946. (defun org-check-deadlines (ndays)
  9947. "Check if there are any deadlines due or past due.
  9948. A deadline is considered due if it happens within `org-deadline-warning-days'
  9949. days from today's date. If the deadline appears in an entry marked DONE,
  9950. it is not shown. The prefix arg NDAYS can be used to test that many
  9951. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9952. (interactive "P")
  9953. (let* ((org-warn-days
  9954. (cond
  9955. ((equal ndays '(4)) 100000)
  9956. (ndays (prefix-numeric-value ndays))
  9957. (t (abs org-deadline-warning-days))))
  9958. (case-fold-search nil)
  9959. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9960. (callback
  9961. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9962. (message "%d deadlines past-due or due within %d days"
  9963. (org-occur regexp nil callback)
  9964. org-warn-days)))
  9965. (defun org-check-before-date (date)
  9966. "Check if there are deadlines or scheduled entries before DATE."
  9967. (interactive (list (org-read-date)))
  9968. (let ((case-fold-search nil)
  9969. (regexp (concat "\\<\\(" org-deadline-string
  9970. "\\|" org-scheduled-string
  9971. "\\) *<\\([^>]+\\)>"))
  9972. (callback
  9973. (lambda () (time-less-p
  9974. (org-time-string-to-time (match-string 2))
  9975. (org-time-string-to-time date)))))
  9976. (message "%d entries before %s"
  9977. (org-occur regexp nil callback) date)))
  9978. (defun org-evaluate-time-range (&optional to-buffer)
  9979. "Evaluate a time range by computing the difference between start and end.
  9980. Normally the result is just printed in the echo area, but with prefix arg
  9981. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9982. If the time range is actually in a table, the result is inserted into the
  9983. next column.
  9984. For time difference computation, a year is assumed to be exactly 365
  9985. days in order to avoid rounding problems."
  9986. (interactive "P")
  9987. (or
  9988. (org-clock-update-time-maybe)
  9989. (save-excursion
  9990. (unless (org-at-date-range-p t)
  9991. (goto-char (point-at-bol))
  9992. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9993. (if (not (org-at-date-range-p t))
  9994. (error "Not at a time-stamp range, and none found in current line")))
  9995. (let* ((ts1 (match-string 1))
  9996. (ts2 (match-string 2))
  9997. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9998. (match-end (match-end 0))
  9999. (time1 (org-time-string-to-time ts1))
  10000. (time2 (org-time-string-to-time ts2))
  10001. (t1 (time-to-seconds time1))
  10002. (t2 (time-to-seconds time2))
  10003. (diff (abs (- t2 t1)))
  10004. (negative (< (- t2 t1) 0))
  10005. ;; (ys (floor (* 365 24 60 60)))
  10006. (ds (* 24 60 60))
  10007. (hs (* 60 60))
  10008. (fy "%dy %dd %02d:%02d")
  10009. (fy1 "%dy %dd")
  10010. (fd "%dd %02d:%02d")
  10011. (fd1 "%dd")
  10012. (fh "%02d:%02d")
  10013. y d h m align)
  10014. (if havetime
  10015. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10016. y 0
  10017. d (floor (/ diff ds)) diff (mod diff ds)
  10018. h (floor (/ diff hs)) diff (mod diff hs)
  10019. m (floor (/ diff 60)))
  10020. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10021. y 0
  10022. d (floor (+ (/ diff ds) 0.5))
  10023. h 0 m 0))
  10024. (if (not to-buffer)
  10025. (message "%s" (org-make-tdiff-string y d h m))
  10026. (if (org-at-table-p)
  10027. (progn
  10028. (goto-char match-end)
  10029. (setq align t)
  10030. (and (looking-at " *|") (goto-char (match-end 0))))
  10031. (goto-char match-end))
  10032. (if (looking-at
  10033. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10034. (replace-match ""))
  10035. (if negative (insert " -"))
  10036. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10037. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10038. (insert " " (format fh h m))))
  10039. (if align (org-table-align))
  10040. (message "Time difference inserted")))))
  10041. (defun org-make-tdiff-string (y d h m)
  10042. (let ((fmt "")
  10043. (l nil))
  10044. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10045. l (push y l)))
  10046. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10047. l (push d l)))
  10048. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10049. l (push h l)))
  10050. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10051. l (push m l)))
  10052. (apply 'format fmt (nreverse l))))
  10053. (defun org-time-string-to-time (s)
  10054. (apply 'encode-time (org-parse-time-string s)))
  10055. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10056. "Convert a time stamp to an absolute day number.
  10057. If there is a specifyer for a cyclic time stamp, get the closest date to
  10058. DAYNR.
  10059. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10060. (cond
  10061. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10062. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10063. daynr
  10064. (+ daynr 1000)))
  10065. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10066. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10067. (time-to-days (current-time))) (match-string 0 s)
  10068. prefer show-all))
  10069. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10070. (defun org-days-to-iso-week (days)
  10071. "Return the iso week number."
  10072. (require 'cal-iso)
  10073. (car (calendar-iso-from-absolute days)))
  10074. (defun org-small-year-to-year (year)
  10075. "Convert 2-digit years into 4-digit years.
  10076. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10077. The year 2000 cannot be abbreviated. Any year lager than 99
  10078. is retrned unchanged."
  10079. (if (< year 38)
  10080. (setq year (+ 2000 year))
  10081. (if (< year 100)
  10082. (setq year (+ 1900 year))))
  10083. year)
  10084. (defun org-time-from-absolute (d)
  10085. "Return the time corresponding to date D.
  10086. D may be an absolute day number, or a calendar-type list (month day year)."
  10087. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10088. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10089. (defun org-calendar-holiday ()
  10090. "List of holidays, for Diary display in Org-mode."
  10091. (require 'holidays)
  10092. (let ((hl (funcall
  10093. (if (fboundp 'calendar-check-holidays)
  10094. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10095. (if hl (mapconcat 'identity hl "; "))))
  10096. (defun org-diary-sexp-entry (sexp entry date)
  10097. "Process a SEXP diary ENTRY for DATE."
  10098. (require 'diary-lib)
  10099. (let ((result (if calendar-debug-sexp
  10100. (let ((stack-trace-on-error t))
  10101. (eval (car (read-from-string sexp))))
  10102. (condition-case nil
  10103. (eval (car (read-from-string sexp)))
  10104. (error
  10105. (beep)
  10106. (message "Bad sexp at line %d in %s: %s"
  10107. (org-current-line)
  10108. (buffer-file-name) sexp)
  10109. (sleep-for 2))))))
  10110. (cond ((stringp result) result)
  10111. ((and (consp result)
  10112. (stringp (cdr result))) (cdr result))
  10113. (result entry)
  10114. (t nil))))
  10115. (defun org-diary-to-ical-string (frombuf)
  10116. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10117. This uses the icalendar.el library."
  10118. (let* ((tmpdir (if (featurep 'xemacs)
  10119. (temp-directory)
  10120. temporary-file-directory))
  10121. (tmpfile (make-temp-name
  10122. (expand-file-name "orgics" tmpdir)))
  10123. buf rtn b e)
  10124. (save-excursion
  10125. (set-buffer frombuf)
  10126. (icalendar-export-region (point-min) (point-max) tmpfile)
  10127. (setq buf (find-buffer-visiting tmpfile))
  10128. (set-buffer buf)
  10129. (goto-char (point-min))
  10130. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10131. (setq b (match-beginning 0)))
  10132. (goto-char (point-max))
  10133. (if (re-search-backward "^END:VEVENT" nil t)
  10134. (setq e (match-end 0)))
  10135. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10136. (kill-buffer buf)
  10137. (delete-file tmpfile)
  10138. rtn))
  10139. (defun org-closest-date (start current change prefer show-all)
  10140. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10141. When PREFER is `past' return a date that is either CURRENT or past.
  10142. When PREFER is `future', return a date that is either CURRENT or future.
  10143. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10144. ;; Make the proper lists from the dates
  10145. (catch 'exit
  10146. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10147. dn dw sday cday n1 n2
  10148. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10149. (setq start (org-date-to-gregorian start)
  10150. current (org-date-to-gregorian
  10151. (if show-all
  10152. current
  10153. (time-to-days (current-time))))
  10154. sday (calendar-absolute-from-gregorian start)
  10155. cday (calendar-absolute-from-gregorian current))
  10156. (if (<= cday sday) (throw 'exit sday))
  10157. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10158. (setq dn (string-to-number (match-string 1 change))
  10159. dw (cdr (assoc (match-string 2 change) a1)))
  10160. (error "Invalid change specifyer: %s" change))
  10161. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10162. (cond
  10163. ((eq dw 'day)
  10164. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10165. n2 (+ n1 dn)))
  10166. ((eq dw 'year)
  10167. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10168. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10169. (setq date1 (list m d y1)
  10170. n1 (calendar-absolute-from-gregorian date1)
  10171. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10172. n2 (calendar-absolute-from-gregorian date2)))
  10173. ((eq dw 'month)
  10174. ;; approx number of month between the two dates
  10175. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10176. ;; How often does dn fit in there?
  10177. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10178. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10179. m (+ m nm)
  10180. ny (floor (/ m 12))
  10181. y (+ y ny)
  10182. m (- m (* ny 12)))
  10183. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10184. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10185. (setq m2 (+ m dn) y2 y)
  10186. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10187. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10188. (while (<= n2 cday)
  10189. (setq n1 n2 m m2 y y2)
  10190. (setq m2 (+ m dn) y2 y)
  10191. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10192. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10193. (if show-all
  10194. (cond
  10195. ((eq prefer 'past) n1)
  10196. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10197. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10198. (cond
  10199. ((eq prefer 'past) n1)
  10200. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10201. (t (if (= cday n1) n1 n2)))))))
  10202. (defun org-date-to-gregorian (date)
  10203. "Turn any specification of DATE into a gregorian date for the calendar."
  10204. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10205. ((and (listp date) (= (length date) 3)) date)
  10206. ((stringp date)
  10207. (setq date (org-parse-time-string date))
  10208. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10209. ((listp date)
  10210. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10211. (defun org-parse-time-string (s &optional nodefault)
  10212. "Parse the standard Org-mode time string.
  10213. This should be a lot faster than the normal `parse-time-string'.
  10214. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10215. hour and minute fields will be nil if not given."
  10216. (if (string-match org-ts-regexp0 s)
  10217. (list 0
  10218. (if (or (match-beginning 8) (not nodefault))
  10219. (string-to-number (or (match-string 8 s) "0")))
  10220. (if (or (match-beginning 7) (not nodefault))
  10221. (string-to-number (or (match-string 7 s) "0")))
  10222. (string-to-number (match-string 4 s))
  10223. (string-to-number (match-string 3 s))
  10224. (string-to-number (match-string 2 s))
  10225. nil nil nil)
  10226. (make-list 9 0)))
  10227. (defun org-timestamp-up (&optional arg)
  10228. "Increase the date item at the cursor by one.
  10229. If the cursor is on the year, change the year. If it is on the month or
  10230. the day, change that.
  10231. With prefix ARG, change by that many units."
  10232. (interactive "p")
  10233. (org-timestamp-change (prefix-numeric-value arg)))
  10234. (defun org-timestamp-down (&optional arg)
  10235. "Decrease the date item at the cursor by one.
  10236. If the cursor is on the year, change the year. If it is on the month or
  10237. the day, change that.
  10238. With prefix ARG, change by that many units."
  10239. (interactive "p")
  10240. (org-timestamp-change (- (prefix-numeric-value arg))))
  10241. (defun org-timestamp-up-day (&optional arg)
  10242. "Increase the date in the time stamp by one day.
  10243. With prefix ARG, change that many days."
  10244. (interactive "p")
  10245. (if (and (not (org-at-timestamp-p t))
  10246. (org-on-heading-p))
  10247. (org-todo 'up)
  10248. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10249. (defun org-timestamp-down-day (&optional arg)
  10250. "Decrease the date in the time stamp by one day.
  10251. With prefix ARG, change that many days."
  10252. (interactive "p")
  10253. (if (and (not (org-at-timestamp-p t))
  10254. (org-on-heading-p))
  10255. (org-todo 'down)
  10256. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10257. (defun org-at-timestamp-p (&optional inactive-ok)
  10258. "Determine if the cursor is in or at a timestamp."
  10259. (interactive)
  10260. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10261. (pos (point))
  10262. (ans (or (looking-at tsr)
  10263. (save-excursion
  10264. (skip-chars-backward "^[<\n\r\t")
  10265. (if (> (point) (point-min)) (backward-char 1))
  10266. (and (looking-at tsr)
  10267. (> (- (match-end 0) pos) -1))))))
  10268. (and ans
  10269. (boundp 'org-ts-what)
  10270. (setq org-ts-what
  10271. (cond
  10272. ((= pos (match-beginning 0)) 'bracket)
  10273. ((= pos (1- (match-end 0))) 'bracket)
  10274. ((org-pos-in-match-range pos 2) 'year)
  10275. ((org-pos-in-match-range pos 3) 'month)
  10276. ((org-pos-in-match-range pos 7) 'hour)
  10277. ((org-pos-in-match-range pos 8) 'minute)
  10278. ((or (org-pos-in-match-range pos 4)
  10279. (org-pos-in-match-range pos 5)) 'day)
  10280. ((and (> pos (or (match-end 8) (match-end 5)))
  10281. (< pos (match-end 0)))
  10282. (- pos (or (match-end 8) (match-end 5))))
  10283. (t 'day))))
  10284. ans))
  10285. (defun org-toggle-timestamp-type ()
  10286. "Toggle the type (<active> or [inactive]) of a time stamp."
  10287. (interactive)
  10288. (when (org-at-timestamp-p t)
  10289. (save-excursion
  10290. (goto-char (match-beginning 0))
  10291. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10292. (goto-char (1- (match-end 0)))
  10293. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10294. (message "Timestamp is now %sactive"
  10295. (if (equal (char-before) ?>) "in" ""))))
  10296. (defun org-timestamp-change (n &optional what)
  10297. "Change the date in the time stamp at point.
  10298. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10299. `year', `minute', `second'. If WHAT is not given, the cursor position
  10300. in the timestamp determines what will be changed."
  10301. (let ((pos (point))
  10302. with-hm inactive
  10303. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10304. org-ts-what
  10305. extra rem
  10306. ts time time0)
  10307. (if (not (org-at-timestamp-p t))
  10308. (error "Not at a timestamp"))
  10309. (if (and (not what) (eq org-ts-what 'bracket))
  10310. (org-toggle-timestamp-type)
  10311. (if (and (not what) (not (eq org-ts-what 'day))
  10312. org-display-custom-times
  10313. (get-text-property (point) 'display)
  10314. (not (get-text-property (1- (point)) 'display)))
  10315. (setq org-ts-what 'day))
  10316. (setq org-ts-what (or what org-ts-what)
  10317. inactive (= (char-after (match-beginning 0)) ?\[)
  10318. ts (match-string 0))
  10319. (replace-match "")
  10320. (if (string-match
  10321. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10322. ts)
  10323. (setq extra (match-string 1 ts)))
  10324. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10325. (setq with-hm t))
  10326. (setq time0 (org-parse-time-string ts))
  10327. (when (and (eq org-ts-what 'minute)
  10328. (eq current-prefix-arg nil))
  10329. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10330. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10331. (setcar (cdr time0) (+ (nth 1 time0)
  10332. (if (> n 0) (- rem) (- dm rem))))))
  10333. (setq time
  10334. (encode-time (or (car time0) 0)
  10335. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10336. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10337. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10338. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10339. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10340. (nthcdr 6 time0)))
  10341. (when (integerp org-ts-what)
  10342. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10343. (if (eq what 'calendar)
  10344. (let ((cal-date (org-get-date-from-calendar)))
  10345. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10346. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10347. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10348. (setcar time0 (or (car time0) 0))
  10349. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10350. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10351. (setq time (apply 'encode-time time0))))
  10352. (setq org-last-changed-timestamp
  10353. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10354. (org-clock-update-time-maybe)
  10355. (goto-char pos)
  10356. ;; Try to recenter the calendar window, if any
  10357. (if (and org-calendar-follow-timestamp-change
  10358. (get-buffer-window "*Calendar*" t)
  10359. (memq org-ts-what '(day month year)))
  10360. (org-recenter-calendar (time-to-days time))))))
  10361. (defun org-modify-ts-extra (s pos n dm)
  10362. "Change the different parts of the lead-time and repeat fields in timestamp."
  10363. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10364. ng h m new rem)
  10365. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10366. (cond
  10367. ((or (org-pos-in-match-range pos 2)
  10368. (org-pos-in-match-range pos 3))
  10369. (setq m (string-to-number (match-string 3 s))
  10370. h (string-to-number (match-string 2 s)))
  10371. (if (org-pos-in-match-range pos 2)
  10372. (setq h (+ h n))
  10373. (setq n (* dm (org-no-warnings (signum n))))
  10374. (when (not (= 0 (setq rem (% m dm))))
  10375. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10376. (setq m (+ m n)))
  10377. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10378. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10379. (setq h (min 24 (max 0 h)))
  10380. (setq ng 1 new (format "-%02d:%02d" h m)))
  10381. ((org-pos-in-match-range pos 6)
  10382. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10383. ((org-pos-in-match-range pos 5)
  10384. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10385. ((org-pos-in-match-range pos 9)
  10386. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10387. ((org-pos-in-match-range pos 8)
  10388. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10389. (when ng
  10390. (setq s (concat
  10391. (substring s 0 (match-beginning ng))
  10392. new
  10393. (substring s (match-end ng))))))
  10394. s))
  10395. (defun org-recenter-calendar (date)
  10396. "If the calendar is visible, recenter it to DATE."
  10397. (let* ((win (selected-window))
  10398. (cwin (get-buffer-window "*Calendar*" t))
  10399. (calendar-move-hook nil))
  10400. (when cwin
  10401. (select-window cwin)
  10402. (calendar-goto-date (if (listp date) date
  10403. (calendar-gregorian-from-absolute date)))
  10404. (select-window win))))
  10405. (defun org-goto-calendar (&optional arg)
  10406. "Go to the Emacs calendar at the current date.
  10407. If there is a time stamp in the current line, go to that date.
  10408. A prefix ARG can be used to force the current date."
  10409. (interactive "P")
  10410. (let ((tsr org-ts-regexp) diff
  10411. (calendar-move-hook nil)
  10412. (calendar-view-holidays-initially-flag nil)
  10413. (view-calendar-holidays-initially nil)
  10414. (calendar-view-diary-initially-flag nil)
  10415. (view-diary-entries-initially nil))
  10416. (if (or (org-at-timestamp-p)
  10417. (save-excursion
  10418. (beginning-of-line 1)
  10419. (looking-at (concat ".*" tsr))))
  10420. (let ((d1 (time-to-days (current-time)))
  10421. (d2 (time-to-days
  10422. (org-time-string-to-time (match-string 1)))))
  10423. (setq diff (- d2 d1))))
  10424. (calendar)
  10425. (calendar-goto-today)
  10426. (if (and diff (not arg)) (calendar-forward-day diff))))
  10427. (defun org-get-date-from-calendar ()
  10428. "Return a list (month day year) of date at point in calendar."
  10429. (with-current-buffer "*Calendar*"
  10430. (save-match-data
  10431. (calendar-cursor-to-date))))
  10432. (defun org-date-from-calendar ()
  10433. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10434. If there is already a time stamp at the cursor position, update it."
  10435. (interactive)
  10436. (if (org-at-timestamp-p t)
  10437. (org-timestamp-change 0 'calendar)
  10438. (let ((cal-date (org-get-date-from-calendar)))
  10439. (org-insert-time-stamp
  10440. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10441. (defun org-minutes-to-hh:mm-string (m)
  10442. "Compute H:MM from a number of minutes."
  10443. (let ((h (/ m 60)))
  10444. (setq m (- m (* 60 h)))
  10445. (format org-time-clocksum-format h m)))
  10446. (defun org-hh:mm-string-to-minutes (s)
  10447. "Convert a string H:MM to a number of minutes."
  10448. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10449. (+ (* (string-to-number (match-string 1 s)) 60)
  10450. (string-to-number (match-string 2 s)))
  10451. 0))
  10452. ;;;; Agenda files
  10453. ;;;###autoload
  10454. (defun org-iswitchb (&optional arg)
  10455. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10456. With a prefix argument, restrict available to files.
  10457. With two prefix arguments, restrict available buffers to agenda files.
  10458. Due to some yet unresolved reason, the global function
  10459. `iswitchb-mode' needs to be active for this function to work."
  10460. (interactive "P")
  10461. (require 'iswitchb)
  10462. (let ((enabled iswitchb-mode) blist)
  10463. (or enabled (iswitchb-mode 1))
  10464. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10465. ((equal arg '(16)) (org-buffer-list 'agenda))
  10466. (t (org-buffer-list))))
  10467. (unwind-protect
  10468. (let ((iswitchb-make-buflist-hook
  10469. (lambda ()
  10470. (setq iswitchb-temp-buflist
  10471. (mapcar 'buffer-name blist)))))
  10472. (switch-to-buffer
  10473. (iswitchb-read-buffer
  10474. "Switch-to: " nil t))
  10475. (or enabled (iswitchb-mode -1))))))
  10476. (defun org-buffer-list (&optional predicate exclude-tmp)
  10477. "Return a list of Org buffers.
  10478. PREDICATE can be `export', `files' or `agenda'.
  10479. export restrict the list to Export buffers.
  10480. files restrict the list to buffers visiting Org files.
  10481. agenda restrict the list to buffers visiting agenda files.
  10482. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10483. (let* ((bfn nil)
  10484. (agenda-files (and (eq predicate 'agenda)
  10485. (mapcar 'file-truename (org-agenda-files t))))
  10486. (filter
  10487. (cond
  10488. ((eq predicate 'files)
  10489. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10490. ((eq predicate 'export)
  10491. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10492. ((eq predicate 'agenda)
  10493. (lambda (b)
  10494. (with-current-buffer b
  10495. (and (eq major-mode 'org-mode)
  10496. (setq bfn (buffer-file-name b))
  10497. (member (file-truename bfn) agenda-files)))))
  10498. (t (lambda (b) (with-current-buffer b
  10499. (or (eq major-mode 'org-mode)
  10500. (string-match "\*Org .*Export"
  10501. (buffer-name b)))))))))
  10502. (delq nil
  10503. (mapcar
  10504. (lambda(b)
  10505. (if (and (funcall filter b)
  10506. (or (not exclude-tmp)
  10507. (not (string-match "tmp" (buffer-name b)))))
  10508. b
  10509. nil))
  10510. (buffer-list)))))
  10511. (defun org-agenda-files (&optional unrestricted archives)
  10512. "Get the list of agenda files.
  10513. Optional UNRESTRICTED means return the full list even if a restriction
  10514. is currently in place.
  10515. When ARCHIVES is t, include all archive files hat are really being
  10516. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10517. `org-agenda-archives-mode' is t."
  10518. (let ((files
  10519. (cond
  10520. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10521. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10522. ((listp org-agenda-files) org-agenda-files)
  10523. (t (error "Invalid value of `org-agenda-files'")))))
  10524. (setq files (apply 'append
  10525. (mapcar (lambda (f)
  10526. (if (file-directory-p f)
  10527. (directory-files
  10528. f t org-agenda-file-regexp)
  10529. (list f)))
  10530. files)))
  10531. (when org-agenda-skip-unavailable-files
  10532. (setq files (delq nil
  10533. (mapcar (function
  10534. (lambda (file)
  10535. (and (file-readable-p file) file)))
  10536. files))))
  10537. (when (or (eq archives t)
  10538. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10539. (setq files (org-add-archive-files files)))
  10540. files))
  10541. (defun org-edit-agenda-file-list ()
  10542. "Edit the list of agenda files.
  10543. Depending on setup, this either uses customize to edit the variable
  10544. `org-agenda-files', or it visits the file that is holding the list. In the
  10545. latter case, the buffer is set up in a way that saving it automatically kills
  10546. the buffer and restores the previous window configuration."
  10547. (interactive)
  10548. (if (stringp org-agenda-files)
  10549. (let ((cw (current-window-configuration)))
  10550. (find-file org-agenda-files)
  10551. (org-set-local 'org-window-configuration cw)
  10552. (org-add-hook 'after-save-hook
  10553. (lambda ()
  10554. (set-window-configuration
  10555. (prog1 org-window-configuration
  10556. (kill-buffer (current-buffer))))
  10557. (org-install-agenda-files-menu)
  10558. (message "New agenda file list installed"))
  10559. nil 'local)
  10560. (message "%s" (substitute-command-keys
  10561. "Edit list and finish with \\[save-buffer]")))
  10562. (customize-variable 'org-agenda-files)))
  10563. (defun org-store-new-agenda-file-list (list)
  10564. "Set new value for the agenda file list and save it correcly."
  10565. (if (stringp org-agenda-files)
  10566. (let ((f org-agenda-files) b)
  10567. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10568. (with-temp-file f
  10569. (insert (mapconcat 'identity list "\n") "\n")))
  10570. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10571. (setq org-agenda-files list)
  10572. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10573. (defun org-read-agenda-file-list ()
  10574. "Read the list of agenda files from a file."
  10575. (when (file-directory-p org-agenda-files)
  10576. (error "`org-agenda-files' cannot be a single directory"))
  10577. (when (stringp org-agenda-files)
  10578. (with-temp-buffer
  10579. (insert-file-contents org-agenda-files)
  10580. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10581. ;;;###autoload
  10582. (defun org-cycle-agenda-files ()
  10583. "Cycle through the files in `org-agenda-files'.
  10584. If the current buffer visits an agenda file, find the next one in the list.
  10585. If the current buffer does not, find the first agenda file."
  10586. (interactive)
  10587. (let* ((fs (org-agenda-files t))
  10588. (files (append fs (list (car fs))))
  10589. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10590. file)
  10591. (unless files (error "No agenda files"))
  10592. (catch 'exit
  10593. (while (setq file (pop files))
  10594. (if (equal (file-truename file) tcf)
  10595. (when (car files)
  10596. (find-file (car files))
  10597. (throw 'exit t))))
  10598. (find-file (car fs)))
  10599. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10600. (defun org-agenda-file-to-front (&optional to-end)
  10601. "Move/add the current file to the top of the agenda file list.
  10602. If the file is not present in the list, it is added to the front. If it is
  10603. present, it is moved there. With optional argument TO-END, add/move to the
  10604. end of the list."
  10605. (interactive "P")
  10606. (let ((org-agenda-skip-unavailable-files nil)
  10607. (file-alist (mapcar (lambda (x)
  10608. (cons (file-truename x) x))
  10609. (org-agenda-files t)))
  10610. (ctf (file-truename buffer-file-name))
  10611. x had)
  10612. (setq x (assoc ctf file-alist) had x)
  10613. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10614. (if to-end
  10615. (setq file-alist (append (delq x file-alist) (list x)))
  10616. (setq file-alist (cons x (delq x file-alist))))
  10617. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10618. (org-install-agenda-files-menu)
  10619. (message "File %s to %s of agenda file list"
  10620. (if had "moved" "added") (if to-end "end" "front"))))
  10621. (defun org-remove-file (&optional file)
  10622. "Remove current file from the list of files in variable `org-agenda-files'.
  10623. These are the files which are being checked for agenda entries.
  10624. Optional argument FILE means, use this file instead of the current."
  10625. (interactive)
  10626. (let* ((org-agenda-skip-unavailable-files nil)
  10627. (file (or file buffer-file-name))
  10628. (true-file (file-truename file))
  10629. (afile (abbreviate-file-name file))
  10630. (files (delq nil (mapcar
  10631. (lambda (x)
  10632. (if (equal true-file
  10633. (file-truename x))
  10634. nil x))
  10635. (org-agenda-files t)))))
  10636. (if (not (= (length files) (length (org-agenda-files t))))
  10637. (progn
  10638. (org-store-new-agenda-file-list files)
  10639. (org-install-agenda-files-menu)
  10640. (message "Removed file: %s" afile))
  10641. (message "File was not in list: %s (not removed)" afile))))
  10642. (defun org-file-menu-entry (file)
  10643. (vector file (list 'find-file file) t))
  10644. (defun org-check-agenda-file (file)
  10645. "Make sure FILE exists. If not, ask user what to do."
  10646. (when (not (file-exists-p file))
  10647. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10648. (abbreviate-file-name file))
  10649. (let ((r (downcase (read-char-exclusive))))
  10650. (cond
  10651. ((equal r ?r)
  10652. (org-remove-file file)
  10653. (throw 'nextfile t))
  10654. (t (error "Abort"))))))
  10655. (defun org-get-agenda-file-buffer (file)
  10656. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10657. it to the list of buffers which might be released later."
  10658. (let ((buf (org-find-base-buffer-visiting file)))
  10659. (if buf
  10660. buf ; just return it
  10661. ;; Make a new buffer and remember it
  10662. (setq buf (find-file-noselect file))
  10663. (if buf (push buf org-agenda-new-buffers))
  10664. buf)))
  10665. (defun org-release-buffers (blist)
  10666. "Release all buffers in list, asking the user for confirmation when needed.
  10667. When a buffer is unmodified, it is just killed. When modified, it is saved
  10668. \(if the user agrees) and then killed."
  10669. (let (buf file)
  10670. (while (setq buf (pop blist))
  10671. (setq file (buffer-file-name buf))
  10672. (when (and (buffer-modified-p buf)
  10673. file
  10674. (y-or-n-p (format "Save file %s? " file)))
  10675. (with-current-buffer buf (save-buffer)))
  10676. (kill-buffer buf))))
  10677. (defun org-prepare-agenda-buffers (files)
  10678. "Create buffers for all agenda files, protect archived trees and comments."
  10679. (interactive)
  10680. (let ((pa '(:org-archived t))
  10681. (pc '(:org-comment t))
  10682. (pall '(:org-archived t :org-comment t))
  10683. (inhibit-read-only t)
  10684. (rea (concat ":" org-archive-tag ":"))
  10685. bmp file re)
  10686. (save-excursion
  10687. (save-restriction
  10688. (while (setq file (pop files))
  10689. (if (bufferp file)
  10690. (set-buffer file)
  10691. (org-check-agenda-file file)
  10692. (set-buffer (org-get-agenda-file-buffer file)))
  10693. (widen)
  10694. (setq bmp (buffer-modified-p))
  10695. (org-refresh-category-properties)
  10696. (setq org-todo-keywords-for-agenda
  10697. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10698. (setq org-done-keywords-for-agenda
  10699. (append org-done-keywords-for-agenda org-done-keywords))
  10700. (setq org-todo-keyword-alist-for-agenda
  10701. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10702. (setq org-tag-alist-for-agenda
  10703. (append org-tag-alist-for-agenda org-tag-alist))
  10704. (save-excursion
  10705. (remove-text-properties (point-min) (point-max) pall)
  10706. (when org-agenda-skip-archived-trees
  10707. (goto-char (point-min))
  10708. (while (re-search-forward rea nil t)
  10709. (if (org-on-heading-p t)
  10710. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10711. (goto-char (point-min))
  10712. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10713. (while (re-search-forward re nil t)
  10714. (add-text-properties
  10715. (match-beginning 0) (org-end-of-subtree t) pc)))
  10716. (set-buffer-modified-p bmp))))
  10717. (setq org-todo-keyword-alist-for-agenda
  10718. (org-uniquify org-todo-keyword-alist-for-agenda)
  10719. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10720. ;;;; Embedded LaTeX
  10721. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10722. "Keymap for the minor `org-cdlatex-mode'.")
  10723. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10724. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10725. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10726. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10727. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10728. (defvar org-cdlatex-texmathp-advice-is-done nil
  10729. "Flag remembering if we have applied the advice to texmathp already.")
  10730. (define-minor-mode org-cdlatex-mode
  10731. "Toggle the minor `org-cdlatex-mode'.
  10732. This mode supports entering LaTeX environment and math in LaTeX fragments
  10733. in Org-mode.
  10734. \\{org-cdlatex-mode-map}"
  10735. nil " OCDL" nil
  10736. (when org-cdlatex-mode (require 'cdlatex))
  10737. (unless org-cdlatex-texmathp-advice-is-done
  10738. (setq org-cdlatex-texmathp-advice-is-done t)
  10739. (defadvice texmathp (around org-math-always-on activate)
  10740. "Always return t in org-mode buffers.
  10741. This is because we want to insert math symbols without dollars even outside
  10742. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10743. en embedded LaTeX fragement, let texmathp do its job.
  10744. \\[org-cdlatex-mode-map]"
  10745. (interactive)
  10746. (let (p)
  10747. (cond
  10748. ((not (org-mode-p)) ad-do-it)
  10749. ((eq this-command 'cdlatex-math-symbol)
  10750. (setq ad-return-value t
  10751. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10752. (t
  10753. (let ((p (org-inside-LaTeX-fragment-p)))
  10754. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10755. (setq ad-return-value t
  10756. texmathp-why '("Org-mode embedded math" . 0))
  10757. (if p ad-do-it)))))))))
  10758. (defun turn-on-org-cdlatex ()
  10759. "Unconditionally turn on `org-cdlatex-mode'."
  10760. (org-cdlatex-mode 1))
  10761. (defun org-inside-LaTeX-fragment-p ()
  10762. "Test if point is inside a LaTeX fragment.
  10763. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10764. sequence appearing also before point.
  10765. Even though the matchers for math are configurable, this function assumes
  10766. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10767. delimiters are skipped when they have been removed by customization.
  10768. The return value is nil, or a cons cell with the delimiter and
  10769. and the position of this delimiter.
  10770. This function does a reasonably good job, but can locally be fooled by
  10771. for example currency specifications. For example it will assume being in
  10772. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10773. fragments that are properly closed, but during editing, we have to live
  10774. with the uncertainty caused by missing closing delimiters. This function
  10775. looks only before point, not after."
  10776. (catch 'exit
  10777. (let ((pos (point))
  10778. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10779. (lim (progn
  10780. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10781. (point)))
  10782. dd-on str (start 0) m re)
  10783. (goto-char pos)
  10784. (when dodollar
  10785. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10786. re (nth 1 (assoc "$" org-latex-regexps)))
  10787. (while (string-match re str start)
  10788. (cond
  10789. ((= (match-end 0) (length str))
  10790. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10791. ((= (match-end 0) (- (length str) 5))
  10792. (throw 'exit nil))
  10793. (t (setq start (match-end 0))))))
  10794. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10795. (goto-char pos)
  10796. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10797. (and (match-beginning 2) (throw 'exit nil))
  10798. ;; count $$
  10799. (while (re-search-backward "\\$\\$" lim t)
  10800. (setq dd-on (not dd-on)))
  10801. (goto-char pos)
  10802. (if dd-on (cons "$$" m))))))
  10803. (defun org-try-cdlatex-tab ()
  10804. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10805. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10806. - inside a LaTeX fragment, or
  10807. - after the first word in a line, where an abbreviation expansion could
  10808. insert a LaTeX environment."
  10809. (when org-cdlatex-mode
  10810. (cond
  10811. ((save-excursion
  10812. (skip-chars-backward "a-zA-Z0-9*")
  10813. (skip-chars-backward " \t")
  10814. (bolp))
  10815. (cdlatex-tab) t)
  10816. ((org-inside-LaTeX-fragment-p)
  10817. (cdlatex-tab) t)
  10818. (t nil))))
  10819. (defun org-cdlatex-underscore-caret (&optional arg)
  10820. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10821. Revert to the normal definition outside of these fragments."
  10822. (interactive "P")
  10823. (if (org-inside-LaTeX-fragment-p)
  10824. (call-interactively 'cdlatex-sub-superscript)
  10825. (let (org-cdlatex-mode)
  10826. (call-interactively (key-binding (vector last-input-event))))))
  10827. (defun org-cdlatex-math-modify (&optional arg)
  10828. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10829. Revert to the normal definition outside of these fragments."
  10830. (interactive "P")
  10831. (if (org-inside-LaTeX-fragment-p)
  10832. (call-interactively 'cdlatex-math-modify)
  10833. (let (org-cdlatex-mode)
  10834. (call-interactively (key-binding (vector last-input-event))))))
  10835. (defvar org-latex-fragment-image-overlays nil
  10836. "List of overlays carrying the images of latex fragments.")
  10837. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10838. (defun org-remove-latex-fragment-image-overlays ()
  10839. "Remove all overlays with LaTeX fragment images in current buffer."
  10840. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10841. (setq org-latex-fragment-image-overlays nil))
  10842. (defun org-preview-latex-fragment (&optional subtree)
  10843. "Preview the LaTeX fragment at point, or all locally or globally.
  10844. If the cursor is in a LaTeX fragment, create the image and overlay
  10845. it over the source code. If there is no fragment at point, display
  10846. all fragments in the current text, from one headline to the next. With
  10847. prefix SUBTREE, display all fragments in the current subtree. With a
  10848. double prefix `C-u C-u', or when the cursor is before the first headline,
  10849. display all fragments in the buffer.
  10850. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10851. (interactive "P")
  10852. (org-remove-latex-fragment-image-overlays)
  10853. (save-excursion
  10854. (save-restriction
  10855. (let (beg end at msg)
  10856. (cond
  10857. ((or (equal subtree '(16))
  10858. (not (save-excursion
  10859. (re-search-backward (concat "^" outline-regexp) nil t))))
  10860. (setq beg (point-min) end (point-max)
  10861. msg "Creating images for buffer...%s"))
  10862. ((equal subtree '(4))
  10863. (org-back-to-heading)
  10864. (setq beg (point) end (org-end-of-subtree t)
  10865. msg "Creating images for subtree...%s"))
  10866. (t
  10867. (if (setq at (org-inside-LaTeX-fragment-p))
  10868. (goto-char (max (point-min) (- (cdr at) 2)))
  10869. (org-back-to-heading))
  10870. (setq beg (point) end (progn (outline-next-heading) (point))
  10871. msg (if at "Creating image...%s"
  10872. "Creating images for entry...%s"))))
  10873. (message msg "")
  10874. (narrow-to-region beg end)
  10875. (goto-char beg)
  10876. (org-format-latex
  10877. (concat "ltxpng/" (file-name-sans-extension
  10878. (file-name-nondirectory
  10879. buffer-file-name)))
  10880. default-directory 'overlays msg at 'forbuffer)
  10881. (message msg "done. Use `C-c C-c' to remove images.")))))
  10882. (defvar org-latex-regexps
  10883. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10884. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10885. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10886. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10887. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10888. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10889. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10890. "Regular expressions for matching embedded LaTeX.")
  10891. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10892. "Replace LaTeX fragments with links to an image, and produce images."
  10893. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10894. (let* ((prefixnodir (file-name-nondirectory prefix))
  10895. (absprefix (expand-file-name prefix dir))
  10896. (todir (file-name-directory absprefix))
  10897. (opt org-format-latex-options)
  10898. (matchers (plist-get opt :matchers))
  10899. (re-list org-latex-regexps)
  10900. (cnt 0) txt link beg end re e checkdir
  10901. m n block linkfile movefile ov)
  10902. ;; Check if there are old images files with this prefix, and remove them
  10903. (when (file-directory-p todir)
  10904. (mapc 'delete-file
  10905. (directory-files
  10906. todir 'full
  10907. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10908. ;; Check the different regular expressions
  10909. (while (setq e (pop re-list))
  10910. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10911. block (if (nth 3 e) "\n\n" ""))
  10912. (when (member m matchers)
  10913. (goto-char (point-min))
  10914. (while (re-search-forward re nil t)
  10915. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10916. (setq txt (match-string n)
  10917. beg (match-beginning n) end (match-end n)
  10918. cnt (1+ cnt)
  10919. linkfile (format "%s_%04d.png" prefix cnt)
  10920. movefile (format "%s_%04d.png" absprefix cnt)
  10921. link (concat block "[[file:" linkfile "]]" block))
  10922. (if msg (message msg cnt))
  10923. (goto-char beg)
  10924. (unless checkdir ; make sure the directory exists
  10925. (setq checkdir t)
  10926. (or (file-directory-p todir) (make-directory todir)))
  10927. (org-create-formula-image
  10928. txt movefile opt forbuffer)
  10929. (if overlays
  10930. (progn
  10931. (setq ov (org-make-overlay beg end))
  10932. (if (featurep 'xemacs)
  10933. (progn
  10934. (org-overlay-put ov 'invisible t)
  10935. (org-overlay-put
  10936. ov 'end-glyph
  10937. (make-glyph (vector 'png :file movefile))))
  10938. (org-overlay-put
  10939. ov 'display
  10940. (list 'image :type 'png :file movefile :ascent 'center)))
  10941. (push ov org-latex-fragment-image-overlays)
  10942. (goto-char end))
  10943. (delete-region beg end)
  10944. (insert link))))))))
  10945. ;; This function borrows from Ganesh Swami's latex2png.el
  10946. (defun org-create-formula-image (string tofile options buffer)
  10947. (let* ((tmpdir (if (featurep 'xemacs)
  10948. (temp-directory)
  10949. temporary-file-directory))
  10950. (texfilebase (make-temp-name
  10951. (expand-file-name "orgtex" tmpdir)))
  10952. (texfile (concat texfilebase ".tex"))
  10953. (dvifile (concat texfilebase ".dvi"))
  10954. (pngfile (concat texfilebase ".png"))
  10955. (fnh (if (featurep 'xemacs)
  10956. (font-height (get-face-font 'default))
  10957. (face-attribute 'default :height nil)))
  10958. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10959. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10960. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10961. "Black"))
  10962. (bg (or (plist-get options (if buffer :background :html-background))
  10963. "Transparent")))
  10964. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10965. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10966. (with-temp-file texfile
  10967. (insert org-format-latex-header
  10968. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10969. (let ((dir default-directory))
  10970. (condition-case nil
  10971. (progn
  10972. (cd tmpdir)
  10973. (call-process "latex" nil nil nil texfile))
  10974. (error nil))
  10975. (cd dir))
  10976. (if (not (file-exists-p dvifile))
  10977. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10978. (condition-case nil
  10979. (call-process "dvipng" nil nil nil
  10980. "-E" "-fg" fg "-bg" bg
  10981. "-D" dpi
  10982. ;;"-x" scale "-y" scale
  10983. "-T" "tight"
  10984. "-o" pngfile
  10985. dvifile)
  10986. (error nil))
  10987. (if (not (file-exists-p pngfile))
  10988. (progn (message "Failed to create png file from %s" texfile) nil)
  10989. ;; Use the requested file name and clean up
  10990. (copy-file pngfile tofile 'replace)
  10991. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10992. (delete-file (concat texfilebase e)))
  10993. pngfile))))
  10994. (defun org-dvipng-color (attr)
  10995. "Return an rgb color specification for dvipng."
  10996. (apply 'format "rgb %s %s %s"
  10997. (mapcar 'org-normalize-color
  10998. (color-values (face-attribute 'default attr nil)))))
  10999. (defun org-normalize-color (value)
  11000. "Return string to be used as color value for an RGB component."
  11001. (format "%g" (/ value 65535.0)))
  11002. ;;;; Key bindings
  11003. ;; Make `C-c C-x' a prefix key
  11004. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11005. ;; TAB key with modifiers
  11006. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11007. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11008. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11009. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11010. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11011. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11012. ;; The following line is necessary under Suse GNU/Linux
  11013. (unless (featurep 'xemacs)
  11014. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11015. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11016. (define-key org-mode-map [backtab] 'org-shifttab)
  11017. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11018. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11019. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11020. ;; Cursor keys with modifiers
  11021. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11022. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11023. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11024. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11025. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11026. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11027. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11028. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11029. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11030. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11031. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11032. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11033. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11034. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11035. ;;; Extra keys for tty access.
  11036. ;; We only set them when really needed because otherwise the
  11037. ;; menus don't show the simple keys
  11038. (when (or org-use-extra-keys
  11039. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11040. (not window-system))
  11041. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11042. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11043. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11044. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11045. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11046. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11047. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11048. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11049. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11050. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11051. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11052. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11053. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11054. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11055. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11056. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11057. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11058. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11059. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11060. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11061. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11062. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11063. ;; All the other keys
  11064. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11065. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11066. (if (boundp 'narrow-map)
  11067. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11068. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11069. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11070. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11071. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11072. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11073. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11074. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11075. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11076. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11077. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11078. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11079. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11080. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11081. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11082. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11083. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11084. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11085. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11086. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11087. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11088. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11089. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11090. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11091. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11092. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11093. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11094. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11095. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11096. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11097. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11098. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11099. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11100. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11101. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11102. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11103. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11104. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11105. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11106. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11107. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11108. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11109. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11110. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11111. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11112. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11113. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11114. (org-defkey org-mode-map "\C-m" 'org-return)
  11115. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11116. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11117. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11118. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11119. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11120. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11121. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11122. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11123. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11124. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11125. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11126. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11127. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11128. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11129. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11130. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11131. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11132. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11133. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11134. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11135. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11136. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11137. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11138. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11139. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11140. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11141. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11142. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11143. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11144. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11145. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11146. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11147. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11148. (when (featurep 'xemacs)
  11149. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11150. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11151. (defun org-self-insert-command (N)
  11152. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11153. If the cursor is in a table looking at whitespace, the whitespace is
  11154. overwritten, and the table is not marked as requiring realignment."
  11155. (interactive "p")
  11156. (if (and (org-table-p)
  11157. (progn
  11158. ;; check if we blank the field, and if that triggers align
  11159. (and (featurep 'org-table) org-table-auto-blank-field
  11160. (member last-command
  11161. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11162. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11163. ;; got extra space, this field does not determine column width
  11164. (let (org-table-may-need-update) (org-table-blank-field))
  11165. ;; no extra space, this field may determine column width
  11166. (org-table-blank-field)))
  11167. t)
  11168. (eq N 1)
  11169. (looking-at "[^|\n]* |"))
  11170. (let (org-table-may-need-update)
  11171. (goto-char (1- (match-end 0)))
  11172. (delete-backward-char 1)
  11173. (goto-char (match-beginning 0))
  11174. (self-insert-command N))
  11175. (setq org-table-may-need-update t)
  11176. (self-insert-command N)
  11177. (org-fix-tags-on-the-fly)))
  11178. (defun org-fix-tags-on-the-fly ()
  11179. (when (and (equal (char-after (point-at-bol)) ?*)
  11180. (org-on-heading-p))
  11181. (org-align-tags-here org-tags-column)))
  11182. (defun org-delete-backward-char (N)
  11183. "Like `delete-backward-char', insert whitespace at field end in tables.
  11184. When deleting backwards, in tables this function will insert whitespace in
  11185. front of the next \"|\" separator, to keep the table aligned. The table will
  11186. still be marked for re-alignment if the field did fill the entire column,
  11187. because, in this case the deletion might narrow the column."
  11188. (interactive "p")
  11189. (if (and (org-table-p)
  11190. (eq N 1)
  11191. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11192. (looking-at ".*?|"))
  11193. (let ((pos (point))
  11194. (noalign (looking-at "[^|\n\r]* |"))
  11195. (c org-table-may-need-update))
  11196. (backward-delete-char N)
  11197. (skip-chars-forward "^|")
  11198. (insert " ")
  11199. (goto-char (1- pos))
  11200. ;; noalign: if there were two spaces at the end, this field
  11201. ;; does not determine the width of the column.
  11202. (if noalign (setq org-table-may-need-update c)))
  11203. (backward-delete-char N)
  11204. (org-fix-tags-on-the-fly)))
  11205. (defun org-delete-char (N)
  11206. "Like `delete-char', but insert whitespace at field end in tables.
  11207. When deleting characters, in tables this function will insert whitespace in
  11208. front of the next \"|\" separator, to keep the table aligned. The table will
  11209. still be marked for re-alignment if the field did fill the entire column,
  11210. because, in this case the deletion might narrow the column."
  11211. (interactive "p")
  11212. (if (and (org-table-p)
  11213. (not (bolp))
  11214. (not (= (char-after) ?|))
  11215. (eq N 1))
  11216. (if (looking-at ".*?|")
  11217. (let ((pos (point))
  11218. (noalign (looking-at "[^|\n\r]* |"))
  11219. (c org-table-may-need-update))
  11220. (replace-match (concat
  11221. (substring (match-string 0) 1 -1)
  11222. " |"))
  11223. (goto-char pos)
  11224. ;; noalign: if there were two spaces at the end, this field
  11225. ;; does not determine the width of the column.
  11226. (if noalign (setq org-table-may-need-update c)))
  11227. (delete-char N))
  11228. (delete-char N)
  11229. (org-fix-tags-on-the-fly)))
  11230. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11231. (put 'org-self-insert-command 'delete-selection t)
  11232. (put 'orgtbl-self-insert-command 'delete-selection t)
  11233. (put 'org-delete-char 'delete-selection 'supersede)
  11234. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11235. ;; Make `flyspell-mode' delay after some commands
  11236. (put 'org-self-insert-command 'flyspell-delayed t)
  11237. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11238. (put 'org-delete-char 'flyspell-delayed t)
  11239. (put 'org-delete-backward-char 'flyspell-delayed t)
  11240. ;; Make pabbrev-mode expand after org-mode commands
  11241. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11242. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11243. ;; How to do this: Measure non-white length of current string
  11244. ;; If equal to column width, we should realign.
  11245. (defun org-remap (map &rest commands)
  11246. "In MAP, remap the functions given in COMMANDS.
  11247. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11248. (let (new old)
  11249. (while commands
  11250. (setq old (pop commands) new (pop commands))
  11251. (if (fboundp 'command-remapping)
  11252. (org-defkey map (vector 'remap old) new)
  11253. (substitute-key-definition old new map global-map)))))
  11254. (when (eq org-enable-table-editor 'optimized)
  11255. ;; If the user wants maximum table support, we need to hijack
  11256. ;; some standard editing functions
  11257. (org-remap org-mode-map
  11258. 'self-insert-command 'org-self-insert-command
  11259. 'delete-char 'org-delete-char
  11260. 'delete-backward-char 'org-delete-backward-char)
  11261. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11262. (defun org-shiftcursor-error ()
  11263. "Throw an error because Shift-Cursor command was applied in wrong context."
  11264. (error "This command is active in special context like tables, headlines or timestamps"))
  11265. (defun org-shifttab (&optional arg)
  11266. "Global visibility cycling or move to previous table field.
  11267. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11268. on context.
  11269. See the individual commands for more information."
  11270. (interactive "P")
  11271. (cond
  11272. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11273. ((integerp arg)
  11274. (message "Content view to level: %d" arg)
  11275. (org-content (prefix-numeric-value arg))
  11276. (setq org-cycle-global-status 'overview))
  11277. (t (call-interactively 'org-global-cycle))))
  11278. (defun org-shiftmetaleft ()
  11279. "Promote subtree or delete table column.
  11280. Calls `org-promote-subtree', `org-outdent-item',
  11281. or `org-table-delete-column', depending on context.
  11282. See the individual commands for more information."
  11283. (interactive)
  11284. (cond
  11285. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11286. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11287. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11288. (t (org-shiftcursor-error))))
  11289. (defun org-shiftmetaright ()
  11290. "Demote subtree or insert table column.
  11291. Calls `org-demote-subtree', `org-indent-item',
  11292. or `org-table-insert-column', depending on context.
  11293. See the individual commands for more information."
  11294. (interactive)
  11295. (cond
  11296. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11297. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11298. ((org-at-item-p) (call-interactively 'org-indent-item))
  11299. (t (org-shiftcursor-error))))
  11300. (defun org-shiftmetaup (&optional arg)
  11301. "Move subtree up or kill table row.
  11302. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11303. `org-move-item-up' depending on context. See the individual commands
  11304. for more information."
  11305. (interactive "P")
  11306. (cond
  11307. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11308. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11309. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11310. (t (org-shiftcursor-error))))
  11311. (defun org-shiftmetadown (&optional arg)
  11312. "Move subtree down or insert table row.
  11313. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11314. `org-move-item-down', depending on context. See the individual
  11315. commands for more information."
  11316. (interactive "P")
  11317. (cond
  11318. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11319. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11320. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11321. (t (org-shiftcursor-error))))
  11322. (defun org-metaleft (&optional arg)
  11323. "Promote heading or move table column to left.
  11324. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11325. With no specific context, calls the Emacs default `backward-word'.
  11326. See the individual commands for more information."
  11327. (interactive "P")
  11328. (cond
  11329. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11330. ((or (org-on-heading-p) (org-region-active-p))
  11331. (call-interactively 'org-do-promote))
  11332. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11333. (t (call-interactively 'backward-word))))
  11334. (defun org-metaright (&optional arg)
  11335. "Demote subtree or move table column to right.
  11336. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11337. With no specific context, calls the Emacs default `forward-word'.
  11338. See the individual commands for more information."
  11339. (interactive "P")
  11340. (cond
  11341. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11342. ((or (org-on-heading-p) (org-region-active-p))
  11343. (call-interactively 'org-do-demote))
  11344. ((org-at-item-p) (call-interactively 'org-indent-item))
  11345. (t (call-interactively 'forward-word))))
  11346. (defun org-metaup (&optional arg)
  11347. "Move subtree up or move table row up.
  11348. Calls `org-move-subtree-up' or `org-table-move-row' or
  11349. `org-move-item-up', depending on context. See the individual commands
  11350. for more information."
  11351. (interactive "P")
  11352. (cond
  11353. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11354. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11355. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11356. (t (transpose-lines 1) (beginning-of-line -1))))
  11357. (defun org-metadown (&optional arg)
  11358. "Move subtree down or move table row down.
  11359. Calls `org-move-subtree-down' or `org-table-move-row' or
  11360. `org-move-item-down', depending on context. See the individual
  11361. commands for more information."
  11362. (interactive "P")
  11363. (cond
  11364. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11365. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11366. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11367. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11368. (defun org-shiftup (&optional arg)
  11369. "Increase item in timestamp or increase priority of current headline.
  11370. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11371. depending on context. See the individual commands for more information."
  11372. (interactive "P")
  11373. (cond
  11374. ((org-at-timestamp-p t)
  11375. (call-interactively (if org-edit-timestamp-down-means-later
  11376. 'org-timestamp-down 'org-timestamp-up)))
  11377. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11378. ((org-at-item-p) (call-interactively 'org-previous-item))
  11379. ((org-clocktable-try-shift 'up arg))
  11380. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11381. (defun org-shiftdown (&optional arg)
  11382. "Decrease item in timestamp or decrease priority of current headline.
  11383. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11384. depending on context. See the individual commands for more information."
  11385. (interactive "P")
  11386. (cond
  11387. ((org-at-timestamp-p t)
  11388. (call-interactively (if org-edit-timestamp-down-means-later
  11389. 'org-timestamp-up 'org-timestamp-down)))
  11390. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11391. ((org-clocktable-try-shift 'down arg))
  11392. (t (call-interactively 'org-next-item))))
  11393. (defun org-shiftright (&optional arg)
  11394. "Next TODO keyword or timestamp one day later, depending on context."
  11395. (interactive "P")
  11396. (cond
  11397. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11398. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11399. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11400. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11401. ((org-clocktable-try-shift 'right arg))
  11402. (t (org-shiftcursor-error))))
  11403. (defun org-shiftleft (&optional arg)
  11404. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11405. (interactive "P")
  11406. (cond
  11407. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11408. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11409. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11410. ((org-at-property-p)
  11411. (call-interactively 'org-property-previous-allowed-value))
  11412. ((org-clocktable-try-shift 'left arg))
  11413. (t (org-shiftcursor-error))))
  11414. (defun org-shiftcontrolright ()
  11415. "Switch to next TODO set."
  11416. (interactive)
  11417. (cond
  11418. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11419. (t (org-shiftcursor-error))))
  11420. (defun org-shiftcontrolleft ()
  11421. "Switch to previous TODO set."
  11422. (interactive)
  11423. (cond
  11424. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11425. (t (org-shiftcursor-error))))
  11426. (defun org-ctrl-c-ret ()
  11427. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11428. (interactive)
  11429. (cond
  11430. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11431. (t (call-interactively 'org-insert-heading))))
  11432. (defun org-copy-special ()
  11433. "Copy region in table or copy current subtree.
  11434. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11435. See the individual commands for more information."
  11436. (interactive)
  11437. (call-interactively
  11438. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11439. (defun org-cut-special ()
  11440. "Cut region in table or cut current subtree.
  11441. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11442. See the individual commands for more information."
  11443. (interactive)
  11444. (call-interactively
  11445. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11446. (defun org-paste-special (arg)
  11447. "Paste rectangular region into table, or past subtree relative to level.
  11448. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11449. See the individual commands for more information."
  11450. (interactive "P")
  11451. (if (org-at-table-p)
  11452. (org-table-paste-rectangle)
  11453. (org-paste-subtree arg)))
  11454. (defun org-edit-special ()
  11455. "Call a special editor for the stuff at point.
  11456. When at a table, call the formula editor with `org-table-edit-formulas'.
  11457. When at the first line of an src example, call `org-edit-src-code'.
  11458. When in an #+include line, visit the include file. Otherwise call
  11459. `ffap' to visit the file at point."
  11460. (interactive)
  11461. (cond
  11462. ((org-at-table-p)
  11463. (call-interactively 'org-table-edit-formulas))
  11464. ((save-excursion
  11465. (beginning-of-line 1)
  11466. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11467. (find-file (org-trim (match-string 1))))
  11468. ((org-edit-src-code))
  11469. ((org-edit-fixed-width-region))
  11470. (t (call-interactively 'ffap))))
  11471. (defun org-ctrl-c-ctrl-c (&optional arg)
  11472. "Set tags in headline, or update according to changed information at point.
  11473. This command does many different things, depending on context:
  11474. - If the cursor is in a headline, prompt for tags and insert them
  11475. into the current line, aligned to `org-tags-column'. When called
  11476. with prefix arg, realign all tags in the current buffer.
  11477. - If the cursor is in one of the special #+KEYWORD lines, this
  11478. triggers scanning the buffer for these lines and updating the
  11479. information.
  11480. - If the cursor is inside a table, realign the table. This command
  11481. works even if the automatic table editor has been turned off.
  11482. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11483. the entire table.
  11484. - If the cursor is a the beginning of a dynamic block, update it.
  11485. - If the cursor is inside a table created by the table.el package,
  11486. activate that table.
  11487. - If the current buffer is a remember buffer, close note and file it.
  11488. with a prefix argument, file it without further interaction to the default
  11489. location.
  11490. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11491. links in this buffer.
  11492. - If the cursor is on a numbered item in a plain list, renumber the
  11493. ordered list.
  11494. - If the cursor is on a checkbox, toggle it."
  11495. (interactive "P")
  11496. (let ((org-enable-table-editor t))
  11497. (cond
  11498. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11499. org-occur-highlights
  11500. org-latex-fragment-image-overlays)
  11501. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11502. (org-remove-occur-highlights)
  11503. (org-remove-latex-fragment-image-overlays)
  11504. (message "Temporary highlights/overlays removed from current buffer"))
  11505. ((and (local-variable-p 'org-finish-function (current-buffer))
  11506. (fboundp org-finish-function))
  11507. (funcall org-finish-function))
  11508. ((org-at-property-p)
  11509. (call-interactively 'org-property-action))
  11510. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11511. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11512. ((org-at-table.el-p)
  11513. (require 'table)
  11514. (beginning-of-line 1)
  11515. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11516. (call-interactively 'table-recognize-table))
  11517. ((org-at-table-p)
  11518. (org-table-maybe-eval-formula)
  11519. (if arg
  11520. (call-interactively 'org-table-recalculate)
  11521. (org-table-maybe-recalculate-line))
  11522. (call-interactively 'org-table-align))
  11523. ((org-at-item-checkbox-p)
  11524. (call-interactively 'org-toggle-checkbox))
  11525. ((org-at-item-p)
  11526. (call-interactively 'org-maybe-renumber-ordered-list))
  11527. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11528. ;; Dynamic block
  11529. (beginning-of-line 1)
  11530. (save-excursion (org-update-dblock)))
  11531. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11532. (cond
  11533. ((equal (match-string 1) "TBLFM")
  11534. ;; Recalculate the table before this line
  11535. (save-excursion
  11536. (beginning-of-line 1)
  11537. (skip-chars-backward " \r\n\t")
  11538. (if (org-at-table-p)
  11539. (org-call-with-arg 'org-table-recalculate t))))
  11540. (t
  11541. ; (org-set-regexps-and-options)
  11542. ; (org-restart-font-lock)
  11543. (let ((org-inhibit-startup t)) (org-mode-restart))
  11544. (message "Local setup has been refreshed"))))
  11545. (t (error "C-c C-c can do nothing useful at this location.")))))
  11546. (defun org-mode-restart ()
  11547. "Restart Org-mode, to scan again for special lines.
  11548. Also updates the keyword regular expressions."
  11549. (interactive)
  11550. (org-mode)
  11551. (message "Org-mode restarted"))
  11552. (defun org-kill-note-or-show-branches ()
  11553. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11554. (interactive)
  11555. (if (not org-finish-function)
  11556. (call-interactively 'show-branches)
  11557. (let ((org-note-abort t))
  11558. (funcall org-finish-function))))
  11559. (defun org-return (&optional indent)
  11560. "Goto next table row or insert a newline.
  11561. Calls `org-table-next-row' or `newline', depending on context.
  11562. See the individual commands for more information."
  11563. (interactive)
  11564. (cond
  11565. ((bobp) (if indent (newline-and-indent) (newline)))
  11566. ((and (org-at-heading-p)
  11567. (looking-at
  11568. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11569. (org-show-entry)
  11570. (end-of-line 1)
  11571. (newline))
  11572. ((org-at-table-p)
  11573. (org-table-justify-field-maybe)
  11574. (call-interactively 'org-table-next-row))
  11575. (t (if indent (newline-and-indent) (newline)))))
  11576. (defun org-return-indent ()
  11577. "Goto next table row or insert a newline and indent.
  11578. Calls `org-table-next-row' or `newline-and-indent', depending on
  11579. context. See the individual commands for more information."
  11580. (interactive)
  11581. (org-return t))
  11582. (defun org-ctrl-c-star ()
  11583. "Compute table, or change heading status of lines.
  11584. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11585. depending on context. This will also turn a plain list item or a normal
  11586. line into a subheading."
  11587. (interactive)
  11588. (cond
  11589. ((org-at-table-p)
  11590. (call-interactively 'org-table-recalculate))
  11591. ((org-region-active-p)
  11592. ;; Convert all lines in region to list items
  11593. (call-interactively 'org-toggle-region-headings))
  11594. ((org-on-heading-p)
  11595. (org-toggle-region-headings (point-at-bol)
  11596. (min (1+ (point-at-eol)) (point-max))))
  11597. ((org-at-item-p)
  11598. ;; Convert to heading
  11599. (let ((level (save-match-data
  11600. (save-excursion
  11601. (condition-case nil
  11602. (progn
  11603. (org-back-to-heading t)
  11604. (funcall outline-level))
  11605. (error 0))))))
  11606. (replace-match
  11607. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11608. (t (org-toggle-region-headings (point-at-bol)
  11609. (min (1+ (point-at-eol)) (point-max))))))
  11610. (defun org-ctrl-c-minus ()
  11611. "Insert separator line in table or modify bullet status of line.
  11612. Also turns a plain line or a region of lines into list items.
  11613. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11614. `org-cycle-list-bullet', depending on context."
  11615. (interactive)
  11616. (cond
  11617. ((org-at-table-p)
  11618. (call-interactively 'org-table-insert-hline))
  11619. ((org-on-heading-p)
  11620. ;; Convert to item
  11621. (save-excursion
  11622. (beginning-of-line 1)
  11623. (if (looking-at "\\*+ ")
  11624. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11625. ((org-region-active-p)
  11626. ;; Convert all lines in region to list items
  11627. (call-interactively 'org-toggle-region-items))
  11628. ((org-in-item-p)
  11629. (call-interactively 'org-cycle-list-bullet))
  11630. (t (org-toggle-region-items (point-at-bol)
  11631. (min (1+ (point-at-eol)) (point-max))))))
  11632. (defun org-toggle-region-items (beg end)
  11633. "Convert all lines in region to list items.
  11634. If the first line is already an item, convert all list items in the region
  11635. to normal lines."
  11636. (interactive "r")
  11637. (let (l2 l)
  11638. (save-excursion
  11639. (goto-char end)
  11640. (setq l2 (org-current-line))
  11641. (goto-char beg)
  11642. (beginning-of-line 1)
  11643. (setq l (1- (org-current-line)))
  11644. (if (org-at-item-p)
  11645. ;; We already have items, de-itemize
  11646. (while (< (setq l (1+ l)) l2)
  11647. (when (org-at-item-p)
  11648. (goto-char (match-beginning 2))
  11649. (delete-region (match-beginning 2) (match-end 2))
  11650. (and (looking-at "[ \t]+") (replace-match "")))
  11651. (beginning-of-line 2))
  11652. (while (< (setq l (1+ l)) l2)
  11653. (unless (org-at-item-p)
  11654. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11655. (replace-match "\\1- \\2")))
  11656. (beginning-of-line 2))))))
  11657. (defun org-toggle-region-headings (beg end)
  11658. "Convert all lines in region to list items.
  11659. If the first line is already an item, convert all list items in the region
  11660. to normal lines."
  11661. (interactive "r")
  11662. (let (l2 l)
  11663. (save-excursion
  11664. (goto-char end)
  11665. (setq l2 (org-current-line))
  11666. (goto-char beg)
  11667. (beginning-of-line 1)
  11668. (setq l (1- (org-current-line)))
  11669. (if (org-on-heading-p)
  11670. ;; We already have headlines, de-star them
  11671. (while (< (setq l (1+ l)) l2)
  11672. (when (org-on-heading-p t)
  11673. (and (looking-at outline-regexp) (replace-match "")))
  11674. (beginning-of-line 2))
  11675. (let* ((stars (save-excursion
  11676. (re-search-backward org-complex-heading-regexp nil t)
  11677. (or (match-string 1) "*")))
  11678. (add-stars (if org-odd-levels-only "**" "*"))
  11679. (rpl (concat stars add-stars " \\2")))
  11680. (while (< (setq l (1+ l)) l2)
  11681. (unless (org-on-heading-p)
  11682. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11683. (replace-match rpl)))
  11684. (beginning-of-line 2)))))))
  11685. (defun org-meta-return (&optional arg)
  11686. "Insert a new heading or wrap a region in a table.
  11687. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11688. See the individual commands for more information."
  11689. (interactive "P")
  11690. (cond
  11691. ((org-at-table-p)
  11692. (call-interactively 'org-table-wrap-region))
  11693. (t (call-interactively 'org-insert-heading))))
  11694. ;;; Menu entries
  11695. ;; Define the Org-mode menus
  11696. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11697. '("Tbl"
  11698. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11699. ["Next Field" org-cycle (org-at-table-p)]
  11700. ["Previous Field" org-shifttab (org-at-table-p)]
  11701. ["Next Row" org-return (org-at-table-p)]
  11702. "--"
  11703. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11704. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11705. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11706. "--"
  11707. ("Column"
  11708. ["Move Column Left" org-metaleft (org-at-table-p)]
  11709. ["Move Column Right" org-metaright (org-at-table-p)]
  11710. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11711. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11712. ("Row"
  11713. ["Move Row Up" org-metaup (org-at-table-p)]
  11714. ["Move Row Down" org-metadown (org-at-table-p)]
  11715. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11716. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11717. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11718. "--"
  11719. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11720. ("Rectangle"
  11721. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11722. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11723. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11724. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11725. "--"
  11726. ("Calculate"
  11727. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11728. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11729. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11730. "--"
  11731. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11732. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11733. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11734. "--"
  11735. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11736. "--"
  11737. ["Sum Column/Rectangle" org-table-sum
  11738. (or (org-at-table-p) (org-region-active-p))]
  11739. ["Which Column?" org-table-current-column (org-at-table-p)])
  11740. ["Debug Formulas"
  11741. org-table-toggle-formula-debugger
  11742. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11743. ["Show Col/Row Numbers"
  11744. org-table-toggle-coordinate-overlays
  11745. :style toggle
  11746. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11747. "--"
  11748. ["Create" org-table-create (and (not (org-at-table-p))
  11749. org-enable-table-editor)]
  11750. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11751. ["Import from File" org-table-import (not (org-at-table-p))]
  11752. ["Export to File" org-table-export (org-at-table-p)]
  11753. "--"
  11754. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11755. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11756. '("Org"
  11757. ("Show/Hide"
  11758. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11759. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11760. ["Sparse Tree..." org-sparse-tree t]
  11761. ["Reveal Context" org-reveal t]
  11762. ["Show All" show-all t]
  11763. "--"
  11764. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11765. "--"
  11766. ["New Heading" org-insert-heading t]
  11767. ("Navigate Headings"
  11768. ["Up" outline-up-heading t]
  11769. ["Next" outline-next-visible-heading t]
  11770. ["Previous" outline-previous-visible-heading t]
  11771. ["Next Same Level" outline-forward-same-level t]
  11772. ["Previous Same Level" outline-backward-same-level t]
  11773. "--"
  11774. ["Jump" org-goto t])
  11775. ("Edit Structure"
  11776. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11777. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11778. "--"
  11779. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11780. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11781. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11782. "--"
  11783. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11784. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11785. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11786. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11787. "--"
  11788. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11789. "--"
  11790. ["Convert to odd levels" org-convert-to-odd-levels t]
  11791. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11792. ("Editing"
  11793. ["Emphasis..." org-emphasize t]
  11794. ["Edit Source Example" org-edit-special t])
  11795. ("Archive"
  11796. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11797. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11798. ; :active t :keys "C-u C-c C-x C-a"]
  11799. ["Sparse trees open ARCHIVE trees"
  11800. (setq org-sparse-tree-open-archived-trees
  11801. (not org-sparse-tree-open-archived-trees))
  11802. :style toggle :selected org-sparse-tree-open-archived-trees]
  11803. ["Cycling opens ARCHIVE trees"
  11804. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11805. :style toggle :selected org-cycle-open-archived-trees]
  11806. "--"
  11807. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  11808. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11809. ; ["Check and Move Children" (org-archive-subtree '(4))
  11810. ; :active t :keys "C-u C-c C-x C-s"]
  11811. )
  11812. "--"
  11813. ("TODO Lists"
  11814. ["TODO/DONE/-" org-todo t]
  11815. ("Select keyword"
  11816. ["Next keyword" org-shiftright (org-on-heading-p)]
  11817. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11818. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11819. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11820. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11821. ["Show TODO Tree" org-show-todo-tree t]
  11822. ["Global TODO list" org-todo-list t]
  11823. "--"
  11824. ["Set Priority" org-priority t]
  11825. ["Priority Up" org-shiftup t]
  11826. ["Priority Down" org-shiftdown t])
  11827. ("TAGS and Properties"
  11828. ["Set Tags" 'org-set-tags-command t]
  11829. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11830. "--"
  11831. ["Set property" 'org-set-property t]
  11832. ["Column view of properties" org-columns t]
  11833. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11834. ("Dates and Scheduling"
  11835. ["Timestamp" org-time-stamp t]
  11836. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11837. ("Change Date"
  11838. ["1 Day Later" org-shiftright t]
  11839. ["1 Day Earlier" org-shiftleft t]
  11840. ["1 ... Later" org-shiftup t]
  11841. ["1 ... Earlier" org-shiftdown t])
  11842. ["Compute Time Range" org-evaluate-time-range t]
  11843. ["Schedule Item" org-schedule t]
  11844. ["Deadline" org-deadline t]
  11845. "--"
  11846. ["Custom time format" org-toggle-time-stamp-overlays
  11847. :style radio :selected org-display-custom-times]
  11848. "--"
  11849. ["Goto Calendar" org-goto-calendar t]
  11850. ["Date from Calendar" org-date-from-calendar t])
  11851. ("Logging work"
  11852. ["Clock in" org-clock-in t]
  11853. ["Clock out" org-clock-out t]
  11854. ["Clock cancel" org-clock-cancel t]
  11855. ["Goto running clock" org-clock-goto t]
  11856. ["Display times" org-clock-display t]
  11857. ["Create clock table" org-clock-report t]
  11858. "--"
  11859. ["Record DONE time"
  11860. (progn (setq org-log-done (not org-log-done))
  11861. (message "Switching to %s will %s record a timestamp"
  11862. (car org-done-keywords)
  11863. (if org-log-done "automatically" "not")))
  11864. :style toggle :selected org-log-done])
  11865. "--"
  11866. ["Agenda Command..." org-agenda t]
  11867. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11868. ("File List for Agenda")
  11869. ("Special views current file"
  11870. ["TODO Tree" org-show-todo-tree t]
  11871. ["Check Deadlines" org-check-deadlines t]
  11872. ["Timeline" org-timeline t]
  11873. ["Tags Tree" org-tags-sparse-tree t])
  11874. "--"
  11875. ("Hyperlinks"
  11876. ["Store Link (Global)" org-store-link t]
  11877. ["Insert Link" org-insert-link t]
  11878. ["Follow Link" org-open-at-point t]
  11879. "--"
  11880. ["Next link" org-next-link t]
  11881. ["Previous link" org-previous-link t]
  11882. "--"
  11883. ["Descriptive Links"
  11884. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11885. :style radio
  11886. :selected (member '(org-link) buffer-invisibility-spec)]
  11887. ["Literal Links"
  11888. (progn
  11889. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11890. :style radio
  11891. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11892. "--"
  11893. ["Export/Publish..." org-export t]
  11894. ("LaTeX"
  11895. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11896. :selected org-cdlatex-mode]
  11897. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11898. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11899. ["Modify math symbol" org-cdlatex-math-modify
  11900. (org-inside-LaTeX-fragment-p)]
  11901. ["Export LaTeX fragments as images"
  11902. (if (featurep 'org-exp)
  11903. (setq org-export-with-LaTeX-fragments
  11904. (not org-export-with-LaTeX-fragments))
  11905. (require 'org-exp))
  11906. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11907. org-export-with-LaTeX-fragments)])
  11908. "--"
  11909. ("Documentation"
  11910. ["Show Version" org-version t]
  11911. ["Info Documentation" org-info t])
  11912. ("Customize"
  11913. ["Browse Org Group" org-customize t]
  11914. "--"
  11915. ["Expand This Menu" org-create-customize-menu
  11916. (fboundp 'customize-menu-create)])
  11917. "--"
  11918. ["Refresh setup" org-mode-restart t]
  11919. ))
  11920. (defun org-info (&optional node)
  11921. "Read documentation for Org-mode in the info system.
  11922. With optional NODE, go directly to that node."
  11923. (interactive)
  11924. (info (format "(org)%s" (or node ""))))
  11925. (defun org-install-agenda-files-menu ()
  11926. (let ((bl (buffer-list)))
  11927. (save-excursion
  11928. (while bl
  11929. (set-buffer (pop bl))
  11930. (if (org-mode-p) (setq bl nil)))
  11931. (when (org-mode-p)
  11932. (easy-menu-change
  11933. '("Org") "File List for Agenda"
  11934. (append
  11935. (list
  11936. ["Edit File List" (org-edit-agenda-file-list) t]
  11937. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11938. ["Remove Current File from List" org-remove-file t]
  11939. ["Cycle through agenda files" org-cycle-agenda-files t]
  11940. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11941. "--")
  11942. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11943. ;;;; Documentation
  11944. ;;;###autoload
  11945. (defun org-require-autoloaded-modules ()
  11946. (interactive)
  11947. (mapc 'require
  11948. '(org-agenda org-archive org-clock org-colview
  11949. org-exp org-id org-export-latex org-publish
  11950. org-remember org-table)))
  11951. ;;;###autoload
  11952. (defun org-customize ()
  11953. "Call the customize function with org as argument."
  11954. (interactive)
  11955. (org-load-modules-maybe)
  11956. (org-require-autoloaded-modules)
  11957. (customize-browse 'org))
  11958. (defun org-create-customize-menu ()
  11959. "Create a full customization menu for Org-mode, insert it into the menu."
  11960. (interactive)
  11961. (org-load-modules-maybe)
  11962. (org-require-autoloaded-modules)
  11963. (if (fboundp 'customize-menu-create)
  11964. (progn
  11965. (easy-menu-change
  11966. '("Org") "Customize"
  11967. `(["Browse Org group" org-customize t]
  11968. "--"
  11969. ,(customize-menu-create 'org)
  11970. ["Set" Custom-set t]
  11971. ["Save" Custom-save t]
  11972. ["Reset to Current" Custom-reset-current t]
  11973. ["Reset to Saved" Custom-reset-saved t]
  11974. ["Reset to Standard Settings" Custom-reset-standard t]))
  11975. (message "\"Org\"-menu now contains full customization menu"))
  11976. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11977. ;;;; Miscellaneous stuff
  11978. ;;; Generally useful functions
  11979. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11980. "Display the given MESSAGE as a warning."
  11981. (if (fboundp 'display-warning)
  11982. (display-warning 'org message
  11983. (if (featurep 'xemacs)
  11984. 'warning
  11985. :warning))
  11986. (let ((buf (get-buffer-create "*Org warnings*")))
  11987. (with-current-buffer buf
  11988. (goto-char (point-max))
  11989. (insert "Warning (Org): " message)
  11990. (unless (bolp)
  11991. (newline)))
  11992. (display-buffer buf)
  11993. (sit-for 0))))
  11994. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  11995. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  11996. (if (and marker (marker-buffer marker)
  11997. (buffer-live-p (marker-buffer marker)))
  11998. (progn
  11999. (switch-to-buffer (marker-buffer marker))
  12000. (if (or (> marker (point-max)) (< marker (point-min)))
  12001. (widen))
  12002. (goto-char marker))
  12003. (if bookmark
  12004. (bookmark-jump bookmark)
  12005. (error "Cannot find location"))))
  12006. (defun org-quote-csv-field (s)
  12007. "Quote field for inclusion in CSV material."
  12008. (if (string-match "[\",]" s)
  12009. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12010. s))
  12011. (defun org-plist-delete (plist property)
  12012. "Delete PROPERTY from PLIST.
  12013. This is in contrast to merely setting it to 0."
  12014. (let (p)
  12015. (while plist
  12016. (if (not (eq property (car plist)))
  12017. (setq p (plist-put p (car plist) (nth 1 plist))))
  12018. (setq plist (cddr plist)))
  12019. p))
  12020. (defun org-force-self-insert (N)
  12021. "Needed to enforce self-insert under remapping."
  12022. (interactive "p")
  12023. (self-insert-command N))
  12024. (defun org-string-width (s)
  12025. "Compute width of string, ignoring invisible characters.
  12026. This ignores character with invisibility property `org-link', and also
  12027. characters with property `org-cwidth', because these will become invisible
  12028. upon the next fontification round."
  12029. (let (b l)
  12030. (when (or (eq t buffer-invisibility-spec)
  12031. (assq 'org-link buffer-invisibility-spec))
  12032. (while (setq b (text-property-any 0 (length s)
  12033. 'invisible 'org-link s))
  12034. (setq s (concat (substring s 0 b)
  12035. (substring s (or (next-single-property-change
  12036. b 'invisible s) (length s)))))))
  12037. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12038. (setq s (concat (substring s 0 b)
  12039. (substring s (or (next-single-property-change
  12040. b 'org-cwidth s) (length s))))))
  12041. (setq l (string-width s) b -1)
  12042. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12043. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12044. l))
  12045. (defun org-get-indentation (&optional line)
  12046. "Get the indentation of the current line, interpreting tabs.
  12047. When LINE is given, assume it represents a line and compute its indentation."
  12048. (if line
  12049. (if (string-match "^ *" (org-remove-tabs line))
  12050. (match-end 0))
  12051. (save-excursion
  12052. (beginning-of-line 1)
  12053. (skip-chars-forward " \t")
  12054. (current-column))))
  12055. (defun org-remove-tabs (s &optional width)
  12056. "Replace tabulators in S with spaces.
  12057. Assumes that s is a single line, starting in column 0."
  12058. (setq width (or width tab-width))
  12059. (while (string-match "\t" s)
  12060. (setq s (replace-match
  12061. (make-string
  12062. (- (* width (/ (+ (match-beginning 0) width) width))
  12063. (match-beginning 0)) ?\ )
  12064. t t s)))
  12065. s)
  12066. (defun org-fix-indentation (line ind)
  12067. "Fix indentation in LINE.
  12068. IND is a cons cell with target and minimum indentation.
  12069. If the current indenation in LINE is smaller than the minimum,
  12070. leave it alone. If it is larger than ind, set it to the target."
  12071. (let* ((l (org-remove-tabs line))
  12072. (i (org-get-indentation l))
  12073. (i1 (car ind)) (i2 (cdr ind)))
  12074. (if (>= i i2) (setq l (substring line i2)))
  12075. (if (> i1 0)
  12076. (concat (make-string i1 ?\ ) l)
  12077. l)))
  12078. (defun org-base-buffer (buffer)
  12079. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12080. (if (not buffer)
  12081. buffer
  12082. (or (buffer-base-buffer buffer)
  12083. buffer)))
  12084. (defun org-trim (s)
  12085. "Remove whitespace at beginning and end of string."
  12086. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12087. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12088. s)
  12089. (defun org-wrap (string &optional width lines)
  12090. "Wrap string to either a number of lines, or a width in characters.
  12091. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12092. that costs. If there is a word longer than WIDTH, the text is actually
  12093. wrapped to the length of that word.
  12094. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12095. many lines, whatever width that takes.
  12096. The return value is a list of lines, without newlines at the end."
  12097. (let* ((words (org-split-string string "[ \t\n]+"))
  12098. (maxword (apply 'max (mapcar 'org-string-width words)))
  12099. w ll)
  12100. (cond (width
  12101. (org-do-wrap words (max maxword width)))
  12102. (lines
  12103. (setq w maxword)
  12104. (setq ll (org-do-wrap words maxword))
  12105. (if (<= (length ll) lines)
  12106. ll
  12107. (setq ll words)
  12108. (while (> (length ll) lines)
  12109. (setq w (1+ w))
  12110. (setq ll (org-do-wrap words w)))
  12111. ll))
  12112. (t (error "Cannot wrap this")))))
  12113. (defun org-do-wrap (words width)
  12114. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12115. (let (lines line)
  12116. (while words
  12117. (setq line (pop words))
  12118. (while (and words (< (+ (length line) (length (car words))) width))
  12119. (setq line (concat line " " (pop words))))
  12120. (setq lines (push line lines)))
  12121. (nreverse lines)))
  12122. (defun org-split-string (string &optional separators)
  12123. "Splits STRING into substrings at SEPARATORS.
  12124. No empty strings are returned if there are matches at the beginning
  12125. and end of string."
  12126. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12127. (start 0)
  12128. notfirst
  12129. (list nil))
  12130. (while (and (string-match rexp string
  12131. (if (and notfirst
  12132. (= start (match-beginning 0))
  12133. (< start (length string)))
  12134. (1+ start) start))
  12135. (< (match-beginning 0) (length string)))
  12136. (setq notfirst t)
  12137. (or (eq (match-beginning 0) 0)
  12138. (and (eq (match-beginning 0) (match-end 0))
  12139. (eq (match-beginning 0) start))
  12140. (setq list
  12141. (cons (substring string start (match-beginning 0))
  12142. list)))
  12143. (setq start (match-end 0)))
  12144. (or (eq start (length string))
  12145. (setq list
  12146. (cons (substring string start)
  12147. list)))
  12148. (nreverse list)))
  12149. (defun org-context ()
  12150. "Return a list of contexts of the current cursor position.
  12151. If several contexts apply, all are returned.
  12152. Each context entry is a list with a symbol naming the context, and
  12153. two positions indicating start and end of the context. Possible
  12154. contexts are:
  12155. :headline anywhere in a headline
  12156. :headline-stars on the leading stars in a headline
  12157. :todo-keyword on a TODO keyword (including DONE) in a headline
  12158. :tags on the TAGS in a headline
  12159. :priority on the priority cookie in a headline
  12160. :item on the first line of a plain list item
  12161. :item-bullet on the bullet/number of a plain list item
  12162. :checkbox on the checkbox in a plain list item
  12163. :table in an org-mode table
  12164. :table-special on a special filed in a table
  12165. :table-table in a table.el table
  12166. :link on a hyperlink
  12167. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12168. :target on a <<target>>
  12169. :radio-target on a <<<radio-target>>>
  12170. :latex-fragment on a LaTeX fragment
  12171. :latex-preview on a LaTeX fragment with overlayed preview image
  12172. This function expects the position to be visible because it uses font-lock
  12173. faces as a help to recognize the following contexts: :table-special, :link,
  12174. and :keyword."
  12175. (let* ((f (get-text-property (point) 'face))
  12176. (faces (if (listp f) f (list f)))
  12177. (p (point)) clist o)
  12178. ;; First the large context
  12179. (cond
  12180. ((org-on-heading-p t)
  12181. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12182. (when (progn
  12183. (beginning-of-line 1)
  12184. (looking-at org-todo-line-tags-regexp))
  12185. (push (org-point-in-group p 1 :headline-stars) clist)
  12186. (push (org-point-in-group p 2 :todo-keyword) clist)
  12187. (push (org-point-in-group p 4 :tags) clist))
  12188. (goto-char p)
  12189. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12190. (if (looking-at "\\[#[A-Z0-9]\\]")
  12191. (push (org-point-in-group p 0 :priority) clist)))
  12192. ((org-at-item-p)
  12193. (push (org-point-in-group p 2 :item-bullet) clist)
  12194. (push (list :item (point-at-bol)
  12195. (save-excursion (org-end-of-item) (point)))
  12196. clist)
  12197. (and (org-at-item-checkbox-p)
  12198. (push (org-point-in-group p 0 :checkbox) clist)))
  12199. ((org-at-table-p)
  12200. (push (list :table (org-table-begin) (org-table-end)) clist)
  12201. (if (memq 'org-formula faces)
  12202. (push (list :table-special
  12203. (previous-single-property-change p 'face)
  12204. (next-single-property-change p 'face)) clist)))
  12205. ((org-at-table-p 'any)
  12206. (push (list :table-table) clist)))
  12207. (goto-char p)
  12208. ;; Now the small context
  12209. (cond
  12210. ((org-at-timestamp-p)
  12211. (push (org-point-in-group p 0 :timestamp) clist))
  12212. ((memq 'org-link faces)
  12213. (push (list :link
  12214. (previous-single-property-change p 'face)
  12215. (next-single-property-change p 'face)) clist))
  12216. ((memq 'org-special-keyword faces)
  12217. (push (list :keyword
  12218. (previous-single-property-change p 'face)
  12219. (next-single-property-change p 'face)) clist))
  12220. ((org-on-target-p)
  12221. (push (org-point-in-group p 0 :target) clist)
  12222. (goto-char (1- (match-beginning 0)))
  12223. (if (looking-at org-radio-target-regexp)
  12224. (push (org-point-in-group p 0 :radio-target) clist))
  12225. (goto-char p))
  12226. ((setq o (car (delq nil
  12227. (mapcar
  12228. (lambda (x)
  12229. (if (memq x org-latex-fragment-image-overlays) x))
  12230. (org-overlays-at (point))))))
  12231. (push (list :latex-fragment
  12232. (org-overlay-start o) (org-overlay-end o)) clist)
  12233. (push (list :latex-preview
  12234. (org-overlay-start o) (org-overlay-end o)) clist))
  12235. ((org-inside-LaTeX-fragment-p)
  12236. ;; FIXME: positions wrong.
  12237. (push (list :latex-fragment (point) (point)) clist)))
  12238. (setq clist (nreverse (delq nil clist)))
  12239. clist))
  12240. ;; FIXME: Compare with at-regexp-p Do we need both?
  12241. (defun org-in-regexp (re &optional nlines visually)
  12242. "Check if point is inside a match of regexp.
  12243. Normally only the current line is checked, but you can include NLINES extra
  12244. lines both before and after point into the search.
  12245. If VISUALLY is set, require that the cursor is not after the match but
  12246. really on, so that the block visually is on the match."
  12247. (catch 'exit
  12248. (let ((pos (point))
  12249. (eol (point-at-eol (+ 1 (or nlines 0))))
  12250. (inc (if visually 1 0)))
  12251. (save-excursion
  12252. (beginning-of-line (- 1 (or nlines 0)))
  12253. (while (re-search-forward re eol t)
  12254. (if (and (<= (match-beginning 0) pos)
  12255. (>= (+ inc (match-end 0)) pos))
  12256. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12257. (defun org-at-regexp-p (regexp)
  12258. "Is point inside a match of REGEXP in the current line?"
  12259. (catch 'exit
  12260. (save-excursion
  12261. (let ((pos (point)) (end (point-at-eol)))
  12262. (beginning-of-line 1)
  12263. (while (re-search-forward regexp end t)
  12264. (if (and (<= (match-beginning 0) pos)
  12265. (>= (match-end 0) pos))
  12266. (throw 'exit t)))
  12267. nil))))
  12268. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12269. "Call `multi-occur' with buffers for all agenda files."
  12270. (interactive "sOrg-files matching: \np")
  12271. (let* ((files (org-agenda-files))
  12272. (tnames (mapcar 'file-truename files))
  12273. (extra org-agenda-text-search-extra-files)
  12274. f)
  12275. (when (eq (car extra) 'agenda-archives)
  12276. (setq extra (cdr extra))
  12277. (setq files (org-add-archive-files files)))
  12278. (while (setq f (pop extra))
  12279. (unless (member (file-truename f) tnames)
  12280. (add-to-list 'files f 'append)
  12281. (add-to-list 'tnames (file-truename f) 'append)))
  12282. (multi-occur
  12283. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12284. regexp)))
  12285. (if (boundp 'occur-mode-find-occurrence-hook)
  12286. ;; Emacs 23
  12287. (add-hook 'occur-mode-find-occurrence-hook
  12288. (lambda ()
  12289. (when (org-mode-p)
  12290. (org-reveal))))
  12291. ;; Emacs 22
  12292. (defadvice occur-mode-goto-occurrence
  12293. (after org-occur-reveal activate)
  12294. (and (org-mode-p) (org-reveal)))
  12295. (defadvice occur-mode-goto-occurrence-other-window
  12296. (after org-occur-reveal activate)
  12297. (and (org-mode-p) (org-reveal)))
  12298. (defadvice occur-mode-display-occurrence
  12299. (after org-occur-reveal activate)
  12300. (when (org-mode-p)
  12301. (let ((pos (occur-mode-find-occurrence)))
  12302. (with-current-buffer (marker-buffer pos)
  12303. (save-excursion
  12304. (goto-char pos)
  12305. (org-reveal)))))))
  12306. (defun org-uniquify (list)
  12307. "Remove duplicate elements from LIST."
  12308. (let (res)
  12309. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12310. res))
  12311. (defun org-delete-all (elts list)
  12312. "Remove all elements in ELTS from LIST."
  12313. (while elts
  12314. (setq list (delete (pop elts) list)))
  12315. list)
  12316. (defun org-back-over-empty-lines ()
  12317. "Move backwards over witespace, to the beginning of the first empty line.
  12318. Returns the number of empty lines passed."
  12319. (let ((pos (point)))
  12320. (skip-chars-backward " \t\n\r")
  12321. (beginning-of-line 2)
  12322. (goto-char (min (point) pos))
  12323. (count-lines (point) pos)))
  12324. (defun org-skip-whitespace ()
  12325. (skip-chars-forward " \t\n\r"))
  12326. (defun org-point-in-group (point group &optional context)
  12327. "Check if POINT is in match-group GROUP.
  12328. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12329. match. If the match group does ot exist or point is not inside it,
  12330. return nil."
  12331. (and (match-beginning group)
  12332. (>= point (match-beginning group))
  12333. (<= point (match-end group))
  12334. (if context
  12335. (list context (match-beginning group) (match-end group))
  12336. t)))
  12337. (defun org-switch-to-buffer-other-window (&rest args)
  12338. "Switch to buffer in a second window on the current frame.
  12339. In particular, do not allow pop-up frames."
  12340. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12341. special-display-function)
  12342. (apply 'switch-to-buffer-other-window args)))
  12343. (defun org-combine-plists (&rest plists)
  12344. "Create a single property list from all plists in PLISTS.
  12345. The process starts by copying the first list, and then setting properties
  12346. from the other lists. Settings in the last list are the most significant
  12347. ones and overrule settings in the other lists."
  12348. (let ((rtn (copy-sequence (pop plists)))
  12349. p v ls)
  12350. (while plists
  12351. (setq ls (pop plists))
  12352. (while ls
  12353. (setq p (pop ls) v (pop ls))
  12354. (setq rtn (plist-put rtn p v))))
  12355. rtn))
  12356. (defun org-move-line-down (arg)
  12357. "Move the current line down. With prefix argument, move it past ARG lines."
  12358. (interactive "p")
  12359. (let ((col (current-column))
  12360. beg end pos)
  12361. (beginning-of-line 1) (setq beg (point))
  12362. (beginning-of-line 2) (setq end (point))
  12363. (beginning-of-line (+ 1 arg))
  12364. (setq pos (move-marker (make-marker) (point)))
  12365. (insert (delete-and-extract-region beg end))
  12366. (goto-char pos)
  12367. (org-move-to-column col)))
  12368. (defun org-move-line-up (arg)
  12369. "Move the current line up. With prefix argument, move it past ARG lines."
  12370. (interactive "p")
  12371. (let ((col (current-column))
  12372. beg end pos)
  12373. (beginning-of-line 1) (setq beg (point))
  12374. (beginning-of-line 2) (setq end (point))
  12375. (beginning-of-line (- arg))
  12376. (setq pos (move-marker (make-marker) (point)))
  12377. (insert (delete-and-extract-region beg end))
  12378. (goto-char pos)
  12379. (org-move-to-column col)))
  12380. (defun org-replace-escapes (string table)
  12381. "Replace %-escapes in STRING with values in TABLE.
  12382. TABLE is an association list with keys like \"%a\" and string values.
  12383. The sequences in STRING may contain normal field width and padding information,
  12384. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12385. so values can contain further %-escapes if they are define later in TABLE."
  12386. (let ((case-fold-search nil)
  12387. e re rpl)
  12388. (while (setq e (pop table))
  12389. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12390. (while (string-match re string)
  12391. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12392. (cdr e)))
  12393. (setq string (replace-match rpl t t string))))
  12394. string))
  12395. (defun org-sublist (list start end)
  12396. "Return a section of LIST, from START to END.
  12397. Counting starts at 1."
  12398. (let (rtn (c start))
  12399. (setq list (nthcdr (1- start) list))
  12400. (while (and list (<= c end))
  12401. (push (pop list) rtn)
  12402. (setq c (1+ c)))
  12403. (nreverse rtn)))
  12404. (defun org-find-base-buffer-visiting (file)
  12405. "Like `find-buffer-visiting' but alway return the base buffer and
  12406. not an indirect buffer."
  12407. (let ((buf (find-buffer-visiting file)))
  12408. (if buf
  12409. (or (buffer-base-buffer buf) buf)
  12410. nil)))
  12411. (defun org-image-file-name-regexp ()
  12412. "Return regexp matching the file names of images."
  12413. (if (fboundp 'image-file-name-regexp)
  12414. (image-file-name-regexp)
  12415. (let ((image-file-name-extensions
  12416. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12417. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12418. (concat "\\."
  12419. (regexp-opt (nconc (mapcar 'upcase
  12420. image-file-name-extensions)
  12421. image-file-name-extensions)
  12422. t)
  12423. "\\'"))))
  12424. (defun org-file-image-p (file)
  12425. "Return non-nil if FILE is an image."
  12426. (save-match-data
  12427. (string-match (org-image-file-name-regexp) file)))
  12428. (defun org-get-cursor-date ()
  12429. "Return the date at cursor in as a time.
  12430. This works in the calendar and in the agenda, anywhere else it just
  12431. returns the current time."
  12432. (let (date day defd)
  12433. (cond
  12434. ((eq major-mode 'calendar-mode)
  12435. (setq date (calendar-cursor-to-date)
  12436. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12437. ((eq major-mode 'org-agenda-mode)
  12438. (setq day (get-text-property (point) 'day))
  12439. (if day
  12440. (setq date (calendar-gregorian-from-absolute day)
  12441. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12442. (nth 2 date))))))
  12443. (or defd (current-time))))
  12444. (defvar org-agenda-action-marker (make-marker)
  12445. "Marker pointing to the entry for the next agenda action.")
  12446. (defun org-mark-entry-for-agenda-action ()
  12447. "Mark the current entry as target of an agenda action.
  12448. Agenda actions are actions executed from the agenda with the key `k',
  12449. which make use of the date at the cursor."
  12450. (interactive)
  12451. (move-marker org-agenda-action-marker
  12452. (save-excursion (org-back-to-heading t) (point))
  12453. (current-buffer))
  12454. (message
  12455. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12456. ;;; Paragraph filling stuff.
  12457. ;; We want this to be just right, so use the full arsenal.
  12458. (defun org-indent-line-function ()
  12459. "Indent line like previous, but further if previous was headline or item."
  12460. (interactive)
  12461. (let* ((pos (point))
  12462. (itemp (org-at-item-p))
  12463. column bpos bcol tpos tcol bullet btype bullet-type)
  12464. ;; Find the previous relevant line
  12465. (beginning-of-line 1)
  12466. (cond
  12467. ((looking-at "#") (setq column 0))
  12468. ((looking-at "\\*+ ") (setq column 0))
  12469. (t
  12470. (beginning-of-line 0)
  12471. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12472. (beginning-of-line 0))
  12473. (cond
  12474. ((looking-at "\\*+[ \t]+")
  12475. (if (not org-adapt-indentation)
  12476. (setq column 0)
  12477. (goto-char (match-end 0))
  12478. (setq column (current-column))))
  12479. ((org-in-item-p)
  12480. (org-beginning-of-item)
  12481. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12482. (setq bpos (match-beginning 1) tpos (match-end 0)
  12483. bcol (progn (goto-char bpos) (current-column))
  12484. tcol (progn (goto-char tpos) (current-column))
  12485. bullet (match-string 1)
  12486. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12487. (if (> tcol (+ bcol org-description-max-indent))
  12488. (setq tcol (+ bcol 5)))
  12489. (if (not itemp)
  12490. (setq column tcol)
  12491. (goto-char pos)
  12492. (beginning-of-line 1)
  12493. (if (looking-at "\\S-")
  12494. (progn
  12495. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12496. (setq bullet (match-string 1)
  12497. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12498. (setq column (if (equal btype bullet-type) bcol tcol)))
  12499. (setq column (org-get-indentation)))))
  12500. (t (setq column (org-get-indentation))))))
  12501. (goto-char pos)
  12502. (if (<= (current-column) (current-indentation))
  12503. (org-indent-line-to column)
  12504. (save-excursion (org-indent-line-to column)))
  12505. (setq column (current-column))
  12506. (beginning-of-line 1)
  12507. (if (looking-at
  12508. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12509. (replace-match (concat "\\1" (format org-property-format
  12510. (match-string 2) (match-string 3)))
  12511. t nil))
  12512. (org-move-to-column column)))
  12513. (defun org-set-autofill-regexps ()
  12514. (interactive)
  12515. ;; In the paragraph separator we include headlines, because filling
  12516. ;; text in a line directly attached to a headline would otherwise
  12517. ;; fill the headline as well.
  12518. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12519. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12520. ;; The paragraph starter includes hand-formatted lists.
  12521. (org-set-local 'paragraph-start
  12522. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12523. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12524. ;; But only if the user has not turned off tables or fixed-width regions
  12525. (org-set-local
  12526. 'auto-fill-inhibit-regexp
  12527. (concat "\\*+ \\|#\\+"
  12528. "\\|[ \t]*" org-keyword-time-regexp
  12529. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12530. (concat
  12531. "\\|[ \t]*["
  12532. (if org-enable-table-editor "|" "")
  12533. (if org-enable-fixed-width-editor ":" "")
  12534. "]"))))
  12535. ;; We use our own fill-paragraph function, to make sure that tables
  12536. ;; and fixed-width regions are not wrapped. That function will pass
  12537. ;; through to `fill-paragraph' when appropriate.
  12538. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12539. ; Adaptive filling: To get full control, first make sure that
  12540. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12541. (org-set-local 'adaptive-fill-regexp "\000")
  12542. (org-set-local 'adaptive-fill-function
  12543. 'org-adaptive-fill-function)
  12544. (org-set-local
  12545. 'align-mode-rules-list
  12546. '((org-in-buffer-settings
  12547. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12548. (modes . '(org-mode))))))
  12549. (defun org-fill-paragraph (&optional justify)
  12550. "Re-align a table, pass through to fill-paragraph if no table."
  12551. (let ((table-p (org-at-table-p))
  12552. (table.el-p (org-at-table.el-p)))
  12553. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12554. (save-excursion (goto-char (point-at-bol))
  12555. (looking-at outline-regexp)))
  12556. t) ; skip headlines
  12557. (table.el-p t) ; skip table.el tables
  12558. (table-p (org-table-align) t) ; align org-mode tables
  12559. (t nil)))) ; call paragraph-fill
  12560. ;; For reference, this is the default value of adaptive-fill-regexp
  12561. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12562. (defun org-adaptive-fill-function ()
  12563. "Return a fill prefix for org-mode files.
  12564. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12565. work correctly."
  12566. (cond ((looking-at "#[ \t]+")
  12567. (match-string 0))
  12568. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12569. (save-excursion
  12570. (if (> (match-end 1) (+ (match-beginning 1)
  12571. org-description-max-indent))
  12572. (goto-char (+ (match-beginning 1) 5))
  12573. (goto-char (match-end 0)))
  12574. (make-string (current-column) ?\ )))
  12575. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12576. (save-excursion
  12577. (goto-char (match-end 0))
  12578. (make-string (current-column) ?\ )))
  12579. (t nil)))
  12580. ;;; Other stuff.
  12581. (defun org-toggle-fixed-width-section (arg)
  12582. "Toggle the fixed-width export.
  12583. If there is no active region, the QUOTE keyword at the current headline is
  12584. inserted or removed. When present, it causes the text between this headline
  12585. and the next to be exported as fixed-width text, and unmodified.
  12586. If there is an active region, this command adds or removes a colon as the
  12587. first character of this line. If the first character of a line is a colon,
  12588. this line is also exported in fixed-width font."
  12589. (interactive "P")
  12590. (let* ((cc 0)
  12591. (regionp (org-region-active-p))
  12592. (beg (if regionp (region-beginning) (point)))
  12593. (end (if regionp (region-end)))
  12594. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12595. (case-fold-search nil)
  12596. (re "[ \t]*\\(:\\)")
  12597. off)
  12598. (if regionp
  12599. (save-excursion
  12600. (goto-char beg)
  12601. (setq cc (current-column))
  12602. (beginning-of-line 1)
  12603. (setq off (looking-at re))
  12604. (while (> nlines 0)
  12605. (setq nlines (1- nlines))
  12606. (beginning-of-line 1)
  12607. (cond
  12608. (arg
  12609. (org-move-to-column cc t)
  12610. (insert ":\n")
  12611. (forward-line -1))
  12612. ((and off (looking-at re))
  12613. (replace-match "" t t nil 1))
  12614. ((not off) (org-move-to-column cc t) (insert ":")))
  12615. (forward-line 1)))
  12616. (save-excursion
  12617. (org-back-to-heading)
  12618. (if (looking-at (concat outline-regexp
  12619. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12620. (replace-match "" t t nil 1)
  12621. (if (looking-at outline-regexp)
  12622. (progn
  12623. (goto-char (match-end 0))
  12624. (insert org-quote-string " "))))))))
  12625. ;;;; Functions extending outline functionality
  12626. (defun org-beginning-of-line (&optional arg)
  12627. "Go to the beginning of the current line. If that is invisible, continue
  12628. to a visible line beginning. This makes the function of C-a more intuitive.
  12629. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12630. first attempt, and only move to after the tags when the cursor is already
  12631. beyond the end of the headline."
  12632. (interactive "P")
  12633. (let ((pos (point)) refpos)
  12634. (beginning-of-line 1)
  12635. (if (bobp)
  12636. nil
  12637. (backward-char 1)
  12638. (if (org-invisible-p)
  12639. (while (and (not (bobp)) (org-invisible-p))
  12640. (backward-char 1)
  12641. (beginning-of-line 1))
  12642. (forward-char 1)))
  12643. (when org-special-ctrl-a/e
  12644. (cond
  12645. ((and (looking-at org-complex-heading-regexp)
  12646. (= (char-after (match-end 1)) ?\ ))
  12647. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12648. (point-at-eol)))
  12649. (goto-char
  12650. (if (eq org-special-ctrl-a/e t)
  12651. (cond ((> pos refpos) refpos)
  12652. ((= pos (point)) refpos)
  12653. (t (point)))
  12654. (cond ((> pos (point)) (point))
  12655. ((not (eq last-command this-command)) (point))
  12656. (t refpos)))))
  12657. ((org-at-item-p)
  12658. (goto-char
  12659. (if (eq org-special-ctrl-a/e t)
  12660. (cond ((> pos (match-end 4)) (match-end 4))
  12661. ((= pos (point)) (match-end 4))
  12662. (t (point)))
  12663. (cond ((> pos (point)) (point))
  12664. ((not (eq last-command this-command)) (point))
  12665. (t (match-end 4))))))))
  12666. (org-no-warnings
  12667. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12668. (defun org-end-of-line (&optional arg)
  12669. "Go to the end of the line.
  12670. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12671. first attempt, and only move to after the tags when the cursor is already
  12672. beyond the end of the headline."
  12673. (interactive "P")
  12674. (if (or (not org-special-ctrl-a/e)
  12675. (not (org-on-heading-p)))
  12676. (end-of-line arg)
  12677. (let ((pos (point)))
  12678. (beginning-of-line 1)
  12679. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12680. (if (eq org-special-ctrl-a/e t)
  12681. (if (or (< pos (match-beginning 1))
  12682. (= pos (match-end 0)))
  12683. (goto-char (match-beginning 1))
  12684. (goto-char (match-end 0)))
  12685. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12686. (goto-char (match-end 0))
  12687. (goto-char (match-beginning 1))))
  12688. (end-of-line arg))))
  12689. (org-no-warnings
  12690. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12691. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12692. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12693. (defun org-kill-line (&optional arg)
  12694. "Kill line, to tags or end of line."
  12695. (interactive "P")
  12696. (cond
  12697. ((or (not org-special-ctrl-k)
  12698. (bolp)
  12699. (not (org-on-heading-p)))
  12700. (call-interactively 'kill-line))
  12701. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12702. (kill-region (point) (match-beginning 1))
  12703. (org-set-tags nil t))
  12704. (t (kill-region (point) (point-at-eol)))))
  12705. (define-key org-mode-map "\C-k" 'org-kill-line)
  12706. (defun org-yank ()
  12707. "Yank, and if the yanked text is a single subtree, fold it.
  12708. In fact, if the yanked text is a sequence of subtrees, fold all of them."
  12709. (interactive)
  12710. (if org-yank-folded-subtrees
  12711. (let ((beg (point)) end)
  12712. (call-interactively 'yank)
  12713. (setq end (point))
  12714. (goto-char beg)
  12715. (when (and (bolp)
  12716. (org-kill-is-subtree-p))
  12717. (or (looking-at outline-regexp)
  12718. (re-search-forward (concat "^" outline-regexp) end t))
  12719. (while (and (< (point) end) (looking-at outline-regexp))
  12720. (hide-subtree)
  12721. (org-cycle-show-empty-lines 'folded)
  12722. (condition-case nil
  12723. (outline-forward-same-level 1)
  12724. (error (goto-char end)))))
  12725. (goto-char end)
  12726. (skip-chars-forward " \t\n\r"))
  12727. (call-interactively 'yank)))
  12728. (define-key org-mode-map "\C-y" 'org-yank)
  12729. (defun org-invisible-p ()
  12730. "Check if point is at a character currently not visible."
  12731. ;; Early versions of noutline don't have `outline-invisible-p'.
  12732. (if (fboundp 'outline-invisible-p)
  12733. (outline-invisible-p)
  12734. (get-char-property (point) 'invisible)))
  12735. (defun org-invisible-p2 ()
  12736. "Check if point is at a character currently not visible."
  12737. (save-excursion
  12738. (if (and (eolp) (not (bobp))) (backward-char 1))
  12739. ;; Early versions of noutline don't have `outline-invisible-p'.
  12740. (if (fboundp 'outline-invisible-p)
  12741. (outline-invisible-p)
  12742. (get-char-property (point) 'invisible))))
  12743. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12744. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12745. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12746. (defun org-at-heading-or-item-p ()
  12747. (or (org-on-heading-p) (org-at-item-p)))
  12748. (defun org-on-target-p ()
  12749. (or (org-in-regexp org-radio-target-regexp)
  12750. (org-in-regexp org-target-regexp)))
  12751. (defun org-up-heading-all (arg)
  12752. "Move to the heading line of which the present line is a subheading.
  12753. This function considers both visible and invisible heading lines.
  12754. With argument, move up ARG levels."
  12755. (if (fboundp 'outline-up-heading-all)
  12756. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12757. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12758. (defun org-up-heading-safe ()
  12759. "Move to the heading line of which the present line is a subheading.
  12760. This version will not throw an error. It will return the level of the
  12761. headline found, or nil if no higher level is found."
  12762. (let ((pos (point)) start-level level
  12763. (re (concat "^" outline-regexp)))
  12764. (catch 'exit
  12765. (outline-back-to-heading t)
  12766. (setq start-level (funcall outline-level))
  12767. (if (equal start-level 1) (throw 'exit nil))
  12768. (while (re-search-backward re nil t)
  12769. (setq level (funcall outline-level))
  12770. (if (< level start-level) (throw 'exit level)))
  12771. nil)))
  12772. (defun org-first-sibling-p ()
  12773. "Is this heading the first child of its parents?"
  12774. (interactive)
  12775. (let ((re (concat "^" outline-regexp))
  12776. level l)
  12777. (unless (org-at-heading-p t)
  12778. (error "Not at a heading"))
  12779. (setq level (funcall outline-level))
  12780. (save-excursion
  12781. (if (not (re-search-backward re nil t))
  12782. t
  12783. (setq l (funcall outline-level))
  12784. (< l level)))))
  12785. (defun org-goto-sibling (&optional previous)
  12786. "Goto the next sibling, even if it is invisible.
  12787. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12788. when a sibling was found. When none is found, return nil and don't
  12789. move point."
  12790. (let ((fun (if previous 're-search-backward 're-search-forward))
  12791. (pos (point))
  12792. (re (concat "^" outline-regexp))
  12793. level l)
  12794. (when (condition-case nil (org-back-to-heading t) (error nil))
  12795. (setq level (funcall outline-level))
  12796. (catch 'exit
  12797. (or previous (forward-char 1))
  12798. (while (funcall fun re nil t)
  12799. (setq l (funcall outline-level))
  12800. (when (< l level) (goto-char pos) (throw 'exit nil))
  12801. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12802. (goto-char pos)
  12803. nil))))
  12804. (defun org-show-siblings ()
  12805. "Show all siblings of the current headline."
  12806. (save-excursion
  12807. (while (org-goto-sibling) (org-flag-heading nil)))
  12808. (save-excursion
  12809. (while (org-goto-sibling 'previous)
  12810. (org-flag-heading nil))))
  12811. (defun org-show-hidden-entry ()
  12812. "Show an entry where even the heading is hidden."
  12813. (save-excursion
  12814. (org-show-entry)))
  12815. (defun org-flag-heading (flag &optional entry)
  12816. "Flag the current heading. FLAG non-nil means make invisible.
  12817. When ENTRY is non-nil, show the entire entry."
  12818. (save-excursion
  12819. (org-back-to-heading t)
  12820. ;; Check if we should show the entire entry
  12821. (if entry
  12822. (progn
  12823. (org-show-entry)
  12824. (save-excursion
  12825. (and (outline-next-heading)
  12826. (org-flag-heading nil))))
  12827. (outline-flag-region (max (point-min) (1- (point)))
  12828. (save-excursion (outline-end-of-heading) (point))
  12829. flag))))
  12830. (defun org-forward-same-level (arg)
  12831. "Move forward to the ARG'th subheading at same level as this one.
  12832. Stop at the first and last subheadings of a superior heading.
  12833. This is like outline-forward-same-level, but invisible headings are ok."
  12834. (interactive "p")
  12835. (outline-back-to-heading t)
  12836. (while (> arg 0)
  12837. (let ((point-to-move-to (save-excursion
  12838. (org-get-next-sibling))))
  12839. (if point-to-move-to
  12840. (progn
  12841. (goto-char point-to-move-to)
  12842. (setq arg (1- arg)))
  12843. (progn
  12844. (setq arg 0)
  12845. (error "No following same-level heading"))))))
  12846. (defun org-get-next-sibling ()
  12847. "Move to next heading of the same level, and return point.
  12848. If there is no such heading, return nil.
  12849. This is like outline-next-sibling, but invisible headings are ok."
  12850. (let ((level (funcall outline-level)))
  12851. (outline-next-heading)
  12852. (while (and (not (eobp)) (> (funcall outline-level) level))
  12853. (outline-next-heading))
  12854. (if (or (eobp) (< (funcall outline-level) level))
  12855. nil
  12856. (point))))
  12857. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12858. ;; This is an exact copy of the original function, but it uses
  12859. ;; `org-back-to-heading', to make it work also in invisible
  12860. ;; trees. And is uses an invisible-OK argument.
  12861. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12862. (org-back-to-heading invisible-OK)
  12863. (let ((first t)
  12864. (level (funcall outline-level)))
  12865. (while (and (not (eobp))
  12866. (or first (> (funcall outline-level) level)))
  12867. (setq first nil)
  12868. (outline-next-heading))
  12869. (unless to-heading
  12870. (if (memq (preceding-char) '(?\n ?\^M))
  12871. (progn
  12872. ;; Go to end of line before heading
  12873. (forward-char -1)
  12874. (if (memq (preceding-char) '(?\n ?\^M))
  12875. ;; leave blank line before heading
  12876. (forward-char -1))))))
  12877. (point))
  12878. (defun org-show-subtree ()
  12879. "Show everything after this heading at deeper levels."
  12880. (outline-flag-region
  12881. (point)
  12882. (save-excursion
  12883. (outline-end-of-subtree) (outline-next-heading) (point))
  12884. nil))
  12885. (defun org-show-entry ()
  12886. "Show the body directly following this heading.
  12887. Show the heading too, if it is currently invisible."
  12888. (interactive)
  12889. (save-excursion
  12890. (condition-case nil
  12891. (progn
  12892. (org-back-to-heading t)
  12893. (outline-flag-region
  12894. (max (point-min) (1- (point)))
  12895. (save-excursion
  12896. (re-search-forward
  12897. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12898. (or (match-beginning 1) (point-max)))
  12899. nil))
  12900. (error nil))))
  12901. (defun org-make-options-regexp (kwds)
  12902. "Make a regular expression for keyword lines."
  12903. (concat
  12904. "^"
  12905. "#?[ \t]*\\+\\("
  12906. (mapconcat 'regexp-quote kwds "\\|")
  12907. "\\):[ \t]*"
  12908. "\\(.+\\)"))
  12909. ;; Make isearch reveal the necessary context
  12910. (defun org-isearch-end ()
  12911. "Reveal context after isearch exits."
  12912. (when isearch-success ; only if search was successful
  12913. (if (featurep 'xemacs)
  12914. ;; Under XEmacs, the hook is run in the correct place,
  12915. ;; we directly show the context.
  12916. (org-show-context 'isearch)
  12917. ;; In Emacs the hook runs *before* restoring the overlays.
  12918. ;; So we have to use a one-time post-command-hook to do this.
  12919. ;; (Emacs 22 has a special variable, see function `org-mode')
  12920. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12921. isearch-mode-end-hook-quit)
  12922. ;; Only when the isearch was not quitted.
  12923. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12924. 'append 'local)))))
  12925. (defun org-isearch-post-command ()
  12926. "Remove self from hook, and show context."
  12927. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12928. (org-show-context 'isearch))
  12929. ;;;; Integration with and fixes for other packages
  12930. ;;; Imenu support
  12931. (defvar org-imenu-markers nil
  12932. "All markers currently used by Imenu.")
  12933. (make-variable-buffer-local 'org-imenu-markers)
  12934. (defun org-imenu-new-marker (&optional pos)
  12935. "Return a new marker for use by Imenu, and remember the marker."
  12936. (let ((m (make-marker)))
  12937. (move-marker m (or pos (point)))
  12938. (push m org-imenu-markers)
  12939. m))
  12940. (defun org-imenu-get-tree ()
  12941. "Produce the index for Imenu."
  12942. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12943. (setq org-imenu-markers nil)
  12944. (let* ((n org-imenu-depth)
  12945. (re (concat "^" outline-regexp))
  12946. (subs (make-vector (1+ n) nil))
  12947. (last-level 0)
  12948. m tree level head)
  12949. (save-excursion
  12950. (save-restriction
  12951. (widen)
  12952. (goto-char (point-max))
  12953. (while (re-search-backward re nil t)
  12954. (setq level (org-reduced-level (funcall outline-level)))
  12955. (when (<= level n)
  12956. (looking-at org-complex-heading-regexp)
  12957. (setq head (org-link-display-format
  12958. (org-match-string-no-properties 4))
  12959. m (org-imenu-new-marker))
  12960. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12961. (if (>= level last-level)
  12962. (push (cons head m) (aref subs level))
  12963. (push (cons head (aref subs (1+ level))) (aref subs level))
  12964. (loop for i from (1+ level) to n do (aset subs i nil)))
  12965. (setq last-level level)))))
  12966. (aref subs 1)))
  12967. (eval-after-load "imenu"
  12968. '(progn
  12969. (add-hook 'imenu-after-jump-hook
  12970. (lambda ()
  12971. (if (eq major-mode 'org-mode)
  12972. (org-show-context 'org-goto))))))
  12973. (defun org-link-display-format (link)
  12974. "Replace a link with either the description, or the link target
  12975. if no description is present"
  12976. (save-match-data
  12977. (if (string-match org-bracket-link-analytic-regexp link)
  12978. (replace-match (or (match-string 5 link)
  12979. (concat (match-string 1 link)
  12980. (match-string 3 link)))
  12981. nil nil link)
  12982. link)))
  12983. ;; Speedbar support
  12984. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12985. "Overlay marking the agenda restriction line in speedbar.")
  12986. (org-overlay-put org-speedbar-restriction-lock-overlay
  12987. 'face 'org-agenda-restriction-lock)
  12988. (org-overlay-put org-speedbar-restriction-lock-overlay
  12989. 'help-echo "Agendas are currently limited to this item.")
  12990. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12991. (defun org-speedbar-set-agenda-restriction ()
  12992. "Restrict future agenda commands to the location at point in speedbar.
  12993. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12994. (interactive)
  12995. (require 'org-agenda)
  12996. (let (p m tp np dir txt w)
  12997. (cond
  12998. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12999. 'org-imenu t))
  13000. (setq m (get-text-property p 'org-imenu-marker))
  13001. (save-excursion
  13002. (save-restriction
  13003. (set-buffer (marker-buffer m))
  13004. (goto-char m)
  13005. (org-agenda-set-restriction-lock 'subtree))))
  13006. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13007. 'speedbar-function 'speedbar-find-file))
  13008. (setq tp (previous-single-property-change
  13009. (1+ p) 'speedbar-function)
  13010. np (next-single-property-change
  13011. tp 'speedbar-function)
  13012. dir (speedbar-line-directory)
  13013. txt (buffer-substring-no-properties (or tp (point-min))
  13014. (or np (point-max))))
  13015. (save-excursion
  13016. (save-restriction
  13017. (set-buffer (find-file-noselect
  13018. (let ((default-directory dir))
  13019. (expand-file-name txt))))
  13020. (unless (org-mode-p)
  13021. (error "Cannot restrict to non-Org-mode file"))
  13022. (org-agenda-set-restriction-lock 'file))))
  13023. (t (error "Don't know how to restrict Org-mode's agenda")))
  13024. (org-move-overlay org-speedbar-restriction-lock-overlay
  13025. (point-at-bol) (point-at-eol))
  13026. (setq current-prefix-arg nil)
  13027. (org-agenda-maybe-redo)))
  13028. (eval-after-load "speedbar"
  13029. '(progn
  13030. (speedbar-add-supported-extension ".org")
  13031. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13032. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13033. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13034. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13035. (add-hook 'speedbar-visiting-tag-hook
  13036. (lambda () (org-show-context 'org-goto)))))
  13037. ;;; Fixes and Hacks for problems with other packages
  13038. ;; Make flyspell not check words in links, to not mess up our keymap
  13039. (defun org-mode-flyspell-verify ()
  13040. "Don't let flyspell put overlays at active buttons."
  13041. (not (get-text-property (point) 'keymap)))
  13042. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13043. (eval-after-load "bookmark"
  13044. '(if (boundp 'bookmark-after-jump-hook)
  13045. ;; We can use the hook
  13046. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13047. ;; Hook not available, use advice
  13048. (defadvice bookmark-jump (after org-make-visible activate)
  13049. "Make the position visible."
  13050. (org-bookmark-jump-unhide))))
  13051. (defun org-bookmark-jump-unhide ()
  13052. "Unhide the current position, to show the bookmark location."
  13053. (and (org-mode-p)
  13054. (or (org-invisible-p)
  13055. (save-excursion (goto-char (max (point-min) (1- (point))))
  13056. (org-invisible-p)))
  13057. (org-show-context 'bookmark-jump)))
  13058. ;; Make session.el ignore our circular variable
  13059. (eval-after-load "session"
  13060. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13061. ;;;; Experimental code
  13062. (defun org-closed-in-range ()
  13063. "Sparse tree of items closed in a certain time range.
  13064. Still experimental, may disappear in the future."
  13065. (interactive)
  13066. ;; Get the time interval from the user.
  13067. (let* ((time1 (time-to-seconds
  13068. (org-read-date nil 'to-time nil "Starting date: ")))
  13069. (time2 (time-to-seconds
  13070. (org-read-date nil 'to-time nil "End date:")))
  13071. ;; callback function
  13072. (callback (lambda ()
  13073. (let ((time
  13074. (time-to-seconds
  13075. (apply 'encode-time
  13076. (org-parse-time-string
  13077. (match-string 1))))))
  13078. ;; check if time in interval
  13079. (and (>= time time1) (<= time time2))))))
  13080. ;; make tree, check each match with the callback
  13081. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13082. ;;;; Finish up
  13083. (provide 'org)
  13084. (run-hooks 'org-load-hook)
  13085. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13086. ;;; org.el ends here