org-agenda.el 218 KB

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