org.el 503 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689
  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.02
  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, or (at your option)
  15. ;; 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; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  65. (defvar org-table-formula-constants-local nil
  66. "Local version of `org-table-formula-constants'.")
  67. (make-variable-buffer-local 'org-table-formula-constants-local)
  68. ;;;; Require other packages
  69. (eval-when-compile
  70. (require 'cl)
  71. (require 'gnus-sum)
  72. (require 'calendar))
  73. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  74. ;; the file noutline.el being loaded.
  75. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  76. ;; We require noutline, which might be provided in outline.el
  77. (require 'outline) (require 'noutline)
  78. ;; Other stuff we need.
  79. (require 'time-date)
  80. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  81. (require 'easymenu)
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (require 'org-faces)
  85. ;;;; Customization variables
  86. ;;; Version
  87. (defconst org-version "6.02"
  88. "The version number of the file org.el.")
  89. (defun org-version (&optional here)
  90. "Show the org-mode version in the echo area.
  91. With prefix arg HERE, insert it at point."
  92. (interactive "P")
  93. (let ((version (format "Org-mode version %s" org-version)))
  94. (message version)
  95. (if here
  96. (insert version))))
  97. ;;; Compatibility constants
  98. ;;; The custom variables
  99. (defgroup org nil
  100. "Outline-based notes management and organizer."
  101. :tag "Org"
  102. :group 'outlines
  103. :group 'hypermedia
  104. :group 'calendar)
  105. (defcustom org-load-hook nil
  106. "Hook that is run after org.el has been loaded."
  107. :group 'org
  108. :type 'hook)
  109. (defvar org-modules) ; defined below
  110. (defvar org-modules-loaded nil
  111. "Have the modules been loaded already?")
  112. (defun org-load-modules-maybe (&optional force)
  113. "Load all extensions listed in `org-default-extensions'."
  114. (when (or force (not org-modules-loaded))
  115. (mapc (lambda (ext)
  116. (condition-case nil (require ext)
  117. (error (message "Problems while trying to load feature `%s'" ext))))
  118. org-modules)
  119. (setq org-modules-loaded t)))
  120. (defun org-set-modules (var value)
  121. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  122. (set var value)
  123. (when (featurep 'org)
  124. (org-load-modules-maybe 'force)))
  125. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-infojs org-irc org-mew org-mhe org-rmail org-vm org-wl)
  126. "Modules that should always be loaded together with org.el.
  127. If a description starts with <C>, the file is not part of emacs
  128. and loading it will require that you have downloaded and properly installed
  129. the org-mode distribution.
  130. You can also use this system to load external packages (i.e. neither Org
  131. core modules, not modules from the CONTRIB directory). Just add symbols
  132. to the end of the list. If the package is called org-xyz.e, then you need
  133. to add the symbol `xyz', and the package must have a call to
  134. (provide 'org-xyz)"
  135. :group 'org
  136. :set 'org-set-modules
  137. :type
  138. '(set :greedy t
  139. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  140. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  141. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  142. (const :tag " info: Links to Info nodes" org-info)
  143. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  144. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  145. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  146. (const :tag " mew Links to Mew folders/messages" org-mew)
  147. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  148. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  149. (const :tag " vm: Links to VM folders/messages" org-vm)
  150. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  151. (const :tag " mouse: Additional mouse support" org-mouse)
  152. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  153. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  154. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  155. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  156. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  157. (const :tag "C id: Global id's for identifying entries" org-id)
  158. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  159. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  160. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  161. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  162. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  163. (const :tag "C registry: A registry for Org links" org-registry)
  164. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  165. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  166. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  167. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  168. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  169. (defgroup org-startup nil
  170. "Options concerning startup of Org-mode."
  171. :tag "Org Startup"
  172. :group 'org)
  173. (defcustom org-startup-folded t
  174. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  175. This can also be configured on a per-file basis by adding one of
  176. the following lines anywhere in the buffer:
  177. #+STARTUP: fold
  178. #+STARTUP: nofold
  179. #+STARTUP: content"
  180. :group 'org-startup
  181. :type '(choice
  182. (const :tag "nofold: show all" nil)
  183. (const :tag "fold: overview" t)
  184. (const :tag "content: all headlines" content)))
  185. (defcustom org-startup-truncated t
  186. "Non-nil means, entering Org-mode will set `truncate-lines'.
  187. This is useful since some lines containing links can be very long and
  188. uninteresting. Also tables look terrible when wrapped."
  189. :group 'org-startup
  190. :type 'boolean)
  191. (defcustom org-startup-align-all-tables nil
  192. "Non-nil means, align all tables when visiting a file.
  193. This is useful when the column width in tables is forced with <N> cookies
  194. in table fields. Such tables will look correct only after the first re-align.
  195. This can also be configured on a per-file basis by adding one of
  196. the following lines anywhere in the buffer:
  197. #+STARTUP: align
  198. #+STARTUP: noalign"
  199. :group 'org-startup
  200. :type 'boolean)
  201. (defcustom org-insert-mode-line-in-empty-file nil
  202. "Non-nil means insert the first line setting Org-mode in empty files.
  203. When the function `org-mode' is called interactively in an empty file, this
  204. normally means that the file name does not automatically trigger Org-mode.
  205. To ensure that the file will always be in Org-mode in the future, a
  206. line enforcing Org-mode will be inserted into the buffer, if this option
  207. has been set."
  208. :group 'org-startup
  209. :type 'boolean)
  210. (defcustom org-replace-disputed-keys nil
  211. "Non-nil means use alternative key bindings for some keys.
  212. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  213. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  214. If you want to use Org-mode together with one of these other modes,
  215. or more generally if you would like to move some Org-mode commands to
  216. other keys, set this variable and configure the keys with the variable
  217. `org-disputed-keys'.
  218. This option is only relevant at load-time of Org-mode, and must be set
  219. *before* org.el is loaded. Changing it requires a restart of Emacs to
  220. become effective."
  221. :group 'org-startup
  222. :type 'boolean)
  223. (if (fboundp 'defvaralias)
  224. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  225. (defcustom org-disputed-keys
  226. '(([(shift up)] . [(meta p)])
  227. ([(shift down)] . [(meta n)])
  228. ([(shift left)] . [(meta -)])
  229. ([(shift right)] . [(meta +)])
  230. ([(control shift right)] . [(meta shift +)])
  231. ([(control shift left)] . [(meta shift -)]))
  232. "Keys for which Org-mode and other modes compete.
  233. This is an alist, cars are the default keys, second element specifies
  234. the alternative to use when `org-replace-disputed-keys' is t.
  235. Keys can be specified in any syntax supported by `define-key'.
  236. The value of this option takes effect only at Org-mode's startup,
  237. therefore you'll have to restart Emacs to apply it after changing."
  238. :group 'org-startup
  239. :type 'alist)
  240. (defun org-key (key)
  241. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  242. Or return the original if not disputed."
  243. (if org-replace-disputed-keys
  244. (let* ((nkey (key-description key))
  245. (x (org-find-if (lambda (x)
  246. (equal (key-description (car x)) nkey))
  247. org-disputed-keys)))
  248. (if x (cdr x) key))
  249. key))
  250. (defun org-find-if (predicate seq)
  251. (catch 'exit
  252. (while seq
  253. (if (funcall predicate (car seq))
  254. (throw 'exit (car seq))
  255. (pop seq)))))
  256. (defun org-defkey (keymap key def)
  257. "Define a key, possibly translated, as returned by `org-key'."
  258. (define-key keymap (org-key key) def))
  259. (defcustom org-ellipsis nil
  260. "The ellipsis to use in the Org-mode outline.
  261. When nil, just use the standard three dots. When a string, use that instead,
  262. When a face, use the standart 3 dots, but with the specified face.
  263. The change affects only Org-mode (which will then use its own display table).
  264. Changing this requires executing `M-x org-mode' in a buffer to become
  265. effective."
  266. :group 'org-startup
  267. :type '(choice (const :tag "Default" nil)
  268. (face :tag "Face" :value org-warning)
  269. (string :tag "String" :value "...#")))
  270. (defvar org-display-table nil
  271. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  272. (defgroup org-keywords nil
  273. "Keywords in Org-mode."
  274. :tag "Org Keywords"
  275. :group 'org)
  276. (defcustom org-deadline-string "DEADLINE:"
  277. "String to mark deadline entries.
  278. A deadline is this string, followed by a time stamp. Should be a word,
  279. terminated by a colon. You can insert a schedule keyword and
  280. a timestamp with \\[org-deadline].
  281. Changes become only effective after restarting Emacs."
  282. :group 'org-keywords
  283. :type 'string)
  284. (defcustom org-scheduled-string "SCHEDULED:"
  285. "String to mark scheduled TODO entries.
  286. A schedule is this string, followed by a time stamp. Should be a word,
  287. terminated by a colon. You can insert a schedule keyword and
  288. a timestamp with \\[org-schedule].
  289. Changes become only effective after restarting Emacs."
  290. :group 'org-keywords
  291. :type 'string)
  292. (defcustom org-closed-string "CLOSED:"
  293. "String used as the prefix for timestamps logging closing a TODO entry."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-clock-string "CLOCK:"
  297. "String used as prefix for timestamps clocking work hours on an item."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-comment-string "COMMENT"
  301. "Entries starting with this keyword will never be exported.
  302. An entry can be toggled between COMMENT and normal with
  303. \\[org-toggle-comment].
  304. Changes become only effective after restarting Emacs."
  305. :group 'org-keywords
  306. :type 'string)
  307. (defcustom org-quote-string "QUOTE"
  308. "Entries starting with this keyword will be exported in fixed-width font.
  309. Quoting applies only to the text in the entry following the headline, and does
  310. not extend beyond the next headline, even if that is lower level.
  311. An entry can be toggled between QUOTE and normal with
  312. \\[org-toggle-fixed-width-section]."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defconst org-repeat-re
  316. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  317. "Regular expression for specifying repeated events.
  318. After a match, group 1 contains the repeat expression.")
  319. (defgroup org-structure nil
  320. "Options concerning the general structure of Org-mode files."
  321. :tag "Org Structure"
  322. :group 'org)
  323. (defgroup org-reveal-location nil
  324. "Options about how to make context of a location visible."
  325. :tag "Org Reveal Location"
  326. :group 'org-structure)
  327. (defconst org-context-choice
  328. '(choice
  329. (const :tag "Always" t)
  330. (const :tag "Never" nil)
  331. (repeat :greedy t :tag "Individual contexts"
  332. (cons
  333. (choice :tag "Context"
  334. (const agenda)
  335. (const org-goto)
  336. (const occur-tree)
  337. (const tags-tree)
  338. (const link-search)
  339. (const mark-goto)
  340. (const bookmark-jump)
  341. (const isearch)
  342. (const default))
  343. (boolean))))
  344. "Contexts for the reveal options.")
  345. (defcustom org-show-hierarchy-above '((default . t))
  346. "Non-nil means, show full hierarchy when revealing a location.
  347. Org-mode often shows locations in an org-mode file which might have
  348. been invisible before. When this is set, the hierarchy of headings
  349. above the exposed location is shown.
  350. Turning this off for example for sparse trees makes them very compact.
  351. Instead of t, this can also be an alist specifying this option for different
  352. contexts. Valid contexts are
  353. agenda when exposing an entry from the agenda
  354. org-goto when using the command `org-goto' on key C-c C-j
  355. occur-tree when using the command `org-occur' on key C-c /
  356. tags-tree when constructing a sparse tree based on tags matches
  357. link-search when exposing search matches associated with a link
  358. mark-goto when exposing the jump goal of a mark
  359. bookmark-jump when exposing a bookmark location
  360. isearch when exiting from an incremental search
  361. default default for all contexts not set explicitly"
  362. :group 'org-reveal-location
  363. :type org-context-choice)
  364. (defcustom org-show-following-heading '((default . nil))
  365. "Non-nil means, show following heading when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the heading following the
  368. match is shown.
  369. Turning this off for example for sparse trees makes them very compact,
  370. but makes it harder to edit the location of the match. In such a case,
  371. use the command \\[org-reveal] to show more context.
  372. Instead of t, this can also be an alist specifying this option for different
  373. contexts. See `org-show-hierarchy-above' for valid contexts."
  374. :group 'org-reveal-location
  375. :type org-context-choice)
  376. (defcustom org-show-siblings '((default . nil) (isearch t))
  377. "Non-nil means, show all sibling heading when revealing a location.
  378. Org-mode often shows locations in an org-mode file which might have
  379. been invisible before. When this is set, the sibling of the current entry
  380. heading are all made visible. If `org-show-hierarchy-above' is t,
  381. the same happens on each level of the hierarchy above the current entry.
  382. By default this is on for the isearch context, off for all other contexts.
  383. Turning this off for example for sparse trees makes them very compact,
  384. but makes it harder to edit the location of the match. In such a case,
  385. use the command \\[org-reveal] to show more context.
  386. Instead of t, this can also be an alist specifying this option for different
  387. contexts. See `org-show-hierarchy-above' for valid contexts."
  388. :group 'org-reveal-location
  389. :type org-context-choice)
  390. (defcustom org-show-entry-below '((default . nil))
  391. "Non-nil means, show the entry below a headline when revealing a location.
  392. Org-mode often shows locations in an org-mode file which might have
  393. been invisible before. When this is set, the text below the headline that is
  394. exposed is also shown.
  395. By default this is off for all contexts.
  396. Instead of t, this can also be an alist specifying this option for different
  397. contexts. See `org-show-hierarchy-above' for valid contexts."
  398. :group 'org-reveal-location
  399. :type org-context-choice)
  400. (defcustom org-indirect-buffer-display 'other-window
  401. "How should indirect tree buffers be displayed?
  402. This applies to indirect buffers created with the commands
  403. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  404. Valid values are:
  405. current-window Display in the current window
  406. other-window Just display in another window.
  407. dedicated-frame Create one new frame, and re-use it each time.
  408. new-frame Make a new frame each time. Note that in this case
  409. previously-made indirect buffers are kept, and you need to
  410. kill these buffers yourself."
  411. :group 'org-structure
  412. :group 'org-agenda-windows
  413. :type '(choice
  414. (const :tag "In current window" current-window)
  415. (const :tag "In current frame, other window" other-window)
  416. (const :tag "Each time a new frame" new-frame)
  417. (const :tag "One dedicated frame" dedicated-frame)))
  418. (defgroup org-cycle nil
  419. "Options concerning visibility cycling in Org-mode."
  420. :tag "Org Cycle"
  421. :group 'org-structure)
  422. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  423. "Names of drawers. Drawers are not opened by cycling on the headline above.
  424. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  425. this:
  426. :DRAWERNAME:
  427. .....
  428. :END:
  429. The drawer \"PROPERTIES\" is special for capturing properties through
  430. the property API.
  431. Drawers can be defined on the per-file basis with a line like:
  432. #+DRAWERS: HIDDEN STATE PROPERTIES"
  433. :group 'org-structure
  434. :type '(repeat (string :tag "Drawer Name")))
  435. (defcustom org-cycle-global-at-bob nil
  436. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  437. This makes it possible to do global cycling without having to use S-TAB or
  438. C-u TAB. For this special case to work, the first line of the buffer
  439. must not be a headline - it may be empty ot some other text. When used in
  440. this way, `org-cycle-hook' is disables temporarily, to make sure the
  441. cursor stays at the beginning of the buffer.
  442. When this option is nil, don't do anything special at the beginning
  443. of the buffer."
  444. :group 'org-cycle
  445. :type 'boolean)
  446. (defcustom org-cycle-emulate-tab t
  447. "Where should `org-cycle' emulate TAB.
  448. nil Never
  449. white Only in completely white lines
  450. whitestart Only at the beginning of lines, before the first non-white char
  451. t Everywhere except in headlines
  452. exc-hl-bol Everywhere except at the start of a headline
  453. If TAB is used in a place where it does not emulate TAB, the current subtree
  454. visibility is cycled."
  455. :group 'org-cycle
  456. :type '(choice (const :tag "Never" nil)
  457. (const :tag "Only in completely white lines" white)
  458. (const :tag "Before first char in a line" whitestart)
  459. (const :tag "Everywhere except in headlines" t)
  460. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  461. ))
  462. (defcustom org-cycle-separator-lines 2
  463. "Number of empty lines needed to keep an empty line between collapsed trees.
  464. If you leave an empty line between the end of a subtree and the following
  465. headline, this empty line is hidden when the subtree is folded.
  466. Org-mode will leave (exactly) one empty line visible if the number of
  467. empty lines is equal or larger to the number given in this variable.
  468. So the default 2 means, at least 2 empty lines after the end of a subtree
  469. are needed to produce free space between a collapsed subtree and the
  470. following headline.
  471. Special case: when 0, never leave empty lines in collapsed view."
  472. :group 'org-cycle
  473. :type 'integer)
  474. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  475. org-cycle-hide-drawers
  476. org-cycle-show-empty-lines
  477. org-optimize-window-after-visibility-change)
  478. "Hook that is run after `org-cycle' has changed the buffer visibility.
  479. The function(s) in this hook must accept a single argument which indicates
  480. the new state that was set by the most recent `org-cycle' command. The
  481. argument is a symbol. After a global state change, it can have the values
  482. `overview', `content', or `all'. After a local state change, it can have
  483. the values `folded', `children', or `subtree'."
  484. :group 'org-cycle
  485. :type 'hook)
  486. (defgroup org-edit-structure nil
  487. "Options concerning structure editing in Org-mode."
  488. :tag "Org Edit Structure"
  489. :group 'org-structure)
  490. (defcustom org-odd-levels-only nil
  491. "Non-nil means, skip even levels and only use odd levels for the outline.
  492. This has the effect that two stars are being added/taken away in
  493. promotion/demotion commands. It also influences how levels are
  494. handled by the exporters.
  495. Changing it requires restart of `font-lock-mode' to become effective
  496. for fontification also in regions already fontified.
  497. You may also set this on a per-file basis by adding one of the following
  498. lines to the buffer:
  499. #+STARTUP: odd
  500. #+STARTUP: oddeven"
  501. :group 'org-edit-structure
  502. :group 'org-font-lock
  503. :type 'boolean)
  504. (defcustom org-adapt-indentation t
  505. "Non-nil means, adapt indentation when promoting and demoting.
  506. When this is set and the *entire* text in an entry is indented, the
  507. indentation is increased by one space in a demotion command, and
  508. decreased by one in a promotion command. If any line in the entry
  509. body starts at column 0, indentation is not changed at all."
  510. :group 'org-edit-structure
  511. :type 'boolean)
  512. (defcustom org-special-ctrl-a/e nil
  513. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  514. When t, `C-a' will bring back the cursor to the beginning of the
  515. headline text, i.e. after the stars and after a possible TODO keyword.
  516. In an item, this will be the position after the bullet.
  517. When the cursor is already at that position, another `C-a' will bring
  518. it to the beginning of the line.
  519. `C-e' will jump to the end of the headline, ignoring the presence of tags
  520. in the headline. A second `C-e' will then jump to the true end of the
  521. line, after any tags.
  522. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  523. and only a directly following, identical keypress will bring the cursor
  524. to the special positions."
  525. :group 'org-edit-structure
  526. :type '(choice
  527. (const :tag "off" nil)
  528. (const :tag "after bullet first" t)
  529. (const :tag "border first" reversed)))
  530. (if (fboundp 'defvaralias)
  531. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  532. (defcustom org-special-ctrl-k nil
  533. "Non-nil means `C-k' will behave specially in headlines.
  534. When nil, `C-k' will call the default `kill-line' command.
  535. When t, the following will happen while the cursor is in the headline:
  536. - When the cursor is at the beginning of a headline, kill the entire
  537. line and possible the folded subtree below the line.
  538. - When in the middle of the headline text, kill the headline up to the tags.
  539. - When after the headline text, kill the tags."
  540. :group 'org-edit-structure
  541. :type 'boolean)
  542. (defcustom org-M-RET-may-split-line '((default . t))
  543. "Non-nil means, M-RET will split the line at the cursor position.
  544. When nil, it will go to the end of the line before making a
  545. new line.
  546. You may also set this option in a different way for different
  547. contexts. Valid contexts are:
  548. headline when creating a new headline
  549. item when creating a new item
  550. table in a table field
  551. default the value to be used for all contexts not explicitly
  552. customized"
  553. :group 'org-structure
  554. :group 'org-table
  555. :type '(choice
  556. (const :tag "Always" t)
  557. (const :tag "Never" nil)
  558. (repeat :greedy t :tag "Individual contexts"
  559. (cons
  560. (choice :tag "Context"
  561. (const headline)
  562. (const item)
  563. (const table)
  564. (const default))
  565. (boolean)))))
  566. (defcustom org-blank-before-new-entry '((heading . nil)
  567. (plain-list-item . nil))
  568. "Should `org-insert-heading' leave a blank line before new heading/item?
  569. The value is an alist, with `heading' and `plain-list-item' as car,
  570. and a boolean flag as cdr."
  571. :group 'org-edit-structure
  572. :type '(list
  573. (cons (const heading) (boolean))
  574. (cons (const plain-list-item) (boolean))))
  575. (defcustom org-insert-heading-hook nil
  576. "Hook being run after inserting a new heading."
  577. :group 'org-edit-structure
  578. :type 'hook)
  579. (defcustom org-enable-fixed-width-editor t
  580. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  581. This currently only means, they are never auto-wrapped.
  582. When nil, such lines will be treated like ordinary lines.
  583. See also the QUOTE keyword."
  584. :group 'org-edit-structure
  585. :type 'boolean)
  586. (defcustom org-goto-auto-isearch t
  587. "Non-nil means, typing characters in org-goto starts incremental search."
  588. :group 'org-edit-structure
  589. :type 'boolean)
  590. (defgroup org-sparse-trees nil
  591. "Options concerning sparse trees in Org-mode."
  592. :tag "Org Sparse Trees"
  593. :group 'org-structure)
  594. (defcustom org-highlight-sparse-tree-matches t
  595. "Non-nil means, highlight all matches that define a sparse tree.
  596. The highlights will automatically disappear the next time the buffer is
  597. changed by an edit command."
  598. :group 'org-sparse-trees
  599. :type 'boolean)
  600. (defcustom org-remove-highlights-with-change t
  601. "Non-nil means, any change to the buffer will remove temporary highlights.
  602. Such highlights are created by `org-occur' and `org-clock-display'.
  603. When nil, `C-c C-c needs to be used to get rid of the highlights.
  604. The highlights created by `org-preview-latex-fragment' always need
  605. `C-c C-c' to be removed."
  606. :group 'org-sparse-trees
  607. :group 'org-time
  608. :type 'boolean)
  609. (defcustom org-occur-hook '(org-first-headline-recenter)
  610. "Hook that is run after `org-occur' has constructed a sparse tree.
  611. This can be used to recenter the window to show as much of the structure
  612. as possible."
  613. :group 'org-sparse-trees
  614. :type 'hook)
  615. (defgroup org-plain-lists nil
  616. "Options concerning plain lists in Org-mode."
  617. :tag "Org Plain lists"
  618. :group 'org-structure)
  619. (defcustom org-cycle-include-plain-lists nil
  620. "Non-nil means, include plain lists into visibility cycling.
  621. This means that during cycling, plain list items will *temporarily* be
  622. interpreted as outline headlines with a level given by 1000+i where i is the
  623. indentation of the bullet. In all other operations, plain list items are
  624. not seen as headlines. For example, you cannot assign a TODO keyword to
  625. such an item."
  626. :group 'org-plain-lists
  627. :type 'boolean)
  628. (defcustom org-plain-list-ordered-item-terminator t
  629. "The character that makes a line with leading number an ordered list item.
  630. Valid values are ?. and ?\). To get both terminators, use t. While
  631. ?. may look nicer, it creates the danger that a line with leading
  632. number may be incorrectly interpreted as an item. ?\) therefore is
  633. the safe choice."
  634. :group 'org-plain-lists
  635. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  636. (const :tag "paren like in \"2)\"" ?\))
  637. (const :tab "both" t)))
  638. (defcustom org-empty-line-terminates-plain-lists nil
  639. "Non-nil means, an empty line ends all plain list levels.
  640. When nil, empty lines are part of the preceeding item."
  641. :group 'org-plain-lists
  642. :type 'boolean)
  643. (defcustom org-auto-renumber-ordered-lists t
  644. "Non-nil means, automatically renumber ordered plain lists.
  645. Renumbering happens when the sequence have been changed with
  646. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  647. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  648. :group 'org-plain-lists
  649. :type 'boolean)
  650. (defcustom org-provide-checkbox-statistics t
  651. "Non-nil means, update checkbox statistics after insert and toggle.
  652. When this is set, checkbox statistics is updated each time you either insert
  653. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  654. with \\[org-ctrl-c-ctrl-c\\]."
  655. :group 'org-plain-lists
  656. :type 'boolean)
  657. (defgroup org-imenu-and-speedbar nil
  658. "Options concerning imenu and speedbar in Org-mode."
  659. :tag "Org Imenu and Speedbar"
  660. :group 'org-structure)
  661. (defcustom org-imenu-depth 2
  662. "The maximum level for Imenu access to Org-mode headlines.
  663. This also applied for speedbar access."
  664. :group 'org-imenu-and-speedbar
  665. :type 'number)
  666. (defgroup org-table nil
  667. "Options concerning tables in Org-mode."
  668. :tag "Org Table"
  669. :group 'org)
  670. (defcustom org-enable-table-editor 'optimized
  671. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  672. When nil, such lines will be treated like ordinary lines.
  673. When equal to the symbol `optimized', the table editor will be optimized to
  674. do the following:
  675. - Automatic overwrite mode in front of whitespace in table fields.
  676. This makes the structure of the table stay in tact as long as the edited
  677. field does not exceed the column width.
  678. - Minimize the number of realigns. Normally, the table is aligned each time
  679. TAB or RET are pressed to move to another field. With optimization this
  680. happens only if changes to a field might have changed the column width.
  681. Optimization requires replacing the functions `self-insert-command',
  682. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  683. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  684. very good at guessing when a re-align will be necessary, but you can always
  685. force one with \\[org-ctrl-c-ctrl-c].
  686. If you would like to use the optimized version in Org-mode, but the
  687. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  688. This variable can be used to turn on and off the table editor during a session,
  689. but in order to toggle optimization, a restart is required.
  690. See also the variable `org-table-auto-blank-field'."
  691. :group 'org-table
  692. :type '(choice
  693. (const :tag "off" nil)
  694. (const :tag "on" t)
  695. (const :tag "on, optimized" optimized)))
  696. (defcustom org-table-tab-recognizes-table.el t
  697. "Non-nil means, TAB will automatically notice a table.el table.
  698. When it sees such a table, it moves point into it and - if necessary -
  699. calls `table-recognize-table'."
  700. :group 'org-table-editing
  701. :type 'boolean)
  702. (defgroup org-link nil
  703. "Options concerning links in Org-mode."
  704. :tag "Org Link"
  705. :group 'org)
  706. (defvar org-link-abbrev-alist-local nil
  707. "Buffer-local version of `org-link-abbrev-alist', which see.
  708. The value of this is taken from the #+LINK lines.")
  709. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  710. (defcustom org-link-abbrev-alist nil
  711. "Alist of link abbreviations.
  712. The car of each element is a string, to be replaced at the start of a link.
  713. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  714. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  715. [[linkkey:tag][description]]
  716. If REPLACE is a string, the tag will simply be appended to create the link.
  717. If the string contains \"%s\", the tag will be inserted there.
  718. REPLACE may also be a function that will be called with the tag as the
  719. only argument to create the link, which should be returned as a string.
  720. See the manual for examples."
  721. :group 'org-link
  722. :type 'alist)
  723. (defcustom org-descriptive-links t
  724. "Non-nil means, hide link part and only show description of bracket links.
  725. Bracket links are like [[link][descritpion]]. This variable sets the initial
  726. state in new org-mode buffers. The setting can then be toggled on a
  727. per-buffer basis from the Org->Hyperlinks menu."
  728. :group 'org-link
  729. :type 'boolean)
  730. (defcustom org-link-file-path-type 'adaptive
  731. "How the path name in file links should be stored.
  732. Valid values are:
  733. relative Relative to the current directory, i.e. the directory of the file
  734. into which the link is being inserted.
  735. absolute Absolute path, if possible with ~ for home directory.
  736. noabbrev Absolute path, no abbreviation of home directory.
  737. adaptive Use relative path for files in the current directory and sub-
  738. directories of it. For other files, use an absolute path."
  739. :group 'org-link
  740. :type '(choice
  741. (const relative)
  742. (const absolute)
  743. (const noabbrev)
  744. (const adaptive)))
  745. (defcustom org-activate-links '(bracket angle plain radio tag date)
  746. "Types of links that should be activated in Org-mode files.
  747. This is a list of symbols, each leading to the activation of a certain link
  748. type. In principle, it does not hurt to turn on most link types - there may
  749. be a small gain when turning off unused link types. The types are:
  750. bracket The recommended [[link][description]] or [[link]] links with hiding.
  751. angular Links in angular brackes that may contain whitespace like
  752. <bbdb:Carsten Dominik>.
  753. plain Plain links in normal text, no whitespace, like http://google.com.
  754. radio Text that is matched by a radio target, see manual for details.
  755. tag Tag settings in a headline (link to tag search).
  756. date Time stamps (link to calendar).
  757. Changing this variable requires a restart of Emacs to become effective."
  758. :group 'org-link
  759. :type '(set (const :tag "Double bracket links (new style)" bracket)
  760. (const :tag "Angular bracket links (old style)" angular)
  761. (const :tag "Plain text links" plain)
  762. (const :tag "Radio target matches" radio)
  763. (const :tag "Tags" tag)
  764. (const :tag "Timestamps" date)))
  765. (defcustom org-make-link-description-function nil
  766. "Function to use to generate link descriptions from links. If
  767. nil the link location will be used. This function must take two
  768. parameters; the first is the link and the second the description
  769. org-insert-link has generated, and should return the description
  770. to use."
  771. :group 'org-link
  772. :type 'function)
  773. (defgroup org-link-store nil
  774. "Options concerning storing links in Org-mode."
  775. :tag "Org Store Link"
  776. :group 'org-link)
  777. (defcustom org-email-link-description-format "Email %c: %.30s"
  778. "Format of the description part of a link to an email or usenet message.
  779. The following %-excapes will be replaced by corresponding information:
  780. %F full \"From\" field
  781. %f name, taken from \"From\" field, address if no name
  782. %T full \"To\" field
  783. %t first name in \"To\" field, address if no name
  784. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  785. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  786. %s subject
  787. %m message-id.
  788. You may use normal field width specification between the % and the letter.
  789. This is for example useful to limit the length of the subject.
  790. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  791. :group 'org-link-store
  792. :type 'string)
  793. (defcustom org-from-is-user-regexp
  794. (let (r1 r2)
  795. (when (and user-mail-address (not (string= user-mail-address "")))
  796. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  797. (when (and user-full-name (not (string= user-full-name "")))
  798. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  799. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  800. "Regexp mached against the \"From:\" header of an email or usenet message.
  801. It should match if the message is from the user him/herself."
  802. :group 'org-link-store
  803. :type 'regexp)
  804. (defcustom org-context-in-file-links t
  805. "Non-nil means, file links from `org-store-link' contain context.
  806. A search string will be added to the file name with :: as separator and
  807. used to find the context when the link is activated by the command
  808. `org-open-at-point'.
  809. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  810. negates this setting for the duration of the command."
  811. :group 'org-link-store
  812. :type 'boolean)
  813. (defcustom org-keep-stored-link-after-insertion nil
  814. "Non-nil means, keep link in list for entire session.
  815. The command `org-store-link' adds a link pointing to the current
  816. location to an internal list. These links accumulate during a session.
  817. The command `org-insert-link' can be used to insert links into any
  818. Org-mode file (offering completion for all stored links). When this
  819. option is nil, every link which has been inserted once using \\[org-insert-link]
  820. will be removed from the list, to make completing the unused links
  821. more efficient."
  822. :group 'org-link-store
  823. :type 'boolean)
  824. (defgroup org-link-follow nil
  825. "Options concerning following links in Org-mode."
  826. :tag "Org Follow Link"
  827. :group 'org-link)
  828. (defcustom org-follow-link-hook nil
  829. "Hook that is run after a link has been followed."
  830. :group 'org-link-follow
  831. :type 'hook)
  832. (defcustom org-tab-follows-link nil
  833. "Non-nil means, on links TAB will follow the link.
  834. Needs to be set before org.el is loaded."
  835. :group 'org-link-follow
  836. :type 'boolean)
  837. (defcustom org-return-follows-link nil
  838. "Non-nil means, on links RET will follow the link.
  839. Needs to be set before org.el is loaded."
  840. :group 'org-link-follow
  841. :type 'boolean)
  842. (defcustom org-mouse-1-follows-link
  843. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  844. "Non-nil means, mouse-1 on a link will follow the link.
  845. A longer mouse click will still set point. Does not work on XEmacs.
  846. Needs to be set before org.el is loaded."
  847. :group 'org-link-follow
  848. :type 'boolean)
  849. (defcustom org-mark-ring-length 4
  850. "Number of different positions to be recorded in the ring
  851. Changing this requires a restart of Emacs to work correctly."
  852. :group 'org-link-follow
  853. :type 'interger)
  854. (defcustom org-link-frame-setup
  855. '((vm . vm-visit-folder-other-frame)
  856. (gnus . gnus-other-frame)
  857. (file . find-file-other-window))
  858. "Setup the frame configuration for following links.
  859. When following a link with Emacs, it may often be useful to display
  860. this link in another window or frame. This variable can be used to
  861. set this up for the different types of links.
  862. For VM, use any of
  863. `vm-visit-folder'
  864. `vm-visit-folder-other-frame'
  865. For Gnus, use any of
  866. `gnus'
  867. `gnus-other-frame'
  868. For FILE, use any of
  869. `find-file'
  870. `find-file-other-window'
  871. `find-file-other-frame'
  872. For the calendar, use the variable `calendar-setup'.
  873. For BBDB, it is currently only possible to display the matches in
  874. another window."
  875. :group 'org-link-follow
  876. :type '(list
  877. (cons (const vm)
  878. (choice
  879. (const vm-visit-folder)
  880. (const vm-visit-folder-other-window)
  881. (const vm-visit-folder-other-frame)))
  882. (cons (const gnus)
  883. (choice
  884. (const gnus)
  885. (const gnus-other-frame)))
  886. (cons (const file)
  887. (choice
  888. (const find-file)
  889. (const find-file-other-window)
  890. (const find-file-other-frame)))))
  891. (defcustom org-display-internal-link-with-indirect-buffer nil
  892. "Non-nil means, use indirect buffer to display infile links.
  893. Activating internal links (from one location in a file to another location
  894. in the same file) normally just jumps to the location. When the link is
  895. activated with a C-u prefix (or with mouse-3), the link is displayed in
  896. another window. When this option is set, the other window actually displays
  897. an indirect buffer clone of the current buffer, to avoid any visibility
  898. changes to the current buffer."
  899. :group 'org-link-follow
  900. :type 'boolean)
  901. (defcustom org-open-non-existing-files nil
  902. "Non-nil means, `org-open-file' will open non-existing files.
  903. When nil, an error will be generated."
  904. :group 'org-link-follow
  905. :type 'boolean)
  906. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  907. "Function and arguments to call for following mailto links.
  908. This is a list with the first element being a lisp function, and the
  909. remaining elements being arguments to the function. In string arguments,
  910. %a will be replaced by the address, and %s will be replaced by the subject
  911. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  912. :group 'org-link-follow
  913. :type '(choice
  914. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  915. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  916. (const :tag "message-mail" (message-mail "%a" "%s"))
  917. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  918. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  919. "Non-nil means, ask for confirmation before executing shell links.
  920. Shell links can be dangerous: just think about a link
  921. [[shell:rm -rf ~/*][Google Search]]
  922. This link would show up in your Org-mode document as \"Google Search\",
  923. but really it would remove your entire home directory.
  924. Therefore we advise against setting this variable to nil.
  925. Just change it to `y-or-n-p' of you want to confirm with a
  926. single keystroke rather than having to type \"yes\"."
  927. :group 'org-link-follow
  928. :type '(choice
  929. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  930. (const :tag "with y-or-n (faster)" y-or-n-p)
  931. (const :tag "no confirmation (dangerous)" nil)))
  932. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  933. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  934. Elisp links can be dangerous: just think about a link
  935. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  936. This link would show up in your Org-mode document as \"Google Search\",
  937. but really it would remove your entire home directory.
  938. Therefore we advise against setting this variable to nil.
  939. Just change it to `y-or-n-p' of you want to confirm with a
  940. single keystroke rather than having to type \"yes\"."
  941. :group 'org-link-follow
  942. :type '(choice
  943. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  944. (const :tag "with y-or-n (faster)" y-or-n-p)
  945. (const :tag "no confirmation (dangerous)" nil)))
  946. (defconst org-file-apps-defaults-gnu
  947. '((remote . emacs)
  948. (t . mailcap))
  949. "Default file applications on a UNIX or GNU/Linux system.
  950. See `org-file-apps'.")
  951. (defconst org-file-apps-defaults-macosx
  952. '((remote . emacs)
  953. (t . "open %s")
  954. ("ps" . "gv %s")
  955. ("ps.gz" . "gv %s")
  956. ("eps" . "gv %s")
  957. ("eps.gz" . "gv %s")
  958. ("dvi" . "xdvi %s")
  959. ("fig" . "xfig %s"))
  960. "Default file applications on a MacOS X system.
  961. The system \"open\" is known as a default, but we use X11 applications
  962. for some files for which the OS does not have a good default.
  963. See `org-file-apps'.")
  964. (defconst org-file-apps-defaults-windowsnt
  965. (list
  966. '(remote . emacs)
  967. (cons t
  968. (list (if (featurep 'xemacs)
  969. 'mswindows-shell-execute
  970. 'w32-shell-execute)
  971. "open" 'file)))
  972. "Default file applications on a Windows NT system.
  973. The system \"open\" is used for most files.
  974. See `org-file-apps'.")
  975. (defcustom org-file-apps
  976. '(
  977. ("txt" . emacs)
  978. ("tex" . emacs)
  979. ("ltx" . emacs)
  980. ("org" . emacs)
  981. ("el" . emacs)
  982. ("bib" . emacs)
  983. )
  984. "External applications for opening `file:path' items in a document.
  985. Org-mode uses system defaults for different file types, but
  986. you can use this variable to set the application for a given file
  987. extension. The entries in this list are cons cells where the car identifies
  988. files and the cdr the corresponding command. Possible values for the
  989. file identifier are
  990. \"ext\" A string identifying an extension
  991. `directory' Matches a directory
  992. `remote' Matches a remote file, accessible through tramp or efs.
  993. Remote files most likely should be visited through Emacs
  994. because external applications cannot handle such paths.
  995. t Default for all remaining files
  996. Possible values for the command are:
  997. `emacs' The file will be visited by the current Emacs process.
  998. `default' Use the default application for this file type.
  999. string A command to be executed by a shell; %s will be replaced
  1000. by the path to the file.
  1001. sexp A Lisp form which will be evaluated. The file path will
  1002. be available in the Lisp variable `file'.
  1003. For more examples, see the system specific constants
  1004. `org-file-apps-defaults-macosx'
  1005. `org-file-apps-defaults-windowsnt'
  1006. `org-file-apps-defaults-gnu'."
  1007. :group 'org-link-follow
  1008. :type '(repeat
  1009. (cons (choice :value ""
  1010. (string :tag "Extension")
  1011. (const :tag "Default for unrecognized files" t)
  1012. (const :tag "Remote file" remote)
  1013. (const :tag "Links to a directory" directory))
  1014. (choice :value ""
  1015. (const :tag "Visit with Emacs" emacs)
  1016. (const :tag "Use system default" default)
  1017. (string :tag "Command")
  1018. (sexp :tag "Lisp form")))))
  1019. (defgroup org-refile nil
  1020. "Options concerning refiling entries in Org-mode."
  1021. :tag "Org Remember"
  1022. :group 'org)
  1023. (defcustom org-directory "~/org"
  1024. "Directory with org files.
  1025. This directory will be used as default to prompt for org files.
  1026. Used by the hooks for remember.el."
  1027. :group 'org-refile
  1028. :group 'org-remember
  1029. :type 'directory)
  1030. (defcustom org-default-notes-file "~/.notes"
  1031. "Default target for storing notes.
  1032. Used by the hooks for remember.el. This can be a string, or nil to mean
  1033. the value of `remember-data-file'.
  1034. You can set this on a per-template basis with the variable
  1035. `org-remember-templates'."
  1036. :group 'org-refile
  1037. :group 'org-remember
  1038. :type '(choice
  1039. (const :tag "Default from remember-data-file" nil)
  1040. file))
  1041. (defcustom org-goto-interface 'outline
  1042. "The default interface to be used for `org-goto'.
  1043. Allowed vaues are:
  1044. outline The interface shows an outline of the relevant file
  1045. and the correct heading is found by moving through
  1046. the outline or by searching with incremental search.
  1047. outline-path-completion Headlines in the current buffer are offered via
  1048. completion."
  1049. :group 'org-refile
  1050. :type '(choice
  1051. (const :tag "Outline" outline)
  1052. (const :tag "Outline-path-completion" outline-path-completion)))
  1053. (defcustom org-reverse-note-order nil
  1054. "Non-nil means, store new notes at the beginning of a file or entry.
  1055. When nil, new notes will be filed to the end of a file or entry.
  1056. This can also be a list with cons cells of regular expressions that
  1057. are matched against file names, and values."
  1058. :group 'org-remember
  1059. :type '(choice
  1060. (const :tag "Reverse always" t)
  1061. (const :tag "Reverse never" nil)
  1062. (repeat :tag "By file name regexp"
  1063. (cons regexp boolean))))
  1064. (defcustom org-refile-targets nil
  1065. "Targets for refiling entries with \\[org-refile].
  1066. This is list of cons cells. Each cell contains:
  1067. - a specification of the files to be considered, either a list of files,
  1068. or a symbol whose function or variable value will be used to retrieve
  1069. a file name or a list of file names. Nil means, refile to a different
  1070. heading in the current buffer.
  1071. - A specification of how to find candidate refile targets. This may be
  1072. any of
  1073. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1074. This tag has to be present in all target headlines, inheritance will
  1075. not be considered.
  1076. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1077. todo keyword.
  1078. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1079. headlines that are refiling targets.
  1080. - a cons cell (:level . N). Any headline of level N is considered a target.
  1081. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1082. :group 'org-remember
  1083. :type '(repeat
  1084. (cons
  1085. (choice :value org-agenda-files
  1086. (const :tag "All agenda files" org-agenda-files)
  1087. (const :tag "Current buffer" nil)
  1088. (function) (variable) (file))
  1089. (choice :tag "Identify target headline by"
  1090. (cons :tag "Specific tag" (const :tag) (string))
  1091. (cons :tag "TODO keyword" (const :todo) (string))
  1092. (cons :tag "Regular expression" (const :regexp) (regexp))
  1093. (cons :tag "Level number" (const :level) (integer))
  1094. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1095. (defcustom org-refile-use-outline-path nil
  1096. "Non-nil means, provide refile targets as paths.
  1097. So a level 3 headline will be available as level1/level2/level3.
  1098. When the value is `file', also include the file name (without directory)
  1099. into the path. When `full-file-path', include the full file path."
  1100. :group 'org-remember
  1101. :type '(choice
  1102. (const :tag "Not" nil)
  1103. (const :tag "Yes" t)
  1104. (const :tag "Start with file name" file)
  1105. (const :tag "Start with full file path" full-file-path)))
  1106. (defgroup org-todo nil
  1107. "Options concerning TODO items in Org-mode."
  1108. :tag "Org TODO"
  1109. :group 'org)
  1110. (defgroup org-progress nil
  1111. "Options concerning Progress logging in Org-mode."
  1112. :tag "Org Progress"
  1113. :group 'org-time)
  1114. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1115. "List of TODO entry keyword sequences and their interpretation.
  1116. \\<org-mode-map>This is a list of sequences.
  1117. Each sequence starts with a symbol, either `sequence' or `type',
  1118. indicating if the keywords should be interpreted as a sequence of
  1119. action steps, or as different types of TODO items. The first
  1120. keywords are states requiring action - these states will select a headline
  1121. for inclusion into the global TODO list Org-mode produces. If one of
  1122. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1123. signify that no further action is necessary. If \"|\" is not found,
  1124. the last keyword is treated as the only DONE state of the sequence.
  1125. The command \\[org-todo] cycles an entry through these states, and one
  1126. additional state where no keyword is present. For details about this
  1127. cycling, see the manual.
  1128. TODO keywords and interpretation can also be set on a per-file basis with
  1129. the special #+SEQ_TODO and #+TYP_TODO lines.
  1130. Each keyword can optionally specify a character for fast state selection
  1131. \(in combination with the variable `org-use-fast-todo-selection')
  1132. and specifiers for state change logging, using the same syntax
  1133. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1134. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1135. indicates to record a time stamp each time this state is selected.
  1136. Each keyword may also specify if a timestamp or a note should be
  1137. recorded when entering or leaving the state, by adding additional
  1138. characters in the parenthesis after the keyword. This looks like this:
  1139. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1140. record only the time of the state change. With X and Y being either
  1141. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1142. Y when leaving the state if and only if the *target* state does not
  1143. define X. You may omit any of the fast-selection key or X or /Y,
  1144. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1145. For backward compatibility, this variable may also be just a list
  1146. of keywords - in this case the interptetation (sequence or type) will be
  1147. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1148. :group 'org-todo
  1149. :group 'org-keywords
  1150. :type '(choice
  1151. (repeat :tag "Old syntax, just keywords"
  1152. (string :tag "Keyword"))
  1153. (repeat :tag "New syntax"
  1154. (cons
  1155. (choice
  1156. :tag "Interpretation"
  1157. (const :tag "Sequence (cycling hits every state)" sequence)
  1158. (const :tag "Type (cycling directly to DONE)" type))
  1159. (repeat
  1160. (string :tag "Keyword"))))))
  1161. (defvar org-todo-keywords-1 nil
  1162. "All TODO and DONE keywords active in a buffer.")
  1163. (make-variable-buffer-local 'org-todo-keywords-1)
  1164. (defvar org-todo-keywords-for-agenda nil)
  1165. (defvar org-done-keywords-for-agenda nil)
  1166. (defvar org-agenda-contributing-files nil)
  1167. (defvar org-not-done-keywords nil)
  1168. (make-variable-buffer-local 'org-not-done-keywords)
  1169. (defvar org-done-keywords nil)
  1170. (make-variable-buffer-local 'org-done-keywords)
  1171. (defvar org-todo-heads nil)
  1172. (make-variable-buffer-local 'org-todo-heads)
  1173. (defvar org-todo-sets nil)
  1174. (make-variable-buffer-local 'org-todo-sets)
  1175. (defvar org-todo-log-states nil)
  1176. (make-variable-buffer-local 'org-todo-log-states)
  1177. (defvar org-todo-kwd-alist nil)
  1178. (make-variable-buffer-local 'org-todo-kwd-alist)
  1179. (defvar org-todo-key-alist nil)
  1180. (make-variable-buffer-local 'org-todo-key-alist)
  1181. (defvar org-todo-key-trigger nil)
  1182. (make-variable-buffer-local 'org-todo-key-trigger)
  1183. (defcustom org-todo-interpretation 'sequence
  1184. "Controls how TODO keywords are interpreted.
  1185. This variable is in principle obsolete and is only used for
  1186. backward compatibility, if the interpretation of todo keywords is
  1187. not given already in `org-todo-keywords'. See that variable for
  1188. more information."
  1189. :group 'org-todo
  1190. :group 'org-keywords
  1191. :type '(choice (const sequence)
  1192. (const type)))
  1193. (defcustom org-use-fast-todo-selection 'prefix
  1194. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1195. This variable describes if and under what circumstances the cycling
  1196. mechanism for TODO keywords will be replaced by a single-key, direct
  1197. selection scheme.
  1198. When nil, fast selection is never used.
  1199. When the symbol `prefix', it will be used when `org-todo' is called with
  1200. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1201. in an agenda buffer.
  1202. When t, fast selection is used by default. In this case, the prefix
  1203. argument forces cycling instead.
  1204. In all cases, the special interface is only used if access keys have actually
  1205. been assigned by the user, i.e. if keywords in the configuration are followed
  1206. by a letter in parenthesis, like TODO(t)."
  1207. :group 'org-todo
  1208. :type '(choice
  1209. (const :tag "Never" nil)
  1210. (const :tag "By default" t)
  1211. (const :tag "Only with C-u C-c C-t" prefix)))
  1212. (defcustom org-after-todo-state-change-hook nil
  1213. "Hook which is run after the state of a TODO item was changed.
  1214. The new state (a string with a TODO keyword, or nil) is available in the
  1215. Lisp variable `state'."
  1216. :group 'org-todo
  1217. :type 'hook)
  1218. (defcustom org-log-done nil
  1219. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1220. When equal to the list (done), also prompt for a closing note.
  1221. This can also be configured on a per-file basis by adding one of
  1222. the following lines anywhere in the buffer:
  1223. #+STARTUP: logdone
  1224. #+STARTUP: lognotedone
  1225. #+STARTUP: nologdone"
  1226. :group 'org-todo
  1227. :group 'org-progress
  1228. :type '(choice
  1229. (const :tag "No logging" nil)
  1230. (const :tag "Record CLOSED timestamp" time)
  1231. (const :tag "Record CLOSED timestamp with closing note." note)))
  1232. ;; Normalize old uses of org-log-done.
  1233. (cond
  1234. ((eq org-log-done t) (setq org-log-done 'time))
  1235. ((and (listp org-log-done) (memq 'done org-log-done))
  1236. (setq org-log-done 'note)))
  1237. (defcustom org-log-note-clock-out nil
  1238. "Non-nil means, recored a note when clocking out of an item.
  1239. This can also be configured on a per-file basis by adding one of
  1240. the following lines anywhere in the buffer:
  1241. #+STARTUP: lognoteclock-out
  1242. #+STARTUP: nolognoteclock-out"
  1243. :group 'org-todo
  1244. :group 'org-progress
  1245. :type 'boolean)
  1246. (defcustom org-log-done-with-time t
  1247. "Non-nil means, the CLOSED time stamp will contain date and time.
  1248. When nil, only the date will be recorded."
  1249. :group 'org-progress
  1250. :type 'boolean)
  1251. (defcustom org-log-note-headings
  1252. '((done . "CLOSING NOTE %t")
  1253. (state . "State %-12s %t")
  1254. (note . "Note taken on %t")
  1255. (clock-out . ""))
  1256. "Headings for notes added to entries.
  1257. The value is an alist, with the car being a symbol indicating the note
  1258. context, and the cdr is the heading to be used. The heading may also be the
  1259. empty string.
  1260. %t in the heading will be replaced by a time stamp.
  1261. %s will be replaced by the new TODO state, in double quotes.
  1262. %u will be replaced by the user name.
  1263. %U will be replaced by the full user name."
  1264. :group 'org-todo
  1265. :group 'org-progress
  1266. :type '(list :greedy t
  1267. (cons (const :tag "Heading when closing an item" done) string)
  1268. (cons (const :tag
  1269. "Heading when changing todo state (todo sequence only)"
  1270. state) string)
  1271. (cons (const :tag "Heading when just taking a note" note) string)
  1272. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1273. (unless (assq 'note org-log-note-headings)
  1274. (push '(note . "%t") org-log-note-headings))
  1275. (defcustom org-log-states-order-reversed t
  1276. "Non-nil means, the latest state change note will be directly after heading.
  1277. When nil, the notes will be orderer according to time."
  1278. :group 'org-todo
  1279. :group 'org-progress
  1280. :type 'boolean)
  1281. (defcustom org-log-repeat 'time
  1282. "Non-nil means, record moving through the DONE state when triggering repeat.
  1283. An auto-repeating tasks is immediately switched back to TODO when marked
  1284. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1285. the TODO keyword definition, or recording a cloing note by setting
  1286. `org-log-done', there will be no record of the task moving trhough DONE.
  1287. This variable forces taking a note anyway. Possible values are:
  1288. nil Don't force a record
  1289. time Record a time stamp
  1290. note Record a note
  1291. This option can also be set with on a per-file-basis with
  1292. #+STARTUP: logrepeat
  1293. #+STARTUP: lognoterepeat
  1294. #+STARTUP: nologrepeat
  1295. You can have local logging settings for a subtree by setting the LOGGING
  1296. property to one or more of these keywords."
  1297. :group 'org-todo
  1298. :group 'org-progress
  1299. :type '(choice
  1300. (const :tag "Don't force a record" nil)
  1301. (const :tag "Force recording the DONE state" time)
  1302. (const :tag "Force recording a note with the DONE state" note)))
  1303. (defgroup org-priorities nil
  1304. "Priorities in Org-mode."
  1305. :tag "Org Priorities"
  1306. :group 'org-todo)
  1307. (defcustom org-highest-priority ?A
  1308. "The highest priority of TODO items. A character like ?A, ?B etc.
  1309. Must have a smaller ASCII number than `org-lowest-priority'."
  1310. :group 'org-priorities
  1311. :type 'character)
  1312. (defcustom org-lowest-priority ?C
  1313. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1314. Must have a larger ASCII number than `org-highest-priority'."
  1315. :group 'org-priorities
  1316. :type 'character)
  1317. (defcustom org-default-priority ?B
  1318. "The default priority of TODO items.
  1319. This is the priority an item get if no explicit priority is given."
  1320. :group 'org-priorities
  1321. :type 'character)
  1322. (defcustom org-priority-start-cycle-with-default t
  1323. "Non-nil means, start with default priority when starting to cycle.
  1324. When this is nil, the first step in the cycle will be (depending on the
  1325. command used) one higher or lower that the default priority."
  1326. :group 'org-priorities
  1327. :type 'boolean)
  1328. (defgroup org-time nil
  1329. "Options concerning time stamps and deadlines in Org-mode."
  1330. :tag "Org Time"
  1331. :group 'org)
  1332. (defcustom org-insert-labeled-timestamps-at-point nil
  1333. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1334. When nil, these labeled time stamps are forces into the second line of an
  1335. entry, just after the headline. When scheduling from the global TODO list,
  1336. the time stamp will always be forced into the second line."
  1337. :group 'org-time
  1338. :type 'boolean)
  1339. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1340. "Formats for `format-time-string' which are used for time stamps.
  1341. It is not recommended to change this constant.")
  1342. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1343. "Number of minutes to round time stamps to.
  1344. These are two values, the first applies when first creating a time stamp.
  1345. The second applies when changing it with the commands `S-up' and `S-down'.
  1346. When changing the time stamp, this means that it will change in steps
  1347. of N minutes, as given by the second value.
  1348. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1349. numbers should be factors of 60, so for example 5, 10, 15.
  1350. When this is larger than 1, you can still force an exact time-stamp by using
  1351. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1352. and by using a prefix arg to `S-up/down' to specify the exact number
  1353. of minutes to shift."
  1354. :group 'org-time
  1355. :get '(lambda (var) ; Make sure all entries have 5 elements
  1356. (if (integerp (default-value var))
  1357. (list (default-value var) 5)
  1358. (default-value var)))
  1359. :type '(list
  1360. (integer :tag "when inserting times")
  1361. (integer :tag "when modifying times")))
  1362. ;; Normalize old customizations of this variable.
  1363. (when (integerp org-time-stamp-rounding-minutes)
  1364. (setq org-time-stamp-rounding-minutes
  1365. (list org-time-stamp-rounding-minutes
  1366. org-time-stamp-rounding-minutes)))
  1367. (defcustom org-display-custom-times nil
  1368. "Non-nil means, overlay custom formats over all time stamps.
  1369. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1370. To turn this on on a per-file basis, insert anywhere in the file:
  1371. #+STARTUP: customtime"
  1372. :group 'org-time
  1373. :set 'set-default
  1374. :type 'sexp)
  1375. (make-variable-buffer-local 'org-display-custom-times)
  1376. (defcustom org-time-stamp-custom-formats
  1377. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1378. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1379. These are overlayed over the default ISO format if the variable
  1380. `org-display-custom-times' is set. Time like %H:%M should be at the
  1381. end of the second format."
  1382. :group 'org-time
  1383. :type 'sexp)
  1384. (defun org-time-stamp-format (&optional long inactive)
  1385. "Get the right format for a time string."
  1386. (let ((f (if long (cdr org-time-stamp-formats)
  1387. (car org-time-stamp-formats))))
  1388. (if inactive
  1389. (concat "[" (substring f 1 -1) "]")
  1390. f)))
  1391. (defcustom org-deadline-warning-days 14
  1392. "No. of days before expiration during which a deadline becomes active.
  1393. This variable governs the display in sparse trees and in the agenda.
  1394. When 0 or negative, it means use this number (the absolute value of it)
  1395. even if a deadline has a different individual lead time specified."
  1396. :group 'org-time
  1397. :group 'org-agenda-daily/weekly
  1398. :type 'number)
  1399. (defcustom org-read-date-prefer-future t
  1400. "Non-nil means, assume future for incomplete date input from user.
  1401. This affects the following situations:
  1402. 1. The user gives a day, but no month.
  1403. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1404. read this as the third of *next* month. However, if you enter \"17\",
  1405. it will be considered as *this* month.
  1406. 2. The user gives a month but not a year.
  1407. For example, if it is april and you enter \"feb 2\", this will be read
  1408. as feb 2, *next* year. \"May 5\", however, will be this year.
  1409. Currently this does not work for ISO week specifications.
  1410. When this option is nil, the current month and year will always be used
  1411. as defaults."
  1412. :group 'org-time
  1413. :type 'boolean)
  1414. (defcustom org-read-date-display-live t
  1415. "Non-nil means, display current interpretation of date prompt live.
  1416. This display will be in an overlay, in the minibuffer."
  1417. :group 'org-time
  1418. :type 'boolean)
  1419. (defcustom org-read-date-popup-calendar t
  1420. "Non-nil means, pop up a calendar when prompting for a date.
  1421. In the calendar, the date can be selected with mouse-1. However, the
  1422. minibuffer will also be active, and you can simply enter the date as well.
  1423. When nil, only the minibuffer will be available."
  1424. :group 'org-time
  1425. :type 'boolean)
  1426. (if (fboundp 'defvaralias)
  1427. (defvaralias 'org-popup-calendar-for-date-prompt
  1428. 'org-read-date-popup-calendar))
  1429. (defcustom org-extend-today-until 0
  1430. "The hour when your day really ends.
  1431. This has influence for the following applications:
  1432. - When switching the agenda to \"today\". It it is still earlier than
  1433. the time given here, the day recognized as TODAY is actually yesterday.
  1434. - When a date is read from the user and it is still before the time given
  1435. here, the current date and time will be assumed to be yesterday, 23:59.
  1436. FIXME:
  1437. IMPORTANT: This is still a very experimental feature, it may disappear
  1438. again or it may be extended to mean more things."
  1439. :group 'org-time
  1440. :type 'number)
  1441. (defcustom org-edit-timestamp-down-means-later nil
  1442. "Non-nil means, S-down will increase the time in a time stamp.
  1443. When nil, S-up will increase."
  1444. :group 'org-time
  1445. :type 'boolean)
  1446. (defcustom org-calendar-follow-timestamp-change t
  1447. "Non-nil means, make the calendar window follow timestamp changes.
  1448. When a timestamp is modified and the calendar window is visible, it will be
  1449. moved to the new date."
  1450. :group 'org-time
  1451. :type 'boolean)
  1452. (defgroup org-tags nil
  1453. "Options concerning tags in Org-mode."
  1454. :tag "Org Tags"
  1455. :group 'org)
  1456. (defcustom org-tag-alist nil
  1457. "List of tags allowed in Org-mode files.
  1458. When this list is nil, Org-mode will base TAG input on what is already in the
  1459. buffer.
  1460. The value of this variable is an alist, the car of each entry must be a
  1461. keyword as a string, the cdr may be a character that is used to select
  1462. that tag through the fast-tag-selection interface.
  1463. See the manual for details."
  1464. :group 'org-tags
  1465. :type '(repeat
  1466. (choice
  1467. (cons (string :tag "Tag name")
  1468. (character :tag "Access char"))
  1469. (const :tag "Start radio group" (:startgroup))
  1470. (const :tag "End radio group" (:endgroup)))))
  1471. (defcustom org-use-fast-tag-selection 'auto
  1472. "Non-nil means, use fast tag selection scheme.
  1473. This is a special interface to select and deselect tags with single keys.
  1474. When nil, fast selection is never used.
  1475. When the symbol `auto', fast selection is used if and only if selection
  1476. characters for tags have been configured, either through the variable
  1477. `org-tag-alist' or through a #+TAGS line in the buffer.
  1478. When t, fast selection is always used and selection keys are assigned
  1479. automatically if necessary."
  1480. :group 'org-tags
  1481. :type '(choice
  1482. (const :tag "Always" t)
  1483. (const :tag "Never" nil)
  1484. (const :tag "When selection characters are configured" 'auto)))
  1485. (defcustom org-fast-tag-selection-single-key nil
  1486. "Non-nil means, fast tag selection exits after first change.
  1487. When nil, you have to press RET to exit it.
  1488. During fast tag selection, you can toggle this flag with `C-c'.
  1489. This variable can also have the value `expert'. In this case, the window
  1490. displaying the tags menu is not even shown, until you press C-c again."
  1491. :group 'org-tags
  1492. :type '(choice
  1493. (const :tag "No" nil)
  1494. (const :tag "Yes" t)
  1495. (const :tag "Expert" expert)))
  1496. (defvar org-fast-tag-selection-include-todo nil
  1497. "Non-nil means, fast tags selection interface will also offer TODO states.
  1498. This is an undocumented feature, you should not rely on it.")
  1499. (defcustom org-tags-column -80
  1500. "The column to which tags should be indented in a headline.
  1501. If this number is positive, it specifies the column. If it is negative,
  1502. it means that the tags should be flushright to that column. For example,
  1503. -80 works well for a normal 80 character screen."
  1504. :group 'org-tags
  1505. :type 'integer)
  1506. (defcustom org-auto-align-tags t
  1507. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1508. These operations change the length of a headline and therefore shift
  1509. the tags around. With this options turned on, after each such operation
  1510. the tags are again aligned to `org-tags-column'."
  1511. :group 'org-tags
  1512. :type 'boolean)
  1513. (defcustom org-use-tag-inheritance t
  1514. "Non-nil means, tags in levels apply also for sublevels.
  1515. When nil, only the tags directly given in a specific line apply there.
  1516. If you turn off this option, you very likely want to turn on the
  1517. companion option `org-tags-match-list-sublevels'.
  1518. This may also be a list of tags that should be inherited, or a regexp that
  1519. matches tags that should be inherited."
  1520. :group 'org-tags
  1521. :type '(choice
  1522. (const :tag "Not" nil)
  1523. (const :tag "Always" t)
  1524. (repeat :tag "Specific tags" (string :tag "Tag"))
  1525. (regexp :tag "Tags matched by regexp")))
  1526. (defun org-tag-inherit-p (tag)
  1527. "Check if TAG is one that should be inherited."
  1528. (cond
  1529. ((eq org-use-tag-inheritance t) t)
  1530. ((not org-use-tag-inheritance) nil)
  1531. ((stringp org-use-tag-inheritance)
  1532. (string-match org-use-tag-inheritance tag))
  1533. ((listp org-use-tag-inheritance)
  1534. (member tag org-use-tag-inheritance))
  1535. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1536. (defcustom org-tags-match-list-sublevels nil
  1537. "Non-nil means list also sublevels of headlines matching tag search.
  1538. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1539. the sublevels of a headline matching a tag search often also match
  1540. the same search. Listing all of them can create very long lists.
  1541. Setting this variable to nil causes subtrees of a match to be skipped.
  1542. This option is off by default, because inheritance in on. If you turn
  1543. inheritance off, you very likely want to turn this option on.
  1544. As a special case, if the tag search is restricted to TODO items, the
  1545. value of this variable is ignored and sublevels are always checked, to
  1546. make sure all corresponding TODO items find their way into the list."
  1547. :group 'org-tags
  1548. :type 'boolean)
  1549. (defvar org-tags-history nil
  1550. "History of minibuffer reads for tags.")
  1551. (defvar org-last-tags-completion-table nil
  1552. "The last used completion table for tags.")
  1553. (defvar org-after-tags-change-hook nil
  1554. "Hook that is run after the tags in a line have changed.")
  1555. (defgroup org-properties nil
  1556. "Options concerning properties in Org-mode."
  1557. :tag "Org Properties"
  1558. :group 'org)
  1559. (defcustom org-property-format "%-10s %s"
  1560. "How property key/value pairs should be formatted by `indent-line'.
  1561. When `indent-line' hits a property definition, it will format the line
  1562. according to this format, mainly to make sure that the values are
  1563. lined-up with respect to each other."
  1564. :group 'org-properties
  1565. :type 'string)
  1566. (defcustom org-use-property-inheritance nil
  1567. "Non-nil means, properties apply also for sublevels.
  1568. This setting is chiefly used during property searches. Turning it on can
  1569. cause significant overhead when doing a search, which is why it is not
  1570. on by default.
  1571. When nil, only the properties directly given in the current entry count.
  1572. When t, every property is inherited. The value may also be a list of
  1573. properties that should have inheritance, or a regular expression matching
  1574. properties that should be inherited.
  1575. However, note that some special properties use inheritance under special
  1576. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1577. and the properties ending in \"_ALL\" when they are used as descriptor
  1578. for valid values of a property.
  1579. Note for programmers:
  1580. When querying an entry with `org-entry-get', you can control if inheritance
  1581. should be used. By default, `org-entry-get' looks only at the local
  1582. properties. You can request inheritance by setting the inherit argument
  1583. to t (to force inheritance) or to `selective' (to respect the setting
  1584. in this variable)."
  1585. :group 'org-properties
  1586. :type '(choice
  1587. (const :tag "Not" nil)
  1588. (const :tag "Always" t)
  1589. (repeat :tag "Specific properties" (string :tag "Property"))
  1590. (regexp :tag "Properties matched by regexp")))
  1591. (defun org-property-inherit-p (property)
  1592. "Check if PROPERTY is one that should be inherited."
  1593. (cond
  1594. ((eq org-use-property-inheritance t) t)
  1595. ((not org-use-property-inheritance) nil)
  1596. ((stringp org-use-property-inheritance)
  1597. (string-match org-use-property-inheritance property))
  1598. ((listp org-use-property-inheritance)
  1599. (member property org-use-property-inheritance))
  1600. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1601. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1602. "The default column format, if no other format has been defined.
  1603. This variable can be set on the per-file basis by inserting a line
  1604. #+COLUMNS: %25ITEM ....."
  1605. :group 'org-properties
  1606. :type 'string)
  1607. (defcustom org-effort-property "Effort"
  1608. "The property that is being used to keep track of effort estimates.
  1609. Effort estimates given in this property need to have the format H:MM."
  1610. :group 'org-properties
  1611. :group 'org-progress
  1612. :type '(string :tag "Property"))
  1613. (defcustom org-global-properties nil
  1614. "List of property/value pairs that can be inherited by any entry.
  1615. You can set buffer-local values for this by adding lines like
  1616. #+PROPERTY: NAME VALUE"
  1617. :group 'org-properties
  1618. :type '(repeat
  1619. (cons (string :tag "Property")
  1620. (string :tag "Value"))))
  1621. (defvar org-local-properties nil
  1622. "List of property/value pairs that can be inherited by any entry.
  1623. Valid for the current buffer.
  1624. This variable is populated from #+PROPERTY lines.")
  1625. (defgroup org-agenda nil
  1626. "Options concerning agenda views in Org-mode."
  1627. :tag "Org Agenda"
  1628. :group 'org)
  1629. (defvar org-category nil
  1630. "Variable used by org files to set a category for agenda display.
  1631. Such files should use a file variable to set it, for example
  1632. # -*- mode: org; org-category: \"ELisp\"
  1633. or contain a special line
  1634. #+CATEGORY: ELisp
  1635. If the file does not specify a category, then file's base name
  1636. is used instead.")
  1637. (make-variable-buffer-local 'org-category)
  1638. (defcustom org-agenda-files nil
  1639. "The files to be used for agenda display.
  1640. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1641. \\[org-remove-file]. You can also use customize to edit the list.
  1642. If an entry is a directory, all files in that directory that are matched by
  1643. `org-agenda-file-regexp' will be part of the file list.
  1644. If the value of the variable is not a list but a single file name, then
  1645. the list of agenda files is actually stored and maintained in that file, one
  1646. agenda file per line."
  1647. :group 'org-agenda
  1648. :type '(choice
  1649. (repeat :tag "List of files and directories" file)
  1650. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1651. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1652. "Regular expression to match files for `org-agenda-files'.
  1653. If any element in the list in that variable contains a directory instead
  1654. of a normal file, all files in that directory that are matched by this
  1655. regular expression will be included."
  1656. :group 'org-agenda
  1657. :type 'regexp)
  1658. (defcustom org-agenda-text-search-extra-files nil
  1659. "List of extra files to be searched by text search commands.
  1660. These files will be search in addition to the agenda files by the
  1661. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1662. Note that these files will only be searched for text search commands,
  1663. not for the other agenda views like todo lists, tag searches or the weekly
  1664. agenda. This variable is intended to list notes and possibly archive files
  1665. that should also be searched by these two commands.
  1666. In fact, if the first element in the list is the symbol `agenda-archives',
  1667. than all archive files of all agenda files will be added to the search
  1668. scope."
  1669. :group 'org-agenda
  1670. :type '(set :greedy t
  1671. (const :tag "Agenda Archives" agenda-archives)
  1672. (repeat :inline t (file))))
  1673. (if (fboundp 'defvaralias)
  1674. (defvaralias 'org-agenda-multi-occur-extra-files
  1675. 'org-agenda-text-search-extra-files))
  1676. (defcustom org-agenda-skip-unavailable-files nil
  1677. "t means to just skip non-reachable files in `org-agenda-files'.
  1678. Nil means to remove them, after a query, from the list."
  1679. :group 'org-agenda
  1680. :type 'boolean)
  1681. (defcustom org-calendar-to-agenda-key [?c]
  1682. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1683. The command `org-calendar-goto-agenda' will be bound to this key. The
  1684. default is the character `c' because then `c' can be used to switch back and
  1685. forth between agenda and calendar."
  1686. :group 'org-agenda
  1687. :type 'sexp)
  1688. (eval-after-load "calendar"
  1689. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1690. 'org-calendar-goto-agenda))
  1691. (defgroup org-latex nil
  1692. "Options for embedding LaTeX code into Org-mode."
  1693. :tag "Org LaTeX"
  1694. :group 'org)
  1695. (defcustom org-format-latex-options
  1696. '(:foreground default :background default :scale 1.0
  1697. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1698. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1699. "Options for creating images from LaTeX fragments.
  1700. This is a property list with the following properties:
  1701. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1702. `default' means use the forground of the default face.
  1703. :background the background color, or \"Transparent\".
  1704. `default' means use the background of the default face.
  1705. :scale a scaling factor for the size of the images
  1706. :html-foreground, :html-background, :html-scale
  1707. The same numbers for HTML export.
  1708. :matchers a list indicating which matchers should be used to
  1709. find LaTeX fragments. Valid members of this list are:
  1710. \"begin\" find environments
  1711. \"$\" find math expressions surrounded by $...$
  1712. \"$$\" find math expressions surrounded by $$....$$
  1713. \"\\(\" find math expressions surrounded by \\(...\\)
  1714. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1715. :group 'org-latex
  1716. :type 'plist)
  1717. (defcustom org-format-latex-header "\\documentclass{article}
  1718. \\usepackage{fullpage} % do not remove
  1719. \\usepackage{amssymb}
  1720. \\usepackage[usenames]{color}
  1721. \\usepackage{amsmath}
  1722. \\usepackage{latexsym}
  1723. \\usepackage[mathscr]{eucal}
  1724. \\pagestyle{empty} % do not remove"
  1725. "The document header used for processing LaTeX fragments."
  1726. :group 'org-latex
  1727. :type 'string)
  1728. (defgroup org-font-lock nil
  1729. "Font-lock settings for highlighting in Org-mode."
  1730. :tag "Org Font Lock"
  1731. :group 'org)
  1732. (defcustom org-level-color-stars-only nil
  1733. "Non-nil means fontify only the stars in each headline.
  1734. When nil, the entire headline is fontified.
  1735. Changing it requires restart of `font-lock-mode' to become effective
  1736. also in regions already fontified."
  1737. :group 'org-font-lock
  1738. :type 'boolean)
  1739. (defcustom org-hide-leading-stars nil
  1740. "Non-nil means, hide the first N-1 stars in a headline.
  1741. This works by using the face `org-hide' for these stars. This
  1742. face is white for a light background, and black for a dark
  1743. background. You may have to customize the face `org-hide' to
  1744. make this work.
  1745. Changing it requires restart of `font-lock-mode' to become effective
  1746. also in regions already fontified.
  1747. You may also set this on a per-file basis by adding one of the following
  1748. lines to the buffer:
  1749. #+STARTUP: hidestars
  1750. #+STARTUP: showstars"
  1751. :group 'org-font-lock
  1752. :type 'boolean)
  1753. (defcustom org-fontify-done-headline nil
  1754. "Non-nil means, change the face of a headline if it is marked DONE.
  1755. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1756. When this is non-nil, the headline after the keyword is set to the
  1757. `org-headline-done' as an additional indication."
  1758. :group 'org-font-lock
  1759. :type 'boolean)
  1760. (defcustom org-fontify-emphasized-text t
  1761. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1762. Changing this variable requires a restart of Emacs to take effect."
  1763. :group 'org-font-lock
  1764. :type 'boolean)
  1765. (defcustom org-highlight-latex-fragments-and-specials nil
  1766. "Non-nil means, fontify what is treated specially by the exporters."
  1767. :group 'org-font-lock
  1768. :type 'boolean)
  1769. (defcustom org-hide-emphasis-markers nil
  1770. "Non-nil mean font-lock should hide the emphasis marker characters."
  1771. :group 'org-font-lock
  1772. :type 'boolean)
  1773. (defvar org-emph-re nil
  1774. "Regular expression for matching emphasis.")
  1775. (defvar org-verbatim-re nil
  1776. "Regular expression for matching verbatim text.")
  1777. (defvar org-emphasis-regexp-components) ; defined just below
  1778. (defvar org-emphasis-alist) ; defined just below
  1779. (defun org-set-emph-re (var val)
  1780. "Set variable and compute the emphasis regular expression."
  1781. (set var val)
  1782. (when (and (boundp 'org-emphasis-alist)
  1783. (boundp 'org-emphasis-regexp-components)
  1784. org-emphasis-alist org-emphasis-regexp-components)
  1785. (let* ((e org-emphasis-regexp-components)
  1786. (pre (car e))
  1787. (post (nth 1 e))
  1788. (border (nth 2 e))
  1789. (body (nth 3 e))
  1790. (nl (nth 4 e))
  1791. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1792. (body1 (concat body "*?"))
  1793. (markers (mapconcat 'car org-emphasis-alist ""))
  1794. (vmarkers (mapconcat
  1795. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1796. org-emphasis-alist "")))
  1797. ;; make sure special characters appear at the right position in the class
  1798. (if (string-match "\\^" markers)
  1799. (setq markers (concat (replace-match "" t t markers) "^")))
  1800. (if (string-match "-" markers)
  1801. (setq markers (concat (replace-match "" t t markers) "-")))
  1802. (if (string-match "\\^" vmarkers)
  1803. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1804. (if (string-match "-" vmarkers)
  1805. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1806. (if (> nl 0)
  1807. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1808. (int-to-string nl) "\\}")))
  1809. ;; Make the regexp
  1810. (setq org-emph-re
  1811. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1812. "\\("
  1813. "\\([" markers "]\\)"
  1814. "\\("
  1815. "[^" border "]\\|"
  1816. "[^" border (if (and nil stacked) markers) "]"
  1817. body1
  1818. "[^" border (if (and nil stacked) markers) "]"
  1819. "\\)"
  1820. "\\3\\)"
  1821. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1822. (setq org-verbatim-re
  1823. (concat "\\([" pre "]\\|^\\)"
  1824. "\\("
  1825. "\\([" vmarkers "]\\)"
  1826. "\\("
  1827. "[^" border "]\\|"
  1828. "[^" border "]"
  1829. body1
  1830. "[^" border "]"
  1831. "\\)"
  1832. "\\3\\)"
  1833. "\\([" post "]\\|$\\)")))))
  1834. (defcustom org-emphasis-regexp-components
  1835. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1836. "Components used to build the regular expression for emphasis.
  1837. This is a list with 6 entries. Terminology: In an emphasis string
  1838. like \" *strong word* \", we call the initial space PREMATCH, the final
  1839. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1840. and \"trong wor\" is the body. The different components in this variable
  1841. specify what is allowed/forbidden in each part:
  1842. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1843. post Chars allowed as postmatch. End of line will be allowed too.
  1844. border The chars *forbidden* as border characters.
  1845. body-regexp A regexp like \".\" to match a body character. Don't use
  1846. non-shy groups here, and don't allow newline here.
  1847. newline The maximum number of newlines allowed in an emphasis exp.
  1848. Use customize to modify this, or restart Emacs after changing it."
  1849. :group 'org-font-lock
  1850. :set 'org-set-emph-re
  1851. :type '(list
  1852. (sexp :tag "Allowed chars in pre ")
  1853. (sexp :tag "Allowed chars in post ")
  1854. (sexp :tag "Forbidden chars in border ")
  1855. (sexp :tag "Regexp for body ")
  1856. (integer :tag "number of newlines allowed")
  1857. (option (boolean :tag "Stacking (DISABLED) "))))
  1858. (defcustom org-emphasis-alist
  1859. `(("*" bold "<b>" "</b>")
  1860. ("/" italic "<i>" "</i>")
  1861. ("_" underline "<u>" "</u>")
  1862. ("=" org-code "<code>" "</code>" verbatim)
  1863. ("~" org-verbatim "" "" verbatim)
  1864. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1865. "<del>" "</del>")
  1866. )
  1867. "Special syntax for emphasized text.
  1868. Text starting and ending with a special character will be emphasized, for
  1869. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1870. characters, the face to be used by font-lock for highlighting in Org-mode
  1871. Emacs buffers, and the HTML tags to be used for this.
  1872. Use customize to modify this, or restart Emacs after changing it."
  1873. :group 'org-font-lock
  1874. :set 'org-set-emph-re
  1875. :type '(repeat
  1876. (list
  1877. (string :tag "Marker character")
  1878. (choice
  1879. (face :tag "Font-lock-face")
  1880. (plist :tag "Face property list"))
  1881. (string :tag "HTML start tag")
  1882. (string :tag "HTML end tag")
  1883. (option (const verbatim)))))
  1884. ;;; Miscellaneous options
  1885. (defgroup org-completion nil
  1886. "Completion in Org-mode."
  1887. :tag "Org Completion"
  1888. :group 'org)
  1889. (defcustom org-completion-fallback-command 'hippie-expand
  1890. "The expansion command called by \\[org-complete] in normal context.
  1891. Normal means, no org-mode-specific context."
  1892. :group 'org-completion
  1893. :type 'function)
  1894. ;;; Functions and variables from ther packages
  1895. ;; Declared here to avoid compiler warnings
  1896. ;; XEmacs only
  1897. (defvar outline-mode-menu-heading)
  1898. (defvar outline-mode-menu-show)
  1899. (defvar outline-mode-menu-hide)
  1900. (defvar zmacs-regions) ; XEmacs regions
  1901. ;; Emacs only
  1902. (defvar mark-active)
  1903. ;; Various packages
  1904. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  1905. (declare-function calendar-forward-day "cal-move" (arg))
  1906. (declare-function calendar-goto-date "cal-move" (date))
  1907. (declare-function calendar-goto-today "cal-move" ())
  1908. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  1909. (defvar calc-embedded-close-formula)
  1910. (defvar calc-embedded-open-formula)
  1911. (declare-function cdlatex-tab "ext:cdlatex" ())
  1912. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1913. (defvar font-lock-unfontify-region-function)
  1914. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1915. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1916. (defvar iswitchb-temp-buflist)
  1917. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1918. (declare-function org-agenda-skip "org-agenda" ())
  1919. (declare-function org-format-agenda-item "org-agenda"
  1920. (extra txt &optional category tags dotime noprefix remove-re))
  1921. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1922. (declare-function org-agenda-change-all-lines "org-agenda"
  1923. (newhead hdmarker &optional fixface))
  1924. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1925. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1926. (declare-function parse-time-string "parse-time" (string))
  1927. (declare-function remember "remember" (&optional initial))
  1928. (declare-function remember-buffer-desc "remember" ())
  1929. (declare-function remember-finalize "remember" ())
  1930. (defvar remember-save-after-remembering)
  1931. (defvar remember-data-file)
  1932. (defvar remember-register)
  1933. (defvar remember-buffer)
  1934. (defvar remember-handler-functions)
  1935. (defvar remember-annotation-functions)
  1936. (defvar texmathp-why)
  1937. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1938. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1939. (defvar w3m-current-url)
  1940. (defvar w3m-current-title)
  1941. (defvar org-latex-regexps)
  1942. ;;; Autoload and prepare some org modules
  1943. ;; Some table stuff that needs to be defined here, because it is used
  1944. ;; by the functions setting up org-mode or checking for table context.
  1945. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1946. "Detects an org-type or table-type table.")
  1947. (defconst org-table-line-regexp "^[ \t]*|"
  1948. "Detects an org-type table line.")
  1949. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1950. "Detects an org-type table line.")
  1951. (defconst org-table-hline-regexp "^[ \t]*|-"
  1952. "Detects an org-type table hline.")
  1953. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1954. "Detects a table-type table hline.")
  1955. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1956. "Searching from within a table (any type) this finds the first line
  1957. outside the table.")
  1958. ;; Autoload the functions in org-table.el that are needed by functions here.
  1959. (eval-and-compile
  1960. (org-autoload "org-table"
  1961. '(org-table-align org-table-begin org-table-blank-field
  1962. org-table-convert org-table-convert-region org-table-copy-down
  1963. org-table-copy-region org-table-create
  1964. org-table-create-or-convert-from-region
  1965. org-table-create-with-table.el org-table-current-dline
  1966. org-table-cut-region org-table-delete-column org-table-edit-field
  1967. org-table-edit-formulas org-table-end org-table-eval-formula
  1968. org-table-export org-table-field-info
  1969. org-table-get-stored-formulas org-table-goto-column
  1970. org-table-hline-and-move org-table-import org-table-insert-column
  1971. org-table-insert-hline org-table-insert-row org-table-iterate
  1972. org-table-justify-field-maybe org-table-kill-row
  1973. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1974. org-table-move-column org-table-move-column-left
  1975. org-table-move-column-right org-table-move-row
  1976. org-table-move-row-down org-table-move-row-up
  1977. org-table-next-field org-table-next-row org-table-paste-rectangle
  1978. org-table-previous-field org-table-recalculate
  1979. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1980. org-table-toggle-coordinate-overlays
  1981. org-table-toggle-formula-debugger org-table-wrap-region
  1982. orgtbl-mode turn-on-orgtbl)))
  1983. (defun org-at-table-p (&optional table-type)
  1984. "Return t if the cursor is inside an org-type table.
  1985. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  1986. (if org-enable-table-editor
  1987. (save-excursion
  1988. (beginning-of-line 1)
  1989. (looking-at (if table-type org-table-any-line-regexp
  1990. org-table-line-regexp)))
  1991. nil))
  1992. (defsubst org-table-p () (org-at-table-p))
  1993. (defun org-at-table.el-p ()
  1994. "Return t if and only if we are at a table.el table."
  1995. (and (org-at-table-p 'any)
  1996. (save-excursion
  1997. (goto-char (org-table-begin 'any))
  1998. (looking-at org-table1-hline-regexp))))
  1999. (defun org-table-recognize-table.el ()
  2000. "If there is a table.el table nearby, recognize it and move into it."
  2001. (if org-table-tab-recognizes-table.el
  2002. (if (org-at-table.el-p)
  2003. (progn
  2004. (beginning-of-line 1)
  2005. (if (looking-at org-table-dataline-regexp)
  2006. nil
  2007. (if (looking-at org-table1-hline-regexp)
  2008. (progn
  2009. (beginning-of-line 2)
  2010. (if (looking-at org-table-any-border-regexp)
  2011. (beginning-of-line -1)))))
  2012. (if (re-search-forward "|" (org-table-end t) t)
  2013. (progn
  2014. (require 'table)
  2015. (if (table--at-cell-p (point))
  2016. t
  2017. (message "recognizing table.el table...")
  2018. (table-recognize-table)
  2019. (message "recognizing table.el table...done")))
  2020. (error "This should not happen..."))
  2021. t)
  2022. nil)
  2023. nil))
  2024. (defun org-at-table-hline-p ()
  2025. "Return t if the cursor is inside a hline in a table."
  2026. (if org-enable-table-editor
  2027. (save-excursion
  2028. (beginning-of-line 1)
  2029. (looking-at org-table-hline-regexp))
  2030. nil))
  2031. (defvar org-table-clean-did-remove-column nil)
  2032. (defun org-table-map-tables (function)
  2033. "Apply FUNCTION to the start of all tables in the buffer."
  2034. (save-excursion
  2035. (save-restriction
  2036. (widen)
  2037. (goto-char (point-min))
  2038. (while (re-search-forward org-table-any-line-regexp nil t)
  2039. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2040. (beginning-of-line 1)
  2041. (if (looking-at org-table-line-regexp)
  2042. (save-excursion (funcall function)))
  2043. (re-search-forward org-table-any-border-regexp nil 1))))
  2044. (message "Mapping tables: done"))
  2045. ;; Declare and autoload functions from org-exp.el
  2046. (declare-function org-default-export-plist "org-exp")
  2047. (declare-function org-infile-export-plist "org-exp")
  2048. (declare-function org-get-current-options "org-exp")
  2049. (eval-and-compile
  2050. (org-autoload "org-exp"
  2051. '(org-export org-export-as-ascii org-export-visible
  2052. org-insert-export-options-template org-export-as-html-and-open
  2053. org-export-as-html-batch org-export-as-html-to-buffer
  2054. org-replace-region-by-html org-export-region-as-html
  2055. org-export-as-html org-export-icalendar-this-file
  2056. org-export-icalendar-all-agenda-files
  2057. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2058. ;; Declare and autoload functions from org-exp.el
  2059. (eval-and-compile
  2060. (org-autoload "org-exp"
  2061. '(org-agenda org-agenda-list org-search-view
  2062. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2063. org-diary org-agenda-to-appt)))
  2064. ;; Autoload org-remember
  2065. (eval-and-compile
  2066. (org-autoload "org-remember"
  2067. '(org-remember-insinuate org-remember-annotation
  2068. org-remember-apply-template org-remember org-remember-handler)))
  2069. ;; Autoload org-clock.el
  2070. (defvar org-clock-marker (make-marker)
  2071. "Marker recording the last clock-in.")
  2072. (eval-and-compile
  2073. (org-autoload
  2074. "org-clock"
  2075. '(org-clock-in org-clock-out org-clock-cancel
  2076. org-clock-goto org-clock-sum org-clock-display
  2077. org-remove-clock-overlays org-clock-report
  2078. org-clocktable-shift org-dblock-write:clocktable
  2079. org-get-clocktable)))
  2080. (defun org-clock-update-time-maybe ()
  2081. "If this is a CLOCK line, update it and return t.
  2082. Otherwise, return nil."
  2083. (interactive)
  2084. (save-excursion
  2085. (beginning-of-line 1)
  2086. (skip-chars-forward " \t")
  2087. (when (looking-at org-clock-string)
  2088. (let ((re (concat "[ \t]*" org-clock-string
  2089. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2090. "\\([ \t]*=>.*\\)?"))
  2091. ts te h m s)
  2092. (if (not (looking-at re))
  2093. nil
  2094. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2095. (end-of-line 1)
  2096. (setq ts (match-string 1)
  2097. te (match-string 2))
  2098. (setq s (- (time-to-seconds
  2099. (apply 'encode-time (org-parse-time-string te)))
  2100. (time-to-seconds
  2101. (apply 'encode-time (org-parse-time-string ts))))
  2102. h (floor (/ s 3600))
  2103. s (- s (* 3600 h))
  2104. m (floor (/ s 60))
  2105. s (- s (* 60 s)))
  2106. (insert " => " (format "%2d:%02d" h m))
  2107. t)))))
  2108. (defun org-check-running-clock ()
  2109. "Check if the current buffer contains the running clock.
  2110. If yes, offer to stop it and to save the buffer with the changes."
  2111. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2112. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2113. (buffer-name))))
  2114. (org-clock-out)
  2115. (when (y-or-n-p "Save changed buffer?")
  2116. (save-buffer))))
  2117. (defun org-clocktable-try-shift (dir n)
  2118. "Check if this line starts a clock table, if yes, shift the time block."
  2119. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2120. (org-clocktable-shift dir n)))
  2121. ;; Autoload archiving code
  2122. ;; The stuff that is needed for cycling and tags has to be defined here.
  2123. (defgroup org-archive nil
  2124. "Options concerning archiving in Org-mode."
  2125. :tag "Org Archive"
  2126. :group 'org-structure)
  2127. (defcustom org-archive-location "%s_archive::"
  2128. "The location where subtrees should be archived.
  2129. Otherwise, the value of this variable is a string, consisting of two
  2130. parts, separated by a double-colon.
  2131. The first part is a file name - when omitted, archiving happens in the same
  2132. file. %s will be replaced by the current file name (without directory part).
  2133. Archiving to a different file is useful to keep archived entries from
  2134. contributing to the Org-mode Agenda.
  2135. The part after the double colon is a headline. The archived entries will be
  2136. filed under that headline. When omitted, the subtrees are simply filed away
  2137. at the end of the file, as top-level entries.
  2138. Here are a few examples:
  2139. \"%s_archive::\"
  2140. If the current file is Projects.org, archive in file
  2141. Projects.org_archive, as top-level trees. This is the default.
  2142. \"::* Archived Tasks\"
  2143. Archive in the current file, under the top-level headline
  2144. \"* Archived Tasks\".
  2145. \"~/org/archive.org::\"
  2146. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2147. \"basement::** Finished Tasks\"
  2148. Archive in file ./basement (relative path), as level 3 trees
  2149. below the level 2 heading \"** Finished Tasks\".
  2150. You may set this option on a per-file basis by adding to the buffer a
  2151. line like
  2152. #+ARCHIVE: basement::** Finished Tasks
  2153. You may also define it locally for a subtree by setting an ARCHIVE property
  2154. in the entry. If such a property is found in an entry, or anywhere up
  2155. the hierarchy, it will be used."
  2156. :group 'org-archive
  2157. :type 'string)
  2158. (defcustom org-archive-tag "ARCHIVE"
  2159. "The tag that marks a subtree as archived.
  2160. An archived subtree does not open during visibility cycling, and does
  2161. not contribute to the agenda listings.
  2162. After changing this, font-lock must be restarted in the relevant buffers to
  2163. get the proper fontification."
  2164. :group 'org-archive
  2165. :group 'org-keywords
  2166. :type 'string)
  2167. (defcustom org-agenda-skip-archived-trees t
  2168. "Non-nil means, the agenda will skip any items located in archived trees.
  2169. An archived tree is a tree marked with the tag ARCHIVE."
  2170. :group 'org-archive
  2171. :group 'org-agenda-skip
  2172. :type 'boolean)
  2173. (defcustom org-cycle-open-archived-trees nil
  2174. "Non-nil means, `org-cycle' will open archived trees.
  2175. An archived tree is a tree marked with the tag ARCHIVE.
  2176. When nil, archived trees will stay folded. You can still open them with
  2177. normal outline commands like `show-all', but not with the cycling commands."
  2178. :group 'org-archive
  2179. :group 'org-cycle
  2180. :type 'boolean)
  2181. (defcustom org-sparse-tree-open-archived-trees nil
  2182. "Non-nil means sparse tree construction shows matches in archived trees.
  2183. When nil, matches in these trees are highlighted, but the trees are kept in
  2184. collapsed state."
  2185. :group 'org-archive
  2186. :group 'org-sparse-trees
  2187. :type 'boolean)
  2188. (defun org-cycle-hide-archived-subtrees (state)
  2189. "Re-hide all archived subtrees after a visibility state change."
  2190. (when (and (not org-cycle-open-archived-trees)
  2191. (not (memq state '(overview folded))))
  2192. (save-excursion
  2193. (let* ((globalp (memq state '(contents all)))
  2194. (beg (if globalp (point-min) (point)))
  2195. (end (if globalp (point-max) (org-end-of-subtree t))))
  2196. (org-hide-archived-subtrees beg end)
  2197. (goto-char beg)
  2198. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2199. (message "%s" (substitute-command-keys
  2200. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2201. (defun org-force-cycle-archived ()
  2202. "Cycle subtree even if it is archived."
  2203. (interactive)
  2204. (setq this-command 'org-cycle)
  2205. (let ((org-cycle-open-archived-trees t))
  2206. (call-interactively 'org-cycle)))
  2207. (defun org-hide-archived-subtrees (beg end)
  2208. "Re-hide all archived subtrees after a visibility state change."
  2209. (save-excursion
  2210. (let* ((re (concat ":" org-archive-tag ":")))
  2211. (goto-char beg)
  2212. (while (re-search-forward re end t)
  2213. (and (org-on-heading-p) (hide-subtree))
  2214. (org-end-of-subtree t)))))
  2215. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2216. (eval-and-compile
  2217. (org-autoload "org-archive"
  2218. '(org-add-archive-files org-archive-subtree
  2219. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2220. ;; Autoload Column View Code
  2221. (declare-function org-columns-number-to-string "org-colview")
  2222. (declare-function org-columns-get-format-and-top-level "org-colview")
  2223. (declare-function org-columns-compute "org-colview")
  2224. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2225. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2226. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2227. org-columns org-insert-columns-dblock))
  2228. ;;; Variables for pre-computed regular expressions, all buffer local
  2229. (defvar org-drawer-regexp nil
  2230. "Matches first line of a hidden block.")
  2231. (make-variable-buffer-local 'org-drawer-regexp)
  2232. (defvar org-todo-regexp nil
  2233. "Matches any of the TODO state keywords.")
  2234. (make-variable-buffer-local 'org-todo-regexp)
  2235. (defvar org-not-done-regexp nil
  2236. "Matches any of the TODO state keywords except the last one.")
  2237. (make-variable-buffer-local 'org-not-done-regexp)
  2238. (defvar org-todo-line-regexp nil
  2239. "Matches a headline and puts TODO state into group 2 if present.")
  2240. (make-variable-buffer-local 'org-todo-line-regexp)
  2241. (defvar org-complex-heading-regexp nil
  2242. "Matches a headline and puts everything into groups:
  2243. group 1: the stars
  2244. group 2: The todo keyword, maybe
  2245. group 3: Priority cookie
  2246. group 4: True headline
  2247. group 5: Tags")
  2248. (make-variable-buffer-local 'org-complex-heading-regexp)
  2249. (defvar org-todo-line-tags-regexp nil
  2250. "Matches a headline and puts TODO state into group 2 if present.
  2251. Also put tags into group 4 if tags are present.")
  2252. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2253. (defvar org-nl-done-regexp nil
  2254. "Matches newline followed by a headline with the DONE keyword.")
  2255. (make-variable-buffer-local 'org-nl-done-regexp)
  2256. (defvar org-looking-at-done-regexp nil
  2257. "Matches the DONE keyword a point.")
  2258. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2259. (defvar org-ds-keyword-length 12
  2260. "Maximum length of the Deadline and SCHEDULED keywords.")
  2261. (make-variable-buffer-local 'org-ds-keyword-length)
  2262. (defvar org-deadline-regexp nil
  2263. "Matches the DEADLINE keyword.")
  2264. (make-variable-buffer-local 'org-deadline-regexp)
  2265. (defvar org-deadline-time-regexp nil
  2266. "Matches the DEADLINE keyword together with a time stamp.")
  2267. (make-variable-buffer-local 'org-deadline-time-regexp)
  2268. (defvar org-deadline-line-regexp nil
  2269. "Matches the DEADLINE keyword and the rest of the line.")
  2270. (make-variable-buffer-local 'org-deadline-line-regexp)
  2271. (defvar org-scheduled-regexp nil
  2272. "Matches the SCHEDULED keyword.")
  2273. (make-variable-buffer-local 'org-scheduled-regexp)
  2274. (defvar org-scheduled-time-regexp nil
  2275. "Matches the SCHEDULED keyword together with a time stamp.")
  2276. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2277. (defvar org-closed-time-regexp nil
  2278. "Matches the CLOSED keyword together with a time stamp.")
  2279. (make-variable-buffer-local 'org-closed-time-regexp)
  2280. (defvar org-keyword-time-regexp nil
  2281. "Matches any of the 4 keywords, together with the time stamp.")
  2282. (make-variable-buffer-local 'org-keyword-time-regexp)
  2283. (defvar org-keyword-time-not-clock-regexp nil
  2284. "Matches any of the 3 keywords, together with the time stamp.")
  2285. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2286. (defvar org-maybe-keyword-time-regexp nil
  2287. "Matches a timestamp, possibly preceeded by a keyword.")
  2288. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2289. (defvar org-planning-or-clock-line-re nil
  2290. "Matches a line with planning or clock info.")
  2291. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2292. (defconst org-plain-time-of-day-regexp
  2293. (concat
  2294. "\\(\\<[012]?[0-9]"
  2295. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2296. "\\(--?"
  2297. "\\(\\<[012]?[0-9]"
  2298. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2299. "\\)?")
  2300. "Regular expression to match a plain time or time range.
  2301. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2302. groups carry important information:
  2303. 0 the full match
  2304. 1 the first time, range or not
  2305. 8 the second time, if it is a range.")
  2306. (defconst org-plain-time-extension-regexp
  2307. (concat
  2308. "\\(\\<[012]?[0-9]"
  2309. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2310. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2311. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2312. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2313. groups carry important information:
  2314. 0 the full match
  2315. 7 hours of duration
  2316. 9 minutes of duration")
  2317. (defconst org-stamp-time-of-day-regexp
  2318. (concat
  2319. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2320. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2321. "\\(--?"
  2322. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2323. "Regular expression to match a timestamp time or time range.
  2324. After a match, the following groups carry important information:
  2325. 0 the full match
  2326. 1 date plus weekday, for backreferencing to make sure both times on same day
  2327. 2 the first time, range or not
  2328. 4 the second time, if it is a range.")
  2329. (defconst org-startup-options
  2330. '(("fold" org-startup-folded t)
  2331. ("overview" org-startup-folded t)
  2332. ("nofold" org-startup-folded nil)
  2333. ("showall" org-startup-folded nil)
  2334. ("content" org-startup-folded content)
  2335. ("hidestars" org-hide-leading-stars t)
  2336. ("showstars" org-hide-leading-stars nil)
  2337. ("odd" org-odd-levels-only t)
  2338. ("oddeven" org-odd-levels-only nil)
  2339. ("align" org-startup-align-all-tables t)
  2340. ("noalign" org-startup-align-all-tables nil)
  2341. ("customtime" org-display-custom-times t)
  2342. ("logdone" org-log-done time)
  2343. ("lognotedone" org-log-done note)
  2344. ("nologdone" org-log-done nil)
  2345. ("lognoteclock-out" org-log-note-clock-out t)
  2346. ("nolognoteclock-out" org-log-note-clock-out nil)
  2347. ("logrepeat" org-log-repeat state)
  2348. ("lognoterepeat" org-log-repeat note)
  2349. ("nologrepeat" org-log-repeat nil)
  2350. ("constcgs" constants-unit-system cgs)
  2351. ("constSI" constants-unit-system SI))
  2352. "Variable associated with STARTUP options for org-mode.
  2353. Each element is a list of three items: The startup options as written
  2354. in the #+STARTUP line, the corresponding variable, and the value to
  2355. set this variable to if the option is found. An optional forth element PUSH
  2356. means to push this value onto the list in the variable.")
  2357. (defun org-set-regexps-and-options ()
  2358. "Precompute regular expressions for current buffer."
  2359. (when (org-mode-p)
  2360. (org-set-local 'org-todo-kwd-alist nil)
  2361. (org-set-local 'org-todo-key-alist nil)
  2362. (org-set-local 'org-todo-key-trigger nil)
  2363. (org-set-local 'org-todo-keywords-1 nil)
  2364. (org-set-local 'org-done-keywords nil)
  2365. (org-set-local 'org-todo-heads nil)
  2366. (org-set-local 'org-todo-sets nil)
  2367. (org-set-local 'org-todo-log-states nil)
  2368. (let ((re (org-make-options-regexp
  2369. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2370. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2371. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2372. (splitre "[ \t]+")
  2373. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2374. tail sep kws1 prio props drawers)
  2375. (save-excursion
  2376. (save-restriction
  2377. (widen)
  2378. (goto-char (point-min))
  2379. (while (re-search-forward re nil t)
  2380. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2381. (cond
  2382. ((equal key "CATEGORY")
  2383. (if (string-match "[ \t]+$" value)
  2384. (setq value (replace-match "" t t value)))
  2385. (setq cat value))
  2386. ((member key '("SEQ_TODO" "TODO"))
  2387. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2388. ((equal key "TYP_TODO")
  2389. (push (cons 'type (org-split-string value splitre)) kwds))
  2390. ((equal key "TAGS")
  2391. (setq tags (append tags (org-split-string value splitre))))
  2392. ((equal key "COLUMNS")
  2393. (org-set-local 'org-columns-default-format value))
  2394. ((equal key "LINK")
  2395. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2396. (push (cons (match-string 1 value)
  2397. (org-trim (match-string 2 value)))
  2398. links)))
  2399. ((equal key "PRIORITIES")
  2400. (setq prio (org-split-string value " +")))
  2401. ((equal key "PROPERTY")
  2402. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2403. (push (cons (match-string 1 value) (match-string 2 value))
  2404. props)))
  2405. ((equal key "DRAWERS")
  2406. (setq drawers (org-split-string value splitre)))
  2407. ((equal key "CONSTANTS")
  2408. (setq const (append const (org-split-string value splitre))))
  2409. ((equal key "STARTUP")
  2410. (let ((opts (org-split-string value splitre))
  2411. l var val)
  2412. (while (setq l (pop opts))
  2413. (when (setq l (assoc l org-startup-options))
  2414. (setq var (nth 1 l) val (nth 2 l))
  2415. (if (not (nth 3 l))
  2416. (set (make-local-variable var) val)
  2417. (if (not (listp (symbol-value var)))
  2418. (set (make-local-variable var) nil))
  2419. (set (make-local-variable var) (symbol-value var))
  2420. (add-to-list var val))))))
  2421. ((equal key "ARCHIVE")
  2422. (string-match " *$" value)
  2423. (setq arch (replace-match "" t t value))
  2424. (remove-text-properties 0 (length arch)
  2425. '(face t fontified t) arch)))
  2426. )))
  2427. (when cat
  2428. (org-set-local 'org-category (intern cat))
  2429. (push (cons "CATEGORY" cat) props))
  2430. (when prio
  2431. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2432. (setq prio (mapcar 'string-to-char prio))
  2433. (org-set-local 'org-highest-priority (nth 0 prio))
  2434. (org-set-local 'org-lowest-priority (nth 1 prio))
  2435. (org-set-local 'org-default-priority (nth 2 prio)))
  2436. (and props (org-set-local 'org-local-properties (nreverse props)))
  2437. (and drawers (org-set-local 'org-drawers drawers))
  2438. (and arch (org-set-local 'org-archive-location arch))
  2439. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2440. ;; Process the TODO keywords
  2441. (unless kwds
  2442. ;; Use the global values as if they had been given locally.
  2443. (setq kwds (default-value 'org-todo-keywords))
  2444. (if (stringp (car kwds))
  2445. (setq kwds (list (cons org-todo-interpretation
  2446. (default-value 'org-todo-keywords)))))
  2447. (setq kwds (reverse kwds)))
  2448. (setq kwds (nreverse kwds))
  2449. (let (inter kws kw)
  2450. (while (setq kws (pop kwds))
  2451. (setq inter (pop kws) sep (member "|" kws)
  2452. kws0 (delete "|" (copy-sequence kws))
  2453. kwsa nil
  2454. kws1 (mapcar
  2455. (lambda (x)
  2456. ;; 1 2
  2457. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2458. (progn
  2459. (setq kw (match-string 1 x)
  2460. key (and (match-end 2) (match-string 2 x))
  2461. log (org-extract-log-state-settings x))
  2462. (push (cons kw (and key (string-to-char key))) kwsa)
  2463. (and log (push log org-todo-log-states))
  2464. kw)
  2465. (error "Invalid TODO keyword %s" x)))
  2466. kws0)
  2467. kwsa (if kwsa (append '((:startgroup))
  2468. (nreverse kwsa)
  2469. '((:endgroup))))
  2470. hw (car kws1)
  2471. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2472. tail (list inter hw (car dws) (org-last dws)))
  2473. (add-to-list 'org-todo-heads hw 'append)
  2474. (push kws1 org-todo-sets)
  2475. (setq org-done-keywords (append org-done-keywords dws nil))
  2476. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2477. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2478. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2479. (setq org-todo-sets (nreverse org-todo-sets)
  2480. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2481. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2482. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2483. ;; Process the constants
  2484. (when const
  2485. (let (e cst)
  2486. (while (setq e (pop const))
  2487. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2488. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2489. (setq org-table-formula-constants-local cst)))
  2490. ;; Process the tags.
  2491. (when tags
  2492. (let (e tgs)
  2493. (while (setq e (pop tags))
  2494. (cond
  2495. ((equal e "{") (push '(:startgroup) tgs))
  2496. ((equal e "}") (push '(:endgroup) tgs))
  2497. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2498. (push (cons (match-string 1 e)
  2499. (string-to-char (match-string 2 e)))
  2500. tgs))
  2501. (t (push (list e) tgs))))
  2502. (org-set-local 'org-tag-alist nil)
  2503. (while (setq e (pop tgs))
  2504. (or (and (stringp (car e))
  2505. (assoc (car e) org-tag-alist))
  2506. (push e org-tag-alist))))))
  2507. ;; Compute the regular expressions and other local variables
  2508. (if (not org-done-keywords)
  2509. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2510. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2511. (length org-scheduled-string)
  2512. (length org-clock-string)
  2513. (length org-closed-string)))
  2514. org-drawer-regexp
  2515. (concat "^[ \t]*:\\("
  2516. (mapconcat 'regexp-quote org-drawers "\\|")
  2517. "\\):[ \t]*$")
  2518. org-not-done-keywords
  2519. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2520. org-todo-regexp
  2521. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2522. "\\|") "\\)\\>")
  2523. org-not-done-regexp
  2524. (concat "\\<\\("
  2525. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2526. "\\)\\>")
  2527. org-todo-line-regexp
  2528. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2529. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2530. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2531. org-complex-heading-regexp
  2532. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2533. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2534. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2535. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2536. org-nl-done-regexp
  2537. (concat "\n\\*+[ \t]+"
  2538. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2539. "\\)" "\\>")
  2540. org-todo-line-tags-regexp
  2541. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2542. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2543. (org-re
  2544. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2545. org-looking-at-done-regexp
  2546. (concat "^" "\\(?:"
  2547. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2548. "\\>")
  2549. org-deadline-regexp (concat "\\<" org-deadline-string)
  2550. org-deadline-time-regexp
  2551. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2552. org-deadline-line-regexp
  2553. (concat "\\<\\(" org-deadline-string "\\).*")
  2554. org-scheduled-regexp
  2555. (concat "\\<" org-scheduled-string)
  2556. org-scheduled-time-regexp
  2557. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2558. org-closed-time-regexp
  2559. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2560. org-keyword-time-regexp
  2561. (concat "\\<\\(" org-scheduled-string
  2562. "\\|" org-deadline-string
  2563. "\\|" org-closed-string
  2564. "\\|" org-clock-string "\\)"
  2565. " *[[<]\\([^]>]+\\)[]>]")
  2566. org-keyword-time-not-clock-regexp
  2567. (concat "\\<\\(" org-scheduled-string
  2568. "\\|" org-deadline-string
  2569. "\\|" org-closed-string
  2570. "\\)"
  2571. " *[[<]\\([^]>]+\\)[]>]")
  2572. org-maybe-keyword-time-regexp
  2573. (concat "\\(\\<\\(" org-scheduled-string
  2574. "\\|" org-deadline-string
  2575. "\\|" org-closed-string
  2576. "\\|" org-clock-string "\\)\\)?"
  2577. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2578. org-planning-or-clock-line-re
  2579. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2580. "\\|" org-deadline-string
  2581. "\\|" org-closed-string "\\|" org-clock-string
  2582. "\\)\\>\\)")
  2583. )
  2584. (org-compute-latex-and-specials-regexp)
  2585. (org-set-font-lock-defaults)))
  2586. (defun org-extract-log-state-settings (x)
  2587. "Extract the log state setting from a TODO keyword string.
  2588. This will extract info from a string like \"WAIT(w@/!)\"."
  2589. (let (kw key log1 log2)
  2590. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2591. (setq kw (match-string 1 x)
  2592. key (and (match-end 2) (match-string 2 x))
  2593. log1 (and (match-end 3) (match-string 3 x))
  2594. log2 (and (match-end 4) (match-string 4 x)))
  2595. (and (or log1 log2)
  2596. (list kw
  2597. (and log1 (if (equal log1 "!") 'time 'note))
  2598. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2599. (defun org-remove-keyword-keys (list)
  2600. "Remove a pair of parenthesis at the end of each string in LIST."
  2601. (mapcar (lambda (x)
  2602. (if (string-match "(.*)$" x)
  2603. (substring x 0 (match-beginning 0))
  2604. x))
  2605. list))
  2606. ;; FIXME: this could be done much better, using second characters etc.
  2607. (defun org-assign-fast-keys (alist)
  2608. "Assign fast keys to a keyword-key alist.
  2609. Respect keys that are already there."
  2610. (let (new e k c c1 c2 (char ?a))
  2611. (while (setq e (pop alist))
  2612. (cond
  2613. ((equal e '(:startgroup)) (push e new))
  2614. ((equal e '(:endgroup)) (push e new))
  2615. (t
  2616. (setq k (car e) c2 nil)
  2617. (if (cdr e)
  2618. (setq c (cdr e))
  2619. ;; automatically assign a character.
  2620. (setq c1 (string-to-char
  2621. (downcase (substring
  2622. k (if (= (string-to-char k) ?@) 1 0)))))
  2623. (if (or (rassoc c1 new) (rassoc c1 alist))
  2624. (while (or (rassoc char new) (rassoc char alist))
  2625. (setq char (1+ char)))
  2626. (setq c2 c1))
  2627. (setq c (or c2 char)))
  2628. (push (cons k c) new))))
  2629. (nreverse new)))
  2630. ;;; Some variables used in various places
  2631. (defvar org-window-configuration nil
  2632. "Used in various places to store a window configuration.")
  2633. (defvar org-finish-function nil
  2634. "Function to be called when `C-c C-c' is used.
  2635. This is for getting out of special buffers like remember.")
  2636. ;; FIXME: Occasionally check by commenting these, to make sure
  2637. ;; no other functions uses these, forgetting to let-bind them.
  2638. (defvar entry)
  2639. (defvar state)
  2640. (defvar last-state)
  2641. (defvar date)
  2642. (defvar description)
  2643. ;; Defined somewhere in this file, but used before definition.
  2644. (defvar org-html-entities)
  2645. (defvar org-struct-menu)
  2646. (defvar org-org-menu)
  2647. (defvar org-tbl-menu)
  2648. (defvar org-agenda-keymap)
  2649. ;;;; Define the Org-mode
  2650. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2651. (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."))
  2652. ;; We use a before-change function to check if a table might need
  2653. ;; an update.
  2654. (defvar org-table-may-need-update t
  2655. "Indicates that a table might need an update.
  2656. This variable is set by `org-before-change-function'.
  2657. `org-table-align' sets it back to nil.")
  2658. (defun org-before-change-function (beg end)
  2659. "Every change indicates that a table might need an update."
  2660. (setq org-table-may-need-update t))
  2661. (defvar org-mode-map)
  2662. (defvar org-mode-hook nil)
  2663. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2664. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2665. (defvar org-table-buffer-is-an nil)
  2666. (defconst org-outline-regexp "\\*+ ")
  2667. ;;;###autoload
  2668. (define-derived-mode org-mode outline-mode "Org"
  2669. "Outline-based notes management and organizer, alias
  2670. \"Carsten's outline-mode for keeping track of everything.\"
  2671. Org-mode develops organizational tasks around a NOTES file which
  2672. contains information about projects as plain text. Org-mode is
  2673. implemented on top of outline-mode, which is ideal to keep the content
  2674. of large files well structured. It supports ToDo items, deadlines and
  2675. time stamps, which magically appear in the diary listing of the Emacs
  2676. calendar. Tables are easily created with a built-in table editor.
  2677. Plain text URL-like links connect to websites, emails (VM), Usenet
  2678. messages (Gnus), BBDB entries, and any files related to the project.
  2679. For printing and sharing of notes, an Org-mode file (or a part of it)
  2680. can be exported as a structured ASCII or HTML file.
  2681. The following commands are available:
  2682. \\{org-mode-map}"
  2683. ;; Get rid of Outline menus, they are not needed
  2684. ;; Need to do this here because define-derived-mode sets up
  2685. ;; the keymap so late. Still, it is a waste to call this each time
  2686. ;; we switch another buffer into org-mode.
  2687. (if (featurep 'xemacs)
  2688. (when (boundp 'outline-mode-menu-heading)
  2689. ;; Assume this is Greg's port, it used easymenu
  2690. (easy-menu-remove outline-mode-menu-heading)
  2691. (easy-menu-remove outline-mode-menu-show)
  2692. (easy-menu-remove outline-mode-menu-hide))
  2693. (define-key org-mode-map [menu-bar headings] 'undefined)
  2694. (define-key org-mode-map [menu-bar hide] 'undefined)
  2695. (define-key org-mode-map [menu-bar show] 'undefined))
  2696. (org-load-modules-maybe)
  2697. (easy-menu-add org-org-menu)
  2698. (easy-menu-add org-tbl-menu)
  2699. (org-install-agenda-files-menu)
  2700. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2701. (org-add-to-invisibility-spec '(org-cwidth))
  2702. (when (featurep 'xemacs)
  2703. (org-set-local 'line-move-ignore-invisible t))
  2704. (org-set-local 'outline-regexp org-outline-regexp)
  2705. (org-set-local 'outline-level 'org-outline-level)
  2706. (when (and org-ellipsis
  2707. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2708. (fboundp 'make-glyph-code))
  2709. (unless org-display-table
  2710. (setq org-display-table (make-display-table)))
  2711. (set-display-table-slot
  2712. org-display-table 4
  2713. (vconcat (mapcar
  2714. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2715. org-ellipsis)))
  2716. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2717. (setq buffer-display-table org-display-table))
  2718. (org-set-regexps-and-options)
  2719. ;; Calc embedded
  2720. (org-set-local 'calc-embedded-open-mode "# ")
  2721. (modify-syntax-entry ?# "<")
  2722. (modify-syntax-entry ?@ "w")
  2723. (if org-startup-truncated (setq truncate-lines t))
  2724. (org-set-local 'font-lock-unfontify-region-function
  2725. 'org-unfontify-region)
  2726. ;; Activate before-change-function
  2727. (org-set-local 'org-table-may-need-update t)
  2728. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2729. 'local)
  2730. ;; Check for running clock before killing a buffer
  2731. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2732. ;; Paragraphs and auto-filling
  2733. (org-set-autofill-regexps)
  2734. (setq indent-line-function 'org-indent-line-function)
  2735. (org-update-radio-target-regexp)
  2736. ;; Comment characters
  2737. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2738. (org-set-local 'comment-padding " ")
  2739. ;; Align options lines
  2740. (org-set-local
  2741. 'align-mode-rules-list
  2742. '((org-in-buffer-settings
  2743. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2744. (modes . '(org-mode)))))
  2745. ;; Imenu
  2746. (org-set-local 'imenu-create-index-function
  2747. 'org-imenu-get-tree)
  2748. ;; Make isearch reveal context
  2749. (if (or (featurep 'xemacs)
  2750. (not (boundp 'outline-isearch-open-invisible-function)))
  2751. ;; Emacs 21 and XEmacs make use of the hook
  2752. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2753. ;; Emacs 22 deals with this through a special variable
  2754. (org-set-local 'outline-isearch-open-invisible-function
  2755. (lambda (&rest ignore) (org-show-context 'isearch))))
  2756. ;; If empty file that did not turn on org-mode automatically, make it to.
  2757. (if (and org-insert-mode-line-in-empty-file
  2758. (interactive-p)
  2759. (= (point-min) (point-max)))
  2760. (insert "# -*- mode: org -*-\n\n"))
  2761. (unless org-inhibit-startup
  2762. (when org-startup-align-all-tables
  2763. (let ((bmp (buffer-modified-p)))
  2764. (org-table-map-tables 'org-table-align)
  2765. (set-buffer-modified-p bmp)))
  2766. (org-cycle-hide-drawers 'all)
  2767. (cond
  2768. ((eq org-startup-folded t)
  2769. (org-cycle '(4)))
  2770. ((eq org-startup-folded 'content)
  2771. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2772. (org-cycle '(4)) (org-cycle '(4)))))))
  2773. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2774. (defun org-current-time ()
  2775. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2776. (if (> (car org-time-stamp-rounding-minutes) 1)
  2777. (let ((r (car org-time-stamp-rounding-minutes))
  2778. (time (decode-time)))
  2779. (apply 'encode-time
  2780. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2781. (nthcdr 2 time))))
  2782. (current-time)))
  2783. ;;;; Font-Lock stuff, including the activators
  2784. (defvar org-mouse-map (make-sparse-keymap))
  2785. (org-defkey org-mouse-map
  2786. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2787. (org-defkey org-mouse-map
  2788. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2789. (when org-mouse-1-follows-link
  2790. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2791. (when org-tab-follows-link
  2792. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2793. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2794. (when org-return-follows-link
  2795. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2796. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2797. (require 'font-lock)
  2798. (defconst org-non-link-chars "]\t\n\r<>")
  2799. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2800. "shell" "elisp"))
  2801. (defvar org-link-types-re nil
  2802. "Matches a link that has a url-like prefix like \"http:\"")
  2803. (defvar org-link-re-with-space nil
  2804. "Matches a link with spaces, optional angular brackets around it.")
  2805. (defvar org-link-re-with-space2 nil
  2806. "Matches a link with spaces, optional angular brackets around it.")
  2807. (defvar org-angle-link-re nil
  2808. "Matches link with angular brackets, spaces are allowed.")
  2809. (defvar org-plain-link-re nil
  2810. "Matches plain link, without spaces.")
  2811. (defvar org-bracket-link-regexp nil
  2812. "Matches a link in double brackets.")
  2813. (defvar org-bracket-link-analytic-regexp nil
  2814. "Regular expression used to analyze links.
  2815. Here is what the match groups contain after a match:
  2816. 1: http:
  2817. 2: http
  2818. 3: path
  2819. 4: [desc]
  2820. 5: desc")
  2821. (defvar org-any-link-re nil
  2822. "Regular expression matching any link.")
  2823. (defun org-make-link-regexps ()
  2824. "Update the link regular expressions.
  2825. This should be called after the variable `org-link-types' has changed."
  2826. (setq org-link-types-re
  2827. (concat
  2828. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2829. org-link-re-with-space
  2830. (concat
  2831. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2832. "\\([^" org-non-link-chars " ]"
  2833. "[^" org-non-link-chars "]*"
  2834. "[^" org-non-link-chars " ]\\)>?")
  2835. org-link-re-with-space2
  2836. (concat
  2837. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2838. "\\([^" org-non-link-chars " ]"
  2839. "[^]\t\n\r]*"
  2840. "[^" org-non-link-chars " ]\\)>?")
  2841. org-angle-link-re
  2842. (concat
  2843. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2844. "\\([^" org-non-link-chars " ]"
  2845. "[^" org-non-link-chars "]*"
  2846. "\\)>")
  2847. org-plain-link-re
  2848. (concat
  2849. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2850. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2851. org-bracket-link-regexp
  2852. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2853. org-bracket-link-analytic-regexp
  2854. (concat
  2855. "\\[\\["
  2856. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2857. "\\([^]]+\\)"
  2858. "\\]"
  2859. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2860. "\\]")
  2861. org-any-link-re
  2862. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2863. org-angle-link-re "\\)\\|\\("
  2864. org-plain-link-re "\\)")))
  2865. (org-make-link-regexps)
  2866. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2867. "Regular expression for fast time stamp matching.")
  2868. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2869. "Regular expression for fast time stamp matching.")
  2870. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2871. "Regular expression matching time strings for analysis.
  2872. This one does not require the space after the date, so it can be used
  2873. on a string that terminates immediately after the date.")
  2874. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2875. "Regular expression matching time strings for analysis.")
  2876. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2877. "Regular expression matching time stamps, with groups.")
  2878. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2879. "Regular expression matching time stamps (also [..]), with groups.")
  2880. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2881. "Regular expression matching a time stamp range.")
  2882. (defconst org-tr-regexp-both
  2883. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2884. "Regular expression matching a time stamp range.")
  2885. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2886. org-ts-regexp "\\)?")
  2887. "Regular expression matching a time stamp or time stamp range.")
  2888. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2889. org-ts-regexp-both "\\)?")
  2890. "Regular expression matching a time stamp or time stamp range.
  2891. The time stamps may be either active or inactive.")
  2892. (defvar org-emph-face nil)
  2893. (defun org-do-emphasis-faces (limit)
  2894. "Run through the buffer and add overlays to links."
  2895. (let (rtn)
  2896. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2897. (if (not (= (char-after (match-beginning 3))
  2898. (char-after (match-beginning 4))))
  2899. (progn
  2900. (setq rtn t)
  2901. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2902. 'face
  2903. (nth 1 (assoc (match-string 3)
  2904. org-emphasis-alist)))
  2905. (add-text-properties (match-beginning 2) (match-end 2)
  2906. '(font-lock-multiline t))
  2907. (when org-hide-emphasis-markers
  2908. (add-text-properties (match-end 4) (match-beginning 5)
  2909. '(invisible org-link))
  2910. (add-text-properties (match-beginning 3) (match-end 3)
  2911. '(invisible org-link)))))
  2912. (backward-char 1))
  2913. rtn))
  2914. (defun org-emphasize (&optional char)
  2915. "Insert or change an emphasis, i.e. a font like bold or italic.
  2916. If there is an active region, change that region to a new emphasis.
  2917. If there is no region, just insert the marker characters and position
  2918. the cursor between them.
  2919. CHAR should be either the marker character, or the first character of the
  2920. HTML tag associated with that emphasis. If CHAR is a space, the means
  2921. to remove the emphasis of the selected region.
  2922. If char is not given (for example in an interactive call) it
  2923. will be prompted for."
  2924. (interactive)
  2925. (let ((eal org-emphasis-alist) e det
  2926. (erc org-emphasis-regexp-components)
  2927. (prompt "")
  2928. (string "") beg end move tag c s)
  2929. (if (org-region-active-p)
  2930. (setq beg (region-beginning) end (region-end)
  2931. string (buffer-substring beg end))
  2932. (setq move t))
  2933. (while (setq e (pop eal))
  2934. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2935. c (aref tag 0))
  2936. (push (cons c (string-to-char (car e))) det)
  2937. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2938. (substring tag 1)))))
  2939. (unless char
  2940. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2941. (setq char (read-char-exclusive)))
  2942. (setq char (or (cdr (assoc char det)) char))
  2943. (if (equal char ?\ )
  2944. (setq s "" move nil)
  2945. (unless (assoc (char-to-string char) org-emphasis-alist)
  2946. (error "No such emphasis marker: \"%c\"" char))
  2947. (setq s (char-to-string char)))
  2948. (while (and (> (length string) 1)
  2949. (equal (substring string 0 1) (substring string -1))
  2950. (assoc (substring string 0 1) org-emphasis-alist))
  2951. (setq string (substring string 1 -1)))
  2952. (setq string (concat s string s))
  2953. (if beg (delete-region beg end))
  2954. (unless (or (bolp)
  2955. (string-match (concat "[" (nth 0 erc) "\n]")
  2956. (char-to-string (char-before (point)))))
  2957. (insert " "))
  2958. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2959. (char-to-string (char-after (point))))
  2960. (insert " ") (backward-char 1))
  2961. (insert string)
  2962. (and move (backward-char 1))))
  2963. (defconst org-nonsticky-props
  2964. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2965. (defun org-activate-plain-links (limit)
  2966. "Run through the buffer and add overlays to links."
  2967. (catch 'exit
  2968. (let (f)
  2969. (while (re-search-forward org-plain-link-re limit t)
  2970. (setq f (get-text-property (match-beginning 0) 'face))
  2971. (if (or (eq f 'org-tag)
  2972. (and (listp f) (memq 'org-tag f)))
  2973. nil
  2974. (add-text-properties (match-beginning 0) (match-end 0)
  2975. (list 'mouse-face 'highlight
  2976. 'rear-nonsticky org-nonsticky-props
  2977. 'keymap org-mouse-map
  2978. ))
  2979. (throw 'exit t))))))
  2980. (defun org-activate-code (limit)
  2981. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  2982. (unless (get-text-property (match-beginning 1) 'face)
  2983. (remove-text-properties (match-beginning 0) (match-end 0)
  2984. '(display t invisible t intangible t))
  2985. t)))
  2986. (defun org-activate-angle-links (limit)
  2987. "Run through the buffer and add overlays to links."
  2988. (if (re-search-forward org-angle-link-re limit t)
  2989. (progn
  2990. (add-text-properties (match-beginning 0) (match-end 0)
  2991. (list 'mouse-face 'highlight
  2992. 'rear-nonsticky org-nonsticky-props
  2993. 'keymap org-mouse-map
  2994. ))
  2995. t)))
  2996. (defun org-activate-bracket-links (limit)
  2997. "Run through the buffer and add overlays to bracketed links."
  2998. (if (re-search-forward org-bracket-link-regexp limit t)
  2999. (let* ((help (concat "LINK: "
  3000. (org-match-string-no-properties 1)))
  3001. ;; FIXME: above we should remove the escapes.
  3002. ;; but that requires another match, protecting match data,
  3003. ;; a lot of overhead for font-lock.
  3004. (ip (org-maybe-intangible
  3005. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3006. 'keymap org-mouse-map 'mouse-face 'highlight
  3007. 'font-lock-multiline t 'help-echo help)))
  3008. (vp (list 'rear-nonsticky org-nonsticky-props
  3009. 'keymap org-mouse-map 'mouse-face 'highlight
  3010. ' font-lock-multiline t 'help-echo help)))
  3011. ;; We need to remove the invisible property here. Table narrowing
  3012. ;; may have made some of this invisible.
  3013. (remove-text-properties (match-beginning 0) (match-end 0)
  3014. '(invisible nil))
  3015. (if (match-end 3)
  3016. (progn
  3017. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3018. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3019. (add-text-properties (match-end 3) (match-end 0) ip))
  3020. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3021. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3022. (add-text-properties (match-end 1) (match-end 0) ip))
  3023. t)))
  3024. (defun org-activate-dates (limit)
  3025. "Run through the buffer and add overlays to dates."
  3026. (if (re-search-forward org-tsr-regexp-both limit t)
  3027. (progn
  3028. (add-text-properties (match-beginning 0) (match-end 0)
  3029. (list 'mouse-face 'highlight
  3030. 'rear-nonsticky org-nonsticky-props
  3031. 'keymap org-mouse-map))
  3032. (when org-display-custom-times
  3033. (if (match-end 3)
  3034. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3035. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3036. t)))
  3037. (defvar org-target-link-regexp nil
  3038. "Regular expression matching radio targets in plain text.")
  3039. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3040. "Regular expression matching a link target.")
  3041. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3042. "Regular expression matching a radio target.")
  3043. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3044. "Regular expression matching any target.")
  3045. (defun org-activate-target-links (limit)
  3046. "Run through the buffer and add overlays to target matches."
  3047. (when org-target-link-regexp
  3048. (let ((case-fold-search t))
  3049. (if (re-search-forward org-target-link-regexp limit t)
  3050. (progn
  3051. (add-text-properties (match-beginning 0) (match-end 0)
  3052. (list 'mouse-face 'highlight
  3053. 'rear-nonsticky org-nonsticky-props
  3054. 'keymap org-mouse-map
  3055. 'help-echo "Radio target link"
  3056. 'org-linked-text t))
  3057. t)))))
  3058. (defun org-update-radio-target-regexp ()
  3059. "Find all radio targets in this file and update the regular expression."
  3060. (interactive)
  3061. (when (memq 'radio org-activate-links)
  3062. (setq org-target-link-regexp
  3063. (org-make-target-link-regexp (org-all-targets 'radio)))
  3064. (org-restart-font-lock)))
  3065. (defun org-hide-wide-columns (limit)
  3066. (let (s e)
  3067. (setq s (text-property-any (point) (or limit (point-max))
  3068. 'org-cwidth t))
  3069. (when s
  3070. (setq e (next-single-property-change s 'org-cwidth))
  3071. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3072. (goto-char e)
  3073. t)))
  3074. (defvar org-latex-and-specials-regexp nil
  3075. "Regular expression for highlighting export special stuff.")
  3076. (defvar org-match-substring-regexp)
  3077. (defvar org-match-substring-with-braces-regexp)
  3078. (defvar org-export-html-special-string-regexps)
  3079. (defun org-compute-latex-and-specials-regexp ()
  3080. "Compute regular expression for stuff treated specially by exporters."
  3081. (if (not org-highlight-latex-fragments-and-specials)
  3082. (org-set-local 'org-latex-and-specials-regexp nil)
  3083. (require 'org-exp)
  3084. (let*
  3085. ((matchers (plist-get org-format-latex-options :matchers))
  3086. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3087. org-latex-regexps)))
  3088. (options (org-combine-plists (org-default-export-plist)
  3089. (org-infile-export-plist)))
  3090. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3091. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3092. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3093. (org-export-html-expand (plist-get options :expand-quoted-html))
  3094. (org-export-with-special-strings (plist-get options :special-strings))
  3095. (re-sub
  3096. (cond
  3097. ((equal org-export-with-sub-superscripts '{})
  3098. (list org-match-substring-with-braces-regexp))
  3099. (org-export-with-sub-superscripts
  3100. (list org-match-substring-regexp))
  3101. (t nil)))
  3102. (re-latex
  3103. (if org-export-with-LaTeX-fragments
  3104. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3105. (re-macros
  3106. (if org-export-with-TeX-macros
  3107. (list (concat "\\\\"
  3108. (regexp-opt
  3109. (append (mapcar 'car org-html-entities)
  3110. (if (boundp 'org-latex-entities)
  3111. org-latex-entities nil))
  3112. 'words))) ; FIXME
  3113. ))
  3114. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3115. (re-special (if org-export-with-special-strings
  3116. (mapcar (lambda (x) (car x))
  3117. org-export-html-special-string-regexps)))
  3118. (re-rest
  3119. (delq nil
  3120. (list
  3121. (if org-export-html-expand "@<[^>\n]+>")
  3122. ))))
  3123. (org-set-local
  3124. 'org-latex-and-specials-regexp
  3125. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3126. re-rest) "\\|")))))
  3127. (defun org-do-latex-and-special-faces (limit)
  3128. "Run through the buffer and add overlays to links."
  3129. (when org-latex-and-specials-regexp
  3130. (let (rtn d)
  3131. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3132. limit t))
  3133. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3134. 'face))
  3135. '(org-code org-verbatim underline)))
  3136. (progn
  3137. (setq rtn t
  3138. d (cond ((member (char-after (1+ (match-beginning 0)))
  3139. '(?_ ?^)) 1)
  3140. (t 0)))
  3141. (font-lock-prepend-text-property
  3142. (+ d (match-beginning 0)) (match-end 0)
  3143. 'face 'org-latex-and-export-specials)
  3144. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3145. '(font-lock-multiline t)))))
  3146. rtn)))
  3147. (defun org-restart-font-lock ()
  3148. "Restart font-lock-mode, to force refontification."
  3149. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3150. (font-lock-mode -1)
  3151. (font-lock-mode 1)))
  3152. (defun org-all-targets (&optional radio)
  3153. "Return a list of all targets in this file.
  3154. With optional argument RADIO, only find radio targets."
  3155. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3156. rtn)
  3157. (save-excursion
  3158. (goto-char (point-min))
  3159. (while (re-search-forward re nil t)
  3160. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3161. rtn)))
  3162. (defun org-make-target-link-regexp (targets)
  3163. "Make regular expression matching all strings in TARGETS.
  3164. The regular expression finds the targets also if there is a line break
  3165. between words."
  3166. (and targets
  3167. (concat
  3168. "\\<\\("
  3169. (mapconcat
  3170. (lambda (x)
  3171. (while (string-match " +" x)
  3172. (setq x (replace-match "\\s-+" t t x)))
  3173. x)
  3174. targets
  3175. "\\|")
  3176. "\\)\\>")))
  3177. (defun org-activate-tags (limit)
  3178. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3179. (progn
  3180. (add-text-properties (match-beginning 1) (match-end 1)
  3181. (list 'mouse-face 'highlight
  3182. 'rear-nonsticky org-nonsticky-props
  3183. 'keymap org-mouse-map))
  3184. t)))
  3185. (defun org-outline-level ()
  3186. (save-excursion
  3187. (looking-at outline-regexp)
  3188. (if (match-beginning 1)
  3189. (+ (org-get-string-indentation (match-string 1)) 1000)
  3190. (1- (- (match-end 0) (match-beginning 0))))))
  3191. (defvar org-font-lock-keywords nil)
  3192. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3193. "Regular expression matching a property line.")
  3194. (defun org-set-font-lock-defaults ()
  3195. (let* ((em org-fontify-emphasized-text)
  3196. (lk org-activate-links)
  3197. (org-font-lock-extra-keywords
  3198. (list
  3199. ;; Headlines
  3200. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3201. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3202. ;; Table lines
  3203. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3204. (1 'org-table t))
  3205. ;; Table internals
  3206. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3207. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3208. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3209. ;; Drawers
  3210. (list org-drawer-regexp '(0 'org-special-keyword t))
  3211. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3212. ;; Properties
  3213. (list org-property-re
  3214. '(1 'org-special-keyword t)
  3215. '(3 'org-property-value t))
  3216. (if org-format-transports-properties-p
  3217. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3218. ;; Links
  3219. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3220. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3221. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3222. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3223. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3224. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3225. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3226. '(org-hide-wide-columns (0 nil append))
  3227. ;; TODO lines
  3228. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3229. '(1 (org-get-todo-face 1) t))
  3230. ;; DONE
  3231. (if org-fontify-done-headline
  3232. (list (concat "^[*]+ +\\<\\("
  3233. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3234. "\\)\\(.*\\)")
  3235. '(2 'org-headline-done t))
  3236. nil)
  3237. ;; Priorities
  3238. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3239. ;; Special keywords
  3240. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3241. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3242. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3243. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3244. ;; Emphasis
  3245. (if em
  3246. (if (featurep 'xemacs)
  3247. '(org-do-emphasis-faces (0 nil append))
  3248. '(org-do-emphasis-faces)))
  3249. ;; Checkboxes
  3250. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3251. 2 'bold prepend)
  3252. (if org-provide-checkbox-statistics
  3253. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3254. (0 (org-get-checkbox-statistics-face) t)))
  3255. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3256. '(1 'org-archived prepend))
  3257. ;; Specials
  3258. '(org-do-latex-and-special-faces)
  3259. ;; Code
  3260. '(org-activate-code (1 'org-code t))
  3261. ;; COMMENT
  3262. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3263. "\\|" org-quote-string "\\)\\>")
  3264. '(1 'org-special-keyword t))
  3265. '("^#.*" (0 'font-lock-comment-face t))
  3266. )))
  3267. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3268. ;; Now set the full font-lock-keywords
  3269. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3270. (org-set-local 'font-lock-defaults
  3271. '(org-font-lock-keywords t nil nil backward-paragraph))
  3272. (kill-local-variable 'font-lock-keywords) nil))
  3273. (defvar org-m nil)
  3274. (defvar org-l nil)
  3275. (defvar org-f nil)
  3276. (defun org-get-level-face (n)
  3277. "Get the right face for match N in font-lock matching of healdines."
  3278. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3279. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3280. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3281. (cond
  3282. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3283. ((eq n 2) org-f)
  3284. (t (if org-level-color-stars-only nil org-f))))
  3285. (defun org-get-todo-face (kwd)
  3286. "Get the right face for a TODO keyword KWD.
  3287. If KWD is a number, get the corresponding match group."
  3288. (if (numberp kwd) (setq kwd (match-string kwd)))
  3289. (or (cdr (assoc kwd org-todo-keyword-faces))
  3290. (and (member kwd org-done-keywords) 'org-done)
  3291. 'org-todo))
  3292. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3293. "Remove fontification and activation overlays from links."
  3294. (font-lock-default-unfontify-region beg end)
  3295. (let* ((buffer-undo-list t)
  3296. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3297. (inhibit-modification-hooks t)
  3298. deactivate-mark buffer-file-name buffer-file-truename)
  3299. (remove-text-properties beg end
  3300. '(mouse-face t keymap t org-linked-text t
  3301. invisible t intangible t))))
  3302. ;;;; Visibility cycling, including org-goto and indirect buffer
  3303. ;;; Cycling
  3304. (defvar org-cycle-global-status nil)
  3305. (make-variable-buffer-local 'org-cycle-global-status)
  3306. (defvar org-cycle-subtree-status nil)
  3307. (make-variable-buffer-local 'org-cycle-subtree-status)
  3308. ;;;###autoload
  3309. (defun org-cycle (&optional arg)
  3310. "Visibility cycling for Org-mode.
  3311. - When this function is called with a prefix argument, rotate the entire
  3312. buffer through 3 states (global cycling)
  3313. 1. OVERVIEW: Show only top-level headlines.
  3314. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3315. 3. SHOW ALL: Show everything.
  3316. - When point is at the beginning of a headline, rotate the subtree started
  3317. by this line through 3 different states (local cycling)
  3318. 1. FOLDED: Only the main headline is shown.
  3319. 2. CHILDREN: The main headline and the direct children are shown.
  3320. From this state, you can move to one of the children
  3321. and zoom in further.
  3322. 3. SUBTREE: Show the entire subtree, including body text.
  3323. - When there is a numeric prefix, go up to a heading with level ARG, do
  3324. a `show-subtree' and return to the previous cursor position. If ARG
  3325. is negative, go up that many levels.
  3326. - When point is not at the beginning of a headline, execute
  3327. `indent-relative', like TAB normally does. See the option
  3328. `org-cycle-emulate-tab' for details.
  3329. - Special case: if point is at the beginning of the buffer and there is
  3330. no headline in line 1, this function will act as if called with prefix arg.
  3331. But only if also the variable `org-cycle-global-at-bob' is t."
  3332. (interactive "P")
  3333. (org-load-modules-maybe)
  3334. (let* ((outline-regexp
  3335. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3336. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3337. outline-regexp))
  3338. (bob-special (and org-cycle-global-at-bob (bobp)
  3339. (not (looking-at outline-regexp))))
  3340. (org-cycle-hook
  3341. (if bob-special
  3342. (delq 'org-optimize-window-after-visibility-change
  3343. (copy-sequence org-cycle-hook))
  3344. org-cycle-hook))
  3345. (pos (point)))
  3346. (if (or bob-special (equal arg '(4)))
  3347. ;; special case: use global cycling
  3348. (setq arg t))
  3349. (cond
  3350. ((org-at-table-p 'any)
  3351. ;; Enter the table or move to the next field in the table
  3352. (or (org-table-recognize-table.el)
  3353. (progn
  3354. (if arg (org-table-edit-field t)
  3355. (org-table-justify-field-maybe)
  3356. (call-interactively 'org-table-next-field)))))
  3357. ((eq arg t) ;; Global cycling
  3358. (cond
  3359. ((and (eq last-command this-command)
  3360. (eq org-cycle-global-status 'overview))
  3361. ;; We just created the overview - now do table of contents
  3362. ;; This can be slow in very large buffers, so indicate action
  3363. (message "CONTENTS...")
  3364. (org-content)
  3365. (message "CONTENTS...done")
  3366. (setq org-cycle-global-status 'contents)
  3367. (run-hook-with-args 'org-cycle-hook 'contents))
  3368. ((and (eq last-command this-command)
  3369. (eq org-cycle-global-status 'contents))
  3370. ;; We just showed the table of contents - now show everything
  3371. (show-all)
  3372. (message "SHOW ALL")
  3373. (setq org-cycle-global-status 'all)
  3374. (run-hook-with-args 'org-cycle-hook 'all))
  3375. (t
  3376. ;; Default action: go to overview
  3377. (org-overview)
  3378. (message "OVERVIEW")
  3379. (setq org-cycle-global-status 'overview)
  3380. (run-hook-with-args 'org-cycle-hook 'overview))))
  3381. ((and org-drawers org-drawer-regexp
  3382. (save-excursion
  3383. (beginning-of-line 1)
  3384. (looking-at org-drawer-regexp)))
  3385. ;; Toggle block visibility
  3386. (org-flag-drawer
  3387. (not (get-char-property (match-end 0) 'invisible))))
  3388. ((integerp arg)
  3389. ;; Show-subtree, ARG levels up from here.
  3390. (save-excursion
  3391. (org-back-to-heading)
  3392. (outline-up-heading (if (< arg 0) (- arg)
  3393. (- (funcall outline-level) arg)))
  3394. (org-show-subtree)))
  3395. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3396. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3397. ;; At a heading: rotate between three different views
  3398. (org-back-to-heading)
  3399. (let ((goal-column 0) eoh eol eos)
  3400. ;; First, some boundaries
  3401. (save-excursion
  3402. (org-back-to-heading)
  3403. (save-excursion
  3404. (beginning-of-line 2)
  3405. (while (and (not (eobp)) ;; this is like `next-line'
  3406. (get-char-property (1- (point)) 'invisible))
  3407. (beginning-of-line 2)) (setq eol (point)))
  3408. (outline-end-of-heading) (setq eoh (point))
  3409. (org-end-of-subtree t)
  3410. (unless (eobp)
  3411. (skip-chars-forward " \t\n")
  3412. (beginning-of-line 1) ; in case this is an item
  3413. )
  3414. (setq eos (1- (point))))
  3415. ;; Find out what to do next and set `this-command'
  3416. (cond
  3417. ((= eos eoh)
  3418. ;; Nothing is hidden behind this heading
  3419. (message "EMPTY ENTRY")
  3420. (setq org-cycle-subtree-status nil)
  3421. (save-excursion
  3422. (goto-char eos)
  3423. (outline-next-heading)
  3424. (if (org-invisible-p) (org-flag-heading nil))))
  3425. ((or (>= eol eos)
  3426. (not (string-match "\\S-" (buffer-substring eol eos))))
  3427. ;; Entire subtree is hidden in one line: open it
  3428. (org-show-entry)
  3429. (show-children)
  3430. (message "CHILDREN")
  3431. (save-excursion
  3432. (goto-char eos)
  3433. (outline-next-heading)
  3434. (if (org-invisible-p) (org-flag-heading nil)))
  3435. (setq org-cycle-subtree-status 'children)
  3436. (run-hook-with-args 'org-cycle-hook 'children))
  3437. ((and (eq last-command this-command)
  3438. (eq org-cycle-subtree-status 'children))
  3439. ;; We just showed the children, now show everything.
  3440. (org-show-subtree)
  3441. (message "SUBTREE")
  3442. (setq org-cycle-subtree-status 'subtree)
  3443. (run-hook-with-args 'org-cycle-hook 'subtree))
  3444. (t
  3445. ;; Default action: hide the subtree.
  3446. (hide-subtree)
  3447. (message "FOLDED")
  3448. (setq org-cycle-subtree-status 'folded)
  3449. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3450. ;; TAB emulation
  3451. (buffer-read-only (org-back-to-heading))
  3452. ((org-try-cdlatex-tab))
  3453. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3454. (or (not (bolp))
  3455. (not (looking-at outline-regexp))))
  3456. (call-interactively (global-key-binding "\t")))
  3457. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3458. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3459. (or (and (eq org-cycle-emulate-tab 'white)
  3460. (= (match-end 0) (point-at-eol)))
  3461. (and (eq org-cycle-emulate-tab 'whitestart)
  3462. (>= (match-end 0) pos))))
  3463. t
  3464. (eq org-cycle-emulate-tab t))
  3465. (call-interactively (global-key-binding "\t")))
  3466. (t (save-excursion
  3467. (org-back-to-heading)
  3468. (org-cycle))))))
  3469. ;;;###autoload
  3470. (defun org-global-cycle (&optional arg)
  3471. "Cycle the global visibility. For details see `org-cycle'."
  3472. (interactive "P")
  3473. (let ((org-cycle-include-plain-lists
  3474. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3475. (if (integerp arg)
  3476. (progn
  3477. (show-all)
  3478. (hide-sublevels arg)
  3479. (setq org-cycle-global-status 'contents))
  3480. (org-cycle '(4)))))
  3481. (defun org-overview ()
  3482. "Switch to overview mode, shoing only top-level headlines.
  3483. Really, this shows all headlines with level equal or greater than the level
  3484. of the first headline in the buffer. This is important, because if the
  3485. first headline is not level one, then (hide-sublevels 1) gives confusing
  3486. results."
  3487. (interactive)
  3488. (let ((level (save-excursion
  3489. (goto-char (point-min))
  3490. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3491. (progn
  3492. (goto-char (match-beginning 0))
  3493. (funcall outline-level))))))
  3494. (and level (hide-sublevels level))))
  3495. (defun org-content (&optional arg)
  3496. "Show all headlines in the buffer, like a table of contents.
  3497. With numerical argument N, show content up to level N."
  3498. (interactive "P")
  3499. (save-excursion
  3500. ;; Visit all headings and show their offspring
  3501. (and (integerp arg) (org-overview))
  3502. (goto-char (point-max))
  3503. (catch 'exit
  3504. (while (and (progn (condition-case nil
  3505. (outline-previous-visible-heading 1)
  3506. (error (goto-char (point-min))))
  3507. t)
  3508. (looking-at outline-regexp))
  3509. (if (integerp arg)
  3510. (show-children (1- arg))
  3511. (show-branches))
  3512. (if (bobp) (throw 'exit nil))))))
  3513. (defun org-optimize-window-after-visibility-change (state)
  3514. "Adjust the window after a change in outline visibility.
  3515. This function is the default value of the hook `org-cycle-hook'."
  3516. (when (get-buffer-window (current-buffer))
  3517. (cond
  3518. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3519. ; ((eq state 'overview) (org-beginning-of-line))
  3520. ((eq state 'content) nil)
  3521. ((eq state 'all) nil)
  3522. ((eq state 'folded) nil)
  3523. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3524. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3525. (defun org-compact-display-after-subtree-move ()
  3526. (let (beg end)
  3527. (save-excursion
  3528. (if (org-up-heading-safe)
  3529. (progn
  3530. (hide-subtree)
  3531. (show-entry)
  3532. (show-children)
  3533. (org-cycle-show-empty-lines 'children)
  3534. (org-cycle-hide-drawers 'children))
  3535. (org-overview)))))
  3536. (defun org-cycle-show-empty-lines (state)
  3537. "Show empty lines above all visible headlines.
  3538. The region to be covered depends on STATE when called through
  3539. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3540. entire buffer covered. Note that an empty line is only shown if there
  3541. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3542. (when (> org-cycle-separator-lines 0)
  3543. (save-excursion
  3544. (let* ((n org-cycle-separator-lines)
  3545. (re (cond
  3546. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3547. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3548. (t (let ((ns (number-to-string (- n 2))))
  3549. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3550. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3551. beg end)
  3552. (cond
  3553. ((memq state '(overview contents t))
  3554. (setq beg (point-min) end (point-max)))
  3555. ((memq state '(children folded))
  3556. (setq beg (point) end (progn (org-end-of-subtree t t)
  3557. (beginning-of-line 2)
  3558. (point)))))
  3559. (when beg
  3560. (goto-char beg)
  3561. (while (re-search-forward re end t)
  3562. (if (not (get-char-property (match-end 1) 'invisible))
  3563. (outline-flag-region
  3564. (match-beginning 1) (match-end 1) nil)))))))
  3565. ;; Never hide empty lines at the end of the file.
  3566. (save-excursion
  3567. (goto-char (point-max))
  3568. (outline-previous-heading)
  3569. (outline-end-of-heading)
  3570. (if (and (looking-at "[ \t\n]+")
  3571. (= (match-end 0) (point-max)))
  3572. (outline-flag-region (point) (match-end 0) nil))))
  3573. (defun org-cycle-hide-drawers (state)
  3574. "Re-hide all drawers after a visibility state change."
  3575. (when (and (org-mode-p)
  3576. (not (memq state '(overview folded))))
  3577. (save-excursion
  3578. (let* ((globalp (memq state '(contents all)))
  3579. (beg (if globalp (point-min) (point)))
  3580. (end (if globalp (point-max) (org-end-of-subtree t))))
  3581. (goto-char beg)
  3582. (while (re-search-forward org-drawer-regexp end t)
  3583. (org-flag-drawer t))))))
  3584. (defun org-flag-drawer (flag)
  3585. (save-excursion
  3586. (beginning-of-line 1)
  3587. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3588. (let ((b (match-end 0))
  3589. (outline-regexp org-outline-regexp))
  3590. (if (re-search-forward
  3591. "^[ \t]*:END:"
  3592. (save-excursion (outline-next-heading) (point)) t)
  3593. (outline-flag-region b (point-at-eol) flag)
  3594. (error ":END: line missing"))))))
  3595. (defun org-subtree-end-visible-p ()
  3596. "Is the end of the current subtree visible?"
  3597. (pos-visible-in-window-p
  3598. (save-excursion (org-end-of-subtree t) (point))))
  3599. (defun org-first-headline-recenter (&optional N)
  3600. "Move cursor to the first headline and recenter the headline.
  3601. Optional argument N means, put the headline into the Nth line of the window."
  3602. (goto-char (point-min))
  3603. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3604. (beginning-of-line)
  3605. (recenter (prefix-numeric-value N))))
  3606. ;;; Org-goto
  3607. (defvar org-goto-window-configuration nil)
  3608. (defvar org-goto-marker nil)
  3609. (defvar org-goto-map
  3610. (let ((map (make-sparse-keymap)))
  3611. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3612. (while (setq cmd (pop cmds))
  3613. (substitute-key-definition cmd cmd map global-map)))
  3614. (suppress-keymap map)
  3615. (org-defkey map "\C-m" 'org-goto-ret)
  3616. (org-defkey map [(return)] 'org-goto-ret)
  3617. (org-defkey map [(left)] 'org-goto-left)
  3618. (org-defkey map [(right)] 'org-goto-right)
  3619. (org-defkey map [(control ?g)] 'org-goto-quit)
  3620. (org-defkey map "\C-i" 'org-cycle)
  3621. (org-defkey map [(tab)] 'org-cycle)
  3622. (org-defkey map [(down)] 'outline-next-visible-heading)
  3623. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3624. (if org-goto-auto-isearch
  3625. (if (fboundp 'define-key-after)
  3626. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3627. nil)
  3628. (org-defkey map "q" 'org-goto-quit)
  3629. (org-defkey map "n" 'outline-next-visible-heading)
  3630. (org-defkey map "p" 'outline-previous-visible-heading)
  3631. (org-defkey map "f" 'outline-forward-same-level)
  3632. (org-defkey map "b" 'outline-backward-same-level)
  3633. (org-defkey map "u" 'outline-up-heading))
  3634. (org-defkey map "/" 'org-occur)
  3635. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3636. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3637. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3638. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3639. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3640. map))
  3641. (defconst org-goto-help
  3642. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3643. RET=jump to location [Q]uit and return to previous location
  3644. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3645. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3646. (defun org-goto (&optional alternative-interface)
  3647. "Look up a different location in the current file, keeping current visibility.
  3648. When you want look-up or go to a different location in a document, the
  3649. fastest way is often to fold the entire buffer and then dive into the tree.
  3650. This method has the disadvantage, that the previous location will be folded,
  3651. which may not be what you want.
  3652. This command works around this by showing a copy of the current buffer
  3653. in an indirect buffer, in overview mode. You can dive into the tree in
  3654. that copy, use org-occur and incremental search to find a location.
  3655. When pressing RET or `Q', the command returns to the original buffer in
  3656. which the visibility is still unchanged. After RET is will also jump to
  3657. the location selected in the indirect buffer and expose the
  3658. the headline hierarchy above."
  3659. (interactive "P")
  3660. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3661. (org-refile-use-outline-path t)
  3662. (interface
  3663. (if (not alternative-interface)
  3664. org-goto-interface
  3665. (if (eq org-goto-interface 'outline)
  3666. 'outline-path-completion
  3667. 'outline)))
  3668. (org-goto-start-pos (point))
  3669. (selected-point
  3670. (if (eq interface 'outline)
  3671. (car (org-get-location (current-buffer) org-goto-help))
  3672. (nth 3 (org-refile-get-location "Goto: ")))))
  3673. (if selected-point
  3674. (progn
  3675. (org-mark-ring-push org-goto-start-pos)
  3676. (goto-char selected-point)
  3677. (if (or (org-invisible-p) (org-invisible-p2))
  3678. (org-show-context 'org-goto)))
  3679. (message "Quit"))))
  3680. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3681. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3682. (defvar org-goto-local-auto-isearch-map) ; defined below
  3683. (defun org-get-location (buf help)
  3684. "Let the user select a location in the Org-mode buffer BUF.
  3685. This function uses a recursive edit. It returns the selected position
  3686. or nil."
  3687. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3688. (isearch-hide-immediately nil)
  3689. (isearch-search-fun-function
  3690. (lambda () 'org-goto-local-search-forward-headings))
  3691. (org-goto-selected-point org-goto-exit-command))
  3692. (save-excursion
  3693. (save-window-excursion
  3694. (delete-other-windows)
  3695. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3696. (switch-to-buffer
  3697. (condition-case nil
  3698. (make-indirect-buffer (current-buffer) "*org-goto*")
  3699. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3700. (with-output-to-temp-buffer "*Help*"
  3701. (princ help))
  3702. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3703. (setq buffer-read-only nil)
  3704. (let ((org-startup-truncated t)
  3705. (org-startup-folded nil)
  3706. (org-startup-align-all-tables nil))
  3707. (org-mode)
  3708. (org-overview))
  3709. (setq buffer-read-only t)
  3710. (if (and (boundp 'org-goto-start-pos)
  3711. (integer-or-marker-p org-goto-start-pos))
  3712. (let ((org-show-hierarchy-above t)
  3713. (org-show-siblings t)
  3714. (org-show-following-heading t))
  3715. (goto-char org-goto-start-pos)
  3716. (and (org-invisible-p) (org-show-context)))
  3717. (goto-char (point-min)))
  3718. (org-beginning-of-line)
  3719. (message "Select location and press RET")
  3720. (use-local-map org-goto-map)
  3721. (recursive-edit)
  3722. ))
  3723. (kill-buffer "*org-goto*")
  3724. (cons org-goto-selected-point org-goto-exit-command)))
  3725. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3726. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3727. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3728. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3729. (defun org-goto-local-search-forward-headings (string bound noerror)
  3730. "Search and make sure that anu matches are in headlines."
  3731. (catch 'return
  3732. (while (search-forward string bound noerror)
  3733. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3734. (and (member :headline context)
  3735. (not (member :tags context))))
  3736. (throw 'return (point))))))
  3737. (defun org-goto-local-auto-isearch ()
  3738. "Start isearch."
  3739. (interactive)
  3740. (goto-char (point-min))
  3741. (let ((keys (this-command-keys)))
  3742. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3743. (isearch-mode t)
  3744. (isearch-process-search-char (string-to-char keys)))))
  3745. (defun org-goto-ret (&optional arg)
  3746. "Finish `org-goto' by going to the new location."
  3747. (interactive "P")
  3748. (setq org-goto-selected-point (point)
  3749. org-goto-exit-command 'return)
  3750. (throw 'exit nil))
  3751. (defun org-goto-left ()
  3752. "Finish `org-goto' by going to the new location."
  3753. (interactive)
  3754. (if (org-on-heading-p)
  3755. (progn
  3756. (beginning-of-line 1)
  3757. (setq org-goto-selected-point (point)
  3758. org-goto-exit-command 'left)
  3759. (throw 'exit nil))
  3760. (error "Not on a heading")))
  3761. (defun org-goto-right ()
  3762. "Finish `org-goto' by going to the new location."
  3763. (interactive)
  3764. (if (org-on-heading-p)
  3765. (progn
  3766. (setq org-goto-selected-point (point)
  3767. org-goto-exit-command 'right)
  3768. (throw 'exit nil))
  3769. (error "Not on a heading")))
  3770. (defun org-goto-quit ()
  3771. "Finish `org-goto' without cursor motion."
  3772. (interactive)
  3773. (setq org-goto-selected-point nil)
  3774. (setq org-goto-exit-command 'quit)
  3775. (throw 'exit nil))
  3776. ;;; Indirect buffer display of subtrees
  3777. (defvar org-indirect-dedicated-frame nil
  3778. "This is the frame being used for indirect tree display.")
  3779. (defvar org-last-indirect-buffer nil)
  3780. (defun org-tree-to-indirect-buffer (&optional arg)
  3781. "Create indirect buffer and narrow it to current subtree.
  3782. With numerical prefix ARG, go up to this level and then take that tree.
  3783. If ARG is negative, go up that many levels.
  3784. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3785. indirect buffer previously made with this command, to avoid proliferation of
  3786. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3787. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3788. is kept so that you can work with several indirect buffers at the same time.
  3789. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3790. requests that a new frame be made for the new buffer, so that the dedicated
  3791. frame is not changed."
  3792. (interactive "P")
  3793. (let ((cbuf (current-buffer))
  3794. (cwin (selected-window))
  3795. (pos (point))
  3796. beg end level heading ibuf)
  3797. (save-excursion
  3798. (org-back-to-heading t)
  3799. (when (numberp arg)
  3800. (setq level (org-outline-level))
  3801. (if (< arg 0) (setq arg (+ level arg)))
  3802. (while (> (setq level (org-outline-level)) arg)
  3803. (outline-up-heading 1 t)))
  3804. (setq beg (point)
  3805. heading (org-get-heading))
  3806. (org-end-of-subtree t) (setq end (point)))
  3807. (if (and (buffer-live-p org-last-indirect-buffer)
  3808. (not (eq org-indirect-buffer-display 'new-frame))
  3809. (not arg))
  3810. (kill-buffer org-last-indirect-buffer))
  3811. (setq ibuf (org-get-indirect-buffer cbuf)
  3812. org-last-indirect-buffer ibuf)
  3813. (cond
  3814. ((or (eq org-indirect-buffer-display 'new-frame)
  3815. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3816. (select-frame (make-frame))
  3817. (delete-other-windows)
  3818. (switch-to-buffer ibuf)
  3819. (org-set-frame-title heading))
  3820. ((eq org-indirect-buffer-display 'dedicated-frame)
  3821. (raise-frame
  3822. (select-frame (or (and org-indirect-dedicated-frame
  3823. (frame-live-p org-indirect-dedicated-frame)
  3824. org-indirect-dedicated-frame)
  3825. (setq org-indirect-dedicated-frame (make-frame)))))
  3826. (delete-other-windows)
  3827. (switch-to-buffer ibuf)
  3828. (org-set-frame-title (concat "Indirect: " heading)))
  3829. ((eq org-indirect-buffer-display 'current-window)
  3830. (switch-to-buffer ibuf))
  3831. ((eq org-indirect-buffer-display 'other-window)
  3832. (pop-to-buffer ibuf))
  3833. (t (error "Invalid value.")))
  3834. (if (featurep 'xemacs)
  3835. (save-excursion (org-mode) (turn-on-font-lock)))
  3836. (narrow-to-region beg end)
  3837. (show-all)
  3838. (goto-char pos)
  3839. (and (window-live-p cwin) (select-window cwin))))
  3840. (defun org-get-indirect-buffer (&optional buffer)
  3841. (setq buffer (or buffer (current-buffer)))
  3842. (let ((n 1) (base (buffer-name buffer)) bname)
  3843. (while (buffer-live-p
  3844. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3845. (setq n (1+ n)))
  3846. (condition-case nil
  3847. (make-indirect-buffer buffer bname 'clone)
  3848. (error (make-indirect-buffer buffer bname)))))
  3849. (defun org-set-frame-title (title)
  3850. "Set the title of the current frame to the string TITLE."
  3851. ;; FIXME: how to name a single frame in XEmacs???
  3852. (unless (featurep 'xemacs)
  3853. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3854. ;;;; Structure editing
  3855. ;;; Inserting headlines
  3856. (defun org-insert-heading (&optional force-heading)
  3857. "Insert a new heading or item with same depth at point.
  3858. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3859. If point is at the beginning of a headline, insert a sibling before the
  3860. current headline. If point is not at the beginning, do not split the line,
  3861. but create the new hedline after the current line."
  3862. (interactive "P")
  3863. (if (= (buffer-size) 0)
  3864. (insert "\n* ")
  3865. (when (or force-heading (not (org-insert-item)))
  3866. (let* ((head (save-excursion
  3867. (condition-case nil
  3868. (progn
  3869. (org-back-to-heading)
  3870. (match-string 0))
  3871. (error "*"))))
  3872. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3873. pos)
  3874. (cond
  3875. ((and (org-on-heading-p) (bolp)
  3876. (or (bobp)
  3877. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3878. ;; insert before the current line
  3879. (open-line (if blank 2 1)))
  3880. ((and (bolp)
  3881. (or (bobp)
  3882. (save-excursion
  3883. (backward-char 1) (not (org-invisible-p)))))
  3884. ;; insert right here
  3885. nil)
  3886. (t
  3887. ;; in the middle of the line
  3888. (org-show-entry)
  3889. (let ((split
  3890. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3891. tags pos)
  3892. (if (org-on-heading-p)
  3893. (progn
  3894. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3895. (setq tags (and (match-end 2) (match-string 2)))
  3896. (and (match-end 1)
  3897. (delete-region (match-beginning 1) (match-end 1)))
  3898. (setq pos (point-at-bol))
  3899. (or split (end-of-line 1))
  3900. (delete-horizontal-space)
  3901. (newline (if blank 2 1))
  3902. (when tags
  3903. (save-excursion
  3904. (goto-char pos)
  3905. (end-of-line 1)
  3906. (insert " " tags)
  3907. (org-set-tags nil 'align))))
  3908. (or split (end-of-line 1))
  3909. (newline (if blank 2 1))))))
  3910. (insert head) (just-one-space)
  3911. (setq pos (point))
  3912. (end-of-line 1)
  3913. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3914. (run-hooks 'org-insert-heading-hook)))))
  3915. (defun org-get-heading (&optional no-tags)
  3916. "Return the heading of the current entry, without the stars."
  3917. (save-excursion
  3918. (org-back-to-heading t)
  3919. (if (looking-at
  3920. (if no-tags
  3921. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  3922. "\\*+[ \t]+\\([^\r\n]*\\)"))
  3923. (match-string 1) "")))
  3924. (defun org-insert-heading-after-current ()
  3925. "Insert a new heading with same level as current, after current subtree."
  3926. (interactive)
  3927. (org-back-to-heading)
  3928. (org-insert-heading)
  3929. (org-move-subtree-down)
  3930. (end-of-line 1))
  3931. (defun org-insert-todo-heading (arg)
  3932. "Insert a new heading with the same level and TODO state as current heading.
  3933. If the heading has no TODO state, or if the state is DONE, use the first
  3934. state (TODO by default). Also with prefix arg, force first state."
  3935. (interactive "P")
  3936. (when (not (org-insert-item 'checkbox))
  3937. (org-insert-heading)
  3938. (save-excursion
  3939. (org-back-to-heading)
  3940. (outline-previous-heading)
  3941. (looking-at org-todo-line-regexp))
  3942. (if (or arg
  3943. (not (match-beginning 2))
  3944. (member (match-string 2) org-done-keywords))
  3945. (insert (car org-todo-keywords-1) " ")
  3946. (insert (match-string 2) " "))))
  3947. (defun org-insert-subheading (arg)
  3948. "Insert a new subheading and demote it.
  3949. Works for outline headings and for plain lists alike."
  3950. (interactive "P")
  3951. (org-insert-heading arg)
  3952. (cond
  3953. ((org-on-heading-p) (org-do-demote))
  3954. ((org-at-item-p) (org-indent-item 1))))
  3955. (defun org-insert-todo-subheading (arg)
  3956. "Insert a new subheading with TODO keyword or checkbox and demote it.
  3957. Works for outline headings and for plain lists alike."
  3958. (interactive "P")
  3959. (org-insert-todo-heading arg)
  3960. (cond
  3961. ((org-on-heading-p) (org-do-demote))
  3962. ((org-at-item-p) (org-indent-item 1))))
  3963. ;;; Promotion and Demotion
  3964. (defun org-promote-subtree ()
  3965. "Promote the entire subtree.
  3966. See also `org-promote'."
  3967. (interactive)
  3968. (save-excursion
  3969. (org-map-tree 'org-promote))
  3970. (org-fix-position-after-promote))
  3971. (defun org-demote-subtree ()
  3972. "Demote the entire subtree. See `org-demote'.
  3973. See also `org-promote'."
  3974. (interactive)
  3975. (save-excursion
  3976. (org-map-tree 'org-demote))
  3977. (org-fix-position-after-promote))
  3978. (defun org-do-promote ()
  3979. "Promote the current heading higher up the tree.
  3980. If the region is active in `transient-mark-mode', promote all headings
  3981. in the region."
  3982. (interactive)
  3983. (save-excursion
  3984. (if (org-region-active-p)
  3985. (org-map-region 'org-promote (region-beginning) (region-end))
  3986. (org-promote)))
  3987. (org-fix-position-after-promote))
  3988. (defun org-do-demote ()
  3989. "Demote the current heading lower down the tree.
  3990. If the region is active in `transient-mark-mode', demote all headings
  3991. in the region."
  3992. (interactive)
  3993. (save-excursion
  3994. (if (org-region-active-p)
  3995. (org-map-region 'org-demote (region-beginning) (region-end))
  3996. (org-demote)))
  3997. (org-fix-position-after-promote))
  3998. (defun org-fix-position-after-promote ()
  3999. "Make sure that after pro/demotion cursor position is right."
  4000. (let ((pos (point)))
  4001. (when (save-excursion
  4002. (beginning-of-line 1)
  4003. (looking-at org-todo-line-regexp)
  4004. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4005. (cond ((eobp) (insert " "))
  4006. ((eolp) (insert " "))
  4007. ((equal (char-after) ?\ ) (forward-char 1))))))
  4008. (defun org-reduced-level (l)
  4009. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4010. (defun org-get-valid-level (level &optional change)
  4011. "Rectify a level change under the influence of `org-odd-levels-only'
  4012. LEVEL is a current level, CHANGE is by how much the level should be
  4013. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4014. even level numbers will become the next higher odd number."
  4015. (if org-odd-levels-only
  4016. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4017. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4018. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4019. (max 1 (+ level change))))
  4020. (if (boundp 'define-obsolete-function-alias)
  4021. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4022. (define-obsolete-function-alias 'org-get-legal-level
  4023. 'org-get-valid-level)
  4024. (define-obsolete-function-alias 'org-get-legal-level
  4025. 'org-get-valid-level "23.1")))
  4026. (defun org-promote ()
  4027. "Promote the current heading higher up the tree.
  4028. If the region is active in `transient-mark-mode', promote all headings
  4029. in the region."
  4030. (org-back-to-heading t)
  4031. (let* ((level (save-match-data (funcall outline-level)))
  4032. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4033. (diff (abs (- level (length up-head) -1))))
  4034. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4035. (replace-match up-head nil t)
  4036. ;; Fixup tag positioning
  4037. (and org-auto-align-tags (org-set-tags nil t))
  4038. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4039. (defun org-demote ()
  4040. "Demote the current heading lower down the tree.
  4041. If the region is active in `transient-mark-mode', demote all headings
  4042. in the region."
  4043. (org-back-to-heading t)
  4044. (let* ((level (save-match-data (funcall outline-level)))
  4045. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4046. (diff (abs (- level (length down-head) -1))))
  4047. (replace-match down-head nil t)
  4048. ;; Fixup tag positioning
  4049. (and org-auto-align-tags (org-set-tags nil t))
  4050. (if org-adapt-indentation (org-fixup-indentation diff))))
  4051. (defun org-map-tree (fun)
  4052. "Call FUN for every heading underneath the current one."
  4053. (org-back-to-heading)
  4054. (let ((level (funcall outline-level)))
  4055. (save-excursion
  4056. (funcall fun)
  4057. (while (and (progn
  4058. (outline-next-heading)
  4059. (> (funcall outline-level) level))
  4060. (not (eobp)))
  4061. (funcall fun)))))
  4062. (defun org-map-region (fun beg end)
  4063. "Call FUN for every heading between BEG and END."
  4064. (let ((org-ignore-region t))
  4065. (save-excursion
  4066. (setq end (copy-marker end))
  4067. (goto-char beg)
  4068. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4069. (< (point) end))
  4070. (funcall fun))
  4071. (while (and (progn
  4072. (outline-next-heading)
  4073. (< (point) end))
  4074. (not (eobp)))
  4075. (funcall fun)))))
  4076. (defun org-fixup-indentation (diff)
  4077. "Change the indentation in the current entry by DIFF
  4078. However, if any line in the current entry has no indentation, or if it
  4079. would end up with no indentation after the change, nothing at all is done."
  4080. (save-excursion
  4081. (let ((end (save-excursion (outline-next-heading)
  4082. (point-marker)))
  4083. (prohibit (if (> diff 0)
  4084. "^\\S-"
  4085. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4086. col)
  4087. (unless (save-excursion (end-of-line 1)
  4088. (re-search-forward prohibit end t))
  4089. (while (and (< (point) end)
  4090. (re-search-forward "^[ \t]+" end t))
  4091. (goto-char (match-end 0))
  4092. (setq col (current-column))
  4093. (if (< diff 0) (replace-match ""))
  4094. (indent-to (+ diff col))))
  4095. (move-marker end nil))))
  4096. (defun org-convert-to-odd-levels ()
  4097. "Convert an org-mode file with all levels allowed to one with odd levels.
  4098. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4099. level 5 etc."
  4100. (interactive)
  4101. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4102. (let ((org-odd-levels-only nil) n)
  4103. (save-excursion
  4104. (goto-char (point-min))
  4105. (while (re-search-forward "^\\*\\*+ " nil t)
  4106. (setq n (- (length (match-string 0)) 2))
  4107. (while (>= (setq n (1- n)) 0)
  4108. (org-demote))
  4109. (end-of-line 1))))))
  4110. (defun org-convert-to-oddeven-levels ()
  4111. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4112. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4113. section with an even level, conversion would destroy the structure of the file. An error
  4114. is signaled in this case."
  4115. (interactive)
  4116. (goto-char (point-min))
  4117. ;; First check if there are no even levels
  4118. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4119. (org-show-context t)
  4120. (error "Not all levels are odd in this file. Conversion not possible."))
  4121. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4122. (let ((org-odd-levels-only nil) n)
  4123. (save-excursion
  4124. (goto-char (point-min))
  4125. (while (re-search-forward "^\\*\\*+ " nil t)
  4126. (setq n (/ (1- (length (match-string 0))) 2))
  4127. (while (>= (setq n (1- n)) 0)
  4128. (org-promote))
  4129. (end-of-line 1))))))
  4130. (defun org-tr-level (n)
  4131. "Make N odd if required."
  4132. (if org-odd-levels-only (1+ (/ n 2)) n))
  4133. ;;; Vertical tree motion, cutting and pasting of subtrees
  4134. (defun org-move-subtree-up (&optional arg)
  4135. "Move the current subtree up past ARG headlines of the same level."
  4136. (interactive "p")
  4137. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4138. (defun org-move-subtree-down (&optional arg)
  4139. "Move the current subtree down past ARG headlines of the same level."
  4140. (interactive "p")
  4141. (setq arg (prefix-numeric-value arg))
  4142. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4143. 'outline-get-last-sibling))
  4144. (ins-point (make-marker))
  4145. (cnt (abs arg))
  4146. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4147. ;; Select the tree
  4148. (org-back-to-heading)
  4149. (setq beg0 (point))
  4150. (save-excursion
  4151. (setq ne-beg (org-back-over-empty-lines))
  4152. (setq beg (point)))
  4153. (save-match-data
  4154. (save-excursion (outline-end-of-heading)
  4155. (setq folded (org-invisible-p)))
  4156. (outline-end-of-subtree))
  4157. (outline-next-heading)
  4158. (setq ne-end (org-back-over-empty-lines))
  4159. (setq end (point))
  4160. (goto-char beg0)
  4161. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4162. ;; include less whitespace
  4163. (save-excursion
  4164. (goto-char beg)
  4165. (forward-line (- ne-beg ne-end))
  4166. (setq beg (point))))
  4167. ;; Find insertion point, with error handling
  4168. (while (> cnt 0)
  4169. (or (and (funcall movfunc) (looking-at outline-regexp))
  4170. (progn (goto-char beg0)
  4171. (error "Cannot move past superior level or buffer limit")))
  4172. (setq cnt (1- cnt)))
  4173. (if (> arg 0)
  4174. ;; Moving forward - still need to move over subtree
  4175. (progn (org-end-of-subtree t t)
  4176. (save-excursion
  4177. (org-back-over-empty-lines)
  4178. (or (bolp) (newline)))))
  4179. (setq ne-ins (org-back-over-empty-lines))
  4180. (move-marker ins-point (point))
  4181. (setq txt (buffer-substring beg end))
  4182. (delete-region beg end)
  4183. (outline-flag-region (1- beg) beg nil)
  4184. (outline-flag-region (1- (point)) (point) nil)
  4185. (insert txt)
  4186. (or (bolp) (insert "\n"))
  4187. (setq ins-end (point))
  4188. (goto-char ins-point)
  4189. (org-skip-whitespace)
  4190. (when (and (< arg 0)
  4191. (org-first-sibling-p)
  4192. (> ne-ins ne-beg))
  4193. ;; Move whitespace back to beginning
  4194. (save-excursion
  4195. (goto-char ins-end)
  4196. (let ((kill-whole-line t))
  4197. (kill-line (- ne-ins ne-beg)) (point)))
  4198. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4199. (move-marker ins-point nil)
  4200. (org-compact-display-after-subtree-move)
  4201. (unless folded
  4202. (org-show-entry)
  4203. (show-children)
  4204. (org-cycle-hide-drawers 'children))))
  4205. (defvar org-subtree-clip ""
  4206. "Clipboard for cut and paste of subtrees.
  4207. This is actually only a copy of the kill, because we use the normal kill
  4208. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4209. (defvar org-subtree-clip-folded nil
  4210. "Was the last copied subtree folded?
  4211. This is used to fold the tree back after pasting.")
  4212. (defun org-cut-subtree (&optional n)
  4213. "Cut the current subtree into the clipboard.
  4214. With prefix arg N, cut this many sequential subtrees.
  4215. This is a short-hand for marking the subtree and then cutting it."
  4216. (interactive "p")
  4217. (org-copy-subtree n 'cut))
  4218. (defun org-copy-subtree (&optional n cut)
  4219. "Cut the current subtree into the clipboard.
  4220. With prefix arg N, cut this many sequential subtrees.
  4221. This is a short-hand for marking the subtree and then copying it.
  4222. If CUT is non-nil, actually cut the subtree."
  4223. (interactive "p")
  4224. (let (beg end folded (beg0 (point)))
  4225. (if (interactive-p)
  4226. (org-back-to-heading nil) ; take what looks like a subtree
  4227. (org-back-to-heading t)) ; take what is really there
  4228. (org-back-over-empty-lines)
  4229. (setq beg (point))
  4230. (skip-chars-forward " \t\r\n")
  4231. (save-match-data
  4232. (save-excursion (outline-end-of-heading)
  4233. (setq folded (org-invisible-p)))
  4234. (condition-case nil
  4235. (outline-forward-same-level (1- n))
  4236. (error nil))
  4237. (org-end-of-subtree t t))
  4238. (org-back-over-empty-lines)
  4239. (setq end (point))
  4240. (goto-char beg0)
  4241. (when (> end beg)
  4242. (setq org-subtree-clip-folded folded)
  4243. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4244. (setq org-subtree-clip (current-kill 0))
  4245. (message "%s: Subtree(s) with %d characters"
  4246. (if cut "Cut" "Copied")
  4247. (length org-subtree-clip)))))
  4248. (defun org-paste-subtree (&optional level tree)
  4249. "Paste the clipboard as a subtree, with modification of headline level.
  4250. The entire subtree is promoted or demoted in order to match a new headline
  4251. level. By default, the new level is derived from the visible headings
  4252. before and after the insertion point, and taken to be the inferior headline
  4253. level of the two. So if the previous visible heading is level 3 and the
  4254. next is level 4 (or vice versa), level 4 will be used for insertion.
  4255. This makes sure that the subtree remains an independent subtree and does
  4256. not swallow low level entries.
  4257. You can also force a different level, either by using a numeric prefix
  4258. argument, or by inserting the heading marker by hand. For example, if the
  4259. cursor is after \"*****\", then the tree will be shifted to level 5.
  4260. If you want to insert the tree as is, just use \\[yank].
  4261. If optional TREE is given, use this text instead of the kill ring."
  4262. (interactive "P")
  4263. (unless (org-kill-is-subtree-p tree)
  4264. (error "%s"
  4265. (substitute-command-keys
  4266. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4267. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4268. (^re (concat "^\\(" outline-regexp "\\)"))
  4269. (re (concat "\\(" outline-regexp "\\)"))
  4270. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4271. (old-level (if (string-match ^re txt)
  4272. (- (match-end 0) (match-beginning 0) 1)
  4273. -1))
  4274. (force-level (cond (level (prefix-numeric-value level))
  4275. ((string-match
  4276. ^re_ (buffer-substring (point-at-bol) (point)))
  4277. (- (match-end 1) (match-beginning 1)))
  4278. (t nil)))
  4279. (previous-level (save-excursion
  4280. (condition-case nil
  4281. (progn
  4282. (outline-previous-visible-heading 1)
  4283. (if (looking-at re)
  4284. (- (match-end 0) (match-beginning 0) 1)
  4285. 1))
  4286. (error 1))))
  4287. (next-level (save-excursion
  4288. (condition-case nil
  4289. (progn
  4290. (or (looking-at outline-regexp)
  4291. (outline-next-visible-heading 1))
  4292. (if (looking-at re)
  4293. (- (match-end 0) (match-beginning 0) 1)
  4294. 1))
  4295. (error 1))))
  4296. (new-level (or force-level (max previous-level next-level)))
  4297. (shift (if (or (= old-level -1)
  4298. (= new-level -1)
  4299. (= old-level new-level))
  4300. 0
  4301. (- new-level old-level)))
  4302. (delta (if (> shift 0) -1 1))
  4303. (func (if (> shift 0) 'org-demote 'org-promote))
  4304. (org-odd-levels-only nil)
  4305. beg end)
  4306. ;; Remove the forced level indicator
  4307. (if force-level
  4308. (delete-region (point-at-bol) (point)))
  4309. ;; Paste
  4310. (beginning-of-line 1)
  4311. (org-back-over-empty-lines)
  4312. (setq beg (point))
  4313. (insert-before-markers txt)
  4314. (unless (string-match "\n\\'" txt) (insert "\n"))
  4315. (setq end (point))
  4316. (goto-char beg)
  4317. (skip-chars-forward " \t\n\r")
  4318. (setq beg (point))
  4319. ;; Shift if necessary
  4320. (unless (= shift 0)
  4321. (save-restriction
  4322. (narrow-to-region beg end)
  4323. (while (not (= shift 0))
  4324. (org-map-region func (point-min) (point-max))
  4325. (setq shift (+ delta shift)))
  4326. (goto-char (point-min))))
  4327. (when (interactive-p)
  4328. (message "Clipboard pasted as level %d subtree" new-level))
  4329. (if (and kill-ring
  4330. (eq org-subtree-clip (current-kill 0))
  4331. org-subtree-clip-folded)
  4332. ;; The tree was folded before it was killed/copied
  4333. (hide-subtree))))
  4334. (defun org-kill-is-subtree-p (&optional txt)
  4335. "Check if the current kill is an outline subtree, or a set of trees.
  4336. Returns nil if kill does not start with a headline, or if the first
  4337. headline level is not the largest headline level in the tree.
  4338. So this will actually accept several entries of equal levels as well,
  4339. which is OK for `org-paste-subtree'.
  4340. If optional TXT is given, check this string instead of the current kill."
  4341. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4342. (start-level (and kill
  4343. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4344. org-outline-regexp "\\)")
  4345. kill)
  4346. (- (match-end 2) (match-beginning 2) 1)))
  4347. (re (concat "^" org-outline-regexp))
  4348. (start (1+ (match-beginning 2))))
  4349. (if (not start-level)
  4350. (progn
  4351. nil) ;; does not even start with a heading
  4352. (catch 'exit
  4353. (while (setq start (string-match re kill (1+ start)))
  4354. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4355. (throw 'exit nil)))
  4356. t))))
  4357. (defun org-narrow-to-subtree ()
  4358. "Narrow buffer to the current subtree."
  4359. (interactive)
  4360. (save-excursion
  4361. (save-match-data
  4362. (narrow-to-region
  4363. (progn (org-back-to-heading) (point))
  4364. (progn (org-end-of-subtree t t) (point))))))
  4365. ;;; Outline Sorting
  4366. (defun org-sort (with-case)
  4367. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4368. Optional argument WITH-CASE means sort case-sensitively."
  4369. (interactive "P")
  4370. (if (org-at-table-p)
  4371. (org-call-with-arg 'org-table-sort-lines with-case)
  4372. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4373. (defun org-sort-remove-invisible (s)
  4374. (remove-text-properties 0 (length s) org-rm-props s)
  4375. (while (string-match org-bracket-link-regexp s)
  4376. (setq s (replace-match (if (match-end 2)
  4377. (match-string 3 s)
  4378. (match-string 1 s)) t t s)))
  4379. s)
  4380. (defvar org-priority-regexp) ; defined later in the file
  4381. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4382. "Sort entries on a certain level of an outline tree.
  4383. If there is an active region, the entries in the region are sorted.
  4384. Else, if the cursor is before the first entry, sort the top-level items.
  4385. Else, the children of the entry at point are sorted.
  4386. Sorting can be alphabetically, numerically, and by date/time as given by
  4387. the first time stamp in the entry. The command prompts for the sorting
  4388. type unless it has been given to the function through the SORTING-TYPE
  4389. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4390. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4391. called with point at the beginning of the record. It must return either
  4392. a string or a number that should serve as the sorting key for that record.
  4393. Comparing entries ignores case by default. However, with an optional argument
  4394. WITH-CASE, the sorting considers case as well."
  4395. (interactive "P")
  4396. (let ((case-func (if with-case 'identity 'downcase))
  4397. start beg end stars re re2
  4398. txt what tmp plain-list-p)
  4399. ;; Find beginning and end of region to sort
  4400. (cond
  4401. ((org-region-active-p)
  4402. ;; we will sort the region
  4403. (setq end (region-end)
  4404. what "region")
  4405. (goto-char (region-beginning))
  4406. (if (not (org-on-heading-p)) (outline-next-heading))
  4407. (setq start (point)))
  4408. ((org-at-item-p)
  4409. ;; we will sort this plain list
  4410. (org-beginning-of-item-list) (setq start (point))
  4411. (org-end-of-item-list) (setq end (point))
  4412. (goto-char start)
  4413. (setq plain-list-p t
  4414. what "plain list"))
  4415. ((or (org-on-heading-p)
  4416. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4417. ;; we will sort the children of the current headline
  4418. (org-back-to-heading)
  4419. (setq start (point)
  4420. end (progn (org-end-of-subtree t t)
  4421. (org-back-over-empty-lines)
  4422. (point))
  4423. what "children")
  4424. (goto-char start)
  4425. (show-subtree)
  4426. (outline-next-heading))
  4427. (t
  4428. ;; we will sort the top-level entries in this file
  4429. (goto-char (point-min))
  4430. (or (org-on-heading-p) (outline-next-heading))
  4431. (setq start (point) end (point-max) what "top-level")
  4432. (goto-char start)
  4433. (show-all)))
  4434. (setq beg (point))
  4435. (if (>= beg end) (error "Nothing to sort"))
  4436. (unless plain-list-p
  4437. (looking-at "\\(\\*+\\)")
  4438. (setq stars (match-string 1)
  4439. re (concat "^" (regexp-quote stars) " +")
  4440. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4441. txt (buffer-substring beg end))
  4442. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4443. (if (and (not (equal stars "*")) (string-match re2 txt))
  4444. (error "Region to sort contains a level above the first entry")))
  4445. (unless sorting-type
  4446. (message
  4447. (if plain-list-p
  4448. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4449. "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:")
  4450. what)
  4451. (setq sorting-type (read-char-exclusive))
  4452. (and (= (downcase sorting-type) ?f)
  4453. (setq getkey-func
  4454. (completing-read "Sort using function: "
  4455. obarray 'fboundp t nil nil))
  4456. (setq getkey-func (intern getkey-func)))
  4457. (and (= (downcase sorting-type) ?r)
  4458. (setq property
  4459. (completing-read "Property: "
  4460. (mapcar 'list (org-buffer-property-keys t))
  4461. nil t))))
  4462. (message "Sorting entries...")
  4463. (save-restriction
  4464. (narrow-to-region start end)
  4465. (let ((dcst (downcase sorting-type))
  4466. (now (current-time)))
  4467. (sort-subr
  4468. (/= dcst sorting-type)
  4469. ;; This function moves to the beginning character of the "record" to
  4470. ;; be sorted.
  4471. (if plain-list-p
  4472. (lambda nil
  4473. (if (org-at-item-p) t (goto-char (point-max))))
  4474. (lambda nil
  4475. (if (re-search-forward re nil t)
  4476. (goto-char (match-beginning 0))
  4477. (goto-char (point-max)))))
  4478. ;; This function moves to the last character of the "record" being
  4479. ;; sorted.
  4480. (if plain-list-p
  4481. 'org-end-of-item
  4482. (lambda nil
  4483. (save-match-data
  4484. (condition-case nil
  4485. (outline-forward-same-level 1)
  4486. (error
  4487. (goto-char (point-max)))))))
  4488. ;; This function returns the value that gets sorted against.
  4489. (if plain-list-p
  4490. (lambda nil
  4491. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4492. (cond
  4493. ((= dcst ?n)
  4494. (string-to-number (buffer-substring (match-end 0)
  4495. (point-at-eol))))
  4496. ((= dcst ?a)
  4497. (buffer-substring (match-end 0) (point-at-eol)))
  4498. ((= dcst ?t)
  4499. (if (re-search-forward org-ts-regexp
  4500. (point-at-eol) t)
  4501. (org-time-string-to-time (match-string 0))
  4502. now))
  4503. ((= dcst ?f)
  4504. (if getkey-func
  4505. (progn
  4506. (setq tmp (funcall getkey-func))
  4507. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4508. tmp)
  4509. (error "Invalid key function `%s'" getkey-func)))
  4510. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4511. (lambda nil
  4512. (cond
  4513. ((= dcst ?n)
  4514. (if (looking-at outline-regexp)
  4515. (string-to-number (buffer-substring (match-end 0)
  4516. (point-at-eol)))
  4517. nil))
  4518. ((= dcst ?a)
  4519. (funcall case-func (buffer-substring (point-at-bol)
  4520. (point-at-eol))))
  4521. ((= dcst ?t)
  4522. (if (re-search-forward org-ts-regexp
  4523. (save-excursion
  4524. (forward-line 2)
  4525. (point)) t)
  4526. (org-time-string-to-time (match-string 0))
  4527. now))
  4528. ((= dcst ?p)
  4529. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4530. (string-to-char (match-string 2))
  4531. org-default-priority))
  4532. ((= dcst ?r)
  4533. (or (org-entry-get nil property) ""))
  4534. ((= dcst ?o)
  4535. (if (looking-at org-complex-heading-regexp)
  4536. (- 9999 (length (member (match-string 2)
  4537. org-todo-keywords-1)))))
  4538. ((= dcst ?f)
  4539. (if getkey-func
  4540. (progn
  4541. (setq tmp (funcall getkey-func))
  4542. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4543. tmp)
  4544. (error "Invalid key function `%s'" getkey-func)))
  4545. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4546. nil
  4547. (cond
  4548. ((= dcst ?a) 'string<)
  4549. ((= dcst ?t) 'time-less-p)
  4550. (t nil)))))
  4551. (message "Sorting entries...done")))
  4552. (defun org-do-sort (table what &optional with-case sorting-type)
  4553. "Sort TABLE of WHAT according to SORTING-TYPE.
  4554. The user will be prompted for the SORTING-TYPE if the call to this
  4555. function does not specify it. WHAT is only for the prompt, to indicate
  4556. what is being sorted. The sorting key will be extracted from
  4557. the car of the elements of the table.
  4558. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4559. (unless sorting-type
  4560. (message
  4561. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4562. what)
  4563. (setq sorting-type (read-char-exclusive)))
  4564. (let ((dcst (downcase sorting-type))
  4565. extractfun comparefun)
  4566. ;; Define the appropriate functions
  4567. (cond
  4568. ((= dcst ?n)
  4569. (setq extractfun 'string-to-number
  4570. comparefun (if (= dcst sorting-type) '< '>)))
  4571. ((= dcst ?a)
  4572. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4573. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4574. comparefun (if (= dcst sorting-type)
  4575. 'string<
  4576. (lambda (a b) (and (not (string< a b))
  4577. (not (string= a b)))))))
  4578. ((= dcst ?t)
  4579. (setq extractfun
  4580. (lambda (x)
  4581. (if (string-match org-ts-regexp x)
  4582. (time-to-seconds
  4583. (org-time-string-to-time (match-string 0 x)))
  4584. 0))
  4585. comparefun (if (= dcst sorting-type) '< '>)))
  4586. (t (error "Invalid sorting type `%c'" sorting-type)))
  4587. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4588. table)
  4589. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4590. ;;;; Plain list items, including checkboxes
  4591. ;;; Plain list items
  4592. (defun org-at-item-p ()
  4593. "Is point in a line starting a hand-formatted item?"
  4594. (let ((llt org-plain-list-ordered-item-terminator))
  4595. (save-excursion
  4596. (goto-char (point-at-bol))
  4597. (looking-at
  4598. (cond
  4599. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4600. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4601. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4602. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4603. (defun org-in-item-p ()
  4604. "It the cursor inside a plain list item.
  4605. Does not have to be the first line."
  4606. (save-excursion
  4607. (condition-case nil
  4608. (progn
  4609. (org-beginning-of-item)
  4610. (org-at-item-p)
  4611. t)
  4612. (error nil))))
  4613. (defun org-insert-item (&optional checkbox)
  4614. "Insert a new item at the current level.
  4615. Return t when things worked, nil when we are not in an item."
  4616. (when (save-excursion
  4617. (condition-case nil
  4618. (progn
  4619. (org-beginning-of-item)
  4620. (org-at-item-p)
  4621. (if (org-invisible-p) (error "Invisible item"))
  4622. t)
  4623. (error nil)))
  4624. (let* ((bul (match-string 0))
  4625. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4626. (match-end 0)))
  4627. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4628. pos)
  4629. (cond
  4630. ((and (org-at-item-p) (<= (point) eow))
  4631. ;; before the bullet
  4632. (beginning-of-line 1)
  4633. (open-line (if blank 2 1)))
  4634. ((<= (point) eow)
  4635. (beginning-of-line 1))
  4636. (t
  4637. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4638. (end-of-line 1)
  4639. (delete-horizontal-space))
  4640. (newline (if blank 2 1))))
  4641. (insert bul (if checkbox "[ ]" ""))
  4642. (just-one-space)
  4643. (setq pos (point))
  4644. (end-of-line 1)
  4645. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4646. (org-maybe-renumber-ordered-list)
  4647. (and checkbox (org-update-checkbox-count-maybe))
  4648. t))
  4649. ;;; Checkboxes
  4650. (defun org-at-item-checkbox-p ()
  4651. "Is point at a line starting a plain-list item with a checklet?"
  4652. (and (org-at-item-p)
  4653. (save-excursion
  4654. (goto-char (match-end 0))
  4655. (skip-chars-forward " \t")
  4656. (looking-at "\\[[- X]\\]"))))
  4657. (defun org-toggle-checkbox (&optional arg)
  4658. "Toggle the checkbox in the current line."
  4659. (interactive "P")
  4660. (catch 'exit
  4661. (let (beg end status (firstnew 'unknown))
  4662. (cond
  4663. ((org-region-active-p)
  4664. (setq beg (region-beginning) end (region-end)))
  4665. ((org-on-heading-p)
  4666. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4667. ((org-at-item-checkbox-p)
  4668. (let ((pos (point)))
  4669. (replace-match
  4670. (cond (arg "[-]")
  4671. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4672. (t "[ ]"))
  4673. t t)
  4674. (goto-char pos))
  4675. (throw 'exit t))
  4676. (t (error "Not at a checkbox or heading, and no active region")))
  4677. (save-excursion
  4678. (goto-char beg)
  4679. (while (< (point) end)
  4680. (when (org-at-item-checkbox-p)
  4681. (setq status (equal (match-string 0) "[X]"))
  4682. (when (eq firstnew 'unknown)
  4683. (setq firstnew (not status)))
  4684. (replace-match
  4685. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4686. (beginning-of-line 2)))))
  4687. (org-update-checkbox-count-maybe))
  4688. (defun org-update-checkbox-count-maybe ()
  4689. "Update checkbox statistics unless turned off by user."
  4690. (when org-provide-checkbox-statistics
  4691. (org-update-checkbox-count)))
  4692. (defun org-update-checkbox-count (&optional all)
  4693. "Update the checkbox statistics in the current section.
  4694. This will find all statistic cookies like [57%] and [6/12] and update them
  4695. with the current numbers. With optional prefix argument ALL, do this for
  4696. the whole buffer."
  4697. (interactive "P")
  4698. (save-excursion
  4699. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4700. (beg (condition-case nil
  4701. (progn (outline-back-to-heading) (point))
  4702. (error (point-min))))
  4703. (end (move-marker (make-marker)
  4704. (progn (outline-next-heading) (point))))
  4705. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4706. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4707. (re-find (concat re "\\|" re-box))
  4708. beg-cookie end-cookie is-percent c-on c-off lim
  4709. eline curr-ind next-ind continue-from startsearch
  4710. (cstat 0)
  4711. )
  4712. (when all
  4713. (goto-char (point-min))
  4714. (outline-next-heading)
  4715. (setq beg (point) end (point-max)))
  4716. (goto-char end)
  4717. ;; find each statistic cookie
  4718. (while (re-search-backward re-find beg t)
  4719. (setq beg-cookie (match-beginning 1)
  4720. end-cookie (match-end 1)
  4721. cstat (+ cstat (if end-cookie 1 0))
  4722. startsearch (point-at-eol)
  4723. continue-from (point-at-bol)
  4724. is-percent (match-beginning 2)
  4725. lim (cond
  4726. ((org-on-heading-p) (outline-next-heading) (point))
  4727. ((org-at-item-p) (org-end-of-item) (point))
  4728. (t nil))
  4729. c-on 0
  4730. c-off 0)
  4731. (when lim
  4732. ;; find first checkbox for this cookie and gather
  4733. ;; statistics from all that are at this indentation level
  4734. (goto-char startsearch)
  4735. (if (re-search-forward re-box lim t)
  4736. (progn
  4737. (org-beginning-of-item)
  4738. (setq curr-ind (org-get-indentation))
  4739. (setq next-ind curr-ind)
  4740. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4741. (save-excursion (end-of-line) (setq eline (point)))
  4742. (if (re-search-forward re-box eline t)
  4743. (if (member (match-string 2) '("[ ]" "[-]"))
  4744. (setq c-off (1+ c-off))
  4745. (setq c-on (1+ c-on))
  4746. )
  4747. )
  4748. (org-end-of-item)
  4749. (setq next-ind (org-get-indentation))
  4750. )))
  4751. (goto-char continue-from)
  4752. ;; update cookie
  4753. (when end-cookie
  4754. (delete-region beg-cookie end-cookie)
  4755. (goto-char beg-cookie)
  4756. (insert
  4757. (if is-percent
  4758. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4759. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4760. ;; update items checkbox if it has one
  4761. (when (org-at-item-p)
  4762. (org-beginning-of-item)
  4763. (when (and (> (+ c-on c-off) 0)
  4764. (re-search-forward re-box (point-at-eol) t))
  4765. (setq beg-cookie (match-beginning 2)
  4766. end-cookie (match-end 2))
  4767. (delete-region beg-cookie end-cookie)
  4768. (goto-char beg-cookie)
  4769. (cond ((= c-off 0) (insert "[X]"))
  4770. ((= c-on 0) (insert "[ ]"))
  4771. (t (insert "[-]")))
  4772. )))
  4773. (goto-char continue-from))
  4774. (when (interactive-p)
  4775. (message "Checkbox satistics updated %s (%d places)"
  4776. (if all "in entire file" "in current outline entry") cstat)))))
  4777. (defun org-get-checkbox-statistics-face ()
  4778. "Select the face for checkbox statistics.
  4779. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4780. it will be `org-todo'."
  4781. (if (match-end 1)
  4782. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4783. (if (and (> (match-end 2) (match-beginning 2))
  4784. (equal (match-string 2) (match-string 3)))
  4785. 'org-done
  4786. 'org-todo)))
  4787. (defun org-get-indentation (&optional line)
  4788. "Get the indentation of the current line, interpreting tabs.
  4789. When LINE is given, assume it represents a line and compute its indentation."
  4790. (if line
  4791. (if (string-match "^ *" (org-remove-tabs line))
  4792. (match-end 0))
  4793. (save-excursion
  4794. (beginning-of-line 1)
  4795. (skip-chars-forward " \t")
  4796. (current-column))))
  4797. (defun org-remove-tabs (s &optional width)
  4798. "Replace tabulators in S with spaces.
  4799. Assumes that s is a single line, starting in column 0."
  4800. (setq width (or width tab-width))
  4801. (while (string-match "\t" s)
  4802. (setq s (replace-match
  4803. (make-string
  4804. (- (* width (/ (+ (match-beginning 0) width) width))
  4805. (match-beginning 0)) ?\ )
  4806. t t s)))
  4807. s)
  4808. (defun org-fix-indentation (line ind)
  4809. "Fix indentation in LINE.
  4810. IND is a cons cell with target and minimum indentation.
  4811. If the current indenation in LINE is smaller than the minimum,
  4812. leave it alone. If it is larger than ind, set it to the target."
  4813. (let* ((l (org-remove-tabs line))
  4814. (i (org-get-indentation l))
  4815. (i1 (car ind)) (i2 (cdr ind)))
  4816. (if (>= i i2) (setq l (substring line i2)))
  4817. (if (> i1 0)
  4818. (concat (make-string i1 ?\ ) l)
  4819. l)))
  4820. (defun org-beginning-of-item ()
  4821. "Go to the beginning of the current hand-formatted item.
  4822. If the cursor is not in an item, throw an error."
  4823. (interactive)
  4824. (let ((pos (point))
  4825. (limit (save-excursion
  4826. (condition-case nil
  4827. (progn
  4828. (org-back-to-heading)
  4829. (beginning-of-line 2) (point))
  4830. (error (point-min)))))
  4831. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4832. ind ind1)
  4833. (if (org-at-item-p)
  4834. (beginning-of-line 1)
  4835. (beginning-of-line 1)
  4836. (skip-chars-forward " \t")
  4837. (setq ind (current-column))
  4838. (if (catch 'exit
  4839. (while t
  4840. (beginning-of-line 0)
  4841. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4842. (if (looking-at "[ \t]*$")
  4843. (setq ind1 ind-empty)
  4844. (skip-chars-forward " \t")
  4845. (setq ind1 (current-column)))
  4846. (if (< ind1 ind)
  4847. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4848. nil
  4849. (goto-char pos)
  4850. (error "Not in an item")))))
  4851. (defun org-end-of-item ()
  4852. "Go to the end of the current hand-formatted item.
  4853. If the cursor is not in an item, throw an error."
  4854. (interactive)
  4855. (let* ((pos (point))
  4856. ind1
  4857. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4858. (limit (save-excursion (outline-next-heading) (point)))
  4859. (ind (save-excursion
  4860. (org-beginning-of-item)
  4861. (skip-chars-forward " \t")
  4862. (current-column)))
  4863. (end (catch 'exit
  4864. (while t
  4865. (beginning-of-line 2)
  4866. (if (eobp) (throw 'exit (point)))
  4867. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  4868. (if (looking-at "[ \t]*$")
  4869. (setq ind1 ind-empty)
  4870. (skip-chars-forward " \t")
  4871. (setq ind1 (current-column)))
  4872. (if (<= ind1 ind)
  4873. (throw 'exit (point-at-bol)))))))
  4874. (if end
  4875. (goto-char end)
  4876. (goto-char pos)
  4877. (error "Not in an item"))))
  4878. (defun org-next-item ()
  4879. "Move to the beginning of the next item in the current plain list.
  4880. Error if not at a plain list, or if this is the last item in the list."
  4881. (interactive)
  4882. (let (ind ind1 (pos (point)))
  4883. (org-beginning-of-item)
  4884. (setq ind (org-get-indentation))
  4885. (org-end-of-item)
  4886. (setq ind1 (org-get-indentation))
  4887. (unless (and (org-at-item-p) (= ind ind1))
  4888. (goto-char pos)
  4889. (error "On last item"))))
  4890. (defun org-previous-item ()
  4891. "Move to the beginning of the previous item in the current plain list.
  4892. Error if not at a plain list, or if this is the first item in the list."
  4893. (interactive)
  4894. (let (beg ind ind1 (pos (point)))
  4895. (org-beginning-of-item)
  4896. (setq beg (point))
  4897. (setq ind (org-get-indentation))
  4898. (goto-char beg)
  4899. (catch 'exit
  4900. (while t
  4901. (beginning-of-line 0)
  4902. (if (looking-at "[ \t]*$")
  4903. nil
  4904. (if (<= (setq ind1 (org-get-indentation)) ind)
  4905. (throw 'exit t)))))
  4906. (condition-case nil
  4907. (if (or (not (org-at-item-p))
  4908. (< ind1 (1- ind)))
  4909. (error "")
  4910. (org-beginning-of-item))
  4911. (error (goto-char pos)
  4912. (error "On first item")))))
  4913. (defun org-first-list-item-p ()
  4914. "Is this heading the item in a plain list?"
  4915. (unless (org-at-item-p)
  4916. (error "Not at a plain list item"))
  4917. (org-beginning-of-item)
  4918. (= (point) (save-excursion (org-beginning-of-item-list))))
  4919. (defun org-move-item-down ()
  4920. "Move the plain list item at point down, i.e. swap with following item.
  4921. Subitems (items with larger indentation) are considered part of the item,
  4922. so this really moves item trees."
  4923. (interactive)
  4924. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  4925. (org-beginning-of-item)
  4926. (setq beg0 (point))
  4927. (save-excursion
  4928. (setq ne-beg (org-back-over-empty-lines))
  4929. (setq beg (point)))
  4930. (goto-char beg0)
  4931. (setq ind (org-get-indentation))
  4932. (org-end-of-item)
  4933. (setq end0 (point))
  4934. (setq ind1 (org-get-indentation))
  4935. (setq ne-end (org-back-over-empty-lines))
  4936. (setq end (point))
  4937. (goto-char beg0)
  4938. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  4939. ;; include less whitespace
  4940. (save-excursion
  4941. (goto-char beg)
  4942. (forward-line (- ne-beg ne-end))
  4943. (setq beg (point))))
  4944. (goto-char end0)
  4945. (if (and (org-at-item-p) (= ind ind1))
  4946. (progn
  4947. (org-end-of-item)
  4948. (org-back-over-empty-lines)
  4949. (setq txt (buffer-substring beg end))
  4950. (save-excursion
  4951. (delete-region beg end))
  4952. (setq pos (point))
  4953. (insert txt)
  4954. (goto-char pos) (org-skip-whitespace)
  4955. (org-maybe-renumber-ordered-list))
  4956. (goto-char pos)
  4957. (error "Cannot move this item further down"))))
  4958. (defun org-move-item-up (arg)
  4959. "Move the plain list item at point up, i.e. swap with previous item.
  4960. Subitems (items with larger indentation) are considered part of the item,
  4961. so this really moves item trees."
  4962. (interactive "p")
  4963. (let (beg beg0 end ind ind1 (pos (point)) txt
  4964. ne-beg ne-ins ins-end)
  4965. (org-beginning-of-item)
  4966. (setq beg0 (point))
  4967. (setq ind (org-get-indentation))
  4968. (save-excursion
  4969. (setq ne-beg (org-back-over-empty-lines))
  4970. (setq beg (point)))
  4971. (goto-char beg0)
  4972. (org-end-of-item)
  4973. (setq end (point))
  4974. (goto-char beg0)
  4975. (catch 'exit
  4976. (while t
  4977. (beginning-of-line 0)
  4978. (if (looking-at "[ \t]*$")
  4979. (if org-empty-line-terminates-plain-lists
  4980. (progn
  4981. (goto-char pos)
  4982. (error "Cannot move this item further up"))
  4983. nil)
  4984. (if (<= (setq ind1 (org-get-indentation)) ind)
  4985. (throw 'exit t)))))
  4986. (condition-case nil
  4987. (org-beginning-of-item)
  4988. (error (goto-char beg)
  4989. (error "Cannot move this item further up")))
  4990. (setq ind1 (org-get-indentation))
  4991. (if (and (org-at-item-p) (= ind ind1))
  4992. (progn
  4993. (setq ne-ins (org-back-over-empty-lines))
  4994. (setq txt (buffer-substring beg end))
  4995. (save-excursion
  4996. (delete-region beg end))
  4997. (setq pos (point))
  4998. (insert txt)
  4999. (setq ins-end (point))
  5000. (goto-char pos) (org-skip-whitespace)
  5001. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5002. ;; Move whitespace back to beginning
  5003. (save-excursion
  5004. (goto-char ins-end)
  5005. (let ((kill-whole-line t))
  5006. (kill-line (- ne-ins ne-beg)) (point)))
  5007. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5008. (org-maybe-renumber-ordered-list))
  5009. (goto-char pos)
  5010. (error "Cannot move this item further up"))))
  5011. (defun org-maybe-renumber-ordered-list ()
  5012. "Renumber the ordered list at point if setup allows it.
  5013. This tests the user option `org-auto-renumber-ordered-lists' before
  5014. doing the renumbering."
  5015. (interactive)
  5016. (when (and org-auto-renumber-ordered-lists
  5017. (org-at-item-p))
  5018. (if (match-beginning 3)
  5019. (org-renumber-ordered-list 1)
  5020. (org-fix-bullet-type))))
  5021. (defun org-maybe-renumber-ordered-list-safe ()
  5022. (condition-case nil
  5023. (save-excursion
  5024. (org-maybe-renumber-ordered-list))
  5025. (error nil)))
  5026. (defun org-cycle-list-bullet (&optional which)
  5027. "Cycle through the different itemize/enumerate bullets.
  5028. This cycle the entire list level through the sequence:
  5029. `-' -> `+' -> `*' -> `1.' -> `1)'
  5030. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5031. 0 meand `-', 1 means `+' etc."
  5032. (interactive "P")
  5033. (org-preserve-lc
  5034. (org-beginning-of-item-list)
  5035. (org-at-item-p)
  5036. (beginning-of-line 1)
  5037. (let ((current (match-string 0))
  5038. (prevp (eq which 'previous))
  5039. new)
  5040. (setq new (cond
  5041. ((and (numberp which)
  5042. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5043. ((string-match "-" current) (if prevp "1)" "+"))
  5044. ((string-match "\\+" current)
  5045. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5046. ((string-match "\\*" current) (if prevp "+" "1."))
  5047. ((string-match "\\." current) (if prevp "*" "1)"))
  5048. ((string-match ")" current) (if prevp "1." "-"))
  5049. (t (error "This should not happen"))))
  5050. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5051. (org-fix-bullet-type)
  5052. (org-maybe-renumber-ordered-list))))
  5053. (defun org-get-string-indentation (s)
  5054. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5055. (let ((n -1) (i 0) (w tab-width) c)
  5056. (catch 'exit
  5057. (while (< (setq n (1+ n)) (length s))
  5058. (setq c (aref s n))
  5059. (cond ((= c ?\ ) (setq i (1+ i)))
  5060. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5061. (t (throw 'exit t)))))
  5062. i))
  5063. (defun org-renumber-ordered-list (arg)
  5064. "Renumber an ordered plain list.
  5065. Cursor needs to be in the first line of an item, the line that starts
  5066. with something like \"1.\" or \"2)\"."
  5067. (interactive "p")
  5068. (unless (and (org-at-item-p)
  5069. (match-beginning 3))
  5070. (error "This is not an ordered list"))
  5071. (let ((line (org-current-line))
  5072. (col (current-column))
  5073. (ind (org-get-string-indentation
  5074. (buffer-substring (point-at-bol) (match-beginning 3))))
  5075. ;; (term (substring (match-string 3) -1))
  5076. ind1 (n (1- arg))
  5077. fmt)
  5078. ;; find where this list begins
  5079. (org-beginning-of-item-list)
  5080. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5081. (setq fmt (concat "%d" (match-string 1)))
  5082. (beginning-of-line 0)
  5083. ;; walk forward and replace these numbers
  5084. (catch 'exit
  5085. (while t
  5086. (catch 'next
  5087. (beginning-of-line 2)
  5088. (if (eobp) (throw 'exit nil))
  5089. (if (looking-at "[ \t]*$") (throw 'next nil))
  5090. (skip-chars-forward " \t") (setq ind1 (current-column))
  5091. (if (> ind1 ind) (throw 'next t))
  5092. (if (< ind1 ind) (throw 'exit t))
  5093. (if (not (org-at-item-p)) (throw 'exit nil))
  5094. (delete-region (match-beginning 2) (match-end 2))
  5095. (goto-char (match-beginning 2))
  5096. (insert (format fmt (setq n (1+ n)))))))
  5097. (goto-line line)
  5098. (org-move-to-column col)))
  5099. (defun org-fix-bullet-type ()
  5100. "Make sure all items in this list have the same bullet as the firsst item."
  5101. (interactive)
  5102. (unless (org-at-item-p) (error "This is not a list"))
  5103. (let ((line (org-current-line))
  5104. (col (current-column))
  5105. (ind (current-indentation))
  5106. ind1 bullet)
  5107. ;; find where this list begins
  5108. (org-beginning-of-item-list)
  5109. (beginning-of-line 1)
  5110. ;; find out what the bullet type is
  5111. (looking-at "[ \t]*\\(\\S-+\\)")
  5112. (setq bullet (match-string 1))
  5113. ;; walk forward and replace these numbers
  5114. (beginning-of-line 0)
  5115. (catch 'exit
  5116. (while t
  5117. (catch 'next
  5118. (beginning-of-line 2)
  5119. (if (eobp) (throw 'exit nil))
  5120. (if (looking-at "[ \t]*$") (throw 'next nil))
  5121. (skip-chars-forward " \t") (setq ind1 (current-column))
  5122. (if (> ind1 ind) (throw 'next t))
  5123. (if (< ind1 ind) (throw 'exit t))
  5124. (if (not (org-at-item-p)) (throw 'exit nil))
  5125. (skip-chars-forward " \t")
  5126. (looking-at "\\S-+")
  5127. (replace-match bullet))))
  5128. (goto-line line)
  5129. (org-move-to-column col)
  5130. (if (string-match "[0-9]" bullet)
  5131. (org-renumber-ordered-list 1))))
  5132. (defun org-beginning-of-item-list ()
  5133. "Go to the beginning of the current item list.
  5134. I.e. to the first item in this list."
  5135. (interactive)
  5136. (org-beginning-of-item)
  5137. (let ((pos (point-at-bol))
  5138. (ind (org-get-indentation))
  5139. ind1)
  5140. ;; find where this list begins
  5141. (catch 'exit
  5142. (while t
  5143. (catch 'next
  5144. (beginning-of-line 0)
  5145. (if (looking-at "[ \t]*$")
  5146. (throw (if (bobp) 'exit 'next) t))
  5147. (skip-chars-forward " \t") (setq ind1 (current-column))
  5148. (if (or (< ind1 ind)
  5149. (and (= ind1 ind)
  5150. (not (org-at-item-p)))
  5151. (bobp))
  5152. (throw 'exit t)
  5153. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5154. (goto-char pos)))
  5155. (defun org-end-of-item-list ()
  5156. "Go to the end of the current item list.
  5157. I.e. to the text after the last item."
  5158. (interactive)
  5159. (org-beginning-of-item)
  5160. (let ((pos (point-at-bol))
  5161. (ind (org-get-indentation))
  5162. ind1)
  5163. ;; find where this list begins
  5164. (catch 'exit
  5165. (while t
  5166. (catch 'next
  5167. (beginning-of-line 2)
  5168. (if (looking-at "[ \t]*$")
  5169. (throw (if (eobp) 'exit 'next) t))
  5170. (skip-chars-forward " \t") (setq ind1 (current-column))
  5171. (if (or (< ind1 ind)
  5172. (and (= ind1 ind)
  5173. (not (org-at-item-p)))
  5174. (eobp))
  5175. (progn
  5176. (setq pos (point-at-bol))
  5177. (throw 'exit t))))))
  5178. (goto-char pos)))
  5179. (defvar org-last-indent-begin-marker (make-marker))
  5180. (defvar org-last-indent-end-marker (make-marker))
  5181. (defun org-outdent-item (arg)
  5182. "Outdent a local list item."
  5183. (interactive "p")
  5184. (org-indent-item (- arg)))
  5185. (defun org-indent-item (arg)
  5186. "Indent a local list item."
  5187. (interactive "p")
  5188. (unless (org-at-item-p)
  5189. (error "Not on an item"))
  5190. (save-excursion
  5191. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5192. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5193. (setq beg org-last-indent-begin-marker
  5194. end org-last-indent-end-marker)
  5195. (org-beginning-of-item)
  5196. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5197. (org-end-of-item)
  5198. (setq end (move-marker org-last-indent-end-marker (point))))
  5199. (goto-char beg)
  5200. (setq tmp (org-item-indent-positions)
  5201. ind (car tmp)
  5202. ind-down (nth 2 tmp)
  5203. ind-up (nth 1 tmp)
  5204. delta (if (> arg 0)
  5205. (if ind-down (- ind-down ind) 2)
  5206. (if ind-up (- ind-up ind) -2)))
  5207. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5208. (while (< (point) end)
  5209. (beginning-of-line 1)
  5210. (skip-chars-forward " \t") (setq ind1 (current-column))
  5211. (delete-region (point-at-bol) (point))
  5212. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5213. (beginning-of-line 2))))
  5214. (org-fix-bullet-type)
  5215. (org-maybe-renumber-ordered-list-safe)
  5216. (save-excursion
  5217. (beginning-of-line 0)
  5218. (condition-case nil (org-beginning-of-item) (error nil))
  5219. (org-maybe-renumber-ordered-list-safe)))
  5220. (defun org-item-indent-positions ()
  5221. "Return indentation for plain list items.
  5222. This returns a list with three values: The current indentation, the
  5223. parent indentation and the indentation a child should habe.
  5224. Assumes cursor in item line."
  5225. (let* ((bolpos (point-at-bol))
  5226. (ind (org-get-indentation))
  5227. ind-down ind-up pos)
  5228. (save-excursion
  5229. (org-beginning-of-item-list)
  5230. (skip-chars-backward "\n\r \t")
  5231. (when (org-in-item-p)
  5232. (org-beginning-of-item)
  5233. (setq ind-up (org-get-indentation))))
  5234. (setq pos (point))
  5235. (save-excursion
  5236. (cond
  5237. ((and (condition-case nil (progn (org-previous-item) t)
  5238. (error nil))
  5239. (or (forward-char 1) t)
  5240. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5241. (setq ind-down (org-get-indentation)))
  5242. ((and (goto-char pos)
  5243. (org-at-item-p))
  5244. (goto-char (match-end 0))
  5245. (skip-chars-forward " \t")
  5246. (setq ind-down (current-column)))))
  5247. (list ind ind-up ind-down)))
  5248. ;;; The orgstruct minor mode
  5249. ;; Define a minor mode which can be used in other modes in order to
  5250. ;; integrate the org-mode structure editing commands.
  5251. ;; This is really a hack, because the org-mode structure commands use
  5252. ;; keys which normally belong to the major mode. Here is how it
  5253. ;; works: The minor mode defines all the keys necessary to operate the
  5254. ;; structure commands, but wraps the commands into a function which
  5255. ;; tests if the cursor is currently at a headline or a plain list
  5256. ;; item. If that is the case, the structure command is used,
  5257. ;; temporarily setting many Org-mode variables like regular
  5258. ;; expressions for filling etc. However, when any of those keys is
  5259. ;; used at a different location, function uses `key-binding' to look
  5260. ;; up if the key has an associated command in another currently active
  5261. ;; keymap (minor modes, major mode, global), and executes that
  5262. ;; command. There might be problems if any of the keys is otherwise
  5263. ;; used as a prefix key.
  5264. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5265. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5266. ;; addresses this by checking explicitly for both bindings.
  5267. (defvar orgstruct-mode-map (make-sparse-keymap)
  5268. "Keymap for the minor `orgstruct-mode'.")
  5269. (defvar org-local-vars nil
  5270. "List of local variables, for use by `orgstruct-mode'")
  5271. ;;;###autoload
  5272. (define-minor-mode orgstruct-mode
  5273. "Toggle the minor more `orgstruct-mode'.
  5274. This mode is for using Org-mode structure commands in other modes.
  5275. The following key behave as if Org-mode was active, if the cursor
  5276. is on a headline, or on a plain list item (both in the definition
  5277. of Org-mode).
  5278. M-up Move entry/item up
  5279. M-down Move entry/item down
  5280. M-left Promote
  5281. M-right Demote
  5282. M-S-up Move entry/item up
  5283. M-S-down Move entry/item down
  5284. M-S-left Promote subtree
  5285. M-S-right Demote subtree
  5286. M-q Fill paragraph and items like in Org-mode
  5287. C-c ^ Sort entries
  5288. C-c - Cycle list bullet
  5289. TAB Cycle item visibility
  5290. M-RET Insert new heading/item
  5291. S-M-RET Insert new TODO heading / Chekbox item
  5292. C-c C-c Set tags / toggle checkbox"
  5293. nil " OrgStruct" nil
  5294. (org-load-modules-maybe)
  5295. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5296. ;;;###autoload
  5297. (defun turn-on-orgstruct ()
  5298. "Unconditionally turn on `orgstruct-mode'."
  5299. (orgstruct-mode 1))
  5300. ;;;###autoload
  5301. (defun turn-on-orgstruct++ ()
  5302. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5303. In addition to setting orgstruct-mode, this also exports all indentation and
  5304. autofilling variables from org-mode into the buffer. Note that turning
  5305. off orgstruct-mode will *not* remove these additional settings."
  5306. (orgstruct-mode 1)
  5307. (let (var val)
  5308. (mapc
  5309. (lambda (x)
  5310. (when (string-match
  5311. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5312. (symbol-name (car x)))
  5313. (setq var (car x) val (nth 1 x))
  5314. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5315. org-local-vars)))
  5316. (defun orgstruct-error ()
  5317. "Error when there is no default binding for a structure key."
  5318. (interactive)
  5319. (error "This key has no function outside structure elements"))
  5320. (defun orgstruct-setup ()
  5321. "Setup orgstruct keymaps."
  5322. (let ((nfunc 0)
  5323. (bindings
  5324. (list
  5325. '([(meta up)] org-metaup)
  5326. '([(meta down)] org-metadown)
  5327. '([(meta left)] org-metaleft)
  5328. '([(meta right)] org-metaright)
  5329. '([(meta shift up)] org-shiftmetaup)
  5330. '([(meta shift down)] org-shiftmetadown)
  5331. '([(meta shift left)] org-shiftmetaleft)
  5332. '([(meta shift right)] org-shiftmetaright)
  5333. '([(shift up)] org-shiftup)
  5334. '([(shift down)] org-shiftdown)
  5335. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5336. '("\M-q" fill-paragraph)
  5337. '("\C-c^" org-sort)
  5338. '("\C-c-" org-cycle-list-bullet)))
  5339. elt key fun cmd)
  5340. (while (setq elt (pop bindings))
  5341. (setq nfunc (1+ nfunc))
  5342. (setq key (org-key (car elt))
  5343. fun (nth 1 elt)
  5344. cmd (orgstruct-make-binding fun nfunc key))
  5345. (org-defkey orgstruct-mode-map key cmd))
  5346. ;; Special treatment needed for TAB and RET
  5347. (org-defkey orgstruct-mode-map [(tab)]
  5348. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5349. (org-defkey orgstruct-mode-map "\C-i"
  5350. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5351. (org-defkey orgstruct-mode-map "\M-\C-m"
  5352. (orgstruct-make-binding 'org-insert-heading 105
  5353. "\M-\C-m" [(meta return)]))
  5354. (org-defkey orgstruct-mode-map [(meta return)]
  5355. (orgstruct-make-binding 'org-insert-heading 106
  5356. [(meta return)] "\M-\C-m"))
  5357. (org-defkey orgstruct-mode-map [(shift meta return)]
  5358. (orgstruct-make-binding 'org-insert-todo-heading 107
  5359. [(meta return)] "\M-\C-m"))
  5360. (unless org-local-vars
  5361. (setq org-local-vars (org-get-local-variables)))
  5362. t))
  5363. (defun orgstruct-make-binding (fun n &rest keys)
  5364. "Create a function for binding in the structure minor mode.
  5365. FUN is the command to call inside a table. N is used to create a unique
  5366. command name. KEYS are keys that should be checked in for a command
  5367. to execute outside of tables."
  5368. (eval
  5369. (list 'defun
  5370. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5371. '(arg)
  5372. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5373. "Outside of structure, run the binding of `"
  5374. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5375. "'.")
  5376. '(interactive "p")
  5377. (list 'if
  5378. '(org-context-p 'headline 'item)
  5379. (list 'org-run-like-in-org-mode (list 'quote fun))
  5380. (list 'let '(orgstruct-mode)
  5381. (list 'call-interactively
  5382. (append '(or)
  5383. (mapcar (lambda (k)
  5384. (list 'key-binding k))
  5385. keys)
  5386. '('orgstruct-error))))))))
  5387. (defun org-context-p (&rest contexts)
  5388. "Check if local context is and of CONTEXTS.
  5389. Possible values in the list of contexts are `table', `headline', and `item'."
  5390. (let ((pos (point)))
  5391. (goto-char (point-at-bol))
  5392. (prog1 (or (and (memq 'table contexts)
  5393. (looking-at "[ \t]*|"))
  5394. (and (memq 'headline contexts)
  5395. (looking-at "\\*+"))
  5396. (and (memq 'item contexts)
  5397. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5398. (goto-char pos))))
  5399. (defun org-get-local-variables ()
  5400. "Return a list of all local variables in an org-mode buffer."
  5401. (let (varlist)
  5402. (with-current-buffer (get-buffer-create "*Org tmp*")
  5403. (erase-buffer)
  5404. (org-mode)
  5405. (setq varlist (buffer-local-variables)))
  5406. (kill-buffer "*Org tmp*")
  5407. (delq nil
  5408. (mapcar
  5409. (lambda (x)
  5410. (setq x
  5411. (if (symbolp x)
  5412. (list x)
  5413. (list (car x) (list 'quote (cdr x)))))
  5414. (if (string-match
  5415. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5416. (symbol-name (car x)))
  5417. x nil))
  5418. varlist))))
  5419. ;;;###autoload
  5420. (defun org-run-like-in-org-mode (cmd)
  5421. (org-load-modules-maybe)
  5422. (unless org-local-vars
  5423. (setq org-local-vars (org-get-local-variables)))
  5424. (eval (list 'let org-local-vars
  5425. (list 'call-interactively (list 'quote cmd)))))
  5426. ;;;; Archiving
  5427. (defun org-get-category (&optional pos)
  5428. "Get the category applying to position POS."
  5429. (get-text-property (or pos (point)) 'org-category))
  5430. (defun org-refresh-category-properties ()
  5431. "Refresh category text properties in the buffer."
  5432. (let ((def-cat (cond
  5433. ((null org-category)
  5434. (if buffer-file-name
  5435. (file-name-sans-extension
  5436. (file-name-nondirectory buffer-file-name))
  5437. "???"))
  5438. ((symbolp org-category) (symbol-name org-category))
  5439. (t org-category)))
  5440. beg end cat pos optionp)
  5441. (org-unmodified
  5442. (save-excursion
  5443. (save-restriction
  5444. (widen)
  5445. (goto-char (point-min))
  5446. (put-text-property (point) (point-max) 'org-category def-cat)
  5447. (while (re-search-forward
  5448. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5449. (setq pos (match-end 0)
  5450. optionp (equal (char-after (match-beginning 0)) ?#)
  5451. cat (org-trim (match-string 2)))
  5452. (if optionp
  5453. (setq beg (point-at-bol) end (point-max))
  5454. (org-back-to-heading t)
  5455. (setq beg (point) end (org-end-of-subtree t t)))
  5456. (put-text-property beg end 'org-category cat)
  5457. (goto-char pos)))))))
  5458. ;;;; Link Stuff
  5459. ;;; Link abbreviations
  5460. (defun org-link-expand-abbrev (link)
  5461. "Apply replacements as defined in `org-link-abbrev-alist."
  5462. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5463. (let* ((key (match-string 1 link))
  5464. (as (or (assoc key org-link-abbrev-alist-local)
  5465. (assoc key org-link-abbrev-alist)))
  5466. (tag (and (match-end 2) (match-string 3 link)))
  5467. rpl)
  5468. (if (not as)
  5469. link
  5470. (setq rpl (cdr as))
  5471. (cond
  5472. ((symbolp rpl) (funcall rpl tag))
  5473. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5474. (t (concat rpl tag)))))
  5475. link))
  5476. ;;; Storing and inserting links
  5477. (defvar org-insert-link-history nil
  5478. "Minibuffer history for links inserted with `org-insert-link'.")
  5479. (defvar org-stored-links nil
  5480. "Contains the links stored with `org-store-link'.")
  5481. (defvar org-store-link-plist nil
  5482. "Plist with info about the most recently link created with `org-store-link'.")
  5483. (defvar org-link-protocols nil
  5484. "Link protocols added to Org-mode using `org-add-link-type'.")
  5485. (defvar org-store-link-functions nil
  5486. "List of functions that are called to create and store a link.
  5487. Each function will be called in turn until one returns a non-nil
  5488. value. Each function should check if it is responsible for creating
  5489. this link (for example by looking at the major mode).
  5490. If not, it must exit and return nil.
  5491. If yes, it should return a non-nil value after a calling
  5492. `org-store-link-props' with a list of properties and values.
  5493. Special properties are:
  5494. :type The link prefix. like \"http\". This must be given.
  5495. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5496. This is obligatory as well.
  5497. :description Optional default description for the second pair
  5498. of brackets in an Org-mode link. The user can still change
  5499. this when inserting this link into an Org-mode buffer.
  5500. In addition to these, any additional properties can be specified
  5501. and then used in remember templates.")
  5502. (defun org-add-link-type (type &optional follow export)
  5503. "Add TYPE to the list of `org-link-types'.
  5504. Re-compute all regular expressions depending on `org-link-types'
  5505. FOLLOW and EXPORT are two functions.
  5506. FOLLOW should take the link path as the single argument and do whatever
  5507. is necessary to follow the link, for example find a file or display
  5508. a mail message.
  5509. EXPORT should format the link path for export to one of the export formats.
  5510. It should be a function accepting three arguments:
  5511. path the path of the link, the text after the prefix (like \"http:\")
  5512. desc the description of the link, if any, nil if there was no descripton
  5513. format the export format, a symbol like `html' or `latex'.
  5514. The function may use the FORMAT information to return different values
  5515. depending on the format. The return value will be put literally into
  5516. the exported file.
  5517. Org-mode has a built-in default for exporting links. If you are happy with
  5518. this default, there is no need to define an export function for the link
  5519. type. For a simple example of an export function, see `org-bbdb.el'."
  5520. (add-to-list 'org-link-types type t)
  5521. (org-make-link-regexps)
  5522. (if (assoc type org-link-protocols)
  5523. (setcdr (assoc type org-link-protocols) (list follow export))
  5524. (push (list type follow export) org-link-protocols)))
  5525. ;;;###autoload
  5526. (defun org-store-link (arg)
  5527. "\\<org-mode-map>Store an org-link to the current location.
  5528. This link is added to `org-stored-links' and can later be inserted
  5529. into an org-buffer with \\[org-insert-link].
  5530. For some link types, a prefix arg is interpreted:
  5531. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5532. For file links, arg negates `org-context-in-file-links'."
  5533. (interactive "P")
  5534. (org-load-modules-maybe)
  5535. (setq org-store-link-plist nil) ; reset
  5536. (let (link cpltxt desc description search txt)
  5537. (cond
  5538. ((run-hook-with-args-until-success 'org-store-link-functions)
  5539. (setq link (plist-get org-store-link-plist :link)
  5540. desc (or (plist-get org-store-link-plist :description) link)))
  5541. ((eq major-mode 'calendar-mode)
  5542. (let ((cd (calendar-cursor-to-date)))
  5543. (setq link
  5544. (format-time-string
  5545. (car org-time-stamp-formats)
  5546. (apply 'encode-time
  5547. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5548. nil nil nil))))
  5549. (org-store-link-props :type "calendar" :date cd)))
  5550. ((eq major-mode 'w3-mode)
  5551. (setq cpltxt (url-view-url t)
  5552. link (org-make-link cpltxt))
  5553. (org-store-link-props :type "w3" :url (url-view-url t)))
  5554. ((eq major-mode 'w3m-mode)
  5555. (setq cpltxt (or w3m-current-title w3m-current-url)
  5556. link (org-make-link w3m-current-url))
  5557. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5558. ((setq search (run-hook-with-args-until-success
  5559. 'org-create-file-search-functions))
  5560. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5561. "::" search))
  5562. (setq cpltxt (or description link)))
  5563. ((eq major-mode 'image-mode)
  5564. (setq cpltxt (concat "file:"
  5565. (abbreviate-file-name buffer-file-name))
  5566. link (org-make-link cpltxt))
  5567. (org-store-link-props :type "image" :file buffer-file-name))
  5568. ((eq major-mode 'dired-mode)
  5569. ;; link to the file in the current line
  5570. (setq cpltxt (concat "file:"
  5571. (abbreviate-file-name
  5572. (expand-file-name
  5573. (dired-get-filename nil t))))
  5574. link (org-make-link cpltxt)))
  5575. ((and buffer-file-name (org-mode-p))
  5576. ;; Just link to current headline
  5577. (setq cpltxt (concat "file:"
  5578. (abbreviate-file-name buffer-file-name)))
  5579. ;; Add a context search string
  5580. (when (org-xor org-context-in-file-links arg)
  5581. ;; Check if we are on a target
  5582. (if (org-in-regexp "<<\\(.*?\\)>>")
  5583. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5584. (setq txt (cond
  5585. ((org-on-heading-p) nil)
  5586. ((org-region-active-p)
  5587. (buffer-substring (region-beginning) (region-end)))
  5588. (t nil)))
  5589. (when (or (null txt) (string-match "\\S-" txt))
  5590. (setq cpltxt
  5591. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5592. desc "NONE"))))
  5593. (if (string-match "::\\'" cpltxt)
  5594. (setq cpltxt (substring cpltxt 0 -2)))
  5595. (setq link (org-make-link cpltxt)))
  5596. ((buffer-file-name (buffer-base-buffer))
  5597. ;; Just link to this file here.
  5598. (setq cpltxt (concat "file:"
  5599. (abbreviate-file-name
  5600. (buffer-file-name (buffer-base-buffer)))))
  5601. ;; Add a context string
  5602. (when (org-xor org-context-in-file-links arg)
  5603. (setq txt (if (org-region-active-p)
  5604. (buffer-substring (region-beginning) (region-end))
  5605. (buffer-substring (point-at-bol) (point-at-eol))))
  5606. ;; Only use search option if there is some text.
  5607. (when (string-match "\\S-" txt)
  5608. (setq cpltxt
  5609. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5610. desc "NONE")))
  5611. (setq link (org-make-link cpltxt)))
  5612. ((interactive-p)
  5613. (error "Cannot link to a buffer which is not visiting a file"))
  5614. (t (setq link nil)))
  5615. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5616. (setq link (or link cpltxt)
  5617. desc (or desc cpltxt))
  5618. (if (equal desc "NONE") (setq desc nil))
  5619. (if (and (interactive-p) link)
  5620. (progn
  5621. (setq org-stored-links
  5622. (cons (list link desc) org-stored-links))
  5623. (message "Stored: %s" (or desc link)))
  5624. (and link (org-make-link-string link desc)))))
  5625. (defun org-store-link-props (&rest plist)
  5626. "Store link properties, extract names and addresses."
  5627. (let (x adr)
  5628. (when (setq x (plist-get plist :from))
  5629. (setq adr (mail-extract-address-components x))
  5630. (plist-put plist :fromname (car adr))
  5631. (plist-put plist :fromaddress (nth 1 adr)))
  5632. (when (setq x (plist-get plist :to))
  5633. (setq adr (mail-extract-address-components x))
  5634. (plist-put plist :toname (car adr))
  5635. (plist-put plist :toaddress (nth 1 adr))))
  5636. (let ((from (plist-get plist :from))
  5637. (to (plist-get plist :to)))
  5638. (when (and from to org-from-is-user-regexp)
  5639. (plist-put plist :fromto
  5640. (if (string-match org-from-is-user-regexp from)
  5641. (concat "to %t")
  5642. (concat "from %f")))))
  5643. (setq org-store-link-plist plist))
  5644. (defun org-add-link-props (&rest plist)
  5645. "Add these properties to the link property list."
  5646. (let (key value)
  5647. (while plist
  5648. (setq key (pop plist) value (pop plist))
  5649. (setq org-store-link-plist
  5650. (plist-put org-store-link-plist key value)))))
  5651. (defun org-email-link-description (&optional fmt)
  5652. "Return the description part of an email link.
  5653. This takes information from `org-store-link-plist' and formats it
  5654. according to FMT (default from `org-email-link-description-format')."
  5655. (setq fmt (or fmt org-email-link-description-format))
  5656. (let* ((p org-store-link-plist)
  5657. (to (plist-get p :toaddress))
  5658. (from (plist-get p :fromaddress))
  5659. (table
  5660. (list
  5661. (cons "%c" (plist-get p :fromto))
  5662. (cons "%F" (plist-get p :from))
  5663. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5664. (cons "%T" (plist-get p :to))
  5665. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5666. (cons "%s" (plist-get p :subject))
  5667. (cons "%m" (plist-get p :message-id)))))
  5668. (when (string-match "%c" fmt)
  5669. ;; Check if the user wrote this message
  5670. (if (and org-from-is-user-regexp from to
  5671. (save-match-data (string-match org-from-is-user-regexp from)))
  5672. (setq fmt (replace-match "to %t" t t fmt))
  5673. (setq fmt (replace-match "from %f" t t fmt))))
  5674. (org-replace-escapes fmt table)))
  5675. (defun org-make-org-heading-search-string (&optional string heading)
  5676. "Make search string for STRING or current headline."
  5677. (interactive)
  5678. (let ((s (or string (org-get-heading))))
  5679. (unless (and string (not heading))
  5680. ;; We are using a headline, clean up garbage in there.
  5681. (if (string-match org-todo-regexp s)
  5682. (setq s (replace-match "" t t s)))
  5683. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5684. (setq s (replace-match "" t t s)))
  5685. (setq s (org-trim s))
  5686. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5687. org-comment-string "\\)") s)
  5688. (setq s (replace-match "" t t s)))
  5689. (while (string-match org-ts-regexp s)
  5690. (setq s (replace-match "" t t s))))
  5691. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5692. (setq s (replace-match " " t t s)))
  5693. (or string (setq s (concat "*" s))) ; Add * for headlines
  5694. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5695. (defun org-make-link (&rest strings)
  5696. "Concatenate STRINGS."
  5697. (apply 'concat strings))
  5698. (defun org-make-link-string (link &optional description)
  5699. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5700. (unless (string-match "\\S-" link)
  5701. (error "Empty link"))
  5702. (when (stringp description)
  5703. ;; Remove brackets from the description, they are fatal.
  5704. (while (string-match "\\[" description)
  5705. (setq description (replace-match "{" t t description)))
  5706. (while (string-match "\\]" description)
  5707. (setq description (replace-match "}" t t description))))
  5708. (when (equal (org-link-escape link) description)
  5709. ;; No description needed, it is identical
  5710. (setq description nil))
  5711. (when (and (not description)
  5712. (not (equal link (org-link-escape link))))
  5713. (setq description link))
  5714. (concat "[[" (org-link-escape link) "]"
  5715. (if description (concat "[" description "]") "")
  5716. "]"))
  5717. (defconst org-link-escape-chars
  5718. '((?\ . "%20")
  5719. (?\[ . "%5B")
  5720. (?\] . "%5D")
  5721. (?\340 . "%E0") ; `a
  5722. (?\342 . "%E2") ; ^a
  5723. (?\347 . "%E7") ; ,c
  5724. (?\350 . "%E8") ; `e
  5725. (?\351 . "%E9") ; 'e
  5726. (?\352 . "%EA") ; ^e
  5727. (?\356 . "%EE") ; ^i
  5728. (?\364 . "%F4") ; ^o
  5729. (?\371 . "%F9") ; `u
  5730. (?\373 . "%FB") ; ^u
  5731. (?\; . "%3B")
  5732. (?? . "%3F")
  5733. (?= . "%3D")
  5734. (?+ . "%2B")
  5735. )
  5736. "Association list of escapes for some characters problematic in links.
  5737. This is the list that is used for internal purposes.")
  5738. (defconst org-link-escape-chars-browser
  5739. '((?\ . "%20")) ; 32 for the SPC char
  5740. "Association list of escapes for some characters problematic in links.
  5741. This is the list that is used before handing over to the browser.")
  5742. (defun org-link-escape (text &optional table)
  5743. "Escape charaters in TEXT that are problematic for links."
  5744. (setq table (or table org-link-escape-chars))
  5745. (when text
  5746. (let ((re (mapconcat (lambda (x) (regexp-quote
  5747. (char-to-string (car x))))
  5748. table "\\|")))
  5749. (while (string-match re text)
  5750. (setq text
  5751. (replace-match
  5752. (cdr (assoc (string-to-char (match-string 0 text))
  5753. table))
  5754. t t text)))
  5755. text)))
  5756. (defun org-link-unescape (text &optional table)
  5757. "Reverse the action of `org-link-escape'."
  5758. (setq table (or table org-link-escape-chars))
  5759. (when text
  5760. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5761. table "\\|")))
  5762. (while (string-match re text)
  5763. (setq text
  5764. (replace-match
  5765. (char-to-string (car (rassoc (match-string 0 text) table)))
  5766. t t text)))
  5767. text)))
  5768. (defun org-xor (a b)
  5769. "Exclusive or."
  5770. (if a (not b) b))
  5771. (defun org-get-header (header)
  5772. "Find a header field in the current buffer."
  5773. (save-excursion
  5774. (goto-char (point-min))
  5775. (let ((case-fold-search t) s)
  5776. (cond
  5777. ((eq header 'from)
  5778. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5779. (setq s (match-string 1)))
  5780. (while (string-match "\"" s)
  5781. (setq s (replace-match "" t t s)))
  5782. (if (string-match "[<(].*" s)
  5783. (setq s (replace-match "" t t s))))
  5784. ((eq header 'message-id)
  5785. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5786. (setq s (match-string 1))))
  5787. ((eq header 'subject)
  5788. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5789. (setq s (match-string 1)))))
  5790. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5791. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5792. s)))
  5793. (defun org-fixup-message-id-for-http (s)
  5794. "Replace special characters in a message id, so it can be used in an http query."
  5795. (while (string-match "<" s)
  5796. (setq s (replace-match "%3C" t t s)))
  5797. (while (string-match ">" s)
  5798. (setq s (replace-match "%3E" t t s)))
  5799. (while (string-match "@" s)
  5800. (setq s (replace-match "%40" t t s)))
  5801. s)
  5802. ;;;###autoload
  5803. (defun org-insert-link-global ()
  5804. "Insert a link like Org-mode does.
  5805. This command can be called in any mode to insert a link in Org-mode syntax."
  5806. (interactive)
  5807. (org-load-modules-maybe)
  5808. (org-run-like-in-org-mode 'org-insert-link))
  5809. (defun org-insert-link (&optional complete-file link-location)
  5810. "Insert a link. At the prompt, enter the link.
  5811. Completion can be used to select a link previously stored with
  5812. `org-store-link'. When the empty string is entered (i.e. if you just
  5813. press RET at the prompt), the link defaults to the most recently
  5814. stored link. As SPC triggers completion in the minibuffer, you need to
  5815. use M-SPC or C-q SPC to force the insertion of a space character.
  5816. You will also be prompted for a description, and if one is given, it will
  5817. be displayed in the buffer instead of the link.
  5818. If there is already a link at point, this command will allow you to edit link
  5819. and description parts.
  5820. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5821. be selected using completion. The path to the file will be relative to the
  5822. current directory if the file is in the current directory or a subdirectory.
  5823. Otherwise, the link will be the absolute path as completed in the minibuffer
  5824. \(i.e. normally ~/path/to/file).
  5825. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5826. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5827. of `org-keep-stored-link-after-insertion'.
  5828. If `org-make-link-description-function' is non-nil, this function will be
  5829. called with the link target, and the result will be the default
  5830. link description.
  5831. If the LINK-LOCATION parameter is non-nil, this value will be
  5832. used as the link location instead of reading one interactively."
  5833. (interactive "P")
  5834. (let* ((wcf (current-window-configuration))
  5835. (region (if (org-region-active-p)
  5836. (buffer-substring (region-beginning) (region-end))))
  5837. (remove (and region (list (region-beginning) (region-end))))
  5838. (desc region)
  5839. tmphist ; byte-compile incorrectly complains about this
  5840. (link link-location)
  5841. entry file)
  5842. (cond
  5843. (link-location) ; specified by arg, just use it.
  5844. ((org-in-regexp org-bracket-link-regexp 1)
  5845. ;; We do have a link at point, and we are going to edit it.
  5846. (setq remove (list (match-beginning 0) (match-end 0)))
  5847. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5848. (setq link (read-string "Link: "
  5849. (org-link-unescape
  5850. (org-match-string-no-properties 1)))))
  5851. ((or (org-in-regexp org-angle-link-re)
  5852. (org-in-regexp org-plain-link-re))
  5853. ;; Convert to bracket link
  5854. (setq remove (list (match-beginning 0) (match-end 0))
  5855. link (read-string "Link: "
  5856. (org-remove-angle-brackets (match-string 0)))))
  5857. ((equal complete-file '(4))
  5858. ;; Completing read for file names.
  5859. (setq file (read-file-name "File: "))
  5860. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5861. (pwd1 (file-name-as-directory (abbreviate-file-name
  5862. (expand-file-name ".")))))
  5863. (cond
  5864. ((equal complete-file '(16))
  5865. (setq link (org-make-link
  5866. "file:"
  5867. (abbreviate-file-name (expand-file-name file)))))
  5868. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5869. (setq link (org-make-link "file:" (match-string 1 file))))
  5870. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5871. (expand-file-name file))
  5872. (setq link (org-make-link
  5873. "file:" (match-string 1 (expand-file-name file)))))
  5874. (t (setq link (org-make-link "file:" file))))))
  5875. (t
  5876. ;; Read link, with completion for stored links.
  5877. (with-output-to-temp-buffer "*Org Links*"
  5878. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5879. (when org-stored-links
  5880. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5881. (princ (mapconcat
  5882. (lambda (x)
  5883. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5884. (reverse org-stored-links) "\n"))))
  5885. (let ((cw (selected-window)))
  5886. (select-window (get-buffer-window "*Org Links*"))
  5887. (shrink-window-if-larger-than-buffer)
  5888. (setq truncate-lines t)
  5889. (select-window cw))
  5890. ;; Fake a link history, containing the stored links.
  5891. (setq tmphist (append (mapcar 'car org-stored-links)
  5892. org-insert-link-history))
  5893. (unwind-protect
  5894. (setq link (org-completing-read
  5895. "Link: "
  5896. (append
  5897. (mapcar (lambda (x) (list (concat (car x) ":")))
  5898. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5899. (mapcar (lambda (x) (list (concat x ":")))
  5900. org-link-types))
  5901. nil nil nil
  5902. 'tmphist
  5903. (or (car (car org-stored-links)))))
  5904. (set-window-configuration wcf)
  5905. (kill-buffer "*Org Links*"))
  5906. (setq entry (assoc link org-stored-links))
  5907. (or entry (push link org-insert-link-history))
  5908. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5909. (not org-keep-stored-link-after-insertion))
  5910. (setq org-stored-links (delq (assoc link org-stored-links)
  5911. org-stored-links)))
  5912. (setq desc (or desc (nth 1 entry)))))
  5913. (if (string-match org-plain-link-re link)
  5914. ;; URL-like link, normalize the use of angular brackets.
  5915. (setq link (org-make-link (org-remove-angle-brackets link))))
  5916. ;; Check if we are linking to the current file with a search option
  5917. ;; If yes, simplify the link by using only the search option.
  5918. (when (and buffer-file-name
  5919. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5920. (let* ((path (match-string 1 link))
  5921. (case-fold-search nil)
  5922. (search (match-string 2 link)))
  5923. (save-match-data
  5924. (if (equal (file-truename buffer-file-name) (file-truename path))
  5925. ;; We are linking to this same file, with a search option
  5926. (setq link search)))))
  5927. ;; Check if we can/should use a relative path. If yes, simplify the link
  5928. (when (string-match "\\<file:\\(.*\\)" link)
  5929. (let* ((path (match-string 1 link))
  5930. (origpath path)
  5931. (case-fold-search nil))
  5932. (cond
  5933. ((eq org-link-file-path-type 'absolute)
  5934. (setq path (abbreviate-file-name (expand-file-name path))))
  5935. ((eq org-link-file-path-type 'noabbrev)
  5936. (setq path (expand-file-name path)))
  5937. ((eq org-link-file-path-type 'relative)
  5938. (setq path (file-relative-name path)))
  5939. (t
  5940. (save-match-data
  5941. (if (string-match (concat "^" (regexp-quote
  5942. (file-name-as-directory
  5943. (expand-file-name "."))))
  5944. (expand-file-name path))
  5945. ;; We are linking a file with relative path name.
  5946. (setq path (substring (expand-file-name path)
  5947. (match-end 0)))))))
  5948. (setq link (concat "file:" path))
  5949. (if (equal desc origpath)
  5950. (setq desc path))))
  5951. (if org-make-link-description-function
  5952. (setq desc (funcall org-make-link-description-function link desc)))
  5953. (setq desc (read-string "Description: " desc))
  5954. (unless (string-match "\\S-" desc) (setq desc nil))
  5955. (if remove (apply 'delete-region remove))
  5956. (insert (org-make-link-string link desc))))
  5957. (defun org-completing-read (&rest args)
  5958. (let ((minibuffer-local-completion-map
  5959. (copy-keymap minibuffer-local-completion-map)))
  5960. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5961. (apply 'completing-read args)))
  5962. ;;; Opening/following a link
  5963. (defvar org-link-search-failed nil)
  5964. (defun org-next-link ()
  5965. "Move forward to the next link.
  5966. If the link is in hidden text, expose it."
  5967. (interactive)
  5968. (when (and org-link-search-failed (eq this-command last-command))
  5969. (goto-char (point-min))
  5970. (message "Link search wrapped back to beginning of buffer"))
  5971. (setq org-link-search-failed nil)
  5972. (let* ((pos (point))
  5973. (ct (org-context))
  5974. (a (assoc :link ct)))
  5975. (if a (goto-char (nth 2 a)))
  5976. (if (re-search-forward org-any-link-re nil t)
  5977. (progn
  5978. (goto-char (match-beginning 0))
  5979. (if (org-invisible-p) (org-show-context)))
  5980. (goto-char pos)
  5981. (setq org-link-search-failed t)
  5982. (error "No further link found"))))
  5983. (defun org-previous-link ()
  5984. "Move backward to the previous link.
  5985. If the link is in hidden text, expose it."
  5986. (interactive)
  5987. (when (and org-link-search-failed (eq this-command last-command))
  5988. (goto-char (point-max))
  5989. (message "Link search wrapped back to end of buffer"))
  5990. (setq org-link-search-failed nil)
  5991. (let* ((pos (point))
  5992. (ct (org-context))
  5993. (a (assoc :link ct)))
  5994. (if a (goto-char (nth 1 a)))
  5995. (if (re-search-backward org-any-link-re nil t)
  5996. (progn
  5997. (goto-char (match-beginning 0))
  5998. (if (org-invisible-p) (org-show-context)))
  5999. (goto-char pos)
  6000. (setq org-link-search-failed t)
  6001. (error "No further link found"))))
  6002. (defun org-find-file-at-mouse (ev)
  6003. "Open file link or URL at mouse."
  6004. (interactive "e")
  6005. (mouse-set-point ev)
  6006. (org-open-at-point 'in-emacs))
  6007. (defun org-open-at-mouse (ev)
  6008. "Open file link or URL at mouse."
  6009. (interactive "e")
  6010. (mouse-set-point ev)
  6011. (org-open-at-point))
  6012. (defvar org-window-config-before-follow-link nil
  6013. "The window configuration before following a link.
  6014. This is saved in case the need arises to restore it.")
  6015. (defvar org-open-link-marker (make-marker)
  6016. "Marker pointing to the location where `org-open-at-point; was called.")
  6017. ;;;###autoload
  6018. (defun org-open-at-point-global ()
  6019. "Follow a link like Org-mode does.
  6020. This command can be called in any mode to follow a link that has
  6021. Org-mode syntax."
  6022. (interactive)
  6023. (org-run-like-in-org-mode 'org-open-at-point))
  6024. ;;;###autoload
  6025. (defun org-open-link-from-string (s &optional arg)
  6026. "Open a link in the string S, as if it was in Org-mode."
  6027. (interactive "sLink: \nP")
  6028. (with-temp-buffer
  6029. (let ((org-inhibit-startup t))
  6030. (org-mode)
  6031. (insert s)
  6032. (goto-char (point-min))
  6033. (org-open-at-point arg))))
  6034. (defun org-open-at-point (&optional in-emacs)
  6035. "Open link at or after point.
  6036. If there is no link at point, this function will search forward up to
  6037. the end of the current subtree.
  6038. Normally, files will be opened by an appropriate application. If the
  6039. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6040. (interactive "P")
  6041. (org-load-modules-maybe)
  6042. (move-marker org-open-link-marker (point))
  6043. (setq org-window-config-before-follow-link (current-window-configuration))
  6044. (org-remove-occur-highlights nil nil t)
  6045. (if (org-at-timestamp-p t)
  6046. (org-follow-timestamp-link)
  6047. (let (type path link line search (pos (point)))
  6048. (catch 'match
  6049. (save-excursion
  6050. (skip-chars-forward "^]\n\r")
  6051. (when (org-in-regexp org-bracket-link-regexp)
  6052. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6053. (while (string-match " *\n *" link)
  6054. (setq link (replace-match " " t t link)))
  6055. (setq link (org-link-expand-abbrev link))
  6056. (if (string-match org-link-re-with-space2 link)
  6057. (setq type (match-string 1 link) path (match-string 2 link))
  6058. (setq type "thisfile" path link))
  6059. (throw 'match t)))
  6060. (when (get-text-property (point) 'org-linked-text)
  6061. (setq type "thisfile"
  6062. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6063. (1+ (point)) (point))
  6064. path (buffer-substring
  6065. (previous-single-property-change pos 'org-linked-text)
  6066. (next-single-property-change pos 'org-linked-text)))
  6067. (throw 'match t))
  6068. (save-excursion
  6069. (when (or (org-in-regexp org-angle-link-re)
  6070. (org-in-regexp org-plain-link-re))
  6071. (setq type (match-string 1) path (match-string 2))
  6072. (throw 'match t)))
  6073. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6074. (setq type "tree-match"
  6075. path (match-string 1))
  6076. (throw 'match t))
  6077. (save-excursion
  6078. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6079. (setq type "tags"
  6080. path (match-string 1))
  6081. (while (string-match ":" path)
  6082. (setq path (replace-match "+" t t path)))
  6083. (throw 'match t))))
  6084. (unless path
  6085. (error "No link found"))
  6086. ;; Remove any trailing spaces in path
  6087. (if (string-match " +\\'" path)
  6088. (setq path (replace-match "" t t path)))
  6089. (cond
  6090. ((assoc type org-link-protocols)
  6091. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6092. ((equal type "mailto")
  6093. (let ((cmd (car org-link-mailto-program))
  6094. (args (cdr org-link-mailto-program)) args1
  6095. (address path) (subject "") a)
  6096. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6097. (setq address (match-string 1 path)
  6098. subject (org-link-escape (match-string 2 path))))
  6099. (while args
  6100. (cond
  6101. ((not (stringp (car args))) (push (pop args) args1))
  6102. (t (setq a (pop args))
  6103. (if (string-match "%a" a)
  6104. (setq a (replace-match address t t a)))
  6105. (if (string-match "%s" a)
  6106. (setq a (replace-match subject t t a)))
  6107. (push a args1))))
  6108. (apply cmd (nreverse args1))))
  6109. ((member type '("http" "https" "ftp" "news"))
  6110. (browse-url (concat type ":" (org-link-escape
  6111. path org-link-escape-chars-browser))))
  6112. ((member type '("message"))
  6113. (browse-url (concat type ":" path)))
  6114. ((string= type "tags")
  6115. (org-tags-view in-emacs path))
  6116. ((string= type "thisfile")
  6117. (if in-emacs
  6118. (switch-to-buffer-other-window
  6119. (org-get-buffer-for-internal-link (current-buffer)))
  6120. (org-mark-ring-push))
  6121. (let ((cmd `(org-link-search
  6122. ,path
  6123. ,(cond ((equal in-emacs '(4)) 'occur)
  6124. ((equal in-emacs '(16)) 'org-occur)
  6125. (t nil))
  6126. ,pos)))
  6127. (condition-case nil (eval cmd)
  6128. (error (progn (widen) (eval cmd))))))
  6129. ((string= type "tree-match")
  6130. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6131. ((string= type "file")
  6132. (if (string-match "::\\([0-9]+\\)\\'" path)
  6133. (setq line (string-to-number (match-string 1 path))
  6134. path (substring path 0 (match-beginning 0)))
  6135. (if (string-match "::\\(.+\\)\\'" path)
  6136. (setq search (match-string 1 path)
  6137. path (substring path 0 (match-beginning 0)))))
  6138. (if (string-match "[*?{]" (file-name-nondirectory path))
  6139. (dired path)
  6140. (org-open-file path in-emacs line search)))
  6141. ((string= type "news")
  6142. (require 'org-gnus)
  6143. (org-gnus-follow-link path))
  6144. ((string= type "shell")
  6145. (let ((cmd path))
  6146. (if (or (not org-confirm-shell-link-function)
  6147. (funcall org-confirm-shell-link-function
  6148. (format "Execute \"%s\" in shell? "
  6149. (org-add-props cmd nil
  6150. 'face 'org-warning))))
  6151. (progn
  6152. (message "Executing %s" cmd)
  6153. (shell-command cmd))
  6154. (error "Abort"))))
  6155. ((string= type "elisp")
  6156. (let ((cmd path))
  6157. (if (or (not org-confirm-elisp-link-function)
  6158. (funcall org-confirm-elisp-link-function
  6159. (format "Execute \"%s\" as elisp? "
  6160. (org-add-props cmd nil
  6161. 'face 'org-warning))))
  6162. (message "%s => %s" cmd (eval (read cmd)))
  6163. (error "Abort"))))
  6164. (t
  6165. (browse-url-at-point)))))
  6166. (move-marker org-open-link-marker nil)
  6167. (run-hook-with-args 'org-follow-link-hook))
  6168. ;;;; Time estimates
  6169. (defun org-get-effort (&optional pom)
  6170. "Get the effort estimate for the current entry."
  6171. (org-entry-get pom org-effort-property))
  6172. ;;; File search
  6173. (defvar org-create-file-search-functions nil
  6174. "List of functions to construct the right search string for a file link.
  6175. These functions are called in turn with point at the location to
  6176. which the link should point.
  6177. A function in the hook should first test if it would like to
  6178. handle this file type, for example by checking the major-mode or
  6179. the file extension. If it decides not to handle this file, it
  6180. should just return nil to give other functions a chance. If it
  6181. does handle the file, it must return the search string to be used
  6182. when following the link. The search string will be part of the
  6183. file link, given after a double colon, and `org-open-at-point'
  6184. will automatically search for it. If special measures must be
  6185. taken to make the search successful, another function should be
  6186. added to the companion hook `org-execute-file-search-functions',
  6187. which see.
  6188. A function in this hook may also use `setq' to set the variable
  6189. `description' to provide a suggestion for the descriptive text to
  6190. be used for this link when it gets inserted into an Org-mode
  6191. buffer with \\[org-insert-link].")
  6192. (defvar org-execute-file-search-functions nil
  6193. "List of functions to execute a file search triggered by a link.
  6194. Functions added to this hook must accept a single argument, the
  6195. search string that was part of the file link, the part after the
  6196. double colon. The function must first check if it would like to
  6197. handle this search, for example by checking the major-mode or the
  6198. file extension. If it decides not to handle this search, it
  6199. should just return nil to give other functions a chance. If it
  6200. does handle the search, it must return a non-nil value to keep
  6201. other functions from trying.
  6202. Each function can access the current prefix argument through the
  6203. variable `current-prefix-argument'. Note that a single prefix is
  6204. used to force opening a link in Emacs, so it may be good to only
  6205. use a numeric or double prefix to guide the search function.
  6206. In case this is needed, a function in this hook can also restore
  6207. the window configuration before `org-open-at-point' was called using:
  6208. (set-window-configuration org-window-config-before-follow-link)")
  6209. (defun org-link-search (s &optional type avoid-pos)
  6210. "Search for a link search option.
  6211. If S is surrounded by forward slashes, it is interpreted as a
  6212. regular expression. In org-mode files, this will create an `org-occur'
  6213. sparse tree. In ordinary files, `occur' will be used to list matches.
  6214. If the current buffer is in `dired-mode', grep will be used to search
  6215. in all files. If AVOID-POS is given, ignore matches near that position."
  6216. (let ((case-fold-search t)
  6217. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6218. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6219. (append '(("") (" ") ("\t") ("\n"))
  6220. org-emphasis-alist)
  6221. "\\|") "\\)"))
  6222. (pos (point))
  6223. (pre nil) (post nil)
  6224. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6225. (cond
  6226. ;; First check if there are any special
  6227. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6228. ;; Now try the builtin stuff
  6229. ((save-excursion
  6230. (goto-char (point-min))
  6231. (and
  6232. (re-search-forward
  6233. (concat "<<" (regexp-quote s0) ">>") nil t)
  6234. (setq type 'dedicated
  6235. pos (match-beginning 0))))
  6236. ;; There is an exact target for this
  6237. (goto-char pos))
  6238. ((string-match "^/\\(.*\\)/$" s)
  6239. ;; A regular expression
  6240. (cond
  6241. ((org-mode-p)
  6242. (org-occur (match-string 1 s)))
  6243. ;;((eq major-mode 'dired-mode)
  6244. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6245. (t (org-do-occur (match-string 1 s)))))
  6246. (t
  6247. ;; A normal search strings
  6248. (when (equal (string-to-char s) ?*)
  6249. ;; Anchor on headlines, post may include tags.
  6250. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6251. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6252. s (substring s 1)))
  6253. (remove-text-properties
  6254. 0 (length s)
  6255. '(face nil mouse-face nil keymap nil fontified nil) s)
  6256. ;; Make a series of regular expressions to find a match
  6257. (setq words (org-split-string s "[ \n\r\t]+")
  6258. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6259. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6260. "\\)" markers)
  6261. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6262. re2a (concat "[ \t\r\n]" re2a_)
  6263. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6264. re4 (concat "[^a-zA-Z_]" re4_)
  6265. re1 (concat pre re2 post)
  6266. re3 (concat pre (if pre re4_ re4) post)
  6267. re5 (concat pre ".*" re4)
  6268. re2 (concat pre re2)
  6269. re2a (concat pre (if pre re2a_ re2a))
  6270. re4 (concat pre (if pre re4_ re4))
  6271. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6272. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6273. re5 "\\)"
  6274. ))
  6275. (cond
  6276. ((eq type 'org-occur) (org-occur reall))
  6277. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6278. (t (goto-char (point-min))
  6279. (setq type 'fuzzy)
  6280. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6281. (org-search-not-self 1 re1 nil t)
  6282. (org-search-not-self 1 re2 nil t)
  6283. (org-search-not-self 1 re2a nil t)
  6284. (org-search-not-self 1 re3 nil t)
  6285. (org-search-not-self 1 re4 nil t)
  6286. (org-search-not-self 1 re5 nil t)
  6287. )
  6288. (goto-char (match-beginning 1))
  6289. (goto-char pos)
  6290. (error "No match")))))
  6291. (t
  6292. ;; Normal string-search
  6293. (goto-char (point-min))
  6294. (if (search-forward s nil t)
  6295. (goto-char (match-beginning 0))
  6296. (error "No match"))))
  6297. (and (org-mode-p) (org-show-context 'link-search))
  6298. type))
  6299. (defun org-search-not-self (group &rest args)
  6300. "Execute `re-search-forward', but only accept matches that do not
  6301. enclose the position of `org-open-link-marker'."
  6302. (let ((m org-open-link-marker))
  6303. (catch 'exit
  6304. (while (apply 're-search-forward args)
  6305. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6306. (goto-char (match-end group))
  6307. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6308. (> (match-beginning 0) (marker-position m))
  6309. (< (match-end 0) (marker-position m)))
  6310. (save-match-data
  6311. (or (not (org-in-regexp
  6312. org-bracket-link-analytic-regexp 1))
  6313. (not (match-end 4)) ; no description
  6314. (and (<= (match-beginning 4) (point))
  6315. (>= (match-end 4) (point))))))
  6316. (throw 'exit (point))))))))
  6317. (defun org-get-buffer-for-internal-link (buffer)
  6318. "Return a buffer to be used for displaying the link target of internal links."
  6319. (cond
  6320. ((not org-display-internal-link-with-indirect-buffer)
  6321. buffer)
  6322. ((string-match "(Clone)$" (buffer-name buffer))
  6323. (message "Buffer is already a clone, not making another one")
  6324. ;; we also do not modify visibility in this case
  6325. buffer)
  6326. (t ; make a new indirect buffer for displaying the link
  6327. (let* ((bn (buffer-name buffer))
  6328. (ibn (concat bn "(Clone)"))
  6329. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6330. (with-current-buffer ib (org-overview))
  6331. ib))))
  6332. (defun org-do-occur (regexp &optional cleanup)
  6333. "Call the Emacs command `occur'.
  6334. If CLEANUP is non-nil, remove the printout of the regular expression
  6335. in the *Occur* buffer. This is useful if the regex is long and not useful
  6336. to read."
  6337. (occur regexp)
  6338. (when cleanup
  6339. (let ((cwin (selected-window)) win beg end)
  6340. (when (setq win (get-buffer-window "*Occur*"))
  6341. (select-window win))
  6342. (goto-char (point-min))
  6343. (when (re-search-forward "match[a-z]+" nil t)
  6344. (setq beg (match-end 0))
  6345. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6346. (setq end (1- (match-beginning 0)))))
  6347. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6348. (goto-char (point-min))
  6349. (select-window cwin))))
  6350. ;;; The mark ring for links jumps
  6351. (defvar org-mark-ring nil
  6352. "Mark ring for positions before jumps in Org-mode.")
  6353. (defvar org-mark-ring-last-goto nil
  6354. "Last position in the mark ring used to go back.")
  6355. ;; Fill and close the ring
  6356. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6357. (loop for i from 1 to org-mark-ring-length do
  6358. (push (make-marker) org-mark-ring))
  6359. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6360. org-mark-ring)
  6361. (defun org-mark-ring-push (&optional pos buffer)
  6362. "Put the current position or POS into the mark ring and rotate it."
  6363. (interactive)
  6364. (setq pos (or pos (point)))
  6365. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6366. (move-marker (car org-mark-ring)
  6367. (or pos (point))
  6368. (or buffer (current-buffer)))
  6369. (message "%s"
  6370. (substitute-command-keys
  6371. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6372. (defun org-mark-ring-goto (&optional n)
  6373. "Jump to the previous position in the mark ring.
  6374. With prefix arg N, jump back that many stored positions. When
  6375. called several times in succession, walk through the entire ring.
  6376. Org-mode commands jumping to a different position in the current file,
  6377. or to another Org-mode file, automatically push the old position
  6378. onto the ring."
  6379. (interactive "p")
  6380. (let (p m)
  6381. (if (eq last-command this-command)
  6382. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6383. (setq p org-mark-ring))
  6384. (setq org-mark-ring-last-goto p)
  6385. (setq m (car p))
  6386. (switch-to-buffer (marker-buffer m))
  6387. (goto-char m)
  6388. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6389. (defun org-remove-angle-brackets (s)
  6390. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6391. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6392. s)
  6393. (defun org-add-angle-brackets (s)
  6394. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6395. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6396. s)
  6397. ;;; Following specific links
  6398. (defun org-follow-timestamp-link ()
  6399. (cond
  6400. ((org-at-date-range-p t)
  6401. (let ((org-agenda-start-on-weekday)
  6402. (t1 (match-string 1))
  6403. (t2 (match-string 2)))
  6404. (setq t1 (time-to-days (org-time-string-to-time t1))
  6405. t2 (time-to-days (org-time-string-to-time t2)))
  6406. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6407. ((org-at-timestamp-p t)
  6408. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6409. (substring (match-string 1) 0 10)))
  6410. 1))
  6411. (t (error "This should not happen"))))
  6412. ;;; Following file links
  6413. (defvar org-wait nil)
  6414. (defun org-open-file (path &optional in-emacs line search)
  6415. "Open the file at PATH.
  6416. First, this expands any special file name abbreviations. Then the
  6417. configuration variable `org-file-apps' is checked if it contains an
  6418. entry for this file type, and if yes, the corresponding command is launched.
  6419. If no application is found, Emacs simply visits the file.
  6420. With optional argument IN-EMACS, Emacs will visit the file.
  6421. Optional LINE specifies a line to go to, optional SEARCH a string to
  6422. search for. If LINE or SEARCH is given, the file will always be
  6423. opened in Emacs.
  6424. If the file does not exist, an error is thrown."
  6425. (setq in-emacs (or in-emacs line search))
  6426. (let* ((file (if (equal path "")
  6427. buffer-file-name
  6428. (substitute-in-file-name (expand-file-name path))))
  6429. (apps (append org-file-apps (org-default-apps)))
  6430. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6431. (dirp (if remp nil (file-directory-p file)))
  6432. (dfile (downcase file))
  6433. (old-buffer (current-buffer))
  6434. (old-pos (point))
  6435. (old-mode major-mode)
  6436. ext cmd)
  6437. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6438. (setq ext (match-string 1 dfile))
  6439. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6440. (setq ext (match-string 1 dfile))))
  6441. (if in-emacs
  6442. (setq cmd 'emacs)
  6443. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6444. (and dirp (cdr (assoc 'directory apps)))
  6445. (cdr (assoc ext apps))
  6446. (cdr (assoc t apps)))))
  6447. (when (eq cmd 'mailcap)
  6448. (require 'mailcap)
  6449. (mailcap-parse-mailcaps)
  6450. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6451. (command (mailcap-mime-info mime-type)))
  6452. (if (stringp command)
  6453. (setq cmd command)
  6454. (setq cmd 'emacs))))
  6455. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6456. (not (file-exists-p file))
  6457. (not org-open-non-existing-files))
  6458. (error "No such file: %s" file))
  6459. (cond
  6460. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6461. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6462. (while (string-match "['\"]%s['\"]" cmd)
  6463. (setq cmd (replace-match "%s" t t cmd)))
  6464. (while (string-match "%s" cmd)
  6465. (setq cmd (replace-match
  6466. (save-match-data (shell-quote-argument file))
  6467. t t cmd)))
  6468. (save-window-excursion
  6469. (start-process-shell-command cmd nil cmd)
  6470. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6471. ))
  6472. ((or (stringp cmd)
  6473. (eq cmd 'emacs))
  6474. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6475. (widen)
  6476. (if line (goto-line line)
  6477. (if search (org-link-search search))))
  6478. ((consp cmd)
  6479. (eval cmd))
  6480. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6481. (and (org-mode-p) (eq old-mode 'org-mode)
  6482. (or (not (equal old-buffer (current-buffer)))
  6483. (not (equal old-pos (point))))
  6484. (org-mark-ring-push old-pos old-buffer))))
  6485. (defun org-default-apps ()
  6486. "Return the default applications for this operating system."
  6487. (cond
  6488. ((eq system-type 'darwin)
  6489. org-file-apps-defaults-macosx)
  6490. ((eq system-type 'windows-nt)
  6491. org-file-apps-defaults-windowsnt)
  6492. (t org-file-apps-defaults-gnu)))
  6493. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6494. (defun org-file-remote-p (file)
  6495. "Test whether FILE specifies a location on a remote system.
  6496. Return non-nil if the location is indeed remote.
  6497. For example, the filename \"/user@host:/foo\" specifies a location
  6498. on the system \"/user@host:\"."
  6499. (cond ((fboundp 'file-remote-p)
  6500. (file-remote-p file))
  6501. ((fboundp 'tramp-handle-file-remote-p)
  6502. (tramp-handle-file-remote-p file))
  6503. ((and (boundp 'ange-ftp-name-format)
  6504. (string-match (car ange-ftp-name-format) file))
  6505. t)
  6506. (t nil)))
  6507. ;;;; Refiling
  6508. (defun org-get-org-file ()
  6509. "Read a filename, with default directory `org-directory'."
  6510. (let ((default (or org-default-notes-file remember-data-file)))
  6511. (read-file-name (format "File name [%s]: " default)
  6512. (file-name-as-directory org-directory)
  6513. default)))
  6514. (defun org-notes-order-reversed-p ()
  6515. "Check if the current file should receive notes in reversed order."
  6516. (cond
  6517. ((not org-reverse-note-order) nil)
  6518. ((eq t org-reverse-note-order) t)
  6519. ((not (listp org-reverse-note-order)) nil)
  6520. (t (catch 'exit
  6521. (let ((all org-reverse-note-order)
  6522. entry)
  6523. (while (setq entry (pop all))
  6524. (if (string-match (car entry) buffer-file-name)
  6525. (throw 'exit (cdr entry))))
  6526. nil)))))
  6527. (defvar org-refile-target-table nil
  6528. "The list of refile targets, created by `org-refile'.")
  6529. (defvar org-agenda-new-buffers nil
  6530. "Buffers created to visit agenda files.")
  6531. (defun org-get-refile-targets (&optional default-buffer)
  6532. "Produce a table with refile targets."
  6533. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6534. targets txt re files f desc descre)
  6535. (with-current-buffer (or default-buffer (current-buffer))
  6536. (while (setq entry (pop entries))
  6537. (setq files (car entry) desc (cdr entry))
  6538. (cond
  6539. ((null files) (setq files (list (current-buffer))))
  6540. ((eq files 'org-agenda-files)
  6541. (setq files (org-agenda-files 'unrestricted)))
  6542. ((and (symbolp files) (fboundp files))
  6543. (setq files (funcall files)))
  6544. ((and (symbolp files) (boundp files))
  6545. (setq files (symbol-value files))))
  6546. (if (stringp files) (setq files (list files)))
  6547. (cond
  6548. ((eq (car desc) :tag)
  6549. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6550. ((eq (car desc) :todo)
  6551. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6552. ((eq (car desc) :regexp)
  6553. (setq descre (cdr desc)))
  6554. ((eq (car desc) :level)
  6555. (setq descre (concat "^\\*\\{" (number-to-string
  6556. (if org-odd-levels-only
  6557. (1- (* 2 (cdr desc)))
  6558. (cdr desc)))
  6559. "\\}[ \t]")))
  6560. ((eq (car desc) :maxlevel)
  6561. (setq descre (concat "^\\*\\{1," (number-to-string
  6562. (if org-odd-levels-only
  6563. (1- (* 2 (cdr desc)))
  6564. (cdr desc)))
  6565. "\\}[ \t]")))
  6566. (t (error "Bad refiling target description %s" desc)))
  6567. (while (setq f (pop files))
  6568. (save-excursion
  6569. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6570. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6571. (save-excursion
  6572. (save-restriction
  6573. (widen)
  6574. (goto-char (point-min))
  6575. (while (re-search-forward descre nil t)
  6576. (goto-char (point-at-bol))
  6577. (when (looking-at org-complex-heading-regexp)
  6578. (setq txt (match-string 4)
  6579. re (concat "^" (regexp-quote
  6580. (buffer-substring (match-beginning 1)
  6581. (match-end 4)))))
  6582. (if (match-end 5) (setq re (concat re "[ \t]+"
  6583. (regexp-quote
  6584. (match-string 5)))))
  6585. (setq re (concat re "[ \t]*$"))
  6586. (when org-refile-use-outline-path
  6587. (setq txt (mapconcat 'identity
  6588. (append
  6589. (if (eq org-refile-use-outline-path 'file)
  6590. (list (file-name-nondirectory
  6591. (buffer-file-name (buffer-base-buffer))))
  6592. (if (eq org-refile-use-outline-path 'full-file-path)
  6593. (list (buffer-file-name (buffer-base-buffer)))))
  6594. (org-get-outline-path)
  6595. (list txt))
  6596. "/")))
  6597. (push (list txt f re (point)) targets))
  6598. (goto-char (point-at-eol))))))))
  6599. (nreverse targets))))
  6600. (defun org-get-outline-path ()
  6601. "Return the outline path to the current entry, as a list."
  6602. (let (rtn)
  6603. (save-excursion
  6604. (while (org-up-heading-safe)
  6605. (when (looking-at org-complex-heading-regexp)
  6606. (push (org-match-string-no-properties 4) rtn)))
  6607. rtn)))
  6608. (defvar org-refile-history nil
  6609. "History for refiling operations.")
  6610. (defun org-refile (&optional goto default-buffer)
  6611. "Move the entry at point to another heading.
  6612. The list of target headings is compiled using the information in
  6613. `org-refile-targets', which see. This list is created before each use
  6614. and will therefore always be up-to-date.
  6615. At the target location, the entry is filed as a subitem of the target heading.
  6616. Depending on `org-reverse-note-order', the new subitem will either be the
  6617. first of the last subitem.
  6618. With prefix arg GOTO, the command will only visit the target location,
  6619. not actually move anything.
  6620. With a double prefix `C-c C-c', go to the location where the last refiling
  6621. operation has put the subtree."
  6622. (interactive "P")
  6623. (let* ((cbuf (current-buffer))
  6624. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6625. pos it nbuf file re level reversed)
  6626. (if (equal goto '(16))
  6627. (org-refile-goto-last-stored)
  6628. (when (setq it (org-refile-get-location
  6629. (if goto "Goto: " "Refile to: ") default-buffer))
  6630. (setq file (nth 1 it)
  6631. re (nth 2 it)
  6632. pos (nth 3 it))
  6633. (setq nbuf (or (find-buffer-visiting file)
  6634. (find-file-noselect file)))
  6635. (if goto
  6636. (progn
  6637. (switch-to-buffer nbuf)
  6638. (goto-char pos)
  6639. (org-show-context 'org-goto))
  6640. (org-copy-special)
  6641. (save-excursion
  6642. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6643. (find-file-noselect file))))
  6644. (setq reversed (org-notes-order-reversed-p))
  6645. (save-excursion
  6646. (save-restriction
  6647. (widen)
  6648. (goto-char pos)
  6649. (looking-at outline-regexp)
  6650. (setq level (org-get-valid-level (funcall outline-level) 1))
  6651. (goto-char
  6652. (if reversed
  6653. (outline-next-heading)
  6654. (or (save-excursion (outline-get-next-sibling))
  6655. (org-end-of-subtree t t)
  6656. (point-max))))
  6657. (bookmark-set "org-refile-last-stored")
  6658. (org-paste-subtree level))))
  6659. (org-cut-special)
  6660. (message "Entry refiled to \"%s\"" (car it)))))))
  6661. (defun org-refile-goto-last-stored ()
  6662. "Go to the location where the last refile was stored."
  6663. (interactive)
  6664. (bookmark-jump "org-refile-last-stored")
  6665. (message "This is the location of the last refile"))
  6666. (defun org-refile-get-location (&optional prompt default-buffer)
  6667. "Prompt the user for a refile location, using PROMPT."
  6668. (let ((org-refile-targets org-refile-targets)
  6669. (org-refile-use-outline-path org-refile-use-outline-path))
  6670. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6671. (unless org-refile-target-table
  6672. (error "No refile targets"))
  6673. (let* ((cbuf (current-buffer))
  6674. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6675. (fname (and filename (file-truename filename)))
  6676. (tbl (mapcar
  6677. (lambda (x)
  6678. (if (not (equal fname (file-truename (nth 1 x))))
  6679. (cons (concat (car x) " (" (file-name-nondirectory
  6680. (nth 1 x)) ")")
  6681. (cdr x))
  6682. x))
  6683. org-refile-target-table))
  6684. (completion-ignore-case t))
  6685. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6686. tbl)))
  6687. ;;;; Dynamic blocks
  6688. (defun org-find-dblock (name)
  6689. "Find the first dynamic block with name NAME in the buffer.
  6690. If not found, stay at current position and return nil."
  6691. (let (pos)
  6692. (save-excursion
  6693. (goto-char (point-min))
  6694. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6695. nil t)
  6696. (match-beginning 0))))
  6697. (if pos (goto-char pos))
  6698. pos))
  6699. (defconst org-dblock-start-re
  6700. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6701. "Matches the startline of a dynamic block, with parameters.")
  6702. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6703. "Matches the end of a dyhamic block.")
  6704. (defun org-create-dblock (plist)
  6705. "Create a dynamic block section, with parameters taken from PLIST.
  6706. PLIST must containe a :name entry which is used as name of the block."
  6707. (unless (bolp) (newline))
  6708. (let ((name (plist-get plist :name)))
  6709. (insert "#+BEGIN: " name)
  6710. (while plist
  6711. (if (eq (car plist) :name)
  6712. (setq plist (cddr plist))
  6713. (insert " " (prin1-to-string (pop plist)))))
  6714. (insert "\n\n#+END:\n")
  6715. (beginning-of-line -2)))
  6716. (defun org-prepare-dblock ()
  6717. "Prepare dynamic block for refresh.
  6718. This empties the block, puts the cursor at the insert position and returns
  6719. the property list including an extra property :name with the block name."
  6720. (unless (looking-at org-dblock-start-re)
  6721. (error "Not at a dynamic block"))
  6722. (let* ((begdel (1+ (match-end 0)))
  6723. (name (org-no-properties (match-string 1)))
  6724. (params (append (list :name name)
  6725. (read (concat "(" (match-string 3) ")")))))
  6726. (unless (re-search-forward org-dblock-end-re nil t)
  6727. (error "Dynamic block not terminated"))
  6728. (setq params
  6729. (append params
  6730. (list :content (buffer-substring
  6731. begdel (match-beginning 0)))))
  6732. (delete-region begdel (match-beginning 0))
  6733. (goto-char begdel)
  6734. (open-line 1)
  6735. params))
  6736. (defun org-map-dblocks (&optional command)
  6737. "Apply COMMAND to all dynamic blocks in the current buffer.
  6738. If COMMAND is not given, use `org-update-dblock'."
  6739. (let ((cmd (or command 'org-update-dblock))
  6740. pos)
  6741. (save-excursion
  6742. (goto-char (point-min))
  6743. (while (re-search-forward org-dblock-start-re nil t)
  6744. (goto-char (setq pos (match-beginning 0)))
  6745. (condition-case nil
  6746. (funcall cmd)
  6747. (error (message "Error during update of dynamic block")))
  6748. (goto-char pos)
  6749. (unless (re-search-forward org-dblock-end-re nil t)
  6750. (error "Dynamic block not terminated"))))))
  6751. (defun org-dblock-update (&optional arg)
  6752. "User command for updating dynamic blocks.
  6753. Update the dynamic block at point. With prefix ARG, update all dynamic
  6754. blocks in the buffer."
  6755. (interactive "P")
  6756. (if arg
  6757. (org-update-all-dblocks)
  6758. (or (looking-at org-dblock-start-re)
  6759. (org-beginning-of-dblock))
  6760. (org-update-dblock)))
  6761. (defun org-update-dblock ()
  6762. "Update the dynamic block at point
  6763. This means to empty the block, parse for parameters and then call
  6764. the correct writing function."
  6765. (save-window-excursion
  6766. (let* ((pos (point))
  6767. (line (org-current-line))
  6768. (params (org-prepare-dblock))
  6769. (name (plist-get params :name))
  6770. (cmd (intern (concat "org-dblock-write:" name))))
  6771. (message "Updating dynamic block `%s' at line %d..." name line)
  6772. (funcall cmd params)
  6773. (message "Updating dynamic block `%s' at line %d...done" name line)
  6774. (goto-char pos))))
  6775. (defun org-beginning-of-dblock ()
  6776. "Find the beginning of the dynamic block at point.
  6777. Error if there is no scuh block at point."
  6778. (let ((pos (point))
  6779. beg)
  6780. (end-of-line 1)
  6781. (if (and (re-search-backward org-dblock-start-re nil t)
  6782. (setq beg (match-beginning 0))
  6783. (re-search-forward org-dblock-end-re nil t)
  6784. (> (match-end 0) pos))
  6785. (goto-char beg)
  6786. (goto-char pos)
  6787. (error "Not in a dynamic block"))))
  6788. (defun org-update-all-dblocks ()
  6789. "Update all dynamic blocks in the buffer.
  6790. This function can be used in a hook."
  6791. (when (org-mode-p)
  6792. (org-map-dblocks 'org-update-dblock)))
  6793. ;;;; Completion
  6794. (defconst org-additional-option-like-keywords
  6795. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6796. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6797. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6798. (defun org-complete (&optional arg)
  6799. "Perform completion on word at point.
  6800. At the beginning of a headline, this completes TODO keywords as given in
  6801. `org-todo-keywords'.
  6802. If the current word is preceded by a backslash, completes the TeX symbols
  6803. that are supported for HTML support.
  6804. If the current word is preceded by \"#+\", completes special words for
  6805. setting file options.
  6806. In the line after \"#+STARTUP:, complete valid keywords.\"
  6807. At all other locations, this simply calls the value of
  6808. `org-completion-fallback-command'."
  6809. (interactive "P")
  6810. (org-without-partial-completion
  6811. (catch 'exit
  6812. (let* ((end (point))
  6813. (beg1 (save-excursion
  6814. (skip-chars-backward (org-re "[:alnum:]_@"))
  6815. (point)))
  6816. (beg (save-excursion
  6817. (skip-chars-backward "a-zA-Z0-9_:$")
  6818. (point)))
  6819. (confirm (lambda (x) (stringp (car x))))
  6820. (searchhead (equal (char-before beg) ?*))
  6821. (tag (and (equal (char-before beg1) ?:)
  6822. (equal (char-after (point-at-bol)) ?*)))
  6823. (prop (and (equal (char-before beg1) ?:)
  6824. (not (equal (char-after (point-at-bol)) ?*))))
  6825. (texp (equal (char-before beg) ?\\))
  6826. (link (equal (char-before beg) ?\[))
  6827. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6828. beg)
  6829. "#+"))
  6830. (startup (string-match "^#\\+STARTUP:.*"
  6831. (buffer-substring (point-at-bol) (point))))
  6832. (completion-ignore-case opt)
  6833. (type nil)
  6834. (tbl nil)
  6835. (table (cond
  6836. (opt
  6837. (setq type :opt)
  6838. (require 'org-exp)
  6839. (append
  6840. (mapcar
  6841. (lambda (x)
  6842. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6843. (cons (match-string 2 x) (match-string 1 x)))
  6844. (org-split-string (org-get-current-options) "\n"))
  6845. (mapcar 'list org-additional-option-like-keywords)))
  6846. (startup
  6847. (setq type :startup)
  6848. org-startup-options)
  6849. (link (append org-link-abbrev-alist-local
  6850. org-link-abbrev-alist))
  6851. (texp
  6852. (setq type :tex)
  6853. org-html-entities)
  6854. ((string-match "\\`\\*+[ \t]+\\'"
  6855. (buffer-substring (point-at-bol) beg))
  6856. (setq type :todo)
  6857. (mapcar 'list org-todo-keywords-1))
  6858. (searchhead
  6859. (setq type :searchhead)
  6860. (save-excursion
  6861. (goto-char (point-min))
  6862. (while (re-search-forward org-todo-line-regexp nil t)
  6863. (push (list
  6864. (org-make-org-heading-search-string
  6865. (match-string 3) t))
  6866. tbl)))
  6867. tbl)
  6868. (tag (setq type :tag beg beg1)
  6869. (or org-tag-alist (org-get-buffer-tags)))
  6870. (prop (setq type :prop beg beg1)
  6871. (mapcar 'list (org-buffer-property-keys nil t t)))
  6872. (t (progn
  6873. (call-interactively org-completion-fallback-command)
  6874. (throw 'exit nil)))))
  6875. (pattern (buffer-substring-no-properties beg end))
  6876. (completion (try-completion pattern table confirm)))
  6877. (cond ((eq completion t)
  6878. (if (not (assoc (upcase pattern) table))
  6879. (message "Already complete")
  6880. (if (and (equal type :opt)
  6881. (not (member (car (assoc (upcase pattern) table))
  6882. org-additional-option-like-keywords)))
  6883. (insert (substring (cdr (assoc (upcase pattern) table))
  6884. (length pattern)))
  6885. (if (memq type '(:tag :prop)) (insert ":")))))
  6886. ((null completion)
  6887. (message "Can't find completion for \"%s\"" pattern)
  6888. (ding))
  6889. ((not (string= pattern completion))
  6890. (delete-region beg end)
  6891. (if (string-match " +$" completion)
  6892. (setq completion (replace-match "" t t completion)))
  6893. (insert completion)
  6894. (if (get-buffer-window "*Completions*")
  6895. (delete-window (get-buffer-window "*Completions*")))
  6896. (if (assoc completion table)
  6897. (if (eq type :todo) (insert " ")
  6898. (if (memq type '(:tag :prop)) (insert ":"))))
  6899. (if (and (equal type :opt) (assoc completion table))
  6900. (message "%s" (substitute-command-keys
  6901. "Press \\[org-complete] again to insert example settings"))))
  6902. (t
  6903. (message "Making completion list...")
  6904. (let ((list (sort (all-completions pattern table confirm)
  6905. 'string<)))
  6906. (with-output-to-temp-buffer "*Completions*"
  6907. (condition-case nil
  6908. ;; Protection needed for XEmacs and emacs 21
  6909. (display-completion-list list pattern)
  6910. (error (display-completion-list list)))))
  6911. (message "Making completion list...%s" "done")))))))
  6912. ;;;; TODO, DEADLINE, Comments
  6913. (defun org-toggle-comment ()
  6914. "Change the COMMENT state of an entry."
  6915. (interactive)
  6916. (save-excursion
  6917. (org-back-to-heading)
  6918. (let (case-fold-search)
  6919. (if (looking-at (concat outline-regexp
  6920. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6921. (replace-match "" t t nil 1)
  6922. (if (looking-at outline-regexp)
  6923. (progn
  6924. (goto-char (match-end 0))
  6925. (insert org-comment-string " ")))))))
  6926. (defvar org-last-todo-state-is-todo nil
  6927. "This is non-nil when the last TODO state change led to a TODO state.
  6928. If the last change removed the TODO tag or switched to DONE, then
  6929. this is nil.")
  6930. (defvar org-setting-tags nil) ; dynamically skiped
  6931. (defun org-parse-local-options (string var)
  6932. "Parse STRING for startup setting relevant for variable VAR."
  6933. (let ((rtn (symbol-value var))
  6934. e opts)
  6935. (save-match-data
  6936. (if (or (not string) (not (string-match "\\S-" string)))
  6937. rtn
  6938. (setq opts (delq nil (mapcar (lambda (x)
  6939. (setq e (assoc x org-startup-options))
  6940. (if (eq (nth 1 e) var) e nil))
  6941. (org-split-string string "[ \t]+"))))
  6942. (if (not opts)
  6943. rtn
  6944. (setq rtn nil)
  6945. (while (setq e (pop opts))
  6946. (if (not (nth 3 e))
  6947. (setq rtn (nth 2 e))
  6948. (if (not (listp rtn)) (setq rtn nil))
  6949. (push (nth 2 e) rtn)))
  6950. rtn)))))
  6951. (defvar org-blocker-hook nil
  6952. "Hook for functions that are allowed to block a state change.
  6953. Each function gets as its single argument a property list, see
  6954. `org-trigger-hook' for more information about this list.
  6955. If any of the functions in this hook returns nil, the state change
  6956. is blocked.")
  6957. (defvar org-trigger-hook nil
  6958. "Hook for functions that are triggered by a state change.
  6959. Each function gets as its single argument a property list with at least
  6960. the following elements:
  6961. (:type type-of-change :position pos-at-entry-start
  6962. :from old-state :to new-state)
  6963. Depending on the type, more properties may be present.
  6964. This mechanism is currently implemented for:
  6965. TODO state changes
  6966. ------------------
  6967. :type todo-state-change
  6968. :from previous state (keyword as a string), or nil
  6969. :to new state (keyword as a string), or nil")
  6970. (defun org-todo (&optional arg)
  6971. "Change the TODO state of an item.
  6972. The state of an item is given by a keyword at the start of the heading,
  6973. like
  6974. *** TODO Write paper
  6975. *** DONE Call mom
  6976. The different keywords are specified in the variable `org-todo-keywords'.
  6977. By default the available states are \"TODO\" and \"DONE\".
  6978. So for this example: when the item starts with TODO, it is changed to DONE.
  6979. When it starts with DONE, the DONE is removed. And when neither TODO nor
  6980. DONE are present, add TODO at the beginning of the heading.
  6981. With C-u prefix arg, use completion to determine the new state.
  6982. With numeric prefix arg, switch to that state.
  6983. For calling through lisp, arg is also interpreted in the following way:
  6984. 'none -> empty state
  6985. \"\"(empty string) -> switch to empty state
  6986. 'done -> switch to DONE
  6987. 'nextset -> switch to the next set of keywords
  6988. 'previousset -> switch to the previous set of keywords
  6989. \"WAITING\" -> switch to the specified keyword, but only if it
  6990. really is a member of `org-todo-keywords'."
  6991. (interactive "P")
  6992. (save-excursion
  6993. (catch 'exit
  6994. (org-back-to-heading)
  6995. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  6996. (or (looking-at (concat " +" org-todo-regexp " *"))
  6997. (looking-at " *"))
  6998. (let* ((match-data (match-data))
  6999. (startpos (point-at-bol))
  7000. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7001. (org-log-done org-log-done)
  7002. (org-log-repeat org-log-repeat)
  7003. (org-todo-log-states org-todo-log-states)
  7004. (this (match-string 1))
  7005. (hl-pos (match-beginning 0))
  7006. (head (org-get-todo-sequence-head this))
  7007. (ass (assoc head org-todo-kwd-alist))
  7008. (interpret (nth 1 ass))
  7009. (done-word (nth 3 ass))
  7010. (final-done-word (nth 4 ass))
  7011. (last-state (or this ""))
  7012. (completion-ignore-case t)
  7013. (member (member this org-todo-keywords-1))
  7014. (tail (cdr member))
  7015. (state (cond
  7016. ((and org-todo-key-trigger
  7017. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7018. (and (not arg) org-use-fast-todo-selection
  7019. (not (eq org-use-fast-todo-selection 'prefix)))))
  7020. ;; Use fast selection
  7021. (org-fast-todo-selection))
  7022. ((and (equal arg '(4))
  7023. (or (not org-use-fast-todo-selection)
  7024. (not org-todo-key-trigger)))
  7025. ;; Read a state with completion
  7026. (completing-read "State: " (mapcar (lambda(x) (list x))
  7027. org-todo-keywords-1)
  7028. nil t))
  7029. ((eq arg 'right)
  7030. (if this
  7031. (if tail (car tail) nil)
  7032. (car org-todo-keywords-1)))
  7033. ((eq arg 'left)
  7034. (if (equal member org-todo-keywords-1)
  7035. nil
  7036. (if this
  7037. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7038. org-todo-keywords-1)
  7039. (org-last org-todo-keywords-1))))
  7040. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7041. (setq arg nil))) ; hack to fall back to cycling
  7042. (arg
  7043. ;; user or caller requests a specific state
  7044. (cond
  7045. ((equal arg "") nil)
  7046. ((eq arg 'none) nil)
  7047. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7048. ((eq arg 'nextset)
  7049. (or (car (cdr (member head org-todo-heads)))
  7050. (car org-todo-heads)))
  7051. ((eq arg 'previousset)
  7052. (let ((org-todo-heads (reverse org-todo-heads)))
  7053. (or (car (cdr (member head org-todo-heads)))
  7054. (car org-todo-heads))))
  7055. ((car (member arg org-todo-keywords-1)))
  7056. ((nth (1- (prefix-numeric-value arg))
  7057. org-todo-keywords-1))))
  7058. ((null member) (or head (car org-todo-keywords-1)))
  7059. ((equal this final-done-word) nil) ;; -> make empty
  7060. ((null tail) nil) ;; -> first entry
  7061. ((eq interpret 'sequence)
  7062. (car tail))
  7063. ((memq interpret '(type priority))
  7064. (if (eq this-command last-command)
  7065. (car tail)
  7066. (if (> (length tail) 0)
  7067. (or done-word (car org-done-keywords))
  7068. nil)))
  7069. (t nil)))
  7070. (next (if state (concat " " state " ") " "))
  7071. (change-plist (list :type 'todo-state-change :from this :to state
  7072. :position startpos))
  7073. dolog now-done-p)
  7074. (when org-blocker-hook
  7075. (unless (save-excursion
  7076. (save-match-data
  7077. (run-hook-with-args-until-failure
  7078. 'org-blocker-hook change-plist)))
  7079. (if (interactive-p)
  7080. (error "TODO state change from %s to %s blocked" this state)
  7081. ;; fail silently
  7082. (message "TODO state change from %s to %s blocked" this state)
  7083. (throw 'exit nil))))
  7084. (store-match-data match-data)
  7085. (replace-match next t t)
  7086. (unless (pos-visible-in-window-p hl-pos)
  7087. (message "TODO state changed to %s" (org-trim next)))
  7088. (unless head
  7089. (setq head (org-get-todo-sequence-head state)
  7090. ass (assoc head org-todo-kwd-alist)
  7091. interpret (nth 1 ass)
  7092. done-word (nth 3 ass)
  7093. final-done-word (nth 4 ass)))
  7094. (when (memq arg '(nextset previousset))
  7095. (message "Keyword-Set %d/%d: %s"
  7096. (- (length org-todo-sets) -1
  7097. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7098. (length org-todo-sets)
  7099. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7100. (setq org-last-todo-state-is-todo
  7101. (not (member state org-done-keywords)))
  7102. (setq now-done-p (and (member state org-done-keywords)
  7103. (not (member this org-done-keywords))))
  7104. (and logging (org-local-logging logging))
  7105. (when (and (or org-todo-log-states org-log-done)
  7106. (not (memq arg '(nextset previousset))))
  7107. ;; we need to look at recording a time and note
  7108. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7109. (nth 2 (assoc this org-todo-log-states))))
  7110. (when (and state
  7111. (member state org-not-done-keywords)
  7112. (not (member this org-not-done-keywords)))
  7113. ;; This is now a todo state and was not one before
  7114. ;; If there was a CLOSED time stamp, get rid of it.
  7115. (org-add-planning-info nil nil 'closed))
  7116. (when (and now-done-p org-log-done)
  7117. ;; It is now done, and it was not done before
  7118. (org-add-planning-info 'closed (org-current-time))
  7119. (if (and (not dolog) (eq 'note org-log-done))
  7120. (org-add-log-setup 'done state 'findpos 'note)))
  7121. (when (and state dolog)
  7122. ;; This is a non-nil state, and we need to log it
  7123. (org-add-log-setup 'state state 'findpos dolog)))
  7124. ;; Fixup tag positioning
  7125. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7126. (run-hooks 'org-after-todo-state-change-hook)
  7127. (if (and arg (not (member state org-done-keywords)))
  7128. (setq head (org-get-todo-sequence-head state)))
  7129. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7130. ;; Do we need to trigger a repeat?
  7131. (when now-done-p (org-auto-repeat-maybe state))
  7132. ;; Fixup cursor location if close to the keyword
  7133. (if (and (outline-on-heading-p)
  7134. (not (bolp))
  7135. (save-excursion (beginning-of-line 1)
  7136. (looking-at org-todo-line-regexp))
  7137. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7138. (progn
  7139. (goto-char (or (match-end 2) (match-end 1)))
  7140. (just-one-space)))
  7141. (when org-trigger-hook
  7142. (save-excursion
  7143. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7144. (defun org-local-logging (value)
  7145. "Get logging settings from a property VALUE."
  7146. (let* (words w a)
  7147. ;; directly set the variables, they are already local.
  7148. (setq org-log-done nil
  7149. org-log-repeat nil
  7150. org-todo-log-states nil)
  7151. (setq words (org-split-string value))
  7152. (while (setq w (pop words))
  7153. (cond
  7154. ((setq a (assoc w org-startup-options))
  7155. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7156. (set (nth 1 a) (nth 2 a))))
  7157. ((setq a (org-extract-log-state-settings w))
  7158. (and (member (car a) org-todo-keywords-1)
  7159. (push a org-todo-log-states)))))))
  7160. (defun org-get-todo-sequence-head (kwd)
  7161. "Return the head of the TODO sequence to which KWD belongs.
  7162. If KWD is not set, check if there is a text property remembering the
  7163. right sequence."
  7164. (let (p)
  7165. (cond
  7166. ((not kwd)
  7167. (or (get-text-property (point-at-bol) 'org-todo-head)
  7168. (progn
  7169. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7170. nil (point-at-eol)))
  7171. (get-text-property p 'org-todo-head))))
  7172. ((not (member kwd org-todo-keywords-1))
  7173. (car org-todo-keywords-1))
  7174. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7175. (defun org-fast-todo-selection ()
  7176. "Fast TODO keyword selection with single keys.
  7177. Returns the new TODO keyword, or nil if no state change should occur."
  7178. (let* ((fulltable org-todo-key-alist)
  7179. (done-keywords org-done-keywords) ;; needed for the faces.
  7180. (maxlen (apply 'max (mapcar
  7181. (lambda (x)
  7182. (if (stringp (car x)) (string-width (car x)) 0))
  7183. fulltable)))
  7184. (expert nil)
  7185. (fwidth (+ maxlen 3 1 3))
  7186. (ncol (/ (- (window-width) 4) fwidth))
  7187. tg cnt e c tbl
  7188. groups ingroup)
  7189. (save-window-excursion
  7190. (if expert
  7191. (set-buffer (get-buffer-create " *Org todo*"))
  7192. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7193. (erase-buffer)
  7194. (org-set-local 'org-done-keywords done-keywords)
  7195. (setq tbl fulltable cnt 0)
  7196. (while (setq e (pop tbl))
  7197. (cond
  7198. ((equal e '(:startgroup))
  7199. (push '() groups) (setq ingroup t)
  7200. (when (not (= cnt 0))
  7201. (setq cnt 0)
  7202. (insert "\n"))
  7203. (insert "{ "))
  7204. ((equal e '(:endgroup))
  7205. (setq ingroup nil cnt 0)
  7206. (insert "}\n"))
  7207. (t
  7208. (setq tg (car e) c (cdr e))
  7209. (if ingroup (push tg (car groups)))
  7210. (setq tg (org-add-props tg nil 'face
  7211. (org-get-todo-face tg)))
  7212. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7213. (insert "[" c "] " tg (make-string
  7214. (- fwidth 4 (length tg)) ?\ ))
  7215. (when (= (setq cnt (1+ cnt)) ncol)
  7216. (insert "\n")
  7217. (if ingroup (insert " "))
  7218. (setq cnt 0)))))
  7219. (insert "\n")
  7220. (goto-char (point-min))
  7221. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7222. (fit-window-to-buffer))
  7223. (message "[a-z..]:Set [SPC]:clear")
  7224. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7225. (cond
  7226. ((or (= c ?\C-g)
  7227. (and (= c ?q) (not (rassoc c fulltable))))
  7228. (setq quit-flag t))
  7229. ((= c ?\ ) nil)
  7230. ((setq e (rassoc c fulltable) tg (car e))
  7231. tg)
  7232. (t (setq quit-flag t))))))
  7233. (defun org-entry-is-todo-p ()
  7234. (member (org-get-todo-state) org-not-done-keywords))
  7235. (defun org-entry-is-done-p ()
  7236. (member (org-get-todo-state) org-done-keywords))
  7237. (defun org-get-todo-state ()
  7238. (save-excursion
  7239. (org-back-to-heading t)
  7240. (and (looking-at org-todo-line-regexp)
  7241. (match-end 2)
  7242. (match-string 2))))
  7243. (defun org-at-date-range-p (&optional inactive-ok)
  7244. "Is the cursor inside a date range?"
  7245. (interactive)
  7246. (save-excursion
  7247. (catch 'exit
  7248. (let ((pos (point)))
  7249. (skip-chars-backward "^[<\r\n")
  7250. (skip-chars-backward "<[")
  7251. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7252. (>= (match-end 0) pos)
  7253. (throw 'exit t))
  7254. (skip-chars-backward "^<[\r\n")
  7255. (skip-chars-backward "<[")
  7256. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7257. (>= (match-end 0) pos)
  7258. (throw 'exit t)))
  7259. nil)))
  7260. (defun org-get-repeat ()
  7261. "Check if tere is a deadline/schedule with repeater in this entry."
  7262. (save-match-data
  7263. (save-excursion
  7264. (org-back-to-heading t)
  7265. (if (re-search-forward
  7266. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7267. (match-string 1)))))
  7268. (defvar org-last-changed-timestamp)
  7269. (defvar org-log-post-message)
  7270. (defvar org-log-note-purpose)
  7271. (defvar org-log-note-how)
  7272. (defun org-auto-repeat-maybe (done-word)
  7273. "Check if the current headline contains a repeated deadline/schedule.
  7274. If yes, set TODO state back to what it was and change the base date
  7275. of repeating deadline/scheduled time stamps to new date.
  7276. This function is run automatically after each state change to a DONE state."
  7277. ;; last-state is dynamically scoped into this function
  7278. (let* ((repeat (org-get-repeat))
  7279. (aa (assoc last-state org-todo-kwd-alist))
  7280. (interpret (nth 1 aa))
  7281. (head (nth 2 aa))
  7282. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7283. (msg "Entry repeats: ")
  7284. (org-log-done nil)
  7285. (org-todo-log-states nil)
  7286. (nshiftmax 10) (nshift 0)
  7287. re type n what ts mb0 time)
  7288. (when repeat
  7289. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7290. (org-todo (if (eq interpret 'type) last-state head))
  7291. (when org-log-repeat
  7292. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7293. (memq 'org-add-log-note post-command-hook))
  7294. ;; OK, we are already setup for some record
  7295. (if (eq org-log-repeat 'note)
  7296. ;; make sure we take a note, not only a time stamp
  7297. (setq org-log-note-how 'note))
  7298. ;; Set up for taking a record
  7299. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7300. 'findpos org-log-repeat)))
  7301. (org-back-to-heading t)
  7302. (org-add-planning-info nil nil 'closed)
  7303. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7304. org-deadline-time-regexp "\\)\\|\\("
  7305. org-ts-regexp "\\)"))
  7306. (while (re-search-forward
  7307. re (save-excursion (outline-next-heading) (point)) t)
  7308. (setq type (if (match-end 1) org-scheduled-string
  7309. (if (match-end 3) org-deadline-string "Plain:"))
  7310. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7311. mb0 (match-beginning 0))
  7312. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7313. (setq n (string-to-number (match-string 2 ts))
  7314. what (match-string 3 ts))
  7315. (if (equal what "w") (setq n (* n 7) what "d"))
  7316. ;; Preparation, see if we need to modify the start date for the change
  7317. (when (match-end 1)
  7318. (setq time (save-match-data (org-time-string-to-time ts)))
  7319. (cond
  7320. ((equal (match-string 1 ts) ".")
  7321. ;; Shift starting date to today
  7322. (org-timestamp-change
  7323. (- (time-to-days (current-time)) (time-to-days time))
  7324. 'day))
  7325. ((equal (match-string 1 ts) "+")
  7326. (while (or (= nshift 0)
  7327. (<= (time-to-days time) (time-to-days (current-time))))
  7328. (when (= (incf nshift) nshiftmax)
  7329. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7330. (error "Abort")))
  7331. (org-timestamp-change n (cdr (assoc what whata)))
  7332. (org-at-timestamp-p t)
  7333. (setq ts (match-string 1))
  7334. (setq time (save-match-data (org-time-string-to-time ts))))
  7335. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7336. ;; rematch, so that we have everything in place for the real shift
  7337. (org-at-timestamp-p t)
  7338. (setq ts (match-string 1))
  7339. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7340. (org-timestamp-change n (cdr (assoc what whata)))
  7341. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7342. (setq org-log-post-message msg)
  7343. (message "%s" msg))))
  7344. (defun org-show-todo-tree (arg)
  7345. "Make a compact tree which shows all headlines marked with TODO.
  7346. The tree will show the lines where the regexp matches, and all higher
  7347. headlines above the match.
  7348. With a \\[universal-argument] prefix, also show the DONE entries.
  7349. With a numeric prefix N, construct a sparse tree for the Nth element
  7350. of `org-todo-keywords-1'."
  7351. (interactive "P")
  7352. (let ((case-fold-search nil)
  7353. (kwd-re
  7354. (cond ((null arg) org-not-done-regexp)
  7355. ((equal arg '(4))
  7356. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7357. (mapcar 'list org-todo-keywords-1))))
  7358. (concat "\\("
  7359. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7360. "\\)\\>")))
  7361. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7362. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7363. org-todo-keywords-1)))
  7364. (t (error "Invalid prefix argument: %s" arg)))))
  7365. (message "%d TODO entries found"
  7366. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7367. (defun org-deadline (&optional remove)
  7368. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7369. With argument REMOVE, remove any deadline from the item."
  7370. (interactive "P")
  7371. (if remove
  7372. (progn
  7373. (org-remove-timestamp-with-keyword org-deadline-string)
  7374. (message "Item no longer has a deadline."))
  7375. (org-add-planning-info 'deadline nil 'closed)))
  7376. (defun org-schedule (&optional remove)
  7377. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7378. With argument REMOVE, remove any scheduling date from the item."
  7379. (interactive "P")
  7380. (if remove
  7381. (progn
  7382. (org-remove-timestamp-with-keyword org-scheduled-string)
  7383. (message "Item is no longer scheduled."))
  7384. (org-add-planning-info 'scheduled nil 'closed)))
  7385. (defun org-remove-timestamp-with-keyword (keyword)
  7386. "Remove all time stamps with KEYWORD in the current entry."
  7387. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7388. beg)
  7389. (save-excursion
  7390. (org-back-to-heading t)
  7391. (setq beg (point))
  7392. (org-end-of-subtree t t)
  7393. (while (re-search-backward re beg t)
  7394. (replace-match "")
  7395. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7396. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7397. (defun org-add-planning-info (what &optional time &rest remove)
  7398. "Insert new timestamp with keyword in the line directly after the headline.
  7399. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7400. If non is given, the user is prompted for a date.
  7401. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7402. be removed."
  7403. (interactive)
  7404. (let (org-time-was-given org-end-time-was-given ts
  7405. end default-time default-input)
  7406. (when (and (not time) (memq what '(scheduled deadline)))
  7407. ;; Try to get a default date/time from existing timestamp
  7408. (save-excursion
  7409. (org-back-to-heading t)
  7410. (setq end (save-excursion (outline-next-heading) (point)))
  7411. (when (re-search-forward (if (eq what 'scheduled)
  7412. org-scheduled-time-regexp
  7413. org-deadline-time-regexp)
  7414. end t)
  7415. (setq ts (match-string 1)
  7416. default-time
  7417. (apply 'encode-time (org-parse-time-string ts))
  7418. default-input (and ts (org-get-compact-tod ts))))))
  7419. (when what
  7420. ;; If necessary, get the time from the user
  7421. (setq time (or time (org-read-date nil 'to-time nil nil
  7422. default-time default-input))))
  7423. (when (and org-insert-labeled-timestamps-at-point
  7424. (member what '(scheduled deadline)))
  7425. (insert
  7426. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7427. (org-insert-time-stamp time org-time-was-given
  7428. nil nil nil (list org-end-time-was-given))
  7429. (setq what nil))
  7430. (save-excursion
  7431. (save-restriction
  7432. (let (col list elt ts buffer-invisibility-spec)
  7433. (org-back-to-heading t)
  7434. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7435. (goto-char (match-end 1))
  7436. (setq col (current-column))
  7437. (goto-char (match-end 0))
  7438. (if (eobp) (insert "\n") (forward-char 1))
  7439. (if (and (not (looking-at outline-regexp))
  7440. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7441. "[^\r\n]*"))
  7442. (not (equal (match-string 1) org-clock-string)))
  7443. (narrow-to-region (match-beginning 0) (match-end 0))
  7444. (insert-before-markers "\n")
  7445. (backward-char 1)
  7446. (narrow-to-region (point) (point))
  7447. (org-indent-to-column col))
  7448. ;; Check if we have to remove something.
  7449. (setq list (cons what remove))
  7450. (while list
  7451. (setq elt (pop list))
  7452. (goto-char (point-min))
  7453. (when (or (and (eq elt 'scheduled)
  7454. (re-search-forward org-scheduled-time-regexp nil t))
  7455. (and (eq elt 'deadline)
  7456. (re-search-forward org-deadline-time-regexp nil t))
  7457. (and (eq elt 'closed)
  7458. (re-search-forward org-closed-time-regexp nil t)))
  7459. (replace-match "")
  7460. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7461. (if (looking-at " +") (replace-match ""))))
  7462. (goto-char (point-max))
  7463. (when what
  7464. (insert
  7465. (if (not (equal (char-before) ?\ )) " " "")
  7466. (cond ((eq what 'scheduled) org-scheduled-string)
  7467. ((eq what 'deadline) org-deadline-string)
  7468. ((eq what 'closed) org-closed-string))
  7469. " ")
  7470. (setq ts (org-insert-time-stamp
  7471. time
  7472. (or org-time-was-given
  7473. (and (eq what 'closed) org-log-done-with-time))
  7474. (eq what 'closed)
  7475. nil nil (list org-end-time-was-given)))
  7476. (end-of-line 1))
  7477. (goto-char (point-min))
  7478. (widen)
  7479. (if (and (looking-at "[ \t]+\n")
  7480. (equal (char-before) ?\n))
  7481. (backward-delete-char 1))
  7482. ts)))))
  7483. (defvar org-log-note-marker (make-marker))
  7484. (defvar org-log-note-purpose nil)
  7485. (defvar org-log-note-state nil)
  7486. (defvar org-log-note-how nil)
  7487. (defvar org-log-note-window-configuration nil)
  7488. (defvar org-log-note-return-to (make-marker))
  7489. (defvar org-log-post-message nil
  7490. "Message to be displayed after a log note has been stored.
  7491. The auto-repeater uses this.")
  7492. (defun org-add-note ()
  7493. "Add a note to the current entry.
  7494. This is done in the same way as adding a state change note."
  7495. (interactive)
  7496. (org-add-log-setup 'note nil t nil))
  7497. (defun org-add-log-setup (&optional purpose state findpos how)
  7498. "Set up the post command hook to take a note.
  7499. If this is about to TODO state change, the new state is expected in STATE.
  7500. When FINDPOS is non-nil, find the correct position for the note in
  7501. the current entry. If not, assume that it can be inserted at point."
  7502. (save-excursion
  7503. (when findpos
  7504. (org-back-to-heading t)
  7505. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7506. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7507. "[^\r\n]*\\)?"))
  7508. (goto-char (match-end 0))
  7509. (unless org-log-states-order-reversed
  7510. (and (= (char-after) ?\n) (forward-char 1))
  7511. (org-skip-over-state-notes)
  7512. (skip-chars-backward " \t\n\r")))
  7513. (move-marker org-log-note-marker (point))
  7514. (setq org-log-note-purpose purpose
  7515. org-log-note-state state
  7516. org-log-note-how how)
  7517. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7518. (defun org-skip-over-state-notes ()
  7519. "Skip past the list of State notes in an entry."
  7520. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7521. (while (looking-at "[ \t]*- State")
  7522. (condition-case nil
  7523. (org-next-item)
  7524. (error (org-end-of-item)))))
  7525. (defun org-add-log-note (&optional purpose)
  7526. "Pop up a window for taking a note, and add this note later at point."
  7527. (remove-hook 'post-command-hook 'org-add-log-note)
  7528. (setq org-log-note-window-configuration (current-window-configuration))
  7529. (delete-other-windows)
  7530. (move-marker org-log-note-return-to (point))
  7531. (switch-to-buffer (marker-buffer org-log-note-marker))
  7532. (goto-char org-log-note-marker)
  7533. (org-switch-to-buffer-other-window "*Org Note*")
  7534. (erase-buffer)
  7535. (if (memq org-log-note-how '(time state))
  7536. (org-store-log-note)
  7537. (let ((org-inhibit-startup t)) (org-mode))
  7538. (insert (format "# Insert note for %s.
  7539. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7540. (cond
  7541. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7542. ((eq org-log-note-purpose 'done) "closed todo item")
  7543. ((eq org-log-note-purpose 'state)
  7544. (format "state change to \"%s\"" org-log-note-state))
  7545. ((eq org-log-note-purpose 'note)
  7546. "this entry")
  7547. (t (error "This should not happen")))))
  7548. (org-set-local 'org-finish-function 'org-store-log-note)))
  7549. (defvar org-note-abort nil) ; dynamically scoped
  7550. (defun org-store-log-note ()
  7551. "Finish taking a log note, and insert it to where it belongs."
  7552. (let ((txt (buffer-string))
  7553. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7554. lines ind)
  7555. (kill-buffer (current-buffer))
  7556. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7557. (setq txt (replace-match "" t t txt)))
  7558. (if (string-match "\\s-+\\'" txt)
  7559. (setq txt (replace-match "" t t txt)))
  7560. (setq lines (org-split-string txt "\n"))
  7561. (when (and note (string-match "\\S-" note))
  7562. (setq note
  7563. (org-replace-escapes
  7564. note
  7565. (list (cons "%u" (user-login-name))
  7566. (cons "%U" user-full-name)
  7567. (cons "%t" (format-time-string
  7568. (org-time-stamp-format 'long 'inactive)
  7569. (current-time)))
  7570. (cons "%s" (if org-log-note-state
  7571. (concat "\"" org-log-note-state "\"")
  7572. "")))))
  7573. (if lines (setq note (concat note " \\\\")))
  7574. (push note lines))
  7575. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7576. (when lines
  7577. (save-excursion
  7578. (set-buffer (marker-buffer org-log-note-marker))
  7579. (save-excursion
  7580. (goto-char org-log-note-marker)
  7581. (move-marker org-log-note-marker nil)
  7582. (end-of-line 1)
  7583. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7584. (indent-relative nil)
  7585. (insert "- " (pop lines))
  7586. (org-indent-line-function)
  7587. (beginning-of-line 1)
  7588. (looking-at "[ \t]*")
  7589. (setq ind (concat (match-string 0) " "))
  7590. (end-of-line 1)
  7591. (while lines (insert "\n" ind (pop lines)))))))
  7592. (set-window-configuration org-log-note-window-configuration)
  7593. (with-current-buffer (marker-buffer org-log-note-return-to)
  7594. (goto-char org-log-note-return-to))
  7595. (move-marker org-log-note-return-to nil)
  7596. (and org-log-post-message (message "%s" org-log-post-message)))
  7597. (defun org-sparse-tree (&optional arg)
  7598. "Create a sparse tree, prompt for the details.
  7599. This command can create sparse trees. You first need to select the type
  7600. of match used to create the tree:
  7601. t Show entries with a specific TODO keyword.
  7602. T Show entries selected by a tags match.
  7603. p Enter a property name and its value (both with completion on existing
  7604. names/values) and show entries with that property.
  7605. r Show entries matching a regular expression
  7606. d Show deadlines due within `org-deadline-warning-days'."
  7607. (interactive "P")
  7608. (let (ans kwd value)
  7609. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7610. (setq ans (read-char-exclusive))
  7611. (cond
  7612. ((equal ans ?d)
  7613. (call-interactively 'org-check-deadlines))
  7614. ((equal ans ?b)
  7615. (call-interactively 'org-check-before-date))
  7616. ((equal ans ?t)
  7617. (org-show-todo-tree '(4)))
  7618. ((equal ans ?T)
  7619. (call-interactively 'org-tags-sparse-tree))
  7620. ((member ans '(?p ?P))
  7621. (setq kwd (completing-read "Property: "
  7622. (mapcar 'list (org-buffer-property-keys))))
  7623. (setq value (completing-read "Value: "
  7624. (mapcar 'list (org-property-values kwd))))
  7625. (unless (string-match "\\`{.*}\\'" value)
  7626. (setq value (concat "\"" value "\"")))
  7627. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7628. ((member ans '(?r ?R ?/))
  7629. (call-interactively 'org-occur))
  7630. (t (error "No such sparse tree command \"%c\"" ans)))))
  7631. (defvar org-occur-highlights nil
  7632. "List of overlays used for occur matches.")
  7633. (make-variable-buffer-local 'org-occur-highlights)
  7634. (defvar org-occur-parameters nil
  7635. "Parameters of the active org-occur calls.
  7636. This is a list, each call to org-occur pushes as cons cell,
  7637. containing the regular expression and the callback, onto the list.
  7638. The list can contain several entries if `org-occur' has been called
  7639. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7640. will only contain one set of parameters. When the highlights are
  7641. removed (for example with `C-c C-c', or with the next edit (depending
  7642. on `org-remove-highlights-with-change'), this variable is emptied
  7643. as well.")
  7644. (make-variable-buffer-local 'org-occur-parameters)
  7645. (defun org-occur (regexp &optional keep-previous callback)
  7646. "Make a compact tree which shows all matches of REGEXP.
  7647. The tree will show the lines where the regexp matches, and all higher
  7648. headlines above the match. It will also show the heading after the match,
  7649. to make sure editing the matching entry is easy.
  7650. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7651. call to `org-occur' will be kept, to allow stacking of calls to this
  7652. command.
  7653. If CALLBACK is non-nil, it is a function which is called to confirm
  7654. that the match should indeed be shown."
  7655. (interactive "sRegexp: \nP")
  7656. (unless keep-previous
  7657. (org-remove-occur-highlights nil nil t))
  7658. (push (cons regexp callback) org-occur-parameters)
  7659. (let ((cnt 0))
  7660. (save-excursion
  7661. (goto-char (point-min))
  7662. (if (or (not keep-previous) ; do not want to keep
  7663. (not org-occur-highlights)) ; no previous matches
  7664. ;; hide everything
  7665. (org-overview))
  7666. (while (re-search-forward regexp nil t)
  7667. (when (or (not callback)
  7668. (save-match-data (funcall callback)))
  7669. (setq cnt (1+ cnt))
  7670. (when org-highlight-sparse-tree-matches
  7671. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7672. (org-show-context 'occur-tree))))
  7673. (when org-remove-highlights-with-change
  7674. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7675. nil 'local))
  7676. (unless org-sparse-tree-open-archived-trees
  7677. (org-hide-archived-subtrees (point-min) (point-max)))
  7678. (run-hooks 'org-occur-hook)
  7679. (if (interactive-p)
  7680. (message "%d match(es) for regexp %s" cnt regexp))
  7681. cnt))
  7682. (defun org-show-context (&optional key)
  7683. "Make sure point and context and visible.
  7684. How much context is shown depends upon the variables
  7685. `org-show-hierarchy-above', `org-show-following-heading'. and
  7686. `org-show-siblings'."
  7687. (let ((heading-p (org-on-heading-p t))
  7688. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7689. (following-p (org-get-alist-option org-show-following-heading key))
  7690. (entry-p (org-get-alist-option org-show-entry-below key))
  7691. (siblings-p (org-get-alist-option org-show-siblings key)))
  7692. (catch 'exit
  7693. ;; Show heading or entry text
  7694. (if (and heading-p (not entry-p))
  7695. (org-flag-heading nil) ; only show the heading
  7696. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7697. (org-show-hidden-entry))) ; show entire entry
  7698. (when following-p
  7699. ;; Show next sibling, or heading below text
  7700. (save-excursion
  7701. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7702. (org-flag-heading nil))))
  7703. (when siblings-p (org-show-siblings))
  7704. (when hierarchy-p
  7705. ;; show all higher headings, possibly with siblings
  7706. (save-excursion
  7707. (while (and (condition-case nil
  7708. (progn (org-up-heading-all 1) t)
  7709. (error nil))
  7710. (not (bobp)))
  7711. (org-flag-heading nil)
  7712. (when siblings-p (org-show-siblings))))))))
  7713. (defun org-reveal (&optional siblings)
  7714. "Show current entry, hierarchy above it, and the following headline.
  7715. This can be used to show a consistent set of context around locations
  7716. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7717. not t for the search context.
  7718. With optional argument SIBLINGS, on each level of the hierarchy all
  7719. siblings are shown. This repairs the tree structure to what it would
  7720. look like when opened with hierarchical calls to `org-cycle'."
  7721. (interactive "P")
  7722. (let ((org-show-hierarchy-above t)
  7723. (org-show-following-heading t)
  7724. (org-show-siblings (if siblings t org-show-siblings)))
  7725. (org-show-context nil)))
  7726. (defun org-highlight-new-match (beg end)
  7727. "Highlight from BEG to END and mark the highlight is an occur headline."
  7728. (let ((ov (org-make-overlay beg end)))
  7729. (org-overlay-put ov 'face 'secondary-selection)
  7730. (push ov org-occur-highlights)))
  7731. (defun org-remove-occur-highlights (&optional beg end noremove)
  7732. "Remove the occur highlights from the buffer.
  7733. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7734. from the `before-change-functions' in the current buffer."
  7735. (interactive)
  7736. (unless org-inhibit-highlight-removal
  7737. (mapc 'org-delete-overlay org-occur-highlights)
  7738. (setq org-occur-highlights nil)
  7739. (setq org-occur-parameters nil)
  7740. (unless noremove
  7741. (remove-hook 'before-change-functions
  7742. 'org-remove-occur-highlights 'local))))
  7743. ;;;; Priorities
  7744. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7745. "Regular expression matching the priority indicator.")
  7746. (defvar org-remove-priority-next-time nil)
  7747. (defun org-priority-up ()
  7748. "Increase the priority of the current item."
  7749. (interactive)
  7750. (org-priority 'up))
  7751. (defun org-priority-down ()
  7752. "Decrease the priority of the current item."
  7753. (interactive)
  7754. (org-priority 'down))
  7755. (defun org-priority (&optional action)
  7756. "Change the priority of an item by ARG.
  7757. ACTION can be `set', `up', `down', or a character."
  7758. (interactive)
  7759. (setq action (or action 'set))
  7760. (let (current new news have remove)
  7761. (save-excursion
  7762. (org-back-to-heading)
  7763. (if (looking-at org-priority-regexp)
  7764. (setq current (string-to-char (match-string 2))
  7765. have t)
  7766. (setq current org-default-priority))
  7767. (cond
  7768. ((or (eq action 'set)
  7769. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7770. (if (not (eq action 'set))
  7771. (setq new action)
  7772. (message "Priority %c-%c, SPC to remove: "
  7773. org-highest-priority org-lowest-priority)
  7774. (setq new (read-char-exclusive)))
  7775. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7776. (= (upcase org-lowest-priority) org-lowest-priority))
  7777. (setq new (upcase new)))
  7778. (cond ((equal new ?\ ) (setq remove t))
  7779. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7780. (error "Priority must be between `%c' and `%c'"
  7781. org-highest-priority org-lowest-priority))))
  7782. ((eq action 'up)
  7783. (if (and (not have) (eq last-command this-command))
  7784. (setq new org-lowest-priority)
  7785. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7786. org-default-priority (1- current)))))
  7787. ((eq action 'down)
  7788. (if (and (not have) (eq last-command this-command))
  7789. (setq new org-highest-priority)
  7790. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7791. org-default-priority (1+ current)))))
  7792. (t (error "Invalid action")))
  7793. (if (or (< (upcase new) org-highest-priority)
  7794. (> (upcase new) org-lowest-priority))
  7795. (setq remove t))
  7796. (setq news (format "%c" new))
  7797. (if have
  7798. (if remove
  7799. (replace-match "" t t nil 1)
  7800. (replace-match news t t nil 2))
  7801. (if remove
  7802. (error "No priority cookie found in line")
  7803. (looking-at org-todo-line-regexp)
  7804. (if (match-end 2)
  7805. (progn
  7806. (goto-char (match-end 2))
  7807. (insert " [#" news "]"))
  7808. (goto-char (match-beginning 3))
  7809. (insert "[#" news "] ")))))
  7810. (org-preserve-lc (org-set-tags nil 'align))
  7811. (if remove
  7812. (message "Priority removed")
  7813. (message "Priority of current item set to %s" news))))
  7814. (defun org-get-priority (s)
  7815. "Find priority cookie and return priority."
  7816. (save-match-data
  7817. (if (not (string-match org-priority-regexp s))
  7818. (* 1000 (- org-lowest-priority org-default-priority))
  7819. (* 1000 (- org-lowest-priority
  7820. (string-to-char (match-string 2 s)))))))
  7821. ;;;; Tags
  7822. (defun org-scan-tags (action matcher &optional todo-only)
  7823. "Scan headline tags with inheritance and produce output ACTION.
  7824. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7825. evaluated, testing if a given set of tags qualifies a headline for
  7826. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7827. are included in the output."
  7828. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7829. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7830. (org-re
  7831. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7832. (props (list 'face nil
  7833. 'done-face 'org-done
  7834. 'undone-face nil
  7835. 'mouse-face 'highlight
  7836. 'org-not-done-regexp org-not-done-regexp
  7837. 'org-todo-regexp org-todo-regexp
  7838. 'keymap org-agenda-keymap
  7839. 'help-echo
  7840. (format "mouse-2 or RET jump to org file %s"
  7841. (abbreviate-file-name
  7842. (or (buffer-file-name (buffer-base-buffer))
  7843. (buffer-name (buffer-base-buffer)))))))
  7844. (case-fold-search nil)
  7845. lspos
  7846. tags tags-list tags-alist (llast 0) rtn level category i txt
  7847. todo marker entry priority)
  7848. (save-excursion
  7849. (goto-char (point-min))
  7850. (when (eq action 'sparse-tree)
  7851. (org-overview)
  7852. (org-remove-occur-highlights))
  7853. (while (re-search-forward re nil t)
  7854. (catch :skip
  7855. (setq todo (if (match-end 1) (match-string 2))
  7856. tags (if (match-end 4) (match-string 4)))
  7857. (goto-char (setq lspos (1+ (match-beginning 0))))
  7858. (setq level (org-reduced-level (funcall outline-level))
  7859. category (org-get-category))
  7860. (setq i llast llast level)
  7861. ;; remove tag lists from same and sublevels
  7862. (while (>= i level)
  7863. (when (setq entry (assoc i tags-alist))
  7864. (setq tags-alist (delete entry tags-alist)))
  7865. (setq i (1- i)))
  7866. ;; add the next tags
  7867. (when tags
  7868. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7869. tags-alist
  7870. (cons (cons level tags) tags-alist)))
  7871. ;; compile tags for current headline
  7872. (setq tags-list
  7873. (if org-use-tag-inheritance
  7874. (apply 'append (mapcar 'cdr tags-alist))
  7875. tags))
  7876. (when (and tags org-use-tag-inheritance
  7877. (not (eq t org-use-tag-inheritance)))
  7878. ;; selective inheritance, remove uninherited ones
  7879. (setcdr (car tags-alist)
  7880. (org-remove-uniherited-tags (cdar tags-alist))))
  7881. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  7882. (eval matcher)
  7883. (or (not org-agenda-skip-archived-trees)
  7884. (not (member org-archive-tag tags-list))))
  7885. (and (eq action 'agenda) (org-agenda-skip))
  7886. ;; list this headline
  7887. (if (eq action 'sparse-tree)
  7888. (progn
  7889. (and org-highlight-sparse-tree-matches
  7890. (org-get-heading) (match-end 0)
  7891. (org-highlight-new-match
  7892. (match-beginning 0) (match-beginning 1)))
  7893. (org-show-context 'tags-tree))
  7894. (setq txt (org-format-agenda-item
  7895. ""
  7896. (concat
  7897. (if org-tags-match-list-sublevels
  7898. (make-string (1- level) ?.) "")
  7899. (org-get-heading))
  7900. category tags-list)
  7901. priority (org-get-priority txt))
  7902. (goto-char lspos)
  7903. (setq marker (org-agenda-new-marker))
  7904. (org-add-props txt props
  7905. 'org-marker marker 'org-hd-marker marker 'org-category category
  7906. 'priority priority 'type "tagsmatch")
  7907. (push txt rtn))
  7908. ;; if we are to skip sublevels, jump to end of subtree
  7909. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  7910. (when (and (eq action 'sparse-tree)
  7911. (not org-sparse-tree-open-archived-trees))
  7912. (org-hide-archived-subtrees (point-min) (point-max)))
  7913. (nreverse rtn)))
  7914. (defun org-remove-uniherited-tags (tags)
  7915. "Remove all tags that are not inherited from the list TAGS."
  7916. (cond
  7917. ((eq org-use-tag-inheritance t) tags)
  7918. ((not org-use-tag-inheritance) nil)
  7919. ((stringp org-use-tag-inheritance)
  7920. (delq nil (mapcar
  7921. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  7922. tags)))
  7923. ((listp org-use-tag-inheritance)
  7924. (org-delete-all org-use-tag-inheritance tags))))
  7925. (defvar todo-only) ;; dynamically scoped
  7926. (defun org-tags-sparse-tree (&optional todo-only match)
  7927. "Create a sparse tree according to tags string MATCH.
  7928. MATCH can contain positive and negative selection of tags, like
  7929. \"+WORK+URGENT-WITHBOSS\".
  7930. If optional argument TODO_ONLY is non-nil, only select lines that are
  7931. also TODO lines."
  7932. (interactive "P")
  7933. (org-prepare-agenda-buffers (list (current-buffer)))
  7934. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  7935. (defvar org-cached-props nil)
  7936. (defun org-cached-entry-get (pom property)
  7937. (if (or (eq t org-use-property-inheritance)
  7938. (and (stringp org-use-property-inheritance)
  7939. (string-match org-use-property-inheritance property))
  7940. (and (listp org-use-property-inheritance)
  7941. (member property org-use-property-inheritance)))
  7942. ;; Caching is not possible, check it directly
  7943. (org-entry-get pom property 'inherit)
  7944. ;; Get all properties, so that we can do complicated checks easily
  7945. (cdr (assoc property (or org-cached-props
  7946. (setq org-cached-props
  7947. (org-entry-properties pom)))))))
  7948. (defun org-global-tags-completion-table (&optional files)
  7949. "Return the list of all tags in all agenda buffer/files."
  7950. (save-excursion
  7951. (org-uniquify
  7952. (delq nil
  7953. (apply 'append
  7954. (mapcar
  7955. (lambda (file)
  7956. (set-buffer (find-file-noselect file))
  7957. (append (org-get-buffer-tags)
  7958. (mapcar (lambda (x) (if (stringp (car-safe x))
  7959. (list (car-safe x)) nil))
  7960. org-tag-alist)))
  7961. (if (and files (car files))
  7962. files
  7963. (org-agenda-files))))))))
  7964. (defun org-make-tags-matcher (match)
  7965. "Create the TAGS//TODO matcher form for the selection string MATCH."
  7966. ;; todo-only is scoped dynamically into this function, and the function
  7967. ;; may change it it the matcher asksk for it.
  7968. (unless match
  7969. ;; Get a new match request, with completion
  7970. (let ((org-last-tags-completion-table
  7971. (org-global-tags-completion-table)))
  7972. (setq match (completing-read
  7973. "Match: " 'org-tags-completion-function nil nil nil
  7974. 'org-tags-history))))
  7975. ;; Parse the string and create a lisp form
  7976. (let ((match0 match)
  7977. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  7978. minus tag mm
  7979. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  7980. orterms term orlist re-p str-p level-p level-op
  7981. prop-p pn pv po cat-p gv)
  7982. (if (string-match "/+" match)
  7983. ;; match contains also a todo-matching request
  7984. (progn
  7985. (setq tagsmatch (substring match 0 (match-beginning 0))
  7986. todomatch (substring match (match-end 0)))
  7987. (if (string-match "^!" todomatch)
  7988. (setq todo-only t todomatch (substring todomatch 1)))
  7989. (if (string-match "^\\s-*$" todomatch)
  7990. (setq todomatch nil)))
  7991. ;; only matching tags
  7992. (setq tagsmatch match todomatch nil))
  7993. ;; Make the tags matcher
  7994. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  7995. (setq tagsmatcher t)
  7996. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  7997. (while (setq term (pop orterms))
  7998. (while (and (equal (substring term -1) "\\") orterms)
  7999. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8000. (while (string-match re term)
  8001. (setq minus (and (match-end 1)
  8002. (equal (match-string 1 term) "-"))
  8003. tag (match-string 2 term)
  8004. re-p (equal (string-to-char tag) ?{)
  8005. level-p (match-end 4)
  8006. prop-p (match-end 5)
  8007. mm (cond
  8008. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8009. (level-p
  8010. (setq level-op (org-op-to-function (match-string 3 term)))
  8011. `(,level-op level ,(string-to-number
  8012. (match-string 4 term))))
  8013. (prop-p
  8014. (setq pn (match-string 5 term)
  8015. po (match-string 6 term)
  8016. pv (match-string 7 term)
  8017. cat-p (equal pn "CATEGORY")
  8018. re-p (equal (string-to-char pv) ?{)
  8019. str-p (equal (string-to-char pv) ?\")
  8020. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8021. (setq po (org-op-to-function po str-p))
  8022. (if (equal pn "CATEGORY")
  8023. (setq gv '(get-text-property (point) 'org-category))
  8024. (setq gv `(org-cached-entry-get nil ,pn)))
  8025. (if re-p
  8026. (if (eq po 'org<>)
  8027. `(not (string-match ,pv (or ,gv "")))
  8028. `(string-match ,pv (or ,gv "")))
  8029. (if str-p
  8030. `(,po (or ,gv "") ,pv)
  8031. `(,po (string-to-number (or ,gv ""))
  8032. ,(string-to-number pv) ))))
  8033. (t `(member ,(downcase tag) tags-list)))
  8034. mm (if minus (list 'not mm) mm)
  8035. term (substring term (match-end 0)))
  8036. (push mm tagsmatcher))
  8037. (push (if (> (length tagsmatcher) 1)
  8038. (cons 'and tagsmatcher)
  8039. (car tagsmatcher))
  8040. orlist)
  8041. (setq tagsmatcher nil))
  8042. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8043. (setq tagsmatcher
  8044. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8045. ;; Make the todo matcher
  8046. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8047. (setq todomatcher t)
  8048. (setq orterms (org-split-string todomatch "|") orlist nil)
  8049. (while (setq term (pop orterms))
  8050. (while (string-match re term)
  8051. (setq minus (and (match-end 1)
  8052. (equal (match-string 1 term) "-"))
  8053. kwd (match-string 2 term)
  8054. re-p (equal (string-to-char kwd) ?{)
  8055. term (substring term (match-end 0))
  8056. mm (if re-p
  8057. `(string-match ,(substring kwd 1 -1) todo)
  8058. (list 'equal 'todo kwd))
  8059. mm (if minus (list 'not mm) mm))
  8060. (push mm todomatcher))
  8061. (push (if (> (length todomatcher) 1)
  8062. (cons 'and todomatcher)
  8063. (car todomatcher))
  8064. orlist)
  8065. (setq todomatcher nil))
  8066. (setq todomatcher (if (> (length orlist) 1)
  8067. (cons 'or orlist) (car orlist))))
  8068. ;; Return the string and lisp forms of the matcher
  8069. (setq matcher (if todomatcher
  8070. (list 'and tagsmatcher todomatcher)
  8071. tagsmatcher))
  8072. (cons match0 matcher)))
  8073. (defun org-op-to-function (op &optional stringp)
  8074. (setq op
  8075. (cond
  8076. ((equal op "<" ) '(< string< ))
  8077. ((equal op ">" ) '(> org-string> ))
  8078. ((member op '("<=" "=<")) '(<= org-string<= ))
  8079. ((member op '(">=" "=>")) '(>= org-string>= ))
  8080. ((member op '("=" "==")) '(= string= ))
  8081. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8082. (nth (if stringp 1 0) op))
  8083. (defun org<> (a b) (not (= a b)))
  8084. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8085. (defun org-string>= (a b) (not (string< a b)))
  8086. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8087. (defun org-string<> (a b) (not (string= a b)))
  8088. (defun org-match-any-p (re list)
  8089. "Does re match any element of list?"
  8090. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8091. (delq nil list))
  8092. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8093. (defvar org-tags-overlay (org-make-overlay 1 1))
  8094. (org-detach-overlay org-tags-overlay)
  8095. (defun org-get-tags-at (&optional pos)
  8096. "Get a list of all headline tags applicable at POS.
  8097. POS defaults to point. If tags are inherited, the list contains
  8098. the targets in the same sequence as the headlines appear, i.e.
  8099. sthe tags of the current headline come last."
  8100. (interactive)
  8101. (let (tags ltags lastpos parent)
  8102. (save-excursion
  8103. (save-restriction
  8104. (widen)
  8105. (goto-char (or pos (point)))
  8106. (save-match-data
  8107. (condition-case nil
  8108. (progn
  8109. (org-back-to-heading t)
  8110. (while (not (equal lastpos (point)))
  8111. (setq lastpos (point))
  8112. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8113. (setq ltags (org-split-string
  8114. (org-match-string-no-properties 1) ":"))
  8115. (setq tags (append (org-remove-uniherited-tags ltags)
  8116. tags)))
  8117. (or org-use-tag-inheritance (error ""))
  8118. (org-up-heading-all 1)
  8119. (setq parent t)))
  8120. (error nil))))
  8121. tags)))
  8122. (defun org-toggle-tag (tag &optional onoff)
  8123. "Toggle the tag TAG for the current line.
  8124. If ONOFF is `on' or `off', don't toggle but set to this state."
  8125. (unless (org-on-heading-p t) (error "Not on headling"))
  8126. (let (res current)
  8127. (save-excursion
  8128. (beginning-of-line)
  8129. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8130. (point-at-eol) t)
  8131. (progn
  8132. (setq current (match-string 1))
  8133. (replace-match ""))
  8134. (setq current ""))
  8135. (setq current (nreverse (org-split-string current ":")))
  8136. (cond
  8137. ((eq onoff 'on)
  8138. (setq res t)
  8139. (or (member tag current) (push tag current)))
  8140. ((eq onoff 'off)
  8141. (or (not (member tag current)) (setq current (delete tag current))))
  8142. (t (if (member tag current)
  8143. (setq current (delete tag current))
  8144. (setq res t)
  8145. (push tag current))))
  8146. (end-of-line 1)
  8147. (if current
  8148. (progn
  8149. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8150. (org-set-tags nil t))
  8151. (delete-horizontal-space))
  8152. (run-hooks 'org-after-tags-change-hook))
  8153. res))
  8154. (defun org-align-tags-here (to-col)
  8155. ;; Assumes that this is a headline
  8156. (let ((pos (point)) (col (current-column)) tags)
  8157. (beginning-of-line 1)
  8158. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8159. (< pos (match-beginning 2)))
  8160. (progn
  8161. (setq tags (match-string 2))
  8162. (goto-char (match-beginning 1))
  8163. (insert " ")
  8164. (delete-region (point) (1+ (match-end 0)))
  8165. (backward-char 1)
  8166. (org-move-to-column
  8167. (max (1+ (current-column))
  8168. (1+ col)
  8169. (if (> to-col 0)
  8170. to-col
  8171. (- (abs to-col) (length tags))))
  8172. t)
  8173. (insert tags)
  8174. (org-move-to-column (min (current-column) col) t))
  8175. (goto-char pos))))
  8176. (defun org-set-tags (&optional arg just-align)
  8177. "Set the tags for the current headline.
  8178. With prefix ARG, realign all tags in headings in the current buffer."
  8179. (interactive "P")
  8180. (let* ((re (concat "^" outline-regexp))
  8181. (current (org-get-tags-string))
  8182. (col (current-column))
  8183. (org-setting-tags t)
  8184. table current-tags inherited-tags ; computed below when needed
  8185. tags p0 c0 c1 rpl)
  8186. (if arg
  8187. (save-excursion
  8188. (goto-char (point-min))
  8189. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8190. (while (re-search-forward re nil t)
  8191. (org-set-tags nil t)
  8192. (end-of-line 1)))
  8193. (message "All tags realigned to column %d" org-tags-column))
  8194. (if just-align
  8195. (setq tags current)
  8196. ;; Get a new set of tags from the user
  8197. (save-excursion
  8198. (setq table (or org-tag-alist (org-get-buffer-tags))
  8199. org-last-tags-completion-table table
  8200. current-tags (org-split-string current ":")
  8201. inherited-tags (nreverse
  8202. (nthcdr (length current-tags)
  8203. (nreverse (org-get-tags-at))))
  8204. tags
  8205. (if (or (eq t org-use-fast-tag-selection)
  8206. (and org-use-fast-tag-selection
  8207. (delq nil (mapcar 'cdr table))))
  8208. (org-fast-tag-selection
  8209. current-tags inherited-tags table
  8210. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8211. (let ((org-add-colon-after-tag-completion t))
  8212. (org-trim
  8213. (org-without-partial-completion
  8214. (completing-read "Tags: " 'org-tags-completion-function
  8215. nil nil current 'org-tags-history)))))))
  8216. (while (string-match "[-+&]+" tags)
  8217. ;; No boolean logic, just a list
  8218. (setq tags (replace-match ":" t t tags))))
  8219. (if (string-match "\\`[\t ]*\\'" tags)
  8220. (setq tags "")
  8221. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8222. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8223. ;; Insert new tags at the correct column
  8224. (beginning-of-line 1)
  8225. (cond
  8226. ((and (equal current "") (equal tags "")))
  8227. ((re-search-forward
  8228. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8229. (point-at-eol) t)
  8230. (if (equal tags "")
  8231. (setq rpl "")
  8232. (goto-char (match-beginning 0))
  8233. (setq c0 (current-column) p0 (point)
  8234. c1 (max (1+ c0) (if (> org-tags-column 0)
  8235. org-tags-column
  8236. (- (- org-tags-column) (length tags))))
  8237. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8238. (replace-match rpl t t)
  8239. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8240. tags)
  8241. (t (error "Tags alignment failed")))
  8242. (org-move-to-column col)
  8243. (unless just-align
  8244. (run-hooks 'org-after-tags-change-hook)))))
  8245. (defun org-change-tag-in-region (beg end tag off)
  8246. "Add or remove TAG for each entry in the region.
  8247. This works in the agenda, and also in an org-mode buffer."
  8248. (interactive
  8249. (list (region-beginning) (region-end)
  8250. (let ((org-last-tags-completion-table
  8251. (if (org-mode-p)
  8252. (org-get-buffer-tags)
  8253. (org-global-tags-completion-table))))
  8254. (completing-read
  8255. "Tag: " 'org-tags-completion-function nil nil nil
  8256. 'org-tags-history))
  8257. (progn
  8258. (message "[s]et or [r]emove? ")
  8259. (equal (read-char-exclusive) ?r))))
  8260. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8261. (let ((agendap (equal major-mode 'org-agenda-mode))
  8262. l1 l2 m buf pos newhead (cnt 0))
  8263. (goto-char end)
  8264. (setq l2 (1- (org-current-line)))
  8265. (goto-char beg)
  8266. (setq l1 (org-current-line))
  8267. (loop for l from l1 to l2 do
  8268. (goto-line l)
  8269. (setq m (get-text-property (point) 'org-hd-marker))
  8270. (when (or (and (org-mode-p) (org-on-heading-p))
  8271. (and agendap m))
  8272. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8273. pos (if agendap m (point)))
  8274. (with-current-buffer buf
  8275. (save-excursion
  8276. (save-restriction
  8277. (goto-char pos)
  8278. (setq cnt (1+ cnt))
  8279. (org-toggle-tag tag (if off 'off 'on))
  8280. (setq newhead (org-get-heading)))))
  8281. (and agendap (org-agenda-change-all-lines newhead m))))
  8282. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8283. (defun org-tags-completion-function (string predicate &optional flag)
  8284. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8285. (confirm (lambda (x) (stringp (car x)))))
  8286. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8287. (setq s1 (match-string 1 string)
  8288. s2 (match-string 2 string))
  8289. (setq s1 "" s2 string))
  8290. (cond
  8291. ((eq flag nil)
  8292. ;; try completion
  8293. (setq rtn (try-completion s2 ctable confirm))
  8294. (if (stringp rtn)
  8295. (setq rtn
  8296. (concat s1 s2 (substring rtn (length s2))
  8297. (if (and org-add-colon-after-tag-completion
  8298. (assoc rtn ctable))
  8299. ":" ""))))
  8300. rtn)
  8301. ((eq flag t)
  8302. ;; all-completions
  8303. (all-completions s2 ctable confirm)
  8304. )
  8305. ((eq flag 'lambda)
  8306. ;; exact match?
  8307. (assoc s2 ctable)))
  8308. ))
  8309. (defun org-fast-tag-insert (kwd tags face &optional end)
  8310. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8311. (insert (format "%-12s" (concat kwd ":"))
  8312. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8313. (or end "")))
  8314. (defun org-fast-tag-show-exit (flag)
  8315. (save-excursion
  8316. (goto-line 3)
  8317. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8318. (replace-match ""))
  8319. (when flag
  8320. (end-of-line 1)
  8321. (org-move-to-column (- (window-width) 19) t)
  8322. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8323. (defun org-set-current-tags-overlay (current prefix)
  8324. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8325. (if (featurep 'xemacs)
  8326. (org-overlay-display org-tags-overlay (concat prefix s)
  8327. 'secondary-selection)
  8328. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8329. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8330. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8331. "Fast tag selection with single keys.
  8332. CURRENT is the current list of tags in the headline, INHERITED is the
  8333. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8334. possibly with grouping information. TODO-TABLE is a similar table with
  8335. TODO keywords, should these have keys assigned to them.
  8336. If the keys are nil, a-z are automatically assigned.
  8337. Returns the new tags string, or nil to not change the current settings."
  8338. (let* ((fulltable (append table todo-table))
  8339. (maxlen (apply 'max (mapcar
  8340. (lambda (x)
  8341. (if (stringp (car x)) (string-width (car x)) 0))
  8342. fulltable)))
  8343. (buf (current-buffer))
  8344. (expert (eq org-fast-tag-selection-single-key 'expert))
  8345. (buffer-tags nil)
  8346. (fwidth (+ maxlen 3 1 3))
  8347. (ncol (/ (- (window-width) 4) fwidth))
  8348. (i-face 'org-done)
  8349. (c-face 'org-todo)
  8350. tg cnt e c char c1 c2 ntable tbl rtn
  8351. ov-start ov-end ov-prefix
  8352. (exit-after-next org-fast-tag-selection-single-key)
  8353. (done-keywords org-done-keywords)
  8354. groups ingroup)
  8355. (save-excursion
  8356. (beginning-of-line 1)
  8357. (if (looking-at
  8358. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8359. (setq ov-start (match-beginning 1)
  8360. ov-end (match-end 1)
  8361. ov-prefix "")
  8362. (setq ov-start (1- (point-at-eol))
  8363. ov-end (1+ ov-start))
  8364. (skip-chars-forward "^\n\r")
  8365. (setq ov-prefix
  8366. (concat
  8367. (buffer-substring (1- (point)) (point))
  8368. (if (> (current-column) org-tags-column)
  8369. " "
  8370. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8371. (org-move-overlay org-tags-overlay ov-start ov-end)
  8372. (save-window-excursion
  8373. (if expert
  8374. (set-buffer (get-buffer-create " *Org tags*"))
  8375. (delete-other-windows)
  8376. (split-window-vertically)
  8377. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8378. (erase-buffer)
  8379. (org-set-local 'org-done-keywords done-keywords)
  8380. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8381. (org-fast-tag-insert "Current" current c-face "\n\n")
  8382. (org-fast-tag-show-exit exit-after-next)
  8383. (org-set-current-tags-overlay current ov-prefix)
  8384. (setq tbl fulltable char ?a cnt 0)
  8385. (while (setq e (pop tbl))
  8386. (cond
  8387. ((equal e '(:startgroup))
  8388. (push '() groups) (setq ingroup t)
  8389. (when (not (= cnt 0))
  8390. (setq cnt 0)
  8391. (insert "\n"))
  8392. (insert "{ "))
  8393. ((equal e '(:endgroup))
  8394. (setq ingroup nil cnt 0)
  8395. (insert "}\n"))
  8396. (t
  8397. (setq tg (car e) c2 nil)
  8398. (if (cdr e)
  8399. (setq c (cdr e))
  8400. ;; automatically assign a character.
  8401. (setq c1 (string-to-char
  8402. (downcase (substring
  8403. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8404. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8405. (while (or (rassoc char ntable) (rassoc char table))
  8406. (setq char (1+ char)))
  8407. (setq c2 c1))
  8408. (setq c (or c2 char)))
  8409. (if ingroup (push tg (car groups)))
  8410. (setq tg (org-add-props tg nil 'face
  8411. (cond
  8412. ((not (assoc tg table))
  8413. (org-get-todo-face tg))
  8414. ((member tg current) c-face)
  8415. ((member tg inherited) i-face)
  8416. (t nil))))
  8417. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8418. (insert "[" c "] " tg (make-string
  8419. (- fwidth 4 (length tg)) ?\ ))
  8420. (push (cons tg c) ntable)
  8421. (when (= (setq cnt (1+ cnt)) ncol)
  8422. (insert "\n")
  8423. (if ingroup (insert " "))
  8424. (setq cnt 0)))))
  8425. (setq ntable (nreverse ntable))
  8426. (insert "\n")
  8427. (goto-char (point-min))
  8428. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8429. (fit-window-to-buffer))
  8430. (setq rtn
  8431. (catch 'exit
  8432. (while t
  8433. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8434. (if groups " [!] no groups" " [!]groups")
  8435. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8436. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8437. (cond
  8438. ((= c ?\r) (throw 'exit t))
  8439. ((= c ?!)
  8440. (setq groups (not groups))
  8441. (goto-char (point-min))
  8442. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8443. ((= c ?\C-c)
  8444. (if (not expert)
  8445. (org-fast-tag-show-exit
  8446. (setq exit-after-next (not exit-after-next)))
  8447. (setq expert nil)
  8448. (delete-other-windows)
  8449. (split-window-vertically)
  8450. (org-switch-to-buffer-other-window " *Org tags*")
  8451. (and (fboundp 'fit-window-to-buffer)
  8452. (fit-window-to-buffer))))
  8453. ((or (= c ?\C-g)
  8454. (and (= c ?q) (not (rassoc c ntable))))
  8455. (org-detach-overlay org-tags-overlay)
  8456. (setq quit-flag t))
  8457. ((= c ?\ )
  8458. (setq current nil)
  8459. (if exit-after-next (setq exit-after-next 'now)))
  8460. ((= c ?\t)
  8461. (condition-case nil
  8462. (setq tg (completing-read
  8463. "Tag: "
  8464. (or buffer-tags
  8465. (with-current-buffer buf
  8466. (org-get-buffer-tags)))))
  8467. (quit (setq tg "")))
  8468. (when (string-match "\\S-" tg)
  8469. (add-to-list 'buffer-tags (list tg))
  8470. (if (member tg current)
  8471. (setq current (delete tg current))
  8472. (push tg current)))
  8473. (if exit-after-next (setq exit-after-next 'now)))
  8474. ((setq e (rassoc c todo-table) tg (car e))
  8475. (with-current-buffer buf
  8476. (save-excursion (org-todo tg)))
  8477. (if exit-after-next (setq exit-after-next 'now)))
  8478. ((setq e (rassoc c ntable) tg (car e))
  8479. (if (member tg current)
  8480. (setq current (delete tg current))
  8481. (loop for g in groups do
  8482. (if (member tg g)
  8483. (mapc (lambda (x)
  8484. (setq current (delete x current)))
  8485. g)))
  8486. (push tg current))
  8487. (if exit-after-next (setq exit-after-next 'now))))
  8488. ;; Create a sorted list
  8489. (setq current
  8490. (sort current
  8491. (lambda (a b)
  8492. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8493. (if (eq exit-after-next 'now) (throw 'exit t))
  8494. (goto-char (point-min))
  8495. (beginning-of-line 2)
  8496. (delete-region (point) (point-at-eol))
  8497. (org-fast-tag-insert "Current" current c-face)
  8498. (org-set-current-tags-overlay current ov-prefix)
  8499. (while (re-search-forward
  8500. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8501. (setq tg (match-string 1))
  8502. (add-text-properties
  8503. (match-beginning 1) (match-end 1)
  8504. (list 'face
  8505. (cond
  8506. ((member tg current) c-face)
  8507. ((member tg inherited) i-face)
  8508. (t (get-text-property (match-beginning 1) 'face))))))
  8509. (goto-char (point-min)))))
  8510. (org-detach-overlay org-tags-overlay)
  8511. (if rtn
  8512. (mapconcat 'identity current ":")
  8513. nil))))
  8514. (defun org-get-tags-string ()
  8515. "Get the TAGS string in the current headline."
  8516. (unless (org-on-heading-p t)
  8517. (error "Not on a heading"))
  8518. (save-excursion
  8519. (beginning-of-line 1)
  8520. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8521. (org-match-string-no-properties 1)
  8522. "")))
  8523. (defun org-get-tags ()
  8524. "Get the list of tags specified in the current headline."
  8525. (org-split-string (org-get-tags-string) ":"))
  8526. (defun org-get-buffer-tags ()
  8527. "Get a table of all tags used in the buffer, for completion."
  8528. (let (tags)
  8529. (save-excursion
  8530. (goto-char (point-min))
  8531. (while (re-search-forward
  8532. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8533. (when (equal (char-after (point-at-bol 0)) ?*)
  8534. (mapc (lambda (x) (add-to-list 'tags x))
  8535. (org-split-string (org-match-string-no-properties 1) ":")))))
  8536. (mapcar 'list tags)))
  8537. ;;;; Properties
  8538. ;;; Setting and retrieving properties
  8539. (defconst org-special-properties
  8540. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8541. "TIMESTAMP" "TIMESTAMP_IA")
  8542. "The special properties valid in Org-mode.
  8543. These are properties that are not defined in the property drawer,
  8544. but in some other way.")
  8545. (defconst org-default-properties
  8546. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8547. "LOCATION" "LOGGING" "COLUMNS")
  8548. "Some properties that are used by Org-mode for various purposes.
  8549. Being in this list makes sure that they are offered for completion.")
  8550. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8551. "Regular expression matching the first line of a property drawer.")
  8552. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8553. "Regular expression matching the first line of a property drawer.")
  8554. (defun org-property-action ()
  8555. "Do an action on properties."
  8556. (interactive)
  8557. (let (c)
  8558. (org-at-property-p)
  8559. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8560. (setq c (read-char-exclusive))
  8561. (cond
  8562. ((equal c ?s)
  8563. (call-interactively 'org-set-property))
  8564. ((equal c ?d)
  8565. (call-interactively 'org-delete-property))
  8566. ((equal c ?D)
  8567. (call-interactively 'org-delete-property-globally))
  8568. ((equal c ?c)
  8569. (call-interactively 'org-compute-property-at-point))
  8570. (t (error "No such property action %c" c)))))
  8571. (defun org-at-property-p ()
  8572. "Is the cursor in a property line?"
  8573. ;; FIXME: Does not check if we are actually in the drawer.
  8574. ;; FIXME: also returns true on any drawers.....
  8575. ;; This is used by C-c C-c for property action.
  8576. (save-excursion
  8577. (beginning-of-line 1)
  8578. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8579. (defun org-get-property-block (&optional beg end force)
  8580. "Return the (beg . end) range of the body of the property drawer.
  8581. BEG and END can be beginning and end of subtree, if not given
  8582. they will be found.
  8583. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8584. (catch 'exit
  8585. (save-excursion
  8586. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8587. (end (or end (progn (outline-next-heading) (point)))))
  8588. (goto-char beg)
  8589. (if (re-search-forward org-property-start-re end t)
  8590. (setq beg (1+ (match-end 0)))
  8591. (if force
  8592. (save-excursion
  8593. (org-insert-property-drawer)
  8594. (setq end (progn (outline-next-heading) (point))))
  8595. (throw 'exit nil))
  8596. (goto-char beg)
  8597. (if (re-search-forward org-property-start-re end t)
  8598. (setq beg (1+ (match-end 0)))))
  8599. (if (re-search-forward org-property-end-re end t)
  8600. (setq end (match-beginning 0))
  8601. (or force (throw 'exit nil))
  8602. (goto-char beg)
  8603. (setq end beg)
  8604. (org-indent-line-function)
  8605. (insert ":END:\n"))
  8606. (cons beg end)))))
  8607. (defun org-entry-properties (&optional pom which)
  8608. "Get all properties of the entry at point-or-marker POM.
  8609. This includes the TODO keyword, the tags, time strings for deadline,
  8610. scheduled, and clocking, and any additional properties defined in the
  8611. entry. The return value is an alist, keys may occur multiple times
  8612. if the property key was used several times.
  8613. POM may also be nil, in which case the current entry is used.
  8614. If WHICH is nil or `all', get all properties. If WHICH is
  8615. `special' or `standard', only get that subclass."
  8616. (setq which (or which 'all))
  8617. (org-with-point-at pom
  8618. (let ((clockstr (substring org-clock-string 0 -1))
  8619. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8620. beg end range props sum-props key value string clocksum)
  8621. (save-excursion
  8622. (when (condition-case nil (org-back-to-heading t) (error nil))
  8623. (setq beg (point))
  8624. (setq sum-props (get-text-property (point) 'org-summaries))
  8625. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8626. (outline-next-heading)
  8627. (setq end (point))
  8628. (when (memq which '(all special))
  8629. ;; Get the special properties, like TODO and tags
  8630. (goto-char beg)
  8631. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8632. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8633. (when (looking-at org-priority-regexp)
  8634. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8635. (when (and (setq value (org-get-tags-string))
  8636. (string-match "\\S-" value))
  8637. (push (cons "TAGS" value) props))
  8638. (when (setq value (org-get-tags-at))
  8639. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8640. props))
  8641. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8642. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8643. string (if (equal key clockstr)
  8644. (org-no-properties
  8645. (org-trim
  8646. (buffer-substring
  8647. (match-beginning 3) (goto-char (point-at-eol)))))
  8648. (substring (org-match-string-no-properties 3) 1 -1)))
  8649. (unless key
  8650. (if (= (char-after (match-beginning 3)) ?\[)
  8651. (setq key "TIMESTAMP_IA")
  8652. (setq key "TIMESTAMP")))
  8653. (when (or (equal key clockstr) (not (assoc key props)))
  8654. (push (cons key string) props)))
  8655. )
  8656. (when (memq which '(all standard))
  8657. ;; Get the standard properties, like :PORP: ...
  8658. (setq range (org-get-property-block beg end))
  8659. (when range
  8660. (goto-char (car range))
  8661. (while (re-search-forward
  8662. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8663. (cdr range) t)
  8664. (setq key (org-match-string-no-properties 1)
  8665. value (org-trim (or (org-match-string-no-properties 2) "")))
  8666. (unless (member key excluded)
  8667. (push (cons key (or value "")) props)))))
  8668. (if clocksum
  8669. (push (cons "CLOCKSUM"
  8670. (org-columns-number-to-string (/ (float clocksum) 60.)
  8671. 'add_times))
  8672. props))
  8673. (append sum-props (nreverse props)))))))
  8674. (defun org-entry-get (pom property &optional inherit)
  8675. "Get value of PROPERTY for entry at point-or-marker POM.
  8676. If INHERIT is non-nil and the entry does not have the property,
  8677. then also check higher levels of the hierarchy.
  8678. If INHERIT is the symbol `selective', use inheritance only if the setting
  8679. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8680. If the property is present but empty, the return value is the empty string.
  8681. If the property is not present at all, nil is returned."
  8682. (org-with-point-at pom
  8683. (if (and inherit (if (eq inherit 'selective)
  8684. (org-property-inherit-p property)
  8685. t))
  8686. (org-entry-get-with-inheritance property)
  8687. (if (member property org-special-properties)
  8688. ;; We need a special property. Use brute force, get all properties.
  8689. (cdr (assoc property (org-entry-properties nil 'special)))
  8690. (let ((range (org-get-property-block)))
  8691. (if (and range
  8692. (goto-char (car range))
  8693. (re-search-forward
  8694. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8695. (cdr range) t))
  8696. ;; Found the property, return it.
  8697. (if (match-end 1)
  8698. (org-match-string-no-properties 1)
  8699. "")))))))
  8700. (defun org-property-or-variable-value (var &optional inherit)
  8701. "Check if there is a property fixing the value of VAR.
  8702. If yes, return this value. If not, return the current value of the variable."
  8703. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8704. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8705. (read prop)
  8706. (symbol-value var))))
  8707. (defun org-entry-delete (pom property)
  8708. "Delete the property PROPERTY from entry at point-or-marker POM."
  8709. (org-with-point-at pom
  8710. (if (member property org-special-properties)
  8711. nil ; cannot delete these properties.
  8712. (let ((range (org-get-property-block)))
  8713. (if (and range
  8714. (goto-char (car range))
  8715. (re-search-forward
  8716. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8717. (cdr range) t))
  8718. (progn
  8719. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8720. t)
  8721. nil)))))
  8722. ;; Multi-values properties are properties that contain multiple values
  8723. ;; These values are assumed to be single words, separated by whitespace.
  8724. (defun org-entry-add-to-multivalued-property (pom property value)
  8725. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8726. (let* ((old (org-entry-get pom property))
  8727. (values (and old (org-split-string old "[ \t]"))))
  8728. (unless (member value values)
  8729. (setq values (cons value values))
  8730. (org-entry-put pom property
  8731. (mapconcat 'identity values " ")))))
  8732. (defun org-entry-remove-from-multivalued-property (pom property value)
  8733. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8734. (let* ((old (org-entry-get pom property))
  8735. (values (and old (org-split-string old "[ \t]"))))
  8736. (when (member value values)
  8737. (setq values (delete value values))
  8738. (org-entry-put pom property
  8739. (mapconcat 'identity values " ")))))
  8740. (defun org-entry-member-in-multivalued-property (pom property value)
  8741. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8742. (let* ((old (org-entry-get pom property))
  8743. (values (and old (org-split-string old "[ \t]"))))
  8744. (member value values)))
  8745. (defvar org-entry-property-inherited-from (make-marker))
  8746. (defun org-entry-get-with-inheritance (property)
  8747. "Get entry property, and search higher levels if not present."
  8748. (let (tmp)
  8749. (save-excursion
  8750. (save-restriction
  8751. (widen)
  8752. (catch 'ex
  8753. (while t
  8754. (when (setq tmp (org-entry-get nil property))
  8755. (org-back-to-heading t)
  8756. (move-marker org-entry-property-inherited-from (point))
  8757. (throw 'ex tmp))
  8758. (or (org-up-heading-safe) (throw 'ex nil)))))
  8759. (or tmp (cdr (assoc property org-local-properties))
  8760. (cdr (assoc property org-global-properties))))))
  8761. (defun org-entry-put (pom property value)
  8762. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8763. (org-with-point-at pom
  8764. (org-back-to-heading t)
  8765. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8766. range)
  8767. (cond
  8768. ((equal property "TODO")
  8769. (when (and (stringp value) (string-match "\\S-" value)
  8770. (not (member value org-todo-keywords-1)))
  8771. (error "\"%s\" is not a valid TODO state" value))
  8772. (if (or (not value)
  8773. (not (string-match "\\S-" value)))
  8774. (setq value 'none))
  8775. (org-todo value)
  8776. (org-set-tags nil 'align))
  8777. ((equal property "PRIORITY")
  8778. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8779. (string-to-char value) ?\ ))
  8780. (org-set-tags nil 'align))
  8781. ((equal property "SCHEDULED")
  8782. (if (re-search-forward org-scheduled-time-regexp end t)
  8783. (cond
  8784. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8785. ((eq value 'later) (org-timestamp-change 1 'day))
  8786. (t (call-interactively 'org-schedule)))
  8787. (call-interactively 'org-schedule)))
  8788. ((equal property "DEADLINE")
  8789. (if (re-search-forward org-deadline-time-regexp end t)
  8790. (cond
  8791. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8792. ((eq value 'later) (org-timestamp-change 1 'day))
  8793. (t (call-interactively 'org-deadline)))
  8794. (call-interactively 'org-deadline)))
  8795. ((member property org-special-properties)
  8796. (error "The %s property can not yet be set with `org-entry-put'"
  8797. property))
  8798. (t ; a non-special property
  8799. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  8800. (setq range (org-get-property-block beg end 'force))
  8801. (goto-char (car range))
  8802. (if (re-search-forward
  8803. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  8804. (progn
  8805. (delete-region (match-beginning 1) (match-end 1))
  8806. (goto-char (match-beginning 1)))
  8807. (goto-char (cdr range))
  8808. (insert "\n")
  8809. (backward-char 1)
  8810. (org-indent-line-function)
  8811. (insert ":" property ":"))
  8812. (and value (insert " " value))
  8813. (org-indent-line-function)))))))
  8814. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  8815. "Get all property keys in the current buffer.
  8816. With INCLUDE-SPECIALS, also list the special properties that relect things
  8817. like tags and TODO state.
  8818. With INCLUDE-DEFAULTS, also include properties that has special meaning
  8819. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  8820. With INCLUDE-COLUMNS, also include property names given in COLUMN
  8821. formats in the current buffer."
  8822. (let (rtn range cfmt cols s p)
  8823. (save-excursion
  8824. (save-restriction
  8825. (widen)
  8826. (goto-char (point-min))
  8827. (while (re-search-forward org-property-start-re nil t)
  8828. (setq range (org-get-property-block))
  8829. (goto-char (car range))
  8830. (while (re-search-forward
  8831. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  8832. (cdr range) t)
  8833. (add-to-list 'rtn (org-match-string-no-properties 1)))
  8834. (outline-next-heading))))
  8835. (when include-specials
  8836. (setq rtn (append org-special-properties rtn)))
  8837. (when include-defaults
  8838. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  8839. (when include-columns
  8840. (save-excursion
  8841. (save-restriction
  8842. (widen)
  8843. (goto-char (point-min))
  8844. (while (re-search-forward
  8845. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  8846. nil t)
  8847. (setq cfmt (match-string 2) s 0)
  8848. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  8849. cfmt s)
  8850. (setq s (match-end 0)
  8851. p (match-string 1 cfmt))
  8852. (unless (or (equal p "ITEM")
  8853. (member p org-special-properties))
  8854. (add-to-list 'rtn (match-string 1 cfmt))))))))
  8855. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  8856. (defun org-property-values (key)
  8857. "Return a list of all values of property KEY."
  8858. (save-excursion
  8859. (save-restriction
  8860. (widen)
  8861. (goto-char (point-min))
  8862. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  8863. values)
  8864. (while (re-search-forward re nil t)
  8865. (add-to-list 'values (org-trim (match-string 1))))
  8866. (delete "" values)))))
  8867. (defun org-insert-property-drawer ()
  8868. "Insert a property drawer into the current entry."
  8869. (interactive)
  8870. (org-back-to-heading t)
  8871. (looking-at outline-regexp)
  8872. (let ((indent (- (match-end 0)(match-beginning 0)))
  8873. (beg (point))
  8874. (re (concat "^[ \t]*" org-keyword-time-regexp))
  8875. end hiddenp)
  8876. (outline-next-heading)
  8877. (setq end (point))
  8878. (goto-char beg)
  8879. (while (re-search-forward re end t))
  8880. (setq hiddenp (org-invisible-p))
  8881. (end-of-line 1)
  8882. (and (equal (char-after) ?\n) (forward-char 1))
  8883. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  8884. (beginning-of-line 2))
  8885. (org-skip-over-state-notes)
  8886. (skip-chars-backward " \t\n\r")
  8887. (if (eq (char-before) ?*) (forward-char 1))
  8888. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  8889. (beginning-of-line 0)
  8890. (org-indent-to-column indent)
  8891. (beginning-of-line 2)
  8892. (org-indent-to-column indent)
  8893. (beginning-of-line 0)
  8894. (if hiddenp
  8895. (save-excursion
  8896. (org-back-to-heading t)
  8897. (hide-entry))
  8898. (org-flag-drawer t))))
  8899. (defun org-set-property (property value)
  8900. "In the current entry, set PROPERTY to VALUE.
  8901. When called interactively, this will prompt for a property name, offering
  8902. completion on existing and default properties. And then it will prompt
  8903. for a value, offering competion either on allowed values (via an inherited
  8904. xxx_ALL property) or on existing values in other instances of this property
  8905. in the current file."
  8906. (interactive
  8907. (let* ((prop (completing-read
  8908. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  8909. (cur (org-entry-get nil prop))
  8910. (allowed (org-property-get-allowed-values nil prop 'table))
  8911. (existing (mapcar 'list (org-property-values prop)))
  8912. (val (if allowed
  8913. (completing-read "Value: " allowed nil 'req-match)
  8914. (completing-read
  8915. (concat "Value" (if (and cur (string-match "\\S-" cur))
  8916. (concat "[" cur "]") "")
  8917. ": ")
  8918. existing nil nil "" nil cur))))
  8919. (list prop (if (equal val "") cur val))))
  8920. (unless (equal (org-entry-get nil property) value)
  8921. (org-entry-put nil property value)))
  8922. (defun org-delete-property (property)
  8923. "In the current entry, delete PROPERTY."
  8924. (interactive
  8925. (let* ((prop (completing-read
  8926. "Property: " (org-entry-properties nil 'standard))))
  8927. (list prop)))
  8928. (message "Property %s %s" property
  8929. (if (org-entry-delete nil property)
  8930. "deleted"
  8931. "was not present in the entry")))
  8932. (defun org-delete-property-globally (property)
  8933. "Remove PROPERTY globally, from all entries."
  8934. (interactive
  8935. (let* ((prop (completing-read
  8936. "Globally remove property: "
  8937. (mapcar 'list (org-buffer-property-keys)))))
  8938. (list prop)))
  8939. (save-excursion
  8940. (save-restriction
  8941. (widen)
  8942. (goto-char (point-min))
  8943. (let ((cnt 0))
  8944. (while (re-search-forward
  8945. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  8946. nil t)
  8947. (setq cnt (1+ cnt))
  8948. (replace-match ""))
  8949. (message "Property \"%s\" removed from %d entries" property cnt)))))
  8950. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  8951. (defun org-compute-property-at-point ()
  8952. "Compute the property at point.
  8953. This looks for an enclosing column format, extracts the operator and
  8954. then applies it to the proerty in the column format's scope."
  8955. (interactive)
  8956. (unless (org-at-property-p)
  8957. (error "Not at a property"))
  8958. (let ((prop (org-match-string-no-properties 2)))
  8959. (org-columns-get-format-and-top-level)
  8960. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  8961. (error "No operator defined for property %s" prop))
  8962. (org-columns-compute prop)))
  8963. (defun org-property-get-allowed-values (pom property &optional table)
  8964. "Get allowed values for the property PROPERTY.
  8965. When TABLE is non-nil, return an alist that can directly be used for
  8966. completion."
  8967. (let (vals)
  8968. (cond
  8969. ((equal property "TODO")
  8970. (setq vals (org-with-point-at pom
  8971. (append org-todo-keywords-1 '("")))))
  8972. ((equal property "PRIORITY")
  8973. (let ((n org-lowest-priority))
  8974. (while (>= n org-highest-priority)
  8975. (push (char-to-string n) vals)
  8976. (setq n (1- n)))))
  8977. ((member property org-special-properties))
  8978. (t
  8979. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  8980. (when (and vals (string-match "\\S-" vals))
  8981. (setq vals (car (read-from-string (concat "(" vals ")"))))
  8982. (setq vals (mapcar (lambda (x)
  8983. (cond ((stringp x) x)
  8984. ((numberp x) (number-to-string x))
  8985. ((symbolp x) (symbol-name x))
  8986. (t "???")))
  8987. vals)))))
  8988. (if table (mapcar 'list vals) vals)))
  8989. (defun org-property-previous-allowed-value (&optional previous)
  8990. "Switch to the next allowed value for this property."
  8991. (interactive)
  8992. (org-property-next-allowed-value t))
  8993. (defun org-property-next-allowed-value (&optional previous)
  8994. "Switch to the next allowed value for this property."
  8995. (interactive)
  8996. (unless (org-at-property-p)
  8997. (error "Not at a property"))
  8998. (let* ((key (match-string 2))
  8999. (value (match-string 3))
  9000. (allowed (or (org-property-get-allowed-values (point) key)
  9001. (and (member value '("[ ]" "[-]" "[X]"))
  9002. '("[ ]" "[X]"))))
  9003. nval)
  9004. (unless allowed
  9005. (error "Allowed values for this property have not been defined"))
  9006. (if previous (setq allowed (reverse allowed)))
  9007. (if (member value allowed)
  9008. (setq nval (car (cdr (member value allowed)))))
  9009. (setq nval (or nval (car allowed)))
  9010. (if (equal nval value)
  9011. (error "Only one allowed value for this property"))
  9012. (org-at-property-p)
  9013. (replace-match (concat " :" key ": " nval) t t)
  9014. (org-indent-line-function)
  9015. (beginning-of-line 1)
  9016. (skip-chars-forward " \t")))
  9017. (defun org-find-entry-with-id (ident)
  9018. "Locate the entry that contains the ID property with exact value IDENT.
  9019. IDENT can be a string, a symbol or a number, this function will search for
  9020. the string representation of it.
  9021. Return the position where this entry starts, or nil if there is no such entry."
  9022. (let ((id (cond
  9023. ((stringp ident) ident)
  9024. ((symbol-name ident) (symbol-name ident))
  9025. ((numberp ident) (number-to-string ident))
  9026. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9027. (case-fold-search nil))
  9028. (save-excursion
  9029. (save-restriction
  9030. (widen)
  9031. (goto-char (point-min))
  9032. (when (re-search-forward
  9033. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9034. nil t)
  9035. (org-back-to-heading)
  9036. (point))))))
  9037. ;;;; Timestamps
  9038. (defvar org-last-changed-timestamp nil)
  9039. (defvar org-time-was-given) ; dynamically scoped parameter
  9040. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9041. (defvar org-ts-what) ; dynamically scoped parameter
  9042. (defun org-time-stamp (arg)
  9043. "Prompt for a date/time and insert a time stamp.
  9044. If the user specifies a time like HH:MM, or if this command is called
  9045. with a prefix argument, the time stamp will contain date and time.
  9046. Otherwise, only the date will be included. All parts of a date not
  9047. specified by the user will be filled in from the current date/time.
  9048. So if you press just return without typing anything, the time stamp
  9049. will represent the current date/time. If there is already a timestamp
  9050. at the cursor, it will be modified."
  9051. (interactive "P")
  9052. (let* ((ts nil)
  9053. (default-time
  9054. ;; Default time is either today, or, when entering a range,
  9055. ;; the range start.
  9056. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9057. (save-excursion
  9058. (re-search-backward
  9059. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9060. (- (point) 20) t)))
  9061. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9062. (current-time)))
  9063. (default-input (and ts (org-get-compact-tod ts)))
  9064. org-time-was-given org-end-time-was-given time)
  9065. (cond
  9066. ((and (org-at-timestamp-p)
  9067. (eq last-command 'org-time-stamp)
  9068. (eq this-command 'org-time-stamp))
  9069. (insert "--")
  9070. (setq time (let ((this-command this-command))
  9071. (org-read-date arg 'totime nil nil default-time default-input)))
  9072. (org-insert-time-stamp time (or org-time-was-given arg)))
  9073. ((org-at-timestamp-p)
  9074. (setq time (let ((this-command this-command))
  9075. (org-read-date arg 'totime nil nil default-time default-input)))
  9076. (when (org-at-timestamp-p) ; just to get the match data
  9077. (replace-match "")
  9078. (setq org-last-changed-timestamp
  9079. (org-insert-time-stamp
  9080. time (or org-time-was-given arg)
  9081. nil nil nil (list org-end-time-was-given))))
  9082. (message "Timestamp updated"))
  9083. (t
  9084. (setq time (let ((this-command this-command))
  9085. (org-read-date arg 'totime nil nil default-time default-input)))
  9086. (org-insert-time-stamp time (or org-time-was-given arg)
  9087. nil nil nil (list org-end-time-was-given))))))
  9088. ;; FIXME: can we use this for something else, like computing time differences?
  9089. (defun org-get-compact-tod (s)
  9090. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9091. (let* ((t1 (match-string 1 s))
  9092. (h1 (string-to-number (match-string 2 s)))
  9093. (m1 (string-to-number (match-string 3 s)))
  9094. (t2 (and (match-end 4) (match-string 5 s)))
  9095. (h2 (and t2 (string-to-number (match-string 6 s))))
  9096. (m2 (and t2 (string-to-number (match-string 7 s))))
  9097. dh dm)
  9098. (if (not t2)
  9099. t1
  9100. (setq dh (- h2 h1) dm (- m2 m1))
  9101. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9102. (concat t1 "+" (number-to-string dh)
  9103. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9104. (defun org-time-stamp-inactive (&optional arg)
  9105. "Insert an inactive time stamp.
  9106. An inactive time stamp is enclosed in square brackets instead of angle
  9107. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9108. does not link to the calendar and cannot be changed with the S-cursor keys.
  9109. So these are more for recording a certain time/date."
  9110. (interactive "P")
  9111. (let (org-time-was-given org-end-time-was-given time)
  9112. (setq time (org-read-date arg 'totime))
  9113. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9114. nil nil (list org-end-time-was-given))))
  9115. (defvar org-date-ovl (org-make-overlay 1 1))
  9116. (org-overlay-put org-date-ovl 'face 'org-warning)
  9117. (org-detach-overlay org-date-ovl)
  9118. (defvar org-ans1) ; dynamically scoped parameter
  9119. (defvar org-ans2) ; dynamically scoped parameter
  9120. (defvar org-plain-time-of-day-regexp) ; defined below
  9121. (defvar org-read-date-overlay nil)
  9122. (defvar org-dcst nil) ; dynamically scoped
  9123. (defun org-read-date (&optional with-time to-time from-string prompt
  9124. default-time default-input)
  9125. "Read a date, possibly a time, and make things smooth for the user.
  9126. The prompt will suggest to enter an ISO date, but you can also enter anything
  9127. which will at least partially be understood by `parse-time-string'.
  9128. Unrecognized parts of the date will default to the current day, month, year,
  9129. hour and minute. If this command is called to replace a timestamp at point,
  9130. of to enter the second timestamp of a range, the default time is taken from the
  9131. existing stamp. For example,
  9132. 3-2-5 --> 2003-02-05
  9133. feb 15 --> currentyear-02-15
  9134. sep 12 9 --> 2009-09-12
  9135. 12:45 --> today 12:45
  9136. 22 sept 0:34 --> currentyear-09-22 0:34
  9137. 12 --> currentyear-currentmonth-12
  9138. Fri --> nearest Friday (today or later)
  9139. etc.
  9140. Furthermore you can specify a relative date by giving, as the *first* thing
  9141. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9142. change in days weeks, months, years.
  9143. With a single plus or minus, the date is relative to today. With a double
  9144. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9145. +4d --> four days from today
  9146. +4 --> same as above
  9147. +2w --> two weeks from today
  9148. ++5 --> five days from default date
  9149. The function understands only English month and weekday abbreviations,
  9150. but this can be configured with the variables `parse-time-months' and
  9151. `parse-time-weekdays'.
  9152. While prompting, a calendar is popped up - you can also select the
  9153. date with the mouse (button 1). The calendar shows a period of three
  9154. months. To scroll it to other months, use the keys `>' and `<'.
  9155. If you don't like the calendar, turn it off with
  9156. \(setq org-read-date-popup-calendar nil)
  9157. With optional argument TO-TIME, the date will immediately be converted
  9158. to an internal time.
  9159. With an optional argument WITH-TIME, the prompt will suggest to also
  9160. insert a time. Note that when WITH-TIME is not set, you can still
  9161. enter a time, and this function will inform the calling routine about
  9162. this change. The calling routine may then choose to change the format
  9163. used to insert the time stamp into the buffer to include the time.
  9164. With optional argument FROM-STRING, read from this string instead from
  9165. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9166. the time/date that is used for everything that is not specified by the
  9167. user."
  9168. (require 'parse-time)
  9169. (let* ((org-time-stamp-rounding-minutes
  9170. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9171. (org-dcst org-display-custom-times)
  9172. (ct (org-current-time))
  9173. (def (or default-time ct))
  9174. (defdecode (decode-time def))
  9175. (dummy (progn
  9176. (when (< (nth 2 defdecode) org-extend-today-until)
  9177. (setcar (nthcdr 2 defdecode) -1)
  9178. (setcar (nthcdr 1 defdecode) 59)
  9179. (setq def (apply 'encode-time defdecode)
  9180. defdecode (decode-time def)))))
  9181. (calendar-move-hook nil)
  9182. (calendar-view-diary-initially-flag nil)
  9183. (view-diary-entries-initially nil)
  9184. (calendar-view-holidays-initially-flag nil)
  9185. (view-calendar-holidays-initially nil)
  9186. (timestr (format-time-string
  9187. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9188. (prompt (concat (if prompt (concat prompt " ") "")
  9189. (format "Date+time [%s]: " timestr)))
  9190. ans (org-ans0 "") org-ans1 org-ans2 final)
  9191. (cond
  9192. (from-string (setq ans from-string))
  9193. (org-read-date-popup-calendar
  9194. (save-excursion
  9195. (save-window-excursion
  9196. (calendar)
  9197. (calendar-forward-day (- (time-to-days def)
  9198. (calendar-absolute-from-gregorian
  9199. (calendar-current-date))))
  9200. (org-eval-in-calendar nil t)
  9201. (let* ((old-map (current-local-map))
  9202. (map (copy-keymap calendar-mode-map))
  9203. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9204. (org-defkey map (kbd "RET") 'org-calendar-select)
  9205. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9206. 'org-calendar-select-mouse)
  9207. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9208. 'org-calendar-select-mouse)
  9209. (org-defkey minibuffer-local-map [(meta shift left)]
  9210. (lambda () (interactive)
  9211. (org-eval-in-calendar '(calendar-backward-month 1))))
  9212. (org-defkey minibuffer-local-map [(meta shift right)]
  9213. (lambda () (interactive)
  9214. (org-eval-in-calendar '(calendar-forward-month 1))))
  9215. (org-defkey minibuffer-local-map [(meta shift up)]
  9216. (lambda () (interactive)
  9217. (org-eval-in-calendar '(calendar-backward-year 1))))
  9218. (org-defkey minibuffer-local-map [(meta shift down)]
  9219. (lambda () (interactive)
  9220. (org-eval-in-calendar '(calendar-forward-year 1))))
  9221. (org-defkey minibuffer-local-map [(shift up)]
  9222. (lambda () (interactive)
  9223. (org-eval-in-calendar '(calendar-backward-week 1))))
  9224. (org-defkey minibuffer-local-map [(shift down)]
  9225. (lambda () (interactive)
  9226. (org-eval-in-calendar '(calendar-forward-week 1))))
  9227. (org-defkey minibuffer-local-map [(shift left)]
  9228. (lambda () (interactive)
  9229. (org-eval-in-calendar '(calendar-backward-day 1))))
  9230. (org-defkey minibuffer-local-map [(shift right)]
  9231. (lambda () (interactive)
  9232. (org-eval-in-calendar '(calendar-forward-day 1))))
  9233. (org-defkey minibuffer-local-map ">"
  9234. (lambda () (interactive)
  9235. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9236. (org-defkey minibuffer-local-map "<"
  9237. (lambda () (interactive)
  9238. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9239. (unwind-protect
  9240. (progn
  9241. (use-local-map map)
  9242. (add-hook 'post-command-hook 'org-read-date-display)
  9243. (setq org-ans0 (read-string prompt default-input nil nil))
  9244. ;; org-ans0: from prompt
  9245. ;; org-ans1: from mouse click
  9246. ;; org-ans2: from calendar motion
  9247. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9248. (remove-hook 'post-command-hook 'org-read-date-display)
  9249. (use-local-map old-map)
  9250. (when org-read-date-overlay
  9251. (org-delete-overlay org-read-date-overlay)
  9252. (setq org-read-date-overlay nil)))))))
  9253. (t ; Naked prompt only
  9254. (unwind-protect
  9255. (setq ans (read-string prompt default-input nil timestr))
  9256. (when org-read-date-overlay
  9257. (org-delete-overlay org-read-date-overlay)
  9258. (setq org-read-date-overlay nil)))))
  9259. (setq final (org-read-date-analyze ans def defdecode))
  9260. (if to-time
  9261. (apply 'encode-time final)
  9262. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9263. (format "%04d-%02d-%02d %02d:%02d"
  9264. (nth 5 final) (nth 4 final) (nth 3 final)
  9265. (nth 2 final) (nth 1 final))
  9266. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9267. (defvar def)
  9268. (defvar defdecode)
  9269. (defvar with-time)
  9270. (defun org-read-date-display ()
  9271. "Display the currrent date prompt interpretation in the minibuffer."
  9272. (when org-read-date-display-live
  9273. (when org-read-date-overlay
  9274. (org-delete-overlay org-read-date-overlay))
  9275. (let ((p (point)))
  9276. (end-of-line 1)
  9277. (while (not (equal (buffer-substring
  9278. (max (point-min) (- (point) 4)) (point))
  9279. " "))
  9280. (insert " "))
  9281. (goto-char p))
  9282. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9283. " " (or org-ans1 org-ans2)))
  9284. (org-end-time-was-given nil)
  9285. (f (org-read-date-analyze ans def defdecode))
  9286. (fmts (if org-dcst
  9287. org-time-stamp-custom-formats
  9288. org-time-stamp-formats))
  9289. (fmt (if (or with-time
  9290. (and (boundp 'org-time-was-given) org-time-was-given))
  9291. (cdr fmts)
  9292. (car fmts)))
  9293. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9294. (when (and org-end-time-was-given
  9295. (string-match org-plain-time-of-day-regexp txt))
  9296. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9297. org-end-time-was-given
  9298. (substring txt (match-end 0)))))
  9299. (setq org-read-date-overlay
  9300. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9301. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9302. (defun org-read-date-analyze (ans def defdecode)
  9303. "Analyze the combined answer of the date prompt."
  9304. ;; FIXME: cleanup and comment
  9305. (let (delta deltan deltaw deltadef year month day
  9306. hour minute second wday pm h2 m2 tl wday1
  9307. iso-year iso-weekday iso-week iso-year iso-date)
  9308. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9309. (setq ans (replace-match "" t t ans)
  9310. deltan (car delta)
  9311. deltaw (nth 1 delta)
  9312. deltadef (nth 2 delta)))
  9313. ;; Check if there is an iso week date in there
  9314. ;; If yes, sore the info and ostpone interpreting it until the rest
  9315. ;; of the parsing is done
  9316. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9317. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9318. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9319. iso-week (string-to-number (match-string 2 ans)))
  9320. (setq ans (replace-match "" t t ans)))
  9321. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9322. (when (string-match
  9323. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9324. (setq year (if (match-end 2)
  9325. (string-to-number (match-string 2 ans))
  9326. (string-to-number (format-time-string "%Y")))
  9327. month (string-to-number (match-string 3 ans))
  9328. day (string-to-number (match-string 4 ans)))
  9329. (if (< year 100) (setq year (+ 2000 year)))
  9330. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9331. t nil ans)))
  9332. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9333. ;; If there is a time with am/pm, and *no* time without it, we convert
  9334. ;; so that matching will be successful.
  9335. (loop for i from 1 to 2 do ; twice, for end time as well
  9336. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9337. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9338. (setq hour (string-to-number (match-string 1 ans))
  9339. minute (if (match-end 3)
  9340. (string-to-number (match-string 3 ans))
  9341. 0)
  9342. pm (equal ?p
  9343. (string-to-char (downcase (match-string 4 ans)))))
  9344. (if (and (= hour 12) (not pm))
  9345. (setq hour 0)
  9346. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9347. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9348. t t ans))))
  9349. ;; Check if a time range is given as a duration
  9350. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9351. (setq hour (string-to-number (match-string 1 ans))
  9352. h2 (+ hour (string-to-number (match-string 3 ans)))
  9353. minute (string-to-number (match-string 2 ans))
  9354. m2 (+ minute (if (match-end 5) (string-to-number
  9355. (match-string 5 ans))0)))
  9356. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9357. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9358. t t ans)))
  9359. ;; Check if there is a time range
  9360. (when (boundp 'org-end-time-was-given)
  9361. (setq org-time-was-given nil)
  9362. (when (and (string-match org-plain-time-of-day-regexp ans)
  9363. (match-end 8))
  9364. (setq org-end-time-was-given (match-string 8 ans))
  9365. (setq ans (concat (substring ans 0 (match-beginning 7))
  9366. (substring ans (match-end 7))))))
  9367. (setq tl (parse-time-string ans)
  9368. day (or (nth 3 tl) (nth 3 defdecode))
  9369. month (or (nth 4 tl)
  9370. (if (and org-read-date-prefer-future
  9371. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9372. (1+ (nth 4 defdecode))
  9373. (nth 4 defdecode)))
  9374. year (or (nth 5 tl)
  9375. (if (and org-read-date-prefer-future
  9376. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9377. (1+ (nth 5 defdecode))
  9378. (nth 5 defdecode)))
  9379. hour (or (nth 2 tl) (nth 2 defdecode))
  9380. minute (or (nth 1 tl) (nth 1 defdecode))
  9381. second (or (nth 0 tl) 0)
  9382. wday (nth 6 tl))
  9383. ;; Special date definitions below
  9384. (cond
  9385. (iso-week
  9386. ;; There was an iso week
  9387. (setq year (or iso-year year)
  9388. day (or iso-weekday wday 1)
  9389. wday nil ; to make sure that the trigger below does not match
  9390. iso-date (calendar-gregorian-from-absolute
  9391. (calendar-absolute-from-iso
  9392. (list iso-week day year))))
  9393. ; FIXME: Should we also push ISO weeks into the future?
  9394. ; (when (and org-read-date-prefer-future
  9395. ; (not iso-year)
  9396. ; (< (calendar-absolute-from-gregorian iso-date)
  9397. ; (time-to-days (current-time))))
  9398. ; (setq year (1+ year)
  9399. ; iso-date (calendar-gregorian-from-absolute
  9400. ; (calendar-absolute-from-iso
  9401. ; (list iso-week day year)))))
  9402. (setq month (car iso-date)
  9403. year (nth 2 iso-date)
  9404. day (nth 1 iso-date)))
  9405. (deltan
  9406. (unless deltadef
  9407. (let ((now (decode-time (current-time))))
  9408. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9409. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9410. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9411. ((equal deltaw "m") (setq month (+ month deltan)))
  9412. ((equal deltaw "y") (setq year (+ year deltan)))))
  9413. ((and wday (not (nth 3 tl)))
  9414. ;; Weekday was given, but no day, so pick that day in the week
  9415. ;; on or after the derived date.
  9416. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9417. (unless (equal wday wday1)
  9418. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9419. (if (and (boundp 'org-time-was-given)
  9420. (nth 2 tl))
  9421. (setq org-time-was-given t))
  9422. (if (< year 100) (setq year (+ 2000 year)))
  9423. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9424. (list second minute hour day month year)))
  9425. (defvar parse-time-weekdays)
  9426. (defun org-read-date-get-relative (s today default)
  9427. "Check string S for special relative date string.
  9428. TODAY and DEFAULT are internal times, for today and for a default.
  9429. Return shift list (N what def-flag)
  9430. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9431. N is the number of WHATs to shift.
  9432. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9433. the DEFAULT date rather than TODAY."
  9434. (when (string-match
  9435. (concat
  9436. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  9437. "\\([0-9]+\\)?"
  9438. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9439. "\\([ \t]\\|$\\)") s)
  9440. (let* ((dir (if (match-end 1)
  9441. (string-to-char (substring (match-string 1 s) -1))
  9442. ?+))
  9443. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9444. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9445. (what (if (match-end 3) (match-string 3 s) "d"))
  9446. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9447. (date (if rel default today))
  9448. (wday (nth 6 (decode-time date)))
  9449. delta)
  9450. (if wday1
  9451. (progn
  9452. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9453. (if (= dir ?-) (setq delta (- delta 7)))
  9454. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9455. (list delta "d" rel))
  9456. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9457. (defun org-eval-in-calendar (form &optional keepdate)
  9458. "Eval FORM in the calendar window and return to current window.
  9459. Also, store the cursor date in variable org-ans2."
  9460. (let ((sw (selected-window)))
  9461. (select-window (get-buffer-window "*Calendar*"))
  9462. (eval form)
  9463. (when (and (not keepdate) (calendar-cursor-to-date))
  9464. (let* ((date (calendar-cursor-to-date))
  9465. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9466. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9467. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9468. (select-window sw)))
  9469. ; ;; Update the prompt to show new default date
  9470. ; (save-excursion
  9471. ; (goto-char (point-min))
  9472. ; (when (and org-ans2
  9473. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9474. ; (get-text-property (match-end 0) 'field))
  9475. ; (let ((inhibit-read-only t))
  9476. ; (replace-match (concat "[" org-ans2 "]") t t)
  9477. ; (add-text-properties (point-min) (1+ (match-end 0))
  9478. ; (text-properties-at (1+ (point-min)))))))))
  9479. (defun org-calendar-select ()
  9480. "Return to `org-read-date' with the date currently selected.
  9481. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9482. (interactive)
  9483. (when (calendar-cursor-to-date)
  9484. (let* ((date (calendar-cursor-to-date))
  9485. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9486. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9487. (if (active-minibuffer-window) (exit-minibuffer))))
  9488. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9489. "Insert a date stamp for the date given by the internal TIME.
  9490. WITH-HM means, use the stamp format that includes the time of the day.
  9491. INACTIVE means use square brackets instead of angular ones, so that the
  9492. stamp will not contribute to the agenda.
  9493. PRE and POST are optional strings to be inserted before and after the
  9494. stamp.
  9495. The command returns the inserted time stamp."
  9496. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9497. stamp)
  9498. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9499. (insert-before-markers (or pre ""))
  9500. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9501. (when (listp extra)
  9502. (setq extra (car extra))
  9503. (if (and (stringp extra)
  9504. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9505. (setq extra (format "-%02d:%02d"
  9506. (string-to-number (match-string 1 extra))
  9507. (string-to-number (match-string 2 extra))))
  9508. (setq extra nil)))
  9509. (when extra
  9510. (backward-char 1)
  9511. (insert-before-markers extra)
  9512. (forward-char 1))
  9513. (insert-before-markers (or post ""))
  9514. stamp))
  9515. (defun org-toggle-time-stamp-overlays ()
  9516. "Toggle the use of custom time stamp formats."
  9517. (interactive)
  9518. (setq org-display-custom-times (not org-display-custom-times))
  9519. (unless org-display-custom-times
  9520. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9521. (while (setq p (next-single-property-change p 'display))
  9522. (if (and (get-text-property p 'display)
  9523. (eq (get-text-property p 'face) 'org-date))
  9524. (remove-text-properties
  9525. p (setq p (next-single-property-change p 'display))
  9526. '(display t))))
  9527. (set-buffer-modified-p bmp)))
  9528. (if (featurep 'xemacs)
  9529. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9530. (org-restart-font-lock)
  9531. (setq org-table-may-need-update t)
  9532. (if org-display-custom-times
  9533. (message "Time stamps are overlayed with custom format")
  9534. (message "Time stamp overlays removed")))
  9535. (defun org-display-custom-time (beg end)
  9536. "Overlay modified time stamp format over timestamp between BED and END."
  9537. (let* ((ts (buffer-substring beg end))
  9538. t1 w1 with-hm tf time str w2 (off 0))
  9539. (save-match-data
  9540. (setq t1 (org-parse-time-string ts t))
  9541. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9542. (setq off (- (match-end 0) (match-beginning 0)))))
  9543. (setq end (- end off))
  9544. (setq w1 (- end beg)
  9545. with-hm (and (nth 1 t1) (nth 2 t1))
  9546. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9547. time (org-fix-decoded-time t1)
  9548. str (org-add-props
  9549. (format-time-string
  9550. (substring tf 1 -1) (apply 'encode-time time))
  9551. nil 'mouse-face 'highlight)
  9552. w2 (length str))
  9553. (if (not (= w2 w1))
  9554. (add-text-properties (1+ beg) (+ 2 beg)
  9555. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9556. (if (featurep 'xemacs)
  9557. (progn
  9558. (put-text-property beg end 'invisible t)
  9559. (put-text-property beg end 'end-glyph (make-glyph str)))
  9560. (put-text-property beg end 'display str))))
  9561. (defun org-translate-time (string)
  9562. "Translate all timestamps in STRING to custom format.
  9563. But do this only if the variable `org-display-custom-times' is set."
  9564. (when org-display-custom-times
  9565. (save-match-data
  9566. (let* ((start 0)
  9567. (re org-ts-regexp-both)
  9568. t1 with-hm inactive tf time str beg end)
  9569. (while (setq start (string-match re string start))
  9570. (setq beg (match-beginning 0)
  9571. end (match-end 0)
  9572. t1 (save-match-data
  9573. (org-parse-time-string (substring string beg end) t))
  9574. with-hm (and (nth 1 t1) (nth 2 t1))
  9575. inactive (equal (substring string beg (1+ beg)) "[")
  9576. tf (funcall (if with-hm 'cdr 'car)
  9577. org-time-stamp-custom-formats)
  9578. time (org-fix-decoded-time t1)
  9579. str (format-time-string
  9580. (concat
  9581. (if inactive "[" "<") (substring tf 1 -1)
  9582. (if inactive "]" ">"))
  9583. (apply 'encode-time time))
  9584. string (replace-match str t t string)
  9585. start (+ start (length str)))))))
  9586. string)
  9587. (defun org-fix-decoded-time (time)
  9588. "Set 0 instead of nil for the first 6 elements of time.
  9589. Don't touch the rest."
  9590. (let ((n 0))
  9591. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9592. (defun org-days-to-time (timestamp-string)
  9593. "Difference between TIMESTAMP-STRING and now in days."
  9594. (- (time-to-days (org-time-string-to-time timestamp-string))
  9595. (time-to-days (current-time))))
  9596. (defun org-deadline-close (timestamp-string &optional ndays)
  9597. "Is the time in TIMESTAMP-STRING close to the current date?"
  9598. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9599. (and (< (org-days-to-time timestamp-string) ndays)
  9600. (not (org-entry-is-done-p))))
  9601. (defun org-get-wdays (ts)
  9602. "Get the deadline lead time appropriate for timestring TS."
  9603. (cond
  9604. ((<= org-deadline-warning-days 0)
  9605. ;; 0 or negative, enforce this value no matter what
  9606. (- org-deadline-warning-days))
  9607. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9608. ;; lead time is specified.
  9609. (floor (* (string-to-number (match-string 1 ts))
  9610. (cdr (assoc (match-string 2 ts)
  9611. '(("d" . 1) ("w" . 7)
  9612. ("m" . 30.4) ("y" . 365.25)))))))
  9613. ;; go for the default.
  9614. (t org-deadline-warning-days)))
  9615. (defun org-calendar-select-mouse (ev)
  9616. "Return to `org-read-date' with the date currently selected.
  9617. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9618. (interactive "e")
  9619. (mouse-set-point ev)
  9620. (when (calendar-cursor-to-date)
  9621. (let* ((date (calendar-cursor-to-date))
  9622. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9623. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9624. (if (active-minibuffer-window) (exit-minibuffer))))
  9625. (defun org-check-deadlines (ndays)
  9626. "Check if there are any deadlines due or past due.
  9627. A deadline is considered due if it happens within `org-deadline-warning-days'
  9628. days from today's date. If the deadline appears in an entry marked DONE,
  9629. it is not shown. The prefix arg NDAYS can be used to test that many
  9630. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9631. (interactive "P")
  9632. (let* ((org-warn-days
  9633. (cond
  9634. ((equal ndays '(4)) 100000)
  9635. (ndays (prefix-numeric-value ndays))
  9636. (t (abs org-deadline-warning-days))))
  9637. (case-fold-search nil)
  9638. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9639. (callback
  9640. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9641. (message "%d deadlines past-due or due within %d days"
  9642. (org-occur regexp nil callback)
  9643. org-warn-days)))
  9644. (defun org-check-before-date (date)
  9645. "Check if there are deadlines or scheduled entries before DATE."
  9646. (interactive (list (org-read-date)))
  9647. (let ((case-fold-search nil)
  9648. (regexp (concat "\\<\\(" org-deadline-string
  9649. "\\|" org-scheduled-string
  9650. "\\) *<\\([^>]+\\)>"))
  9651. (callback
  9652. (lambda () (time-less-p
  9653. (org-time-string-to-time (match-string 2))
  9654. (org-time-string-to-time date)))))
  9655. (message "%d entries before %s"
  9656. (org-occur regexp nil callback) date)))
  9657. (defun org-evaluate-time-range (&optional to-buffer)
  9658. "Evaluate a time range by computing the difference between start and end.
  9659. Normally the result is just printed in the echo area, but with prefix arg
  9660. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9661. If the time range is actually in a table, the result is inserted into the
  9662. next column.
  9663. For time difference computation, a year is assumed to be exactly 365
  9664. days in order to avoid rounding problems."
  9665. (interactive "P")
  9666. (or
  9667. (org-clock-update-time-maybe)
  9668. (save-excursion
  9669. (unless (org-at-date-range-p t)
  9670. (goto-char (point-at-bol))
  9671. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9672. (if (not (org-at-date-range-p t))
  9673. (error "Not at a time-stamp range, and none found in current line")))
  9674. (let* ((ts1 (match-string 1))
  9675. (ts2 (match-string 2))
  9676. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9677. (match-end (match-end 0))
  9678. (time1 (org-time-string-to-time ts1))
  9679. (time2 (org-time-string-to-time ts2))
  9680. (t1 (time-to-seconds time1))
  9681. (t2 (time-to-seconds time2))
  9682. (diff (abs (- t2 t1)))
  9683. (negative (< (- t2 t1) 0))
  9684. ;; (ys (floor (* 365 24 60 60)))
  9685. (ds (* 24 60 60))
  9686. (hs (* 60 60))
  9687. (fy "%dy %dd %02d:%02d")
  9688. (fy1 "%dy %dd")
  9689. (fd "%dd %02d:%02d")
  9690. (fd1 "%dd")
  9691. (fh "%02d:%02d")
  9692. y d h m align)
  9693. (if havetime
  9694. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9695. y 0
  9696. d (floor (/ diff ds)) diff (mod diff ds)
  9697. h (floor (/ diff hs)) diff (mod diff hs)
  9698. m (floor (/ diff 60)))
  9699. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9700. y 0
  9701. d (floor (+ (/ diff ds) 0.5))
  9702. h 0 m 0))
  9703. (if (not to-buffer)
  9704. (message "%s" (org-make-tdiff-string y d h m))
  9705. (if (org-at-table-p)
  9706. (progn
  9707. (goto-char match-end)
  9708. (setq align t)
  9709. (and (looking-at " *|") (goto-char (match-end 0))))
  9710. (goto-char match-end))
  9711. (if (looking-at
  9712. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9713. (replace-match ""))
  9714. (if negative (insert " -"))
  9715. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9716. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9717. (insert " " (format fh h m))))
  9718. (if align (org-table-align))
  9719. (message "Time difference inserted")))))
  9720. (defun org-make-tdiff-string (y d h m)
  9721. (let ((fmt "")
  9722. (l nil))
  9723. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9724. l (push y l)))
  9725. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9726. l (push d l)))
  9727. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9728. l (push h l)))
  9729. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9730. l (push m l)))
  9731. (apply 'format fmt (nreverse l))))
  9732. (defun org-time-string-to-time (s)
  9733. (apply 'encode-time (org-parse-time-string s)))
  9734. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9735. "Convert a time stamp to an absolute day number.
  9736. If there is a specifyer for a cyclic time stamp, get the closest date to
  9737. DAYNR.
  9738. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9739. (cond
  9740. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9741. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9742. daynr
  9743. (+ daynr 1000)))
  9744. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9745. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9746. (time-to-days (current-time))) (match-string 0 s)
  9747. prefer show-all))
  9748. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9749. (defun org-days-to-iso-week (days)
  9750. "Return the iso week number."
  9751. (require 'cal-iso)
  9752. (car (calendar-iso-from-absolute days)))
  9753. (defun org-small-year-to-year (year)
  9754. "Convert 2-digit years into 4-digit years.
  9755. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9756. The year 2000 cannot be abbreviated. Any year lager than 99
  9757. is retrned unchanged."
  9758. (if (< year 38)
  9759. (setq year (+ 2000 year))
  9760. (if (< year 100)
  9761. (setq year (+ 1900 year))))
  9762. year)
  9763. (defun org-time-from-absolute (d)
  9764. "Return the time corresponding to date D.
  9765. D may be an absolute day number, or a calendar-type list (month day year)."
  9766. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9767. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9768. (defun org-calendar-holiday ()
  9769. "List of holidays, for Diary display in Org-mode."
  9770. (require 'holidays)
  9771. (let ((hl (funcall
  9772. (if (fboundp 'calendar-check-holidays)
  9773. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9774. (if hl (mapconcat 'identity hl "; "))))
  9775. (defun org-diary-sexp-entry (sexp entry date)
  9776. "Process a SEXP diary ENTRY for DATE."
  9777. (require 'diary-lib)
  9778. (let ((result (if calendar-debug-sexp
  9779. (let ((stack-trace-on-error t))
  9780. (eval (car (read-from-string sexp))))
  9781. (condition-case nil
  9782. (eval (car (read-from-string sexp)))
  9783. (error
  9784. (beep)
  9785. (message "Bad sexp at line %d in %s: %s"
  9786. (org-current-line)
  9787. (buffer-file-name) sexp)
  9788. (sleep-for 2))))))
  9789. (cond ((stringp result) result)
  9790. ((and (consp result)
  9791. (stringp (cdr result))) (cdr result))
  9792. (result entry)
  9793. (t nil))))
  9794. (defun org-diary-to-ical-string (frombuf)
  9795. "Get iCalendar entries from diary entries in buffer FROMBUF.
  9796. This uses the icalendar.el library."
  9797. (let* ((tmpdir (if (featurep 'xemacs)
  9798. (temp-directory)
  9799. temporary-file-directory))
  9800. (tmpfile (make-temp-name
  9801. (expand-file-name "orgics" tmpdir)))
  9802. buf rtn b e)
  9803. (save-excursion
  9804. (set-buffer frombuf)
  9805. (icalendar-export-region (point-min) (point-max) tmpfile)
  9806. (setq buf (find-buffer-visiting tmpfile))
  9807. (set-buffer buf)
  9808. (goto-char (point-min))
  9809. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  9810. (setq b (match-beginning 0)))
  9811. (goto-char (point-max))
  9812. (if (re-search-backward "^END:VEVENT" nil t)
  9813. (setq e (match-end 0)))
  9814. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  9815. (kill-buffer buf)
  9816. (kill-buffer frombuf)
  9817. (delete-file tmpfile)
  9818. rtn))
  9819. (defun org-closest-date (start current change prefer show-all)
  9820. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  9821. When PREFER is `past' return a date that is either CURRENT or past.
  9822. When PREFER is `future', return a date that is either CURRENT or future.
  9823. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  9824. ;; Make the proper lists from the dates
  9825. (catch 'exit
  9826. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  9827. dn dw sday cday n1 n2
  9828. d m y y1 y2 date1 date2 nmonths nm ny m2)
  9829. (setq start (org-date-to-gregorian start)
  9830. current (org-date-to-gregorian
  9831. (if show-all
  9832. current
  9833. (time-to-days (current-time))))
  9834. sday (calendar-absolute-from-gregorian start)
  9835. cday (calendar-absolute-from-gregorian current))
  9836. (if (<= cday sday) (throw 'exit sday))
  9837. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  9838. (setq dn (string-to-number (match-string 1 change))
  9839. dw (cdr (assoc (match-string 2 change) a1)))
  9840. (error "Invalid change specifyer: %s" change))
  9841. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  9842. (cond
  9843. ((eq dw 'day)
  9844. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  9845. n2 (+ n1 dn)))
  9846. ((eq dw 'year)
  9847. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  9848. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  9849. (setq date1 (list m d y1)
  9850. n1 (calendar-absolute-from-gregorian date1)
  9851. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  9852. n2 (calendar-absolute-from-gregorian date2)))
  9853. ((eq dw 'month)
  9854. ;; approx number of month between the tow dates
  9855. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  9856. ;; How often does dn fit in there?
  9857. (setq d (nth 1 start) m (car start) y (nth 2 start)
  9858. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  9859. m (+ m nm)
  9860. ny (floor (/ m 12))
  9861. y (+ y ny)
  9862. m (- m (* ny 12)))
  9863. (while (> m 12) (setq m (- m 12) y (1+ y)))
  9864. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  9865. (setq m2 (+ m dn) y2 y)
  9866. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9867. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  9868. (while (< n2 cday)
  9869. (setq n1 n2 m m2 y y2)
  9870. (setq m2 (+ m dn) y2 y)
  9871. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9872. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  9873. (if show-all
  9874. (cond
  9875. ((eq prefer 'past) n1)
  9876. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9877. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  9878. (cond
  9879. ((eq prefer 'past) n1)
  9880. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9881. (t (if (= cday n1) n1 n2)))))))
  9882. (defun org-date-to-gregorian (date)
  9883. "Turn any specification of DATE into a gregorian date for the calendar."
  9884. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  9885. ((and (listp date) (= (length date) 3)) date)
  9886. ((stringp date)
  9887. (setq date (org-parse-time-string date))
  9888. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  9889. ((listp date)
  9890. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  9891. (defun org-parse-time-string (s &optional nodefault)
  9892. "Parse the standard Org-mode time string.
  9893. This should be a lot faster than the normal `parse-time-string'.
  9894. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  9895. hour and minute fields will be nil if not given."
  9896. (if (string-match org-ts-regexp0 s)
  9897. (list 0
  9898. (if (or (match-beginning 8) (not nodefault))
  9899. (string-to-number (or (match-string 8 s) "0")))
  9900. (if (or (match-beginning 7) (not nodefault))
  9901. (string-to-number (or (match-string 7 s) "0")))
  9902. (string-to-number (match-string 4 s))
  9903. (string-to-number (match-string 3 s))
  9904. (string-to-number (match-string 2 s))
  9905. nil nil nil)
  9906. (make-list 9 0)))
  9907. (defun org-timestamp-up (&optional arg)
  9908. "Increase the date item at the cursor by one.
  9909. If the cursor is on the year, change the year. If it is on the month or
  9910. the day, change that.
  9911. With prefix ARG, change by that many units."
  9912. (interactive "p")
  9913. (org-timestamp-change (prefix-numeric-value arg)))
  9914. (defun org-timestamp-down (&optional arg)
  9915. "Decrease the date item at the cursor by one.
  9916. If the cursor is on the year, change the year. If it is on the month or
  9917. the day, change that.
  9918. With prefix ARG, change by that many units."
  9919. (interactive "p")
  9920. (org-timestamp-change (- (prefix-numeric-value arg))))
  9921. (defun org-timestamp-up-day (&optional arg)
  9922. "Increase the date in the time stamp by one day.
  9923. With prefix ARG, change that many days."
  9924. (interactive "p")
  9925. (if (and (not (org-at-timestamp-p t))
  9926. (org-on-heading-p))
  9927. (org-todo 'up)
  9928. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  9929. (defun org-timestamp-down-day (&optional arg)
  9930. "Decrease the date in the time stamp by one day.
  9931. With prefix ARG, change that many days."
  9932. (interactive "p")
  9933. (if (and (not (org-at-timestamp-p t))
  9934. (org-on-heading-p))
  9935. (org-todo 'down)
  9936. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  9937. (defun org-at-timestamp-p (&optional inactive-ok)
  9938. "Determine if the cursor is in or at a timestamp."
  9939. (interactive)
  9940. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  9941. (pos (point))
  9942. (ans (or (looking-at tsr)
  9943. (save-excursion
  9944. (skip-chars-backward "^[<\n\r\t")
  9945. (if (> (point) (point-min)) (backward-char 1))
  9946. (and (looking-at tsr)
  9947. (> (- (match-end 0) pos) -1))))))
  9948. (and ans
  9949. (boundp 'org-ts-what)
  9950. (setq org-ts-what
  9951. (cond
  9952. ((= pos (match-beginning 0)) 'bracket)
  9953. ((= pos (1- (match-end 0))) 'bracket)
  9954. ((org-pos-in-match-range pos 2) 'year)
  9955. ((org-pos-in-match-range pos 3) 'month)
  9956. ((org-pos-in-match-range pos 7) 'hour)
  9957. ((org-pos-in-match-range pos 8) 'minute)
  9958. ((or (org-pos-in-match-range pos 4)
  9959. (org-pos-in-match-range pos 5)) 'day)
  9960. ((and (> pos (or (match-end 8) (match-end 5)))
  9961. (< pos (match-end 0)))
  9962. (- pos (or (match-end 8) (match-end 5))))
  9963. (t 'day))))
  9964. ans))
  9965. (defun org-toggle-timestamp-type ()
  9966. "Toggle the type (<active> or [inactive]) of a time stamp."
  9967. (interactive)
  9968. (when (org-at-timestamp-p t)
  9969. (save-excursion
  9970. (goto-char (match-beginning 0))
  9971. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  9972. (goto-char (1- (match-end 0)))
  9973. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  9974. (message "Timestamp is now %sactive"
  9975. (if (equal (char-before) ?>) "in" ""))))
  9976. (defun org-timestamp-change (n &optional what)
  9977. "Change the date in the time stamp at point.
  9978. The date will be changed by N times WHAT. WHAT can be `day', `month',
  9979. `year', `minute', `second'. If WHAT is not given, the cursor position
  9980. in the timestamp determines what will be changed."
  9981. (let ((pos (point))
  9982. with-hm inactive
  9983. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  9984. org-ts-what
  9985. extra rem
  9986. ts time time0)
  9987. (if (not (org-at-timestamp-p t))
  9988. (error "Not at a timestamp"))
  9989. (if (and (not what) (eq org-ts-what 'bracket))
  9990. (org-toggle-timestamp-type)
  9991. (if (and (not what) (not (eq org-ts-what 'day))
  9992. org-display-custom-times
  9993. (get-text-property (point) 'display)
  9994. (not (get-text-property (1- (point)) 'display)))
  9995. (setq org-ts-what 'day))
  9996. (setq org-ts-what (or what org-ts-what)
  9997. inactive (= (char-after (match-beginning 0)) ?\[)
  9998. ts (match-string 0))
  9999. (replace-match "")
  10000. (if (string-match
  10001. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10002. ts)
  10003. (setq extra (match-string 1 ts)))
  10004. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10005. (setq with-hm t))
  10006. (setq time0 (org-parse-time-string ts))
  10007. (when (and (eq org-ts-what 'minute)
  10008. (eq current-prefix-arg nil))
  10009. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10010. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10011. (setcar (cdr time0) (+ (nth 1 time0)
  10012. (if (> n 0) (- rem) (- dm rem))))))
  10013. (setq time
  10014. (encode-time (or (car time0) 0)
  10015. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10016. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10017. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10018. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10019. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10020. (nthcdr 6 time0)))
  10021. (when (integerp org-ts-what)
  10022. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10023. (if (eq what 'calendar)
  10024. (let ((cal-date (org-get-date-from-calendar)))
  10025. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10026. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10027. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10028. (setcar time0 (or (car time0) 0))
  10029. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10030. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10031. (setq time (apply 'encode-time time0))))
  10032. (setq org-last-changed-timestamp
  10033. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10034. (org-clock-update-time-maybe)
  10035. (goto-char pos)
  10036. ;; Try to recenter the calendar window, if any
  10037. (if (and org-calendar-follow-timestamp-change
  10038. (get-buffer-window "*Calendar*" t)
  10039. (memq org-ts-what '(day month year)))
  10040. (org-recenter-calendar (time-to-days time))))))
  10041. (defun org-modify-ts-extra (s pos n dm)
  10042. "Change the different parts of the lead-time and repeat fields in timestamp."
  10043. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10044. ng h m new rem)
  10045. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10046. (cond
  10047. ((or (org-pos-in-match-range pos 2)
  10048. (org-pos-in-match-range pos 3))
  10049. (setq m (string-to-number (match-string 3 s))
  10050. h (string-to-number (match-string 2 s)))
  10051. (if (org-pos-in-match-range pos 2)
  10052. (setq h (+ h n))
  10053. (setq n (* dm (org-no-warnings (signum n))))
  10054. (when (not (= 0 (setq rem (% m dm))))
  10055. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10056. (setq m (+ m n)))
  10057. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10058. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10059. (setq h (min 24 (max 0 h)))
  10060. (setq ng 1 new (format "-%02d:%02d" h m)))
  10061. ((org-pos-in-match-range pos 6)
  10062. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10063. ((org-pos-in-match-range pos 5)
  10064. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10065. ((org-pos-in-match-range pos 9)
  10066. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10067. ((org-pos-in-match-range pos 8)
  10068. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10069. (when ng
  10070. (setq s (concat
  10071. (substring s 0 (match-beginning ng))
  10072. new
  10073. (substring s (match-end ng))))))
  10074. s))
  10075. (defun org-recenter-calendar (date)
  10076. "If the calendar is visible, recenter it to DATE."
  10077. (let* ((win (selected-window))
  10078. (cwin (get-buffer-window "*Calendar*" t))
  10079. (calendar-move-hook nil))
  10080. (when cwin
  10081. (select-window cwin)
  10082. (calendar-goto-date (if (listp date) date
  10083. (calendar-gregorian-from-absolute date)))
  10084. (select-window win))))
  10085. (defun org-goto-calendar (&optional arg)
  10086. "Go to the Emacs calendar at the current date.
  10087. If there is a time stamp in the current line, go to that date.
  10088. A prefix ARG can be used to force the current date."
  10089. (interactive "P")
  10090. (let ((tsr org-ts-regexp) diff
  10091. (calendar-move-hook nil)
  10092. (calendar-view-holidays-initially-flag nil)
  10093. (view-calendar-holidays-initially nil)
  10094. (calendar-view-diary-initially-flag nil)
  10095. (view-diary-entries-initially nil))
  10096. (if (or (org-at-timestamp-p)
  10097. (save-excursion
  10098. (beginning-of-line 1)
  10099. (looking-at (concat ".*" tsr))))
  10100. (let ((d1 (time-to-days (current-time)))
  10101. (d2 (time-to-days
  10102. (org-time-string-to-time (match-string 1)))))
  10103. (setq diff (- d2 d1))))
  10104. (calendar)
  10105. (calendar-goto-today)
  10106. (if (and diff (not arg)) (calendar-forward-day diff))))
  10107. (defun org-get-date-from-calendar ()
  10108. "Return a list (month day year) of date at point in calendar."
  10109. (with-current-buffer "*Calendar*"
  10110. (save-match-data
  10111. (calendar-cursor-to-date))))
  10112. (defun org-date-from-calendar ()
  10113. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10114. If there is already a time stamp at the cursor position, update it."
  10115. (interactive)
  10116. (if (org-at-timestamp-p t)
  10117. (org-timestamp-change 0 'calendar)
  10118. (let ((cal-date (org-get-date-from-calendar)))
  10119. (org-insert-time-stamp
  10120. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10121. (defun org-minutes-to-hh:mm-string (m)
  10122. "Compute H:MM from a number of minutes."
  10123. (let ((h (/ m 60)))
  10124. (setq m (- m (* 60 h)))
  10125. (format "%d:%02d" h m)))
  10126. (defun org-hh:mm-string-to-minutes (s)
  10127. "Convert a string H:MM to a number of minutes."
  10128. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10129. (+ (* (string-to-number (match-string 1 s)) 60)
  10130. (string-to-number (match-string 2 s)))
  10131. 0))
  10132. ;;;; Agenda files
  10133. ;;;###autoload
  10134. (defun org-iswitchb (&optional arg)
  10135. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10136. With a prefix argument, restrict available to files.
  10137. With two prefix arguments, restrict available buffers to agenda files.
  10138. Due to some yet unresolved reason, global function
  10139. `iswitchb-mode' needs to be active for this function to work."
  10140. (interactive "P")
  10141. (require 'iswitchb)
  10142. (let ((enabled iswitchb-mode) blist)
  10143. (or enabled (iswitchb-mode 1))
  10144. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10145. ((equal arg '(16)) (org-buffer-list 'agenda))
  10146. (t (org-buffer-list))))
  10147. (unwind-protect
  10148. (let ((iswitchb-make-buflist-hook
  10149. (lambda ()
  10150. (setq iswitchb-temp-buflist
  10151. (mapcar 'buffer-name blist)))))
  10152. (switch-to-buffer
  10153. (iswitchb-read-buffer
  10154. "Switch-to: " nil t))
  10155. (or enabled (iswitchb-mode -1))))))
  10156. (defun org-buffer-list (&optional predicate tmp)
  10157. "Return a list of Org buffers.
  10158. PREDICATE can be either 'export, 'files or 'agenda.
  10159. 'export restrict the list to Export buffers.
  10160. 'files restrict the list to buffers visiting Org files.
  10161. 'agenda restrict the list to buffers visiting agenda files.
  10162. If TMP is non-nil, don't include temporary buffers."
  10163. (let (filter blist)
  10164. (setq filter
  10165. (cond ((eq predicate 'files) "\.org$")
  10166. ((eq predicate 'export) "\*Org .*Export")
  10167. (t "\*Org \\|\.org$")))
  10168. (setq blist
  10169. (mapcar
  10170. (lambda(b)
  10171. (let ((bname (buffer-name b))
  10172. (bfile (buffer-file-name b)))
  10173. (if (and (string-match filter bname)
  10174. (if (eq predicate 'agenda)
  10175. (member bfile
  10176. (mapcar (lambda(f) (file-truename f))
  10177. org-agenda-files)) t)
  10178. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10179. (buffer-list)))
  10180. (delete nil blist)))
  10181. (defun org-agenda-files (&optional unrestricted ext)
  10182. "Get the list of agenda files.
  10183. Optional UNRESTRICTED means return the full list even if a restriction
  10184. is currently in place.
  10185. When EXT is non-nil, try to add all files that are created by adding EXT
  10186. to the file nemes. Basically, this is a way to add the archive files
  10187. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10188. a string, \"_archive\" will be used."
  10189. (let ((files
  10190. (cond
  10191. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10192. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10193. ((listp org-agenda-files) org-agenda-files)
  10194. (t (error "Invalid value of `org-agenda-files'")))))
  10195. (setq files (apply 'append
  10196. (mapcar (lambda (f)
  10197. (if (file-directory-p f)
  10198. (directory-files
  10199. f t org-agenda-file-regexp)
  10200. (list f)))
  10201. files)))
  10202. (when org-agenda-skip-unavailable-files
  10203. (setq files (delq nil
  10204. (mapcar (function
  10205. (lambda (file)
  10206. (and (file-readable-p file) file)))
  10207. files))))
  10208. (when ext
  10209. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10210. ext "_archive"))
  10211. (setq files (apply 'append
  10212. (mapcar
  10213. (lambda (f)
  10214. (if (file-exists-p (concat f ext))
  10215. (list f (concat f ext))
  10216. (list f)))
  10217. files))))
  10218. files))
  10219. (defun org-edit-agenda-file-list ()
  10220. "Edit the list of agenda files.
  10221. Depending on setup, this either uses customize to edit the variable
  10222. `org-agenda-files', or it visits the file that is holding the list. In the
  10223. latter case, the buffer is set up in a way that saving it automatically kills
  10224. the buffer and restores the previous window configuration."
  10225. (interactive)
  10226. (if (stringp org-agenda-files)
  10227. (let ((cw (current-window-configuration)))
  10228. (find-file org-agenda-files)
  10229. (org-set-local 'org-window-configuration cw)
  10230. (org-add-hook 'after-save-hook
  10231. (lambda ()
  10232. (set-window-configuration
  10233. (prog1 org-window-configuration
  10234. (kill-buffer (current-buffer))))
  10235. (org-install-agenda-files-menu)
  10236. (message "New agenda file list installed"))
  10237. nil 'local)
  10238. (message "%s" (substitute-command-keys
  10239. "Edit list and finish with \\[save-buffer]")))
  10240. (customize-variable 'org-agenda-files)))
  10241. (defun org-store-new-agenda-file-list (list)
  10242. "Set new value for the agenda file list and save it correcly."
  10243. (if (stringp org-agenda-files)
  10244. (let ((f org-agenda-files) b)
  10245. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10246. (with-temp-file f
  10247. (insert (mapconcat 'identity list "\n") "\n")))
  10248. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10249. (setq org-agenda-files list)
  10250. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10251. (defun org-read-agenda-file-list ()
  10252. "Read the list of agenda files from a file."
  10253. (when (file-directory-p org-agenda-files)
  10254. (error "`org-agenda-files' cannot be a single directory"))
  10255. (when (stringp org-agenda-files)
  10256. (with-temp-buffer
  10257. (insert-file-contents org-agenda-files)
  10258. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10259. ;;;###autoload
  10260. (defun org-cycle-agenda-files ()
  10261. "Cycle through the files in `org-agenda-files'.
  10262. If the current buffer visits an agenda file, find the next one in the list.
  10263. If the current buffer does not, find the first agenda file."
  10264. (interactive)
  10265. (let* ((fs (org-agenda-files t))
  10266. (files (append fs (list (car fs))))
  10267. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10268. file)
  10269. (unless files (error "No agenda files"))
  10270. (catch 'exit
  10271. (while (setq file (pop files))
  10272. (if (equal (file-truename file) tcf)
  10273. (when (car files)
  10274. (find-file (car files))
  10275. (throw 'exit t))))
  10276. (find-file (car fs)))
  10277. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10278. (defun org-agenda-file-to-front (&optional to-end)
  10279. "Move/add the current file to the top of the agenda file list.
  10280. If the file is not present in the list, it is added to the front. If it is
  10281. present, it is moved there. With optional argument TO-END, add/move to the
  10282. end of the list."
  10283. (interactive "P")
  10284. (let ((org-agenda-skip-unavailable-files nil)
  10285. (file-alist (mapcar (lambda (x)
  10286. (cons (file-truename x) x))
  10287. (org-agenda-files t)))
  10288. (ctf (file-truename buffer-file-name))
  10289. x had)
  10290. (setq x (assoc ctf file-alist) had x)
  10291. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10292. (if to-end
  10293. (setq file-alist (append (delq x file-alist) (list x)))
  10294. (setq file-alist (cons x (delq x file-alist))))
  10295. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10296. (org-install-agenda-files-menu)
  10297. (message "File %s to %s of agenda file list"
  10298. (if had "moved" "added") (if to-end "end" "front"))))
  10299. (defun org-remove-file (&optional file)
  10300. "Remove current file from the list of files in variable `org-agenda-files'.
  10301. These are the files which are being checked for agenda entries.
  10302. Optional argument FILE means, use this file instead of the current."
  10303. (interactive)
  10304. (let* ((org-agenda-skip-unavailable-files nil)
  10305. (file (or file buffer-file-name))
  10306. (true-file (file-truename file))
  10307. (afile (abbreviate-file-name file))
  10308. (files (delq nil (mapcar
  10309. (lambda (x)
  10310. (if (equal true-file
  10311. (file-truename x))
  10312. nil x))
  10313. (org-agenda-files t)))))
  10314. (if (not (= (length files) (length (org-agenda-files t))))
  10315. (progn
  10316. (org-store-new-agenda-file-list files)
  10317. (org-install-agenda-files-menu)
  10318. (message "Removed file: %s" afile))
  10319. (message "File was not in list: %s (not removed)" afile))))
  10320. (defun org-file-menu-entry (file)
  10321. (vector file (list 'find-file file) t))
  10322. (defun org-check-agenda-file (file)
  10323. "Make sure FILE exists. If not, ask user what to do."
  10324. (when (not (file-exists-p file))
  10325. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10326. (abbreviate-file-name file))
  10327. (let ((r (downcase (read-char-exclusive))))
  10328. (cond
  10329. ((equal r ?r)
  10330. (org-remove-file file)
  10331. (throw 'nextfile t))
  10332. (t (error "Abort"))))))
  10333. (defun org-get-agenda-file-buffer (file)
  10334. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10335. it to the list of buffers which might be released later."
  10336. (let ((buf (org-find-base-buffer-visiting file)))
  10337. (if buf
  10338. buf ; just return it
  10339. ;; Make a new buffer and remember it
  10340. (setq buf (find-file-noselect file))
  10341. (if buf (push buf org-agenda-new-buffers))
  10342. buf)))
  10343. (defun org-release-buffers (blist)
  10344. "Release all buffers in list, asking the user for confirmation when needed.
  10345. When a buffer is unmodified, it is just killed. When modified, it is saved
  10346. \(if the user agrees) and then killed."
  10347. (let (buf file)
  10348. (while (setq buf (pop blist))
  10349. (setq file (buffer-file-name buf))
  10350. (when (and (buffer-modified-p buf)
  10351. file
  10352. (y-or-n-p (format "Save file %s? " file)))
  10353. (with-current-buffer buf (save-buffer)))
  10354. (kill-buffer buf))))
  10355. (defun org-prepare-agenda-buffers (files)
  10356. "Create buffers for all agenda files, protect archived trees and comments."
  10357. (interactive)
  10358. (let ((pa '(:org-archived t))
  10359. (pc '(:org-comment t))
  10360. (pall '(:org-archived t :org-comment t))
  10361. (inhibit-read-only t)
  10362. (rea (concat ":" org-archive-tag ":"))
  10363. bmp file re)
  10364. (save-excursion
  10365. (save-restriction
  10366. (while (setq file (pop files))
  10367. (if (bufferp file)
  10368. (set-buffer file)
  10369. (org-check-agenda-file file)
  10370. (set-buffer (org-get-agenda-file-buffer file)))
  10371. (widen)
  10372. (setq bmp (buffer-modified-p))
  10373. (org-refresh-category-properties)
  10374. (setq org-todo-keywords-for-agenda
  10375. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10376. (setq org-done-keywords-for-agenda
  10377. (append org-done-keywords-for-agenda org-done-keywords))
  10378. (save-excursion
  10379. (remove-text-properties (point-min) (point-max) pall)
  10380. (when org-agenda-skip-archived-trees
  10381. (goto-char (point-min))
  10382. (while (re-search-forward rea nil t)
  10383. (if (org-on-heading-p t)
  10384. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10385. (goto-char (point-min))
  10386. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10387. (while (re-search-forward re nil t)
  10388. (add-text-properties
  10389. (match-beginning 0) (org-end-of-subtree t) pc)))
  10390. (set-buffer-modified-p bmp))))))
  10391. ;;;; Embedded LaTeX
  10392. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10393. "Keymap for the minor `org-cdlatex-mode'.")
  10394. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10395. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10396. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10397. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10398. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10399. (defvar org-cdlatex-texmathp-advice-is-done nil
  10400. "Flag remembering if we have applied the advice to texmathp already.")
  10401. (define-minor-mode org-cdlatex-mode
  10402. "Toggle the minor `org-cdlatex-mode'.
  10403. This mode supports entering LaTeX environment and math in LaTeX fragments
  10404. in Org-mode.
  10405. \\{org-cdlatex-mode-map}"
  10406. nil " OCDL" nil
  10407. (when org-cdlatex-mode (require 'cdlatex))
  10408. (unless org-cdlatex-texmathp-advice-is-done
  10409. (setq org-cdlatex-texmathp-advice-is-done t)
  10410. (defadvice texmathp (around org-math-always-on activate)
  10411. "Always return t in org-mode buffers.
  10412. This is because we want to insert math symbols without dollars even outside
  10413. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10414. en embedded LaTeX fragement, let texmathp do its job.
  10415. \\[org-cdlatex-mode-map]"
  10416. (interactive)
  10417. (let (p)
  10418. (cond
  10419. ((not (org-mode-p)) ad-do-it)
  10420. ((eq this-command 'cdlatex-math-symbol)
  10421. (setq ad-return-value t
  10422. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10423. (t
  10424. (let ((p (org-inside-LaTeX-fragment-p)))
  10425. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10426. (setq ad-return-value t
  10427. texmathp-why '("Org-mode embedded math" . 0))
  10428. (if p ad-do-it)))))))))
  10429. (defun turn-on-org-cdlatex ()
  10430. "Unconditionally turn on `org-cdlatex-mode'."
  10431. (org-cdlatex-mode 1))
  10432. (defun org-inside-LaTeX-fragment-p ()
  10433. "Test if point is inside a LaTeX fragment.
  10434. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10435. sequence appearing also before point.
  10436. Even though the matchers for math are configurable, this function assumes
  10437. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10438. delimiters are skipped when they have been removed by customization.
  10439. The return value is nil, or a cons cell with the delimiter and
  10440. and the position of this delimiter.
  10441. This function does a reasonably good job, but can locally be fooled by
  10442. for example currency specifications. For example it will assume being in
  10443. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10444. fragments that are properly closed, but during editing, we have to live
  10445. with the uncertainty caused by missing closing delimiters. This function
  10446. looks only before point, not after."
  10447. (catch 'exit
  10448. (let ((pos (point))
  10449. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10450. (lim (progn
  10451. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10452. (point)))
  10453. dd-on str (start 0) m re)
  10454. (goto-char pos)
  10455. (when dodollar
  10456. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10457. re (nth 1 (assoc "$" org-latex-regexps)))
  10458. (while (string-match re str start)
  10459. (cond
  10460. ((= (match-end 0) (length str))
  10461. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10462. ((= (match-end 0) (- (length str) 5))
  10463. (throw 'exit nil))
  10464. (t (setq start (match-end 0))))))
  10465. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10466. (goto-char pos)
  10467. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10468. (and (match-beginning 2) (throw 'exit nil))
  10469. ;; count $$
  10470. (while (re-search-backward "\\$\\$" lim t)
  10471. (setq dd-on (not dd-on)))
  10472. (goto-char pos)
  10473. (if dd-on (cons "$$" m))))))
  10474. (defun org-try-cdlatex-tab ()
  10475. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10476. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10477. - inside a LaTeX fragment, or
  10478. - after the first word in a line, where an abbreviation expansion could
  10479. insert a LaTeX environment."
  10480. (when org-cdlatex-mode
  10481. (cond
  10482. ((save-excursion
  10483. (skip-chars-backward "a-zA-Z0-9*")
  10484. (skip-chars-backward " \t")
  10485. (bolp))
  10486. (cdlatex-tab) t)
  10487. ((org-inside-LaTeX-fragment-p)
  10488. (cdlatex-tab) t)
  10489. (t nil))))
  10490. (defun org-cdlatex-underscore-caret (&optional arg)
  10491. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10492. Revert to the normal definition outside of these fragments."
  10493. (interactive "P")
  10494. (if (org-inside-LaTeX-fragment-p)
  10495. (call-interactively 'cdlatex-sub-superscript)
  10496. (let (org-cdlatex-mode)
  10497. (call-interactively (key-binding (vector last-input-event))))))
  10498. (defun org-cdlatex-math-modify (&optional arg)
  10499. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10500. Revert to the normal definition outside of these fragments."
  10501. (interactive "P")
  10502. (if (org-inside-LaTeX-fragment-p)
  10503. (call-interactively 'cdlatex-math-modify)
  10504. (let (org-cdlatex-mode)
  10505. (call-interactively (key-binding (vector last-input-event))))))
  10506. (defvar org-latex-fragment-image-overlays nil
  10507. "List of overlays carrying the images of latex fragments.")
  10508. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10509. (defun org-remove-latex-fragment-image-overlays ()
  10510. "Remove all overlays with LaTeX fragment images in current buffer."
  10511. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10512. (setq org-latex-fragment-image-overlays nil))
  10513. (defun org-preview-latex-fragment (&optional subtree)
  10514. "Preview the LaTeX fragment at point, or all locally or globally.
  10515. If the cursor is in a LaTeX fragment, create the image and overlay
  10516. it over the source code. If there is no fragment at point, display
  10517. all fragments in the current text, from one headline to the next. With
  10518. prefix SUBTREE, display all fragments in the current subtree. With a
  10519. double prefix `C-u C-u', or when the cursor is before the first headline,
  10520. display all fragments in the buffer.
  10521. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10522. (interactive "P")
  10523. (org-remove-latex-fragment-image-overlays)
  10524. (save-excursion
  10525. (save-restriction
  10526. (let (beg end at msg)
  10527. (cond
  10528. ((or (equal subtree '(16))
  10529. (not (save-excursion
  10530. (re-search-backward (concat "^" outline-regexp) nil t))))
  10531. (setq beg (point-min) end (point-max)
  10532. msg "Creating images for buffer...%s"))
  10533. ((equal subtree '(4))
  10534. (org-back-to-heading)
  10535. (setq beg (point) end (org-end-of-subtree t)
  10536. msg "Creating images for subtree...%s"))
  10537. (t
  10538. (if (setq at (org-inside-LaTeX-fragment-p))
  10539. (goto-char (max (point-min) (- (cdr at) 2)))
  10540. (org-back-to-heading))
  10541. (setq beg (point) end (progn (outline-next-heading) (point))
  10542. msg (if at "Creating image...%s"
  10543. "Creating images for entry...%s"))))
  10544. (message msg "")
  10545. (narrow-to-region beg end)
  10546. (goto-char beg)
  10547. (org-format-latex
  10548. (concat "ltxpng/" (file-name-sans-extension
  10549. (file-name-nondirectory
  10550. buffer-file-name)))
  10551. default-directory 'overlays msg at 'forbuffer)
  10552. (message msg "done. Use `C-c C-c' to remove images.")))))
  10553. (defvar org-latex-regexps
  10554. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10555. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10556. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10557. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10558. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10559. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10560. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10561. "Regular expressions for matching embedded LaTeX.")
  10562. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10563. "Replace LaTeX fragments with links to an image, and produce images."
  10564. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10565. (let* ((prefixnodir (file-name-nondirectory prefix))
  10566. (absprefix (expand-file-name prefix dir))
  10567. (todir (file-name-directory absprefix))
  10568. (opt org-format-latex-options)
  10569. (matchers (plist-get opt :matchers))
  10570. (re-list org-latex-regexps)
  10571. (cnt 0) txt link beg end re e checkdir
  10572. m n block linkfile movefile ov)
  10573. ;; Check if there are old images files with this prefix, and remove them
  10574. (when (file-directory-p todir)
  10575. (mapc 'delete-file
  10576. (directory-files
  10577. todir 'full
  10578. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10579. ;; Check the different regular expressions
  10580. (while (setq e (pop re-list))
  10581. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10582. block (if (nth 3 e) "\n\n" ""))
  10583. (when (member m matchers)
  10584. (goto-char (point-min))
  10585. (while (re-search-forward re nil t)
  10586. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10587. (setq txt (match-string n)
  10588. beg (match-beginning n) end (match-end n)
  10589. cnt (1+ cnt)
  10590. linkfile (format "%s_%04d.png" prefix cnt)
  10591. movefile (format "%s_%04d.png" absprefix cnt)
  10592. link (concat block "[[file:" linkfile "]]" block))
  10593. (if msg (message msg cnt))
  10594. (goto-char beg)
  10595. (unless checkdir ; make sure the directory exists
  10596. (setq checkdir t)
  10597. (or (file-directory-p todir) (make-directory todir)))
  10598. (org-create-formula-image
  10599. txt movefile opt forbuffer)
  10600. (if overlays
  10601. (progn
  10602. (setq ov (org-make-overlay beg end))
  10603. (if (featurep 'xemacs)
  10604. (progn
  10605. (org-overlay-put ov 'invisible t)
  10606. (org-overlay-put
  10607. ov 'end-glyph
  10608. (make-glyph (vector 'png :file movefile))))
  10609. (org-overlay-put
  10610. ov 'display
  10611. (list 'image :type 'png :file movefile :ascent 'center)))
  10612. (push ov org-latex-fragment-image-overlays)
  10613. (goto-char end))
  10614. (delete-region beg end)
  10615. (insert link))))))))
  10616. ;; This function borrows from Ganesh Swami's latex2png.el
  10617. (defun org-create-formula-image (string tofile options buffer)
  10618. (let* ((tmpdir (if (featurep 'xemacs)
  10619. (temp-directory)
  10620. temporary-file-directory))
  10621. (texfilebase (make-temp-name
  10622. (expand-file-name "orgtex" tmpdir)))
  10623. (texfile (concat texfilebase ".tex"))
  10624. (dvifile (concat texfilebase ".dvi"))
  10625. (pngfile (concat texfilebase ".png"))
  10626. (fnh (if (featurep 'xemacs)
  10627. (font-height (get-face-font 'default))
  10628. (face-attribute 'default :height nil)))
  10629. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10630. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10631. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10632. "Black"))
  10633. (bg (or (plist-get options (if buffer :background :html-background))
  10634. "Transparent")))
  10635. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10636. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10637. (with-temp-file texfile
  10638. (insert org-format-latex-header
  10639. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10640. (let ((dir default-directory))
  10641. (condition-case nil
  10642. (progn
  10643. (cd tmpdir)
  10644. (call-process "latex" nil nil nil texfile))
  10645. (error nil))
  10646. (cd dir))
  10647. (if (not (file-exists-p dvifile))
  10648. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10649. (call-process "dvipng" nil nil nil
  10650. "-E" "-fg" fg "-bg" bg
  10651. "-D" dpi
  10652. ;;"-x" scale "-y" scale
  10653. "-T" "tight"
  10654. "-o" pngfile
  10655. dvifile)
  10656. (if (not (file-exists-p pngfile))
  10657. (progn (message "Failed to create png file from %s" texfile) nil)
  10658. ;; Use the requested file name and clean up
  10659. (copy-file pngfile tofile 'replace)
  10660. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10661. (delete-file (concat texfilebase e)))
  10662. pngfile))))
  10663. (defun org-dvipng-color (attr)
  10664. "Return an rgb color specification for dvipng."
  10665. (apply 'format "rgb %s %s %s"
  10666. (mapcar 'org-normalize-color
  10667. (color-values (face-attribute 'default attr nil)))))
  10668. (defun org-normalize-color (value)
  10669. "Return string to be used as color value for an RGB component."
  10670. (format "%g" (/ value 65535.0)))
  10671. ;;;; Key bindings
  10672. ;; Make `C-c C-x' a prefix key
  10673. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10674. ;; TAB key with modifiers
  10675. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10676. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10677. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10678. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10679. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10680. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10681. ;; The following line is necessary under Suse GNU/Linux
  10682. (unless (featurep 'xemacs)
  10683. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10684. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10685. (define-key org-mode-map [backtab] 'org-shifttab)
  10686. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10687. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10688. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10689. ;; Cursor keys with modifiers
  10690. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10691. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10692. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10693. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10694. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10695. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10696. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10697. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10698. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10699. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10700. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10701. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10702. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10703. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10704. ;;; Extra keys for tty access.
  10705. ;; We only set them when really needed because otherwise the
  10706. ;; menus don't show the simple keys
  10707. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10708. (not window-system))
  10709. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10710. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10711. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10712. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10713. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10714. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10715. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10716. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10717. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10718. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10719. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10720. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10721. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10722. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10723. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10724. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10725. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10726. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10727. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10728. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10729. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10730. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10731. ;; All the other keys
  10732. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10733. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10734. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10735. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10736. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10737. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10738. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10739. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10740. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10741. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10742. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10743. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10744. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10745. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10746. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10747. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10748. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10749. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10750. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10751. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10752. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10753. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10754. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10755. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10756. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10757. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10758. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10759. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10760. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10761. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10762. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10763. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10764. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10765. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10766. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10767. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10768. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10769. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10770. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10771. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10772. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10773. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10774. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10775. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10776. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10777. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10778. (org-defkey org-mode-map "\C-m" 'org-return)
  10779. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10780. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10781. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  10782. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  10783. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  10784. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  10785. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  10786. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  10787. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  10788. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  10789. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  10790. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  10791. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  10792. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  10793. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  10794. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  10795. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  10796. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  10797. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  10798. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  10799. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  10800. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  10801. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  10802. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  10803. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  10804. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  10805. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  10806. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  10807. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  10808. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  10809. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  10810. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  10811. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  10812. (when (featurep 'xemacs)
  10813. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  10814. (defvar org-table-auto-blank-field) ; defined in org-table.el
  10815. (defun org-self-insert-command (N)
  10816. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10817. If the cursor is in a table looking at whitespace, the whitespace is
  10818. overwritten, and the table is not marked as requiring realignment."
  10819. (interactive "p")
  10820. (if (and (org-table-p)
  10821. (progn
  10822. ;; check if we blank the field, and if that triggers align
  10823. (and (featurep 'org-table) org-table-auto-blank-field
  10824. (member last-command
  10825. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  10826. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  10827. ;; got extra space, this field does not determine column width
  10828. (let (org-table-may-need-update) (org-table-blank-field))
  10829. ;; no extra space, this field may determine column width
  10830. (org-table-blank-field)))
  10831. t)
  10832. (eq N 1)
  10833. (looking-at "[^|\n]* |"))
  10834. (let (org-table-may-need-update)
  10835. (goto-char (1- (match-end 0)))
  10836. (delete-backward-char 1)
  10837. (goto-char (match-beginning 0))
  10838. (self-insert-command N))
  10839. (setq org-table-may-need-update t)
  10840. (self-insert-command N)
  10841. (org-fix-tags-on-the-fly)))
  10842. (defun org-fix-tags-on-the-fly ()
  10843. (when (and (equal (char-after (point-at-bol)) ?*)
  10844. (org-on-heading-p))
  10845. (org-align-tags-here org-tags-column)))
  10846. (defun org-delete-backward-char (N)
  10847. "Like `delete-backward-char', insert whitespace at field end in tables.
  10848. When deleting backwards, in tables this function will insert whitespace in
  10849. front of the next \"|\" separator, to keep the table aligned. The table will
  10850. still be marked for re-alignment if the field did fill the entire column,
  10851. because, in this case the deletion might narrow the column."
  10852. (interactive "p")
  10853. (if (and (org-table-p)
  10854. (eq N 1)
  10855. (string-match "|" (buffer-substring (point-at-bol) (point)))
  10856. (looking-at ".*?|"))
  10857. (let ((pos (point))
  10858. (noalign (looking-at "[^|\n\r]* |"))
  10859. (c org-table-may-need-update))
  10860. (backward-delete-char N)
  10861. (skip-chars-forward "^|")
  10862. (insert " ")
  10863. (goto-char (1- pos))
  10864. ;; noalign: if there were two spaces at the end, this field
  10865. ;; does not determine the width of the column.
  10866. (if noalign (setq org-table-may-need-update c)))
  10867. (backward-delete-char N)
  10868. (org-fix-tags-on-the-fly)))
  10869. (defun org-delete-char (N)
  10870. "Like `delete-char', but insert whitespace at field end in tables.
  10871. When deleting characters, in tables this function will insert whitespace in
  10872. front of the next \"|\" separator, to keep the table aligned. The table will
  10873. still be marked for re-alignment if the field did fill the entire column,
  10874. because, in this case the deletion might narrow the column."
  10875. (interactive "p")
  10876. (if (and (org-table-p)
  10877. (not (bolp))
  10878. (not (= (char-after) ?|))
  10879. (eq N 1))
  10880. (if (looking-at ".*?|")
  10881. (let ((pos (point))
  10882. (noalign (looking-at "[^|\n\r]* |"))
  10883. (c org-table-may-need-update))
  10884. (replace-match (concat
  10885. (substring (match-string 0) 1 -1)
  10886. " |"))
  10887. (goto-char pos)
  10888. ;; noalign: if there were two spaces at the end, this field
  10889. ;; does not determine the width of the column.
  10890. (if noalign (setq org-table-may-need-update c)))
  10891. (delete-char N))
  10892. (delete-char N)
  10893. (org-fix-tags-on-the-fly)))
  10894. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  10895. (put 'org-self-insert-command 'delete-selection t)
  10896. (put 'orgtbl-self-insert-command 'delete-selection t)
  10897. (put 'org-delete-char 'delete-selection 'supersede)
  10898. (put 'org-delete-backward-char 'delete-selection 'supersede)
  10899. ;; Make `flyspell-mode' delay after some commands
  10900. (put 'org-self-insert-command 'flyspell-delayed t)
  10901. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  10902. (put 'org-delete-char 'flyspell-delayed t)
  10903. (put 'org-delete-backward-char 'flyspell-delayed t)
  10904. ;; Make pabbrev-mode expand after org-mode commands
  10905. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  10906. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  10907. ;; How to do this: Measure non-white length of current string
  10908. ;; If equal to column width, we should realign.
  10909. (defun org-remap (map &rest commands)
  10910. "In MAP, remap the functions given in COMMANDS.
  10911. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  10912. (let (new old)
  10913. (while commands
  10914. (setq old (pop commands) new (pop commands))
  10915. (if (fboundp 'command-remapping)
  10916. (org-defkey map (vector 'remap old) new)
  10917. (substitute-key-definition old new map global-map)))))
  10918. (when (eq org-enable-table-editor 'optimized)
  10919. ;; If the user wants maximum table support, we need to hijack
  10920. ;; some standard editing functions
  10921. (org-remap org-mode-map
  10922. 'self-insert-command 'org-self-insert-command
  10923. 'delete-char 'org-delete-char
  10924. 'delete-backward-char 'org-delete-backward-char)
  10925. (org-defkey org-mode-map "|" 'org-force-self-insert))
  10926. (defun org-shiftcursor-error ()
  10927. "Throw an error because Shift-Cursor command was applied in wrong context."
  10928. (error "This command is active in special context like tables, headlines or timestamps"))
  10929. (defun org-shifttab (&optional arg)
  10930. "Global visibility cycling or move to previous table field.
  10931. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  10932. on context.
  10933. See the individual commands for more information."
  10934. (interactive "P")
  10935. (cond
  10936. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  10937. (arg (message "Content view to level: ")
  10938. (org-content (prefix-numeric-value arg))
  10939. (setq org-cycle-global-status 'overview))
  10940. (t (call-interactively 'org-global-cycle))))
  10941. (defun org-shiftmetaleft ()
  10942. "Promote subtree or delete table column.
  10943. Calls `org-promote-subtree', `org-outdent-item',
  10944. or `org-table-delete-column', depending on context.
  10945. See the individual commands for more information."
  10946. (interactive)
  10947. (cond
  10948. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  10949. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  10950. ((org-at-item-p) (call-interactively 'org-outdent-item))
  10951. (t (org-shiftcursor-error))))
  10952. (defun org-shiftmetaright ()
  10953. "Demote subtree or insert table column.
  10954. Calls `org-demote-subtree', `org-indent-item',
  10955. or `org-table-insert-column', depending on context.
  10956. See the individual commands for more information."
  10957. (interactive)
  10958. (cond
  10959. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  10960. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  10961. ((org-at-item-p) (call-interactively 'org-indent-item))
  10962. (t (org-shiftcursor-error))))
  10963. (defun org-shiftmetaup (&optional arg)
  10964. "Move subtree up or kill table row.
  10965. Calls `org-move-subtree-up' or `org-table-kill-row' or
  10966. `org-move-item-up' depending on context. See the individual commands
  10967. for more information."
  10968. (interactive "P")
  10969. (cond
  10970. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  10971. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  10972. ((org-at-item-p) (call-interactively 'org-move-item-up))
  10973. (t (org-shiftcursor-error))))
  10974. (defun org-shiftmetadown (&optional arg)
  10975. "Move subtree down or insert table row.
  10976. Calls `org-move-subtree-down' or `org-table-insert-row' or
  10977. `org-move-item-down', depending on context. See the individual
  10978. commands for more information."
  10979. (interactive "P")
  10980. (cond
  10981. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  10982. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  10983. ((org-at-item-p) (call-interactively 'org-move-item-down))
  10984. (t (org-shiftcursor-error))))
  10985. (defun org-metaleft (&optional arg)
  10986. "Promote heading or move table column to left.
  10987. Calls `org-do-promote' or `org-table-move-column', depending on context.
  10988. With no specific context, calls the Emacs default `backward-word'.
  10989. See the individual commands for more information."
  10990. (interactive "P")
  10991. (cond
  10992. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  10993. ((or (org-on-heading-p) (org-region-active-p))
  10994. (call-interactively 'org-do-promote))
  10995. ((org-at-item-p) (call-interactively 'org-outdent-item))
  10996. (t (call-interactively 'backward-word))))
  10997. (defun org-metaright (&optional arg)
  10998. "Demote subtree or move table column to right.
  10999. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11000. With no specific context, calls the Emacs default `forward-word'.
  11001. See the individual commands for more information."
  11002. (interactive "P")
  11003. (cond
  11004. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11005. ((or (org-on-heading-p) (org-region-active-p))
  11006. (call-interactively 'org-do-demote))
  11007. ((org-at-item-p) (call-interactively 'org-indent-item))
  11008. (t (call-interactively 'forward-word))))
  11009. (defun org-metaup (&optional arg)
  11010. "Move subtree up or move table row up.
  11011. Calls `org-move-subtree-up' or `org-table-move-row' or
  11012. `org-move-item-up', depending on context. See the individual commands
  11013. for more information."
  11014. (interactive "P")
  11015. (cond
  11016. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11017. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11018. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11019. (t (transpose-lines 1) (beginning-of-line -1))))
  11020. (defun org-metadown (&optional arg)
  11021. "Move subtree down or move table row down.
  11022. Calls `org-move-subtree-down' or `org-table-move-row' or
  11023. `org-move-item-down', depending on context. See the individual
  11024. commands for more information."
  11025. (interactive "P")
  11026. (cond
  11027. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11028. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11029. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11030. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11031. (defun org-shiftup (&optional arg)
  11032. "Increase item in timestamp or increase priority of current headline.
  11033. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11034. depending on context. See the individual commands for more information."
  11035. (interactive "P")
  11036. (cond
  11037. ((org-at-timestamp-p t)
  11038. (call-interactively (if org-edit-timestamp-down-means-later
  11039. 'org-timestamp-down 'org-timestamp-up)))
  11040. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11041. ((org-at-item-p) (call-interactively 'org-previous-item))
  11042. ((org-clocktable-try-shift 'up arg))
  11043. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11044. (defun org-shiftdown (&optional arg)
  11045. "Decrease item in timestamp or decrease priority of current headline.
  11046. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11047. depending on context. See the individual commands for more information."
  11048. (interactive "P")
  11049. (cond
  11050. ((org-at-timestamp-p t)
  11051. (call-interactively (if org-edit-timestamp-down-means-later
  11052. 'org-timestamp-up 'org-timestamp-down)))
  11053. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11054. ((org-clocktable-try-shift 'down arg))
  11055. (t (call-interactively 'org-next-item))))
  11056. (defun org-shiftright (&optional arg)
  11057. "Next TODO keyword or timestamp one day later, depending on context."
  11058. (interactive "P")
  11059. (cond
  11060. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11061. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11062. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11063. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11064. ((org-clocktable-try-shift 'right arg))
  11065. (t (org-shiftcursor-error))))
  11066. (defun org-shiftleft (&optional arg)
  11067. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11068. (interactive "P")
  11069. (cond
  11070. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11071. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11072. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11073. ((org-at-property-p)
  11074. (call-interactively 'org-property-previous-allowed-value))
  11075. ((org-clocktable-try-shift 'left arg))
  11076. (t (org-shiftcursor-error))))
  11077. (defun org-shiftcontrolright ()
  11078. "Switch to next TODO set."
  11079. (interactive)
  11080. (cond
  11081. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11082. (t (org-shiftcursor-error))))
  11083. (defun org-shiftcontrolleft ()
  11084. "Switch to previous TODO set."
  11085. (interactive)
  11086. (cond
  11087. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11088. (t (org-shiftcursor-error))))
  11089. (defun org-ctrl-c-ret ()
  11090. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11091. (interactive)
  11092. (cond
  11093. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11094. (t (call-interactively 'org-insert-heading))))
  11095. (defun org-copy-special ()
  11096. "Copy region in table or copy current subtree.
  11097. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11098. See the individual commands for more information."
  11099. (interactive)
  11100. (call-interactively
  11101. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11102. (defun org-cut-special ()
  11103. "Cut region in table or cut current subtree.
  11104. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11105. See the individual commands for more information."
  11106. (interactive)
  11107. (call-interactively
  11108. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11109. (defun org-paste-special (arg)
  11110. "Paste rectangular region into table, or past subtree relative to level.
  11111. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11112. See the individual commands for more information."
  11113. (interactive "P")
  11114. (if (org-at-table-p)
  11115. (org-table-paste-rectangle)
  11116. (org-paste-subtree arg)))
  11117. (defun org-ctrl-c-ctrl-c (&optional arg)
  11118. "Set tags in headline, or update according to changed information at point.
  11119. This command does many different things, depending on context:
  11120. - If the cursor is in a headline, prompt for tags and insert them
  11121. into the current line, aligned to `org-tags-column'. When called
  11122. with prefix arg, realign all tags in the current buffer.
  11123. - If the cursor is in one of the special #+KEYWORD lines, this
  11124. triggers scanning the buffer for these lines and updating the
  11125. information.
  11126. - If the cursor is inside a table, realign the table. This command
  11127. works even if the automatic table editor has been turned off.
  11128. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11129. the entire table.
  11130. - If the cursor is a the beginning of a dynamic block, update it.
  11131. - If the cursor is inside a table created by the table.el package,
  11132. activate that table.
  11133. - If the current buffer is a remember buffer, close note and file it.
  11134. with a prefix argument, file it without further interaction to the default
  11135. location.
  11136. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11137. links in this buffer.
  11138. - If the cursor is on a numbered item in a plain list, renumber the
  11139. ordered list.
  11140. - If the cursor is on a checkbox, toggle it."
  11141. (interactive "P")
  11142. (let ((org-enable-table-editor t))
  11143. (cond
  11144. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11145. org-occur-highlights
  11146. org-latex-fragment-image-overlays)
  11147. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11148. (org-remove-occur-highlights)
  11149. (org-remove-latex-fragment-image-overlays)
  11150. (message "Temporary highlights/overlays removed from current buffer"))
  11151. ((and (local-variable-p 'org-finish-function (current-buffer))
  11152. (fboundp org-finish-function))
  11153. (funcall org-finish-function))
  11154. ((org-at-property-p)
  11155. (call-interactively 'org-property-action))
  11156. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11157. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11158. ((org-at-table.el-p)
  11159. (require 'table)
  11160. (beginning-of-line 1)
  11161. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11162. (call-interactively 'table-recognize-table))
  11163. ((org-at-table-p)
  11164. (org-table-maybe-eval-formula)
  11165. (if arg
  11166. (call-interactively 'org-table-recalculate)
  11167. (org-table-maybe-recalculate-line))
  11168. (call-interactively 'org-table-align))
  11169. ((org-at-item-checkbox-p)
  11170. (call-interactively 'org-toggle-checkbox))
  11171. ((org-at-item-p)
  11172. (call-interactively 'org-maybe-renumber-ordered-list))
  11173. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11174. ;; Dynamic block
  11175. (beginning-of-line 1)
  11176. (org-update-dblock))
  11177. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11178. (cond
  11179. ((equal (match-string 1) "TBLFM")
  11180. ;; Recalculate the table before this line
  11181. (save-excursion
  11182. (beginning-of-line 1)
  11183. (skip-chars-backward " \r\n\t")
  11184. (if (org-at-table-p)
  11185. (org-call-with-arg 'org-table-recalculate t))))
  11186. (t
  11187. (call-interactively 'org-mode-restart))))
  11188. (t (error "C-c C-c can do nothing useful at this location.")))))
  11189. (defun org-mode-restart ()
  11190. "Restart Org-mode, to scan again for special lines.
  11191. Also updates the keyword regular expressions."
  11192. (interactive)
  11193. (let ((org-inhibit-startup t)) (org-mode))
  11194. (message "Org-mode restarted to refresh keyword and special line setup"))
  11195. (defun org-kill-note-or-show-branches ()
  11196. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11197. (interactive)
  11198. (if (not org-finish-function)
  11199. (call-interactively 'show-branches)
  11200. (let ((org-note-abort t))
  11201. (funcall org-finish-function))))
  11202. (defun org-return (&optional indent)
  11203. "Goto next table row or insert a newline.
  11204. Calls `org-table-next-row' or `newline', depending on context.
  11205. See the individual commands for more information."
  11206. (interactive)
  11207. (cond
  11208. ((bobp) (if indent (newline-and-indent) (newline)))
  11209. ((and (org-at-heading-p)
  11210. (looking-at
  11211. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11212. (org-show-entry)
  11213. (end-of-line 1)
  11214. (newline))
  11215. ((org-at-table-p)
  11216. (org-table-justify-field-maybe)
  11217. (call-interactively 'org-table-next-row))
  11218. (t (if indent (newline-and-indent) (newline)))))
  11219. (defun org-return-indent ()
  11220. "Goto next table row or insert a newline and indent.
  11221. Calls `org-table-next-row' or `newline-and-indent', depending on
  11222. context. See the individual commands for more information."
  11223. (interactive)
  11224. (org-return t))
  11225. (defun org-ctrl-c-star ()
  11226. "Compute table, or change heading status of lines.
  11227. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11228. depending on context. This will also turn a plain list item or a normal
  11229. line into a subheading."
  11230. (interactive)
  11231. (cond
  11232. ((org-at-table-p)
  11233. (call-interactively 'org-table-recalculate))
  11234. ((org-region-active-p)
  11235. ;; Convert all lines in region to list items
  11236. (call-interactively 'org-toggle-region-headings))
  11237. ((org-on-heading-p)
  11238. (org-toggle-region-headings (point-at-bol)
  11239. (min (1+ (point-at-eol)) (point-max))))
  11240. ((org-at-item-p)
  11241. ;; Convert to heading
  11242. (let ((level (save-match-data
  11243. (save-excursion
  11244. (condition-case nil
  11245. (progn
  11246. (org-back-to-heading t)
  11247. (funcall outline-level))
  11248. (error 0))))))
  11249. (replace-match
  11250. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11251. (t (org-toggle-region-headings (point-at-bol)
  11252. (min (1+ (point-at-eol)) (point-max))))))
  11253. (defun org-ctrl-c-minus ()
  11254. "Insert separator line in table or modify bullet status of line.
  11255. Also turns a plain line or a region of lines into list items.
  11256. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11257. `org-cycle-list-bullet', depending on context."
  11258. (interactive)
  11259. (cond
  11260. ((org-at-table-p)
  11261. (call-interactively 'org-table-insert-hline))
  11262. ((org-on-heading-p)
  11263. ;; Convert to item
  11264. (save-excursion
  11265. (beginning-of-line 1)
  11266. (if (looking-at "\\*+ ")
  11267. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11268. ((org-region-active-p)
  11269. ;; Convert all lines in region to list items
  11270. (call-interactively 'org-toggle-region-items))
  11271. ((org-in-item-p)
  11272. (call-interactively 'org-cycle-list-bullet))
  11273. (t (org-toggle-region-items (point-at-bol)
  11274. (min (1+ (point-at-eol)) (point-max))))))
  11275. (defun org-toggle-region-items (beg end)
  11276. "Convert all lines in region to list items.
  11277. If the first line is already an item, convert all list items in the region
  11278. to normal lines."
  11279. (interactive "r")
  11280. (let (l2 l)
  11281. (save-excursion
  11282. (goto-char end)
  11283. (setq l2 (org-current-line))
  11284. (goto-char beg)
  11285. (beginning-of-line 1)
  11286. (setq l (1- (org-current-line)))
  11287. (if (org-at-item-p)
  11288. ;; We already have items, de-itemize
  11289. (while (< (setq l (1+ l)) l2)
  11290. (when (org-at-item-p)
  11291. (goto-char (match-beginning 2))
  11292. (delete-region (match-beginning 2) (match-end 2))
  11293. (and (looking-at "[ \t]+") (replace-match "")))
  11294. (beginning-of-line 2))
  11295. (while (< (setq l (1+ l)) l2)
  11296. (unless (org-at-item-p)
  11297. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11298. (replace-match "\\1- \\2")))
  11299. (beginning-of-line 2))))))
  11300. (defun org-toggle-region-headings (beg end)
  11301. "Convert all lines in region to list items.
  11302. If the first line is already an item, convert all list items in the region
  11303. to normal lines."
  11304. (interactive "r")
  11305. (let (l2 l)
  11306. (save-excursion
  11307. (goto-char end)
  11308. (setq l2 (org-current-line))
  11309. (goto-char beg)
  11310. (beginning-of-line 1)
  11311. (setq l (1- (org-current-line)))
  11312. (if (org-on-heading-p)
  11313. ;; We already have headlines, de-star them
  11314. (while (< (setq l (1+ l)) l2)
  11315. (when (org-on-heading-p t)
  11316. (and (looking-at outline-regexp) (replace-match "")))
  11317. (beginning-of-line 2))
  11318. (let* ((stars (save-excursion
  11319. (re-search-backward org-complex-heading-regexp nil t)
  11320. (or (match-string 1) "*")))
  11321. (add-stars (if org-odd-levels-only "**" "*"))
  11322. (rpl (concat stars add-stars " \\2")))
  11323. (while (< (setq l (1+ l)) l2)
  11324. (unless (org-on-heading-p)
  11325. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11326. (replace-match rpl)))
  11327. (beginning-of-line 2)))))))
  11328. (defun org-meta-return (&optional arg)
  11329. "Insert a new heading or wrap a region in a table.
  11330. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11331. See the individual commands for more information."
  11332. (interactive "P")
  11333. (cond
  11334. ((org-at-table-p)
  11335. (call-interactively 'org-table-wrap-region))
  11336. (t (call-interactively 'org-insert-heading))))
  11337. ;;; Menu entries
  11338. ;; Define the Org-mode menus
  11339. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11340. '("Tbl"
  11341. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11342. ["Next Field" org-cycle (org-at-table-p)]
  11343. ["Previous Field" org-shifttab (org-at-table-p)]
  11344. ["Next Row" org-return (org-at-table-p)]
  11345. "--"
  11346. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11347. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11348. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11349. "--"
  11350. ("Column"
  11351. ["Move Column Left" org-metaleft (org-at-table-p)]
  11352. ["Move Column Right" org-metaright (org-at-table-p)]
  11353. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11354. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11355. ("Row"
  11356. ["Move Row Up" org-metaup (org-at-table-p)]
  11357. ["Move Row Down" org-metadown (org-at-table-p)]
  11358. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11359. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11360. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11361. "--"
  11362. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11363. ("Rectangle"
  11364. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11365. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11366. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11367. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11368. "--"
  11369. ("Calculate"
  11370. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11371. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11372. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11373. "--"
  11374. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11375. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11376. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11377. "--"
  11378. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11379. "--"
  11380. ["Sum Column/Rectangle" org-table-sum
  11381. (or (org-at-table-p) (org-region-active-p))]
  11382. ["Which Column?" org-table-current-column (org-at-table-p)])
  11383. ["Debug Formulas"
  11384. org-table-toggle-formula-debugger
  11385. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11386. ["Show Col/Row Numbers"
  11387. org-table-toggle-coordinate-overlays
  11388. :style toggle
  11389. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11390. "--"
  11391. ["Create" org-table-create (and (not (org-at-table-p))
  11392. org-enable-table-editor)]
  11393. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11394. ["Import from File" org-table-import (not (org-at-table-p))]
  11395. ["Export to File" org-table-export (org-at-table-p)]
  11396. "--"
  11397. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11398. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11399. '("Org"
  11400. ("Show/Hide"
  11401. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11402. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11403. ["Sparse Tree..." org-sparse-tree t]
  11404. ["Reveal Context" org-reveal t]
  11405. ["Show All" show-all t]
  11406. "--"
  11407. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11408. "--"
  11409. ["New Heading" org-insert-heading t]
  11410. ("Navigate Headings"
  11411. ["Up" outline-up-heading t]
  11412. ["Next" outline-next-visible-heading t]
  11413. ["Previous" outline-previous-visible-heading t]
  11414. ["Next Same Level" outline-forward-same-level t]
  11415. ["Previous Same Level" outline-backward-same-level t]
  11416. "--"
  11417. ["Jump" org-goto t])
  11418. ("Edit Structure"
  11419. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11420. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11421. "--"
  11422. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11423. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11424. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11425. "--"
  11426. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11427. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11428. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11429. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11430. "--"
  11431. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11432. "--"
  11433. ["Convert to odd levels" org-convert-to-odd-levels t]
  11434. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11435. ("Editing"
  11436. ["Emphasis..." org-emphasize t])
  11437. ("Archive"
  11438. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11439. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11440. ; :active t :keys "C-u C-c C-x C-a"]
  11441. ["Sparse trees open ARCHIVE trees"
  11442. (setq org-sparse-tree-open-archived-trees
  11443. (not org-sparse-tree-open-archived-trees))
  11444. :style toggle :selected org-sparse-tree-open-archived-trees]
  11445. ["Cycling opens ARCHIVE trees"
  11446. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11447. :style toggle :selected org-cycle-open-archived-trees]
  11448. ["Agenda includes ARCHIVE trees"
  11449. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11450. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11451. "--"
  11452. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11453. ; ["Check and Move Children" (org-archive-subtree '(4))
  11454. ; :active t :keys "C-u C-c C-x C-s"]
  11455. )
  11456. "--"
  11457. ("TODO Lists"
  11458. ["TODO/DONE/-" org-todo t]
  11459. ("Select keyword"
  11460. ["Next keyword" org-shiftright (org-on-heading-p)]
  11461. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11462. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11463. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11464. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11465. ["Show TODO Tree" org-show-todo-tree t]
  11466. ["Global TODO list" org-todo-list t]
  11467. "--"
  11468. ["Set Priority" org-priority t]
  11469. ["Priority Up" org-shiftup t]
  11470. ["Priority Down" org-shiftdown t])
  11471. ("TAGS and Properties"
  11472. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11473. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11474. "--"
  11475. ["Set property" 'org-set-property t]
  11476. ["Column view of properties" org-columns t]
  11477. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11478. ("Dates and Scheduling"
  11479. ["Timestamp" org-time-stamp t]
  11480. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11481. ("Change Date"
  11482. ["1 Day Later" org-shiftright t]
  11483. ["1 Day Earlier" org-shiftleft t]
  11484. ["1 ... Later" org-shiftup t]
  11485. ["1 ... Earlier" org-shiftdown t])
  11486. ["Compute Time Range" org-evaluate-time-range t]
  11487. ["Schedule Item" org-schedule t]
  11488. ["Deadline" org-deadline t]
  11489. "--"
  11490. ["Custom time format" org-toggle-time-stamp-overlays
  11491. :style radio :selected org-display-custom-times]
  11492. "--"
  11493. ["Goto Calendar" org-goto-calendar t]
  11494. ["Date from Calendar" org-date-from-calendar t])
  11495. ("Logging work"
  11496. ["Clock in" org-clock-in t]
  11497. ["Clock out" org-clock-out t]
  11498. ["Clock cancel" org-clock-cancel t]
  11499. ["Goto running clock" org-clock-goto t]
  11500. ["Display times" org-clock-display t]
  11501. ["Create clock table" org-clock-report t]
  11502. "--"
  11503. ["Record DONE time"
  11504. (progn (setq org-log-done (not org-log-done))
  11505. (message "Switching to %s will %s record a timestamp"
  11506. (car org-done-keywords)
  11507. (if org-log-done "automatically" "not")))
  11508. :style toggle :selected org-log-done])
  11509. "--"
  11510. ["Agenda Command..." org-agenda t]
  11511. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11512. ("File List for Agenda")
  11513. ("Special views current file"
  11514. ["TODO Tree" org-show-todo-tree t]
  11515. ["Check Deadlines" org-check-deadlines t]
  11516. ["Timeline" org-timeline t]
  11517. ["Tags Tree" org-tags-sparse-tree t])
  11518. "--"
  11519. ("Hyperlinks"
  11520. ["Store Link (Global)" org-store-link t]
  11521. ["Insert Link" org-insert-link t]
  11522. ["Follow Link" org-open-at-point t]
  11523. "--"
  11524. ["Next link" org-next-link t]
  11525. ["Previous link" org-previous-link t]
  11526. "--"
  11527. ["Descriptive Links"
  11528. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11529. :style radio
  11530. :selected (member '(org-link) buffer-invisibility-spec)]
  11531. ["Literal Links"
  11532. (progn
  11533. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11534. :style radio
  11535. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11536. "--"
  11537. ["Export/Publish..." org-export t]
  11538. ("LaTeX"
  11539. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11540. :selected org-cdlatex-mode]
  11541. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11542. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11543. ["Modify math symbol" org-cdlatex-math-modify
  11544. (org-inside-LaTeX-fragment-p)]
  11545. ["Export LaTeX fragments as images"
  11546. (if (featurep 'org-exp)
  11547. (setq org-export-with-LaTeX-fragments
  11548. (not org-export-with-LaTeX-fragments))
  11549. (require 'org-exp))
  11550. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11551. org-export-with-LaTeX-fragments)])
  11552. "--"
  11553. ("Documentation"
  11554. ["Show Version" org-version t]
  11555. ["Info Documentation" org-info t])
  11556. ("Customize"
  11557. ["Browse Org Group" org-customize t]
  11558. "--"
  11559. ["Expand This Menu" org-create-customize-menu
  11560. (fboundp 'customize-menu-create)])
  11561. "--"
  11562. ["Refresh setup" org-mode-restart t]
  11563. ))
  11564. (defun org-info (&optional node)
  11565. "Read documentation for Org-mode in the info system.
  11566. With optional NODE, go directly to that node."
  11567. (interactive)
  11568. (info (format "(org)%s" (or node ""))))
  11569. (defun org-install-agenda-files-menu ()
  11570. (let ((bl (buffer-list)))
  11571. (save-excursion
  11572. (while bl
  11573. (set-buffer (pop bl))
  11574. (if (org-mode-p) (setq bl nil)))
  11575. (when (org-mode-p)
  11576. (easy-menu-change
  11577. '("Org") "File List for Agenda"
  11578. (append
  11579. (list
  11580. ["Edit File List" (org-edit-agenda-file-list) t]
  11581. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11582. ["Remove Current File from List" org-remove-file t]
  11583. ["Cycle through agenda files" org-cycle-agenda-files t]
  11584. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11585. "--")
  11586. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11587. ;;;; Documentation
  11588. (defun org-require-autoloaded-modules ()
  11589. (interactive)
  11590. (mapc 'require
  11591. '(org-agenda org-archive org-clock org-colview
  11592. org-exp org-export-latex org-publish
  11593. org-remember org-table)))
  11594. (defun org-customize ()
  11595. "Call the customize function with org as argument."
  11596. (interactive)
  11597. (org-load-modules-maybe)
  11598. (org-require-autoloaded-modules)
  11599. (customize-browse 'org))
  11600. (defun org-create-customize-menu ()
  11601. "Create a full customization menu for Org-mode, insert it into the menu."
  11602. (interactive)
  11603. (org-load-modules-maybe)
  11604. (org-require-autoloaded-modules)
  11605. (if (fboundp 'customize-menu-create)
  11606. (progn
  11607. (easy-menu-change
  11608. '("Org") "Customize"
  11609. `(["Browse Org group" org-customize t]
  11610. "--"
  11611. ,(customize-menu-create 'org)
  11612. ["Set" Custom-set t]
  11613. ["Save" Custom-save t]
  11614. ["Reset to Current" Custom-reset-current t]
  11615. ["Reset to Saved" Custom-reset-saved t]
  11616. ["Reset to Standard Settings" Custom-reset-standard t]))
  11617. (message "\"Org\"-menu now contains full customization menu"))
  11618. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11619. ;;;; Miscellaneous stuff
  11620. ;;; Generally useful functions
  11621. (defun org-plist-delete (plist property)
  11622. "Delete PROPERTY from PLIST.
  11623. This is in contrast to merely setting it to 0."
  11624. (let (p)
  11625. (while plist
  11626. (if (not (eq property (car plist)))
  11627. (setq p (plist-put p (car plist) (nth 1 plist))))
  11628. (setq plist (cddr plist)))
  11629. p))
  11630. (defun org-force-self-insert (N)
  11631. "Needed to enforce self-insert under remapping."
  11632. (interactive "p")
  11633. (self-insert-command N))
  11634. (defun org-string-width (s)
  11635. "Compute width of string, ignoring invisible characters.
  11636. This ignores character with invisibility property `org-link', and also
  11637. characters with property `org-cwidth', because these will become invisible
  11638. upon the next fontification round."
  11639. (let (b l)
  11640. (when (or (eq t buffer-invisibility-spec)
  11641. (assq 'org-link buffer-invisibility-spec))
  11642. (while (setq b (text-property-any 0 (length s)
  11643. 'invisible 'org-link s))
  11644. (setq s (concat (substring s 0 b)
  11645. (substring s (or (next-single-property-change
  11646. b 'invisible s) (length s)))))))
  11647. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11648. (setq s (concat (substring s 0 b)
  11649. (substring s (or (next-single-property-change
  11650. b 'org-cwidth s) (length s))))))
  11651. (setq l (string-width s) b -1)
  11652. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11653. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11654. l))
  11655. (defun org-trim (s)
  11656. "Remove whitespace at beginning and end of string."
  11657. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11658. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11659. s)
  11660. (defun org-wrap (string &optional width lines)
  11661. "Wrap string to either a number of lines, or a width in characters.
  11662. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11663. that costs. If there is a word longer than WIDTH, the text is actually
  11664. wrapped to the length of that word.
  11665. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11666. many lines, whatever width that takes.
  11667. The return value is a list of lines, without newlines at the end."
  11668. (let* ((words (org-split-string string "[ \t\n]+"))
  11669. (maxword (apply 'max (mapcar 'org-string-width words)))
  11670. w ll)
  11671. (cond (width
  11672. (org-do-wrap words (max maxword width)))
  11673. (lines
  11674. (setq w maxword)
  11675. (setq ll (org-do-wrap words maxword))
  11676. (if (<= (length ll) lines)
  11677. ll
  11678. (setq ll words)
  11679. (while (> (length ll) lines)
  11680. (setq w (1+ w))
  11681. (setq ll (org-do-wrap words w)))
  11682. ll))
  11683. (t (error "Cannot wrap this")))))
  11684. (defun org-do-wrap (words width)
  11685. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11686. (let (lines line)
  11687. (while words
  11688. (setq line (pop words))
  11689. (while (and words (< (+ (length line) (length (car words))) width))
  11690. (setq line (concat line " " (pop words))))
  11691. (setq lines (push line lines)))
  11692. (nreverse lines)))
  11693. (defun org-split-string (string &optional separators)
  11694. "Splits STRING into substrings at SEPARATORS.
  11695. No empty strings are returned if there are matches at the beginning
  11696. and end of string."
  11697. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11698. (start 0)
  11699. notfirst
  11700. (list nil))
  11701. (while (and (string-match rexp string
  11702. (if (and notfirst
  11703. (= start (match-beginning 0))
  11704. (< start (length string)))
  11705. (1+ start) start))
  11706. (< (match-beginning 0) (length string)))
  11707. (setq notfirst t)
  11708. (or (eq (match-beginning 0) 0)
  11709. (and (eq (match-beginning 0) (match-end 0))
  11710. (eq (match-beginning 0) start))
  11711. (setq list
  11712. (cons (substring string start (match-beginning 0))
  11713. list)))
  11714. (setq start (match-end 0)))
  11715. (or (eq start (length string))
  11716. (setq list
  11717. (cons (substring string start)
  11718. list)))
  11719. (nreverse list)))
  11720. (defun org-context ()
  11721. "Return a list of contexts of the current cursor position.
  11722. If several contexts apply, all are returned.
  11723. Each context entry is a list with a symbol naming the context, and
  11724. two positions indicating start and end of the context. Possible
  11725. contexts are:
  11726. :headline anywhere in a headline
  11727. :headline-stars on the leading stars in a headline
  11728. :todo-keyword on a TODO keyword (including DONE) in a headline
  11729. :tags on the TAGS in a headline
  11730. :priority on the priority cookie in a headline
  11731. :item on the first line of a plain list item
  11732. :item-bullet on the bullet/number of a plain list item
  11733. :checkbox on the checkbox in a plain list item
  11734. :table in an org-mode table
  11735. :table-special on a special filed in a table
  11736. :table-table in a table.el table
  11737. :link on a hyperlink
  11738. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11739. :target on a <<target>>
  11740. :radio-target on a <<<radio-target>>>
  11741. :latex-fragment on a LaTeX fragment
  11742. :latex-preview on a LaTeX fragment with overlayed preview image
  11743. This function expects the position to be visible because it uses font-lock
  11744. faces as a help to recognize the following contexts: :table-special, :link,
  11745. and :keyword."
  11746. (let* ((f (get-text-property (point) 'face))
  11747. (faces (if (listp f) f (list f)))
  11748. (p (point)) clist o)
  11749. ;; First the large context
  11750. (cond
  11751. ((org-on-heading-p t)
  11752. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  11753. (when (progn
  11754. (beginning-of-line 1)
  11755. (looking-at org-todo-line-tags-regexp))
  11756. (push (org-point-in-group p 1 :headline-stars) clist)
  11757. (push (org-point-in-group p 2 :todo-keyword) clist)
  11758. (push (org-point-in-group p 4 :tags) clist))
  11759. (goto-char p)
  11760. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  11761. (if (looking-at "\\[#[A-Z0-9]\\]")
  11762. (push (org-point-in-group p 0 :priority) clist)))
  11763. ((org-at-item-p)
  11764. (push (org-point-in-group p 2 :item-bullet) clist)
  11765. (push (list :item (point-at-bol)
  11766. (save-excursion (org-end-of-item) (point)))
  11767. clist)
  11768. (and (org-at-item-checkbox-p)
  11769. (push (org-point-in-group p 0 :checkbox) clist)))
  11770. ((org-at-table-p)
  11771. (push (list :table (org-table-begin) (org-table-end)) clist)
  11772. (if (memq 'org-formula faces)
  11773. (push (list :table-special
  11774. (previous-single-property-change p 'face)
  11775. (next-single-property-change p 'face)) clist)))
  11776. ((org-at-table-p 'any)
  11777. (push (list :table-table) clist)))
  11778. (goto-char p)
  11779. ;; Now the small context
  11780. (cond
  11781. ((org-at-timestamp-p)
  11782. (push (org-point-in-group p 0 :timestamp) clist))
  11783. ((memq 'org-link faces)
  11784. (push (list :link
  11785. (previous-single-property-change p 'face)
  11786. (next-single-property-change p 'face)) clist))
  11787. ((memq 'org-special-keyword faces)
  11788. (push (list :keyword
  11789. (previous-single-property-change p 'face)
  11790. (next-single-property-change p 'face)) clist))
  11791. ((org-on-target-p)
  11792. (push (org-point-in-group p 0 :target) clist)
  11793. (goto-char (1- (match-beginning 0)))
  11794. (if (looking-at org-radio-target-regexp)
  11795. (push (org-point-in-group p 0 :radio-target) clist))
  11796. (goto-char p))
  11797. ((setq o (car (delq nil
  11798. (mapcar
  11799. (lambda (x)
  11800. (if (memq x org-latex-fragment-image-overlays) x))
  11801. (org-overlays-at (point))))))
  11802. (push (list :latex-fragment
  11803. (org-overlay-start o) (org-overlay-end o)) clist)
  11804. (push (list :latex-preview
  11805. (org-overlay-start o) (org-overlay-end o)) clist))
  11806. ((org-inside-LaTeX-fragment-p)
  11807. ;; FIXME: positions wrong.
  11808. (push (list :latex-fragment (point) (point)) clist)))
  11809. (setq clist (nreverse (delq nil clist)))
  11810. clist))
  11811. ;; FIXME: Compare with at-regexp-p Do we need both?
  11812. (defun org-in-regexp (re &optional nlines visually)
  11813. "Check if point is inside a match of regexp.
  11814. Normally only the current line is checked, but you can include NLINES extra
  11815. lines both before and after point into the search.
  11816. If VISUALLY is set, require that the cursor is not after the match but
  11817. really on, so that the block visually is on the match."
  11818. (catch 'exit
  11819. (let ((pos (point))
  11820. (eol (point-at-eol (+ 1 (or nlines 0))))
  11821. (inc (if visually 1 0)))
  11822. (save-excursion
  11823. (beginning-of-line (- 1 (or nlines 0)))
  11824. (while (re-search-forward re eol t)
  11825. (if (and (<= (match-beginning 0) pos)
  11826. (>= (+ inc (match-end 0)) pos))
  11827. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  11828. (defun org-at-regexp-p (regexp)
  11829. "Is point inside a match of REGEXP in the current line?"
  11830. (catch 'exit
  11831. (save-excursion
  11832. (let ((pos (point)) (end (point-at-eol)))
  11833. (beginning-of-line 1)
  11834. (while (re-search-forward regexp end t)
  11835. (if (and (<= (match-beginning 0) pos)
  11836. (>= (match-end 0) pos))
  11837. (throw 'exit t)))
  11838. nil))))
  11839. (defun org-occur-in-agenda-files (regexp &optional nlines)
  11840. "Call `multi-occur' with buffers for all agenda files."
  11841. (interactive "sOrg-files matching: \np")
  11842. (let* ((files (org-agenda-files))
  11843. (tnames (mapcar 'file-truename files))
  11844. (extra org-agenda-text-search-extra-files)
  11845. f)
  11846. (when (eq (car extra) 'agenda-archives)
  11847. (setq extra (cdr extra))
  11848. (setq files (org-add-archive-files files)))
  11849. (while (setq f (pop extra))
  11850. (unless (member (file-truename f) tnames)
  11851. (add-to-list 'files f 'append)
  11852. (add-to-list 'tnames (file-truename f) 'append)))
  11853. (multi-occur
  11854. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  11855. regexp)))
  11856. (if (boundp 'occur-mode-find-occurrence-hook)
  11857. ;; Emacs 23
  11858. (add-hook 'occur-mode-find-occurrence-hook
  11859. (lambda ()
  11860. (when (org-mode-p)
  11861. (org-reveal))))
  11862. ;; Emacs 22
  11863. (defadvice occur-mode-goto-occurrence
  11864. (after org-occur-reveal activate)
  11865. (and (org-mode-p) (org-reveal)))
  11866. (defadvice occur-mode-goto-occurrence-other-window
  11867. (after org-occur-reveal activate)
  11868. (and (org-mode-p) (org-reveal)))
  11869. (defadvice occur-mode-display-occurrence
  11870. (after org-occur-reveal activate)
  11871. (when (org-mode-p)
  11872. (let ((pos (occur-mode-find-occurrence)))
  11873. (with-current-buffer (marker-buffer pos)
  11874. (save-excursion
  11875. (goto-char pos)
  11876. (org-reveal)))))))
  11877. (defun org-uniquify (list)
  11878. "Remove duplicate elements from LIST."
  11879. (let (res)
  11880. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  11881. res))
  11882. (defun org-delete-all (elts list)
  11883. "Remove all elements in ELTS from LIST."
  11884. (while elts
  11885. (setq list (delete (pop elts) list)))
  11886. list)
  11887. (defun org-back-over-empty-lines ()
  11888. "Move backwards over witespace, to the beginning of the first empty line.
  11889. Returns the number of empty lines passed."
  11890. (let ((pos (point)))
  11891. (skip-chars-backward " \t\n\r")
  11892. (beginning-of-line 2)
  11893. (goto-char (min (point) pos))
  11894. (count-lines (point) pos)))
  11895. (defun org-skip-whitespace ()
  11896. (skip-chars-forward " \t\n\r"))
  11897. (defun org-point-in-group (point group &optional context)
  11898. "Check if POINT is in match-group GROUP.
  11899. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  11900. match. If the match group does ot exist or point is not inside it,
  11901. return nil."
  11902. (and (match-beginning group)
  11903. (>= point (match-beginning group))
  11904. (<= point (match-end group))
  11905. (if context
  11906. (list context (match-beginning group) (match-end group))
  11907. t)))
  11908. (defun org-switch-to-buffer-other-window (&rest args)
  11909. "Switch to buffer in a second window on the current frame.
  11910. In particular, do not allow pop-up frames."
  11911. (let (pop-up-frames special-display-buffer-names special-display-regexps
  11912. special-display-function)
  11913. (apply 'switch-to-buffer-other-window args)))
  11914. (defun org-combine-plists (&rest plists)
  11915. "Create a single property list from all plists in PLISTS.
  11916. The process starts by copying the first list, and then setting properties
  11917. from the other lists. Settings in the last list are the most significant
  11918. ones and overrule settings in the other lists."
  11919. (let ((rtn (copy-sequence (pop plists)))
  11920. p v ls)
  11921. (while plists
  11922. (setq ls (pop plists))
  11923. (while ls
  11924. (setq p (pop ls) v (pop ls))
  11925. (setq rtn (plist-put rtn p v))))
  11926. rtn))
  11927. (defun org-move-line-down (arg)
  11928. "Move the current line down. With prefix argument, move it past ARG lines."
  11929. (interactive "p")
  11930. (let ((col (current-column))
  11931. beg end pos)
  11932. (beginning-of-line 1) (setq beg (point))
  11933. (beginning-of-line 2) (setq end (point))
  11934. (beginning-of-line (+ 1 arg))
  11935. (setq pos (move-marker (make-marker) (point)))
  11936. (insert (delete-and-extract-region beg end))
  11937. (goto-char pos)
  11938. (org-move-to-column col)))
  11939. (defun org-move-line-up (arg)
  11940. "Move the current line up. With prefix argument, move it past ARG lines."
  11941. (interactive "p")
  11942. (let ((col (current-column))
  11943. beg end pos)
  11944. (beginning-of-line 1) (setq beg (point))
  11945. (beginning-of-line 2) (setq end (point))
  11946. (beginning-of-line (- arg))
  11947. (setq pos (move-marker (make-marker) (point)))
  11948. (insert (delete-and-extract-region beg end))
  11949. (goto-char pos)
  11950. (org-move-to-column col)))
  11951. (defun org-replace-escapes (string table)
  11952. "Replace %-escapes in STRING with values in TABLE.
  11953. TABLE is an association list with keys like \"%a\" and string values.
  11954. The sequences in STRING may contain normal field width and padding information,
  11955. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  11956. so values can contain further %-escapes if they are define later in TABLE."
  11957. (let ((case-fold-search nil)
  11958. e re rpl)
  11959. (while (setq e (pop table))
  11960. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  11961. (while (string-match re string)
  11962. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  11963. (cdr e)))
  11964. (setq string (replace-match rpl t t string))))
  11965. string))
  11966. (defun org-sublist (list start end)
  11967. "Return a section of LIST, from START to END.
  11968. Counting starts at 1."
  11969. (let (rtn (c start))
  11970. (setq list (nthcdr (1- start) list))
  11971. (while (and list (<= c end))
  11972. (push (pop list) rtn)
  11973. (setq c (1+ c)))
  11974. (nreverse rtn)))
  11975. (defun org-find-base-buffer-visiting (file)
  11976. "Like `find-buffer-visiting' but alway return the base buffer and
  11977. not an indirect buffer."
  11978. (let ((buf (find-buffer-visiting file)))
  11979. (if buf
  11980. (or (buffer-base-buffer buf) buf)
  11981. nil)))
  11982. (defun org-image-file-name-regexp ()
  11983. "Return regexp matching the file names of images."
  11984. (if (fboundp 'image-file-name-regexp)
  11985. (image-file-name-regexp)
  11986. (let ((image-file-name-extensions
  11987. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  11988. "xbm" "xpm" "pbm" "pgm" "ppm")))
  11989. (concat "\\."
  11990. (regexp-opt (nconc (mapcar 'upcase
  11991. image-file-name-extensions)
  11992. image-file-name-extensions)
  11993. t)
  11994. "\\'"))))
  11995. (defun org-file-image-p (file)
  11996. "Return non-nil if FILE is an image."
  11997. (save-match-data
  11998. (string-match (org-image-file-name-regexp) file)))
  11999. ;;; Paragraph filling stuff.
  12000. ;; We want this to be just right, so use the full arsenal.
  12001. (defun org-indent-line-function ()
  12002. "Indent line like previous, but further if previous was headline or item."
  12003. (interactive)
  12004. (let* ((pos (point))
  12005. (itemp (org-at-item-p))
  12006. column bpos bcol tpos tcol bullet btype bullet-type)
  12007. ;; Find the previous relevant line
  12008. (beginning-of-line 1)
  12009. (cond
  12010. ((looking-at "#") (setq column 0))
  12011. ((looking-at "\\*+ ") (setq column 0))
  12012. (t
  12013. (beginning-of-line 0)
  12014. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12015. (beginning-of-line 0))
  12016. (cond
  12017. ((looking-at "\\*+[ \t]+")
  12018. (goto-char (match-end 0))
  12019. (setq column (current-column)))
  12020. ((org-in-item-p)
  12021. (org-beginning-of-item)
  12022. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12023. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  12024. (setq bpos (match-beginning 1) tpos (match-end 0)
  12025. bcol (progn (goto-char bpos) (current-column))
  12026. tcol (progn (goto-char tpos) (current-column))
  12027. bullet (match-string 1)
  12028. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12029. (if (not itemp)
  12030. (setq column tcol)
  12031. (goto-char pos)
  12032. (beginning-of-line 1)
  12033. (if (looking-at "\\S-")
  12034. (progn
  12035. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12036. (setq bullet (match-string 1)
  12037. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12038. (setq column (if (equal btype bullet-type) bcol tcol)))
  12039. (setq column (org-get-indentation)))))
  12040. (t (setq column (org-get-indentation))))))
  12041. (goto-char pos)
  12042. (if (<= (current-column) (current-indentation))
  12043. (org-indent-line-to column)
  12044. (save-excursion (org-indent-line-to column)))
  12045. (setq column (current-column))
  12046. (beginning-of-line 1)
  12047. (if (looking-at
  12048. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12049. (replace-match (concat "\\1" (format org-property-format
  12050. (match-string 2) (match-string 3)))
  12051. t nil))
  12052. (org-move-to-column column)))
  12053. (defun org-set-autofill-regexps ()
  12054. (interactive)
  12055. ;; In the paragraph separator we include headlines, because filling
  12056. ;; text in a line directly attached to a headline would otherwise
  12057. ;; fill the headline as well.
  12058. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12059. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12060. ;; The paragraph starter includes hand-formatted lists.
  12061. (org-set-local 'paragraph-start
  12062. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12063. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12064. ;; But only if the user has not turned off tables or fixed-width regions
  12065. (org-set-local
  12066. 'auto-fill-inhibit-regexp
  12067. (concat "\\*+ \\|#\\+"
  12068. "\\|[ \t]*" org-keyword-time-regexp
  12069. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12070. (concat
  12071. "\\|[ \t]*["
  12072. (if org-enable-table-editor "|" "")
  12073. (if org-enable-fixed-width-editor ":" "")
  12074. "]"))))
  12075. ;; We use our own fill-paragraph function, to make sure that tables
  12076. ;; and fixed-width regions are not wrapped. That function will pass
  12077. ;; through to `fill-paragraph' when appropriate.
  12078. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12079. ; Adaptive filling: To get full control, first make sure that
  12080. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12081. (org-set-local 'adaptive-fill-regexp "\000")
  12082. (org-set-local 'adaptive-fill-function
  12083. 'org-adaptive-fill-function)
  12084. (org-set-local
  12085. 'align-mode-rules-list
  12086. '((org-in-buffer-settings
  12087. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12088. (modes . '(org-mode))))))
  12089. (defun org-fill-paragraph (&optional justify)
  12090. "Re-align a table, pass through to fill-paragraph if no table."
  12091. (let ((table-p (org-at-table-p))
  12092. (table.el-p (org-at-table.el-p)))
  12093. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12094. (save-excursion (goto-char (point-at-bol))
  12095. (looking-at outline-regexp)))
  12096. t) ; skip headlines
  12097. (table.el-p t) ; skip table.el tables
  12098. (table-p (org-table-align) t) ; align org-mode tables
  12099. (t nil)))) ; call paragraph-fill
  12100. ;; For reference, this is the default value of adaptive-fill-regexp
  12101. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12102. (defun org-adaptive-fill-function ()
  12103. "Return a fill prefix for org-mode files.
  12104. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12105. work correctly."
  12106. (cond ((looking-at "#[ \t]+")
  12107. (match-string 0))
  12108. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12109. (save-excursion
  12110. (goto-char (match-end 0))
  12111. (make-string (current-column) ?\ )))
  12112. (t nil)))
  12113. ;;; Other stuff.
  12114. (defun org-toggle-fixed-width-section (arg)
  12115. "Toggle the fixed-width export.
  12116. If there is no active region, the QUOTE keyword at the current headline is
  12117. inserted or removed. When present, it causes the text between this headline
  12118. and the next to be exported as fixed-width text, and unmodified.
  12119. If there is an active region, this command adds or removes a colon as the
  12120. first character of this line. If the first character of a line is a colon,
  12121. this line is also exported in fixed-width font."
  12122. (interactive "P")
  12123. (let* ((cc 0)
  12124. (regionp (org-region-active-p))
  12125. (beg (if regionp (region-beginning) (point)))
  12126. (end (if regionp (region-end)))
  12127. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12128. (case-fold-search nil)
  12129. (re "[ \t]*\\(:\\)")
  12130. off)
  12131. (if regionp
  12132. (save-excursion
  12133. (goto-char beg)
  12134. (setq cc (current-column))
  12135. (beginning-of-line 1)
  12136. (setq off (looking-at re))
  12137. (while (> nlines 0)
  12138. (setq nlines (1- nlines))
  12139. (beginning-of-line 1)
  12140. (cond
  12141. (arg
  12142. (org-move-to-column cc t)
  12143. (insert ":\n")
  12144. (forward-line -1))
  12145. ((and off (looking-at re))
  12146. (replace-match "" t t nil 1))
  12147. ((not off) (org-move-to-column cc t) (insert ":")))
  12148. (forward-line 1)))
  12149. (save-excursion
  12150. (org-back-to-heading)
  12151. (if (looking-at (concat outline-regexp
  12152. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12153. (replace-match "" t t nil 1)
  12154. (if (looking-at outline-regexp)
  12155. (progn
  12156. (goto-char (match-end 0))
  12157. (insert org-quote-string " "))))))))
  12158. ;;;; Functions extending outline functionality
  12159. (defun org-beginning-of-line (&optional arg)
  12160. "Go to the beginning of the current line. If that is invisible, continue
  12161. to a visible line beginning. This makes the function of C-a more intuitive.
  12162. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12163. first attempt, and only move to after the tags when the cursor is already
  12164. beyond the end of the headline."
  12165. (interactive "P")
  12166. (let ((pos (point)))
  12167. (beginning-of-line 1)
  12168. (if (bobp)
  12169. nil
  12170. (backward-char 1)
  12171. (if (org-invisible-p)
  12172. (while (and (not (bobp)) (org-invisible-p))
  12173. (backward-char 1)
  12174. (beginning-of-line 1))
  12175. (forward-char 1)))
  12176. (when org-special-ctrl-a/e
  12177. (cond
  12178. ((and (looking-at org-todo-line-regexp)
  12179. (= (char-after (match-end 1)) ?\ ))
  12180. (goto-char
  12181. (if (eq org-special-ctrl-a/e t)
  12182. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12183. ((= pos (point)) (match-beginning 3))
  12184. (t (point)))
  12185. (cond ((> pos (point)) (point))
  12186. ((not (eq last-command this-command)) (point))
  12187. (t (match-beginning 3))))))
  12188. ((org-at-item-p)
  12189. (goto-char
  12190. (if (eq org-special-ctrl-a/e t)
  12191. (cond ((> pos (match-end 4)) (match-end 4))
  12192. ((= pos (point)) (match-end 4))
  12193. (t (point)))
  12194. (cond ((> pos (point)) (point))
  12195. ((not (eq last-command this-command)) (point))
  12196. (t (match-end 4))))))))))
  12197. (defun org-end-of-line (&optional arg)
  12198. "Go to the end of the line.
  12199. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12200. first attempt, and only move to after the tags when the cursor is already
  12201. beyond the end of the headline."
  12202. (interactive "P")
  12203. (if (or (not org-special-ctrl-a/e)
  12204. (not (org-on-heading-p)))
  12205. (end-of-line arg)
  12206. (let ((pos (point)))
  12207. (beginning-of-line 1)
  12208. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12209. (if (eq org-special-ctrl-a/e t)
  12210. (if (or (< pos (match-beginning 1))
  12211. (= pos (match-end 0)))
  12212. (goto-char (match-beginning 1))
  12213. (goto-char (match-end 0)))
  12214. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12215. (goto-char (match-end 0))
  12216. (goto-char (match-beginning 1))))
  12217. (end-of-line arg)))))
  12218. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12219. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12220. (defun org-kill-line (&optional arg)
  12221. "Kill line, to tags or end of line."
  12222. (interactive "P")
  12223. (cond
  12224. ((or (not org-special-ctrl-k)
  12225. (bolp)
  12226. (not (org-on-heading-p)))
  12227. (call-interactively 'kill-line))
  12228. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12229. (kill-region (point) (match-beginning 1))
  12230. (org-set-tags nil t))
  12231. (t (kill-region (point) (point-at-eol)))))
  12232. (define-key org-mode-map "\C-k" 'org-kill-line)
  12233. (defun org-invisible-p ()
  12234. "Check if point is at a character currently not visible."
  12235. ;; Early versions of noutline don't have `outline-invisible-p'.
  12236. (if (fboundp 'outline-invisible-p)
  12237. (outline-invisible-p)
  12238. (get-char-property (point) 'invisible)))
  12239. (defun org-invisible-p2 ()
  12240. "Check if point is at a character currently not visible."
  12241. (save-excursion
  12242. (if (and (eolp) (not (bobp))) (backward-char 1))
  12243. ;; Early versions of noutline don't have `outline-invisible-p'.
  12244. (if (fboundp 'outline-invisible-p)
  12245. (outline-invisible-p)
  12246. (get-char-property (point) 'invisible))))
  12247. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12248. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12249. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12250. (defun org-at-heading-or-item-p ()
  12251. (or (org-on-heading-p) (org-at-item-p)))
  12252. (defun org-on-target-p ()
  12253. (or (org-in-regexp org-radio-target-regexp)
  12254. (org-in-regexp org-target-regexp)))
  12255. (defun org-up-heading-all (arg)
  12256. "Move to the heading line of which the present line is a subheading.
  12257. This function considers both visible and invisible heading lines.
  12258. With argument, move up ARG levels."
  12259. (if (fboundp 'outline-up-heading-all)
  12260. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12261. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12262. (defun org-up-heading-safe ()
  12263. "Move to the heading line of which the present line is a subheading.
  12264. This version will not throw an error. It will return the level of the
  12265. headline found, or nil if no higher level is found."
  12266. (let ((pos (point)) start-level level
  12267. (re (concat "^" outline-regexp)))
  12268. (catch 'exit
  12269. (outline-back-to-heading t)
  12270. (setq start-level (funcall outline-level))
  12271. (if (equal start-level 1) (throw 'exit nil))
  12272. (while (re-search-backward re nil t)
  12273. (setq level (funcall outline-level))
  12274. (if (< level start-level) (throw 'exit level)))
  12275. nil)))
  12276. (defun org-first-sibling-p ()
  12277. "Is this heading the first child of its parents?"
  12278. (interactive)
  12279. (let ((re (concat "^" outline-regexp))
  12280. level l)
  12281. (unless (org-at-heading-p t)
  12282. (error "Not at a heading"))
  12283. (setq level (funcall outline-level))
  12284. (save-excursion
  12285. (if (not (re-search-backward re nil t))
  12286. t
  12287. (setq l (funcall outline-level))
  12288. (< l level)))))
  12289. (defun org-goto-sibling (&optional previous)
  12290. "Goto the next sibling, even if it is invisible.
  12291. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12292. when a sibling was found. When none is found, return nil and don't
  12293. move point."
  12294. (let ((fun (if previous 're-search-backward 're-search-forward))
  12295. (pos (point))
  12296. (re (concat "^" outline-regexp))
  12297. level l)
  12298. (when (condition-case nil (org-back-to-heading t) (error nil))
  12299. (setq level (funcall outline-level))
  12300. (catch 'exit
  12301. (or previous (forward-char 1))
  12302. (while (funcall fun re nil t)
  12303. (setq l (funcall outline-level))
  12304. (when (< l level) (goto-char pos) (throw 'exit nil))
  12305. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12306. (goto-char pos)
  12307. nil))))
  12308. (defun org-show-siblings ()
  12309. "Show all siblings of the current headline."
  12310. (save-excursion
  12311. (while (org-goto-sibling) (org-flag-heading nil)))
  12312. (save-excursion
  12313. (while (org-goto-sibling 'previous)
  12314. (org-flag-heading nil))))
  12315. (defun org-show-hidden-entry ()
  12316. "Show an entry where even the heading is hidden."
  12317. (save-excursion
  12318. (org-show-entry)))
  12319. (defun org-flag-heading (flag &optional entry)
  12320. "Flag the current heading. FLAG non-nil means make invisible.
  12321. When ENTRY is non-nil, show the entire entry."
  12322. (save-excursion
  12323. (org-back-to-heading t)
  12324. ;; Check if we should show the entire entry
  12325. (if entry
  12326. (progn
  12327. (org-show-entry)
  12328. (save-excursion
  12329. (and (outline-next-heading)
  12330. (org-flag-heading nil))))
  12331. (outline-flag-region (max (point-min) (1- (point)))
  12332. (save-excursion (outline-end-of-heading) (point))
  12333. flag))))
  12334. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12335. ;; This is an exact copy of the original function, but it uses
  12336. ;; `org-back-to-heading', to make it work also in invisible
  12337. ;; trees. And is uses an invisible-OK argument.
  12338. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12339. (org-back-to-heading invisible-OK)
  12340. (let ((first t)
  12341. (level (funcall outline-level)))
  12342. (while (and (not (eobp))
  12343. (or first (> (funcall outline-level) level)))
  12344. (setq first nil)
  12345. (outline-next-heading))
  12346. (unless to-heading
  12347. (if (memq (preceding-char) '(?\n ?\^M))
  12348. (progn
  12349. ;; Go to end of line before heading
  12350. (forward-char -1)
  12351. (if (memq (preceding-char) '(?\n ?\^M))
  12352. ;; leave blank line before heading
  12353. (forward-char -1))))))
  12354. (point))
  12355. (defun org-show-subtree ()
  12356. "Show everything after this heading at deeper levels."
  12357. (outline-flag-region
  12358. (point)
  12359. (save-excursion
  12360. (outline-end-of-subtree) (outline-next-heading) (point))
  12361. nil))
  12362. (defun org-show-entry ()
  12363. "Show the body directly following this heading.
  12364. Show the heading too, if it is currently invisible."
  12365. (interactive)
  12366. (save-excursion
  12367. (condition-case nil
  12368. (progn
  12369. (org-back-to-heading t)
  12370. (outline-flag-region
  12371. (max (point-min) (1- (point)))
  12372. (save-excursion
  12373. (re-search-forward
  12374. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12375. (or (match-beginning 1) (point-max)))
  12376. nil))
  12377. (error nil))))
  12378. (defun org-make-options-regexp (kwds)
  12379. "Make a regular expression for keyword lines."
  12380. (concat
  12381. "^"
  12382. "#?[ \t]*\\+\\("
  12383. (mapconcat 'regexp-quote kwds "\\|")
  12384. "\\):[ \t]*"
  12385. "\\(.+\\)"))
  12386. ;; Make isearch reveal the necessary context
  12387. (defun org-isearch-end ()
  12388. "Reveal context after isearch exits."
  12389. (when isearch-success ; only if search was successful
  12390. (if (featurep 'xemacs)
  12391. ;; Under XEmacs, the hook is run in the correct place,
  12392. ;; we directly show the context.
  12393. (org-show-context 'isearch)
  12394. ;; In Emacs the hook runs *before* restoring the overlays.
  12395. ;; So we have to use a one-time post-command-hook to do this.
  12396. ;; (Emacs 22 has a special variable, see function `org-mode')
  12397. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12398. isearch-mode-end-hook-quit)
  12399. ;; Only when the isearch was not quitted.
  12400. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12401. 'append 'local)))))
  12402. (defun org-isearch-post-command ()
  12403. "Remove self from hook, and show context."
  12404. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12405. (org-show-context 'isearch))
  12406. ;;;; Integration with and fixes for other packages
  12407. ;;; Imenu support
  12408. (defvar org-imenu-markers nil
  12409. "All markers currently used by Imenu.")
  12410. (make-variable-buffer-local 'org-imenu-markers)
  12411. (defun org-imenu-new-marker (&optional pos)
  12412. "Return a new marker for use by Imenu, and remember the marker."
  12413. (let ((m (make-marker)))
  12414. (move-marker m (or pos (point)))
  12415. (push m org-imenu-markers)
  12416. m))
  12417. (defun org-imenu-get-tree ()
  12418. "Produce the index for Imenu."
  12419. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12420. (setq org-imenu-markers nil)
  12421. (let* ((n org-imenu-depth)
  12422. (re (concat "^" outline-regexp))
  12423. (subs (make-vector (1+ n) nil))
  12424. (last-level 0)
  12425. m tree level head)
  12426. (save-excursion
  12427. (save-restriction
  12428. (widen)
  12429. (goto-char (point-max))
  12430. (while (re-search-backward re nil t)
  12431. (setq level (org-reduced-level (funcall outline-level)))
  12432. (when (<= level n)
  12433. (looking-at org-complex-heading-regexp)
  12434. (setq head (org-match-string-no-properties 4)
  12435. m (org-imenu-new-marker))
  12436. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12437. (if (>= level last-level)
  12438. (push (cons head m) (aref subs level))
  12439. (push (cons head (aref subs (1+ level))) (aref subs level))
  12440. (loop for i from (1+ level) to n do (aset subs i nil)))
  12441. (setq last-level level)))))
  12442. (aref subs 1)))
  12443. (eval-after-load "imenu"
  12444. '(progn
  12445. (add-hook 'imenu-after-jump-hook
  12446. (lambda () (org-show-context 'org-goto)))))
  12447. ;; Speedbar support
  12448. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12449. "Overlay marking the agenda restriction line in speedbar.")
  12450. (org-overlay-put org-speedbar-restriction-lock-overlay
  12451. 'face 'org-agenda-restriction-lock)
  12452. (org-overlay-put org-speedbar-restriction-lock-overlay
  12453. 'help-echo "Agendas are currently limited to this item.")
  12454. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12455. (defun org-speedbar-set-agenda-restriction ()
  12456. "Restrict future agenda commands to the location at point in speedbar.
  12457. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12458. (interactive)
  12459. (require 'org-agenda)
  12460. (let (p m tp np dir txt w)
  12461. (cond
  12462. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12463. 'org-imenu t))
  12464. (setq m (get-text-property p 'org-imenu-marker))
  12465. (save-excursion
  12466. (save-restriction
  12467. (set-buffer (marker-buffer m))
  12468. (goto-char m)
  12469. (org-agenda-set-restriction-lock 'subtree))))
  12470. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12471. 'speedbar-function 'speedbar-find-file))
  12472. (setq tp (previous-single-property-change
  12473. (1+ p) 'speedbar-function)
  12474. np (next-single-property-change
  12475. tp 'speedbar-function)
  12476. dir (speedbar-line-directory)
  12477. txt (buffer-substring-no-properties (or tp (point-min))
  12478. (or np (point-max))))
  12479. (save-excursion
  12480. (save-restriction
  12481. (set-buffer (find-file-noselect
  12482. (let ((default-directory dir))
  12483. (expand-file-name txt))))
  12484. (unless (org-mode-p)
  12485. (error "Cannot restrict to non-Org-mode file"))
  12486. (org-agenda-set-restriction-lock 'file))))
  12487. (t (error "Don't know how to restrict Org-mode's agenda")))
  12488. (org-move-overlay org-speedbar-restriction-lock-overlay
  12489. (point-at-bol) (point-at-eol))
  12490. (setq current-prefix-arg nil)
  12491. (org-agenda-maybe-redo)))
  12492. (eval-after-load "speedbar"
  12493. '(progn
  12494. (speedbar-add-supported-extension ".org")
  12495. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12496. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12497. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12498. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12499. (add-hook 'speedbar-visiting-tag-hook
  12500. (lambda () (org-show-context 'org-goto)))))
  12501. ;;; Fixes and Hacks for problems with other packages
  12502. ;; Make flyspell not check words in links, to not mess up our keymap
  12503. (defun org-mode-flyspell-verify ()
  12504. "Don't let flyspell put overlays at active buttons."
  12505. (not (get-text-property (point) 'keymap)))
  12506. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12507. (eval-after-load "bookmark"
  12508. '(if (boundp 'bookmark-after-jump-hook)
  12509. ;; We can use the hook
  12510. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12511. ;; Hook not available, use advice
  12512. (defadvice bookmark-jump (after org-make-visible activate)
  12513. "Make the position visible."
  12514. (org-bookmark-jump-unhide))))
  12515. (defun org-bookmark-jump-unhide ()
  12516. "Unhide the current position, to show the bookmark location."
  12517. (and (org-mode-p)
  12518. (or (org-invisible-p)
  12519. (save-excursion (goto-char (max (point-min) (1- (point))))
  12520. (org-invisible-p)))
  12521. (org-show-context 'bookmark-jump)))
  12522. ;; Make session.el ignore our circular variable
  12523. (eval-after-load "session"
  12524. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12525. ;;;; Experimental code
  12526. (defun org-closed-in-range ()
  12527. "Sparse tree of items closed in a certain time range.
  12528. Still experimental, may disappear in the future."
  12529. (interactive)
  12530. ;; Get the time interval from the user.
  12531. (let* ((time1 (time-to-seconds
  12532. (org-read-date nil 'to-time nil "Starting date: ")))
  12533. (time2 (time-to-seconds
  12534. (org-read-date nil 'to-time nil "End date:")))
  12535. ;; callback function
  12536. (callback (lambda ()
  12537. (let ((time
  12538. (time-to-seconds
  12539. (apply 'encode-time
  12540. (org-parse-time-string
  12541. (match-string 1))))))
  12542. ;; check if time in interval
  12543. (and (>= time time1) (<= time time2))))))
  12544. ;; make tree, check each match with the callback
  12545. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12546. ;;;; Finish up
  12547. (provide 'org)
  12548. (run-hooks 'org-load-hook)
  12549. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12550. ;;; org.el ends here