org-agenda.el 237 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210
  1. ;;; org-agenda.el --- Dynamic task and appointment lists for Org
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.24trans
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file contains the code for creating and using the Agenda for Org-mode.
  25. ;;; Code:
  26. (require 'org)
  27. (eval-when-compile
  28. (require 'cl)
  29. (require 'calendar))
  30. (declare-function diary-add-to-list "diary-lib"
  31. (date string specifier &optional marker globcolor literal))
  32. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  33. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  34. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  35. (declare-function calendar-check-holidays "holidays" (date))
  36. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  37. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  38. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  39. (declare-function calendar-french-date-string "cal-french" (&optional date))
  40. (declare-function calendar-goto-date "cal-move" (date))
  41. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  42. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  43. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  44. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  45. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  46. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  47. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  48. (declare-function org-columns-quit "org-colview" ())
  49. (defvar calendar-mode-map)
  50. ;; Defined somewhere in this file, but used before definition.
  51. (defvar org-agenda-buffer-name)
  52. (defvar org-agenda-overriding-header)
  53. (defvar entry)
  54. (defvar date)
  55. (defvar org-agenda-undo-list)
  56. (defvar org-agenda-pending-undo-list)
  57. (defvar original-date) ; dynamically scoped, calendar.el does scope this
  58. (defcustom org-agenda-confirm-kill 1
  59. "When set, remote killing from the agenda buffer needs confirmation.
  60. When t, a confirmation is always needed. When a number N, confirmation is
  61. only needed when the text to be killed contains more than N non-white lines."
  62. :group 'org-agenda
  63. :type '(choice
  64. (const :tag "Never" nil)
  65. (const :tag "Always" t)
  66. (number :tag "When more than N lines")))
  67. (defcustom org-agenda-compact-blocks nil
  68. "Non-nil means, make the block agenda more compact.
  69. This is done by leaving out unnecessary lines."
  70. :group 'org-agenda
  71. :type 'boolean)
  72. (defcustom org-agenda-block-separator ?=
  73. "The separator between blocks in the agenda.
  74. If this is a string, it will be used as the separator, with a newline added.
  75. If it is a character, it will be repeated to fill the window width."
  76. :group 'org-agenda
  77. :type '(choice
  78. (character)
  79. (string)))
  80. (defgroup org-agenda-export nil
  81. "Options concerning exporting agenda views in Org-mode."
  82. :tag "Org Agenda Export"
  83. :group 'org-agenda)
  84. (defcustom org-agenda-with-colors t
  85. "Non-nil means, use colors in agenda views."
  86. :group 'org-agenda-export
  87. :type 'boolean)
  88. (defcustom org-agenda-exporter-settings nil
  89. "Alist of variable/value pairs that should be active during agenda export.
  90. This is a good place to set options for ps-print and for htmlize."
  91. :group 'org-agenda-export
  92. :type '(repeat
  93. (list
  94. (variable)
  95. (sexp :tag "Value"))))
  96. (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
  97. "Hook run in temporary buffer before writing it to an export file.
  98. A useful function is `org-agenda-add-entry-text'."
  99. :group 'org-agenda-export
  100. :type 'hook
  101. :options '(org-agenda-add-entry-text))
  102. (defcustom org-agenda-add-entry-text-maxlines 0
  103. "Maximum number of entry text lines to be added to agenda.
  104. This is only relevant when `org-agenda-add-entry-text' is part of
  105. `org-agenda-before-write-hook', which it is by default.
  106. When this is 0, nothing will happen. When it is greater than 0, it
  107. specifies the maximum number of lines that will be added for each entry
  108. that is listed in the agenda view."
  109. :group 'org-agenda
  110. :type 'integer)
  111. (defcustom org-agenda-add-entry-text-descriptive-links t
  112. "Non-nil means, export org-links as descriptive links in agenda added text.
  113. This variable applies to the text added to the agenda when
  114. `org-agenda-add-entry-text-maxlines' is larger than 0.
  115. When this variable nil, the URL will (also) be shown."
  116. :group 'org-agenda
  117. :type 'boolean)
  118. (defcustom org-agenda-export-html-style ""
  119. "The style specification for exported HTML Agenda files.
  120. If this variable contains a string, it will replace the default <style>
  121. section as produced by `htmlize'.
  122. Since there are different ways of setting style information, this variable
  123. needs to contain the full HTML structure to provide a style, including the
  124. surrounding HTML tags. The style specifications should include definitions
  125. the fonts used by the agenda, here is an example:
  126. <style type=\"text/css\">
  127. p { font-weight: normal; color: gray; }
  128. .org-agenda-structure {
  129. font-size: 110%;
  130. color: #003399;
  131. font-weight: 600;
  132. }
  133. .org-todo {
  134. color: #cc6666;
  135. font-weight: bold;
  136. }
  137. .org-agenda-done {
  138. color: #339933;
  139. }
  140. .org-done {
  141. color: #339933;
  142. }
  143. .title { text-align: center; }
  144. .todo, .deadline { color: red; }
  145. .done { color: green; }
  146. </style>
  147. or, if you want to keep the style in a file,
  148. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  149. As the value of this option simply gets inserted into the HTML <head> header,
  150. you can \"misuse\" it to also add other text to the header. However,
  151. <style>...</style> is required, if not present the variable will be ignored."
  152. :group 'org-agenda-export
  153. :group 'org-export-html
  154. :type 'string)
  155. (defgroup org-agenda-custom-commands nil
  156. "Options concerning agenda views in Org-mode."
  157. :tag "Org Agenda Custom Commands"
  158. :group 'org-agenda)
  159. (defconst org-sorting-choice
  160. '(choice
  161. (const time-up) (const time-down)
  162. (const category-keep) (const category-up) (const category-down)
  163. (const tag-down) (const tag-up)
  164. (const priority-up) (const priority-down)
  165. (const todo-state-up) (const todo-state-down)
  166. (const effort-up) (const effort-down)
  167. (const user-defined-up) (const user-defined-down))
  168. "Sorting choices.")
  169. (defconst org-agenda-custom-commands-local-options
  170. `(repeat :tag "Local settings for this command. Remember to quote values"
  171. (choice :tag "Setting"
  172. (list :tag "Heading for this block"
  173. (const org-agenda-overriding-header)
  174. (string :tag "Headline"))
  175. (list :tag "Files to be searched"
  176. (const org-agenda-files)
  177. (list
  178. (const :format "" quote)
  179. (repeat (file))))
  180. (list :tag "Sorting strategy"
  181. (const org-agenda-sorting-strategy)
  182. (list
  183. (const :format "" quote)
  184. (repeat
  185. ,org-sorting-choice)))
  186. (list :tag "Prefix format"
  187. (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
  188. (string))
  189. (list :tag "Number of days in agenda"
  190. (const org-agenda-ndays)
  191. (integer :value 1))
  192. (list :tag "Fixed starting date"
  193. (const org-agenda-start-day)
  194. (string :value "2007-11-01"))
  195. (list :tag "Start on day of week"
  196. (const org-agenda-start-on-weekday)
  197. (choice :value 1
  198. (const :tag "Today" nil)
  199. (number :tag "Weekday No.")))
  200. (list :tag "Include data from diary"
  201. (const org-agenda-include-diary)
  202. (boolean))
  203. (list :tag "Deadline Warning days"
  204. (const org-deadline-warning-days)
  205. (integer :value 1))
  206. (list :tag "Tags filter preset"
  207. (const org-agenda-filter-preset)
  208. (list
  209. (const :format "" quote)
  210. (repeat
  211. (string :tag "+tag or -tag"))))
  212. (list :tag "Standard skipping condition"
  213. :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
  214. (const org-agenda-skip-function)
  215. (list
  216. (const :format "" quote)
  217. (list
  218. (choice
  219. :tag "Skipping range"
  220. (const :tag "Skip entry" org-agenda-skip-entry-if)
  221. (const :tag "Skip subtree" org-agenda-skip-subtree-if))
  222. (repeat :inline t :tag "Conditions for skipping"
  223. (choice
  224. :tag "Condition type"
  225. (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
  226. (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
  227. (const :tag "scheduled" 'scheduled)
  228. (const :tag "not scheduled" 'notscheduled)
  229. (const :tag "deadline" 'deadline)
  230. (const :tag "no deadline" 'notdeadline)
  231. (const :tag "timestamp" 'timestamp)
  232. (const :tag "no timestamp" 'nottimestamp))))))
  233. (list :tag "Non-standard skipping condition"
  234. :value (org-agenda-skip-function)
  235. (const org-agenda-skip-function)
  236. (sexp :tag "Function or form (quoted!)"))
  237. (list :tag "Any variable"
  238. (variable :tag "Variable")
  239. (sexp :tag "Value (sexp)"))))
  240. "Selection of examples for agenda command settings.
  241. This will be spliced into the custom type of
  242. `org-agenda-custom-commands'.")
  243. (defcustom org-agenda-custom-commands nil
  244. "Custom commands for the agenda.
  245. These commands will be offered on the splash screen displayed by the
  246. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  247. (key desc type match settings files)
  248. key The key (one or more characters as a string) to be associated
  249. with the command.
  250. desc A description of the command, when omitted or nil, a default
  251. description is built using MATCH.
  252. type The command type, any of the following symbols:
  253. agenda The daily/weekly agenda.
  254. todo Entries with a specific TODO keyword, in all agenda files.
  255. search Entries containing search words entry or headline.
  256. tags Tags/Property/TODO match in all agenda files.
  257. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  258. todo-tree Sparse tree of specific TODO keyword in *current* file.
  259. tags-tree Sparse tree with all tags matches in *current* file.
  260. occur-tree Occur sparse tree for *current* file.
  261. ... A user-defined function.
  262. match What to search for:
  263. - a single keyword for TODO keyword searches
  264. - a tags match expression for tags searches
  265. - a word search expression for text searches.
  266. - a regular expression for occur searches
  267. For all other commands, this should be the empty string.
  268. settings A list of option settings, similar to that in a let form, so like
  269. this: ((opt1 val1) (opt2 val2) ...). The values will be
  270. evaluated at the moment of execution, so quote them when needed.
  271. files A list of files file to write the produced agenda buffer to
  272. with the command `org-store-agenda-views'.
  273. If a file name ends in \".html\", an HTML version of the buffer
  274. is written out. If it ends in \".ps\", a postscript version is
  275. produced. Otherwise, only the plain text is written to the file.
  276. You can also define a set of commands, to create a composite agenda buffer.
  277. In this case, an entry looks like this:
  278. (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
  279. where
  280. desc A description string to be displayed in the dispatcher menu.
  281. cmd An agenda command, similar to the above. However, tree commands
  282. are no allowed, but instead you can get agenda and global todo list.
  283. So valid commands for a set are:
  284. (agenda \"\" settings)
  285. (alltodo \"\" settings)
  286. (stuck \"\" settings)
  287. (todo \"match\" settings files)
  288. (search \"match\" settings files)
  289. (tags \"match\" settings files)
  290. (tags-todo \"match\" settings files)
  291. Each command can carry a list of options, and another set of options can be
  292. given for the whole set of commands. Individual command options take
  293. precedence over the general options.
  294. When using several characters as key to a command, the first characters
  295. are prefix commands. For the dispatcher to display useful information, you
  296. should provide a description for the prefix, like
  297. (setq org-agenda-custom-commands
  298. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  299. (\"hl\" tags \"+HOME+Lisa\")
  300. (\"hp\" tags \"+HOME+Peter\")
  301. (\"hk\" tags \"+HOME+Kim\")))"
  302. :group 'org-agenda-custom-commands
  303. :type `(repeat
  304. (choice :value ("x" "Describe command here" tags "" nil)
  305. (list :tag "Single command"
  306. (string :tag "Access Key(s) ")
  307. (option (string :tag "Description"))
  308. (choice
  309. (const :tag "Agenda" agenda)
  310. (const :tag "TODO list" alltodo)
  311. (const :tag "Search words" search)
  312. (const :tag "Stuck projects" stuck)
  313. (const :tag "Tags/Property match (all agenda files)" tags)
  314. (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
  315. (const :tag "TODO keyword search (all agenda files)" todo)
  316. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  317. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  318. (const :tag "Occur tree (current buffer)" occur-tree)
  319. (sexp :tag "Other, user-defined function"))
  320. (string :tag "Match (only for some commands)")
  321. ,org-agenda-custom-commands-local-options
  322. (option (repeat :tag "Export" (file :tag "Export to"))))
  323. (list :tag "Command series, all agenda files"
  324. (string :tag "Access Key(s)")
  325. (string :tag "Description ")
  326. (repeat :tag "Component"
  327. (choice
  328. (list :tag "Agenda"
  329. (const :format "" agenda)
  330. (const :tag "" :format "" "")
  331. ,org-agenda-custom-commands-local-options)
  332. (list :tag "TODO list (all keywords)"
  333. (const :format "" alltodo)
  334. (const :tag "" :format "" "")
  335. ,org-agenda-custom-commands-local-options)
  336. (list :tag "Search words"
  337. (const :format "" search)
  338. (string :tag "Match")
  339. ,org-agenda-custom-commands-local-options)
  340. (list :tag "Stuck projects"
  341. (const :format "" stuck)
  342. (const :tag "" :format "" "")
  343. ,org-agenda-custom-commands-local-options)
  344. (list :tag "Tags search"
  345. (const :format "" tags)
  346. (string :tag "Match")
  347. ,org-agenda-custom-commands-local-options)
  348. (list :tag "Tags search, TODO entries only"
  349. (const :format "" tags-todo)
  350. (string :tag "Match")
  351. ,org-agenda-custom-commands-local-options)
  352. (list :tag "TODO keyword search"
  353. (const :format "" todo)
  354. (string :tag "Match")
  355. ,org-agenda-custom-commands-local-options)
  356. (list :tag "Other, user-defined function"
  357. (symbol :tag "function")
  358. (string :tag "Match")
  359. ,org-agenda-custom-commands-local-options)))
  360. (repeat :tag "Settings for entire command set"
  361. (list (variable :tag "Any variable")
  362. (sexp :tag "Value")))
  363. (option (repeat :tag "Export" (file :tag "Export to"))))
  364. (cons :tag "Prefix key documentation"
  365. (string :tag "Access Key(s)")
  366. (string :tag "Description ")))))
  367. (defcustom org-agenda-query-register ?o
  368. "The register holding the current query string.
  369. The purpose of this is that if you construct a query string interactively,
  370. you can then use it to define a custom command."
  371. :group 'org-agenda-custom-commands
  372. :type 'character)
  373. (defcustom org-stuck-projects
  374. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  375. "How to identify stuck projects.
  376. This is a list of four items:
  377. 1. A tags/todo/property matcher string that is used to identify a project.
  378. See the manual for a description of tag and property searches.
  379. The entire tree below a headline matched by this is considered one project.
  380. 2. A list of TODO keywords identifying non-stuck projects.
  381. If the project subtree contains any headline with one of these todo
  382. keywords, the project is considered to be not stuck. If you specify
  383. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  384. 3. A list of tags identifying non-stuck projects.
  385. If the project subtree contains any headline with one of these tags,
  386. the project is considered to be not stuck. If you specify \"*\" as
  387. a tag, any tag will mark the project unstuck.
  388. 4. An arbitrary regular expression matching non-stuck projects.
  389. If the project turns out to be not stuck, search continues also in the
  390. subtree to see if any of the subtasks have project status.
  391. See also the variable `org-tags-match-list-sublevels' which applies
  392. to projects matched by this search as well.
  393. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  394. or `C-c a #' to produce the list."
  395. :group 'org-agenda-custom-commands
  396. :type '(list
  397. (string :tag "Tags/TODO match to identify a project")
  398. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  399. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  400. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  401. (defcustom org-agenda-filter-effort-default-operator "<"
  402. "The default operator for effort estimate filtering.
  403. If you select an effort estimate limit without first pressing an operator,
  404. this one will be used."
  405. :group 'org-agenda-custom-commands
  406. :type '(choice (const :tag "less or equal" "<")
  407. (const :tag "greater or equal"">")
  408. (const :tag "equal" "=")))
  409. (defgroup org-agenda-skip nil
  410. "Options concerning skipping parts of agenda files."
  411. :tag "Org Agenda Skip"
  412. :group 'org-agenda)
  413. (defgroup org-agenda-daily/weekly nil
  414. "Options concerning the daily/weekly agenda."
  415. :tag "Org Agenda Daily/Weekly"
  416. :group 'org-agenda)
  417. (defgroup org-agenda-todo-list nil
  418. "Options concerning the global todo list agenda view."
  419. :tag "Org Agenda Todo List"
  420. :group 'org-agenda)
  421. (defgroup org-agenda-match-view nil
  422. "Options concerning the general tags/property/todo match agenda view."
  423. :tag "Org Agenda Match View"
  424. :group 'org-agenda)
  425. (defvar org-agenda-archives-mode nil
  426. "Non-nil means, the agenda will include archived items.
  427. If this is the symbol `trees', trees in the selected agenda scope
  428. that are marked with the ARCHIVE tag will be included anyway. When this is
  429. t, also all archive files associated with the current selection of agenda
  430. files will be included.")
  431. (defcustom org-agenda-skip-comment-trees t
  432. "Non-nil means, skip trees that start with the COMMENT keyword.
  433. When nil, these trees are also scanned by agenda commands."
  434. :group 'org-agenda-skip
  435. :type 'boolean)
  436. (defcustom org-agenda-todo-list-sublevels t
  437. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  438. When nil, the sublevels of a TODO entry are not checked, resulting in
  439. potentially much shorter TODO lists."
  440. :group 'org-agenda-skip
  441. :group 'org-agenda-todo-list
  442. :type 'boolean)
  443. (defcustom org-agenda-todo-ignore-with-date nil
  444. "Non-nil means, don't show entries with a date in the global todo list.
  445. You can use this if you prefer to mark mere appointments with a TODO keyword,
  446. but don't want them to show up in the TODO list.
  447. When this is set, it also covers deadlines and scheduled items, the settings
  448. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  449. will be ignored.
  450. See also the variable `org-agenda-tags-todo-honor-ignore-options'."
  451. :group 'org-agenda-skip
  452. :group 'org-agenda-todo-list
  453. :type 'boolean)
  454. (defcustom org-agenda-todo-ignore-scheduled nil
  455. "Non-nil means, don't show scheduled entries in the global todo list.
  456. The idea behind this is that by scheduling it, you have already taken care
  457. of this item.
  458. See also `org-agenda-todo-ignore-with-date'.
  459. See also the variable `org-agenda-tags-todo-honor-ignore-options'."
  460. :group 'org-agenda-skip
  461. :group 'org-agenda-todo-list
  462. :type 'boolean)
  463. (defcustom org-agenda-todo-ignore-deadlines nil
  464. "Non-nil means, don't show near deadline entries in the global todo list.
  465. Near means closer than `org-deadline-warning-days' days.
  466. The idea behind this is that such items will appear in the agenda anyway.
  467. See also `org-agenda-todo-ignore-with-date'.
  468. See also the variable `org-agenda-tags-todo-honor-ignore-options'."
  469. :group 'org-agenda-skip
  470. :group 'org-agenda-todo-list
  471. :type 'boolean)
  472. (defcustom org-agenda-tags-todo-honor-ignore-options nil
  473. "Non-nil means, honor todo-list ...ignore options also in tags-todo search.
  474. The variables
  475. `org-agenda-todo-ignore-with-date',
  476. `org-agenda-todo-ignore-scheduled'
  477. `org-agenda-todo-ignore-deadlines'
  478. make the global TODO list skip entries that have time stamps of certain
  479. kinds. If this option is set, the same options will also apply for the
  480. tags-todo search, which is the general tags/property matcher
  481. restricted to unfinished TODO entries only."
  482. :group 'org-agenda-skip
  483. :group 'org-agenda-todo-list
  484. :group 'org-agenda-match-view
  485. :type 'boolean)
  486. (defcustom org-agenda-skip-scheduled-if-done nil
  487. "Non-nil means don't show scheduled items in agenda when they are done.
  488. This is relevant for the daily/weekly agenda, not for the TODO list. And
  489. it applies only to the actual date of the scheduling. Warnings about
  490. an item with a past scheduling dates are always turned off when the item
  491. is DONE."
  492. :group 'org-agenda-skip
  493. :group 'org-agenda-daily/weekly
  494. :type 'boolean)
  495. (defcustom org-agenda-skip-deadline-if-done nil
  496. "Non-nil means don't show deadlines when the corresponding item is done.
  497. When nil, the deadline is still shown and should give you a happy feeling.
  498. This is relevant for the daily/weekly agenda. And it applied only to the
  499. actually date of the deadline. Warnings about approaching and past-due
  500. deadlines are always turned off when the item is DONE."
  501. :group 'org-agenda-skip
  502. :group 'org-agenda-daily/weekly
  503. :type 'boolean)
  504. (defcustom org-agenda-skip-timestamp-if-done nil
  505. "Non-nil means don't select item by timestamp or -range if it is DONE."
  506. :group 'org-agenda-skip
  507. :group 'org-agenda-daily/weekly
  508. :type 'boolean)
  509. (defcustom org-agenda-dim-blocked-tasks t
  510. "Non-nil means, dim blocked tasks in the agenda display.
  511. This causes some overhead during agenda construction, but if you
  512. have turned on `org-enforce-todo-dependencies',
  513. `org-enforce-todo-checkbox-dependencies', or any other blocking
  514. mechanism, this will create useful feedback in the agenda.
  515. Instead ot t, this variable can also have the value `invisible'.
  516. Then blocked tasks will be invisible and only become visible when
  517. they become unblocked. An exemption to this behavior is when a task is
  518. blocked because of unchecked checkboxes below it. Since checkboxes do
  519. not show up in the agenda views, making this task invisible you remove any
  520. trace from agenda views that there is something to do. Therefore, a task
  521. that is blocked because of checkboxes will never be made invisible, it
  522. will only be dimmed."
  523. :group 'org-agenda-daily/weekly
  524. :group 'org-agenda-todo-list
  525. :type '(choice
  526. (const :tag "Do not dim" nil)
  527. (const :tag "Dim to a grey face" t)
  528. (const :tag "Make invisibe" invisible)))
  529. (defcustom org-timeline-show-empty-dates 3
  530. "Non-nil means, `org-timeline' also shows dates without an entry.
  531. When nil, only the days which actually have entries are shown.
  532. When t, all days between the first and the last date are shown.
  533. When an integer, show also empty dates, but if there is a gap of more than
  534. N days, just insert a special line indicating the size of the gap."
  535. :group 'org-agenda-skip
  536. :type '(choice
  537. (const :tag "None" nil)
  538. (const :tag "All" t)
  539. (number :tag "at most")))
  540. (defgroup org-agenda-startup nil
  541. "Options concerning initial settings in the Agenda in Org Mode."
  542. :tag "Org Agenda Startup"
  543. :group 'org-agenda)
  544. (defcustom org-finalize-agenda-hook nil
  545. "Hook run just before displaying an agenda buffer."
  546. :group 'org-agenda-startup
  547. :type 'hook)
  548. (defcustom org-agenda-mouse-1-follows-link nil
  549. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  550. A longer mouse click will still set point. Does not work on XEmacs.
  551. Needs to be set before org.el is loaded."
  552. :group 'org-agenda-startup
  553. :type 'boolean)
  554. (defcustom org-agenda-start-with-follow-mode nil
  555. "The initial value of follow-mode in a newly created agenda window."
  556. :group 'org-agenda-startup
  557. :type 'boolean)
  558. (defvar org-agenda-include-inactive-timestamps nil
  559. "Non-nil means, include inactive time stamps in agenda and timeline.")
  560. (defgroup org-agenda-windows nil
  561. "Options concerning the windows used by the Agenda in Org Mode."
  562. :tag "Org Agenda Windows"
  563. :group 'org-agenda)
  564. (defcustom org-agenda-window-setup 'reorganize-frame
  565. "How the agenda buffer should be displayed.
  566. Possible values for this option are:
  567. current-window Show agenda in the current window, keeping all other windows.
  568. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  569. other-window Use `switch-to-buffer-other-window' to display agenda.
  570. reorganize-frame Show only two windows on the current frame, the current
  571. window and the agenda.
  572. See also the variable `org-agenda-restore-windows-after-quit'."
  573. :group 'org-agenda-windows
  574. :type '(choice
  575. (const current-window)
  576. (const other-frame)
  577. (const other-window)
  578. (const reorganize-frame)))
  579. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  580. "The min and max height of the agenda window as a fraction of frame height.
  581. The value of the variable is a cons cell with two numbers between 0 and 1.
  582. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  583. :group 'org-agenda-windows
  584. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  585. (defcustom org-agenda-restore-windows-after-quit nil
  586. "Non-nil means, restore window configuration open exiting agenda.
  587. Before the window configuration is changed for displaying the agenda,
  588. the current status is recorded. When the agenda is exited with
  589. `q' or `x' and this option is set, the old state is restored. If
  590. `org-agenda-window-setup' is `other-frame', the value of this
  591. option will be ignored.."
  592. :group 'org-agenda-windows
  593. :type 'boolean)
  594. (defcustom org-agenda-ndays 7
  595. "Number of days to include in overview display.
  596. Should be 1 or 7.
  597. Custom commands can set this variable in the options section."
  598. :group 'org-agenda-daily/weekly
  599. :type 'number)
  600. (defcustom org-agenda-start-on-weekday 1
  601. "Non-nil means, start the overview always on the specified weekday.
  602. 0 denotes Sunday, 1 denotes Monday etc.
  603. When nil, always start on the current day.
  604. Custom commands can set this variable in the options section."
  605. :group 'org-agenda-daily/weekly
  606. :type '(choice (const :tag "Today" nil)
  607. (number :tag "Weekday No.")))
  608. (defcustom org-agenda-show-all-dates t
  609. "Non-nil means, `org-agenda' shows every day in the selected range.
  610. When nil, only the days which actually have entries are shown."
  611. :group 'org-agenda-daily/weekly
  612. :type 'boolean)
  613. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  614. "Format string for displaying dates in the agenda.
  615. Used by the daily/weekly agenda and by the timeline. This should be
  616. a format string understood by `format-time-string', or a function returning
  617. the formatted date as a string. The function must take a single argument,
  618. a calendar-style date list like (month day year)."
  619. :group 'org-agenda-daily/weekly
  620. :type '(choice
  621. (string :tag "Format string")
  622. (function :tag "Function")))
  623. (defun org-agenda-format-date-aligned (date)
  624. "Format a date string for display in the daily/weekly agenda, or timeline.
  625. This function makes sure that dates are aligned for easy reading."
  626. (require 'cal-iso)
  627. (let* ((dayname (calendar-day-name date))
  628. (day (cadr date))
  629. (day-of-week (calendar-day-of-week date))
  630. (month (car date))
  631. (monthname (calendar-month-name month))
  632. (year (nth 2 date))
  633. (iso-week (org-days-to-iso-week
  634. (calendar-absolute-from-gregorian date)))
  635. (weekyear (cond ((and (= month 1) (>= iso-week 52))
  636. (1- year))
  637. ((and (= month 12) (<= iso-week 1))
  638. (1+ year))
  639. (t year)))
  640. (weekstring (if (= day-of-week 1)
  641. (format " W%02d" iso-week)
  642. "")))
  643. (format "%-10s %2d %s %4d%s"
  644. dayname day monthname year weekstring)))
  645. (defcustom org-agenda-weekend-days '(6 0)
  646. "Which days are weekend?
  647. These days get the special face `org-agenda-date-weekend' in the agenda
  648. and timeline buffers."
  649. :group 'org-agenda-daily/weekly
  650. :type '(set :greedy t
  651. (const :tag "Monday" 1)
  652. (const :tag "Tuesday" 2)
  653. (const :tag "Wednesday" 3)
  654. (const :tag "Thursday" 4)
  655. (const :tag "Friday" 5)
  656. (const :tag "Saturday" 6)
  657. (const :tag "Sunday" 0)))
  658. (defcustom org-agenda-include-diary nil
  659. "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
  660. Custom commands can set this variable in the options section."
  661. :group 'org-agenda-daily/weekly
  662. :type 'boolean)
  663. (defcustom org-agenda-include-all-todo nil
  664. "Set means weekly/daily agenda will always contain all TODO entries.
  665. The TODO entries will be listed at the top of the agenda, before
  666. the entries for specific days.
  667. This option is deprecated, it is better to define a block agenda instead."
  668. :group 'org-agenda-daily/weekly
  669. :type 'boolean)
  670. (defcustom org-agenda-repeating-timestamp-show-all t
  671. "Non-nil means, show all occurrences of a repeating stamp in the agenda.
  672. When nil, only one occurrence is shown, either today or the
  673. nearest into the future."
  674. :group 'org-agenda-daily/weekly
  675. :type 'boolean)
  676. (defcustom org-scheduled-past-days 10000
  677. "No. of days to continue listing scheduled items that are not marked DONE.
  678. When an item is scheduled on a date, it shows up in the agenda on this
  679. day and will be listed until it is marked done for the number of days
  680. given here."
  681. :group 'org-agenda-daily/weekly
  682. :type 'number)
  683. (defcustom org-agenda-log-mode-items '(closed clock)
  684. "List of items that should be shown in agenda log mode.
  685. This list may contain the following symbols:
  686. closed Show entries that have been closed on that day.
  687. clock Show entries that have received clocked time on that day.
  688. state Show all logged state changes.
  689. Note that instead of changing this variable, you can also press `C-u l' in
  690. the agenda to display all available LOG items temporarily."
  691. :group 'org-agenda-daily/weekly
  692. :type '(set :greedy t (const closed) (const clock) (const state)))
  693. (defcustom org-agenda-start-with-clockreport-mode nil
  694. "The initial value of clockreport-mode in a newly created agenda window."
  695. :group 'org-agenda-startup
  696. :group 'org-agenda-daily/weekly
  697. :type 'boolean)
  698. (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
  699. "Property list with parameters for the clocktable in clockreport mode.
  700. This is the display mode that shows a clock table in the daily/weekly
  701. agenda, the properties for this dynamic block can be set here.
  702. The usual clocktable parameters are allowed here, but you cannot set
  703. the properties :name, :tstart, :tend, :block, and :scope - these will
  704. be overwritten to make sure the content accurately reflects the
  705. current display in the agenda."
  706. :group 'org-agenda-daily/weekly
  707. :type 'plist)
  708. (defgroup org-agenda-time-grid nil
  709. "Options concerning the time grid in the Org-mode Agenda."
  710. :tag "Org Agenda Time Grid"
  711. :group 'org-agenda)
  712. (defcustom org-agenda-search-headline-for-time t
  713. "Non-nil means, search headline for a time-of-day.
  714. If the headline contains a time-of-day in one format or another, it will
  715. be used to sort the entry into the time sequence of items for a day.
  716. Some people have time stamps in the headline that refer to the creation
  717. time or so, and then this produces an unwanted side effect. If this is
  718. the case for your, use this variable to turn off searching the headline
  719. for a time."
  720. :group 'org-agenda-time-grid
  721. :type 'boolean)
  722. (defcustom org-agenda-use-time-grid t
  723. "Non-nil means, show a time grid in the agenda schedule.
  724. A time grid is a set of lines for specific times (like every two hours between
  725. 8:00 and 20:00). The items scheduled for a day at specific times are
  726. sorted in between these lines.
  727. For details about when the grid will be shown, and what it will look like, see
  728. the variable `org-agenda-time-grid'."
  729. :group 'org-agenda-time-grid
  730. :type 'boolean)
  731. (defcustom org-agenda-time-grid
  732. '((daily today require-timed)
  733. "----------------"
  734. (800 1000 1200 1400 1600 1800 2000))
  735. "The settings for time grid for agenda display.
  736. This is a list of three items. The first item is again a list. It contains
  737. symbols specifying conditions when the grid should be displayed:
  738. daily if the agenda shows a single day
  739. weekly if the agenda shows an entire week
  740. today show grid on current date, independent of daily/weekly display
  741. require-timed show grid only if at least one item has a time specification
  742. The second item is a string which will be placed behind the grid time.
  743. The third item is a list of integers, indicating the times that should have
  744. a grid line."
  745. :group 'org-agenda-time-grid
  746. :type
  747. '(list
  748. (set :greedy t :tag "Grid Display Options"
  749. (const :tag "Show grid in single day agenda display" daily)
  750. (const :tag "Show grid in weekly agenda display" weekly)
  751. (const :tag "Always show grid for today" today)
  752. (const :tag "Show grid only if any timed entries are present"
  753. require-timed)
  754. (const :tag "Skip grid times already present in an entry"
  755. remove-match))
  756. (string :tag "Grid String")
  757. (repeat :tag "Grid Times" (integer :tag "Time"))))
  758. (defgroup org-agenda-sorting nil
  759. "Options concerning sorting in the Org-mode Agenda."
  760. :tag "Org Agenda Sorting"
  761. :group 'org-agenda)
  762. (defcustom org-agenda-sorting-strategy
  763. '((agenda time-up priority-down category-keep)
  764. (todo priority-down category-keep)
  765. (tags priority-down category-keep)
  766. (search category-keep))
  767. "Sorting structure for the agenda items of a single day.
  768. This is a list of symbols which will be used in sequence to determine
  769. if an entry should be listed before another entry. The following
  770. symbols are recognized:
  771. time-up Put entries with time-of-day indications first, early first
  772. time-down Put entries with time-of-day indications first, late first
  773. category-keep Keep the default order of categories, corresponding to the
  774. sequence in `org-agenda-files'.
  775. category-up Sort alphabetically by category, A-Z.
  776. category-down Sort alphabetically by category, Z-A.
  777. tag-up Sort alphabetically by last tag, A-Z.
  778. tag-down Sort alphabetically by last tag, Z-A.
  779. priority-up Sort numerically by priority, high priority last.
  780. priority-down Sort numerically by priority, high priority first.
  781. todo-state-up Sort by todo state, tasks that are done last.
  782. todo-state-down Sort by todo state, tasks that are done first.
  783. effort-up Sort numerically by estimated effort, high effort last.
  784. effort-down Sort numerically by estimated effort, high effort first.
  785. user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
  786. user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
  787. The different possibilities will be tried in sequence, and testing stops
  788. if one comparison returns a \"not-equal\". For example, the default
  789. '(time-up category-keep priority-down)
  790. means: Pull out all entries having a specified time of day and sort them,
  791. in order to make a time schedule for the current day the first thing in the
  792. agenda listing for the day. Of the entries without a time indication, keep
  793. the grouped in categories, don't sort the categories, but keep them in
  794. the sequence given in `org-agenda-files'. Within each category sort by
  795. priority.
  796. Leaving out `category-keep' would mean that items will be sorted across
  797. categories by priority.
  798. Instead of a single list, this can also be a set of list for specific
  799. contents, with a context symbol in the car of the list, any of
  800. `agenda', `todo', `tags' for the corresponding agenda views.
  801. Custom commands can bind this variable in the options section."
  802. :group 'org-agenda-sorting
  803. :type `(choice
  804. (repeat :tag "General" ,org-sorting-choice)
  805. (list :tag "Individually"
  806. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  807. (repeat ,org-sorting-choice))
  808. (cons (const :tag "Strategy for TODO lists" todo)
  809. (repeat ,org-sorting-choice))
  810. (cons (const :tag "Strategy for Tags matches" tags)
  811. (repeat ,org-sorting-choice)))))
  812. (defcustom org-agenda-cmp-user-defined nil
  813. "A function to define the comparison `user-defined'.
  814. This function must receive two arguments, agenda entry a and b.
  815. If a>b, return +1. If a<b, return -1. If they are equal as seen by
  816. the user comparison, return nil.
  817. When this is defined, you can make `user-defined-up' and `user-defined-down'
  818. part of an agenda sorting strategy."
  819. :group 'org-agenda-sorting
  820. :type 'symbol)
  821. (defcustom org-sort-agenda-notime-is-late t
  822. "Non-nil means, items without time are considered late.
  823. This is only relevant for sorting. When t, items which have no explicit
  824. time like 15:30 will be considered as 99:01, i.e. later than any items which
  825. do have a time. When nil, the default time is before 0:00. You can use this
  826. option to decide if the schedule for today should come before or after timeless
  827. agenda entries."
  828. :group 'org-agenda-sorting
  829. :type 'boolean)
  830. (defcustom org-sort-agenda-noeffort-is-high t
  831. "Non-nil means, items without effort estimate are sorted as high effort.
  832. This also applies when filtering an agenda view with respect to the
  833. < or > effort operator. Then, tasks with no effort defined will be treated
  834. as tasks with high effort.
  835. When nil, such items are sorted as 0 minutes effort."
  836. :group 'org-agenda-sorting
  837. :type 'boolean)
  838. (defgroup org-agenda-line-format nil
  839. "Options concerning the entry prefix in the Org-mode agenda display."
  840. :tag "Org Agenda Line Format"
  841. :group 'org-agenda)
  842. (defcustom org-agenda-prefix-format
  843. '((agenda . " %-12:c%?-12t% s")
  844. (timeline . " % s")
  845. (todo . " %-12:c")
  846. (tags . " %-12:c")
  847. (search . " %-12:c"))
  848. "Format specifications for the prefix of items in the agenda views.
  849. An alist with four entries, for the different agenda types. The keys to the
  850. sublists are `agenda', `timeline', `todo', and `tags'. The values
  851. are format strings.
  852. This format works similar to a printf format, with the following meaning:
  853. %c the category of the item, \"Diary\" for entries from the diary, or
  854. as given by the CATEGORY keyword or derived from the file name.
  855. %T the *last* tag of the item. Last because inherited tags come
  856. first in the list.
  857. %t the time-of-day specification if one applies to the entry, in the
  858. format HH:MM
  859. %s Scheduling/Deadline information, a short string
  860. All specifiers work basically like the standard `%s' of printf, but may
  861. contain two additional characters: A question mark just after the `%' and
  862. a whitespace/punctuation character just before the final letter.
  863. If the first character after `%' is a question mark, the entire field
  864. will only be included if the corresponding value applies to the
  865. current entry. This is useful for fields which should have fixed
  866. width when present, but zero width when absent. For example,
  867. \"%?-12t\" will result in a 12 character time field if a time of the
  868. day is specified, but will completely disappear in entries which do
  869. not contain a time.
  870. If there is punctuation or whitespace character just before the final
  871. format letter, this character will be appended to the field value if
  872. the value is not empty. For example, the format \"%-12:c\" leads to
  873. \"Diary: \" if the category is \"Diary\". If the category were be
  874. empty, no additional colon would be interted.
  875. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  876. - Indent the line with two space characters
  877. - Give the category in a 12 chars wide field, padded with whitespace on
  878. the right (because of `-'). Append a colon if there is a category
  879. (because of `:').
  880. - If there is a time-of-day, put it into a 12 chars wide field. If no
  881. time, don't put in an empty field, just skip it (because of '?').
  882. - Finally, put the scheduling information and append a whitespace.
  883. As another example, if you don't want the time-of-day of entries in
  884. the prefix, you could use:
  885. (setq org-agenda-prefix-format \" %-11:c% s\")
  886. See also the variables `org-agenda-remove-times-when-in-prefix' and
  887. `org-agenda-remove-tags'.
  888. Custom commands can set this variable in the options section."
  889. :type '(choice
  890. (string :tag "General format")
  891. (list :greedy t :tag "View dependent"
  892. (cons (const agenda) (string :tag "Format"))
  893. (cons (const timeline) (string :tag "Format"))
  894. (cons (const todo) (string :tag "Format"))
  895. (cons (const tags) (string :tag "Format"))
  896. (cons (const search) (string :tag "Format"))))
  897. :group 'org-agenda-line-format)
  898. (defvar org-prefix-format-compiled nil
  899. "The compiled version of the most recently used prefix format.
  900. See the variable `org-agenda-prefix-format'.")
  901. (defcustom org-agenda-todo-keyword-format "%-1s"
  902. "Format for the TODO keyword in agenda lines.
  903. Set this to something like \"%-12s\" if you want all TODO keywords
  904. to occupy a fixed space in the agenda display."
  905. :group 'org-agenda-line-format
  906. :type 'string)
  907. (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
  908. "Text preceding timerange entries in the agenda view.
  909. This is a list with two strings. The first applies when the range
  910. is entirely on one day. The second applies if the range spans several days.
  911. The strings may have two \"%d\" format specifiers which will be filled
  912. with the sequence number of the days, and the total number of days in the
  913. range, respectively."
  914. :group 'org-agenda-line-format
  915. :type '(list
  916. (string :tag "Deadline today ")
  917. (choice :tag "Deadline relative"
  918. (string :tag "Format string")
  919. (function))))
  920. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  921. "Text preceeding scheduled items in the agenda view.
  922. This is a list with two strings. The first applies when the item is
  923. scheduled on the current day. The second applies when it has been scheduled
  924. previously, it may contain a %d indicating that this is the nth time that
  925. this item is scheduled, due to automatic rescheduling of unfinished items
  926. for the following day. So this number is one larger than the number of days
  927. that passed since this item was scheduled first."
  928. :group 'org-agenda-line-format
  929. :type '(list
  930. (string :tag "Scheduled today ")
  931. (string :tag "Scheduled previously")))
  932. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  933. "Text preceeding deadline items in the agenda view.
  934. This is a list with two strings. The first applies when the item has its
  935. deadline on the current day. The second applies when it is in the past or
  936. in the future, it may contain %d to capture how many days away the deadline
  937. is (was)."
  938. :group 'org-agenda-line-format
  939. :type '(list
  940. (string :tag "Deadline today ")
  941. (choice :tag "Deadline relative"
  942. (string :tag "Format string")
  943. (function))))
  944. (defcustom org-agenda-remove-times-when-in-prefix t
  945. "Non-nil means, remove duplicate time specifications in agenda items.
  946. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  947. time-of-day specification in a headline or diary entry is extracted and
  948. placed into the prefix. If this option is non-nil, the original specification
  949. \(a timestamp or -range, or just a plain time(range) specification like
  950. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  951. cluttered.
  952. The option can be t or nil. It may also be the symbol `beg', indicating
  953. that the time should only be removed what it is located at the beginning of
  954. the headline/diary entry."
  955. :group 'org-agenda-line-format
  956. :type '(choice
  957. (const :tag "Always" t)
  958. (const :tag "Never" nil)
  959. (const :tag "When at beginning of entry" beg)))
  960. (defcustom org-agenda-default-appointment-duration nil
  961. "Default duration for appointments that only have a starting time.
  962. When nil, no duration is specified in such cases.
  963. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  964. :group 'org-agenda-line-format
  965. :type '(choice
  966. (integer :tag "Minutes")
  967. (const :tag "No default duration")))
  968. (defcustom org-agenda-show-inherited-tags t
  969. "Non-nil means, show inherited tags in each agenda line."
  970. :group 'org-agenda-line-format
  971. :type 'boolean)
  972. (defcustom org-agenda-remove-tags nil
  973. "Non-nil means, remove the tags from the headline copy in the agenda.
  974. When this is the symbol `prefix', only remove tags when
  975. `org-agenda-prefix-format' contains a `%T' specifier."
  976. :group 'org-agenda-line-format
  977. :type '(choice
  978. (const :tag "Always" t)
  979. (const :tag "Never" nil)
  980. (const :tag "When prefix format contains %T" prefix)))
  981. (if (fboundp 'defvaralias)
  982. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  983. 'org-agenda-remove-tags))
  984. (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
  985. "Shift tags in agenda items to this column.
  986. If this number is positive, it specifies the column. If it is negative,
  987. it means that the tags should be flushright to that column. For example,
  988. -80 works well for a normal 80 character screen."
  989. :group 'org-agenda-line-format
  990. :type 'integer)
  991. (if (fboundp 'defvaralias)
  992. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  993. (defcustom org-agenda-fontify-priorities 'cookies
  994. "Non-nil means, highlight low and high priorities in agenda.
  995. When t, the highest priority entries are bold, lowest priority italic.
  996. However, settings in org-priority-faces will overrule these faces.
  997. When this variable is the symbol `cookies', only fontify the
  998. cookies, not the entire task.
  999. This may also be an association list of priority faces, whose
  1000. keys are the character values of `org-highest-priority',
  1001. `org-default-priority', and `org-lowest-priority' (the default values
  1002. are ?A, ?B, and ?C, respectively). The face may be a named face,
  1003. or a list like `(:background \"Red\")'."
  1004. :group 'org-agenda-line-format
  1005. :type '(choice
  1006. (const :tag "Never" nil)
  1007. (const :tag "Defaults" t)
  1008. (const :tag "Cookies only" cookies)
  1009. (repeat :tag "Specify"
  1010. (list (character :tag "Priority" :value ?A)
  1011. (sexp :tag "face")))))
  1012. (defgroup org-agenda-column-view nil
  1013. "Options concerning column view in the agenda."
  1014. :tag "Org Agenda Column View"
  1015. :group 'org-agenda)
  1016. (defcustom org-agenda-columns-show-summaries t
  1017. "Non-nil means, show summaries for columns displayed in the agenda view."
  1018. :group 'org-agenda-column-view
  1019. :type 'boolean)
  1020. (defcustom org-agenda-columns-remove-prefix-from-item t
  1021. "Non-nil means, remove the prefix from a headline for agenda column view.
  1022. The special ITEM field in the columns format contains the current line, with
  1023. all information shown in other columns (like the TODO state or a tag).
  1024. When this variable is non-nil, also the agenda prefix will be removed from
  1025. the content of the ITEM field, to make sure as much as possible of the
  1026. headline can be shown in the limited width of the field."
  1027. :group 'org-agenda
  1028. :type 'boolean)
  1029. (defcustom org-agenda-columns-compute-summary-properties t
  1030. "Non-nil means, recompute all summary properties before column view.
  1031. When column view in the agenda is listing properties that have a summary
  1032. operator, it can go to all relevant buffers and recompute the summaries
  1033. there. This can mean overhead for the agenda column view, but is necessary
  1034. to have thing up to date.
  1035. As a special case, a CLOCKSUM property also makes sure that the clock
  1036. computations are current."
  1037. :group 'org-agenda-column-view
  1038. :type 'boolean)
  1039. (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
  1040. "Non-nil means, the duration of an appointment will add to day effort.
  1041. The property to which appointment durations will be added is the one given
  1042. in the option `org-effort-property'. If an appointment does not have
  1043. an end time, `org-agenda-default-appointment-duration' will be used. If that
  1044. is not set, an appointment without end time will not contribute to the time
  1045. estimate."
  1046. :group 'org-agenda-column-view
  1047. :type 'boolean)
  1048. (eval-when-compile
  1049. (require 'cl))
  1050. (require 'org)
  1051. (defun org-add-agenda-custom-command (entry)
  1052. "Replace or add a command in `org-agenda-custom-commands'.
  1053. This is mostly for hacking and trying a new command - once the command
  1054. works you probably want to add it to `org-agenda-custom-commands' for good."
  1055. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  1056. (if ass
  1057. (setcdr ass (cdr entry))
  1058. (push entry org-agenda-custom-commands))))
  1059. ;;; Define the Org-agenda-mode
  1060. (defvar org-agenda-mode-map (make-sparse-keymap)
  1061. "Keymap for `org-agenda-mode'.")
  1062. (defvar org-agenda-menu) ; defined later in this file.
  1063. (defvar org-agenda-follow-mode nil)
  1064. (defvar org-agenda-clockreport-mode nil)
  1065. (defvar org-agenda-show-log nil)
  1066. (defvar org-agenda-redo-command nil)
  1067. (defvar org-agenda-query-string nil)
  1068. (defvar org-agenda-mode-hook nil
  1069. "Hook for org-agenda-mode, run after the mode is turned on.")
  1070. (defvar org-agenda-type nil)
  1071. (defvar org-agenda-force-single-file nil)
  1072. (defun org-agenda-mode ()
  1073. "Mode for time-sorted view on action items in Org-mode files.
  1074. The following commands are available:
  1075. \\{org-agenda-mode-map}"
  1076. (interactive)
  1077. (kill-all-local-variables)
  1078. (setq org-agenda-undo-list nil
  1079. org-agenda-pending-undo-list nil)
  1080. (setq major-mode 'org-agenda-mode)
  1081. ;; Keep global-font-lock-mode from turning on font-lock-mode
  1082. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  1083. (setq mode-name "Org-Agenda")
  1084. (use-local-map org-agenda-mode-map)
  1085. (easy-menu-add org-agenda-menu)
  1086. (if org-startup-truncated (setq truncate-lines t))
  1087. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  1088. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  1089. ;; Make sure properties are removed when copying text
  1090. (when (boundp 'buffer-substring-filters)
  1091. (org-set-local 'buffer-substring-filters
  1092. (cons (lambda (x)
  1093. (set-text-properties 0 (length x) nil x) x)
  1094. buffer-substring-filters)))
  1095. (unless org-agenda-keep-modes
  1096. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  1097. org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
  1098. org-agenda-show-log nil))
  1099. (easy-menu-change
  1100. '("Agenda") "Agenda Files"
  1101. (append
  1102. (list
  1103. (vector
  1104. (if (get 'org-agenda-files 'org-restrict)
  1105. "Restricted to single file"
  1106. "Edit File List")
  1107. '(org-edit-agenda-file-list)
  1108. (not (get 'org-agenda-files 'org-restrict)))
  1109. "--")
  1110. (mapcar 'org-file-menu-entry (org-agenda-files))))
  1111. (org-agenda-set-mode-name)
  1112. (apply
  1113. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  1114. (list 'org-agenda-mode-hook)))
  1115. (substitute-key-definition 'undo 'org-agenda-undo
  1116. org-agenda-mode-map global-map)
  1117. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  1118. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  1119. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  1120. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  1121. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  1122. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  1123. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  1124. (org-defkey org-agenda-mode-map "A" 'org-agenda-archive-to-archive-sibling)
  1125. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  1126. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  1127. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  1128. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  1129. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  1130. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  1131. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  1132. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  1133. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  1134. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  1135. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  1136. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  1137. (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
  1138. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  1139. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  1140. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  1141. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  1142. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  1143. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  1144. (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
  1145. (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
  1146. (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
  1147. (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
  1148. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
  1149. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
  1150. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
  1151. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
  1152. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  1153. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  1154. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  1155. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  1156. (while l (org-defkey org-agenda-mode-map
  1157. (int-to-string (pop l)) 'digit-argument)))
  1158. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  1159. (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
  1160. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  1161. (org-defkey org-agenda-mode-map "v" 'org-agenda-archives-mode)
  1162. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  1163. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  1164. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  1165. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  1166. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  1167. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  1168. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  1169. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  1170. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  1171. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  1172. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  1173. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  1174. (org-defkey org-agenda-mode-map "n" 'next-line)
  1175. (org-defkey org-agenda-mode-map "p" 'previous-line)
  1176. (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
  1177. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  1178. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  1179. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  1180. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  1181. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  1182. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  1183. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  1184. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  1185. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  1186. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  1187. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  1188. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  1189. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  1190. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  1191. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  1192. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  1193. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  1194. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  1195. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  1196. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  1197. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  1198. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  1199. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  1200. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  1201. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  1202. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  1203. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  1204. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  1205. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  1206. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  1207. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  1208. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  1209. (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
  1210. (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
  1211. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  1212. "Local keymap for agenda entries from Org-mode.")
  1213. (org-defkey org-agenda-keymap
  1214. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  1215. (org-defkey org-agenda-keymap
  1216. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  1217. (when org-agenda-mouse-1-follows-link
  1218. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  1219. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  1220. '("Agenda"
  1221. ("Agenda Files")
  1222. "--"
  1223. ["Show" org-agenda-show t]
  1224. ["Go To (other window)" org-agenda-goto t]
  1225. ["Go To (this window)" org-agenda-switch-to t]
  1226. ["Follow Mode" org-agenda-follow-mode
  1227. :style toggle :selected org-agenda-follow-mode :active t]
  1228. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  1229. "--"
  1230. ["Cycle TODO" org-agenda-todo t]
  1231. ("Archive"
  1232. ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
  1233. ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
  1234. ["Archive subtree" org-agenda-archive t])
  1235. ["Delete subtree" org-agenda-kill t]
  1236. ["Add note" org-agenda-add-note t]
  1237. "--"
  1238. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  1239. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  1240. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  1241. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  1242. "--"
  1243. ("Tags and Properties"
  1244. ["Show all Tags" org-agenda-show-tags t]
  1245. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  1246. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  1247. "--"
  1248. ["Column View" org-columns t])
  1249. ("Date/Schedule"
  1250. ["Schedule" org-agenda-schedule t]
  1251. ["Set Deadline" org-agenda-deadline t]
  1252. "--"
  1253. ["Mark item" org-agenda-action :active t :keys "k m"]
  1254. ["Show mark item" org-agenda-action :active t :keys "k v"]
  1255. ["Schedule marked item" org-agenda-action :active t :keys "k s"]
  1256. ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
  1257. "--"
  1258. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  1259. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  1260. ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
  1261. ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
  1262. ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
  1263. ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
  1264. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  1265. ("Clock"
  1266. ["Clock in" org-agenda-clock-in t]
  1267. ["Clock out" org-agenda-clock-out t]
  1268. ["Clock cancel" org-agenda-clock-cancel t]
  1269. ["Goto running clock" org-clock-goto t])
  1270. ("Priority"
  1271. ["Set Priority" org-agenda-priority t]
  1272. ["Increase Priority" org-agenda-priority-up t]
  1273. ["Decrease Priority" org-agenda-priority-down t]
  1274. ["Show Priority" org-agenda-show-priority t])
  1275. ("Calendar/Diary"
  1276. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  1277. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  1278. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  1279. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  1280. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  1281. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  1282. "--"
  1283. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  1284. "--"
  1285. ("View"
  1286. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  1287. :style radio :selected (equal org-agenda-ndays 1)]
  1288. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  1289. :style radio :selected (equal org-agenda-ndays 7)]
  1290. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  1291. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  1292. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  1293. :style radio :selected (member org-agenda-ndays '(365 366))]
  1294. "--"
  1295. ["Include Diary" org-agenda-toggle-diary
  1296. :style toggle :selected org-agenda-include-diary
  1297. :active (org-agenda-check-type nil 'agenda)]
  1298. ["Use Time Grid" org-agenda-toggle-time-grid
  1299. :style toggle :selected org-agenda-use-time-grid
  1300. :active (org-agenda-check-type nil 'agenda)]
  1301. "--"
  1302. ["Show clock report" org-agenda-clockreport-mode
  1303. :style toggle :selected org-agenda-clockreport-mode
  1304. :active (org-agenda-check-type nil 'agenda)]
  1305. "--"
  1306. ["Show Logbook entries" org-agenda-log-mode
  1307. :style toggle :selected org-agenda-show-log
  1308. :active (org-agenda-check-type nil 'agenda 'timeline)]
  1309. ["Include archived trees" org-agenda-archives-mode
  1310. :style toggle :selected org-agenda-archives-mode :active t]
  1311. ["Include archive files" (org-agenda-archives-mode t)
  1312. :style toggle :selected (eq org-agenda-archives-mode t) :active t
  1313. :keys "C-u v"])
  1314. ["Write view to file" org-write-agenda t]
  1315. ["Rebuild buffer" org-agenda-redo t]
  1316. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  1317. "--"
  1318. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  1319. "--"
  1320. ["Quit" org-agenda-quit t]
  1321. ["Exit and Release Buffers" org-agenda-exit t]
  1322. ))
  1323. ;;; Agenda undo
  1324. (defvar org-agenda-allow-remote-undo t
  1325. "Non-nil means, allow remote undo from the agenda buffer.")
  1326. (defvar org-agenda-undo-list nil
  1327. "List of undoable operations in the agenda since last refresh.")
  1328. (defvar org-agenda-undo-has-started-in nil
  1329. "Buffers that have already seen `undo-start' in the current undo sequence.")
  1330. (defvar org-agenda-pending-undo-list nil
  1331. "In a series of undo commands, this is the list of remaining undo items.")
  1332. (defun org-agenda-undo ()
  1333. "Undo a remote editing step in the agenda.
  1334. This undoes changes both in the agenda buffer and in the remote buffer
  1335. that have been changed along."
  1336. (interactive)
  1337. (or org-agenda-allow-remote-undo
  1338. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  1339. (if (not (eq this-command last-command))
  1340. (setq org-agenda-undo-has-started-in nil
  1341. org-agenda-pending-undo-list org-agenda-undo-list))
  1342. (if (not org-agenda-pending-undo-list)
  1343. (error "No further undo information"))
  1344. (let* ((entry (pop org-agenda-pending-undo-list))
  1345. buf line cmd rembuf)
  1346. (setq cmd (pop entry) line (pop entry))
  1347. (setq rembuf (nth 2 entry))
  1348. (org-with-remote-undo rembuf
  1349. (while (bufferp (setq buf (pop entry)))
  1350. (if (pop entry)
  1351. (with-current-buffer buf
  1352. (let ((last-undo-buffer buf)
  1353. (inhibit-read-only t))
  1354. (unless (memq buf org-agenda-undo-has-started-in)
  1355. (push buf org-agenda-undo-has-started-in)
  1356. (make-local-variable 'pending-undo-list)
  1357. (undo-start))
  1358. (while (and pending-undo-list
  1359. (listp pending-undo-list)
  1360. (not (car pending-undo-list)))
  1361. (pop pending-undo-list))
  1362. (undo-more 1))))))
  1363. (goto-line line)
  1364. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  1365. (defun org-verify-change-for-undo (l1 l2)
  1366. "Verify that a real change occurred between the undo lists L1 and L2."
  1367. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  1368. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  1369. (not (eq l1 l2)))
  1370. ;;; Agenda dispatch
  1371. (defvar org-agenda-restrict nil)
  1372. (defvar org-agenda-restrict-begin (make-marker))
  1373. (defvar org-agenda-restrict-end (make-marker))
  1374. (defvar org-agenda-last-dispatch-buffer nil)
  1375. (defvar org-agenda-overriding-restriction nil)
  1376. ;;;###autoload
  1377. (defun org-agenda (&optional arg keys restriction)
  1378. "Dispatch agenda commands to collect entries to the agenda buffer.
  1379. Prompts for a command to execute. Any prefix arg will be passed
  1380. on to the selected command. The default selections are:
  1381. a Call `org-agenda-list' to display the agenda for current day or week.
  1382. t Call `org-todo-list' to display the global todo list.
  1383. T Call `org-todo-list' to display the global todo list, select only
  1384. entries with a specific TODO keyword (the user gets a prompt).
  1385. m Call `org-tags-view' to display headlines with tags matching
  1386. a condition (the user is prompted for the condition).
  1387. M Like `m', but select only TODO entries, no ordinary headlines.
  1388. L Create a timeline for the current buffer.
  1389. e Export views to associated files.
  1390. s Search entries for keywords.
  1391. / Multi occur accros all agenda files and also files listed
  1392. in `org-agenda-text-search-extra-files'.
  1393. < Restrict agenda commands to buffer, subtree, or region.
  1394. Press several times to get the desired effect.
  1395. > Remove a previous restriction.
  1396. # List \"stuck\" projects.
  1397. ! Configure what \"stuck\" means.
  1398. C Configure custom agenda commands.
  1399. More commands can be added by configuring the variable
  1400. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  1401. searches can be pre-defined in this way.
  1402. If the current buffer is in Org-mode and visiting a file, you can also
  1403. first press `<' once to indicate that the agenda should be temporarily
  1404. \(until the next use of \\[org-agenda]) restricted to the current file.
  1405. Pressing `<' twice means to restrict to the current subtree or region
  1406. \(if active)."
  1407. (interactive "P")
  1408. (catch 'exit
  1409. (let* ((prefix-descriptions nil)
  1410. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  1411. (org-agenda-custom-commands
  1412. ;; normalize different versions
  1413. (delq nil
  1414. (mapcar
  1415. (lambda (x)
  1416. (cond ((stringp (cdr x))
  1417. (push x prefix-descriptions)
  1418. nil)
  1419. ((stringp (nth 1 x)) x)
  1420. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  1421. (t (cons (car x) (cons "" (cdr x))))))
  1422. org-agenda-custom-commands)))
  1423. (buf (current-buffer))
  1424. (bfn (buffer-file-name (buffer-base-buffer)))
  1425. entry key type match lprops ans)
  1426. ;; Turn off restriction unless there is an overriding one
  1427. (unless org-agenda-overriding-restriction
  1428. (put 'org-agenda-files 'org-restrict nil)
  1429. (setq org-agenda-restrict nil)
  1430. (move-marker org-agenda-restrict-begin nil)
  1431. (move-marker org-agenda-restrict-end nil))
  1432. ;; Delete old local properties
  1433. (put 'org-agenda-redo-command 'org-lprops nil)
  1434. ;; Remember where this call originated
  1435. (setq org-agenda-last-dispatch-buffer (current-buffer))
  1436. (unless keys
  1437. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  1438. keys (car ans)
  1439. restriction (cdr ans)))
  1440. ;; Estabish the restriction, if any
  1441. (when (and (not org-agenda-overriding-restriction) restriction)
  1442. (put 'org-agenda-files 'org-restrict (list bfn))
  1443. (cond
  1444. ((eq restriction 'region)
  1445. (setq org-agenda-restrict t)
  1446. (move-marker org-agenda-restrict-begin (region-beginning))
  1447. (move-marker org-agenda-restrict-end (region-end)))
  1448. ((eq restriction 'subtree)
  1449. (save-excursion
  1450. (setq org-agenda-restrict t)
  1451. (org-back-to-heading t)
  1452. (move-marker org-agenda-restrict-begin (point))
  1453. (move-marker org-agenda-restrict-end
  1454. (progn (org-end-of-subtree t)))))))
  1455. (require 'calendar) ; FIXME: can we avoid this for some commands?
  1456. ;; For example the todo list should not need it (but does...)
  1457. (cond
  1458. ((setq entry (assoc keys org-agenda-custom-commands))
  1459. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  1460. (progn
  1461. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  1462. (put 'org-agenda-redo-command 'org-lprops lprops)
  1463. (cond
  1464. ((eq type 'agenda)
  1465. (org-let lprops '(org-agenda-list current-prefix-arg)))
  1466. ((eq type 'alltodo)
  1467. (org-let lprops '(org-todo-list current-prefix-arg)))
  1468. ((eq type 'search)
  1469. (org-let lprops '(org-search-view current-prefix-arg match nil)))
  1470. ((eq type 'stuck)
  1471. (org-let lprops '(org-agenda-list-stuck-projects
  1472. current-prefix-arg)))
  1473. ((eq type 'tags)
  1474. (org-let lprops '(org-tags-view current-prefix-arg match)))
  1475. ((eq type 'tags-todo)
  1476. (org-let lprops '(org-tags-view '(4) match)))
  1477. ((eq type 'todo)
  1478. (org-let lprops '(org-todo-list match)))
  1479. ((eq type 'tags-tree)
  1480. (org-check-for-org-mode)
  1481. (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
  1482. ((eq type 'todo-tree)
  1483. (org-check-for-org-mode)
  1484. (org-let lprops
  1485. '(org-occur (concat "^" outline-regexp "[ \t]*"
  1486. (regexp-quote match) "\\>"))))
  1487. ((eq type 'occur-tree)
  1488. (org-check-for-org-mode)
  1489. (org-let lprops '(org-occur match)))
  1490. ((functionp type)
  1491. (org-let lprops '(funcall type match)))
  1492. ((fboundp type)
  1493. (org-let lprops '(funcall type match)))
  1494. (t (error "Invalid custom agenda command type %s" type))))
  1495. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  1496. ((equal keys "C")
  1497. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  1498. (customize-variable 'org-agenda-custom-commands))
  1499. ((equal keys "a") (call-interactively 'org-agenda-list))
  1500. ((equal keys "s") (call-interactively 'org-search-view))
  1501. ((equal keys "t") (call-interactively 'org-todo-list))
  1502. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  1503. ((equal keys "m") (call-interactively 'org-tags-view))
  1504. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  1505. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  1506. ((equal keys "L")
  1507. (unless (org-mode-p)
  1508. (error "This is not an Org-mode file"))
  1509. (unless restriction
  1510. (put 'org-agenda-files 'org-restrict (list bfn))
  1511. (org-call-with-arg 'org-timeline arg)))
  1512. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  1513. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  1514. ((equal keys "!") (customize-variable 'org-stuck-projects))
  1515. (t (error "Invalid agenda key"))))))
  1516. (defun org-agenda-normalize-custom-commands (cmds)
  1517. (delq nil
  1518. (mapcar
  1519. (lambda (x)
  1520. (cond ((stringp (cdr x)) nil)
  1521. ((stringp (nth 1 x)) x)
  1522. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  1523. (t (cons (car x) (cons "" (cdr x))))))
  1524. cmds)))
  1525. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  1526. "The user interface for selecting an agenda command."
  1527. (catch 'exit
  1528. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  1529. (restrict-ok (and bfn (org-mode-p)))
  1530. (region-p (org-region-active-p))
  1531. (custom org-agenda-custom-commands)
  1532. (selstring "")
  1533. restriction second-time
  1534. c entry key type match prefixes rmheader header-end custom1 desc)
  1535. (save-window-excursion
  1536. (delete-other-windows)
  1537. (org-switch-to-buffer-other-window " *Agenda Commands*")
  1538. (erase-buffer)
  1539. (insert (eval-when-compile
  1540. (let ((header
  1541. "
  1542. Press key for an agenda command: < Buffer, subtree/region restriction
  1543. -------------------------------- > Remove restriction
  1544. a Agenda for current week or day e Export agenda views
  1545. t List of all TODO entries T Entries with special TODO kwd
  1546. m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
  1547. L Timeline for current buffer # List stuck projects (!=configure)
  1548. s Search for keywords C Configure custom agenda commands
  1549. / Multi-occur
  1550. ")
  1551. (start 0))
  1552. (while (string-match
  1553. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  1554. header start)
  1555. (setq start (match-end 0))
  1556. (add-text-properties (match-beginning 2) (match-end 2)
  1557. '(face bold) header))
  1558. header)))
  1559. (setq header-end (move-marker (make-marker) (point)))
  1560. (while t
  1561. (setq custom1 custom)
  1562. (when (eq rmheader t)
  1563. (goto-line 1)
  1564. (re-search-forward ":" nil t)
  1565. (delete-region (match-end 0) (point-at-eol))
  1566. (forward-char 1)
  1567. (looking-at "-+")
  1568. (delete-region (match-end 0) (point-at-eol))
  1569. (move-marker header-end (match-end 0)))
  1570. (goto-char header-end)
  1571. (delete-region (point) (point-max))
  1572. (while (setq entry (pop custom1))
  1573. (setq key (car entry) desc (nth 1 entry)
  1574. type (nth 2 entry) match (nth 3 entry))
  1575. (if (> (length key) 1)
  1576. (add-to-list 'prefixes (string-to-char key))
  1577. (insert
  1578. (format
  1579. "\n%-4s%-14s: %s"
  1580. (org-add-props (copy-sequence key)
  1581. '(face bold))
  1582. (cond
  1583. ((string-match "\\S-" desc) desc)
  1584. ((eq type 'agenda) "Agenda for current week or day")
  1585. ((eq type 'alltodo) "List of all TODO entries")
  1586. ((eq type 'search) "Word search")
  1587. ((eq type 'stuck) "List of stuck projects")
  1588. ((eq type 'todo) "TODO keyword")
  1589. ((eq type 'tags) "Tags query")
  1590. ((eq type 'tags-todo) "Tags (TODO)")
  1591. ((eq type 'tags-tree) "Tags tree")
  1592. ((eq type 'todo-tree) "TODO kwd tree")
  1593. ((eq type 'occur-tree) "Occur tree")
  1594. ((functionp type) (if (symbolp type)
  1595. (symbol-name type)
  1596. "Lambda expression"))
  1597. (t "???"))
  1598. (cond
  1599. ((stringp match)
  1600. (org-add-props match nil 'face 'org-warning))
  1601. (match
  1602. (format "set of %d commands" (length match)))
  1603. (t ""))))))
  1604. (when prefixes
  1605. (mapc (lambda (x)
  1606. (insert
  1607. (format "\n%s %s"
  1608. (org-add-props (char-to-string x)
  1609. nil 'face 'bold)
  1610. (or (cdr (assoc (concat selstring (char-to-string x))
  1611. prefix-descriptions))
  1612. "Prefix key"))))
  1613. prefixes))
  1614. (goto-char (point-min))
  1615. (if second-time
  1616. (if (not (pos-visible-in-window-p (point-max)))
  1617. (org-fit-window-to-buffer))
  1618. (setq second-time t)
  1619. (org-fit-window-to-buffer))
  1620. (message "Press key for agenda command%s:"
  1621. (if (or restrict-ok org-agenda-overriding-restriction)
  1622. (if org-agenda-overriding-restriction
  1623. " (restriction lock active)"
  1624. (if restriction
  1625. (format " (restricted to %s)" restriction)
  1626. " (unrestricted)"))
  1627. ""))
  1628. (setq c (read-char-exclusive))
  1629. (message "")
  1630. (cond
  1631. ((assoc (char-to-string c) custom)
  1632. (setq selstring (concat selstring (char-to-string c)))
  1633. (throw 'exit (cons selstring restriction)))
  1634. ((memq c prefixes)
  1635. (setq selstring (concat selstring (char-to-string c))
  1636. prefixes nil
  1637. rmheader (or rmheader t)
  1638. custom (delq nil (mapcar
  1639. (lambda (x)
  1640. (if (or (= (length (car x)) 1)
  1641. (/= (string-to-char (car x)) c))
  1642. nil
  1643. (cons (substring (car x) 1) (cdr x))))
  1644. custom))))
  1645. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  1646. (message "Restriction is only possible in Org-mode buffers")
  1647. (ding) (sit-for 1))
  1648. ((eq c ?1)
  1649. (org-agenda-remove-restriction-lock 'noupdate)
  1650. (setq restriction 'buffer))
  1651. ((eq c ?0)
  1652. (org-agenda-remove-restriction-lock 'noupdate)
  1653. (setq restriction (if region-p 'region 'subtree)))
  1654. ((eq c ?<)
  1655. (org-agenda-remove-restriction-lock 'noupdate)
  1656. (setq restriction
  1657. (cond
  1658. ((eq restriction 'buffer)
  1659. (if region-p 'region 'subtree))
  1660. ((memq restriction '(subtree region))
  1661. nil)
  1662. (t 'buffer))))
  1663. ((eq c ?>)
  1664. (org-agenda-remove-restriction-lock 'noupdate)
  1665. (setq restriction nil))
  1666. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  1667. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  1668. ((and (> (length selstring) 0) (eq c ?\d))
  1669. (delete-window)
  1670. (org-agenda-get-restriction-and-command prefix-descriptions))
  1671. ((equal c ?q) (error "Abort"))
  1672. (t (error "Invalid key %c" c))))))))
  1673. (defun org-run-agenda-series (name series)
  1674. (org-prepare-agenda name)
  1675. (let* ((org-agenda-multi t)
  1676. (redo (list 'org-run-agenda-series name (list 'quote series)))
  1677. (cmds (car series))
  1678. (gprops (nth 1 series))
  1679. match ;; The byte compiler incorrectly complains about this. Keep it!
  1680. cmd type lprops)
  1681. (while (setq cmd (pop cmds))
  1682. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  1683. (cond
  1684. ((eq type 'agenda)
  1685. (org-let2 gprops lprops
  1686. '(call-interactively 'org-agenda-list)))
  1687. ((eq type 'alltodo)
  1688. (org-let2 gprops lprops
  1689. '(call-interactively 'org-todo-list)))
  1690. ((eq type 'search)
  1691. (org-let2 gprops lprops
  1692. '(org-search-view current-prefix-arg match nil)))
  1693. ((eq type 'stuck)
  1694. (org-let2 gprops lprops
  1695. '(call-interactively 'org-agenda-list-stuck-projects)))
  1696. ((eq type 'tags)
  1697. (org-let2 gprops lprops
  1698. '(org-tags-view current-prefix-arg match)))
  1699. ((eq type 'tags-todo)
  1700. (org-let2 gprops lprops
  1701. '(org-tags-view '(4) match)))
  1702. ((eq type 'todo)
  1703. (org-let2 gprops lprops
  1704. '(org-todo-list match)))
  1705. ((fboundp type)
  1706. (org-let2 gprops lprops
  1707. '(funcall type match)))
  1708. (t (error "Invalid type in command series"))))
  1709. (widen)
  1710. (setq org-agenda-redo-command redo)
  1711. (goto-char (point-min)))
  1712. (org-fit-agenda-window)
  1713. (org-let (nth 1 series) '(org-finalize-agenda)))
  1714. ;;;###autoload
  1715. (defmacro org-batch-agenda (cmd-key &rest parameters)
  1716. "Run an agenda command in batch mode and send the result to STDOUT.
  1717. If CMD-KEY is a string of length 1, it is used as a key in
  1718. `org-agenda-custom-commands' and triggers this command. If it is a
  1719. longer string it is used as a tags/todo match string.
  1720. Paramters are alternating variable names and values that will be bound
  1721. before running the agenda command."
  1722. (let (pars)
  1723. (while parameters
  1724. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  1725. (if (> (length cmd-key) 2)
  1726. (eval (list 'let (nreverse pars)
  1727. (list 'org-tags-view nil cmd-key)))
  1728. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  1729. (set-buffer org-agenda-buffer-name)
  1730. (princ (org-encode-for-stdout (buffer-string)))))
  1731. (defun org-encode-for-stdout (string)
  1732. (if (fboundp 'encode-coding-string)
  1733. (encode-coding-string string buffer-file-coding-system)
  1734. string))
  1735. (defvar org-agenda-info nil)
  1736. ;;;###autoload
  1737. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  1738. "Run an agenda command in batch mode and send the result to STDOUT.
  1739. If CMD-KEY is a string of length 1, it is used as a key in
  1740. `org-agenda-custom-commands' and triggers this command. If it is a
  1741. longer string it is used as a tags/todo match string.
  1742. Paramters are alternating variable names and values that will be bound
  1743. before running the agenda command.
  1744. The output gives a line for each selected agenda item. Each
  1745. item is a list of comma-separated values, like this:
  1746. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  1747. category The category of the item
  1748. head The headline, without TODO kwd, TAGS and PRIORITY
  1749. type The type of the agenda entry, can be
  1750. todo selected in TODO match
  1751. tagsmatch selected in tags match
  1752. diary imported from diary
  1753. deadline a deadline on given date
  1754. scheduled scheduled on given date
  1755. timestamp entry has timestamp on given date
  1756. closed entry was closed on given date
  1757. upcoming-deadline warning about deadline
  1758. past-scheduled forwarded scheduled item
  1759. block entry has date block including g. date
  1760. todo The todo keyword, if any
  1761. tags All tags including inherited ones, separated by colons
  1762. date The relevant date, like 2007-2-14
  1763. time The time, like 15:00-16:50
  1764. extra Sting with extra planning info
  1765. priority-l The priority letter if any was given
  1766. priority-n The computed numerical priority
  1767. agenda-day The day in the agenda where this is listed"
  1768. (let (pars)
  1769. (while parameters
  1770. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  1771. (push (list 'org-agenda-remove-tags t) pars)
  1772. (if (> (length cmd-key) 2)
  1773. (eval (list 'let (nreverse pars)
  1774. (list 'org-tags-view nil cmd-key)))
  1775. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  1776. (set-buffer org-agenda-buffer-name)
  1777. (let* ((lines (org-split-string (buffer-string) "\n"))
  1778. line)
  1779. (while (setq line (pop lines))
  1780. (catch 'next
  1781. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  1782. (setq org-agenda-info
  1783. (org-fix-agenda-info (text-properties-at 0 line)))
  1784. (princ
  1785. (org-encode-for-stdout
  1786. (mapconcat 'org-agenda-export-csv-mapper
  1787. '(org-category txt type todo tags date time-of-day extra
  1788. priority-letter priority agenda-day)
  1789. ",")))
  1790. (princ "\n"))))))
  1791. (defun org-fix-agenda-info (props)
  1792. "Make sure all properties on an agenda item have a canonical form,
  1793. so the export commands can easily use it."
  1794. (let (tmp re)
  1795. (when (setq tmp (plist-get props 'tags))
  1796. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  1797. (when (setq tmp (plist-get props 'date))
  1798. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  1799. (let ((calendar-date-display-form '(year "-" month "-" day)))
  1800. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  1801. (setq tmp (calendar-date-string tmp)))
  1802. (setq props (plist-put props 'date tmp)))
  1803. (when (setq tmp (plist-get props 'day))
  1804. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  1805. (let ((calendar-date-display-form '(year "-" month "-" day)))
  1806. (setq tmp (calendar-date-string tmp)))
  1807. (setq props (plist-put props 'day tmp))
  1808. (setq props (plist-put props 'agenda-day tmp)))
  1809. (when (setq tmp (plist-get props 'txt))
  1810. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  1811. (plist-put props 'priority-letter (match-string 1 tmp))
  1812. (setq tmp (replace-match "" t t tmp)))
  1813. (when (and (setq re (plist-get props 'org-todo-regexp))
  1814. (setq re (concat "\\`\\.*" re " ?"))
  1815. (string-match re tmp))
  1816. (plist-put props 'todo (match-string 1 tmp))
  1817. (setq tmp (replace-match "" t t tmp)))
  1818. (plist-put props 'txt tmp)))
  1819. props)
  1820. (defun org-agenda-export-csv-mapper (prop)
  1821. (let ((res (plist-get org-agenda-info prop)))
  1822. (setq res
  1823. (cond
  1824. ((not res) "")
  1825. ((stringp res) res)
  1826. (t (prin1-to-string res))))
  1827. (while (string-match "," res)
  1828. (setq res (replace-match ";" t t res)))
  1829. (org-trim res)))
  1830. ;;;###autoload
  1831. (defun org-store-agenda-views (&rest parameters)
  1832. (interactive)
  1833. (eval (list 'org-batch-store-agenda-views)))
  1834. ;; FIXME, why is this a macro?????
  1835. ;;;###autoload
  1836. (defmacro org-batch-store-agenda-views (&rest parameters)
  1837. "Run all custom agenda commands that have a file argument."
  1838. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  1839. (pop-up-frames nil)
  1840. (dir default-directory)
  1841. pars cmd thiscmdkey files opts cmd-or-set)
  1842. (while parameters
  1843. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  1844. (setq pars (reverse pars))
  1845. (save-window-excursion
  1846. (while cmds
  1847. (setq cmd (pop cmds)
  1848. thiscmdkey (car cmd)
  1849. cmd-or-set (nth 2 cmd)
  1850. opts (nth (if (listp cmd-or-set) 3 4) cmd)
  1851. files (nth (if (listp cmd-or-set) 4 5) cmd))
  1852. (if (stringp files) (setq files (list files)))
  1853. (when files
  1854. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  1855. (list 'org-agenda nil thiscmdkey)))
  1856. (set-buffer org-agenda-buffer-name)
  1857. (while files
  1858. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  1859. (list 'org-write-agenda
  1860. (expand-file-name (pop files) dir) nil t))))
  1861. (and (get-buffer org-agenda-buffer-name)
  1862. (kill-buffer org-agenda-buffer-name)))))))
  1863. (defun org-write-agenda (file &optional open nosettings)
  1864. "Write the current buffer (an agenda view) as a file.
  1865. Depending on the extension of the file name, plain text (.txt),
  1866. HTML (.html or .htm) or Postscript (.ps) is produced.
  1867. If the extension is .ics, run icalendar export over all files used
  1868. to construct the agenda and limit the export to entries listed in the
  1869. agenda now.
  1870. With prefic argument OPEN, open the new file immediately.
  1871. If NOSETTINGS is given, do not scope the settings of
  1872. `org-agenda-exporter-settings' into the export commands. This is used when
  1873. the settings have already been scoped and we do not wish to overrule other,
  1874. higher priority settings."
  1875. (interactive "FWrite agenda to file: \nP")
  1876. (if (not (file-writable-p file))
  1877. (error "Cannot write agenda to file %s" file))
  1878. (cond
  1879. ((string-match "\\.html?\\'" file) (require 'htmlize))
  1880. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  1881. (org-let (if nosettings nil org-agenda-exporter-settings)
  1882. '(save-excursion
  1883. (save-window-excursion
  1884. (org-agenda-mark-filtered-text)
  1885. (let ((bs (copy-sequence (buffer-string))) beg)
  1886. (org-agenda-unmark-filtered-text)
  1887. (with-temp-buffer
  1888. (insert bs)
  1889. (org-agenda-remove-marked-text 'org-filtered)
  1890. (while (setq beg (text-property-any (point-min) (point-max)
  1891. 'org-filtered t))
  1892. (delete-region
  1893. beg (or (next-single-property-change beg 'org-filtered)
  1894. (point-max))))
  1895. (run-hooks 'org-agenda-before-write-hook)
  1896. (cond
  1897. ((string-match "\\.html?\\'" file)
  1898. (set-buffer (htmlize-buffer (current-buffer)))
  1899. (when (and org-agenda-export-html-style
  1900. (string-match "<style>" org-agenda-export-html-style))
  1901. ;; replace <style> section with org-agenda-export-html-style
  1902. (goto-char (point-min))
  1903. (kill-region (- (search-forward "<style") 6)
  1904. (search-forward "</style>"))
  1905. (insert org-agenda-export-html-style))
  1906. (write-file file)
  1907. (kill-buffer (current-buffer))
  1908. (message "HTML written to %s" file))
  1909. ((string-match "\\.ps\\'" file)
  1910. (require 'ps-print)
  1911. (flet ((ps-get-buffer-name () "Agenda View"))
  1912. (ps-print-buffer-with-faces file))
  1913. (message "Postscript written to %s" file))
  1914. ((string-match "\\.pdf\\'" file)
  1915. (require 'ps-print)
  1916. (flet ((ps-get-buffer-name () "Agenda View"))
  1917. (ps-print-buffer-with-faces
  1918. (concat (file-name-sans-extension file) ".ps")))
  1919. (call-process "ps2pdf" nil nil nil
  1920. (expand-file-name
  1921. (concat (file-name-sans-extension file) ".ps"))
  1922. (expand-file-name file))
  1923. (message "PDF written to %s" file))
  1924. ((string-match "\\.ics\\'" file)
  1925. (let ((org-agenda-marker-table
  1926. (org-create-marker-find-array
  1927. (org-agenda-collect-markers)))
  1928. (org-icalendar-verify-function 'org-check-agenda-marker-table)
  1929. (org-combined-agenda-icalendar-file file))
  1930. (apply 'org-export-icalendar 'combine
  1931. (org-agenda-files nil 'ifmode))))
  1932. (t
  1933. (let ((bs (buffer-string)))
  1934. (find-file file)
  1935. (erase-buffer)
  1936. (insert bs)
  1937. (save-buffer 0)
  1938. (kill-buffer (current-buffer))
  1939. (message "Plain text written to %s" file))))))))
  1940. (set-buffer org-agenda-buffer-name))
  1941. (when open (org-open-file file)))
  1942. (defvar org-agenda-filter-overlays nil)
  1943. (defun org-agenda-mark-filtered-text ()
  1944. "Mark all text hidden by filtering with a text property."
  1945. (let ((inhibit-read-only t))
  1946. (mapc
  1947. (lambda (o)
  1948. (when (equal (org-overlay-buffer o) (current-buffer))
  1949. (put-text-property
  1950. (org-overlay-start o) (org-overlay-end o)
  1951. 'org-filtered t)))
  1952. org-agenda-filter-overlays)))
  1953. (defun org-agenda-unmark-filtered-text ()
  1954. "Remove the filtering text property."
  1955. (let ((inhibit-read-only t))
  1956. (remove-text-properties (point-min) (point-max) '(org-filtered t))))
  1957. (defun org-agenda-remove-marked-text (property &optional value)
  1958. "Delete all text marked with VALUE of PROPERTY.
  1959. VALUE defaults to t."
  1960. (let (beg)
  1961. (setq value (or value t))
  1962. (while (setq beg (text-property-any (point-min) (point-max)
  1963. property value))
  1964. (delete-region
  1965. beg (or (next-single-property-change beg 'org-filtered)
  1966. (point-max))))))
  1967. (defun org-agenda-add-entry-text ()
  1968. "Add entry text to agenda lines.
  1969. This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
  1970. entry text following headings shown in the agenda.
  1971. Drawers will be excluded, also the line with scheduling/deadline info."
  1972. (when (> org-agenda-add-entry-text-maxlines 0)
  1973. (let (m txt drawer-re kwd-time-re ind)
  1974. (goto-char (point-min))
  1975. (while (not (eobp))
  1976. (if (not (setq m (get-text-property (point) 'org-hd-marker)))
  1977. (beginning-of-line 2)
  1978. (save-excursion
  1979. (with-current-buffer (marker-buffer m)
  1980. (if (not (org-mode-p))
  1981. (setq txt "")
  1982. (save-excursion
  1983. (save-restriction
  1984. (widen)
  1985. (goto-char m)
  1986. (beginning-of-line 2)
  1987. (setq txt (buffer-substring
  1988. (point)
  1989. (progn (outline-next-heading) (point)))
  1990. drawer-re org-drawer-regexp
  1991. kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
  1992. ".*\n?"))
  1993. (with-temp-buffer
  1994. (insert txt)
  1995. (when org-agenda-add-entry-text-descriptive-links
  1996. (goto-char (point-min))
  1997. (while (org-activate-bracket-links (point-max))
  1998. (add-text-properties (match-beginning 0) (match-end 0)
  1999. '(face org-link))))
  2000. (goto-char (point-min))
  2001. (while (re-search-forward org-bracket-link-regexp (point-max) t)
  2002. (set-text-properties (match-beginning 0) (match-end 0)
  2003. nil))
  2004. (goto-char (point-min))
  2005. (while (re-search-forward drawer-re nil t)
  2006. (delete-region
  2007. (match-beginning 0)
  2008. (progn (re-search-forward
  2009. "^[ \t]*:END:.*\n?" nil 'move)
  2010. (point))))
  2011. (goto-char (point-min))
  2012. (while (re-search-forward kwd-time-re nil t)
  2013. (replace-match ""))
  2014. (if (re-search-forward "[ \t\n]+\\'" nil t)
  2015. (replace-match ""))
  2016. (goto-char (point-min))
  2017. ;; find min indentation
  2018. (goto-char (point-min))
  2019. (untabify (point-min) (point-max))
  2020. (setq ind (org-get-indentation))
  2021. (while (not (eobp))
  2022. (unless (looking-at "[ \t]*$")
  2023. (setq ind (min ind (org-get-indentation))))
  2024. (beginning-of-line 2))
  2025. (goto-char (point-min))
  2026. (while (not (eobp))
  2027. (unless (looking-at "[ \t]*$")
  2028. (move-to-column ind)
  2029. (delete-region (point-at-bol) (point)))
  2030. (beginning-of-line 2))
  2031. (goto-char (point-min))
  2032. (while (and (not (eobp)) (re-search-forward "^" nil t))
  2033. (replace-match " > "))
  2034. (goto-char (point-min))
  2035. (while (looking-at "[ \t]*\n") (replace-match ""))
  2036. (goto-char (point-max))
  2037. (when (> (org-current-line)
  2038. (1+ org-agenda-add-entry-text-maxlines))
  2039. (goto-line (1+ org-agenda-add-entry-text-maxlines))
  2040. (backward-char 1))
  2041. (setq txt (buffer-substring (point-min) (point)))))))))
  2042. (end-of-line 1)
  2043. (if (string-match "\\S-" txt) (insert "\n" txt)))))))
  2044. (defun org-agenda-collect-markers ()
  2045. "Collect the markers pointing to entries in the agenda buffer."
  2046. (let (m markers)
  2047. (save-excursion
  2048. (goto-char (point-min))
  2049. (while (not (eobp))
  2050. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  2051. (get-text-property (point) 'org-marker)))
  2052. (push m markers))
  2053. (beginning-of-line 2)))
  2054. (nreverse markers)))
  2055. (defun org-create-marker-find-array (marker-list)
  2056. "Create a alist of files names with all marker positions in that file."
  2057. (let (f tbl m a p)
  2058. (while (setq m (pop marker-list))
  2059. (setq p (marker-position m)
  2060. f (buffer-file-name (or (buffer-base-buffer
  2061. (marker-buffer m))
  2062. (marker-buffer m))))
  2063. (if (setq a (assoc f tbl))
  2064. (push (marker-position m) (cdr a))
  2065. (push (list f p) tbl)))
  2066. (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
  2067. tbl)))
  2068. (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
  2069. (defun org-check-agenda-marker-table ()
  2070. "Check of the current entry is on the marker list."
  2071. (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  2072. a)
  2073. (and (setq a (assoc file org-agenda-marker-table))
  2074. (save-match-data
  2075. (save-excursion
  2076. (org-back-to-heading t)
  2077. (member (point) (cdr a)))))))
  2078. (defun org-check-for-org-mode ()
  2079. "Make sure current buffer is in org-mode. Error if not."
  2080. (or (org-mode-p)
  2081. (error "Cannot execute org-mode agenda command on buffer in %s."
  2082. major-mode)))
  2083. (defun org-fit-agenda-window ()
  2084. "Fit the window to the buffer size."
  2085. (and (memq org-agenda-window-setup '(reorganize-frame))
  2086. (fboundp 'fit-window-to-buffer)
  2087. (org-fit-window-to-buffer
  2088. nil
  2089. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  2090. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  2091. ;;; Agenda prepare and finalize
  2092. (defvar org-agenda-multi nil) ; dynamically scoped
  2093. (defvar org-agenda-buffer-name "*Org Agenda*")
  2094. (defvar org-pre-agenda-window-conf nil)
  2095. (defvar org-agenda-columns-active nil)
  2096. (defvar org-agenda-name nil)
  2097. (defvar org-agenda-filter nil)
  2098. (defvar org-agenda-filter-preset nil
  2099. "A preset of the tags filter used for secondary agenda filtering.
  2100. This must be a list of strings, each string must be a single tag preceeded
  2101. by \"+\" or \"-\".
  2102. This variable should not be set directly, but agenda custom commands can
  2103. bind it in the options section.")
  2104. (defun org-prepare-agenda (&optional name)
  2105. (setq org-todo-keywords-for-agenda nil)
  2106. (setq org-done-keywords-for-agenda nil)
  2107. (setq org-agenda-filter nil)
  2108. (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
  2109. (if org-agenda-multi
  2110. (progn
  2111. (setq buffer-read-only nil)
  2112. (goto-char (point-max))
  2113. (unless (or (bobp) org-agenda-compact-blocks)
  2114. (insert "\n"
  2115. (if (stringp org-agenda-block-separator)
  2116. org-agenda-block-separator
  2117. (make-string (window-width) org-agenda-block-separator))
  2118. "\n"))
  2119. (narrow-to-region (point) (point-max)))
  2120. (org-agenda-reset-markers)
  2121. (setq org-agenda-contributing-files nil)
  2122. (setq org-agenda-columns-active nil)
  2123. (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
  2124. (setq org-todo-keywords-for-agenda
  2125. (org-uniquify org-todo-keywords-for-agenda))
  2126. (setq org-done-keywords-for-agenda
  2127. (org-uniquify org-done-keywords-for-agenda))
  2128. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  2129. (awin (get-buffer-window abuf)))
  2130. (cond
  2131. ((equal (current-buffer) abuf) nil)
  2132. (awin (select-window awin))
  2133. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  2134. ((equal org-agenda-window-setup 'current-window)
  2135. (switch-to-buffer abuf))
  2136. ((equal org-agenda-window-setup 'other-window)
  2137. (org-switch-to-buffer-other-window abuf))
  2138. ((equal org-agenda-window-setup 'other-frame)
  2139. (switch-to-buffer-other-frame abuf))
  2140. ((equal org-agenda-window-setup 'reorganize-frame)
  2141. (delete-other-windows)
  2142. (org-switch-to-buffer-other-window abuf))))
  2143. (setq buffer-read-only nil)
  2144. (let ((inhibit-read-only t)) (erase-buffer))
  2145. (org-agenda-mode)
  2146. (and name (not org-agenda-name)
  2147. (org-set-local 'org-agenda-name name)))
  2148. (setq buffer-read-only nil))
  2149. (defun org-finalize-agenda ()
  2150. "Finishing touch for the agenda buffer, called just before displaying it."
  2151. (unless org-agenda-multi
  2152. (save-excursion
  2153. (let ((inhibit-read-only t))
  2154. (goto-char (point-min))
  2155. (while (org-activate-bracket-links (point-max))
  2156. (add-text-properties (match-beginning 0) (match-end 0)
  2157. '(face org-link)))
  2158. (org-agenda-align-tags)
  2159. (unless org-agenda-with-colors
  2160. (remove-text-properties (point-min) (point-max) '(face nil))))
  2161. (if (and (boundp 'org-agenda-overriding-columns-format)
  2162. org-agenda-overriding-columns-format)
  2163. (org-set-local 'org-agenda-overriding-columns-format
  2164. org-agenda-overriding-columns-format))
  2165. (if (and (boundp 'org-agenda-view-columns-initially)
  2166. org-agenda-view-columns-initially)
  2167. (org-agenda-columns))
  2168. (when org-agenda-fontify-priorities
  2169. (org-agenda-fontify-priorities))
  2170. (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
  2171. (org-agenda-dim-blocked-tasks))
  2172. (run-hooks 'org-finalize-agenda-hook)
  2173. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  2174. (when (get 'org-agenda-filter :preset-filter)
  2175. (org-agenda-filter-apply org-agenda-filter))
  2176. )))
  2177. (defun org-agenda-fontify-priorities ()
  2178. "Make highest priority lines bold, and lowest italic."
  2179. (interactive)
  2180. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  2181. (org-delete-overlay o)))
  2182. (org-overlays-in (point-min) (point-max)))
  2183. (save-excursion
  2184. (let ((inhibit-read-only t)
  2185. b e p ov h l)
  2186. (goto-char (point-min))
  2187. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  2188. (setq h (or (get-char-property (point) 'org-highest-priority)
  2189. org-highest-priority)
  2190. l (or (get-char-property (point) 'org-lowest-priority)
  2191. org-lowest-priority)
  2192. p (string-to-char (match-string 1))
  2193. b (match-beginning 0)
  2194. e (if (eq org-agenda-fontify-priorities 'cookies)
  2195. (match-end 0)
  2196. (point-at-eol))
  2197. ov (org-make-overlay b e))
  2198. (org-overlay-put
  2199. ov 'face
  2200. (cond ((cdr (assoc p org-priority-faces)))
  2201. ((and (listp org-agenda-fontify-priorities)
  2202. (cdr (assoc p org-agenda-fontify-priorities))))
  2203. ((equal p l) 'italic)
  2204. ((equal p h) 'bold)))
  2205. (org-overlay-put ov 'org-type 'org-priority)))))
  2206. (defun org-agenda-dim-blocked-tasks ()
  2207. "Dim currently blocked TODO's in the agenda display."
  2208. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
  2209. (org-delete-overlay o)))
  2210. (org-overlays-in (point-min) (point-max)))
  2211. (save-excursion
  2212. (let ((inhibit-read-only t)
  2213. (org-depend-tag-blocked nil)
  2214. (invis (eq org-agenda-dim-blocked-tasks 'invisible))
  2215. org-blocked-by-checkboxes
  2216. invis1 b e p ov h l)
  2217. (goto-char (point-min))
  2218. (while (let ((pos (next-single-property-change (point) 'todo-state)))
  2219. (and pos (goto-char (1+ pos))))
  2220. (setq org-blocked-by-checkboxes nil invis1 invis)
  2221. (let ((marker (get-text-property (point) 'org-hd-marker)))
  2222. (when (and marker
  2223. (not (with-current-buffer (marker-buffer marker)
  2224. (save-excursion
  2225. (goto-char marker)
  2226. (run-hook-with-args-until-failure
  2227. 'org-blocker-hook
  2228. (list :type 'todo-state-change
  2229. :position marker
  2230. :from 'todo
  2231. :to 'done))))))
  2232. (if org-blocked-by-checkboxes (setq invis1 nil))
  2233. (setq b (if invis1 (max (point-min) (1- (point))) (point))
  2234. e (point-at-eol)
  2235. ov (org-make-overlay b e))
  2236. (if invis1
  2237. (org-overlay-put ov 'invisible t)
  2238. (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
  2239. (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
  2240. (defvar org-agenda-skip-function nil
  2241. "Function to be called at each match during agenda construction.
  2242. If this function returns nil, the current match should not be skipped.
  2243. Otherwise, the function must return a position from where the search
  2244. should be continued.
  2245. This may also be a Lisp form, it will be evaluated.
  2246. Never set this variable using `setq' or so, because then it will apply
  2247. to all future agenda commands. Instead, bind it with `let' to scope
  2248. it dynamically into the agenda-constructing command. A good way to set
  2249. it is through options in org-agenda-custom-commands.")
  2250. (defun org-agenda-skip ()
  2251. "Throw to `:skip' in places that should be skipped.
  2252. Also moves point to the end of the skipped region, so that search can
  2253. continue from there."
  2254. (let ((p (point-at-bol)) to fp)
  2255. (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
  2256. (get-text-property p :org-archived)
  2257. (org-end-of-subtree t)
  2258. (throw :skip t))
  2259. (and org-agenda-skip-comment-trees
  2260. (get-text-property p :org-comment)
  2261. (org-end-of-subtree t)
  2262. (throw :skip t))
  2263. (if (equal (char-after p) ?#) (throw :skip t))
  2264. (when (and (or (setq fp (functionp org-agenda-skip-function))
  2265. (consp org-agenda-skip-function))
  2266. (setq to (save-excursion
  2267. (save-match-data
  2268. (if fp
  2269. (funcall org-agenda-skip-function)
  2270. (eval org-agenda-skip-function))))))
  2271. (goto-char to)
  2272. (throw :skip t))))
  2273. (defvar org-agenda-markers nil
  2274. "List of all currently active markers created by `org-agenda'.")
  2275. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  2276. "Creation time of the last agenda marker.")
  2277. (defun org-agenda-new-marker (&optional pos)
  2278. "Return a new agenda marker.
  2279. Org-mode keeps a list of these markers and resets them when they are
  2280. no longer in use."
  2281. (let ((m (copy-marker (or pos (point)))))
  2282. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  2283. (push m org-agenda-markers)
  2284. m))
  2285. (defun org-agenda-reset-markers ()
  2286. "Reset markers created by `org-agenda'."
  2287. (while org-agenda-markers
  2288. (move-marker (pop org-agenda-markers) nil)))
  2289. (defun org-agenda-save-markers-for-cut-and-paste (beg end)
  2290. "Save relative positions of markers in region."
  2291. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  2292. org-agenda-markers))
  2293. ;;; Agenda timeline
  2294. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  2295. (defun org-timeline (&optional include-all)
  2296. "Show a time-sorted view of the entries in the current org file.
  2297. Only entries with a time stamp of today or later will be listed. With
  2298. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  2299. under the current date.
  2300. If the buffer contains an active region, only check the region for
  2301. dates."
  2302. (interactive "P")
  2303. (require 'calendar)
  2304. (org-compile-prefix-format 'timeline)
  2305. (org-set-sorting-strategy 'timeline)
  2306. (let* ((dopast t)
  2307. (dotodo include-all)
  2308. (doclosed org-agenda-show-log)
  2309. (entry buffer-file-name)
  2310. (date (calendar-current-date))
  2311. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  2312. (end (if (org-region-active-p) (region-end) (point-max)))
  2313. (day-numbers (org-get-all-dates beg end 'no-ranges
  2314. t doclosed ; always include today
  2315. org-timeline-show-empty-dates))
  2316. (org-deadline-warning-days 0)
  2317. (org-agenda-only-exact-dates t)
  2318. (today (time-to-days (current-time)))
  2319. (past t)
  2320. args
  2321. s e rtn d emptyp wd)
  2322. (setq org-agenda-redo-command
  2323. (list 'progn
  2324. (list 'org-switch-to-buffer-other-window (current-buffer))
  2325. (list 'org-timeline (list 'quote include-all))))
  2326. (if (not dopast)
  2327. ;; Remove past dates from the list of dates.
  2328. (setq day-numbers (delq nil (mapcar (lambda(x)
  2329. (if (>= x today) x nil))
  2330. day-numbers))))
  2331. (org-prepare-agenda (concat "Timeline "
  2332. (file-name-nondirectory buffer-file-name)))
  2333. (if doclosed (push :closed args))
  2334. (push :timestamp args)
  2335. (push :deadline args)
  2336. (push :scheduled args)
  2337. (push :sexp args)
  2338. (if dotodo (push :todo args))
  2339. (while (setq d (pop day-numbers))
  2340. (if (and (listp d) (eq (car d) :omitted))
  2341. (progn
  2342. (setq s (point))
  2343. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  2344. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  2345. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  2346. (if (and (>= d today)
  2347. dopast
  2348. past)
  2349. (progn
  2350. (setq past nil)
  2351. (insert (make-string 79 ?-) "\n")))
  2352. (setq date (calendar-gregorian-from-absolute d)
  2353. wd (calendar-day-of-week date))
  2354. (setq s (point))
  2355. (setq rtn (and (not emptyp)
  2356. (apply 'org-agenda-get-day-entries entry
  2357. date args)))
  2358. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  2359. (progn
  2360. (insert
  2361. (if (stringp org-agenda-format-date)
  2362. (format-time-string org-agenda-format-date
  2363. (org-time-from-absolute date))
  2364. (funcall org-agenda-format-date date))
  2365. "\n")
  2366. (put-text-property s (1- (point)) 'face
  2367. (if (member wd org-agenda-weekend-days)
  2368. 'org-agenda-date-weekend
  2369. 'org-agenda-date))
  2370. (put-text-property s (1- (point)) 'org-date-line t)
  2371. (if (equal d today)
  2372. (put-text-property s (1- (point)) 'org-today t))
  2373. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  2374. (put-text-property s (1- (point)) 'day d)))))
  2375. (goto-char (point-min))
  2376. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  2377. (point-min)))
  2378. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  2379. (org-finalize-agenda)
  2380. (setq buffer-read-only t)))
  2381. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  2382. "Return a list of all relevant day numbers from BEG to END buffer positions.
  2383. If NO-RANGES is non-nil, include only the start and end dates of a range,
  2384. not every single day in the range. If FORCE-TODAY is non-nil, make
  2385. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  2386. inactive time stamps (those in square brackets) are included.
  2387. When EMPTY is non-nil, also include days without any entries."
  2388. (let ((re (concat
  2389. (if pre-re pre-re "")
  2390. (if inactive org-ts-regexp-both org-ts-regexp)))
  2391. dates dates1 date day day1 day2 ts1 ts2)
  2392. (if force-today
  2393. (setq dates (list (time-to-days (current-time)))))
  2394. (save-excursion
  2395. (goto-char beg)
  2396. (while (re-search-forward re end t)
  2397. (setq day (time-to-days (org-time-string-to-time
  2398. (substring (match-string 1) 0 10))))
  2399. (or (memq day dates) (push day dates)))
  2400. (unless no-ranges
  2401. (goto-char beg)
  2402. (while (re-search-forward org-tr-regexp end t)
  2403. (setq ts1 (substring (match-string 1) 0 10)
  2404. ts2 (substring (match-string 2) 0 10)
  2405. day1 (time-to-days (org-time-string-to-time ts1))
  2406. day2 (time-to-days (org-time-string-to-time ts2)))
  2407. (while (< (setq day1 (1+ day1)) day2)
  2408. (or (memq day1 dates) (push day1 dates)))))
  2409. (setq dates (sort dates '<))
  2410. (when empty
  2411. (while (setq day (pop dates))
  2412. (setq day2 (car dates))
  2413. (push day dates1)
  2414. (when (and day2 empty)
  2415. (if (or (eq empty t)
  2416. (and (numberp empty) (<= (- day2 day) empty)))
  2417. (while (< (setq day (1+ day)) day2)
  2418. (push (list day) dates1))
  2419. (push (cons :omitted (- day2 day)) dates1))))
  2420. (setq dates (nreverse dates1)))
  2421. dates)))
  2422. ;;; Agenda Daily/Weekly
  2423. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  2424. (defvar org-agenda-start-day nil ; dynamically scoped parameter
  2425. "Custom commands can set this variable in the options section.")
  2426. (defvar org-agenda-last-arguments nil
  2427. "The arguments of the previous call to org-agenda")
  2428. (defvar org-starting-day nil) ; local variable in the agenda buffer
  2429. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  2430. (defvar org-include-all-loc nil) ; local variable
  2431. (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
  2432. ;;;###autoload
  2433. (defun org-agenda-list (&optional include-all start-day ndays)
  2434. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  2435. The view will be for the current day or week, but from the overview buffer
  2436. you will be able to go to other days/weeks.
  2437. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  2438. all unfinished TODO items will also be shown, before the agenda.
  2439. This feature is considered obsolete, please use the TODO list or a block
  2440. agenda instead.
  2441. With a numeric prefix argument in an interactive call, the agenda will
  2442. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  2443. the number of days. NDAYS defaults to `org-agenda-ndays'.
  2444. START-DAY defaults to TODAY, or to the most recent match for the weekday
  2445. given in `org-agenda-start-on-weekday'."
  2446. (interactive "P")
  2447. (if (and (integerp include-all) (> include-all 0))
  2448. (setq ndays include-all include-all nil))
  2449. (setq ndays (or ndays org-agenda-ndays)
  2450. start-day (or start-day org-agenda-start-day))
  2451. (if org-agenda-overriding-arguments
  2452. (setq include-all (car org-agenda-overriding-arguments)
  2453. start-day (nth 1 org-agenda-overriding-arguments)
  2454. ndays (nth 2 org-agenda-overriding-arguments)))
  2455. (if (stringp start-day)
  2456. ;; Convert to an absolute day number
  2457. (setq start-day (time-to-days (org-read-date nil t start-day))))
  2458. (setq org-agenda-last-arguments (list include-all start-day ndays))
  2459. (org-compile-prefix-format 'agenda)
  2460. (org-set-sorting-strategy 'agenda)
  2461. (require 'calendar)
  2462. (let* ((org-agenda-start-on-weekday
  2463. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  2464. org-agenda-start-on-weekday nil))
  2465. (thefiles (org-agenda-files nil 'ifmode))
  2466. (files thefiles)
  2467. (today (time-to-days
  2468. (time-subtract (current-time)
  2469. (list 0 (* 3600 org-extend-today-until) 0))))
  2470. (sd (or start-day today))
  2471. (start (if (or (null org-agenda-start-on-weekday)
  2472. (< org-agenda-ndays 7))
  2473. sd
  2474. (let* ((nt (calendar-day-of-week
  2475. (calendar-gregorian-from-absolute sd)))
  2476. (n1 org-agenda-start-on-weekday)
  2477. (d (- nt n1)))
  2478. (- sd (+ (if (< d 0) 7 0) d)))))
  2479. (day-numbers (list start))
  2480. (day-cnt 0)
  2481. (inhibit-redisplay (not debug-on-error))
  2482. s e rtn rtnall file date d start-pos end-pos todayp nd wd
  2483. clocktable-start clocktable-end)
  2484. (setq org-agenda-redo-command
  2485. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  2486. ;; Make the list of days
  2487. (setq ndays (or ndays org-agenda-ndays)
  2488. nd ndays)
  2489. (while (> ndays 1)
  2490. (push (1+ (car day-numbers)) day-numbers)
  2491. (setq ndays (1- ndays)))
  2492. (setq day-numbers (nreverse day-numbers))
  2493. (setq clocktable-start (car day-numbers)
  2494. clocktable-end (1+ (or (org-last day-numbers) 0)))
  2495. (org-prepare-agenda "Day/Week")
  2496. (org-set-local 'org-starting-day (car day-numbers))
  2497. (org-set-local 'org-include-all-loc include-all)
  2498. (org-set-local 'org-agenda-span
  2499. (org-agenda-ndays-to-span nd))
  2500. (when (and (or include-all org-agenda-include-all-todo)
  2501. (member today day-numbers))
  2502. (setq files thefiles
  2503. rtnall nil)
  2504. (while (setq file (pop files))
  2505. (catch 'nextfile
  2506. (org-check-agenda-file file)
  2507. (setq date (calendar-gregorian-from-absolute today)
  2508. rtn (org-agenda-get-day-entries
  2509. file date :todo))
  2510. (setq rtnall (append rtnall rtn))))
  2511. (when rtnall
  2512. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  2513. (add-text-properties (point-min) (1- (point))
  2514. (list 'face 'org-agenda-structure))
  2515. (insert (org-finalize-agenda-entries rtnall) "\n")))
  2516. (unless org-agenda-compact-blocks
  2517. (let* ((d1 (car day-numbers))
  2518. (d2 (org-last day-numbers))
  2519. (w1 (org-days-to-iso-week d1))
  2520. (w2 (org-days-to-iso-week d2)))
  2521. (setq s (point))
  2522. (if org-agenda-overriding-header
  2523. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  2524. nil 'face 'org-agenda-structure) "\n")
  2525. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  2526. "-agenda"
  2527. (if (< (- d2 d1) 350)
  2528. (if (= w1 w2)
  2529. (format " (W%02d)" w1)
  2530. (format " (W%02d-W%02d)" w1 w2))
  2531. "")
  2532. ":\n")))
  2533. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  2534. 'org-date-line t)))
  2535. (while (setq d (pop day-numbers))
  2536. (setq date (calendar-gregorian-from-absolute d)
  2537. wd (calendar-day-of-week date)
  2538. s (point))
  2539. (if (or (setq todayp (= d today))
  2540. (and (not start-pos) (= d sd)))
  2541. (setq start-pos (point))
  2542. (if (and start-pos (not end-pos))
  2543. (setq end-pos (point))))
  2544. (setq files thefiles
  2545. rtnall nil)
  2546. (while (setq file (pop files))
  2547. (catch 'nextfile
  2548. (org-check-agenda-file file)
  2549. (cond
  2550. ((eq org-agenda-show-log 'only)
  2551. (setq rtn (org-agenda-get-day-entries
  2552. file date :closed)))
  2553. (org-agenda-show-log
  2554. (setq rtn (org-agenda-get-day-entries
  2555. file date
  2556. :deadline :scheduled :timestamp :sexp :closed)))
  2557. (t
  2558. (setq rtn (org-agenda-get-day-entries
  2559. file date
  2560. :deadline :scheduled :sexp :timestamp))))
  2561. (setq rtnall (append rtnall rtn))))
  2562. (if org-agenda-include-diary
  2563. (let ((org-agenda-search-headline-for-time t))
  2564. (require 'diary-lib)
  2565. (setq rtn (org-get-entries-from-diary date))
  2566. (setq rtnall (append rtnall rtn))))
  2567. (if (or rtnall org-agenda-show-all-dates)
  2568. (progn
  2569. (setq day-cnt (1+ day-cnt))
  2570. (insert
  2571. (if (stringp org-agenda-format-date)
  2572. (format-time-string org-agenda-format-date
  2573. (org-time-from-absolute date))
  2574. (funcall org-agenda-format-date date))
  2575. "\n")
  2576. (put-text-property s (1- (point)) 'face
  2577. (if (member wd org-agenda-weekend-days)
  2578. 'org-agenda-date-weekend
  2579. 'org-agenda-date))
  2580. (put-text-property s (1- (point)) 'org-date-line t)
  2581. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  2582. (if todayp (put-text-property s (1- (point)) 'org-today t))
  2583. (if rtnall (insert
  2584. (org-finalize-agenda-entries
  2585. (org-agenda-add-time-grid-maybe
  2586. rtnall nd todayp))
  2587. "\n"))
  2588. (put-text-property s (1- (point)) 'day d)
  2589. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  2590. (when (and org-agenda-clockreport-mode clocktable-start)
  2591. (let ((org-agenda-files (org-agenda-files nil 'ifmode))
  2592. ;; the above line is to ensure the restricted range!
  2593. (p org-agenda-clockreport-parameter-plist)
  2594. tbl)
  2595. (setq p (org-plist-delete p :block))
  2596. (setq p (plist-put p :tstart clocktable-start))
  2597. (setq p (plist-put p :tend clocktable-end))
  2598. (setq p (plist-put p :scope 'agenda))
  2599. (setq tbl (apply 'org-get-clocktable p))
  2600. (insert tbl)))
  2601. (goto-char (point-min))
  2602. (or org-agenda-multi (org-fit-agenda-window))
  2603. (unless (and (pos-visible-in-window-p (point-min))
  2604. (pos-visible-in-window-p (point-max)))
  2605. (goto-char (1- (point-max)))
  2606. (recenter -1)
  2607. (if (not (pos-visible-in-window-p (or start-pos 1)))
  2608. (progn
  2609. (goto-char (or start-pos 1))
  2610. (recenter 1))))
  2611. (goto-char (or start-pos 1))
  2612. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  2613. (org-finalize-agenda)
  2614. (setq buffer-read-only t)
  2615. (message "")))
  2616. (defun org-agenda-ndays-to-span (n)
  2617. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  2618. ;;; Agenda word search
  2619. (defvar org-agenda-search-history nil)
  2620. (defvar org-todo-only nil)
  2621. (defvar org-search-syntax-table nil
  2622. "Special syntax table for org-mode search.
  2623. In this table, we have single quotes not as word constituents, to
  2624. that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
  2625. (defun org-search-syntax-table ()
  2626. (unless org-search-syntax-table
  2627. (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
  2628. (modify-syntax-entry ?' "." org-search-syntax-table)
  2629. (modify-syntax-entry ?` "." org-search-syntax-table))
  2630. org-search-syntax-table)
  2631. ;;;###autoload
  2632. (defun org-search-view (&optional todo-only string edit-at)
  2633. "Show all entries that contain words or regular expressions.
  2634. If the first character of the search string is an asterisks,
  2635. search only the headlines.
  2636. With optional prefix argument TODO-ONLY, only consider entries that are
  2637. TODO entries. The argument STRING can be used to pass a default search
  2638. string into this function. If EDIT-AT is non-nil, it means that the
  2639. user should get a chance to edit this string, with cursor at position
  2640. EDIT-AT.
  2641. The search string is broken into \"words\" by splitting at whitespace.
  2642. The individual words are then interpreted as a boolean expression with
  2643. logical AND. Words prefixed with a minus must not occur in the entry.
  2644. Words without a prefix or prefixed with a plus must occur in the entry.
  2645. Matching is case-insensitive and the words are enclosed by word delimiters.
  2646. Words enclosed by curly braces are interpreted as regular expressions
  2647. that must or must not match in the entry.
  2648. If the search string starts with an asterisk, search only in headlines.
  2649. If (possibly after the leading star) the search string starts with an
  2650. exclamation mark, this also means to look at TODO entries only, an effect
  2651. that can also be achieved with a prefix argument.
  2652. This command searches the agenda files, and in addition the files listed
  2653. in `org-agenda-text-search-extra-files'."
  2654. (interactive "P")
  2655. (org-compile-prefix-format 'search)
  2656. (org-set-sorting-strategy 'search)
  2657. (org-prepare-agenda "SEARCH")
  2658. (let* ((props (list 'face nil
  2659. 'done-face 'org-agenda-done
  2660. 'org-not-done-regexp org-not-done-regexp
  2661. 'org-todo-regexp org-todo-regexp
  2662. 'org-complex-heading-regexp org-complex-heading-regexp
  2663. 'mouse-face 'highlight
  2664. 'keymap org-agenda-keymap
  2665. 'help-echo (format "mouse-2 or RET jump to location")))
  2666. regexp rtn rtnall files file pos
  2667. marker category tags c neg re
  2668. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  2669. (unless (and (not edit-at)
  2670. (stringp string)
  2671. (string-match "\\S-" string))
  2672. (setq string (read-string "[+-]Word/{Regexp} ...: "
  2673. (cond
  2674. ((integerp edit-at) (cons string edit-at))
  2675. (edit-at string))
  2676. 'org-agenda-search-history)))
  2677. (org-set-local 'org-todo-only todo-only)
  2678. (setq org-agenda-redo-command
  2679. (list 'org-search-view (if todo-only t nil) string
  2680. '(if current-prefix-arg 1 nil)))
  2681. (setq org-agenda-query-string string)
  2682. (if (equal (string-to-char string) ?*)
  2683. (setq hdl-only t
  2684. words (substring string 1))
  2685. (setq words string))
  2686. (when (equal (string-to-char words) ?!)
  2687. (setq todo-only t
  2688. words (substring words 1)))
  2689. (setq words (org-split-string words))
  2690. (mapc (lambda (w)
  2691. (setq c (string-to-char w))
  2692. (if (equal c ?-)
  2693. (setq neg t w (substring w 1))
  2694. (if (equal c ?+)
  2695. (setq neg nil w (substring w 1))
  2696. (setq neg nil)))
  2697. (if (string-match "\\`{.*}\\'" w)
  2698. (setq re (substring w 1 -1))
  2699. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  2700. (if neg (push re regexps-) (push re regexps+)))
  2701. words)
  2702. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  2703. (if (not regexps+)
  2704. (setq regexp (concat "^" org-outline-regexp))
  2705. (setq regexp (pop regexps+))
  2706. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  2707. regexp))))
  2708. (setq files (org-agenda-files nil 'ifmode))
  2709. (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
  2710. (pop org-agenda-text-search-extra-files)
  2711. (setq files (org-add-archive-files files)))
  2712. (setq files (append files org-agenda-text-search-extra-files)
  2713. rtnall nil)
  2714. (while (setq file (pop files))
  2715. (setq ee nil)
  2716. (catch 'nextfile
  2717. (org-check-agenda-file file)
  2718. (setq buffer (if (file-exists-p file)
  2719. (org-get-agenda-file-buffer file)
  2720. (error "No such file %s" file)))
  2721. (if (not buffer)
  2722. ;; If file does not exist, make sure an error message is sent
  2723. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  2724. file))))
  2725. (with-current-buffer buffer
  2726. (with-syntax-table (org-search-syntax-table)
  2727. (unless (org-mode-p)
  2728. (error "Agenda file %s is not in `org-mode'" file))
  2729. (let ((case-fold-search t))
  2730. (save-excursion
  2731. (save-restriction
  2732. (if org-agenda-restrict
  2733. (narrow-to-region org-agenda-restrict-begin
  2734. org-agenda-restrict-end)
  2735. (widen))
  2736. (goto-char (point-min))
  2737. (unless (or (org-on-heading-p)
  2738. (outline-next-heading))
  2739. (throw 'nextfile t))
  2740. (goto-char (max (point-min) (1- (point))))
  2741. (while (re-search-forward regexp nil t)
  2742. (org-back-to-heading t)
  2743. (skip-chars-forward "* ")
  2744. (setq beg (point-at-bol)
  2745. beg1 (point)
  2746. end (progn (outline-next-heading) (point)))
  2747. (catch :skip
  2748. (goto-char beg)
  2749. (org-agenda-skip)
  2750. (setq str (buffer-substring-no-properties
  2751. (point-at-bol)
  2752. (if hdl-only (point-at-eol) end)))
  2753. (mapc (lambda (wr) (when (string-match wr str)
  2754. (goto-char (1- end))
  2755. (throw :skip t)))
  2756. regexps-)
  2757. (mapc (lambda (wr) (unless (string-match wr str)
  2758. (goto-char (1- end))
  2759. (throw :skip t)))
  2760. (if todo-only
  2761. (cons (concat "^\*+[ \t]+" org-not-done-regexp)
  2762. regexps+)
  2763. regexps+))
  2764. (goto-char beg)
  2765. (setq marker (org-agenda-new-marker (point))
  2766. category (org-get-category)
  2767. tags (org-get-tags-at (point))
  2768. txt (org-format-agenda-item
  2769. ""
  2770. (buffer-substring-no-properties
  2771. beg1 (point-at-eol))
  2772. category tags))
  2773. (org-add-props txt props
  2774. 'org-marker marker 'org-hd-marker marker
  2775. 'org-todo-regexp org-todo-regexp
  2776. 'org-complex-heading-regexp org-complex-heading-regexp
  2777. 'priority 1000 'org-category category
  2778. 'type "search")
  2779. (push txt ee)
  2780. (goto-char (1- end))))))))))
  2781. (setq rtn (nreverse ee))
  2782. (setq rtnall (append rtnall rtn)))
  2783. (if org-agenda-overriding-header
  2784. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  2785. nil 'face 'org-agenda-structure) "\n")
  2786. (insert "Search words: ")
  2787. (add-text-properties (point-min) (1- (point))
  2788. (list 'face 'org-agenda-structure))
  2789. (setq pos (point))
  2790. (insert string "\n")
  2791. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  2792. (setq pos (point))
  2793. (unless org-agenda-multi
  2794. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  2795. (add-text-properties pos (1- (point))
  2796. (list 'face 'org-agenda-structure))))
  2797. (when rtnall
  2798. (insert (org-finalize-agenda-entries rtnall) "\n"))
  2799. (goto-char (point-min))
  2800. (or org-agenda-multi (org-fit-agenda-window))
  2801. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  2802. (org-finalize-agenda)
  2803. (setq buffer-read-only t)))
  2804. ;;; Agenda TODO list
  2805. (defvar org-select-this-todo-keyword nil)
  2806. (defvar org-last-arg nil)
  2807. ;;;###autoload
  2808. (defun org-todo-list (arg)
  2809. "Show all TODO entries from all agenda file in a single list.
  2810. The prefix arg can be used to select a specific TODO keyword and limit
  2811. the list to these. When using \\[universal-argument], you will be prompted
  2812. for a keyword. A numeric prefix directly selects the Nth keyword in
  2813. `org-todo-keywords-1'."
  2814. (interactive "P")
  2815. (require 'calendar)
  2816. (org-compile-prefix-format 'todo)
  2817. (org-set-sorting-strategy 'todo)
  2818. (org-prepare-agenda "TODO")
  2819. (let* ((today (time-to-days (current-time)))
  2820. (date (calendar-gregorian-from-absolute today))
  2821. (kwds org-todo-keywords-for-agenda)
  2822. (completion-ignore-case t)
  2823. (org-select-this-todo-keyword
  2824. (if (stringp arg) arg
  2825. (and arg (integerp arg) (> arg 0)
  2826. (nth (1- arg) kwds))))
  2827. rtn rtnall files file pos)
  2828. (when (equal arg '(4))
  2829. (setq org-select-this-todo-keyword
  2830. (org-ido-completing-read "Keyword (or KWD1|K2D2|...): "
  2831. (mapcar 'list kwds) nil nil)))
  2832. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  2833. (org-set-local 'org-last-arg arg)
  2834. (setq org-agenda-redo-command
  2835. '(org-todo-list (or current-prefix-arg org-last-arg)))
  2836. (setq files (org-agenda-files nil 'ifmode)
  2837. rtnall nil)
  2838. (while (setq file (pop files))
  2839. (catch 'nextfile
  2840. (org-check-agenda-file file)
  2841. (setq rtn (org-agenda-get-day-entries file date :todo))
  2842. (setq rtnall (append rtnall rtn))))
  2843. (if org-agenda-overriding-header
  2844. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  2845. nil 'face 'org-agenda-structure) "\n")
  2846. (insert "Global list of TODO items of type: ")
  2847. (add-text-properties (point-min) (1- (point))
  2848. (list 'face 'org-agenda-structure))
  2849. (setq pos (point))
  2850. (insert (or org-select-this-todo-keyword "ALL") "\n")
  2851. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  2852. (setq pos (point))
  2853. (unless org-agenda-multi
  2854. (insert "Available with `N r': (0)ALL")
  2855. (let ((n 0) s)
  2856. (mapc (lambda (x)
  2857. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  2858. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  2859. (insert "\n "))
  2860. (insert " " s))
  2861. kwds))
  2862. (insert "\n"))
  2863. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  2864. (when rtnall
  2865. (insert (org-finalize-agenda-entries rtnall) "\n"))
  2866. (goto-char (point-min))
  2867. (or org-agenda-multi (org-fit-agenda-window))
  2868. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  2869. (org-finalize-agenda)
  2870. (setq buffer-read-only t)))
  2871. ;;; Agenda tags match
  2872. ;;;###autoload
  2873. (defun org-tags-view (&optional todo-only match)
  2874. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  2875. The prefix arg TODO-ONLY limits the search to TODO entries."
  2876. (interactive "P")
  2877. (org-compile-prefix-format 'tags)
  2878. (org-set-sorting-strategy 'tags)
  2879. (let* ((org-tags-match-list-sublevels
  2880. (if todo-only t org-tags-match-list-sublevels))
  2881. (completion-ignore-case t)
  2882. rtn rtnall files file pos matcher
  2883. buffer)
  2884. (setq matcher (org-make-tags-matcher match)
  2885. match (car matcher) matcher (cdr matcher))
  2886. (org-prepare-agenda (concat "TAGS " match))
  2887. (setq org-agenda-query-string match)
  2888. (setq org-agenda-redo-command
  2889. (list 'org-tags-view (list 'quote todo-only)
  2890. (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
  2891. (setq files (org-agenda-files nil 'ifmode)
  2892. rtnall nil)
  2893. (while (setq file (pop files))
  2894. (catch 'nextfile
  2895. (org-check-agenda-file file)
  2896. (setq buffer (if (file-exists-p file)
  2897. (org-get-agenda-file-buffer file)
  2898. (error "No such file %s" file)))
  2899. (if (not buffer)
  2900. ;; If file does not exist, error message to agenda
  2901. (setq rtn (list
  2902. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  2903. rtnall (append rtnall rtn))
  2904. (with-current-buffer buffer
  2905. (unless (org-mode-p)
  2906. (error "Agenda file %s is not in `org-mode'" file))
  2907. (save-excursion
  2908. (save-restriction
  2909. (if org-agenda-restrict
  2910. (narrow-to-region org-agenda-restrict-begin
  2911. org-agenda-restrict-end)
  2912. (widen))
  2913. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  2914. (setq rtnall (append rtnall rtn))))))))
  2915. (if org-agenda-overriding-header
  2916. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  2917. nil 'face 'org-agenda-structure) "\n")
  2918. (insert "Headlines with TAGS match: ")
  2919. (add-text-properties (point-min) (1- (point))
  2920. (list 'face 'org-agenda-structure))
  2921. (setq pos (point))
  2922. (insert match "\n")
  2923. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  2924. (setq pos (point))
  2925. (unless org-agenda-multi
  2926. (insert "Press `C-u r' to search again with new search string\n"))
  2927. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  2928. (when rtnall
  2929. (insert (org-finalize-agenda-entries rtnall) "\n"))
  2930. (goto-char (point-min))
  2931. (or org-agenda-multi (org-fit-agenda-window))
  2932. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  2933. (org-finalize-agenda)
  2934. (setq buffer-read-only t)))
  2935. ;;; Agenda Finding stuck projects
  2936. (defvar org-agenda-skip-regexp nil
  2937. "Regular expression used in skipping subtrees for the agenda.
  2938. This is basically a temporary global variable that can be set and then
  2939. used by user-defined selections using `org-agenda-skip-function'.")
  2940. (defvar org-agenda-overriding-header nil
  2941. "When this is set during todo and tags searches, will replace header.
  2942. This variable should not be set directly, but custom commands can bind it
  2943. in the options section.")
  2944. (defun org-agenda-skip-entry-when-regexp-matches ()
  2945. "Checks if the current entry contains match for `org-agenda-skip-regexp'.
  2946. If yes, it returns the end position of this entry, causing agenda commands
  2947. to skip the entry but continuing the search in the subtree. This is a
  2948. function that can be put into `org-agenda-skip-function' for the duration
  2949. of a command."
  2950. (let ((end (save-excursion (org-end-of-subtree t)))
  2951. skip)
  2952. (save-excursion
  2953. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  2954. (and skip end)))
  2955. (defun org-agenda-skip-subtree-when-regexp-matches ()
  2956. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  2957. If yes, it returns the end position of this tree, causing agenda commands
  2958. to skip this subtree. This is a function that can be put into
  2959. `org-agenda-skip-function' for the duration of a command."
  2960. (let ((end (save-excursion (org-end-of-subtree t)))
  2961. skip)
  2962. (save-excursion
  2963. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  2964. (and skip end)))
  2965. (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
  2966. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  2967. If yes, it returns the end position of the current entry (NOT the tree),
  2968. causing agenda commands to skip the entry but continuing the search in
  2969. the subtree. This is a function that can be put into
  2970. `org-agenda-skip-function' for the duration of a command. An important
  2971. use of this function is for the stuck project list."
  2972. (let ((end (save-excursion (org-end-of-subtree t)))
  2973. (entry-end (save-excursion (outline-next-heading) (1- (point))))
  2974. skip)
  2975. (save-excursion
  2976. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  2977. (and skip entry-end)))
  2978. (defun org-agenda-skip-entry-if (&rest conditions)
  2979. "Skip entry if any of CONDITIONS is true.
  2980. See `org-agenda-skip-if' for details."
  2981. (org-agenda-skip-if nil conditions))
  2982. (defun org-agenda-skip-subtree-if (&rest conditions)
  2983. "Skip entry if any of CONDITIONS is true.
  2984. See `org-agenda-skip-if' for details."
  2985. (org-agenda-skip-if t conditions))
  2986. (defun org-agenda-skip-if (subtree conditions)
  2987. "Checks current entity for CONDITIONS.
  2988. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  2989. the entry, i.e. the text before the next heading is checked.
  2990. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  2991. from different tests. Valid conditions are:
  2992. scheduled Check if there is a scheduled cookie
  2993. notscheduled Check if there is no scheduled cookie
  2994. deadline Check if there is a deadline
  2995. notdeadline Check if there is no deadline
  2996. timestamp Check if there is a timestamp (also deadline or scheduled)
  2997. nottimestamp Check if there is no timestamp (also deadline or scheduled)
  2998. regexp Check if regexp matches
  2999. notregexp Check if regexp does not match.
  3000. The regexp is taken from the conditions list, it must come right after
  3001. the `regexp' or `notregexp' element.
  3002. If any of these conditions is met, this function returns the end point of
  3003. the entity, causing the search to continue from there. This is a function
  3004. that can be put into `org-agenda-skip-function' for the duration of a command."
  3005. (let (beg end m)
  3006. (org-back-to-heading t)
  3007. (setq beg (point)
  3008. end (if subtree
  3009. (progn (org-end-of-subtree t) (point))
  3010. (progn (outline-next-heading) (1- (point)))))
  3011. (goto-char beg)
  3012. (and
  3013. (or
  3014. (and (memq 'scheduled conditions)
  3015. (re-search-forward org-scheduled-time-regexp end t))
  3016. (and (memq 'notscheduled conditions)
  3017. (not (re-search-forward org-scheduled-time-regexp end t)))
  3018. (and (memq 'deadline conditions)
  3019. (re-search-forward org-deadline-time-regexp end t))
  3020. (and (memq 'notdeadline conditions)
  3021. (not (re-search-forward org-deadline-time-regexp end t)))
  3022. (and (memq 'timestamp conditions)
  3023. (re-search-forward org-ts-regexp end t))
  3024. (and (memq 'nottimestamp conditions)
  3025. (not (re-search-forward org-ts-regexp end t)))
  3026. (and (setq m (memq 'regexp conditions))
  3027. (stringp (nth 1 m))
  3028. (re-search-forward (nth 1 m) end t))
  3029. (and (setq m (memq 'notregexp conditions))
  3030. (stringp (nth 1 m))
  3031. (not (re-search-forward (nth 1 m) end t))))
  3032. end)))
  3033. ;;;###autoload
  3034. (defun org-agenda-list-stuck-projects (&rest ignore)
  3035. "Create agenda view for projects that are stuck.
  3036. Stuck projects are project that have no next actions. For the definitions
  3037. of what a project is and how to check if it stuck, customize the variable
  3038. `org-stuck-projects'.
  3039. MATCH is being ignored."
  3040. (interactive)
  3041. (let* ((org-agenda-skip-function
  3042. 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
  3043. ;; We could have used org-agenda-skip-if here.
  3044. (org-agenda-overriding-header "List of stuck projects: ")
  3045. (matcher (nth 0 org-stuck-projects))
  3046. (todo (nth 1 org-stuck-projects))
  3047. (todo-wds (if (member "*" todo)
  3048. (progn
  3049. (org-prepare-agenda-buffers (org-agenda-files
  3050. nil 'ifmode))
  3051. (org-delete-all
  3052. org-done-keywords-for-agenda
  3053. (copy-sequence org-todo-keywords-for-agenda)))
  3054. todo))
  3055. (todo-re (concat "^\\*+[ \t]+\\("
  3056. (mapconcat 'identity todo-wds "\\|")
  3057. "\\)\\>"))
  3058. (tags (nth 2 org-stuck-projects))
  3059. (tags-re (if (member "*" tags)
  3060. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  3061. (if tags
  3062. (concat "^\\*+ .*:\\("
  3063. (mapconcat 'identity tags "\\|")
  3064. (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
  3065. (gen-re (nth 3 org-stuck-projects))
  3066. (re-list
  3067. (delq nil
  3068. (list
  3069. (if todo todo-re)
  3070. (if tags tags-re)
  3071. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  3072. gen-re)))))
  3073. (setq org-agenda-skip-regexp
  3074. (if re-list
  3075. (mapconcat 'identity re-list "\\|")
  3076. (error "No information how to identify unstuck projects")))
  3077. (org-tags-view nil matcher)
  3078. (with-current-buffer org-agenda-buffer-name
  3079. (setq org-agenda-redo-command
  3080. '(org-agenda-list-stuck-projects
  3081. (or current-prefix-arg org-last-arg))))))
  3082. ;;; Diary integration
  3083. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  3084. (defvar list-diary-entries-hook)
  3085. (defun org-get-entries-from-diary (date)
  3086. "Get the (Emacs Calendar) diary entries for DATE."
  3087. (require 'diary-lib)
  3088. (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
  3089. (fancy-diary-buffer diary-fancy-buffer)
  3090. (diary-display-hook '(fancy-diary-display))
  3091. (diary-display-function 'fancy-diary-display)
  3092. (pop-up-frames nil)
  3093. (list-diary-entries-hook
  3094. (cons 'org-diary-default-entry list-diary-entries-hook))
  3095. (diary-file-name-prefix-function nil) ; turn this feature off
  3096. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  3097. entries
  3098. (org-disable-agenda-to-diary t))
  3099. (save-excursion
  3100. (save-window-excursion
  3101. (funcall (if (fboundp 'diary-list-entries)
  3102. 'diary-list-entries 'list-diary-entries)
  3103. date 1)))
  3104. (if (not (get-buffer diary-fancy-buffer))
  3105. (setq entries nil)
  3106. (with-current-buffer diary-fancy-buffer
  3107. (setq buffer-read-only nil)
  3108. (if (zerop (buffer-size))
  3109. ;; No entries
  3110. (setq entries nil)
  3111. ;; Omit the date and other unnecessary stuff
  3112. (org-agenda-cleanup-fancy-diary)
  3113. ;; Add prefix to each line and extend the text properties
  3114. (if (zerop (buffer-size))
  3115. (setq entries nil)
  3116. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  3117. (set-buffer-modified-p nil)
  3118. (kill-buffer diary-fancy-buffer)))
  3119. (when entries
  3120. (setq entries (org-split-string entries "\n"))
  3121. (setq entries
  3122. (mapcar
  3123. (lambda (x)
  3124. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  3125. ;; Extend the text properties to the beginning of the line
  3126. (org-add-props x (text-properties-at (1- (length x)) x)
  3127. 'type "diary" 'date date))
  3128. entries)))))
  3129. (defun org-agenda-cleanup-fancy-diary ()
  3130. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  3131. This gets rid of the date, the underline under the date, and
  3132. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  3133. date. It also removes lines that contain only whitespace."
  3134. (goto-char (point-min))
  3135. (if (looking-at ".*?:[ \t]*")
  3136. (progn
  3137. (replace-match "")
  3138. (re-search-forward "\n=+$" nil t)
  3139. (replace-match "")
  3140. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  3141. (re-search-forward "\n=+$" nil t)
  3142. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  3143. (goto-char (point-min))
  3144. (while (re-search-forward "^ +\n" nil t)
  3145. (replace-match ""))
  3146. (goto-char (point-min))
  3147. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  3148. (replace-match "")))
  3149. ;; Make sure entries from the diary have the right text properties.
  3150. (eval-after-load "diary-lib"
  3151. '(if (boundp 'diary-modify-entry-list-string-function)
  3152. ;; We can rely on the hook, nothing to do
  3153. nil
  3154. ;; Hook not available, must use advice to make this work
  3155. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  3156. "Make the position visible."
  3157. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  3158. (stringp string)
  3159. buffer-file-name)
  3160. (setq string (org-modify-diary-entry-string string))))))
  3161. (defun org-modify-diary-entry-string (string)
  3162. "Add text properties to string, allowing org-mode to act on it."
  3163. (org-add-props string nil
  3164. 'mouse-face 'highlight
  3165. 'keymap org-agenda-keymap
  3166. 'help-echo (if buffer-file-name
  3167. (format "mouse-2 or RET jump to diary file %s"
  3168. (abbreviate-file-name buffer-file-name))
  3169. "")
  3170. 'org-agenda-diary-link t
  3171. 'org-marker (org-agenda-new-marker (point-at-bol))))
  3172. (defun org-diary-default-entry ()
  3173. "Add a dummy entry to the diary.
  3174. Needed to avoid empty dates which mess up holiday display."
  3175. ;; Catch the error if dealing with the new add-to-diary-alist
  3176. (when org-disable-agenda-to-diary
  3177. (condition-case nil
  3178. (org-add-to-diary-list original-date "Org-mode dummy" "")
  3179. (error
  3180. (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  3181. (defun org-add-to-diary-list (&rest args)
  3182. (if (fboundp 'diary-add-to-list)
  3183. (apply 'diary-add-to-list args)
  3184. (apply 'add-to-diary-list args)))
  3185. ;;;###autoload
  3186. (defun org-diary (&rest args)
  3187. "Return diary information from org-files.
  3188. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  3189. It accesses org files and extracts information from those files to be
  3190. listed in the diary. The function accepts arguments specifying what
  3191. items should be listed. The following arguments are allowed:
  3192. :timestamp List the headlines of items containing a date stamp or
  3193. date range matching the selected date. Deadlines will
  3194. also be listed, on the expiration day.
  3195. :sexp List entries resulting from diary-like sexps.
  3196. :deadline List any deadlines past due, or due within
  3197. `org-deadline-warning-days'. The listing occurs only
  3198. in the diary for *today*, not at any other date. If
  3199. an entry is marked DONE, it is no longer listed.
  3200. :scheduled List all items which are scheduled for the given date.
  3201. The diary for *today* also contains items which were
  3202. scheduled earlier and are not yet marked DONE.
  3203. :todo List all TODO items from the org-file. This may be a
  3204. long list - so this is not turned on by default.
  3205. Like deadlines, these entries only show up in the
  3206. diary for *today*, not at any other date.
  3207. The call in the diary file should look like this:
  3208. &%%(org-diary) ~/path/to/some/orgfile.org
  3209. Use a separate line for each org file to check. Or, if you omit the file name,
  3210. all files listed in `org-agenda-files' will be checked automatically:
  3211. &%%(org-diary)
  3212. If you don't give any arguments (as in the example above), the default
  3213. arguments (:deadline :scheduled :timestamp :sexp) are used.
  3214. So the example above may also be written as
  3215. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  3216. The function expects the lisp variables `entry' and `date' to be provided
  3217. by the caller, because this is how the calendar works. Don't use this
  3218. function from a program - use `org-agenda-get-day-entries' instead."
  3219. (when (> (- (time-to-seconds (current-time))
  3220. org-agenda-last-marker-time)
  3221. 5)
  3222. (org-agenda-reset-markers))
  3223. (org-compile-prefix-format 'agenda)
  3224. (org-set-sorting-strategy 'agenda)
  3225. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  3226. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  3227. (list entry)
  3228. (org-agenda-files t)))
  3229. file rtn results)
  3230. (org-prepare-agenda-buffers files)
  3231. ;; If this is called during org-agenda, don't return any entries to
  3232. ;; the calendar. Org Agenda will list these entries itself.
  3233. (if org-disable-agenda-to-diary (setq files nil))
  3234. (while (setq file (pop files))
  3235. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  3236. (setq results (append results rtn)))
  3237. (if results
  3238. (concat (org-finalize-agenda-entries results) "\n"))))
  3239. ;;; Agenda entry finders
  3240. (defun org-agenda-get-day-entries (file date &rest args)
  3241. "Does the work for `org-diary' and `org-agenda'.
  3242. FILE is the path to a file to be checked for entries. DATE is date like
  3243. the one returned by `calendar-current-date'. ARGS are symbols indicating
  3244. which kind of entries should be extracted. For details about these, see
  3245. the documentation of `org-diary'."
  3246. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  3247. (let* ((org-startup-folded nil)
  3248. (org-startup-align-all-tables nil)
  3249. (buffer (if (file-exists-p file)
  3250. (org-get-agenda-file-buffer file)
  3251. (error "No such file %s" file)))
  3252. arg results rtn)
  3253. (if (not buffer)
  3254. ;; If file does not exist, make sure an error message ends up in diary
  3255. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  3256. (with-current-buffer buffer
  3257. (unless (org-mode-p)
  3258. (error "Agenda file %s is not in `org-mode'" file))
  3259. (let ((case-fold-search nil))
  3260. (save-excursion
  3261. (save-restriction
  3262. (if org-agenda-restrict
  3263. (narrow-to-region org-agenda-restrict-begin
  3264. org-agenda-restrict-end)
  3265. (widen))
  3266. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  3267. (while (setq arg (pop args))
  3268. (cond
  3269. ((and (eq arg :todo)
  3270. (equal date (calendar-current-date)))
  3271. (setq rtn (org-agenda-get-todos))
  3272. (setq results (append results rtn)))
  3273. ((eq arg :timestamp)
  3274. (setq rtn (org-agenda-get-blocks))
  3275. (setq results (append results rtn))
  3276. (setq rtn (org-agenda-get-timestamps))
  3277. (setq results (append results rtn)))
  3278. ((eq arg :sexp)
  3279. (setq rtn (org-agenda-get-sexps))
  3280. (setq results (append results rtn)))
  3281. ((eq arg :scheduled)
  3282. (setq rtn (org-agenda-get-scheduled))
  3283. (setq results (append results rtn)))
  3284. ((eq arg :closed)
  3285. (setq rtn (org-agenda-get-progress))
  3286. (setq results (append results rtn)))
  3287. ((eq arg :deadline)
  3288. (setq rtn (org-agenda-get-deadlines))
  3289. (setq results (append results rtn))))))))
  3290. results))))
  3291. (defun org-agenda-get-todos ()
  3292. "Return the TODO information for agenda display."
  3293. (let* ((props (list 'face nil
  3294. 'done-face 'org-agenda-done
  3295. 'org-not-done-regexp org-not-done-regexp
  3296. 'org-todo-regexp org-todo-regexp
  3297. 'org-complex-heading-regexp org-complex-heading-regexp
  3298. 'mouse-face 'highlight
  3299. 'keymap org-agenda-keymap
  3300. 'help-echo
  3301. (format "mouse-2 or RET jump to org file %s"
  3302. (abbreviate-file-name buffer-file-name))))
  3303. (regexp (concat "^\\*+[ \t]+\\("
  3304. (if org-select-this-todo-keyword
  3305. (if (equal org-select-this-todo-keyword "*")
  3306. org-todo-regexp
  3307. (concat "\\<\\("
  3308. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  3309. "\\)\\>"))
  3310. org-not-done-regexp)
  3311. "[^\n\r]*\\)"))
  3312. marker priority category tags todo-state
  3313. ee txt beg end)
  3314. (goto-char (point-min))
  3315. (while (re-search-forward regexp nil t)
  3316. (catch :skip
  3317. (save-match-data
  3318. (beginning-of-line)
  3319. (setq beg (point) end (save-excursion (outline-next-heading) (point)))
  3320. (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
  3321. (goto-char (1+ beg))
  3322. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  3323. (throw :skip nil)))
  3324. (goto-char beg)
  3325. (org-agenda-skip)
  3326. (goto-char (match-beginning 1))
  3327. (setq marker (org-agenda-new-marker (match-beginning 0))
  3328. category (org-get-category)
  3329. txt (match-string 1)
  3330. tags (org-get-tags-at (point))
  3331. txt (org-format-agenda-item "" txt category tags)
  3332. priority (1+ (org-get-priority txt))
  3333. todo-state (org-get-todo-state))
  3334. (org-add-props txt props
  3335. 'org-marker marker 'org-hd-marker marker
  3336. 'priority priority 'org-category category
  3337. 'type "todo" 'todo-state todo-state)
  3338. (push txt ee)
  3339. (if org-agenda-todo-list-sublevels
  3340. (goto-char (match-end 1))
  3341. (org-end-of-subtree 'invisible))))
  3342. (nreverse ee)))
  3343. ;;;###autoload
  3344. (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
  3345. "Do we have a reason to ignore this todo entry because it has a time stamp?"
  3346. (when (or org-agenda-todo-ignore-with-date
  3347. org-agenda-todo-ignore-scheduled
  3348. org-agenda-todo-ignore-deadlines)
  3349. (setq end (or end (save-excursion (outline-next-heading) (point))))
  3350. (save-excursion
  3351. (or (and org-agenda-todo-ignore-with-date
  3352. (re-search-forward org-ts-regexp end t))
  3353. (and org-agenda-todo-ignore-scheduled
  3354. (re-search-forward org-scheduled-time-regexp end t))
  3355. (and org-agenda-todo-ignore-deadlines
  3356. (re-search-forward org-deadline-time-regexp end t)
  3357. (org-deadline-close (match-string 1)))))))
  3358. (defconst org-agenda-no-heading-message
  3359. "No heading for this item in buffer or region.")
  3360. (defun org-agenda-get-timestamps ()
  3361. "Return the date stamp information for agenda display."
  3362. (let* ((props (list 'face nil
  3363. 'org-not-done-regexp org-not-done-regexp
  3364. 'org-todo-regexp org-todo-regexp
  3365. 'org-complex-heading-regexp org-complex-heading-regexp
  3366. 'mouse-face 'highlight
  3367. 'keymap org-agenda-keymap
  3368. 'help-echo
  3369. (format "mouse-2 or RET jump to org file %s"
  3370. (abbreviate-file-name buffer-file-name))))
  3371. (d1 (calendar-absolute-from-gregorian date))
  3372. (remove-re
  3373. (concat
  3374. (regexp-quote
  3375. (format-time-string
  3376. "<%Y-%m-%d"
  3377. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3378. ".*?>"))
  3379. (regexp
  3380. (concat
  3381. (if org-agenda-include-inactive-timestamps "[[<]" "<")
  3382. (regexp-quote
  3383. (substring
  3384. (format-time-string
  3385. (car org-time-stamp-formats)
  3386. (apply 'encode-time ; DATE bound by calendar
  3387. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  3388. 1 11))
  3389. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  3390. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  3391. marker hdmarker deadlinep scheduledp clockp closedp inactivep
  3392. donep tmp priority category ee txt timestr tags b0 b3 e3 head
  3393. todo-state)
  3394. (goto-char (point-min))
  3395. (while (re-search-forward regexp nil t)
  3396. (setq b0 (match-beginning 0)
  3397. b3 (match-beginning 3) e3 (match-end 3))
  3398. (catch :skip
  3399. (and (org-at-date-range-p) (throw :skip nil))
  3400. (org-agenda-skip)
  3401. (if (and (match-end 1)
  3402. (not (= d1 (org-time-string-to-absolute
  3403. (match-string 1) d1 nil
  3404. org-agenda-repeating-timestamp-show-all))))
  3405. (throw :skip nil))
  3406. (if (and e3
  3407. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  3408. (throw :skip nil))
  3409. (setq marker (org-agenda-new-marker b0)
  3410. category (org-get-category b0)
  3411. tmp (buffer-substring (max (point-min)
  3412. (- b0 org-ds-keyword-length))
  3413. b0)
  3414. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  3415. inactivep (= (char-after b0) ?\[)
  3416. deadlinep (string-match org-deadline-regexp tmp)
  3417. scheduledp (string-match org-scheduled-regexp tmp)
  3418. closedp (and org-agenda-include-inactive-timestamps
  3419. (string-match org-closed-string tmp))
  3420. clockp (and org-agenda-include-inactive-timestamps
  3421. (or (string-match org-clock-string tmp)
  3422. (string-match "]-+\\'" tmp)))
  3423. todo-state (org-get-todo-state)
  3424. donep (member todo-state org-done-keywords))
  3425. (if (or scheduledp deadlinep closedp clockp)
  3426. (throw :skip t))
  3427. (if (string-match ">" timestr)
  3428. ;; substring should only run to end of time stamp
  3429. (setq timestr (substring timestr 0 (match-end 0))))
  3430. (save-excursion
  3431. (if (re-search-backward "^\\*+ " nil t)
  3432. (progn
  3433. (goto-char (match-beginning 0))
  3434. (setq hdmarker (org-agenda-new-marker)
  3435. tags (org-get-tags-at))
  3436. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  3437. (setq head (match-string 1))
  3438. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  3439. (setq txt (org-format-agenda-item
  3440. (if inactivep "[" nil)
  3441. head category tags timestr nil
  3442. remove-re)))
  3443. (setq txt org-agenda-no-heading-message))
  3444. (setq priority (org-get-priority txt))
  3445. (org-add-props txt props
  3446. 'org-marker marker 'org-hd-marker hdmarker)
  3447. (org-add-props txt nil 'priority priority
  3448. 'org-category category 'date date
  3449. 'todo-state todo-state
  3450. 'type "timestamp")
  3451. (push txt ee))
  3452. (outline-next-heading)))
  3453. (nreverse ee)))
  3454. (defun org-agenda-get-sexps ()
  3455. "Return the sexp information for agenda display."
  3456. (require 'diary-lib)
  3457. (let* ((props (list 'face nil
  3458. 'mouse-face 'highlight
  3459. 'keymap org-agenda-keymap
  3460. 'help-echo
  3461. (format "mouse-2 or RET jump to org file %s"
  3462. (abbreviate-file-name buffer-file-name))))
  3463. (regexp "^&?%%(")
  3464. marker category ee txt tags entry result beg b sexp sexp-entry
  3465. todo-state)
  3466. (goto-char (point-min))
  3467. (while (re-search-forward regexp nil t)
  3468. (catch :skip
  3469. (org-agenda-skip)
  3470. (setq beg (match-beginning 0))
  3471. (goto-char (1- (match-end 0)))
  3472. (setq b (point))
  3473. (forward-sexp 1)
  3474. (setq sexp (buffer-substring b (point)))
  3475. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  3476. (org-trim (match-string 1))
  3477. ""))
  3478. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  3479. (when result
  3480. (setq marker (org-agenda-new-marker beg)
  3481. category (org-get-category beg)
  3482. todo-state (org-get-todo-state))
  3483. (if (string-match "\\S-" result)
  3484. (setq txt result)
  3485. (setq txt "SEXP entry returned empty string"))
  3486. (setq txt (org-format-agenda-item
  3487. "" txt category tags 'time))
  3488. (org-add-props txt props 'org-marker marker)
  3489. (org-add-props txt nil
  3490. 'org-category category 'date date 'todo-state todo-state
  3491. 'type "sexp")
  3492. (push txt ee))))
  3493. (nreverse ee)))
  3494. (defalias 'org-get-closed 'org-agenda-get-progress)
  3495. (defun org-agenda-get-progress ()
  3496. "Return the logged TODO entries for agenda display."
  3497. (let* ((props (list 'mouse-face 'highlight
  3498. 'org-not-done-regexp org-not-done-regexp
  3499. 'org-todo-regexp org-todo-regexp
  3500. 'org-complex-heading-regexp org-complex-heading-regexp
  3501. 'keymap org-agenda-keymap
  3502. 'help-echo
  3503. (format "mouse-2 or RET jump to org file %s"
  3504. (abbreviate-file-name buffer-file-name))))
  3505. (items (if (consp org-agenda-show-log)
  3506. org-agenda-show-log
  3507. org-agenda-log-mode-items))
  3508. (parts
  3509. (delq nil
  3510. (list
  3511. (if (memq 'closed items) (concat "\\<" org-closed-string))
  3512. (if (memq 'clock items) (concat "\\<" org-clock-string))
  3513. (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
  3514. (parts-re (if parts (mapconcat 'identity parts "\\|")
  3515. (error "`org-agenda-log-mode-items' is empty")))
  3516. (regexp (concat
  3517. "\\(" parts-re "\\)"
  3518. " *\\["
  3519. (regexp-quote
  3520. (substring
  3521. (format-time-string
  3522. (car org-time-stamp-formats)
  3523. (apply 'encode-time ; DATE bound by calendar
  3524. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  3525. 1 11))))
  3526. marker hdmarker priority category tags closedp statep state
  3527. ee txt timestr rest clocked)
  3528. (goto-char (point-min))
  3529. (while (re-search-forward regexp nil t)
  3530. (catch :skip
  3531. (org-agenda-skip)
  3532. (setq marker (org-agenda-new-marker (match-beginning 0))
  3533. closedp (equal (match-string 1) org-closed-string)
  3534. statep (equal (string-to-char (match-string 1)) ?-)
  3535. state (and statep (match-string 2))
  3536. category (org-get-category (match-beginning 0))
  3537. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  3538. ;; donep (org-entry-is-done-p)
  3539. )
  3540. (when (string-match "\\]" timestr)
  3541. ;; substring should only run to end of time stamp
  3542. (setq rest (substring timestr (match-end 0))
  3543. timestr (substring timestr 0 (match-end 0)))
  3544. (if (and (not closedp) (not statep)
  3545. (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
  3546. (progn (setq timestr (concat (substring timestr 0 -1)
  3547. "-" (match-string 1 rest) "]"))
  3548. (setq clocked (match-string 2 rest)))
  3549. (setq clocked "-")))
  3550. (save-excursion
  3551. (if (re-search-backward "^\\*+ " nil t)
  3552. (progn
  3553. (goto-char (match-beginning 0))
  3554. (setq hdmarker (org-agenda-new-marker)
  3555. tags (org-get-tags-at))
  3556. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  3557. (setq txt (org-format-agenda-item
  3558. (cond
  3559. (closedp "Closed: ")
  3560. (statep (concat "State: (" state ")"))
  3561. (t (concat "Clocked: (" clocked ")")))
  3562. (match-string 1) category tags timestr)))
  3563. (setq txt org-agenda-no-heading-message))
  3564. (setq priority 100000)
  3565. (org-add-props txt props
  3566. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
  3567. 'priority priority 'org-category category
  3568. 'type "closed" 'date date
  3569. 'undone-face 'org-warning 'done-face 'org-agenda-done)
  3570. (push txt ee))
  3571. (goto-char (point-at-eol))))
  3572. (nreverse ee)))
  3573. (defun org-agenda-get-deadlines ()
  3574. "Return the deadline information for agenda display."
  3575. (let* ((props (list 'mouse-face 'highlight
  3576. 'org-not-done-regexp org-not-done-regexp
  3577. 'org-todo-regexp org-todo-regexp
  3578. 'org-complex-heading-regexp org-complex-heading-regexp
  3579. 'keymap org-agenda-keymap
  3580. 'help-echo
  3581. (format "mouse-2 or RET jump to org file %s"
  3582. (abbreviate-file-name buffer-file-name))))
  3583. (regexp org-deadline-time-regexp)
  3584. (todayp (org-agenda-todayp date)) ; DATE bound by calendar
  3585. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  3586. d2 diff dfrac wdays pos pos1 category tags
  3587. ee txt head face s todo-state upcomingp donep timestr)
  3588. (goto-char (point-min))
  3589. (while (re-search-forward regexp nil t)
  3590. (catch :skip
  3591. (org-agenda-skip)
  3592. (setq s (match-string 1)
  3593. pos (1- (match-beginning 1))
  3594. d2 (org-time-string-to-absolute
  3595. (match-string 1) d1 'past
  3596. org-agenda-repeating-timestamp-show-all)
  3597. diff (- d2 d1)
  3598. wdays (org-get-wdays s)
  3599. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  3600. upcomingp (and todayp (> diff 0)))
  3601. ;; When to show a deadline in the calendar:
  3602. ;; If the expiration is within wdays warning time.
  3603. ;; Past-due deadlines are only shown on the current date
  3604. (if (or (and (<= diff wdays)
  3605. (and todayp (not org-agenda-only-exact-dates)))
  3606. (= diff 0))
  3607. (save-excursion
  3608. (setq category (org-get-category))
  3609. (setq todo-state (org-get-todo-state))
  3610. (if (re-search-backward "^\\*+[ \t]+" nil t)
  3611. (progn
  3612. (goto-char (match-end 0))
  3613. (setq pos1 (match-beginning 0))
  3614. (setq tags (org-get-tags-at pos1))
  3615. (setq head (buffer-substring-no-properties
  3616. (point)
  3617. (progn (skip-chars-forward "^\r\n")
  3618. (point))))
  3619. (setq donep (member todo-state org-done-keywords))
  3620. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  3621. (setq timestr
  3622. (concat (substring s (match-beginning 1)) " "))
  3623. (setq timestr 'time))
  3624. (if (and donep
  3625. (or org-agenda-skip-deadline-if-done
  3626. (not (= diff 0))))
  3627. (setq txt nil)
  3628. (setq txt (org-format-agenda-item
  3629. (if (= diff 0)
  3630. (car org-agenda-deadline-leaders)
  3631. (if (functionp (nth 1 org-agenda-deadline-leaders))
  3632. (funcall (nth 1 org-agenda-deadline-leaders) diff date)
  3633. (format (nth 1 org-agenda-deadline-leaders)
  3634. diff)))
  3635. head category tags
  3636. (if (not (= diff 0)) nil timestr)))))
  3637. (setq txt org-agenda-no-heading-message))
  3638. (when txt
  3639. (setq face (org-agenda-deadline-face dfrac wdays))
  3640. (org-add-props txt props
  3641. 'org-marker (org-agenda-new-marker pos)
  3642. 'org-hd-marker (org-agenda-new-marker pos1)
  3643. 'priority (+ (- diff)
  3644. (org-get-priority txt))
  3645. 'org-category category
  3646. 'todo-state todo-state
  3647. 'type (if upcomingp "upcoming-deadline" "deadline")
  3648. 'date (if upcomingp date d2)
  3649. 'face (if donep 'org-agenda-done face)
  3650. 'undone-face face 'done-face 'org-agenda-done)
  3651. (push txt ee))))))
  3652. (nreverse ee)))
  3653. (defun org-agenda-deadline-face (fraction &optional wdays)
  3654. "Return the face to displaying a deadline item.
  3655. FRACTION is what fraction of the head-warning time has passed."
  3656. (if (equal wdays 0) (setq fraction 1.))
  3657. (let ((faces org-agenda-deadline-faces) f)
  3658. (catch 'exit
  3659. (while (setq f (pop faces))
  3660. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  3661. (defun org-agenda-get-scheduled ()
  3662. "Return the scheduled information for agenda display."
  3663. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  3664. 'org-todo-regexp org-todo-regexp
  3665. 'org-complex-heading-regexp org-complex-heading-regexp
  3666. 'done-face 'org-agenda-done
  3667. 'mouse-face 'highlight
  3668. 'keymap org-agenda-keymap
  3669. 'help-echo
  3670. (format "mouse-2 or RET jump to org file %s"
  3671. (abbreviate-file-name buffer-file-name))))
  3672. (regexp org-scheduled-time-regexp)
  3673. (todayp (org-agenda-todayp date)) ; DATE bound by calendar
  3674. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  3675. d2 diff pos pos1 category tags donep
  3676. ee txt head pastschedp todo-state face timestr s)
  3677. (goto-char (point-min))
  3678. (while (re-search-forward regexp nil t)
  3679. (catch :skip
  3680. (org-agenda-skip)
  3681. (setq s (match-string 1)
  3682. pos (1- (match-beginning 1))
  3683. d2 (org-time-string-to-absolute
  3684. (match-string 1) d1 'past
  3685. org-agenda-repeating-timestamp-show-all)
  3686. diff (- d2 d1))
  3687. (setq pastschedp (and todayp (< diff 0)))
  3688. ;; When to show a scheduled item in the calendar:
  3689. ;; If it is on or past the date.
  3690. (if (or (and (< diff 0)
  3691. (< (abs diff) org-scheduled-past-days)
  3692. (and todayp (not org-agenda-only-exact-dates)))
  3693. (= diff 0))
  3694. (save-excursion
  3695. (setq category (org-get-category))
  3696. (setq todo-state (org-get-todo-state))
  3697. (if (re-search-backward "^\\*+[ \t]+" nil t)
  3698. (progn
  3699. (goto-char (match-end 0))
  3700. (setq pos1 (match-beginning 0))
  3701. (setq tags (org-get-tags-at))
  3702. (setq head (buffer-substring-no-properties
  3703. (point)
  3704. (progn (skip-chars-forward "^\r\n") (point))))
  3705. (setq donep (member todo-state org-done-keywords))
  3706. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  3707. (setq timestr
  3708. (concat (substring s (match-beginning 1)) " "))
  3709. (setq timestr 'time))
  3710. (if (and donep
  3711. (or org-agenda-skip-scheduled-if-done
  3712. (not (= diff 0))))
  3713. (setq txt nil)
  3714. (setq txt (org-format-agenda-item
  3715. (if (= diff 0)
  3716. (car org-agenda-scheduled-leaders)
  3717. (format (nth 1 org-agenda-scheduled-leaders)
  3718. (- 1 diff)))
  3719. head category tags
  3720. (if (not (= diff 0)) nil timestr)))))
  3721. (setq txt org-agenda-no-heading-message))
  3722. (when txt
  3723. (setq face
  3724. (cond
  3725. (pastschedp 'org-scheduled-previously)
  3726. (todayp 'org-scheduled-today)
  3727. (t 'org-scheduled)))
  3728. (org-add-props txt props
  3729. 'undone-face face
  3730. 'face (if donep 'org-agenda-done face)
  3731. 'org-marker (org-agenda-new-marker pos)
  3732. 'org-hd-marker (org-agenda-new-marker pos1)
  3733. 'type (if pastschedp "past-scheduled" "scheduled")
  3734. 'date (if pastschedp d2 date)
  3735. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  3736. 'org-category category
  3737. 'todo-state todo-state)
  3738. (push txt ee))))))
  3739. (nreverse ee)))
  3740. (defun org-agenda-get-blocks ()
  3741. "Return the date-range information for agenda display."
  3742. (let* ((props (list 'face nil
  3743. 'org-not-done-regexp org-not-done-regexp
  3744. 'org-todo-regexp org-todo-regexp
  3745. 'org-complex-heading-regexp org-complex-heading-regexp
  3746. 'mouse-face 'highlight
  3747. 'keymap org-agenda-keymap
  3748. 'help-echo
  3749. (format "mouse-2 or RET jump to org file %s"
  3750. (abbreviate-file-name buffer-file-name))))
  3751. (regexp org-tr-regexp)
  3752. (d0 (calendar-absolute-from-gregorian date))
  3753. marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
  3754. head)
  3755. (goto-char (point-min))
  3756. (while (re-search-forward regexp nil t)
  3757. (catch :skip
  3758. (org-agenda-skip)
  3759. (setq pos (point))
  3760. (setq timestr (match-string 0)
  3761. s1 (match-string 1)
  3762. s2 (match-string 2)
  3763. d1 (time-to-days (org-time-string-to-time s1))
  3764. d2 (time-to-days (org-time-string-to-time s2)))
  3765. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  3766. ;; Only allow days between the limits, because the normal
  3767. ;; date stamps will catch the limits.
  3768. (save-excursion
  3769. (setq marker (org-agenda-new-marker (point)))
  3770. (setq category (org-get-category))
  3771. (setq todo-state (org-get-todo-state))
  3772. (if (re-search-backward "^\\*+ " nil t)
  3773. (progn
  3774. (goto-char (match-beginning 0))
  3775. (setq hdmarker (org-agenda-new-marker (point)))
  3776. (setq tags (org-get-tags-at))
  3777. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  3778. (setq head (match-string 1))
  3779. (and org-agenda-skip-timestamp-if-done
  3780. (org-entry-is-done-p)
  3781. (throw :skip t))
  3782. (setq txt (org-format-agenda-item
  3783. (format
  3784. (nth (if (= d1 d2) 0 1)
  3785. org-agenda-timerange-leaders)
  3786. (1+ (- d0 d1)) (1+ (- d2 d1)))
  3787. head category tags
  3788. (if (= d0 d1) timestr))))
  3789. (setq txt org-agenda-no-heading-message))
  3790. (org-add-props txt props
  3791. 'org-marker marker 'org-hd-marker hdmarker
  3792. 'type "block" 'date date
  3793. 'todo-state todo-state
  3794. 'priority (org-get-priority txt) 'org-category category)
  3795. (push txt ee)))
  3796. (goto-char pos)))
  3797. ;; Sort the entries by expiration date.
  3798. (nreverse ee)))
  3799. ;;; Agenda presentation and sorting
  3800. (defvar org-prefix-has-time nil
  3801. "A flag, set by `org-compile-prefix-format'.
  3802. The flag is set if the currently compiled format contains a `%t'.")
  3803. (defvar org-prefix-has-tag nil
  3804. "A flag, set by `org-compile-prefix-format'.
  3805. The flag is set if the currently compiled format contains a `%T'.")
  3806. (defvar org-prefix-has-effort nil
  3807. "A flag, set by `org-compile-prefix-format'.
  3808. The flag is set if the currently compiled format contains a `%e'.")
  3809. (defun org-format-agenda-item (extra txt &optional category tags dotime
  3810. noprefix remove-re)
  3811. "Format TXT to be inserted into the agenda buffer.
  3812. In particular, it adds the prefix and corresponding text properties. EXTRA
  3813. must be a string and replaces the `%s' specifier in the prefix format.
  3814. CATEGORY (string, symbol or nil) may be used to overrule the default
  3815. category taken from local variable or file name. It will replace the `%c'
  3816. specifier in the format. DOTIME, when non-nil, indicates that a
  3817. time-of-day should be extracted from TXT for sorting of this entry, and for
  3818. the `%t' specifier in the format. When DOTIME is a string, this string is
  3819. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  3820. only the correctly processes TXT should be returned - this is used by
  3821. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  3822. Any match of REMOVE-RE will be removed from TXT."
  3823. (save-match-data
  3824. ;; Diary entries sometimes have extra whitespace at the beginning
  3825. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  3826. (when org-agenda-show-inherited-tags
  3827. ;; Fix the tags part in txt
  3828. (setq txt (org-agenda-add-inherited-tags txt tags)))
  3829. (let* ((category (or category
  3830. org-category
  3831. (if buffer-file-name
  3832. (file-name-sans-extension
  3833. (file-name-nondirectory buffer-file-name))
  3834. "")))
  3835. ;; time, tag, effort are needed for the eval of the prefix format
  3836. (tag (if tags (nth (1- (length tags)) tags) ""))
  3837. time effort neffort
  3838. (ts (if dotime (concat
  3839. (if (stringp dotime) dotime "")
  3840. (and org-agenda-search-headline-for-time txt))))
  3841. (time-of-day (and dotime (org-get-time-of-day ts)))
  3842. stamp plain s0 s1 s2 t1 t2 rtn srp
  3843. duration)
  3844. (and (org-mode-p) buffer-file-name
  3845. (add-to-list 'org-agenda-contributing-files buffer-file-name))
  3846. (when (and dotime time-of-day)
  3847. ;; Extract starting and ending time and move them to prefix
  3848. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  3849. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  3850. (setq s0 (match-string 0 ts)
  3851. srp (and stamp (match-end 3))
  3852. s1 (match-string (if plain 1 2) ts)
  3853. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  3854. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  3855. ;; them, we might want to remove them there to avoid duplication.
  3856. ;; The user can turn this off with a variable.
  3857. (if (and org-prefix-has-time
  3858. org-agenda-remove-times-when-in-prefix (or stamp plain)
  3859. (string-match (concat (regexp-quote s0) " *") txt)
  3860. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  3861. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  3862. (= (match-beginning 0) 0)
  3863. t))
  3864. (setq txt (replace-match "" nil nil txt))))
  3865. ;; Normalize the time(s) to 24 hour
  3866. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  3867. (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
  3868. ;; Compute the duration
  3869. (when s1
  3870. (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
  3871. (string-to-number (substring s1 3)))
  3872. t2 (cond
  3873. (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
  3874. (string-to-number (substring s2 3))))
  3875. (org-agenda-default-appointment-duration
  3876. (+ t1 org-agenda-default-appointment-duration))
  3877. (t nil)))
  3878. (setq duration (if t2 (- t2 t1)))))
  3879. (when (and s1 (not s2) org-agenda-default-appointment-duration
  3880. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  3881. (let ((m (+ (string-to-number (match-string 2 s1))
  3882. (* 60 (string-to-number (match-string 1 s1)))
  3883. org-agenda-default-appointment-duration))
  3884. h)
  3885. (setq h (/ m 60) m (- m (* h 60)))
  3886. (setq s2 (format "%02d:%02d" h m))))
  3887. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  3888. txt)
  3889. ;; Tags are in the string
  3890. (if (or (eq org-agenda-remove-tags t)
  3891. (and org-agenda-remove-tags
  3892. org-prefix-has-tag))
  3893. (setq txt (replace-match "" t t txt))
  3894. (setq txt (replace-match
  3895. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  3896. (match-string 2 txt))
  3897. t t txt))))
  3898. (when (org-mode-p)
  3899. (setq effort
  3900. (condition-case nil
  3901. (org-get-effort
  3902. (or (get-text-property 0 'org-hd-marker txt)
  3903. (get-text-property 0 'org-marker txt)))
  3904. (error nil)))
  3905. (when effort
  3906. (setq neffort (org-hh:mm-string-to-minutes effort)
  3907. effort (setq effort (concat "[" effort"]" )))))
  3908. (when remove-re
  3909. (while (string-match remove-re txt)
  3910. (setq txt (replace-match "" t t txt))))
  3911. ;; Create the final string
  3912. (if noprefix
  3913. (setq rtn txt)
  3914. ;; Prepare the variables needed in the eval of the compiled format
  3915. (setq time (cond (s2 (concat s1 "-" s2))
  3916. (s1 (concat s1 "......"))
  3917. (t ""))
  3918. extra (or extra "")
  3919. category (if (symbolp category) (symbol-name category) category))
  3920. ;; Evaluate the compiled format
  3921. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  3922. ;; And finally add the text properties
  3923. (org-add-props rtn nil
  3924. 'org-category (downcase category)
  3925. 'tags (mapcar 'org-downcase-keep-props tags)
  3926. 'org-highest-priority org-highest-priority
  3927. 'org-lowest-priority org-lowest-priority
  3928. 'prefix-length (- (length rtn) (length txt))
  3929. 'time-of-day time-of-day
  3930. 'duration duration
  3931. 'effort effort
  3932. 'effort-minutes neffort
  3933. 'txt txt
  3934. 'time time
  3935. 'extra extra
  3936. 'dotime dotime))))
  3937. (defun org-agenda-add-inherited-tags (txt tags)
  3938. "Remove tags string from TXT, and add complete list of tags.
  3939. The new list includes inherited tags. If any inherited tags are present,
  3940. a double colon separates inherited tags from local tags."
  3941. (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
  3942. (setq txt (substring txt 0 (match-beginning 0))))
  3943. (when tags
  3944. (let ((have-i (get-text-property 0 'inherited (car tags)))
  3945. i)
  3946. (setq txt (concat txt " :"
  3947. (mapconcat
  3948. (lambda (x)
  3949. (setq i (get-text-property 0 'inherited x))
  3950. (if (and have-i (not i))
  3951. (progn
  3952. (setq have-i nil)
  3953. (concat ":" x))
  3954. x))
  3955. tags ":")
  3956. (if have-i "::" ":")))))
  3957. txt)
  3958. (defun org-downcase-keep-props (s)
  3959. (let ((props (text-properties-at 0 s)))
  3960. (setq s (downcase s))
  3961. (add-text-properties 0 (length s) props s)
  3962. s))
  3963. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  3964. (defvar org-agenda-sorting-strategy-selected nil)
  3965. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  3966. (catch 'exit
  3967. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  3968. ((and todayp (member 'today (car org-agenda-time-grid))))
  3969. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  3970. ((member 'weekly (car org-agenda-time-grid)))
  3971. (t (throw 'exit list)))
  3972. (let* ((have (delq nil (mapcar
  3973. (lambda (x) (get-text-property 1 'time-of-day x))
  3974. list)))
  3975. (string (nth 1 org-agenda-time-grid))
  3976. (gridtimes (nth 2 org-agenda-time-grid))
  3977. (req (car org-agenda-time-grid))
  3978. (remove (member 'remove-match req))
  3979. new time)
  3980. (if (and (member 'require-timed req) (not have))
  3981. ;; don't show empty grid
  3982. (throw 'exit list))
  3983. (while (setq time (pop gridtimes))
  3984. (unless (and remove (member time have))
  3985. (setq time (int-to-string time))
  3986. (push (org-format-agenda-item
  3987. nil string "" nil
  3988. (concat (substring time 0 -2) ":" (substring time -2)))
  3989. new)
  3990. (put-text-property
  3991. 1 (length (car new)) 'face 'org-time-grid (car new))))
  3992. (if (member 'time-up org-agenda-sorting-strategy-selected)
  3993. (append new list)
  3994. (append list new)))))
  3995. (defun org-compile-prefix-format (key)
  3996. "Compile the prefix format into a Lisp form that can be evaluated.
  3997. The resulting form is returned and stored in the variable
  3998. `org-prefix-format-compiled'."
  3999. (setq org-prefix-has-time nil org-prefix-has-tag nil
  4000. org-prefix-has-effort nil)
  4001. (let ((s (cond
  4002. ((stringp org-agenda-prefix-format)
  4003. org-agenda-prefix-format)
  4004. ((assq key org-agenda-prefix-format)
  4005. (cdr (assq key org-agenda-prefix-format)))
  4006. (t " %-12:c%?-12t% s")))
  4007. (start 0)
  4008. varform vars var e c f opt)
  4009. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
  4010. s start)
  4011. (setq var (cdr (assoc (match-string 4 s)
  4012. '(("c" . category) ("t" . time) ("s" . extra)
  4013. ("T" . tag) ("e" . effort))))
  4014. c (or (match-string 3 s) "")
  4015. opt (match-beginning 1)
  4016. start (1+ (match-beginning 0)))
  4017. (if (equal var 'time) (setq org-prefix-has-time t))
  4018. (if (equal var 'tag) (setq org-prefix-has-tag t))
  4019. (if (equal var 'effort) (setq org-prefix-has-effort t))
  4020. (setq f (concat "%" (match-string 2 s) "s"))
  4021. (if opt
  4022. (setq varform
  4023. `(if (equal "" ,var)
  4024. ""
  4025. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  4026. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  4027. (setq s (replace-match "%s" t nil s))
  4028. (push varform vars))
  4029. (setq vars (nreverse vars))
  4030. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  4031. (defun org-set-sorting-strategy (key)
  4032. (if (symbolp (car org-agenda-sorting-strategy))
  4033. ;; the old format
  4034. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  4035. (setq org-agenda-sorting-strategy-selected
  4036. (or (cdr (assq key org-agenda-sorting-strategy))
  4037. (cdr (assq 'agenda org-agenda-sorting-strategy))
  4038. '(time-up category-keep priority-down)))))
  4039. (defun org-get-time-of-day (s &optional string mod24)
  4040. "Check string S for a time of day.
  4041. If found, return it as a military time number between 0 and 2400.
  4042. If not found, return nil.
  4043. The optional STRING argument forces conversion into a 5 character wide string
  4044. HH:MM."
  4045. (save-match-data
  4046. (when
  4047. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  4048. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  4049. (let* ((h (string-to-number (match-string 1 s)))
  4050. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  4051. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  4052. (am-p (equal ampm "am"))
  4053. (h1 (cond ((not ampm) h)
  4054. ((= h 12) (if am-p 0 12))
  4055. (t (+ h (if am-p 0 12)))))
  4056. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  4057. (mod h1 24) h1))
  4058. (t0 (+ (* 100 h2) m))
  4059. (t1 (concat (if (>= h1 24) "+" " ")
  4060. (if (< t0 100) "0" "")
  4061. (if (< t0 10) "0" "")
  4062. (int-to-string t0))))
  4063. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  4064. (defun org-finalize-agenda-entries (list &optional nosort)
  4065. "Sort and concatenate the agenda items."
  4066. (setq list (mapcar 'org-agenda-highlight-todo list))
  4067. (if nosort
  4068. list
  4069. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  4070. (defun org-agenda-highlight-todo (x)
  4071. (let ((org-done-keywords org-done-keywords-for-agenda)
  4072. re pl)
  4073. (if (eq x 'line)
  4074. (save-excursion
  4075. (beginning-of-line 1)
  4076. (setq re (get-text-property (point) 'org-todo-regexp))
  4077. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  4078. (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
  4079. (add-text-properties (match-beginning 0) (match-end 0)
  4080. (list 'face (org-get-todo-face 1)))
  4081. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  4082. (delete-region (match-beginning 1) (1- (match-end 0)))
  4083. (goto-char (match-beginning 1))
  4084. (insert (format org-agenda-todo-keyword-format s)))))
  4085. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  4086. pl (get-text-property 0 'prefix-length x))
  4087. (when (and re
  4088. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  4089. x (or pl 0)) pl))
  4090. (add-text-properties
  4091. (or (match-end 1) (match-end 0)) (match-end 0)
  4092. (list 'face (org-get-todo-face (match-string 2 x)))
  4093. x)
  4094. (setq x (concat (substring x 0 (match-end 1))
  4095. (format org-agenda-todo-keyword-format
  4096. (match-string 2 x))
  4097. " "
  4098. (substring x (match-end 3)))))
  4099. x)))
  4100. (defsubst org-cmp-priority (a b)
  4101. "Compare the priorities of string A and B."
  4102. (let ((pa (or (get-text-property 1 'priority a) 0))
  4103. (pb (or (get-text-property 1 'priority b) 0)))
  4104. (cond ((> pa pb) +1)
  4105. ((< pa pb) -1)
  4106. (t nil))))
  4107. (defsubst org-cmp-effort (a b)
  4108. "Compare the priorities of string A and B."
  4109. (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
  4110. (ea (or (get-text-property 1 'effort-minutes a) def))
  4111. (eb (or (get-text-property 1 'effort-minutes b) def)))
  4112. (cond ((> ea eb) +1)
  4113. ((< ea eb) -1)
  4114. (t nil))))
  4115. (defsubst org-cmp-category (a b)
  4116. "Compare the string values of categories of strings A and B."
  4117. (let ((ca (or (get-text-property 1 'org-category a) ""))
  4118. (cb (or (get-text-property 1 'org-category b) "")))
  4119. (cond ((string-lessp ca cb) -1)
  4120. ((string-lessp cb ca) +1)
  4121. (t nil))))
  4122. (defsubst org-cmp-todo-state (a b)
  4123. "Compare the todo states of strings A and B."
  4124. (let* ((ma (or (get-text-property 1 'org-marker a)
  4125. (get-text-property 1 'org-hd-marker a)))
  4126. (mb (or (get-text-property 1 'org-marker b)
  4127. (get-text-property 1 'org-hd-marker b)))
  4128. (fa (and ma (marker-buffer ma)))
  4129. (fb (and mb (marker-buffer mb)))
  4130. (todo-kwds
  4131. (or (and fa (with-current-buffer fa org-todo-keywords-1))
  4132. (and fb (with-current-buffer fb org-todo-keywords-1))))
  4133. (ta (or (get-text-property 1 'todo-state a) ""))
  4134. (tb (or (get-text-property 1 'todo-state b) ""))
  4135. (la (- (length (member ta todo-kwds))))
  4136. (lb (- (length (member tb todo-kwds))))
  4137. (donepa (member ta org-done-keywords-for-agenda))
  4138. (donepb (member tb org-done-keywords-for-agenda)))
  4139. (cond ((and donepa (not donepb)) -1)
  4140. ((and (not donepa) donepb) +1)
  4141. ((< la lb) -1)
  4142. ((< lb la) +1)
  4143. (t nil))))
  4144. (defsubst org-cmp-tag (a b)
  4145. "Compare the string values of the first tags of A and B."
  4146. (let ((ta (car (last (get-text-property 1 'tags a))))
  4147. (tb (car (last (get-text-property 1 'tags b)))))
  4148. (cond ((not ta) +1)
  4149. ((not tb) -1)
  4150. ((string-lessp ta tb) -1)
  4151. ((string-lessp tb ta) +1)
  4152. (t nil))))
  4153. (defsubst org-cmp-time (a b)
  4154. "Compare the time-of-day values of strings A and B."
  4155. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  4156. (ta (or (get-text-property 1 'time-of-day a) def))
  4157. (tb (or (get-text-property 1 'time-of-day b) def)))
  4158. (cond ((< ta tb) -1)
  4159. ((< tb ta) +1)
  4160. (t nil))))
  4161. (defun org-entries-lessp (a b)
  4162. "Predicate for sorting agenda entries."
  4163. ;; The following variables will be used when the form is evaluated.
  4164. ;; So even though the compiler complains, keep them.
  4165. (let* ((time-up (org-cmp-time a b))
  4166. (time-down (if time-up (- time-up) nil))
  4167. (priority-up (org-cmp-priority a b))
  4168. (priority-down (if priority-up (- priority-up) nil))
  4169. (effort-up (org-cmp-effort a b))
  4170. (effort-down (if effort-up (- effort-up) nil))
  4171. (category-up (org-cmp-category a b))
  4172. (category-down (if category-up (- category-up) nil))
  4173. (category-keep (if category-up +1 nil))
  4174. (tag-up (org-cmp-tag a b))
  4175. (tag-down (if tag-up (- tag-up) nil))
  4176. (todo-state-up (org-cmp-todo-state a b))
  4177. (todo-state-down (if todo-state-up (- todo-state-up) nil))
  4178. user-defined-up user-defined-down)
  4179. (if (and org-agenda-cmp-user-defined
  4180. (functionp org-agenda-cmp-user-defined))
  4181. (setq user-defined-up
  4182. (funcall org-agenda-cmp-user-defined a b)
  4183. user-defined-down (if user-defined-up (- user-defined-up) nil)))
  4184. (cdr (assoc
  4185. (eval (cons 'or org-agenda-sorting-strategy-selected))
  4186. '((-1 . t) (1 . nil) (nil . nil))))))
  4187. ;;; Agenda restriction lock
  4188. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  4189. "Overlay to mark the headline to which arenda commands are restricted.")
  4190. (org-overlay-put org-agenda-restriction-lock-overlay
  4191. 'face 'org-agenda-restriction-lock)
  4192. (org-overlay-put org-agenda-restriction-lock-overlay
  4193. 'help-echo "Agendas are currently limited to this subtree.")
  4194. (org-detach-overlay org-agenda-restriction-lock-overlay)
  4195. (defun org-agenda-set-restriction-lock (&optional type)
  4196. "Set restriction lock for agenda, to current subtree or file.
  4197. Restriction will be the file if TYPE is `file', or if type is the
  4198. universal prefix '(4), or if the cursor is before the first headline
  4199. in the file. Otherwise, restriction will be to the current subtree."
  4200. (interactive "P")
  4201. (and (equal type '(4)) (setq type 'file))
  4202. (setq type (cond
  4203. (type type)
  4204. ((org-at-heading-p) 'subtree)
  4205. ((condition-case nil (org-back-to-heading t) (error nil))
  4206. 'subtree)
  4207. (t 'file)))
  4208. (if (eq type 'subtree)
  4209. (progn
  4210. (setq org-agenda-restrict t)
  4211. (setq org-agenda-overriding-restriction 'subtree)
  4212. (put 'org-agenda-files 'org-restrict
  4213. (list (buffer-file-name (buffer-base-buffer))))
  4214. (org-back-to-heading t)
  4215. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  4216. (move-marker org-agenda-restrict-begin (point))
  4217. (move-marker org-agenda-restrict-end
  4218. (save-excursion (org-end-of-subtree t)))
  4219. (message "Locking agenda restriction to subtree"))
  4220. (put 'org-agenda-files 'org-restrict
  4221. (list (buffer-file-name (buffer-base-buffer))))
  4222. (setq org-agenda-restrict nil)
  4223. (setq org-agenda-overriding-restriction 'file)
  4224. (move-marker org-agenda-restrict-begin nil)
  4225. (move-marker org-agenda-restrict-end nil)
  4226. (message "Locking agenda restriction to file"))
  4227. (setq current-prefix-arg nil)
  4228. (org-agenda-maybe-redo))
  4229. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  4230. "Remove the agenda restriction lock."
  4231. (interactive "P")
  4232. (org-detach-overlay org-agenda-restriction-lock-overlay)
  4233. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  4234. (setq org-agenda-overriding-restriction nil)
  4235. (setq org-agenda-restrict nil)
  4236. (put 'org-agenda-files 'org-restrict nil)
  4237. (move-marker org-agenda-restrict-begin nil)
  4238. (move-marker org-agenda-restrict-end nil)
  4239. (setq current-prefix-arg nil)
  4240. (message "Agenda restriction lock removed")
  4241. (or noupdate (org-agenda-maybe-redo)))
  4242. (defun org-agenda-maybe-redo ()
  4243. "If there is any window showing the agenda view, update it."
  4244. (let ((w (get-buffer-window org-agenda-buffer-name t))
  4245. (w0 (selected-window)))
  4246. (when w
  4247. (select-window w)
  4248. (org-agenda-redo)
  4249. (select-window w0)
  4250. (if org-agenda-overriding-restriction
  4251. (message "Agenda view shifted to new %s restriction"
  4252. org-agenda-overriding-restriction)
  4253. (message "Agenda restriction lock removed")))))
  4254. ;;; Agenda commands
  4255. (defun org-agenda-check-type (error &rest types)
  4256. "Check if agenda buffer is of allowed type.
  4257. If ERROR is non-nil, throw an error, otherwise just return nil."
  4258. (if (memq org-agenda-type types)
  4259. t
  4260. (if error
  4261. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  4262. nil)))
  4263. (defun org-agenda-quit ()
  4264. "Exit agenda by removing the window or the buffer."
  4265. (interactive)
  4266. (if org-agenda-columns-active
  4267. (org-columns-quit)
  4268. (let ((buf (current-buffer)))
  4269. (and (not (eq org-agenda-window-setup 'current-window))
  4270. (not (one-window-p))
  4271. (delete-window))
  4272. (kill-buffer buf)
  4273. (org-agenda-reset-markers)
  4274. (org-columns-remove-overlays)
  4275. (setq org-agenda-archives-mode nil))
  4276. ;; Maybe restore the pre-agenda window configuration.
  4277. (and org-agenda-restore-windows-after-quit
  4278. (not (eq org-agenda-window-setup 'other-frame))
  4279. org-pre-agenda-window-conf
  4280. (set-window-configuration org-pre-agenda-window-conf))))
  4281. (defun org-agenda-exit ()
  4282. "Exit agenda by removing the window or the buffer.
  4283. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  4284. Org-mode buffers visited directly by the user will not be touched."
  4285. (interactive)
  4286. (org-release-buffers org-agenda-new-buffers)
  4287. (setq org-agenda-new-buffers nil)
  4288. (org-agenda-quit))
  4289. (defun org-agenda-execute (arg)
  4290. "Execute another agenda command, keeping same window.\\<global-map>
  4291. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  4292. (interactive "P")
  4293. (let ((org-agenda-window-setup 'current-window))
  4294. (org-agenda arg)))
  4295. (defun org-agenda-redo ()
  4296. "Rebuild Agenda.
  4297. When this is the global TODO list, a prefix argument will be interpreted."
  4298. (interactive)
  4299. (let* ((org-agenda-keep-modes t)
  4300. (filter org-agenda-filter)
  4301. (preset (get 'org-agenda-filter :preset-filter))
  4302. (cols org-agenda-columns-active)
  4303. (line (org-current-line))
  4304. (window-line (- line (org-current-line (window-start))))
  4305. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  4306. (put 'org-agenda-filter :preset-filter nil)
  4307. (and cols (org-columns-quit))
  4308. (message "Rebuilding agenda buffer...")
  4309. (org-let lprops '(eval org-agenda-redo-command))
  4310. (setq org-agenda-undo-list nil
  4311. org-agenda-pending-undo-list nil)
  4312. (message "Rebuilding agenda buffer...done")
  4313. (put 'org-agenda-filter :preset-filter preset)
  4314. (and (or filter preset) (org-agenda-filter-apply filter))
  4315. (and cols (interactive-p) (org-agenda-columns))
  4316. (goto-line line)
  4317. (recenter window-line)))
  4318. (defvar org-global-tags-completion-table nil)
  4319. (defvar org-agenda-filter-form nil)
  4320. (defun org-agenda-filter-by-tag (strip &optional char narrow)
  4321. "Keep only those lines in the agenda buffer that have a specific tag.
  4322. The tag is selected with its fast selection letter, as configured.
  4323. With prefix argument STRIP, remove all lines that do have the tag.
  4324. A lisp caller can specify CHAR. NARROW means that the new tag should be
  4325. used to narrow the search - the interactive user can also press `-' or `+'
  4326. to switch to narrowing."
  4327. (interactive "P")
  4328. (let* ((alist org-tag-alist-for-agenda)
  4329. (tag-chars (mapconcat
  4330. (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
  4331. alist ""))
  4332. (efforts (org-split-string
  4333. (or (cdr (assoc (concat org-effort-property "_ALL")
  4334. org-global-properties))
  4335. "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
  4336. (effort-op org-agenda-filter-effort-default-operator)
  4337. (effort-prompt "")
  4338. (inhibit-read-only t)
  4339. (current org-agenda-filter)
  4340. char a n tag)
  4341. (unless char
  4342. (message
  4343. "%s by tag [%s ], [TAB], [/]:off, [+-]:narrow, [>=<?]:effort: "
  4344. (if narrow "Narrow" "Filter") tag-chars)
  4345. (setq char (read-char)))
  4346. (when (member char '(?+ ?-))
  4347. ;; Narrowing down
  4348. (cond ((equal char ?-) (setq strip t narrow t))
  4349. ((equal char ?+) (setq strip nil narrow t)))
  4350. (message
  4351. "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
  4352. (setq char (read-char)))
  4353. (when (member char '(?< ?> ?= ??))
  4354. ;; An effort operator
  4355. (setq effort-op (char-to-string char))
  4356. (setq alist nil) ; to make sure it will be interpreted as effort.
  4357. (unless (equal char ??)
  4358. (loop for i from 0 to 9 do
  4359. (setq effort-prompt
  4360. (concat
  4361. effort-prompt " ["
  4362. (if (= i 9) "0" (int-to-string (1+ i)))
  4363. "]" (nth i efforts))))
  4364. (message "Effort%s: %s " effort-op effort-prompt)
  4365. (setq char (read-char))
  4366. (when (or (< char ?0) (> char ?9))
  4367. (error "Need 1-9,0 to select effort" ))))
  4368. (when (equal char ?\t)
  4369. (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
  4370. (org-set-local 'org-global-tags-completion-table
  4371. (org-global-tags-completion-table)))
  4372. (let ((completion-ignore-case t))
  4373. (setq tag (org-ido-completing-read
  4374. "Tag: " org-global-tags-completion-table))))
  4375. (cond
  4376. ((equal char ?/)
  4377. (org-agenda-filter-by-tag-show-all)
  4378. (when (get 'org-agenda-filter :preset-filter)
  4379. (org-agenda-filter-apply org-agenda-filter)))
  4380. ((or (equal char ?\ )
  4381. (setq a (rassoc char alist))
  4382. (and (>= char ?0) (<= char ?9)
  4383. (setq n (if (= char ?0) 9 (- char ?0 1))
  4384. tag (concat effort-op (nth n efforts))
  4385. a (cons tag nil)))
  4386. (and (= char ??)
  4387. (setq tag "?eff")
  4388. a (cons tag nil))
  4389. (and tag (setq a (cons tag nil))))
  4390. (org-agenda-filter-by-tag-show-all)
  4391. (setq tag (car a))
  4392. (setq org-agenda-filter
  4393. (cons (concat (if strip "-" "+") tag)
  4394. (if narrow current nil)))
  4395. (org-agenda-filter-apply org-agenda-filter))
  4396. (t (error "Invalid tag selection character %c" char)))))
  4397. (defun org-agenda-filter-by-tag-refine (strip &optional char)
  4398. "Refine the current filter. See `org-agenda-filter-by-tag."
  4399. (interactive "P")
  4400. (org-agenda-filter-by-tag strip char 'refine))
  4401. (defun org-agenda-filter-make-matcher ()
  4402. "Create the form that tests a line for the agenda filter."
  4403. (let (f f1)
  4404. (dolist (x (append (get 'org-agenda-filter :preset-filter)
  4405. org-agenda-filter))
  4406. (if (member x '("-" "+"))
  4407. (setq f1 '(not tags))
  4408. (if (string-match "[<=>?]" x)
  4409. (setq f1 (org-agenda-filter-effort-form x))
  4410. (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
  4411. (if (equal (string-to-char x) ?-)
  4412. (setq f1 (list 'not f1))))
  4413. (push f1 f))
  4414. (cons 'and (nreverse f))))
  4415. (defun org-agenda-filter-effort-form (e)
  4416. "Return the form to compare the effort of the current line with what E says.
  4417. E looks line \"+<2:25\"."
  4418. (let (op)
  4419. (setq e (substring e 1))
  4420. (setq op (string-to-char e) e (substring e 1))
  4421. (setq op (cond ((equal op ?<) '<=)
  4422. ((equal op ?>) '>=)
  4423. ((equal op ??) op)
  4424. (t '=)))
  4425. (list 'org-agenda-compare-effort (list 'quote op)
  4426. (org-hh:mm-string-to-minutes e))))
  4427. (defun org-agenda-compare-effort (op value)
  4428. "Compare the effort of the current line with VALUE, using OP.
  4429. If the line does not have an effort defined, return nil."
  4430. (let ((eff (get-text-property (point) 'effort-minutes)))
  4431. (if (equal op ??)
  4432. (not eff)
  4433. (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
  4434. value))))
  4435. (defun org-agenda-filter-apply (filter)
  4436. "Set FILTER as the new agenda filter and apply it."
  4437. (let (tags)
  4438. (setq org-agenda-filter filter
  4439. org-agenda-filter-form (org-agenda-filter-make-matcher))
  4440. (org-agenda-set-mode-name)
  4441. (save-excursion
  4442. (goto-char (point-min))
  4443. (while (not (eobp))
  4444. (if (get-text-property (point) 'org-marker)
  4445. (progn
  4446. (setq tags (get-text-property (point) 'tags)) ; used in eval
  4447. (if (not (eval org-agenda-filter-form))
  4448. (org-agenda-filter-by-tag-hide-line))
  4449. (beginning-of-line 2))
  4450. (beginning-of-line 2))))))
  4451. (defun org-agenda-filter-by-tag-hide-line ()
  4452. (let (ov)
  4453. (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
  4454. (point-at-eol)))
  4455. (org-overlay-put ov 'invisible t)
  4456. (org-overlay-put ov 'type 'tags-filter)
  4457. (push ov org-agenda-filter-overlays)))
  4458. (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
  4459. (setq pos (or pos (point)))
  4460. (save-excursion
  4461. (dolist (ov (org-overlays-at pos))
  4462. (when (and (org-overlay-get ov 'invisible)
  4463. (eq (org-overlay-get ov 'type) 'tags-filter))
  4464. (goto-char pos)
  4465. (if (< (org-overlay-start ov) (point-at-eol))
  4466. (org-move-overlay ov (point-at-eol)
  4467. (org-overlay-end ov)))))))
  4468. (defun org-agenda-filter-by-tag-show-all ()
  4469. (mapc 'org-delete-overlay org-agenda-filter-overlays)
  4470. (setq org-agenda-filter-overlays nil)
  4471. (setq org-agenda-filter nil)
  4472. (setq org-agenda-filter-form nil)
  4473. (org-agenda-set-mode-name))
  4474. (defun org-agenda-manipulate-query-add ()
  4475. "Manipulate the query by adding a search term with positive selection.
  4476. Positive selection means, the term must be matched for selection of an entry."
  4477. (interactive)
  4478. (org-agenda-manipulate-query ?\[))
  4479. (defun org-agenda-manipulate-query-subtract ()
  4480. "Manipulate the query by adding a search term with negative selection.
  4481. Negative selection means, term must not be matched for selection of an entry."
  4482. (interactive)
  4483. (org-agenda-manipulate-query ?\]))
  4484. (defun org-agenda-manipulate-query-add-re ()
  4485. "Manipulate the query by adding a search regexp with positive selection.
  4486. Positive selection means, the regexp must match for selection of an entry."
  4487. (interactive)
  4488. (org-agenda-manipulate-query ?\{))
  4489. (defun org-agenda-manipulate-query-subtract-re ()
  4490. "Manipulate the query by adding a search regexp with negative selection.
  4491. Negative selection means, regexp must not match for selection of an entry."
  4492. (interactive)
  4493. (org-agenda-manipulate-query ?\}))
  4494. (defun org-agenda-manipulate-query (char)
  4495. (cond
  4496. ((memq org-agenda-type '(timeline agenda))
  4497. (if (y-or-n-p "Re-display with inactive time stamps included? ")
  4498. (let ((org-agenda-include-inactive-timestamps t))
  4499. (org-agenda-redo))
  4500. (error "Abort")))
  4501. ((eq org-agenda-type 'search)
  4502. (org-add-to-string
  4503. 'org-agenda-query-string
  4504. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  4505. (?\{ . " +{}") (?\} . " -{}")))))
  4506. (setq org-agenda-redo-command
  4507. (list 'org-search-view
  4508. org-todo-only
  4509. org-agenda-query-string
  4510. (+ (length org-agenda-query-string)
  4511. (if (member char '(?\{ ?\})) 0 1))))
  4512. (set-register org-agenda-query-register org-agenda-query-string)
  4513. (org-agenda-redo))
  4514. (t (error "Cannot manipulate query for %s-type agenda buffers"
  4515. org-agenda-type))))
  4516. (defun org-add-to-string (var string)
  4517. (set var (concat (symbol-value var) string)))
  4518. (defun org-agenda-goto-date (date)
  4519. "Jump to DATE in agenda."
  4520. (interactive (list (org-read-date)))
  4521. (org-agenda-list nil date))
  4522. (defun org-agenda-goto-today ()
  4523. "Go to today."
  4524. (interactive)
  4525. (org-agenda-check-type t 'timeline 'agenda)
  4526. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  4527. (cond
  4528. (tdpos (goto-char tdpos))
  4529. ((eq org-agenda-type 'agenda)
  4530. (let* ((sd (time-to-days
  4531. (time-subtract (current-time)
  4532. (list 0 (* 3600 org-extend-today-until) 0))))
  4533. (comp (org-agenda-compute-time-span sd org-agenda-span))
  4534. (org-agenda-overriding-arguments org-agenda-last-arguments))
  4535. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  4536. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  4537. (org-agenda-redo)
  4538. (org-agenda-find-same-or-today-or-agenda)))
  4539. (t (error "Cannot find today")))))
  4540. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  4541. (goto-char
  4542. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  4543. (text-property-any (point-min) (point-max) 'org-today t)
  4544. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  4545. (point-min))))
  4546. (defun org-agenda-later (arg)
  4547. "Go forward in time by thee current span.
  4548. With prefix ARG, go forward that many times the current span."
  4549. (interactive "p")
  4550. (org-agenda-check-type t 'agenda)
  4551. (let* ((span org-agenda-span)
  4552. (sd org-starting-day)
  4553. (greg (calendar-gregorian-from-absolute sd))
  4554. (cnt (get-text-property (point) 'org-day-cnt))
  4555. greg2 nd)
  4556. (cond
  4557. ((eq span 'day)
  4558. (setq sd (+ arg sd) nd 1))
  4559. ((eq span 'week)
  4560. (setq sd (+ (* 7 arg) sd) nd 7))
  4561. ((eq span 'month)
  4562. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  4563. sd (calendar-absolute-from-gregorian greg2))
  4564. (setcar greg2 (1+ (car greg2)))
  4565. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  4566. ((eq span 'year)
  4567. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  4568. sd (calendar-absolute-from-gregorian greg2))
  4569. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  4570. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  4571. (let ((org-agenda-overriding-arguments
  4572. (list (car org-agenda-last-arguments) sd nd t)))
  4573. (org-agenda-redo)
  4574. (org-agenda-find-same-or-today-or-agenda cnt))))
  4575. (defun org-agenda-earlier (arg)
  4576. "Go backward in time by the current span.
  4577. With prefix ARG, go backward that many times the current span."
  4578. (interactive "p")
  4579. (org-agenda-later (- arg)))
  4580. (defun org-agenda-day-view (&optional day-of-year)
  4581. "Switch to daily view for agenda.
  4582. With argument DAY-OF-YEAR, switch to that day of the year."
  4583. (interactive "P")
  4584. (setq org-agenda-ndays 1)
  4585. (org-agenda-change-time-span 'day day-of-year))
  4586. (defun org-agenda-week-view (&optional iso-week)
  4587. "Switch to daily view for agenda.
  4588. With argument ISO-WEEK, switch to the corresponding ISO week.
  4589. If ISO-WEEK has more then 2 digits, only the last two encode the
  4590. week. Any digits before this encode a year. So 200712 means
  4591. week 12 of year 2007. Years in the range 1938-2037 can also be
  4592. written as 2-digit years."
  4593. (interactive "P")
  4594. (setq org-agenda-ndays 7)
  4595. (org-agenda-change-time-span 'week iso-week))
  4596. (defun org-agenda-month-view (&optional month)
  4597. "Switch to monthly view for agenda.
  4598. With argument MONTH, switch to that month."
  4599. (interactive "P")
  4600. (org-agenda-change-time-span 'month month))
  4601. (defun org-agenda-year-view (&optional year)
  4602. "Switch to yearly view for agenda.
  4603. With argument YEAR, switch to that year.
  4604. If MONTH has more then 2 digits, only the last two encode the
  4605. month. Any digits before this encode a year. So 200712 means
  4606. December year 2007. Years in the range 1938-2037 can also be
  4607. written as 2-digit years."
  4608. (interactive "P")
  4609. (when year
  4610. (setq year (org-small-year-to-year year)))
  4611. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  4612. (org-agenda-change-time-span 'year year)
  4613. (error "Abort")))
  4614. (defun org-agenda-change-time-span (span &optional n)
  4615. "Change the agenda view to SPAN.
  4616. SPAN may be `day', `week', `month', `year'."
  4617. (org-agenda-check-type t 'agenda)
  4618. (if (and (not n) (equal org-agenda-span span))
  4619. (error "Viewing span is already \"%s\"" span))
  4620. (let* ((sd (or (get-text-property (point) 'day)
  4621. org-starting-day))
  4622. (computed (org-agenda-compute-time-span sd span n))
  4623. (org-agenda-overriding-arguments
  4624. (list (car org-agenda-last-arguments)
  4625. (car computed) (cdr computed) t)))
  4626. (org-agenda-redo)
  4627. (org-agenda-find-same-or-today-or-agenda))
  4628. (org-agenda-set-mode-name)
  4629. (message "Switched to %s view" span))
  4630. (defun org-agenda-compute-time-span (sd span &optional n)
  4631. "Compute starting date and number of days for agenda.
  4632. SPAN may be `day', `week', `month', `year'. The return value
  4633. is a cons cell with the starting date and the number of days,
  4634. so that the date SD will be in that range."
  4635. (let* ((greg (calendar-gregorian-from-absolute sd))
  4636. (dg (nth 1 greg))
  4637. (mg (car greg))
  4638. (yg (nth 2 greg))
  4639. nd w1 y1 m1 thisweek)
  4640. (cond
  4641. ((eq span 'day)
  4642. (when n
  4643. (setq sd (+ (calendar-absolute-from-gregorian
  4644. (list mg 1 yg))
  4645. n -1)))
  4646. (setq nd 1))
  4647. ((eq span 'week)
  4648. (let* ((nt (calendar-day-of-week
  4649. (calendar-gregorian-from-absolute sd)))
  4650. (d (if org-agenda-start-on-weekday
  4651. (- nt org-agenda-start-on-weekday)
  4652. 0)))
  4653. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  4654. (when n
  4655. (require 'cal-iso)
  4656. (setq thisweek (car (calendar-iso-from-absolute sd)))
  4657. (when (> n 99)
  4658. (setq y1 (org-small-year-to-year (/ n 100))
  4659. n (mod n 100)))
  4660. (setq sd
  4661. (calendar-absolute-from-iso
  4662. (list n 1
  4663. (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
  4664. (setq nd 7)))
  4665. ((eq span 'month)
  4666. (when (and n (> n 99))
  4667. (setq y1 (org-small-year-to-year (/ n 100))
  4668. n (mod n 100)))
  4669. (setq sd (calendar-absolute-from-gregorian
  4670. (list (or n mg) 1 (or y1 yg)))
  4671. nd (- (calendar-absolute-from-gregorian
  4672. (list (1+ (or n mg)) 1 (or y1 yg)))
  4673. sd)))
  4674. ((eq span 'year)
  4675. (setq sd (calendar-absolute-from-gregorian
  4676. (list 1 1 (or n yg)))
  4677. nd (- (calendar-absolute-from-gregorian
  4678. (list 1 1 (1+ (or n yg))))
  4679. sd))))
  4680. (cons sd nd)))
  4681. (defun org-agenda-next-date-line (&optional arg)
  4682. "Jump to the next line indicating a date in agenda buffer."
  4683. (interactive "p")
  4684. (org-agenda-check-type t 'agenda 'timeline)
  4685. (beginning-of-line 1)
  4686. ;; This does not work if user makes date format that starts with a blank
  4687. (if (looking-at "^\\S-") (forward-char 1))
  4688. (if (not (re-search-forward "^\\S-" nil t arg))
  4689. (progn
  4690. (backward-char 1)
  4691. (error "No next date after this line in this buffer")))
  4692. (goto-char (match-beginning 0)))
  4693. (defun org-agenda-previous-date-line (&optional arg)
  4694. "Jump to the previous line indicating a date in agenda buffer."
  4695. (interactive "p")
  4696. (org-agenda-check-type t 'agenda 'timeline)
  4697. (beginning-of-line 1)
  4698. (if (not (re-search-backward "^\\S-" nil t arg))
  4699. (error "No previous date before this line in this buffer")))
  4700. ;; Initialize the highlight
  4701. (defvar org-hl (org-make-overlay 1 1))
  4702. (org-overlay-put org-hl 'face 'highlight)
  4703. (defun org-highlight (begin end &optional buffer)
  4704. "Highlight a region with overlay."
  4705. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  4706. org-hl begin end (or buffer (current-buffer))))
  4707. (defun org-unhighlight ()
  4708. "Detach overlay INDEX."
  4709. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  4710. ;; FIXME this is currently not used.
  4711. (defun org-highlight-until-next-command (beg end &optional buffer)
  4712. "Move the highlight overlay to BEG/END, remove it before the next command."
  4713. (org-highlight beg end buffer)
  4714. (add-hook 'pre-command-hook 'org-unhighlight-once))
  4715. (defun org-unhighlight-once ()
  4716. "Remove the highlight from its position, and this function from the hook."
  4717. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  4718. (org-unhighlight))
  4719. (defun org-agenda-follow-mode ()
  4720. "Toggle follow mode in an agenda buffer."
  4721. (interactive)
  4722. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  4723. (org-agenda-set-mode-name)
  4724. (message "Follow mode is %s"
  4725. (if org-agenda-follow-mode "on" "off")))
  4726. (defun org-agenda-clockreport-mode ()
  4727. "Toggle clocktable mode in an agenda buffer."
  4728. (interactive)
  4729. (org-agenda-check-type t 'agenda)
  4730. (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
  4731. (org-agenda-set-mode-name)
  4732. (org-agenda-redo)
  4733. (message "Clocktable mode is %s"
  4734. (if org-agenda-clockreport-mode "on" "off")))
  4735. (defun org-agenda-log-mode (&optional special)
  4736. "Toggle log mode in an agenda buffer.
  4737. With argument SPECIAL, show all possible log items, not only the ones
  4738. configured in `org-agenda-log-mode-items'.
  4739. With a double `C-u' prefix arg, show *only* log items, nothing else."
  4740. (interactive "P")
  4741. (org-agenda-check-type t 'agenda 'timeline)
  4742. (setq org-agenda-show-log
  4743. (if (equal special '(16))
  4744. 'only
  4745. (if special '(closed clock state)
  4746. (not org-agenda-show-log))))
  4747. (org-agenda-set-mode-name)
  4748. (org-agenda-redo)
  4749. (message "Log mode is %s"
  4750. (if org-agenda-show-log "on" "off")))
  4751. (defun org-agenda-archives-mode (&optional with-files)
  4752. "Toggle log mode in an agenda buffer."
  4753. (interactive "P")
  4754. (setq org-agenda-archives-mode
  4755. (if with-files t (if org-agenda-archives-mode nil 'trees)))
  4756. (org-agenda-set-mode-name)
  4757. (org-agenda-redo)
  4758. (message
  4759. "%s"
  4760. (cond
  4761. ((eq org-agenda-archives-mode nil)
  4762. "No archives are included")
  4763. ((eq org-agenda-archives-mode 'trees)
  4764. (format "Trees with :%s: tag are included" org-archive-tag))
  4765. ((eq org-agenda-archives-mode t)
  4766. (format "Trees with :%s: tag and all active archive files are included"
  4767. org-archive-tag)))))
  4768. (defun org-agenda-toggle-diary ()
  4769. "Toggle diary inclusion in an agenda buffer."
  4770. (interactive)
  4771. (org-agenda-check-type t 'agenda)
  4772. (setq org-agenda-include-diary (not org-agenda-include-diary))
  4773. (org-agenda-redo)
  4774. (org-agenda-set-mode-name)
  4775. (message "Diary inclusion turned %s"
  4776. (if org-agenda-include-diary "on" "off")))
  4777. (defun org-agenda-toggle-time-grid ()
  4778. "Toggle time grid in an agenda buffer."
  4779. (interactive)
  4780. (org-agenda-check-type t 'agenda)
  4781. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  4782. (org-agenda-redo)
  4783. (org-agenda-set-mode-name)
  4784. (message "Time-grid turned %s"
  4785. (if org-agenda-use-time-grid "on" "off")))
  4786. (defun org-agenda-set-mode-name ()
  4787. "Set the mode name to indicate all the small mode settings."
  4788. (setq mode-name
  4789. (concat "Org-Agenda"
  4790. (if (equal org-agenda-ndays 1) " Day" "")
  4791. (if (equal org-agenda-ndays 7) " Week" "")
  4792. (if org-agenda-follow-mode " Follow" "")
  4793. (if org-agenda-include-diary " Diary" "")
  4794. (if org-agenda-use-time-grid " Grid" "")
  4795. (if (consp org-agenda-show-log) " LogAll"
  4796. (if org-agenda-show-log " Log" ""))
  4797. (if (or org-agenda-filter (get 'org-agenda-filter
  4798. :preset-filter))
  4799. (concat " {" (mapconcat
  4800. 'identity
  4801. (append (get 'org-agenda-filter
  4802. :preset-filter)
  4803. org-agenda-filter) "") "}")
  4804. "")
  4805. (if org-agenda-archives-mode
  4806. (if (eq org-agenda-archives-mode t)
  4807. " Archives"
  4808. (format " :%s:" org-archive-tag))
  4809. "")
  4810. (if org-agenda-clockreport-mode " Clock" "")))
  4811. (force-mode-line-update))
  4812. (defun org-agenda-post-command-hook ()
  4813. (and (eolp) (not (bolp)) (backward-char 1))
  4814. (setq org-agenda-type
  4815. (or (get-text-property (point) 'org-agenda-type)
  4816. (get-text-property (max (point-min) (1- (point)))
  4817. 'org-agenda-type)))
  4818. (if (and org-agenda-follow-mode
  4819. (get-text-property (point) 'org-marker))
  4820. (org-agenda-show)))
  4821. (defun org-agenda-show-priority ()
  4822. "Show the priority of the current item.
  4823. This priority is composed of the main priority given with the [#A] cookies,
  4824. and by additional input from the age of a schedules or deadline entry."
  4825. (interactive)
  4826. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  4827. (message "Priority is %d" (if pri pri -1000))))
  4828. (defun org-agenda-show-tags ()
  4829. "Show the tags applicable to the current item."
  4830. (interactive)
  4831. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  4832. (if tags
  4833. (message "Tags are :%s:"
  4834. (org-no-properties (mapconcat 'identity tags ":")))
  4835. (message "No tags associated with this line"))))
  4836. (defun org-agenda-goto (&optional highlight)
  4837. "Go to the Org-mode file which contains the item at point."
  4838. (interactive)
  4839. (let* ((marker (or (get-text-property (point) 'org-marker)
  4840. (org-agenda-error)))
  4841. (buffer (marker-buffer marker))
  4842. (pos (marker-position marker)))
  4843. (switch-to-buffer-other-window buffer)
  4844. (widen)
  4845. (goto-char pos)
  4846. (when (org-mode-p)
  4847. (org-show-context 'agenda)
  4848. (save-excursion
  4849. (and (outline-next-heading)
  4850. (org-flag-heading nil)))) ; show the next heading
  4851. (recenter (/ (window-height) 2))
  4852. (run-hooks 'org-agenda-after-show-hook)
  4853. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  4854. (defvar org-agenda-after-show-hook nil
  4855. "Normal hook run after an item has been shown from the agenda.
  4856. Point is in the buffer where the item originated.")
  4857. (defun org-agenda-kill ()
  4858. "Kill the entry or subtree belonging to the current agenda entry."
  4859. (interactive)
  4860. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  4861. (let* ((marker (or (get-text-property (point) 'org-marker)
  4862. (org-agenda-error)))
  4863. (buffer (marker-buffer marker))
  4864. (pos (marker-position marker))
  4865. (type (get-text-property (point) 'type))
  4866. dbeg dend (n 0) conf)
  4867. (org-with-remote-undo buffer
  4868. (with-current-buffer buffer
  4869. (save-excursion
  4870. (goto-char pos)
  4871. (if (and (org-mode-p) (not (member type '("sexp"))))
  4872. (setq dbeg (progn (org-back-to-heading t) (point))
  4873. dend (org-end-of-subtree t t))
  4874. (setq dbeg (point-at-bol)
  4875. dend (min (point-max) (1+ (point-at-eol)))))
  4876. (goto-char dbeg)
  4877. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  4878. (setq conf (or (eq t org-agenda-confirm-kill)
  4879. (and (numberp org-agenda-confirm-kill)
  4880. (> n org-agenda-confirm-kill))))
  4881. (and conf
  4882. (not (y-or-n-p
  4883. (format "Delete entry with %d lines in buffer \"%s\"? "
  4884. n (buffer-name buffer))))
  4885. (error "Abort"))
  4886. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  4887. (with-current-buffer buffer (delete-region dbeg dend))
  4888. (message "Agenda item and source killed"))))
  4889. (defun org-agenda-archive ()
  4890. "Archive the entry or subtree belonging to the current agenda entry."
  4891. (interactive)
  4892. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  4893. (let* ((marker (or (get-text-property (point) 'org-marker)
  4894. (org-agenda-error)))
  4895. (buffer (marker-buffer marker))
  4896. (pos (marker-position marker)))
  4897. (org-with-remote-undo buffer
  4898. (with-current-buffer buffer
  4899. (if (org-mode-p)
  4900. (save-excursion
  4901. (goto-char pos)
  4902. (org-remove-subtree-entries-from-agenda)
  4903. (org-back-to-heading t)
  4904. (org-archive-subtree))
  4905. (error "Archiving works only in Org-mode files"))))))
  4906. (defun org-agenda-archive-to-archive-sibling ()
  4907. "Move the entry to the archive sibling."
  4908. (interactive)
  4909. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  4910. (let* ((marker (or (get-text-property (point) 'org-marker)
  4911. (org-agenda-error)))
  4912. (buffer (marker-buffer marker))
  4913. (pos (marker-position marker)))
  4914. (org-with-remote-undo buffer
  4915. (with-current-buffer buffer
  4916. (if (org-mode-p)
  4917. (save-excursion
  4918. (goto-char pos)
  4919. (org-remove-subtree-entries-from-agenda)
  4920. (org-back-to-heading t)
  4921. (org-archive-to-archive-sibling))
  4922. (error "Archiving works only in Org-mode files"))))))
  4923. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  4924. "Remove all lines in the agenda that correspond to a given subtree.
  4925. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  4926. If this information is not given, the function uses the tree at point."
  4927. (let ((buf (or buf (current-buffer))) m p)
  4928. (save-excursion
  4929. (unless (and beg end)
  4930. (org-back-to-heading t)
  4931. (setq beg (point))
  4932. (org-end-of-subtree t)
  4933. (setq end (point)))
  4934. (set-buffer (get-buffer org-agenda-buffer-name))
  4935. (save-excursion
  4936. (goto-char (point-max))
  4937. (beginning-of-line 1)
  4938. (while (not (bobp))
  4939. (when (and (setq m (get-text-property (point) 'org-marker))
  4940. (equal buf (marker-buffer m))
  4941. (setq p (marker-position m))
  4942. (>= p beg)
  4943. (<= p end))
  4944. (let ((inhibit-read-only t))
  4945. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  4946. (beginning-of-line 0))))))
  4947. (defun org-agenda-open-link ()
  4948. "Follow the link in the current line, if any."
  4949. (interactive)
  4950. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  4951. (save-excursion
  4952. (save-restriction
  4953. (narrow-to-region (point-at-bol) (point-at-eol))
  4954. (org-open-at-point))))
  4955. (defun org-agenda-copy-local-variable (var)
  4956. "Get a variable from a referenced buffer and install it here."
  4957. (let ((m (get-text-property (point) 'org-marker)))
  4958. (when (and m (buffer-live-p (marker-buffer m)))
  4959. (org-set-local var (with-current-buffer (marker-buffer m)
  4960. (symbol-value var))))))
  4961. (defun org-agenda-switch-to (&optional delete-other-windows)
  4962. "Go to the Org-mode file which contains the item at point."
  4963. (interactive)
  4964. (let* ((marker (or (get-text-property (point) 'org-marker)
  4965. (org-agenda-error)))
  4966. (buffer (marker-buffer marker))
  4967. (pos (marker-position marker)))
  4968. (switch-to-buffer buffer)
  4969. (and delete-other-windows (delete-other-windows))
  4970. (widen)
  4971. (goto-char pos)
  4972. (when (org-mode-p)
  4973. (org-show-context 'agenda)
  4974. (save-excursion
  4975. (and (outline-next-heading)
  4976. (org-flag-heading nil)))))) ; show the next heading
  4977. (defun org-agenda-goto-mouse (ev)
  4978. "Go to the Org-mode file which contains the item at the mouse click."
  4979. (interactive "e")
  4980. (mouse-set-point ev)
  4981. (org-agenda-goto))
  4982. (defun org-agenda-show (&optional full-entry)
  4983. "Display the Org-mode file which contains the item at point.
  4984. With prefix argument FULL-ENTRY, make the entire entry visible
  4985. if it was hidden in the outline."
  4986. (interactive "P")
  4987. (let ((win (selected-window)))
  4988. (if full-entry
  4989. (let ((org-show-entry-below t))
  4990. (org-agenda-goto t))
  4991. (org-agenda-goto t))
  4992. (select-window win)))
  4993. (defun org-agenda-show-1 (&optional more)
  4994. "Display the Org-mode file which contains the item at point.
  4995. The prefix arg causes further revieling:
  4996. 0 hide the subtree
  4997. 1 just show the entry according to defaults.
  4998. 2 show the text below the heading
  4999. 3 show the entire subtree
  5000. 4 show the entire subtree and any LOGBOOK drawers
  5001. 5 show the entire subtree and any drawers
  5002. With prefix argument FULL-ENTRY, make the entire entry visible
  5003. if it was hidden in the outline."
  5004. (interactive "p")
  5005. (let ((win (selected-window)))
  5006. (org-agenda-goto t)
  5007. (org-recenter-heading 1)
  5008. (cond
  5009. ((= more 0)
  5010. (hide-subtree)
  5011. (message "Remote: hide subtree"))
  5012. ((and (interactive-p) (= more 1))
  5013. (message "Remote: show with default settings"))
  5014. ((= more 2)
  5015. (show-entry)
  5016. (save-excursion
  5017. (org-back-to-heading)
  5018. (org-cycle-hide-drawers 'children))
  5019. (message "Remote: show entry"))
  5020. ((= more 3)
  5021. (show-subtree)
  5022. (save-excursion
  5023. (org-back-to-heading)
  5024. (org-cycle-hide-drawers 'subtree))
  5025. (message "Remote: show subtree"))
  5026. ((= more 4)
  5027. (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
  5028. (org-drawer-regexp
  5029. (concat "^[ \t]*:\\("
  5030. (mapconcat 'regexp-quote org-drawers "\\|")
  5031. "\\):[ \t]*$")))
  5032. (show-subtree)
  5033. (save-excursion
  5034. (org-back-to-heading)
  5035. (org-cycle-hide-drawers 'subtree)))
  5036. (message "Remote: show subtree and LOGBOOK"))
  5037. ((> more 4)
  5038. (show-subtree)
  5039. (message "Remote: show subtree and LOGBOOK")))
  5040. (select-window win)))
  5041. (defun org-recenter-heading (n)
  5042. (save-excursion
  5043. (org-back-to-heading)
  5044. (recenter n)))
  5045. (defvar org-agenda-cycle-counter nil)
  5046. (defun org-agenda-cycle-show (n)
  5047. "Show the current entry in another window, with default settings.
  5048. Default settings are taken from `org-show-hierarchy-above' and siblings.
  5049. When use repeadedly in immediate succession, the remote entry will cycle
  5050. through visibility
  5051. entry -> subtree -> subtree with logbook"
  5052. (interactive "p")
  5053. (when (and (= n 1)
  5054. (not (eq last-command this-command)))
  5055. (setq org-agenda-cycle-counter 0))
  5056. (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
  5057. (if (> org-agenda-cycle-counter 4)
  5058. (setq org-agenda-cycle-counter 0))
  5059. (org-agenda-show-1 org-agenda-cycle-counter))
  5060. (defun org-agenda-recenter (arg)
  5061. "Display the Org-mode file which contains the item at point and recenter."
  5062. (interactive "P")
  5063. (let ((win (selected-window)))
  5064. (org-agenda-goto t)
  5065. (recenter arg)
  5066. (select-window win)))
  5067. (defun org-agenda-show-mouse (ev)
  5068. "Display the Org-mode file which contains the item at the mouse click."
  5069. (interactive "e")
  5070. (mouse-set-point ev)
  5071. (org-agenda-show))
  5072. (defun org-agenda-check-no-diary ()
  5073. "Check if the entry is a diary link and abort if yes."
  5074. (if (get-text-property (point) 'org-agenda-diary-link)
  5075. (org-agenda-error)))
  5076. (defun org-agenda-error ()
  5077. (error "Command not allowed in this line"))
  5078. (defun org-agenda-tree-to-indirect-buffer ()
  5079. "Show the subtree corresponding to the current entry in an indirect buffer.
  5080. This calls the command `org-tree-to-indirect-buffer' from the original
  5081. Org-mode buffer.
  5082. With numerical prefix arg ARG, go up to this level and then take that tree.
  5083. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  5084. dedicated frame)."
  5085. (interactive)
  5086. (org-agenda-check-no-diary)
  5087. (let* ((marker (or (get-text-property (point) 'org-marker)
  5088. (org-agenda-error)))
  5089. (buffer (marker-buffer marker))
  5090. (pos (marker-position marker)))
  5091. (with-current-buffer buffer
  5092. (save-excursion
  5093. (goto-char pos)
  5094. (call-interactively 'org-tree-to-indirect-buffer)))))
  5095. (defvar org-last-heading-marker (make-marker)
  5096. "Marker pointing to the headline that last changed its TODO state
  5097. by a remote command from the agenda.")
  5098. (defun org-agenda-todo-nextset ()
  5099. "Switch TODO entry to next sequence."
  5100. (interactive)
  5101. (org-agenda-todo 'nextset))
  5102. (defun org-agenda-todo-previousset ()
  5103. "Switch TODO entry to previous sequence."
  5104. (interactive)
  5105. (org-agenda-todo 'previousset))
  5106. (defun org-agenda-todo (&optional arg)
  5107. "Cycle TODO state of line at point, also in Org-mode file.
  5108. This changes the line at point, all other lines in the agenda referring to
  5109. the same tree node, and the headline of the tree node in the Org-mode file."
  5110. (interactive "P")
  5111. (org-agenda-check-no-diary)
  5112. (let* ((col (current-column))
  5113. (marker (or (get-text-property (point) 'org-marker)
  5114. (org-agenda-error)))
  5115. (buffer (marker-buffer marker))
  5116. (pos (marker-position marker))
  5117. (hdmarker (get-text-property (point) 'org-hd-marker))
  5118. (todayp (equal (get-text-property (point) 'day)
  5119. (time-to-days (current-time))))
  5120. (inhibit-read-only t)
  5121. org-agenda-headline-snapshot-before-repeat newhead just-one)
  5122. (org-with-remote-undo buffer
  5123. (with-current-buffer buffer
  5124. (widen)
  5125. (goto-char pos)
  5126. (org-show-context 'agenda)
  5127. (save-excursion
  5128. (and (outline-next-heading)
  5129. (org-flag-heading nil))) ; show the next heading
  5130. (let ((current-prefix-arg arg))
  5131. (call-interactively 'org-todo))
  5132. (and (bolp) (forward-char 1))
  5133. (setq newhead (org-get-heading))
  5134. (when (and (org-bound-and-true-p
  5135. org-agenda-headline-snapshot-before-repeat)
  5136. (not (equal org-agenda-headline-snapshot-before-repeat
  5137. newhead))
  5138. todayp)
  5139. (setq newhead org-agenda-headline-snapshot-before-repeat
  5140. just-one t))
  5141. (save-excursion
  5142. (org-back-to-heading)
  5143. (move-marker org-last-heading-marker (point))))
  5144. (beginning-of-line 1)
  5145. (save-excursion
  5146. (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
  5147. (org-move-to-column col))))
  5148. (defun org-agenda-add-note (&optional arg)
  5149. "Add a time-stamped note to the entry at point."
  5150. (interactive "P")
  5151. (org-agenda-check-no-diary)
  5152. (let* ((marker (or (get-text-property (point) 'org-marker)
  5153. (org-agenda-error)))
  5154. (buffer (marker-buffer marker))
  5155. (pos (marker-position marker))
  5156. (hdmarker (get-text-property (point) 'org-hd-marker))
  5157. (inhibit-read-only t))
  5158. (with-current-buffer buffer
  5159. (widen)
  5160. (goto-char pos)
  5161. (org-show-context 'agenda)
  5162. (save-excursion
  5163. (and (outline-next-heading)
  5164. (org-flag-heading nil))) ; show the next heading
  5165. (org-add-note))))
  5166. (defun org-agenda-change-all-lines (newhead hdmarker
  5167. &optional fixface just-this)
  5168. "Change all lines in the agenda buffer which match HDMARKER.
  5169. The new content of the line will be NEWHEAD (as modified by
  5170. `org-format-agenda-item'). HDMARKER is checked with
  5171. `equal' against all `org-hd-marker' text properties in the file.
  5172. If FIXFACE is non-nil, the face of each item is modified according to
  5173. the new TODO state.
  5174. If JUST-THIS is non-nil, change just the current line, not all.
  5175. If FORCE-TAGS is non nil, the car of it returns the new tags."
  5176. (let* ((inhibit-read-only t)
  5177. (line (org-current-line))
  5178. (thetags (with-current-buffer (marker-buffer hdmarker)
  5179. (save-excursion (save-restriction (widen)
  5180. (goto-char hdmarker)
  5181. (org-get-tags-at)))))
  5182. props m pl undone-face done-face finish new dotime cat tags)
  5183. (save-excursion
  5184. (goto-char (point-max))
  5185. (beginning-of-line 1)
  5186. (while (not finish)
  5187. (setq finish (bobp))
  5188. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  5189. (or (not just-this) (= (org-current-line) line))
  5190. (equal m hdmarker))
  5191. (setq props (text-properties-at (point))
  5192. dotime (get-text-property (point) 'dotime)
  5193. cat (get-text-property (point) 'org-category)
  5194. tags thetags
  5195. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  5196. pl (get-text-property (point) 'prefix-length)
  5197. undone-face (get-text-property (point) 'undone-face)
  5198. done-face (get-text-property (point) 'done-face))
  5199. (org-move-to-column pl)
  5200. (cond
  5201. ((equal new "")
  5202. (beginning-of-line 1)
  5203. (and (looking-at ".*\n?") (replace-match "")))
  5204. ((looking-at ".*")
  5205. (replace-match new t t)
  5206. (beginning-of-line 1)
  5207. (add-text-properties (point-at-bol) (point-at-eol) props)
  5208. (when fixface
  5209. (add-text-properties
  5210. (point-at-bol) (point-at-eol)
  5211. (list 'face
  5212. (if org-last-todo-state-is-todo
  5213. undone-face done-face))))
  5214. (org-agenda-highlight-todo 'line)
  5215. (beginning-of-line 1))
  5216. (t (error "Line update did not work"))))
  5217. (beginning-of-line 0)))
  5218. (org-finalize-agenda)))
  5219. (defun org-agenda-align-tags (&optional line)
  5220. "Align all tags in agenda items to `org-agenda-tags-column'."
  5221. (let ((inhibit-read-only t) l c)
  5222. (save-excursion
  5223. (goto-char (if line (point-at-bol) (point-min)))
  5224. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  5225. (if line (point-at-eol) nil) t)
  5226. (add-text-properties
  5227. (match-beginning 2) (match-end 2)
  5228. (list 'face (delq nil (let ((prop (get-text-property
  5229. (match-beginning 2) 'face)))
  5230. (or (listp prop) (setq prop (list prop)))
  5231. (if (memq 'org-tag prop)
  5232. prop
  5233. (cons 'org-tag prop))))))
  5234. (setq l (- (match-end 2) (match-beginning 2))
  5235. c (if (< org-agenda-tags-column 0)
  5236. (- (abs org-agenda-tags-column) l)
  5237. org-agenda-tags-column))
  5238. (delete-region (match-beginning 1) (match-end 1))
  5239. (goto-char (match-beginning 1))
  5240. (insert (org-add-props
  5241. (make-string (max 1 (- c (current-column))) ?\ )
  5242. (text-properties-at (point)))))
  5243. (goto-char (point-min))
  5244. (org-font-lock-add-tag-faces (point-max)))))
  5245. (defun org-agenda-priority-up ()
  5246. "Increase the priority of line at point, also in Org-mode file."
  5247. (interactive)
  5248. (org-agenda-priority 'up))
  5249. (defun org-agenda-priority-down ()
  5250. "Decrease the priority of line at point, also in Org-mode file."
  5251. (interactive)
  5252. (org-agenda-priority 'down))
  5253. (defun org-agenda-priority (&optional force-direction)
  5254. "Set the priority of line at point, also in Org-mode file.
  5255. This changes the line at point, all other lines in the agenda referring to
  5256. the same tree node, and the headline of the tree node in the Org-mode file."
  5257. (interactive)
  5258. (org-agenda-check-no-diary)
  5259. (let* ((marker (or (get-text-property (point) 'org-marker)
  5260. (org-agenda-error)))
  5261. (hdmarker (get-text-property (point) 'org-hd-marker))
  5262. (buffer (marker-buffer hdmarker))
  5263. (pos (marker-position hdmarker))
  5264. (inhibit-read-only t)
  5265. newhead)
  5266. (org-with-remote-undo buffer
  5267. (with-current-buffer buffer
  5268. (widen)
  5269. (goto-char pos)
  5270. (org-show-context 'agenda)
  5271. (save-excursion
  5272. (and (outline-next-heading)
  5273. (org-flag-heading nil))) ; show the next heading
  5274. (funcall 'org-priority force-direction)
  5275. (end-of-line 1)
  5276. (setq newhead (org-get-heading)))
  5277. (org-agenda-change-all-lines newhead hdmarker)
  5278. (beginning-of-line 1))))
  5279. ;; FIXME: should fix the tags property of the agenda line.
  5280. (defun org-agenda-set-tags ()
  5281. "Set tags for the current headline."
  5282. (interactive)
  5283. (org-agenda-check-no-diary)
  5284. (if (and (org-region-active-p) (interactive-p))
  5285. (call-interactively 'org-change-tag-in-region)
  5286. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  5287. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  5288. (org-agenda-error)))
  5289. (buffer (marker-buffer hdmarker))
  5290. (pos (marker-position hdmarker))
  5291. (inhibit-read-only t)
  5292. newhead)
  5293. (org-with-remote-undo buffer
  5294. (with-current-buffer buffer
  5295. (widen)
  5296. (goto-char pos)
  5297. (save-excursion
  5298. (org-show-context 'agenda))
  5299. (save-excursion
  5300. (and (outline-next-heading)
  5301. (org-flag-heading nil))) ; show the next heading
  5302. (goto-char pos)
  5303. (call-interactively 'org-set-tags)
  5304. (end-of-line 1)
  5305. (setq newhead (org-get-heading)))
  5306. (org-agenda-change-all-lines newhead hdmarker)
  5307. (beginning-of-line 1)))))
  5308. (defun org-agenda-toggle-archive-tag ()
  5309. "Toggle the archive tag for the current entry."
  5310. (interactive)
  5311. (org-agenda-check-no-diary)
  5312. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  5313. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  5314. (org-agenda-error)))
  5315. (buffer (marker-buffer hdmarker))
  5316. (pos (marker-position hdmarker))
  5317. (inhibit-read-only t)
  5318. newhead)
  5319. (org-with-remote-undo buffer
  5320. (with-current-buffer buffer
  5321. (widen)
  5322. (goto-char pos)
  5323. (org-show-context 'agenda)
  5324. (save-excursion
  5325. (and (outline-next-heading)
  5326. (org-flag-heading nil))) ; show the next heading
  5327. (call-interactively 'org-toggle-archive-tag)
  5328. (end-of-line 1)
  5329. (setq newhead (org-get-heading)))
  5330. (org-agenda-change-all-lines newhead hdmarker)
  5331. (beginning-of-line 1))))
  5332. (defun org-agenda-do-date-later (arg)
  5333. (interactive "P")
  5334. (cond
  5335. ((or (equal arg '(16))
  5336. (memq last-command
  5337. '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
  5338. (setq this-command 'org-agenda-date-later-minutes)
  5339. (org-agenda-date-later-minutes 1))
  5340. ((or (equal arg '(4))
  5341. (memq last-command
  5342. '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
  5343. (setq this-command 'org-agenda-date-later-hours)
  5344. (org-agenda-date-later-hours 1))
  5345. (t
  5346. (org-agenda-date-later (prefix-numeric-value arg)))))
  5347. (defun org-agenda-do-date-earlier (arg)
  5348. (interactive "P")
  5349. (cond
  5350. ((or (equal arg '(16))
  5351. (memq last-command
  5352. '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
  5353. (setq this-command 'org-agenda-date-earlier-minutes)
  5354. (org-agenda-date-earlier-minutes 1))
  5355. ((or (equal arg '(4))
  5356. (memq last-command
  5357. '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
  5358. (setq this-command 'org-agenda-date-earlier-hours)
  5359. (org-agenda-date-earlier-hours 1))
  5360. (t
  5361. (org-agenda-date-earlier (prefix-numeric-value arg)))))
  5362. (defun org-agenda-date-later (arg &optional what)
  5363. "Change the date of this item to one day later."
  5364. (interactive "p")
  5365. (org-agenda-check-type t 'agenda 'timeline)
  5366. (org-agenda-check-no-diary)
  5367. (let* ((marker (or (get-text-property (point) 'org-marker)
  5368. (org-agenda-error)))
  5369. (buffer (marker-buffer marker))
  5370. (pos (marker-position marker)))
  5371. (org-with-remote-undo buffer
  5372. (with-current-buffer buffer
  5373. (widen)
  5374. (goto-char pos)
  5375. (if (not (org-at-timestamp-p))
  5376. (error "Cannot find time stamp"))
  5377. (org-timestamp-change arg (or what 'day)))
  5378. (org-agenda-show-new-time marker org-last-changed-timestamp))
  5379. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  5380. (defun org-agenda-date-earlier (arg &optional what)
  5381. "Change the date of this item to one day earlier."
  5382. (interactive "p")
  5383. (org-agenda-date-later (- arg) what))
  5384. (defun org-agenda-date-later-minutes (arg)
  5385. "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
  5386. (interactive "p")
  5387. (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
  5388. (org-agenda-date-later arg 'minute))
  5389. (defun org-agenda-date-earlier-minutes (arg)
  5390. "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
  5391. (interactive "p")
  5392. (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
  5393. (org-agenda-date-earlier arg 'minute))
  5394. (defun org-agenda-date-later-hours (arg)
  5395. "Change the time of this item, in hour steps."
  5396. (interactive "p")
  5397. (org-agenda-date-later arg 'hour))
  5398. (defun org-agenda-date-earlier-hours (arg)
  5399. "Change the time of this item, in hour steps."
  5400. (interactive "p")
  5401. (org-agenda-date-earlier arg 'hour))
  5402. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  5403. "Show new date stamp via text properties."
  5404. ;; We use text properties to make this undoable
  5405. (let ((inhibit-read-only t)
  5406. (buffer-invisibility-spec))
  5407. (setq stamp (concat " " prefix " => " stamp))
  5408. (save-excursion
  5409. (goto-char (point-max))
  5410. (while (not (bobp))
  5411. (when (equal marker (get-text-property (point) 'org-marker))
  5412. (org-move-to-column (- (window-width) (length stamp)) t)
  5413. (org-agenda-fix-tags-filter-overlays-at (point))
  5414. (if (featurep 'xemacs)
  5415. ;; Use `duplicable' property to trigger undo recording
  5416. (let ((ex (make-extent nil nil))
  5417. (gl (make-glyph stamp)))
  5418. (set-glyph-face gl 'secondary-selection)
  5419. (set-extent-properties
  5420. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  5421. (insert-extent ex (1- (point)) (point-at-eol)))
  5422. (add-text-properties
  5423. (1- (point)) (point-at-eol)
  5424. (list 'display (org-add-props stamp nil
  5425. 'face 'secondary-selection))))
  5426. (beginning-of-line 1))
  5427. (beginning-of-line 0)))))
  5428. (defun org-agenda-date-prompt (arg)
  5429. "Change the date of this item. Date is prompted for, with default today.
  5430. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  5431. be used to request time specification in the time stamp."
  5432. (interactive "P")
  5433. (org-agenda-check-type t 'agenda 'timeline)
  5434. (org-agenda-check-no-diary)
  5435. (let* ((marker (or (get-text-property (point) 'org-marker)
  5436. (org-agenda-error)))
  5437. (buffer (marker-buffer marker))
  5438. (pos (marker-position marker)))
  5439. (org-with-remote-undo buffer
  5440. (with-current-buffer buffer
  5441. (widen)
  5442. (goto-char pos)
  5443. (if (not (org-at-timestamp-p))
  5444. (error "Cannot find time stamp"))
  5445. (org-time-stamp arg)
  5446. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  5447. (defun org-agenda-schedule (arg)
  5448. "Schedule the item at point."
  5449. (interactive "P")
  5450. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
  5451. (org-agenda-check-no-diary)
  5452. (let* ((marker (or (get-text-property (point) 'org-marker)
  5453. (org-agenda-error)))
  5454. (type (marker-insertion-type marker))
  5455. (buffer (marker-buffer marker))
  5456. (pos (marker-position marker))
  5457. (org-insert-labeled-timestamps-at-point nil)
  5458. ts)
  5459. (set-marker-insertion-type marker t)
  5460. (org-with-remote-undo buffer
  5461. (with-current-buffer buffer
  5462. (widen)
  5463. (goto-char pos)
  5464. (setq ts (org-schedule arg)))
  5465. (org-agenda-show-new-time marker ts "S"))
  5466. (message "Item scheduled for %s" ts)))
  5467. (defun org-agenda-deadline (arg)
  5468. "Schedule the item at point."
  5469. (interactive "P")
  5470. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
  5471. (org-agenda-check-no-diary)
  5472. (let* ((marker (or (get-text-property (point) 'org-marker)
  5473. (org-agenda-error)))
  5474. (buffer (marker-buffer marker))
  5475. (pos (marker-position marker))
  5476. (org-insert-labeled-timestamps-at-point nil)
  5477. ts)
  5478. (org-with-remote-undo buffer
  5479. (with-current-buffer buffer
  5480. (widen)
  5481. (goto-char pos)
  5482. (setq ts (org-deadline arg)))
  5483. (org-agenda-show-new-time marker ts "S"))
  5484. (message "Deadline for this item set to %s" ts)))
  5485. (defun org-agenda-action ()
  5486. "Select entry for agenda action, or execute an agenda action.
  5487. This command prompts for another letter. Valid inputs are:
  5488. m Mark the entry at point for an agenda action
  5489. s Schedule the marked entry to the date at the cursor
  5490. d Set the deadline of the marked entry to the date at the cursor
  5491. r Call `org-remember' with cursor date as the default date
  5492. SPC Show marked entry in other window
  5493. TAB Visit marked entry in other window
  5494. The cursor may be at a date in the calendar, or in the Org agenda."
  5495. (interactive)
  5496. (let (ans)
  5497. (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
  5498. (setq ans (read-char-exclusive))
  5499. (cond
  5500. ((equal ans ?m)
  5501. ;; Mark this entry
  5502. (if (eq major-mode 'org-agenda-mode)
  5503. (let ((m (or (get-text-property (point) 'org-hd-marker)
  5504. (get-text-property (point) 'org-marker))))
  5505. (if m
  5506. (progn
  5507. (move-marker org-agenda-action-marker
  5508. (marker-position m) (marker-buffer m))
  5509. (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
  5510. (error "Don't know which entry to mark")))
  5511. (error "This command works only in the agenda")))
  5512. ((equal ans ?s)
  5513. (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
  5514. ((equal ans ?d)
  5515. (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
  5516. ((equal ans ?r)
  5517. (org-agenda-do-action '(org-remember) t))
  5518. ((equal ans ?\ )
  5519. (let ((cw (selected-window)))
  5520. (org-switch-to-buffer-other-window
  5521. (marker-buffer org-agenda-action-marker))
  5522. (goto-char org-agenda-action-marker)
  5523. (org-show-context 'agenda)
  5524. (select-window cw)))
  5525. ((equal ans ?\C-i)
  5526. (org-switch-to-buffer-other-window
  5527. (marker-buffer org-agenda-action-marker))
  5528. (goto-char org-agenda-action-marker)
  5529. (org-show-context 'agenda))
  5530. (t (error "Invalid agenda action %c" ans)))))
  5531. (defun org-agenda-do-action (form &optional current-buffer)
  5532. "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
  5533. (let ((org-overriding-default-time (org-get-cursor-date)))
  5534. (if current-buffer
  5535. (eval form)
  5536. (if (not (marker-buffer org-agenda-action-marker))
  5537. (error "No entry has bee selected for agenda action")
  5538. (with-current-buffer (marker-buffer org-agenda-action-marker)
  5539. (save-excursion
  5540. (save-restriction
  5541. (widen)
  5542. (goto-char org-agenda-action-marker)
  5543. (eval form))))))))
  5544. (defun org-agenda-clock-in (&optional arg)
  5545. "Start the clock on the currently selected item."
  5546. (interactive "P")
  5547. (org-agenda-check-no-diary)
  5548. (if (equal arg '(4))
  5549. (org-clock-in arg)
  5550. (let* ((marker (or (get-text-property (point) 'org-marker)
  5551. (org-agenda-error)))
  5552. (hdmarker (or (get-text-property (point) 'org-hd-marker)
  5553. marker))
  5554. (pos (marker-position marker))
  5555. newhead)
  5556. (org-with-remote-undo (marker-buffer marker)
  5557. (with-current-buffer (marker-buffer marker)
  5558. (widen)
  5559. (goto-char pos)
  5560. (org-show-context 'agenda)
  5561. (org-show-entry)
  5562. (org-cycle-hide-drawers 'children)
  5563. (org-clock-in arg)
  5564. (setq newhead (org-get-heading)))
  5565. (org-agenda-change-all-lines newhead hdmarker t)))))
  5566. (defun org-agenda-clock-out (&optional arg)
  5567. "Stop the currently running clock."
  5568. (interactive "P")
  5569. (unless (marker-buffer org-clock-marker)
  5570. (error "No running clock"))
  5571. (org-with-remote-undo (marker-buffer org-clock-marker)
  5572. (org-clock-out)))
  5573. (defun org-agenda-clock-cancel (&optional arg)
  5574. "Cancel the currently running clock."
  5575. (interactive "P")
  5576. (unless (marker-buffer org-clock-marker)
  5577. (error "No running clock"))
  5578. (org-with-remote-undo (marker-buffer org-clock-marker)
  5579. (org-clock-cancel)))
  5580. (defun org-agenda-diary-entry ()
  5581. "Make a diary entry, like the `i' command from the calendar.
  5582. All the standard commands work: block, weekly etc."
  5583. (interactive)
  5584. (org-agenda-check-type t 'agenda 'timeline)
  5585. (require 'diary-lib)
  5586. (let* ((char (progn
  5587. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  5588. (read-char-exclusive)))
  5589. (cmd (cdr (assoc char
  5590. '((?d . insert-diary-entry)
  5591. (?w . insert-weekly-diary-entry)
  5592. (?m . insert-monthly-diary-entry)
  5593. (?y . insert-yearly-diary-entry)
  5594. (?a . insert-anniversary-diary-entry)
  5595. (?b . insert-block-diary-entry)
  5596. (?c . insert-cyclic-diary-entry)))))
  5597. (oldf (symbol-function 'calendar-cursor-to-date))
  5598. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  5599. (point (point))
  5600. (mark (or (mark t) (point))))
  5601. (unless cmd
  5602. (error "No command associated with <%c>" char))
  5603. (unless (and (get-text-property point 'day)
  5604. (or (not (equal ?b char))
  5605. (get-text-property mark 'day)))
  5606. (error "Don't know which date to use for diary entry"))
  5607. ;; We implement this by hacking the `calendar-cursor-to-date' function
  5608. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  5609. (let ((calendar-mark-ring
  5610. (list (calendar-gregorian-from-absolute
  5611. (or (get-text-property mark 'day)
  5612. (get-text-property point 'day))))))
  5613. (unwind-protect
  5614. (progn
  5615. (fset 'calendar-cursor-to-date
  5616. (lambda (&optional error dummy)
  5617. (calendar-gregorian-from-absolute
  5618. (get-text-property point 'day))))
  5619. (call-interactively cmd))
  5620. (fset 'calendar-cursor-to-date oldf)))))
  5621. (defun org-agenda-execute-calendar-command (cmd)
  5622. "Execute a calendar command from the agenda, with the date associated to
  5623. the cursor position."
  5624. (org-agenda-check-type t 'agenda 'timeline)
  5625. (require 'diary-lib)
  5626. (unless (get-text-property (point) 'day)
  5627. (error "Don't know which date to use for calendar command"))
  5628. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  5629. (point (point))
  5630. (date (calendar-gregorian-from-absolute
  5631. (get-text-property point 'day)))
  5632. ;; the following 2 vars are needed in the calendar
  5633. (displayed-month (car date))
  5634. (displayed-year (nth 2 date)))
  5635. (unwind-protect
  5636. (progn
  5637. (fset 'calendar-cursor-to-date
  5638. (lambda (&optional error dummy)
  5639. (calendar-gregorian-from-absolute
  5640. (get-text-property point 'day))))
  5641. (call-interactively cmd))
  5642. (fset 'calendar-cursor-to-date oldf))))
  5643. (defun org-agenda-phases-of-moon ()
  5644. "Display the phases of the moon for the 3 months around the cursor date."
  5645. (interactive)
  5646. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  5647. (defun org-agenda-holidays ()
  5648. "Display the holidays for the 3 months around the cursor date."
  5649. (interactive)
  5650. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  5651. (defvar calendar-longitude)
  5652. (defvar calendar-latitude)
  5653. (defvar calendar-location-name)
  5654. (defun org-agenda-sunrise-sunset (arg)
  5655. "Display sunrise and sunset for the cursor date.
  5656. Latitude and longitude can be specified with the variables
  5657. `calendar-latitude' and `calendar-longitude'. When called with prefix
  5658. argument, latitude and longitude will be prompted for."
  5659. (interactive "P")
  5660. (require 'solar)
  5661. (let ((calendar-longitude (if arg nil calendar-longitude))
  5662. (calendar-latitude (if arg nil calendar-latitude))
  5663. (calendar-location-name
  5664. (if arg "the given coordinates" calendar-location-name)))
  5665. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  5666. (defun org-agenda-goto-calendar ()
  5667. "Open the Emacs calendar with the date at the cursor."
  5668. (interactive)
  5669. (org-agenda-check-type t 'agenda 'timeline)
  5670. (let* ((day (or (get-text-property (point) 'day)
  5671. (error "Don't know which date to open in calendar")))
  5672. (date (calendar-gregorian-from-absolute day))
  5673. (calendar-move-hook nil)
  5674. (calendar-view-holidays-initially-flag nil)
  5675. (calendar-view-diary-initially-flag nil)
  5676. (view-calendar-holidays-initially nil)
  5677. (view-diary-entries-initially nil))
  5678. (calendar)
  5679. (calendar-goto-date date)))
  5680. ;;;###autoload
  5681. (defun org-calendar-goto-agenda ()
  5682. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  5683. This is a command that has to be installed in `calendar-mode-map'."
  5684. (interactive)
  5685. (org-agenda-list nil (calendar-absolute-from-gregorian
  5686. (calendar-cursor-to-date))
  5687. nil))
  5688. (defun org-agenda-convert-date ()
  5689. (interactive)
  5690. (org-agenda-check-type t 'agenda 'timeline)
  5691. (let ((day (get-text-property (point) 'day))
  5692. date s)
  5693. (unless day
  5694. (error "Don't know which date to convert"))
  5695. (setq date (calendar-gregorian-from-absolute day))
  5696. (setq s (concat
  5697. "Gregorian: " (calendar-date-string date) "\n"
  5698. "ISO: " (calendar-iso-date-string date) "\n"
  5699. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  5700. "Julian: " (calendar-julian-date-string date) "\n"
  5701. "Astron. JD: " (calendar-astro-date-string date)
  5702. " (Julian date number at noon UTC)\n"
  5703. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  5704. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  5705. "French: " (calendar-french-date-string date) "\n"
  5706. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  5707. "Mayan: " (calendar-mayan-date-string date) "\n"
  5708. "Coptic: " (calendar-coptic-date-string date) "\n"
  5709. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  5710. "Persian: " (calendar-persian-date-string date) "\n"
  5711. "Chinese: " (calendar-chinese-date-string date) "\n"))
  5712. (with-output-to-temp-buffer "*Dates*"
  5713. (princ s))
  5714. (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
  5715. ;;; Appointment reminders
  5716. (defvar appt-time-msg-list)
  5717. ;;;###autoload
  5718. (defun org-agenda-to-appt (&optional refresh filter)
  5719. "Activate appointments found in `org-agenda-files'.
  5720. With a \\[universal-argument] prefix, refresh the list of
  5721. appointments.
  5722. If FILTER is t, interactively prompt the user for a regular
  5723. expression, and filter out entries that don't match it.
  5724. If FILTER is a string, use this string as a regular expression
  5725. for filtering entries out.
  5726. FILTER can also be an alist with the car of each cell being
  5727. either 'headline or 'category. For example:
  5728. '((headline \"IMPORTANT\")
  5729. (category \"Work\"))
  5730. will only add headlines containing IMPORTANT or headlines
  5731. belonging to the \"Work\" category."
  5732. (interactive "P")
  5733. (require 'calendar)
  5734. (if refresh (setq appt-time-msg-list nil))
  5735. (if (eq filter t)
  5736. (setq filter (read-from-minibuffer "Regexp filter: ")))
  5737. (let* ((cnt 0) ; count added events
  5738. (org-agenda-new-buffers nil)
  5739. (org-deadline-warning-days 0)
  5740. (today (org-date-to-gregorian
  5741. (time-to-days (current-time))))
  5742. (files (org-agenda-files 'unrestricted)) entries file)
  5743. ;; Get all entries which may contain an appt
  5744. (org-prepare-agenda-buffers files)
  5745. (while (setq file (pop files))
  5746. (setq entries
  5747. (append entries
  5748. (org-agenda-get-day-entries
  5749. file today :timestamp :scheduled :deadline))))
  5750. (setq entries (delq nil entries))
  5751. ;; Map thru entries and find if we should filter them out
  5752. (mapc
  5753. (lambda(x)
  5754. (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
  5755. (cat (get-text-property 1 'org-category x))
  5756. (tod (get-text-property 1 'time-of-day x))
  5757. (ok (or (null filter)
  5758. (and (stringp filter) (string-match filter evt))
  5759. (and (listp filter)
  5760. (or (string-match
  5761. (cadr (assoc 'category filter)) cat)
  5762. (string-match
  5763. (cadr (assoc 'headline filter)) evt))))))
  5764. ;; FIXME: Shall we remove text-properties for the appt text?
  5765. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  5766. (when (and ok tod)
  5767. (setq tod (concat "00" (number-to-string tod))
  5768. tod (when (string-match
  5769. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
  5770. (concat (match-string 1 tod) ":"
  5771. (match-string 2 tod))))
  5772. (appt-add tod evt)
  5773. (setq cnt (1+ cnt))))) entries)
  5774. (org-release-buffers org-agenda-new-buffers)
  5775. (if (eq cnt 0)
  5776. (message "No event to add")
  5777. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  5778. (defun org-agenda-todayp (date)
  5779. "Does DATE mean today, when considering `org-extend-today-until'?"
  5780. (let (today h)
  5781. (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
  5782. (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
  5783. (setq h (nth 2 (decode-time (current-time))))
  5784. (or (and (>= h org-extend-today-until)
  5785. (= date today))
  5786. (and (< h org-extend-today-until)
  5787. (= date (1- today))))))
  5788. (provide 'org-agenda)
  5789. ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
  5790. ;;; org-agenda.el ends here