org-agenda.el 192 KB

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