org-agenda.el 195 KB

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