org-agenda.el 238 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240
  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.26c
  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. (if (org-entry-get nil "NOBLOCKING")
  2234. t ;; Never block this entry
  2235. (run-hook-with-args-until-failure
  2236. 'org-blocker-hook
  2237. (list :type 'todo-state-change
  2238. :position marker
  2239. :from 'todo
  2240. :to 'done)))))))
  2241. (if org-blocked-by-checkboxes (setq invis1 nil))
  2242. (setq b (if invis1 (max (point-min) (1- (point))) (point))
  2243. e (point-at-eol)
  2244. ov (org-make-overlay b e))
  2245. (if invis1
  2246. (org-overlay-put ov 'invisible t)
  2247. (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
  2248. (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
  2249. (defvar org-agenda-skip-function nil
  2250. "Function to be called at each match during agenda construction.
  2251. If this function returns nil, the current match should not be skipped.
  2252. Otherwise, the function must return a position from where the search
  2253. should be continued.
  2254. This may also be a Lisp form, it will be evaluated.
  2255. Never set this variable using `setq' or so, because then it will apply
  2256. to all future agenda commands. Instead, bind it with `let' to scope
  2257. it dynamically into the agenda-constructing command. A good way to set
  2258. it is through options in org-agenda-custom-commands.")
  2259. (defun org-agenda-skip ()
  2260. "Throw to `:skip' in places that should be skipped.
  2261. Also moves point to the end of the skipped region, so that search can
  2262. continue from there."
  2263. (let ((p (point-at-bol)) to fp)
  2264. (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
  2265. (get-text-property p :org-archived)
  2266. (org-end-of-subtree t)
  2267. (throw :skip t))
  2268. (and org-agenda-skip-comment-trees
  2269. (get-text-property p :org-comment)
  2270. (org-end-of-subtree t)
  2271. (throw :skip t))
  2272. (if (equal (char-after p) ?#) (throw :skip t))
  2273. (when (and (or (setq fp (functionp org-agenda-skip-function))
  2274. (consp org-agenda-skip-function))
  2275. (setq to (save-excursion
  2276. (save-match-data
  2277. (if fp
  2278. (funcall org-agenda-skip-function)
  2279. (eval org-agenda-skip-function))))))
  2280. (goto-char to)
  2281. (throw :skip t))))
  2282. (defvar org-agenda-markers nil
  2283. "List of all currently active markers created by `org-agenda'.")
  2284. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  2285. "Creation time of the last agenda marker.")
  2286. (defun org-agenda-new-marker (&optional pos)
  2287. "Return a new agenda marker.
  2288. Org-mode keeps a list of these markers and resets them when they are
  2289. no longer in use."
  2290. (let ((m (copy-marker (or pos (point)))))
  2291. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  2292. (push m org-agenda-markers)
  2293. m))
  2294. (defun org-agenda-reset-markers ()
  2295. "Reset markers created by `org-agenda'."
  2296. (while org-agenda-markers
  2297. (move-marker (pop org-agenda-markers) nil)))
  2298. (defun org-agenda-save-markers-for-cut-and-paste (beg end)
  2299. "Save relative positions of markers in region."
  2300. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  2301. org-agenda-markers))
  2302. ;;; Agenda timeline
  2303. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  2304. (defun org-timeline (&optional include-all)
  2305. "Show a time-sorted view of the entries in the current org file.
  2306. Only entries with a time stamp of today or later will be listed. With
  2307. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  2308. under the current date.
  2309. If the buffer contains an active region, only check the region for
  2310. dates."
  2311. (interactive "P")
  2312. (require 'calendar)
  2313. (org-compile-prefix-format 'timeline)
  2314. (org-set-sorting-strategy 'timeline)
  2315. (let* ((dopast t)
  2316. (dotodo include-all)
  2317. (doclosed org-agenda-show-log)
  2318. (entry buffer-file-name)
  2319. (date (calendar-current-date))
  2320. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  2321. (end (if (org-region-active-p) (region-end) (point-max)))
  2322. (day-numbers (org-get-all-dates beg end 'no-ranges
  2323. t doclosed ; always include today
  2324. org-timeline-show-empty-dates))
  2325. (org-deadline-warning-days 0)
  2326. (org-agenda-only-exact-dates t)
  2327. (today (time-to-days (current-time)))
  2328. (past t)
  2329. args
  2330. s e rtn d emptyp wd)
  2331. (setq org-agenda-redo-command
  2332. (list 'progn
  2333. (list 'org-switch-to-buffer-other-window (current-buffer))
  2334. (list 'org-timeline (list 'quote include-all))))
  2335. (if (not dopast)
  2336. ;; Remove past dates from the list of dates.
  2337. (setq day-numbers (delq nil (mapcar (lambda(x)
  2338. (if (>= x today) x nil))
  2339. day-numbers))))
  2340. (org-prepare-agenda (concat "Timeline "
  2341. (file-name-nondirectory buffer-file-name)))
  2342. (if doclosed (push :closed args))
  2343. (push :timestamp args)
  2344. (push :deadline args)
  2345. (push :scheduled args)
  2346. (push :sexp args)
  2347. (if dotodo (push :todo args))
  2348. (while (setq d (pop day-numbers))
  2349. (if (and (listp d) (eq (car d) :omitted))
  2350. (progn
  2351. (setq s (point))
  2352. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  2353. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  2354. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  2355. (if (and (>= d today)
  2356. dopast
  2357. past)
  2358. (progn
  2359. (setq past nil)
  2360. (insert (make-string 79 ?-) "\n")))
  2361. (setq date (calendar-gregorian-from-absolute d)
  2362. wd (calendar-day-of-week date))
  2363. (setq s (point))
  2364. (setq rtn (and (not emptyp)
  2365. (apply 'org-agenda-get-day-entries entry
  2366. date args)))
  2367. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  2368. (progn
  2369. (insert
  2370. (if (stringp org-agenda-format-date)
  2371. (format-time-string org-agenda-format-date
  2372. (org-time-from-absolute date))
  2373. (funcall org-agenda-format-date date))
  2374. "\n")
  2375. (put-text-property s (1- (point)) 'face
  2376. (if (member wd org-agenda-weekend-days)
  2377. 'org-agenda-date-weekend
  2378. 'org-agenda-date))
  2379. (put-text-property s (1- (point)) 'org-date-line t)
  2380. (if (equal d today)
  2381. (put-text-property s (1- (point)) 'org-today t))
  2382. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  2383. (put-text-property s (1- (point)) 'day d)))))
  2384. (goto-char (point-min))
  2385. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  2386. (point-min)))
  2387. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  2388. (org-finalize-agenda)
  2389. (setq buffer-read-only t)))
  2390. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  2391. "Return a list of all relevant day numbers from BEG to END buffer positions.
  2392. If NO-RANGES is non-nil, include only the start and end dates of a range,
  2393. not every single day in the range. If FORCE-TODAY is non-nil, make
  2394. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  2395. inactive time stamps (those in square brackets) are included.
  2396. When EMPTY is non-nil, also include days without any entries."
  2397. (let ((re (concat
  2398. (if pre-re pre-re "")
  2399. (if inactive org-ts-regexp-both org-ts-regexp)))
  2400. dates dates1 date day day1 day2 ts1 ts2)
  2401. (if force-today
  2402. (setq dates (list (time-to-days (current-time)))))
  2403. (save-excursion
  2404. (goto-char beg)
  2405. (while (re-search-forward re end t)
  2406. (setq day (time-to-days (org-time-string-to-time
  2407. (substring (match-string 1) 0 10))))
  2408. (or (memq day dates) (push day dates)))
  2409. (unless no-ranges
  2410. (goto-char beg)
  2411. (while (re-search-forward org-tr-regexp end t)
  2412. (setq ts1 (substring (match-string 1) 0 10)
  2413. ts2 (substring (match-string 2) 0 10)
  2414. day1 (time-to-days (org-time-string-to-time ts1))
  2415. day2 (time-to-days (org-time-string-to-time ts2)))
  2416. (while (< (setq day1 (1+ day1)) day2)
  2417. (or (memq day1 dates) (push day1 dates)))))
  2418. (setq dates (sort dates '<))
  2419. (when empty
  2420. (while (setq day (pop dates))
  2421. (setq day2 (car dates))
  2422. (push day dates1)
  2423. (when (and day2 empty)
  2424. (if (or (eq empty t)
  2425. (and (numberp empty) (<= (- day2 day) empty)))
  2426. (while (< (setq day (1+ day)) day2)
  2427. (push (list day) dates1))
  2428. (push (cons :omitted (- day2 day)) dates1))))
  2429. (setq dates (nreverse dates1)))
  2430. dates)))
  2431. ;;; Agenda Daily/Weekly
  2432. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  2433. (defvar org-agenda-start-day nil ; dynamically scoped parameter
  2434. "Custom commands can set this variable in the options section.")
  2435. (defvar org-agenda-last-arguments nil
  2436. "The arguments of the previous call to org-agenda")
  2437. (defvar org-starting-day nil) ; local variable in the agenda buffer
  2438. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  2439. (defvar org-include-all-loc nil) ; local variable
  2440. (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
  2441. ;;;###autoload
  2442. (defun org-agenda-list (&optional include-all start-day ndays)
  2443. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  2444. The view will be for the current day or week, but from the overview buffer
  2445. you will be able to go to other days/weeks.
  2446. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  2447. all unfinished TODO items will also be shown, before the agenda.
  2448. This feature is considered obsolete, please use the TODO list or a block
  2449. agenda instead.
  2450. With a numeric prefix argument in an interactive call, the agenda will
  2451. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  2452. the number of days. NDAYS defaults to `org-agenda-ndays'.
  2453. START-DAY defaults to TODAY, or to the most recent match for the weekday
  2454. given in `org-agenda-start-on-weekday'."
  2455. (interactive "P")
  2456. (if (and (integerp include-all) (> include-all 0))
  2457. (setq ndays include-all include-all nil))
  2458. (setq ndays (or ndays org-agenda-ndays)
  2459. start-day (or start-day org-agenda-start-day))
  2460. (if org-agenda-overriding-arguments
  2461. (setq include-all (car org-agenda-overriding-arguments)
  2462. start-day (nth 1 org-agenda-overriding-arguments)
  2463. ndays (nth 2 org-agenda-overriding-arguments)))
  2464. (if (stringp start-day)
  2465. ;; Convert to an absolute day number
  2466. (setq start-day (time-to-days (org-read-date nil t start-day))))
  2467. (setq org-agenda-last-arguments (list include-all start-day ndays))
  2468. (org-compile-prefix-format 'agenda)
  2469. (org-set-sorting-strategy 'agenda)
  2470. (require 'calendar)
  2471. (let* ((org-agenda-start-on-weekday
  2472. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  2473. org-agenda-start-on-weekday nil))
  2474. (thefiles (org-agenda-files nil 'ifmode))
  2475. (files thefiles)
  2476. (today (time-to-days
  2477. (time-subtract (current-time)
  2478. (list 0 (* 3600 org-extend-today-until) 0))))
  2479. (sd (or start-day today))
  2480. (start (if (or (null org-agenda-start-on-weekday)
  2481. (< org-agenda-ndays 7))
  2482. sd
  2483. (let* ((nt (calendar-day-of-week
  2484. (calendar-gregorian-from-absolute sd)))
  2485. (n1 org-agenda-start-on-weekday)
  2486. (d (- nt n1)))
  2487. (- sd (+ (if (< d 0) 7 0) d)))))
  2488. (day-numbers (list start))
  2489. (day-cnt 0)
  2490. (inhibit-redisplay (not debug-on-error))
  2491. s e rtn rtnall file date d start-pos end-pos todayp nd wd
  2492. clocktable-start clocktable-end)
  2493. (setq org-agenda-redo-command
  2494. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  2495. ;; Make the list of days
  2496. (setq ndays (or ndays org-agenda-ndays)
  2497. nd ndays)
  2498. (while (> ndays 1)
  2499. (push (1+ (car day-numbers)) day-numbers)
  2500. (setq ndays (1- ndays)))
  2501. (setq day-numbers (nreverse day-numbers))
  2502. (setq clocktable-start (car day-numbers)
  2503. clocktable-end (1+ (or (org-last day-numbers) 0)))
  2504. (org-prepare-agenda "Day/Week")
  2505. (org-set-local 'org-starting-day (car day-numbers))
  2506. (org-set-local 'org-include-all-loc include-all)
  2507. (org-set-local 'org-agenda-span
  2508. (org-agenda-ndays-to-span nd))
  2509. (when (and (or include-all org-agenda-include-all-todo)
  2510. (member today day-numbers))
  2511. (setq files thefiles
  2512. rtnall nil)
  2513. (while (setq file (pop files))
  2514. (catch 'nextfile
  2515. (org-check-agenda-file file)
  2516. (setq date (calendar-gregorian-from-absolute today)
  2517. rtn (org-agenda-get-day-entries
  2518. file date :todo))
  2519. (setq rtnall (append rtnall rtn))))
  2520. (when rtnall
  2521. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  2522. (add-text-properties (point-min) (1- (point))
  2523. (list 'face 'org-agenda-structure))
  2524. (insert (org-finalize-agenda-entries rtnall) "\n")))
  2525. (unless org-agenda-compact-blocks
  2526. (let* ((d1 (car day-numbers))
  2527. (d2 (org-last day-numbers))
  2528. (w1 (org-days-to-iso-week d1))
  2529. (w2 (org-days-to-iso-week d2)))
  2530. (setq s (point))
  2531. (if org-agenda-overriding-header
  2532. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  2533. nil 'face 'org-agenda-structure) "\n")
  2534. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  2535. "-agenda"
  2536. (if (< (- d2 d1) 350)
  2537. (if (= w1 w2)
  2538. (format " (W%02d)" w1)
  2539. (format " (W%02d-W%02d)" w1 w2))
  2540. "")
  2541. ":\n")))
  2542. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  2543. 'org-date-line t)))
  2544. (while (setq d (pop day-numbers))
  2545. (setq date (calendar-gregorian-from-absolute d)
  2546. wd (calendar-day-of-week date)
  2547. s (point))
  2548. (if (or (setq todayp (= d today))
  2549. (and (not start-pos) (= d sd)))
  2550. (setq start-pos (point))
  2551. (if (and start-pos (not end-pos))
  2552. (setq end-pos (point))))
  2553. (setq files thefiles
  2554. rtnall nil)
  2555. (while (setq file (pop files))
  2556. (catch 'nextfile
  2557. (org-check-agenda-file file)
  2558. (cond
  2559. ((eq org-agenda-show-log 'only)
  2560. (setq rtn (org-agenda-get-day-entries
  2561. file date :closed)))
  2562. (org-agenda-show-log
  2563. (setq rtn (org-agenda-get-day-entries
  2564. file date
  2565. :deadline :scheduled :timestamp :sexp :closed)))
  2566. (t
  2567. (setq rtn (org-agenda-get-day-entries
  2568. file date
  2569. :deadline :scheduled :sexp :timestamp))))
  2570. (setq rtnall (append rtnall rtn))))
  2571. (if org-agenda-include-diary
  2572. (let ((org-agenda-search-headline-for-time t))
  2573. (require 'diary-lib)
  2574. (setq rtn (org-get-entries-from-diary date))
  2575. (setq rtnall (append rtnall rtn))))
  2576. (if (or rtnall org-agenda-show-all-dates)
  2577. (progn
  2578. (setq day-cnt (1+ day-cnt))
  2579. (insert
  2580. (if (stringp org-agenda-format-date)
  2581. (format-time-string org-agenda-format-date
  2582. (org-time-from-absolute date))
  2583. (funcall org-agenda-format-date date))
  2584. "\n")
  2585. (put-text-property s (1- (point)) 'face
  2586. (if (member wd org-agenda-weekend-days)
  2587. 'org-agenda-date-weekend
  2588. 'org-agenda-date))
  2589. (put-text-property s (1- (point)) 'org-date-line t)
  2590. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  2591. (if todayp (put-text-property s (1- (point)) 'org-today t))
  2592. (if rtnall (insert
  2593. (org-finalize-agenda-entries
  2594. (org-agenda-add-time-grid-maybe
  2595. rtnall nd todayp))
  2596. "\n"))
  2597. (put-text-property s (1- (point)) 'day d)
  2598. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  2599. (when (and org-agenda-clockreport-mode clocktable-start)
  2600. (let ((org-agenda-files (org-agenda-files nil 'ifmode))
  2601. ;; the above line is to ensure the restricted range!
  2602. (p org-agenda-clockreport-parameter-plist)
  2603. tbl)
  2604. (setq p (org-plist-delete p :block))
  2605. (setq p (plist-put p :tstart clocktable-start))
  2606. (setq p (plist-put p :tend clocktable-end))
  2607. (setq p (plist-put p :scope 'agenda))
  2608. (setq tbl (apply 'org-get-clocktable p))
  2609. (insert tbl)))
  2610. (goto-char (point-min))
  2611. (or org-agenda-multi (org-fit-agenda-window))
  2612. (unless (and (pos-visible-in-window-p (point-min))
  2613. (pos-visible-in-window-p (point-max)))
  2614. (goto-char (1- (point-max)))
  2615. (recenter -1)
  2616. (if (not (pos-visible-in-window-p (or start-pos 1)))
  2617. (progn
  2618. (goto-char (or start-pos 1))
  2619. (recenter 1))))
  2620. (goto-char (or start-pos 1))
  2621. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  2622. (org-finalize-agenda)
  2623. (setq buffer-read-only t)
  2624. (message "")))
  2625. (defun org-agenda-ndays-to-span (n)
  2626. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  2627. ;;; Agenda word search
  2628. (defvar org-agenda-search-history nil)
  2629. (defvar org-todo-only nil)
  2630. (defvar org-search-syntax-table nil
  2631. "Special syntax table for org-mode search.
  2632. In this table, we have single quotes not as word constituents, to
  2633. that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
  2634. (defun org-search-syntax-table ()
  2635. (unless org-search-syntax-table
  2636. (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
  2637. (modify-syntax-entry ?' "." org-search-syntax-table)
  2638. (modify-syntax-entry ?` "." org-search-syntax-table))
  2639. org-search-syntax-table)
  2640. ;;;###autoload
  2641. (defun org-search-view (&optional todo-only string edit-at)
  2642. "Show all entries that contain words or regular expressions.
  2643. If the first character of the search string is an asterisks,
  2644. search only the headlines.
  2645. With optional prefix argument TODO-ONLY, only consider entries that are
  2646. TODO entries. The argument STRING can be used to pass a default search
  2647. string into this function. If EDIT-AT is non-nil, it means that the
  2648. user should get a chance to edit this string, with cursor at position
  2649. EDIT-AT.
  2650. The search string is broken into \"words\" by splitting at whitespace.
  2651. The individual words are then interpreted as a boolean expression with
  2652. logical AND. Words prefixed with a minus must not occur in the entry.
  2653. Words without a prefix or prefixed with a plus must occur in the entry.
  2654. Matching is case-insensitive and the words are enclosed by word delimiters.
  2655. Words enclosed by curly braces are interpreted as regular expressions
  2656. that must or must not match in the entry.
  2657. If the search string starts with an asterisk, search only in headlines.
  2658. If (possibly after the leading star) the search string starts with an
  2659. exclamation mark, this also means to look at TODO entries only, an effect
  2660. that can also be achieved with a prefix argument.
  2661. This command searches the agenda files, and in addition the files listed
  2662. in `org-agenda-text-search-extra-files'."
  2663. (interactive "P")
  2664. (org-compile-prefix-format 'search)
  2665. (org-set-sorting-strategy 'search)
  2666. (org-prepare-agenda "SEARCH")
  2667. (let* ((props (list 'face nil
  2668. 'done-face 'org-agenda-done
  2669. 'org-not-done-regexp org-not-done-regexp
  2670. 'org-todo-regexp org-todo-regexp
  2671. 'org-complex-heading-regexp org-complex-heading-regexp
  2672. 'mouse-face 'highlight
  2673. 'keymap org-agenda-keymap
  2674. 'help-echo (format "mouse-2 or RET jump to location")))
  2675. regexp rtn rtnall files file pos
  2676. marker category tags c neg re
  2677. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  2678. (unless (and (not edit-at)
  2679. (stringp string)
  2680. (string-match "\\S-" string))
  2681. (setq string (read-string "[+-]Word/{Regexp} ...: "
  2682. (cond
  2683. ((integerp edit-at) (cons string edit-at))
  2684. (edit-at string))
  2685. 'org-agenda-search-history)))
  2686. (org-set-local 'org-todo-only todo-only)
  2687. (setq org-agenda-redo-command
  2688. (list 'org-search-view (if todo-only t nil) string
  2689. '(if current-prefix-arg 1 nil)))
  2690. (setq org-agenda-query-string string)
  2691. (if (equal (string-to-char string) ?*)
  2692. (setq hdl-only t
  2693. words (substring string 1))
  2694. (setq words string))
  2695. (when (equal (string-to-char words) ?!)
  2696. (setq todo-only t
  2697. words (substring words 1)))
  2698. (setq words (org-split-string words))
  2699. (mapc (lambda (w)
  2700. (setq c (string-to-char w))
  2701. (if (equal c ?-)
  2702. (setq neg t w (substring w 1))
  2703. (if (equal c ?+)
  2704. (setq neg nil w (substring w 1))
  2705. (setq neg nil)))
  2706. (if (string-match "\\`{.*}\\'" w)
  2707. (setq re (substring w 1 -1))
  2708. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  2709. (if neg (push re regexps-) (push re regexps+)))
  2710. words)
  2711. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  2712. (if (not regexps+)
  2713. (setq regexp (concat "^" org-outline-regexp))
  2714. (setq regexp (pop regexps+))
  2715. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  2716. regexp))))
  2717. (setq files (org-agenda-files nil 'ifmode))
  2718. (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
  2719. (pop org-agenda-text-search-extra-files)
  2720. (setq files (org-add-archive-files files)))
  2721. (setq files (append files org-agenda-text-search-extra-files)
  2722. rtnall nil)
  2723. (while (setq file (pop files))
  2724. (setq ee nil)
  2725. (catch 'nextfile
  2726. (org-check-agenda-file file)
  2727. (setq buffer (if (file-exists-p file)
  2728. (org-get-agenda-file-buffer file)
  2729. (error "No such file %s" file)))
  2730. (if (not buffer)
  2731. ;; If file does not exist, make sure an error message is sent
  2732. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  2733. file))))
  2734. (with-current-buffer buffer
  2735. (with-syntax-table (org-search-syntax-table)
  2736. (unless (org-mode-p)
  2737. (error "Agenda file %s is not in `org-mode'" file))
  2738. (let ((case-fold-search t))
  2739. (save-excursion
  2740. (save-restriction
  2741. (if org-agenda-restrict
  2742. (narrow-to-region org-agenda-restrict-begin
  2743. org-agenda-restrict-end)
  2744. (widen))
  2745. (goto-char (point-min))
  2746. (unless (or (org-on-heading-p)
  2747. (outline-next-heading))
  2748. (throw 'nextfile t))
  2749. (goto-char (max (point-min) (1- (point))))
  2750. (while (re-search-forward regexp nil t)
  2751. (org-back-to-heading t)
  2752. (skip-chars-forward "* ")
  2753. (setq beg (point-at-bol)
  2754. beg1 (point)
  2755. end (progn (outline-next-heading) (point)))
  2756. (catch :skip
  2757. (goto-char beg)
  2758. (org-agenda-skip)
  2759. (setq str (buffer-substring-no-properties
  2760. (point-at-bol)
  2761. (if hdl-only (point-at-eol) end)))
  2762. (mapc (lambda (wr) (when (string-match wr str)
  2763. (goto-char (1- end))
  2764. (throw :skip t)))
  2765. regexps-)
  2766. (mapc (lambda (wr) (unless (string-match wr str)
  2767. (goto-char (1- end))
  2768. (throw :skip t)))
  2769. (if todo-only
  2770. (cons (concat "^\*+[ \t]+" org-not-done-regexp)
  2771. regexps+)
  2772. regexps+))
  2773. (goto-char beg)
  2774. (setq marker (org-agenda-new-marker (point))
  2775. category (org-get-category)
  2776. tags (org-get-tags-at (point))
  2777. txt (org-format-agenda-item
  2778. ""
  2779. (buffer-substring-no-properties
  2780. beg1 (point-at-eol))
  2781. category tags))
  2782. (org-add-props txt props
  2783. 'org-marker marker 'org-hd-marker marker
  2784. 'org-todo-regexp org-todo-regexp
  2785. 'org-complex-heading-regexp org-complex-heading-regexp
  2786. 'priority 1000 'org-category category
  2787. 'type "search")
  2788. (push txt ee)
  2789. (goto-char (1- end))))))))))
  2790. (setq rtn (nreverse ee))
  2791. (setq rtnall (append rtnall rtn)))
  2792. (if org-agenda-overriding-header
  2793. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  2794. nil 'face 'org-agenda-structure) "\n")
  2795. (insert "Search words: ")
  2796. (add-text-properties (point-min) (1- (point))
  2797. (list 'face 'org-agenda-structure))
  2798. (setq pos (point))
  2799. (insert string "\n")
  2800. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  2801. (setq pos (point))
  2802. (unless org-agenda-multi
  2803. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  2804. (add-text-properties pos (1- (point))
  2805. (list 'face 'org-agenda-structure))))
  2806. (when rtnall
  2807. (insert (org-finalize-agenda-entries rtnall) "\n"))
  2808. (goto-char (point-min))
  2809. (or org-agenda-multi (org-fit-agenda-window))
  2810. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  2811. (org-finalize-agenda)
  2812. (setq buffer-read-only t)))
  2813. ;;; Agenda TODO list
  2814. (defvar org-select-this-todo-keyword nil)
  2815. (defvar org-last-arg nil)
  2816. ;;;###autoload
  2817. (defun org-todo-list (arg)
  2818. "Show all TODO entries from all agenda file in a single list.
  2819. The prefix arg can be used to select a specific TODO keyword and limit
  2820. the list to these. When using \\[universal-argument], you will be prompted
  2821. for a keyword. A numeric prefix directly selects the Nth keyword in
  2822. `org-todo-keywords-1'."
  2823. (interactive "P")
  2824. (require 'calendar)
  2825. (org-compile-prefix-format 'todo)
  2826. (org-set-sorting-strategy 'todo)
  2827. (org-prepare-agenda "TODO")
  2828. (let* ((today (time-to-days (current-time)))
  2829. (date (calendar-gregorian-from-absolute today))
  2830. (kwds org-todo-keywords-for-agenda)
  2831. (completion-ignore-case t)
  2832. (org-select-this-todo-keyword
  2833. (if (stringp arg) arg
  2834. (and arg (integerp arg) (> arg 0)
  2835. (nth (1- arg) kwds))))
  2836. rtn rtnall files file pos)
  2837. (when (equal arg '(4))
  2838. (setq org-select-this-todo-keyword
  2839. (org-ido-completing-read "Keyword (or KWD1|K2D2|...): "
  2840. (mapcar 'list kwds) nil nil)))
  2841. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  2842. (org-set-local 'org-last-arg arg)
  2843. (setq org-agenda-redo-command
  2844. '(org-todo-list (or current-prefix-arg org-last-arg)))
  2845. (setq files (org-agenda-files nil 'ifmode)
  2846. rtnall nil)
  2847. (while (setq file (pop files))
  2848. (catch 'nextfile
  2849. (org-check-agenda-file file)
  2850. (setq rtn (org-agenda-get-day-entries file date :todo))
  2851. (setq rtnall (append rtnall rtn))))
  2852. (if org-agenda-overriding-header
  2853. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  2854. nil 'face 'org-agenda-structure) "\n")
  2855. (insert "Global list of TODO items of type: ")
  2856. (add-text-properties (point-min) (1- (point))
  2857. (list 'face 'org-agenda-structure))
  2858. (setq pos (point))
  2859. (insert (or org-select-this-todo-keyword "ALL") "\n")
  2860. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  2861. (setq pos (point))
  2862. (unless org-agenda-multi
  2863. (insert "Available with `N r': (0)ALL")
  2864. (let ((n 0) s)
  2865. (mapc (lambda (x)
  2866. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  2867. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  2868. (insert "\n "))
  2869. (insert " " s))
  2870. kwds))
  2871. (insert "\n"))
  2872. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  2873. (when rtnall
  2874. (insert (org-finalize-agenda-entries rtnall) "\n"))
  2875. (goto-char (point-min))
  2876. (or org-agenda-multi (org-fit-agenda-window))
  2877. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  2878. (org-finalize-agenda)
  2879. (setq buffer-read-only t)))
  2880. ;;; Agenda tags match
  2881. ;;;###autoload
  2882. (defun org-tags-view (&optional todo-only match)
  2883. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  2884. The prefix arg TODO-ONLY limits the search to TODO entries."
  2885. (interactive "P")
  2886. (org-compile-prefix-format 'tags)
  2887. (org-set-sorting-strategy 'tags)
  2888. (let* ((org-tags-match-list-sublevels
  2889. (if todo-only t org-tags-match-list-sublevels))
  2890. (completion-ignore-case t)
  2891. rtn rtnall files file pos matcher
  2892. buffer)
  2893. (setq matcher (org-make-tags-matcher match)
  2894. match (car matcher) matcher (cdr matcher))
  2895. (org-prepare-agenda (concat "TAGS " match))
  2896. (setq org-agenda-query-string match)
  2897. (setq org-agenda-redo-command
  2898. (list 'org-tags-view (list 'quote todo-only)
  2899. (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
  2900. (setq files (org-agenda-files nil 'ifmode)
  2901. rtnall nil)
  2902. (while (setq file (pop files))
  2903. (catch 'nextfile
  2904. (org-check-agenda-file file)
  2905. (setq buffer (if (file-exists-p file)
  2906. (org-get-agenda-file-buffer file)
  2907. (error "No such file %s" file)))
  2908. (if (not buffer)
  2909. ;; If file does not exist, error message to agenda
  2910. (setq rtn (list
  2911. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  2912. rtnall (append rtnall rtn))
  2913. (with-current-buffer buffer
  2914. (unless (org-mode-p)
  2915. (error "Agenda file %s is not in `org-mode'" file))
  2916. (save-excursion
  2917. (save-restriction
  2918. (if org-agenda-restrict
  2919. (narrow-to-region org-agenda-restrict-begin
  2920. org-agenda-restrict-end)
  2921. (widen))
  2922. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  2923. (setq rtnall (append rtnall rtn))))))))
  2924. (if org-agenda-overriding-header
  2925. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  2926. nil 'face 'org-agenda-structure) "\n")
  2927. (insert "Headlines with TAGS match: ")
  2928. (add-text-properties (point-min) (1- (point))
  2929. (list 'face 'org-agenda-structure))
  2930. (setq pos (point))
  2931. (insert match "\n")
  2932. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  2933. (setq pos (point))
  2934. (unless org-agenda-multi
  2935. (insert "Press `C-u r' to search again with new search string\n"))
  2936. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  2937. (when rtnall
  2938. (insert (org-finalize-agenda-entries rtnall) "\n"))
  2939. (goto-char (point-min))
  2940. (or org-agenda-multi (org-fit-agenda-window))
  2941. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  2942. (org-finalize-agenda)
  2943. (setq buffer-read-only t)))
  2944. ;;; Agenda Finding stuck projects
  2945. (defvar org-agenda-skip-regexp nil
  2946. "Regular expression used in skipping subtrees for the agenda.
  2947. This is basically a temporary global variable that can be set and then
  2948. used by user-defined selections using `org-agenda-skip-function'.")
  2949. (defvar org-agenda-overriding-header nil
  2950. "When this is set during todo and tags searches, will replace header.
  2951. This variable should not be set directly, but custom commands can bind it
  2952. in the options section.")
  2953. (defun org-agenda-skip-entry-when-regexp-matches ()
  2954. "Checks if the current entry contains match for `org-agenda-skip-regexp'.
  2955. If yes, it returns the end position of this entry, causing agenda commands
  2956. to skip the entry but continuing the search in the subtree. This is a
  2957. function that can be put into `org-agenda-skip-function' for the duration
  2958. of a command."
  2959. (let ((end (save-excursion (org-end-of-subtree t)))
  2960. skip)
  2961. (save-excursion
  2962. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  2963. (and skip end)))
  2964. (defun org-agenda-skip-subtree-when-regexp-matches ()
  2965. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  2966. If yes, it returns the end position of this tree, causing agenda commands
  2967. to skip this subtree. This is a function that can be put into
  2968. `org-agenda-skip-function' for the duration of a command."
  2969. (let ((end (save-excursion (org-end-of-subtree t)))
  2970. skip)
  2971. (save-excursion
  2972. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  2973. (and skip end)))
  2974. (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
  2975. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  2976. If yes, it returns the end position of the current entry (NOT the tree),
  2977. causing agenda commands to skip the entry but continuing the search in
  2978. the subtree. This is a function that can be put into
  2979. `org-agenda-skip-function' for the duration of a command. An important
  2980. use of this function is for the stuck project list."
  2981. (let ((end (save-excursion (org-end-of-subtree t)))
  2982. (entry-end (save-excursion (outline-next-heading) (1- (point))))
  2983. skip)
  2984. (save-excursion
  2985. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  2986. (and skip entry-end)))
  2987. (defun org-agenda-skip-entry-if (&rest conditions)
  2988. "Skip entry if any of CONDITIONS is true.
  2989. See `org-agenda-skip-if' for details."
  2990. (org-agenda-skip-if nil conditions))
  2991. (defun org-agenda-skip-subtree-if (&rest conditions)
  2992. "Skip entry if any of CONDITIONS is true.
  2993. See `org-agenda-skip-if' for details."
  2994. (org-agenda-skip-if t conditions))
  2995. (defun org-agenda-skip-if (subtree conditions)
  2996. "Checks current entity for CONDITIONS.
  2997. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  2998. the entry, i.e. the text before the next heading is checked.
  2999. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  3000. from different tests. Valid conditions are:
  3001. scheduled Check if there is a scheduled cookie
  3002. notscheduled Check if there is no scheduled cookie
  3003. deadline Check if there is a deadline
  3004. notdeadline Check if there is no deadline
  3005. timestamp Check if there is a timestamp (also deadline or scheduled)
  3006. nottimestamp Check if there is no timestamp (also deadline or scheduled)
  3007. regexp Check if regexp matches
  3008. notregexp Check if regexp does not match.
  3009. The regexp is taken from the conditions list, it must come right after
  3010. the `regexp' or `notregexp' element.
  3011. If any of these conditions is met, this function returns the end point of
  3012. the entity, causing the search to continue from there. This is a function
  3013. that can be put into `org-agenda-skip-function' for the duration of a command."
  3014. (let (beg end m)
  3015. (org-back-to-heading t)
  3016. (setq beg (point)
  3017. end (if subtree
  3018. (progn (org-end-of-subtree t) (point))
  3019. (progn (outline-next-heading) (1- (point)))))
  3020. (goto-char beg)
  3021. (and
  3022. (or
  3023. (and (memq 'scheduled conditions)
  3024. (re-search-forward org-scheduled-time-regexp end t))
  3025. (and (memq 'notscheduled conditions)
  3026. (not (re-search-forward org-scheduled-time-regexp end t)))
  3027. (and (memq 'deadline conditions)
  3028. (re-search-forward org-deadline-time-regexp end t))
  3029. (and (memq 'notdeadline conditions)
  3030. (not (re-search-forward org-deadline-time-regexp end t)))
  3031. (and (memq 'timestamp conditions)
  3032. (re-search-forward org-ts-regexp end t))
  3033. (and (memq 'nottimestamp conditions)
  3034. (not (re-search-forward org-ts-regexp end t)))
  3035. (and (setq m (memq 'regexp conditions))
  3036. (stringp (nth 1 m))
  3037. (re-search-forward (nth 1 m) end t))
  3038. (and (setq m (memq 'notregexp conditions))
  3039. (stringp (nth 1 m))
  3040. (not (re-search-forward (nth 1 m) end t))))
  3041. end)))
  3042. ;;;###autoload
  3043. (defun org-agenda-list-stuck-projects (&rest ignore)
  3044. "Create agenda view for projects that are stuck.
  3045. Stuck projects are project that have no next actions. For the definitions
  3046. of what a project is and how to check if it stuck, customize the variable
  3047. `org-stuck-projects'.
  3048. MATCH is being ignored."
  3049. (interactive)
  3050. (let* ((org-agenda-skip-function
  3051. 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
  3052. ;; We could have used org-agenda-skip-if here.
  3053. (org-agenda-overriding-header "List of stuck projects: ")
  3054. (matcher (nth 0 org-stuck-projects))
  3055. (todo (nth 1 org-stuck-projects))
  3056. (todo-wds (if (member "*" todo)
  3057. (progn
  3058. (org-prepare-agenda-buffers (org-agenda-files
  3059. nil 'ifmode))
  3060. (org-delete-all
  3061. org-done-keywords-for-agenda
  3062. (copy-sequence org-todo-keywords-for-agenda)))
  3063. todo))
  3064. (todo-re (concat "^\\*+[ \t]+\\("
  3065. (mapconcat 'identity todo-wds "\\|")
  3066. "\\)\\>"))
  3067. (tags (nth 2 org-stuck-projects))
  3068. (tags-re (if (member "*" tags)
  3069. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  3070. (if tags
  3071. (concat "^\\*+ .*:\\("
  3072. (mapconcat 'identity tags "\\|")
  3073. (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
  3074. (gen-re (nth 3 org-stuck-projects))
  3075. (re-list
  3076. (delq nil
  3077. (list
  3078. (if todo todo-re)
  3079. (if tags tags-re)
  3080. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  3081. gen-re)))))
  3082. (setq org-agenda-skip-regexp
  3083. (if re-list
  3084. (mapconcat 'identity re-list "\\|")
  3085. (error "No information how to identify unstuck projects")))
  3086. (org-tags-view nil matcher)
  3087. (with-current-buffer org-agenda-buffer-name
  3088. (setq org-agenda-redo-command
  3089. '(org-agenda-list-stuck-projects
  3090. (or current-prefix-arg org-last-arg))))))
  3091. ;;; Diary integration
  3092. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  3093. (defvar list-diary-entries-hook)
  3094. (defun org-get-entries-from-diary (date)
  3095. "Get the (Emacs Calendar) diary entries for DATE."
  3096. (require 'diary-lib)
  3097. (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
  3098. (fancy-diary-buffer diary-fancy-buffer)
  3099. (diary-display-hook '(fancy-diary-display))
  3100. (diary-display-function 'fancy-diary-display)
  3101. (pop-up-frames nil)
  3102. (list-diary-entries-hook
  3103. (cons 'org-diary-default-entry list-diary-entries-hook))
  3104. (diary-file-name-prefix-function nil) ; turn this feature off
  3105. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  3106. entries
  3107. (org-disable-agenda-to-diary t))
  3108. (save-excursion
  3109. (save-window-excursion
  3110. (funcall (if (fboundp 'diary-list-entries)
  3111. 'diary-list-entries 'list-diary-entries)
  3112. date 1)))
  3113. (if (not (get-buffer diary-fancy-buffer))
  3114. (setq entries nil)
  3115. (with-current-buffer diary-fancy-buffer
  3116. (setq buffer-read-only nil)
  3117. (if (zerop (buffer-size))
  3118. ;; No entries
  3119. (setq entries nil)
  3120. ;; Omit the date and other unnecessary stuff
  3121. (org-agenda-cleanup-fancy-diary)
  3122. ;; Add prefix to each line and extend the text properties
  3123. (if (zerop (buffer-size))
  3124. (setq entries nil)
  3125. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  3126. (set-buffer-modified-p nil)
  3127. (kill-buffer diary-fancy-buffer)))
  3128. (when entries
  3129. (setq entries (org-split-string entries "\n"))
  3130. (setq entries
  3131. (mapcar
  3132. (lambda (x)
  3133. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  3134. ;; Extend the text properties to the beginning of the line
  3135. (org-add-props x (text-properties-at (1- (length x)) x)
  3136. 'type "diary" 'date date))
  3137. entries)))))
  3138. (defvar org-agenda-cleanup-fancy-diary-hook nil
  3139. "Hook run when the fancy diary buffer is cleaned up.")
  3140. (defun org-agenda-cleanup-fancy-diary ()
  3141. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  3142. This gets rid of the date, the underline under the date, and
  3143. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  3144. date. It also removes lines that contain only whitespace."
  3145. (goto-char (point-min))
  3146. (if (looking-at ".*?:[ \t]*")
  3147. (progn
  3148. (replace-match "")
  3149. (re-search-forward "\n=+$" nil t)
  3150. (replace-match "")
  3151. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  3152. (re-search-forward "\n=+$" nil t)
  3153. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  3154. (goto-char (point-min))
  3155. (while (re-search-forward "^ +\n" nil t)
  3156. (replace-match ""))
  3157. (goto-char (point-min))
  3158. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  3159. (replace-match ""))
  3160. (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
  3161. ;; Make sure entries from the diary have the right text properties.
  3162. (eval-after-load "diary-lib"
  3163. '(if (boundp 'diary-modify-entry-list-string-function)
  3164. ;; We can rely on the hook, nothing to do
  3165. nil
  3166. ;; Hook not available, must use advice to make this work
  3167. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  3168. "Make the position visible."
  3169. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  3170. (stringp string)
  3171. buffer-file-name)
  3172. (setq string (org-modify-diary-entry-string string))))))
  3173. (defun org-modify-diary-entry-string (string)
  3174. "Add text properties to string, allowing org-mode to act on it."
  3175. (org-add-props string nil
  3176. 'mouse-face 'highlight
  3177. 'keymap org-agenda-keymap
  3178. 'help-echo (if buffer-file-name
  3179. (format "mouse-2 or RET jump to diary file %s"
  3180. (abbreviate-file-name buffer-file-name))
  3181. "")
  3182. 'org-agenda-diary-link t
  3183. 'org-marker (org-agenda-new-marker (point-at-bol))))
  3184. (defun org-diary-default-entry ()
  3185. "Add a dummy entry to the diary.
  3186. Needed to avoid empty dates which mess up holiday display."
  3187. ;; Catch the error if dealing with the new add-to-diary-alist
  3188. (when org-disable-agenda-to-diary
  3189. (condition-case nil
  3190. (org-add-to-diary-list original-date "Org-mode dummy" "")
  3191. (error
  3192. (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  3193. (defun org-add-to-diary-list (&rest args)
  3194. (if (fboundp 'diary-add-to-list)
  3195. (apply 'diary-add-to-list args)
  3196. (apply 'add-to-diary-list args)))
  3197. ;;;###autoload
  3198. (defun org-diary (&rest args)
  3199. "Return diary information from org-files.
  3200. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  3201. It accesses org files and extracts information from those files to be
  3202. listed in the diary. The function accepts arguments specifying what
  3203. items should be listed. The following arguments are allowed:
  3204. :timestamp List the headlines of items containing a date stamp or
  3205. date range matching the selected date. Deadlines will
  3206. also be listed, on the expiration day.
  3207. :sexp List entries resulting from diary-like sexps.
  3208. :deadline List any deadlines past due, or due within
  3209. `org-deadline-warning-days'. The listing occurs only
  3210. in the diary for *today*, not at any other date. If
  3211. an entry is marked DONE, it is no longer listed.
  3212. :scheduled List all items which are scheduled for the given date.
  3213. The diary for *today* also contains items which were
  3214. scheduled earlier and are not yet marked DONE.
  3215. :todo List all TODO items from the org-file. This may be a
  3216. long list - so this is not turned on by default.
  3217. Like deadlines, these entries only show up in the
  3218. diary for *today*, not at any other date.
  3219. The call in the diary file should look like this:
  3220. &%%(org-diary) ~/path/to/some/orgfile.org
  3221. Use a separate line for each org file to check. Or, if you omit the file name,
  3222. all files listed in `org-agenda-files' will be checked automatically:
  3223. &%%(org-diary)
  3224. If you don't give any arguments (as in the example above), the default
  3225. arguments (:deadline :scheduled :timestamp :sexp) are used.
  3226. So the example above may also be written as
  3227. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  3228. The function expects the lisp variables `entry' and `date' to be provided
  3229. by the caller, because this is how the calendar works. Don't use this
  3230. function from a program - use `org-agenda-get-day-entries' instead."
  3231. (when (> (- (time-to-seconds (current-time))
  3232. org-agenda-last-marker-time)
  3233. 5)
  3234. (org-agenda-reset-markers))
  3235. (org-compile-prefix-format 'agenda)
  3236. (org-set-sorting-strategy 'agenda)
  3237. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  3238. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  3239. (list entry)
  3240. (org-agenda-files t)))
  3241. file rtn results)
  3242. (org-prepare-agenda-buffers files)
  3243. ;; If this is called during org-agenda, don't return any entries to
  3244. ;; the calendar. Org Agenda will list these entries itself.
  3245. (if org-disable-agenda-to-diary (setq files nil))
  3246. (while (setq file (pop files))
  3247. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  3248. (setq results (append results rtn)))
  3249. (if results
  3250. (concat (org-finalize-agenda-entries results) "\n"))))
  3251. ;;; Agenda entry finders
  3252. (defun org-agenda-get-day-entries (file date &rest args)
  3253. "Does the work for `org-diary' and `org-agenda'.
  3254. FILE is the path to a file to be checked for entries. DATE is date like
  3255. the one returned by `calendar-current-date'. ARGS are symbols indicating
  3256. which kind of entries should be extracted. For details about these, see
  3257. the documentation of `org-diary'."
  3258. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  3259. (let* ((org-startup-folded nil)
  3260. (org-startup-align-all-tables nil)
  3261. (buffer (if (file-exists-p file)
  3262. (org-get-agenda-file-buffer file)
  3263. (error "No such file %s" file)))
  3264. arg results rtn)
  3265. (if (not buffer)
  3266. ;; If file does not exist, make sure an error message ends up in diary
  3267. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  3268. (with-current-buffer buffer
  3269. (unless (org-mode-p)
  3270. (error "Agenda file %s is not in `org-mode'" file))
  3271. (let ((case-fold-search nil))
  3272. (save-excursion
  3273. (save-restriction
  3274. (if org-agenda-restrict
  3275. (narrow-to-region org-agenda-restrict-begin
  3276. org-agenda-restrict-end)
  3277. (widen))
  3278. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  3279. (while (setq arg (pop args))
  3280. (cond
  3281. ((and (eq arg :todo)
  3282. (equal date (calendar-current-date)))
  3283. (setq rtn (org-agenda-get-todos))
  3284. (setq results (append results rtn)))
  3285. ((eq arg :timestamp)
  3286. (setq rtn (org-agenda-get-blocks))
  3287. (setq results (append results rtn))
  3288. (setq rtn (org-agenda-get-timestamps))
  3289. (setq results (append results rtn)))
  3290. ((eq arg :sexp)
  3291. (setq rtn (org-agenda-get-sexps))
  3292. (setq results (append results rtn)))
  3293. ((eq arg :scheduled)
  3294. (setq rtn (org-agenda-get-scheduled))
  3295. (setq results (append results rtn)))
  3296. ((eq arg :closed)
  3297. (setq rtn (org-agenda-get-progress))
  3298. (setq results (append results rtn)))
  3299. ((eq arg :deadline)
  3300. (setq rtn (org-agenda-get-deadlines))
  3301. (setq results (append results rtn))))))))
  3302. results))))
  3303. (defun org-agenda-get-todos ()
  3304. "Return the TODO information for agenda display."
  3305. (let* ((props (list 'face nil
  3306. 'done-face 'org-agenda-done
  3307. 'org-not-done-regexp org-not-done-regexp
  3308. 'org-todo-regexp org-todo-regexp
  3309. 'org-complex-heading-regexp org-complex-heading-regexp
  3310. 'mouse-face 'highlight
  3311. 'keymap org-agenda-keymap
  3312. 'help-echo
  3313. (format "mouse-2 or RET jump to org file %s"
  3314. (abbreviate-file-name buffer-file-name))))
  3315. (regexp (concat "^\\*+[ \t]+\\("
  3316. (if org-select-this-todo-keyword
  3317. (if (equal org-select-this-todo-keyword "*")
  3318. org-todo-regexp
  3319. (concat "\\<\\("
  3320. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  3321. "\\)\\>"))
  3322. org-not-done-regexp)
  3323. "[^\n\r]*\\)"))
  3324. marker priority category tags todo-state
  3325. ee txt beg end)
  3326. (goto-char (point-min))
  3327. (while (re-search-forward regexp nil t)
  3328. (catch :skip
  3329. (save-match-data
  3330. (beginning-of-line)
  3331. (setq beg (point) end (save-excursion (outline-next-heading) (point)))
  3332. (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
  3333. (goto-char (1+ beg))
  3334. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  3335. (throw :skip nil)))
  3336. (goto-char beg)
  3337. (org-agenda-skip)
  3338. (goto-char (match-beginning 1))
  3339. (setq marker (org-agenda-new-marker (match-beginning 0))
  3340. category (org-get-category)
  3341. txt (match-string 1)
  3342. tags (org-get-tags-at (point))
  3343. txt (org-format-agenda-item "" txt category tags)
  3344. priority (1+ (org-get-priority txt))
  3345. todo-state (org-get-todo-state))
  3346. (org-add-props txt props
  3347. 'org-marker marker 'org-hd-marker marker
  3348. 'priority priority 'org-category category
  3349. 'type "todo" 'todo-state todo-state)
  3350. (push txt ee)
  3351. (if org-agenda-todo-list-sublevels
  3352. (goto-char (match-end 1))
  3353. (org-end-of-subtree 'invisible))))
  3354. (nreverse ee)))
  3355. ;;;###autoload
  3356. (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
  3357. "Do we have a reason to ignore this todo entry because it has a time stamp?"
  3358. (when (or org-agenda-todo-ignore-with-date
  3359. org-agenda-todo-ignore-scheduled
  3360. org-agenda-todo-ignore-deadlines)
  3361. (setq end (or end (save-excursion (outline-next-heading) (point))))
  3362. (save-excursion
  3363. (or (and org-agenda-todo-ignore-with-date
  3364. (re-search-forward org-ts-regexp end t))
  3365. (and org-agenda-todo-ignore-scheduled
  3366. (re-search-forward org-scheduled-time-regexp end t))
  3367. (and org-agenda-todo-ignore-deadlines
  3368. (re-search-forward org-deadline-time-regexp end t)
  3369. (org-deadline-close (match-string 1)))))))
  3370. (defconst org-agenda-no-heading-message
  3371. "No heading for this item in buffer or region.")
  3372. (defun org-agenda-get-timestamps ()
  3373. "Return the date stamp information for agenda display."
  3374. (let* ((props (list 'face nil
  3375. 'org-not-done-regexp org-not-done-regexp
  3376. 'org-todo-regexp org-todo-regexp
  3377. 'org-complex-heading-regexp org-complex-heading-regexp
  3378. 'mouse-face 'highlight
  3379. 'keymap org-agenda-keymap
  3380. 'help-echo
  3381. (format "mouse-2 or RET jump to org file %s"
  3382. (abbreviate-file-name buffer-file-name))))
  3383. (d1 (calendar-absolute-from-gregorian date))
  3384. (remove-re
  3385. (concat
  3386. (regexp-quote
  3387. (format-time-string
  3388. "<%Y-%m-%d"
  3389. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3390. ".*?>"))
  3391. (regexp
  3392. (concat
  3393. (if org-agenda-include-inactive-timestamps "[[<]" "<")
  3394. (regexp-quote
  3395. (substring
  3396. (format-time-string
  3397. (car org-time-stamp-formats)
  3398. (apply 'encode-time ; DATE bound by calendar
  3399. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  3400. 1 11))
  3401. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  3402. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  3403. marker hdmarker deadlinep scheduledp clockp closedp inactivep
  3404. donep tmp priority category ee txt timestr tags b0 b3 e3 head
  3405. todo-state)
  3406. (goto-char (point-min))
  3407. (while (re-search-forward regexp nil t)
  3408. (setq b0 (match-beginning 0)
  3409. b3 (match-beginning 3) e3 (match-end 3))
  3410. (catch :skip
  3411. (and (org-at-date-range-p) (throw :skip nil))
  3412. (org-agenda-skip)
  3413. (if (and (match-end 1)
  3414. (not (= d1 (org-time-string-to-absolute
  3415. (match-string 1) d1 nil
  3416. org-agenda-repeating-timestamp-show-all))))
  3417. (throw :skip nil))
  3418. (if (and e3
  3419. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  3420. (throw :skip nil))
  3421. (setq marker (org-agenda-new-marker b0)
  3422. category (org-get-category b0)
  3423. tmp (buffer-substring (max (point-min)
  3424. (- b0 org-ds-keyword-length))
  3425. b0)
  3426. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  3427. inactivep (= (char-after b0) ?\[)
  3428. deadlinep (string-match org-deadline-regexp tmp)
  3429. scheduledp (string-match org-scheduled-regexp tmp)
  3430. closedp (and org-agenda-include-inactive-timestamps
  3431. (string-match org-closed-string tmp))
  3432. clockp (and org-agenda-include-inactive-timestamps
  3433. (or (string-match org-clock-string tmp)
  3434. (string-match "]-+\\'" tmp)))
  3435. todo-state (org-get-todo-state)
  3436. donep (member todo-state org-done-keywords))
  3437. (if (or scheduledp deadlinep closedp clockp)
  3438. (throw :skip t))
  3439. (if (string-match ">" timestr)
  3440. ;; substring should only run to end of time stamp
  3441. (setq timestr (substring timestr 0 (match-end 0))))
  3442. (save-excursion
  3443. (if (re-search-backward "^\\*+ " nil t)
  3444. (progn
  3445. (goto-char (match-beginning 0))
  3446. (setq hdmarker (org-agenda-new-marker)
  3447. tags (org-get-tags-at))
  3448. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  3449. (setq head (match-string 1))
  3450. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  3451. (setq txt (org-format-agenda-item
  3452. (if inactivep "[" nil)
  3453. head category tags timestr nil
  3454. remove-re)))
  3455. (setq txt org-agenda-no-heading-message))
  3456. (setq priority (org-get-priority txt))
  3457. (org-add-props txt props
  3458. 'org-marker marker 'org-hd-marker hdmarker)
  3459. (org-add-props txt nil 'priority priority
  3460. 'org-category category 'date date
  3461. 'todo-state todo-state
  3462. 'type "timestamp")
  3463. (push txt ee))
  3464. (outline-next-heading)))
  3465. (nreverse ee)))
  3466. (defun org-agenda-get-sexps ()
  3467. "Return the sexp information for agenda display."
  3468. (require 'diary-lib)
  3469. (let* ((props (list 'face nil
  3470. 'mouse-face 'highlight
  3471. 'keymap org-agenda-keymap
  3472. 'help-echo
  3473. (format "mouse-2 or RET jump to org file %s"
  3474. (abbreviate-file-name buffer-file-name))))
  3475. (regexp "^&?%%(")
  3476. marker category ee txt tags entry result beg b sexp sexp-entry
  3477. todo-state)
  3478. (goto-char (point-min))
  3479. (while (re-search-forward regexp nil t)
  3480. (catch :skip
  3481. (org-agenda-skip)
  3482. (setq beg (match-beginning 0))
  3483. (goto-char (1- (match-end 0)))
  3484. (setq b (point))
  3485. (forward-sexp 1)
  3486. (setq sexp (buffer-substring b (point)))
  3487. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  3488. (org-trim (match-string 1))
  3489. ""))
  3490. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  3491. (when result
  3492. (setq marker (org-agenda-new-marker beg)
  3493. category (org-get-category beg)
  3494. todo-state (org-get-todo-state))
  3495. (if (string-match "\\S-" result)
  3496. (setq txt result)
  3497. (setq txt "SEXP entry returned empty string"))
  3498. (setq txt (org-format-agenda-item
  3499. "" txt category tags 'time))
  3500. (org-add-props txt props 'org-marker marker)
  3501. (org-add-props txt nil
  3502. 'org-category category 'date date 'todo-state todo-state
  3503. 'type "sexp")
  3504. (push txt ee))))
  3505. (nreverse ee)))
  3506. (defalias 'org-get-closed 'org-agenda-get-progress)
  3507. (defun org-agenda-get-progress ()
  3508. "Return the logged TODO entries for agenda display."
  3509. (let* ((props (list 'mouse-face 'highlight
  3510. 'org-not-done-regexp org-not-done-regexp
  3511. 'org-todo-regexp org-todo-regexp
  3512. 'org-complex-heading-regexp org-complex-heading-regexp
  3513. 'keymap org-agenda-keymap
  3514. 'help-echo
  3515. (format "mouse-2 or RET jump to org file %s"
  3516. (abbreviate-file-name buffer-file-name))))
  3517. (items (if (consp org-agenda-show-log)
  3518. org-agenda-show-log
  3519. org-agenda-log-mode-items))
  3520. (parts
  3521. (delq nil
  3522. (list
  3523. (if (memq 'closed items) (concat "\\<" org-closed-string))
  3524. (if (memq 'clock items) (concat "\\<" org-clock-string))
  3525. (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
  3526. (parts-re (if parts (mapconcat 'identity parts "\\|")
  3527. (error "`org-agenda-log-mode-items' is empty")))
  3528. (regexp (concat
  3529. "\\(" parts-re "\\)"
  3530. " *\\["
  3531. (regexp-quote
  3532. (substring
  3533. (format-time-string
  3534. (car org-time-stamp-formats)
  3535. (apply 'encode-time ; DATE bound by calendar
  3536. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  3537. 1 11))))
  3538. marker hdmarker priority category tags closedp statep clockp state
  3539. ee txt extra timestr rest clocked)
  3540. (goto-char (point-min))
  3541. (while (re-search-forward regexp nil t)
  3542. (catch :skip
  3543. (org-agenda-skip)
  3544. (setq marker (org-agenda-new-marker (match-beginning 0))
  3545. closedp (equal (match-string 1) org-closed-string)
  3546. statep (equal (string-to-char (match-string 1)) ?-)
  3547. clockp (not (or closedp statep))
  3548. state (and statep (match-string 2))
  3549. category (org-get-category (match-beginning 0))
  3550. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  3551. ;; donep (org-entry-is-done-p)
  3552. )
  3553. (when (string-match "\\]" timestr)
  3554. ;; substring should only run to end of time stamp
  3555. (setq rest (substring timestr (match-end 0))
  3556. timestr (substring timestr 0 (match-end 0)))
  3557. (if (and (not closedp) (not statep)
  3558. (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
  3559. (progn (setq timestr (concat (substring timestr 0 -1)
  3560. "-" (match-string 1 rest) "]"))
  3561. (setq clocked (match-string 2 rest)))
  3562. (setq clocked "-")))
  3563. (save-excursion
  3564. (cond
  3565. ((not org-agenda-log-mode-add-notes) (setq extra nil))
  3566. (statep
  3567. (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
  3568. (setq extra (match-string 1))))
  3569. (clockp
  3570. (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
  3571. (setq extra (match-string 1))))
  3572. (t (setq extra nil)))
  3573. (if (re-search-backward "^\\*+ " nil t)
  3574. (progn
  3575. (goto-char (match-beginning 0))
  3576. (setq hdmarker (org-agenda-new-marker)
  3577. tags (org-get-tags-at))
  3578. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  3579. (setq txt (match-string 1))
  3580. (when extra
  3581. (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
  3582. (setq txt (concat (substring txt 0 (match-beginning 1))
  3583. " - " extra " " (match-string 2 txt)))
  3584. (setq txt (concat txt " - " extra))))
  3585. (setq txt (org-format-agenda-item
  3586. (cond
  3587. (closedp "Closed: ")
  3588. (statep (concat "State: (" state ")"))
  3589. (t (concat "Clocked: (" clocked ")")))
  3590. txt category tags timestr)))
  3591. (setq txt org-agenda-no-heading-message))
  3592. (setq priority 100000)
  3593. (org-add-props txt props
  3594. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
  3595. 'priority priority 'org-category category
  3596. 'type "closed" 'date date
  3597. 'undone-face 'org-warning 'done-face 'org-agenda-done)
  3598. (push txt ee))
  3599. (goto-char (point-at-eol))))
  3600. (nreverse ee)))
  3601. (defun org-agenda-get-deadlines ()
  3602. "Return the deadline information for agenda display."
  3603. (let* ((props (list 'mouse-face 'highlight
  3604. 'org-not-done-regexp org-not-done-regexp
  3605. 'org-todo-regexp org-todo-regexp
  3606. 'org-complex-heading-regexp org-complex-heading-regexp
  3607. 'keymap org-agenda-keymap
  3608. 'help-echo
  3609. (format "mouse-2 or RET jump to org file %s"
  3610. (abbreviate-file-name buffer-file-name))))
  3611. (regexp org-deadline-time-regexp)
  3612. (todayp (org-agenda-todayp date)) ; DATE bound by calendar
  3613. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  3614. d2 diff dfrac wdays pos pos1 category tags
  3615. ee txt head face s todo-state upcomingp donep timestr)
  3616. (goto-char (point-min))
  3617. (while (re-search-forward regexp nil t)
  3618. (catch :skip
  3619. (org-agenda-skip)
  3620. (setq s (match-string 1)
  3621. pos (1- (match-beginning 1))
  3622. d2 (org-time-string-to-absolute
  3623. (match-string 1) d1 'past
  3624. org-agenda-repeating-timestamp-show-all)
  3625. diff (- d2 d1)
  3626. wdays (org-get-wdays s)
  3627. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  3628. upcomingp (and todayp (> diff 0)))
  3629. ;; When to show a deadline in the calendar:
  3630. ;; If the expiration is within wdays warning time.
  3631. ;; Past-due deadlines are only shown on the current date
  3632. (if (or (and (<= diff wdays)
  3633. (and todayp (not org-agenda-only-exact-dates)))
  3634. (= diff 0))
  3635. (save-excursion
  3636. (setq category (org-get-category))
  3637. (setq todo-state (org-get-todo-state))
  3638. (if (re-search-backward "^\\*+[ \t]+" nil t)
  3639. (progn
  3640. (goto-char (match-end 0))
  3641. (setq pos1 (match-beginning 0))
  3642. (setq tags (org-get-tags-at pos1))
  3643. (setq head (buffer-substring-no-properties
  3644. (point)
  3645. (progn (skip-chars-forward "^\r\n")
  3646. (point))))
  3647. (setq donep (member todo-state org-done-keywords))
  3648. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  3649. (setq timestr
  3650. (concat (substring s (match-beginning 1)) " "))
  3651. (setq timestr 'time))
  3652. (if (and donep
  3653. (or org-agenda-skip-deadline-if-done
  3654. (not (= diff 0))))
  3655. (setq txt nil)
  3656. (setq txt (org-format-agenda-item
  3657. (if (= diff 0)
  3658. (car org-agenda-deadline-leaders)
  3659. (if (functionp (nth 1 org-agenda-deadline-leaders))
  3660. (funcall (nth 1 org-agenda-deadline-leaders) diff date)
  3661. (format (nth 1 org-agenda-deadline-leaders)
  3662. diff)))
  3663. head category tags
  3664. (if (not (= diff 0)) nil timestr)))))
  3665. (setq txt org-agenda-no-heading-message))
  3666. (when txt
  3667. (setq face (org-agenda-deadline-face dfrac wdays))
  3668. (org-add-props txt props
  3669. 'org-marker (org-agenda-new-marker pos)
  3670. 'org-hd-marker (org-agenda-new-marker pos1)
  3671. 'priority (+ (- diff)
  3672. (org-get-priority txt))
  3673. 'org-category category
  3674. 'todo-state todo-state
  3675. 'type (if upcomingp "upcoming-deadline" "deadline")
  3676. 'date (if upcomingp date d2)
  3677. 'face (if donep 'org-agenda-done face)
  3678. 'undone-face face 'done-face 'org-agenda-done)
  3679. (push txt ee))))))
  3680. (nreverse ee)))
  3681. (defun org-agenda-deadline-face (fraction &optional wdays)
  3682. "Return the face to displaying a deadline item.
  3683. FRACTION is what fraction of the head-warning time has passed."
  3684. (if (equal wdays 0) (setq fraction 1.))
  3685. (let ((faces org-agenda-deadline-faces) f)
  3686. (catch 'exit
  3687. (while (setq f (pop faces))
  3688. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  3689. (defun org-agenda-get-scheduled ()
  3690. "Return the scheduled information for agenda display."
  3691. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  3692. 'org-todo-regexp org-todo-regexp
  3693. 'org-complex-heading-regexp org-complex-heading-regexp
  3694. 'done-face 'org-agenda-done
  3695. 'mouse-face 'highlight
  3696. 'keymap org-agenda-keymap
  3697. 'help-echo
  3698. (format "mouse-2 or RET jump to org file %s"
  3699. (abbreviate-file-name buffer-file-name))))
  3700. (regexp org-scheduled-time-regexp)
  3701. (todayp (org-agenda-todayp date)) ; DATE bound by calendar
  3702. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  3703. d2 diff pos pos1 category tags donep
  3704. ee txt head pastschedp todo-state face timestr s)
  3705. (goto-char (point-min))
  3706. (while (re-search-forward regexp nil t)
  3707. (catch :skip
  3708. (org-agenda-skip)
  3709. (setq s (match-string 1)
  3710. pos (1- (match-beginning 1))
  3711. d2 (org-time-string-to-absolute
  3712. (match-string 1) d1 'past
  3713. org-agenda-repeating-timestamp-show-all)
  3714. diff (- d2 d1))
  3715. (setq pastschedp (and todayp (< diff 0)))
  3716. ;; When to show a scheduled item in the calendar:
  3717. ;; If it is on or past the date.
  3718. (if (or (and (< diff 0)
  3719. (< (abs diff) org-scheduled-past-days)
  3720. (and todayp (not org-agenda-only-exact-dates)))
  3721. (= diff 0))
  3722. (save-excursion
  3723. (setq category (org-get-category))
  3724. (setq todo-state (org-get-todo-state))
  3725. (if (re-search-backward "^\\*+[ \t]+" nil t)
  3726. (progn
  3727. (goto-char (match-end 0))
  3728. (setq pos1 (match-beginning 0))
  3729. (setq tags (org-get-tags-at))
  3730. (setq head (buffer-substring-no-properties
  3731. (point)
  3732. (progn (skip-chars-forward "^\r\n") (point))))
  3733. (setq donep (member todo-state org-done-keywords))
  3734. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  3735. (setq timestr
  3736. (concat (substring s (match-beginning 1)) " "))
  3737. (setq timestr 'time))
  3738. (if (and donep
  3739. (or org-agenda-skip-scheduled-if-done
  3740. (not (= diff 0))))
  3741. (setq txt nil)
  3742. (setq txt (org-format-agenda-item
  3743. (if (= diff 0)
  3744. (car org-agenda-scheduled-leaders)
  3745. (format (nth 1 org-agenda-scheduled-leaders)
  3746. (- 1 diff)))
  3747. head category tags
  3748. (if (not (= diff 0)) nil timestr)))))
  3749. (setq txt org-agenda-no-heading-message))
  3750. (when txt
  3751. (setq face
  3752. (cond
  3753. (pastschedp 'org-scheduled-previously)
  3754. (todayp 'org-scheduled-today)
  3755. (t 'org-scheduled)))
  3756. (org-add-props txt props
  3757. 'undone-face face
  3758. 'face (if donep 'org-agenda-done face)
  3759. 'org-marker (org-agenda-new-marker pos)
  3760. 'org-hd-marker (org-agenda-new-marker pos1)
  3761. 'type (if pastschedp "past-scheduled" "scheduled")
  3762. 'date (if pastschedp d2 date)
  3763. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  3764. 'org-category category
  3765. 'todo-state todo-state)
  3766. (push txt ee))))))
  3767. (nreverse ee)))
  3768. (defun org-agenda-get-blocks ()
  3769. "Return the date-range information for agenda display."
  3770. (let* ((props (list 'face nil
  3771. 'org-not-done-regexp org-not-done-regexp
  3772. 'org-todo-regexp org-todo-regexp
  3773. 'org-complex-heading-regexp org-complex-heading-regexp
  3774. 'mouse-face 'highlight
  3775. 'keymap org-agenda-keymap
  3776. 'help-echo
  3777. (format "mouse-2 or RET jump to org file %s"
  3778. (abbreviate-file-name buffer-file-name))))
  3779. (regexp org-tr-regexp)
  3780. (d0 (calendar-absolute-from-gregorian date))
  3781. marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
  3782. head)
  3783. (goto-char (point-min))
  3784. (while (re-search-forward regexp nil t)
  3785. (catch :skip
  3786. (org-agenda-skip)
  3787. (setq pos (point))
  3788. (setq timestr (match-string 0)
  3789. s1 (match-string 1)
  3790. s2 (match-string 2)
  3791. d1 (time-to-days (org-time-string-to-time s1))
  3792. d2 (time-to-days (org-time-string-to-time s2)))
  3793. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  3794. ;; Only allow days between the limits, because the normal
  3795. ;; date stamps will catch the limits.
  3796. (save-excursion
  3797. (setq marker (org-agenda-new-marker (point)))
  3798. (setq category (org-get-category))
  3799. (setq todo-state (org-get-todo-state))
  3800. (if (re-search-backward "^\\*+ " nil t)
  3801. (progn
  3802. (goto-char (match-beginning 0))
  3803. (setq hdmarker (org-agenda-new-marker (point)))
  3804. (setq tags (org-get-tags-at))
  3805. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  3806. (setq head (match-string 1))
  3807. (and org-agenda-skip-timestamp-if-done
  3808. (org-entry-is-done-p)
  3809. (throw :skip t))
  3810. (setq txt (org-format-agenda-item
  3811. (format
  3812. (nth (if (= d1 d2) 0 1)
  3813. org-agenda-timerange-leaders)
  3814. (1+ (- d0 d1)) (1+ (- d2 d1)))
  3815. head category tags
  3816. (if (= d0 d1) timestr))))
  3817. (setq txt org-agenda-no-heading-message))
  3818. (org-add-props txt props
  3819. 'org-marker marker 'org-hd-marker hdmarker
  3820. 'type "block" 'date date
  3821. 'todo-state todo-state
  3822. 'priority (org-get-priority txt) 'org-category category)
  3823. (push txt ee)))
  3824. (goto-char pos)))
  3825. ;; Sort the entries by expiration date.
  3826. (nreverse ee)))
  3827. ;;; Agenda presentation and sorting
  3828. (defvar org-prefix-has-time nil
  3829. "A flag, set by `org-compile-prefix-format'.
  3830. The flag is set if the currently compiled format contains a `%t'.")
  3831. (defvar org-prefix-has-tag nil
  3832. "A flag, set by `org-compile-prefix-format'.
  3833. The flag is set if the currently compiled format contains a `%T'.")
  3834. (defvar org-prefix-has-effort nil
  3835. "A flag, set by `org-compile-prefix-format'.
  3836. The flag is set if the currently compiled format contains a `%e'.")
  3837. (defun org-format-agenda-item (extra txt &optional category tags dotime
  3838. noprefix remove-re)
  3839. "Format TXT to be inserted into the agenda buffer.
  3840. In particular, it adds the prefix and corresponding text properties. EXTRA
  3841. must be a string and replaces the `%s' specifier in the prefix format.
  3842. CATEGORY (string, symbol or nil) may be used to overrule the default
  3843. category taken from local variable or file name. It will replace the `%c'
  3844. specifier in the format. DOTIME, when non-nil, indicates that a
  3845. time-of-day should be extracted from TXT for sorting of this entry, and for
  3846. the `%t' specifier in the format. When DOTIME is a string, this string is
  3847. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  3848. only the correctly processes TXT should be returned - this is used by
  3849. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  3850. Any match of REMOVE-RE will be removed from TXT."
  3851. (save-match-data
  3852. ;; Diary entries sometimes have extra whitespace at the beginning
  3853. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  3854. (when org-agenda-show-inherited-tags
  3855. ;; Fix the tags part in txt
  3856. (setq txt (org-agenda-add-inherited-tags txt tags)))
  3857. (let* ((category (or category
  3858. org-category
  3859. (if buffer-file-name
  3860. (file-name-sans-extension
  3861. (file-name-nondirectory buffer-file-name))
  3862. "")))
  3863. ;; time, tag, effort are needed for the eval of the prefix format
  3864. (tag (if tags (nth (1- (length tags)) tags) ""))
  3865. time effort neffort
  3866. (ts (if dotime (concat
  3867. (if (stringp dotime) dotime "")
  3868. (and org-agenda-search-headline-for-time txt))))
  3869. (time-of-day (and dotime (org-get-time-of-day ts)))
  3870. stamp plain s0 s1 s2 t1 t2 rtn srp
  3871. duration)
  3872. (and (org-mode-p) buffer-file-name
  3873. (add-to-list 'org-agenda-contributing-files buffer-file-name))
  3874. (when (and dotime time-of-day)
  3875. ;; Extract starting and ending time and move them to prefix
  3876. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  3877. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  3878. (setq s0 (match-string 0 ts)
  3879. srp (and stamp (match-end 3))
  3880. s1 (match-string (if plain 1 2) ts)
  3881. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  3882. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  3883. ;; them, we might want to remove them there to avoid duplication.
  3884. ;; The user can turn this off with a variable.
  3885. (if (and org-prefix-has-time
  3886. org-agenda-remove-times-when-in-prefix (or stamp plain)
  3887. (string-match (concat (regexp-quote s0) " *") txt)
  3888. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  3889. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  3890. (= (match-beginning 0) 0)
  3891. t))
  3892. (setq txt (replace-match "" nil nil txt))))
  3893. ;; Normalize the time(s) to 24 hour
  3894. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  3895. (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
  3896. ;; Compute the duration
  3897. (when s1
  3898. (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
  3899. (string-to-number (substring s1 3)))
  3900. t2 (cond
  3901. (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
  3902. (string-to-number (substring s2 3))))
  3903. (org-agenda-default-appointment-duration
  3904. (+ t1 org-agenda-default-appointment-duration))
  3905. (t nil)))
  3906. (setq duration (if t2 (- t2 t1)))))
  3907. (when (and s1 (not s2) org-agenda-default-appointment-duration
  3908. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  3909. (let ((m (+ (string-to-number (match-string 2 s1))
  3910. (* 60 (string-to-number (match-string 1 s1)))
  3911. org-agenda-default-appointment-duration))
  3912. h)
  3913. (setq h (/ m 60) m (- m (* h 60)))
  3914. (setq s2 (format "%02d:%02d" h m))))
  3915. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  3916. txt)
  3917. ;; Tags are in the string
  3918. (if (or (eq org-agenda-remove-tags t)
  3919. (and org-agenda-remove-tags
  3920. org-prefix-has-tag))
  3921. (setq txt (replace-match "" t t txt))
  3922. (setq txt (replace-match
  3923. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  3924. (match-string 2 txt))
  3925. t t txt))))
  3926. (when (org-mode-p)
  3927. (setq effort
  3928. (condition-case nil
  3929. (org-get-effort
  3930. (or (get-text-property 0 'org-hd-marker txt)
  3931. (get-text-property 0 'org-marker txt)))
  3932. (error nil)))
  3933. (when effort
  3934. (setq neffort (org-hh:mm-string-to-minutes effort)
  3935. effort (setq effort (concat "[" effort"]" )))))
  3936. (when remove-re
  3937. (while (string-match remove-re txt)
  3938. (setq txt (replace-match "" t t txt))))
  3939. ;; Create the final string
  3940. (if noprefix
  3941. (setq rtn txt)
  3942. ;; Prepare the variables needed in the eval of the compiled format
  3943. (setq time (cond (s2 (concat s1 "-" s2))
  3944. (s1 (concat s1 "......"))
  3945. (t ""))
  3946. extra (or extra "")
  3947. category (if (symbolp category) (symbol-name category) category))
  3948. ;; Evaluate the compiled format
  3949. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  3950. ;; And finally add the text properties
  3951. (org-add-props rtn nil
  3952. 'org-category (downcase category)
  3953. 'tags (mapcar 'org-downcase-keep-props tags)
  3954. 'org-highest-priority org-highest-priority
  3955. 'org-lowest-priority org-lowest-priority
  3956. 'prefix-length (- (length rtn) (length txt))
  3957. 'time-of-day time-of-day
  3958. 'duration duration
  3959. 'effort effort
  3960. 'effort-minutes neffort
  3961. 'txt txt
  3962. 'time time
  3963. 'extra extra
  3964. 'dotime dotime))))
  3965. (defun org-agenda-add-inherited-tags (txt tags)
  3966. "Remove tags string from TXT, and add complete list of tags.
  3967. The new list includes inherited tags. If any inherited tags are present,
  3968. a double colon separates inherited tags from local tags."
  3969. (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
  3970. (setq txt (substring txt 0 (match-beginning 0))))
  3971. (when tags
  3972. (let ((have-i (get-text-property 0 'inherited (car tags)))
  3973. i)
  3974. (setq txt (concat txt " :"
  3975. (mapconcat
  3976. (lambda (x)
  3977. (setq i (get-text-property 0 'inherited x))
  3978. (if (and have-i (not i))
  3979. (progn
  3980. (setq have-i nil)
  3981. (concat ":" x))
  3982. x))
  3983. tags ":")
  3984. (if have-i "::" ":")))))
  3985. txt)
  3986. (defun org-downcase-keep-props (s)
  3987. (let ((props (text-properties-at 0 s)))
  3988. (setq s (downcase s))
  3989. (add-text-properties 0 (length s) props s)
  3990. s))
  3991. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  3992. (defvar org-agenda-sorting-strategy-selected nil)
  3993. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  3994. (catch 'exit
  3995. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  3996. ((and todayp (member 'today (car org-agenda-time-grid))))
  3997. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  3998. ((member 'weekly (car org-agenda-time-grid)))
  3999. (t (throw 'exit list)))
  4000. (let* ((have (delq nil (mapcar
  4001. (lambda (x) (get-text-property 1 'time-of-day x))
  4002. list)))
  4003. (string (nth 1 org-agenda-time-grid))
  4004. (gridtimes (nth 2 org-agenda-time-grid))
  4005. (req (car org-agenda-time-grid))
  4006. (remove (member 'remove-match req))
  4007. new time)
  4008. (if (and (member 'require-timed req) (not have))
  4009. ;; don't show empty grid
  4010. (throw 'exit list))
  4011. (while (setq time (pop gridtimes))
  4012. (unless (and remove (member time have))
  4013. (setq time (int-to-string time))
  4014. (push (org-format-agenda-item
  4015. nil string "" nil
  4016. (concat (substring time 0 -2) ":" (substring time -2)))
  4017. new)
  4018. (put-text-property
  4019. 1 (length (car new)) 'face 'org-time-grid (car new))))
  4020. (if (member 'time-up org-agenda-sorting-strategy-selected)
  4021. (append new list)
  4022. (append list new)))))
  4023. (defun org-compile-prefix-format (key)
  4024. "Compile the prefix format into a Lisp form that can be evaluated.
  4025. The resulting form is returned and stored in the variable
  4026. `org-prefix-format-compiled'."
  4027. (setq org-prefix-has-time nil org-prefix-has-tag nil
  4028. org-prefix-has-effort nil)
  4029. (let ((s (cond
  4030. ((stringp org-agenda-prefix-format)
  4031. org-agenda-prefix-format)
  4032. ((assq key org-agenda-prefix-format)
  4033. (cdr (assq key org-agenda-prefix-format)))
  4034. (t " %-12:c%?-12t% s")))
  4035. (start 0)
  4036. varform vars var e c f opt)
  4037. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
  4038. s start)
  4039. (setq var (cdr (assoc (match-string 4 s)
  4040. '(("c" . category) ("t" . time) ("s" . extra)
  4041. ("T" . tag) ("e" . effort))))
  4042. c (or (match-string 3 s) "")
  4043. opt (match-beginning 1)
  4044. start (1+ (match-beginning 0)))
  4045. (if (equal var 'time) (setq org-prefix-has-time t))
  4046. (if (equal var 'tag) (setq org-prefix-has-tag t))
  4047. (if (equal var 'effort) (setq org-prefix-has-effort t))
  4048. (setq f (concat "%" (match-string 2 s) "s"))
  4049. (if opt
  4050. (setq varform
  4051. `(if (equal "" ,var)
  4052. ""
  4053. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  4054. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  4055. (setq s (replace-match "%s" t nil s))
  4056. (push varform vars))
  4057. (setq vars (nreverse vars))
  4058. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  4059. (defun org-set-sorting-strategy (key)
  4060. (if (symbolp (car org-agenda-sorting-strategy))
  4061. ;; the old format
  4062. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  4063. (setq org-agenda-sorting-strategy-selected
  4064. (or (cdr (assq key org-agenda-sorting-strategy))
  4065. (cdr (assq 'agenda org-agenda-sorting-strategy))
  4066. '(time-up category-keep priority-down)))))
  4067. (defun org-get-time-of-day (s &optional string mod24)
  4068. "Check string S for a time of day.
  4069. If found, return it as a military time number between 0 and 2400.
  4070. If not found, return nil.
  4071. The optional STRING argument forces conversion into a 5 character wide string
  4072. HH:MM."
  4073. (save-match-data
  4074. (when
  4075. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  4076. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  4077. (let* ((h (string-to-number (match-string 1 s)))
  4078. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  4079. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  4080. (am-p (equal ampm "am"))
  4081. (h1 (cond ((not ampm) h)
  4082. ((= h 12) (if am-p 0 12))
  4083. (t (+ h (if am-p 0 12)))))
  4084. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  4085. (mod h1 24) h1))
  4086. (t0 (+ (* 100 h2) m))
  4087. (t1 (concat (if (>= h1 24) "+" " ")
  4088. (if (< t0 100) "0" "")
  4089. (if (< t0 10) "0" "")
  4090. (int-to-string t0))))
  4091. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  4092. (defun org-finalize-agenda-entries (list &optional nosort)
  4093. "Sort and concatenate the agenda items."
  4094. (setq list (mapcar 'org-agenda-highlight-todo list))
  4095. (if nosort
  4096. list
  4097. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  4098. (defun org-agenda-highlight-todo (x)
  4099. (let ((org-done-keywords org-done-keywords-for-agenda)
  4100. re pl)
  4101. (if (eq x 'line)
  4102. (save-excursion
  4103. (beginning-of-line 1)
  4104. (setq re (get-text-property (point) 'org-todo-regexp))
  4105. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  4106. (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
  4107. (add-text-properties (match-beginning 0) (match-end 0)
  4108. (list 'face (org-get-todo-face 1)))
  4109. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  4110. (delete-region (match-beginning 1) (1- (match-end 0)))
  4111. (goto-char (match-beginning 1))
  4112. (insert (format org-agenda-todo-keyword-format s)))))
  4113. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  4114. pl (get-text-property 0 'prefix-length x))
  4115. (when (and re
  4116. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  4117. x (or pl 0)) pl))
  4118. (add-text-properties
  4119. (or (match-end 1) (match-end 0)) (match-end 0)
  4120. (list 'face (org-get-todo-face (match-string 2 x)))
  4121. x)
  4122. (setq x (concat (substring x 0 (match-end 1))
  4123. (format org-agenda-todo-keyword-format
  4124. (match-string 2 x))
  4125. " "
  4126. (substring x (match-end 3)))))
  4127. x)))
  4128. (defsubst org-cmp-priority (a b)
  4129. "Compare the priorities of string A and B."
  4130. (let ((pa (or (get-text-property 1 'priority a) 0))
  4131. (pb (or (get-text-property 1 'priority b) 0)))
  4132. (cond ((> pa pb) +1)
  4133. ((< pa pb) -1)
  4134. (t nil))))
  4135. (defsubst org-cmp-effort (a b)
  4136. "Compare the priorities of string A and B."
  4137. (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
  4138. (ea (or (get-text-property 1 'effort-minutes a) def))
  4139. (eb (or (get-text-property 1 'effort-minutes b) def)))
  4140. (cond ((> ea eb) +1)
  4141. ((< ea eb) -1)
  4142. (t nil))))
  4143. (defsubst org-cmp-category (a b)
  4144. "Compare the string values of categories of strings A and B."
  4145. (let ((ca (or (get-text-property 1 'org-category a) ""))
  4146. (cb (or (get-text-property 1 'org-category b) "")))
  4147. (cond ((string-lessp ca cb) -1)
  4148. ((string-lessp cb ca) +1)
  4149. (t nil))))
  4150. (defsubst org-cmp-todo-state (a b)
  4151. "Compare the todo states of strings A and B."
  4152. (let* ((ma (or (get-text-property 1 'org-marker a)
  4153. (get-text-property 1 'org-hd-marker a)))
  4154. (mb (or (get-text-property 1 'org-marker b)
  4155. (get-text-property 1 'org-hd-marker b)))
  4156. (fa (and ma (marker-buffer ma)))
  4157. (fb (and mb (marker-buffer mb)))
  4158. (todo-kwds
  4159. (or (and fa (with-current-buffer fa org-todo-keywords-1))
  4160. (and fb (with-current-buffer fb org-todo-keywords-1))))
  4161. (ta (or (get-text-property 1 'todo-state a) ""))
  4162. (tb (or (get-text-property 1 'todo-state b) ""))
  4163. (la (- (length (member ta todo-kwds))))
  4164. (lb (- (length (member tb todo-kwds))))
  4165. (donepa (member ta org-done-keywords-for-agenda))
  4166. (donepb (member tb org-done-keywords-for-agenda)))
  4167. (cond ((and donepa (not donepb)) -1)
  4168. ((and (not donepa) donepb) +1)
  4169. ((< la lb) -1)
  4170. ((< lb la) +1)
  4171. (t nil))))
  4172. (defsubst org-cmp-tag (a b)
  4173. "Compare the string values of the first tags of A and B."
  4174. (let ((ta (car (last (get-text-property 1 'tags a))))
  4175. (tb (car (last (get-text-property 1 'tags b)))))
  4176. (cond ((not ta) +1)
  4177. ((not tb) -1)
  4178. ((string-lessp ta tb) -1)
  4179. ((string-lessp tb ta) +1)
  4180. (t nil))))
  4181. (defsubst org-cmp-time (a b)
  4182. "Compare the time-of-day values of strings A and B."
  4183. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  4184. (ta (or (get-text-property 1 'time-of-day a) def))
  4185. (tb (or (get-text-property 1 'time-of-day b) def)))
  4186. (cond ((< ta tb) -1)
  4187. ((< tb ta) +1)
  4188. (t nil))))
  4189. (defun org-entries-lessp (a b)
  4190. "Predicate for sorting agenda entries."
  4191. ;; The following variables will be used when the form is evaluated.
  4192. ;; So even though the compiler complains, keep them.
  4193. (let* ((time-up (org-cmp-time a b))
  4194. (time-down (if time-up (- time-up) nil))
  4195. (priority-up (org-cmp-priority a b))
  4196. (priority-down (if priority-up (- priority-up) nil))
  4197. (effort-up (org-cmp-effort a b))
  4198. (effort-down (if effort-up (- effort-up) nil))
  4199. (category-up (org-cmp-category a b))
  4200. (category-down (if category-up (- category-up) nil))
  4201. (category-keep (if category-up +1 nil))
  4202. (tag-up (org-cmp-tag a b))
  4203. (tag-down (if tag-up (- tag-up) nil))
  4204. (todo-state-up (org-cmp-todo-state a b))
  4205. (todo-state-down (if todo-state-up (- todo-state-up) nil))
  4206. user-defined-up user-defined-down)
  4207. (if (and org-agenda-cmp-user-defined
  4208. (functionp org-agenda-cmp-user-defined))
  4209. (setq user-defined-up
  4210. (funcall org-agenda-cmp-user-defined a b)
  4211. user-defined-down (if user-defined-up (- user-defined-up) nil)))
  4212. (cdr (assoc
  4213. (eval (cons 'or org-agenda-sorting-strategy-selected))
  4214. '((-1 . t) (1 . nil) (nil . nil))))))
  4215. ;;; Agenda restriction lock
  4216. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  4217. "Overlay to mark the headline to which arenda commands are restricted.")
  4218. (org-overlay-put org-agenda-restriction-lock-overlay
  4219. 'face 'org-agenda-restriction-lock)
  4220. (org-overlay-put org-agenda-restriction-lock-overlay
  4221. 'help-echo "Agendas are currently limited to this subtree.")
  4222. (org-detach-overlay org-agenda-restriction-lock-overlay)
  4223. (defun org-agenda-set-restriction-lock (&optional type)
  4224. "Set restriction lock for agenda, to current subtree or file.
  4225. Restriction will be the file if TYPE is `file', or if type is the
  4226. universal prefix '(4), or if the cursor is before the first headline
  4227. in the file. Otherwise, restriction will be to the current subtree."
  4228. (interactive "P")
  4229. (and (equal type '(4)) (setq type 'file))
  4230. (setq type (cond
  4231. (type type)
  4232. ((org-at-heading-p) 'subtree)
  4233. ((condition-case nil (org-back-to-heading t) (error nil))
  4234. 'subtree)
  4235. (t 'file)))
  4236. (if (eq type 'subtree)
  4237. (progn
  4238. (setq org-agenda-restrict t)
  4239. (setq org-agenda-overriding-restriction 'subtree)
  4240. (put 'org-agenda-files 'org-restrict
  4241. (list (buffer-file-name (buffer-base-buffer))))
  4242. (org-back-to-heading t)
  4243. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  4244. (move-marker org-agenda-restrict-begin (point))
  4245. (move-marker org-agenda-restrict-end
  4246. (save-excursion (org-end-of-subtree t)))
  4247. (message "Locking agenda restriction to subtree"))
  4248. (put 'org-agenda-files 'org-restrict
  4249. (list (buffer-file-name (buffer-base-buffer))))
  4250. (setq org-agenda-restrict nil)
  4251. (setq org-agenda-overriding-restriction 'file)
  4252. (move-marker org-agenda-restrict-begin nil)
  4253. (move-marker org-agenda-restrict-end nil)
  4254. (message "Locking agenda restriction to file"))
  4255. (setq current-prefix-arg nil)
  4256. (org-agenda-maybe-redo))
  4257. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  4258. "Remove the agenda restriction lock."
  4259. (interactive "P")
  4260. (org-detach-overlay org-agenda-restriction-lock-overlay)
  4261. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  4262. (setq org-agenda-overriding-restriction nil)
  4263. (setq org-agenda-restrict nil)
  4264. (put 'org-agenda-files 'org-restrict nil)
  4265. (move-marker org-agenda-restrict-begin nil)
  4266. (move-marker org-agenda-restrict-end nil)
  4267. (setq current-prefix-arg nil)
  4268. (message "Agenda restriction lock removed")
  4269. (or noupdate (org-agenda-maybe-redo)))
  4270. (defun org-agenda-maybe-redo ()
  4271. "If there is any window showing the agenda view, update it."
  4272. (let ((w (get-buffer-window org-agenda-buffer-name t))
  4273. (w0 (selected-window)))
  4274. (when w
  4275. (select-window w)
  4276. (org-agenda-redo)
  4277. (select-window w0)
  4278. (if org-agenda-overriding-restriction
  4279. (message "Agenda view shifted to new %s restriction"
  4280. org-agenda-overriding-restriction)
  4281. (message "Agenda restriction lock removed")))))
  4282. ;;; Agenda commands
  4283. (defun org-agenda-check-type (error &rest types)
  4284. "Check if agenda buffer is of allowed type.
  4285. If ERROR is non-nil, throw an error, otherwise just return nil."
  4286. (if (memq org-agenda-type types)
  4287. t
  4288. (if error
  4289. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  4290. nil)))
  4291. (defun org-agenda-quit ()
  4292. "Exit agenda by removing the window or the buffer."
  4293. (interactive)
  4294. (if org-agenda-columns-active
  4295. (org-columns-quit)
  4296. (let ((buf (current-buffer)))
  4297. (and (not (eq org-agenda-window-setup 'current-window))
  4298. (not (one-window-p))
  4299. (delete-window))
  4300. (kill-buffer buf)
  4301. (org-agenda-reset-markers)
  4302. (org-columns-remove-overlays)
  4303. (setq org-agenda-archives-mode nil))
  4304. ;; Maybe restore the pre-agenda window configuration.
  4305. (and org-agenda-restore-windows-after-quit
  4306. (not (eq org-agenda-window-setup 'other-frame))
  4307. org-pre-agenda-window-conf
  4308. (set-window-configuration org-pre-agenda-window-conf))))
  4309. (defun org-agenda-exit ()
  4310. "Exit agenda by removing the window or the buffer.
  4311. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  4312. Org-mode buffers visited directly by the user will not be touched."
  4313. (interactive)
  4314. (org-release-buffers org-agenda-new-buffers)
  4315. (setq org-agenda-new-buffers nil)
  4316. (org-agenda-quit))
  4317. (defun org-agenda-execute (arg)
  4318. "Execute another agenda command, keeping same window.\\<global-map>
  4319. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  4320. (interactive "P")
  4321. (let ((org-agenda-window-setup 'current-window))
  4322. (org-agenda arg)))
  4323. (defun org-agenda-redo ()
  4324. "Rebuild Agenda.
  4325. When this is the global TODO list, a prefix argument will be interpreted."
  4326. (interactive)
  4327. (let* ((org-agenda-keep-modes t)
  4328. (filter org-agenda-filter)
  4329. (preset (get 'org-agenda-filter :preset-filter))
  4330. (cols org-agenda-columns-active)
  4331. (line (org-current-line))
  4332. (window-line (- line (org-current-line (window-start))))
  4333. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  4334. (put 'org-agenda-filter :preset-filter nil)
  4335. (and cols (org-columns-quit))
  4336. (message "Rebuilding agenda buffer...")
  4337. (org-let lprops '(eval org-agenda-redo-command))
  4338. (setq org-agenda-undo-list nil
  4339. org-agenda-pending-undo-list nil)
  4340. (message "Rebuilding agenda buffer...done")
  4341. (put 'org-agenda-filter :preset-filter preset)
  4342. (and (or filter preset) (org-agenda-filter-apply filter))
  4343. (and cols (interactive-p) (org-agenda-columns))
  4344. (goto-line line)
  4345. (recenter window-line)))
  4346. (defvar org-global-tags-completion-table nil)
  4347. (defvar org-agenda-filter-form nil)
  4348. (defun org-agenda-filter-by-tag (strip &optional char narrow)
  4349. "Keep only those lines in the agenda buffer that have a specific tag.
  4350. The tag is selected with its fast selection letter, as configured.
  4351. With prefix argument STRIP, remove all lines that do have the tag.
  4352. A lisp caller can specify CHAR. NARROW means that the new tag should be
  4353. used to narrow the search - the interactive user can also press `-' or `+'
  4354. to switch to narrowing."
  4355. (interactive "P")
  4356. (let* ((alist org-tag-alist-for-agenda)
  4357. (tag-chars (mapconcat
  4358. (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
  4359. alist ""))
  4360. (efforts (org-split-string
  4361. (or (cdr (assoc (concat org-effort-property "_ALL")
  4362. org-global-properties))
  4363. "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
  4364. (effort-op org-agenda-filter-effort-default-operator)
  4365. (effort-prompt "")
  4366. (inhibit-read-only t)
  4367. (current org-agenda-filter)
  4368. char a n tag)
  4369. (unless char
  4370. (message
  4371. "%s by tag [%s ], [TAB], [/]:off, [+-]:narrow, [>=<?]:effort: "
  4372. (if narrow "Narrow" "Filter") tag-chars)
  4373. (setq char (read-char)))
  4374. (when (member char '(?+ ?-))
  4375. ;; Narrowing down
  4376. (cond ((equal char ?-) (setq strip t narrow t))
  4377. ((equal char ?+) (setq strip nil narrow t)))
  4378. (message
  4379. "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
  4380. (setq char (read-char)))
  4381. (when (member char '(?< ?> ?= ??))
  4382. ;; An effort operator
  4383. (setq effort-op (char-to-string char))
  4384. (setq alist nil) ; to make sure it will be interpreted as effort.
  4385. (unless (equal char ??)
  4386. (loop for i from 0 to 9 do
  4387. (setq effort-prompt
  4388. (concat
  4389. effort-prompt " ["
  4390. (if (= i 9) "0" (int-to-string (1+ i)))
  4391. "]" (nth i efforts))))
  4392. (message "Effort%s: %s " effort-op effort-prompt)
  4393. (setq char (read-char))
  4394. (when (or (< char ?0) (> char ?9))
  4395. (error "Need 1-9,0 to select effort" ))))
  4396. (when (equal char ?\t)
  4397. (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
  4398. (org-set-local 'org-global-tags-completion-table
  4399. (org-global-tags-completion-table)))
  4400. (let ((completion-ignore-case t))
  4401. (setq tag (org-ido-completing-read
  4402. "Tag: " org-global-tags-completion-table))))
  4403. (cond
  4404. ((equal char ?/)
  4405. (org-agenda-filter-by-tag-show-all)
  4406. (when (get 'org-agenda-filter :preset-filter)
  4407. (org-agenda-filter-apply org-agenda-filter)))
  4408. ((or (equal char ?\ )
  4409. (setq a (rassoc char alist))
  4410. (and (>= char ?0) (<= char ?9)
  4411. (setq n (if (= char ?0) 9 (- char ?0 1))
  4412. tag (concat effort-op (nth n efforts))
  4413. a (cons tag nil)))
  4414. (and (= char ??)
  4415. (setq tag "?eff")
  4416. a (cons tag nil))
  4417. (and tag (setq a (cons tag nil))))
  4418. (org-agenda-filter-by-tag-show-all)
  4419. (setq tag (car a))
  4420. (setq org-agenda-filter
  4421. (cons (concat (if strip "-" "+") tag)
  4422. (if narrow current nil)))
  4423. (org-agenda-filter-apply org-agenda-filter))
  4424. (t (error "Invalid tag selection character %c" char)))))
  4425. (defun org-agenda-filter-by-tag-refine (strip &optional char)
  4426. "Refine the current filter. See `org-agenda-filter-by-tag."
  4427. (interactive "P")
  4428. (org-agenda-filter-by-tag strip char 'refine))
  4429. (defun org-agenda-filter-make-matcher ()
  4430. "Create the form that tests a line for the agenda filter."
  4431. (let (f f1)
  4432. (dolist (x (append (get 'org-agenda-filter :preset-filter)
  4433. org-agenda-filter))
  4434. (if (member x '("-" "+"))
  4435. (setq f1 '(not tags))
  4436. (if (string-match "[<=>?]" x)
  4437. (setq f1 (org-agenda-filter-effort-form x))
  4438. (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
  4439. (if (equal (string-to-char x) ?-)
  4440. (setq f1 (list 'not f1))))
  4441. (push f1 f))
  4442. (cons 'and (nreverse f))))
  4443. (defun org-agenda-filter-effort-form (e)
  4444. "Return the form to compare the effort of the current line with what E says.
  4445. E looks line \"+<2:25\"."
  4446. (let (op)
  4447. (setq e (substring e 1))
  4448. (setq op (string-to-char e) e (substring e 1))
  4449. (setq op (cond ((equal op ?<) '<=)
  4450. ((equal op ?>) '>=)
  4451. ((equal op ??) op)
  4452. (t '=)))
  4453. (list 'org-agenda-compare-effort (list 'quote op)
  4454. (org-hh:mm-string-to-minutes e))))
  4455. (defun org-agenda-compare-effort (op value)
  4456. "Compare the effort of the current line with VALUE, using OP.
  4457. If the line does not have an effort defined, return nil."
  4458. (let ((eff (get-text-property (point) 'effort-minutes)))
  4459. (if (equal op ??)
  4460. (not eff)
  4461. (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
  4462. value))))
  4463. (defun org-agenda-filter-apply (filter)
  4464. "Set FILTER as the new agenda filter and apply it."
  4465. (let (tags)
  4466. (setq org-agenda-filter filter
  4467. org-agenda-filter-form (org-agenda-filter-make-matcher))
  4468. (org-agenda-set-mode-name)
  4469. (save-excursion
  4470. (goto-char (point-min))
  4471. (while (not (eobp))
  4472. (if (get-text-property (point) 'org-marker)
  4473. (progn
  4474. (setq tags (get-text-property (point) 'tags)) ; used in eval
  4475. (if (not (eval org-agenda-filter-form))
  4476. (org-agenda-filter-by-tag-hide-line))
  4477. (beginning-of-line 2))
  4478. (beginning-of-line 2))))))
  4479. (defun org-agenda-filter-by-tag-hide-line ()
  4480. (let (ov)
  4481. (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
  4482. (point-at-eol)))
  4483. (org-overlay-put ov 'invisible t)
  4484. (org-overlay-put ov 'type 'tags-filter)
  4485. (push ov org-agenda-filter-overlays)))
  4486. (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
  4487. (setq pos (or pos (point)))
  4488. (save-excursion
  4489. (dolist (ov (org-overlays-at pos))
  4490. (when (and (org-overlay-get ov 'invisible)
  4491. (eq (org-overlay-get ov 'type) 'tags-filter))
  4492. (goto-char pos)
  4493. (if (< (org-overlay-start ov) (point-at-eol))
  4494. (org-move-overlay ov (point-at-eol)
  4495. (org-overlay-end ov)))))))
  4496. (defun org-agenda-filter-by-tag-show-all ()
  4497. (mapc 'org-delete-overlay org-agenda-filter-overlays)
  4498. (setq org-agenda-filter-overlays nil)
  4499. (setq org-agenda-filter nil)
  4500. (setq org-agenda-filter-form nil)
  4501. (org-agenda-set-mode-name))
  4502. (defun org-agenda-manipulate-query-add ()
  4503. "Manipulate the query by adding a search term with positive selection.
  4504. Positive selection means, the term must be matched for selection of an entry."
  4505. (interactive)
  4506. (org-agenda-manipulate-query ?\[))
  4507. (defun org-agenda-manipulate-query-subtract ()
  4508. "Manipulate the query by adding a search term with negative selection.
  4509. Negative selection means, term must not be matched for selection of an entry."
  4510. (interactive)
  4511. (org-agenda-manipulate-query ?\]))
  4512. (defun org-agenda-manipulate-query-add-re ()
  4513. "Manipulate the query by adding a search regexp with positive selection.
  4514. Positive selection means, the regexp must match for selection of an entry."
  4515. (interactive)
  4516. (org-agenda-manipulate-query ?\{))
  4517. (defun org-agenda-manipulate-query-subtract-re ()
  4518. "Manipulate the query by adding a search regexp with negative selection.
  4519. Negative selection means, regexp must not match for selection of an entry."
  4520. (interactive)
  4521. (org-agenda-manipulate-query ?\}))
  4522. (defun org-agenda-manipulate-query (char)
  4523. (cond
  4524. ((memq org-agenda-type '(timeline agenda))
  4525. (if (y-or-n-p "Re-display with inactive time stamps included? ")
  4526. (let ((org-agenda-include-inactive-timestamps t))
  4527. (org-agenda-redo))
  4528. (error "Abort")))
  4529. ((eq org-agenda-type 'search)
  4530. (org-add-to-string
  4531. 'org-agenda-query-string
  4532. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  4533. (?\{ . " +{}") (?\} . " -{}")))))
  4534. (setq org-agenda-redo-command
  4535. (list 'org-search-view
  4536. org-todo-only
  4537. org-agenda-query-string
  4538. (+ (length org-agenda-query-string)
  4539. (if (member char '(?\{ ?\})) 0 1))))
  4540. (set-register org-agenda-query-register org-agenda-query-string)
  4541. (org-agenda-redo))
  4542. (t (error "Cannot manipulate query for %s-type agenda buffers"
  4543. org-agenda-type))))
  4544. (defun org-add-to-string (var string)
  4545. (set var (concat (symbol-value var) string)))
  4546. (defun org-agenda-goto-date (date)
  4547. "Jump to DATE in agenda."
  4548. (interactive (list (org-read-date)))
  4549. (org-agenda-list nil date))
  4550. (defun org-agenda-goto-today ()
  4551. "Go to today."
  4552. (interactive)
  4553. (org-agenda-check-type t 'timeline 'agenda)
  4554. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  4555. (cond
  4556. (tdpos (goto-char tdpos))
  4557. ((eq org-agenda-type 'agenda)
  4558. (let* ((sd (time-to-days
  4559. (time-subtract (current-time)
  4560. (list 0 (* 3600 org-extend-today-until) 0))))
  4561. (comp (org-agenda-compute-time-span sd org-agenda-span))
  4562. (org-agenda-overriding-arguments org-agenda-last-arguments))
  4563. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  4564. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  4565. (org-agenda-redo)
  4566. (org-agenda-find-same-or-today-or-agenda)))
  4567. (t (error "Cannot find today")))))
  4568. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  4569. (goto-char
  4570. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  4571. (text-property-any (point-min) (point-max) 'org-today t)
  4572. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  4573. (point-min))))
  4574. (defun org-agenda-later (arg)
  4575. "Go forward in time by thee current span.
  4576. With prefix ARG, go forward that many times the current span."
  4577. (interactive "p")
  4578. (org-agenda-check-type t 'agenda)
  4579. (let* ((span org-agenda-span)
  4580. (sd org-starting-day)
  4581. (greg (calendar-gregorian-from-absolute sd))
  4582. (cnt (get-text-property (point) 'org-day-cnt))
  4583. greg2 nd)
  4584. (cond
  4585. ((eq span 'day)
  4586. (setq sd (+ arg sd) nd 1))
  4587. ((eq span 'week)
  4588. (setq sd (+ (* 7 arg) sd) nd 7))
  4589. ((eq span 'month)
  4590. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  4591. sd (calendar-absolute-from-gregorian greg2))
  4592. (setcar greg2 (1+ (car greg2)))
  4593. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  4594. ((eq span 'year)
  4595. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  4596. sd (calendar-absolute-from-gregorian greg2))
  4597. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  4598. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  4599. (let ((org-agenda-overriding-arguments
  4600. (list (car org-agenda-last-arguments) sd nd t)))
  4601. (org-agenda-redo)
  4602. (org-agenda-find-same-or-today-or-agenda cnt))))
  4603. (defun org-agenda-earlier (arg)
  4604. "Go backward in time by the current span.
  4605. With prefix ARG, go backward that many times the current span."
  4606. (interactive "p")
  4607. (org-agenda-later (- arg)))
  4608. (defun org-agenda-day-view (&optional day-of-year)
  4609. "Switch to daily view for agenda.
  4610. With argument DAY-OF-YEAR, switch to that day of the year."
  4611. (interactive "P")
  4612. (setq org-agenda-ndays 1)
  4613. (org-agenda-change-time-span 'day day-of-year))
  4614. (defun org-agenda-week-view (&optional iso-week)
  4615. "Switch to daily view for agenda.
  4616. With argument ISO-WEEK, switch to the corresponding ISO week.
  4617. If ISO-WEEK has more then 2 digits, only the last two encode the
  4618. week. Any digits before this encode a year. So 200712 means
  4619. week 12 of year 2007. Years in the range 1938-2037 can also be
  4620. written as 2-digit years."
  4621. (interactive "P")
  4622. (setq org-agenda-ndays 7)
  4623. (org-agenda-change-time-span 'week iso-week))
  4624. (defun org-agenda-month-view (&optional month)
  4625. "Switch to monthly view for agenda.
  4626. With argument MONTH, switch to that month."
  4627. (interactive "P")
  4628. (org-agenda-change-time-span 'month month))
  4629. (defun org-agenda-year-view (&optional year)
  4630. "Switch to yearly view for agenda.
  4631. With argument YEAR, switch to that year.
  4632. If MONTH has more then 2 digits, only the last two encode the
  4633. month. Any digits before this encode a year. So 200712 means
  4634. December year 2007. Years in the range 1938-2037 can also be
  4635. written as 2-digit years."
  4636. (interactive "P")
  4637. (when year
  4638. (setq year (org-small-year-to-year year)))
  4639. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  4640. (org-agenda-change-time-span 'year year)
  4641. (error "Abort")))
  4642. (defun org-agenda-change-time-span (span &optional n)
  4643. "Change the agenda view to SPAN.
  4644. SPAN may be `day', `week', `month', `year'."
  4645. (org-agenda-check-type t 'agenda)
  4646. (if (and (not n) (equal org-agenda-span span))
  4647. (error "Viewing span is already \"%s\"" span))
  4648. (let* ((sd (or (get-text-property (point) 'day)
  4649. org-starting-day))
  4650. (computed (org-agenda-compute-time-span sd span n))
  4651. (org-agenda-overriding-arguments
  4652. (list (car org-agenda-last-arguments)
  4653. (car computed) (cdr computed) t)))
  4654. (org-agenda-redo)
  4655. (org-agenda-find-same-or-today-or-agenda))
  4656. (org-agenda-set-mode-name)
  4657. (message "Switched to %s view" span))
  4658. (defun org-agenda-compute-time-span (sd span &optional n)
  4659. "Compute starting date and number of days for agenda.
  4660. SPAN may be `day', `week', `month', `year'. The return value
  4661. is a cons cell with the starting date and the number of days,
  4662. so that the date SD will be in that range."
  4663. (let* ((greg (calendar-gregorian-from-absolute sd))
  4664. (dg (nth 1 greg))
  4665. (mg (car greg))
  4666. (yg (nth 2 greg))
  4667. nd w1 y1 m1 thisweek)
  4668. (cond
  4669. ((eq span 'day)
  4670. (when n
  4671. (setq sd (+ (calendar-absolute-from-gregorian
  4672. (list mg 1 yg))
  4673. n -1)))
  4674. (setq nd 1))
  4675. ((eq span 'week)
  4676. (let* ((nt (calendar-day-of-week
  4677. (calendar-gregorian-from-absolute sd)))
  4678. (d (if org-agenda-start-on-weekday
  4679. (- nt org-agenda-start-on-weekday)
  4680. 0)))
  4681. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  4682. (when n
  4683. (require 'cal-iso)
  4684. (setq thisweek (car (calendar-iso-from-absolute sd)))
  4685. (when (> n 99)
  4686. (setq y1 (org-small-year-to-year (/ n 100))
  4687. n (mod n 100)))
  4688. (setq sd
  4689. (calendar-absolute-from-iso
  4690. (list n 1
  4691. (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
  4692. (setq nd 7)))
  4693. ((eq span 'month)
  4694. (when (and n (> n 99))
  4695. (setq y1 (org-small-year-to-year (/ n 100))
  4696. n (mod n 100)))
  4697. (setq sd (calendar-absolute-from-gregorian
  4698. (list (or n mg) 1 (or y1 yg)))
  4699. nd (- (calendar-absolute-from-gregorian
  4700. (list (1+ (or n mg)) 1 (or y1 yg)))
  4701. sd)))
  4702. ((eq span 'year)
  4703. (setq sd (calendar-absolute-from-gregorian
  4704. (list 1 1 (or n yg)))
  4705. nd (- (calendar-absolute-from-gregorian
  4706. (list 1 1 (1+ (or n yg))))
  4707. sd))))
  4708. (cons sd nd)))
  4709. (defun org-agenda-next-date-line (&optional arg)
  4710. "Jump to the next line indicating a date in agenda buffer."
  4711. (interactive "p")
  4712. (org-agenda-check-type t 'agenda 'timeline)
  4713. (beginning-of-line 1)
  4714. ;; This does not work if user makes date format that starts with a blank
  4715. (if (looking-at "^\\S-") (forward-char 1))
  4716. (if (not (re-search-forward "^\\S-" nil t arg))
  4717. (progn
  4718. (backward-char 1)
  4719. (error "No next date after this line in this buffer")))
  4720. (goto-char (match-beginning 0)))
  4721. (defun org-agenda-previous-date-line (&optional arg)
  4722. "Jump to the previous line indicating a date in agenda buffer."
  4723. (interactive "p")
  4724. (org-agenda-check-type t 'agenda 'timeline)
  4725. (beginning-of-line 1)
  4726. (if (not (re-search-backward "^\\S-" nil t arg))
  4727. (error "No previous date before this line in this buffer")))
  4728. ;; Initialize the highlight
  4729. (defvar org-hl (org-make-overlay 1 1))
  4730. (org-overlay-put org-hl 'face 'highlight)
  4731. (defun org-highlight (begin end &optional buffer)
  4732. "Highlight a region with overlay."
  4733. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  4734. org-hl begin end (or buffer (current-buffer))))
  4735. (defun org-unhighlight ()
  4736. "Detach overlay INDEX."
  4737. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  4738. ;; FIXME this is currently not used.
  4739. (defun org-highlight-until-next-command (beg end &optional buffer)
  4740. "Move the highlight overlay to BEG/END, remove it before the next command."
  4741. (org-highlight beg end buffer)
  4742. (add-hook 'pre-command-hook 'org-unhighlight-once))
  4743. (defun org-unhighlight-once ()
  4744. "Remove the highlight from its position, and this function from the hook."
  4745. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  4746. (org-unhighlight))
  4747. (defun org-agenda-follow-mode ()
  4748. "Toggle follow mode in an agenda buffer."
  4749. (interactive)
  4750. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  4751. (org-agenda-set-mode-name)
  4752. (message "Follow mode is %s"
  4753. (if org-agenda-follow-mode "on" "off")))
  4754. (defun org-agenda-clockreport-mode ()
  4755. "Toggle clocktable mode in an agenda buffer."
  4756. (interactive)
  4757. (org-agenda-check-type t 'agenda)
  4758. (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
  4759. (org-agenda-set-mode-name)
  4760. (org-agenda-redo)
  4761. (message "Clocktable mode is %s"
  4762. (if org-agenda-clockreport-mode "on" "off")))
  4763. (defun org-agenda-log-mode (&optional special)
  4764. "Toggle log mode in an agenda buffer.
  4765. With argument SPECIAL, show all possible log items, not only the ones
  4766. configured in `org-agenda-log-mode-items'.
  4767. With a double `C-u' prefix arg, show *only* log items, nothing else."
  4768. (interactive "P")
  4769. (org-agenda-check-type t 'agenda 'timeline)
  4770. (setq org-agenda-show-log
  4771. (if (equal special '(16))
  4772. 'only
  4773. (if special '(closed clock state)
  4774. (not org-agenda-show-log))))
  4775. (org-agenda-set-mode-name)
  4776. (org-agenda-redo)
  4777. (message "Log mode is %s"
  4778. (if org-agenda-show-log "on" "off")))
  4779. (defun org-agenda-archives-mode (&optional with-files)
  4780. "Toggle log mode in an agenda buffer."
  4781. (interactive "P")
  4782. (setq org-agenda-archives-mode
  4783. (if with-files t (if org-agenda-archives-mode nil 'trees)))
  4784. (org-agenda-set-mode-name)
  4785. (org-agenda-redo)
  4786. (message
  4787. "%s"
  4788. (cond
  4789. ((eq org-agenda-archives-mode nil)
  4790. "No archives are included")
  4791. ((eq org-agenda-archives-mode 'trees)
  4792. (format "Trees with :%s: tag are included" org-archive-tag))
  4793. ((eq org-agenda-archives-mode t)
  4794. (format "Trees with :%s: tag and all active archive files are included"
  4795. org-archive-tag)))))
  4796. (defun org-agenda-toggle-diary ()
  4797. "Toggle diary inclusion in an agenda buffer."
  4798. (interactive)
  4799. (org-agenda-check-type t 'agenda)
  4800. (setq org-agenda-include-diary (not org-agenda-include-diary))
  4801. (org-agenda-redo)
  4802. (org-agenda-set-mode-name)
  4803. (message "Diary inclusion turned %s"
  4804. (if org-agenda-include-diary "on" "off")))
  4805. (defun org-agenda-toggle-time-grid ()
  4806. "Toggle time grid in an agenda buffer."
  4807. (interactive)
  4808. (org-agenda-check-type t 'agenda)
  4809. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  4810. (org-agenda-redo)
  4811. (org-agenda-set-mode-name)
  4812. (message "Time-grid turned %s"
  4813. (if org-agenda-use-time-grid "on" "off")))
  4814. (defun org-agenda-set-mode-name ()
  4815. "Set the mode name to indicate all the small mode settings."
  4816. (setq mode-name
  4817. (concat "Org-Agenda"
  4818. (if (equal org-agenda-ndays 1) " Day" "")
  4819. (if (equal org-agenda-ndays 7) " Week" "")
  4820. (if org-agenda-follow-mode " Follow" "")
  4821. (if org-agenda-include-diary " Diary" "")
  4822. (if org-agenda-use-time-grid " Grid" "")
  4823. (if (consp org-agenda-show-log) " LogAll"
  4824. (if org-agenda-show-log " Log" ""))
  4825. (if (or org-agenda-filter (get 'org-agenda-filter
  4826. :preset-filter))
  4827. (concat " {" (mapconcat
  4828. 'identity
  4829. (append (get 'org-agenda-filter
  4830. :preset-filter)
  4831. org-agenda-filter) "") "}")
  4832. "")
  4833. (if org-agenda-archives-mode
  4834. (if (eq org-agenda-archives-mode t)
  4835. " Archives"
  4836. (format " :%s:" org-archive-tag))
  4837. "")
  4838. (if org-agenda-clockreport-mode " Clock" "")))
  4839. (force-mode-line-update))
  4840. (defun org-agenda-post-command-hook ()
  4841. (and (eolp) (not (bolp)) (backward-char 1))
  4842. (setq org-agenda-type
  4843. (or (get-text-property (point) 'org-agenda-type)
  4844. (get-text-property (max (point-min) (1- (point)))
  4845. 'org-agenda-type)))
  4846. (if (and org-agenda-follow-mode
  4847. (get-text-property (point) 'org-marker))
  4848. (org-agenda-show)))
  4849. (defun org-agenda-show-priority ()
  4850. "Show the priority of the current item.
  4851. This priority is composed of the main priority given with the [#A] cookies,
  4852. and by additional input from the age of a schedules or deadline entry."
  4853. (interactive)
  4854. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  4855. (message "Priority is %d" (if pri pri -1000))))
  4856. (defun org-agenda-show-tags ()
  4857. "Show the tags applicable to the current item."
  4858. (interactive)
  4859. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  4860. (if tags
  4861. (message "Tags are :%s:"
  4862. (org-no-properties (mapconcat 'identity tags ":")))
  4863. (message "No tags associated with this line"))))
  4864. (defun org-agenda-goto (&optional highlight)
  4865. "Go to the Org-mode file which contains the item at point."
  4866. (interactive)
  4867. (let* ((marker (or (get-text-property (point) 'org-marker)
  4868. (org-agenda-error)))
  4869. (buffer (marker-buffer marker))
  4870. (pos (marker-position marker)))
  4871. (switch-to-buffer-other-window buffer)
  4872. (widen)
  4873. (goto-char pos)
  4874. (when (org-mode-p)
  4875. (org-show-context 'agenda)
  4876. (save-excursion
  4877. (and (outline-next-heading)
  4878. (org-flag-heading nil)))) ; show the next heading
  4879. (recenter (/ (window-height) 2))
  4880. (run-hooks 'org-agenda-after-show-hook)
  4881. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  4882. (defvar org-agenda-after-show-hook nil
  4883. "Normal hook run after an item has been shown from the agenda.
  4884. Point is in the buffer where the item originated.")
  4885. (defun org-agenda-kill ()
  4886. "Kill the entry or subtree belonging to the current agenda entry."
  4887. (interactive)
  4888. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  4889. (let* ((marker (or (get-text-property (point) 'org-marker)
  4890. (org-agenda-error)))
  4891. (buffer (marker-buffer marker))
  4892. (pos (marker-position marker))
  4893. (type (get-text-property (point) 'type))
  4894. dbeg dend (n 0) conf)
  4895. (org-with-remote-undo buffer
  4896. (with-current-buffer buffer
  4897. (save-excursion
  4898. (goto-char pos)
  4899. (if (and (org-mode-p) (not (member type '("sexp"))))
  4900. (setq dbeg (progn (org-back-to-heading t) (point))
  4901. dend (org-end-of-subtree t t))
  4902. (setq dbeg (point-at-bol)
  4903. dend (min (point-max) (1+ (point-at-eol)))))
  4904. (goto-char dbeg)
  4905. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  4906. (setq conf (or (eq t org-agenda-confirm-kill)
  4907. (and (numberp org-agenda-confirm-kill)
  4908. (> n org-agenda-confirm-kill))))
  4909. (and conf
  4910. (not (y-or-n-p
  4911. (format "Delete entry with %d lines in buffer \"%s\"? "
  4912. n (buffer-name buffer))))
  4913. (error "Abort"))
  4914. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  4915. (with-current-buffer buffer (delete-region dbeg dend))
  4916. (message "Agenda item and source killed"))))
  4917. (defun org-agenda-archive ()
  4918. "Archive the entry or subtree belonging to the current agenda entry."
  4919. (interactive)
  4920. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  4921. (let* ((marker (or (get-text-property (point) 'org-marker)
  4922. (org-agenda-error)))
  4923. (buffer (marker-buffer marker))
  4924. (pos (marker-position marker)))
  4925. (org-with-remote-undo buffer
  4926. (with-current-buffer buffer
  4927. (if (org-mode-p)
  4928. (save-excursion
  4929. (goto-char pos)
  4930. (org-remove-subtree-entries-from-agenda)
  4931. (org-back-to-heading t)
  4932. (org-archive-subtree))
  4933. (error "Archiving works only in Org-mode files"))))))
  4934. (defun org-agenda-archive-to-archive-sibling ()
  4935. "Move the entry to the archive sibling."
  4936. (interactive)
  4937. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  4938. (let* ((marker (or (get-text-property (point) 'org-marker)
  4939. (org-agenda-error)))
  4940. (buffer (marker-buffer marker))
  4941. (pos (marker-position marker)))
  4942. (org-with-remote-undo buffer
  4943. (with-current-buffer buffer
  4944. (if (org-mode-p)
  4945. (save-excursion
  4946. (goto-char pos)
  4947. (org-remove-subtree-entries-from-agenda)
  4948. (org-back-to-heading t)
  4949. (org-archive-to-archive-sibling))
  4950. (error "Archiving works only in Org-mode files"))))))
  4951. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  4952. "Remove all lines in the agenda that correspond to a given subtree.
  4953. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  4954. If this information is not given, the function uses the tree at point."
  4955. (let ((buf (or buf (current-buffer))) m p)
  4956. (save-excursion
  4957. (unless (and beg end)
  4958. (org-back-to-heading t)
  4959. (setq beg (point))
  4960. (org-end-of-subtree t)
  4961. (setq end (point)))
  4962. (set-buffer (get-buffer org-agenda-buffer-name))
  4963. (save-excursion
  4964. (goto-char (point-max))
  4965. (beginning-of-line 1)
  4966. (while (not (bobp))
  4967. (when (and (setq m (get-text-property (point) 'org-marker))
  4968. (equal buf (marker-buffer m))
  4969. (setq p (marker-position m))
  4970. (>= p beg)
  4971. (<= p end))
  4972. (let ((inhibit-read-only t))
  4973. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  4974. (beginning-of-line 0))))))
  4975. (defun org-agenda-open-link ()
  4976. "Follow the link in the current line, if any."
  4977. (interactive)
  4978. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  4979. (save-excursion
  4980. (save-restriction
  4981. (narrow-to-region (point-at-bol) (point-at-eol))
  4982. (org-open-at-point))))
  4983. (defun org-agenda-copy-local-variable (var)
  4984. "Get a variable from a referenced buffer and install it here."
  4985. (let ((m (get-text-property (point) 'org-marker)))
  4986. (when (and m (buffer-live-p (marker-buffer m)))
  4987. (org-set-local var (with-current-buffer (marker-buffer m)
  4988. (symbol-value var))))))
  4989. (defun org-agenda-switch-to (&optional delete-other-windows)
  4990. "Go to the Org-mode file which contains the item at point."
  4991. (interactive)
  4992. (let* ((marker (or (get-text-property (point) 'org-marker)
  4993. (org-agenda-error)))
  4994. (buffer (marker-buffer marker))
  4995. (pos (marker-position marker)))
  4996. (switch-to-buffer buffer)
  4997. (and delete-other-windows (delete-other-windows))
  4998. (widen)
  4999. (goto-char pos)
  5000. (when (org-mode-p)
  5001. (org-show-context 'agenda)
  5002. (save-excursion
  5003. (and (outline-next-heading)
  5004. (org-flag-heading nil)))))) ; show the next heading
  5005. (defun org-agenda-goto-mouse (ev)
  5006. "Go to the Org-mode file which contains the item at the mouse click."
  5007. (interactive "e")
  5008. (mouse-set-point ev)
  5009. (org-agenda-goto))
  5010. (defun org-agenda-show (&optional full-entry)
  5011. "Display the Org-mode file which contains the item at point.
  5012. With prefix argument FULL-ENTRY, make the entire entry visible
  5013. if it was hidden in the outline."
  5014. (interactive "P")
  5015. (let ((win (selected-window)))
  5016. (if full-entry
  5017. (let ((org-show-entry-below t))
  5018. (org-agenda-goto t))
  5019. (org-agenda-goto t))
  5020. (select-window win)))
  5021. (defun org-agenda-show-1 (&optional more)
  5022. "Display the Org-mode file which contains the item at point.
  5023. The prefix arg causes further revieling:
  5024. 0 hide the subtree
  5025. 1 just show the entry according to defaults.
  5026. 2 show the text below the heading
  5027. 3 show the entire subtree
  5028. 4 show the entire subtree and any LOGBOOK drawers
  5029. 5 show the entire subtree and any drawers
  5030. With prefix argument FULL-ENTRY, make the entire entry visible
  5031. if it was hidden in the outline."
  5032. (interactive "p")
  5033. (let ((win (selected-window)))
  5034. (org-agenda-goto t)
  5035. (org-recenter-heading 1)
  5036. (cond
  5037. ((= more 0)
  5038. (hide-subtree)
  5039. (message "Remote: hide subtree"))
  5040. ((and (interactive-p) (= more 1))
  5041. (message "Remote: show with default settings"))
  5042. ((= more 2)
  5043. (show-entry)
  5044. (save-excursion
  5045. (org-back-to-heading)
  5046. (org-cycle-hide-drawers 'children))
  5047. (message "Remote: show entry"))
  5048. ((= more 3)
  5049. (show-subtree)
  5050. (save-excursion
  5051. (org-back-to-heading)
  5052. (org-cycle-hide-drawers 'subtree))
  5053. (message "Remote: show subtree"))
  5054. ((= more 4)
  5055. (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
  5056. (org-drawer-regexp
  5057. (concat "^[ \t]*:\\("
  5058. (mapconcat 'regexp-quote org-drawers "\\|")
  5059. "\\):[ \t]*$")))
  5060. (show-subtree)
  5061. (save-excursion
  5062. (org-back-to-heading)
  5063. (org-cycle-hide-drawers 'subtree)))
  5064. (message "Remote: show subtree and LOGBOOK"))
  5065. ((> more 4)
  5066. (show-subtree)
  5067. (message "Remote: show subtree and LOGBOOK")))
  5068. (select-window win)))
  5069. (defun org-recenter-heading (n)
  5070. (save-excursion
  5071. (org-back-to-heading)
  5072. (recenter n)))
  5073. (defvar org-agenda-cycle-counter nil)
  5074. (defun org-agenda-cycle-show (n)
  5075. "Show the current entry in another window, with default settings.
  5076. Default settings are taken from `org-show-hierarchy-above' and siblings.
  5077. When use repeadedly in immediate succession, the remote entry will cycle
  5078. through visibility
  5079. entry -> subtree -> subtree with logbook"
  5080. (interactive "p")
  5081. (when (and (= n 1)
  5082. (not (eq last-command this-command)))
  5083. (setq org-agenda-cycle-counter 0))
  5084. (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
  5085. (if (> org-agenda-cycle-counter 4)
  5086. (setq org-agenda-cycle-counter 0))
  5087. (org-agenda-show-1 org-agenda-cycle-counter))
  5088. (defun org-agenda-recenter (arg)
  5089. "Display the Org-mode file which contains the item at point and recenter."
  5090. (interactive "P")
  5091. (let ((win (selected-window)))
  5092. (org-agenda-goto t)
  5093. (recenter arg)
  5094. (select-window win)))
  5095. (defun org-agenda-show-mouse (ev)
  5096. "Display the Org-mode file which contains the item at the mouse click."
  5097. (interactive "e")
  5098. (mouse-set-point ev)
  5099. (org-agenda-show))
  5100. (defun org-agenda-check-no-diary ()
  5101. "Check if the entry is a diary link and abort if yes."
  5102. (if (get-text-property (point) 'org-agenda-diary-link)
  5103. (org-agenda-error)))
  5104. (defun org-agenda-error ()
  5105. (error "Command not allowed in this line"))
  5106. (defun org-agenda-tree-to-indirect-buffer ()
  5107. "Show the subtree corresponding to the current entry in an indirect buffer.
  5108. This calls the command `org-tree-to-indirect-buffer' from the original
  5109. Org-mode buffer.
  5110. With numerical prefix arg ARG, go up to this level and then take that tree.
  5111. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  5112. dedicated frame)."
  5113. (interactive)
  5114. (org-agenda-check-no-diary)
  5115. (let* ((marker (or (get-text-property (point) 'org-marker)
  5116. (org-agenda-error)))
  5117. (buffer (marker-buffer marker))
  5118. (pos (marker-position marker)))
  5119. (with-current-buffer buffer
  5120. (save-excursion
  5121. (goto-char pos)
  5122. (call-interactively 'org-tree-to-indirect-buffer)))))
  5123. (defvar org-last-heading-marker (make-marker)
  5124. "Marker pointing to the headline that last changed its TODO state
  5125. by a remote command from the agenda.")
  5126. (defun org-agenda-todo-nextset ()
  5127. "Switch TODO entry to next sequence."
  5128. (interactive)
  5129. (org-agenda-todo 'nextset))
  5130. (defun org-agenda-todo-previousset ()
  5131. "Switch TODO entry to previous sequence."
  5132. (interactive)
  5133. (org-agenda-todo 'previousset))
  5134. (defun org-agenda-todo (&optional arg)
  5135. "Cycle TODO state of line at point, also in Org-mode file.
  5136. This changes the line at point, all other lines in the agenda referring to
  5137. the same tree node, and the headline of the tree node in the Org-mode file."
  5138. (interactive "P")
  5139. (org-agenda-check-no-diary)
  5140. (let* ((col (current-column))
  5141. (marker (or (get-text-property (point) 'org-marker)
  5142. (org-agenda-error)))
  5143. (buffer (marker-buffer marker))
  5144. (pos (marker-position marker))
  5145. (hdmarker (get-text-property (point) 'org-hd-marker))
  5146. (todayp (equal (get-text-property (point) 'day)
  5147. (time-to-days (current-time))))
  5148. (inhibit-read-only t)
  5149. org-agenda-headline-snapshot-before-repeat newhead just-one)
  5150. (org-with-remote-undo buffer
  5151. (with-current-buffer buffer
  5152. (widen)
  5153. (goto-char pos)
  5154. (org-show-context 'agenda)
  5155. (save-excursion
  5156. (and (outline-next-heading)
  5157. (org-flag-heading nil))) ; show the next heading
  5158. (let ((current-prefix-arg arg))
  5159. (call-interactively 'org-todo))
  5160. (and (bolp) (forward-char 1))
  5161. (setq newhead (org-get-heading))
  5162. (when (and (org-bound-and-true-p
  5163. org-agenda-headline-snapshot-before-repeat)
  5164. (not (equal org-agenda-headline-snapshot-before-repeat
  5165. newhead))
  5166. todayp)
  5167. (setq newhead org-agenda-headline-snapshot-before-repeat
  5168. just-one t))
  5169. (save-excursion
  5170. (org-back-to-heading)
  5171. (move-marker org-last-heading-marker (point))))
  5172. (beginning-of-line 1)
  5173. (save-excursion
  5174. (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
  5175. (org-move-to-column col))))
  5176. (defun org-agenda-add-note (&optional arg)
  5177. "Add a time-stamped note to the entry at point."
  5178. (interactive "P")
  5179. (org-agenda-check-no-diary)
  5180. (let* ((marker (or (get-text-property (point) 'org-marker)
  5181. (org-agenda-error)))
  5182. (buffer (marker-buffer marker))
  5183. (pos (marker-position marker))
  5184. (hdmarker (get-text-property (point) 'org-hd-marker))
  5185. (inhibit-read-only t))
  5186. (with-current-buffer buffer
  5187. (widen)
  5188. (goto-char pos)
  5189. (org-show-context 'agenda)
  5190. (save-excursion
  5191. (and (outline-next-heading)
  5192. (org-flag-heading nil))) ; show the next heading
  5193. (org-add-note))))
  5194. (defun org-agenda-change-all-lines (newhead hdmarker
  5195. &optional fixface just-this)
  5196. "Change all lines in the agenda buffer which match HDMARKER.
  5197. The new content of the line will be NEWHEAD (as modified by
  5198. `org-format-agenda-item'). HDMARKER is checked with
  5199. `equal' against all `org-hd-marker' text properties in the file.
  5200. If FIXFACE is non-nil, the face of each item is modified according to
  5201. the new TODO state.
  5202. If JUST-THIS is non-nil, change just the current line, not all.
  5203. If FORCE-TAGS is non nil, the car of it returns the new tags."
  5204. (let* ((inhibit-read-only t)
  5205. (line (org-current-line))
  5206. (thetags (with-current-buffer (marker-buffer hdmarker)
  5207. (save-excursion (save-restriction (widen)
  5208. (goto-char hdmarker)
  5209. (org-get-tags-at)))))
  5210. props m pl undone-face done-face finish new dotime cat tags)
  5211. (save-excursion
  5212. (goto-char (point-max))
  5213. (beginning-of-line 1)
  5214. (while (not finish)
  5215. (setq finish (bobp))
  5216. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  5217. (or (not just-this) (= (org-current-line) line))
  5218. (equal m hdmarker))
  5219. (setq props (text-properties-at (point))
  5220. dotime (get-text-property (point) 'dotime)
  5221. cat (get-text-property (point) 'org-category)
  5222. tags thetags
  5223. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  5224. pl (get-text-property (point) 'prefix-length)
  5225. undone-face (get-text-property (point) 'undone-face)
  5226. done-face (get-text-property (point) 'done-face))
  5227. (org-move-to-column pl)
  5228. (cond
  5229. ((equal new "")
  5230. (beginning-of-line 1)
  5231. (and (looking-at ".*\n?") (replace-match "")))
  5232. ((looking-at ".*")
  5233. (replace-match new t t)
  5234. (beginning-of-line 1)
  5235. (add-text-properties (point-at-bol) (point-at-eol) props)
  5236. (when fixface
  5237. (add-text-properties
  5238. (point-at-bol) (point-at-eol)
  5239. (list 'face
  5240. (if org-last-todo-state-is-todo
  5241. undone-face done-face))))
  5242. (org-agenda-highlight-todo 'line)
  5243. (beginning-of-line 1))
  5244. (t (error "Line update did not work"))))
  5245. (beginning-of-line 0)))
  5246. (org-finalize-agenda)))
  5247. (defun org-agenda-align-tags (&optional line)
  5248. "Align all tags in agenda items to `org-agenda-tags-column'."
  5249. (let ((inhibit-read-only t) l c)
  5250. (save-excursion
  5251. (goto-char (if line (point-at-bol) (point-min)))
  5252. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  5253. (if line (point-at-eol) nil) t)
  5254. (add-text-properties
  5255. (match-beginning 2) (match-end 2)
  5256. (list 'face (delq nil (let ((prop (get-text-property
  5257. (match-beginning 2) 'face)))
  5258. (or (listp prop) (setq prop (list prop)))
  5259. (if (memq 'org-tag prop)
  5260. prop
  5261. (cons 'org-tag prop))))))
  5262. (setq l (- (match-end 2) (match-beginning 2))
  5263. c (if (< org-agenda-tags-column 0)
  5264. (- (abs org-agenda-tags-column) l)
  5265. org-agenda-tags-column))
  5266. (delete-region (match-beginning 1) (match-end 1))
  5267. (goto-char (match-beginning 1))
  5268. (insert (org-add-props
  5269. (make-string (max 1 (- c (current-column))) ?\ )
  5270. (text-properties-at (point)))))
  5271. (goto-char (point-min))
  5272. (org-font-lock-add-tag-faces (point-max)))))
  5273. (defun org-agenda-priority-up ()
  5274. "Increase the priority of line at point, also in Org-mode file."
  5275. (interactive)
  5276. (org-agenda-priority 'up))
  5277. (defun org-agenda-priority-down ()
  5278. "Decrease the priority of line at point, also in Org-mode file."
  5279. (interactive)
  5280. (org-agenda-priority 'down))
  5281. (defun org-agenda-priority (&optional force-direction)
  5282. "Set the priority of line at point, also in Org-mode file.
  5283. This changes the line at point, all other lines in the agenda referring to
  5284. the same tree node, and the headline of the tree node in the Org-mode file."
  5285. (interactive)
  5286. (org-agenda-check-no-diary)
  5287. (let* ((marker (or (get-text-property (point) 'org-marker)
  5288. (org-agenda-error)))
  5289. (hdmarker (get-text-property (point) 'org-hd-marker))
  5290. (buffer (marker-buffer hdmarker))
  5291. (pos (marker-position hdmarker))
  5292. (inhibit-read-only t)
  5293. newhead)
  5294. (org-with-remote-undo buffer
  5295. (with-current-buffer buffer
  5296. (widen)
  5297. (goto-char pos)
  5298. (org-show-context 'agenda)
  5299. (save-excursion
  5300. (and (outline-next-heading)
  5301. (org-flag-heading nil))) ; show the next heading
  5302. (funcall 'org-priority force-direction)
  5303. (end-of-line 1)
  5304. (setq newhead (org-get-heading)))
  5305. (org-agenda-change-all-lines newhead hdmarker)
  5306. (beginning-of-line 1))))
  5307. ;; FIXME: should fix the tags property of the agenda line.
  5308. (defun org-agenda-set-tags ()
  5309. "Set tags for the current headline."
  5310. (interactive)
  5311. (org-agenda-check-no-diary)
  5312. (if (and (org-region-active-p) (interactive-p))
  5313. (call-interactively 'org-change-tag-in-region)
  5314. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  5315. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  5316. (org-agenda-error)))
  5317. (buffer (marker-buffer hdmarker))
  5318. (pos (marker-position hdmarker))
  5319. (inhibit-read-only t)
  5320. newhead)
  5321. (org-with-remote-undo buffer
  5322. (with-current-buffer buffer
  5323. (widen)
  5324. (goto-char pos)
  5325. (save-excursion
  5326. (org-show-context 'agenda))
  5327. (save-excursion
  5328. (and (outline-next-heading)
  5329. (org-flag-heading nil))) ; show the next heading
  5330. (goto-char pos)
  5331. (call-interactively 'org-set-tags)
  5332. (end-of-line 1)
  5333. (setq newhead (org-get-heading)))
  5334. (org-agenda-change-all-lines newhead hdmarker)
  5335. (beginning-of-line 1)))))
  5336. (defun org-agenda-toggle-archive-tag ()
  5337. "Toggle the archive tag for the current entry."
  5338. (interactive)
  5339. (org-agenda-check-no-diary)
  5340. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  5341. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  5342. (org-agenda-error)))
  5343. (buffer (marker-buffer hdmarker))
  5344. (pos (marker-position hdmarker))
  5345. (inhibit-read-only t)
  5346. newhead)
  5347. (org-with-remote-undo buffer
  5348. (with-current-buffer buffer
  5349. (widen)
  5350. (goto-char pos)
  5351. (org-show-context 'agenda)
  5352. (save-excursion
  5353. (and (outline-next-heading)
  5354. (org-flag-heading nil))) ; show the next heading
  5355. (call-interactively 'org-toggle-archive-tag)
  5356. (end-of-line 1)
  5357. (setq newhead (org-get-heading)))
  5358. (org-agenda-change-all-lines newhead hdmarker)
  5359. (beginning-of-line 1))))
  5360. (defun org-agenda-do-date-later (arg)
  5361. (interactive "P")
  5362. (cond
  5363. ((or (equal arg '(16))
  5364. (memq last-command
  5365. '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
  5366. (setq this-command 'org-agenda-date-later-minutes)
  5367. (org-agenda-date-later-minutes 1))
  5368. ((or (equal arg '(4))
  5369. (memq last-command
  5370. '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
  5371. (setq this-command 'org-agenda-date-later-hours)
  5372. (org-agenda-date-later-hours 1))
  5373. (t
  5374. (org-agenda-date-later (prefix-numeric-value arg)))))
  5375. (defun org-agenda-do-date-earlier (arg)
  5376. (interactive "P")
  5377. (cond
  5378. ((or (equal arg '(16))
  5379. (memq last-command
  5380. '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
  5381. (setq this-command 'org-agenda-date-earlier-minutes)
  5382. (org-agenda-date-earlier-minutes 1))
  5383. ((or (equal arg '(4))
  5384. (memq last-command
  5385. '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
  5386. (setq this-command 'org-agenda-date-earlier-hours)
  5387. (org-agenda-date-earlier-hours 1))
  5388. (t
  5389. (org-agenda-date-earlier (prefix-numeric-value arg)))))
  5390. (defun org-agenda-date-later (arg &optional what)
  5391. "Change the date of this item to one day later."
  5392. (interactive "p")
  5393. (org-agenda-check-type t 'agenda 'timeline)
  5394. (org-agenda-check-no-diary)
  5395. (let* ((marker (or (get-text-property (point) 'org-marker)
  5396. (org-agenda-error)))
  5397. (buffer (marker-buffer marker))
  5398. (pos (marker-position marker)))
  5399. (org-with-remote-undo buffer
  5400. (with-current-buffer buffer
  5401. (widen)
  5402. (goto-char pos)
  5403. (if (not (org-at-timestamp-p))
  5404. (error "Cannot find time stamp"))
  5405. (org-timestamp-change arg (or what 'day)))
  5406. (org-agenda-show-new-time marker org-last-changed-timestamp))
  5407. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  5408. (defun org-agenda-date-earlier (arg &optional what)
  5409. "Change the date of this item to one day earlier."
  5410. (interactive "p")
  5411. (org-agenda-date-later (- arg) what))
  5412. (defun org-agenda-date-later-minutes (arg)
  5413. "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
  5414. (interactive "p")
  5415. (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
  5416. (org-agenda-date-later arg 'minute))
  5417. (defun org-agenda-date-earlier-minutes (arg)
  5418. "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
  5419. (interactive "p")
  5420. (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
  5421. (org-agenda-date-earlier arg 'minute))
  5422. (defun org-agenda-date-later-hours (arg)
  5423. "Change the time of this item, in hour steps."
  5424. (interactive "p")
  5425. (org-agenda-date-later arg 'hour))
  5426. (defun org-agenda-date-earlier-hours (arg)
  5427. "Change the time of this item, in hour steps."
  5428. (interactive "p")
  5429. (org-agenda-date-earlier arg 'hour))
  5430. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  5431. "Show new date stamp via text properties."
  5432. ;; We use text properties to make this undoable
  5433. (let ((inhibit-read-only t)
  5434. (buffer-invisibility-spec))
  5435. (setq stamp (concat " " prefix " => " stamp))
  5436. (save-excursion
  5437. (goto-char (point-max))
  5438. (while (not (bobp))
  5439. (when (equal marker (get-text-property (point) 'org-marker))
  5440. (org-move-to-column (- (window-width) (length stamp)) t)
  5441. (org-agenda-fix-tags-filter-overlays-at (point))
  5442. (if (featurep 'xemacs)
  5443. ;; Use `duplicable' property to trigger undo recording
  5444. (let ((ex (make-extent nil nil))
  5445. (gl (make-glyph stamp)))
  5446. (set-glyph-face gl 'secondary-selection)
  5447. (set-extent-properties
  5448. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  5449. (insert-extent ex (1- (point)) (point-at-eol)))
  5450. (add-text-properties
  5451. (1- (point)) (point-at-eol)
  5452. (list 'display (org-add-props stamp nil
  5453. 'face 'secondary-selection))))
  5454. (beginning-of-line 1))
  5455. (beginning-of-line 0)))))
  5456. (defun org-agenda-date-prompt (arg)
  5457. "Change the date of this item. Date is prompted for, with default today.
  5458. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  5459. be used to request time specification in the time stamp."
  5460. (interactive "P")
  5461. (org-agenda-check-type t 'agenda 'timeline)
  5462. (org-agenda-check-no-diary)
  5463. (let* ((marker (or (get-text-property (point) 'org-marker)
  5464. (org-agenda-error)))
  5465. (buffer (marker-buffer marker))
  5466. (pos (marker-position marker)))
  5467. (org-with-remote-undo buffer
  5468. (with-current-buffer buffer
  5469. (widen)
  5470. (goto-char pos)
  5471. (if (not (org-at-timestamp-p))
  5472. (error "Cannot find time stamp"))
  5473. (org-time-stamp arg)
  5474. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  5475. (defun org-agenda-schedule (arg)
  5476. "Schedule the item at point."
  5477. (interactive "P")
  5478. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
  5479. (org-agenda-check-no-diary)
  5480. (let* ((marker (or (get-text-property (point) 'org-marker)
  5481. (org-agenda-error)))
  5482. (type (marker-insertion-type marker))
  5483. (buffer (marker-buffer marker))
  5484. (pos (marker-position marker))
  5485. (org-insert-labeled-timestamps-at-point nil)
  5486. ts)
  5487. (set-marker-insertion-type marker t)
  5488. (org-with-remote-undo buffer
  5489. (with-current-buffer buffer
  5490. (widen)
  5491. (goto-char pos)
  5492. (setq ts (org-schedule arg)))
  5493. (org-agenda-show-new-time marker ts "S"))
  5494. (message "Item scheduled for %s" ts)))
  5495. (defun org-agenda-deadline (arg)
  5496. "Schedule the item at point."
  5497. (interactive "P")
  5498. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
  5499. (org-agenda-check-no-diary)
  5500. (let* ((marker (or (get-text-property (point) 'org-marker)
  5501. (org-agenda-error)))
  5502. (buffer (marker-buffer marker))
  5503. (pos (marker-position marker))
  5504. (org-insert-labeled-timestamps-at-point nil)
  5505. ts)
  5506. (org-with-remote-undo buffer
  5507. (with-current-buffer buffer
  5508. (widen)
  5509. (goto-char pos)
  5510. (setq ts (org-deadline arg)))
  5511. (org-agenda-show-new-time marker ts "S"))
  5512. (message "Deadline for this item set to %s" ts)))
  5513. (defun org-agenda-action ()
  5514. "Select entry for agenda action, or execute an agenda action.
  5515. This command prompts for another letter. Valid inputs are:
  5516. m Mark the entry at point for an agenda action
  5517. s Schedule the marked entry to the date at the cursor
  5518. d Set the deadline of the marked entry to the date at the cursor
  5519. r Call `org-remember' with cursor date as the default date
  5520. SPC Show marked entry in other window
  5521. TAB Visit marked entry in other window
  5522. The cursor may be at a date in the calendar, or in the Org agenda."
  5523. (interactive)
  5524. (let (ans)
  5525. (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
  5526. (setq ans (read-char-exclusive))
  5527. (cond
  5528. ((equal ans ?m)
  5529. ;; Mark this entry
  5530. (if (eq major-mode 'org-agenda-mode)
  5531. (let ((m (or (get-text-property (point) 'org-hd-marker)
  5532. (get-text-property (point) 'org-marker))))
  5533. (if m
  5534. (progn
  5535. (move-marker org-agenda-action-marker
  5536. (marker-position m) (marker-buffer m))
  5537. (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
  5538. (error "Don't know which entry to mark")))
  5539. (error "This command works only in the agenda")))
  5540. ((equal ans ?s)
  5541. (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
  5542. ((equal ans ?d)
  5543. (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
  5544. ((equal ans ?r)
  5545. (org-agenda-do-action '(org-remember) t))
  5546. ((equal ans ?\ )
  5547. (let ((cw (selected-window)))
  5548. (org-switch-to-buffer-other-window
  5549. (marker-buffer org-agenda-action-marker))
  5550. (goto-char org-agenda-action-marker)
  5551. (org-show-context 'agenda)
  5552. (select-window cw)))
  5553. ((equal ans ?\C-i)
  5554. (org-switch-to-buffer-other-window
  5555. (marker-buffer org-agenda-action-marker))
  5556. (goto-char org-agenda-action-marker)
  5557. (org-show-context 'agenda))
  5558. (t (error "Invalid agenda action %c" ans)))))
  5559. (defun org-agenda-do-action (form &optional current-buffer)
  5560. "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
  5561. (let ((org-overriding-default-time (org-get-cursor-date)))
  5562. (if current-buffer
  5563. (eval form)
  5564. (if (not (marker-buffer org-agenda-action-marker))
  5565. (error "No entry has bee selected for agenda action")
  5566. (with-current-buffer (marker-buffer org-agenda-action-marker)
  5567. (save-excursion
  5568. (save-restriction
  5569. (widen)
  5570. (goto-char org-agenda-action-marker)
  5571. (eval form))))))))
  5572. (defun org-agenda-clock-in (&optional arg)
  5573. "Start the clock on the currently selected item."
  5574. (interactive "P")
  5575. (org-agenda-check-no-diary)
  5576. (if (equal arg '(4))
  5577. (org-clock-in arg)
  5578. (let* ((marker (or (get-text-property (point) 'org-marker)
  5579. (org-agenda-error)))
  5580. (hdmarker (or (get-text-property (point) 'org-hd-marker)
  5581. marker))
  5582. (pos (marker-position marker))
  5583. newhead)
  5584. (org-with-remote-undo (marker-buffer marker)
  5585. (with-current-buffer (marker-buffer marker)
  5586. (widen)
  5587. (goto-char pos)
  5588. (org-show-context 'agenda)
  5589. (org-show-entry)
  5590. (org-cycle-hide-drawers 'children)
  5591. (org-clock-in arg)
  5592. (setq newhead (org-get-heading)))
  5593. (org-agenda-change-all-lines newhead hdmarker t)))))
  5594. (defun org-agenda-clock-out (&optional arg)
  5595. "Stop the currently running clock."
  5596. (interactive "P")
  5597. (unless (marker-buffer org-clock-marker)
  5598. (error "No running clock"))
  5599. (org-with-remote-undo (marker-buffer org-clock-marker)
  5600. (org-clock-out)))
  5601. (defun org-agenda-clock-cancel (&optional arg)
  5602. "Cancel the currently running clock."
  5603. (interactive "P")
  5604. (unless (marker-buffer org-clock-marker)
  5605. (error "No running clock"))
  5606. (org-with-remote-undo (marker-buffer org-clock-marker)
  5607. (org-clock-cancel)))
  5608. (defun org-agenda-diary-entry ()
  5609. "Make a diary entry, like the `i' command from the calendar.
  5610. All the standard commands work: block, weekly etc."
  5611. (interactive)
  5612. (org-agenda-check-type t 'agenda 'timeline)
  5613. (require 'diary-lib)
  5614. (let* ((char (progn
  5615. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  5616. (read-char-exclusive)))
  5617. (cmd (cdr (assoc char
  5618. '((?d . insert-diary-entry)
  5619. (?w . insert-weekly-diary-entry)
  5620. (?m . insert-monthly-diary-entry)
  5621. (?y . insert-yearly-diary-entry)
  5622. (?a . insert-anniversary-diary-entry)
  5623. (?b . insert-block-diary-entry)
  5624. (?c . insert-cyclic-diary-entry)))))
  5625. (oldf (symbol-function 'calendar-cursor-to-date))
  5626. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  5627. (point (point))
  5628. (mark (or (mark t) (point))))
  5629. (unless cmd
  5630. (error "No command associated with <%c>" char))
  5631. (unless (and (get-text-property point 'day)
  5632. (or (not (equal ?b char))
  5633. (get-text-property mark 'day)))
  5634. (error "Don't know which date to use for diary entry"))
  5635. ;; We implement this by hacking the `calendar-cursor-to-date' function
  5636. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  5637. (let ((calendar-mark-ring
  5638. (list (calendar-gregorian-from-absolute
  5639. (or (get-text-property mark 'day)
  5640. (get-text-property point 'day))))))
  5641. (unwind-protect
  5642. (progn
  5643. (fset 'calendar-cursor-to-date
  5644. (lambda (&optional error dummy)
  5645. (calendar-gregorian-from-absolute
  5646. (get-text-property point 'day))))
  5647. (call-interactively cmd))
  5648. (fset 'calendar-cursor-to-date oldf)))))
  5649. (defun org-agenda-execute-calendar-command (cmd)
  5650. "Execute a calendar command from the agenda, with the date associated to
  5651. the cursor position."
  5652. (org-agenda-check-type t 'agenda 'timeline)
  5653. (require 'diary-lib)
  5654. (unless (get-text-property (point) 'day)
  5655. (error "Don't know which date to use for calendar command"))
  5656. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  5657. (point (point))
  5658. (date (calendar-gregorian-from-absolute
  5659. (get-text-property point 'day)))
  5660. ;; the following 2 vars are needed in the calendar
  5661. (displayed-month (car date))
  5662. (displayed-year (nth 2 date)))
  5663. (unwind-protect
  5664. (progn
  5665. (fset 'calendar-cursor-to-date
  5666. (lambda (&optional error dummy)
  5667. (calendar-gregorian-from-absolute
  5668. (get-text-property point 'day))))
  5669. (call-interactively cmd))
  5670. (fset 'calendar-cursor-to-date oldf))))
  5671. (defun org-agenda-phases-of-moon ()
  5672. "Display the phases of the moon for the 3 months around the cursor date."
  5673. (interactive)
  5674. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  5675. (defun org-agenda-holidays ()
  5676. "Display the holidays for the 3 months around the cursor date."
  5677. (interactive)
  5678. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  5679. (defvar calendar-longitude)
  5680. (defvar calendar-latitude)
  5681. (defvar calendar-location-name)
  5682. (defun org-agenda-sunrise-sunset (arg)
  5683. "Display sunrise and sunset for the cursor date.
  5684. Latitude and longitude can be specified with the variables
  5685. `calendar-latitude' and `calendar-longitude'. When called with prefix
  5686. argument, latitude and longitude will be prompted for."
  5687. (interactive "P")
  5688. (require 'solar)
  5689. (let ((calendar-longitude (if arg nil calendar-longitude))
  5690. (calendar-latitude (if arg nil calendar-latitude))
  5691. (calendar-location-name
  5692. (if arg "the given coordinates" calendar-location-name)))
  5693. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  5694. (defun org-agenda-goto-calendar ()
  5695. "Open the Emacs calendar with the date at the cursor."
  5696. (interactive)
  5697. (org-agenda-check-type t 'agenda 'timeline)
  5698. (let* ((day (or (get-text-property (point) 'day)
  5699. (error "Don't know which date to open in calendar")))
  5700. (date (calendar-gregorian-from-absolute day))
  5701. (calendar-move-hook nil)
  5702. (calendar-view-holidays-initially-flag nil)
  5703. (calendar-view-diary-initially-flag nil)
  5704. (view-calendar-holidays-initially nil)
  5705. (view-diary-entries-initially nil))
  5706. (calendar)
  5707. (calendar-goto-date date)))
  5708. ;;;###autoload
  5709. (defun org-calendar-goto-agenda ()
  5710. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  5711. This is a command that has to be installed in `calendar-mode-map'."
  5712. (interactive)
  5713. (org-agenda-list nil (calendar-absolute-from-gregorian
  5714. (calendar-cursor-to-date))
  5715. nil))
  5716. (defun org-agenda-convert-date ()
  5717. (interactive)
  5718. (org-agenda-check-type t 'agenda 'timeline)
  5719. (let ((day (get-text-property (point) 'day))
  5720. date s)
  5721. (unless day
  5722. (error "Don't know which date to convert"))
  5723. (setq date (calendar-gregorian-from-absolute day))
  5724. (setq s (concat
  5725. "Gregorian: " (calendar-date-string date) "\n"
  5726. "ISO: " (calendar-iso-date-string date) "\n"
  5727. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  5728. "Julian: " (calendar-julian-date-string date) "\n"
  5729. "Astron. JD: " (calendar-astro-date-string date)
  5730. " (Julian date number at noon UTC)\n"
  5731. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  5732. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  5733. "French: " (calendar-french-date-string date) "\n"
  5734. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  5735. "Mayan: " (calendar-mayan-date-string date) "\n"
  5736. "Coptic: " (calendar-coptic-date-string date) "\n"
  5737. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  5738. "Persian: " (calendar-persian-date-string date) "\n"
  5739. "Chinese: " (calendar-chinese-date-string date) "\n"))
  5740. (with-output-to-temp-buffer "*Dates*"
  5741. (princ s))
  5742. (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
  5743. ;;; Appointment reminders
  5744. (defvar appt-time-msg-list)
  5745. ;;;###autoload
  5746. (defun org-agenda-to-appt (&optional refresh filter)
  5747. "Activate appointments found in `org-agenda-files'.
  5748. With a \\[universal-argument] prefix, refresh the list of
  5749. appointments.
  5750. If FILTER is t, interactively prompt the user for a regular
  5751. expression, and filter out entries that don't match it.
  5752. If FILTER is a string, use this string as a regular expression
  5753. for filtering entries out.
  5754. FILTER can also be an alist with the car of each cell being
  5755. either 'headline or 'category. For example:
  5756. '((headline \"IMPORTANT\")
  5757. (category \"Work\"))
  5758. will only add headlines containing IMPORTANT or headlines
  5759. belonging to the \"Work\" category."
  5760. (interactive "P")
  5761. (require 'calendar)
  5762. (if refresh (setq appt-time-msg-list nil))
  5763. (if (eq filter t)
  5764. (setq filter (read-from-minibuffer "Regexp filter: ")))
  5765. (let* ((cnt 0) ; count added events
  5766. (org-agenda-new-buffers nil)
  5767. (org-deadline-warning-days 0)
  5768. (today (org-date-to-gregorian
  5769. (time-to-days (current-time))))
  5770. (files (org-agenda-files 'unrestricted)) entries file)
  5771. ;; Get all entries which may contain an appt
  5772. (org-prepare-agenda-buffers files)
  5773. (while (setq file (pop files))
  5774. (setq entries
  5775. (append entries
  5776. (org-agenda-get-day-entries
  5777. file today :timestamp :scheduled :deadline))))
  5778. (setq entries (delq nil entries))
  5779. ;; Map thru entries and find if we should filter them out
  5780. (mapc
  5781. (lambda(x)
  5782. (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
  5783. (cat (get-text-property 1 'org-category x))
  5784. (tod (get-text-property 1 'time-of-day x))
  5785. (ok (or (null filter)
  5786. (and (stringp filter) (string-match filter evt))
  5787. (and (listp filter)
  5788. (or (string-match
  5789. (cadr (assoc 'category filter)) cat)
  5790. (string-match
  5791. (cadr (assoc 'headline filter)) evt))))))
  5792. ;; FIXME: Shall we remove text-properties for the appt text?
  5793. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  5794. (when (and ok tod)
  5795. (setq tod (concat "00" (number-to-string tod))
  5796. tod (when (string-match
  5797. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
  5798. (concat (match-string 1 tod) ":"
  5799. (match-string 2 tod))))
  5800. (appt-add tod evt)
  5801. (setq cnt (1+ cnt))))) entries)
  5802. (org-release-buffers org-agenda-new-buffers)
  5803. (if (eq cnt 0)
  5804. (message "No event to add")
  5805. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  5806. (defun org-agenda-todayp (date)
  5807. "Does DATE mean today, when considering `org-extend-today-until'?"
  5808. (let (today h)
  5809. (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
  5810. (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
  5811. (setq h (nth 2 (decode-time (current-time))))
  5812. (or (and (>= h org-extend-today-until)
  5813. (= date today))
  5814. (and (< h org-extend-today-until)
  5815. (= date (1- today))))))
  5816. (provide 'org-agenda)
  5817. ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
  5818. ;;; org-agenda.el ends here