org-agenda.el 220 KB

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