org-agenda.el 191 KB

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