org-agenda.el 216 KB

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