org-agenda.el 407 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554
  1. ;;; org-agenda.el --- Dynamic task and appointment lists for Org
  2. ;; Copyright (C) 2004-2020 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: https://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains the code for creating and using the Agenda for Org.
  23. ;;
  24. ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
  25. ;; `org-batch-store-agenda-views' are implemented as macros to provide
  26. ;; a convenient way for extracting agenda information from the command
  27. ;; line. The Lisp does not evaluate parameters of a macro call; thus
  28. ;; it is not necessary to quote the parameters passed to one of those
  29. ;; functions. E.g. you can write:
  30. ;;
  31. ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
  32. ;;
  33. ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
  34. ;; have been implemented as a regular function you'd have to quote the
  35. ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
  36. ;; value you would have to double quote the symbol.
  37. ;;
  38. ;; This is a hack, but it works even when running Org byte-compiled.
  39. ;;
  40. ;;; Code:
  41. (require 'cl-lib)
  42. (require 'ol)
  43. (require 'org)
  44. (require 'org-macs)
  45. (declare-function diary-add-to-list "diary-lib"
  46. (date string specifier &optional marker globcolor literal))
  47. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  48. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  49. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  50. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  51. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  52. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  53. (declare-function calendar-french-date-string "cal-french" (&optional date))
  54. (declare-function calendar-goto-date "cal-move" (date))
  55. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  56. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  57. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  58. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  59. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  60. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  61. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  62. (declare-function calendar-check-holidays "holidays" (date))
  63. (declare-function org-columns-remove-overlays "org-colview" ())
  64. (declare-function org-datetree-find-date-create "org-datetree"
  65. (date &optional keep-restriction))
  66. (declare-function org-columns-quit "org-colview" ())
  67. (declare-function diary-date-display-form "diary-lib" (&optional type))
  68. (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
  69. (declare-function org-habit-insert-consistency-graphs
  70. "org-habit" (&optional line))
  71. (declare-function org-is-habit-p "org-habit" (&optional pom))
  72. (declare-function org-habit-parse-todo "org-habit" (&optional pom))
  73. (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
  74. (declare-function org-agenda-columns "org-colview" ())
  75. (declare-function org-add-archive-files "org-archive" (files))
  76. (declare-function org-capture "org-capture" (&optional goto keys))
  77. (defvar calendar-mode-map)
  78. (defvar org-clock-current-task)
  79. (defvar org-current-tag-alist)
  80. (defvar org-mobile-force-id-on-agenda-items)
  81. (defvar org-habit-show-habits)
  82. (defvar org-habit-show-habits-only-for-today)
  83. (defvar org-habit-show-all-today)
  84. (defvar org-habit-scheduled-past-days)
  85. ;; Defined somewhere in this file, but used before definition.
  86. (defvar org-agenda-buffer-name "*Org Agenda*")
  87. (defvar org-agenda-overriding-header nil)
  88. (defvar org-agenda-title-append nil)
  89. (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  90. (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  91. (defvar original-date) ; dynamically scoped, calendar.el does scope this
  92. (defvar org-agenda-undo-list nil
  93. "List of undoable operations in the agenda since last refresh.")
  94. (defvar org-agenda-pending-undo-list nil
  95. "In a series of undo commands, this is the list of remaining undo items.")
  96. (defcustom org-agenda-confirm-kill 1
  97. "When set, remote killing from the agenda buffer needs confirmation.
  98. When t, a confirmation is always needed. When a number N, confirmation is
  99. only needed when the text to be killed contains more than N non-white lines."
  100. :group 'org-agenda
  101. :type '(choice
  102. (const :tag "Never" nil)
  103. (const :tag "Always" t)
  104. (integer :tag "When more than N lines")))
  105. (defcustom org-agenda-compact-blocks nil
  106. "Non-nil means make the block agenda more compact.
  107. This is done globally by leaving out lines like the agenda span
  108. name and week number or the separator lines."
  109. :group 'org-agenda
  110. :type 'boolean)
  111. (defcustom org-agenda-block-separator ?=
  112. "The separator between blocks in the agenda.
  113. If this is a string, it will be used as the separator, with a newline added.
  114. If it is a character, it will be repeated to fill the window width.
  115. If nil the separator is disabled. In `org-agenda-custom-commands' this
  116. addresses the separator between the current and the previous block."
  117. :group 'org-agenda
  118. :type '(choice
  119. (const :tag "Disabled" nil)
  120. (character)
  121. (string)))
  122. (defgroup org-agenda-export nil
  123. "Options concerning exporting agenda views in Org mode."
  124. :tag "Org Agenda Export"
  125. :group 'org-agenda)
  126. (defcustom org-agenda-with-colors t
  127. "Non-nil means use colors in agenda views."
  128. :group 'org-agenda-export
  129. :type 'boolean)
  130. (defcustom org-agenda-exporter-settings nil
  131. "Alist of variable/value pairs that should be active during agenda export.
  132. This is a good place to set options for ps-print and for htmlize.
  133. Note that the way this is implemented, the values will be evaluated
  134. before assigned to the variables. So make sure to quote values you do
  135. *not* want evaluated, for example
  136. (setq org-agenda-exporter-settings
  137. \\='((ps-print-color-p \\='black-white)))"
  138. :group 'org-agenda-export
  139. :type '(repeat
  140. (list
  141. (variable)
  142. (sexp :tag "Value"))))
  143. (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
  144. "Hook run in a temporary buffer before writing the agenda to an export file.
  145. A useful function for this hook is `org-agenda-add-entry-text'."
  146. :group 'org-agenda-export
  147. :type 'hook
  148. :options '(org-agenda-add-entry-text))
  149. (defcustom org-agenda-add-entry-text-maxlines 0
  150. "Maximum number of entry text lines to be added to agenda.
  151. This is only relevant when `org-agenda-add-entry-text' is part of
  152. `org-agenda-before-write-hook', which is the default.
  153. When this is 0, nothing will happen. When it is greater than 0, it
  154. specifies the maximum number of lines that will be added for each entry
  155. that is listed in the agenda view.
  156. Note that this variable is not used during display, only when exporting
  157. the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
  158. and `org-agenda-entry-text-maxlines'."
  159. :group 'org-agenda
  160. :type 'integer)
  161. (defcustom org-agenda-add-entry-text-descriptive-links t
  162. "Non-nil means export org-links as descriptive links in agenda added text.
  163. This variable applies to the text added to the agenda when
  164. `org-agenda-add-entry-text-maxlines' is larger than 0.
  165. When this variable nil, the URL will (also) be shown."
  166. :group 'org-agenda
  167. :type 'boolean)
  168. (defcustom org-agenda-export-html-style nil
  169. "The style specification for exported HTML Agenda files.
  170. If this variable contains a string, it will replace the default <style>
  171. section as produced by `htmlize'.
  172. Since there are different ways of setting style information, this variable
  173. needs to contain the full HTML structure to provide a style, including the
  174. surrounding HTML tags. The style specifications should include definitions
  175. the fonts used by the agenda, here is an example:
  176. <style type=\"text/css\">
  177. p { font-weight: normal; color: gray; }
  178. .org-agenda-structure {
  179. font-size: 110%;
  180. color: #003399;
  181. font-weight: 600;
  182. }
  183. .org-todo {
  184. color: #cc6666;
  185. font-weight: bold;
  186. }
  187. .org-agenda-done {
  188. color: #339933;
  189. }
  190. .org-done {
  191. color: #339933;
  192. }
  193. .title { text-align: center; }
  194. .todo, .deadline { color: red; }
  195. .done { color: green; }
  196. </style>
  197. or, if you want to keep the style in a file,
  198. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  199. As the value of this option simply gets inserted into the HTML <head> header,
  200. you can \"misuse\" it to also add other text to the header."
  201. :group 'org-agenda-export
  202. :group 'org-export-html
  203. :type '(choice
  204. (const nil)
  205. (string)))
  206. (defcustom org-agenda-persistent-filter nil
  207. "When set, keep filters from one agenda view to the next."
  208. :group 'org-agenda
  209. :type 'boolean)
  210. (defgroup org-agenda-custom-commands nil
  211. "Options concerning agenda views in Org mode."
  212. :tag "Org Agenda Custom Commands"
  213. :group 'org-agenda)
  214. (defconst org-sorting-choice
  215. '(choice
  216. (const time-up) (const time-down)
  217. (const timestamp-up) (const timestamp-down)
  218. (const scheduled-up) (const scheduled-down)
  219. (const deadline-up) (const deadline-down)
  220. (const ts-up) (const ts-down)
  221. (const tsia-up) (const tsia-down)
  222. (const category-keep) (const category-up) (const category-down)
  223. (const tag-down) (const tag-up)
  224. (const priority-up) (const priority-down)
  225. (const todo-state-up) (const todo-state-down)
  226. (const effort-up) (const effort-down)
  227. (const habit-up) (const habit-down)
  228. (const alpha-up) (const alpha-down)
  229. (const user-defined-up) (const user-defined-down))
  230. "Sorting choices.")
  231. ;; Keep custom values for `org-agenda-filter-preset' compatible with
  232. ;; the new variable `org-agenda-tag-filter-preset'.
  233. (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
  234. (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
  235. (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
  236. "List of types searched for when creating the daily/weekly agenda.
  237. This variable is a list of symbols that controls the types of
  238. items that appear in the daily/weekly agenda. Allowed symbols in this
  239. list are
  240. :timestamp List items containing a date stamp or date range matching
  241. the selected date. This includes sexp entries in angular
  242. brackets.
  243. :sexp List entries resulting from plain diary-like sexps.
  244. :deadline List deadline due on that date. When the date is today,
  245. also list any deadlines past due, or due within
  246. `org-deadline-warning-days'.
  247. :deadline* Same as above, but only include the deadline if it has an
  248. hour specification as [h]h:mm.
  249. :scheduled List all items which are scheduled for the given date.
  250. The diary for *today* also contains items which were
  251. scheduled earlier and are not yet marked DONE.
  252. :scheduled* Same as above, but only include the scheduled item if it
  253. has an hour specification as [h]h:mm.
  254. By default, all four non-starred types are turned on.
  255. When :scheduled* or :deadline* are included, :schedule or :deadline
  256. will be ignored.
  257. Never set this variable globally using `setq', because then it
  258. will apply to all future agenda commands. Instead, bind it with
  259. `let' to scope it dynamically into the agenda-constructing
  260. command. A good way to set it is through options in
  261. `org-agenda-custom-commands'. For a more flexible (though
  262. somewhat less efficient) way of determining what is included in
  263. the daily/weekly agenda, see `org-agenda-skip-function'.")
  264. (defconst org-agenda-custom-commands-local-options
  265. `(repeat :tag "Local settings for this command. Remember to quote values"
  266. (choice :tag "Setting"
  267. (list :tag "Heading for this block"
  268. (const org-agenda-overriding-header)
  269. (string :tag "Headline"))
  270. (list :tag "Files to be searched"
  271. (const org-agenda-files)
  272. (list
  273. (const :format "" quote)
  274. (repeat (file))))
  275. (list :tag "Sorting strategy"
  276. (const org-agenda-sorting-strategy)
  277. (list
  278. (const :format "" quote)
  279. (repeat
  280. ,org-sorting-choice)))
  281. (list :tag "Prefix format"
  282. (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
  283. (string))
  284. (list :tag "Number of days in agenda"
  285. (const org-agenda-span)
  286. (list
  287. (const :format "" quote)
  288. (choice (const :tag "Day" day)
  289. (const :tag "Week" week)
  290. (const :tag "Fortnight" fortnight)
  291. (const :tag "Month" month)
  292. (const :tag "Year" year)
  293. (integer :tag "Custom"))))
  294. (list :tag "Fixed starting date"
  295. (const org-agenda-start-day)
  296. (string :value "2007-11-01"))
  297. (list :tag "Start on day of week"
  298. (const org-agenda-start-on-weekday)
  299. (choice :value 1
  300. (const :tag "Today" nil)
  301. (integer :tag "Weekday No.")))
  302. (list :tag "Include data from diary"
  303. (const org-agenda-include-diary)
  304. (boolean))
  305. (list :tag "Deadline Warning days"
  306. (const org-deadline-warning-days)
  307. (integer :value 1))
  308. (list :tag "Category filter preset"
  309. (const org-agenda-category-filter-preset)
  310. (list
  311. (const :format "" quote)
  312. (repeat
  313. (string :tag "+category or -category"))))
  314. (list :tag "Tags filter preset"
  315. (const org-agenda-tag-filter-preset)
  316. (list
  317. (const :format "" quote)
  318. (repeat
  319. (string :tag "+tag or -tag"))))
  320. (list :tag "Effort filter preset"
  321. (const org-agenda-effort-filter-preset)
  322. (list
  323. (const :format "" quote)
  324. (repeat
  325. (string :tag "+=10 or -=10 or +<10 or ->10"))))
  326. (list :tag "Regexp filter preset"
  327. (const org-agenda-regexp-filter-preset)
  328. (list
  329. (const :format "" quote)
  330. (repeat
  331. (string :tag "+regexp or -regexp"))))
  332. (list :tag "Set daily/weekly entry types"
  333. (const org-agenda-entry-types)
  334. (list
  335. (const :format "" quote)
  336. (set :greedy t :value ,org-agenda-entry-types
  337. (const :deadline)
  338. (const :scheduled)
  339. (const :deadline*)
  340. (const :scheduled*)
  341. (const :timestamp)
  342. (const :sexp))))
  343. (list :tag "Columns format"
  344. (const org-overriding-columns-format)
  345. (string :tag "Format"))
  346. (list :tag "Standard skipping condition"
  347. :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
  348. (const org-agenda-skip-function)
  349. (list
  350. (const :format "" quote)
  351. (list
  352. (choice
  353. :tag "Skipping range"
  354. (const :tag "Skip entry" org-agenda-skip-entry-if)
  355. (const :tag "Skip subtree" org-agenda-skip-subtree-if))
  356. (repeat :inline t :tag "Conditions for skipping"
  357. (choice
  358. :tag "Condition type"
  359. (list :tag "Regexp matches" :inline t
  360. (const :format "" regexp)
  361. (regexp))
  362. (list :tag "Regexp does not match" :inline t
  363. (const :format "" notregexp)
  364. (regexp))
  365. (list :tag "TODO state is" :inline t
  366. (const todo)
  367. (choice
  368. (const :tag "Any not-done state" todo)
  369. (const :tag "Any done state" done)
  370. (const :tag "Any state" any)
  371. (list :tag "Keyword list"
  372. (const :format "" quote)
  373. (repeat (string :tag "Keyword")))))
  374. (list :tag "TODO state is not" :inline t
  375. (const nottodo)
  376. (choice
  377. (const :tag "Any not-done state" todo)
  378. (const :tag "Any done state" done)
  379. (const :tag "Any state" any)
  380. (list :tag "Keyword list"
  381. (const :format "" quote)
  382. (repeat (string :tag "Keyword")))))
  383. (const :tag "scheduled" scheduled)
  384. (const :tag "not scheduled" notscheduled)
  385. (const :tag "deadline" deadline)
  386. (const :tag "no deadline" notdeadline)
  387. (const :tag "timestamp" timestamp)
  388. (const :tag "no timestamp" nottimestamp))))))
  389. (list :tag "Non-standard skipping condition"
  390. :value (org-agenda-skip-function)
  391. (const org-agenda-skip-function)
  392. (sexp :tag "Function or form (quoted!)"))
  393. (list :tag "Any variable"
  394. (variable :tag "Variable")
  395. (sexp :tag "Value (sexp)"))))
  396. "Selection of examples for agenda command settings.
  397. This will be spliced into the custom type of
  398. `org-agenda-custom-commands'.")
  399. (defcustom org-agenda-custom-commands
  400. '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
  401. "Custom commands for the agenda.
  402. \\<org-mode-map>
  403. These commands will be offered on the splash screen displayed by the
  404. agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
  405. (key desc type match settings files)
  406. key The key (one or more characters as a string) to be associated
  407. with the command.
  408. desc A description of the command, when omitted or nil, a default
  409. description is built using MATCH.
  410. type The command type, any of the following symbols:
  411. agenda The daily/weekly agenda.
  412. todo Entries with a specific TODO keyword, in all agenda files.
  413. search Entries containing search words entry or headline.
  414. tags Tags/Property/TODO match in all agenda files.
  415. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  416. todo-tree Sparse tree of specific TODO keyword in *current* file.
  417. tags-tree Sparse tree with all tags matches in *current* file.
  418. occur-tree Occur sparse tree for *current* file.
  419. ... A user-defined function.
  420. match What to search for:
  421. - a single keyword for TODO keyword searches
  422. - a tags/property/todo match expression for searches
  423. - a word search expression for text searches.
  424. - a regular expression for occur searches
  425. For all other commands, this should be the empty string.
  426. settings A list of option settings, similar to that in a let form, so like
  427. this: ((opt1 val1) (opt2 val2) ...). The values will be
  428. evaluated at the moment of execution, so quote them when needed.
  429. files A list of files to write the produced agenda buffer to with
  430. the command `org-store-agenda-views'.
  431. If a file name ends in \".html\", an HTML version of the buffer
  432. is written out. If it ends in \".ps\", a postscript version is
  433. produced. Otherwise, only the plain text is written to the file.
  434. You can also define a set of commands, to create a composite agenda buffer.
  435. In this case, an entry looks like this:
  436. (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
  437. where
  438. desc A description string to be displayed in the dispatcher menu.
  439. cmd An agenda command, similar to the above. However, tree commands
  440. are not allowed, but instead you can get agenda and global todo list.
  441. So valid commands for a set are:
  442. (agenda \"\" settings)
  443. (alltodo \"\" settings)
  444. (stuck \"\" settings)
  445. (todo \"match\" settings files)
  446. (search \"match\" settings files)
  447. (tags \"match\" settings files)
  448. (tags-todo \"match\" settings files)
  449. Each command can carry a list of options, and another set of options can be
  450. given for the whole set of commands. Individual command options take
  451. precedence over the general options.
  452. When using several characters as key to a command, the first characters
  453. are prefix commands. For the dispatcher to display useful information, you
  454. should provide a description for the prefix, like
  455. (setq org-agenda-custom-commands
  456. \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  457. (\"hl\" tags \"+HOME+Lisa\")
  458. (\"hp\" tags \"+HOME+Peter\")
  459. (\"hk\" tags \"+HOME+Kim\")))"
  460. :group 'org-agenda-custom-commands
  461. :type `(repeat
  462. (choice :value ("x" "Describe command here" tags "" nil)
  463. (list :tag "Single command"
  464. (string :tag "Access Key(s) ")
  465. (option (string :tag "Description"))
  466. (choice
  467. (const :tag "Agenda" agenda)
  468. (const :tag "TODO list" alltodo)
  469. (const :tag "Search words" search)
  470. (const :tag "Stuck projects" stuck)
  471. (const :tag "Tags/Property match (all agenda files)" tags)
  472. (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
  473. (const :tag "TODO keyword search (all agenda files)" todo)
  474. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  475. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  476. (const :tag "Occur tree (current buffer)" occur-tree)
  477. (sexp :tag "Other, user-defined function"))
  478. (string :tag "Match (only for some commands)")
  479. ,org-agenda-custom-commands-local-options
  480. (option (repeat :tag "Export" (file :tag "Export to"))))
  481. (list :tag "Command series, all agenda files"
  482. (string :tag "Access Key(s)")
  483. (string :tag "Description ")
  484. (repeat :tag "Component"
  485. (choice
  486. (list :tag "Agenda"
  487. (const :format "" agenda)
  488. (const :tag "" :format "" "")
  489. ,org-agenda-custom-commands-local-options)
  490. (list :tag "TODO list (all keywords)"
  491. (const :format "" alltodo)
  492. (const :tag "" :format "" "")
  493. ,org-agenda-custom-commands-local-options)
  494. (list :tag "Search words"
  495. (const :format "" search)
  496. (string :tag "Match")
  497. ,org-agenda-custom-commands-local-options)
  498. (list :tag "Stuck projects"
  499. (const :format "" stuck)
  500. (const :tag "" :format "" "")
  501. ,org-agenda-custom-commands-local-options)
  502. (list :tag "Tags/Property match (all agenda files)"
  503. (const :format "" tags)
  504. (string :tag "Match")
  505. ,org-agenda-custom-commands-local-options)
  506. (list :tag "Tags/Property match of TODO entries (all agenda files)"
  507. (const :format "" tags-todo)
  508. (string :tag "Match")
  509. ,org-agenda-custom-commands-local-options)
  510. (list :tag "TODO keyword search"
  511. (const :format "" todo)
  512. (string :tag "Match")
  513. ,org-agenda-custom-commands-local-options)
  514. (list :tag "Other, user-defined function"
  515. (symbol :tag "function")
  516. (string :tag "Match")
  517. ,org-agenda-custom-commands-local-options)))
  518. (repeat :tag "Settings for entire command set"
  519. (list (variable :tag "Any variable")
  520. (sexp :tag "Value")))
  521. (option (repeat :tag "Export" (file :tag "Export to"))))
  522. (cons :tag "Prefix key documentation"
  523. (string :tag "Access Key(s)")
  524. (string :tag "Description ")))))
  525. (defcustom org-agenda-query-register ?o
  526. "The register holding the current query string.
  527. The purpose of this is that if you construct a query string interactively,
  528. you can then use it to define a custom command."
  529. :group 'org-agenda-custom-commands
  530. :type 'character)
  531. (defcustom org-stuck-projects
  532. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  533. "How to identify stuck projects.
  534. This is a list of four items:
  535. 1. A tags/todo/property matcher string that is used to identify a project.
  536. See the manual for a description of tag and property searches.
  537. The entire tree below a headline matched by this is considered one project.
  538. 2. A list of TODO keywords identifying non-stuck projects.
  539. If the project subtree contains any headline with one of these todo
  540. keywords, the project is considered to be not stuck. If you specify
  541. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  542. 3. A list of tags identifying non-stuck projects.
  543. If the project subtree contains any headline with one of these tags,
  544. the project is considered to be not stuck. If you specify \"*\" as
  545. a tag, any tag will mark the project unstuck. Note that this is about
  546. the explicit presence of a tag somewhere in the subtree, inherited
  547. tags do not count here. If inherited tags make a project not stuck,
  548. use \"-TAG\" in the tags part of the matcher under (1.) above.
  549. 4. An arbitrary regular expression matching non-stuck projects.
  550. If the project turns out to be not stuck, search continues also in the
  551. subtree to see if any of the subtasks have project status.
  552. See also the variable `org-tags-match-list-sublevels' which applies
  553. to projects matched by this search as well.
  554. After defining this variable, you may use `org-agenda-list-stuck-projects'
  555. \(bound to `\\[org-agenda] #') to produce the list."
  556. :group 'org-agenda-custom-commands
  557. :type '(list
  558. (string :tag "Tags/TODO match to identify a project")
  559. (repeat :tag "Projects are *not* stuck if they have an entry with \
  560. TODO keyword any of" (string))
  561. (repeat :tag "Projects are *not* stuck if they have an entry with \
  562. TAG being any of" (string))
  563. (regexp :tag "Projects are *not* stuck if this regexp matches inside \
  564. the subtree")))
  565. (defgroup org-agenda-skip nil
  566. "Options concerning skipping parts of agenda files."
  567. :tag "Org Agenda Skip"
  568. :group 'org-agenda)
  569. (defcustom org-agenda-skip-function-global nil
  570. "Function to be called at each match during agenda construction.
  571. If this function returns nil, the current match should not be skipped.
  572. If the function decided to skip an agenda match, is must return the
  573. buffer position from which the search should be continued.
  574. This may also be a Lisp form, which will be evaluated.
  575. This variable will be applied to every agenda match, including
  576. tags/property searches and TODO lists. So try to make the test function
  577. do its checking as efficiently as possible. To implement a skipping
  578. condition just for specific agenda commands, use the variable
  579. `org-agenda-skip-function' which can be set in the options section
  580. of custom agenda commands."
  581. :group 'org-agenda-skip
  582. :type 'sexp)
  583. (defgroup org-agenda-daily/weekly nil
  584. "Options concerning the daily/weekly agenda."
  585. :tag "Org Agenda Daily/Weekly"
  586. :group 'org-agenda)
  587. (defgroup org-agenda-todo-list nil
  588. "Options concerning the global todo list agenda view."
  589. :tag "Org Agenda Todo List"
  590. :group 'org-agenda)
  591. (defgroup org-agenda-match-view nil
  592. "Options concerning the general tags/property/todo match agenda view."
  593. :tag "Org Agenda Match View"
  594. :group 'org-agenda)
  595. (defgroup org-agenda-search-view nil
  596. "Options concerning the search agenda view."
  597. :tag "Org Agenda Search View"
  598. :group 'org-agenda)
  599. (defvar org-agenda-archives-mode nil
  600. "Non-nil means the agenda will include archived items.
  601. If this is the symbol `trees', trees in the selected agenda scope
  602. that are marked with the ARCHIVE tag will be included anyway. When this is
  603. t, also all archive files associated with the current selection of agenda
  604. files will be included.")
  605. (defcustom org-agenda-restriction-lock-highlight-subtree t
  606. "Non-nil means highlight the whole subtree when restriction is active.
  607. Otherwise only highlight the headline. Highlighting the whole subtree is
  608. useful to ensure no edits happen beyond the restricted region."
  609. :group 'org-agenda
  610. :type 'boolean)
  611. (defcustom org-agenda-skip-comment-trees t
  612. "Non-nil means skip trees that start with the COMMENT keyword.
  613. When nil, these trees are also scanned by agenda commands."
  614. :group 'org-agenda-skip
  615. :type 'boolean)
  616. (defcustom org-agenda-todo-list-sublevels t
  617. "Non-nil means check also the sublevels of a TODO entry for TODO entries.
  618. When nil, the sublevels of a TODO entry are not checked, resulting in
  619. potentially much shorter TODO lists."
  620. :group 'org-agenda-skip
  621. :group 'org-agenda-todo-list
  622. :type 'boolean)
  623. (defcustom org-agenda-todo-ignore-with-date nil
  624. "Non-nil means don't show entries with a date in the global todo list.
  625. You can use this if you prefer to mark mere appointments with a TODO keyword,
  626. but don't want them to show up in the TODO list.
  627. When this is set, it also covers deadlines and scheduled items, the settings
  628. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  629. will be ignored.
  630. See also the variable `org-agenda-tags-todo-honor-ignore-options'."
  631. :group 'org-agenda-skip
  632. :group 'org-agenda-todo-list
  633. :type 'boolean)
  634. (defcustom org-agenda-todo-ignore-timestamp nil
  635. "Non-nil means don't show entries with a timestamp.
  636. This applies when creating the global todo list.
  637. Valid values are:
  638. past Don't show entries for today or in the past.
  639. future Don't show entries with a timestamp in the future.
  640. The idea behind this is that if it has a future
  641. timestamp, you don't want to think about it until the
  642. date.
  643. all Don't show any entries with a timestamp in the global todo list.
  644. The idea behind this is that by setting a timestamp, you
  645. have already \"taken care\" of this item.
  646. This variable can also have an integer as a value. If positive (N),
  647. todos with a timestamp N or more days in the future will be ignored. If
  648. negative (-N), todos with a timestamp N or more days in the past will be
  649. ignored. If 0, todos with a timestamp either today or in the future will
  650. be ignored. For example, a value of -1 will exclude todos with a
  651. timestamp in the past (yesterday or earlier), while a value of 7 will
  652. exclude todos with a timestamp a week or more in the future.
  653. See also `org-agenda-todo-ignore-with-date'.
  654. See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
  655. to make his option also apply to the tags-todo list."
  656. :group 'org-agenda-skip
  657. :group 'org-agenda-todo-list
  658. :version "24.1"
  659. :type '(choice
  660. (const :tag "Ignore future timestamp todos" future)
  661. (const :tag "Ignore past or present timestamp todos" past)
  662. (const :tag "Ignore all timestamp todos" all)
  663. (const :tag "Show timestamp todos" nil)
  664. (integer :tag "Ignore if N or more days in past(-) or future(+).")))
  665. (defcustom org-agenda-todo-ignore-scheduled nil
  666. "Non-nil means, ignore some scheduled TODO items when making TODO list.
  667. This applies when creating the global todo list.
  668. Valid values are:
  669. past Don't show entries scheduled today or in the past.
  670. future Don't show entries scheduled in the future.
  671. The idea behind this is that by scheduling it, you don't want to
  672. think about it until the scheduled date.
  673. all Don't show any scheduled entries in the global todo list.
  674. The idea behind this is that by scheduling it, you have already
  675. \"taken care\" of this item.
  676. t Same as `all', for backward compatibility.
  677. This variable can also have an integer as a value. See
  678. `org-agenda-todo-ignore-timestamp' for more details.
  679. See also `org-agenda-todo-ignore-with-date'.
  680. See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
  681. to make his option also apply to the tags-todo list."
  682. :group 'org-agenda-skip
  683. :group 'org-agenda-todo-list
  684. :type '(choice
  685. (const :tag "Ignore future-scheduled todos" future)
  686. (const :tag "Ignore past- or present-scheduled todos" past)
  687. (const :tag "Ignore all scheduled todos" all)
  688. (const :tag "Ignore all scheduled todos (compatibility)" t)
  689. (const :tag "Show scheduled todos" nil)
  690. (integer :tag "Ignore if N or more days in past(-) or future(+).")))
  691. (defcustom org-agenda-todo-ignore-deadlines nil
  692. "Non-nil means ignore some deadline TODO items when making TODO list.
  693. There are different motivations for using different values, please think
  694. carefully when configuring this variable.
  695. This applies when creating the global TODO list.
  696. Valid values are:
  697. near Don't show near deadline entries. A deadline is near when it is
  698. closer than `org-deadline-warning-days' days. The idea behind this
  699. is that such items will appear in the agenda anyway.
  700. far Don't show TODO entries where a deadline has been defined, but
  701. is not going to happen anytime soon. This is useful if you want to use
  702. the TODO list to figure out what to do now.
  703. past Don't show entries with a deadline timestamp for today or in the past.
  704. future Don't show entries with a deadline timestamp in the future, not even
  705. when they become `near' ones. Use it with caution.
  706. all Ignore all TODO entries that do have a deadline.
  707. t Same as `near', for backward compatibility.
  708. This variable can also have an integer as a value. See
  709. `org-agenda-todo-ignore-timestamp' for more details.
  710. See also `org-agenda-todo-ignore-with-date'.
  711. See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
  712. to make his option also apply to the tags-todo list."
  713. :group 'org-agenda-skip
  714. :group 'org-agenda-todo-list
  715. :type '(choice
  716. (const :tag "Ignore near deadlines" near)
  717. (const :tag "Ignore near deadlines (compatibility)" t)
  718. (const :tag "Ignore far deadlines" far)
  719. (const :tag "Ignore all TODOs with a deadlines" all)
  720. (const :tag "Show all TODOs, even if they have a deadline" nil)
  721. (integer :tag "Ignore if N or more days in past(-) or future(+).")))
  722. (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
  723. "Time unit to use when possibly ignoring an agenda item.
  724. See the docstring of various `org-agenda-todo-ignore-*' options.
  725. The default is to compare time stamps using days. An item is thus
  726. considered to be in the future if it is at least one day after today.
  727. Non-nil means to compare time stamps using seconds. An item is then
  728. considered future if it has a time value later than current time."
  729. :group 'org-agenda-skip
  730. :group 'org-agenda-todo-list
  731. :version "24.4"
  732. :package-version '(Org . "8.0")
  733. :type '(choice
  734. (const :tag "Compare time with days" nil)
  735. (const :tag "Compare time with seconds" t)))
  736. (defcustom org-agenda-tags-todo-honor-ignore-options nil
  737. "Non-nil means honor todo-list ignores options also in tags-todo search.
  738. The variables
  739. `org-agenda-todo-ignore-with-date',
  740. `org-agenda-todo-ignore-timestamp',
  741. `org-agenda-todo-ignore-scheduled',
  742. `org-agenda-todo-ignore-deadlines'
  743. make the global TODO list skip entries that have time stamps of certain
  744. kinds. If this option is set, the same options will also apply for the
  745. tags-todo search, which is the general tags/property matcher
  746. restricted to unfinished TODO entries only."
  747. :group 'org-agenda-skip
  748. :group 'org-agenda-todo-list
  749. :group 'org-agenda-match-view
  750. :type 'boolean)
  751. (defcustom org-agenda-skip-scheduled-if-done nil
  752. "Non-nil means don't show scheduled items in agenda when they are done.
  753. This is relevant for the daily/weekly agenda, not for the TODO list. It
  754. applies only to the actual date of the scheduling. Warnings about an item
  755. with a past scheduling dates are always turned off when the item is DONE."
  756. :group 'org-agenda-skip
  757. :group 'org-agenda-daily/weekly
  758. :type 'boolean)
  759. (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
  760. "Non-nil means skip scheduling line if same entry shows because of deadline.
  761. In the agenda of today, an entry can show up multiple times
  762. because it is both scheduled and has a nearby deadline, and maybe
  763. a plain time stamp as well.
  764. When this variable is nil, the entry will be shown several times.
  765. When set to t, then only the deadline is shown and the fact that
  766. the entry is scheduled today or was scheduled previously is not
  767. shown.
  768. When set to the symbol `not-today', skip scheduled previously,
  769. but not scheduled today.
  770. When set to the symbol `repeated-after-deadline', skip scheduled
  771. items if they are repeated beyond the current deadline."
  772. :group 'org-agenda-skip
  773. :group 'org-agenda-daily/weekly
  774. :type '(choice
  775. (const :tag "Never" nil)
  776. (const :tag "Always" t)
  777. (const :tag "Not when scheduled today" not-today)
  778. (const :tag "When repeated past deadline" repeated-after-deadline)))
  779. (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
  780. "Non-nil means skip timestamp line if same entry shows because of deadline.
  781. In the agenda of today, an entry can show up multiple times
  782. because it has both a plain timestamp and has a nearby deadline.
  783. When this variable is t, then only the deadline is shown and the
  784. fact that the entry has a timestamp for or including today is not
  785. shown. When this variable is nil, the entry will be shown
  786. several times."
  787. :group 'org-agenda-skip
  788. :group 'org-agenda-daily/weekly
  789. :version "24.1"
  790. :type '(choice
  791. (const :tag "Never" nil)
  792. (const :tag "Always" t)))
  793. (defcustom org-agenda-skip-deadline-if-done nil
  794. "Non-nil means don't show deadlines when the corresponding item is done.
  795. When nil, the deadline is still shown and should give you a happy feeling.
  796. This is relevant for the daily/weekly agenda. It applies only to the
  797. actual date of the deadline. Warnings about approaching and past-due
  798. deadlines are always turned off when the item is DONE."
  799. :group 'org-agenda-skip
  800. :group 'org-agenda-daily/weekly
  801. :type 'boolean)
  802. (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
  803. "Non-nil means skip deadline prewarning when entry is also scheduled.
  804. This will apply on all days where a prewarning for the deadline would
  805. be shown, but not at the day when the entry is actually due. On that day,
  806. the deadline will be shown anyway.
  807. This variable may be set to nil, t, the symbol `pre-scheduled',
  808. or a number which will then give the number of days before the actual
  809. deadline when the prewarnings should resume. The symbol `pre-scheduled'
  810. eliminates the deadline prewarning only prior to the scheduled date.
  811. This can be used in a workflow where the first showing of the deadline will
  812. trigger you to schedule it, and then you don't want to be reminded of it
  813. because you will take care of it on the day when scheduled."
  814. :group 'org-agenda-skip
  815. :group 'org-agenda-daily/weekly
  816. :version "24.1"
  817. :type '(choice
  818. (const :tag "Always show prewarning" nil)
  819. (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
  820. (const :tag "Remove prewarning if entry is scheduled" t)
  821. (integer :tag "Restart prewarning N days before deadline")))
  822. (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
  823. "Non-nil means skip scheduled delay when entry also has a deadline.
  824. This variable may be set to nil, t, the symbol `post-deadline',
  825. or a number which will then give the number of days after the actual
  826. scheduled date when the delay should expire. The symbol `post-deadline'
  827. eliminates the schedule delay when the date is posterior to the deadline."
  828. :group 'org-agenda-skip
  829. :group 'org-agenda-daily/weekly
  830. :version "24.4"
  831. :package-version '(Org . "8.0")
  832. :type '(choice
  833. (const :tag "Always honor delay" nil)
  834. (const :tag "Ignore delay if posterior to the deadline" post-deadline)
  835. (const :tag "Ignore delay if entry has a deadline" t)
  836. (integer :tag "Honor delay up until N days after the scheduled date")))
  837. (defcustom org-agenda-skip-additional-timestamps-same-entry nil
  838. "When nil, multiple same-day timestamps in entry make multiple agenda lines.
  839. When non-nil, after the search for timestamps has matched once in an
  840. entry, the rest of the entry will not be searched."
  841. :group 'org-agenda-skip
  842. :type 'boolean)
  843. (defcustom org-agenda-skip-timestamp-if-done nil
  844. "Non-nil means don't select item by timestamp or -range if it is DONE."
  845. :group 'org-agenda-skip
  846. :group 'org-agenda-daily/weekly
  847. :type 'boolean)
  848. (defcustom org-agenda-dim-blocked-tasks t
  849. "Non-nil means dim blocked tasks in the agenda display.
  850. This causes some overhead during agenda construction, but if you
  851. have turned on `org-enforce-todo-dependencies',
  852. `org-enforce-todo-checkbox-dependencies', or any other blocking
  853. mechanism, this will create useful feedback in the agenda.
  854. Instead of t, this variable can also have the value `invisible'.
  855. Then blocked tasks will be invisible and only become visible when
  856. they become unblocked. An exemption to this behavior is when a task is
  857. blocked because of unchecked checkboxes below it. Since checkboxes do
  858. not show up in the agenda views, making this task invisible you remove any
  859. trace from agenda views that there is something to do. Therefore, a task
  860. that is blocked because of checkboxes will never be made invisible, it
  861. will only be dimmed."
  862. :group 'org-agenda-daily/weekly
  863. :group 'org-agenda-todo-list
  864. :version "24.3"
  865. :type '(choice
  866. (const :tag "Do not dim" nil)
  867. (const :tag "Dim to a gray face" t)
  868. (const :tag "Make invisible" invisible)))
  869. (defgroup org-agenda-startup nil
  870. "Options concerning initial settings in the Agenda in Org Mode."
  871. :tag "Org Agenda Startup"
  872. :group 'org-agenda)
  873. (defcustom org-agenda-menu-show-matcher t
  874. "Non-nil means show the match string in the agenda dispatcher menu.
  875. When nil, the matcher string is not shown, but is put into the help-echo
  876. property so than moving the mouse over the command shows it.
  877. Setting it to nil is good if matcher strings are very long and/or if
  878. you want to use two-columns display (see `org-agenda-menu-two-columns')."
  879. :group 'org-agenda
  880. :version "24.1"
  881. :type 'boolean)
  882. (defcustom org-agenda-menu-two-columns nil
  883. "Non-nil means, use two columns to show custom commands in the dispatcher.
  884. If you use this, you probably want to set `org-agenda-menu-show-matcher'
  885. to nil."
  886. :group 'org-agenda
  887. :version "24.1"
  888. :type 'boolean)
  889. (defcustom org-agenda-finalize-hook nil
  890. "Hook run just before displaying an agenda buffer.
  891. The buffer is still writable when the hook is called.
  892. You can modify some of the buffer substrings but you should be
  893. extra careful not to modify the text properties of the agenda
  894. headlines as the agenda display heavily relies on them."
  895. :group 'org-agenda-startup
  896. :type 'hook)
  897. (defcustom org-agenda-mouse-1-follows-link nil
  898. "Non-nil means mouse-1 on a link will follow the link in the agenda.
  899. A longer mouse click will still set point. Needs to be set
  900. before org.el is loaded."
  901. :group 'org-agenda-startup
  902. :type 'boolean)
  903. (defcustom org-agenda-start-with-follow-mode nil
  904. "The initial value of follow mode in a newly created agenda window."
  905. :group 'org-agenda-startup
  906. :type 'boolean)
  907. (defcustom org-agenda-follow-indirect nil
  908. "Non-nil means `org-agenda-follow-mode' displays only the
  909. current item's tree, in an indirect buffer."
  910. :group 'org-agenda
  911. :version "24.1"
  912. :type 'boolean)
  913. (defcustom org-agenda-show-outline-path t
  914. "Non-nil means show outline path in echo area after line motion."
  915. :group 'org-agenda-startup
  916. :type 'boolean)
  917. (defcustom org-agenda-start-with-entry-text-mode nil
  918. "The initial value of entry-text-mode in a newly created agenda window."
  919. :group 'org-agenda-startup
  920. :type 'boolean)
  921. (defcustom org-agenda-entry-text-maxlines 5
  922. "Number of text lines to be added when `E' is pressed in the agenda.
  923. Note that this variable only used during agenda display. To add entry text
  924. when exporting the agenda, configure the variable
  925. `org-agenda-add-entry-text-maxlines'."
  926. :group 'org-agenda
  927. :type 'integer)
  928. (defcustom org-agenda-entry-text-exclude-regexps nil
  929. "List of regular expressions to clean up entry text.
  930. The complete matches of all regular expressions in this list will be
  931. removed from entry text before it is shown in the agenda."
  932. :group 'org-agenda
  933. :type '(repeat (regexp)))
  934. (defcustom org-agenda-entry-text-leaders " > "
  935. "Text prepended to the entry text in agenda buffers."
  936. :version "24.4"
  937. :package-version '(Org . "8.0")
  938. :group 'org-agenda
  939. :type 'string)
  940. (defvar org-agenda-entry-text-cleanup-hook nil
  941. "Hook that is run after basic cleanup of entry text to be shown in agenda.
  942. This cleanup is done in a temporary buffer, so the function may inspect and
  943. change the entire buffer.
  944. Some default stuff like drawers and scheduling/deadline dates will already
  945. have been removed when this is called, as will any matches for regular
  946. expressions listed in `org-agenda-entry-text-exclude-regexps'.")
  947. (defvar org-agenda-include-inactive-timestamps nil
  948. "Non-nil means include inactive time stamps in agenda.
  949. Dynamically scoped.")
  950. (defgroup org-agenda-windows nil
  951. "Options concerning the windows used by the Agenda in Org Mode."
  952. :tag "Org Agenda Windows"
  953. :group 'org-agenda)
  954. (defcustom org-agenda-window-setup 'reorganize-frame
  955. "How the agenda buffer should be displayed.
  956. Possible values for this option are:
  957. current-window Show agenda in the current window, keeping all other windows.
  958. other-window Use `switch-to-buffer-other-window' to display agenda.
  959. only-window Show agenda, deleting all other windows.
  960. reorganize-frame Show only two windows on the current frame, the current
  961. window and the agenda.
  962. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  963. Also, when exiting the agenda, kill that frame.
  964. See also the variable `org-agenda-restore-windows-after-quit'."
  965. :group 'org-agenda-windows
  966. :type '(choice
  967. (const current-window)
  968. (const other-frame)
  969. (const other-window)
  970. (const only-window)
  971. (const reorganize-frame)))
  972. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  973. "The min and max height of the agenda window as a fraction of frame height.
  974. The value of the variable is a cons cell with two numbers between 0 and 1.
  975. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  976. :group 'org-agenda-windows
  977. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  978. (defcustom org-agenda-restore-windows-after-quit nil
  979. "Non-nil means restore window configuration upon exiting agenda.
  980. Before the window configuration is changed for displaying the agenda,
  981. the current status is recorded. When the agenda is exited with
  982. `q' or `x' and this option is set, the old state is restored. If
  983. `org-agenda-window-setup' is `other-frame', the value of this
  984. option will be ignored."
  985. :group 'org-agenda-windows
  986. :type 'boolean)
  987. (defcustom org-agenda-span 'week
  988. "Number of days to include in overview display.
  989. Can be day, week, month, year, or any number of days.
  990. Custom commands can set this variable in the options section."
  991. :group 'org-agenda-daily/weekly
  992. :type '(choice (const :tag "Day" day)
  993. (const :tag "Week" week)
  994. (const :tag "Fortnight" fortnight)
  995. (const :tag "Month" month)
  996. (const :tag "Year" year)
  997. (integer :tag "Custom")))
  998. (defcustom org-agenda-start-on-weekday 1
  999. "Non-nil means start the overview always on the specified weekday.
  1000. 0 denotes Sunday, 1 denotes Monday, etc.
  1001. When nil, always start on the current day.
  1002. Custom commands can set this variable in the options section."
  1003. :group 'org-agenda-daily/weekly
  1004. :type '(choice (const :tag "Today" nil)
  1005. (integer :tag "Weekday No.")))
  1006. (defcustom org-agenda-show-all-dates t
  1007. "Non-nil means `org-agenda' shows every day in the selected range.
  1008. When nil, only the days which actually have entries are shown."
  1009. :group 'org-agenda-daily/weekly
  1010. :type 'boolean)
  1011. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  1012. "Format string for displaying dates in the agenda.
  1013. Used by the daily/weekly agenda. This should be a format string
  1014. understood by `format-time-string', or a function returning the
  1015. formatted date as a string. The function must take a single
  1016. argument, a calendar-style date list like (month day year)."
  1017. :group 'org-agenda-daily/weekly
  1018. :type '(choice
  1019. (string :tag "Format string")
  1020. (function :tag "Function")))
  1021. (defun org-agenda-format-date-aligned (date)
  1022. "Format a DATE string for display in the daily/weekly agenda.
  1023. This function makes sure that dates are aligned for easy reading."
  1024. (require 'cal-iso)
  1025. (let* ((dayname (calendar-day-name date))
  1026. (day (cadr date))
  1027. (day-of-week (calendar-day-of-week date))
  1028. (month (car date))
  1029. (monthname (calendar-month-name month))
  1030. (year (nth 2 date))
  1031. (iso-week (org-days-to-iso-week
  1032. (calendar-absolute-from-gregorian date)))
  1033. (weekyear (cond ((and (= month 1) (>= iso-week 52))
  1034. (1- year))
  1035. ((and (= month 12) (<= iso-week 1))
  1036. (1+ year))
  1037. (t year)))
  1038. (weekstring (if (= day-of-week 1)
  1039. (format " W%02d" iso-week)
  1040. "")))
  1041. (format "%-10s %2d %s %4d%s"
  1042. dayname day monthname year weekstring)))
  1043. (defcustom org-agenda-time-leading-zero nil
  1044. "Non-nil means use leading zero for military times in agenda.
  1045. For example, 9:30am would become 09:30 rather than 9:30."
  1046. :group 'org-agenda-daily/weekly
  1047. :version "24.1"
  1048. :type 'boolean)
  1049. (defcustom org-agenda-timegrid-use-ampm nil
  1050. "When set, show AM/PM style timestamps on the timegrid."
  1051. :group 'org-agenda
  1052. :version "24.1"
  1053. :type 'boolean)
  1054. (defun org-agenda-time-of-day-to-ampm (time)
  1055. "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
  1056. (let* ((hour-number (string-to-number (substring time 0 -3)))
  1057. (minute (substring time -2))
  1058. (ampm "am"))
  1059. (cond
  1060. ((equal hour-number 12)
  1061. (setq ampm "pm"))
  1062. ((> hour-number 12)
  1063. (setq ampm "pm")
  1064. (setq hour-number (- hour-number 12))))
  1065. (concat
  1066. (if org-agenda-time-leading-zero
  1067. (format "%02d" hour-number)
  1068. (format "%02s" (number-to-string hour-number)))
  1069. ":" minute ampm)))
  1070. (defun org-agenda-time-of-day-to-ampm-maybe (time)
  1071. "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
  1072. (if org-agenda-timegrid-use-ampm
  1073. (org-agenda-time-of-day-to-ampm time)
  1074. time))
  1075. (defcustom org-agenda-weekend-days '(6 0)
  1076. "Which days are weekend?
  1077. These days get the special face `org-agenda-date-weekend' in the agenda."
  1078. :group 'org-agenda-daily/weekly
  1079. :type '(set :greedy t
  1080. (const :tag "Monday" 1)
  1081. (const :tag "Tuesday" 2)
  1082. (const :tag "Wednesday" 3)
  1083. (const :tag "Thursday" 4)
  1084. (const :tag "Friday" 5)
  1085. (const :tag "Saturday" 6)
  1086. (const :tag "Sunday" 0)))
  1087. (defcustom org-agenda-move-date-from-past-immediately-to-today t
  1088. "Non-nil means jump to today when moving a past date forward in time.
  1089. When using S-right in the agenda to move a date forward, and the date
  1090. stamp currently points to the past, the first key press will move it
  1091. to today. When nil, just move one day forward even if the date stays
  1092. in the past."
  1093. :group 'org-agenda-daily/weekly
  1094. :version "24.1"
  1095. :type 'boolean)
  1096. (defcustom org-agenda-include-diary nil
  1097. "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
  1098. Custom commands can set this variable in the options section."
  1099. :group 'org-agenda-daily/weekly
  1100. :type 'boolean)
  1101. (defcustom org-agenda-include-deadlines t
  1102. "If non-nil, include entries within their deadline warning period.
  1103. Custom commands can set this variable in the options section."
  1104. :group 'org-agenda-daily/weekly
  1105. :version "24.1"
  1106. :type 'boolean)
  1107. (defcustom org-agenda-show-future-repeats t
  1108. "Non-nil shows repeated entries in the future part of the agenda.
  1109. When set to the symbol `next' only the first future repeat is shown."
  1110. :group 'org-agenda-daily/weekly
  1111. :type '(choice
  1112. (const :tag "Show all repeated entries" t)
  1113. (const :tag "Show next repeated entry" next)
  1114. (const :tag "Do not show repeated entries" nil))
  1115. :version "26.1"
  1116. :package-version '(Org . "9.1")
  1117. :safe #'symbolp)
  1118. (defcustom org-agenda-prefer-last-repeat nil
  1119. "Non-nil sets date for repeated entries to their last repeat.
  1120. When nil, display SCHEDULED and DEADLINE dates at their base
  1121. date, and in today's agenda, as a reminder. Display plain
  1122. time-stamps, on the other hand, at every repeat date in the past
  1123. in addition to the base date.
  1124. When non-nil, show a repeated entry at its latest repeat date,
  1125. possibly being today even if it wasn't marked as done. This
  1126. setting is useful if you do not always mark repeated entries as
  1127. done and, yet, consider that reaching repeat date starts the task
  1128. anew.
  1129. When set to a list of strings, prefer last repeats only for
  1130. entries with these TODO keywords."
  1131. :group 'org-agenda-daily/weekly
  1132. :type '(choice
  1133. (const :tag "Prefer last repeat" t)
  1134. (const :tag "Prefer base date" nil)
  1135. (repeat :tag "Prefer last repeat for entries with these TODO keywords"
  1136. (string :tag "TODO keyword")))
  1137. :version "26.1"
  1138. :package-version '(Org . "9.1")
  1139. :safe (lambda (x) (or (booleanp x) (consp x))))
  1140. (defcustom org-scheduled-past-days 10000
  1141. "Number of days to continue listing scheduled items not marked DONE.
  1142. When an item is scheduled on a date, it shows up in the agenda on
  1143. this day and will be listed until it is marked done or for the
  1144. number of days given here."
  1145. :group 'org-agenda-daily/weekly
  1146. :type 'integer
  1147. :safe 'integerp)
  1148. (defcustom org-deadline-past-days 10000
  1149. "Number of days to warn about missed deadlines.
  1150. When an item has deadline on a date, it shows up in the agenda on
  1151. this day and will appear as a reminder until it is marked DONE or
  1152. for the number of days given here."
  1153. :group 'org-agenda-daily/weekly
  1154. :type 'integer
  1155. :version "26.1"
  1156. :package-version '(Org . "9.1")
  1157. :safe 'integerp)
  1158. (defcustom org-agenda-log-mode-items '(closed clock)
  1159. "List of items that should be shown in agenda log mode.
  1160. \\<org-agenda-mode-map>\
  1161. This list may contain the following symbols:
  1162. closed Show entries that have been closed on that day.
  1163. clock Show entries that have received clocked time on that day.
  1164. state Show all logged state changes.
  1165. Note that instead of changing this variable, you can also press \
  1166. `\\[universal-argument] \\[org-agenda-log-mode]' in
  1167. the agenda to display all available LOG items temporarily."
  1168. :group 'org-agenda-daily/weekly
  1169. :type '(set :greedy t (const closed) (const clock) (const state)))
  1170. (defcustom org-agenda-clock-consistency-checks
  1171. '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
  1172. :gap-ok-around ("4:00")
  1173. :default-face ((:background "DarkRed") (:foreground "white"))
  1174. :overlap-face nil :gap-face nil :no-end-time-face nil
  1175. :long-face nil :short-face nil)
  1176. "This is a property list, with the following keys:
  1177. :max-duration Mark clocking chunks that are longer than this time.
  1178. This is a time string like \"HH:MM\", or the number
  1179. of minutes as an integer.
  1180. :min-duration Mark clocking chunks that are shorter that this.
  1181. This is a time string like \"HH:MM\", or the number
  1182. of minutes as an integer.
  1183. :max-gap Mark gaps between clocking chunks that are longer than
  1184. this duration. A number of minutes, or a string
  1185. like \"HH:MM\".
  1186. :gap-ok-around List of times during the day which are usually not working
  1187. times. When a gap is detected, but the gap contains any
  1188. of these times, the gap is *not* reported. For example,
  1189. if this is (\"4:00\" \"13:00\") then gaps that contain
  1190. 4:00 in the morning (i.e. the night) and 13:00
  1191. (i.e. a typical lunch time) do not cause a warning.
  1192. You should have at least one time during the night in this
  1193. list, or otherwise the first task each morning will trigger
  1194. a warning because it follows a long gap.
  1195. Furthermore, the following properties can be used to define faces for
  1196. issue display.
  1197. :default-face the default face, if the specific face is undefined
  1198. :overlap-face face for overlapping clocks
  1199. :gap-face face for gaps between clocks
  1200. :no-end-time-face face for incomplete clocks
  1201. :long-face face for clock intervals that are too long
  1202. :short-face face for clock intervals that are too short"
  1203. :group 'org-agenda-daily/weekly
  1204. :group 'org-clock
  1205. :version "24.1"
  1206. :type 'plist)
  1207. (defcustom org-agenda-log-mode-add-notes t
  1208. "Non-nil means add first line of notes to log entries in agenda views.
  1209. If a log item like a state change or a clock entry is associated with
  1210. notes, the first line of these notes will be added to the entry in the
  1211. agenda display."
  1212. :group 'org-agenda-daily/weekly
  1213. :type 'boolean)
  1214. (defcustom org-agenda-start-with-log-mode nil
  1215. "The initial value of log-mode in a newly created agenda window.
  1216. See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
  1217. explanations on the possible values."
  1218. :group 'org-agenda-startup
  1219. :group 'org-agenda-daily/weekly
  1220. :type '(choice (const :tag "Don't show log items" nil)
  1221. (const :tag "Show only log items" only)
  1222. (const :tag "Show all possible log items" clockcheck)
  1223. (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
  1224. (choice (const :tag "Show closed log items" closed)
  1225. (const :tag "Show clocked log items" clock)
  1226. (const :tag "Show all logged state changes" state)))))
  1227. (defcustom org-agenda-start-with-clockreport-mode nil
  1228. "The initial value of clockreport-mode in a newly created agenda window."
  1229. :group 'org-agenda-startup
  1230. :group 'org-agenda-daily/weekly
  1231. :type 'boolean)
  1232. (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
  1233. "Property list with parameters for the clocktable in clockreport mode.
  1234. This is the display mode that shows a clock table in the daily/weekly
  1235. agenda, the properties for this dynamic block can be set here.
  1236. The usual clocktable parameters are allowed here, but you cannot set
  1237. the properties :name, :tstart, :tend, :block, and :scope - these will
  1238. be overwritten to make sure the content accurately reflects the
  1239. current display in the agenda."
  1240. :group 'org-agenda-daily/weekly
  1241. :type 'plist)
  1242. (defvaralias 'org-agenda-search-view-search-words-only
  1243. 'org-agenda-search-view-always-boolean)
  1244. (defcustom org-agenda-search-view-always-boolean nil
  1245. "Non-nil means the search string is interpreted as individual parts.
  1246. The search string for search view can either be interpreted as a phrase,
  1247. or as a list of snippets that define a boolean search for a number of
  1248. strings.
  1249. When this is non-nil, the string will be split on whitespace, and each
  1250. snippet will be searched individually, and all must match in order to
  1251. select an entry. A snippet is then a single string of non-white
  1252. characters, or a string in double quotes, or a regexp in {} braces.
  1253. If a snippet is preceded by \"-\", the snippet must *not* match.
  1254. \"+\" is syntactic sugar for positive selection. Each snippet may
  1255. be found as a full word or a partial word, but see the variable
  1256. `org-agenda-search-view-force-full-words'.
  1257. When this is nil, search will look for the entire search phrase as one,
  1258. with each space character matching any amount of whitespace, including
  1259. line breaks.
  1260. Even when this is nil, you can still switch to Boolean search dynamically
  1261. by preceding the first snippet with \"+\" or \"-\". If the first snippet
  1262. is a regexp marked with braces like \"{abc}\", this will also switch to
  1263. boolean search."
  1264. :group 'org-agenda-search-view
  1265. :version "24.1"
  1266. :type 'boolean)
  1267. (defcustom org-agenda-search-view-force-full-words nil
  1268. "Non-nil means, search words must be matches as complete words.
  1269. When nil, they may also match part of a word."
  1270. :group 'org-agenda-search-view
  1271. :version "24.1"
  1272. :type 'boolean)
  1273. (defcustom org-agenda-search-view-max-outline-level 0
  1274. "Maximum outline level to display in search view.
  1275. E.g. when this is set to 1, the search view will only
  1276. show headlines of level 1. When set to 0, the default
  1277. value, don't limit agenda view by outline level."
  1278. :group 'org-agenda-search-view
  1279. :version "26.1"
  1280. :package-version '(Org . "8.3")
  1281. :type 'integer)
  1282. (defgroup org-agenda-time-grid nil
  1283. "Options concerning the time grid in the Org Agenda."
  1284. :tag "Org Agenda Time Grid"
  1285. :group 'org-agenda)
  1286. (defcustom org-agenda-search-headline-for-time t
  1287. "Non-nil means search headline for a time-of-day.
  1288. If the headline contains a time-of-day in one format or another, it will
  1289. be used to sort the entry into the time sequence of items for a day.
  1290. Some people have time stamps in the headline that refer to the creation
  1291. time or so, and then this produces an unwanted side effect. If this is
  1292. the case for your, use this variable to turn off searching the headline
  1293. for a time."
  1294. :group 'org-agenda-time-grid
  1295. :type 'boolean)
  1296. (defcustom org-agenda-use-time-grid t
  1297. "Non-nil means show a time grid in the agenda schedule.
  1298. A time grid is a set of lines for specific times (like every two hours between
  1299. 8:00 and 20:00). The items scheduled for a day at specific times are
  1300. sorted in between these lines.
  1301. For details about when the grid will be shown, and what it will look like, see
  1302. the variable `org-agenda-time-grid'."
  1303. :group 'org-agenda-time-grid
  1304. :type 'boolean)
  1305. (defcustom org-agenda-time-grid
  1306. '((daily today require-timed)
  1307. (800 1000 1200 1400 1600 1800 2000)
  1308. "......"
  1309. "----------------")
  1310. "The settings for time grid for agenda display.
  1311. This is a list of four items. The first item is again a list. It contains
  1312. symbols specifying conditions when the grid should be displayed:
  1313. daily if the agenda shows a single day
  1314. weekly if the agenda shows an entire week
  1315. today show grid on current date, independent of daily/weekly display
  1316. require-timed show grid only if at least one item has a time specification
  1317. remove-match skip grid times already present in an entry
  1318. The second item is a list of integers, indicating the times that
  1319. should have a grid line.
  1320. The third item is a string which will be placed right after the
  1321. times that have a grid line.
  1322. The fourth item is a string placed after the grid times. This
  1323. will align with agenda items."
  1324. :group 'org-agenda-time-grid
  1325. :type
  1326. '(list
  1327. (set :greedy t :tag "Grid Display Options"
  1328. (const :tag "Show grid in single day agenda display" daily)
  1329. (const :tag "Show grid in weekly agenda display" weekly)
  1330. (const :tag "Always show grid for today" today)
  1331. (const :tag "Show grid only if any timed entries are present"
  1332. require-timed)
  1333. (const :tag "Skip grid times already present in an entry"
  1334. remove-match))
  1335. (repeat :tag "Grid Times" (integer :tag "Time"))
  1336. (string :tag "Grid String (after agenda times)")
  1337. (string :tag "Grid String (aligns with agenda items)")))
  1338. (defcustom org-agenda-show-current-time-in-grid t
  1339. "Non-nil means show the current time in the time grid."
  1340. :group 'org-agenda-time-grid
  1341. :version "24.1"
  1342. :type 'boolean)
  1343. (defcustom org-agenda-current-time-string
  1344. "now - - - - - - - - - - - - - - - - - - - - - - - - -"
  1345. "The string for the current time marker in the agenda."
  1346. :group 'org-agenda-time-grid
  1347. :version "24.1"
  1348. :type 'string)
  1349. (defgroup org-agenda-sorting nil
  1350. "Options concerning sorting in the Org Agenda."
  1351. :tag "Org Agenda Sorting"
  1352. :group 'org-agenda)
  1353. (defcustom org-agenda-sorting-strategy
  1354. '((agenda habit-down time-up priority-down category-keep)
  1355. (todo priority-down category-keep)
  1356. (tags priority-down category-keep)
  1357. (search category-keep))
  1358. "Sorting structure for the agenda items of a single day.
  1359. This is a list of symbols which will be used in sequence to determine
  1360. if an entry should be listed before another entry. The following
  1361. symbols are recognized:
  1362. time-up Put entries with time-of-day indications first, early first.
  1363. time-down Put entries with time-of-day indications first, late first.
  1364. timestamp-up Sort by any timestamp, early first.
  1365. timestamp-down Sort by any timestamp, late first.
  1366. scheduled-up Sort by scheduled timestamp, early first.
  1367. scheduled-down Sort by scheduled timestamp, late first.
  1368. deadline-up Sort by deadline timestamp, early first.
  1369. deadline-down Sort by deadline timestamp, late first.
  1370. ts-up Sort by active timestamp, early first.
  1371. ts-down Sort by active timestamp, late first.
  1372. tsia-up Sort by inactive timestamp, early first.
  1373. tsia-down Sort by inactive timestamp, late first.
  1374. category-keep Keep the default order of categories, corresponding to the
  1375. sequence in `org-agenda-files'.
  1376. category-up Sort alphabetically by category, A-Z.
  1377. category-down Sort alphabetically by category, Z-A.
  1378. tag-up Sort alphabetically by last tag, A-Z.
  1379. tag-down Sort alphabetically by last tag, Z-A.
  1380. priority-up Sort numerically by priority, high priority last.
  1381. priority-down Sort numerically by priority, high priority first.
  1382. todo-state-up Sort by todo state, tasks that are done last.
  1383. todo-state-down Sort by todo state, tasks that are done first.
  1384. effort-up Sort numerically by estimated effort, high effort last.
  1385. effort-down Sort numerically by estimated effort, high effort first.
  1386. user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
  1387. user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
  1388. habit-up Put entries that are habits first.
  1389. habit-down Put entries that are habits last.
  1390. alpha-up Sort headlines alphabetically.
  1391. alpha-down Sort headlines alphabetically, reversed.
  1392. The different possibilities will be tried in sequence, and testing stops
  1393. if one comparison returns a \"not-equal\". For example, the default
  1394. '(time-up category-keep priority-down)
  1395. means: Pull out all entries having a specified time of day and sort them,
  1396. in order to make a time schedule for the current day the first thing in the
  1397. agenda listing for the day. Of the entries without a time indication, keep
  1398. the grouped in categories, don't sort the categories, but keep them in
  1399. the sequence given in `org-agenda-files'. Within each category sort by
  1400. priority.
  1401. Leaving out `category-keep' would mean that items will be sorted across
  1402. categories by priority.
  1403. Instead of a single list, this can also be a set of list for specific
  1404. contents, with a context symbol in the car of the list, any of
  1405. `agenda', `todo', `tags', `search' for the corresponding agenda views.
  1406. Custom commands can bind this variable in the options section."
  1407. :group 'org-agenda-sorting
  1408. :type `(choice
  1409. (repeat :tag "General" ,org-sorting-choice)
  1410. (list :tag "Individually"
  1411. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1412. (repeat ,org-sorting-choice))
  1413. (cons (const :tag "Strategy for TODO lists" todo)
  1414. (repeat ,org-sorting-choice))
  1415. (cons (const :tag "Strategy for Tags matches" tags)
  1416. (repeat ,org-sorting-choice))
  1417. (cons (const :tag "Strategy for search matches" search)
  1418. (repeat ,org-sorting-choice)))))
  1419. (defcustom org-agenda-cmp-user-defined nil
  1420. "A function to define the comparison `user-defined'.
  1421. This function must receive two arguments, agenda entry a and b.
  1422. If a>b, return +1. If a<b, return -1. If they are equal as seen by
  1423. the user comparison, return nil.
  1424. When this is defined, you can make `user-defined-up' and `user-defined-down'
  1425. part of an agenda sorting strategy."
  1426. :group 'org-agenda-sorting
  1427. :type 'symbol)
  1428. (defcustom org-sort-agenda-notime-is-late t
  1429. "Non-nil means items without time are considered late.
  1430. This is only relevant for sorting. When t, items which have no explicit
  1431. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1432. do have a time. When nil, the default time is before 0:00. You can use this
  1433. option to decide if the schedule for today should come before or after timeless
  1434. agenda entries."
  1435. :group 'org-agenda-sorting
  1436. :type 'boolean)
  1437. (defcustom org-sort-agenda-noeffort-is-high t
  1438. "Non-nil means items without effort estimate are sorted as high effort.
  1439. This also applies when filtering an agenda view with respect to the
  1440. < or > effort operator. Then, tasks with no effort defined will be treated
  1441. as tasks with high effort.
  1442. When nil, such items are sorted as 0 minutes effort."
  1443. :group 'org-agenda-sorting
  1444. :type 'boolean)
  1445. (defgroup org-agenda-line-format nil
  1446. "Options concerning the entry prefix in the Org agenda display."
  1447. :tag "Org Agenda Line Format"
  1448. :group 'org-agenda)
  1449. (defcustom org-agenda-prefix-format
  1450. '((agenda . " %i %-12:c%?-12t% s")
  1451. (todo . " %i %-12:c")
  1452. (tags . " %i %-12:c")
  1453. (search . " %i %-12:c"))
  1454. "Format specifications for the prefix of items in the agenda views.
  1455. An alist with one entry per agenda type. The keys of the
  1456. sublists are `agenda', `todo', `search' and `tags'. The values
  1457. are format strings.
  1458. This format works similar to a printf format, with the following meaning:
  1459. %c the category of the item, \"Diary\" for entries from the diary,
  1460. or as given by the CATEGORY keyword or derived from the file name
  1461. %e the effort required by the item
  1462. %l the level of the item (insert X space(s) if item is of level X)
  1463. %i the icon category of the item, see `org-agenda-category-icon-alist'
  1464. %T the last tag of the item (ignore inherited tags, which come first)
  1465. %t the HH:MM time-of-day specification if one applies to the entry
  1466. %s Scheduling/Deadline information, a short string
  1467. %b show breadcrumbs, i.e., the names of the higher levels
  1468. %(expression) Eval EXPRESSION and replace the control string
  1469. by the result
  1470. All specifiers work basically like the standard `%s' of printf, but may
  1471. contain two additional characters: a question mark just after the `%'
  1472. and a whitespace/punctuation character just before the final letter.
  1473. If the first character after `%' is a question mark, the entire field
  1474. will only be included if the corresponding value applies to the current
  1475. entry. This is useful for fields which should have fixed width when
  1476. present, but zero width when absent. For example, \"%?-12t\" will
  1477. result in a 12 character time field if a time of the day is specified,
  1478. but will completely disappear in entries which do not contain a time.
  1479. If there is punctuation or whitespace character just before the
  1480. final format letter, this character will be appended to the field
  1481. value if the value is not empty. For example, the format
  1482. \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
  1483. the category is empty, no additional colon is inserted.
  1484. The default value for the agenda sublist is \" %-12:c%?-12t% s\",
  1485. which means:
  1486. - Indent the line with two space characters
  1487. - Give the category a 12 chars wide field, padded with whitespace on
  1488. the right (because of `-'). Append a colon if there is a category
  1489. (because of `:').
  1490. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1491. time, don't put in an empty field, just skip it (because of '?').
  1492. - Finally, put the scheduling information.
  1493. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1494. `org-agenda-remove-tags'.
  1495. Custom commands can set this variable in the options section."
  1496. :type '(choice
  1497. (string :tag "General format")
  1498. (list :greedy t :tag "View dependent"
  1499. (cons (const agenda) (string :tag "Format"))
  1500. (cons (const todo) (string :tag "Format"))
  1501. (cons (const tags) (string :tag "Format"))
  1502. (cons (const search) (string :tag "Format"))))
  1503. :group 'org-agenda-line-format
  1504. :version "26.1"
  1505. :package-version '(Org . "9.1"))
  1506. (defcustom org-agenda-breadcrumbs-separator "->"
  1507. "The separator of breadcrumbs in agenda lines."
  1508. :group 'org-agenda-line-format
  1509. :package-version '(Org . "9.3")
  1510. :type 'string
  1511. :safe #'stringp)
  1512. (defvar org-prefix-format-compiled nil
  1513. "The compiled prefix format and associated variables.
  1514. This is a list where first element is a list of variable bindings, and second
  1515. element is the compiled format expression. See the variable
  1516. `org-agenda-prefix-format'.")
  1517. (defcustom org-agenda-todo-keyword-format "%-1s"
  1518. "Format for the TODO keyword in agenda lines.
  1519. Set this to something like \"%-12s\" if you want all TODO keywords
  1520. to occupy a fixed space in the agenda display."
  1521. :group 'org-agenda-line-format
  1522. :type 'string)
  1523. (defcustom org-agenda-diary-sexp-prefix nil
  1524. "A regexp that matches part of a diary sexp entry
  1525. which should be treated as scheduling/deadline information in
  1526. `org-agenda'.
  1527. For example, you can use this to extract the `diary-remind-message' from
  1528. `diary-remind' entries."
  1529. :group 'org-agenda-line-format
  1530. :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
  1531. (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
  1532. "Text preceding timerange entries in the agenda view.
  1533. This is a list with two strings. The first applies when the range
  1534. is entirely on one day. The second applies if the range spans several days.
  1535. The strings may have two \"%d\" format specifiers which will be filled
  1536. with the sequence number of the days, and the total number of days in the
  1537. range, respectively."
  1538. :group 'org-agenda-line-format
  1539. :type '(list
  1540. (string :tag "Deadline today ")
  1541. (choice :tag "Deadline relative"
  1542. (string :tag "Format string")
  1543. (function))))
  1544. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  1545. "Text preceding scheduled items in the agenda view.
  1546. This is a list with two strings. The first applies when the item is
  1547. scheduled on the current day. The second applies when it has been scheduled
  1548. previously, it may contain a %d indicating that this is the nth time that
  1549. this item is scheduled, due to automatic rescheduling of unfinished items
  1550. for the following day. So this number is one larger than the number of days
  1551. that passed since this item was scheduled first."
  1552. :group 'org-agenda-line-format
  1553. :version "24.4"
  1554. :package-version '(Org . "8.0")
  1555. :type '(list
  1556. (string :tag "Scheduled today ")
  1557. (string :tag "Scheduled previously")))
  1558. (defcustom org-agenda-inactive-leader "["
  1559. "Text preceding item pulled into the agenda by inactive time stamps.
  1560. These entries are added to the agenda when pressing \"[\"."
  1561. :group 'org-agenda-line-format
  1562. :version "24.1"
  1563. :type 'string)
  1564. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
  1565. "Text preceding deadline items in the agenda view.
  1566. This is a list with three strings. The first applies when the item has its
  1567. deadline on the current day. The second applies when the deadline is in the
  1568. future, the third one when it is in the past. The strings may contain %d
  1569. to capture the number of days."
  1570. :group 'org-agenda-line-format
  1571. :version "24.4"
  1572. :package-version '(Org . "8.0")
  1573. :type '(list
  1574. (string :tag "Deadline today ")
  1575. (string :tag "Deadline in the future ")
  1576. (string :tag "Deadline in the past ")))
  1577. (defcustom org-agenda-remove-times-when-in-prefix t
  1578. "Non-nil means remove duplicate time specifications in agenda items.
  1579. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1580. time-of-day specification in a headline or diary entry is extracted and
  1581. placed into the prefix. If this option is non-nil, the original specification
  1582. \(a timestamp or -range, or just a plain time(range) specification like
  1583. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1584. cluttered.
  1585. The option can be t or nil. It may also be the symbol `beg', indicating
  1586. that the time should only be removed when it is located at the beginning of
  1587. the headline/diary entry."
  1588. :group 'org-agenda-line-format
  1589. :type '(choice
  1590. (const :tag "Always" t)
  1591. (const :tag "Never" nil)
  1592. (const :tag "When at beginning of entry" beg)))
  1593. (defcustom org-agenda-remove-timeranges-from-blocks nil
  1594. "Non-nil means remove time ranges specifications in agenda
  1595. items that span on several days."
  1596. :group 'org-agenda-line-format
  1597. :version "24.1"
  1598. :type 'boolean)
  1599. (defcustom org-agenda-default-appointment-duration nil
  1600. "Default duration for appointments that only have a starting time.
  1601. When nil, no duration is specified in such cases.
  1602. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  1603. :group 'org-agenda-line-format
  1604. :type '(choice
  1605. (integer :tag "Minutes")
  1606. (const :tag "No default duration")))
  1607. (defcustom org-agenda-show-inherited-tags t
  1608. "Non-nil means show inherited tags in each agenda line.
  1609. When this option is set to `always', it takes precedence over
  1610. `org-agenda-use-tag-inheritance' and inherited tags are shown
  1611. in every agenda.
  1612. When this option is set to t (the default), inherited tags are
  1613. shown when they are available, i.e. when the value of
  1614. `org-agenda-use-tag-inheritance' enables tag inheritance for the
  1615. given agenda type.
  1616. This can be set to a list of agenda types in which the agenda
  1617. must display the inherited tags. Available types are `todo',
  1618. `agenda' and `search'.
  1619. When set to nil, never show inherited tags in agenda lines."
  1620. :group 'org-agenda-line-format
  1621. :group 'org-agenda
  1622. :version "24.3"
  1623. :type '(choice
  1624. (const :tag "Show inherited tags when available" t)
  1625. (const :tag "Always show inherited tags" always)
  1626. (repeat :tag "Show inherited tags only in selected agenda types"
  1627. (symbol :tag "Agenda type"))))
  1628. (defcustom org-agenda-use-tag-inheritance '(todo search agenda)
  1629. "List of agenda view types where to use tag inheritance.
  1630. In tags/tags-todo/tags-tree agenda views, tag inheritance is
  1631. controlled by `org-use-tag-inheritance'. In other agenda types,
  1632. `org-use-tag-inheritance' is not used for the selection of the
  1633. agenda entries. Still, you may want the agenda to be aware of
  1634. the inherited tags anyway, e.g. for later tag filtering.
  1635. Allowed value are `todo', `search' and `agenda'.
  1636. This variable has no effect if `org-agenda-show-inherited-tags'
  1637. is set to `always'. In that case, the agenda is aware of those
  1638. tags.
  1639. The default value sets tags in every agenda type. Setting this
  1640. option to nil will speed up non-tags agenda view a lot."
  1641. :group 'org-agenda
  1642. :version "26.1"
  1643. :package-version '(Org . "9.1")
  1644. :type '(choice
  1645. (const :tag "Use tag inheritance in all agenda types" t)
  1646. (repeat :tag "Use tag inheritance in selected agenda types"
  1647. (symbol :tag "Agenda type"))))
  1648. (defcustom org-agenda-hide-tags-regexp nil
  1649. "Regular expression used to filter away specific tags in agenda views.
  1650. This means that these tags will be present, but not be shown in the agenda
  1651. line. Secondary filtering will still work on the hidden tags.
  1652. Nil means don't hide any tags."
  1653. :group 'org-agenda-line-format
  1654. :type '(choice
  1655. (const :tag "Hide none" nil)
  1656. (string :tag "Regexp ")))
  1657. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  1658. 'org-agenda-remove-tags)
  1659. (defcustom org-agenda-remove-tags nil
  1660. "Non-nil means remove the tags from the headline copy in the agenda.
  1661. When this is the symbol `prefix', only remove tags when
  1662. `org-agenda-prefix-format' contains a `%T' specifier."
  1663. :group 'org-agenda-line-format
  1664. :type '(choice
  1665. (const :tag "Always" t)
  1666. (const :tag "Never" nil)
  1667. (const :tag "When prefix format contains %T" prefix)))
  1668. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
  1669. (defcustom org-agenda-tags-column 'auto
  1670. "Shift tags in agenda items to this column.
  1671. If set to `auto', tags will be automatically aligned to the right
  1672. edge of the window.
  1673. If set to a positive number, tags will be left-aligned to that
  1674. column. If set to a negative number, tags will be right-aligned
  1675. to that column. For example, -80 works well for a normal 80
  1676. character screen."
  1677. :group 'org-agenda-line-format
  1678. :type '(choice
  1679. (const :tag "Automatically align to right edge of window" auto)
  1680. (integer :tag "Specific column" -80))
  1681. :package-version '(Org . "9.1")
  1682. :version "26.1")
  1683. (defcustom org-agenda-fontify-priorities 'cookies
  1684. "Non-nil means highlight low and high priorities in agenda.
  1685. When t, the highest priority entries are bold, lowest priority italic.
  1686. However, settings in `org-priority-faces' will overrule these faces.
  1687. When this variable is the symbol `cookies', only fontify the
  1688. cookies, not the entire task.
  1689. This may also be an association list of priority faces, whose
  1690. keys are the character values of `org-highest-priority',
  1691. `org-default-priority', and `org-lowest-priority' (the default values
  1692. are ?A, ?B, and ?C, respectively). The face may be a named face, a
  1693. color as a string, or a list like `(:background \"Red\")'.
  1694. If it is a color, the variable `org-faces-easy-properties'
  1695. determines if it is a foreground or a background color."
  1696. :group 'org-agenda-line-format
  1697. :type '(choice
  1698. (const :tag "Never" nil)
  1699. (const :tag "Defaults" t)
  1700. (const :tag "Cookies only" cookies)
  1701. (repeat :tag "Specify"
  1702. (list (character :tag "Priority" :value ?A)
  1703. (choice :tag "Face "
  1704. (string :tag "Color")
  1705. (sexp :tag "Face"))))))
  1706. (defcustom org-agenda-day-face-function nil
  1707. "Function called to determine what face should be used to display a day.
  1708. The only argument passed to that function is the day. It should
  1709. returns a face, or nil if does not want to specify a face and let
  1710. the normal rules apply."
  1711. :group 'org-agenda-line-format
  1712. :version "24.1"
  1713. :type '(choice (const nil) (function)))
  1714. (defcustom org-agenda-category-icon-alist nil
  1715. "Alist of category icon to be displayed in agenda views.
  1716. Each entry should have the following format:
  1717. (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
  1718. Where CATEGORY-REGEXP is a regexp matching the categories where
  1719. the icon should be displayed.
  1720. FILE-OR-DATA either a file path or a string containing image data.
  1721. The other fields can be omitted safely if not needed:
  1722. TYPE indicates the image type.
  1723. DATA-P is a boolean indicating whether the FILE-OR-DATA string is
  1724. image data.
  1725. PROPS are additional image attributes to assign to the image,
  1726. like, e.g. `:ascent center'.
  1727. (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
  1728. If you want to set the display properties yourself, just put a
  1729. list as second element:
  1730. (CATEGORY-REGEXP (MY PROPERTY LIST))
  1731. For example, to display a 16px horizontal space for Emacs
  1732. category, you can use:
  1733. (\"Emacs\" \\='(space . (:width (16))))"
  1734. :group 'org-agenda-line-format
  1735. :version "24.1"
  1736. :type '(alist :key-type (string :tag "Regexp matching category")
  1737. :value-type (choice (list :tag "Icon"
  1738. (string :tag "File or data")
  1739. (symbol :tag "Type")
  1740. (boolean :tag "Data?")
  1741. (repeat :tag "Extra image properties" :inline t symbol))
  1742. (list :tag "Display properties" sexp))))
  1743. (defgroup org-agenda-column-view nil
  1744. "Options concerning column view in the agenda."
  1745. :tag "Org Agenda Column View"
  1746. :group 'org-agenda)
  1747. (defcustom org-agenda-view-columns-initially nil
  1748. "When non-nil, switch to columns view right after creating the agenda."
  1749. :group 'org-agenda-column-view
  1750. :type 'boolean
  1751. :version "26.1"
  1752. :package-version '(Org . "9.0")
  1753. :safe #'booleanp)
  1754. (defcustom org-agenda-columns-show-summaries t
  1755. "Non-nil means show summaries for columns displayed in the agenda view."
  1756. :group 'org-agenda-column-view
  1757. :type 'boolean)
  1758. (defcustom org-agenda-columns-compute-summary-properties t
  1759. "Non-nil means recompute all summary properties before column view.
  1760. When column view in the agenda is listing properties that have a summary
  1761. operator, it can go to all relevant buffers and recompute the summaries
  1762. there. This can mean overhead for the agenda column view, but is necessary
  1763. to have thing up to date.
  1764. As a special case, a CLOCKSUM property also makes sure that the clock
  1765. computations are current."
  1766. :group 'org-agenda-column-view
  1767. :type 'boolean)
  1768. (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
  1769. "Non-nil means the duration of an appointment will add to day effort.
  1770. The property to which appointment durations will be added is the one given
  1771. in the option `org-effort-property'. If an appointment does not have
  1772. an end time, `org-agenda-default-appointment-duration' will be used. If that
  1773. is not set, an appointment without end time will not contribute to the time
  1774. estimate."
  1775. :group 'org-agenda-column-view
  1776. :type 'boolean)
  1777. (defcustom org-agenda-auto-exclude-function nil
  1778. "A function called with a tag to decide if it is filtered on \
  1779. \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
  1780. The sole argument to the function, which is called once for each
  1781. possible tag, is a string giving the name of the tag. The
  1782. function should return either nil if the tag should be included
  1783. as normal, \"-<TAG>\" to exclude the tag, or \"+<TAG>\" to exclude
  1784. lines not carrying this tag.
  1785. Note that for the purpose of tag filtering, only the lower-case version of
  1786. all tags will be considered, so that this function will only ever see
  1787. the lower-case version of all tags."
  1788. :group 'org-agenda
  1789. :type '(choice (const nil) (function)))
  1790. (defcustom org-agenda-bulk-custom-functions nil
  1791. "Alist of characters and custom functions for bulk actions.
  1792. For example, this value makes those two functions available:
  1793. \\='((?R set-category)
  1794. (?C bulk-cut))
  1795. With selected entries in an agenda buffer, `B R' will call
  1796. the custom function `set-category' on the selected entries.
  1797. Note that functions in this alist don't need to be quoted."
  1798. :type '(alist :key-type character :value-type (group function))
  1799. :version "24.1"
  1800. :group 'org-agenda)
  1801. (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
  1802. "Execute BODY with point at location given by `org-hd-marker' property.
  1803. If STRING is non-nil, the text property will be fetched from position 0
  1804. in that string. If STRING is nil, it will be fetched from the beginning
  1805. of the current line."
  1806. (org-with-gensyms (marker)
  1807. `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
  1808. 'org-hd-marker ,string)))
  1809. (with-current-buffer (marker-buffer ,marker)
  1810. (save-excursion
  1811. (goto-char ,marker)
  1812. ,@body)))))
  1813. (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
  1814. (defun org-add-agenda-custom-command (entry)
  1815. "Replace or add a command in `org-agenda-custom-commands'.
  1816. This is mostly for hacking and trying a new command - once the command
  1817. works you probably want to add it to `org-agenda-custom-commands' for good."
  1818. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  1819. (if ass
  1820. (setcdr ass (cdr entry))
  1821. (push entry org-agenda-custom-commands))))
  1822. (defmacro org-agenda--insert-overriding-header (default)
  1823. "Insert header into agenda view.
  1824. The inserted header depends on `org-agenda-overriding-header'.
  1825. If the empty string, don't insert a header. If any other string,
  1826. insert it as a header. If nil, insert DEFAULT, which should
  1827. evaluate to a string."
  1828. (declare (debug (form)) (indent defun))
  1829. `(cond
  1830. ((not org-agenda-overriding-header) (insert ,default))
  1831. ((equal org-agenda-overriding-header "") nil)
  1832. ((stringp org-agenda-overriding-header)
  1833. (insert (propertize org-agenda-overriding-header
  1834. 'face 'org-agenda-structure)
  1835. "\n"))
  1836. (t (user-error "Invalid value for `org-agenda-overriding-header': %S"
  1837. org-agenda-overriding-header))))
  1838. ;;; Define the org-agenda-mode
  1839. (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
  1840. (defvar org-agenda-mode-map (make-sparse-keymap)
  1841. "Keymap for `org-agenda-mode'.")
  1842. (defvar org-agenda-menu) ; defined later in this file.
  1843. (defvar org-agenda-restrict nil) ; defined later in this file.
  1844. (defvar org-agenda-follow-mode nil)
  1845. (defvar org-agenda-entry-text-mode nil)
  1846. (defvar org-agenda-clockreport-mode nil)
  1847. (defvar org-agenda-show-log nil
  1848. "When non-nil, show the log in the agenda.
  1849. Do not set this directly; instead use
  1850. `org-agenda-start-with-log-mode', which see.")
  1851. (defvar org-agenda-redo-command nil)
  1852. (defvar org-agenda-query-string nil)
  1853. (defvar org-agenda-mode-hook nil
  1854. "Hook run after `org-agenda-mode' is turned on.
  1855. The buffer is still writable when this hook is called.")
  1856. (defvar org-agenda-type nil)
  1857. (defvar org-agenda-force-single-file nil)
  1858. (defvar org-agenda-bulk-marked-entries nil
  1859. "List of markers that refer to marked entries in the agenda.")
  1860. (defvar org-agenda-current-date nil
  1861. "Active date when building the agenda.")
  1862. ;;; Multiple agenda buffers support
  1863. (defcustom org-agenda-sticky nil
  1864. "Non-nil means agenda q key will bury agenda buffers.
  1865. Agenda commands will then show existing buffer instead of generating new ones.
  1866. When nil, `q' will kill the single agenda buffer."
  1867. :group 'org-agenda
  1868. :version "24.3"
  1869. :type 'boolean)
  1870. ;;;###autoload
  1871. (defun org-toggle-sticky-agenda (&optional arg)
  1872. "Toggle `org-agenda-sticky'."
  1873. (interactive "P")
  1874. (let ((new-value (if arg
  1875. (> (prefix-numeric-value arg) 0)
  1876. (not org-agenda-sticky))))
  1877. (if (equal new-value org-agenda-sticky)
  1878. (and (called-interactively-p 'interactive)
  1879. (message "Sticky agenda was already %s"
  1880. (if org-agenda-sticky "enabled" "disabled")))
  1881. (setq org-agenda-sticky new-value)
  1882. (org-agenda-kill-all-agenda-buffers)
  1883. (and (called-interactively-p 'interactive)
  1884. (message "Sticky agenda %s"
  1885. (if org-agenda-sticky "enabled" "disabled"))))))
  1886. (defvar org-agenda-buffer nil
  1887. "Agenda buffer currently being generated.")
  1888. (defvar org-agenda-last-prefix-arg nil)
  1889. (defvar org-agenda-this-buffer-name nil)
  1890. (defvar org-agenda-doing-sticky-redo nil)
  1891. (defvar org-agenda-this-buffer-is-sticky nil)
  1892. (defvar org-agenda-last-indirect-buffer nil
  1893. "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
  1894. (defconst org-agenda-local-vars
  1895. '(org-agenda-this-buffer-name
  1896. org-agenda-undo-list
  1897. org-agenda-pending-undo-list
  1898. org-agenda-follow-mode
  1899. org-agenda-entry-text-mode
  1900. org-agenda-clockreport-mode
  1901. org-agenda-show-log
  1902. org-agenda-redo-command
  1903. org-agenda-query-string
  1904. org-agenda-type
  1905. org-agenda-bulk-marked-entries
  1906. org-agenda-undo-has-started-in
  1907. org-agenda-info
  1908. org-agenda-pre-window-conf
  1909. org-agenda-columns-active
  1910. org-agenda-tag-filter
  1911. org-agenda-category-filter
  1912. org-agenda-top-headline-filter
  1913. org-agenda-regexp-filter
  1914. org-agenda-effort-filter
  1915. org-agenda-markers
  1916. org-agenda-last-search-view-search-was-boolean
  1917. org-agenda-last-indirect-buffer
  1918. org-agenda-filtered-by-category
  1919. org-agenda-filter-form
  1920. org-agenda-cycle-counter
  1921. org-agenda-last-prefix-arg)
  1922. "Variables that must be local in agenda buffers to allow multiple buffers.")
  1923. (defun org-agenda-mode ()
  1924. "Mode for time-sorted view on action items in Org files.
  1925. The following commands are available:
  1926. \\{org-agenda-mode-map}"
  1927. (interactive)
  1928. (ignore-errors (require 'face-remap))
  1929. (let ((agenda-local-vars-to-keep
  1930. '(text-scale-mode-amount
  1931. text-scale-mode
  1932. text-scale-mode-lighter
  1933. face-remapping-alist))
  1934. (save (buffer-local-variables)))
  1935. (kill-all-local-variables)
  1936. (cl-flet ((reset-saved (var-set)
  1937. "Reset variables in VAR-SET to possibly stored value in SAVE."
  1938. (dolist (elem save)
  1939. (pcase elem
  1940. (`(,var . ,val) ;ignore unbound variables
  1941. (when (and val (memq var var-set))
  1942. (set var val)))))))
  1943. (cond (org-agenda-doing-sticky-redo
  1944. ;; Refreshing sticky agenda-buffer
  1945. ;;
  1946. ;; Preserve the value of `org-agenda-local-vars' variables.
  1947. (mapc #'make-local-variable org-agenda-local-vars)
  1948. (reset-saved org-agenda-local-vars)
  1949. (setq-local org-agenda-this-buffer-is-sticky t))
  1950. (org-agenda-sticky
  1951. ;; Creating a sticky Agenda buffer for the first time
  1952. (mapc 'make-local-variable org-agenda-local-vars)
  1953. (setq-local org-agenda-this-buffer-is-sticky t))
  1954. (t
  1955. ;; Creating a non-sticky agenda buffer
  1956. (setq-local org-agenda-this-buffer-is-sticky nil)))
  1957. (mapc #'make-local-variable agenda-local-vars-to-keep)
  1958. (reset-saved agenda-local-vars-to-keep)))
  1959. (setq org-agenda-undo-list nil
  1960. org-agenda-pending-undo-list nil
  1961. org-agenda-bulk-marked-entries nil)
  1962. (setq major-mode 'org-agenda-mode)
  1963. ;; Keep global-font-lock-mode from turning on font-lock-mode
  1964. (setq-local font-lock-global-modes (list 'not major-mode))
  1965. (setq mode-name "Org-Agenda")
  1966. (setq indent-tabs-mode nil)
  1967. (use-local-map org-agenda-mode-map)
  1968. (easy-menu-add org-agenda-menu)
  1969. (when org-startup-truncated (setq truncate-lines t))
  1970. (setq-local line-move-visual nil)
  1971. (add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
  1972. (add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  1973. ;; Make sure properties are removed when copying text
  1974. (if (boundp 'filter-buffer-substring-functions)
  1975. (add-hook 'filter-buffer-substring-functions
  1976. (lambda (fun start end delete)
  1977. (substring-no-properties (funcall fun start end delete)))
  1978. nil t)
  1979. ;; Emacs >= 24.4.
  1980. (add-function :filter-return (local 'filter-buffer-substring-function)
  1981. #'substring-no-properties))
  1982. (unless org-agenda-keep-modes
  1983. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  1984. org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
  1985. org-agenda-show-log org-agenda-start-with-log-mode
  1986. org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode))
  1987. (add-to-invisibility-spec '(org-filtered))
  1988. (add-to-invisibility-spec '(org-link))
  1989. (easy-menu-change
  1990. '("Agenda") "Agenda Files"
  1991. (append
  1992. (list
  1993. (vector
  1994. (if (get 'org-agenda-files 'org-restrict)
  1995. "Restricted to single file"
  1996. "Edit File List")
  1997. '(org-edit-agenda-file-list)
  1998. (not (get 'org-agenda-files 'org-restrict)))
  1999. "--")
  2000. (mapcar 'org-file-menu-entry (org-agenda-files))))
  2001. (org-agenda-set-mode-name)
  2002. (apply
  2003. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  2004. (list 'org-agenda-mode-hook)))
  2005. (substitute-key-definition 'undo 'org-agenda-undo
  2006. org-agenda-mode-map global-map)
  2007. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  2008. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  2009. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  2010. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  2011. (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
  2012. (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
  2013. (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
  2014. (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
  2015. (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
  2016. (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
  2017. (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
  2018. (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
  2019. (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
  2020. (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
  2021. (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
  2022. (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
  2023. (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
  2024. (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
  2025. (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
  2026. (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
  2027. (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
  2028. (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
  2029. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  2030. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  2031. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  2032. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  2033. (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
  2034. (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
  2035. (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
  2036. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  2037. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  2038. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  2039. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  2040. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  2041. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  2042. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  2043. (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
  2044. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  2045. (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
  2046. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  2047. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  2048. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  2049. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  2050. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  2051. (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
  2052. (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
  2053. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
  2054. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
  2055. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
  2056. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
  2057. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  2058. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  2059. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  2060. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  2061. (while l (org-defkey org-agenda-mode-map
  2062. (int-to-string (pop l)) 'digit-argument)))
  2063. (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
  2064. (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
  2065. (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
  2066. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  2067. (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
  2068. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  2069. (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
  2070. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  2071. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  2072. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo-all)
  2073. (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
  2074. (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
  2075. (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
  2076. 'org-clock-modify-effort-estimate)
  2077. (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
  2078. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  2079. (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
  2080. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  2081. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
  2082. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  2083. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  2084. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  2085. (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
  2086. (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
  2087. (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
  2088. (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
  2089. (substitute-key-definition 'next-line 'org-agenda-next-line
  2090. org-agenda-mode-map global-map)
  2091. (substitute-key-definition 'previous-line 'org-agenda-previous-line
  2092. org-agenda-mode-map global-map)
  2093. (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
  2094. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  2095. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  2096. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  2097. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  2098. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  2099. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  2100. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  2101. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  2102. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  2103. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  2104. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  2105. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  2106. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  2107. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  2108. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  2109. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  2110. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  2111. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  2112. (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
  2113. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  2114. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  2115. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  2116. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  2117. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  2118. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  2119. (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
  2120. (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
  2121. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  2122. (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  2123. (org-defkey org-agenda-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock-from-agenda)
  2124. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  2125. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  2126. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  2127. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  2128. (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag)
  2129. (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
  2130. (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
  2131. (org-defkey org-agenda-mode-map "/" 'org-agenda-filter)
  2132. (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
  2133. (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
  2134. (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
  2135. (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
  2136. (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
  2137. (org-defkey org-agenda-mode-map "\C-c\C-x_" 'org-timer-stop)
  2138. (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
  2139. (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  2140. (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  2141. (org-defkey org-agenda-mode-map "\C-c\C-xI" 'org-info-find-node)
  2142. (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
  2143. (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
  2144. (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
  2145. (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
  2146. (when org-agenda-mouse-1-follows-link
  2147. (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
  2148. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  2149. '("Agenda"
  2150. ("Agenda Files")
  2151. "--"
  2152. ("Agenda Dates"
  2153. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda)]
  2154. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  2155. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  2156. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
  2157. "--"
  2158. ("View"
  2159. ["Day View" org-agenda-day-view
  2160. :active (org-agenda-check-type nil 'agenda)
  2161. :style radio :selected (eq org-agenda-current-span 'day)
  2162. :keys "v d (or just d)"]
  2163. ["Week View" org-agenda-week-view
  2164. :active (org-agenda-check-type nil 'agenda)
  2165. :style radio :selected (eq org-agenda-current-span 'week)
  2166. :keys "v w"]
  2167. ["Fortnight View" org-agenda-fortnight-view
  2168. :active (org-agenda-check-type nil 'agenda)
  2169. :style radio :selected (eq org-agenda-current-span 'fortnight)
  2170. :keys "v t"]
  2171. ["Month View" org-agenda-month-view
  2172. :active (org-agenda-check-type nil 'agenda)
  2173. :style radio :selected (eq org-agenda-current-span 'month)
  2174. :keys "v m"]
  2175. ["Year View" org-agenda-year-view
  2176. :active (org-agenda-check-type nil 'agenda)
  2177. :style radio :selected (eq org-agenda-current-span 'year)
  2178. :keys "v y"]
  2179. "--"
  2180. ["Include Diary" org-agenda-toggle-diary
  2181. :style toggle :selected org-agenda-include-diary
  2182. :active (org-agenda-check-type nil 'agenda)]
  2183. ["Include Deadlines" org-agenda-toggle-deadlines
  2184. :style toggle :selected org-agenda-include-deadlines
  2185. :active (org-agenda-check-type nil 'agenda)]
  2186. ["Use Time Grid" org-agenda-toggle-time-grid
  2187. :style toggle :selected org-agenda-use-time-grid
  2188. :active (org-agenda-check-type nil 'agenda)]
  2189. "--"
  2190. ["Show clock report" org-agenda-clockreport-mode
  2191. :style toggle :selected org-agenda-clockreport-mode
  2192. :active (org-agenda-check-type nil 'agenda)]
  2193. ["Show some entry text" org-agenda-entry-text-mode
  2194. :style toggle :selected org-agenda-entry-text-mode
  2195. :active t]
  2196. "--"
  2197. ["Show Logbook entries" org-agenda-log-mode
  2198. :style toggle :selected org-agenda-show-log
  2199. :active (org-agenda-check-type nil 'agenda)
  2200. :keys "v l (or just l)"]
  2201. ["Include archived trees" org-agenda-archives-mode
  2202. :style toggle :selected org-agenda-archives-mode :active t
  2203. :keys "v a"]
  2204. ["Include archive files" (org-agenda-archives-mode t)
  2205. :style toggle :selected (eq org-agenda-archives-mode t) :active t
  2206. :keys "v A"]
  2207. "--"
  2208. ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
  2209. ("Filter current view"
  2210. ["with generic interface" org-agenda-filter t]
  2211. "--"
  2212. ["by category at cursor" org-agenda-filter-by-category t]
  2213. ["by tag" org-agenda-filter-by-tag t]
  2214. ["by effort" org-agenda-filter-by-effort t]
  2215. ["by regexp" org-agenda-filter-by-regexp t]
  2216. ["by top-level headline" org-agenda-filter-by-top-headline t]
  2217. "--"
  2218. ["Remove all filtering" org-agenda-filter-remove-all t]
  2219. "--"
  2220. ["limit" org-agenda-limit-interactively t])
  2221. ["Rebuild buffer" org-agenda-redo t]
  2222. ["Write view to file" org-agenda-write t]
  2223. ["Save all Org buffers" org-save-all-org-buffers t]
  2224. "--"
  2225. ["Show original entry" org-agenda-show t]
  2226. ["Go To (other window)" org-agenda-goto t]
  2227. ["Go To (this window)" org-agenda-switch-to t]
  2228. ["Capture with cursor date" org-agenda-capture t]
  2229. ["Follow Mode" org-agenda-follow-mode
  2230. :style toggle :selected org-agenda-follow-mode :active t]
  2231. ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  2232. "--"
  2233. ("TODO"
  2234. ["Cycle TODO" org-agenda-todo t]
  2235. ["Next TODO set" org-agenda-todo-nextset t]
  2236. ["Previous TODO set" org-agenda-todo-previousset t]
  2237. ["Add note" org-agenda-add-note t])
  2238. ("Archive/Refile/Delete"
  2239. ["Archive default" org-agenda-archive-default t]
  2240. ["Archive default" org-agenda-archive-default-with-confirmation t]
  2241. ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
  2242. ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
  2243. ["Archive subtree" org-agenda-archive t]
  2244. "--"
  2245. ["Refile" org-agenda-refile t]
  2246. "--"
  2247. ["Delete subtree" org-agenda-kill t])
  2248. ("Bulk action"
  2249. ["Mark entry" org-agenda-bulk-mark t]
  2250. ["Mark all" org-agenda-bulk-mark-all t]
  2251. ["Unmark entry" org-agenda-bulk-unmark t]
  2252. ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
  2253. ["Toggle mark" org-agenda-bulk-toggle t]
  2254. ["Toggle all" org-agenda-bulk-toggle-all t]
  2255. ["Mark regexp" org-agenda-bulk-mark-regexp t])
  2256. ["Act on all marked" org-agenda-bulk-action t]
  2257. "--"
  2258. ("Tags and Properties"
  2259. ["Show all Tags" org-agenda-show-tags t]
  2260. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  2261. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  2262. "--"
  2263. ["Column View" org-columns t])
  2264. ("Deadline/Schedule"
  2265. ["Schedule" org-agenda-schedule t]
  2266. ["Set Deadline" org-agenda-deadline t]
  2267. "--"
  2268. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda)]
  2269. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda)]
  2270. ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u S-right"]
  2271. ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u S-left"]
  2272. ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-right"]
  2273. ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-left"]
  2274. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda)])
  2275. ("Clock and Effort"
  2276. ["Clock in" org-agenda-clock-in t]
  2277. ["Clock out" org-agenda-clock-out t]
  2278. ["Clock cancel" org-agenda-clock-cancel t]
  2279. ["Goto running clock" org-clock-goto t]
  2280. "--"
  2281. ["Set Effort" org-agenda-set-effort t]
  2282. ["Change clocked effort" org-clock-modify-effort-estimate
  2283. (org-clock-is-active)])
  2284. ("Priority"
  2285. ["Set Priority" org-agenda-priority t]
  2286. ["Increase Priority" org-agenda-priority-up t]
  2287. ["Decrease Priority" org-agenda-priority-down t]
  2288. ["Show Priority" org-show-priority t])
  2289. ("Calendar/Diary"
  2290. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda)]
  2291. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda)]
  2292. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda)]
  2293. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda)]
  2294. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda)]
  2295. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda)]
  2296. "--"
  2297. ["Create iCalendar File" org-icalendar-combine-agenda-files t])
  2298. "--"
  2299. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  2300. "--"
  2301. ("MobileOrg"
  2302. ["Push Files and Views" org-mobile-push t]
  2303. ["Get Captured and Flagged" org-mobile-pull t]
  2304. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "\\[org-agenda] ?"]
  2305. ["Show note / unflag" org-agenda-show-the-flagging-note t]
  2306. "--"
  2307. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  2308. "--"
  2309. ["Quit" org-agenda-quit t]
  2310. ["Exit and Release Buffers" org-agenda-exit t]
  2311. ))
  2312. ;;; Agenda undo
  2313. (defvar org-agenda-allow-remote-undo t
  2314. "Non-nil means allow remote undo from the agenda buffer.")
  2315. (defvar org-agenda-undo-has-started-in nil
  2316. "Buffers that have already seen `undo-start' in the current undo sequence.")
  2317. (defun org-agenda-undo ()
  2318. "Undo a remote editing step in the agenda.
  2319. This undoes changes both in the agenda buffer and in the remote buffer
  2320. that have been changed along."
  2321. (interactive)
  2322. (or org-agenda-allow-remote-undo
  2323. (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
  2324. (when (not (eq this-command last-command))
  2325. (setq org-agenda-undo-has-started-in nil
  2326. org-agenda-pending-undo-list org-agenda-undo-list))
  2327. (when (not org-agenda-pending-undo-list)
  2328. (user-error "No further undo information"))
  2329. (let* ((entry (pop org-agenda-pending-undo-list))
  2330. buf line cmd rembuf)
  2331. (setq cmd (pop entry) line (pop entry))
  2332. (setq rembuf (nth 2 entry))
  2333. (org-with-remote-undo rembuf
  2334. (while (bufferp (setq buf (pop entry)))
  2335. (when (pop entry)
  2336. (with-current-buffer buf
  2337. (let ((last-undo-buffer buf)
  2338. (inhibit-read-only t))
  2339. (unless (memq buf org-agenda-undo-has-started-in)
  2340. (push buf org-agenda-undo-has-started-in)
  2341. (make-local-variable 'pending-undo-list)
  2342. (undo-start))
  2343. (while (and pending-undo-list
  2344. (listp pending-undo-list)
  2345. (not (car pending-undo-list)))
  2346. (pop pending-undo-list))
  2347. (undo-more 1))))))
  2348. (org-goto-line line)
  2349. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  2350. (defun org-verify-change-for-undo (l1 l2)
  2351. "Verify that a real change occurred between the undo lists L1 and L2."
  2352. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  2353. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  2354. (not (eq l1 l2)))
  2355. ;;; Agenda dispatch
  2356. (defvar org-agenda-restrict-begin (make-marker))
  2357. (defvar org-agenda-restrict-end (make-marker))
  2358. (defvar org-agenda-last-dispatch-buffer nil)
  2359. (defvar org-agenda-overriding-restriction nil)
  2360. (defcustom org-agenda-custom-commands-contexts nil
  2361. "Alist of custom agenda keys and contextual rules.
  2362. For example, if you have a custom agenda command \"p\" and you
  2363. want this command to be accessible only from plain text files,
  2364. use this:
  2365. \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
  2366. Here are the available contexts definitions:
  2367. in-file: command displayed only in matching files
  2368. in-mode: command displayed only in matching modes
  2369. not-in-file: command not displayed in matching files
  2370. not-in-mode: command not displayed in matching modes
  2371. in-buffer: command displayed only in matching buffers
  2372. not-in-buffer: command not displayed in matching buffers
  2373. [function]: a custom function taking no argument
  2374. If you define several checks, the agenda command will be
  2375. accessible if there is at least one valid check.
  2376. You can also bind a key to another agenda custom command
  2377. depending on contextual rules.
  2378. \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
  2379. Here it means: in .txt files, use \"p\" as the key for the
  2380. agenda command otherwise associated with \"q\". (The command
  2381. originally associated with \"q\" is not displayed to avoid
  2382. duplicates.)"
  2383. :version "24.3"
  2384. :group 'org-agenda-custom-commands
  2385. :type '(repeat (list :tag "Rule"
  2386. (string :tag " Agenda key")
  2387. (string :tag "Replace by command")
  2388. (repeat :tag "Available when"
  2389. (choice
  2390. (cons :tag "Condition"
  2391. (choice
  2392. (const :tag "In file" in-file)
  2393. (const :tag "Not in file" not-in-file)
  2394. (const :tag "In buffer" in-buffer)
  2395. (const :tag "Not in buffer" not-in-buffer)
  2396. (const :tag "In mode" in-mode)
  2397. (const :tag "Not in mode" not-in-mode))
  2398. (regexp))
  2399. (function :tag "Custom function"))))))
  2400. (defcustom org-agenda-max-entries nil
  2401. "Maximum number of entries to display in an agenda.
  2402. This can be nil (no limit) or an integer or an alist of agenda
  2403. types with an associated number of entries to display in this
  2404. type."
  2405. :version "24.4"
  2406. :package-version '(Org . "8.0")
  2407. :group 'org-agenda-custom-commands
  2408. :type '(choice (symbol :tag "No limit" nil)
  2409. (integer :tag "Max number of entries")
  2410. (repeat
  2411. (cons (choice :tag "Agenda type"
  2412. (const agenda)
  2413. (const todo)
  2414. (const tags)
  2415. (const search))
  2416. (integer :tag "Max number of entries")))))
  2417. (defcustom org-agenda-max-todos nil
  2418. "Maximum number of TODOs to display in an agenda.
  2419. This can be nil (no limit) or an integer or an alist of agenda
  2420. types with an associated number of entries to display in this
  2421. type."
  2422. :version "24.4"
  2423. :package-version '(Org . "8.0")
  2424. :group 'org-agenda-custom-commands
  2425. :type '(choice (symbol :tag "No limit" nil)
  2426. (integer :tag "Max number of TODOs")
  2427. (repeat
  2428. (cons (choice :tag "Agenda type"
  2429. (const agenda)
  2430. (const todo)
  2431. (const tags)
  2432. (const search))
  2433. (integer :tag "Max number of TODOs")))))
  2434. (defcustom org-agenda-max-tags nil
  2435. "Maximum number of tagged entries to display in an agenda.
  2436. This can be nil (no limit) or an integer or an alist of agenda
  2437. types with an associated number of entries to display in this
  2438. type."
  2439. :version "24.4"
  2440. :package-version '(Org . "8.0")
  2441. :group 'org-agenda-custom-commands
  2442. :type '(choice (symbol :tag "No limit" nil)
  2443. (integer :tag "Max number of tagged entries")
  2444. (repeat
  2445. (cons (choice :tag "Agenda type"
  2446. (const agenda)
  2447. (const todo)
  2448. (const tags)
  2449. (const search))
  2450. (integer :tag "Max number of tagged entries")))))
  2451. (defcustom org-agenda-max-effort nil
  2452. "Maximum cumulated effort duration for the agenda.
  2453. This can be nil (no limit) or a number of minutes (as an integer)
  2454. or an alist of agenda types with an associated number of minutes
  2455. to limit entries to in this type."
  2456. :version "24.4"
  2457. :package-version '(Org . "8.0")
  2458. :group 'org-agenda-custom-commands
  2459. :type '(choice (symbol :tag "No limit" nil)
  2460. (integer :tag "Max number of minutes")
  2461. (repeat
  2462. (cons (choice :tag "Agenda type"
  2463. (const agenda)
  2464. (const todo)
  2465. (const tags)
  2466. (const search))
  2467. (integer :tag "Max number of minutes")))))
  2468. (defvar org-agenda-keep-restricted-file-list nil)
  2469. (defvar org-keys nil)
  2470. (defvar org-match nil)
  2471. ;;;###autoload
  2472. (defun org-agenda (&optional arg org-keys restriction)
  2473. "Dispatch agenda commands to collect entries to the agenda buffer.
  2474. Prompts for a command to execute. Any prefix arg will be passed
  2475. on to the selected command. The default selections are:
  2476. a Call `org-agenda-list' to display the agenda for current day or week.
  2477. t Call `org-todo-list' to display the global todo list.
  2478. T Call `org-todo-list' to display the global todo list, select only
  2479. entries with a specific TODO keyword (the user gets a prompt).
  2480. m Call `org-tags-view' to display headlines with tags matching
  2481. a condition (the user is prompted for the condition).
  2482. M Like `m', but select only TODO entries, no ordinary headlines.
  2483. e Export views to associated files.
  2484. s Search entries for keywords.
  2485. S Search entries for keywords, only with TODO keywords.
  2486. / Multi occur across all agenda files and also files listed
  2487. in `org-agenda-text-search-extra-files'.
  2488. < Restrict agenda commands to buffer, subtree, or region.
  2489. Press several times to get the desired effect.
  2490. > Remove a previous restriction.
  2491. # List \"stuck\" projects.
  2492. ! Configure what \"stuck\" means.
  2493. C Configure custom agenda commands.
  2494. More commands can be added by configuring the variable
  2495. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  2496. searches can be pre-defined in this way.
  2497. If the current buffer is in Org mode and visiting a file, you can also
  2498. first press `<' once to indicate that the agenda should be temporarily
  2499. \(until the next use of `\\[org-agenda]') restricted to the current file.
  2500. Pressing `<' twice means to restrict to the current subtree or region
  2501. \(if active)."
  2502. (interactive "P")
  2503. (catch 'exit
  2504. (let* ((prefix-descriptions nil)
  2505. (org-agenda-buffer-name org-agenda-buffer-name)
  2506. (org-agenda-window-setup (if (equal (buffer-name)
  2507. org-agenda-buffer-name)
  2508. 'current-window
  2509. org-agenda-window-setup))
  2510. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  2511. (org-agenda-custom-commands
  2512. ;; normalize different versions
  2513. (delq nil
  2514. (mapcar
  2515. (lambda (x)
  2516. (cond ((stringp (cdr x))
  2517. (push x prefix-descriptions)
  2518. nil)
  2519. ((stringp (nth 1 x)) x)
  2520. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  2521. (t (cons (car x) (cons "" (cdr x))))))
  2522. org-agenda-custom-commands)))
  2523. (org-agenda-custom-commands
  2524. (org-contextualize-keys
  2525. org-agenda-custom-commands org-agenda-custom-commands-contexts))
  2526. (buf (current-buffer))
  2527. (bfn (buffer-file-name (buffer-base-buffer)))
  2528. entry key type org-match lprops ans)
  2529. ;; Turn off restriction unless there is an overriding one,
  2530. (unless org-agenda-overriding-restriction
  2531. (unless org-agenda-keep-restricted-file-list
  2532. ;; There is a request to keep the file list in place
  2533. (put 'org-agenda-files 'org-restrict nil))
  2534. (setq org-agenda-restrict nil)
  2535. (move-marker org-agenda-restrict-begin nil)
  2536. (move-marker org-agenda-restrict-end nil))
  2537. ;; Delete old local properties
  2538. (put 'org-agenda-redo-command 'org-lprops nil)
  2539. ;; Delete previously set last-arguments
  2540. (put 'org-agenda-redo-command 'last-args nil)
  2541. ;; Remember where this call originated
  2542. (setq org-agenda-last-dispatch-buffer (current-buffer))
  2543. (unless org-keys
  2544. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  2545. org-keys (car ans)
  2546. restriction (cdr ans)))
  2547. ;; If we have sticky agenda buffers, set a name for the buffer,
  2548. ;; depending on the invoking keys. The user may still set this
  2549. ;; as a command option, which will overwrite what we do here.
  2550. (when org-agenda-sticky
  2551. (setq org-agenda-buffer-name
  2552. (format "*Org Agenda(%s)*" org-keys)))
  2553. ;; Establish the restriction, if any
  2554. (when (and (not org-agenda-overriding-restriction) restriction)
  2555. (put 'org-agenda-files 'org-restrict (list bfn))
  2556. (cond
  2557. ((eq restriction 'region)
  2558. (setq org-agenda-restrict (current-buffer))
  2559. (move-marker org-agenda-restrict-begin (region-beginning))
  2560. (move-marker org-agenda-restrict-end (region-end)))
  2561. ((eq restriction 'subtree)
  2562. (save-excursion
  2563. (setq org-agenda-restrict (current-buffer))
  2564. (org-back-to-heading t)
  2565. (move-marker org-agenda-restrict-begin (point))
  2566. (move-marker org-agenda-restrict-end
  2567. (progn (org-end-of-subtree t)))))
  2568. ((and (eq restriction 'buffer)
  2569. (or (< 1 (point-min))
  2570. (< (point-max) (1+ (buffer-size)))))
  2571. (setq org-agenda-restrict (current-buffer))
  2572. (move-marker org-agenda-restrict-begin (point-min))
  2573. (move-marker org-agenda-restrict-end (point-max)))))
  2574. ;; For example the todo list should not need it (but does...)
  2575. (cond
  2576. ((setq entry (assoc org-keys org-agenda-custom-commands))
  2577. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  2578. (progn
  2579. (setq type (nth 2 entry) org-match (eval (nth 3 entry))
  2580. lprops (nth 4 entry))
  2581. (when org-agenda-sticky
  2582. (setq org-agenda-buffer-name
  2583. (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
  2584. (format "*Org Agenda(%s)*" org-keys))))
  2585. (put 'org-agenda-redo-command 'org-lprops lprops)
  2586. (cond
  2587. ((eq type 'agenda)
  2588. (org-let lprops '(org-agenda-list current-prefix-arg)))
  2589. ((eq type 'agenda*)
  2590. (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
  2591. ((eq type 'alltodo)
  2592. (org-let lprops '(org-todo-list current-prefix-arg)))
  2593. ((eq type 'search)
  2594. (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
  2595. ((eq type 'stuck)
  2596. (org-let lprops '(org-agenda-list-stuck-projects
  2597. current-prefix-arg)))
  2598. ((eq type 'tags)
  2599. (org-let lprops '(org-tags-view current-prefix-arg org-match)))
  2600. ((eq type 'tags-todo)
  2601. (org-let lprops '(org-tags-view '(4) org-match)))
  2602. ((eq type 'todo)
  2603. (org-let lprops '(org-todo-list org-match)))
  2604. ((eq type 'tags-tree)
  2605. (org-check-for-org-mode)
  2606. (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
  2607. ((eq type 'todo-tree)
  2608. (org-check-for-org-mode)
  2609. (org-let lprops
  2610. '(org-occur (concat "^" org-outline-regexp "[ \t]*"
  2611. (regexp-quote org-match) "\\>"))))
  2612. ((eq type 'occur-tree)
  2613. (org-check-for-org-mode)
  2614. (org-let lprops '(org-occur org-match)))
  2615. ((functionp type)
  2616. (org-let lprops '(funcall type org-match)))
  2617. ((fboundp type)
  2618. (org-let lprops '(funcall type org-match)))
  2619. (t (user-error "Invalid custom agenda command type %s" type))))
  2620. (org-agenda-run-series (nth 1 entry) (cddr entry))))
  2621. ((equal org-keys "C")
  2622. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  2623. (customize-variable 'org-agenda-custom-commands))
  2624. ((equal org-keys "a") (call-interactively 'org-agenda-list))
  2625. ((equal org-keys "s") (call-interactively 'org-search-view))
  2626. ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
  2627. ((equal org-keys "t") (call-interactively 'org-todo-list))
  2628. ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  2629. ((equal org-keys "m") (call-interactively 'org-tags-view))
  2630. ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  2631. ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
  2632. ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
  2633. (add-hook
  2634. 'post-command-hook
  2635. (lambda ()
  2636. (unless (current-message)
  2637. (let* ((m (org-agenda-get-any-marker))
  2638. (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
  2639. (when note
  2640. (message "FLAGGING-NOTE ([?] for more info): %s"
  2641. (org-add-props
  2642. (replace-regexp-in-string
  2643. "\\\\n" "//"
  2644. (copy-sequence note))
  2645. nil 'face 'org-warning))))))
  2646. t t))
  2647. ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  2648. ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
  2649. ((equal org-keys "!") (customize-variable 'org-stuck-projects))
  2650. (t (user-error "Invalid agenda key"))))))
  2651. (defvar org-agenda-multi)
  2652. (defun org-agenda-append-agenda ()
  2653. "Append another agenda view to the current one.
  2654. This function allows interactive building of block agendas.
  2655. Agenda views are separated by `org-agenda-block-separator'."
  2656. (interactive)
  2657. (unless (derived-mode-p 'org-agenda-mode)
  2658. (user-error "Can only append from within agenda buffer"))
  2659. (let ((org-agenda-multi t))
  2660. (org-agenda)
  2661. (widen)
  2662. (org-agenda-finalize)
  2663. (setq buffer-read-only t)
  2664. (org-agenda-fit-window-to-buffer)))
  2665. (defun org-agenda-normalize-custom-commands (cmds)
  2666. "Normalize custom commands CMDS."
  2667. (delq nil
  2668. (mapcar
  2669. (lambda (x)
  2670. (cond ((stringp (cdr x)) nil)
  2671. ((stringp (nth 1 x)) x)
  2672. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  2673. (t (cons (car x) (cons "" (cdr x))))))
  2674. cmds)))
  2675. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  2676. "The user interface for selecting an agenda command."
  2677. (catch 'exit
  2678. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  2679. (restrict-ok (and bfn (derived-mode-p 'org-mode)))
  2680. (region-p (org-region-active-p))
  2681. (custom org-agenda-custom-commands)
  2682. (selstring "")
  2683. restriction second-time
  2684. c entry key type match prefixes rmheader header-end custom1 desc
  2685. line lines left right n n1)
  2686. (save-window-excursion
  2687. (delete-other-windows)
  2688. (org-switch-to-buffer-other-window " *Agenda Commands*")
  2689. (erase-buffer)
  2690. (insert (eval-when-compile
  2691. (let ((header
  2692. "Press key for an agenda command:
  2693. -------------------------------- < Buffer, subtree/region restriction
  2694. a Agenda for current week or day > Remove restriction
  2695. t List of all TODO entries e Export agenda views
  2696. m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
  2697. s Search for keywords M Like m, but only TODO entries
  2698. / Multi-occur S Like s, but only TODO entries
  2699. ? Find :FLAGGED: entries C Configure custom agenda commands
  2700. * Toggle sticky agenda views # List stuck projects (!=configure)
  2701. ")
  2702. (start 0))
  2703. (while (string-match
  2704. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  2705. header start)
  2706. (setq start (match-end 0))
  2707. (add-text-properties (match-beginning 2) (match-end 2)
  2708. '(face bold) header))
  2709. header)))
  2710. (setq header-end (point-marker))
  2711. (while t
  2712. (setq custom1 custom)
  2713. (when (eq rmheader t)
  2714. (org-goto-line 1)
  2715. (re-search-forward ":" nil t)
  2716. (delete-region (match-end 0) (point-at-eol))
  2717. (forward-char 1)
  2718. (looking-at "-+")
  2719. (delete-region (match-end 0) (point-at-eol))
  2720. (move-marker header-end (match-end 0)))
  2721. (goto-char header-end)
  2722. (delete-region (point) (point-max))
  2723. ;; Produce all the lines that describe custom commands and prefixes
  2724. (setq lines nil)
  2725. (while (setq entry (pop custom1))
  2726. (setq key (car entry) desc (nth 1 entry)
  2727. type (nth 2 entry)
  2728. match (nth 3 entry))
  2729. (if (> (length key) 1)
  2730. (cl-pushnew (string-to-char key) prefixes :test #'equal)
  2731. (setq line
  2732. (format
  2733. "%-4s%-14s"
  2734. (org-add-props (copy-sequence key)
  2735. '(face bold))
  2736. (cond
  2737. ((string-match "\\S-" desc) desc)
  2738. ((eq type 'agenda) "Agenda for current week or day")
  2739. ((eq type 'agenda*) "Appointments for current week or day")
  2740. ((eq type 'alltodo) "List of all TODO entries")
  2741. ((eq type 'search) "Word search")
  2742. ((eq type 'stuck) "List of stuck projects")
  2743. ((eq type 'todo) "TODO keyword")
  2744. ((eq type 'tags) "Tags query")
  2745. ((eq type 'tags-todo) "Tags (TODO)")
  2746. ((eq type 'tags-tree) "Tags tree")
  2747. ((eq type 'todo-tree) "TODO kwd tree")
  2748. ((eq type 'occur-tree) "Occur tree")
  2749. ((functionp type) (if (symbolp type)
  2750. (symbol-name type)
  2751. "Lambda expression"))
  2752. (t "???"))))
  2753. (cond
  2754. ((not (org-string-nw-p match)) nil)
  2755. (org-agenda-menu-show-matcher
  2756. (setq line
  2757. (concat line ": "
  2758. (cond
  2759. ((stringp match)
  2760. (propertize match 'face 'org-warning))
  2761. ((listp type)
  2762. (format "set of %d commands" (length type)))))))
  2763. (t
  2764. (org-add-props line nil 'help-echo (concat "Matcher: " match))))
  2765. (push line lines)))
  2766. (setq lines (nreverse lines))
  2767. (when prefixes
  2768. (mapc (lambda (x)
  2769. (push
  2770. (format "%s %s"
  2771. (org-add-props (char-to-string x)
  2772. nil 'face 'bold)
  2773. (or (cdr (assoc (concat selstring
  2774. (char-to-string x))
  2775. prefix-descriptions))
  2776. "Prefix key"))
  2777. lines))
  2778. prefixes))
  2779. ;; Check if we should display in two columns
  2780. (if org-agenda-menu-two-columns
  2781. (progn
  2782. (setq n (length lines)
  2783. n1 (+ (/ n 2) (mod n 2))
  2784. right (nthcdr n1 lines)
  2785. left (copy-sequence lines))
  2786. (setcdr (nthcdr (1- n1) left) nil))
  2787. (setq left lines right nil))
  2788. (while left
  2789. (insert "\n" (pop left))
  2790. (when right
  2791. (if (< (current-column) 40)
  2792. (move-to-column 40 t)
  2793. (insert " "))
  2794. (insert (pop right))))
  2795. ;; Make the window the right size
  2796. (goto-char (point-min))
  2797. (if second-time
  2798. (when (not (pos-visible-in-window-p (point-max)))
  2799. (org-fit-window-to-buffer))
  2800. (setq second-time t)
  2801. (org-fit-window-to-buffer))
  2802. ;; Hint to navigation if window too small for all information
  2803. (setq header-line-format
  2804. (when (not (pos-visible-in-window-p (point-max)))
  2805. "Use SPC, DEL, C-n or C-p to navigate."))
  2806. ;; Ask for selection
  2807. (cl-loop
  2808. do (progn
  2809. (message "Press key for agenda command%s:"
  2810. (if (or restrict-ok org-agenda-overriding-restriction)
  2811. (if org-agenda-overriding-restriction
  2812. " (restriction lock active)"
  2813. (if restriction
  2814. (format " (restricted to %s)" restriction)
  2815. " (unrestricted)"))
  2816. ""))
  2817. (setq c (read-char-exclusive)))
  2818. until (not (memq c '(14 16 ?\s ?\d)))
  2819. do (cl-case c
  2820. (14 (if (not (pos-visible-in-window-p (point-max)))
  2821. (ignore-errors (scroll-up 1))
  2822. (message "End of buffer")
  2823. (sit-for 1)))
  2824. (16 (if (not (pos-visible-in-window-p (point-min)))
  2825. (ignore-errors (scroll-down 1))
  2826. (message "Beginning of buffer")
  2827. (sit-for 1)))
  2828. (?\s (if (not (pos-visible-in-window-p (point-max)))
  2829. (scroll-up nil)
  2830. (message "End of buffer")
  2831. (sit-for 1)))
  2832. (?\d (if (not (pos-visible-in-window-p (point-min)))
  2833. (scroll-down nil)
  2834. (message "Beginning of buffer")
  2835. (sit-for 1)))))
  2836. (message "")
  2837. (cond
  2838. ((assoc (char-to-string c) custom)
  2839. (setq selstring (concat selstring (char-to-string c)))
  2840. (throw 'exit (cons selstring restriction)))
  2841. ((memq c prefixes)
  2842. (setq selstring (concat selstring (char-to-string c))
  2843. prefixes nil
  2844. rmheader (or rmheader t)
  2845. custom (delq nil (mapcar
  2846. (lambda (x)
  2847. (if (or (= (length (car x)) 1)
  2848. (/= (string-to-char (car x)) c))
  2849. nil
  2850. (cons (substring (car x) 1) (cdr x))))
  2851. custom))))
  2852. ((eq c ?*)
  2853. (call-interactively 'org-toggle-sticky-agenda)
  2854. (sit-for 2))
  2855. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  2856. (message "Restriction is only possible in Org buffers")
  2857. (ding) (sit-for 1))
  2858. ((eq c ?1)
  2859. (org-agenda-remove-restriction-lock 'noupdate)
  2860. (setq restriction 'buffer))
  2861. ((eq c ?0)
  2862. (org-agenda-remove-restriction-lock 'noupdate)
  2863. (setq restriction (if region-p 'region 'subtree)))
  2864. ((eq c ?<)
  2865. (org-agenda-remove-restriction-lock 'noupdate)
  2866. (setq restriction
  2867. (cond
  2868. ((eq restriction 'buffer)
  2869. (if region-p 'region 'subtree))
  2870. ((memq restriction '(subtree region))
  2871. nil)
  2872. (t 'buffer))))
  2873. ((eq c ?>)
  2874. (org-agenda-remove-restriction-lock 'noupdate)
  2875. (setq restriction nil))
  2876. ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
  2877. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  2878. ((and (> (length selstring) 0) (eq c ?\d))
  2879. (delete-window)
  2880. (org-agenda-get-restriction-and-command prefix-descriptions))
  2881. ((equal c ?q) (error "Abort"))
  2882. (t (user-error "Invalid key %c" c))))))))
  2883. (defun org-agenda-fit-window-to-buffer ()
  2884. "Fit the window to the buffer size."
  2885. (and (memq org-agenda-window-setup '(reorganize-frame))
  2886. (fboundp 'fit-window-to-buffer)
  2887. (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
  2888. (= (car org-agenda-window-frame-fractions) 1.0))
  2889. (delete-other-windows)
  2890. (org-fit-window-to-buffer
  2891. nil
  2892. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  2893. (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
  2894. (defvar org-cmd nil)
  2895. (defvar org-agenda-overriding-cmd nil)
  2896. (defvar org-agenda-overriding-arguments nil)
  2897. (defvar org-agenda-overriding-cmd-arguments nil)
  2898. (defun org-agenda-run-series (name series)
  2899. "Run agenda NAME as a SERIES of agenda commands."
  2900. (org-let (nth 1 series) '(org-agenda-prepare name))
  2901. ;; We need to reset agenda markers here, because when constructing a
  2902. ;; block agenda, the individual blocks do not do that.
  2903. (org-agenda-reset-markers)
  2904. (let* ((org-agenda-multi t)
  2905. (redo (list 'org-agenda-run-series name (list 'quote series)))
  2906. (cmds (car series))
  2907. (gprops (nth 1 series))
  2908. match ;; The byte compiler incorrectly complains about this. Keep it!
  2909. org-cmd type lprops)
  2910. (while (setq org-cmd (pop cmds))
  2911. (setq type (car org-cmd))
  2912. (setq match (eval (nth 1 org-cmd)))
  2913. (setq lprops (nth 2 org-cmd))
  2914. (let ((org-agenda-overriding-arguments
  2915. (if (eq org-agenda-overriding-cmd org-cmd)
  2916. (or org-agenda-overriding-arguments
  2917. org-agenda-overriding-cmd-arguments))))
  2918. (cond
  2919. ((eq type 'agenda)
  2920. (org-let2 gprops lprops
  2921. '(call-interactively 'org-agenda-list)))
  2922. ((eq type 'agenda*)
  2923. (org-let2 gprops lprops
  2924. '(funcall 'org-agenda-list nil nil t)))
  2925. ((eq type 'alltodo)
  2926. (org-let2 gprops lprops
  2927. '(call-interactively 'org-todo-list)))
  2928. ((eq type 'search)
  2929. (org-let2 gprops lprops
  2930. '(org-search-view current-prefix-arg match nil)))
  2931. ((eq type 'stuck)
  2932. (org-let2 gprops lprops
  2933. '(call-interactively 'org-agenda-list-stuck-projects)))
  2934. ((eq type 'tags)
  2935. (org-let2 gprops lprops
  2936. '(org-tags-view current-prefix-arg match)))
  2937. ((eq type 'tags-todo)
  2938. (org-let2 gprops lprops
  2939. '(org-tags-view '(4) match)))
  2940. ((eq type 'todo)
  2941. (org-let2 gprops lprops
  2942. '(org-todo-list match)))
  2943. ((fboundp type)
  2944. (org-let2 gprops lprops
  2945. '(funcall type match)))
  2946. (t (error "Invalid type in command series")))))
  2947. (widen)
  2948. (let ((inhibit-read-only t))
  2949. (add-text-properties (point-min) (point-max)
  2950. `(org-series t org-series-redo-cmd ,redo)))
  2951. (setq org-agenda-redo-command redo)
  2952. (goto-char (point-min)))
  2953. (org-agenda-fit-window-to-buffer)
  2954. (org-let (nth 1 series) '(org-agenda-finalize)))
  2955. ;;;###autoload
  2956. (defmacro org-batch-agenda (cmd-key &rest parameters)
  2957. "Run an agenda command in batch mode and send the result to STDOUT.
  2958. If CMD-KEY is a string of length 1, it is used as a key in
  2959. `org-agenda-custom-commands' and triggers this command. If it is a
  2960. longer string it is used as a tags/todo match string.
  2961. Parameters are alternating variable names and values that will be bound
  2962. before running the agenda command."
  2963. (org-eval-in-environment (org-make-parameter-alist parameters)
  2964. (let (org-agenda-sticky)
  2965. (if (> (length cmd-key) 1)
  2966. (org-tags-view nil cmd-key)
  2967. (org-agenda nil cmd-key))))
  2968. (set-buffer org-agenda-buffer-name)
  2969. (princ (buffer-string)))
  2970. (defvar org-agenda-info nil)
  2971. ;;;###autoload
  2972. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  2973. "Run an agenda command in batch mode and send the result to STDOUT.
  2974. If CMD-KEY is a string of length 1, it is used as a key in
  2975. `org-agenda-custom-commands' and triggers this command. If it is a
  2976. longer string it is used as a tags/todo match string.
  2977. Parameters are alternating variable names and values that will be bound
  2978. before running the agenda command.
  2979. The output gives a line for each selected agenda item. Each
  2980. item is a list of comma-separated values, like this:
  2981. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  2982. category The category of the item
  2983. head The headline, without TODO kwd, TAGS and PRIORITY
  2984. type The type of the agenda entry, can be
  2985. todo selected in TODO match
  2986. tagsmatch selected in tags match
  2987. diary imported from diary
  2988. deadline a deadline on given date
  2989. scheduled scheduled on given date
  2990. timestamp entry has timestamp on given date
  2991. closed entry was closed on given date
  2992. upcoming-deadline warning about deadline
  2993. past-scheduled forwarded scheduled item
  2994. block entry has date block including g. date
  2995. todo The todo keyword, if any
  2996. tags All tags including inherited ones, separated by colons
  2997. date The relevant date, like 2007-2-14
  2998. time The time, like 15:00-16:50
  2999. extra String with extra planning info
  3000. priority-l The priority letter if any was given
  3001. priority-n The computed numerical priority
  3002. agenda-day The day in the agenda where this is listed"
  3003. (org-eval-in-environment (append '((org-agenda-remove-tags t))
  3004. (org-make-parameter-alist parameters))
  3005. (if (> (length cmd-key) 2)
  3006. (org-tags-view nil cmd-key)
  3007. (org-agenda nil cmd-key)))
  3008. (set-buffer org-agenda-buffer-name)
  3009. (let ((lines (org-split-string (buffer-string) "\n")))
  3010. (dolist (line lines)
  3011. (when (get-text-property 0 'org-category line)
  3012. (setq org-agenda-info
  3013. (org-fix-agenda-info (text-properties-at 0 line)))
  3014. (princ
  3015. (mapconcat 'org-agenda-export-csv-mapper
  3016. '(org-category txt type todo tags date time extra
  3017. priority-letter priority agenda-day)
  3018. ","))
  3019. (princ "\n")))))
  3020. (defun org-fix-agenda-info (props)
  3021. "Make sure all properties on an agenda item have a canonical form.
  3022. This ensures the export commands can easily use it."
  3023. (let (tmp re)
  3024. (when (setq tmp (plist-get props 'tags))
  3025. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  3026. (when (setq tmp (plist-get props 'date))
  3027. (when (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  3028. (let ((calendar-date-display-form '(year "-" month "-" day)))
  3029. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  3030. (setq tmp (calendar-date-string tmp)))
  3031. (setq props (plist-put props 'date tmp)))
  3032. (when (setq tmp (plist-get props 'day))
  3033. (when (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  3034. (let ((calendar-date-display-form '(year "-" month "-" day)))
  3035. (setq tmp (calendar-date-string tmp)))
  3036. (setq props (plist-put props 'day tmp))
  3037. (setq props (plist-put props 'agenda-day tmp)))
  3038. (when (setq tmp (plist-get props 'txt))
  3039. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  3040. (plist-put props 'priority-letter (match-string 1 tmp))
  3041. (setq tmp (replace-match "" t t tmp)))
  3042. (when (and (setq re (plist-get props 'org-todo-regexp))
  3043. (setq re (concat "\\`\\.*" re " ?"))
  3044. (let ((case-fold-search nil)) (string-match re tmp)))
  3045. (plist-put props 'todo (match-string 1 tmp))
  3046. (setq tmp (replace-match "" t t tmp)))
  3047. (plist-put props 'txt tmp)))
  3048. props)
  3049. (defun org-agenda-export-csv-mapper (prop)
  3050. (let ((res (plist-get org-agenda-info prop)))
  3051. (setq res
  3052. (cond
  3053. ((not res) "")
  3054. ((stringp res) res)
  3055. (t (prin1-to-string res))))
  3056. (org-trim (replace-regexp-in-string "," ";" res nil t))))
  3057. ;;;###autoload
  3058. (defun org-store-agenda-views (&rest parameters)
  3059. "Store agenda views."
  3060. (interactive)
  3061. (eval (list 'org-batch-store-agenda-views)))
  3062. ;;;###autoload
  3063. (defmacro org-batch-store-agenda-views (&rest parameters)
  3064. "Run all custom agenda commands that have a file argument."
  3065. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  3066. (pop-up-frames nil)
  3067. (dir default-directory)
  3068. (pars (org-make-parameter-alist parameters))
  3069. cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
  3070. (save-window-excursion
  3071. (while cmds
  3072. (setq cmd (pop cmds)
  3073. thiscmdkey (car cmd)
  3074. thiscmdcmd (cdr cmd)
  3075. match (nth 2 thiscmdcmd)
  3076. bufname (if org-agenda-sticky
  3077. (or (and (stringp match)
  3078. (format "*Org Agenda(%s:%s)*" thiscmdkey match))
  3079. (format "*Org Agenda(%s)*" thiscmdkey))
  3080. org-agenda-buffer-name)
  3081. cmd-or-set (nth 2 cmd)
  3082. opts (nth (if (listp cmd-or-set) 3 4) cmd)
  3083. files (nth (if (listp cmd-or-set) 4 5) cmd))
  3084. (if (stringp files) (setq files (list files)))
  3085. (when files
  3086. (org-eval-in-environment (append org-agenda-exporter-settings
  3087. opts pars)
  3088. (org-agenda nil thiscmdkey))
  3089. (set-buffer bufname)
  3090. (while files
  3091. (org-eval-in-environment (append org-agenda-exporter-settings
  3092. opts pars)
  3093. (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
  3094. (and (get-buffer bufname)
  3095. (kill-buffer bufname)))))))
  3096. (defvar org-agenda-current-span nil
  3097. "The current span used in the agenda view.") ; local variable in the agenda buffer
  3098. (defun org-agenda-mark-header-line (pos)
  3099. "Mark the line at POS as an agenda structure header."
  3100. (save-excursion
  3101. (goto-char pos)
  3102. (put-text-property (point-at-bol) (point-at-eol)
  3103. 'org-agenda-structural-header t)
  3104. (when org-agenda-title-append
  3105. (put-text-property (point-at-bol) (point-at-eol)
  3106. 'org-agenda-title-append org-agenda-title-append))))
  3107. (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
  3108. (defvar org-agenda-write-buffer-name "Agenda View")
  3109. (defun org-agenda-write (file &optional open nosettings agenda-bufname)
  3110. "Write the current buffer (an agenda view) as a file.
  3111. Depending on the extension of the file name, plain text (.txt),
  3112. HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
  3113. If the extension is .ics, translate visible agenda into iCalendar
  3114. format. If the extension is .org, collect all subtrees
  3115. corresponding to the agenda entries and add them in an .org file.
  3116. With prefix argument OPEN, open the new file immediately. If
  3117. NOSETTINGS is given, do not scope the settings of
  3118. `org-agenda-exporter-settings' into the export commands. This is
  3119. used when the settings have already been scoped and we do not
  3120. wish to overrule other, higher priority settings. If
  3121. AGENDA-BUFFER-NAME is provided, use this as the buffer name for
  3122. the agenda to write."
  3123. (interactive "FWrite agenda to file: \nP")
  3124. (if (or (not (file-writable-p file))
  3125. (and (file-exists-p file)
  3126. (if (called-interactively-p 'any)
  3127. (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
  3128. (user-error "Cannot write agenda to file %s" file))
  3129. (org-let (if nosettings nil org-agenda-exporter-settings)
  3130. '(save-excursion
  3131. (save-window-excursion
  3132. (let ((bs (copy-sequence (buffer-string)))
  3133. (extension (file-name-extension file))
  3134. (default-directory (file-name-directory file))
  3135. beg content)
  3136. (with-temp-buffer
  3137. (rename-buffer org-agenda-write-buffer-name t)
  3138. (set-buffer-modified-p nil)
  3139. (insert bs)
  3140. (org-agenda-remove-marked-text 'invisible 'org-filtered)
  3141. (run-hooks 'org-agenda-before-write-hook)
  3142. (cond
  3143. ((bound-and-true-p org-mobile-creating-agendas)
  3144. (org-mobile-write-agenda-for-mobile file))
  3145. ((string= "org" extension)
  3146. (let (content p m message-log-max)
  3147. (goto-char (point-min))
  3148. (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
  3149. (goto-char p)
  3150. (setq m (get-text-property (point) 'org-hd-marker))
  3151. (when m
  3152. (push (save-excursion
  3153. (set-buffer (marker-buffer m))
  3154. (goto-char m)
  3155. (org-copy-subtree 1 nil t t)
  3156. org-subtree-clip)
  3157. content)))
  3158. (find-file file)
  3159. (erase-buffer)
  3160. (dolist (s content) (org-paste-subtree 1 s))
  3161. (write-file file)
  3162. (kill-buffer (current-buffer))
  3163. (message "Org file written to %s" file)))
  3164. ((member extension '("html" "htm"))
  3165. (or (require 'htmlize nil t)
  3166. (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
  3167. (set-buffer (htmlize-buffer (current-buffer)))
  3168. (when org-agenda-export-html-style
  3169. ;; replace <style> section with org-agenda-export-html-style
  3170. (goto-char (point-min))
  3171. (kill-region (- (search-forward "<style") 6)
  3172. (search-forward "</style>"))
  3173. (insert org-agenda-export-html-style))
  3174. (write-file file)
  3175. (kill-buffer (current-buffer))
  3176. (message "HTML written to %s" file))
  3177. ((string= "ps" extension)
  3178. (require 'ps-print)
  3179. (ps-print-buffer-with-faces file)
  3180. (message "Postscript written to %s" file))
  3181. ((string= "pdf" extension)
  3182. (require 'ps-print)
  3183. (ps-print-buffer-with-faces
  3184. (concat (file-name-sans-extension file) ".ps"))
  3185. (call-process "ps2pdf" nil nil nil
  3186. (expand-file-name
  3187. (concat (file-name-sans-extension file) ".ps"))
  3188. (expand-file-name file))
  3189. (delete-file (concat (file-name-sans-extension file) ".ps"))
  3190. (message "PDF written to %s" file))
  3191. ((string= "ics" extension)
  3192. (require 'ox-icalendar)
  3193. (org-icalendar-export-current-agenda (expand-file-name file)))
  3194. (t
  3195. (let ((bs (buffer-string)))
  3196. (find-file file)
  3197. (erase-buffer)
  3198. (insert bs)
  3199. (save-buffer 0)
  3200. (kill-buffer (current-buffer))
  3201. (message "Plain text written to %s" file))))))))
  3202. (set-buffer (or agenda-bufname
  3203. (and (called-interactively-p 'any) (buffer-name))
  3204. org-agenda-buffer-name)))
  3205. (when open (org-open-file file)))
  3206. (defun org-agenda-remove-marked-text (property &optional value)
  3207. "Delete all text marked with VALUE of PROPERTY.
  3208. VALUE defaults to t."
  3209. (let (beg)
  3210. (setq value (or value t))
  3211. (while (setq beg (text-property-any (point-min) (point-max)
  3212. property value))
  3213. (delete-region
  3214. beg (or (next-single-property-change beg property)
  3215. (point-max))))))
  3216. (defun org-agenda-add-entry-text ()
  3217. "Add entry text to agenda lines.
  3218. This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
  3219. entry text following headings shown in the agenda.
  3220. Drawers will be excluded, also the line with scheduling/deadline info."
  3221. (when (and (> org-agenda-add-entry-text-maxlines 0)
  3222. (not (bound-and-true-p org-mobile-creating-agendas)))
  3223. (let (m txt)
  3224. (goto-char (point-min))
  3225. (while (not (eobp))
  3226. (if (not (setq m (org-get-at-bol 'org-hd-marker)))
  3227. (beginning-of-line 2)
  3228. (setq txt (org-agenda-get-some-entry-text
  3229. m org-agenda-add-entry-text-maxlines " > "))
  3230. (end-of-line 1)
  3231. (if (string-match "\\S-" txt)
  3232. (insert "\n" txt)
  3233. (or (eobp) (forward-char 1))))))))
  3234. (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
  3235. &rest keep)
  3236. "Extract entry text from MARKER, at most N-LINES lines.
  3237. This will ignore drawers etc, just get the text.
  3238. If INDENT is given, prefix every line with this string. If KEEP is
  3239. given, it is a list of symbols, defining stuff that should not be
  3240. removed from the entry content. Currently only `planning' is allowed here."
  3241. (let (txt drawer-re kwd-time-re ind)
  3242. (save-excursion
  3243. (with-current-buffer (marker-buffer marker)
  3244. (if (not (derived-mode-p 'org-mode))
  3245. (setq txt "")
  3246. (org-with-wide-buffer
  3247. (goto-char marker)
  3248. (end-of-line 1)
  3249. (setq txt (buffer-substring
  3250. (min (1+ (point)) (point-max))
  3251. (progn (outline-next-heading) (point)))
  3252. drawer-re org-drawer-regexp
  3253. kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
  3254. ".*\n?"))
  3255. (with-temp-buffer
  3256. (insert txt)
  3257. (when org-agenda-add-entry-text-descriptive-links
  3258. (goto-char (point-min))
  3259. (while (org-activate-links (point-max))
  3260. (add-text-properties (match-beginning 0) (match-end 0)
  3261. '(face org-link))))
  3262. (goto-char (point-min))
  3263. (while (re-search-forward org-link-bracket-re (point-max) t)
  3264. (set-text-properties (match-beginning 0) (match-end 0)
  3265. nil))
  3266. (goto-char (point-min))
  3267. (while (re-search-forward drawer-re nil t)
  3268. (delete-region
  3269. (match-beginning 0)
  3270. (progn (re-search-forward
  3271. "^[ \t]*:END:.*\n?" nil 'move)
  3272. (point))))
  3273. (unless (member 'planning keep)
  3274. (goto-char (point-min))
  3275. (while (re-search-forward kwd-time-re nil t)
  3276. (replace-match "")))
  3277. (goto-char (point-min))
  3278. (when org-agenda-entry-text-exclude-regexps
  3279. (let ((re-list org-agenda-entry-text-exclude-regexps) re)
  3280. (while (setq re (pop re-list))
  3281. (goto-char (point-min))
  3282. (while (re-search-forward re nil t)
  3283. (replace-match "")))))
  3284. (goto-char (point-max))
  3285. (skip-chars-backward " \t\n")
  3286. (when (looking-at "[ \t\n]+\\'") (replace-match ""))
  3287. ;; find and remove min common indentation
  3288. (goto-char (point-min))
  3289. (untabify (point-min) (point-max))
  3290. (setq ind (current-indentation))
  3291. (while (not (eobp))
  3292. (unless (looking-at "[ \t]*$")
  3293. (setq ind (min ind (current-indentation))))
  3294. (beginning-of-line 2))
  3295. (goto-char (point-min))
  3296. (while (not (eobp))
  3297. (unless (looking-at "[ \t]*$")
  3298. (move-to-column ind)
  3299. (delete-region (point-at-bol) (point)))
  3300. (beginning-of-line 2))
  3301. (run-hooks 'org-agenda-entry-text-cleanup-hook)
  3302. (goto-char (point-min))
  3303. (when indent
  3304. (while (and (not (eobp)) (re-search-forward "^" nil t))
  3305. (replace-match indent t t)))
  3306. (goto-char (point-min))
  3307. (while (looking-at "[ \t]*\n") (replace-match ""))
  3308. (goto-char (point-max))
  3309. (when (> (org-current-line)
  3310. n-lines)
  3311. (org-goto-line (1+ n-lines))
  3312. (backward-char 1))
  3313. (setq txt (buffer-substring (point-min) (point))))))))
  3314. txt))
  3315. (defun org-check-for-org-mode ()
  3316. "Make sure current buffer is in Org mode. Error if not."
  3317. (or (derived-mode-p 'org-mode)
  3318. (error "Cannot execute Org agenda command on buffer in %s"
  3319. major-mode)))
  3320. ;;; Agenda prepare and finalize
  3321. (defvar org-agenda-multi nil) ; dynamically scoped
  3322. (defvar org-agenda-pre-window-conf nil)
  3323. (defvar org-agenda-columns-active nil)
  3324. (defvar org-agenda-name nil)
  3325. (defvar org-agenda-tag-filter nil)
  3326. (defvar org-agenda-category-filter nil)
  3327. (defvar org-agenda-regexp-filter nil)
  3328. (defvar org-agenda-effort-filter nil)
  3329. (defvar org-agenda-top-headline-filter nil)
  3330. (defvar org-agenda-represented-categories nil
  3331. "Cache for the list of all categories in the agenda.")
  3332. (defvar org-agenda-represented-tags nil
  3333. "Cache for the list of all categories in the agenda.")
  3334. (defvar org-agenda-tag-filter-preset nil
  3335. "A preset of the tags filter used for secondary agenda filtering.
  3336. This must be a list of strings, each string must be a single tag preceded
  3337. by \"+\" or \"-\".
  3338. This variable should not be set directly, but agenda custom commands can
  3339. bind it in the options section. The preset filter is a global property of
  3340. the entire agenda view. In a block agenda, it will not work reliably to
  3341. define a filter for one of the individual blocks. You need to set it in
  3342. the global options and expect it to be applied to the entire view.")
  3343. (defconst org-agenda-filter-variables
  3344. '((category . org-agenda-category-filter)
  3345. (tag . org-agenda-tag-filter)
  3346. (effort . org-agenda-effort-filter)
  3347. (regexp . org-agenda-regexp-filter))
  3348. "Alist of filter types and associated variables")
  3349. (defun org-agenda-filter-any ()
  3350. "Is any filter active?"
  3351. (let ((form (cons 'or (mapcar (lambda (x)
  3352. (if (or (symbol-value (cdr x))
  3353. (get :preset-filter x))
  3354. t nil))
  3355. org-agenda-filter-variables))))
  3356. (eval form)))
  3357. (defvar org-agenda-category-filter-preset nil
  3358. "A preset of the category filter used for secondary agenda filtering.
  3359. This must be a list of strings, each string must be a single category
  3360. preceded by \"+\" or \"-\".
  3361. This variable should not be set directly, but agenda custom commands can
  3362. bind it in the options section. The preset filter is a global property of
  3363. the entire agenda view. In a block agenda, it will not work reliably to
  3364. define a filter for one of the individual blocks. You need to set it in
  3365. the global options and expect it to be applied to the entire view.")
  3366. (defvar org-agenda-regexp-filter-preset nil
  3367. "A preset of the regexp filter used for secondary agenda filtering.
  3368. This must be a list of strings, each string must be a single regexp
  3369. preceded by \"+\" or \"-\".
  3370. This variable should not be set directly, but agenda custom commands can
  3371. bind it in the options section. The preset filter is a global property of
  3372. the entire agenda view. In a block agenda, it will not work reliably to
  3373. define a filter for one of the individual blocks. You need to set it in
  3374. the global options and expect it to be applied to the entire view.")
  3375. (defvar org-agenda-effort-filter-preset nil
  3376. "A preset of the effort condition used for secondary agenda filtering.
  3377. This must be a list of strings, each string must be a single regexp
  3378. preceded by \"+\" or \"-\".
  3379. This variable should not be set directly, but agenda custom commands can
  3380. bind it in the options section. The preset filter is a global property of
  3381. the entire agenda view. In a block agenda, it will not work reliably to
  3382. define a filter for one of the individual blocks. You need to set it in
  3383. the global options and expect it to be applied to the entire view.")
  3384. (defun org-agenda-use-sticky-p ()
  3385. "Return non-nil if an agenda buffer named
  3386. `org-agenda-buffer-name' exists and should be shown instead of
  3387. generating a new one."
  3388. (and
  3389. ;; turned off by user
  3390. org-agenda-sticky
  3391. ;; For multi-agenda buffer already exists
  3392. (not org-agenda-multi)
  3393. ;; buffer found
  3394. (get-buffer org-agenda-buffer-name)
  3395. ;; C-u parameter is same as last call
  3396. (with-current-buffer (get-buffer org-agenda-buffer-name)
  3397. (and
  3398. (equal current-prefix-arg
  3399. org-agenda-last-prefix-arg)
  3400. ;; In case user turned stickiness on, while having existing
  3401. ;; Agenda buffer active, don't reuse that buffer, because it
  3402. ;; does not have org variables local
  3403. org-agenda-this-buffer-is-sticky))))
  3404. (defun org-agenda-prepare-window (abuf filter-alist)
  3405. "Setup agenda buffer in the window.
  3406. ABUF is the buffer for the agenda window.
  3407. FILTER-ALIST is an alist of filters we need to apply when
  3408. `org-agenda-persistent-filter' is non-nil."
  3409. (let* ((awin (get-buffer-window abuf)) wconf)
  3410. (cond
  3411. ((equal (current-buffer) abuf) nil)
  3412. (awin (select-window awin))
  3413. ((not (setq wconf (current-window-configuration))))
  3414. ((eq org-agenda-window-setup 'current-window)
  3415. (pop-to-buffer-same-window abuf))
  3416. ((eq org-agenda-window-setup 'other-window)
  3417. (org-switch-to-buffer-other-window abuf))
  3418. ((eq org-agenda-window-setup 'other-frame)
  3419. (switch-to-buffer-other-frame abuf))
  3420. ((eq org-agenda-window-setup 'only-window)
  3421. (delete-other-windows)
  3422. (pop-to-buffer-same-window abuf))
  3423. ((eq org-agenda-window-setup 'reorganize-frame)
  3424. (delete-other-windows)
  3425. (org-switch-to-buffer-other-window abuf)))
  3426. (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
  3427. (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
  3428. (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
  3429. (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
  3430. ;; Additional test in case agenda is invoked from within agenda
  3431. ;; buffer via elisp link.
  3432. (unless (equal (current-buffer) abuf)
  3433. (pop-to-buffer-same-window abuf))
  3434. (setq org-agenda-pre-window-conf
  3435. (or wconf org-agenda-pre-window-conf))))
  3436. (defun org-agenda-prepare (&optional name)
  3437. (let ((filter-alist (when org-agenda-persistent-filter
  3438. (with-current-buffer
  3439. (get-buffer-create org-agenda-buffer-name)
  3440. `((tag . ,org-agenda-tag-filter)
  3441. (re . ,org-agenda-regexp-filter)
  3442. (effort . ,org-agenda-effort-filter)
  3443. (cat . ,org-agenda-category-filter))))))
  3444. (if (org-agenda-use-sticky-p)
  3445. (progn
  3446. (put 'org-agenda-tag-filter :preset-filter nil)
  3447. (put 'org-agenda-category-filter :preset-filter nil)
  3448. (put 'org-agenda-regexp-filter :preset-filter nil)
  3449. (put 'org-agenda-effort-filter :preset-filter nil)
  3450. ;; Popup existing buffer
  3451. (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
  3452. filter-alist)
  3453. (message "Sticky Agenda buffer, use `r' to refresh")
  3454. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  3455. (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
  3456. (setq org-todo-keywords-for-agenda nil)
  3457. (put 'org-agenda-tag-filter :preset-filter
  3458. org-agenda-tag-filter-preset)
  3459. (put 'org-agenda-category-filter :preset-filter
  3460. org-agenda-category-filter-preset)
  3461. (put 'org-agenda-regexp-filter :preset-filter
  3462. org-agenda-regexp-filter-preset)
  3463. (put 'org-agenda-effort-filter :preset-filter
  3464. org-agenda-effort-filter-preset)
  3465. (if org-agenda-multi
  3466. (progn
  3467. (setq buffer-read-only nil)
  3468. (goto-char (point-max))
  3469. (unless (or (bobp) org-agenda-compact-blocks
  3470. (not org-agenda-block-separator))
  3471. (insert "\n"
  3472. (if (stringp org-agenda-block-separator)
  3473. org-agenda-block-separator
  3474. (make-string (window-width) org-agenda-block-separator))
  3475. "\n"))
  3476. (narrow-to-region (point) (point-max)))
  3477. (setq org-done-keywords-for-agenda nil)
  3478. ;; Setting any org variables that are in org-agenda-local-vars
  3479. ;; list need to be done after the prepare call
  3480. (org-agenda-prepare-window
  3481. (get-buffer-create org-agenda-buffer-name) filter-alist)
  3482. (setq buffer-read-only nil)
  3483. (org-agenda-reset-markers)
  3484. (let ((inhibit-read-only t)) (erase-buffer))
  3485. (org-agenda-mode)
  3486. (setq org-agenda-buffer (current-buffer))
  3487. (setq org-agenda-contributing-files nil)
  3488. (setq org-agenda-columns-active nil)
  3489. (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
  3490. (setq org-todo-keywords-for-agenda
  3491. (org-uniquify org-todo-keywords-for-agenda))
  3492. (setq org-done-keywords-for-agenda
  3493. (org-uniquify org-done-keywords-for-agenda))
  3494. (setq org-agenda-last-prefix-arg current-prefix-arg)
  3495. (setq org-agenda-this-buffer-name org-agenda-buffer-name)
  3496. (and name (not org-agenda-name)
  3497. (setq-local org-agenda-name name)))
  3498. (setq buffer-read-only nil))))
  3499. (defvar org-overriding-columns-format)
  3500. (defvar org-local-columns-format)
  3501. (defun org-agenda-finalize ()
  3502. "Finishing touch for the agenda buffer, called just before displaying it."
  3503. (unless org-agenda-multi
  3504. (save-excursion
  3505. (let ((inhibit-read-only t))
  3506. (goto-char (point-min))
  3507. (save-excursion
  3508. (while (org-activate-links (point-max))
  3509. (add-text-properties (match-beginning 0) (match-end 0)
  3510. '(face org-link))))
  3511. (unless (eq org-agenda-remove-tags t)
  3512. (org-agenda-align-tags))
  3513. (unless org-agenda-with-colors
  3514. (remove-text-properties (point-min) (point-max) '(face nil)))
  3515. (when (bound-and-true-p org-overriding-columns-format)
  3516. (setq-local org-local-columns-format
  3517. org-overriding-columns-format))
  3518. (when org-agenda-view-columns-initially
  3519. (org-agenda-columns))
  3520. (when org-agenda-fontify-priorities
  3521. (org-agenda-fontify-priorities))
  3522. (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
  3523. (org-agenda-dim-blocked-tasks))
  3524. (org-agenda-mark-clocking-task)
  3525. (when org-agenda-entry-text-mode
  3526. (org-agenda-entry-text-hide)
  3527. (org-agenda-entry-text-show))
  3528. (when (and (featurep 'org-habit)
  3529. (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
  3530. (org-habit-insert-consistency-graphs))
  3531. (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
  3532. (unless (or (eq org-agenda-show-inherited-tags 'always)
  3533. (and (listp org-agenda-show-inherited-tags)
  3534. (memq org-agenda-type org-agenda-show-inherited-tags))
  3535. (and (eq org-agenda-show-inherited-tags t)
  3536. (or (eq org-agenda-use-tag-inheritance t)
  3537. (and (listp org-agenda-use-tag-inheritance)
  3538. (not (memq org-agenda-type
  3539. org-agenda-use-tag-inheritance))))))
  3540. (let (mrk)
  3541. (save-excursion
  3542. (goto-char (point-min))
  3543. (while (equal (forward-line) 0)
  3544. (when (setq mrk (get-text-property (point) 'org-hd-marker))
  3545. (put-text-property (point-at-bol) (point-at-eol)
  3546. 'tags
  3547. (org-with-point-at mrk
  3548. (mapcar #'downcase (org-get-tags)))))))))
  3549. (run-hooks 'org-agenda-finalize-hook)
  3550. (setq org-agenda-represented-tags nil
  3551. org-agenda-represented-categories nil)
  3552. (when org-agenda-top-headline-filter
  3553. (org-agenda-filter-top-headline-apply
  3554. org-agenda-top-headline-filter))
  3555. (when org-agenda-tag-filter
  3556. (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
  3557. (when (get 'org-agenda-tag-filter :preset-filter)
  3558. (org-agenda-filter-apply
  3559. (get 'org-agenda-tag-filter :preset-filter) 'tag t))
  3560. (when org-agenda-category-filter
  3561. (org-agenda-filter-apply org-agenda-category-filter 'category))
  3562. (when (get 'org-agenda-category-filter :preset-filter)
  3563. (org-agenda-filter-apply
  3564. (get 'org-agenda-category-filter :preset-filter) 'category))
  3565. (when org-agenda-regexp-filter
  3566. (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
  3567. (when (get 'org-agenda-regexp-filter :preset-filter)
  3568. (org-agenda-filter-apply
  3569. (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
  3570. (when org-agenda-effort-filter
  3571. (org-agenda-filter-apply org-agenda-effort-filter 'effort))
  3572. (when (get 'org-agenda-effort-filter :preset-filter)
  3573. (org-agenda-filter-apply
  3574. (get 'org-agenda-effort-filter :preset-filter) 'effort))
  3575. (add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
  3576. (defun org-agenda-mark-clocking-task ()
  3577. "Mark the current clock entry in the agenda if it is present."
  3578. ;; We need to widen when `org-agenda-finalize' is called from
  3579. ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
  3580. (when (bound-and-true-p org-clock-current-task)
  3581. (save-restriction
  3582. (widen)
  3583. (org-agenda-unmark-clocking-task)
  3584. (when (marker-buffer org-clock-hd-marker)
  3585. (save-excursion
  3586. (goto-char (point-min))
  3587. (let (s ov)
  3588. (while (setq s (next-single-property-change (point) 'org-hd-marker))
  3589. (goto-char s)
  3590. (when (equal (org-get-at-bol 'org-hd-marker)
  3591. org-clock-hd-marker)
  3592. (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
  3593. (overlay-put ov 'type 'org-agenda-clocking)
  3594. (overlay-put ov 'face 'org-agenda-clocking)
  3595. (overlay-put ov 'help-echo
  3596. "The clock is running in this item")))))))))
  3597. (defun org-agenda-unmark-clocking-task ()
  3598. "Unmark the current clocking task."
  3599. (mapc (lambda (o)
  3600. (when (eq (overlay-get o 'type) 'org-agenda-clocking)
  3601. (delete-overlay o)))
  3602. (overlays-in (point-min) (point-max))))
  3603. (defun org-agenda-fontify-priorities ()
  3604. "Make highest priority lines bold, and lowest italic."
  3605. (interactive)
  3606. (mapc (lambda (o) (when (eq (overlay-get o 'org-type) 'org-priority)
  3607. (delete-overlay o)))
  3608. (overlays-in (point-min) (point-max)))
  3609. (save-excursion
  3610. (let (b e p ov h l)
  3611. (goto-char (point-min))
  3612. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  3613. (setq h (or (get-char-property (point) 'org-highest-priority)
  3614. org-highest-priority)
  3615. l (or (get-char-property (point) 'org-lowest-priority)
  3616. org-lowest-priority)
  3617. p (string-to-char (match-string 1))
  3618. b (match-beginning 0)
  3619. e (if (eq org-agenda-fontify-priorities 'cookies)
  3620. (match-end 0)
  3621. (point-at-eol))
  3622. ov (make-overlay b e))
  3623. (overlay-put
  3624. ov 'face
  3625. (let ((special-face
  3626. (cond ((org-face-from-face-or-color
  3627. 'priority 'org-priority
  3628. (cdr (assoc p org-priority-faces))))
  3629. ((and (listp org-agenda-fontify-priorities)
  3630. (org-face-from-face-or-color
  3631. 'priority 'org-priority
  3632. (cdr (assoc p org-agenda-fontify-priorities)))))
  3633. ((equal p l) 'italic)
  3634. ((equal p h) 'bold))))
  3635. (if special-face (list special-face 'org-priority) 'org-priority)))
  3636. (overlay-put ov 'org-type 'org-priority)))))
  3637. (defvar org-depend-tag-blocked)
  3638. (defun org-agenda-dim-blocked-tasks (&optional invisible)
  3639. "Dim currently blocked TODOs in the agenda display.
  3640. When INVISIBLE is non-nil, hide currently blocked TODO instead of
  3641. dimming them."
  3642. (interactive "P")
  3643. (when (called-interactively-p 'interactive)
  3644. (message "Dim or hide blocked tasks..."))
  3645. (dolist (o (overlays-in (point-min) (point-max)))
  3646. (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
  3647. (delete-overlay o)))
  3648. (save-excursion
  3649. (let ((inhibit-read-only t))
  3650. (goto-char (point-min))
  3651. (while (let ((pos (text-property-not-all
  3652. (point) (point-max) 'org-todo-blocked nil)))
  3653. (when pos (goto-char pos)))
  3654. (let* ((invisible (eq (org-get-at-bol 'org-todo-blocked) 'invisible))
  3655. (ov (make-overlay (if invisible
  3656. (line-end-position 0)
  3657. (line-beginning-position))
  3658. (line-end-position))))
  3659. (if invisible
  3660. (overlay-put ov 'invisible t)
  3661. (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
  3662. (overlay-put ov 'org-type 'org-blocked-todo))
  3663. (forward-line))))
  3664. (when (called-interactively-p 'interactive)
  3665. (message "Dim or hide blocked tasks...done")))
  3666. (defun org-agenda--mark-blocked-entry (entry)
  3667. "For ENTRY a string with the text property `org-hd-marker', if
  3668. the header at `org-hd-marker' is blocked according to
  3669. `org-entry-blocked-p', then if `org-agenda-dim-blocked-tasks' is
  3670. 'invisible and the header is not blocked by checkboxes, set the
  3671. text property `org-todo-blocked' to `invisible', otherwise set it
  3672. to t."
  3673. (when (get-text-property 0 'todo-state entry)
  3674. (let ((entry-marker (get-text-property 0 'org-hd-marker entry))
  3675. (org-blocked-by-checkboxes nil)
  3676. ;; Necessary so that `org-entry-blocked-p' does not change
  3677. ;; the buffer.
  3678. (org-depend-tag-blocked nil))
  3679. (when entry-marker
  3680. (let ((blocked
  3681. (with-current-buffer (marker-buffer entry-marker)
  3682. (save-excursion
  3683. (goto-char entry-marker)
  3684. (org-entry-blocked-p)))))
  3685. (when blocked
  3686. (let ((really-invisible
  3687. (and (not org-blocked-by-checkboxes)
  3688. (eq org-agenda-dim-blocked-tasks 'invisible))))
  3689. (put-text-property
  3690. 0 (length entry) 'org-todo-blocked
  3691. (if really-invisible 'invisible t)
  3692. entry)))))))
  3693. entry)
  3694. (defvar org-agenda-skip-function nil
  3695. "Function to be called at each match during agenda construction.
  3696. If this function returns nil, the current match should not be skipped.
  3697. Otherwise, the function must return a position from where the search
  3698. should be continued.
  3699. This may also be a Lisp form, it will be evaluated.
  3700. Never set this variable using `setq' or so, because then it will apply
  3701. to all future agenda commands. If you do want a global skipping condition,
  3702. use the option `org-agenda-skip-function-global' instead.
  3703. The correct usage for `org-agenda-skip-function' is to bind it with
  3704. `let' to scope it dynamically into the agenda-constructing command.
  3705. A good way to set it is through options in `org-agenda-custom-commands'.")
  3706. (defun org-agenda-skip ()
  3707. "Throw to `:skip' in places that should be skipped.
  3708. Also moves point to the end of the skipped region, so that search can
  3709. continue from there."
  3710. (let ((p (point-at-bol)) to)
  3711. (when (or
  3712. (save-excursion (goto-char p) (looking-at comment-start-skip))
  3713. (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
  3714. (get-text-property p :org-archived)
  3715. (org-end-of-subtree t))
  3716. (and org-agenda-skip-comment-trees
  3717. (get-text-property p :org-comment)
  3718. (org-end-of-subtree t))
  3719. (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
  3720. (org-agenda-skip-eval org-agenda-skip-function)))
  3721. (goto-char to))
  3722. (org-in-src-block-p t))
  3723. (throw :skip t))))
  3724. (defun org-agenda-skip-eval (form)
  3725. "If FORM is a function or a list, call (or eval) it and return the result.
  3726. `save-excursion' and `save-match-data' are wrapped around the call, so point
  3727. and match data are returned to the previous state no matter what these
  3728. functions do."
  3729. (let (fp)
  3730. (and form
  3731. (or (setq fp (functionp form))
  3732. (consp form))
  3733. (save-excursion
  3734. (save-match-data
  3735. (if fp
  3736. (funcall form)
  3737. (eval form)))))))
  3738. (defvar org-agenda-markers nil
  3739. "List of all currently active markers created by `org-agenda'.")
  3740. (defvar org-agenda-last-marker-time (float-time)
  3741. "Creation time of the last agenda marker.")
  3742. (defun org-agenda-new-marker (&optional pos)
  3743. "Return a new agenda marker.
  3744. Maker is at point, or at POS if non-nil. Org mode keeps a list of
  3745. these markers and resets them when they are no longer in use."
  3746. (let ((m (copy-marker (or pos (point)) t)))
  3747. (setq org-agenda-last-marker-time (float-time))
  3748. (if org-agenda-buffer
  3749. (with-current-buffer org-agenda-buffer
  3750. (push m org-agenda-markers))
  3751. (push m org-agenda-markers))
  3752. m))
  3753. (defun org-agenda-reset-markers ()
  3754. "Reset markers created by `org-agenda'."
  3755. (while org-agenda-markers
  3756. (move-marker (pop org-agenda-markers) nil)))
  3757. (defun org-agenda-save-markers-for-cut-and-paste (beg end)
  3758. "Save relative positions of markers in region.
  3759. This check for agenda markers in all agenda buffers currently active."
  3760. (dolist (buf (buffer-list))
  3761. (with-current-buffer buf
  3762. (when (eq major-mode 'org-agenda-mode)
  3763. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  3764. org-agenda-markers)))))
  3765. ;;; Entry text mode
  3766. (defun org-agenda-entry-text-show-here ()
  3767. "Add some text from the entry as context to the current line."
  3768. (let (m txt o)
  3769. (setq m (org-get-at-bol 'org-hd-marker))
  3770. (unless (marker-buffer m)
  3771. (error "No marker points to an entry here"))
  3772. (setq txt (concat "\n" (org-no-properties
  3773. (org-agenda-get-some-entry-text
  3774. m org-agenda-entry-text-maxlines
  3775. org-agenda-entry-text-leaders))))
  3776. (when (string-match "\\S-" txt)
  3777. (setq o (make-overlay (point-at-bol) (point-at-eol)))
  3778. (overlay-put o 'evaporate t)
  3779. (overlay-put o 'org-overlay-type 'agenda-entry-content)
  3780. (overlay-put o 'after-string txt))))
  3781. (defun org-agenda-entry-text-show ()
  3782. "Add entry context for all agenda lines."
  3783. (interactive)
  3784. (save-excursion
  3785. (goto-char (point-max))
  3786. (beginning-of-line 1)
  3787. (while (not (bobp))
  3788. (when (org-get-at-bol 'org-hd-marker)
  3789. (org-agenda-entry-text-show-here))
  3790. (beginning-of-line 0))))
  3791. (defun org-agenda-entry-text-hide ()
  3792. "Remove any shown entry context."
  3793. (mapc (lambda (o)
  3794. (when (eq (overlay-get o 'org-overlay-type)
  3795. 'agenda-entry-content)
  3796. (delete-overlay o)))
  3797. (overlays-in (point-min) (point-max))))
  3798. (defun org-agenda-get-day-face (date)
  3799. "Return the face DATE should be displayed with."
  3800. (cond ((and (functionp org-agenda-day-face-function)
  3801. (funcall org-agenda-day-face-function date)))
  3802. ((org-agenda-today-p date) 'org-agenda-date-today)
  3803. ((memq (calendar-day-of-week date) org-agenda-weekend-days)
  3804. 'org-agenda-date-weekend)
  3805. (t 'org-agenda-date)))
  3806. (defvar org-agenda-show-log-scoped)
  3807. ;;; Agenda Daily/Weekly
  3808. (defvar org-agenda-start-day nil ; dynamically scoped parameter
  3809. "Start day for the agenda view.
  3810. Custom commands can set this variable in the options section.
  3811. This is usually a string like \"2007-11-01\", \"+2d\" or any other
  3812. input allowed when reading a date through the Org calendar.
  3813. See the docstring of `org-read-date' for details.")
  3814. (defvar org-starting-day nil) ; local variable in the agenda buffer
  3815. (defvar org-arg-loc nil) ; local variable
  3816. (defvar org-agenda-buffer-tmp-name nil)
  3817. ;;;###autoload
  3818. (defun org-agenda-list (&optional arg start-day span with-hour)
  3819. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  3820. The view will be for the current day or week, but from the overview buffer
  3821. you will be able to go to other days/weeks.
  3822. With a numeric prefix argument in an interactive call, the agenda will
  3823. span ARG days. Lisp programs should instead specify SPAN to change
  3824. the number of days. SPAN defaults to `org-agenda-span'.
  3825. START-DAY defaults to TODAY, or to the most recent match for the weekday
  3826. given in `org-agenda-start-on-weekday'.
  3827. When WITH-HOUR is non-nil, only include scheduled and deadline
  3828. items if they have an hour specification like [h]h:mm."
  3829. (interactive "P")
  3830. (when org-agenda-overriding-arguments
  3831. (setq arg (car org-agenda-overriding-arguments)
  3832. start-day (nth 1 org-agenda-overriding-arguments)
  3833. span (nth 2 org-agenda-overriding-arguments)))
  3834. (when (and (integerp arg) (> arg 0))
  3835. (setq span arg arg nil))
  3836. (when (numberp span)
  3837. (unless (< 0 span)
  3838. (user-error "Agenda creation impossible for this span(=%d days)." span)))
  3839. (catch 'exit
  3840. (setq org-agenda-buffer-name
  3841. (or org-agenda-buffer-tmp-name
  3842. (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
  3843. (when org-agenda-sticky
  3844. (cond ((and org-keys (stringp org-match))
  3845. (format "*Org Agenda(%s:%s)*" org-keys org-match))
  3846. (org-keys
  3847. (format "*Org Agenda(%s)*" org-keys))
  3848. (t "*Org Agenda(a)*")))
  3849. "*Org Agenda*"))
  3850. (org-agenda-prepare "Day/Week")
  3851. (setq start-day (or start-day org-agenda-start-day))
  3852. (when (stringp start-day)
  3853. ;; Convert to an absolute day number
  3854. (setq start-day (time-to-days (org-read-date nil t start-day))))
  3855. (org-compile-prefix-format 'agenda)
  3856. (org-set-sorting-strategy 'agenda)
  3857. (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
  3858. (today (org-today))
  3859. (sd (or start-day today))
  3860. (ndays (org-agenda-span-to-ndays span sd))
  3861. (org-agenda-start-on-weekday
  3862. (and (or (eq ndays 7) (eq ndays 14))
  3863. org-agenda-start-on-weekday))
  3864. (thefiles (org-agenda-files nil 'ifmode))
  3865. (files thefiles)
  3866. (start (if (or (null org-agenda-start-on-weekday)
  3867. (< ndays 7))
  3868. sd
  3869. (let* ((nt (calendar-day-of-week
  3870. (calendar-gregorian-from-absolute sd)))
  3871. (n1 org-agenda-start-on-weekday)
  3872. (d (- nt n1)))
  3873. (- sd (+ (if (< d 0) 7 0) d)))))
  3874. (day-numbers (list start))
  3875. (day-cnt 0)
  3876. (inhibit-redisplay (not debug-on-error))
  3877. (org-agenda-show-log-scoped org-agenda-show-log)
  3878. s e rtn rtnall file date d start-pos end-pos todayp
  3879. clocktable-start clocktable-end filter)
  3880. (setq org-agenda-redo-command
  3881. (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
  3882. (dotimes (n (1- ndays))
  3883. (push (1+ (car day-numbers)) day-numbers))
  3884. (setq day-numbers (nreverse day-numbers))
  3885. (setq clocktable-start (car day-numbers)
  3886. clocktable-end (1+ (or (org-last day-numbers) 0)))
  3887. (setq-local org-starting-day (car day-numbers))
  3888. (setq-local org-arg-loc arg)
  3889. (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
  3890. (unless org-agenda-compact-blocks
  3891. (let* ((d1 (car day-numbers))
  3892. (d2 (org-last day-numbers))
  3893. (w1 (org-days-to-iso-week d1))
  3894. (w2 (org-days-to-iso-week d2)))
  3895. (setq s (point))
  3896. (org-agenda--insert-overriding-header
  3897. (concat (org-agenda-span-name span)
  3898. "-agenda"
  3899. (cond ((<= 350 (- d2 d1)) "")
  3900. ((= w1 w2) (format " (W%02d)" w1))
  3901. (t (format " (W%02d-W%02d)" w1 w2)))
  3902. ":\n")))
  3903. ;; Add properties if we actually inserted a header.
  3904. (when (> (point) s)
  3905. (add-text-properties s (1- (point))
  3906. (list 'face 'org-agenda-structure
  3907. 'org-date-line t))
  3908. (org-agenda-mark-header-line s)))
  3909. (while (setq d (pop day-numbers))
  3910. (setq date (calendar-gregorian-from-absolute d)
  3911. s (point))
  3912. (if (or (setq todayp (= d today))
  3913. (and (not start-pos) (= d sd)))
  3914. (setq start-pos (point))
  3915. (when (and start-pos (not end-pos))
  3916. (setq end-pos (point))))
  3917. (setq files thefiles
  3918. rtnall nil)
  3919. (while (setq file (pop files))
  3920. (catch 'nextfile
  3921. (org-check-agenda-file file)
  3922. (let ((org-agenda-entry-types org-agenda-entry-types))
  3923. ;; Starred types override non-starred equivalents
  3924. (when (member :deadline* org-agenda-entry-types)
  3925. (setq org-agenda-entry-types
  3926. (delq :deadline org-agenda-entry-types)))
  3927. (when (member :scheduled* org-agenda-entry-types)
  3928. (setq org-agenda-entry-types
  3929. (delq :scheduled org-agenda-entry-types)))
  3930. ;; Honor with-hour
  3931. (when with-hour
  3932. (when (member :deadline org-agenda-entry-types)
  3933. (setq org-agenda-entry-types
  3934. (delq :deadline org-agenda-entry-types))
  3935. (push :deadline* org-agenda-entry-types))
  3936. (when (member :scheduled org-agenda-entry-types)
  3937. (setq org-agenda-entry-types
  3938. (delq :scheduled org-agenda-entry-types))
  3939. (push :scheduled* org-agenda-entry-types)))
  3940. (unless org-agenda-include-deadlines
  3941. (setq org-agenda-entry-types
  3942. (delq :deadline* (delq :deadline org-agenda-entry-types))))
  3943. (cond
  3944. ((memq org-agenda-show-log-scoped '(only clockcheck))
  3945. (setq rtn (org-agenda-get-day-entries
  3946. file date :closed)))
  3947. (org-agenda-show-log-scoped
  3948. (setq rtn (apply 'org-agenda-get-day-entries
  3949. file date
  3950. (append '(:closed) org-agenda-entry-types))))
  3951. (t
  3952. (setq rtn (apply 'org-agenda-get-day-entries
  3953. file date
  3954. org-agenda-entry-types)))))
  3955. (setq rtnall (append rtnall rtn)))) ;; all entries
  3956. (when org-agenda-include-diary
  3957. (let ((org-agenda-search-headline-for-time t))
  3958. (require 'diary-lib)
  3959. (setq rtn (org-get-entries-from-diary date))
  3960. (setq rtnall (append rtnall rtn))))
  3961. (when (or rtnall org-agenda-show-all-dates)
  3962. (setq day-cnt (1+ day-cnt))
  3963. (insert
  3964. (if (stringp org-agenda-format-date)
  3965. (format-time-string org-agenda-format-date
  3966. (org-time-from-absolute date))
  3967. (funcall org-agenda-format-date date))
  3968. "\n")
  3969. (put-text-property s (1- (point)) 'face
  3970. (org-agenda-get-day-face date))
  3971. (put-text-property s (1- (point)) 'org-date-line t)
  3972. (put-text-property s (1- (point)) 'org-agenda-date-header t)
  3973. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  3974. (when todayp
  3975. (put-text-property s (1- (point)) 'org-today t))
  3976. (setq rtnall
  3977. (org-agenda-add-time-grid-maybe rtnall ndays todayp))
  3978. (when rtnall (insert ;; all entries
  3979. (org-agenda-finalize-entries rtnall 'agenda)
  3980. "\n"))
  3981. (put-text-property s (1- (point)) 'day d)
  3982. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)))
  3983. (when (and org-agenda-clockreport-mode clocktable-start)
  3984. (let ((org-agenda-files (org-agenda-files nil 'ifmode))
  3985. ;; the above line is to ensure the restricted range!
  3986. (p (copy-sequence org-agenda-clockreport-parameter-plist))
  3987. tbl)
  3988. (setq p (org-plist-delete p :block))
  3989. (setq p (plist-put p :tstart clocktable-start))
  3990. (setq p (plist-put p :tend clocktable-end))
  3991. (setq p (plist-put p :scope 'agenda))
  3992. (setq tbl (apply 'org-clock-get-clocktable p))
  3993. (insert tbl)))
  3994. (goto-char (point-min))
  3995. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  3996. (unless (or (not (get-buffer-window))
  3997. (and (pos-visible-in-window-p (point-min))
  3998. (pos-visible-in-window-p (point-max))))
  3999. (goto-char (1- (point-max)))
  4000. (recenter -1)
  4001. (when (not (pos-visible-in-window-p (or start-pos 1)))
  4002. (goto-char (or start-pos 1))
  4003. (recenter 1)))
  4004. (goto-char (or start-pos 1))
  4005. (add-text-properties (point-min) (point-max)
  4006. `(org-agenda-type agenda
  4007. org-last-args (,arg ,start-day ,span)
  4008. org-redo-cmd ,org-agenda-redo-command
  4009. org-series-cmd ,org-cmd))
  4010. (when (eq org-agenda-show-log-scoped 'clockcheck)
  4011. (org-agenda-show-clocking-issues))
  4012. (org-agenda-finalize)
  4013. (setq buffer-read-only t)
  4014. (message ""))))
  4015. (defun org-agenda-ndays-to-span (n)
  4016. "Return a span symbol for a span of N days, or N if none matches."
  4017. (cond ((symbolp n) n)
  4018. ((= n 1) 'day)
  4019. ((= n 7) 'week)
  4020. ((= n 14) 'fortnight)
  4021. (t n)))
  4022. (defun org-agenda-span-to-ndays (span &optional start-day)
  4023. "Return ndays from SPAN, possibly starting at START-DAY.
  4024. START-DAY is an absolute time value."
  4025. (cond ((numberp span) span)
  4026. ((eq span 'day) 1)
  4027. ((eq span 'week) 7)
  4028. ((eq span 'fortnight) 14)
  4029. ((eq span 'month)
  4030. (let ((date (calendar-gregorian-from-absolute start-day)))
  4031. (calendar-last-day-of-month (car date) (cl-caddr date))))
  4032. ((eq span 'year)
  4033. (let ((date (calendar-gregorian-from-absolute start-day)))
  4034. (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
  4035. (defun org-agenda-span-name (span)
  4036. "Return a SPAN name."
  4037. (if (null span)
  4038. ""
  4039. (if (symbolp span)
  4040. (capitalize (symbol-name span))
  4041. (format "%d days" span))))
  4042. ;;; Agenda word search
  4043. (defvar org-agenda-search-history nil)
  4044. (defvar org-search-syntax-table nil
  4045. "Special syntax table for Org search.
  4046. In this table, we have single quotes not as word constituents, to
  4047. that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
  4048. (defvar org-mode-syntax-table) ; From org.el
  4049. (defun org-search-syntax-table ()
  4050. (unless org-search-syntax-table
  4051. (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
  4052. (modify-syntax-entry ?' "." org-search-syntax-table)
  4053. (modify-syntax-entry ?` "." org-search-syntax-table))
  4054. org-search-syntax-table)
  4055. (defvar org-agenda-last-search-view-search-was-boolean nil)
  4056. ;;;###autoload
  4057. (defun org-search-view (&optional todo-only string edit-at)
  4058. "Show all entries that contain a phrase or words or regular expressions.
  4059. With optional prefix argument TODO-ONLY, only consider entries that are
  4060. TODO entries. The argument STRING can be used to pass a default search
  4061. string into this function. If EDIT-AT is non-nil, it means that the
  4062. user should get a chance to edit this string, with cursor at position
  4063. EDIT-AT.
  4064. The search string can be viewed either as a phrase that should be found as
  4065. is, or it can be broken into a number of snippets, each of which must match
  4066. in a Boolean way to select an entry. The default depends on the variable
  4067. `org-agenda-search-view-always-boolean'.
  4068. Even if this is turned off (the default) you can always switch to
  4069. Boolean search dynamically by preceding the first word with \"+\" or \"-\".
  4070. The default is a direct search of the whole phrase, where each space in
  4071. the search string can expand to an arbitrary amount of whitespace,
  4072. including newlines.
  4073. If using a Boolean search, the search string is split on whitespace and
  4074. each snippet is searched separately, with logical AND to select an entry.
  4075. Words prefixed with a minus must *not* occur in the entry. Words without
  4076. a prefix or prefixed with a plus must occur in the entry. Matching is
  4077. case-insensitive. Words are enclosed by word delimiters (i.e. they must
  4078. match whole words, not parts of a word) if
  4079. `org-agenda-search-view-force-full-words' is set (default is nil).
  4080. Boolean search snippets enclosed by curly braces are interpreted as
  4081. regular expressions that must or (when preceded with \"-\") must not
  4082. match in the entry. Snippets enclosed into double quotes will be taken
  4083. as a whole, to include whitespace.
  4084. - If the search string starts with an asterisk, search only in headlines.
  4085. - If (possibly after the leading star) the search string starts with an
  4086. exclamation mark, this also means to look at TODO entries only, an effect
  4087. that can also be achieved with a prefix argument.
  4088. - If (possibly after star and exclamation mark) the search string starts
  4089. with a colon, this will mean that the (non-regexp) snippets of the
  4090. Boolean search must match as full words.
  4091. This command searches the agenda files, and in addition the files
  4092. listed in `org-agenda-text-search-extra-files' unless a restriction lock
  4093. is active."
  4094. (interactive "P")
  4095. (when org-agenda-overriding-arguments
  4096. (setq todo-only (car org-agenda-overriding-arguments)
  4097. string (nth 1 org-agenda-overriding-arguments)
  4098. edit-at (nth 2 org-agenda-overriding-arguments)))
  4099. (let* ((props (list 'face nil
  4100. 'done-face 'org-agenda-done
  4101. 'org-not-done-regexp org-not-done-regexp
  4102. 'org-todo-regexp org-todo-regexp
  4103. 'org-complex-heading-regexp org-complex-heading-regexp
  4104. 'mouse-face 'highlight
  4105. 'help-echo (format "mouse-2 or RET jump to location")))
  4106. (full-words org-agenda-search-view-force-full-words)
  4107. (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
  4108. regexp rtn rtnall files file pos inherited-tags
  4109. marker category level tags c neg re boolean
  4110. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  4111. (unless (and (not edit-at)
  4112. (stringp string)
  4113. (string-match "\\S-" string))
  4114. (setq string (read-string
  4115. (if org-agenda-search-view-always-boolean
  4116. "[+-]Word/{Regexp} ...: "
  4117. "Phrase or [+-]Word/{Regexp} ...: ")
  4118. (cond
  4119. ((integerp edit-at) (cons string edit-at))
  4120. (edit-at string))
  4121. 'org-agenda-search-history)))
  4122. (catch 'exit
  4123. (when org-agenda-sticky
  4124. (setq org-agenda-buffer-name
  4125. (if (stringp string)
  4126. (format "*Org Agenda(%s:%s)*"
  4127. (or org-keys (or (and todo-only "S") "s")) string)
  4128. (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
  4129. (org-agenda-prepare "SEARCH")
  4130. (org-compile-prefix-format 'search)
  4131. (org-set-sorting-strategy 'search)
  4132. (setq org-agenda-redo-command
  4133. (list 'org-search-view (if todo-only t nil)
  4134. (list 'if 'current-prefix-arg nil string)))
  4135. (setq org-agenda-query-string string)
  4136. (if (equal (string-to-char string) ?*)
  4137. (setq hdl-only t
  4138. words (substring string 1))
  4139. (setq words string))
  4140. (when (equal (string-to-char words) ?!)
  4141. (setq todo-only t
  4142. words (substring words 1)))
  4143. (when (equal (string-to-char words) ?:)
  4144. (setq full-words t
  4145. words (substring words 1)))
  4146. (when (or org-agenda-search-view-always-boolean
  4147. (member (string-to-char words) '(?- ?+ ?\{)))
  4148. (setq boolean t))
  4149. (setq words (split-string words))
  4150. (let (www w)
  4151. (while (setq w (pop words))
  4152. (while (and (string-match "\\\\\\'" w) words)
  4153. (setq w (concat (substring w 0 -1) " " (pop words))))
  4154. (push w www))
  4155. (setq words (nreverse www) www nil)
  4156. (while (setq w (pop words))
  4157. (when (and (string-match "\\`[-+]?{" w)
  4158. (not (string-match "}\\'" w)))
  4159. (while (and words (not (string-match "}\\'" (car words))))
  4160. (setq w (concat w " " (pop words))))
  4161. (setq w (concat w " " (pop words))))
  4162. (push w www))
  4163. (setq words (nreverse www)))
  4164. (setq org-agenda-last-search-view-search-was-boolean boolean)
  4165. (when boolean
  4166. (let (wds w)
  4167. (while (setq w (pop words))
  4168. (when (or (equal (substring w 0 1) "\"")
  4169. (and (> (length w) 1)
  4170. (member (substring w 0 1) '("+" "-"))
  4171. (equal (substring w 1 2) "\"")))
  4172. (while (and words (not (equal (substring w -1) "\"")))
  4173. (setq w (concat w " " (pop words)))))
  4174. (and (string-match "\\`\\([-+]?\\)\"" w)
  4175. (setq w (replace-match "\\1" nil nil w)))
  4176. (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
  4177. (push w wds))
  4178. (setq words (nreverse wds))))
  4179. (if boolean
  4180. (mapc (lambda (w)
  4181. (setq c (string-to-char w))
  4182. (if (equal c ?-)
  4183. (setq neg t w (substring w 1))
  4184. (if (equal c ?+)
  4185. (setq neg nil w (substring w 1))
  4186. (setq neg nil)))
  4187. (if (string-match "\\`{.*}\\'" w)
  4188. (setq re (substring w 1 -1))
  4189. (if full-words
  4190. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
  4191. (setq re (regexp-quote (downcase w)))))
  4192. (if neg (push re regexps-) (push re regexps+)))
  4193. words)
  4194. (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
  4195. regexps+))
  4196. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  4197. (if (not regexps+)
  4198. (setq regexp org-outline-regexp-bol)
  4199. (setq regexp (pop regexps+))
  4200. (when hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
  4201. regexp))))
  4202. (setq files (org-agenda-files nil 'ifmode))
  4203. ;; Add `org-agenda-text-search-extra-files' unless there is some
  4204. ;; restriction.
  4205. (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
  4206. (pop org-agenda-text-search-extra-files)
  4207. (unless (get 'org-agenda-files 'org-restrict)
  4208. (setq files (org-add-archive-files files))))
  4209. ;; Uniquify files. However, let `org-check-agenda-file' handle
  4210. ;; non-existent ones.
  4211. (setq files (cl-remove-duplicates
  4212. (append files org-agenda-text-search-extra-files)
  4213. :test (lambda (a b)
  4214. (and (file-exists-p a)
  4215. (file-exists-p b)
  4216. (file-equal-p a b))))
  4217. rtnall nil)
  4218. (while (setq file (pop files))
  4219. (setq ee nil)
  4220. (catch 'nextfile
  4221. (org-check-agenda-file file)
  4222. (setq buffer (if (file-exists-p file)
  4223. (org-get-agenda-file-buffer file)
  4224. (error "No such file %s" file)))
  4225. (unless buffer
  4226. ;; If file does not exist, make sure an error message is sent
  4227. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  4228. file))))
  4229. (with-current-buffer buffer
  4230. (with-syntax-table (org-search-syntax-table)
  4231. (unless (derived-mode-p 'org-mode)
  4232. (error "Agenda file %s is not in Org mode" file))
  4233. (let ((case-fold-search t))
  4234. (save-excursion
  4235. (save-restriction
  4236. (if (eq buffer org-agenda-restrict)
  4237. (narrow-to-region org-agenda-restrict-begin
  4238. org-agenda-restrict-end)
  4239. (widen))
  4240. (goto-char (point-min))
  4241. (unless (or (org-at-heading-p)
  4242. (outline-next-heading))
  4243. (throw 'nextfile t))
  4244. (goto-char (max (point-min) (1- (point))))
  4245. (while (re-search-forward regexp nil t)
  4246. (org-back-to-heading t)
  4247. (while (and (not (zerop org-agenda-search-view-max-outline-level))
  4248. (> (org-reduced-level (org-outline-level))
  4249. org-agenda-search-view-max-outline-level)
  4250. (forward-line -1)
  4251. (org-back-to-heading t)))
  4252. (skip-chars-forward "* ")
  4253. (setq beg (point-at-bol)
  4254. beg1 (point)
  4255. end (progn
  4256. (outline-next-heading)
  4257. (while (and (not (zerop org-agenda-search-view-max-outline-level))
  4258. (> (org-reduced-level (org-outline-level))
  4259. org-agenda-search-view-max-outline-level)
  4260. (forward-line 1)
  4261. (outline-next-heading)))
  4262. (point)))
  4263. (catch :skip
  4264. (goto-char beg)
  4265. (org-agenda-skip)
  4266. (setq str (buffer-substring-no-properties
  4267. (point-at-bol)
  4268. (if hdl-only (point-at-eol) end)))
  4269. (mapc (lambda (wr) (when (string-match wr str)
  4270. (goto-char (1- end))
  4271. (throw :skip t)))
  4272. regexps-)
  4273. (mapc (lambda (wr) (unless (string-match wr str)
  4274. (goto-char (1- end))
  4275. (throw :skip t)))
  4276. (if todo-only
  4277. (cons (concat "^\\*+[ \t]+"
  4278. org-not-done-regexp)
  4279. regexps+)
  4280. regexps+))
  4281. (goto-char beg)
  4282. (setq marker (org-agenda-new-marker (point))
  4283. category (org-get-category)
  4284. level (make-string (org-reduced-level (org-outline-level)) ? )
  4285. inherited-tags
  4286. (or (eq org-agenda-show-inherited-tags 'always)
  4287. (and (listp org-agenda-show-inherited-tags)
  4288. (memq 'todo org-agenda-show-inherited-tags))
  4289. (and (eq org-agenda-show-inherited-tags t)
  4290. (or (eq org-agenda-use-tag-inheritance t)
  4291. (memq 'todo org-agenda-use-tag-inheritance))))
  4292. tags (org-get-tags nil (not inherited-tags))
  4293. txt (org-agenda-format-item
  4294. ""
  4295. (buffer-substring-no-properties
  4296. beg1 (point-at-eol))
  4297. level category tags t))
  4298. (org-add-props txt props
  4299. 'org-marker marker 'org-hd-marker marker
  4300. 'org-todo-regexp org-todo-regexp
  4301. 'level level
  4302. 'org-complex-heading-regexp org-complex-heading-regexp
  4303. 'priority 1000
  4304. 'type "search")
  4305. (push txt ee)
  4306. (goto-char (1- end))))))))))
  4307. (setq rtn (nreverse ee))
  4308. (setq rtnall (append rtnall rtn)))
  4309. (org-agenda--insert-overriding-header
  4310. (with-temp-buffer
  4311. (insert "Search words: ")
  4312. (add-text-properties (point-min) (1- (point))
  4313. (list 'face 'org-agenda-structure))
  4314. (setq pos (point))
  4315. (insert string "\n")
  4316. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  4317. (setq pos (point))
  4318. (unless org-agenda-multi
  4319. (insert (substitute-command-keys "\\<org-agenda-mode-map>\
  4320. Press `\\[org-agenda-manipulate-query-add]', \
  4321. `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
  4322. `\\[org-agenda-manipulate-query-add-re]', \
  4323. `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
  4324. `\\[universal-argument] \\[org-agenda-redo]' for a fresh search\n"))
  4325. (add-text-properties pos (1- (point))
  4326. (list 'face 'org-agenda-structure)))
  4327. (buffer-string)))
  4328. (org-agenda-mark-header-line (point-min))
  4329. (when rtnall
  4330. (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
  4331. (goto-char (point-min))
  4332. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  4333. (add-text-properties (point-min) (point-max)
  4334. `(org-agenda-type search
  4335. org-last-args (,todo-only ,string ,edit-at)
  4336. org-redo-cmd ,org-agenda-redo-command
  4337. org-series-cmd ,org-cmd))
  4338. (org-agenda-finalize)
  4339. (setq buffer-read-only t))))
  4340. ;;; Agenda TODO list
  4341. (defun org-agenda-propertize-selected-todo-keywords (keywords)
  4342. "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
  4343. (concat
  4344. (if (or (equal keywords "ALL") (not keywords))
  4345. (propertize "ALL" 'face 'warning)
  4346. (mapconcat
  4347. (lambda (kw)
  4348. (propertize kw 'face (org-get-todo-face kw)))
  4349. (org-split-string keywords "|")
  4350. "|"))
  4351. "\n"))
  4352. (defvar org-select-this-todo-keyword nil)
  4353. (defvar org-last-arg nil)
  4354. ;;;###autoload
  4355. (defun org-todo-list (&optional arg)
  4356. "Show all (not done) TODO entries from all agenda file in a single list.
  4357. The prefix arg can be used to select a specific TODO keyword and limit
  4358. the list to these. When using `\\[universal-argument]', you will be prompted
  4359. for a keyword. A numeric prefix directly selects the Nth keyword in
  4360. `org-todo-keywords-1'."
  4361. (interactive "P")
  4362. (when org-agenda-overriding-arguments
  4363. (setq arg org-agenda-overriding-arguments))
  4364. (when (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
  4365. (let* ((today (org-today))
  4366. (date (calendar-gregorian-from-absolute today))
  4367. (completion-ignore-case t)
  4368. kwds org-select-this-todo-keyword rtn rtnall files file pos)
  4369. (catch 'exit
  4370. (when org-agenda-sticky
  4371. (setq org-agenda-buffer-name
  4372. (if (stringp org-select-this-todo-keyword)
  4373. (format "*Org Agenda(%s:%s)*" (or org-keys "t")
  4374. org-select-this-todo-keyword)
  4375. (format "*Org Agenda(%s)*" (or org-keys "t")))))
  4376. (org-agenda-prepare "TODO")
  4377. (setq kwds org-todo-keywords-for-agenda
  4378. org-select-this-todo-keyword (if (stringp arg) arg
  4379. (and (integerp arg)
  4380. (> arg 0)
  4381. (nth (1- arg) kwds))))
  4382. (when (equal arg '(4))
  4383. (setq org-select-this-todo-keyword
  4384. (completing-read "Keyword (or KWD1|K2D2|...): "
  4385. (mapcar #'list kwds) nil nil)))
  4386. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  4387. (org-compile-prefix-format 'todo)
  4388. (org-set-sorting-strategy 'todo)
  4389. (setq org-agenda-redo-command
  4390. `(org-todo-list (or (and (numberp current-prefix-arg)
  4391. current-prefix-arg)
  4392. ,org-select-this-todo-keyword
  4393. current-prefix-arg ,arg)))
  4394. (setq files (org-agenda-files nil 'ifmode)
  4395. rtnall nil)
  4396. (while (setq file (pop files))
  4397. (catch 'nextfile
  4398. (org-check-agenda-file file)
  4399. (setq rtn (org-agenda-get-day-entries file date :todo))
  4400. (setq rtnall (append rtnall rtn))))
  4401. (org-agenda--insert-overriding-header
  4402. (with-temp-buffer
  4403. (insert "Global list of TODO items of type: ")
  4404. (add-text-properties (point-min) (1- (point))
  4405. (list 'face 'org-agenda-structure
  4406. 'short-heading
  4407. (concat "ToDo: "
  4408. (or org-select-this-todo-keyword "ALL"))))
  4409. (org-agenda-mark-header-line (point-min))
  4410. (insert (org-agenda-propertize-selected-todo-keywords
  4411. org-select-this-todo-keyword))
  4412. (setq pos (point))
  4413. (unless org-agenda-multi
  4414. (insert (substitute-command-keys "Press \
  4415. \\<org-agenda-mode-map>`N \\[org-agenda-redo]' (e.g. `0 \\[org-agenda-redo]') \
  4416. to search again: (0)[ALL]"))
  4417. (let ((n 0))
  4418. (dolist (k kwds)
  4419. (let ((s (format "(%d)%s" (cl-incf n) k)))
  4420. (when (> (+ (current-column) (string-width s) 1) (window-width))
  4421. (insert "\n "))
  4422. (insert " " s))))
  4423. (insert "\n"))
  4424. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure))
  4425. (buffer-string)))
  4426. (org-agenda-mark-header-line (point-min))
  4427. (when rtnall
  4428. (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
  4429. (goto-char (point-min))
  4430. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  4431. (add-text-properties (point-min) (point-max)
  4432. `(org-agenda-type todo
  4433. org-last-args ,arg
  4434. org-redo-cmd ,org-agenda-redo-command
  4435. org-series-cmd ,org-cmd))
  4436. (org-agenda-finalize)
  4437. (setq buffer-read-only t))))
  4438. ;;; Agenda tags match
  4439. ;;;###autoload
  4440. (defun org-tags-view (&optional todo-only match)
  4441. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  4442. The prefix arg TODO-ONLY limits the search to TODO entries."
  4443. (interactive "P")
  4444. (when org-agenda-overriding-arguments
  4445. (setq todo-only (car org-agenda-overriding-arguments)
  4446. match (nth 1 org-agenda-overriding-arguments)))
  4447. (let* ((org-tags-match-list-sublevels
  4448. org-tags-match-list-sublevels)
  4449. (completion-ignore-case t)
  4450. (org--matcher-tags-todo-only todo-only)
  4451. rtn rtnall files file pos matcher
  4452. buffer)
  4453. (when (and (stringp match) (not (string-match "\\S-" match)))
  4454. (setq match nil))
  4455. (catch 'exit
  4456. ;; TODO: this code is repeated a lot...
  4457. (when org-agenda-sticky
  4458. (setq org-agenda-buffer-name
  4459. (if (stringp match)
  4460. (format "*Org Agenda(%s:%s)*"
  4461. (or org-keys (or (and todo-only "M") "m")) match)
  4462. (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
  4463. (setq matcher (org-make-tags-matcher match))
  4464. ;; Prepare agendas (and `org-tag-alist-for-agenda') before
  4465. ;; expanding tags within `org-make-tags-matcher'
  4466. (org-agenda-prepare (concat "TAGS " match))
  4467. (setq match (car matcher)
  4468. matcher (cdr matcher))
  4469. (org-compile-prefix-format 'tags)
  4470. (org-set-sorting-strategy 'tags)
  4471. (setq org-agenda-query-string match)
  4472. (setq org-agenda-redo-command
  4473. (list 'org-tags-view
  4474. `(quote ,org--matcher-tags-todo-only)
  4475. `(if current-prefix-arg nil ,org-agenda-query-string)))
  4476. (setq files (org-agenda-files nil 'ifmode)
  4477. rtnall nil)
  4478. (while (setq file (pop files))
  4479. (catch 'nextfile
  4480. (org-check-agenda-file file)
  4481. (setq buffer (if (file-exists-p file)
  4482. (org-get-agenda-file-buffer file)
  4483. (error "No such file %s" file)))
  4484. (if (not buffer)
  4485. ;; If file does not exist, error message to agenda
  4486. (setq rtn (list
  4487. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  4488. rtnall (append rtnall rtn))
  4489. (with-current-buffer buffer
  4490. (unless (derived-mode-p 'org-mode)
  4491. (error "Agenda file %s is not in Org mode" file))
  4492. (save-excursion
  4493. (save-restriction
  4494. (if (eq buffer org-agenda-restrict)
  4495. (narrow-to-region org-agenda-restrict-begin
  4496. org-agenda-restrict-end)
  4497. (widen))
  4498. (setq rtn (org-scan-tags 'agenda
  4499. matcher
  4500. org--matcher-tags-todo-only))
  4501. (setq rtnall (append rtnall rtn))))))))
  4502. (org-agenda--insert-overriding-header
  4503. (with-temp-buffer
  4504. (insert "Headlines with TAGS match: ")
  4505. (add-text-properties (point-min) (1- (point))
  4506. (list 'face 'org-agenda-structure
  4507. 'short-heading
  4508. (concat "Match: " match)))
  4509. (setq pos (point))
  4510. (insert match "\n")
  4511. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  4512. (setq pos (point))
  4513. (unless org-agenda-multi
  4514. (insert (substitute-command-keys
  4515. "Press \
  4516. \\<org-agenda-mode-map>`\\[universal-argument] \\[org-agenda-redo]' \
  4517. to search again\n")))
  4518. (add-text-properties pos (1- (point))
  4519. (list 'face 'org-agenda-structure))
  4520. (buffer-string)))
  4521. (org-agenda-mark-header-line (point-min))
  4522. (when rtnall
  4523. (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
  4524. (goto-char (point-min))
  4525. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  4526. (add-text-properties
  4527. (point-min) (point-max)
  4528. `(org-agenda-type tags
  4529. org-last-args (,org--matcher-tags-todo-only ,match)
  4530. org-redo-cmd ,org-agenda-redo-command
  4531. org-series-cmd ,org-cmd))
  4532. (org-agenda-finalize)
  4533. (setq buffer-read-only t))))
  4534. ;;; Agenda Finding stuck projects
  4535. (defvar org-agenda-skip-regexp nil
  4536. "Regular expression used in skipping subtrees for the agenda.
  4537. This is basically a temporary global variable that can be set and then
  4538. used by user-defined selections using `org-agenda-skip-function'.")
  4539. (defvar org-agenda-overriding-header nil
  4540. "When set during agenda, todo and tags searches it replaces the header.
  4541. If an empty string, no header will be inserted. If any other
  4542. string, it will be inserted as a header. If nil, a header will
  4543. be generated automatically according to the command. This
  4544. variable should not be set directly, but custom commands can bind
  4545. it in the options section.")
  4546. (defun org-agenda-skip-entry-if (&rest conditions)
  4547. "Skip entry if any of CONDITIONS is true.
  4548. See `org-agenda-skip-if' for details."
  4549. (org-agenda-skip-if nil conditions))
  4550. (defun org-agenda-skip-subtree-if (&rest conditions)
  4551. "Skip subtree if any of CONDITIONS is true.
  4552. See `org-agenda-skip-if' for details."
  4553. (org-agenda-skip-if t conditions))
  4554. (defun org-agenda-skip-if (subtree conditions)
  4555. "Checks current entity for CONDITIONS.
  4556. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  4557. the entry (i.e. the text before the next heading) is checked.
  4558. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  4559. from different tests. Valid conditions are:
  4560. scheduled Check if there is a scheduled cookie
  4561. notscheduled Check if there is no scheduled cookie
  4562. deadline Check if there is a deadline
  4563. notdeadline Check if there is no deadline
  4564. timestamp Check if there is a timestamp (also deadline or scheduled)
  4565. nottimestamp Check if there is no timestamp (also deadline or scheduled)
  4566. regexp Check if regexp matches
  4567. notregexp Check if regexp does not match.
  4568. todo Check if TODO keyword matches
  4569. nottodo Check if TODO keyword does not match
  4570. The regexp is taken from the conditions list, it must come right after
  4571. the `regexp' or `notregexp' element.
  4572. `todo' and `nottodo' accept as an argument a list of todo
  4573. keywords, which may include \"*\" to match any todo keyword.
  4574. (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
  4575. would skip all entries with \"TODO\" or \"WAITING\" keywords.
  4576. Instead of a list, a keyword class may be given. For example:
  4577. (org-agenda-skip-entry-if \\='nottodo \\='done)
  4578. would skip entries that haven't been marked with any of \"DONE\"
  4579. keywords. Possible classes are: `todo', `done', `any'.
  4580. If any of these conditions is met, this function returns the end point of
  4581. the entity, causing the search to continue from there. This is a function
  4582. that can be put into `org-agenda-skip-function' for the duration of a command."
  4583. (org-back-to-heading t)
  4584. (let* ((beg (point))
  4585. (end (if subtree (save-excursion (org-end-of-subtree t) (point))
  4586. (org-entry-end-position)))
  4587. (planning-end (if subtree end (line-end-position 2)))
  4588. m)
  4589. (and
  4590. (or (and (memq 'scheduled conditions)
  4591. (re-search-forward org-scheduled-time-regexp planning-end t))
  4592. (and (memq 'notscheduled conditions)
  4593. (not
  4594. (save-excursion
  4595. (re-search-forward org-scheduled-time-regexp planning-end t))))
  4596. (and (memq 'deadline conditions)
  4597. (re-search-forward org-deadline-time-regexp planning-end t))
  4598. (and (memq 'notdeadline conditions)
  4599. (not
  4600. (save-excursion
  4601. (re-search-forward org-deadline-time-regexp planning-end t))))
  4602. (and (memq 'timestamp conditions)
  4603. (re-search-forward org-ts-regexp end t))
  4604. (and (memq 'nottimestamp conditions)
  4605. (not (save-excursion (re-search-forward org-ts-regexp end t))))
  4606. (and (setq m (memq 'regexp conditions))
  4607. (stringp (nth 1 m))
  4608. (re-search-forward (nth 1 m) end t))
  4609. (and (setq m (memq 'notregexp conditions))
  4610. (stringp (nth 1 m))
  4611. (not (save-excursion (re-search-forward (nth 1 m) end t))))
  4612. (and (or
  4613. (setq m (memq 'nottodo conditions))
  4614. (setq m (memq 'todo-unblocked conditions))
  4615. (setq m (memq 'nottodo-unblocked conditions))
  4616. (setq m (memq 'todo conditions)))
  4617. (org-agenda-skip-if-todo m end)))
  4618. end)))
  4619. (defun org-agenda-skip-if-todo (args end)
  4620. "Helper function for `org-agenda-skip-if', do not use it directly.
  4621. ARGS is a list with first element either `todo', `nottodo',
  4622. `todo-unblocked' or `nottodo-unblocked'. The remainder is either
  4623. a list of TODO keywords, or a state symbol `todo' or `done' or
  4624. `any'."
  4625. (let ((todo-re
  4626. (concat "^\\*+[ \t]+"
  4627. (regexp-opt
  4628. (pcase args
  4629. (`(,_ todo)
  4630. (org-delete-all org-done-keywords
  4631. (copy-sequence org-todo-keywords-1)))
  4632. (`(,_ done) org-done-keywords)
  4633. (`(,_ any) org-todo-keywords-1)
  4634. (`(,_ ,(pred atom))
  4635. (error "Invalid TODO class or type: %S" args))
  4636. (`(,_ ,(pred (member "*"))) org-todo-keywords-1)
  4637. (`(,_ ,todo-list) todo-list))
  4638. 'words))))
  4639. (pcase args
  4640. (`(todo . ,_)
  4641. (let (case-fold-search) (re-search-forward todo-re end t)))
  4642. (`(nottodo . ,_)
  4643. (not (let (case-fold-search) (re-search-forward todo-re end t))))
  4644. (`(todo-unblocked . ,_)
  4645. (catch :unblocked
  4646. (while (let (case-fold-search) (re-search-forward todo-re end t))
  4647. (when (org-entry-blocked-p) (throw :unblocked t)))
  4648. nil))
  4649. (`(nottodo-unblocked . ,_)
  4650. (catch :unblocked
  4651. (while (let (case-fold-search) (re-search-forward todo-re end t))
  4652. (when (org-entry-blocked-p) (throw :unblocked nil)))
  4653. t))
  4654. (`(,type . ,_) (error "Unknown TODO skip type: %S" type)))))
  4655. ;;;###autoload
  4656. (defun org-agenda-list-stuck-projects (&rest ignore)
  4657. "Create agenda view for projects that are stuck.
  4658. Stuck projects are project that have no next actions. For the definitions
  4659. of what a project is and how to check if it stuck, customize the variable
  4660. `org-stuck-projects'."
  4661. (interactive)
  4662. (let* ((org-agenda-overriding-header
  4663. (or org-agenda-overriding-header "List of stuck projects: "))
  4664. (matcher (nth 0 org-stuck-projects))
  4665. (todo (nth 1 org-stuck-projects))
  4666. (tags (nth 2 org-stuck-projects))
  4667. (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
  4668. (todo-wds
  4669. (if (not (member "*" todo)) todo
  4670. (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
  4671. (org-delete-all org-done-keywords-for-agenda
  4672. (copy-sequence org-todo-keywords-for-agenda))))
  4673. (todo-re (and todo
  4674. (format "^\\*+[ \t]+\\(%s\\)\\>"
  4675. (mapconcat #'identity todo-wds "\\|"))))
  4676. (tags-re (cond ((null tags) nil)
  4677. ((member "*" tags) org-tag-line-re)
  4678. (tags
  4679. (let ((other-tags (format "\\(?:%s:\\)*" org-tag-re)))
  4680. (concat org-outline-regexp-bol
  4681. ".*?[ \t]:"
  4682. other-tags
  4683. (regexp-opt tags t)
  4684. ":" other-tags "[ \t]*$")))
  4685. (t nil)))
  4686. (re-list (delq nil (list todo-re tags-re gen-re)))
  4687. (skip-re
  4688. (if (null re-list)
  4689. (error "Missing information to identify unstuck projects")
  4690. (mapconcat #'identity re-list "\\|")))
  4691. (org-agenda-skip-function
  4692. ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
  4693. ;; in the subtree.
  4694. `(lambda ()
  4695. (and (save-excursion
  4696. (let ((case-fold-search nil))
  4697. (re-search-forward
  4698. ,skip-re (save-excursion (org-end-of-subtree t)) t)))
  4699. (progn (outline-next-heading) (point))))))
  4700. (org-tags-view nil matcher)
  4701. (setq org-agenda-buffer-name (buffer-name))
  4702. (with-current-buffer org-agenda-buffer-name
  4703. (setq org-agenda-redo-command
  4704. `(org-agenda-list-stuck-projects ,current-prefix-arg))
  4705. (let ((inhibit-read-only t))
  4706. (add-text-properties
  4707. (point-min) (point-max)
  4708. `(org-redo-cmd ,org-agenda-redo-command))))))
  4709. ;;; Diary integration
  4710. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  4711. (defvar diary-list-entries-hook)
  4712. (defvar diary-time-regexp)
  4713. (defun org-get-entries-from-diary (date)
  4714. "Get the (Emacs Calendar) diary entries for DATE."
  4715. (require 'diary-lib)
  4716. (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
  4717. (diary-display-function 'diary-fancy-display)
  4718. (pop-up-frames nil)
  4719. (diary-list-entries-hook
  4720. (cons 'org-diary-default-entry diary-list-entries-hook))
  4721. (diary-file-name-prefix nil) ; turn this feature off
  4722. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  4723. (diary-time-regexp (concat "^" diary-time-regexp))
  4724. entries
  4725. (org-disable-agenda-to-diary t))
  4726. (save-excursion
  4727. (save-window-excursion
  4728. (funcall (if (fboundp 'diary-list-entries)
  4729. 'diary-list-entries 'list-diary-entries)
  4730. date 1)))
  4731. (if (not (get-buffer diary-fancy-buffer))
  4732. (setq entries nil)
  4733. (with-current-buffer diary-fancy-buffer
  4734. (setq buffer-read-only nil)
  4735. (if (zerop (buffer-size))
  4736. ;; No entries
  4737. (setq entries nil)
  4738. ;; Omit the date and other unnecessary stuff
  4739. (org-agenda-cleanup-fancy-diary)
  4740. ;; Add prefix to each line and extend the text properties
  4741. (if (zerop (buffer-size))
  4742. (setq entries nil)
  4743. (setq entries (buffer-substring (point-min) (- (point-max) 1)))
  4744. (setq entries
  4745. (with-temp-buffer
  4746. (insert entries) (goto-char (point-min))
  4747. (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
  4748. (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
  4749. (replace-match (concat "; " (match-string 1)))))
  4750. (buffer-string)))))
  4751. (set-buffer-modified-p nil)
  4752. (kill-buffer diary-fancy-buffer)))
  4753. (when entries
  4754. (setq entries (org-split-string entries "\n"))
  4755. (setq entries
  4756. (mapcar
  4757. (lambda (x)
  4758. (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
  4759. ;; Extend the text properties to the beginning of the line
  4760. (org-add-props x (text-properties-at (1- (length x)) x)
  4761. 'type "diary" 'date date 'face 'org-agenda-diary))
  4762. entries)))))
  4763. (defvar org-agenda-cleanup-fancy-diary-hook nil
  4764. "Hook run when the fancy diary buffer is cleaned up.")
  4765. (defun org-agenda-cleanup-fancy-diary ()
  4766. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  4767. This gets rid of the date, the underline under the date, and the
  4768. dummy entry installed by Org mode to ensure non-empty diary for
  4769. each date. It also removes lines that contain only whitespace."
  4770. (goto-char (point-min))
  4771. (if (looking-at ".*?:[ \t]*")
  4772. (progn
  4773. (replace-match "")
  4774. (re-search-forward "\n=+$" nil t)
  4775. (replace-match "")
  4776. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  4777. (re-search-forward "\n=+$" nil t)
  4778. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  4779. (goto-char (point-min))
  4780. (while (re-search-forward "^ +\n" nil t)
  4781. (replace-match ""))
  4782. (goto-char (point-min))
  4783. (when (re-search-forward "^Org mode dummy\n?" nil t)
  4784. (replace-match ""))
  4785. (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
  4786. (defun org-modify-diary-entry-string (string)
  4787. "Add text properties to string, allowing Org to act on it."
  4788. (org-add-props string nil
  4789. 'mouse-face 'highlight
  4790. 'help-echo (if buffer-file-name
  4791. (format "mouse-2 or RET jump to diary file %s"
  4792. (abbreviate-file-name buffer-file-name))
  4793. "")
  4794. 'org-agenda-diary-link t
  4795. 'org-marker (org-agenda-new-marker (point-at-bol))))
  4796. (defun org-diary-default-entry ()
  4797. "Add a dummy entry to the diary.
  4798. Needed to avoid empty dates which mess up holiday display."
  4799. ;; Catch the error if dealing with the new add-to-diary-alist
  4800. (when org-disable-agenda-to-diary
  4801. (condition-case nil
  4802. (org-add-to-diary-list original-date "Org mode dummy" "")
  4803. (error
  4804. (org-add-to-diary-list original-date "Org mode dummy" "" nil)))))
  4805. (defun org-add-to-diary-list (&rest args)
  4806. (if (fboundp 'diary-add-to-list)
  4807. (apply 'diary-add-to-list args)
  4808. (apply 'add-to-diary-list args)))
  4809. (defvar org-diary-last-run-time nil)
  4810. ;;;###autoload
  4811. (defun org-diary (&rest args)
  4812. "Return diary information from org files.
  4813. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  4814. It accesses org files and extracts information from those files to be
  4815. listed in the diary. The function accepts arguments specifying what
  4816. items should be listed. For a list of arguments allowed here, see the
  4817. variable `org-agenda-entry-types'.
  4818. The call in the diary file should look like this:
  4819. &%%(org-diary) ~/path/to/some/orgfile.org
  4820. Use a separate line for each org file to check. Or, if you omit the file name,
  4821. all files listed in `org-agenda-files' will be checked automatically:
  4822. &%%(org-diary)
  4823. If you don't give any arguments (as in the example above), the default value
  4824. of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
  4825. So the example above may also be written as
  4826. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  4827. The function expects the lisp variables `entry' and `date' to be provided
  4828. by the caller, because this is how the calendar works. Don't use this
  4829. function from a program - use `org-agenda-get-day-entries' instead."
  4830. (when (> (- (float-time)
  4831. org-agenda-last-marker-time)
  4832. 5)
  4833. ;; I am not sure if this works with sticky agendas, because the marker
  4834. ;; list is then no longer a global variable.
  4835. (org-agenda-reset-markers))
  4836. (org-compile-prefix-format 'agenda)
  4837. (org-set-sorting-strategy 'agenda)
  4838. (setq args (or args org-agenda-entry-types))
  4839. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  4840. (list entry)
  4841. (org-agenda-files t)))
  4842. (time (float-time))
  4843. file rtn results)
  4844. (when (or (not org-diary-last-run-time)
  4845. (> (- time
  4846. org-diary-last-run-time)
  4847. 3))
  4848. (org-agenda-prepare-buffers files))
  4849. (setq org-diary-last-run-time time)
  4850. ;; If this is called during org-agenda, don't return any entries to
  4851. ;; the calendar. Org Agenda will list these entries itself.
  4852. (when org-disable-agenda-to-diary (setq files nil))
  4853. (while (setq file (pop files))
  4854. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  4855. (setq results (append results rtn)))
  4856. (when results
  4857. (setq results
  4858. (mapcar (lambda (i) (replace-regexp-in-string
  4859. org-link-bracket-re "\\2" i)) results))
  4860. (concat (org-agenda-finalize-entries results) "\n"))))
  4861. ;;; Agenda entry finders
  4862. (defun org-agenda--timestamp-to-absolute (&rest args)
  4863. "Call `org-time-string-to-absolute' with ARGS.
  4864. However, throw `:skip' whenever an error is raised."
  4865. (condition-case e
  4866. (apply #'org-time-string-to-absolute args)
  4867. (org-diary-sexp-no-match (throw :skip nil))
  4868. (error
  4869. (message "%s; Skipping entry" (error-message-string e))
  4870. (throw :skip nil))))
  4871. (defun org-agenda-get-day-entries (file date &rest args)
  4872. "Does the work for `org-diary' and `org-agenda'.
  4873. FILE is the path to a file to be checked for entries. DATE is date like
  4874. the one returned by `calendar-current-date'. ARGS are symbols indicating
  4875. which kind of entries should be extracted. For details about these, see
  4876. the documentation of `org-diary'."
  4877. (let* ((org-startup-folded nil)
  4878. (org-startup-align-all-tables nil)
  4879. (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
  4880. (error "No such file %s" file))))
  4881. (if (not buffer)
  4882. ;; If file does not exist, signal it in diary nonetheless.
  4883. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  4884. (with-current-buffer buffer
  4885. (unless (derived-mode-p 'org-mode)
  4886. (error "Agenda file %s is not in Org mode" file))
  4887. (setq org-agenda-buffer (or org-agenda-buffer buffer))
  4888. (setf org-agenda-current-date date)
  4889. (save-excursion
  4890. (save-restriction
  4891. (if (eq buffer org-agenda-restrict)
  4892. (narrow-to-region org-agenda-restrict-begin
  4893. org-agenda-restrict-end)
  4894. (widen))
  4895. ;; Rationalize ARGS. Also make sure `:deadline' comes
  4896. ;; first in order to populate DEADLINES before passing it.
  4897. ;;
  4898. ;; We use `delq' since `org-uniquify' duplicates ARGS,
  4899. ;; guarding us from modifying `org-agenda-entry-types'.
  4900. (setf args (org-uniquify (or args org-agenda-entry-types)))
  4901. (when (and (memq :scheduled args) (memq :scheduled* args))
  4902. (setf args (delq :scheduled* args)))
  4903. (cond
  4904. ((memq :deadline args)
  4905. (setf args (cons :deadline
  4906. (delq :deadline (delq :deadline* args)))))
  4907. ((memq :deadline* args)
  4908. (setf args (cons :deadline* (delq :deadline* args)))))
  4909. ;; Collect list of headlines. Return them flattened.
  4910. (let ((case-fold-search nil) results deadlines)
  4911. (dolist (arg args (apply #'nconc (nreverse results)))
  4912. (pcase arg
  4913. ((and :todo (guard (org-agenda-today-p date)))
  4914. (push (org-agenda-get-todos) results))
  4915. (:timestamp
  4916. (push (org-agenda-get-blocks) results)
  4917. (push (org-agenda-get-timestamps deadlines) results))
  4918. (:sexp
  4919. (push (org-agenda-get-sexps) results))
  4920. (:scheduled
  4921. (push (org-agenda-get-scheduled deadlines) results))
  4922. (:scheduled*
  4923. (push (org-agenda-get-scheduled deadlines t) results))
  4924. (:closed
  4925. (push (org-agenda-get-progress) results))
  4926. (:deadline
  4927. (setf deadlines (org-agenda-get-deadlines))
  4928. (push deadlines results))
  4929. (:deadline*
  4930. (setf deadlines (org-agenda-get-deadlines t))
  4931. (push deadlines results)))))))))))
  4932. (defsubst org-em (x y list)
  4933. "Is X or Y a member of LIST?"
  4934. (or (memq x list) (memq y list)))
  4935. (defvar org-heading-keyword-regexp-format) ; defined in org.el
  4936. (defvar org-agenda-sorting-strategy-selected nil)
  4937. (defun org-agenda-entry-get-agenda-timestamp (pom)
  4938. "Retrieve timestamp information for sorting agenda views.
  4939. Given a point or marker POM, returns a cons cell of the timestamp
  4940. and the timestamp type relevant for the sorting strategy in
  4941. `org-agenda-sorting-strategy-selected'."
  4942. (let (ts ts-date-type)
  4943. (save-match-data
  4944. (cond ((org-em 'scheduled-up 'scheduled-down
  4945. org-agenda-sorting-strategy-selected)
  4946. (setq ts (org-entry-get pom "SCHEDULED")
  4947. ts-date-type " scheduled"))
  4948. ((org-em 'deadline-up 'deadline-down
  4949. org-agenda-sorting-strategy-selected)
  4950. (setq ts (org-entry-get pom "DEADLINE")
  4951. ts-date-type " deadline"))
  4952. ((org-em 'ts-up 'ts-down
  4953. org-agenda-sorting-strategy-selected)
  4954. (setq ts (org-entry-get pom "TIMESTAMP")
  4955. ts-date-type " timestamp"))
  4956. ((org-em 'tsia-up 'tsia-down
  4957. org-agenda-sorting-strategy-selected)
  4958. (setq ts (org-entry-get pom "TIMESTAMP_IA")
  4959. ts-date-type " timestamp_ia"))
  4960. ((org-em 'timestamp-up 'timestamp-down
  4961. org-agenda-sorting-strategy-selected)
  4962. (setq ts (or (org-entry-get pom "SCHEDULED")
  4963. (org-entry-get pom "DEADLINE")
  4964. (org-entry-get pom "TIMESTAMP")
  4965. (org-entry-get pom "TIMESTAMP_IA"))
  4966. ts-date-type ""))
  4967. (t (setq ts-date-type "")))
  4968. (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
  4969. ts-date-type))))
  4970. (defun org-agenda-get-todos ()
  4971. "Return the TODO information for agenda display."
  4972. (let* ((props (list 'face nil
  4973. 'done-face 'org-agenda-done
  4974. 'org-not-done-regexp org-not-done-regexp
  4975. 'org-todo-regexp org-todo-regexp
  4976. 'org-complex-heading-regexp org-complex-heading-regexp
  4977. 'mouse-face 'highlight
  4978. 'help-echo
  4979. (format "mouse-2 or RET jump to org file %s"
  4980. (abbreviate-file-name buffer-file-name))))
  4981. (case-fold-search nil)
  4982. (regexp (format org-heading-keyword-regexp-format
  4983. (cond
  4984. ((and org-select-this-todo-keyword
  4985. (equal org-select-this-todo-keyword "*"))
  4986. org-todo-regexp)
  4987. (org-select-this-todo-keyword
  4988. (concat "\\("
  4989. (mapconcat 'identity
  4990. (org-split-string
  4991. org-select-this-todo-keyword
  4992. "|")
  4993. "\\|") "\\)"))
  4994. (t org-not-done-regexp))))
  4995. marker priority category level tags todo-state
  4996. ts-date ts-date-type ts-date-pair
  4997. ee txt beg end inherited-tags todo-state-end-pos)
  4998. (goto-char (point-min))
  4999. (while (re-search-forward regexp nil t)
  5000. (catch :skip
  5001. (save-match-data
  5002. (beginning-of-line)
  5003. (org-agenda-skip)
  5004. (setq beg (point) end (save-excursion (outline-next-heading) (point)))
  5005. (unless (and (setq todo-state (org-get-todo-state))
  5006. (setq todo-state-end-pos (match-end 2)))
  5007. (goto-char end)
  5008. (throw :skip nil))
  5009. (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
  5010. (goto-char (1+ beg))
  5011. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  5012. (throw :skip nil)))
  5013. (goto-char (match-beginning 2))
  5014. (setq marker (org-agenda-new-marker (match-beginning 0))
  5015. category (org-get-category)
  5016. ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  5017. ts-date (car ts-date-pair)
  5018. ts-date-type (cdr ts-date-pair)
  5019. txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
  5020. inherited-tags
  5021. (or (eq org-agenda-show-inherited-tags 'always)
  5022. (and (listp org-agenda-show-inherited-tags)
  5023. (memq 'todo org-agenda-show-inherited-tags))
  5024. (and (eq org-agenda-show-inherited-tags t)
  5025. (or (eq org-agenda-use-tag-inheritance t)
  5026. (memq 'todo org-agenda-use-tag-inheritance))))
  5027. tags (org-get-tags nil (not inherited-tags))
  5028. level (make-string (org-reduced-level (org-outline-level)) ? )
  5029. txt (org-agenda-format-item "" txt level category tags t)
  5030. priority (1+ (org-get-priority txt)))
  5031. (org-add-props txt props
  5032. 'org-marker marker 'org-hd-marker marker
  5033. 'priority priority
  5034. 'level level
  5035. 'ts-date ts-date
  5036. 'type (concat "todo" ts-date-type) 'todo-state todo-state)
  5037. (push txt ee)
  5038. (if org-agenda-todo-list-sublevels
  5039. (goto-char todo-state-end-pos)
  5040. (org-end-of-subtree 'invisible))))
  5041. (nreverse ee)))
  5042. (defun org-agenda-todo-custom-ignore-p (time n)
  5043. "Check whether timestamp is farther away than n number of days.
  5044. This function is invoked if `org-agenda-todo-ignore-deadlines',
  5045. `org-agenda-todo-ignore-scheduled' or
  5046. `org-agenda-todo-ignore-timestamp' is set to an integer."
  5047. (let ((days (org-time-stamp-to-now
  5048. time org-agenda-todo-ignore-time-comparison-use-seconds)))
  5049. (if (>= n 0)
  5050. (>= days n)
  5051. (<= days n))))
  5052. ;;;###autoload
  5053. (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  5054. (&optional end)
  5055. "Do we have a reason to ignore this TODO entry because it has a time stamp?"
  5056. (when (or org-agenda-todo-ignore-with-date
  5057. org-agenda-todo-ignore-scheduled
  5058. org-agenda-todo-ignore-deadlines
  5059. org-agenda-todo-ignore-timestamp)
  5060. (setq end (or end (save-excursion (outline-next-heading) (point))))
  5061. (save-excursion
  5062. (or (and org-agenda-todo-ignore-with-date
  5063. (re-search-forward org-ts-regexp end t))
  5064. (and org-agenda-todo-ignore-scheduled
  5065. (re-search-forward org-scheduled-time-regexp end t)
  5066. (cond
  5067. ((eq org-agenda-todo-ignore-scheduled 'future)
  5068. (> (org-time-stamp-to-now
  5069. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
  5070. ((eq org-agenda-todo-ignore-scheduled 'past)
  5071. (<= (org-time-stamp-to-now
  5072. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
  5073. ((numberp org-agenda-todo-ignore-scheduled)
  5074. (org-agenda-todo-custom-ignore-p
  5075. (match-string 1) org-agenda-todo-ignore-scheduled))
  5076. (t)))
  5077. (and org-agenda-todo-ignore-deadlines
  5078. (re-search-forward org-deadline-time-regexp end t)
  5079. (cond
  5080. ((eq org-agenda-todo-ignore-deadlines 'all) t)
  5081. ((eq org-agenda-todo-ignore-deadlines 'far)
  5082. (not (org-deadline-close-p (match-string 1))))
  5083. ((eq org-agenda-todo-ignore-deadlines 'future)
  5084. (> (org-time-stamp-to-now
  5085. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
  5086. ((eq org-agenda-todo-ignore-deadlines 'past)
  5087. (<= (org-time-stamp-to-now
  5088. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
  5089. ((numberp org-agenda-todo-ignore-deadlines)
  5090. (org-agenda-todo-custom-ignore-p
  5091. (match-string 1) org-agenda-todo-ignore-deadlines))
  5092. (t (org-deadline-close-p (match-string 1)))))
  5093. (and org-agenda-todo-ignore-timestamp
  5094. (let ((buffer (current-buffer))
  5095. (regexp
  5096. (concat
  5097. org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
  5098. (start (point)))
  5099. ;; Copy current buffer into a temporary one
  5100. (with-temp-buffer
  5101. (insert-buffer-substring buffer start end)
  5102. (goto-char (point-min))
  5103. ;; Delete SCHEDULED and DEADLINE items
  5104. (while (re-search-forward regexp end t)
  5105. (delete-region (match-beginning 0) (match-end 0)))
  5106. (goto-char (point-min))
  5107. ;; No search for timestamp left
  5108. (when (re-search-forward org-ts-regexp nil t)
  5109. (cond
  5110. ((eq org-agenda-todo-ignore-timestamp 'future)
  5111. (> (org-time-stamp-to-now
  5112. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
  5113. ((eq org-agenda-todo-ignore-timestamp 'past)
  5114. (<= (org-time-stamp-to-now
  5115. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
  5116. ((numberp org-agenda-todo-ignore-timestamp)
  5117. (org-agenda-todo-custom-ignore-p
  5118. (match-string 1) org-agenda-todo-ignore-timestamp))
  5119. (t))))))))))
  5120. (defun org-agenda-get-timestamps (&optional deadlines)
  5121. "Return the date stamp information for agenda display.
  5122. Optional argument DEADLINES is a list of deadline items to be
  5123. displayed in agenda view."
  5124. (let* ((props (list 'face 'org-agenda-calendar-event
  5125. 'org-not-done-regexp org-not-done-regexp
  5126. 'org-todo-regexp org-todo-regexp
  5127. 'org-complex-heading-regexp org-complex-heading-regexp
  5128. 'mouse-face 'highlight
  5129. 'help-echo
  5130. (format "mouse-2 or RET jump to Org file %s"
  5131. (abbreviate-file-name buffer-file-name))))
  5132. (current (calendar-absolute-from-gregorian date))
  5133. (today (org-today))
  5134. (deadline-position-alist
  5135. (mapcar (lambda (d)
  5136. (let ((m (get-text-property 0 'org-hd-marker d)))
  5137. (and m (marker-position m))))
  5138. deadlines))
  5139. ;; Match time-stamps set to current date, time-stamps with
  5140. ;; a repeater, and S-exp time-stamps.
  5141. (regexp
  5142. (concat
  5143. (if org-agenda-include-inactive-timestamps "[[<]" "<")
  5144. (regexp-quote
  5145. (substring
  5146. (format-time-string
  5147. (car org-time-stamp-formats)
  5148. (encode-time ; DATE bound by calendar
  5149. 0 0 0 (nth 1 date) (car date) (nth 2 date)))
  5150. 1 11))
  5151. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
  5152. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  5153. timestamp-items)
  5154. (goto-char (point-min))
  5155. (while (re-search-forward regexp nil t)
  5156. ;; Skip date ranges, scheduled and deadlines, which are handled
  5157. ;; specially. Also skip time-stamps before first headline as
  5158. ;; there would be no entry to add to the agenda. Eventually,
  5159. ;; ignore clock entries.
  5160. (catch :skip
  5161. (save-match-data
  5162. (when (or (org-at-date-range-p)
  5163. (org-at-planning-p)
  5164. (org-before-first-heading-p)
  5165. (and org-agenda-include-inactive-timestamps
  5166. (org-at-clock-log-p)))
  5167. (throw :skip nil))
  5168. (org-agenda-skip))
  5169. (let* ((pos (match-beginning 0))
  5170. (repeat (match-string 1))
  5171. (sexp-entry (match-string 3))
  5172. (time-stamp (if (or repeat sexp-entry) (match-string 0)
  5173. (save-excursion
  5174. (goto-char pos)
  5175. (looking-at org-ts-regexp-both)
  5176. (match-string 0))))
  5177. (todo-state (org-get-todo-state))
  5178. (warntime (get-text-property (point) 'org-appt-warntime))
  5179. (done? (member todo-state org-done-keywords)))
  5180. ;; Possibly skip done tasks.
  5181. (when (and done? org-agenda-skip-timestamp-if-done)
  5182. (throw :skip t))
  5183. ;; S-exp entry doesn't match current day: skip it.
  5184. (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
  5185. (throw :skip nil))
  5186. (when repeat
  5187. (let* ((past
  5188. ;; A repeating time stamp is shown at its base
  5189. ;; date and every repeated date up to TODAY. If
  5190. ;; `org-agenda-prefer-last-repeat' is non-nil,
  5191. ;; however, only the last repeat before today
  5192. ;; (inclusive) is shown.
  5193. (org-agenda--timestamp-to-absolute
  5194. repeat
  5195. (if (or (> current today)
  5196. (eq org-agenda-prefer-last-repeat t)
  5197. (member todo-state org-agenda-prefer-last-repeat))
  5198. today
  5199. current)
  5200. 'past (current-buffer) pos))
  5201. (future
  5202. ;; Display every repeated date past TODAY
  5203. ;; (exclusive) unless
  5204. ;; `org-agenda-show-future-repeats' is nil. If
  5205. ;; this variable is set to `next', only display
  5206. ;; the first repeated date after TODAY
  5207. ;; (exclusive).
  5208. (cond
  5209. ((<= current today) past)
  5210. ((not org-agenda-show-future-repeats) past)
  5211. (t
  5212. (let ((base (if (eq org-agenda-show-future-repeats 'next)
  5213. (1+ today)
  5214. current)))
  5215. (org-agenda--timestamp-to-absolute
  5216. repeat base 'future (current-buffer) pos))))))
  5217. (when (and (/= current past) (/= current future))
  5218. (throw :skip nil))))
  5219. (save-excursion
  5220. (re-search-backward org-outline-regexp-bol nil t)
  5221. ;; Possibly skip time-stamp when a deadline is set.
  5222. (when (and org-agenda-skip-timestamp-if-deadline-is-shown
  5223. (assq (point) deadline-position-alist))
  5224. (throw :skip nil))
  5225. (let* ((category (org-get-category pos))
  5226. (inherited-tags
  5227. (or (eq org-agenda-show-inherited-tags 'always)
  5228. (and (consp org-agenda-show-inherited-tags)
  5229. (memq 'agenda org-agenda-show-inherited-tags))
  5230. (and (eq org-agenda-show-inherited-tags t)
  5231. (or (eq org-agenda-use-tag-inheritance t)
  5232. (memq 'agenda
  5233. org-agenda-use-tag-inheritance)))))
  5234. (tags (org-get-tags nil (not inherited-tags)))
  5235. (level (make-string (org-reduced-level (org-outline-level))
  5236. ?\s))
  5237. (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
  5238. (match-string 1)))
  5239. (inactive? (= (char-after pos) ?\[))
  5240. (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
  5241. (item
  5242. (org-agenda-format-item
  5243. (and inactive? org-agenda-inactive-leader)
  5244. head level category tags time-stamp org-ts-regexp habit?)))
  5245. (org-add-props item props
  5246. 'priority (if habit?
  5247. (org-habit-get-priority (org-habit-parse-todo))
  5248. (org-get-priority item))
  5249. 'org-marker (org-agenda-new-marker pos)
  5250. 'org-hd-marker (org-agenda-new-marker)
  5251. 'date date
  5252. 'level level
  5253. 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
  5254. current)
  5255. 'todo-state todo-state
  5256. 'warntime warntime
  5257. 'type "timestamp")
  5258. (push item timestamp-items))))
  5259. (when org-agenda-skip-additional-timestamps-same-entry
  5260. (outline-next-heading))))
  5261. (nreverse timestamp-items)))
  5262. (defun org-agenda-get-sexps ()
  5263. "Return the sexp information for agenda display."
  5264. (require 'diary-lib)
  5265. (let* ((props (list 'face 'org-agenda-calendar-sexp
  5266. 'mouse-face 'highlight
  5267. 'help-echo
  5268. (format "mouse-2 or RET jump to org file %s"
  5269. (abbreviate-file-name buffer-file-name))))
  5270. (regexp "^&?%%(")
  5271. marker category extra level ee txt tags entry
  5272. result beg b sexp sexp-entry todo-state warntime inherited-tags)
  5273. (goto-char (point-min))
  5274. (while (re-search-forward regexp nil t)
  5275. (catch :skip
  5276. (org-agenda-skip)
  5277. (setq beg (match-beginning 0))
  5278. (goto-char (1- (match-end 0)))
  5279. (setq b (point))
  5280. (forward-sexp 1)
  5281. (setq sexp (buffer-substring b (point)))
  5282. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  5283. (org-trim (match-string 1))
  5284. ""))
  5285. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  5286. (when result
  5287. (setq marker (org-agenda-new-marker beg)
  5288. level (make-string (org-reduced-level (org-outline-level)) ? )
  5289. category (org-get-category beg)
  5290. inherited-tags
  5291. (or (eq org-agenda-show-inherited-tags 'always)
  5292. (and (listp org-agenda-show-inherited-tags)
  5293. (memq 'agenda org-agenda-show-inherited-tags))
  5294. (and (eq org-agenda-show-inherited-tags t)
  5295. (or (eq org-agenda-use-tag-inheritance t)
  5296. (memq 'agenda org-agenda-use-tag-inheritance))))
  5297. tags (org-get-tags nil (not inherited-tags))
  5298. todo-state (org-get-todo-state)
  5299. warntime (get-text-property (point) 'org-appt-warntime)
  5300. extra nil)
  5301. (dolist (r (if (stringp result)
  5302. (list result)
  5303. result)) ;; we expect a list here
  5304. (when (and org-agenda-diary-sexp-prefix
  5305. (string-match org-agenda-diary-sexp-prefix r))
  5306. (setq extra (match-string 0 r)
  5307. r (replace-match "" nil nil r)))
  5308. (if (string-match "\\S-" r)
  5309. (setq txt r)
  5310. (setq txt "SEXP entry returned empty string"))
  5311. (setq txt (org-agenda-format-item extra txt level category tags 'time))
  5312. (org-add-props txt props 'org-marker marker
  5313. 'date date 'todo-state todo-state
  5314. 'level level 'type "sexp" 'warntime warntime)
  5315. (push txt ee)))))
  5316. (nreverse ee)))
  5317. ;; Calendar sanity: define some functions that are independent of
  5318. ;; `calendar-date-style'.
  5319. (defun org-anniversary (year month day &optional mark)
  5320. "Like `diary-anniversary', but with fixed (ISO) order of arguments."
  5321. (with-no-warnings
  5322. (let ((calendar-date-style 'iso))
  5323. (diary-anniversary year month day mark))))
  5324. (defun org-cyclic (N year month day &optional mark)
  5325. "Like `diary-cyclic', but with fixed (ISO) order of arguments."
  5326. (with-no-warnings
  5327. (let ((calendar-date-style 'iso))
  5328. (diary-cyclic N year month day mark))))
  5329. (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
  5330. "Like `diary-block', but with fixed (ISO) order of arguments."
  5331. (with-no-warnings
  5332. (let ((calendar-date-style 'iso))
  5333. (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
  5334. (defun org-date (year month day &optional mark)
  5335. "Like `diary-date', but with fixed (ISO) order of arguments."
  5336. (with-no-warnings
  5337. (let ((calendar-date-style 'iso))
  5338. (diary-date year month day mark))))
  5339. ;; Define the `org-class' function
  5340. (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
  5341. "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
  5342. DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
  5343. SKIP-WEEKS is any number of ISO weeks in the block period for which the
  5344. item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
  5345. `holidays', then any date that is known by the Emacs calendar to be a
  5346. holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
  5347. then those holidays will be skipped."
  5348. (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
  5349. (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
  5350. (d (calendar-absolute-from-gregorian date))
  5351. (h (when skip-weeks (calendar-check-holidays date))))
  5352. (and
  5353. (<= date1 d)
  5354. (<= d date2)
  5355. (= (calendar-day-of-week date) dayname)
  5356. (or (not skip-weeks)
  5357. (progn
  5358. (require 'cal-iso)
  5359. (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
  5360. (not (or (and h (memq 'holidays skip-weeks))
  5361. (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
  5362. entry)))
  5363. (defalias 'org-get-closed 'org-agenda-get-progress)
  5364. (defun org-agenda-get-progress ()
  5365. "Return the logged TODO entries for agenda display."
  5366. (let* ((props (list 'mouse-face 'highlight
  5367. 'org-not-done-regexp org-not-done-regexp
  5368. 'org-todo-regexp org-todo-regexp
  5369. 'org-complex-heading-regexp org-complex-heading-regexp
  5370. 'help-echo
  5371. (format "mouse-2 or RET jump to org file %s"
  5372. (abbreviate-file-name buffer-file-name))))
  5373. (items (if (consp org-agenda-show-log-scoped)
  5374. org-agenda-show-log-scoped
  5375. (if (eq org-agenda-show-log-scoped 'clockcheck)
  5376. '(clock)
  5377. org-agenda-log-mode-items)))
  5378. (parts
  5379. (delq nil
  5380. (list
  5381. (when (memq 'closed items) (concat "\\<" org-closed-string))
  5382. (when (memq 'clock items) (concat "\\<" org-clock-string))
  5383. (when (memq 'state items)
  5384. (format "- +State \"%s\".*?" org-todo-regexp)))))
  5385. (parts-re (if parts (mapconcat 'identity parts "\\|")
  5386. (error "`org-agenda-log-mode-items' is empty")))
  5387. (regexp (concat
  5388. "\\(" parts-re "\\)"
  5389. " *\\["
  5390. (regexp-quote
  5391. (substring
  5392. (format-time-string
  5393. (car org-time-stamp-formats)
  5394. (encode-time ; DATE bound by calendar
  5395. 0 0 0 (nth 1 date) (car date) (nth 2 date)))
  5396. 1 11))))
  5397. (org-agenda-search-headline-for-time nil)
  5398. marker hdmarker priority category level tags closedp type
  5399. statep clockp state ee txt extra timestr rest clocked inherited-tags)
  5400. (goto-char (point-min))
  5401. (while (re-search-forward regexp nil t)
  5402. (catch :skip
  5403. (org-agenda-skip)
  5404. (setq marker (org-agenda-new-marker (match-beginning 0))
  5405. closedp (equal (match-string 1) org-closed-string)
  5406. statep (equal (string-to-char (match-string 1)) ?-)
  5407. clockp (not (or closedp statep))
  5408. state (and statep (match-string 2))
  5409. category (org-get-category (match-beginning 0))
  5410. timestr (buffer-substring (match-beginning 0) (point-at-eol)))
  5411. (when (string-match "\\]" timestr)
  5412. ;; substring should only run to end of time stamp
  5413. (setq rest (substring timestr (match-end 0))
  5414. timestr (substring timestr 0 (match-end 0)))
  5415. (if (and (not closedp) (not statep)
  5416. (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
  5417. rest))
  5418. (progn (setq timestr (concat (substring timestr 0 -1)
  5419. "-" (match-string 1 rest) "]"))
  5420. (setq clocked (match-string 2 rest)))
  5421. (setq clocked "-")))
  5422. (save-excursion
  5423. (setq extra
  5424. (cond
  5425. ((not org-agenda-log-mode-add-notes) nil)
  5426. (statep
  5427. (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
  5428. (match-string 1)))
  5429. (clockp
  5430. (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
  5431. (match-string 1)))))
  5432. (if (not (re-search-backward org-outline-regexp-bol nil t))
  5433. (throw :skip nil)
  5434. (goto-char (match-beginning 0))
  5435. (setq hdmarker (org-agenda-new-marker)
  5436. inherited-tags
  5437. (or (eq org-agenda-show-inherited-tags 'always)
  5438. (and (listp org-agenda-show-inherited-tags)
  5439. (memq 'todo org-agenda-show-inherited-tags))
  5440. (and (eq org-agenda-show-inherited-tags t)
  5441. (or (eq org-agenda-use-tag-inheritance t)
  5442. (memq 'todo org-agenda-use-tag-inheritance))))
  5443. tags (org-get-tags nil (not inherited-tags))
  5444. level (make-string (org-reduced-level (org-outline-level)) ? ))
  5445. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  5446. (setq txt (match-string 1))
  5447. (when extra
  5448. (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
  5449. (setq txt (concat (substring txt 0 (match-beginning 1))
  5450. " - " extra " " (match-string 2 txt)))
  5451. (setq txt (concat txt " - " extra))))
  5452. (setq txt (org-agenda-format-item
  5453. (cond
  5454. (closedp "Closed: ")
  5455. (statep (concat "State: (" state ")"))
  5456. (t (concat "Clocked: (" clocked ")")))
  5457. txt level category tags timestr)))
  5458. (setq type (cond (closedp "closed")
  5459. (statep "state")
  5460. (t "clock")))
  5461. (setq priority 100000)
  5462. (org-add-props txt props
  5463. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
  5464. 'priority priority 'level level
  5465. 'type type 'date date
  5466. 'undone-face 'org-warning 'done-face 'org-agenda-done)
  5467. (push txt ee))
  5468. (goto-char (point-at-eol))))
  5469. (nreverse ee)))
  5470. (defun org-agenda-show-clocking-issues ()
  5471. "Add overlays, showing issues with clocking.
  5472. See also the user option `org-agenda-clock-consistency-checks'."
  5473. (interactive)
  5474. (let* ((pl org-agenda-clock-consistency-checks)
  5475. (re (concat "^[ \t]*"
  5476. org-clock-string
  5477. "[ \t]+"
  5478. "\\(\\[.*?\\]\\)" ; group 1 is first stamp
  5479. "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
  5480. (tlstart 0.)
  5481. (tlend 0.)
  5482. (maxtime (org-duration-to-minutes
  5483. (or (plist-get pl :max-duration) "24:00")))
  5484. (mintime (org-duration-to-minutes
  5485. (or (plist-get pl :min-duration) 0)))
  5486. (maxgap (org-duration-to-minutes
  5487. ;; default 30:00 means never complain
  5488. (or (plist-get pl :max-gap) "30:00")))
  5489. (gapok (mapcar #'org-duration-to-minutes
  5490. (plist-get pl :gap-ok-around)))
  5491. (def-face (or (plist-get pl :default-face)
  5492. '((:background "DarkRed") (:foreground "white"))))
  5493. issue face m te ts dt ov)
  5494. (goto-char (point-min))
  5495. (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
  5496. (setq issue nil face def-face)
  5497. (catch 'next
  5498. (setq m (org-get-at-bol 'org-marker)
  5499. te nil ts nil)
  5500. (unless (and m (markerp m))
  5501. (setq issue "No valid clock line") (throw 'next t))
  5502. (org-with-point-at m
  5503. (save-excursion
  5504. (goto-char (point-at-bol))
  5505. (unless (looking-at re)
  5506. (error "No valid Clock line")
  5507. (throw 'next t))
  5508. (unless (match-end 3)
  5509. (setq issue
  5510. (format
  5511. "No end time: (%s)"
  5512. (org-duration-from-minutes
  5513. (floor
  5514. (- (float-time (org-current-time))
  5515. (float-time (org-time-string-to-time (match-string 1))))
  5516. 60)))
  5517. face (or (plist-get pl :no-end-time-face) face))
  5518. (throw 'next t))
  5519. (setq ts (match-string 1)
  5520. te (match-string 3)
  5521. ts (float-time (org-time-string-to-time ts))
  5522. te (float-time (org-time-string-to-time te))
  5523. dt (- te ts))))
  5524. (cond
  5525. ((> dt (* 60 maxtime))
  5526. ;; a very long clocking chunk
  5527. (setq issue (format "Clocking interval is very long: %s"
  5528. (org-duration-from-minutes (floor dt 60)))
  5529. face (or (plist-get pl :long-face) face)))
  5530. ((< dt (* 60 mintime))
  5531. ;; a very short clocking chunk
  5532. (setq issue (format "Clocking interval is very short: %s"
  5533. (org-duration-from-minutes (floor dt 60)))
  5534. face (or (plist-get pl :short-face) face)))
  5535. ((and (> tlend 0) (< ts tlend))
  5536. ;; Two clock entries are overlapping
  5537. (setq issue (format "Clocking overlap: %d minutes"
  5538. (/ (- tlend ts) 60))
  5539. face (or (plist-get pl :overlap-face) face)))
  5540. ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
  5541. ;; There is a gap, lets see if we need to report it
  5542. (unless (org-agenda-check-clock-gap tlend ts gapok)
  5543. (setq issue (format "Clocking gap: %d minutes"
  5544. (/ (- ts tlend) 60))
  5545. face (or (plist-get pl :gap-face) face))))
  5546. (t nil)))
  5547. (setq tlend (or te tlend) tlstart (or ts tlstart))
  5548. (when issue
  5549. ;; OK, there was some issue, add an overlay to show the issue
  5550. (setq ov (make-overlay (point-at-bol) (point-at-eol)))
  5551. (overlay-put ov 'before-string
  5552. (concat
  5553. (org-add-props
  5554. (format "%-43s" (concat " " issue))
  5555. nil
  5556. 'face face)
  5557. "\n"))
  5558. (overlay-put ov 'evaporate t)))))
  5559. (defun org-agenda-check-clock-gap (t1 t2 ok-list)
  5560. "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
  5561. (catch 'exit
  5562. (unless ok-list
  5563. ;; there are no OK times for gaps...
  5564. (throw 'exit nil))
  5565. (when (> (- (/ t2 36000) (/ t1 36000)) 24)
  5566. ;; This is more than 24 hours, so it is OK.
  5567. ;; because we have at least one OK time, that must be in the
  5568. ;; 24 hour interval.
  5569. (throw 'exit t))
  5570. ;; We have a shorter gap.
  5571. ;; Now we have to get the minute of the day when these times are
  5572. (let* ((t1dec (org-decode-time t1))
  5573. (t2dec (org-decode-time t2))
  5574. ;; compute the minute on the day
  5575. (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
  5576. (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
  5577. (when (< min2 min1)
  5578. ;; if min2 is smaller than min1, this means it is on the next day.
  5579. ;; Wrap it to after midnight.
  5580. (setq min2 (+ min2 1440)))
  5581. ;; Now check if any of the OK times is in the gap
  5582. (mapc (lambda (x)
  5583. ;; Wrap the time to after midnight if necessary
  5584. (when (< x min1) (setq x (+ x 1440)))
  5585. ;; Check if in interval
  5586. (and (<= min1 x) (>= min2 x) (throw 'exit t)))
  5587. ok-list)
  5588. ;; Nope, this gap is not OK
  5589. nil)))
  5590. (defun org-agenda-get-deadlines (&optional with-hour)
  5591. "Return the deadline information for agenda display.
  5592. When WITH-HOUR is non-nil, only return deadlines with an hour
  5593. specification like [h]h:mm."
  5594. (let* ((props (list 'mouse-face 'highlight
  5595. 'org-not-done-regexp org-not-done-regexp
  5596. 'org-todo-regexp org-todo-regexp
  5597. 'org-complex-heading-regexp org-complex-heading-regexp
  5598. 'help-echo
  5599. (format "mouse-2 or RET jump to org file %s"
  5600. (abbreviate-file-name buffer-file-name))))
  5601. (regexp (if with-hour
  5602. org-deadline-time-hour-regexp
  5603. org-deadline-time-regexp))
  5604. (today (org-today))
  5605. (today? (org-agenda-today-p date)) ; DATE bound by calendar.
  5606. (current (calendar-absolute-from-gregorian date))
  5607. deadline-items)
  5608. (goto-char (point-min))
  5609. (while (re-search-forward regexp nil t)
  5610. (catch :skip
  5611. (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
  5612. (org-agenda-skip)
  5613. (let* ((s (match-string 1))
  5614. (pos (1- (match-beginning 1)))
  5615. (todo-state (save-match-data (org-get-todo-state)))
  5616. (done? (member todo-state org-done-keywords))
  5617. (sexp? (string-prefix-p "%%" s))
  5618. ;; DEADLINE is the deadline date for the entry. It is
  5619. ;; either the base date or the last repeat, according
  5620. ;; to `org-agenda-prefer-last-repeat'.
  5621. (deadline
  5622. (cond
  5623. (sexp? (org-agenda--timestamp-to-absolute s current))
  5624. ((or (eq org-agenda-prefer-last-repeat t)
  5625. (member todo-state org-agenda-prefer-last-repeat))
  5626. (org-agenda--timestamp-to-absolute
  5627. s today 'past (current-buffer) pos))
  5628. (t (org-agenda--timestamp-to-absolute s))))
  5629. ;; REPEAT is the future repeat closest from CURRENT,
  5630. ;; according to `org-agenda-show-future-repeats'. If
  5631. ;; the latter is nil, or if the time stamp has no
  5632. ;; repeat part, default to DEADLINE.
  5633. (repeat
  5634. (cond
  5635. (sexp? deadline)
  5636. ((<= current today) deadline)
  5637. ((not org-agenda-show-future-repeats) deadline)
  5638. (t
  5639. (let ((base (if (eq org-agenda-show-future-repeats 'next)
  5640. (1+ today)
  5641. current)))
  5642. (org-agenda--timestamp-to-absolute
  5643. s base 'future (current-buffer) pos)))))
  5644. (diff (- deadline current))
  5645. (suppress-prewarning
  5646. (let ((scheduled
  5647. (and org-agenda-skip-deadline-prewarning-if-scheduled
  5648. (org-entry-get nil "SCHEDULED"))))
  5649. (cond
  5650. ((not scheduled) nil)
  5651. ;; The current item has a scheduled date, so
  5652. ;; evaluate its prewarning lead time.
  5653. ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
  5654. ;; Use global prewarning-restart lead time.
  5655. org-agenda-skip-deadline-prewarning-if-scheduled)
  5656. ((eq org-agenda-skip-deadline-prewarning-if-scheduled
  5657. 'pre-scheduled)
  5658. ;; Set pre-warning to no earlier than SCHEDULED.
  5659. (min (- deadline
  5660. (org-agenda--timestamp-to-absolute scheduled))
  5661. org-deadline-warning-days))
  5662. ;; Set pre-warning to deadline.
  5663. (t 0))))
  5664. (wdays (or suppress-prewarning (org-get-wdays s))))
  5665. (cond
  5666. ;; Only display deadlines at their base date, at future
  5667. ;; repeat occurrences or in today agenda.
  5668. ((= current deadline) nil)
  5669. ((= current repeat) nil)
  5670. ((not today?) (throw :skip nil))
  5671. ;; Upcoming deadline: display within warning period WDAYS.
  5672. ((> deadline current) (when (> diff wdays) (throw :skip nil)))
  5673. ;; Overdue deadline: warn about it for
  5674. ;; `org-deadline-past-days' duration.
  5675. (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
  5676. ;; Possibly skip done tasks.
  5677. (when (and done?
  5678. (or org-agenda-skip-deadline-if-done
  5679. (/= deadline current)))
  5680. (throw :skip nil))
  5681. (save-excursion
  5682. (re-search-backward "^\\*+[ \t]+" nil t)
  5683. (goto-char (match-end 0))
  5684. (let* ((category (org-get-category))
  5685. (level (make-string (org-reduced-level (org-outline-level))
  5686. ?\s))
  5687. (head (buffer-substring (point) (line-end-position)))
  5688. (inherited-tags
  5689. (or (eq org-agenda-show-inherited-tags 'always)
  5690. (and (listp org-agenda-show-inherited-tags)
  5691. (memq 'agenda org-agenda-show-inherited-tags))
  5692. (and (eq org-agenda-show-inherited-tags t)
  5693. (or (eq org-agenda-use-tag-inheritance t)
  5694. (memq 'agenda
  5695. org-agenda-use-tag-inheritance)))))
  5696. (tags (org-get-tags nil (not inherited-tags)))
  5697. (time
  5698. (cond
  5699. ;; No time of day designation if it is only
  5700. ;; a reminder.
  5701. ((and (/= current deadline) (/= current repeat)) nil)
  5702. ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  5703. (concat (substring s (match-beginning 1)) " "))
  5704. (t 'time)))
  5705. (item
  5706. (org-agenda-format-item
  5707. ;; Insert appropriate suffixes before deadlines.
  5708. ;; Those only apply to today agenda.
  5709. (pcase-let ((`(,now ,future ,past)
  5710. org-agenda-deadline-leaders))
  5711. (cond
  5712. ((and today? (< deadline today)) (format past (- diff)))
  5713. ((and today? (> deadline today)) (format future diff))
  5714. (t now)))
  5715. head level category tags time))
  5716. (face (org-agenda-deadline-face
  5717. (- 1 (/ (float diff) (max wdays 1)))))
  5718. (upcoming? (and today? (> deadline today)))
  5719. (warntime (get-text-property (point) 'org-appt-warntime)))
  5720. (org-add-props item props
  5721. 'org-marker (org-agenda-new-marker pos)
  5722. 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
  5723. 'warntime warntime
  5724. 'level level
  5725. 'ts-date deadline
  5726. 'priority
  5727. ;; Adjust priority to today reminders about deadlines.
  5728. ;; Overdue deadlines get the highest priority
  5729. ;; increase, then imminent deadlines and eventually
  5730. ;; more distant deadlines.
  5731. (let ((adjust (if today? (- diff) 0)))
  5732. (+ adjust (org-get-priority item)))
  5733. 'todo-state todo-state
  5734. 'type (if upcoming? "upcoming-deadline" "deadline")
  5735. 'date (if upcoming? date deadline)
  5736. 'face (if done? 'org-agenda-done face)
  5737. 'undone-face face
  5738. 'done-face 'org-agenda-done)
  5739. (push item deadline-items))))))
  5740. (nreverse deadline-items)))
  5741. (defun org-agenda-deadline-face (fraction)
  5742. "Return the face to displaying a deadline item.
  5743. FRACTION is what fraction of the head-warning time has passed."
  5744. (assoc-default fraction org-agenda-deadline-faces #'<=))
  5745. (defun org-agenda-get-scheduled (&optional deadlines with-hour)
  5746. "Return the scheduled information for agenda display.
  5747. Optional argument DEADLINES is a list of deadline items to be
  5748. displayed in agenda view. When WITH-HOUR is non-nil, only return
  5749. scheduled items with an hour specification like [h]h:mm."
  5750. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  5751. 'org-todo-regexp org-todo-regexp
  5752. 'org-complex-heading-regexp org-complex-heading-regexp
  5753. 'done-face 'org-agenda-done
  5754. 'mouse-face 'highlight
  5755. 'help-echo
  5756. (format "mouse-2 or RET jump to Org file %s"
  5757. (abbreviate-file-name buffer-file-name))))
  5758. (regexp (if with-hour
  5759. org-scheduled-time-hour-regexp
  5760. org-scheduled-time-regexp))
  5761. (today (org-today))
  5762. (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
  5763. (current (calendar-absolute-from-gregorian date))
  5764. (deadline-pos
  5765. (mapcar (lambda (d)
  5766. (let ((m (get-text-property 0 'org-hd-marker d)))
  5767. (and m (marker-position m))))
  5768. deadlines))
  5769. scheduled-items)
  5770. (goto-char (point-min))
  5771. (while (re-search-forward regexp nil t)
  5772. (catch :skip
  5773. (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
  5774. (org-agenda-skip)
  5775. (let* ((s (match-string 1))
  5776. (pos (1- (match-beginning 1)))
  5777. (todo-state (save-match-data (org-get-todo-state)))
  5778. (donep (member todo-state org-done-keywords))
  5779. (sexp? (string-prefix-p "%%" s))
  5780. ;; SCHEDULE is the scheduled date for the entry. It is
  5781. ;; either the bare date or the last repeat, according
  5782. ;; to `org-agenda-prefer-last-repeat'.
  5783. (schedule
  5784. (cond
  5785. (sexp? (org-agenda--timestamp-to-absolute s current))
  5786. ((or (eq org-agenda-prefer-last-repeat t)
  5787. (member todo-state org-agenda-prefer-last-repeat))
  5788. (org-agenda--timestamp-to-absolute
  5789. s today 'past (current-buffer) pos))
  5790. (t (org-agenda--timestamp-to-absolute s))))
  5791. ;; REPEAT is the future repeat closest from CURRENT,
  5792. ;; according to `org-agenda-show-future-repeats'. If
  5793. ;; the latter is nil, or if the time stamp has no
  5794. ;; repeat part, default to SCHEDULE.
  5795. (repeat
  5796. (cond
  5797. (sexp? schedule)
  5798. ((<= current today) schedule)
  5799. ((not org-agenda-show-future-repeats) schedule)
  5800. (t
  5801. (let ((base (if (eq org-agenda-show-future-repeats 'next)
  5802. (1+ today)
  5803. current)))
  5804. (org-agenda--timestamp-to-absolute
  5805. s base 'future (current-buffer) pos)))))
  5806. (diff (- current schedule))
  5807. (warntime (get-text-property (point) 'org-appt-warntime))
  5808. (pastschedp (< schedule today))
  5809. (futureschedp (> schedule today))
  5810. (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
  5811. (suppress-delay
  5812. (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
  5813. (org-entry-get nil "DEADLINE"))))
  5814. (cond
  5815. ((not deadline) nil)
  5816. ;; The current item has a deadline date, so
  5817. ;; evaluate its delay time.
  5818. ((integerp org-agenda-skip-scheduled-delay-if-deadline)
  5819. ;; Use global delay time.
  5820. (- org-agenda-skip-scheduled-delay-if-deadline))
  5821. ((eq org-agenda-skip-scheduled-delay-if-deadline
  5822. 'post-deadline)
  5823. ;; Set delay to no later than DEADLINE.
  5824. (min (- schedule
  5825. (org-agenda--timestamp-to-absolute deadline))
  5826. org-scheduled-delay-days))
  5827. (t 0))))
  5828. (ddays
  5829. (cond
  5830. ;; Nullify delay when a repeater triggered already
  5831. ;; and the delay is of the form --Xd.
  5832. ((and (string-match-p "--[0-9]+[hdwmy]" s)
  5833. (> schedule (org-agenda--timestamp-to-absolute s)))
  5834. 0)
  5835. (suppress-delay
  5836. (let ((org-scheduled-delay-days suppress-delay))
  5837. (org-get-wdays s t t)))
  5838. (t (org-get-wdays s t)))))
  5839. ;; Display scheduled items at base date (SCHEDULE), today if
  5840. ;; scheduled before the current date, and at any repeat past
  5841. ;; today. However, skip delayed items and items that have
  5842. ;; been displayed for more than `org-scheduled-past-days'.
  5843. (unless (and todayp
  5844. habitp
  5845. (bound-and-true-p org-habit-show-all-today))
  5846. (when (or (and (> ddays 0) (< diff ddays))
  5847. (> diff (or (and habitp org-habit-scheduled-past-days)
  5848. org-scheduled-past-days))
  5849. (> schedule current)
  5850. (and (/= current schedule)
  5851. (/= current today)
  5852. (/= current repeat)))
  5853. (throw :skip nil)))
  5854. ;; Possibly skip done tasks.
  5855. (when (and donep
  5856. (or org-agenda-skip-scheduled-if-done
  5857. (/= schedule current)))
  5858. (throw :skip nil))
  5859. ;; Skip entry if it already appears as a deadline, per
  5860. ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
  5861. ;; doesn't apply to habits.
  5862. (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
  5863. ((guard
  5864. (or (not (memq (line-beginning-position 0) deadline-pos))
  5865. habitp))
  5866. nil)
  5867. (`repeated-after-deadline
  5868. (let ((deadline (time-to-days
  5869. (org-get-deadline-time (point)))))
  5870. (and (<= schedule deadline) (> current deadline))))
  5871. (`not-today pastschedp)
  5872. (`t t)
  5873. (_ nil))
  5874. (throw :skip nil))
  5875. ;; Skip habits if `org-habit-show-habits' is nil, or if we
  5876. ;; only show them for today. Also skip done habits.
  5877. (when (and habitp
  5878. (or donep
  5879. (not (bound-and-true-p org-habit-show-habits))
  5880. (and (not todayp)
  5881. (bound-and-true-p
  5882. org-habit-show-habits-only-for-today))))
  5883. (throw :skip nil))
  5884. (save-excursion
  5885. (re-search-backward "^\\*+[ \t]+" nil t)
  5886. (goto-char (match-end 0))
  5887. (let* ((category (org-get-category))
  5888. (inherited-tags
  5889. (or (eq org-agenda-show-inherited-tags 'always)
  5890. (and (listp org-agenda-show-inherited-tags)
  5891. (memq 'agenda org-agenda-show-inherited-tags))
  5892. (and (eq org-agenda-show-inherited-tags t)
  5893. (or (eq org-agenda-use-tag-inheritance t)
  5894. (memq 'agenda
  5895. org-agenda-use-tag-inheritance)))))
  5896. (tags (org-get-tags nil (not inherited-tags)))
  5897. (level (make-string (org-reduced-level (org-outline-level))
  5898. ?\s))
  5899. (head (buffer-substring (point) (line-end-position)))
  5900. (time
  5901. (cond
  5902. ;; No time of day designation if it is only a
  5903. ;; reminder, except for habits, which always show
  5904. ;; the time of day. Habits are an exception
  5905. ;; because if there is a time of day, that is
  5906. ;; interpreted to mean they should usually happen
  5907. ;; then, even if doing the habit was missed.
  5908. ((and
  5909. (not habitp)
  5910. (/= current schedule)
  5911. (/= current repeat))
  5912. nil)
  5913. ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  5914. (concat (substring s (match-beginning 1)) " "))
  5915. (t 'time)))
  5916. (item
  5917. (org-agenda-format-item
  5918. (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
  5919. ;; Show a reminder of a past scheduled today.
  5920. (if (and todayp pastschedp)
  5921. (format past diff)
  5922. first))
  5923. head level category tags time nil habitp))
  5924. (face (cond ((and (not habitp) pastschedp)
  5925. 'org-scheduled-previously)
  5926. ((and habitp futureschedp)
  5927. 'org-agenda-done)
  5928. (todayp 'org-scheduled-today)
  5929. (t 'org-scheduled)))
  5930. (habitp (and habitp (org-habit-parse-todo))))
  5931. (org-add-props item props
  5932. 'undone-face face
  5933. 'face (if donep 'org-agenda-done face)
  5934. 'org-marker (org-agenda-new-marker pos)
  5935. 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
  5936. 'type (if pastschedp "past-scheduled" "scheduled")
  5937. 'date (if pastschedp schedule date)
  5938. 'ts-date schedule
  5939. 'warntime warntime
  5940. 'level level
  5941. 'priority (if habitp (org-habit-get-priority habitp)
  5942. (+ 99 diff (org-get-priority item)))
  5943. 'org-habit-p habitp
  5944. 'todo-state todo-state)
  5945. (push item scheduled-items))))))
  5946. (nreverse scheduled-items)))
  5947. (defun org-agenda-get-blocks ()
  5948. "Return the date-range information for agenda display."
  5949. (let* ((props (list 'face nil
  5950. 'org-not-done-regexp org-not-done-regexp
  5951. 'org-todo-regexp org-todo-regexp
  5952. 'org-complex-heading-regexp org-complex-heading-regexp
  5953. 'mouse-face 'highlight
  5954. 'help-echo
  5955. (format "mouse-2 or RET jump to org file %s"
  5956. (abbreviate-file-name buffer-file-name))))
  5957. (regexp org-tr-regexp)
  5958. (d0 (calendar-absolute-from-gregorian date))
  5959. marker hdmarker ee txt d1 d2 s1 s2 category
  5960. level todo-state tags pos head donep inherited-tags)
  5961. (goto-char (point-min))
  5962. (while (re-search-forward regexp nil t)
  5963. (catch :skip
  5964. (org-agenda-skip)
  5965. (setq pos (point))
  5966. (let ((start-time (match-string 1))
  5967. (end-time (match-string 2)))
  5968. (setq s1 (match-string 1)
  5969. s2 (match-string 2)
  5970. d1 (time-to-days
  5971. (condition-case err
  5972. (org-time-string-to-time s1)
  5973. (error
  5974. (error
  5975. "Bad timestamp %S at %d in buffer %S\nError was: %s"
  5976. s1
  5977. pos
  5978. (current-buffer)
  5979. (error-message-string err)))))
  5980. d2 (time-to-days
  5981. (condition-case err
  5982. (org-time-string-to-time s2)
  5983. (error
  5984. (error
  5985. "Bad timestamp %S at %d in buffer %S\nError was: %s"
  5986. s2
  5987. pos
  5988. (current-buffer)
  5989. (error-message-string err))))))
  5990. (when (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  5991. ;; Only allow days between the limits, because the normal
  5992. ;; date stamps will catch the limits.
  5993. (save-excursion
  5994. (setq todo-state (org-get-todo-state))
  5995. (setq donep (member todo-state org-done-keywords))
  5996. (when (and donep org-agenda-skip-timestamp-if-done)
  5997. (throw :skip t))
  5998. (setq marker (org-agenda-new-marker (point))
  5999. category (org-get-category))
  6000. (if (not (re-search-backward org-outline-regexp-bol nil t))
  6001. (throw :skip nil)
  6002. (goto-char (match-beginning 0))
  6003. (setq hdmarker (org-agenda-new-marker (point))
  6004. inherited-tags
  6005. (or (eq org-agenda-show-inherited-tags 'always)
  6006. (and (listp org-agenda-show-inherited-tags)
  6007. (memq 'agenda org-agenda-show-inherited-tags))
  6008. (and (eq org-agenda-show-inherited-tags t)
  6009. (or (eq org-agenda-use-tag-inheritance t)
  6010. (memq 'agenda org-agenda-use-tag-inheritance))))
  6011. tags (org-get-tags nil (not inherited-tags)))
  6012. (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
  6013. (looking-at "\\*+[ \t]+\\(.*\\)")
  6014. (setq head (match-string 1))
  6015. (let ((remove-re
  6016. (if org-agenda-remove-timeranges-from-blocks
  6017. (concat
  6018. "<" (regexp-quote s1) ".*?>"
  6019. "--"
  6020. "<" (regexp-quote s2) ".*?>")
  6021. nil)))
  6022. (setq txt (org-agenda-format-item
  6023. (format
  6024. (nth (if (= d1 d2) 0 1)
  6025. org-agenda-timerange-leaders)
  6026. (1+ (- d0 d1)) (1+ (- d2 d1)))
  6027. head level category tags
  6028. (cond ((and (= d1 d0) (= d2 d0))
  6029. (concat "<" start-time ">--<" end-time ">"))
  6030. ((= d1 d0)
  6031. (concat "<" start-time ">"))
  6032. ((= d2 d0)
  6033. (concat "<" end-time ">")))
  6034. remove-re))))
  6035. (org-add-props txt props
  6036. 'org-marker marker 'org-hd-marker hdmarker
  6037. 'type "block" 'date date
  6038. 'level level
  6039. 'todo-state todo-state
  6040. 'priority (org-get-priority txt))
  6041. (push txt ee))))
  6042. (goto-char pos)))
  6043. ;; Sort the entries by expiration date.
  6044. (nreverse ee)))
  6045. ;;; Agenda presentation and sorting
  6046. (defvar org-prefix-has-time nil
  6047. "A flag, set by `org-compile-prefix-format'.
  6048. The flag is set if the currently compiled format contains a `%t'.")
  6049. (defvar org-prefix-has-tag nil
  6050. "A flag, set by `org-compile-prefix-format'.
  6051. The flag is set if the currently compiled format contains a `%T'.")
  6052. (defvar org-prefix-has-effort nil
  6053. "A flag, set by `org-compile-prefix-format'.
  6054. The flag is set if the currently compiled format contains a `%e'.")
  6055. (defvar org-prefix-has-breadcrumbs nil
  6056. "A flag, set by `org-compile-prefix-format'.
  6057. The flag is set if the currently compiled format contains a `%b'.")
  6058. (defvar org-prefix-category-length nil
  6059. "Used by `org-compile-prefix-format' to remember the category field width.")
  6060. (defvar org-prefix-category-max-length nil
  6061. "Used by `org-compile-prefix-format' to remember the category field width.")
  6062. (defun org-agenda-get-category-icon (category)
  6063. "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
  6064. (cl-dolist (entry org-agenda-category-icon-alist)
  6065. (when (string-match-p (car entry) category)
  6066. (if (listp (cadr entry))
  6067. (cl-return (cadr entry))
  6068. (cl-return (apply #'create-image (cdr entry)))))))
  6069. (defun org-agenda-format-item (extra txt &optional level category tags dotime
  6070. remove-re habitp)
  6071. "Format TXT to be inserted into the agenda buffer.
  6072. In particular, add the prefix and corresponding text properties.
  6073. EXTRA must be a string to replace the `%s' specifier in the prefix format.
  6074. LEVEL may be a string to replace the `%l' specifier.
  6075. CATEGORY (a string, a symbol or nil) may be used to overrule the default
  6076. category taken from local variable or file name. It will replace the `%c'
  6077. specifier in the format.
  6078. DOTIME, when non-nil, indicates that a time-of-day should be extracted from
  6079. TXT for sorting of this entry, and for the `%t' specifier in the format.
  6080. When DOTIME is a string, this string is searched for a time before TXT is.
  6081. TAGS can be the tags of the headline.
  6082. Any match of REMOVE-RE will be removed from TXT."
  6083. ;; We keep the org-prefix-* variable values along with a compiled
  6084. ;; formatter, so that multiple agendas existing at the same time do
  6085. ;; not step on each other toes.
  6086. ;;
  6087. ;; It was inconvenient to make these variables buffer local in
  6088. ;; Agenda buffers, because this function expects to be called with
  6089. ;; the buffer where item comes from being current, and not agenda
  6090. ;; buffer
  6091. (let* ((bindings (car org-prefix-format-compiled))
  6092. (formatter (cadr org-prefix-format-compiled)))
  6093. (cl-loop for (var value) in bindings
  6094. do (set var value))
  6095. (save-match-data
  6096. ;; Diary entries sometimes have extra whitespace at the beginning
  6097. (setq txt (org-trim txt))
  6098. ;; Fix the tags part in txt
  6099. (setq txt (org-agenda-fix-displayed-tags
  6100. txt tags
  6101. org-agenda-show-inherited-tags
  6102. org-agenda-hide-tags-regexp))
  6103. (let* ((category (or category
  6104. (if buffer-file-name
  6105. (file-name-sans-extension
  6106. (file-name-nondirectory buffer-file-name))
  6107. "")))
  6108. (category-icon (org-agenda-get-category-icon category))
  6109. (category-icon (if category-icon
  6110. (propertize " " 'display category-icon)
  6111. ""))
  6112. (effort (and (not (string= txt ""))
  6113. (get-text-property 1 'effort txt)))
  6114. ;; time, tag, effort are needed for the eval of the prefix format
  6115. (tag (if tags (nth (1- (length tags)) tags) ""))
  6116. (time-grid-trailing-characters (nth 2 org-agenda-time-grid))
  6117. time
  6118. (ts (when dotime (concat
  6119. (if (stringp dotime) dotime "")
  6120. (and org-agenda-search-headline-for-time txt))))
  6121. (time-of-day (and dotime (org-get-time-of-day ts)))
  6122. stamp plain s0 s1 s2 rtn srp l
  6123. duration breadcrumbs)
  6124. (and (derived-mode-p 'org-mode) buffer-file-name
  6125. (add-to-list 'org-agenda-contributing-files buffer-file-name))
  6126. (when (and dotime time-of-day)
  6127. ;; Extract starting and ending time and move them to prefix
  6128. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  6129. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  6130. (setq s0 (match-string 0 ts)
  6131. srp (and stamp (match-end 3))
  6132. s1 (match-string (if plain 1 2) ts)
  6133. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  6134. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  6135. ;; them, we might want to remove them there to avoid duplication.
  6136. ;; The user can turn this off with a variable.
  6137. (when (and org-prefix-has-time
  6138. org-agenda-remove-times-when-in-prefix (or stamp plain)
  6139. (string-match (concat (regexp-quote s0) " *") txt)
  6140. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  6141. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  6142. (= (match-beginning 0) 0)
  6143. t))
  6144. (setq txt (replace-match "" nil nil txt))))
  6145. ;; Normalize the time(s) to 24 hour
  6146. (when s1 (setq s1 (org-get-time-of-day s1 'string t)))
  6147. (when s2 (setq s2 (org-get-time-of-day s2 'string t)))
  6148. ;; Try to set s2 if s1 and
  6149. ;; `org-agenda-default-appointment-duration' are set
  6150. (when (and s1 (not s2) org-agenda-default-appointment-duration)
  6151. (setq s2
  6152. (org-duration-from-minutes
  6153. (+ (org-duration-to-minutes s1 t)
  6154. org-agenda-default-appointment-duration)
  6155. nil t)))
  6156. ;; Compute the duration
  6157. (when s2
  6158. (setq duration (- (org-duration-to-minutes s2)
  6159. (org-duration-to-minutes s1)))))
  6160. (when (string-match org-tag-group-re txt)
  6161. ;; Tags are in the string
  6162. (if (or (eq org-agenda-remove-tags t)
  6163. (and org-agenda-remove-tags
  6164. org-prefix-has-tag))
  6165. (setq txt (replace-match "" t t txt))
  6166. (setq txt (replace-match
  6167. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  6168. (match-string 1 txt))
  6169. t t txt))))
  6170. (when remove-re
  6171. (while (string-match remove-re txt)
  6172. (setq txt (replace-match "" t t txt))))
  6173. ;; Set org-heading property on `txt' to mark the start of the
  6174. ;; heading.
  6175. (add-text-properties 0 (length txt) '(org-heading t) txt)
  6176. ;; Prepare the variables needed in the eval of the compiled format
  6177. (when org-prefix-has-breadcrumbs
  6178. (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
  6179. (let ((s (org-format-outline-path (org-get-outline-path)
  6180. (1- (frame-width))
  6181. nil org-agenda-breadcrumbs-separator)))
  6182. (if (eq "" s) "" (concat s org-agenda-breadcrumbs-separator))))))
  6183. (setq time (cond (s2 (concat
  6184. (org-agenda-time-of-day-to-ampm-maybe s1)
  6185. "-" (org-agenda-time-of-day-to-ampm-maybe s2)
  6186. (when org-agenda-timegrid-use-ampm " ")))
  6187. (s1 (concat
  6188. (org-agenda-time-of-day-to-ampm-maybe s1)
  6189. (if org-agenda-timegrid-use-ampm
  6190. (concat time-grid-trailing-characters " ")
  6191. time-grid-trailing-characters)))
  6192. (t ""))
  6193. extra (or (and (not habitp) extra) "")
  6194. category (if (symbolp category) (symbol-name category) category)
  6195. level (or level ""))
  6196. (if (string-match org-link-bracket-re category)
  6197. (progn
  6198. (setq l (string-width (or (match-string 2) (match-string 1))))
  6199. (when (< l (or org-prefix-category-length 0))
  6200. (setq category (copy-sequence category))
  6201. (org-add-props category nil
  6202. 'extra-space (make-string
  6203. (- org-prefix-category-length l 1) ?\ ))))
  6204. (when (and org-prefix-category-max-length
  6205. (>= (length category) org-prefix-category-max-length))
  6206. (setq category (substring category 0 (1- org-prefix-category-max-length)))))
  6207. ;; Evaluate the compiled format
  6208. (setq rtn (concat (eval formatter) txt))
  6209. ;; And finally add the text properties
  6210. (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
  6211. (org-add-props rtn nil
  6212. 'org-category category
  6213. 'tags (mapcar 'org-downcase-keep-props tags)
  6214. 'org-highest-priority org-highest-priority
  6215. 'org-lowest-priority org-lowest-priority
  6216. 'time-of-day time-of-day
  6217. 'duration duration
  6218. 'breadcrumbs breadcrumbs
  6219. 'txt txt
  6220. 'level level
  6221. 'time time
  6222. 'extra extra
  6223. 'format org-prefix-format-compiled
  6224. 'dotime dotime)))))
  6225. (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
  6226. "Remove tags string from TXT, and add a modified list of tags.
  6227. The modified list may contain inherited tags, and tags matched by
  6228. `org-agenda-hide-tags-regexp' will be removed."
  6229. (when (or add-inherited hide-re)
  6230. (when (string-match org-tag-group-re txt)
  6231. (setq txt (substring txt 0 (match-beginning 0))))
  6232. (setq tags
  6233. (delq nil
  6234. (mapcar (lambda (tg)
  6235. (if (or (and hide-re (string-match hide-re tg))
  6236. (and (not add-inherited)
  6237. (get-text-property 0 'inherited tg)))
  6238. nil
  6239. tg))
  6240. tags)))
  6241. (when tags
  6242. (let ((have-i (get-text-property 0 'inherited (car tags)))
  6243. i)
  6244. (setq txt (concat txt " :"
  6245. (mapconcat
  6246. (lambda (x)
  6247. (setq i (get-text-property 0 'inherited x))
  6248. (if (and have-i (not i))
  6249. (progn
  6250. (setq have-i nil)
  6251. (concat ":" x))
  6252. x))
  6253. tags ":")
  6254. (if have-i "::" ":"))))))
  6255. txt)
  6256. (defun org-downcase-keep-props (s)
  6257. (let ((props (text-properties-at 0 s)))
  6258. (setq s (downcase s))
  6259. (add-text-properties 0 (length s) props s)
  6260. s))
  6261. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  6262. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  6263. "Add a time-grid for agenda items which need it.
  6264. LIST is the list of agenda items formatted by `org-agenda-list'.
  6265. NDAYS is the span of the current agenda view.
  6266. TODAYP is t when the current agenda view is on today."
  6267. (catch 'exit
  6268. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  6269. ((and todayp (member 'today (car org-agenda-time-grid))))
  6270. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  6271. ((member 'weekly (car org-agenda-time-grid)))
  6272. (t (throw 'exit list)))
  6273. (let* ((have (delq nil (mapcar
  6274. (lambda (x) (get-text-property 1 'time-of-day x))
  6275. list)))
  6276. (string (nth 3 org-agenda-time-grid))
  6277. (gridtimes (nth 1 org-agenda-time-grid))
  6278. (req (car org-agenda-time-grid))
  6279. (remove (member 'remove-match req))
  6280. new time)
  6281. (when (and (member 'require-timed req) (not have))
  6282. ;; don't show empty grid
  6283. (throw 'exit list))
  6284. (while (setq time (pop gridtimes))
  6285. (unless (and remove (member time have))
  6286. (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
  6287. (push (org-agenda-format-item
  6288. nil string nil "" nil
  6289. (concat (substring time 0 -2) ":" (substring time -2)))
  6290. new)
  6291. (put-text-property
  6292. 2 (length (car new)) 'face 'org-time-grid (car new))))
  6293. (when (and todayp org-agenda-show-current-time-in-grid)
  6294. (push (org-agenda-format-item
  6295. nil org-agenda-current-time-string nil "" nil
  6296. (format-time-string "%H:%M "))
  6297. new)
  6298. (put-text-property
  6299. 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
  6300. (if (member 'time-up org-agenda-sorting-strategy-selected)
  6301. (append new list)
  6302. (append list new)))))
  6303. (defun org-compile-prefix-format (key)
  6304. "Compile the prefix format into a Lisp form that can be evaluated.
  6305. The resulting form and associated variable bindings is returned
  6306. and stored in the variable `org-prefix-format-compiled'."
  6307. (setq org-prefix-has-time nil
  6308. org-prefix-has-tag nil
  6309. org-prefix-category-length nil
  6310. org-prefix-has-effort nil
  6311. org-prefix-has-breadcrumbs nil)
  6312. (let ((s (cond
  6313. ((stringp org-agenda-prefix-format)
  6314. org-agenda-prefix-format)
  6315. ((assq key org-agenda-prefix-format)
  6316. (cdr (assq key org-agenda-prefix-format)))
  6317. (t " %-12:c%?-12t% s")))
  6318. (start 0)
  6319. varform vars var e c f opt)
  6320. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
  6321. s start)
  6322. (setq var (or (cdr (assoc (match-string 4 s)
  6323. '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
  6324. ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
  6325. 'eval)
  6326. c (or (match-string 3 s) "")
  6327. opt (match-beginning 1)
  6328. start (1+ (match-beginning 0)))
  6329. (cl-case var
  6330. (time (setq org-prefix-has-time t))
  6331. (tag (setq org-prefix-has-tag t))
  6332. (effort (setq org-prefix-has-effort t))
  6333. (breadcrumbs (setq org-prefix-has-breadcrumbs t)))
  6334. (setq f (concat "%" (match-string 2 s) "s"))
  6335. (when (eq var 'category)
  6336. (setq org-prefix-category-length
  6337. (floor (abs (string-to-number (match-string 2 s)))))
  6338. (setq org-prefix-category-max-length
  6339. (let ((x (match-string 2 s)))
  6340. (save-match-data
  6341. (and (string-match "\\.[0-9]+" x)
  6342. (string-to-number (substring (match-string 0 x) 1)))))))
  6343. (if (eq var 'eval)
  6344. (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
  6345. (if opt
  6346. (setq varform
  6347. `(if (or (equal "" ,var) (equal nil ,var))
  6348. ""
  6349. (format ,f (concat ,var ,c))))
  6350. (setq varform
  6351. `(format ,f (if (or (equal ,var "")
  6352. (equal ,var nil)) ""
  6353. (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
  6354. (setq s (replace-match "%s" t nil s))
  6355. (push varform vars))
  6356. (setq vars (nreverse vars))
  6357. (with-current-buffer (or org-agenda-buffer (current-buffer))
  6358. (setq org-prefix-format-compiled
  6359. (list
  6360. `((org-prefix-has-time ,org-prefix-has-time)
  6361. (org-prefix-has-tag ,org-prefix-has-tag)
  6362. (org-prefix-category-length ,org-prefix-category-length)
  6363. (org-prefix-has-effort ,org-prefix-has-effort)
  6364. (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
  6365. `(format ,s ,@vars))))))
  6366. (defun org-set-sorting-strategy (key)
  6367. (if (symbolp (car org-agenda-sorting-strategy))
  6368. ;; the old format
  6369. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  6370. (setq org-agenda-sorting-strategy-selected
  6371. (or (cdr (assq key org-agenda-sorting-strategy))
  6372. (cdr (assq 'agenda org-agenda-sorting-strategy))
  6373. '(time-up category-keep priority-down)))))
  6374. (defun org-get-time-of-day (s &optional string mod24)
  6375. "Check string S for a time of day.
  6376. If found, return it as a military time number between 0 and 2400.
  6377. If not found, return nil.
  6378. The optional STRING argument forces conversion into a 5 character wide string
  6379. HH:MM."
  6380. (save-match-data
  6381. (when
  6382. (and
  6383. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  6384. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  6385. (not (eq (get-text-property 1 'face s) 'org-link)))
  6386. (let* ((h (string-to-number (match-string 1 s)))
  6387. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  6388. (ampm (when (match-end 4) (downcase (match-string 4 s))))
  6389. (am-p (equal ampm "am"))
  6390. (h1 (cond ((not ampm) h)
  6391. ((= h 12) (if am-p 0 12))
  6392. (t (+ h (if am-p 0 12)))))
  6393. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  6394. (mod h1 24) h1))
  6395. (t0 (+ (* 100 h2) m))
  6396. (t1 (concat (if (>= h1 24) "+" " ")
  6397. (if (and org-agenda-time-leading-zero
  6398. (< t0 1000)) "0" "")
  6399. (if (< t0 100) "0" "")
  6400. (if (< t0 10) "0" "")
  6401. (int-to-string t0))))
  6402. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  6403. (defvar org-agenda-before-sorting-filter-function nil
  6404. "Function to be applied to agenda items prior to sorting.
  6405. Prior to sorting also means just before they are inserted into the agenda.
  6406. To aid sorting, you may revisit the original entries and add more text
  6407. properties which will later be used by the sorting functions.
  6408. The function should take a string argument, an agenda line.
  6409. It has access to the text properties in that line, which contain among
  6410. other things, the property `org-hd-marker' that points to the entry
  6411. where the line comes from. Note that not all lines going into the agenda
  6412. have this property, only most.
  6413. The function should return the modified string. It is probably best
  6414. to ONLY change text properties.
  6415. You can also use this function as a filter, by returning nil for lines
  6416. you don't want to have in the agenda at all. For this application, you
  6417. could bind the variable in the options section of a custom command.")
  6418. (defun org-agenda-finalize-entries (list &optional type)
  6419. "Sort, limit and concatenate the LIST of agenda items.
  6420. The optional argument TYPE tells the agenda type."
  6421. (let ((max-effort (cond ((listp org-agenda-max-effort)
  6422. (cdr (assoc type org-agenda-max-effort)))
  6423. (t org-agenda-max-effort)))
  6424. (max-todo (cond ((listp org-agenda-max-todos)
  6425. (cdr (assoc type org-agenda-max-todos)))
  6426. (t org-agenda-max-todos)))
  6427. (max-tags (cond ((listp org-agenda-max-tags)
  6428. (cdr (assoc type org-agenda-max-tags)))
  6429. (t org-agenda-max-tags)))
  6430. (max-entries (cond ((listp org-agenda-max-entries)
  6431. (cdr (assoc type org-agenda-max-entries)))
  6432. (t org-agenda-max-entries))))
  6433. (when org-agenda-before-sorting-filter-function
  6434. (setq list
  6435. (delq nil
  6436. (mapcar
  6437. org-agenda-before-sorting-filter-function list))))
  6438. (setq list (mapcar 'org-agenda-highlight-todo list)
  6439. list (mapcar 'identity (sort list 'org-entries-lessp)))
  6440. (when max-effort
  6441. (setq list (org-agenda-limit-entries
  6442. list 'effort-minutes max-effort
  6443. (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
  6444. 32767 -1))))))
  6445. (when max-todo
  6446. (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
  6447. (when max-tags
  6448. (setq list (org-agenda-limit-entries list 'tags max-tags)))
  6449. (when max-entries
  6450. (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
  6451. (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
  6452. (setq list (mapcar #'org-agenda--mark-blocked-entry list)))
  6453. (mapconcat 'identity list "\n")))
  6454. (defun org-agenda-limit-entries (list prop limit &optional fn)
  6455. "Limit the number of agenda entries."
  6456. (let ((include (and limit (< limit 0))))
  6457. (if limit
  6458. (let ((fun (or fn (lambda (p) (when p 1))))
  6459. (lim 0))
  6460. (delq nil
  6461. (mapcar
  6462. (lambda (e)
  6463. (let ((pval (funcall
  6464. fun (get-text-property (1- (length e))
  6465. prop e))))
  6466. (when pval (setq lim (+ lim pval)))
  6467. (cond ((and pval (<= lim (abs limit))) e)
  6468. ((and include (not pval)) e))))
  6469. list)))
  6470. list)))
  6471. (defun org-agenda-limit-interactively (remove)
  6472. "In agenda, interactively limit entries to various maximums."
  6473. (interactive "P")
  6474. (if remove
  6475. (progn (setq org-agenda-max-entries nil
  6476. org-agenda-max-todos nil
  6477. org-agenda-max-tags nil
  6478. org-agenda-max-effort nil)
  6479. (org-agenda-redo))
  6480. (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
  6481. (msg (cond ((= max ?E) "How many minutes? ")
  6482. ((= max ?e) "How many entries? ")
  6483. ((= max ?t) "How many TODO entries? ")
  6484. ((= max ?T) "How many tagged entries? ")
  6485. (t (user-error "Wrong input"))))
  6486. (num (string-to-number (read-from-minibuffer msg))))
  6487. (cond ((equal max ?e)
  6488. (let ((org-agenda-max-entries num)) (org-agenda-redo)))
  6489. ((equal max ?t)
  6490. (let ((org-agenda-max-todos num)) (org-agenda-redo)))
  6491. ((equal max ?T)
  6492. (let ((org-agenda-max-tags num)) (org-agenda-redo)))
  6493. ((equal max ?E)
  6494. (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
  6495. (org-agenda-fit-window-to-buffer))
  6496. (defun org-agenda-highlight-todo (x)
  6497. (let ((org-done-keywords org-done-keywords-for-agenda)
  6498. (case-fold-search nil)
  6499. re)
  6500. (if (eq x 'line)
  6501. (save-excursion
  6502. (beginning-of-line 1)
  6503. (setq re (org-get-at-bol 'org-todo-regexp))
  6504. (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
  6505. (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
  6506. (add-text-properties (match-beginning 0) (match-end 1)
  6507. (list 'face (org-get-todo-face 1)))
  6508. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  6509. (delete-region (match-beginning 1) (1- (match-end 0)))
  6510. (goto-char (match-beginning 1))
  6511. (insert (format org-agenda-todo-keyword-format s)))))
  6512. (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
  6513. (setq re (get-text-property 0 'org-todo-regexp x))
  6514. (when (and re
  6515. ;; Test `pl' because if there's no heading content,
  6516. ;; there's no point matching to highlight. Note
  6517. ;; that if we didn't test `pl' first, and there
  6518. ;; happened to be no keyword from `org-todo-regexp'
  6519. ;; on this heading line, then the `equal' comparison
  6520. ;; afterwards would spuriously succeed in the case
  6521. ;; where `pl' is nil -- causing an args-out-of-range
  6522. ;; error when we try to add text properties to text
  6523. ;; that isn't there.
  6524. pl
  6525. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  6526. x pl) pl))
  6527. (add-text-properties
  6528. (or (match-end 1) (match-end 0)) (match-end 0)
  6529. (list 'face (org-get-todo-face (match-string 2 x)))
  6530. x)
  6531. (when (match-end 1)
  6532. (setq x
  6533. (concat
  6534. (substring x 0 (match-end 1))
  6535. (format org-agenda-todo-keyword-format
  6536. (match-string 2 x))
  6537. ;; Remove `display' property as the icon could leak
  6538. ;; on the white space.
  6539. (org-add-props " " (org-plist-delete (text-properties-at 0 x)
  6540. 'display))
  6541. (substring x (match-end 3)))))))
  6542. x)))
  6543. (defsubst org-cmp-values (a b property)
  6544. "Compare the numeric value of text PROPERTY for string A and B."
  6545. (let ((pa (or (get-text-property (1- (length a)) property a) 0))
  6546. (pb (or (get-text-property (1- (length b)) property b) 0)))
  6547. (cond ((> pa pb) +1)
  6548. ((< pa pb) -1))))
  6549. (defsubst org-cmp-effort (a b)
  6550. "Compare the effort values of string A and B."
  6551. (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
  6552. ;; `effort-minutes' property is not directly accessible from
  6553. ;; the strings, but is stored as a property in `txt'.
  6554. (ea (or (get-text-property
  6555. 0 'effort-minutes (get-text-property 0 'txt a))
  6556. def))
  6557. (eb (or (get-text-property
  6558. 0 'effort-minutes (get-text-property 0 'txt b))
  6559. def)))
  6560. (cond ((> ea eb) +1)
  6561. ((< ea eb) -1))))
  6562. (defsubst org-cmp-category (a b)
  6563. "Compare the string values of categories of strings A and B."
  6564. (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
  6565. (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
  6566. (cond ((string-lessp ca cb) -1)
  6567. ((string-lessp cb ca) +1))))
  6568. (defsubst org-cmp-todo-state (a b)
  6569. "Compare the todo states of strings A and B."
  6570. (let* ((ma (or (get-text-property 1 'org-marker a)
  6571. (get-text-property 1 'org-hd-marker a)))
  6572. (mb (or (get-text-property 1 'org-marker b)
  6573. (get-text-property 1 'org-hd-marker b)))
  6574. (fa (and ma (marker-buffer ma)))
  6575. (fb (and mb (marker-buffer mb)))
  6576. (todo-kwds
  6577. (or (and fa (with-current-buffer fa org-todo-keywords-1))
  6578. (and fb (with-current-buffer fb org-todo-keywords-1))))
  6579. (ta (or (get-text-property 1 'todo-state a) ""))
  6580. (tb (or (get-text-property 1 'todo-state b) ""))
  6581. (la (- (length (member ta todo-kwds))))
  6582. (lb (- (length (member tb todo-kwds))))
  6583. (donepa (member ta org-done-keywords-for-agenda))
  6584. (donepb (member tb org-done-keywords-for-agenda)))
  6585. (cond ((and donepa (not donepb)) -1)
  6586. ((and (not donepa) donepb) +1)
  6587. ((< la lb) -1)
  6588. ((< lb la) +1))))
  6589. (defsubst org-cmp-alpha (a b)
  6590. "Compare the headlines, alphabetically."
  6591. (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
  6592. (plb (text-property-any 0 (length b) 'org-heading t b))
  6593. (ta (and pla (substring a pla)))
  6594. (tb (and plb (substring b plb)))
  6595. (case-fold-search nil))
  6596. (when pla
  6597. (when (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
  6598. "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
  6599. (setq ta (substring ta (match-end 0))))
  6600. (setq ta (downcase ta)))
  6601. (when plb
  6602. (when (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
  6603. "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
  6604. (setq tb (substring tb (match-end 0))))
  6605. (setq tb (downcase tb)))
  6606. (cond ((not (or ta tb)) nil)
  6607. ((not ta) +1)
  6608. ((not tb) -1)
  6609. ((string-lessp ta tb) -1)
  6610. ((string-lessp tb ta) +1))))
  6611. (defsubst org-cmp-tag (a b)
  6612. "Compare the string values of the first tags of A and B."
  6613. (let ((ta (car (last (get-text-property 1 'tags a))))
  6614. (tb (car (last (get-text-property 1 'tags b)))))
  6615. (cond ((not (or ta tb)) nil)
  6616. ((not ta) +1)
  6617. ((not tb) -1)
  6618. ((string-lessp ta tb) -1)
  6619. ((string-lessp tb ta) +1))))
  6620. (defsubst org-cmp-time (a b)
  6621. "Compare the time-of-day values of strings A and B."
  6622. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  6623. (ta (or (get-text-property 1 'time-of-day a) def))
  6624. (tb (or (get-text-property 1 'time-of-day b) def)))
  6625. (cond ((< ta tb) -1)
  6626. ((< tb ta) +1))))
  6627. (defsubst org-cmp-ts (a b type)
  6628. "Compare the timestamps values of entries A and B.
  6629. When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
  6630. \"timestamp_ia\", compare within each of these type. When TYPE
  6631. is the empty string, compare all timestamps without respect of
  6632. their type."
  6633. (let* ((def (and (not org-sort-agenda-notime-is-late) -1))
  6634. (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
  6635. (get-text-property 1 'ts-date a))
  6636. def))
  6637. (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
  6638. (get-text-property 1 'ts-date b))
  6639. def)))
  6640. (cond ((if ta (and tb (< ta tb)) tb) -1)
  6641. ((if tb (and ta (< tb ta)) ta) +1))))
  6642. (defsubst org-cmp-habit-p (a b)
  6643. "Compare the todo states of strings A and B."
  6644. (let ((ha (get-text-property 1 'org-habit-p a))
  6645. (hb (get-text-property 1 'org-habit-p b)))
  6646. (cond ((and ha (not hb)) -1)
  6647. ((and (not ha) hb) +1))))
  6648. (defun org-entries-lessp (a b)
  6649. "Predicate for sorting agenda entries."
  6650. ;; The following variables will be used when the form is evaluated.
  6651. ;; So even though the compiler complains, keep them.
  6652. (let* ((ss org-agenda-sorting-strategy-selected)
  6653. (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
  6654. (org-cmp-ts a b "")))
  6655. (timestamp-down (if timestamp-up (- timestamp-up) nil))
  6656. (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
  6657. (org-cmp-ts a b "scheduled")))
  6658. (scheduled-down (if scheduled-up (- scheduled-up) nil))
  6659. (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
  6660. (org-cmp-ts a b "deadline")))
  6661. (deadline-down (if deadline-up (- deadline-up) nil))
  6662. (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
  6663. (org-cmp-ts a b "timestamp_ia")))
  6664. (tsia-down (if tsia-up (- tsia-up) nil))
  6665. (ts-up (and (org-em 'ts-up 'ts-down ss)
  6666. (org-cmp-ts a b "timestamp")))
  6667. (ts-down (if ts-up (- ts-up) nil))
  6668. (time-up (and (org-em 'time-up 'time-down ss)
  6669. (org-cmp-time a b)))
  6670. (time-down (if time-up (- time-up) nil))
  6671. (stats-up (and (org-em 'stats-up 'stats-down ss)
  6672. (org-cmp-values a b 'org-stats)))
  6673. (stats-down (if stats-up (- stats-up) nil))
  6674. (priority-up (and (org-em 'priority-up 'priority-down ss)
  6675. (org-cmp-values a b 'priority)))
  6676. (priority-down (if priority-up (- priority-up) nil))
  6677. (effort-up (and (org-em 'effort-up 'effort-down ss)
  6678. (org-cmp-effort a b)))
  6679. (effort-down (if effort-up (- effort-up) nil))
  6680. (category-up (and (or (org-em 'category-up 'category-down ss)
  6681. (memq 'category-keep ss))
  6682. (org-cmp-category a b)))
  6683. (category-down (if category-up (- category-up) nil))
  6684. (category-keep (if category-up +1 nil))
  6685. (tag-up (and (org-em 'tag-up 'tag-down ss)
  6686. (org-cmp-tag a b)))
  6687. (tag-down (if tag-up (- tag-up) nil))
  6688. (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
  6689. (org-cmp-todo-state a b)))
  6690. (todo-state-down (if todo-state-up (- todo-state-up) nil))
  6691. (habit-up (and (org-em 'habit-up 'habit-down ss)
  6692. (org-cmp-habit-p a b)))
  6693. (habit-down (if habit-up (- habit-up) nil))
  6694. (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
  6695. (org-cmp-alpha a b)))
  6696. (alpha-down (if alpha-up (- alpha-up) nil))
  6697. (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
  6698. user-defined-up user-defined-down)
  6699. (when (and need-user-cmp org-agenda-cmp-user-defined
  6700. (functionp org-agenda-cmp-user-defined))
  6701. (setq user-defined-up
  6702. (funcall org-agenda-cmp-user-defined a b)
  6703. user-defined-down (if user-defined-up (- user-defined-up) nil)))
  6704. (cdr (assoc
  6705. (eval (cons 'or org-agenda-sorting-strategy-selected))
  6706. '((-1 . t) (1 . nil) (nil . nil))))))
  6707. ;;; Agenda restriction lock
  6708. (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
  6709. "Overlay to mark the headline to which agenda commands are restricted.")
  6710. (overlay-put org-agenda-restriction-lock-overlay
  6711. 'face 'org-agenda-restriction-lock)
  6712. (overlay-put org-agenda-restriction-lock-overlay
  6713. 'help-echo "Agendas are currently limited to this subtree.")
  6714. (delete-overlay org-agenda-restriction-lock-overlay)
  6715. (defun org-agenda-set-restriction-lock-from-agenda (arg)
  6716. "Set the restriction lock to the agenda item at point from within the agenda.
  6717. When called with a `\\[universal-argument]' prefix, restrict to
  6718. the file which contains the item.
  6719. Argument ARG is the prefix argument."
  6720. (interactive "P")
  6721. (unless (derived-mode-p 'org-agenda-mode)
  6722. (user-error "Not in an Org agenda buffer"))
  6723. (let* ((marker (or (org-get-at-bol 'org-marker)
  6724. (org-agenda-error)))
  6725. (buffer (marker-buffer marker))
  6726. (pos (marker-position marker)))
  6727. (with-current-buffer buffer
  6728. (goto-char pos)
  6729. (org-agenda-set-restriction-lock arg))))
  6730. ;;;###autoload
  6731. (defun org-agenda-set-restriction-lock (&optional type)
  6732. "Set restriction lock for agenda to current subtree or file.
  6733. When in a restricted subtree, remove it.
  6734. The restriction will span over the entire file if TYPE is `file',
  6735. or if type is '(4), or if the cursor is before the first headline
  6736. in the file. Otherwise, only apply the restriction to the current
  6737. subtree."
  6738. (interactive "P")
  6739. (if (and org-agenda-overriding-restriction
  6740. (member org-agenda-restriction-lock-overlay
  6741. (overlays-at (point)))
  6742. (equal (overlay-start org-agenda-restriction-lock-overlay)
  6743. (point)))
  6744. (org-agenda-remove-restriction-lock 'noupdate)
  6745. (org-agenda-remove-restriction-lock 'noupdate)
  6746. (and (equal type '(4)) (setq type 'file))
  6747. (setq type (cond
  6748. (type type)
  6749. ((org-at-heading-p) 'subtree)
  6750. ((condition-case nil (org-back-to-heading t) (error nil))
  6751. 'subtree)
  6752. (t 'file)))
  6753. (if (eq type 'subtree)
  6754. (progn
  6755. (setq org-agenda-restrict (current-buffer))
  6756. (setq org-agenda-overriding-restriction 'subtree)
  6757. (put 'org-agenda-files 'org-restrict
  6758. (list (buffer-file-name (buffer-base-buffer))))
  6759. (org-back-to-heading t)
  6760. (move-overlay org-agenda-restriction-lock-overlay
  6761. (point)
  6762. (if org-agenda-restriction-lock-highlight-subtree
  6763. (save-excursion (org-end-of-subtree t t) (point))
  6764. (point-at-eol)))
  6765. (move-marker org-agenda-restrict-begin (point))
  6766. (move-marker org-agenda-restrict-end
  6767. (save-excursion (org-end-of-subtree t t)))
  6768. (message "Locking agenda restriction to subtree"))
  6769. (put 'org-agenda-files 'org-restrict
  6770. (list (buffer-file-name (buffer-base-buffer))))
  6771. (setq org-agenda-restrict nil)
  6772. (setq org-agenda-overriding-restriction 'file)
  6773. (move-marker org-agenda-restrict-begin nil)
  6774. (move-marker org-agenda-restrict-end nil)
  6775. (message "Locking agenda restriction to file"))
  6776. (setq current-prefix-arg nil))
  6777. (org-agenda-maybe-redo))
  6778. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  6779. "Remove agenda restriction lock."
  6780. (interactive "P")
  6781. (if (not org-agenda-restrict)
  6782. (message "No agenda restriction to remove.")
  6783. (delete-overlay org-agenda-restriction-lock-overlay)
  6784. (delete-overlay org-speedbar-restriction-lock-overlay)
  6785. (setq org-agenda-overriding-restriction nil)
  6786. (setq org-agenda-restrict nil)
  6787. (put 'org-agenda-files 'org-restrict nil)
  6788. (move-marker org-agenda-restrict-begin nil)
  6789. (move-marker org-agenda-restrict-end nil)
  6790. (setq current-prefix-arg nil)
  6791. (message "Agenda restriction lock removed")
  6792. (or noupdate (org-agenda-maybe-redo))))
  6793. (defun org-agenda-maybe-redo ()
  6794. "If there is any window showing the agenda view, update it."
  6795. (let ((w (get-buffer-window (or org-agenda-this-buffer-name
  6796. org-agenda-buffer-name)
  6797. t))
  6798. (w0 (selected-window)))
  6799. (when w
  6800. (select-window w)
  6801. (org-agenda-redo)
  6802. (select-window w0)
  6803. (if org-agenda-overriding-restriction
  6804. (message "Agenda view shifted to new %s restriction"
  6805. org-agenda-overriding-restriction)
  6806. (message "Agenda restriction lock removed")))))
  6807. ;;; Agenda commands
  6808. (defun org-agenda-check-type (error &rest types)
  6809. "Check if agenda buffer or component is of allowed type.
  6810. If ERROR is non-nil, throw an error, otherwise just return nil.
  6811. Allowed types are `agenda' `todo' `tags' `search'."
  6812. (cond ((not org-agenda-type)
  6813. (error "No Org agenda currently displayed"))
  6814. ((memq org-agenda-type types) t)
  6815. (error
  6816. (error "Not allowed in '%s'-type agenda buffer or component" org-agenda-type))
  6817. (t nil)))
  6818. (defun org-agenda-Quit ()
  6819. "Exit the agenda, killing the agenda buffer.
  6820. Like `org-agenda-quit', but kill the buffer even when
  6821. `org-agenda-sticky' is non-nil."
  6822. (interactive)
  6823. (org-agenda--quit))
  6824. (defun org-agenda-quit ()
  6825. "Exit the agenda.
  6826. When `org-agenda-sticky' is non-nil, bury the agenda buffer
  6827. instead of killing it.
  6828. When `org-agenda-restore-windows-after-quit' is non-nil, restore
  6829. the pre-agenda window configuration.
  6830. When column view is active, exit column view instead of the
  6831. agenda."
  6832. (interactive)
  6833. (org-agenda--quit org-agenda-sticky))
  6834. (defun org-agenda--quit (&optional bury)
  6835. (if org-agenda-columns-active
  6836. (org-columns-quit)
  6837. (let ((wconf org-agenda-pre-window-conf)
  6838. (buf (current-buffer))
  6839. (org-agenda-last-indirect-window
  6840. (and (eq org-indirect-buffer-display 'other-window)
  6841. org-agenda-last-indirect-buffer
  6842. (get-buffer-window org-agenda-last-indirect-buffer))))
  6843. (cond
  6844. ((eq org-agenda-window-setup 'other-frame)
  6845. (delete-frame))
  6846. ((and org-agenda-restore-windows-after-quit
  6847. wconf)
  6848. ;; Maybe restore the pre-agenda window configuration. Reset
  6849. ;; `org-agenda-pre-window-conf' before running
  6850. ;; `set-window-configuration', which loses the current buffer.
  6851. (setq org-agenda-pre-window-conf nil)
  6852. (set-window-configuration wconf))
  6853. (t
  6854. (when org-agenda-last-indirect-window
  6855. (delete-window org-agenda-last-indirect-window))
  6856. (and (not (eq org-agenda-window-setup 'current-window))
  6857. (not (one-window-p))
  6858. (delete-window))))
  6859. (if bury
  6860. ;; Set the agenda buffer as the current buffer instead of
  6861. ;; passing it as an argument to `bury-buffer' so that
  6862. ;; `bury-buffer' removes it from the window.
  6863. (with-current-buffer buf
  6864. (bury-buffer))
  6865. (kill-buffer buf)
  6866. (setq org-agenda-archives-mode nil
  6867. org-agenda-buffer nil)))))
  6868. (defun org-agenda-exit ()
  6869. "Exit the agenda, killing Org buffers loaded by the agenda.
  6870. Like `org-agenda-Quit', but kill any buffers that were created by
  6871. the agenda. Org buffers visited directly by the user will not be
  6872. touched. Also, exit the agenda even if it is in column view."
  6873. (interactive)
  6874. (when org-agenda-columns-active
  6875. (org-columns-quit))
  6876. (org-release-buffers org-agenda-new-buffers)
  6877. (setq org-agenda-new-buffers nil)
  6878. (org-agenda-Quit))
  6879. (defun org-agenda-kill-all-agenda-buffers ()
  6880. "Kill all buffers in `org-agenda-mode'.
  6881. This is used when toggling sticky agendas."
  6882. (interactive)
  6883. (let (blist)
  6884. (dolist (buf (buffer-list))
  6885. (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
  6886. (push buf blist)))
  6887. (mapc 'kill-buffer blist)))
  6888. (defun org-agenda-execute (arg)
  6889. "Execute another agenda command, keeping same window.
  6890. So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
  6891. in the agenda."
  6892. (interactive "P")
  6893. (let ((org-agenda-window-setup 'current-window))
  6894. (org-agenda arg)))
  6895. (defun org-agenda-redo (&optional all)
  6896. "Rebuild possibly ALL agenda view(s) in the current buffer."
  6897. (interactive "P")
  6898. (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
  6899. (cpa (unless (eq all t) current-prefix-arg))
  6900. (org-agenda-doing-sticky-redo org-agenda-sticky)
  6901. (org-agenda-sticky nil)
  6902. (org-agenda-buffer-name (or org-agenda-this-buffer-name
  6903. org-agenda-buffer-name))
  6904. (org-agenda-keep-modes t)
  6905. (tag-filter org-agenda-tag-filter)
  6906. (tag-preset (get 'org-agenda-tag-filter :preset-filter))
  6907. (top-hl-filter org-agenda-top-headline-filter)
  6908. (cat-filter org-agenda-category-filter)
  6909. (cat-preset (get 'org-agenda-category-filter :preset-filter))
  6910. (re-filter org-agenda-regexp-filter)
  6911. (re-preset (get 'org-agenda-regexp-filter :preset-filter))
  6912. (effort-filter org-agenda-effort-filter)
  6913. (effort-preset (get 'org-agenda-effort-filter :preset-filter))
  6914. (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
  6915. (cols org-agenda-columns-active)
  6916. (line (org-current-line))
  6917. (window-line (- line (org-current-line (window-start))))
  6918. (lprops (get 'org-agenda-redo-command 'org-lprops))
  6919. (redo-cmd (get-text-property p 'org-redo-cmd))
  6920. (last-args (get-text-property p 'org-last-args))
  6921. (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
  6922. (org-agenda-overriding-cmd-arguments
  6923. (unless (eq all t)
  6924. (cond ((listp last-args)
  6925. (cons (or cpa (car last-args)) (cdr last-args)))
  6926. ((stringp last-args)
  6927. last-args))))
  6928. (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
  6929. (put 'org-agenda-tag-filter :preset-filter nil)
  6930. (put 'org-agenda-category-filter :preset-filter nil)
  6931. (put 'org-agenda-regexp-filter :preset-filter nil)
  6932. (put 'org-agenda-effort-filter :preset-filter nil)
  6933. (and cols (org-columns-quit))
  6934. (message "Rebuilding agenda buffer...")
  6935. (if series-redo-cmd
  6936. (eval series-redo-cmd)
  6937. (org-let lprops redo-cmd))
  6938. (setq org-agenda-undo-list nil
  6939. org-agenda-pending-undo-list nil
  6940. org-agenda-tag-filter tag-filter
  6941. org-agenda-category-filter cat-filter
  6942. org-agenda-regexp-filter re-filter
  6943. org-agenda-effort-filter effort-filter
  6944. org-agenda-top-headline-filter top-hl-filter)
  6945. (message "Rebuilding agenda buffer...done")
  6946. (put 'org-agenda-tag-filter :preset-filter tag-preset)
  6947. (put 'org-agenda-category-filter :preset-filter cat-preset)
  6948. (put 'org-agenda-regexp-filter :preset-filter re-preset)
  6949. (put 'org-agenda-effort-filter :preset-filter effort-preset)
  6950. (let ((tag (or tag-filter tag-preset))
  6951. (cat (or cat-filter cat-preset))
  6952. (effort (or effort-filter effort-preset))
  6953. (re (or re-filter re-preset)))
  6954. (when tag (org-agenda-filter-apply tag 'tag t))
  6955. (when cat (org-agenda-filter-apply cat 'category))
  6956. (when effort (org-agenda-filter-apply effort 'effort))
  6957. (when re (org-agenda-filter-apply re 'regexp)))
  6958. (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
  6959. (and cols (called-interactively-p 'any) (org-agenda-columns))
  6960. (org-goto-line line)
  6961. (recenter window-line)))
  6962. (defun org-agenda-redo-all (&optional exhaustive)
  6963. "Rebuild all agenda views in the current buffer.
  6964. With a prefix argument, do so in all agenda buffers."
  6965. (interactive "P")
  6966. (if exhaustive
  6967. (dolist (buffer (buffer-list))
  6968. (with-current-buffer buffer
  6969. (when (derived-mode-p 'org-agenda-mode)
  6970. (org-agenda-redo t))))
  6971. (org-agenda-redo t)))
  6972. (defvar org-global-tags-completion-table nil)
  6973. (defvar org-agenda-filter-form nil)
  6974. (defvar org-agenda-filtered-by-category nil)
  6975. (defsubst org-agenda-get-category ()
  6976. "Return the category of the agenda line."
  6977. (org-get-at-bol 'org-category))
  6978. (defun org-agenda-filter-by-category (strip)
  6979. "Filter lines in the agenda buffer that have a specific category.
  6980. The category is that of the current line.
  6981. With a `\\[universal-argument]' prefix argument, exclude the lines of that category.
  6982. When there is already a category filter in place, this command removes the filter."
  6983. (interactive "P")
  6984. (if (and org-agenda-filtered-by-category
  6985. org-agenda-category-filter)
  6986. (org-agenda-filter-show-all-cat)
  6987. (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
  6988. (cond
  6989. ((and cat strip)
  6990. (org-agenda-filter-apply
  6991. (push (concat "-" cat) org-agenda-category-filter) 'category))
  6992. (cat
  6993. (org-agenda-filter-apply
  6994. (setq org-agenda-category-filter
  6995. (list (concat "+" cat))) 'category))
  6996. (t (error "No category at point"))))))
  6997. (defun org-find-top-headline (&optional pos)
  6998. "Find the topmost parent headline and return it.
  6999. POS when non-nil is the marker or buffer position to start the
  7000. search from."
  7001. (save-excursion
  7002. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  7003. (when pos (goto-char pos))
  7004. ;; Skip up to the topmost parent.
  7005. (while (org-up-heading-safe))
  7006. (ignore-errors (nth 4 (org-heading-components))))))
  7007. (defvar org-agenda-filtered-by-top-headline nil)
  7008. (defun org-agenda-filter-by-top-headline (strip)
  7009. "Keep only those lines that are descendants from the same top headline.
  7010. The top headline is that of the current line. With prefix arg STRIP, hide
  7011. all lines of the category at point."
  7012. (interactive "P")
  7013. (if org-agenda-filtered-by-top-headline
  7014. (progn
  7015. (setq org-agenda-filtered-by-top-headline nil
  7016. org-agenda-top-headline-filter nil)
  7017. (org-agenda-filter-show-all-top-filter))
  7018. (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
  7019. (if toph (org-agenda-filter-top-headline-apply toph strip)
  7020. (error "No top-level headline at point")))))
  7021. (defvar org-agenda-regexp-filter nil)
  7022. (defun org-agenda-filter-by-regexp (strip-or-accumulate)
  7023. "Filter agenda entries by a regular expressions.
  7024. You will be prompted for the regular expression, and the agenda
  7025. view will only show entries that are matched by that expression.
  7026. With one `\\[universal-argument]' prefix argument, hide entries matching the regexp.
  7027. When there is already a regexp filter active, this command removed the
  7028. filter. However, with two `\\[universal-argument]' prefix arguments, add a new condition to
  7029. an already existing regexp filter."
  7030. (interactive "P")
  7031. (let* ((strip (equal strip-or-accumulate '(4)))
  7032. (accumulate (equal strip-or-accumulate '(16))))
  7033. (cond
  7034. ((and org-agenda-regexp-filter (not accumulate))
  7035. (org-agenda-filter-show-all-re)
  7036. (message "Regexp filter removed"))
  7037. (t (let ((flt (concat (if strip "-" "+")
  7038. (read-from-minibuffer
  7039. (if strip
  7040. "Hide entries matching regexp: "
  7041. "Narrow to entries matching regexp: ")))))
  7042. (push flt org-agenda-regexp-filter)
  7043. (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))))))
  7044. (defvar org-agenda-effort-filter nil)
  7045. (defun org-agenda-filter-by-effort (strip-or-accumulate)
  7046. "Filter agenda entries by effort.
  7047. With no `\\[universal-argument]' prefix argument, keep entries matching the effort condition.
  7048. With one `\\[universal-argument]' prefix argument, filter out entries matching the condition.
  7049. With two `\\[universal-argument]' prefix arguments, add a second condition to the existing filter.
  7050. This last option is in practice not very useful, but it is available for
  7051. consistency with the other filter commands."
  7052. (interactive "P")
  7053. (let* ((efforts (split-string
  7054. (or (cdr (assoc (concat org-effort-property "_ALL")
  7055. org-global-properties))
  7056. "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
  7057. ;; XXX: the following handles only up to 10 different
  7058. ;; effort values.
  7059. (allowed-keys (if (null efforts) nil
  7060. (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
  7061. (number-sequence 1 (length efforts)))))
  7062. (keep (equal strip-or-accumulate '(16)))
  7063. (negative (equal strip-or-accumulate '(4)))
  7064. (current org-agenda-effort-filter)
  7065. (op nil))
  7066. (while (not (memq op '(?< ?> ?= ?_)))
  7067. (setq op (read-char-exclusive
  7068. "Effort operator? (> = or <) or press `_' again to remove filter")))
  7069. ;; Select appropriate duration. Ignore non-digit characters.
  7070. (if (eq op ?_)
  7071. (progn
  7072. (org-agenda-filter-show-all-effort)
  7073. (message "Effort filter removed"))
  7074. (let ((prompt
  7075. (apply #'format
  7076. (concat "Effort %c "
  7077. (mapconcat (lambda (s) (concat "[%d]" s))
  7078. efforts
  7079. " "))
  7080. op allowed-keys))
  7081. (eff -1))
  7082. (while (not (memq eff allowed-keys))
  7083. (message prompt)
  7084. (setq eff (- (read-char-exclusive) 48)))
  7085. (org-agenda-filter-show-all-effort)
  7086. (setq org-agenda-effort-filter
  7087. (append
  7088. (list (concat (if negative "-" "+")
  7089. (char-to-string op)
  7090. ;; Numbering is 1 2 3 ... 9 0, but we want
  7091. ;; 0 1 2 ... 8 9.
  7092. (nth (mod (1- eff) 10) efforts)))
  7093. (if keep current nil)))
  7094. (org-agenda-filter-apply org-agenda-effort-filter 'effort)))))
  7095. (defun org-agenda-filter (&optional strip-or-accumulate)
  7096. "Prompt for a general filter string and apply it to the agenda.
  7097. The string may contain filter elements like
  7098. +category
  7099. +tag
  7100. +<effort > and = are also allowed as effort operators
  7101. +/regexp/
  7102. Instead of `+', `-' is allowed to strip the agenda of matching entries.
  7103. `+' is optional if it is not required to separate two string parts.
  7104. Multiple filter elements can be concatenated without spaces, for example
  7105. +work-John<0:10-/plot/
  7106. selects entries with category `work' and effort estimates below 10 minutes,
  7107. and deselects entries with tag `John' or matching the regexp `plot'.
  7108. During entry of the filter, completion for tags, categories and effort
  7109. values is offered. Since the syntax for categories and tags is identical
  7110. there should be no overlap between categories and tags. If there is, tags
  7111. get priority.
  7112. A single `\\[universal-argument]' prefix arg STRIP-OR-ACCUMULATE will negate the
  7113. entire filter, which can be useful in connection with the prompt history.
  7114. A double `\\[universal-argument] \\[universal-argument]' prefix arg will add the new filter elements to the
  7115. existing ones. A shortcut for this is to add an additional `+' at the
  7116. beginning of the string, like `+-John'.
  7117. With a triple prefix argument, execute the computed filtering defined in
  7118. the variable `org-agenda-auto-exclude-function'."
  7119. (interactive "P")
  7120. (if (equal strip-or-accumulate '(64))
  7121. ;; Execute the auto-exclude action
  7122. (if (not org-agenda-auto-exclude-function)
  7123. (user-error "`org-agenda-auto-exclude-function' is undefined")
  7124. (org-agenda-filter-show-all-tag)
  7125. (setq org-agenda-tag-filter nil)
  7126. (dolist (tag (org-agenda-get-represented-tags))
  7127. (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
  7128. (when modifier
  7129. (push modifier org-agenda-tag-filter))))
  7130. (unless (null org-agenda-tag-filter)
  7131. (org-agenda-filter-apply org-agenda-tag-filter 'tag 'expand)))
  7132. ;; Prompt for a filter and act
  7133. (let* ((tag-list (org-agenda-get-represented-tags))
  7134. (category-list (org-agenda-get-represented-categories))
  7135. (negate (equal strip-or-accumulate '(4)))
  7136. (f-string (completing-read
  7137. (concat
  7138. (if negate "Negative filter" "Filter")
  7139. " [+cat-tag<0:10-/regexp/]: ")
  7140. 'org-agenda-filter-completion-function))
  7141. (keep (or (if (string-match "^\\+[+-]" f-string)
  7142. (progn (setq f-string (substring f-string 1)) t))
  7143. (equal strip-or-accumulate '(16))))
  7144. (fc (if keep org-agenda-category-filter))
  7145. (ft (if keep org-agenda-tag-filter))
  7146. (fe (if keep org-agenda-effort-filter))
  7147. (fr (if keep org-agenda-regexp-filter))
  7148. pm s)
  7149. (while (string-match "^[ \t]*\\([-+]\\)?\\(\\([^-+<>=/ \t]+\\)\\|\\([<>=][0-9:]+\\)\\|\\(/\\([^/]+\\)/?\\)\\)" f-string)
  7150. (setq pm (if (match-beginning 1) (match-string 1 f-string) "+"))
  7151. (when negate
  7152. (setq pm (if (equal pm "+") "-" "+")))
  7153. (cond
  7154. ((match-beginning 3)
  7155. ;; category or tag
  7156. (setq s (match-string 3 f-string))
  7157. (cond
  7158. ((member s tag-list)
  7159. (add-to-list 'ft (concat pm s) 'append 'equal))
  7160. ((member s category-list)
  7161. (add-to-list 'fc (concat pm s) 'append 'equal))
  7162. (t (message
  7163. "`%s%s' filter ignored because tag/category is not represented"
  7164. pm s))))
  7165. ((match-beginning 4)
  7166. ;; effort
  7167. (add-to-list 'fe (concat pm (match-string 4 f-string)) t 'equal))
  7168. ((match-beginning 5)
  7169. ;; regexp
  7170. (add-to-list 'fr (concat pm (match-string 6 f-string)) t 'equal)))
  7171. (setq f-string (substring f-string (match-end 0))))
  7172. (org-agenda-filter-remove-all)
  7173. (and fc (org-agenda-filter-apply
  7174. (setq org-agenda-category-filter fc) 'category))
  7175. (and ft (org-agenda-filter-apply
  7176. (setq org-agenda-tag-filter ft) 'tag))
  7177. (and fe (org-agenda-filter-apply
  7178. (setq org-agenda-effort-filter fe) 'effort))
  7179. (and fr (org-agenda-filter-apply
  7180. (setq org-agenda-regexp-filter fr) 'regexp))
  7181. )))
  7182. (defun org-agenda-filter-completion-function (string _predicate &optional flag)
  7183. "Complete a complex filter string
  7184. FLAG specifies the type of completion operation to perform. This
  7185. function is passed as a collection function to `completing-read',
  7186. which see."
  7187. (let ((completion-ignore-case t) ;tags are case-sensitive
  7188. (confirm (lambda (x) (stringp x)))
  7189. (prefix "")
  7190. (operator "")
  7191. table)
  7192. (when (string-match "^\\(.*\\([-+<>=]\\)\\)\\([^-+<>=]*\\)$" string)
  7193. (setq prefix (match-string 1 string)
  7194. operator (match-string 2 string)
  7195. string (match-string 3 string)))
  7196. (cond
  7197. ((member operator '("+" "-" "" nil))
  7198. (setq table (append (org-agenda-get-represented-categories)
  7199. (org-agenda-get-represented-tags))))
  7200. ((member operator '("<" ">" "="))
  7201. (setq table (split-string
  7202. (or (cdr (assoc (concat org-effort-property "_ALL")
  7203. org-global-properties))
  7204. "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")
  7205. " +")))
  7206. (t (setq table nil)))
  7207. (pcase flag
  7208. (`t (all-completions string table confirm))
  7209. (`lambda (assoc string table)) ;exact match?
  7210. (`nil
  7211. (pcase (try-completion string table confirm)
  7212. ((and completion (pred stringp))
  7213. (concat prefix completion))
  7214. (completion completion)))
  7215. (_ nil))))
  7216. (defun org-agenda-filter-remove-all ()
  7217. "Remove all filters from the current agenda buffer."
  7218. (interactive)
  7219. (when org-agenda-tag-filter
  7220. (org-agenda-filter-show-all-tag))
  7221. (when org-agenda-category-filter
  7222. (org-agenda-filter-show-all-cat))
  7223. (when org-agenda-regexp-filter
  7224. (org-agenda-filter-show-all-re))
  7225. (when org-agenda-top-headline-filter
  7226. (org-agenda-filter-show-all-top-filter))
  7227. (when org-agenda-effort-filter
  7228. (org-agenda-filter-show-all-effort))
  7229. (org-agenda-finalize))
  7230. (defun org-agenda-filter-by-tag (strip-or-accumulate &optional char exclude)
  7231. "Keep only those lines in the agenda buffer that have a specific tag.
  7232. The tag is selected with its fast selection letter, as configured.
  7233. With a `\\[universal-argument]' prefix, apply the filter negatively, stripping all matches.
  7234. With a `\\[universal-argument] \\[universal-argument]' prefix, add the new tag to the existing filter
  7235. instead of replacing it.
  7236. With a `\\[universal-argument] \\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
  7237. i.e. don't
  7238. filter on all its group members.
  7239. A lisp caller can specify CHAR. EXCLUDE means that the new tag
  7240. should be used to exclude the search - the interactive user can
  7241. also press `-' or `+' to switch between filtering and excluding."
  7242. (interactive "P")
  7243. (let* ((alist org-tag-alist-for-agenda)
  7244. (seen-chars nil)
  7245. (tag-chars (mapconcat
  7246. (lambda (x) (if (and (not (symbolp (car x)))
  7247. (cdr x)
  7248. (not (member (cdr x) seen-chars)))
  7249. (progn
  7250. (push (cdr x) seen-chars)
  7251. (char-to-string (cdr x)))
  7252. ""))
  7253. org-tag-alist-for-agenda ""))
  7254. (valid-char-list (append '(?\t ?\r ?\\ ?. ?\s ?q)
  7255. (string-to-list tag-chars)))
  7256. (exclude (or exclude (equal strip-or-accumulate '(4))))
  7257. (accumulate (equal strip-or-accumulate '(16)))
  7258. (expand (not (equal strip-or-accumulate '(64))))
  7259. (inhibit-read-only t)
  7260. (current org-agenda-tag-filter)
  7261. a n tag)
  7262. (unless char
  7263. (while (not (memq char valid-char-list))
  7264. (org-unlogged-message
  7265. "%s by tag%s: [%s ]tag-char [TAB]tag %s[\\]off [q]uit"
  7266. (if exclude "Exclude[+]" "Filter[-]")
  7267. (if expand "" " (no grouptag expand)")
  7268. tag-chars
  7269. (if org-agenda-auto-exclude-function "[RET] " ""))
  7270. (setq char (read-char-exclusive))
  7271. ;; Excluding or filtering down
  7272. (cond ((eq char ?-) (setq exclude t))
  7273. ((eq char ?+) (setq exclude nil)))))
  7274. (when (eq char ?\t)
  7275. (unless (local-variable-p 'org-global-tags-completion-table)
  7276. (setq-local org-global-tags-completion-table
  7277. (org-global-tags-completion-table)))
  7278. (let ((completion-ignore-case t))
  7279. (setq tag (completing-read
  7280. "Tag: " org-global-tags-completion-table nil t))))
  7281. (cond
  7282. ((eq char ?\r)
  7283. (org-agenda-filter-show-all-tag)
  7284. (when org-agenda-auto-exclude-function
  7285. (setq org-agenda-tag-filter nil)
  7286. (dolist (tag (org-agenda-get-represented-tags))
  7287. (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
  7288. (when modifier
  7289. (push modifier org-agenda-tag-filter))))
  7290. (unless (null org-agenda-tag-filter)
  7291. (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
  7292. ((eq char ?\\)
  7293. (org-agenda-filter-show-all-tag)
  7294. (when (get 'org-agenda-tag-filter :preset-filter)
  7295. (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
  7296. ((eq char ?.)
  7297. (setq org-agenda-tag-filter
  7298. (mapcar (lambda(tag) (concat "+" tag))
  7299. (org-get-at-bol 'tags)))
  7300. (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
  7301. ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
  7302. ((or (eq char ?\s)
  7303. (setq a (rassoc char alist))
  7304. (and tag (setq a (cons tag nil))))
  7305. (org-agenda-filter-show-all-tag)
  7306. (setq tag (car a))
  7307. (setq org-agenda-tag-filter
  7308. (cons (concat (if exclude "-" "+") tag)
  7309. (if accumulate current nil)))
  7310. (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
  7311. (t (error "Invalid tag selection character %c" char)))))
  7312. (defun org-agenda-get-represented-categories ()
  7313. "Return a list of all categories used in this agenda buffer."
  7314. (or org-agenda-represented-categories
  7315. (when (derived-mode-p 'org-agenda-mode)
  7316. (let ((pos (point-min)) categories)
  7317. (while (and (< pos (point-max))
  7318. (setq pos (next-single-property-change
  7319. pos 'org-category nil (point-max))))
  7320. (push (get-text-property pos 'org-category) categories))
  7321. (setq org-agenda-represented-categories
  7322. (nreverse (org-uniquify (delq nil categories))))))))
  7323. (defun org-agenda-get-represented-tags ()
  7324. "Return a list of all tags used in this agenda buffer.
  7325. These will be lower-case, for filtering."
  7326. (or org-agenda-represented-tags
  7327. (when (derived-mode-p 'org-agenda-mode)
  7328. (let ((pos (point-min)) tags-lists tt)
  7329. (while (and (< pos (point-max))
  7330. (setq pos (next-single-property-change
  7331. pos 'tags nil (point-max))))
  7332. (setq tt (get-text-property pos 'tags))
  7333. (if tt (push tt tags-lists)))
  7334. (setq org-agenda-represented-tags
  7335. (nreverse (org-uniquify
  7336. (delq nil (apply 'append tags-lists)))))))))
  7337. (defun org-agenda-filter-make-matcher (filter type &optional expand)
  7338. "Create the form that tests a line for agenda filter. Optional
  7339. argument EXPAND can be used for the TYPE tag and will expand the
  7340. tags in the FILTER if any of the tags in FILTER are grouptags."
  7341. (let (f f1)
  7342. (cond
  7343. ;; Tag filter
  7344. ((eq type 'tag)
  7345. (setq filter
  7346. (delete-dups
  7347. (append (get 'org-agenda-tag-filter :preset-filter)
  7348. filter)))
  7349. (dolist (x filter)
  7350. (let ((op (string-to-char x)))
  7351. (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
  7352. (setq x (list x)))
  7353. (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
  7354. (push f1 f))))
  7355. ;; Category filter
  7356. ((eq type 'category)
  7357. (setq filter
  7358. (delete-dups
  7359. (append (get 'org-agenda-category-filter :preset-filter)
  7360. filter)))
  7361. (dolist (x filter)
  7362. (if (equal "-" (substring x 0 1))
  7363. (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
  7364. (setq f1 (list 'equal (substring x 1) 'cat)))
  7365. (push f1 f)))
  7366. ;; Regexp filter
  7367. ((eq type 'regexp)
  7368. (setq filter
  7369. (delete-dups
  7370. (append (get 'org-agenda-regexp-filter :preset-filter)
  7371. filter)))
  7372. (dolist (x filter)
  7373. (if (equal "-" (substring x 0 1))
  7374. (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
  7375. (setq f1 (list 'string-match (substring x 1) 'txt)))
  7376. (push f1 f)))
  7377. ;; Effort filter
  7378. ((eq type 'effort)
  7379. (setq filter
  7380. (delete-dups
  7381. (append (get 'org-agenda-effort-filter :preset-filter)
  7382. filter)))
  7383. (dolist (x filter)
  7384. (push (org-agenda-filter-effort-form x) f))))
  7385. (cons (if (eq type 'category) 'or 'and) (nreverse f))))
  7386. (defun org-agenda-filter-make-matcher-tag-exp (tags op)
  7387. "Return a form associated to tag-expression TAGS.
  7388. Build a form testing a line for agenda filter for
  7389. tag-expressions. OP is an operator of type CHAR that allows the
  7390. function to set the right switches in the returned form."
  7391. (let (form)
  7392. ;; Any of the expressions can match if OP is +, all must match if
  7393. ;; the operator is -.
  7394. (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
  7395. (let* ((tag (substring x 1))
  7396. (f (cond
  7397. ((string= "" tag) 'tags)
  7398. ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
  7399. ;; TAG is a regexp.
  7400. (list 'org-match-any-p (substring tag 1 -1) 'tags))
  7401. (t (list 'member (downcase tag) 'tags)))))
  7402. (push (if (eq op ?-) (list 'not f) f) form)))))
  7403. (defun org-agenda-filter-effort-form (e)
  7404. "Return the form to compare the effort of the current line with what E says.
  7405. E looks like \"+<2:25\"."
  7406. (let (op)
  7407. (setq e (substring e 1))
  7408. (setq op (string-to-char e) e (substring e 1))
  7409. (setq op (cond ((equal op ?<) '<=)
  7410. ((equal op ?>) '>=)
  7411. ((equal op ??) op)
  7412. (t '=)))
  7413. (list 'org-agenda-compare-effort (list 'quote op)
  7414. (org-duration-to-minutes e))))
  7415. (defun org-agenda-compare-effort (op value)
  7416. "Compare the effort of the current line with VALUE, using OP.
  7417. If the line does not have an effort defined, return nil."
  7418. ;; `effort-minutes' property cannot be extracted directly from
  7419. ;; current line but is stored as a property in `txt'.
  7420. (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
  7421. (funcall op
  7422. (or effort (if org-sort-agenda-noeffort-is-high 32767 -1))
  7423. value)))
  7424. (defun org-agenda-filter-expand-tags (filter &optional no-operator)
  7425. "Expand group tags in FILTER for the agenda.
  7426. When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
  7427. (if org-group-tags
  7428. (let ((case-fold-search t) rtn)
  7429. (mapc
  7430. (lambda (f)
  7431. (let (f0 dir)
  7432. (if (string-match "^\\([+-]\\)\\(.+\\)" f)
  7433. (setq dir (match-string 1 f) f0 (match-string 2 f))
  7434. (setq dir (if no-operator "" "+") f0 f))
  7435. (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
  7436. (org-tags-expand f0 t t))
  7437. rtn))))
  7438. filter)
  7439. (reverse rtn))
  7440. filter))
  7441. (defun org-agenda-filter-apply (filter type &optional expand)
  7442. "Set FILTER as the new agenda filter and apply it. Optional
  7443. argument EXPAND can be used for the TYPE tag and will expand the
  7444. tags in the FILTER if any of the tags in FILTER are grouptags."
  7445. ;; Deactivate `org-agenda-entry-text-mode' when filtering
  7446. (when org-agenda-entry-text-mode (org-agenda-entry-text-mode))
  7447. (let (tags cat txt)
  7448. (setq org-agenda-filter-form (org-agenda-filter-make-matcher
  7449. filter type expand))
  7450. ;; Only set `org-agenda-filtered-by-category' to t when a unique
  7451. ;; category is used as the filter:
  7452. (setq org-agenda-filtered-by-category
  7453. (and (eq type 'category)
  7454. (not (equal (substring (car filter) 0 1) "-"))))
  7455. (org-agenda-set-mode-name)
  7456. (save-excursion
  7457. (goto-char (point-min))
  7458. (while (not (eobp))
  7459. (if (org-get-at-bol 'org-hd-marker)
  7460. (progn
  7461. (setq tags (org-get-at-bol 'tags)
  7462. cat (org-agenda-get-category)
  7463. txt (org-get-at-bol 'txt))
  7464. (unless (eval org-agenda-filter-form)
  7465. (org-agenda-filter-hide-line type))
  7466. (beginning-of-line 2))
  7467. (beginning-of-line 2))))
  7468. (when (get-char-property (point) 'invisible)
  7469. (ignore-errors (org-agenda-previous-line)))))
  7470. (defun org-agenda-filter-top-headline-apply (hl &optional negative)
  7471. "Filter by top headline HL."
  7472. (org-agenda-set-mode-name)
  7473. (save-excursion
  7474. (goto-char (point-min))
  7475. (while (not (eobp))
  7476. (let* ((pos (org-get-at-bol 'org-hd-marker))
  7477. (tophl (and pos (org-find-top-headline pos))))
  7478. (when (and tophl (funcall (if negative 'identity 'not)
  7479. (string= hl tophl)))
  7480. (org-agenda-filter-hide-line 'top-headline)))
  7481. (beginning-of-line 2)))
  7482. (when (get-char-property (point) 'invisible)
  7483. (org-agenda-previous-line))
  7484. (setq org-agenda-top-headline-filter hl
  7485. org-agenda-filtered-by-top-headline t))
  7486. (defun org-agenda-filter-hide-line (type)
  7487. "Hide lines with TYPE in the agenda buffer."
  7488. (let* ((b (max (point-min) (1- (point-at-bol))))
  7489. (e (point-at-eol)))
  7490. (let ((inhibit-read-only t))
  7491. (add-text-properties
  7492. b e `(invisible org-filtered org-filter-type ,type)))))
  7493. (defun org-agenda-remove-filter (type)
  7494. (interactive)
  7495. "Remove filter of type TYPE from the agenda buffer."
  7496. (save-excursion
  7497. (goto-char (point-min))
  7498. (let ((inhibit-read-only t) pos)
  7499. (while (setq pos (text-property-any (point) (point-max)
  7500. 'org-filter-type type))
  7501. (goto-char pos)
  7502. (remove-text-properties
  7503. (point) (next-single-property-change (point) 'org-filter-type)
  7504. `(invisible org-filtered org-filter-type ,type))))
  7505. (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
  7506. (setq org-agenda-filter-form nil)
  7507. (org-agenda-set-mode-name)
  7508. (org-agenda-finalize)))
  7509. (defun org-agenda-filter-show-all-tag nil
  7510. (org-agenda-remove-filter 'tag))
  7511. (defun org-agenda-filter-show-all-re nil
  7512. (org-agenda-remove-filter 'regexp))
  7513. (defun org-agenda-filter-show-all-effort nil
  7514. (org-agenda-remove-filter 'effort))
  7515. (defun org-agenda-filter-show-all-cat nil
  7516. (org-agenda-remove-filter 'category))
  7517. (defun org-agenda-filter-show-all-top-filter nil
  7518. (org-agenda-remove-filter 'top-headline))
  7519. (defun org-agenda-manipulate-query-add ()
  7520. "Manipulate the query by adding a search term with positive selection.
  7521. Positive selection means the term must be matched for selection of an entry."
  7522. (interactive)
  7523. (org-agenda-manipulate-query ?\[))
  7524. (defun org-agenda-manipulate-query-subtract ()
  7525. "Manipulate the query by adding a search term with negative selection.
  7526. Negative selection means term must not be matched for selection of an entry."
  7527. (interactive)
  7528. (org-agenda-manipulate-query ?\]))
  7529. (defun org-agenda-manipulate-query-add-re ()
  7530. "Manipulate the query by adding a search regexp with positive selection.
  7531. Positive selection means the regexp must match for selection of an entry."
  7532. (interactive)
  7533. (org-agenda-manipulate-query ?\{))
  7534. (defun org-agenda-manipulate-query-subtract-re ()
  7535. "Manipulate the query by adding a search regexp with negative selection.
  7536. Negative selection means regexp must not match for selection of an entry."
  7537. (interactive)
  7538. (org-agenda-manipulate-query ?\}))
  7539. (defun org-agenda-manipulate-query (char)
  7540. (cond
  7541. ((eq org-agenda-type 'agenda)
  7542. (let ((org-agenda-include-inactive-timestamps t))
  7543. (org-agenda-redo))
  7544. (message "Display now includes inactive timestamps as well"))
  7545. ((eq org-agenda-type 'search)
  7546. (org-add-to-string
  7547. 'org-agenda-query-string
  7548. (if org-agenda-last-search-view-search-was-boolean
  7549. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  7550. (?\{ . " +{}") (?\} . " -{}"))))
  7551. " "))
  7552. (setq org-agenda-redo-command
  7553. (list 'org-search-view
  7554. (car (get-text-property (min (1- (point-max)) (point))
  7555. 'org-last-args))
  7556. org-agenda-query-string
  7557. (+ (length org-agenda-query-string)
  7558. (if (member char '(?\{ ?\})) 0 1))))
  7559. (set-register org-agenda-query-register org-agenda-query-string)
  7560. (let ((org-agenda-overriding-arguments
  7561. (cdr org-agenda-redo-command)))
  7562. (org-agenda-redo)))
  7563. (t (error "Cannot manipulate query for %s-type agenda buffers"
  7564. org-agenda-type))))
  7565. (defun org-add-to-string (var string)
  7566. (set var (concat (symbol-value var) string)))
  7567. (defun org-agenda-goto-date (span)
  7568. "Jump to DATE in agenda."
  7569. (interactive "P")
  7570. (let* ((org-read-date-prefer-future
  7571. (eval org-agenda-jump-prefer-future))
  7572. (date (org-read-date))
  7573. (day (time-to-days (org-time-string-to-time date)))
  7574. (org-agenda-sticky-orig org-agenda-sticky)
  7575. (org-agenda-buffer-tmp-name (buffer-name))
  7576. (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
  7577. (0-arg (or current-prefix-arg (car args)))
  7578. (2-arg (nth 2 args))
  7579. (with-hour-p (nth 4 org-agenda-redo-command))
  7580. (newcmd (list 'org-agenda-list 0-arg date
  7581. (org-agenda-span-to-ndays
  7582. 2-arg (org-time-string-to-absolute date))
  7583. with-hour-p))
  7584. (newargs (cdr newcmd))
  7585. (inhibit-read-only t)
  7586. org-agenda-sticky)
  7587. (if (not (org-agenda-check-type t 'agenda))
  7588. (error "Not available in non-agenda views")
  7589. (add-text-properties (point-min) (point-max)
  7590. `(org-redo-cmd ,newcmd org-last-args ,newargs))
  7591. (org-agenda-redo)
  7592. (goto-char (point-min))
  7593. (while (not (or (= (or (get-text-property (point) 'day) 0) day)
  7594. (save-excursion (move-beginning-of-line 2) (eobp))))
  7595. (move-beginning-of-line 2))
  7596. (setq org-agenda-sticky org-agenda-sticky-orig
  7597. org-agenda-this-buffer-is-sticky org-agenda-sticky))))
  7598. (defun org-agenda-goto-today ()
  7599. "Go to today."
  7600. (interactive)
  7601. (org-agenda-check-type t 'agenda)
  7602. (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
  7603. (curspan (nth 2 args))
  7604. (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  7605. (cond
  7606. (tdpos (goto-char tdpos))
  7607. ((eq org-agenda-type 'agenda)
  7608. (let* ((sd (org-agenda-compute-starting-span
  7609. (org-today) (or curspan org-agenda-span)))
  7610. (org-agenda-overriding-arguments args))
  7611. (setf (nth 1 org-agenda-overriding-arguments) sd)
  7612. (org-agenda-redo)
  7613. (org-agenda-find-same-or-today-or-agenda)))
  7614. (t (error "Cannot find today")))))
  7615. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  7616. (goto-char
  7617. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  7618. (text-property-any (point-min) (point-max) 'org-today t)
  7619. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  7620. (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
  7621. (org-agenda-backward-block))
  7622. (point-min))))
  7623. (defun org-agenda-backward-block ()
  7624. "Move backward by one agenda block."
  7625. (interactive)
  7626. (org-agenda-forward-block 'backward))
  7627. (defun org-agenda-forward-block (&optional backward)
  7628. "Move forward by one agenda block.
  7629. When optional argument BACKWARD is set, go backward."
  7630. (interactive)
  7631. (cond ((not (derived-mode-p 'org-agenda-mode))
  7632. (user-error
  7633. "Cannot execute this command outside of org-agenda-mode buffers"))
  7634. ((looking-at (if backward "\\`" "\\'"))
  7635. (message "Already at the %s block" (if backward "first" "last")))
  7636. (t (let ((pos (prog1 (point)
  7637. (ignore-errors (if backward (backward-char 1)
  7638. (move-end-of-line 1)))))
  7639. (f (if backward
  7640. 'previous-single-property-change
  7641. 'next-single-property-change))
  7642. moved dest)
  7643. (while (and (setq dest (funcall
  7644. f (point) 'org-agenda-structural-header))
  7645. (not (get-text-property
  7646. (point) 'org-agenda-structural-header)))
  7647. (setq moved t)
  7648. (goto-char dest))
  7649. (if moved (move-beginning-of-line 1)
  7650. (goto-char (if backward (point-min) (point-max)))
  7651. (move-beginning-of-line 1)
  7652. (message "No %s block" (if backward "previous" "further")))))))
  7653. (defun org-agenda-later (arg)
  7654. "Go forward in time by the current span.
  7655. With prefix ARG, go forward that many times the current span."
  7656. (interactive "p")
  7657. (org-agenda-check-type t 'agenda)
  7658. (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
  7659. (span (or (nth 2 args) org-agenda-current-span))
  7660. (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
  7661. (greg (calendar-gregorian-from-absolute sd))
  7662. (cnt (org-get-at-bol 'org-day-cnt))
  7663. greg2)
  7664. (cond
  7665. ((numberp span)
  7666. (setq sd (+ (* span arg) sd)))
  7667. ((eq span 'day)
  7668. (setq sd (+ arg sd)))
  7669. ((eq span 'week)
  7670. (setq sd (+ (* 7 arg) sd)))
  7671. ((eq span 'fortnight)
  7672. (setq sd (+ (* 14 arg) sd)))
  7673. ((eq span 'month)
  7674. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  7675. sd (calendar-absolute-from-gregorian greg2))
  7676. (setcar greg2 (1+ (car greg2))))
  7677. ((eq span 'year)
  7678. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  7679. sd (calendar-absolute-from-gregorian greg2))
  7680. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
  7681. (t
  7682. (setq sd (+ (* span arg) sd))))
  7683. (let ((org-agenda-overriding-cmd
  7684. ;; `cmd' may have been set by `org-agenda-run-series' which
  7685. ;; uses `org-agenda-overriding-cmd' to decide whether
  7686. ;; overriding is allowed for `cmd'
  7687. (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
  7688. (org-agenda-overriding-arguments
  7689. (list (car args) sd span)))
  7690. (org-agenda-redo)
  7691. (org-agenda-find-same-or-today-or-agenda cnt))))
  7692. (defun org-agenda-earlier (arg)
  7693. "Go backward in time by the current span.
  7694. With prefix ARG, go backward that many times the current span."
  7695. (interactive "p")
  7696. (org-agenda-later (- arg)))
  7697. (defun org-agenda-view-mode-dispatch ()
  7698. "Call one of the view mode commands."
  7699. (interactive)
  7700. (org-unlogged-message
  7701. "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
  7702. time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
  7703. [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
  7704. (pcase (read-char-exclusive)
  7705. (?\ (call-interactively 'org-agenda-reset-view))
  7706. (?d (call-interactively 'org-agenda-day-view))
  7707. (?w (call-interactively 'org-agenda-week-view))
  7708. (?t (call-interactively 'org-agenda-fortnight-view))
  7709. (?m (call-interactively 'org-agenda-month-view))
  7710. (?y (call-interactively 'org-agenda-year-view))
  7711. (?l (call-interactively 'org-agenda-log-mode))
  7712. (?L (org-agenda-log-mode '(4)))
  7713. (?c (org-agenda-log-mode 'clockcheck))
  7714. ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
  7715. (?a (call-interactively 'org-agenda-archives-mode))
  7716. (?A (org-agenda-archives-mode 'files))
  7717. ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
  7718. ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
  7719. (?G (call-interactively 'org-agenda-toggle-time-grid))
  7720. (?D (call-interactively 'org-agenda-toggle-diary))
  7721. (?\! (call-interactively 'org-agenda-toggle-deadlines))
  7722. (?\[ (let ((org-agenda-include-inactive-timestamps t))
  7723. (org-agenda-check-type t 'agenda)
  7724. (org-agenda-redo))
  7725. (message "Display now includes inactive timestamps as well"))
  7726. (?q (message "Abort"))
  7727. (key (user-error "Invalid key: %s" key))))
  7728. (defun org-agenda-reset-view ()
  7729. "Switch to default view for agenda."
  7730. (interactive)
  7731. (org-agenda-change-time-span org-agenda-span))
  7732. (defun org-agenda-day-view (&optional day-of-month)
  7733. "Switch to daily view for agenda.
  7734. With argument DAY-OF-MONTH, switch to that day of the month."
  7735. (interactive "P")
  7736. (org-agenda-change-time-span 'day day-of-month))
  7737. (defun org-agenda-week-view (&optional iso-week)
  7738. "Switch to weekly view for agenda.
  7739. With argument ISO-WEEK, switch to the corresponding ISO week.
  7740. If ISO-WEEK has more then 2 digits, only the last two encode
  7741. the week. Any digits before this encode a year. So 200712
  7742. means week 12 of year 2007. Years ranging from 70 years ago
  7743. to 30 years in the future can also be written as 2-digit years."
  7744. (interactive "P")
  7745. (org-agenda-change-time-span 'week iso-week))
  7746. (defun org-agenda-fortnight-view (&optional iso-week)
  7747. "Switch to fortnightly view for agenda.
  7748. With argument ISO-WEEK, switch to the corresponding ISO week.
  7749. If ISO-WEEK has more then 2 digits, only the last two encode
  7750. the week. Any digits before this encode a year. So 200712
  7751. means week 12 of year 2007. Years ranging from 70 years ago
  7752. to 30 years in the future can also be written as 2-digit years."
  7753. (interactive "P")
  7754. (org-agenda-change-time-span 'fortnight iso-week))
  7755. (defun org-agenda-month-view (&optional month)
  7756. "Switch to monthly view for agenda.
  7757. With argument MONTH, switch to that month. If MONTH has more
  7758. then 2 digits, only the last two encode the month. Any digits
  7759. before this encode a year. So 200712 means December year 2007.
  7760. Years ranging from 70 years ago to 30 years in the future can
  7761. also be written as 2-digit years."
  7762. (interactive "P")
  7763. (org-agenda-change-time-span 'month month))
  7764. (defun org-agenda-year-view (&optional year)
  7765. "Switch to yearly view for agenda.
  7766. With argument YEAR, switch to that year. Years ranging from 70
  7767. years ago to 30 years in the future can also be written as
  7768. 2-digit years."
  7769. (interactive "P")
  7770. (when year
  7771. (setq year (org-small-year-to-year year)))
  7772. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  7773. (org-agenda-change-time-span 'year year)
  7774. (error "Abort")))
  7775. (defun org-agenda-change-time-span (span &optional n)
  7776. "Change the agenda view to SPAN.
  7777. SPAN may be `day', `week', `fortnight', `month', `year'."
  7778. (org-agenda-check-type t 'agenda)
  7779. (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
  7780. (curspan (nth 2 args)))
  7781. (when (and (not n) (equal curspan span))
  7782. (error "Viewing span is already \"%s\"" span))
  7783. (let* ((sd (or (org-get-at-bol 'day)
  7784. (nth 1 args)
  7785. org-starting-day))
  7786. (sd (org-agenda-compute-starting-span sd span n))
  7787. (org-agenda-overriding-cmd
  7788. (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
  7789. (org-agenda-overriding-arguments
  7790. (list (car args) sd span)))
  7791. (org-agenda-redo)
  7792. (org-agenda-find-same-or-today-or-agenda))
  7793. (org-agenda-set-mode-name)
  7794. (message "Switched to %s view" span)))
  7795. (defun org-agenda-compute-starting-span (sd span &optional n)
  7796. "Compute starting date for agenda.
  7797. SPAN may be `day', `week', `fortnight', `month', `year'. The return value
  7798. is a cons cell with the starting date and the number of days,
  7799. so that the date SD will be in that range."
  7800. (let* ((greg (calendar-gregorian-from-absolute sd))
  7801. (dg (nth 1 greg))
  7802. (mg (car greg))
  7803. (yg (nth 2 greg)))
  7804. (cond
  7805. ((eq span 'day)
  7806. (when n
  7807. (setq sd (+ (calendar-absolute-from-gregorian
  7808. (list mg 1 yg))
  7809. n -1))))
  7810. ((or (eq span 'week) (eq span 'fortnight))
  7811. (let* ((nt (calendar-day-of-week
  7812. (calendar-gregorian-from-absolute sd)))
  7813. (d (if org-agenda-start-on-weekday
  7814. (- nt org-agenda-start-on-weekday)
  7815. 0))
  7816. y1)
  7817. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  7818. (when n
  7819. (require 'cal-iso)
  7820. (when (> n 99)
  7821. (setq y1 (org-small-year-to-year (/ n 100))
  7822. n (mod n 100)))
  7823. (setq sd
  7824. (calendar-iso-to-absolute
  7825. (list n 1
  7826. (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
  7827. ((eq span 'month)
  7828. (let (y1)
  7829. (when (and n (> n 99))
  7830. (setq y1 (org-small-year-to-year (/ n 100))
  7831. n (mod n 100)))
  7832. (setq sd (calendar-absolute-from-gregorian
  7833. (list (or n mg) 1 (or y1 yg))))))
  7834. ((eq span 'year)
  7835. (setq sd (calendar-absolute-from-gregorian
  7836. (list 1 1 (or n yg))))))
  7837. sd))
  7838. (defun org-agenda-next-date-line (&optional arg)
  7839. "Jump to the next line indicating a date in agenda buffer."
  7840. (interactive "p")
  7841. (org-agenda-check-type t 'agenda)
  7842. (beginning-of-line 1)
  7843. ;; This does not work if user makes date format that starts with a blank
  7844. (when (looking-at-p "^\\S-") (forward-char 1))
  7845. (unless (re-search-forward "^\\S-" nil t arg)
  7846. (backward-char 1)
  7847. (error "No next date after this line in this buffer"))
  7848. (goto-char (match-beginning 0)))
  7849. (defun org-agenda-previous-date-line (&optional arg)
  7850. "Jump to the previous line indicating a date in agenda buffer."
  7851. (interactive "p")
  7852. (org-agenda-check-type t 'agenda)
  7853. (beginning-of-line 1)
  7854. (unless (re-search-backward "^\\S-" nil t arg)
  7855. (error "No previous date before this line in this buffer")))
  7856. ;; Initialize the highlight
  7857. (defvar org-hl (make-overlay 1 1))
  7858. (overlay-put org-hl 'face 'highlight)
  7859. (defun org-highlight (begin end &optional buffer)
  7860. "Highlight a region with overlay."
  7861. (move-overlay org-hl begin end (or buffer (current-buffer))))
  7862. (defun org-unhighlight ()
  7863. "Detach overlay INDEX."
  7864. (delete-overlay org-hl))
  7865. (defun org-unhighlight-once ()
  7866. "Remove the highlight from its position, and this function from the hook."
  7867. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  7868. (org-unhighlight))
  7869. (defvar org-agenda-pre-follow-window-conf nil)
  7870. (defun org-agenda-follow-mode ()
  7871. "Toggle follow mode in an agenda buffer."
  7872. (interactive)
  7873. (unless org-agenda-follow-mode
  7874. (setq org-agenda-pre-follow-window-conf
  7875. (current-window-configuration)))
  7876. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  7877. (unless org-agenda-follow-mode
  7878. (set-window-configuration org-agenda-pre-follow-window-conf))
  7879. (org-agenda-set-mode-name)
  7880. (org-agenda-do-context-action)
  7881. (message "Follow mode is %s"
  7882. (if org-agenda-follow-mode "on" "off")))
  7883. (defun org-agenda-entry-text-mode (&optional arg)
  7884. "Toggle entry text mode in an agenda buffer."
  7885. (interactive "P")
  7886. (if (or org-agenda-tag-filter
  7887. org-agenda-category-filter
  7888. org-agenda-regexp-filter
  7889. org-agenda-top-headline-filter)
  7890. (user-error "Can't show entry text in filtered views")
  7891. (setq org-agenda-entry-text-mode (or (integerp arg)
  7892. (not org-agenda-entry-text-mode)))
  7893. (org-agenda-entry-text-hide)
  7894. (and org-agenda-entry-text-mode
  7895. (let ((org-agenda-entry-text-maxlines
  7896. (if (integerp arg) arg org-agenda-entry-text-maxlines)))
  7897. (org-agenda-entry-text-show)))
  7898. (org-agenda-set-mode-name)
  7899. (message "Entry text mode is %s%s"
  7900. (if org-agenda-entry-text-mode "on" "off")
  7901. (if (not org-agenda-entry-text-mode) ""
  7902. (format " (maximum number of lines is %d)"
  7903. (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
  7904. (defun org-agenda-clockreport-mode ()
  7905. "Toggle clocktable mode in an agenda buffer."
  7906. (interactive)
  7907. (org-agenda-check-type t 'agenda)
  7908. (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
  7909. (org-agenda-set-mode-name)
  7910. (org-agenda-redo)
  7911. (message "Clocktable mode is %s"
  7912. (if org-agenda-clockreport-mode "on" "off")))
  7913. (defun org-agenda-log-mode (&optional special)
  7914. "Toggle log mode in an agenda buffer.
  7915. With argument SPECIAL, show all possible log items, not only the ones
  7916. configured in `org-agenda-log-mode-items'.
  7917. With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
  7918. log items, nothing else."
  7919. (interactive "P")
  7920. (org-agenda-check-type t 'agenda)
  7921. (setq org-agenda-show-log
  7922. (cond
  7923. ((equal special '(16)) 'only)
  7924. ((eq special 'clockcheck)
  7925. (if (eq org-agenda-show-log 'clockcheck)
  7926. nil 'clockcheck))
  7927. (special '(closed clock state))
  7928. (t (not org-agenda-show-log))))
  7929. (org-agenda-set-mode-name)
  7930. (org-agenda-redo)
  7931. (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
  7932. (defun org-agenda-archives-mode (&optional with-files)
  7933. "Toggle inclusion of items in trees marked with :ARCHIVE:.
  7934. When called with a prefix argument, include all archive files as well."
  7935. (interactive "P")
  7936. (setq org-agenda-archives-mode
  7937. (if with-files t (if org-agenda-archives-mode nil 'trees)))
  7938. (org-agenda-set-mode-name)
  7939. (org-agenda-redo)
  7940. (message
  7941. "%s"
  7942. (cond
  7943. ((eq org-agenda-archives-mode nil)
  7944. "No archives are included")
  7945. ((eq org-agenda-archives-mode 'trees)
  7946. (format "Trees with :%s: tag are included" org-archive-tag))
  7947. ((eq org-agenda-archives-mode t)
  7948. (format "Trees with :%s: tag and all active archive files are included"
  7949. org-archive-tag)))))
  7950. (defun org-agenda-toggle-diary ()
  7951. "Toggle diary inclusion in an agenda buffer."
  7952. (interactive)
  7953. (org-agenda-check-type t 'agenda)
  7954. (setq org-agenda-include-diary (not org-agenda-include-diary))
  7955. (org-agenda-redo)
  7956. (org-agenda-set-mode-name)
  7957. (message "Diary inclusion turned %s"
  7958. (if org-agenda-include-diary "on" "off")))
  7959. (defun org-agenda-toggle-deadlines ()
  7960. "Toggle inclusion of entries with a deadline in an agenda buffer."
  7961. (interactive)
  7962. (org-agenda-check-type t 'agenda)
  7963. (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
  7964. (org-agenda-redo)
  7965. (org-agenda-set-mode-name)
  7966. (message "Deadlines inclusion turned %s"
  7967. (if org-agenda-include-deadlines "on" "off")))
  7968. (defun org-agenda-toggle-time-grid ()
  7969. "Toggle time grid in an agenda buffer."
  7970. (interactive)
  7971. (org-agenda-check-type t 'agenda)
  7972. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  7973. (org-agenda-redo)
  7974. (org-agenda-set-mode-name)
  7975. (message "Time-grid turned %s"
  7976. (if org-agenda-use-time-grid "on" "off")))
  7977. (defun org-agenda-set-mode-name ()
  7978. "Set the mode name to indicate all the small mode settings."
  7979. (setq mode-name
  7980. (list "Org-Agenda"
  7981. (if (get 'org-agenda-files 'org-restrict) " []" "")
  7982. " "
  7983. '(:eval (org-agenda-span-name org-agenda-current-span))
  7984. (if org-agenda-follow-mode " Follow" "")
  7985. (if org-agenda-entry-text-mode " ETxt" "")
  7986. (if org-agenda-include-diary " Diary" "")
  7987. (if org-agenda-include-deadlines " Ddl" "")
  7988. (if org-agenda-use-time-grid " Grid" "")
  7989. (if (and (boundp 'org-habit-show-habits)
  7990. org-habit-show-habits) " Habit" "")
  7991. (cond
  7992. ((consp org-agenda-show-log) " LogAll")
  7993. ((eq org-agenda-show-log 'clockcheck) " ClkCk")
  7994. (org-agenda-show-log " Log")
  7995. (t ""))
  7996. (if (org-agenda-filter-any) " " "")
  7997. (if (or org-agenda-category-filter
  7998. (get 'org-agenda-category-filter :preset-filter))
  7999. '(:eval (propertize
  8000. (concat "["
  8001. (mapconcat
  8002. 'identity
  8003. (append
  8004. (get 'org-agenda-category-filter :preset-filter)
  8005. org-agenda-category-filter)
  8006. "")
  8007. "]")
  8008. 'face 'org-agenda-filter-category
  8009. 'help-echo "Category used in filtering")) "")
  8010. (if (or org-agenda-tag-filter
  8011. (get 'org-agenda-tag-filter :preset-filter))
  8012. '(:eval (propertize
  8013. (concat (mapconcat
  8014. 'identity
  8015. (append
  8016. (get 'org-agenda-tag-filter :preset-filter)
  8017. org-agenda-tag-filter)
  8018. ""))
  8019. 'face 'org-agenda-filter-tags
  8020. 'help-echo "Tags used in filtering")) "")
  8021. (if (or org-agenda-effort-filter
  8022. (get 'org-agenda-effort-filter :preset-filter))
  8023. '(:eval (propertize
  8024. (concat (mapconcat
  8025. 'identity
  8026. (append
  8027. (get 'org-agenda-effort-filter :preset-filter)
  8028. org-agenda-effort-filter)
  8029. ""))
  8030. 'face 'org-agenda-filter-effort
  8031. 'help-echo "Effort conditions used in filtering")) "")
  8032. (if (or org-agenda-regexp-filter
  8033. (get 'org-agenda-regexp-filter :preset-filter))
  8034. '(:eval (propertize
  8035. (concat (mapconcat
  8036. (lambda (x) (concat (substring x 0 1) "/" (substring x 1) "/"))
  8037. (append
  8038. (get 'org-agenda-regexp-filter :preset-filter)
  8039. org-agenda-regexp-filter)
  8040. ""))
  8041. 'face 'org-agenda-filter-regexp
  8042. 'help-echo "Regexp used in filtering")) "")
  8043. (if org-agenda-archives-mode
  8044. (if (eq org-agenda-archives-mode t)
  8045. " Archives"
  8046. (format " :%s:" org-archive-tag))
  8047. "")
  8048. (if org-agenda-clockreport-mode " Clock" "")))
  8049. (force-mode-line-update))
  8050. (defun org-agenda-update-agenda-type ()
  8051. "Update the agenda type after each command."
  8052. (setq org-agenda-type
  8053. (or (get-text-property (point) 'org-agenda-type)
  8054. (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
  8055. (defun org-agenda-next-line ()
  8056. "Move cursor to the next line, and show if follow mode is active."
  8057. (interactive)
  8058. (call-interactively 'next-line)
  8059. (org-agenda-do-context-action))
  8060. (defun org-agenda-previous-line ()
  8061. "Move cursor to the previous line, and show if follow-mode is active."
  8062. (interactive)
  8063. (call-interactively 'previous-line)
  8064. (org-agenda-do-context-action))
  8065. (defun org-agenda-next-item (n)
  8066. "Move cursor to next agenda item."
  8067. (interactive "p")
  8068. (let ((col (current-column)))
  8069. (dotimes (c n)
  8070. (when (next-single-property-change (point-at-eol) 'org-marker)
  8071. (move-end-of-line 1)
  8072. (goto-char (next-single-property-change (point) 'org-marker))))
  8073. (org-move-to-column col))
  8074. (org-agenda-do-context-action))
  8075. (defun org-agenda-previous-item (n)
  8076. "Move cursor to next agenda item."
  8077. (interactive "p")
  8078. (dotimes (c n)
  8079. (let ((col (current-column))
  8080. (goto (save-excursion
  8081. (move-end-of-line 0)
  8082. (previous-single-property-change (point) 'org-marker))))
  8083. (when goto (goto-char goto))
  8084. (org-move-to-column col)))
  8085. (org-agenda-do-context-action))
  8086. (defun org-agenda-do-context-action ()
  8087. "Show outline path and, maybe, follow mode window."
  8088. (let ((m (org-get-at-bol 'org-marker)))
  8089. (when (and (markerp m) (marker-buffer m))
  8090. (and org-agenda-follow-mode
  8091. (if org-agenda-follow-indirect
  8092. (org-agenda-tree-to-indirect-buffer nil)
  8093. (org-agenda-show)))
  8094. (and org-agenda-show-outline-path
  8095. (org-with-point-at m (org-display-outline-path t))))))
  8096. (defun org-agenda-show-tags ()
  8097. "Show the tags applicable to the current item."
  8098. (interactive)
  8099. (let* ((tags (org-get-at-bol 'tags)))
  8100. (if tags
  8101. (message "Tags are :%s:"
  8102. (org-no-properties (mapconcat 'identity tags ":")))
  8103. (message "No tags associated with this line"))))
  8104. (defun org-agenda-goto (&optional highlight)
  8105. "Go to the entry at point in the corresponding Org file."
  8106. (interactive)
  8107. (let* ((marker (or (org-get-at-bol 'org-marker)
  8108. (org-agenda-error)))
  8109. (buffer (marker-buffer marker))
  8110. (pos (marker-position marker)))
  8111. (switch-to-buffer-other-window buffer)
  8112. (widen)
  8113. (push-mark)
  8114. (goto-char pos)
  8115. (when (derived-mode-p 'org-mode)
  8116. (org-show-context 'agenda)
  8117. (recenter (/ (window-height) 2))
  8118. (org-back-to-heading t)
  8119. (let ((case-fold-search nil))
  8120. (when (re-search-forward org-complex-heading-regexp nil t)
  8121. (goto-char (match-beginning 4)))))
  8122. (run-hooks 'org-agenda-after-show-hook)
  8123. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  8124. (defvar org-agenda-after-show-hook nil
  8125. "Normal hook run after an item has been shown from the agenda.
  8126. Point is in the buffer where the item originated.")
  8127. (defun org-agenda-kill ()
  8128. "Kill the entry or subtree belonging to the current agenda entry."
  8129. (interactive)
  8130. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  8131. (let* ((bufname-orig (buffer-name))
  8132. (marker (or (org-get-at-bol 'org-marker)
  8133. (org-agenda-error)))
  8134. (buffer (marker-buffer marker))
  8135. (pos (marker-position marker))
  8136. (type (org-get-at-bol 'type))
  8137. dbeg dend (n 0))
  8138. (org-with-remote-undo buffer
  8139. (with-current-buffer buffer
  8140. (save-excursion
  8141. (goto-char pos)
  8142. (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
  8143. (setq dbeg (progn (org-back-to-heading t) (point))
  8144. dend (org-end-of-subtree t t))
  8145. (setq dbeg (point-at-bol)
  8146. dend (min (point-max) (1+ (point-at-eol)))))
  8147. (goto-char dbeg)
  8148. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  8149. (when (or (eq t org-agenda-confirm-kill)
  8150. (and (numberp org-agenda-confirm-kill)
  8151. (> n org-agenda-confirm-kill)))
  8152. (let ((win-conf (current-window-configuration)))
  8153. (unwind-protect
  8154. (and
  8155. (prog2
  8156. (org-agenda-tree-to-indirect-buffer nil)
  8157. (not (y-or-n-p
  8158. (format "Delete entry with %d lines in buffer \"%s\"? "
  8159. n (buffer-name buffer))))
  8160. (kill-buffer org-last-indirect-buffer))
  8161. (error "Abort"))
  8162. (set-window-configuration win-conf))))
  8163. (let ((org-agenda-buffer-name bufname-orig))
  8164. (org-remove-subtree-entries-from-agenda buffer dbeg dend))
  8165. (with-current-buffer buffer (delete-region dbeg dend))
  8166. (message "Agenda item and source killed"))))
  8167. (defvar org-archive-default-command) ; defined in org-archive.el
  8168. (defun org-agenda-archive-default ()
  8169. "Archive the entry or subtree belonging to the current agenda entry."
  8170. (interactive)
  8171. (require 'org-archive)
  8172. (org-agenda-archive-with org-archive-default-command))
  8173. (defun org-agenda-archive-default-with-confirmation ()
  8174. "Archive the entry or subtree belonging to the current agenda entry."
  8175. (interactive)
  8176. (require 'org-archive)
  8177. (org-agenda-archive-with org-archive-default-command 'confirm))
  8178. (defun org-agenda-archive ()
  8179. "Archive the entry or subtree belonging to the current agenda entry."
  8180. (interactive)
  8181. (org-agenda-archive-with 'org-archive-subtree))
  8182. (defun org-agenda-archive-to-archive-sibling ()
  8183. "Move the entry to the archive sibling."
  8184. (interactive)
  8185. (org-agenda-archive-with 'org-archive-to-archive-sibling))
  8186. (defun org-agenda-archive-with (cmd &optional confirm)
  8187. "Move the entry to the archive sibling."
  8188. (interactive)
  8189. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  8190. (let* ((bufname-orig (buffer-name))
  8191. (marker (or (org-get-at-bol 'org-marker)
  8192. (org-agenda-error)))
  8193. (buffer (marker-buffer marker))
  8194. (pos (marker-position marker)))
  8195. (org-with-remote-undo buffer
  8196. (with-current-buffer buffer
  8197. (if (derived-mode-p 'org-mode)
  8198. (if (and confirm
  8199. (not (y-or-n-p "Archive this subtree or entry? ")))
  8200. (error "Abort")
  8201. (save-window-excursion
  8202. (goto-char pos)
  8203. (let ((org-agenda-buffer-name bufname-orig))
  8204. (org-remove-subtree-entries-from-agenda))
  8205. (org-back-to-heading t)
  8206. (funcall cmd)))
  8207. (error "Archiving works only in Org files"))))))
  8208. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  8209. "Remove all lines in the agenda that correspond to a given subtree.
  8210. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  8211. If this information is not given, the function uses the tree at point."
  8212. (let ((buf (or buf (current-buffer))) m p)
  8213. (save-excursion
  8214. (unless (and beg end)
  8215. (org-back-to-heading t)
  8216. (setq beg (point))
  8217. (org-end-of-subtree t)
  8218. (setq end (point)))
  8219. (set-buffer (get-buffer org-agenda-buffer-name))
  8220. (save-excursion
  8221. (goto-char (point-max))
  8222. (beginning-of-line 1)
  8223. (while (not (bobp))
  8224. (when (and (setq m (org-get-at-bol 'org-marker))
  8225. (equal buf (marker-buffer m))
  8226. (setq p (marker-position m))
  8227. (>= p beg)
  8228. (< p end))
  8229. (let ((inhibit-read-only t))
  8230. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  8231. (beginning-of-line 0))))))
  8232. (defun org-agenda-refile (&optional goto rfloc no-update)
  8233. "Refile the item at point.
  8234. When called with `\\[universal-argument] \\[universal-argument]', \
  8235. go to the location of the last
  8236. refiled item.
  8237. When called with `\\[universal-argument] \\[universal-argument] \
  8238. \\[universal-argument]' prefix or when GOTO is 0, clear
  8239. the refile cache.
  8240. RFLOC can be a refile location obtained in a different way.
  8241. When NO-UPDATE is non-nil, don't redo the agenda buffer."
  8242. (interactive "P")
  8243. (cond
  8244. ((member goto '(0 (64)))
  8245. (org-refile-cache-clear))
  8246. ((equal goto '(16))
  8247. (org-refile-goto-last-stored))
  8248. (t
  8249. (let* ((buffer-orig (buffer-name))
  8250. (marker (or (org-get-at-bol 'org-hd-marker)
  8251. (org-agenda-error)))
  8252. (buffer (marker-buffer marker))
  8253. (pos (marker-position marker))
  8254. (rfloc (or rfloc
  8255. (org-refile-get-location
  8256. (if goto "Goto" "Refile to") buffer
  8257. org-refile-allow-creating-parent-nodes))))
  8258. (with-current-buffer buffer
  8259. (org-with-wide-buffer
  8260. (goto-char marker)
  8261. (let ((org-agenda-buffer-name buffer-orig))
  8262. (org-remove-subtree-entries-from-agenda))
  8263. (org-refile goto buffer rfloc))))
  8264. (unless no-update (org-agenda-redo)))))
  8265. (defun org-agenda-open-link (&optional arg)
  8266. "Open the link(s) in the current entry, if any.
  8267. This looks for a link in the displayed line in the agenda.
  8268. It also looks at the text of the entry itself."
  8269. (interactive "P")
  8270. (let* ((marker (or (org-get-at-bol 'org-hd-marker)
  8271. (org-get-at-bol 'org-marker)))
  8272. (buffer (and marker (marker-buffer marker)))
  8273. (prefix (buffer-substring (point-at-bol) (point-at-eol)))
  8274. (lkall (and buffer (org-offer-links-in-entry
  8275. buffer marker arg prefix)))
  8276. (lk0 (car lkall))
  8277. (lk (if (stringp lk0) (list lk0) lk0))
  8278. (lkend (cdr lkall))
  8279. trg)
  8280. (cond
  8281. ((and buffer lk)
  8282. (mapcar (lambda(l)
  8283. (with-current-buffer buffer
  8284. (setq trg (and (string-match org-link-bracket-re l)
  8285. (match-string 1 l)))
  8286. (if (or (not trg) (string-match org-link-any-re trg))
  8287. (org-with-wide-buffer
  8288. (goto-char marker)
  8289. (when (search-forward l nil lkend)
  8290. (goto-char (match-beginning 0))
  8291. (org-open-at-point)))
  8292. ;; This is an internal link, widen the buffer
  8293. (switch-to-buffer-other-window buffer)
  8294. (widen)
  8295. (goto-char marker)
  8296. (when (search-forward l nil lkend)
  8297. (goto-char (match-beginning 0))
  8298. (org-open-at-point)))))
  8299. lk))
  8300. ((or (org-in-regexp (concat "\\(" org-link-bracket-re "\\)"))
  8301. (save-excursion
  8302. (beginning-of-line 1)
  8303. (looking-at (concat ".*?\\(" org-link-bracket-re "\\)"))))
  8304. (org-link-open-from-string (match-string 1)))
  8305. (t (message "No link to open here")))))
  8306. (defun org-agenda-copy-local-variable (var)
  8307. "Get a variable from a referenced buffer and install it here."
  8308. (let ((m (org-get-at-bol 'org-marker)))
  8309. (when (and m (buffer-live-p (marker-buffer m)))
  8310. (set (make-local-variable var)
  8311. (with-current-buffer (marker-buffer m)
  8312. (symbol-value var))))))
  8313. (defun org-agenda-switch-to (&optional delete-other-windows)
  8314. "Go to the Org mode file which contains the item at point.
  8315. When optional argument DELETE-OTHER-WINDOWS is non-nil, the
  8316. displayed Org file fills the frame."
  8317. (interactive)
  8318. (if (and org-return-follows-link
  8319. (not (org-get-at-bol 'org-marker))
  8320. (org-in-regexp org-link-bracket-re))
  8321. (org-link-open-from-string (match-string 0))
  8322. (let* ((marker (or (org-get-at-bol 'org-marker)
  8323. (org-agenda-error)))
  8324. (buffer (marker-buffer marker))
  8325. (pos (marker-position marker)))
  8326. (unless buffer (user-error "Trying to switch to non-existent buffer"))
  8327. (pop-to-buffer-same-window buffer)
  8328. (when delete-other-windows (delete-other-windows))
  8329. (widen)
  8330. (goto-char pos)
  8331. (when (derived-mode-p 'org-mode)
  8332. (org-show-context 'agenda)
  8333. (run-hooks 'org-agenda-after-show-hook)))))
  8334. (defun org-agenda-goto-mouse (ev)
  8335. "Go to the Org file which contains the item at the mouse click."
  8336. (interactive "e")
  8337. (mouse-set-point ev)
  8338. (org-agenda-goto))
  8339. (defun org-agenda-show (&optional full-entry)
  8340. "Display the Org file which contains the item at point.
  8341. With prefix argument FULL-ENTRY, make the entire entry visible
  8342. if it was hidden in the outline."
  8343. (interactive "P")
  8344. (let ((win (selected-window)))
  8345. (org-agenda-goto t)
  8346. (when full-entry (org-show-entry))
  8347. (select-window win)))
  8348. (defvar org-agenda-show-window nil)
  8349. (defun org-agenda-show-and-scroll-up (&optional arg)
  8350. "Display the Org file which contains the item at point.
  8351. When called repeatedly, scroll the window that is displaying the buffer.
  8352. With a `\\[universal-argument]' prefix argument, display the item, but \
  8353. fold drawers."
  8354. (interactive "P")
  8355. (let ((win (selected-window)))
  8356. (if (and (window-live-p org-agenda-show-window)
  8357. (eq this-command last-command))
  8358. (progn
  8359. (select-window org-agenda-show-window)
  8360. (ignore-errors (scroll-up)))
  8361. (org-agenda-goto t)
  8362. (org-show-entry)
  8363. (if arg (org-cycle-hide-drawers 'children)
  8364. (org-with-wide-buffer
  8365. (narrow-to-region (org-entry-beginning-position)
  8366. (org-entry-end-position))
  8367. (org-show-all '(drawers))))
  8368. (when arg )
  8369. (setq org-agenda-show-window (selected-window)))
  8370. (select-window win)))
  8371. (defun org-agenda-show-scroll-down ()
  8372. "Scroll down the window showing the agenda."
  8373. (interactive)
  8374. (let ((win (selected-window)))
  8375. (when (window-live-p org-agenda-show-window)
  8376. (select-window org-agenda-show-window)
  8377. (ignore-errors (scroll-down))
  8378. (select-window win))))
  8379. (defun org-agenda-show-1 (&optional more)
  8380. "Display the Org file which contains the item at point.
  8381. The prefix arg selects the amount of information to display:
  8382. 0 hide the subtree
  8383. 1 just show the entry according to defaults.
  8384. 2 show the children view
  8385. 3 show the subtree view
  8386. 4 show the entire subtree and any LOGBOOK drawers
  8387. 5 show the entire subtree and any drawers
  8388. With prefix argument FULL-ENTRY, make the entire entry visible
  8389. if it was hidden in the outline."
  8390. (interactive "p")
  8391. (let ((win (selected-window)))
  8392. (org-agenda-goto t)
  8393. (org-back-to-heading)
  8394. (set-window-start (selected-window) (point-at-bol))
  8395. (cond
  8396. ((= more 0)
  8397. (org-flag-subtree t)
  8398. (save-excursion
  8399. (org-back-to-heading)
  8400. (run-hook-with-args 'org-cycle-hook 'folded))
  8401. (message "Remote: FOLDED"))
  8402. ((and (called-interactively-p 'any) (= more 1))
  8403. (message "Remote: show with default settings"))
  8404. ((= more 2)
  8405. (outline-show-entry)
  8406. (org-show-children)
  8407. (save-excursion
  8408. (org-back-to-heading)
  8409. (run-hook-with-args 'org-cycle-hook 'children))
  8410. (message "Remote: CHILDREN"))
  8411. ((= more 3)
  8412. (outline-show-subtree)
  8413. (save-excursion
  8414. (org-back-to-heading)
  8415. (run-hook-with-args 'org-cycle-hook 'subtree))
  8416. (message "Remote: SUBTREE"))
  8417. ((= more 4)
  8418. (outline-show-subtree)
  8419. (save-excursion
  8420. (org-back-to-heading)
  8421. (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
  8422. (message "Remote: SUBTREE AND LOGBOOK"))
  8423. ((> more 4)
  8424. (outline-show-subtree)
  8425. (message "Remote: SUBTREE AND ALL DRAWERS")))
  8426. (select-window win)))
  8427. (defvar org-agenda-cycle-counter nil)
  8428. (defun org-agenda-cycle-show (&optional n)
  8429. "Show the current entry in another window, with default settings.
  8430. Default settings are taken from `org-show-context-detail'. When
  8431. use repeatedly in immediate succession, the remote entry will
  8432. cycle through visibility
  8433. children -> subtree -> folded
  8434. When called with a numeric prefix arg, that arg will be passed through to
  8435. `org-agenda-show-1'. For the interpretation of that argument, see the
  8436. docstring of `org-agenda-show-1'."
  8437. (interactive "P")
  8438. (if (integerp n)
  8439. (setq org-agenda-cycle-counter n)
  8440. (if (not (eq last-command this-command))
  8441. (setq org-agenda-cycle-counter 1)
  8442. (if (equal org-agenda-cycle-counter 0)
  8443. (setq org-agenda-cycle-counter 2)
  8444. (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
  8445. (when (> org-agenda-cycle-counter 3)
  8446. (setq org-agenda-cycle-counter 0)))))
  8447. (org-agenda-show-1 org-agenda-cycle-counter))
  8448. (defun org-agenda-recenter (arg)
  8449. "Display the Org file which contains the item at point and recenter."
  8450. (interactive "P")
  8451. (let ((win (selected-window)))
  8452. (org-agenda-goto t)
  8453. (recenter arg)
  8454. (select-window win)))
  8455. (defun org-agenda-show-mouse (ev)
  8456. "Display the Org file which contains the item at the mouse click."
  8457. (interactive "e")
  8458. (mouse-set-point ev)
  8459. (org-agenda-show))
  8460. (defun org-agenda-check-no-diary ()
  8461. "Check if the entry is a diary link and abort if yes."
  8462. (when (org-get-at-bol 'org-agenda-diary-link)
  8463. (org-agenda-error)))
  8464. (defun org-agenda-error ()
  8465. "Throw an error when a command is not allowed in the agenda."
  8466. (user-error "Command not allowed in this line"))
  8467. (defun org-agenda-tree-to-indirect-buffer (arg)
  8468. "Show the subtree corresponding to the current entry in an indirect buffer.
  8469. This calls the command `org-tree-to-indirect-buffer' from the original buffer.
  8470. With a numerical prefix ARG, go up to this level and then take that tree.
  8471. With a negative numeric ARG, go up by this number of levels.
  8472. With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
  8473. i.e. don't use
  8474. the dedicated frame."
  8475. (interactive "P")
  8476. (if current-prefix-arg
  8477. (org-agenda-do-tree-to-indirect-buffer arg)
  8478. (let ((agenda-buffer (buffer-name))
  8479. (agenda-window (selected-window))
  8480. (indirect-window
  8481. (and org-last-indirect-buffer
  8482. (get-buffer-window org-last-indirect-buffer))))
  8483. (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
  8484. (unless (or (eq org-indirect-buffer-display 'new-frame)
  8485. (eq org-indirect-buffer-display 'dedicated-frame))
  8486. (unwind-protect
  8487. (unless (and indirect-window (window-live-p indirect-window))
  8488. (setq indirect-window (split-window agenda-window)))
  8489. (and indirect-window (select-window indirect-window))
  8490. (switch-to-buffer org-last-indirect-buffer :norecord)
  8491. (fit-window-to-buffer indirect-window)))
  8492. (select-window (get-buffer-window agenda-buffer))
  8493. (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
  8494. (defun org-agenda-do-tree-to-indirect-buffer (arg)
  8495. "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
  8496. (org-agenda-check-no-diary)
  8497. (let* ((marker (or (org-get-at-bol 'org-marker)
  8498. (org-agenda-error)))
  8499. (buffer (marker-buffer marker))
  8500. (pos (marker-position marker)))
  8501. (with-current-buffer buffer
  8502. (save-excursion
  8503. (goto-char pos)
  8504. (org-tree-to-indirect-buffer arg)))))
  8505. (defvar org-last-heading-marker (make-marker)
  8506. "Marker pointing to the headline that last changed its TODO state
  8507. by a remote command from the agenda.")
  8508. (defun org-agenda-todo-nextset ()
  8509. "Switch TODO entry to next sequence."
  8510. (interactive)
  8511. (org-agenda-todo 'nextset))
  8512. (defun org-agenda-todo-previousset ()
  8513. "Switch TODO entry to previous sequence."
  8514. (interactive)
  8515. (org-agenda-todo 'previousset))
  8516. (defun org-agenda-todo (&optional arg)
  8517. "Cycle TODO state of line at point, also in Org file.
  8518. This changes the line at point, all other lines in the agenda referring to
  8519. the same tree node, and the headline of the tree node in the Org file."
  8520. (interactive "P")
  8521. (org-agenda-check-no-diary)
  8522. (let* ((col (current-column))
  8523. (marker (or (org-get-at-bol 'org-marker)
  8524. (org-agenda-error)))
  8525. (buffer (marker-buffer marker))
  8526. (pos (marker-position marker))
  8527. (hdmarker (org-get-at-bol 'org-hd-marker))
  8528. (todayp (org-agenda-today-p (org-get-at-bol 'day)))
  8529. (inhibit-read-only t)
  8530. org-loop-over-headlines-in-active-region
  8531. org-agenda-headline-snapshot-before-repeat newhead just-one)
  8532. (org-with-remote-undo buffer
  8533. (with-current-buffer buffer
  8534. (widen)
  8535. (goto-char pos)
  8536. (org-show-context 'agenda)
  8537. (let ((current-prefix-arg arg))
  8538. (call-interactively 'org-todo))
  8539. (and (bolp) (forward-char 1))
  8540. (setq newhead (org-get-heading))
  8541. (when (and (bound-and-true-p
  8542. org-agenda-headline-snapshot-before-repeat)
  8543. (not (equal org-agenda-headline-snapshot-before-repeat
  8544. newhead))
  8545. todayp)
  8546. (setq newhead org-agenda-headline-snapshot-before-repeat
  8547. just-one t))
  8548. (save-excursion
  8549. (org-back-to-heading)
  8550. (move-marker org-last-heading-marker (point))))
  8551. (beginning-of-line 1)
  8552. (save-window-excursion
  8553. (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
  8554. (when (bound-and-true-p org-clock-out-when-done)
  8555. (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
  8556. newhead)
  8557. (org-agenda-unmark-clocking-task))
  8558. (org-move-to-column col)
  8559. (org-agenda-mark-clocking-task))))
  8560. (defun org-agenda-add-note (&optional arg)
  8561. "Add a time-stamped note to the entry at point."
  8562. (interactive "P")
  8563. (org-agenda-check-no-diary)
  8564. (let* ((marker (or (org-get-at-bol 'org-marker)
  8565. (org-agenda-error)))
  8566. (buffer (marker-buffer marker))
  8567. (pos (marker-position marker))
  8568. (hdmarker (org-get-at-bol 'org-hd-marker))
  8569. (inhibit-read-only t))
  8570. (with-current-buffer buffer
  8571. (widen)
  8572. (goto-char pos)
  8573. (org-show-context 'agenda)
  8574. (org-add-note))))
  8575. (defun org-agenda-change-all-lines (newhead hdmarker
  8576. &optional fixface just-this)
  8577. "Change all lines in the agenda buffer which match HDMARKER.
  8578. The new content of the line will be NEWHEAD (as modified by
  8579. `org-agenda-format-item'). HDMARKER is checked with
  8580. `equal' against all `org-hd-marker' text properties in the file.
  8581. If FIXFACE is non-nil, the face of each item is modified according to
  8582. the new TODO state.
  8583. If JUST-THIS is non-nil, change just the current line, not all.
  8584. If FORCE-TAGS is non-nil, the car of it returns the new tags."
  8585. (let* ((inhibit-read-only t)
  8586. (line (org-current-line))
  8587. (org-agenda-buffer (current-buffer))
  8588. (thetags (with-current-buffer (marker-buffer hdmarker)
  8589. (org-get-tags hdmarker)))
  8590. props m pl undone-face done-face finish new dotime level cat tags)
  8591. (save-excursion
  8592. (goto-char (point-max))
  8593. (beginning-of-line 1)
  8594. (while (not finish)
  8595. (setq finish (bobp))
  8596. (when (and (setq m (org-get-at-bol 'org-hd-marker))
  8597. (or (not just-this) (= (org-current-line) line))
  8598. (equal m hdmarker))
  8599. (setq props (text-properties-at (point))
  8600. dotime (org-get-at-bol 'dotime)
  8601. cat (org-agenda-get-category)
  8602. level (org-get-at-bol 'level)
  8603. tags thetags
  8604. new
  8605. (let ((org-prefix-format-compiled
  8606. (or (get-text-property (min (1- (point-max)) (point)) 'format)
  8607. org-prefix-format-compiled))
  8608. (extra (org-get-at-bol 'extra)))
  8609. (with-current-buffer (marker-buffer hdmarker)
  8610. (org-with-wide-buffer
  8611. (org-agenda-format-item extra newhead level cat tags dotime))))
  8612. pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
  8613. undone-face (org-get-at-bol 'undone-face)
  8614. done-face (org-get-at-bol 'done-face))
  8615. (beginning-of-line 1)
  8616. (cond
  8617. ((equal new "") (delete-region (point) (line-beginning-position 2)))
  8618. ((looking-at ".*")
  8619. ;; When replacing the whole line, preserve bulk mark
  8620. ;; overlay, if any.
  8621. (let ((mark (catch :overlay
  8622. (dolist (o (overlays-in (point) (+ 2 (point))))
  8623. (when (eq (overlay-get o 'type)
  8624. 'org-marked-entry-overlay)
  8625. (throw :overlay o))))))
  8626. (replace-match new t t)
  8627. (beginning-of-line)
  8628. (when mark (move-overlay mark (point) (+ 2 (point)))))
  8629. (add-text-properties (point-at-bol) (point-at-eol) props)
  8630. (when fixface
  8631. (add-text-properties
  8632. (point-at-bol) (point-at-eol)
  8633. (list 'face
  8634. (if org-last-todo-state-is-todo
  8635. undone-face done-face))))
  8636. (org-agenda-highlight-todo 'line)
  8637. (beginning-of-line 1))
  8638. (t (error "Line update did not work")))
  8639. (save-restriction
  8640. (narrow-to-region (point-at-bol) (point-at-eol))
  8641. (org-agenda-finalize)))
  8642. (beginning-of-line 0)))))
  8643. (defun org-agenda-align-tags (&optional line)
  8644. "Align all tags in agenda items to `org-agenda-tags-column'.
  8645. When optional argument LINE is non-nil, align tags only on the
  8646. current line."
  8647. (let ((inhibit-read-only t)
  8648. (org-agenda-tags-column (if (eq 'auto org-agenda-tags-column)
  8649. (- (window-text-width))
  8650. org-agenda-tags-column))
  8651. (end (and line (line-end-position)))
  8652. l c)
  8653. (save-excursion
  8654. (goto-char (if line (line-beginning-position) (point-min)))
  8655. (while (re-search-forward org-tag-group-re end t)
  8656. (add-text-properties
  8657. (match-beginning 1) (match-end 1)
  8658. (list 'face (delq nil (let ((prop (get-text-property
  8659. (match-beginning 1) 'face)))
  8660. (or (listp prop) (setq prop (list prop)))
  8661. (if (memq 'org-tag prop)
  8662. prop
  8663. (cons 'org-tag prop))))))
  8664. (setq l (string-width (match-string 1))
  8665. c (if (< org-agenda-tags-column 0)
  8666. (- (abs org-agenda-tags-column) l)
  8667. org-agenda-tags-column))
  8668. (goto-char (match-beginning 1))
  8669. (delete-region (save-excursion (skip-chars-backward " \t") (point))
  8670. (point))
  8671. (insert (org-add-props
  8672. (make-string (max 1 (- c (current-column))) ?\s)
  8673. (plist-put (copy-sequence (text-properties-at (point)))
  8674. 'face nil))))
  8675. (goto-char (point-min))
  8676. (org-font-lock-add-tag-faces (point-max)))))
  8677. (defun org-agenda-priority-up ()
  8678. "Increase the priority of line at point, also in Org file."
  8679. (interactive)
  8680. (org-agenda-priority 'up))
  8681. (defun org-agenda-priority-down ()
  8682. "Decrease the priority of line at point, also in Org file."
  8683. (interactive)
  8684. (org-agenda-priority 'down))
  8685. (defun org-agenda-priority (&optional force-direction)
  8686. "Set the priority of line at point, also in Org file.
  8687. This changes the line at point, all other lines in the agenda referring to
  8688. the same tree node, and the headline of the tree node in the Org file.
  8689. Called with a universal prefix arg, show the priority instead of setting it."
  8690. (interactive "P")
  8691. (if (equal force-direction '(4))
  8692. (org-show-priority)
  8693. (unless org-enable-priority-commands
  8694. (error "Priority commands are disabled"))
  8695. (org-agenda-check-no-diary)
  8696. (let* ((col (current-column))
  8697. (marker (or (org-get-at-bol 'org-marker)
  8698. (org-agenda-error)))
  8699. (hdmarker (org-get-at-bol 'org-hd-marker))
  8700. (buffer (marker-buffer hdmarker))
  8701. (pos (marker-position hdmarker))
  8702. (inhibit-read-only t)
  8703. newhead)
  8704. (org-with-remote-undo buffer
  8705. (with-current-buffer buffer
  8706. (widen)
  8707. (goto-char pos)
  8708. (org-show-context 'agenda)
  8709. (org-priority force-direction)
  8710. (end-of-line 1)
  8711. (setq newhead (org-get-heading)))
  8712. (org-agenda-change-all-lines newhead hdmarker)
  8713. (org-move-to-column col)))))
  8714. ;; FIXME: should fix the tags property of the agenda line.
  8715. (defun org-agenda-set-tags (&optional tag onoff)
  8716. "Set tags for the current headline."
  8717. (interactive)
  8718. (org-agenda-check-no-diary)
  8719. (if (and (org-region-active-p) (called-interactively-p 'any))
  8720. (call-interactively 'org-change-tag-in-region)
  8721. (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
  8722. (org-agenda-error)))
  8723. (buffer (marker-buffer hdmarker))
  8724. (pos (marker-position hdmarker))
  8725. (inhibit-read-only t)
  8726. newhead)
  8727. (org-with-remote-undo buffer
  8728. (with-current-buffer buffer
  8729. (widen)
  8730. (goto-char pos)
  8731. (org-show-context 'agenda)
  8732. (if tag
  8733. (org-toggle-tag tag onoff)
  8734. (call-interactively #'org-set-tags-command))
  8735. (end-of-line 1)
  8736. (setq newhead (org-get-heading)))
  8737. (org-agenda-change-all-lines newhead hdmarker)
  8738. (beginning-of-line 1)))))
  8739. (defun org-agenda-set-property ()
  8740. "Set a property for the current headline."
  8741. (interactive)
  8742. (org-agenda-check-no-diary)
  8743. (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
  8744. (org-agenda-error)))
  8745. (buffer (marker-buffer hdmarker))
  8746. (pos (marker-position hdmarker))
  8747. (inhibit-read-only t)
  8748. newhead)
  8749. (org-with-remote-undo buffer
  8750. (with-current-buffer buffer
  8751. (widen)
  8752. (goto-char pos)
  8753. (org-show-context 'agenda)
  8754. (call-interactively 'org-set-property)))))
  8755. (defun org-agenda-set-effort ()
  8756. "Set the effort property for the current headline."
  8757. (interactive)
  8758. (org-agenda-check-no-diary)
  8759. (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
  8760. (org-agenda-error)))
  8761. (buffer (marker-buffer hdmarker))
  8762. (pos (marker-position hdmarker))
  8763. (inhibit-read-only t)
  8764. newhead)
  8765. (org-with-remote-undo buffer
  8766. (with-current-buffer buffer
  8767. (widen)
  8768. (goto-char pos)
  8769. (org-show-context 'agenda)
  8770. (call-interactively 'org-set-effort)
  8771. (end-of-line 1)
  8772. (setq newhead (org-get-heading)))
  8773. (org-agenda-change-all-lines newhead hdmarker))))
  8774. (defun org-agenda-toggle-archive-tag ()
  8775. "Toggle the archive tag for the current entry."
  8776. (interactive)
  8777. (org-agenda-check-no-diary)
  8778. (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
  8779. (org-agenda-error)))
  8780. (buffer (marker-buffer hdmarker))
  8781. (pos (marker-position hdmarker))
  8782. (inhibit-read-only t)
  8783. newhead)
  8784. (org-with-remote-undo buffer
  8785. (with-current-buffer buffer
  8786. (widen)
  8787. (goto-char pos)
  8788. (org-show-context 'agenda)
  8789. (call-interactively 'org-toggle-archive-tag)
  8790. (end-of-line 1)
  8791. (setq newhead (org-get-heading)))
  8792. (org-agenda-change-all-lines newhead hdmarker)
  8793. (beginning-of-line 1))))
  8794. (defun org-agenda-do-date-later (arg)
  8795. (interactive "P")
  8796. (cond
  8797. ((or (equal arg '(16))
  8798. (memq last-command
  8799. '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
  8800. (setq this-command 'org-agenda-date-later-minutes)
  8801. (org-agenda-date-later-minutes 1))
  8802. ((or (equal arg '(4))
  8803. (memq last-command
  8804. '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
  8805. (setq this-command 'org-agenda-date-later-hours)
  8806. (org-agenda-date-later-hours 1))
  8807. (t
  8808. (org-agenda-date-later (prefix-numeric-value arg)))))
  8809. (defun org-agenda-do-date-earlier (arg)
  8810. (interactive "P")
  8811. (cond
  8812. ((or (equal arg '(16))
  8813. (memq last-command
  8814. '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
  8815. (setq this-command 'org-agenda-date-earlier-minutes)
  8816. (org-agenda-date-earlier-minutes 1))
  8817. ((or (equal arg '(4))
  8818. (memq last-command
  8819. '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
  8820. (setq this-command 'org-agenda-date-earlier-hours)
  8821. (org-agenda-date-earlier-hours 1))
  8822. (t
  8823. (org-agenda-date-earlier (prefix-numeric-value arg)))))
  8824. (defun org-agenda-date-later (arg &optional what)
  8825. "Change the date of this item to ARG day(s) later."
  8826. (interactive "p")
  8827. (org-agenda-check-type t 'agenda)
  8828. (org-agenda-check-no-diary)
  8829. (let* ((marker (or (org-get-at-bol 'org-marker)
  8830. (org-agenda-error)))
  8831. (buffer (marker-buffer marker))
  8832. (pos (marker-position marker))
  8833. cdate today)
  8834. (org-with-remote-undo buffer
  8835. (with-current-buffer buffer
  8836. (widen)
  8837. (goto-char pos)
  8838. (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
  8839. (when (and org-agenda-move-date-from-past-immediately-to-today
  8840. (equal arg 1)
  8841. (or (not what) (eq what 'day))
  8842. (not (save-match-data (org-at-date-range-p))))
  8843. (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
  8844. cdate (calendar-absolute-from-gregorian
  8845. (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
  8846. today (org-today))
  8847. (when (> today cdate)
  8848. ;; immediately shift to today
  8849. (setq arg (- today cdate))))
  8850. (org-timestamp-change arg (or what 'day))
  8851. (when (and (org-at-date-range-p)
  8852. (re-search-backward org-tr-regexp-both (point-at-bol)))
  8853. (let ((end org-last-changed-timestamp))
  8854. (org-timestamp-change arg (or what 'day))
  8855. (setq org-last-changed-timestamp
  8856. (concat org-last-changed-timestamp "--" end)))))
  8857. (org-agenda-show-new-time marker org-last-changed-timestamp))
  8858. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  8859. (defun org-agenda-date-earlier (arg &optional what)
  8860. "Change the date of this item to ARG day(s) earlier."
  8861. (interactive "p")
  8862. (org-agenda-date-later (- arg) what))
  8863. (defun org-agenda-date-later-minutes (arg)
  8864. "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
  8865. (interactive "p")
  8866. (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
  8867. (org-agenda-date-later arg 'minute))
  8868. (defun org-agenda-date-earlier-minutes (arg)
  8869. "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
  8870. (interactive "p")
  8871. (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
  8872. (org-agenda-date-earlier arg 'minute))
  8873. (defun org-agenda-date-later-hours (arg)
  8874. "Change the time of this item, in hour steps."
  8875. (interactive "p")
  8876. (org-agenda-date-later arg 'hour))
  8877. (defun org-agenda-date-earlier-hours (arg)
  8878. "Change the time of this item, in hour steps."
  8879. (interactive "p")
  8880. (org-agenda-date-earlier arg 'hour))
  8881. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  8882. "Show new date stamp via text properties."
  8883. ;; We use text properties to make this undoable
  8884. (let ((inhibit-read-only t))
  8885. (setq stamp (concat prefix " => " stamp " "))
  8886. (save-excursion
  8887. (goto-char (point-max))
  8888. (while (not (bobp))
  8889. (when (equal marker (org-get-at-bol 'org-marker))
  8890. (remove-text-properties (point-at-bol) (point-at-eol) '(display nil))
  8891. (org-move-to-column (- (window-width) (length stamp)) t)
  8892. (add-text-properties
  8893. (1- (point)) (point-at-eol)
  8894. (list 'display (org-add-props stamp nil
  8895. 'face '(secondary-selection default))))
  8896. (beginning-of-line 1))
  8897. (beginning-of-line 0)))))
  8898. (defun org-agenda-date-prompt (arg)
  8899. "Change the date of this item. Date is prompted for, with default today.
  8900. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  8901. be used to request time specification in the time stamp."
  8902. (interactive "P")
  8903. (org-agenda-check-type t 'agenda)
  8904. (org-agenda-check-no-diary)
  8905. (let* ((marker (or (org-get-at-bol 'org-marker)
  8906. (org-agenda-error)))
  8907. (buffer (marker-buffer marker))
  8908. (pos (marker-position marker)))
  8909. (org-with-remote-undo buffer
  8910. (with-current-buffer buffer
  8911. (widen)
  8912. (goto-char pos)
  8913. (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
  8914. (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
  8915. (org-agenda-show-new-time marker org-last-changed-timestamp))
  8916. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  8917. (defun org-agenda-schedule (arg &optional time)
  8918. "Schedule the item at point.
  8919. ARG is passed through to `org-schedule'."
  8920. (interactive "P")
  8921. (org-agenda-check-type t 'agenda 'todo 'tags 'search)
  8922. (org-agenda-check-no-diary)
  8923. (let* ((marker (or (org-get-at-bol 'org-marker)
  8924. (org-agenda-error)))
  8925. (type (marker-insertion-type marker))
  8926. (buffer (marker-buffer marker))
  8927. (pos (marker-position marker))
  8928. ts)
  8929. (set-marker-insertion-type marker t)
  8930. (org-with-remote-undo buffer
  8931. (with-current-buffer buffer
  8932. (widen)
  8933. (goto-char pos)
  8934. (setq ts (org-schedule arg time)))
  8935. (org-agenda-show-new-time marker ts " S"))
  8936. (message "%s" ts)))
  8937. (defun org-agenda-deadline (arg &optional time)
  8938. "Schedule the item at point.
  8939. ARG is passed through to `org-deadline'."
  8940. (interactive "P")
  8941. (org-agenda-check-type t 'agenda 'todo 'tags 'search)
  8942. (org-agenda-check-no-diary)
  8943. (let* ((marker (or (org-get-at-bol 'org-marker)
  8944. (org-agenda-error)))
  8945. (buffer (marker-buffer marker))
  8946. (pos (marker-position marker))
  8947. ts)
  8948. (org-with-remote-undo buffer
  8949. (with-current-buffer buffer
  8950. (widen)
  8951. (goto-char pos)
  8952. (setq ts (org-deadline arg time)))
  8953. (org-agenda-show-new-time marker ts " D"))
  8954. (message "%s" ts)))
  8955. (defun org-agenda-clock-in (&optional arg)
  8956. "Start the clock on the currently selected item."
  8957. (interactive "P")
  8958. (org-agenda-check-no-diary)
  8959. (if (equal arg '(4))
  8960. (org-clock-in arg)
  8961. (let* ((marker (or (org-get-at-bol 'org-marker)
  8962. (org-agenda-error)))
  8963. (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
  8964. (pos (marker-position marker))
  8965. (col (current-column))
  8966. newhead)
  8967. (org-with-remote-undo (marker-buffer marker)
  8968. (with-current-buffer (marker-buffer marker)
  8969. (widen)
  8970. (goto-char pos)
  8971. (org-show-context 'agenda)
  8972. (org-clock-in arg)
  8973. (setq newhead (org-get-heading)))
  8974. (org-agenda-change-all-lines newhead hdmarker))
  8975. (org-move-to-column col))))
  8976. (defun org-agenda-clock-out ()
  8977. "Stop the currently running clock."
  8978. (interactive)
  8979. (unless (marker-buffer org-clock-marker)
  8980. (error "No running clock"))
  8981. (let ((marker (make-marker)) (col (current-column)) newhead)
  8982. (org-with-remote-undo (marker-buffer org-clock-marker)
  8983. (with-current-buffer (marker-buffer org-clock-marker)
  8984. (org-with-wide-buffer
  8985. (goto-char org-clock-marker)
  8986. (org-back-to-heading t)
  8987. (move-marker marker (point))
  8988. (org-clock-out)
  8989. (setq newhead (org-get-heading)))))
  8990. (org-agenda-change-all-lines newhead marker)
  8991. (move-marker marker nil)
  8992. (org-move-to-column col)
  8993. (org-agenda-unmark-clocking-task)))
  8994. (defun org-agenda-clock-cancel (&optional arg)
  8995. "Cancel the currently running clock."
  8996. (interactive "P")
  8997. (unless (marker-buffer org-clock-marker)
  8998. (user-error "No running clock"))
  8999. (org-with-remote-undo (marker-buffer org-clock-marker)
  9000. (org-clock-cancel)))
  9001. (defun org-agenda-clock-goto ()
  9002. "Jump to the currently clocked in task within the agenda.
  9003. If the currently clocked in task is not listed in the agenda
  9004. buffer, display it in another window."
  9005. (interactive)
  9006. (let (pos)
  9007. (mapc (lambda (o)
  9008. (when (eq (overlay-get o 'type) 'org-agenda-clocking)
  9009. (setq pos (overlay-start o))))
  9010. (overlays-in (point-min) (point-max)))
  9011. (cond (pos (goto-char pos))
  9012. ;; If the currently clocked entry is not in the agenda
  9013. ;; buffer, we visit it in another window:
  9014. ((bound-and-true-p org-clock-current-task)
  9015. (org-switch-to-buffer-other-window (org-clock-goto)))
  9016. (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
  9017. (defun org-agenda-diary-entry-in-org-file ()
  9018. "Make a diary entry in the file `org-agenda-diary-file'."
  9019. (let (d1 d2 char (text "") dp1 dp2)
  9020. (if (equal (buffer-name) "*Calendar*")
  9021. (setq d1 (calendar-cursor-to-date t)
  9022. d2 (car calendar-mark-ring))
  9023. (setq dp1 (get-text-property (point-at-bol) 'day))
  9024. (unless dp1 (user-error "No date defined in current line"))
  9025. (setq d1 (calendar-gregorian-from-absolute dp1)
  9026. d2 (and (ignore-errors (mark))
  9027. (save-excursion
  9028. (goto-char (mark))
  9029. (setq dp2 (get-text-property (point-at-bol) 'day)))
  9030. (calendar-gregorian-from-absolute dp2))))
  9031. (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
  9032. (setq char (read-char-exclusive))
  9033. (cond
  9034. ((equal char ?d)
  9035. (setq text (read-string "Day entry: "))
  9036. (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
  9037. (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
  9038. ((equal char ?a)
  9039. (setq d1 (list (car d1) (nth 1 d1)
  9040. (read-number (format "Reference year [%d]: " (nth 2 d1))
  9041. (nth 2 d1))))
  9042. (setq text (read-string "Anniversary (use %d to show years): "))
  9043. (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
  9044. (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
  9045. ((equal char ?b)
  9046. (setq text (read-string "Block entry: "))
  9047. (unless (and d1 d2 (not (equal d1 d2)))
  9048. (user-error "No block of days selected"))
  9049. (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
  9050. (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
  9051. ((equal char ?j)
  9052. (org-switch-to-buffer-other-window
  9053. (find-file-noselect org-agenda-diary-file))
  9054. (require 'org-datetree)
  9055. (org-datetree-find-date-create d1)
  9056. (org-reveal t))
  9057. (t (user-error "Invalid selection character `%c'" char)))))
  9058. (defcustom org-agenda-insert-diary-strategy 'date-tree
  9059. "Where in `org-agenda-diary-file' should new entries be added?
  9060. Valid values:
  9061. date-tree in the date tree, as first child of the date
  9062. date-tree-last in the date tree, as last child of the date
  9063. top-level as top-level entries at the end of the file."
  9064. :group 'org-agenda
  9065. :type '(choice
  9066. (const :tag "first in a date tree" date-tree)
  9067. (const :tag "last in a date tree" date-tree-last)
  9068. (const :tag "as top level at end of file" top-level)))
  9069. (defcustom org-agenda-insert-diary-extract-time nil
  9070. "Non-nil means extract any time specification from the diary entry."
  9071. :group 'org-agenda
  9072. :version "24.1"
  9073. :type 'boolean)
  9074. (defcustom org-agenda-bulk-mark-char ">"
  9075. "A single-character string to be used as the bulk mark."
  9076. :group 'org-agenda
  9077. :version "24.1"
  9078. :type 'string)
  9079. (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
  9080. "Add a diary entry with TYPE to `org-agenda-diary-file'.
  9081. If TEXT is not empty, it will become the headline of the new entry, and
  9082. the resulting entry will not be shown. When TEXT is empty, switch to
  9083. `org-agenda-diary-file' and let the user finish the entry there."
  9084. (let ((cw (current-window-configuration)))
  9085. (org-switch-to-buffer-other-window
  9086. (find-file-noselect org-agenda-diary-file))
  9087. (widen)
  9088. (goto-char (point-min))
  9089. (cl-case type
  9090. (anniversary
  9091. (or (re-search-forward "^\\*[ \t]+Anniversaries" nil t)
  9092. (progn
  9093. (or (org-at-heading-p t)
  9094. (progn
  9095. (outline-next-heading)
  9096. (insert "* Anniversaries\n\n")
  9097. (beginning-of-line -1)))))
  9098. (outline-next-heading)
  9099. (org-back-over-empty-lines)
  9100. (backward-char 1)
  9101. (insert "\n")
  9102. (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
  9103. (nth 2 d1) (car d1) (nth 1 d1) text)))
  9104. (day
  9105. (let ((org-prefix-has-time t)
  9106. (org-agenda-time-leading-zero t)
  9107. fmt time time2)
  9108. (when org-agenda-insert-diary-extract-time
  9109. ;; Use org-agenda-format-item to parse text for a time-range and
  9110. ;; remove it. FIXME: This is a hack, we should refactor
  9111. ;; that function to make time extraction available separately
  9112. (setq fmt (org-agenda-format-item nil text nil nil nil t)
  9113. time (get-text-property 0 'time fmt)
  9114. time2 (if (> (length time) 0)
  9115. ;; split-string removes trailing ...... if
  9116. ;; no end time given. First space
  9117. ;; separates time from date.
  9118. (concat " " (car (split-string time "\\.")))
  9119. nil)
  9120. text (get-text-property 0 'txt fmt)))
  9121. (if (eq org-agenda-insert-diary-strategy 'top-level)
  9122. (org-agenda-insert-diary-as-top-level text)
  9123. (require 'org-datetree)
  9124. (org-datetree-find-date-create d1)
  9125. (org-agenda-insert-diary-make-new-entry text))
  9126. (org-insert-time-stamp (org-time-from-absolute
  9127. (calendar-absolute-from-gregorian d1))
  9128. nil nil nil nil time2))
  9129. (end-of-line 0))
  9130. ((block) ;; Wrap this in (strictly unnecessary) parens because
  9131. ;; otherwise the indentation gets confused by the
  9132. ;; special meaning of 'block
  9133. (when (> (calendar-absolute-from-gregorian d1)
  9134. (calendar-absolute-from-gregorian d2))
  9135. (setq d1 (prog1 d2 (setq d2 d1))))
  9136. (if (eq org-agenda-insert-diary-strategy 'top-level)
  9137. (org-agenda-insert-diary-as-top-level text)
  9138. (require 'org-datetree)
  9139. (org-datetree-find-date-create d1)
  9140. (org-agenda-insert-diary-make-new-entry text))
  9141. (org-insert-time-stamp (org-time-from-absolute
  9142. (calendar-absolute-from-gregorian d1)))
  9143. (insert "--")
  9144. (org-insert-time-stamp (org-time-from-absolute
  9145. (calendar-absolute-from-gregorian d2)))
  9146. (end-of-line 0)))
  9147. (if (string-match "\\S-" text)
  9148. (progn
  9149. (set-window-configuration cw)
  9150. (message "%s entry added to %s"
  9151. (capitalize (symbol-name type))
  9152. (abbreviate-file-name org-agenda-diary-file)))
  9153. (org-reveal t)
  9154. (message "Please finish entry here"))))
  9155. (defun org-agenda-insert-diary-as-top-level (text)
  9156. "Make new entry as a top-level entry at the end of the file.
  9157. Add TEXT as headline, and position the cursor in the second line so that
  9158. a timestamp can be added there."
  9159. (widen)
  9160. (goto-char (point-max))
  9161. (unless (bolp) (insert "\n"))
  9162. (org-insert-heading nil t t)
  9163. (insert text)
  9164. (org-end-of-meta-data)
  9165. (unless (bolp) (insert "\n"))
  9166. (when org-adapt-indentation (indent-to-column 2)))
  9167. (defun org-agenda-insert-diary-make-new-entry (text)
  9168. "Make a new entry with TEXT as a child of the current subtree.
  9169. Position the point in the heading's first body line so that
  9170. a timestamp can be added there."
  9171. (cond
  9172. ((eq org-agenda-insert-diary-strategy 'date-tree-last)
  9173. (end-of-line)
  9174. (org-insert-heading '(4) t)
  9175. (org-do-demote))
  9176. (t
  9177. (outline-next-heading)
  9178. (org-back-over-empty-lines)
  9179. (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
  9180. (org-insert-heading nil t)
  9181. (org-do-demote)))
  9182. (let ((col (current-column)))
  9183. (insert text)
  9184. (org-end-of-meta-data)
  9185. ;; Ensure point is left on a blank line, at proper indentation.
  9186. (unless (bolp) (insert "\n"))
  9187. (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
  9188. (when org-adapt-indentation (indent-to-column col)))
  9189. (org-show-set-visibility 'lineage))
  9190. (defun org-agenda-diary-entry ()
  9191. "Make a diary entry, like the `i' command from the calendar.
  9192. All the standard commands work: block, weekly etc.
  9193. When `org-agenda-diary-file' points to a file,
  9194. `org-agenda-diary-entry-in-org-file' is called instead to create
  9195. entries in that Org file."
  9196. (interactive)
  9197. (if (not (eq org-agenda-diary-file 'diary-file))
  9198. (org-agenda-diary-entry-in-org-file)
  9199. (require 'diary-lib)
  9200. (let* ((char (read-char-exclusive
  9201. "Diary entry: [d]ay [w]eekly [m]onthly [y]early\
  9202. [a]nniversary [b]lock [c]yclic"))
  9203. (cmd (cdr (assoc char
  9204. '((?d . diary-insert-entry)
  9205. (?w . diary-insert-weekly-entry)
  9206. (?m . diary-insert-monthly-entry)
  9207. (?y . diary-insert-yearly-entry)
  9208. (?a . diary-insert-anniversary-entry)
  9209. (?b . diary-insert-block-entry)
  9210. (?c . diary-insert-cyclic-entry)))))
  9211. (oldf (symbol-function 'calendar-cursor-to-date))
  9212. ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  9213. (point (point))
  9214. (mark (or (mark t) (point))))
  9215. (unless cmd
  9216. (user-error "No command associated with <%c>" char))
  9217. (unless (and (get-text-property point 'day)
  9218. (or (not (equal ?b char))
  9219. (get-text-property mark 'day)))
  9220. (user-error "Don't know which date to use for diary entry"))
  9221. ;; We implement this by hacking the `calendar-cursor-to-date' function
  9222. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  9223. (let ((calendar-mark-ring
  9224. (list (calendar-gregorian-from-absolute
  9225. (or (get-text-property mark 'day)
  9226. (get-text-property point 'day))))))
  9227. (unwind-protect
  9228. (progn
  9229. (fset 'calendar-cursor-to-date
  9230. (lambda (&optional error dummy)
  9231. (calendar-gregorian-from-absolute
  9232. (get-text-property point 'day))))
  9233. (call-interactively cmd))
  9234. (fset 'calendar-cursor-to-date oldf))))))
  9235. (defun org-agenda-execute-calendar-command (cmd)
  9236. "Execute a calendar command from the agenda with date from cursor."
  9237. (org-agenda-check-type t 'agenda)
  9238. (require 'diary-lib)
  9239. (unless (get-text-property (min (1- (point-max)) (point)) 'day)
  9240. (user-error "Don't know which date to use for the calendar command"))
  9241. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  9242. (point (point))
  9243. (date (calendar-gregorian-from-absolute
  9244. (get-text-property point 'day)))
  9245. ;; the following 2 vars are needed in the calendar
  9246. (displayed-month (car date))
  9247. (displayed-year (nth 2 date)))
  9248. (unwind-protect
  9249. (progn
  9250. (fset 'calendar-cursor-to-date
  9251. (lambda (&optional error dummy)
  9252. (calendar-gregorian-from-absolute
  9253. (get-text-property point 'day))))
  9254. (call-interactively cmd))
  9255. (fset 'calendar-cursor-to-date oldf))))
  9256. (defun org-agenda-phases-of-moon ()
  9257. "Display the phases of the moon for the 3 months around the cursor date."
  9258. (interactive)
  9259. (org-agenda-execute-calendar-command 'calendar-lunar-phases))
  9260. (defun org-agenda-holidays ()
  9261. "Display the holidays for the 3 months around the cursor date."
  9262. (interactive)
  9263. (org-agenda-execute-calendar-command 'calendar-list-holidays))
  9264. (defvar calendar-longitude) ; defined in calendar.el
  9265. (defvar calendar-latitude) ; defined in calendar.el
  9266. (defvar calendar-location-name) ; defined in calendar.el
  9267. (defun org-agenda-sunrise-sunset (arg)
  9268. "Display sunrise and sunset for the cursor date.
  9269. Latitude and longitude can be specified with the variables
  9270. `calendar-latitude' and `calendar-longitude'. When called with prefix
  9271. argument, latitude and longitude will be prompted for."
  9272. (interactive "P")
  9273. (require 'solar)
  9274. (let ((calendar-longitude (if arg nil calendar-longitude))
  9275. (calendar-latitude (if arg nil calendar-latitude))
  9276. (calendar-location-name
  9277. (if arg "the given coordinates" calendar-location-name)))
  9278. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  9279. (defun org-agenda-goto-calendar ()
  9280. "Open the Emacs calendar with the date at the cursor."
  9281. (interactive)
  9282. (org-agenda-check-type t 'agenda)
  9283. (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
  9284. (user-error "Don't know which date to open in calendar")))
  9285. (date (calendar-gregorian-from-absolute day))
  9286. (calendar-move-hook nil)
  9287. (calendar-view-holidays-initially-flag nil)
  9288. (calendar-view-diary-initially-flag nil))
  9289. (calendar)
  9290. (calendar-goto-date date)))
  9291. ;;;###autoload
  9292. (defun org-calendar-goto-agenda ()
  9293. "Compute the Org agenda for the calendar date displayed at the cursor.
  9294. This is a command that has to be installed in `calendar-mode-map'."
  9295. (interactive)
  9296. ;; Temporarily disable sticky agenda since user clearly wants to
  9297. ;; refresh view anyway.
  9298. (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
  9299. (org-agenda-sticky nil))
  9300. (org-agenda-list nil (calendar-absolute-from-gregorian
  9301. (calendar-cursor-to-date))
  9302. nil)))
  9303. (defun org-agenda-convert-date ()
  9304. (interactive)
  9305. (org-agenda-check-type t 'agenda)
  9306. (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
  9307. date s)
  9308. (unless day
  9309. (user-error "Don't know which date to convert"))
  9310. (setq date (calendar-gregorian-from-absolute day))
  9311. (setq s (concat
  9312. "Gregorian: " (calendar-date-string date) "\n"
  9313. "ISO: " (calendar-iso-date-string date) "\n"
  9314. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  9315. "Julian: " (calendar-julian-date-string date) "\n"
  9316. "Astron. JD: " (calendar-astro-date-string date)
  9317. " (Julian date number at noon UTC)\n"
  9318. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  9319. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  9320. "French: " (calendar-french-date-string date) "\n"
  9321. "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
  9322. "Mayan: " (calendar-mayan-date-string date) "\n"
  9323. "Coptic: " (calendar-coptic-date-string date) "\n"
  9324. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  9325. "Persian: " (calendar-persian-date-string date) "\n"
  9326. "Chinese: " (calendar-chinese-date-string date) "\n"))
  9327. (with-output-to-temp-buffer "*Dates*"
  9328. (princ s))
  9329. (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
  9330. ;;; Bulk commands
  9331. (defun org-agenda-bulk-marked-p ()
  9332. "Non-nil when current entry is marked for bulk action."
  9333. (eq (get-char-property (point-at-bol) 'type)
  9334. 'org-marked-entry-overlay))
  9335. (defun org-agenda-bulk-mark (&optional arg)
  9336. "Mark entries for future bulk action.
  9337. When ARG is nil or one and region is not active then mark the
  9338. entry at point.
  9339. When ARG is nil or one and region is active then mark the entries
  9340. in the region.
  9341. When ARG is greater than one mark ARG lines."
  9342. (interactive "p")
  9343. (when (and (or (not arg) (= arg 1)) (use-region-p))
  9344. (setq arg (count-lines (region-beginning) (region-end)))
  9345. (goto-char (region-beginning))
  9346. (deactivate-mark))
  9347. (dotimes (i (or arg 1))
  9348. (unless (org-get-at-bol 'org-agenda-diary-link)
  9349. (let* ((m (org-get-at-bol 'org-hd-marker))
  9350. ov)
  9351. (unless (org-agenda-bulk-marked-p)
  9352. (unless m (user-error "Nothing to mark at point"))
  9353. (push m org-agenda-bulk-marked-entries)
  9354. (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
  9355. (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
  9356. (org-get-todo-face "TODO")
  9357. 'evaporate)
  9358. (overlay-put ov 'type 'org-marked-entry-overlay))
  9359. (end-of-line 1)
  9360. (or (ignore-errors
  9361. (goto-char (next-single-property-change (point) 'org-hd-marker)))
  9362. (beginning-of-line 2))
  9363. (while (and (get-char-property (point) 'invisible) (not (eobp)))
  9364. (beginning-of-line 2)))))
  9365. (message "%d entries marked for bulk action"
  9366. (length org-agenda-bulk-marked-entries)))
  9367. (defun org-agenda-bulk-mark-all ()
  9368. "Mark all entries for future agenda bulk action."
  9369. (interactive)
  9370. (org-agenda-bulk-mark-regexp "."))
  9371. (defun org-agenda-bulk-mark-regexp (regexp)
  9372. "Mark entries matching REGEXP for future agenda bulk action."
  9373. (interactive "sMark entries matching regexp: ")
  9374. (let ((entries-marked 0) txt-at-point)
  9375. (save-excursion
  9376. (goto-char (point-min))
  9377. (goto-char (next-single-property-change (point) 'org-hd-marker))
  9378. (while (and (re-search-forward regexp nil t)
  9379. (setq txt-at-point (get-text-property (point) 'txt)))
  9380. (if (get-char-property (point) 'invisible)
  9381. (beginning-of-line 2)
  9382. (when (string-match regexp txt-at-point)
  9383. (setq entries-marked (1+ entries-marked))
  9384. (call-interactively 'org-agenda-bulk-mark)))))
  9385. (unless entries-marked
  9386. (message "No entry matching this regexp."))))
  9387. (defun org-agenda-bulk-unmark (&optional arg)
  9388. "Unmark the entry at point for future bulk action."
  9389. (interactive "P")
  9390. (if arg
  9391. (org-agenda-bulk-unmark-all)
  9392. (cond ((org-agenda-bulk-marked-p)
  9393. (org-agenda-bulk-remove-overlays
  9394. (point-at-bol) (+ 2 (point-at-bol)))
  9395. (setq org-agenda-bulk-marked-entries
  9396. (delete (org-get-at-bol 'org-hd-marker)
  9397. org-agenda-bulk-marked-entries))
  9398. (end-of-line 1)
  9399. (or (ignore-errors
  9400. (goto-char (next-single-property-change (point) 'txt)))
  9401. (beginning-of-line 2))
  9402. (while (and (get-char-property (point) 'invisible) (not (eobp)))
  9403. (beginning-of-line 2))
  9404. (message "%d entries left marked for bulk action"
  9405. (length org-agenda-bulk-marked-entries)))
  9406. (t (message "No entry to unmark here")))))
  9407. (defun org-agenda-bulk-toggle-all ()
  9408. "Toggle all marks for bulk action."
  9409. (interactive)
  9410. (save-excursion
  9411. (goto-char (point-min))
  9412. (while (ignore-errors
  9413. (goto-char (next-single-property-change (point) 'org-hd-marker)))
  9414. (org-agenda-bulk-toggle))))
  9415. (defun org-agenda-bulk-toggle ()
  9416. "Toggle the mark at point for bulk action."
  9417. (interactive)
  9418. (if (org-agenda-bulk-marked-p)
  9419. (org-agenda-bulk-unmark)
  9420. (org-agenda-bulk-mark)))
  9421. (defun org-agenda-bulk-remove-overlays (&optional beg end)
  9422. "Remove the mark overlays between BEG and END in the agenda buffer.
  9423. BEG and END default to the buffer limits.
  9424. This only removes the overlays, it does not remove the markers
  9425. from the list in `org-agenda-bulk-marked-entries'."
  9426. (interactive)
  9427. (mapc (lambda (ov)
  9428. (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
  9429. (delete-overlay ov)))
  9430. (overlays-in (or beg (point-min)) (or end (point-max)))))
  9431. (defun org-agenda-bulk-unmark-all ()
  9432. "Remove all marks in the agenda buffer.
  9433. This will remove the markers and the overlays."
  9434. (interactive)
  9435. (if (null org-agenda-bulk-marked-entries)
  9436. (message "No entry to unmark")
  9437. (setq org-agenda-bulk-marked-entries nil)
  9438. (org-agenda-bulk-remove-overlays (point-min) (point-max))))
  9439. (defcustom org-agenda-persistent-marks nil
  9440. "Non-nil means marked items will stay marked after a bulk action.
  9441. You can toggle this interactively by typing `p' when prompted for a
  9442. bulk action."
  9443. :group 'org-agenda
  9444. :version "24.1"
  9445. :type 'boolean)
  9446. (defun org-agenda-bulk-action (&optional arg)
  9447. "Execute an remote-editing action on all marked entries.
  9448. The prefix arg is passed through to the command if possible."
  9449. (interactive "P")
  9450. ;; When there is no mark, act on the agenda entry at point.
  9451. (if (not org-agenda-bulk-marked-entries)
  9452. (save-excursion (org-agenda-bulk-mark)))
  9453. (dolist (m org-agenda-bulk-marked-entries)
  9454. (unless (and (markerp m)
  9455. (marker-buffer m)
  9456. (buffer-live-p (marker-buffer m))
  9457. (marker-position m))
  9458. (user-error "Marker %s for bulk command is invalid" m)))
  9459. ;; Prompt for the bulk command.
  9460. (org-unlogged-message
  9461. (concat "Bulk (" (if org-agenda-persistent-marks "" "don't ") "[p]ersist marks): "
  9462. "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
  9463. "[S]catter [f]unction "
  9464. (and org-agenda-bulk-custom-functions
  9465. (format " Custom: [%s]"
  9466. (mapconcat (lambda (f) (char-to-string (car f)))
  9467. org-agenda-bulk-custom-functions
  9468. "")))))
  9469. (catch 'exit
  9470. (let* ((org-log-refile (if org-log-refile 'time nil))
  9471. (entries (reverse org-agenda-bulk-marked-entries))
  9472. (org-overriding-default-time
  9473. (and (get-text-property (point) 'org-agenda-date-header)
  9474. (org-get-cursor-date)))
  9475. redo-at-end
  9476. cmd)
  9477. (pcase (read-char-exclusive)
  9478. (?p
  9479. (let ((org-agenda-persistent-marks
  9480. (not org-agenda-persistent-marks)))
  9481. (org-agenda-bulk-action)
  9482. (throw 'exit nil)))
  9483. (?$
  9484. (setq cmd #'org-agenda-archive))
  9485. (?A
  9486. (setq cmd #'org-agenda-archive-to-archive-sibling))
  9487. ((or ?r ?w)
  9488. (let ((refile-location
  9489. (org-refile-get-location
  9490. "Refile to"
  9491. (marker-buffer (car entries))
  9492. org-refile-allow-creating-parent-nodes)))
  9493. (when (nth 3 refile-location)
  9494. (setcar (nthcdr 3 refile-location)
  9495. (move-marker
  9496. (make-marker)
  9497. (nth 3 refile-location)
  9498. (or (get-file-buffer (nth 1 refile-location))
  9499. (find-buffer-visiting (nth 1 refile-location))
  9500. (error "This should not happen")))))
  9501. (setq cmd `(lambda () (org-agenda-refile nil ',refile-location t)))
  9502. (setq redo-at-end t)))
  9503. (?t
  9504. (let ((state (completing-read
  9505. "Todo state: "
  9506. (with-current-buffer (marker-buffer (car entries))
  9507. (mapcar #'list org-todo-keywords-1)))))
  9508. (setq cmd `(lambda ()
  9509. (let ((org-inhibit-blocking t)
  9510. (org-inhibit-logging 'note))
  9511. (org-agenda-todo ,state))))))
  9512. ((and (or ?- ?+) action)
  9513. (let ((tag (completing-read
  9514. (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
  9515. (with-current-buffer (marker-buffer (car entries))
  9516. (delq nil
  9517. (mapcar (lambda (x) (and (stringp (car x)) x))
  9518. org-current-tag-alist))))))
  9519. (setq cmd
  9520. `(lambda ()
  9521. (org-agenda-set-tags ,tag
  9522. ,(if (eq action ?+) ''on ''off))))))
  9523. ((and (or ?s ?d) c)
  9524. (let* ((schedule? (eq c ?s))
  9525. (prompt (if schedule? "(Re)Schedule to" "(Re)Set Deadline to"))
  9526. (time
  9527. (and (not arg)
  9528. (let ((new (org-read-date
  9529. nil nil nil prompt org-overriding-default-time)))
  9530. ;; A "double plus" answer applies to every
  9531. ;; scheduled time. Do not turn it into
  9532. ;; a fixed date yet.
  9533. (if (string-match-p "\\`[ \t]*\\+\\+"
  9534. org-read-date-final-answer)
  9535. org-read-date-final-answer
  9536. new)))))
  9537. ;; Make sure to not prompt for a note when bulk
  9538. ;; rescheduling/resetting deadline as Org cannot cope with
  9539. ;; simultaneous notes. Besides, it could be annoying
  9540. ;; depending on the number of marked items.
  9541. (setq cmd
  9542. (if schedule?
  9543. `(lambda ()
  9544. (let ((org-log-reschedule
  9545. (and org-log-reschedule 'time)))
  9546. (org-agenda-schedule arg ,time)))
  9547. `(lambda ()
  9548. (let ((org-log-redeadline (and org-log-redeadline 'time)))
  9549. (org-agenda-deadline arg ,time)))))))
  9550. (?S
  9551. (unless (org-agenda-check-type nil 'agenda 'todo)
  9552. (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type))
  9553. (let ((days (read-number
  9554. (format "Scatter tasks across how many %sdays: "
  9555. (if arg "week" ""))
  9556. 7)))
  9557. (setq cmd
  9558. `(lambda ()
  9559. (let ((distance (1+ (random ,days))))
  9560. (when arg
  9561. (let ((dist distance)
  9562. (day-of-week
  9563. (calendar-day-of-week
  9564. (calendar-gregorian-from-absolute (org-today)))))
  9565. (dotimes (i (1+ dist))
  9566. (while (member day-of-week org-agenda-weekend-days)
  9567. (cl-incf distance)
  9568. (cl-incf day-of-week)
  9569. (when (= day-of-week 7)
  9570. (setq day-of-week 0)))
  9571. (cl-incf day-of-week)
  9572. (when (= day-of-week 7)
  9573. (setq day-of-week 0)))))
  9574. ;; Silently fail when try to replan a sexp entry.
  9575. (ignore-errors
  9576. (let* ((date (calendar-gregorian-from-absolute
  9577. (+ (org-today) distance)))
  9578. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  9579. (nth 2 date))))
  9580. (org-agenda-schedule nil time))))))))
  9581. (?f
  9582. (setq cmd
  9583. (intern
  9584. (completing-read "Function: " obarray #'fboundp t nil nil))))
  9585. (action
  9586. (pcase (assoc action org-agenda-bulk-custom-functions)
  9587. (`(,_ ,f) (setq cmd f) (setq redo-at-end t))
  9588. (_ (user-error "Invalid bulk action: %c" action)))))
  9589. ;; Sort the markers, to make sure that parents are handled
  9590. ;; before children.
  9591. (setq entries (sort entries
  9592. (lambda (a b)
  9593. (cond
  9594. ((eq (marker-buffer a) (marker-buffer b))
  9595. (< (marker-position a) (marker-position b)))
  9596. (t
  9597. (string< (buffer-name (marker-buffer a))
  9598. (buffer-name (marker-buffer b))))))))
  9599. ;; Now loop over all markers and apply CMD.
  9600. (let ((processed 0)
  9601. (skipped 0))
  9602. (dolist (e entries)
  9603. (let ((pos (text-property-any (point-min) (point-max) 'org-hd-marker e)))
  9604. (if (not pos)
  9605. (progn (message "Skipping removed entry at %s" e)
  9606. (cl-incf skipped))
  9607. (goto-char pos)
  9608. (let (org-loop-over-headlines-in-active-region) (funcall cmd))
  9609. ;; `post-command-hook' is not run yet. We make sure any
  9610. ;; pending log note is processed.
  9611. (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9612. (memq 'org-add-log-note post-command-hook))
  9613. (org-add-log-note))
  9614. (cl-incf processed))))
  9615. (when redo-at-end (org-agenda-redo))
  9616. (unless org-agenda-persistent-marks (org-agenda-bulk-unmark-all))
  9617. (message "Acted on %d entries%s%s"
  9618. processed
  9619. (if (= skipped 0)
  9620. ""
  9621. (format ", skipped %d (disappeared before their turn)"
  9622. skipped))
  9623. (if (not org-agenda-persistent-marks) "" " (kept marked)"))))))
  9624. (defun org-agenda-capture (&optional with-time)
  9625. "Call `org-capture' with the date at point.
  9626. With a `C-1' prefix, use the HH:MM value at point (if any) or the
  9627. current HH:MM time."
  9628. (interactive "P")
  9629. (if (not (eq major-mode 'org-agenda-mode))
  9630. (user-error "You cannot do this outside of agenda buffers")
  9631. (let ((org-overriding-default-time
  9632. (org-get-cursor-date (equal with-time 1))))
  9633. (call-interactively 'org-capture))))
  9634. ;;; Dragging agenda lines forward/backward
  9635. (defun org-agenda-reapply-filters ()
  9636. "Re-apply all agenda filters."
  9637. (mapcar
  9638. (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
  9639. `((,org-agenda-tag-filter tag)
  9640. (,org-agenda-category-filter category)
  9641. (,org-agenda-regexp-filter regexp)
  9642. (,org-agenda-effort-filter effort)
  9643. (,(get 'org-agenda-tag-filter :preset-filter) tag)
  9644. (,(get 'org-agenda-category-filter :preset-filter) category)
  9645. (,(get 'org-agenda-effort-filter :preset-filter) effort)
  9646. (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
  9647. (defun org-agenda-drag-line-forward (arg &optional backward)
  9648. "Drag an agenda line forward by ARG lines.
  9649. When the optional argument `backward' is non-nil, move backward."
  9650. (interactive "p")
  9651. (let ((inhibit-read-only t) lst line)
  9652. (if (or (not (get-text-property (point) 'txt))
  9653. (save-excursion
  9654. (dotimes (n arg)
  9655. (move-beginning-of-line (if backward 0 2))
  9656. (push (not (get-text-property (point) 'txt)) lst))
  9657. (delq nil lst)))
  9658. (message "Cannot move line forward")
  9659. (let ((end (save-excursion (move-beginning-of-line 2) (point))))
  9660. (move-beginning-of-line 1)
  9661. (setq line (buffer-substring (point) end))
  9662. (delete-region (point) end)
  9663. (move-beginning-of-line (funcall (if backward '1- '1+) arg))
  9664. (insert line)
  9665. (org-agenda-reapply-filters)
  9666. (org-agenda-mark-clocking-task)
  9667. (move-beginning-of-line 0)))))
  9668. (defun org-agenda-drag-line-backward (arg)
  9669. "Drag an agenda line backward by ARG lines."
  9670. (interactive "p")
  9671. (org-agenda-drag-line-forward arg t))
  9672. ;;; Flagging notes
  9673. (defun org-agenda-show-the-flagging-note ()
  9674. "Display the flagging note in the other window.
  9675. When called a second time in direct sequence, offer to remove the FLAGGING
  9676. tag and (if present) the flagging note."
  9677. (interactive)
  9678. (let ((hdmarker (org-get-at-bol 'org-hd-marker))
  9679. (win (selected-window))
  9680. note heading newhead)
  9681. (unless hdmarker
  9682. (user-error "No linked entry at point"))
  9683. (if (and (eq this-command last-command)
  9684. (y-or-n-p "Unflag and remove any flagging note? "))
  9685. (progn
  9686. (org-agenda-remove-flag hdmarker)
  9687. (let ((win (get-buffer-window "*Flagging Note*")))
  9688. (and win (delete-window win)))
  9689. (message "Entry unflagged"))
  9690. (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
  9691. (unless note
  9692. (user-error "No flagging note"))
  9693. (org-kill-new note)
  9694. (org-switch-to-buffer-other-window "*Flagging Note*")
  9695. (erase-buffer)
  9696. (insert note)
  9697. (goto-char (point-min))
  9698. (while (re-search-forward "\\\\n" nil t)
  9699. (replace-match "\n" t t))
  9700. (goto-char (point-min))
  9701. (select-window win)
  9702. (message "%s" (substitute-command-keys "Flagging note pushed to \
  9703. kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
  9704. tag and note")))))
  9705. (defun org-agenda-remove-flag (marker)
  9706. "Remove the FLAGGED tag and any flagging note in the entry."
  9707. (let (newhead)
  9708. (org-with-point-at marker
  9709. (org-toggle-tag "FLAGGED" 'off)
  9710. (org-entry-delete nil "THEFLAGGINGNOTE")
  9711. (setq newhead (org-get-heading)))
  9712. (org-agenda-change-all-lines newhead marker)
  9713. (message "Entry unflagged")))
  9714. (defun org-agenda-get-any-marker (&optional pos)
  9715. (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
  9716. (get-text-property (or pos (point-at-bol)) 'org-marker)))
  9717. ;;; Appointment reminders
  9718. (defvar appt-time-msg-list) ; defined in appt.el
  9719. ;;;###autoload
  9720. (defun org-agenda-to-appt (&optional refresh filter &rest args)
  9721. "Activate appointments found in `org-agenda-files'.
  9722. With a `\\[universal-argument]' prefix, refresh the list of \
  9723. appointments.
  9724. If FILTER is t, interactively prompt the user for a regular
  9725. expression, and filter out entries that don't match it.
  9726. If FILTER is a string, use this string as a regular expression
  9727. for filtering entries out.
  9728. If FILTER is a function, filter out entries against which
  9729. calling the function returns nil. This function takes one
  9730. argument: an entry from `org-agenda-get-day-entries'.
  9731. FILTER can also be an alist with the car of each cell being
  9732. either `headline' or `category'. For example:
  9733. \\='((headline \"IMPORTANT\")
  9734. (category \"Work\"))
  9735. will only add headlines containing IMPORTANT or headlines
  9736. belonging to the \"Work\" category.
  9737. ARGS are symbols indicating what kind of entries to consider.
  9738. By default `org-agenda-to-appt' will use :deadline*, :scheduled*
  9739. \(i.e., deadlines and scheduled items with a hh:mm specification)
  9740. and :timestamp entries. See the docstring of `org-diary' for
  9741. details and examples.
  9742. If an entry has a APPT_WARNTIME property, its value will be used
  9743. to override `appt-message-warning-time'."
  9744. (interactive "P")
  9745. (when refresh (setq appt-time-msg-list nil))
  9746. (when (eq filter t)
  9747. (setq filter (read-from-minibuffer "Regexp filter: ")))
  9748. (let* ((cnt 0) ; count added events
  9749. (scope (or args '(:deadline* :scheduled* :timestamp)))
  9750. (org-agenda-new-buffers nil)
  9751. (org-deadline-warning-days 0)
  9752. ;; Do not use `org-today' here because appt only takes
  9753. ;; time and without date as argument, so it may pass wrong
  9754. ;; information otherwise
  9755. (today (org-date-to-gregorian
  9756. (time-to-days nil)))
  9757. (org-agenda-restrict nil)
  9758. (files (org-agenda-files 'unrestricted)) entries file
  9759. (org-agenda-buffer nil))
  9760. ;; Get all entries which may contain an appt
  9761. (org-agenda-prepare-buffers files)
  9762. (while (setq file (pop files))
  9763. (setq entries
  9764. (delq nil
  9765. (append entries
  9766. (apply 'org-agenda-get-day-entries
  9767. file today scope)))))
  9768. ;; Map through entries and find if we should filter them out
  9769. (mapc
  9770. (lambda (x)
  9771. (let* ((evt (org-trim
  9772. (replace-regexp-in-string
  9773. org-link-bracket-re "\\2"
  9774. (or (get-text-property 1 'txt x) ""))))
  9775. (cat (get-text-property (1- (length x)) 'org-category x))
  9776. (tod (get-text-property 1 'time-of-day x))
  9777. (ok (or (null filter)
  9778. (and (stringp filter) (string-match filter evt))
  9779. (and (functionp filter) (funcall filter x))
  9780. (and (listp filter)
  9781. (let ((cat-filter (cadr (assq 'category filter)))
  9782. (evt-filter (cadr (assq 'headline filter))))
  9783. (or (and (stringp cat-filter)
  9784. (string-match cat-filter cat))
  9785. (and (stringp evt-filter)
  9786. (string-match evt-filter evt)))))))
  9787. (wrn (get-text-property 1 'warntime x)))
  9788. ;; FIXME: Shall we remove text-properties for the appt text?
  9789. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  9790. (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
  9791. (setq tod (concat "00" (number-to-string tod)))
  9792. (setq tod (when (string-match
  9793. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
  9794. (concat (match-string 1 tod) ":"
  9795. (match-string 2 tod))))
  9796. (when (appt-add tod evt wrn)
  9797. (setq cnt (1+ cnt))))))
  9798. entries)
  9799. (org-release-buffers org-agenda-new-buffers)
  9800. (if (eq cnt 0)
  9801. (message "No event to add")
  9802. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  9803. (defun org-agenda-today-p (date)
  9804. "Non-nil when DATE means today.
  9805. DATE is either a list of the form (month day year) or a number of
  9806. days as returned by `calendar-absolute-from-gregorian' or
  9807. `org-today'. This function considers `org-extend-today-until'
  9808. when defining today."
  9809. (eq (org-today)
  9810. (if (consp date) (calendar-absolute-from-gregorian date) date)))
  9811. (defun org-agenda-todo-yesterday (&optional arg)
  9812. "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
  9813. (interactive "P")
  9814. (let* ((org-use-effective-time t)
  9815. (hour (nth 2 (decode-time (org-current-time))))
  9816. (org-extend-today-until (1+ hour)))
  9817. (org-agenda-todo arg)))
  9818. (provide 'org-agenda)
  9819. ;;; org-agenda.el ends here