org-agenda.el 186 KB

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