org-agenda.el 196 KB

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