org-agenda.el 195 KB

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