org-agenda.el 231 KB

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