org-agenda.el 238 KB

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