org-agenda.el 188 KB

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