org-agenda.el 449 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449
  1. ;;; org-agenda.el --- Dynamic task and appointment lists for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; URL: 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 'org-macs)
  42. (org-assert-version)
  43. (require 'cl-lib)
  44. (require 'ol)
  45. (require 'org-fold-core)
  46. (require 'org)
  47. (require 'org-macs)
  48. (require 'org-refile)
  49. (declare-function diary-add-to-list "diary-lib"
  50. (date string specifier &optional marker globcolor literal))
  51. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  52. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  53. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  54. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  55. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  56. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  57. (declare-function calendar-french-date-string "cal-french" (&optional date))
  58. (declare-function calendar-goto-date "cal-move" (date))
  59. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  60. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  61. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  62. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  63. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  64. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  65. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  66. (declare-function calendar-check-holidays "holidays" (date))
  67. (declare-function org-columns-remove-overlays "org-colview" ())
  68. (declare-function org-datetree-find-date-create "org-datetree"
  69. (date &optional keep-restriction))
  70. (declare-function org-columns-quit "org-colview" ())
  71. (declare-function diary-date-display-form "diary-lib" (&optional type))
  72. (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
  73. (declare-function org-element-property "org-element" (property element))
  74. (declare-function org-element--cache-active-p "org-element"
  75. (&optional called-from-cache-change-func-p))
  76. (declare-function org-element-lineage "org-element"
  77. (datum &optional types with-self))
  78. (declare-function org-habit-insert-consistency-graphs
  79. "org-habit" (&optional line))
  80. (declare-function org-is-habit-p "org-habit" (&optional pom))
  81. (declare-function org-habit-parse-todo "org-habit" (&optional pom))
  82. (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
  83. (declare-function org-agenda-columns "org-colview" ())
  84. (declare-function org-add-archive-files "org-archive" (files))
  85. (declare-function org-capture "org-capture" (&optional goto keys))
  86. (declare-function org-clock-modify-effort-estimate "org-clock" (&optional value))
  87. (declare-function org-element-type "org-element" (&optional element))
  88. (defvar calendar-mode-map)
  89. (defvar org-clock-current-task)
  90. (defvar org-current-tag-alist)
  91. (defvar org-mobile-force-id-on-agenda-items)
  92. (defvar org-habit-show-habits)
  93. (defvar org-habit-show-habits-only-for-today)
  94. (defvar org-habit-show-all-today)
  95. (defvar org-habit-scheduled-past-days)
  96. ;; Defined somewhere in this file, but used before definition.
  97. (defvar org-agenda-buffer-name "*Org Agenda*")
  98. (defvar org-agenda-title-append nil)
  99. (defvar org-agenda-overriding-header)
  100. ;; (with-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  101. ;; (with-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  102. (defvar original-date) ; dynamically scoped, calendar.el does scope this
  103. (defvar org-agenda-undo-list nil
  104. "List of undoable operations in the agenda since last refresh.")
  105. (defvar org-agenda-pending-undo-list nil
  106. "In a series of undo commands, this is the list of remaining undo items.")
  107. (defcustom org-agenda-confirm-kill 1
  108. "When set, remote killing from the agenda buffer needs confirmation.
  109. When t, a confirmation is always needed. When a number N, confirmation is
  110. only needed when the text to be killed contains more than N non-white lines."
  111. :group 'org-agenda
  112. :type '(choice
  113. (const :tag "Never" nil)
  114. (const :tag "Always" t)
  115. (integer :tag "When more than N lines")))
  116. (defcustom org-agenda-compact-blocks nil
  117. "Non-nil means make the block agenda more compact.
  118. This is done globally by leaving out lines like the agenda span
  119. name and week number or the separator lines."
  120. :group 'org-agenda
  121. :type 'boolean)
  122. (defcustom org-agenda-block-separator
  123. (if (and (display-graphic-p)
  124. (char-displayable-p ?─))
  125. ?─
  126. ?=)
  127. "The separator between blocks in the agenda.
  128. If this is a string, it will be used as the separator, with a newline added.
  129. If it is a character, it will be repeated to fill the window width.
  130. If nil the separator is disabled. In `org-agenda-custom-commands' this
  131. addresses the separator between the current and the previous block."
  132. :group 'org-agenda
  133. :package-version '(Org . "9.6")
  134. :type '(choice
  135. (const :tag "Disabled" nil)
  136. (character)
  137. (string)))
  138. (defgroup org-agenda-export nil
  139. "Options concerning exporting agenda views in Org mode."
  140. :tag "Org Agenda Export"
  141. :group 'org-agenda)
  142. (defcustom org-agenda-with-colors t
  143. "Non-nil means use colors in agenda views."
  144. :group 'org-agenda-export
  145. :type 'boolean)
  146. (defcustom org-agenda-exporter-settings nil
  147. ;; FIXME: Do we really want to evaluate those settings and thus force
  148. ;; the user to use `quote' all the time?
  149. "Alist of variable/value pairs that should be active during agenda export.
  150. This is a good place to set options for ps-print and for htmlize.
  151. Note that the way this is implemented, the values will be evaluated
  152. before assigned to the variables. So make sure to quote values you do
  153. *not* want evaluated, for example
  154. (setq org-agenda-exporter-settings
  155. \\='((ps-print-color-p \\='black-white)))"
  156. :group 'org-agenda-export
  157. :type '(repeat
  158. (list
  159. (variable)
  160. (sexp :tag "Value"))))
  161. (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
  162. "Hook run in a temporary buffer before writing the agenda to an export file.
  163. A useful function for this hook is `org-agenda-add-entry-text'."
  164. :group 'org-agenda-export
  165. :type 'hook
  166. :options '(org-agenda-add-entry-text))
  167. (defcustom org-agenda-add-entry-text-maxlines 0
  168. "Maximum number of entry text lines to be added to agenda.
  169. This is only relevant when `org-agenda-add-entry-text' is part of
  170. `org-agenda-before-write-hook', which is the default.
  171. When this is 0, nothing will happen. When it is greater than 0, it
  172. specifies the maximum number of lines that will be added for each entry
  173. that is listed in the agenda view.
  174. Note that this variable is not used during display, only when exporting
  175. the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
  176. and `org-agenda-entry-text-maxlines'."
  177. :group 'org-agenda
  178. :type 'integer)
  179. (defcustom org-agenda-add-entry-text-descriptive-links t
  180. "Non-nil means export org-links as descriptive links in agenda added text.
  181. This variable applies to the text added to the agenda when
  182. `org-agenda-add-entry-text-maxlines' is larger than 0.
  183. When this variable is nil, the URL will (also) be shown."
  184. :group 'org-agenda
  185. :type 'boolean)
  186. (defcustom org-agenda-export-html-style nil
  187. "The style specification for exported HTML Agenda files.
  188. If this variable contains a string, it will replace the default <style>
  189. section as produced by `htmlize'.
  190. Since there are different ways of setting style information, this variable
  191. needs to contain the full HTML structure to provide a style, including the
  192. surrounding HTML tags. The style specifications should include definitions
  193. the fonts used by the agenda, here is an example:
  194. <style type=\"text/css\">
  195. p { font-weight: normal; color: gray; }
  196. .org-agenda-structure {
  197. font-size: 110%;
  198. color: #003399;
  199. font-weight: 600;
  200. }
  201. .org-todo {
  202. color: #cc6666;
  203. font-weight: bold;
  204. }
  205. .org-agenda-done {
  206. color: #339933;
  207. }
  208. .org-done {
  209. color: #339933;
  210. }
  211. .title { text-align: center; }
  212. .todo, .deadline { color: red; }
  213. .done { color: green; }
  214. </style>
  215. or, if you want to keep the style in a file,
  216. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  217. As the value of this option simply gets inserted into the HTML <head> header,
  218. you can \"misuse\" it to also add other text to the header."
  219. :group 'org-agenda-export
  220. :group 'org-export-html
  221. :type '(choice
  222. (const nil)
  223. (string)))
  224. (defcustom org-agenda-persistent-filter nil
  225. "When set, keep filters from one agenda view to the next."
  226. :group 'org-agenda
  227. :type 'boolean)
  228. (defgroup org-agenda-custom-commands nil
  229. "Options concerning agenda views in Org mode."
  230. :tag "Org Agenda Custom Commands"
  231. :group 'org-agenda)
  232. (defconst org-sorting-choice
  233. '(choice
  234. (const time-up) (const time-down)
  235. (const timestamp-up) (const timestamp-down)
  236. (const scheduled-up) (const scheduled-down)
  237. (const deadline-up) (const deadline-down)
  238. (const ts-up) (const ts-down)
  239. (const tsia-up) (const tsia-down)
  240. (const category-keep) (const category-up) (const category-down)
  241. (const tag-down) (const tag-up)
  242. (const priority-up) (const priority-down)
  243. (const todo-state-up) (const todo-state-down)
  244. (const effort-up) (const effort-down)
  245. (const habit-up) (const habit-down)
  246. (const alpha-up) (const alpha-down)
  247. (const user-defined-up) (const user-defined-down))
  248. "Sorting choices.")
  249. ;; Keep custom values for `org-agenda-filter-preset' compatible with
  250. ;; the new variable `org-agenda-tag-filter-preset'.
  251. (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
  252. (defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
  253. (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
  254. "List of types searched for when creating the daily/weekly agenda.
  255. This variable is a list of symbols that controls the types of
  256. items that appear in the daily/weekly agenda. Allowed symbols in this
  257. list are
  258. :timestamp List items containing a date stamp or date range matching
  259. the selected date. This includes sexp entries in angular
  260. brackets.
  261. :sexp List entries resulting from plain diary-like sexps.
  262. :deadline List deadline due on that date. When the date is today,
  263. also list any deadlines past due, or due within
  264. `org-deadline-warning-days'.
  265. :deadline* Same as above, but only include the deadline if it has an
  266. hour specification as [h]h:mm.
  267. :scheduled List all items which are scheduled for the given date.
  268. The diary for *today* also contains items which were
  269. scheduled earlier and are not yet marked DONE.
  270. :scheduled* Same as above, but only include the scheduled item if it
  271. has an hour specification as [h]h:mm.
  272. By default, all four non-starred types are turned on.
  273. When :scheduled* or :deadline* are included, :schedule or :deadline
  274. will be ignored.
  275. Never set this variable globally using `setq', because then it
  276. will apply to all future agenda commands. Instead, bind it with
  277. `let' to scope it dynamically into the agenda-constructing
  278. command. A good way to set it is through options in
  279. `org-agenda-custom-commands'. For a more flexible (though
  280. somewhat less efficient) way of determining what is included in
  281. the daily/weekly agenda, see `org-agenda-skip-function'.")
  282. (defconst org-agenda-custom-commands-local-options
  283. `(repeat :tag "Local settings for this command. Remember to quote values"
  284. (choice :tag "Setting"
  285. (list :tag "Heading for this block"
  286. (const org-agenda-overriding-header)
  287. (string :tag "Headline"))
  288. (list :tag "Files to be searched"
  289. (const org-agenda-files)
  290. (list
  291. (const :format "" quote)
  292. (repeat (file))))
  293. (list :tag "Sorting strategy"
  294. (const org-agenda-sorting-strategy)
  295. (list
  296. (const :format "" quote)
  297. (repeat
  298. ,org-sorting-choice)))
  299. (list :tag "Prefix format"
  300. (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
  301. (string))
  302. (list :tag "Number of days in agenda"
  303. (const org-agenda-span)
  304. (list
  305. (const :format "" quote)
  306. (choice (const :tag "Day" day)
  307. (const :tag "Week" week)
  308. (const :tag "Fortnight" fortnight)
  309. (const :tag "Month" month)
  310. (const :tag "Year" year)
  311. (integer :tag "Custom"))))
  312. (list :tag "Fixed starting date"
  313. (const org-agenda-start-day)
  314. (string :value "2007-11-01"))
  315. (list :tag "Start on day of week"
  316. (const org-agenda-start-on-weekday)
  317. (choice :value 1
  318. (const :tag "Today" nil)
  319. (integer :tag "Weekday No.")))
  320. (list :tag "Include data from diary"
  321. (const org-agenda-include-diary)
  322. (boolean))
  323. (list :tag "Deadline Warning days"
  324. (const org-deadline-warning-days)
  325. (integer :value 1))
  326. (list :tag "Category filter preset"
  327. (const org-agenda-category-filter-preset)
  328. (list
  329. (const :format "" quote)
  330. (repeat
  331. (string :tag "+category or -category"))))
  332. (list :tag "Tags filter preset"
  333. (const org-agenda-tag-filter-preset)
  334. (list
  335. (const :format "" quote)
  336. (repeat
  337. (string :tag "+tag or -tag"))))
  338. (list :tag "Effort filter preset"
  339. (const org-agenda-effort-filter-preset)
  340. (list
  341. (const :format "" quote)
  342. (repeat
  343. (string :tag "+=10 or -=10 or +<10 or ->10"))))
  344. (list :tag "Regexp filter preset"
  345. (const org-agenda-regexp-filter-preset)
  346. (list
  347. (const :format "" quote)
  348. (repeat
  349. (string :tag "+regexp or -regexp"))))
  350. (list :tag "Set daily/weekly entry types"
  351. (const org-agenda-entry-types)
  352. (list
  353. (const :format "" quote)
  354. (set :greedy t :value ,org-agenda-entry-types
  355. (const :deadline)
  356. (const :scheduled)
  357. (const :deadline*)
  358. (const :scheduled*)
  359. (const :timestamp)
  360. (const :sexp))))
  361. (list :tag "Columns format"
  362. (const org-overriding-columns-format)
  363. (string :tag "Format"))
  364. (list :tag "Standard skipping condition"
  365. :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
  366. (const org-agenda-skip-function)
  367. (list
  368. (const :format "" quote)
  369. (list
  370. (choice
  371. :tag "Skipping range"
  372. (const :tag "Skip entry" org-agenda-skip-entry-if)
  373. (const :tag "Skip subtree" org-agenda-skip-subtree-if))
  374. (repeat :inline t :tag "Conditions for skipping"
  375. (choice
  376. :tag "Condition type"
  377. (list :tag "Regexp matches" :inline t
  378. (const :format "" regexp)
  379. (regexp))
  380. (list :tag "Regexp does not match" :inline t
  381. (const :format "" notregexp)
  382. (regexp))
  383. (list :tag "TODO state is" :inline t
  384. (const todo)
  385. (choice
  386. (const :tag "Any not-done state" todo)
  387. (const :tag "Any done state" done)
  388. (const :tag "Any state" any)
  389. (list :tag "Keyword list"
  390. (const :format "" quote)
  391. (repeat (string :tag "Keyword")))))
  392. (list :tag "TODO state is not" :inline t
  393. (const nottodo)
  394. (choice
  395. (const :tag "Any not-done state" todo)
  396. (const :tag "Any done state" done)
  397. (const :tag "Any state" any)
  398. (list :tag "Keyword list"
  399. (const :format "" quote)
  400. (repeat (string :tag "Keyword")))))
  401. (const :tag "scheduled" scheduled)
  402. (const :tag "not scheduled" notscheduled)
  403. (const :tag "deadline" deadline)
  404. (const :tag "no deadline" notdeadline)
  405. (const :tag "timestamp" timestamp)
  406. (const :tag "no timestamp" nottimestamp))))))
  407. (list :tag "Non-standard skipping condition"
  408. :value (org-agenda-skip-function)
  409. (const org-agenda-skip-function)
  410. (sexp :tag "Function or form (quoted!)"))
  411. (list :tag "Any variable"
  412. (variable :tag "Variable")
  413. (sexp :tag "Value (sexp)"))))
  414. "Selection of examples for agenda command settings.
  415. This will be spliced into the custom type of
  416. `org-agenda-custom-commands'.")
  417. (defcustom org-agenda-custom-commands
  418. '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
  419. "Custom commands for the agenda.
  420. \\<org-mode-map>
  421. These commands will be offered on the splash screen displayed by the
  422. agenda dispatcher `\\[org-agenda]'. Each entry is a list like this:
  423. (key desc type match settings files)
  424. key The key (one or more characters as a string) to be associated
  425. with the command.
  426. desc A description of the command. When omitted or nil, a default
  427. description is built using MATCH.
  428. type The command type, any of the following symbols:
  429. agenda The daily/weekly agenda.
  430. agenda* Appointments for current week/day.
  431. todo Entries with a specific TODO keyword, in all agenda files.
  432. search Entries containing search words entry or headline.
  433. tags Tags/Property/TODO match in all agenda files.
  434. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  435. todo-tree Sparse tree of specific TODO keyword in *current* file.
  436. tags-tree Sparse tree with all tags matches in *current* file.
  437. occur-tree Occur sparse tree for *current* file.
  438. alltodo The global TODO list.
  439. stuck Stuck projects.
  440. ... A user-defined function.
  441. match What to search for:
  442. - a single keyword for TODO keyword searches
  443. - a tags/property/todo match expression for searches
  444. - a word search expression for text searches.
  445. - a regular expression for occur searches
  446. For all other commands, this should be the empty string.
  447. settings A list of option settings, similar to that in a let form, so like
  448. this: ((opt1 val1) (opt2 val2) ...). The values will be
  449. evaluated at the moment of execution, so quote them when needed.
  450. files A list of files to write the produced agenda buffer to with
  451. the command `org-store-agenda-views'.
  452. If a file name ends in \".html\", an HTML version of the buffer
  453. is written out. If it ends in \".ps\", a PostScript version is
  454. produced. Otherwise, only the plain text is written to the file.
  455. You can also define a set of commands, to create a composite agenda buffer.
  456. In this case, an entry looks like this:
  457. (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
  458. where
  459. desc A description string to be displayed in the dispatcher menu.
  460. cmd An agenda command, similar to the above. However, tree commands
  461. are not allowed. Valid commands for a set are:
  462. (agenda \"\" settings)
  463. (agenda* \"\" settings)
  464. (alltodo \"\" settings)
  465. (stuck \"\" settings)
  466. (todo \"match\" settings files)
  467. (search \"match\" settings files)
  468. (tags \"match\" settings files)
  469. (tags-todo \"match\" settings files)
  470. Each command can carry a list of options, and another set of options can be
  471. given for the whole set of commands. Individual command options take
  472. precedence over the general options.
  473. When using several characters as key to a command, the first characters
  474. are prefix commands. For the dispatcher to display useful information, you
  475. should provide a description for the prefix, like
  476. (setq org-agenda-custom-commands
  477. \\='((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  478. (\"hl\" tags \"+HOME+Lisa\")
  479. (\"hp\" tags \"+HOME+Peter\")
  480. (\"hk\" tags \"+HOME+Kim\")))
  481. See also Info node `(org) Custom Agenda Views'."
  482. :group 'org-agenda-custom-commands
  483. :type `(repeat
  484. (choice :value ("x" "Describe command here" tags "" nil)
  485. (list :tag "Single command"
  486. (string :tag "Access Key(s) ")
  487. (option (string :tag "Description"))
  488. (choice
  489. (const :tag "Agenda" agenda)
  490. (const :tag "TODO list" alltodo)
  491. (const :tag "Search words" search)
  492. (const :tag "Stuck projects" stuck)
  493. (const :tag "Tags/Property match (all agenda files)" tags)
  494. (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
  495. (const :tag "TODO keyword search (all agenda files)" todo)
  496. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  497. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  498. (const :tag "Occur tree (current buffer)" occur-tree)
  499. (sexp :tag "Other, user-defined function"))
  500. (string :tag "Match (only for some commands)")
  501. ,org-agenda-custom-commands-local-options
  502. (option (repeat :tag "Export" (file :tag "Export to"))))
  503. (list :tag "Command series, all agenda files"
  504. (string :tag "Access Key(s)")
  505. (string :tag "Description ")
  506. (repeat :tag "Component"
  507. (choice
  508. (list :tag "Agenda"
  509. (const :format "" agenda)
  510. (const :tag "" :format "" "")
  511. ,org-agenda-custom-commands-local-options)
  512. (list :tag "TODO list (all keywords)"
  513. (const :format "" alltodo)
  514. (const :tag "" :format "" "")
  515. ,org-agenda-custom-commands-local-options)
  516. (list :tag "Search words"
  517. (const :format "" search)
  518. (string :tag "Match")
  519. ,org-agenda-custom-commands-local-options)
  520. (list :tag "Stuck projects"
  521. (const :format "" stuck)
  522. (const :tag "" :format "" "")
  523. ,org-agenda-custom-commands-local-options)
  524. (list :tag "Tags/Property match (all agenda files)"
  525. (const :format "" tags)
  526. (string :tag "Match")
  527. ,org-agenda-custom-commands-local-options)
  528. (list :tag "Tags/Property match of TODO entries (all agenda files)"
  529. (const :format "" tags-todo)
  530. (string :tag "Match")
  531. ,org-agenda-custom-commands-local-options)
  532. (list :tag "TODO keyword search"
  533. (const :format "" todo)
  534. (string :tag "Match")
  535. ,org-agenda-custom-commands-local-options)
  536. (list :tag "Other, user-defined function"
  537. (symbol :tag "function")
  538. (string :tag "Match")
  539. ,org-agenda-custom-commands-local-options)))
  540. (repeat :tag "Settings for entire command set"
  541. (list (variable :tag "Any variable")
  542. (sexp :tag "Value")))
  543. (option (repeat :tag "Export" (file :tag "Export to"))))
  544. (cons :tag "Prefix key documentation"
  545. (string :tag "Access Key(s)")
  546. (string :tag "Description ")))))
  547. (defcustom org-agenda-query-register ?o
  548. "The register holding the current query string.
  549. The purpose of this is that if you construct a query string interactively,
  550. you can then use it to define a custom command."
  551. :group 'org-agenda-custom-commands
  552. :type 'character)
  553. (defcustom org-stuck-projects
  554. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  555. "How to identify stuck projects.
  556. This is a list of four items:
  557. 1. A tags/todo/property matcher string that is used to identify a project.
  558. See Info node `(org) Matching tags and properties' for a
  559. description of tag and property searches. The entire tree
  560. below a headline matched by this is considered one project.
  561. 2. A list of TODO keywords identifying non-stuck projects.
  562. If the project subtree contains any headline with one of these todo
  563. keywords, the project is considered to be not stuck. If you specify
  564. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  565. 3. A list of tags identifying non-stuck projects.
  566. If the project subtree contains any headline with one of these tags,
  567. the project is considered to be not stuck. If you specify \"*\" as
  568. a tag, any tag will mark the project unstuck. Note that this is about
  569. the explicit presence of a tag somewhere in the subtree, inherited
  570. tags do not count here. If inherited tags make a project not stuck,
  571. use \"-TAG\" in the tags part of the matcher under (1.) above.
  572. 4. An arbitrary regular expression matching non-stuck projects.
  573. If the project turns out to be not stuck, search continues also in the
  574. subtree to see if any of the subtasks have project status.
  575. See also the variable `org-tags-match-list-sublevels' which applies
  576. to projects matched by this search as well.
  577. After defining this variable, you may use `org-agenda-list-stuck-projects'
  578. \(bound to `\\[org-agenda] #') to produce the list."
  579. :group 'org-agenda-custom-commands
  580. :type '(list
  581. (string :tag "Tags/TODO match to identify a project")
  582. (repeat :tag "Projects are *not* stuck if they have an entry with \
  583. TODO keyword any of" (string))
  584. (repeat :tag "Projects are *not* stuck if they have an entry with \
  585. TAG being any of" (string))
  586. (regexp :tag "Projects are *not* stuck if this regexp matches inside \
  587. the subtree")))
  588. (defgroup org-agenda-skip nil
  589. "Options concerning skipping parts of agenda files."
  590. :tag "Org Agenda Skip"
  591. :group 'org-agenda)
  592. (defcustom org-agenda-skip-function-global nil
  593. "Function to be called at each match during agenda construction.
  594. If this function returns nil, the current match should not be skipped.
  595. If the function decided to skip an agenda match, is must return the
  596. buffer position from which the search should be continued.
  597. This may also be a Lisp form, which will be evaluated.
  598. This variable will be applied to every agenda match, including
  599. tags/property searches and TODO lists. So try to make the test function
  600. do its checking as efficiently as possible. To implement a skipping
  601. condition just for specific agenda commands, use the variable
  602. `org-agenda-skip-function' which can be set in the options section
  603. of custom agenda commands."
  604. :group 'org-agenda-skip
  605. :type 'sexp)
  606. (defgroup org-agenda-daily/weekly nil
  607. "Options concerning the daily/weekly agenda."
  608. :tag "Org Agenda Daily/Weekly"
  609. :group 'org-agenda)
  610. (defgroup org-agenda-todo-list nil
  611. "Options concerning the global todo list agenda view."
  612. :tag "Org Agenda Todo List"
  613. :group 'org-agenda)
  614. (defgroup org-agenda-match-view nil
  615. "Options concerning the general tags/property/todo match agenda view."
  616. :tag "Org Agenda Match View"
  617. :group 'org-agenda)
  618. (defgroup org-agenda-search-view nil
  619. "Options concerning the search agenda view."
  620. :tag "Org Agenda Search View"
  621. :group 'org-agenda)
  622. (defvar org-agenda-archives-mode nil
  623. "Non-nil means the agenda will include archived items.
  624. If this is the symbol `trees', trees in the selected agenda scope
  625. that are marked with the ARCHIVE tag will be included anyway. When this is
  626. t, also all archive files associated with the current selection of agenda
  627. files will be included.")
  628. (defcustom org-agenda-restriction-lock-highlight-subtree t
  629. "Non-nil means highlight the whole subtree when restriction is active.
  630. Otherwise only highlight the headline. Highlighting the whole subtree is
  631. useful to ensure no edits happen beyond the restricted region."
  632. :group 'org-agenda
  633. :type 'boolean)
  634. (defcustom org-agenda-skip-comment-trees t
  635. "Non-nil means skip trees that start with the COMMENT keyword.
  636. When nil, these trees are also scanned by agenda commands."
  637. :group 'org-agenda-skip
  638. :type 'boolean)
  639. (defcustom org-agenda-todo-list-sublevels t
  640. "Non-nil means check also the sublevels of a TODO entry for TODO entries.
  641. When nil, the sublevels of a TODO entry are not checked, resulting in
  642. potentially much shorter TODO lists."
  643. :group 'org-agenda-skip
  644. :group 'org-agenda-todo-list
  645. :type 'boolean)
  646. (defcustom org-agenda-todo-ignore-with-date nil
  647. "Non-nil means don't show entries with a date in the global todo list.
  648. You can use this if you prefer to mark mere appointments with a TODO keyword,
  649. but don't want them to show up in the TODO list.
  650. When this is set, it also covers deadlines and scheduled items, the settings
  651. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  652. will be ignored.
  653. See also the variable `org-agenda-tags-todo-honor-ignore-options'."
  654. :group 'org-agenda-skip
  655. :group 'org-agenda-todo-list
  656. :type 'boolean)
  657. (defcustom org-agenda-todo-ignore-timestamp nil
  658. "Non-nil means don't show entries with a timestamp.
  659. This applies when creating the global todo list.
  660. Valid values are:
  661. past Don't show entries for today or in the past.
  662. future Don't show entries with a timestamp in the future.
  663. The idea behind this is that if it has a future
  664. timestamp, you don't want to think about it until the
  665. date.
  666. all Don't show any entries with a timestamp in the global todo list.
  667. The idea behind this is that by setting a timestamp, you
  668. have already \"taken care\" of this item.
  669. This variable can also have an integer as a value. If positive (N),
  670. todos with a timestamp N or more days in the future will be ignored. If
  671. negative (-N), todos with a timestamp N or more days in the past will be
  672. ignored. If 0, todos with a timestamp either today or in the future will
  673. be ignored. For example, a value of -1 will exclude todos with a
  674. timestamp in the past (yesterday or earlier), while a value of 7 will
  675. exclude todos with a timestamp a week or more in the future.
  676. See also `org-agenda-todo-ignore-with-date'.
  677. See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
  678. to make his option also apply to the tags-todo list."
  679. :group 'org-agenda-skip
  680. :group 'org-agenda-todo-list
  681. :version "24.1"
  682. :type '(choice
  683. (const :tag "Ignore future timestamp todos" future)
  684. (const :tag "Ignore past or present timestamp todos" past)
  685. (const :tag "Ignore all timestamp todos" all)
  686. (const :tag "Show timestamp todos" nil)
  687. (integer :tag "Ignore if N or more days in past(-) or future(+).")))
  688. (defcustom org-agenda-todo-ignore-scheduled nil
  689. "Non-nil means, ignore some scheduled TODO items when making TODO list.
  690. This applies when creating the global todo list.
  691. Valid values are:
  692. past Don't show entries scheduled today or in the past.
  693. future Don't show entries scheduled in the future.
  694. The idea behind this is that by scheduling it, you don't want to
  695. think about it until the scheduled date.
  696. all Don't show any scheduled entries in the global todo list.
  697. The idea behind this is that by scheduling it, you have already
  698. \"taken care\" of this item.
  699. t Same as `all', for backward compatibility.
  700. This variable can also have an integer as a value. See
  701. `org-agenda-todo-ignore-timestamp' for more details.
  702. See also `org-agenda-todo-ignore-with-date'.
  703. See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
  704. to make his option also apply to the tags-todo list."
  705. :group 'org-agenda-skip
  706. :group 'org-agenda-todo-list
  707. :type '(choice
  708. (const :tag "Ignore future-scheduled todos" future)
  709. (const :tag "Ignore past- or present-scheduled todos" past)
  710. (const :tag "Ignore all scheduled todos" all)
  711. (const :tag "Ignore all scheduled todos (compatibility)" t)
  712. (const :tag "Show scheduled todos" nil)
  713. (integer :tag "Ignore if N or more days in past(-) or future(+).")))
  714. (defcustom org-agenda-todo-ignore-deadlines nil
  715. "Non-nil means ignore some deadline TODO items when making TODO list.
  716. There are different motivations for using different values, please think
  717. carefully when configuring this variable.
  718. This applies when creating the global TODO list.
  719. Valid values are:
  720. near Don't show near deadline entries. A deadline is near when it is
  721. closer than `org-deadline-warning-days' days. The idea behind this
  722. is that such items will appear in the agenda anyway.
  723. far Don't show TODO entries where a deadline has been defined, but
  724. is not going to happen anytime soon. This is useful if you want to use
  725. the TODO list to figure out what to do now.
  726. past Don't show entries with a deadline timestamp for today or in the past.
  727. future Don't show entries with a deadline timestamp in the future, not even
  728. when they become `near' ones. Use it with caution.
  729. all Ignore all TODO entries that do have a deadline.
  730. t Same as `near', for backward compatibility.
  731. This variable can also have an integer as a value. See
  732. `org-agenda-todo-ignore-timestamp' for more details.
  733. See also `org-agenda-todo-ignore-with-date'.
  734. See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
  735. to make his option also apply to the tags-todo list."
  736. :group 'org-agenda-skip
  737. :group 'org-agenda-todo-list
  738. :type '(choice
  739. (const :tag "Ignore near deadlines" near)
  740. (const :tag "Ignore near deadlines (compatibility)" t)
  741. (const :tag "Ignore far deadlines" far)
  742. (const :tag "Ignore all TODOs with a deadlines" all)
  743. (const :tag "Show all TODOs, even if they have a deadline" nil)
  744. (integer :tag "Ignore if N or more days in past(-) or future(+).")))
  745. (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
  746. "Time unit to use when possibly ignoring an agenda item.
  747. See the docstring of various `org-agenda-todo-ignore-*' options.
  748. The default is to compare time stamps using days. An item is thus
  749. considered to be in the future if it is at least one day after today.
  750. Non-nil means to compare time stamps using seconds. An item is then
  751. considered future if it has a time value later than current time."
  752. :group 'org-agenda-skip
  753. :group 'org-agenda-todo-list
  754. :version "24.4"
  755. :package-version '(Org . "8.0")
  756. :type '(choice
  757. (const :tag "Compare time with days" nil)
  758. (const :tag "Compare time with seconds" t)))
  759. (defcustom org-agenda-tags-todo-honor-ignore-options nil
  760. "Non-nil means honor todo-list ignores options also in tags-todo search.
  761. The variables
  762. `org-agenda-todo-ignore-with-date',
  763. `org-agenda-todo-ignore-timestamp',
  764. `org-agenda-todo-ignore-scheduled',
  765. `org-agenda-todo-ignore-deadlines'
  766. make the global TODO list skip entries that have time stamps of certain
  767. kinds. If this option is set, the same options will also apply for the
  768. tags-todo search, which is the general tags/property matcher
  769. restricted to unfinished TODO entries only."
  770. :group 'org-agenda-skip
  771. :group 'org-agenda-todo-list
  772. :group 'org-agenda-match-view
  773. :type 'boolean)
  774. (defcustom org-agenda-skip-scheduled-if-done nil
  775. "Non-nil means don't show scheduled items in agenda when they are done.
  776. This is relevant for the daily/weekly agenda, not for the TODO list. It
  777. applies only to the actual date of the scheduling. Warnings about an item
  778. with a past scheduling dates are always turned off when the item is DONE."
  779. :group 'org-agenda-skip
  780. :group 'org-agenda-daily/weekly
  781. :type 'boolean)
  782. (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
  783. "Non-nil means skip scheduling line if same entry shows because of deadline.
  784. In the agenda of today, an entry can show up multiple times
  785. because it is both scheduled and has a nearby deadline, and maybe
  786. a plain time stamp as well.
  787. When this variable is nil, the entry will be shown several times.
  788. When set to t, then only the deadline is shown and the fact that
  789. the entry is scheduled today or was scheduled previously is not
  790. shown.
  791. When set to the symbol `not-today', skip scheduled previously,
  792. but not scheduled today.
  793. When set to the symbol `repeated-after-deadline', skip scheduled
  794. items if they are repeated beyond the current deadline."
  795. :group 'org-agenda-skip
  796. :group 'org-agenda-daily/weekly
  797. :type '(choice
  798. (const :tag "Never" nil)
  799. (const :tag "Always" t)
  800. (const :tag "Not when scheduled today" not-today)
  801. (const :tag "When repeated past deadline" repeated-after-deadline)))
  802. (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
  803. "Non-nil means skip timestamp line if same entry shows because of deadline.
  804. In the agenda of today, an entry can show up multiple times
  805. because it has both a plain timestamp and has a nearby deadline.
  806. When this variable is t, then only the deadline is shown and the
  807. fact that the entry has a timestamp for or including today is not
  808. shown. When this variable is nil, the entry will be shown
  809. several times."
  810. :group 'org-agenda-skip
  811. :group 'org-agenda-daily/weekly
  812. :version "24.1"
  813. :type '(choice
  814. (const :tag "Never" nil)
  815. (const :tag "Always" t)))
  816. (defcustom org-agenda-skip-deadline-if-done nil
  817. "Non-nil means don't show deadlines when the corresponding item is done.
  818. When nil, the deadline is still shown and should give you a happy feeling.
  819. This is relevant for the daily/weekly agenda. It applies only to the
  820. actual date of the deadline. Warnings about approaching and past-due
  821. deadlines are always turned off when the item is DONE."
  822. :group 'org-agenda-skip
  823. :group 'org-agenda-daily/weekly
  824. :type 'boolean)
  825. (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
  826. "Non-nil means skip deadline prewarning when entry is also scheduled.
  827. This will apply on all days where a prewarning for the deadline would
  828. be shown, but not at the day when the entry is actually due. On that day,
  829. the deadline will be shown anyway.
  830. This variable may be set to nil, t, the symbol `pre-scheduled',
  831. or a number which will then give the number of days before the actual
  832. deadline when the prewarnings should resume. The symbol `pre-scheduled'
  833. eliminates the deadline prewarning only prior to the scheduled date.
  834. This can be used in a workflow where the first showing of the deadline will
  835. trigger you to schedule it, and then you don't want to be reminded of it
  836. because you will take care of it on the day when scheduled."
  837. :group 'org-agenda-skip
  838. :group 'org-agenda-daily/weekly
  839. :version "24.1"
  840. :type '(choice
  841. (const :tag "Always show prewarning" nil)
  842. (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
  843. (const :tag "Remove prewarning if entry is scheduled" t)
  844. (integer :tag "Restart prewarning N days before deadline")))
  845. (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
  846. "Non-nil means skip scheduled delay when entry also has a deadline.
  847. This variable may be set to nil, t, the symbol `post-deadline',
  848. or a number which will then give the number of days after the actual
  849. scheduled date when the delay should expire. The symbol `post-deadline'
  850. eliminates the schedule delay when the date is posterior to the deadline."
  851. :group 'org-agenda-skip
  852. :group 'org-agenda-daily/weekly
  853. :version "24.4"
  854. :package-version '(Org . "8.0")
  855. :type '(choice
  856. (const :tag "Always honor delay" nil)
  857. (const :tag "Ignore delay if posterior to the deadline" post-deadline)
  858. (const :tag "Ignore delay if entry has a deadline" t)
  859. (integer :tag "Honor delay up until N days after the scheduled date")))
  860. (defcustom org-agenda-skip-additional-timestamps-same-entry nil
  861. "When nil, multiple same-day timestamps in entry make multiple agenda lines.
  862. When non-nil, after the search for timestamps has matched once in an
  863. entry, the rest of the entry will not be searched."
  864. :group 'org-agenda-skip
  865. :type 'boolean)
  866. (defcustom org-agenda-skip-timestamp-if-done nil
  867. "Non-nil means don't select item by timestamp or -range if it is DONE."
  868. :group 'org-agenda-skip
  869. :group 'org-agenda-daily/weekly
  870. :type 'boolean)
  871. (defcustom org-agenda-dim-blocked-tasks t
  872. "Non-nil means dim blocked tasks in the agenda display.
  873. This causes some overhead during agenda construction, but if you
  874. have turned on `org-enforce-todo-dependencies',
  875. `org-enforce-todo-checkbox-dependencies', or any other blocking
  876. mechanism, this will create useful feedback in the agenda.
  877. Instead of t, this variable can also have the value `invisible'.
  878. Then blocked tasks will be invisible and only become visible when
  879. they become unblocked. An exemption to this behavior is when a task is
  880. blocked because of unchecked checkboxes below it. Since checkboxes do
  881. not show up in the agenda views, making this task invisible you remove any
  882. trace from agenda views that there is something to do. Therefore, a task
  883. that is blocked because of checkboxes will never be made invisible, it
  884. will only be dimmed."
  885. :group 'org-agenda-daily/weekly
  886. :group 'org-agenda-todo-list
  887. :version "24.3"
  888. :type '(choice
  889. (const :tag "Do not dim" nil)
  890. (const :tag "Dim to a gray face" t)
  891. (const :tag "Make invisible" invisible)))
  892. (defgroup org-agenda-startup nil
  893. "Options concerning initial settings in the Agenda in Org Mode."
  894. :tag "Org Agenda Startup"
  895. :group 'org-agenda)
  896. (defcustom org-agenda-menu-show-matcher t
  897. "Non-nil means show the match string in the agenda dispatcher menu.
  898. When nil, the matcher string is not shown, but is put into the help-echo
  899. property so than moving the mouse over the command shows it.
  900. Setting it to nil is good if matcher strings are very long and/or if
  901. you want to use two-columns display (see `org-agenda-menu-two-columns')."
  902. :group 'org-agenda
  903. :version "24.1"
  904. :type 'boolean)
  905. (defcustom org-agenda-menu-two-columns nil
  906. "Non-nil means, use two columns to show custom commands in the dispatcher.
  907. If you use this, you probably want to set `org-agenda-menu-show-matcher'
  908. to nil."
  909. :group 'org-agenda
  910. :version "24.1"
  911. :type 'boolean)
  912. (defcustom org-agenda-finalize-hook nil
  913. "Hook run just before displaying an agenda buffer.
  914. The buffer is still writable when the hook is called.
  915. You can modify some of the buffer substrings but you should be
  916. extra careful not to modify the text properties of the agenda
  917. headlines as the agenda display heavily relies on them."
  918. :group 'org-agenda-startup
  919. :type 'hook)
  920. (defcustom org-agenda-filter-hook nil
  921. "Hook run just after filtering with `org-agenda-filter'."
  922. :group 'org-agenda-startup
  923. :package-version '(Org . "9.4")
  924. :type 'hook)
  925. (defcustom org-agenda-mouse-1-follows-link nil
  926. "Non-nil means mouse-1 on a link will follow the link in the agenda.
  927. A longer mouse click will still set point. Needs to be set
  928. before org.el is loaded."
  929. :group 'org-agenda-startup
  930. :type 'boolean)
  931. (defcustom org-agenda-start-with-follow-mode nil
  932. "The initial value of follow mode in a newly created agenda window."
  933. :group 'org-agenda-startup
  934. :type 'boolean)
  935. (defcustom org-agenda-follow-indirect nil
  936. "Non-nil means `org-agenda-follow-mode' displays only the
  937. current item's tree, in an indirect buffer."
  938. :group 'org-agenda
  939. :version "24.1"
  940. :type 'boolean)
  941. (defcustom org-agenda-show-outline-path t
  942. "Non-nil means show outline path in echo area after line motion."
  943. :group 'org-agenda-startup
  944. :type 'boolean)
  945. (defcustom org-agenda-start-with-entry-text-mode nil
  946. "The initial value of entry-text-mode in a newly created agenda window."
  947. :group 'org-agenda-startup
  948. :type 'boolean)
  949. (defcustom org-agenda-entry-text-maxlines 5
  950. "Number of text lines to be added when `E' is pressed in the agenda.
  951. Note that this variable only used during agenda display. To add entry text
  952. when exporting the agenda, configure the variable
  953. `org-agenda-add-entry-text-maxlines'."
  954. :group 'org-agenda
  955. :type 'integer)
  956. (defcustom org-agenda-entry-text-exclude-regexps nil
  957. "List of regular expressions to clean up entry text.
  958. The complete matches of all regular expressions in this list will be
  959. removed from entry text before it is shown in the agenda."
  960. :group 'org-agenda
  961. :type '(repeat (regexp)))
  962. (defcustom org-agenda-entry-text-leaders " > "
  963. "Text prepended to the entry text in agenda buffers."
  964. :version "24.4"
  965. :package-version '(Org . "8.0")
  966. :group 'org-agenda
  967. :type 'string)
  968. (defvar org-agenda-entry-text-cleanup-hook nil
  969. "Hook that is run after basic cleanup of entry text to be shown in agenda.
  970. This cleanup is done in a temporary buffer, so the function may inspect and
  971. change the entire buffer.
  972. Some default stuff like drawers and scheduling/deadline dates will already
  973. have been removed when this is called, as will any matches for regular
  974. expressions listed in `org-agenda-entry-text-exclude-regexps'.")
  975. (defvar org-agenda-include-inactive-timestamps nil
  976. "Non-nil means include inactive time stamps in agenda.
  977. Dynamically scoped.")
  978. (defgroup org-agenda-windows nil
  979. "Options concerning the windows used by the Agenda in Org Mode."
  980. :tag "Org Agenda Windows"
  981. :group 'org-agenda)
  982. (defcustom org-agenda-window-setup 'reorganize-frame
  983. "How the agenda buffer should be displayed.
  984. Possible values for this option are:
  985. current-window Show agenda in the current window, keeping all other windows.
  986. other-window Use `switch-to-buffer-other-window' to display agenda.
  987. only-window Show agenda, deleting all other windows.
  988. reorganize-frame Show only two windows on the current frame, the current
  989. window and the agenda.
  990. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  991. Also, when exiting the agenda, kill that frame.
  992. other-tab Use `switch-to-buffer-other-tab' to display the
  993. agenda, making use of the `tab-bar-mode' introduced
  994. in Emacs version 27.1. Also, kill that tab when
  995. exiting the agenda view.
  996. See also the variable `org-agenda-restore-windows-after-quit'."
  997. :group 'org-agenda-windows
  998. :type '(choice
  999. (const current-window)
  1000. (const other-frame)
  1001. (const other-tab)
  1002. (const other-window)
  1003. (const only-window)
  1004. (const reorganize-frame))
  1005. :package-version '(Org . "9.4"))
  1006. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  1007. "The min and max height of the agenda window as a fraction of frame height.
  1008. The value of the variable is a cons cell with two numbers between 0 and 1.
  1009. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  1010. :group 'org-agenda-windows
  1011. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  1012. (defcustom org-agenda-restore-windows-after-quit nil
  1013. "Non-nil means restore window configuration upon exiting agenda.
  1014. Before the window configuration is changed for displaying the
  1015. agenda, the current status is recorded. When the agenda is
  1016. exited with `q' or `x' and this option is set, the old state is
  1017. restored. If `org-agenda-window-setup' is `other-frame' or
  1018. `other-tab', the value of this option will be ignored."
  1019. :group 'org-agenda-windows
  1020. :type 'boolean)
  1021. (defcustom org-agenda-span 'week
  1022. "Number of days to include in overview display.
  1023. Can be day, week, month, year, or any number of days.
  1024. Custom commands can set this variable in the options section."
  1025. :group 'org-agenda-daily/weekly
  1026. :type '(choice (const :tag "Day" day)
  1027. (const :tag "Week" week)
  1028. (const :tag "Fortnight" fortnight)
  1029. (const :tag "Month" month)
  1030. (const :tag "Year" year)
  1031. (integer :tag "Custom")))
  1032. (defcustom org-agenda-start-on-weekday 1
  1033. "Non-nil means start the overview always on the specified weekday.
  1034. 0 denotes Sunday, 1 denotes Monday, etc.
  1035. When nil, always start on the current day.
  1036. Custom commands can set this variable in the options section."
  1037. :group 'org-agenda-daily/weekly
  1038. :type '(choice (const :tag "Today" nil)
  1039. (integer :tag "Weekday No.")))
  1040. (defcustom org-agenda-show-all-dates t
  1041. "Non-nil means `org-agenda' shows every day in the selected range.
  1042. When nil, only the days which actually have entries are shown."
  1043. :group 'org-agenda-daily/weekly
  1044. :type 'boolean)
  1045. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  1046. "Format string for displaying dates in the agenda.
  1047. Used by the daily/weekly agenda. This should be a format string
  1048. understood by `format-time-string', or a function returning the
  1049. formatted date as a string. The function must take a single
  1050. argument, a calendar-style date list like (month day year)."
  1051. :group 'org-agenda-daily/weekly
  1052. :type '(choice
  1053. (string :tag "Format string")
  1054. (function :tag "Function")))
  1055. (defun org-agenda-end-of-line ()
  1056. "Go to the end of visible line."
  1057. (interactive)
  1058. (goto-char (line-end-position)))
  1059. (defun org-agenda-format-date-aligned (date)
  1060. "Format a DATE string for display in the daily/weekly agenda.
  1061. This function makes sure that dates are aligned for easy reading."
  1062. (require 'cal-iso)
  1063. (let* ((dayname (calendar-day-name date))
  1064. (day (cadr date))
  1065. (day-of-week (calendar-day-of-week date))
  1066. (month (car date))
  1067. (monthname (calendar-month-name month))
  1068. (year (nth 2 date))
  1069. (iso-week (org-days-to-iso-week
  1070. (calendar-absolute-from-gregorian date)))
  1071. ;; (weekyear (cond ((and (= month 1) (>= iso-week 52))
  1072. ;; (1- year))
  1073. ;; ((and (= month 12) (<= iso-week 1))
  1074. ;; (1+ year))
  1075. ;; (t year)))
  1076. (weekstring (if (= day-of-week 1)
  1077. (format " W%02d" iso-week)
  1078. "")))
  1079. (format "%-10s %2d %s %4d%s"
  1080. dayname day monthname year weekstring)))
  1081. (defcustom org-agenda-time-leading-zero nil
  1082. "Non-nil means use leading zero for military times in agenda.
  1083. For example, 9:30am would become 09:30 rather than 9:30."
  1084. :group 'org-agenda-daily/weekly
  1085. :version "24.1"
  1086. :type 'boolean)
  1087. (defcustom org-agenda-timegrid-use-ampm nil
  1088. "When set, show AM/PM style timestamps on the timegrid."
  1089. :group 'org-agenda
  1090. :version "24.1"
  1091. :type 'boolean)
  1092. (defcustom org-agenda-clock-report-header nil
  1093. "Header inserted before the table in Org agenda clock report mode.
  1094. See Info node `(org) Agenda Commands' for more details."
  1095. :group 'org-agenda
  1096. :type '(choice
  1097. (string :tag "Header")
  1098. (const :tag "No header" nil))
  1099. :safe #'stringp
  1100. :package-version '(Org . "9.6"))
  1101. (defun org-agenda-time-of-day-to-ampm (time)
  1102. "Convert TIME of a string like \"13:45\" to an AM/PM style time string."
  1103. (let* ((hour-number (string-to-number (substring time 0 -3)))
  1104. (minute (substring time -2))
  1105. (ampm "am"))
  1106. (cond
  1107. ((equal hour-number 12)
  1108. (setq ampm "pm"))
  1109. ((> hour-number 12)
  1110. (setq ampm "pm")
  1111. (setq hour-number (- hour-number 12))))
  1112. (concat
  1113. (if org-agenda-time-leading-zero
  1114. (format "%02d" hour-number)
  1115. (format "%02s" (number-to-string hour-number)))
  1116. ":" minute ampm)))
  1117. (defun org-agenda-time-of-day-to-ampm-maybe (time)
  1118. "Conditionally convert TIME to AM/PM format.
  1119. This is based on `org-agenda-timegrid-use-ampm'."
  1120. (if org-agenda-timegrid-use-ampm
  1121. (org-agenda-time-of-day-to-ampm time)
  1122. time))
  1123. (defcustom org-agenda-weekend-days '(6 0)
  1124. "Which days are weekend?
  1125. These days get the special face `org-agenda-date-weekend' in the agenda."
  1126. :group 'org-agenda-daily/weekly
  1127. :type '(set :greedy t
  1128. (const :tag "Monday" 1)
  1129. (const :tag "Tuesday" 2)
  1130. (const :tag "Wednesday" 3)
  1131. (const :tag "Thursday" 4)
  1132. (const :tag "Friday" 5)
  1133. (const :tag "Saturday" 6)
  1134. (const :tag "Sunday" 0)))
  1135. (defcustom org-agenda-move-date-from-past-immediately-to-today t
  1136. "Non-nil means jump to today when moving a past date forward in time.
  1137. When using S-right in the agenda to move a date forward, and the date
  1138. stamp currently points to the past, the first key press will move it
  1139. to today. When nil, just move one day forward even if the date stays
  1140. in the past."
  1141. :group 'org-agenda-daily/weekly
  1142. :version "24.1"
  1143. :type 'boolean)
  1144. (defcustom org-agenda-diary-file 'diary-file
  1145. "File to which to add new entries with the `i' key in agenda and calendar.
  1146. When this is the symbol `diary-file', the functionality in the Emacs
  1147. calendar will be used to add entries to the `diary-file'. But when this
  1148. points to a file, `org-agenda-diary-entry' will be used instead."
  1149. :group 'org-agenda
  1150. :type '(choice
  1151. (const :tag "The standard Emacs diary file" diary-file)
  1152. (file :tag "Special Org file diary entries")))
  1153. (defcustom org-agenda-include-diary nil
  1154. "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
  1155. Custom commands can set this variable in the options section."
  1156. :group 'org-agenda-daily/weekly
  1157. :type 'boolean)
  1158. (defcustom org-agenda-include-deadlines t
  1159. "If non-nil, include entries within their deadline warning period.
  1160. Custom commands can set this variable in the options section."
  1161. :group 'org-agenda-daily/weekly
  1162. :version "24.1"
  1163. :type 'boolean)
  1164. (defcustom org-agenda-show-future-repeats t
  1165. "Non-nil shows repeated entries in the future part of the agenda.
  1166. When set to the symbol `next' only the first future repeat is shown."
  1167. :group 'org-agenda-daily/weekly
  1168. :type '(choice
  1169. (const :tag "Show all repeated entries" t)
  1170. (const :tag "Show next repeated entry" next)
  1171. (const :tag "Do not show repeated entries" nil))
  1172. :version "26.1"
  1173. :package-version '(Org . "9.1")
  1174. :safe #'symbolp)
  1175. (defcustom org-agenda-prefer-last-repeat nil
  1176. "Non-nil sets date for repeated entries to their last repeat.
  1177. When nil, display SCHEDULED and DEADLINE dates at their base
  1178. date, and in today's agenda, as a reminder. Display plain
  1179. time-stamps, on the other hand, at every repeat date in the past
  1180. in addition to the base date.
  1181. When non-nil, show a repeated entry at its latest repeat date,
  1182. possibly being today even if it wasn't marked as done. This
  1183. setting is useful if you do not always mark repeated entries as
  1184. done and, yet, consider that reaching repeat date starts the task
  1185. anew.
  1186. When set to a list of strings, prefer last repeats only for
  1187. entries with these TODO keywords."
  1188. :group 'org-agenda-daily/weekly
  1189. :type '(choice
  1190. (const :tag "Prefer last repeat" t)
  1191. (const :tag "Prefer base date" nil)
  1192. (repeat :tag "Prefer last repeat for entries with these TODO keywords"
  1193. (string :tag "TODO keyword")))
  1194. :version "26.1"
  1195. :package-version '(Org . "9.1")
  1196. :safe (lambda (x) (or (booleanp x) (consp x))))
  1197. (defcustom org-scheduled-past-days 10000
  1198. "Number of days to continue listing scheduled items not marked DONE.
  1199. When an item is scheduled on a date, it shows up in the agenda on
  1200. this day and will be listed until it is marked done or for the
  1201. number of days given here."
  1202. :group 'org-agenda-daily/weekly
  1203. :type 'integer
  1204. :safe 'integerp)
  1205. (defcustom org-deadline-past-days 10000
  1206. "Number of days to warn about missed deadlines.
  1207. When an item has deadline on a date, it shows up in the agenda on
  1208. this day and will appear as a reminder until it is marked DONE or
  1209. for the number of days given here."
  1210. :group 'org-agenda-daily/weekly
  1211. :type 'integer
  1212. :version "26.1"
  1213. :package-version '(Org . "9.1")
  1214. :safe 'integerp)
  1215. (defcustom org-agenda-log-mode-items '(closed clock)
  1216. "List of items that should be shown in agenda log mode.
  1217. \\<org-agenda-mode-map>\
  1218. This list may contain the following symbols:
  1219. closed Show entries that have been closed on that day.
  1220. clock Show entries that have received clocked time on that day.
  1221. state Show all logged state changes.
  1222. Note that instead of changing this variable, you can also press \
  1223. `\\[universal-argument] \\[org-agenda-log-mode]' in
  1224. the agenda to display all available LOG items temporarily."
  1225. :group 'org-agenda-daily/weekly
  1226. :type '(set :greedy t (const closed) (const clock) (const state)))
  1227. (defcustom org-agenda-clock-consistency-checks
  1228. '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
  1229. :gap-ok-around ("4:00")
  1230. :default-face ((:background "DarkRed") (:foreground "white"))
  1231. :overlap-face nil :gap-face nil :no-end-time-face nil
  1232. :long-face nil :short-face nil)
  1233. "This is a property list, with the following keys:
  1234. :max-duration Mark clocking chunks that are longer than this time.
  1235. This is a time string like \"HH:MM\", or the number
  1236. of minutes as an integer.
  1237. :min-duration Mark clocking chunks that are shorter that this.
  1238. This is a time string like \"HH:MM\", or the number
  1239. of minutes as an integer.
  1240. :max-gap Mark gaps between clocking chunks that are longer than
  1241. this duration. A number of minutes, or a string
  1242. like \"HH:MM\".
  1243. :gap-ok-around List of times during the day which are usually not working
  1244. times. When a gap is detected, but the gap contains any
  1245. of these times, the gap is *not* reported. For example,
  1246. if this is (\"4:00\" \"13:00\") then gaps that contain
  1247. 4:00 in the morning (i.e. the night) and 13:00
  1248. (i.e. a typical lunch time) do not cause a warning.
  1249. You should have at least one time during the night in this
  1250. list, or otherwise the first task each morning will trigger
  1251. a warning because it follows a long gap.
  1252. Furthermore, the following properties can be used to define faces for
  1253. issue display.
  1254. :default-face the default face, if the specific face is undefined
  1255. :overlap-face face for overlapping clocks
  1256. :gap-face face for gaps between clocks
  1257. :no-end-time-face face for incomplete clocks
  1258. :long-face face for clock intervals that are too long
  1259. :short-face face for clock intervals that are too short"
  1260. :group 'org-agenda-daily/weekly
  1261. :group 'org-clock
  1262. :version "24.1"
  1263. :type 'plist)
  1264. (defcustom org-agenda-log-mode-add-notes t
  1265. "Non-nil means add first line of notes to log entries in agenda views.
  1266. If a log item like a state change or a clock entry is associated with
  1267. notes, the first line of these notes will be added to the entry in the
  1268. agenda display."
  1269. :group 'org-agenda-daily/weekly
  1270. :type 'boolean)
  1271. (defcustom org-agenda-start-with-log-mode nil
  1272. "The initial value of log-mode in a newly created agenda window.
  1273. See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
  1274. explanations on the possible values."
  1275. :group 'org-agenda-startup
  1276. :group 'org-agenda-daily/weekly
  1277. :type '(choice (const :tag "Don't show log items" nil)
  1278. (const :tag "Show only log items" only)
  1279. (const :tag "Show all possible log items" clockcheck)
  1280. (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
  1281. (choice (const :tag "Show closed log items" closed)
  1282. (const :tag "Show clocked log items" clock)
  1283. (const :tag "Show all logged state changes" state)))))
  1284. (defcustom org-agenda-start-with-clockreport-mode nil
  1285. "The initial value of clockreport-mode in a newly created agenda window."
  1286. :group 'org-agenda-startup
  1287. :group 'org-agenda-daily/weekly
  1288. :type 'boolean)
  1289. (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
  1290. "Property list with parameters for the clocktable in clockreport mode.
  1291. This is the display mode that shows a clock table in the daily/weekly
  1292. agenda, the properties for this dynamic block can be set here.
  1293. The usual clocktable parameters are allowed here, but you cannot set
  1294. the properties :name, :tstart, :tend, :block, and :scope - these will
  1295. be overwritten to make sure the content accurately reflects the
  1296. current display in the agenda."
  1297. :group 'org-agenda-daily/weekly
  1298. :type 'plist)
  1299. (defvaralias 'org-agenda-search-view-search-words-only
  1300. 'org-agenda-search-view-always-boolean)
  1301. (defcustom org-agenda-search-view-always-boolean nil
  1302. "Non-nil means the search string is interpreted as individual parts.
  1303. The search string for search view can either be interpreted as a phrase,
  1304. or as a list of snippets that define a boolean search for a number of
  1305. strings.
  1306. When this is non-nil, the string will be split on whitespace, and each
  1307. snippet will be searched individually, and all must match in order to
  1308. select an entry. A snippet is then a single string of non-white
  1309. characters, or a string in double quotes, or a regexp in {} braces.
  1310. If a snippet is preceded by \"-\", the snippet must *not* match.
  1311. \"+\" is syntactic sugar for positive selection. Each snippet may
  1312. be found as a full word or a partial word, but see the variable
  1313. `org-agenda-search-view-force-full-words'.
  1314. When this is nil, search will look for the entire search phrase as one,
  1315. with each space character matching any amount of whitespace, including
  1316. line breaks.
  1317. Even when this is nil, you can still switch to Boolean search dynamically
  1318. by preceding the first snippet with \"+\" or \"-\". If the first snippet
  1319. is a regexp marked with braces like \"{abc}\", this will also switch to
  1320. boolean search."
  1321. :group 'org-agenda-search-view
  1322. :version "24.1"
  1323. :type 'boolean)
  1324. (defcustom org-agenda-search-view-force-full-words nil
  1325. "Non-nil means, search words must be matches as complete words.
  1326. When nil, they may also match part of a word."
  1327. :group 'org-agenda-search-view
  1328. :version "24.1"
  1329. :type 'boolean)
  1330. (defcustom org-agenda-search-view-max-outline-level 0
  1331. "Maximum outline level to display in search view.
  1332. E.g. when this is set to 1, the search view will only
  1333. show headlines of level 1. When set to 0, the default
  1334. value, don't limit agenda view by outline level."
  1335. :group 'org-agenda-search-view
  1336. :version "26.1"
  1337. :package-version '(Org . "8.3")
  1338. :type 'integer)
  1339. (defgroup org-agenda-time-grid nil
  1340. "Options concerning the time grid in the Org Agenda."
  1341. :tag "Org Agenda Time Grid"
  1342. :group 'org-agenda)
  1343. (defcustom org-agenda-search-headline-for-time t
  1344. "Non-nil means search headline for a time-of-day.
  1345. If the headline contains a time-of-day in one format or another, it will
  1346. be used to sort the entry into the time sequence of items for a day.
  1347. Some people have time stamps in the headline that refer to the creation
  1348. time or so, and then this produces an unwanted side effect. If this is
  1349. the case for your, use this variable to turn off searching the headline
  1350. for a time."
  1351. :group 'org-agenda-time-grid
  1352. :type 'boolean)
  1353. (defcustom org-agenda-use-time-grid t
  1354. "Non-nil means show a time grid in the agenda schedule.
  1355. A time grid is a set of lines for specific times (like every two hours between
  1356. 8:00 and 20:00). The items scheduled for a day at specific times are
  1357. sorted in between these lines.
  1358. For details about when the grid will be shown, and what it will look like, see
  1359. the variable `org-agenda-time-grid'."
  1360. :group 'org-agenda-time-grid
  1361. :type 'boolean)
  1362. (defcustom org-agenda-time-grid
  1363. (let ((graphical (and (display-graphic-p)
  1364. (char-displayable-p ?┄))))
  1365. `((daily today require-timed)
  1366. (800 1000 1200 1400 1600 1800 2000)
  1367. ,(if graphical " ┄┄┄┄┄ " "......")
  1368. ,(if graphical "┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄" "----------------")))
  1369. "The settings for time grid for agenda display.
  1370. This is a list of four items. The first item is again a list. It contains
  1371. symbols specifying conditions when the grid should be displayed:
  1372. daily if the agenda shows a single day
  1373. weekly if the agenda shows an entire week
  1374. today show grid on current date, independent of daily/weekly display
  1375. require-timed show grid only if at least one item has a time specification
  1376. remove-match skip grid times already present in an entry
  1377. The second item is a list of integers, indicating the times that
  1378. should have a grid line.
  1379. The third item is a string which will be placed right after the
  1380. times that have a grid line.
  1381. The fourth item is a string placed after the grid times. This
  1382. will align with agenda items."
  1383. :group 'org-agenda-time-grid
  1384. :package-version '(Org . "9.6")
  1385. :type
  1386. '(list
  1387. (set :greedy t :tag "Grid Display Options"
  1388. (const :tag "Show grid in single day agenda display" daily)
  1389. (const :tag "Show grid in weekly agenda display" weekly)
  1390. (const :tag "Always show grid for today" today)
  1391. (const :tag "Show grid only if any timed entries are present"
  1392. require-timed)
  1393. (const :tag "Skip grid times already present in an entry"
  1394. remove-match))
  1395. (repeat :tag "Grid Times" (integer :tag "Time"))
  1396. (string :tag "Grid String (after agenda times)")
  1397. (string :tag "Grid String (aligns with agenda items)")))
  1398. (defcustom org-agenda-show-current-time-in-grid t
  1399. "Non-nil means show the current time in the time grid."
  1400. :group 'org-agenda-time-grid
  1401. :version "24.1"
  1402. :type 'boolean)
  1403. (defcustom org-agenda-current-time-string
  1404. (if (and (display-graphic-p)
  1405. (char-displayable-p ?←)
  1406. (char-displayable-p ?─))
  1407. "← now ───────────────────────────────────────────────"
  1408. "now - - - - - - - - - - - - - - - - - - - - - - - - -")
  1409. "The string for the current time marker in the agenda."
  1410. :group 'org-agenda-time-grid
  1411. :package-version '(Org . "9.6")
  1412. :type 'string)
  1413. (defgroup org-agenda-sorting nil
  1414. "Options concerning sorting in the Org Agenda."
  1415. :tag "Org Agenda Sorting"
  1416. :group 'org-agenda)
  1417. (defcustom org-agenda-sorting-strategy
  1418. '((agenda habit-down time-up priority-down category-keep)
  1419. (todo priority-down category-keep)
  1420. (tags priority-down category-keep)
  1421. (search category-keep))
  1422. "Sorting structure for the agenda items of a single day.
  1423. This is a list of symbols which will be used in sequence to determine
  1424. if an entry should be listed before another entry. The following
  1425. symbols are recognized:
  1426. time-up Put entries with time-of-day indications first, early first.
  1427. time-down Put entries with time-of-day indications first, late first.
  1428. timestamp-up Sort by any timestamp, early first.
  1429. timestamp-down Sort by any timestamp, late first.
  1430. scheduled-up Sort by scheduled timestamp, early first.
  1431. scheduled-down Sort by scheduled timestamp, late first.
  1432. deadline-up Sort by deadline timestamp, early first.
  1433. deadline-down Sort by deadline timestamp, late first.
  1434. ts-up Sort by active timestamp, early first.
  1435. ts-down Sort by active timestamp, late first.
  1436. tsia-up Sort by inactive timestamp, early first.
  1437. tsia-down Sort by inactive timestamp, late first.
  1438. category-keep Keep the default order of categories, corresponding to the
  1439. sequence in `org-agenda-files'.
  1440. category-up Sort alphabetically by category, A-Z.
  1441. category-down Sort alphabetically by category, Z-A.
  1442. tag-up Sort alphabetically by last tag, A-Z.
  1443. tag-down Sort alphabetically by last tag, Z-A.
  1444. priority-up Sort numerically by priority, high priority last.
  1445. priority-down Sort numerically by priority, high priority first.
  1446. todo-state-up Sort by todo state, tasks that are done last.
  1447. todo-state-down Sort by todo state, tasks that are done first.
  1448. effort-up Sort numerically by estimated effort, high effort last.
  1449. effort-down Sort numerically by estimated effort, high effort first.
  1450. user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
  1451. user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
  1452. habit-up Put entries that are habits first.
  1453. habit-down Put entries that are habits last.
  1454. alpha-up Sort headlines alphabetically.
  1455. alpha-down Sort headlines alphabetically, reversed.
  1456. The different possibilities will be tried in sequence, and testing stops
  1457. if one comparison returns a \"not-equal\". For example,
  1458. (setq org-agenda-sorting-strategy
  1459. \\='(time-up category-keep priority-down))
  1460. means: Pull out all entries having a specified time of day and sort them,
  1461. in order to make a time schedule for the current day the first thing in the
  1462. agenda listing for the day. Of the entries without a time indication, keep
  1463. the grouped in categories, don't sort the categories, but keep them in
  1464. the sequence given in `org-agenda-files'. Within each category sort by
  1465. priority.
  1466. Leaving out `category-keep' would mean that items will be sorted across
  1467. categories by priority.
  1468. Instead of a single list, this can also be a set of list for specific
  1469. contents, with a context symbol in the car of the list, any of
  1470. `agenda', `todo', `tags', `search' for the corresponding agenda views.
  1471. Custom commands can bind this variable in the options section."
  1472. :group 'org-agenda-sorting
  1473. :type `(choice
  1474. (repeat :tag "General" ,org-sorting-choice)
  1475. (list :tag "Individually"
  1476. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1477. (repeat ,org-sorting-choice))
  1478. (cons (const :tag "Strategy for TODO lists" todo)
  1479. (repeat ,org-sorting-choice))
  1480. (cons (const :tag "Strategy for Tags matches" tags)
  1481. (repeat ,org-sorting-choice))
  1482. (cons (const :tag "Strategy for search matches" search)
  1483. (repeat ,org-sorting-choice)))))
  1484. (defcustom org-agenda-cmp-user-defined nil
  1485. "A function to define the comparison `user-defined'.
  1486. This function must receive two arguments, agenda entry a and b.
  1487. If a>b, return +1. If a<b, return -1. If they are equal as seen by
  1488. the user comparison, return nil.
  1489. When this is defined, you can make `user-defined-up' and `user-defined-down'
  1490. part of an agenda sorting strategy."
  1491. :group 'org-agenda-sorting
  1492. :type 'symbol)
  1493. (defcustom org-agenda-sort-notime-is-late t
  1494. "Non-nil means items without time are considered late.
  1495. This is only relevant for sorting. When t, items which have no explicit
  1496. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1497. do have a time. When nil, the default time is before 0:00. You can use this
  1498. option to decide if the schedule for today should come before or after timeless
  1499. agenda entries."
  1500. :group 'org-agenda-sorting
  1501. :type 'boolean)
  1502. (defcustom org-agenda-sort-noeffort-is-high t
  1503. "Non-nil means items without effort estimate are sorted as high effort.
  1504. This also applies when filtering an agenda view with respect to the
  1505. < or > effort operator. Then, tasks with no effort defined will be treated
  1506. as tasks with high effort.
  1507. When nil, such items are sorted as 0 minutes effort."
  1508. :group 'org-agenda-sorting
  1509. :type 'boolean)
  1510. (defgroup org-agenda-line-format nil
  1511. "Options concerning the entry prefix in the Org agenda display."
  1512. :tag "Org Agenda Line Format"
  1513. :group 'org-agenda)
  1514. (defcustom org-agenda-prefix-format
  1515. '((agenda . " %i %-12:c%?-12t% s")
  1516. (todo . " %i %-12:c")
  1517. (tags . " %i %-12:c")
  1518. (search . " %i %-12:c"))
  1519. "Format specifications for the prefix of items in the agenda views.
  1520. An alist with one entry per agenda type. The keys of the
  1521. sublists are `agenda', `todo', `search' and `tags'. The values
  1522. are format strings.
  1523. This format works similar to a printf format, with the following meaning:
  1524. %c the category of the item, \"Diary\" for entries from the diary,
  1525. or as given by the CATEGORY keyword or derived from the file name
  1526. %e the effort required by the item
  1527. %l the level of the item (insert X space(s) if item is of level X)
  1528. %i the icon category of the item, see `org-agenda-category-icon-alist'
  1529. %T the last tag of the item (ignore inherited tags, which come first)
  1530. %t the HH:MM time-of-day specification if one applies to the entry
  1531. %s Scheduling/Deadline information, a short string
  1532. %b show breadcrumbs, i.e., the names of the higher levels
  1533. %(expression) Eval EXPRESSION and replace the control string
  1534. by the result
  1535. All specifiers work basically like the standard `%s' of printf, but may
  1536. contain two additional characters: a question mark just after the `%'
  1537. and a whitespace/punctuation character just before the final letter.
  1538. If the first character after `%' is a question mark, the entire field
  1539. will only be included if the corresponding value applies to the current
  1540. entry. This is useful for fields which should have fixed width when
  1541. present, but zero width when absent. For example, \"%?-12t\" will
  1542. result in a 12 character time field if a time of the day is specified,
  1543. but will completely disappear in entries which do not contain a time.
  1544. If there is punctuation or whitespace character just before the
  1545. final format letter, this character will be appended to the field
  1546. value if the value is not empty. For example, the format
  1547. \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
  1548. the category is empty, no additional colon is inserted.
  1549. The default value for the agenda sublist is \" %-12:c%?-12t% s\",
  1550. which means:
  1551. - Indent the line with two space characters
  1552. - Give the category a 12 chars wide field, padded with whitespace on
  1553. the right (because of `-'). Append a colon if there is a category
  1554. (because of `:').
  1555. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1556. time, don't put in an empty field, just skip it (because of '?').
  1557. - Finally, put the scheduling information.
  1558. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1559. `org-agenda-remove-tags'.
  1560. Custom commands can set this variable in the options section."
  1561. :type '(choice
  1562. (string :tag "General format")
  1563. (list :greedy t :tag "View dependent"
  1564. (cons (const agenda) (string :tag "Format"))
  1565. (cons (const todo) (string :tag "Format"))
  1566. (cons (const tags) (string :tag "Format"))
  1567. (cons (const search) (string :tag "Format"))))
  1568. :group 'org-agenda-line-format
  1569. :version "26.1"
  1570. :package-version '(Org . "9.1"))
  1571. (defcustom org-agenda-breadcrumbs-separator "->"
  1572. "The separator of breadcrumbs in agenda lines."
  1573. :group 'org-agenda-line-format
  1574. :package-version '(Org . "9.3")
  1575. :type 'string
  1576. :safe #'stringp)
  1577. (defvar org-prefix-format-compiled nil
  1578. "The compiled prefix format and associated variables.
  1579. This is a list where first element is a list of variable bindings, and second
  1580. element is the compiled format expression. See the variable
  1581. `org-agenda-prefix-format'.")
  1582. (defcustom org-agenda-todo-keyword-format "%-1s"
  1583. "Format for the TODO keyword in agenda lines.
  1584. Set this to something like \"%-12s\" if you want all TODO keywords
  1585. to occupy a fixed space in the agenda display."
  1586. :group 'org-agenda-line-format
  1587. :type 'string)
  1588. (defcustom org-agenda-diary-sexp-prefix nil
  1589. "A regexp that matches part of a diary sexp entry
  1590. which should be treated as scheduling/deadline information in
  1591. `org-agenda'.
  1592. For example, you can use this to extract the `diary-remind-message' from
  1593. `diary-remind' entries."
  1594. :group 'org-agenda-line-format
  1595. :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
  1596. (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
  1597. "Text preceding timerange entries in the agenda view.
  1598. This is a list with two strings. The first applies when the range
  1599. is entirely on one day. The second applies if the range spans several days.
  1600. The strings may have two \"%d\" format specifiers which will be filled
  1601. with the sequence number of the days, and the total number of days in the
  1602. range, respectively."
  1603. :group 'org-agenda-line-format
  1604. :type '(list
  1605. (string :tag "Deadline today ")
  1606. (choice :tag "Deadline relative"
  1607. (string :tag "Format string")
  1608. (function))))
  1609. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  1610. "Text preceding scheduled items in the agenda view.
  1611. This is a list with two strings. The first applies when the item is
  1612. scheduled on the current day. The second applies when it has been scheduled
  1613. previously, it may contain a %d indicating that this is the nth time that
  1614. this item is scheduled, due to automatic rescheduling of unfinished items
  1615. for the following day. So this number is one larger than the number of days
  1616. that passed since this item was scheduled first."
  1617. :group 'org-agenda-line-format
  1618. :version "24.4"
  1619. :package-version '(Org . "8.0")
  1620. :type '(list
  1621. (string :tag "Scheduled today ")
  1622. (string :tag "Scheduled previously")))
  1623. (defcustom org-agenda-inactive-leader "["
  1624. "Text preceding item pulled into the agenda by inactive time stamps.
  1625. These entries are added to the agenda when pressing \"[\"."
  1626. :group 'org-agenda-line-format
  1627. :version "24.1"
  1628. :type 'string)
  1629. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
  1630. "Text preceding deadline items in the agenda view.
  1631. This is a list with three strings. The first applies when the item has its
  1632. deadline on the current day. The second applies when the deadline is in the
  1633. future, the third one when it is in the past. The strings may contain %d
  1634. to capture the number of days."
  1635. :group 'org-agenda-line-format
  1636. :version "24.4"
  1637. :package-version '(Org . "8.0")
  1638. :type '(list
  1639. (string :tag "Deadline today ")
  1640. (string :tag "Deadline in the future ")
  1641. (string :tag "Deadline in the past ")))
  1642. (defcustom org-agenda-remove-times-when-in-prefix t
  1643. "Non-nil means remove duplicate time specifications in agenda items.
  1644. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1645. time-of-day specification in a headline or diary entry is extracted and
  1646. placed into the prefix. If this option is non-nil, the original specification
  1647. \(a timestamp or -range, or just a plain time(range) specification like
  1648. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1649. cluttered.
  1650. The option can be t or nil. It may also be the symbol `beg', indicating
  1651. that the time should only be removed when it is located at the beginning of
  1652. the headline/diary entry."
  1653. :group 'org-agenda-line-format
  1654. :type '(choice
  1655. (const :tag "Always" t)
  1656. (const :tag "Never" nil)
  1657. (const :tag "When at beginning of entry" beg)))
  1658. (defcustom org-agenda-remove-timeranges-from-blocks nil
  1659. "Non-nil means remove time ranges specifications in agenda
  1660. items that span on several days."
  1661. :group 'org-agenda-line-format
  1662. :version "24.1"
  1663. :type 'boolean)
  1664. (defcustom org-agenda-default-appointment-duration nil
  1665. "Default duration for appointments that only have a starting time.
  1666. When nil, no duration is specified in such cases.
  1667. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  1668. :group 'org-agenda-line-format
  1669. :type '(choice
  1670. (integer :tag "Minutes")
  1671. (const :tag "No default duration")))
  1672. (defcustom org-agenda-show-inherited-tags t
  1673. "Non-nil means show inherited tags in each agenda line.
  1674. When this option is set to `always', it takes precedence over
  1675. `org-agenda-use-tag-inheritance' and inherited tags are shown
  1676. in every agenda.
  1677. When this option is set to t (the default), inherited tags are
  1678. shown when they are available, i.e. when the value of
  1679. `org-agenda-use-tag-inheritance' enables tag inheritance for the
  1680. given agenda type.
  1681. This can be set to a list of agenda types in which the agenda
  1682. must display the inherited tags. Available types are `todo',
  1683. `agenda' and `search'.
  1684. When set to nil, never show inherited tags in agenda lines."
  1685. :group 'org-agenda-line-format
  1686. :group 'org-agenda
  1687. :version "24.3"
  1688. :type '(choice
  1689. (const :tag "Show inherited tags when available" t)
  1690. (const :tag "Always show inherited tags" always)
  1691. (repeat :tag "Show inherited tags only in selected agenda types"
  1692. (symbol :tag "Agenda type"))))
  1693. (defcustom org-agenda-use-tag-inheritance '(todo search agenda)
  1694. "List of agenda view types where to use tag inheritance.
  1695. In tags/tags-todo/tags-tree agenda views, tag inheritance is
  1696. controlled by `org-use-tag-inheritance'. In other agenda types,
  1697. `org-use-tag-inheritance' is not used for the selection of the
  1698. agenda entries. Still, you may want the agenda to be aware of
  1699. the inherited tags anyway, e.g. for later tag filtering.
  1700. Allowed value are `todo', `search' and `agenda'.
  1701. This variable has no effect if `org-agenda-show-inherited-tags'
  1702. is set to `always'. In that case, the agenda is aware of those
  1703. tags.
  1704. The default value sets tags in every agenda type. Setting this
  1705. option to nil will speed up non-tags agenda view a lot."
  1706. :group 'org-agenda
  1707. :version "26.1"
  1708. :package-version '(Org . "9.1")
  1709. :type '(choice
  1710. (const :tag "Use tag inheritance in all agenda types" t)
  1711. (repeat :tag "Use tag inheritance in selected agenda types"
  1712. (symbol :tag "Agenda type"))))
  1713. (defcustom org-agenda-hide-tags-regexp nil
  1714. "Regular expression used to filter away specific tags in agenda views.
  1715. This means that these tags will be present, but not be shown in the agenda
  1716. line. Secondary filtering will still work on the hidden tags.
  1717. Nil means don't hide any tags."
  1718. :group 'org-agenda-line-format
  1719. :type '(choice
  1720. (const :tag "Hide none" nil)
  1721. (regexp :tag "Regexp ")))
  1722. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  1723. 'org-agenda-remove-tags)
  1724. (defcustom org-agenda-remove-tags nil
  1725. "Non-nil means remove the tags from the headline copy in the agenda.
  1726. When this is the symbol `prefix', only remove tags when
  1727. `org-agenda-prefix-format' contains a `%T' specifier."
  1728. :group 'org-agenda-line-format
  1729. :type '(choice
  1730. (const :tag "Always" t)
  1731. (const :tag "Never" nil)
  1732. (const :tag "When prefix format contains %T" prefix)))
  1733. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
  1734. (defcustom org-agenda-tags-column 'auto
  1735. "Shift tags in agenda items to this column.
  1736. If set to `auto', tags will be automatically aligned to the right
  1737. edge of the window.
  1738. If set to a positive number, tags will be left-aligned to that
  1739. column. If set to a negative number, tags will be right-aligned
  1740. to that column. For example, -80 works well for a normal 80
  1741. character screen."
  1742. :group 'org-agenda-line-format
  1743. :type '(choice
  1744. (const :tag "Automatically align to right edge of window" auto)
  1745. (integer :tag "Specific column" -80))
  1746. :package-version '(Org . "9.1")
  1747. :version "26.1")
  1748. (defcustom org-agenda-fontify-priorities 'cookies
  1749. "Non-nil means highlight low and high priorities in agenda.
  1750. When t, the highest priority entries are bold, lowest priority italic.
  1751. However, settings in `org-priority-faces' will overrule these faces.
  1752. When this variable is the symbol `cookies', only fontify the
  1753. cookies, not the entire task.
  1754. This may also be an association list of priority faces, whose
  1755. keys are the character values of `org-priority-highest',
  1756. `org-priority-default', and `org-priority-lowest' (the default values
  1757. are ?A, ?B, and ?C, respectively). The face may be a named face, a
  1758. color as a string, or a list like `(:background \"Red\")'.
  1759. If it is a color, the variable `org-faces-easy-properties'
  1760. determines if it is a foreground or a background color."
  1761. :group 'org-agenda-line-format
  1762. :type '(choice
  1763. (const :tag "Never" nil)
  1764. (const :tag "Defaults" t)
  1765. (const :tag "Cookies only" cookies)
  1766. (repeat :tag "Specify"
  1767. (list (character :tag "Priority" :value ?A)
  1768. (choice :tag "Face "
  1769. (string :tag "Color")
  1770. (sexp :tag "Face"))))))
  1771. (defcustom org-agenda-day-face-function nil
  1772. "Function called to determine what face should be used to display a day.
  1773. The only argument passed to that function is the day. It should
  1774. returns a face, or nil if does not want to specify a face and let
  1775. the normal rules apply."
  1776. :group 'org-agenda-line-format
  1777. :version "24.1"
  1778. :type '(choice (const nil) (function)))
  1779. (defcustom org-agenda-category-icon-alist nil
  1780. "Alist of category icon to be displayed in agenda views.
  1781. Each entry should have the following format:
  1782. (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
  1783. Where CATEGORY-REGEXP is a regexp matching the categories where
  1784. the icon should be displayed.
  1785. FILE-OR-DATA either a file path or a string containing image data.
  1786. The other fields can be omitted safely if not needed:
  1787. TYPE indicates the image type.
  1788. DATA-P is a boolean indicating whether the FILE-OR-DATA string is
  1789. image data.
  1790. PROPS are additional image attributes to assign to the image,
  1791. like, e.g. `:ascent center'.
  1792. (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
  1793. If you want to set the display properties yourself, just put a
  1794. list as second element:
  1795. (CATEGORY-REGEXP (MY PROPERTY LIST))
  1796. For example, to display a 16px horizontal space for Emacs
  1797. category, you can use:
  1798. (\"Emacs\" \\='(space . (:width (16))))"
  1799. :group 'org-agenda-line-format
  1800. :version "24.1"
  1801. :type '(alist :key-type (regexp :tag "Regexp matching category")
  1802. :value-type (choice (list :tag "Icon"
  1803. (string :tag "File or data")
  1804. (symbol :tag "Type")
  1805. (boolean :tag "Data?")
  1806. (repeat :tag "Extra image properties" :inline t sexp))
  1807. (list :tag "Display properties" sexp))))
  1808. (defgroup org-agenda-column-view nil
  1809. "Options concerning column view in the agenda."
  1810. :tag "Org Agenda Column View"
  1811. :group 'org-agenda)
  1812. (defcustom org-agenda-view-columns-initially nil
  1813. "When non-nil, switch to columns view right after creating the agenda."
  1814. :group 'org-agenda-column-view
  1815. :type 'boolean
  1816. :version "26.1"
  1817. :package-version '(Org . "9.0")
  1818. :safe #'booleanp)
  1819. (defcustom org-agenda-columns-show-summaries t
  1820. "Non-nil means show summaries for columns displayed in the agenda view."
  1821. :group 'org-agenda-column-view
  1822. :type 'boolean)
  1823. (defcustom org-agenda-columns-compute-summary-properties t
  1824. "Non-nil means recompute all summary properties before column view.
  1825. When column view in the agenda is listing properties that have a summary
  1826. operator, it can go to all relevant buffers and recompute the summaries
  1827. there. This can mean overhead for the agenda column view, but is necessary
  1828. to have thing up to date.
  1829. As a special case, a CLOCKSUM property also makes sure that the clock
  1830. computations are current."
  1831. :group 'org-agenda-column-view
  1832. :type 'boolean)
  1833. (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
  1834. "Non-nil means the duration of an appointment will add to day effort.
  1835. The property to which appointment durations will be added is the one given
  1836. in the option `org-effort-property'. If an appointment does not have
  1837. an end time, `org-agenda-default-appointment-duration' will be used. If that
  1838. is not set, an appointment without end time will not contribute to the time
  1839. estimate."
  1840. :group 'org-agenda-column-view
  1841. :type 'boolean)
  1842. (defcustom org-agenda-auto-exclude-function nil
  1843. "A function called with a tag to decide if it is filtered on \
  1844. \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
  1845. The sole argument to the function, which is called once for each
  1846. possible tag, is a string giving the name of the tag. The
  1847. function should return either nil if the tag should be included
  1848. as normal, \"-<TAG>\" to exclude the tag, or \"+<TAG>\" to exclude
  1849. lines not carrying this tag.
  1850. Note that for the purpose of tag filtering, only the lower-case version of
  1851. all tags will be considered, so that this function will only ever see
  1852. the lower-case version of all tags."
  1853. :group 'org-agenda
  1854. :type '(choice (const nil) (function)))
  1855. (defcustom org-agenda-bulk-custom-functions nil
  1856. "Alist of characters and custom functions for bulk actions.
  1857. For example, this makes those two functions available:
  1858. (setq org-agenda-bulk-custom-functions
  1859. \\='((?R set-category)
  1860. (?C bulk-cut)))
  1861. With selected entries in an agenda buffer, `B R' will call
  1862. the custom function `set-category' on the selected entries.
  1863. Note that functions in this alist don't need to be quoted.
  1864. You can also specify a function which collects arguments to be
  1865. used for each call to your bulk custom function. The argument
  1866. collecting function will be run once and should return a list of
  1867. arguments to pass to the bulk function. For example:
  1868. (setq org-agenda-bulk-custom-functions
  1869. \\='((?R set-category get-category)))
  1870. Now, `B R' will call the custom `get-category' which would prompt
  1871. the user once for a category. That category is then passed as an
  1872. argument to `set-category' for each entry it's called against."
  1873. :type
  1874. '(alist :key-type character
  1875. :value-type
  1876. (group (function :tag "Bulk Custom Function")
  1877. (choice (function :tag "Bulk Custom Argument Function")
  1878. (const :tag "No Bulk Custom Argument Function" nil))))
  1879. :package-version '(Org . "9.5")
  1880. :group 'org-agenda)
  1881. (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
  1882. "Execute BODY with point at location given by `org-hd-marker' property.
  1883. If STRING is non-nil, the text property will be fetched from position 0
  1884. in that string. If STRING is nil, it will be fetched from the beginning
  1885. of the current line."
  1886. (declare (debug t) (indent 1))
  1887. (org-with-gensyms (marker)
  1888. `(let ((,marker (get-text-property (if ,string 0 (line-beginning-position))
  1889. 'org-hd-marker ,string)))
  1890. (with-current-buffer (marker-buffer ,marker)
  1891. (save-excursion
  1892. (goto-char ,marker)
  1893. ,@body)))))
  1894. (defun org-add-agenda-custom-command (entry)
  1895. "Replace or add a command in `org-agenda-custom-commands'.
  1896. This is mostly for hacking and trying a new command - once the command
  1897. works you probably want to add it to `org-agenda-custom-commands' for good."
  1898. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  1899. (if ass
  1900. (setcdr ass (cdr entry))
  1901. (push entry org-agenda-custom-commands))))
  1902. (defmacro org-agenda--insert-overriding-header (default)
  1903. "Insert header into agenda view.
  1904. The inserted header depends on `org-agenda-overriding-header'.
  1905. If the empty string, don't insert a header. If any other string,
  1906. insert it as a header. If nil, insert DEFAULT, which should
  1907. evaluate to a string. If a function, call it and insert the
  1908. string that it returns."
  1909. (declare (debug (form)) (indent defun))
  1910. `(cond
  1911. ((not org-agenda-overriding-header) (insert ,default))
  1912. ((equal org-agenda-overriding-header "") nil)
  1913. ((stringp org-agenda-overriding-header)
  1914. (insert (propertize org-agenda-overriding-header
  1915. 'face 'org-agenda-structure)
  1916. "\n"))
  1917. ((functionp org-agenda-overriding-header)
  1918. (insert (funcall org-agenda-overriding-header)))
  1919. (t (user-error "Invalid value for `org-agenda-overriding-header': %S"
  1920. org-agenda-overriding-header))))
  1921. ;;; Define the org-agenda-mode
  1922. (defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
  1923. (defvar org-agenda-mode-map (make-sparse-keymap)
  1924. "Keymap for `org-agenda-mode'.")
  1925. (org-remap org-agenda-mode-map 'move-end-of-line 'org-agenda-end-of-line)
  1926. (defvar org-agenda-menu) ; defined later in this file.
  1927. (defvar org-agenda-restrict nil)
  1928. (defvar org-agenda-follow-mode nil)
  1929. (defvar org-agenda-entry-text-mode nil)
  1930. (defvar org-agenda-clockreport-mode nil)
  1931. (defvar org-agenda-show-log nil
  1932. "When non-nil, show the log in the agenda.
  1933. Do not set this directly; instead use
  1934. `org-agenda-start-with-log-mode', which see.")
  1935. (defvar org-agenda-redo-command nil)
  1936. (defvar org-agenda-query-string nil)
  1937. (defvar org-agenda-mode-hook nil
  1938. "Hook run after `org-agenda-mode' is turned on.
  1939. The buffer is still writable when this hook is called.")
  1940. (defvar org-agenda-type nil)
  1941. (defvar org-agenda-force-single-file nil)
  1942. (defvar org-agenda-bulk-marked-entries nil
  1943. "List of markers that refer to marked entries in the agenda.")
  1944. (defvar org-agenda-current-date nil
  1945. "Active date when building the agenda.")
  1946. ;;; Multiple agenda buffers support
  1947. (defcustom org-agenda-sticky nil
  1948. "Non-nil means agenda q key will bury agenda buffers.
  1949. Agenda commands will then show existing buffer instead of generating new ones.
  1950. When nil, `q' will kill the single agenda buffer."
  1951. :group 'org-agenda
  1952. :version "24.3"
  1953. :type 'boolean)
  1954. ;;;###autoload
  1955. (defun org-toggle-sticky-agenda (&optional arg)
  1956. "Toggle `org-agenda-sticky'."
  1957. (interactive "P")
  1958. (let ((new-value (if arg
  1959. (> (prefix-numeric-value arg) 0)
  1960. (not org-agenda-sticky))))
  1961. (if (equal new-value org-agenda-sticky)
  1962. (and (called-interactively-p 'interactive)
  1963. (message "Sticky agenda was already %s"
  1964. (if org-agenda-sticky "enabled" "disabled")))
  1965. (setq org-agenda-sticky new-value)
  1966. (org-agenda-kill-all-agenda-buffers)
  1967. (and (called-interactively-p 'interactive)
  1968. (message "Sticky agenda %s"
  1969. (if org-agenda-sticky "enabled" "disabled"))))))
  1970. (defvar org-agenda-buffer nil
  1971. "Agenda buffer currently being generated.")
  1972. (defvar org-agenda-last-prefix-arg nil)
  1973. (defvar org-agenda-this-buffer-name nil)
  1974. (defvar org-agenda-doing-sticky-redo nil)
  1975. (defvar org-agenda-this-buffer-is-sticky nil)
  1976. (defvar org-agenda-last-indirect-buffer nil
  1977. "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
  1978. (defconst org-agenda-local-vars
  1979. '(org-agenda-this-buffer-name
  1980. org-agenda-undo-list
  1981. org-agenda-pending-undo-list
  1982. org-agenda-follow-mode
  1983. org-agenda-entry-text-mode
  1984. org-agenda-clockreport-mode
  1985. org-agenda-show-log
  1986. org-agenda-redo-command
  1987. org-agenda-query-string
  1988. org-agenda-type
  1989. org-agenda-bulk-marked-entries
  1990. org-agenda-undo-has-started-in
  1991. org-agenda-info
  1992. org-agenda-pre-window-conf
  1993. org-agenda-columns-active
  1994. org-agenda-tag-filter
  1995. org-agenda-category-filter
  1996. org-agenda-top-headline-filter
  1997. org-agenda-regexp-filter
  1998. org-agenda-effort-filter
  1999. org-agenda-filters-preset
  2000. org-agenda-markers
  2001. org-agenda-last-search-view-search-was-boolean
  2002. org-agenda-last-indirect-buffer
  2003. org-agenda-filtered-by-category
  2004. org-agenda-filter-form
  2005. org-agenda-cycle-counter
  2006. org-agenda-last-prefix-arg)
  2007. "Variables that must be local in agenda buffers to allow multiple buffers.")
  2008. (defun org-agenda-mode ()
  2009. "Mode for time-sorted view on action items in Org files.
  2010. The following commands are available:
  2011. \\{org-agenda-mode-map}"
  2012. (interactive)
  2013. (ignore-errors (require 'face-remap))
  2014. (let ((agenda-local-vars-to-keep
  2015. '(text-scale-mode-amount
  2016. text-scale-mode
  2017. text-scale-mode-lighter
  2018. face-remapping-alist))
  2019. (save (buffer-local-variables)))
  2020. (kill-all-local-variables)
  2021. (cl-flet ((reset-saved (var-set)
  2022. "Reset variables in VAR-SET to possibly stored value in SAVE."
  2023. (dolist (elem save)
  2024. (pcase elem
  2025. (`(,var . ,val) ;ignore unbound variables
  2026. (when (and val (memq var var-set))
  2027. (set var val)))))))
  2028. (cond (org-agenda-doing-sticky-redo
  2029. ;; Refreshing sticky agenda-buffer
  2030. ;;
  2031. ;; Preserve the value of `org-agenda-local-vars' variables.
  2032. (mapc #'make-local-variable org-agenda-local-vars)
  2033. (reset-saved org-agenda-local-vars)
  2034. (setq-local org-agenda-this-buffer-is-sticky t))
  2035. (org-agenda-sticky
  2036. ;; Creating a sticky Agenda buffer for the first time
  2037. (mapc #'make-local-variable org-agenda-local-vars)
  2038. (setq-local org-agenda-this-buffer-is-sticky t))
  2039. (t
  2040. ;; Creating a non-sticky agenda buffer
  2041. (setq-local org-agenda-this-buffer-is-sticky nil)))
  2042. (mapc #'make-local-variable agenda-local-vars-to-keep)
  2043. (reset-saved agenda-local-vars-to-keep)))
  2044. (setq org-agenda-undo-list nil
  2045. org-agenda-pending-undo-list nil
  2046. org-agenda-bulk-marked-entries nil)
  2047. (setq major-mode 'org-agenda-mode)
  2048. ;; Keep global-font-lock-mode from turning on font-lock-mode
  2049. (setq-local font-lock-global-modes (list 'not major-mode))
  2050. (setq mode-name "Org-Agenda")
  2051. (setq indent-tabs-mode nil)
  2052. (use-local-map org-agenda-mode-map)
  2053. (when org-startup-truncated (setq truncate-lines t))
  2054. (setq-local line-move-visual nil)
  2055. (add-hook 'post-command-hook #'org-agenda-update-agenda-type nil 'local)
  2056. (add-hook 'pre-command-hook #'org-unhighlight nil 'local)
  2057. ;; Make sure properties are removed when copying text
  2058. (if (boundp 'filter-buffer-substring-functions)
  2059. (add-hook 'filter-buffer-substring-functions
  2060. (lambda (fun start end delete)
  2061. (substring-no-properties (funcall fun start end delete)))
  2062. nil t)
  2063. ;; Emacs >= 24.4.
  2064. (add-function :filter-return (local 'filter-buffer-substring-function)
  2065. #'substring-no-properties))
  2066. (unless org-agenda-keep-modes
  2067. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  2068. org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
  2069. org-agenda-show-log org-agenda-start-with-log-mode
  2070. org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode))
  2071. (add-to-invisibility-spec '(org-filtered))
  2072. (org-fold-core-initialize `(,org-link--description-folding-spec
  2073. ,org-link--link-folding-spec))
  2074. (easy-menu-change
  2075. '("Agenda") "Agenda Files"
  2076. (append
  2077. (list
  2078. (vector
  2079. (if (get 'org-agenda-files 'org-restrict)
  2080. "Restricted to single file"
  2081. "Edit File List")
  2082. '(org-edit-agenda-file-list)
  2083. (not (get 'org-agenda-files 'org-restrict)))
  2084. "--")
  2085. (mapcar #'org-file-menu-entry (org-agenda-files))))
  2086. (org-agenda-set-mode-name)
  2087. (run-mode-hooks 'org-agenda-mode-hook))
  2088. (substitute-key-definition #'undo #'org-agenda-undo
  2089. org-agenda-mode-map global-map)
  2090. (org-defkey org-agenda-mode-map "\C-i" #'org-agenda-goto)
  2091. (org-defkey org-agenda-mode-map [(tab)] #'org-agenda-goto)
  2092. (org-defkey org-agenda-mode-map "\C-m" #'org-agenda-switch-to)
  2093. (org-defkey org-agenda-mode-map "\C-k" #'org-agenda-kill)
  2094. (org-defkey org-agenda-mode-map "\C-c\C-w" #'org-agenda-refile)
  2095. (org-defkey org-agenda-mode-map [(meta down)] #'org-agenda-drag-line-forward)
  2096. (org-defkey org-agenda-mode-map [(meta up)] #'org-agenda-drag-line-backward)
  2097. (org-defkey org-agenda-mode-map "m" #'org-agenda-bulk-mark)
  2098. (org-defkey org-agenda-mode-map "\M-m" #'org-agenda-bulk-toggle)
  2099. (org-defkey org-agenda-mode-map "*" #'org-agenda-bulk-mark-all)
  2100. (org-defkey org-agenda-mode-map "\M-*" #'org-agenda-bulk-toggle-all)
  2101. (org-defkey org-agenda-mode-map "#" #'org-agenda-dim-blocked-tasks)
  2102. (org-defkey org-agenda-mode-map "%" #'org-agenda-bulk-mark-regexp)
  2103. (org-defkey org-agenda-mode-map "u" #'org-agenda-bulk-unmark)
  2104. (org-defkey org-agenda-mode-map "U" #'org-agenda-bulk-unmark-all)
  2105. (org-defkey org-agenda-mode-map "B" #'org-agenda-bulk-action)
  2106. (org-defkey org-agenda-mode-map "k" #'org-agenda-capture)
  2107. (org-defkey org-agenda-mode-map "A" #'org-agenda-append-agenda)
  2108. (org-defkey org-agenda-mode-map "\C-c\C-x!" #'org-reload)
  2109. (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" #'org-agenda-archive-default)
  2110. (org-defkey org-agenda-mode-map "\C-c\C-xa" #'org-agenda-toggle-archive-tag)
  2111. (org-defkey org-agenda-mode-map "\C-c\C-xA" #'org-agenda-archive-to-archive-sibling)
  2112. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" #'org-agenda-archive)
  2113. (org-defkey org-agenda-mode-map "\C-c$" #'org-agenda-archive)
  2114. (org-defkey org-agenda-mode-map "$" #'org-agenda-archive)
  2115. (org-defkey org-agenda-mode-map "\C-c\C-o" #'org-agenda-open-link)
  2116. (org-defkey org-agenda-mode-map " " #'org-agenda-show-and-scroll-up)
  2117. (org-defkey org-agenda-mode-map [backspace] #'org-agenda-show-scroll-down)
  2118. (org-defkey org-agenda-mode-map "\d" #'org-agenda-show-scroll-down)
  2119. (org-defkey org-agenda-mode-map [(control shift right)] #'org-agenda-todo-nextset)
  2120. (org-defkey org-agenda-mode-map [(control shift left)] #'org-agenda-todo-previousset)
  2121. (org-defkey org-agenda-mode-map "\C-c\C-xb" #'org-agenda-tree-to-indirect-buffer)
  2122. (org-defkey org-agenda-mode-map "o" #'delete-other-windows)
  2123. (org-defkey org-agenda-mode-map "L" #'org-agenda-recenter)
  2124. (org-defkey org-agenda-mode-map "\C-c\C-t" #'org-agenda-todo)
  2125. (org-defkey org-agenda-mode-map "t" #'org-agenda-todo)
  2126. (org-defkey org-agenda-mode-map "a" #'org-agenda-archive-default-with-confirmation)
  2127. (org-defkey org-agenda-mode-map ":" #'org-agenda-set-tags)
  2128. (org-defkey org-agenda-mode-map "\C-c\C-q" #'org-agenda-set-tags)
  2129. (org-defkey org-agenda-mode-map "." #'org-agenda-goto-today)
  2130. (org-defkey org-agenda-mode-map "j" #'org-agenda-goto-date)
  2131. (org-defkey org-agenda-mode-map "d" #'org-agenda-day-view)
  2132. (org-defkey org-agenda-mode-map "w" #'org-agenda-week-view)
  2133. (org-defkey org-agenda-mode-map "y" #'org-agenda-year-view)
  2134. (org-defkey org-agenda-mode-map "\C-c\C-z" #'org-agenda-add-note)
  2135. (org-defkey org-agenda-mode-map "z" #'org-agenda-add-note)
  2136. (org-defkey org-agenda-mode-map [(shift right)] #'org-agenda-do-date-later)
  2137. (org-defkey org-agenda-mode-map [(shift left)] #'org-agenda-do-date-earlier)
  2138. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] #'org-agenda-do-date-later)
  2139. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] #'org-agenda-do-date-earlier)
  2140. (org-defkey org-agenda-mode-map ">" #'org-agenda-date-prompt)
  2141. (org-defkey org-agenda-mode-map "\C-c\C-s" #'org-agenda-schedule)
  2142. (org-defkey org-agenda-mode-map "\C-c\C-d" #'org-agenda-deadline)
  2143. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  2144. (while l (org-defkey org-agenda-mode-map
  2145. (number-to-string (pop l)) #'digit-argument)))
  2146. (org-defkey org-agenda-mode-map "F" #'org-agenda-follow-mode)
  2147. (org-defkey org-agenda-mode-map "R" #'org-agenda-clockreport-mode)
  2148. (org-defkey org-agenda-mode-map "E" #'org-agenda-entry-text-mode)
  2149. (org-defkey org-agenda-mode-map "l" #'org-agenda-log-mode)
  2150. (org-defkey org-agenda-mode-map "v" #'org-agenda-view-mode-dispatch)
  2151. (org-defkey org-agenda-mode-map "D" #'org-agenda-toggle-diary)
  2152. (org-defkey org-agenda-mode-map "!" #'org-agenda-toggle-deadlines)
  2153. (org-defkey org-agenda-mode-map "G" #'org-agenda-toggle-time-grid)
  2154. (org-defkey org-agenda-mode-map "r" #'org-agenda-redo)
  2155. (org-defkey org-agenda-mode-map "g" #'org-agenda-redo-all)
  2156. (org-defkey org-agenda-mode-map "e" #'org-agenda-set-effort)
  2157. (org-defkey org-agenda-mode-map "\C-c\C-xe" #'org-agenda-set-effort)
  2158. (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
  2159. #'org-clock-modify-effort-estimate)
  2160. (org-defkey org-agenda-mode-map "\C-c\C-xp" #'org-agenda-set-property)
  2161. (org-defkey org-agenda-mode-map "q" #'org-agenda-quit)
  2162. (org-defkey org-agenda-mode-map "Q" #'org-agenda-Quit)
  2163. (org-defkey org-agenda-mode-map "x" #'org-agenda-exit)
  2164. (org-defkey org-agenda-mode-map "\C-x\C-w" #'org-agenda-write)
  2165. (org-defkey org-agenda-mode-map "\C-x\C-s" #'org-save-all-org-buffers)
  2166. (org-defkey org-agenda-mode-map "s" #'org-save-all-org-buffers)
  2167. (org-defkey org-agenda-mode-map "T" #'org-agenda-show-tags)
  2168. (org-defkey org-agenda-mode-map "n" #'org-agenda-next-line)
  2169. (org-defkey org-agenda-mode-map "p" #'org-agenda-previous-line)
  2170. (org-defkey org-agenda-mode-map "N" #'org-agenda-next-item)
  2171. (org-defkey org-agenda-mode-map "P" #'org-agenda-previous-item)
  2172. (substitute-key-definition #'next-line #'org-agenda-next-line
  2173. org-agenda-mode-map global-map)
  2174. (substitute-key-definition #'previous-line #'org-agenda-previous-line
  2175. org-agenda-mode-map global-map)
  2176. (org-defkey org-agenda-mode-map "\C-c\C-a" #'org-attach)
  2177. (org-defkey org-agenda-mode-map "\C-c\C-n" #'org-agenda-next-date-line)
  2178. (org-defkey org-agenda-mode-map "\C-c\C-p" #'org-agenda-previous-date-line)
  2179. (org-defkey org-agenda-mode-map "\C-c," #'org-agenda-priority)
  2180. (org-defkey org-agenda-mode-map "," #'org-agenda-priority)
  2181. (org-defkey org-agenda-mode-map "i" #'org-agenda-diary-entry)
  2182. (org-defkey org-agenda-mode-map "c" #'org-agenda-goto-calendar)
  2183. (org-defkey org-agenda-mode-map "C" #'org-agenda-convert-date)
  2184. (org-defkey org-agenda-mode-map "M" #'org-agenda-phases-of-moon)
  2185. (org-defkey org-agenda-mode-map "S" #'org-agenda-sunrise-sunset)
  2186. (org-defkey org-agenda-mode-map "h" #'org-agenda-holidays)
  2187. (org-defkey org-agenda-mode-map "H" #'org-agenda-holidays)
  2188. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" #'org-agenda-clock-in)
  2189. (org-defkey org-agenda-mode-map "I" #'org-agenda-clock-in)
  2190. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" #'org-agenda-clock-out)
  2191. (org-defkey org-agenda-mode-map "O" #'org-agenda-clock-out)
  2192. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" #'org-agenda-clock-cancel)
  2193. (org-defkey org-agenda-mode-map "X" #'org-agenda-clock-cancel)
  2194. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" #'org-clock-goto)
  2195. (org-defkey org-agenda-mode-map "J" #'org-agenda-clock-goto)
  2196. (org-defkey org-agenda-mode-map "+" #'org-agenda-priority-up)
  2197. (org-defkey org-agenda-mode-map "-" #'org-agenda-priority-down)
  2198. (org-defkey org-agenda-mode-map [(shift up)] #'org-agenda-priority-up)
  2199. (org-defkey org-agenda-mode-map [(shift down)] #'org-agenda-priority-down)
  2200. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] #'org-agenda-priority-up)
  2201. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] #'org-agenda-priority-down)
  2202. (org-defkey org-agenda-mode-map "f" #'org-agenda-later)
  2203. (org-defkey org-agenda-mode-map "b" #'org-agenda-earlier)
  2204. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" #'org-agenda-columns)
  2205. (org-defkey org-agenda-mode-map "\C-c\C-x>" #'org-agenda-remove-restriction-lock)
  2206. (org-defkey org-agenda-mode-map "\C-c\C-x<" #'org-agenda-set-restriction-lock-from-agenda)
  2207. (org-defkey org-agenda-mode-map "[" #'org-agenda-manipulate-query-add)
  2208. (org-defkey org-agenda-mode-map "]" #'org-agenda-manipulate-query-subtract)
  2209. (org-defkey org-agenda-mode-map "{" #'org-agenda-manipulate-query-add-re)
  2210. (org-defkey org-agenda-mode-map "}" #'org-agenda-manipulate-query-subtract-re)
  2211. (org-defkey org-agenda-mode-map "\\" #'org-agenda-filter-by-tag)
  2212. (org-defkey org-agenda-mode-map "_" #'org-agenda-filter-by-effort)
  2213. (org-defkey org-agenda-mode-map "=" #'org-agenda-filter-by-regexp)
  2214. (org-defkey org-agenda-mode-map "/" #'org-agenda-filter)
  2215. (org-defkey org-agenda-mode-map "|" #'org-agenda-filter-remove-all)
  2216. (org-defkey org-agenda-mode-map "~" #'org-agenda-limit-interactively)
  2217. (org-defkey org-agenda-mode-map "<" #'org-agenda-filter-by-category)
  2218. (org-defkey org-agenda-mode-map "^" #'org-agenda-filter-by-top-headline)
  2219. (org-defkey org-agenda-mode-map ";" #'org-timer-set-timer)
  2220. (org-defkey org-agenda-mode-map "\C-c\C-x_" #'org-timer-stop)
  2221. (org-defkey org-agenda-mode-map "?" #'org-agenda-show-the-flagging-note)
  2222. (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" #'org-mobile-pull)
  2223. (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" #'org-mobile-push)
  2224. (org-defkey org-agenda-mode-map "\C-c\C-xI" #'org-info-find-node)
  2225. (org-defkey org-agenda-mode-map [mouse-2] #'org-agenda-goto-mouse)
  2226. (org-defkey org-agenda-mode-map [mouse-3] #'org-agenda-show-mouse)
  2227. (org-defkey org-agenda-mode-map [remap forward-paragraph] #'org-agenda-forward-block)
  2228. (org-defkey org-agenda-mode-map [remap backward-paragraph] #'org-agenda-backward-block)
  2229. (org-defkey org-agenda-mode-map "\C-c\C-c" #'org-agenda-ctrl-c-ctrl-c)
  2230. (when org-agenda-mouse-1-follows-link
  2231. (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
  2232. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu."
  2233. '("Agenda"
  2234. ("Agenda Files")
  2235. "--"
  2236. ("Agenda Dates"
  2237. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda)]
  2238. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  2239. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  2240. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
  2241. "--"
  2242. ("View"
  2243. ["Day View" org-agenda-day-view
  2244. :active (org-agenda-check-type nil 'agenda)
  2245. :style radio :selected (eq org-agenda-current-span 'day)
  2246. :keys "v d (or just d)"]
  2247. ["Week View" org-agenda-week-view
  2248. :active (org-agenda-check-type nil 'agenda)
  2249. :style radio :selected (eq org-agenda-current-span 'week)
  2250. :keys "v w"]
  2251. ["Fortnight View" org-agenda-fortnight-view
  2252. :active (org-agenda-check-type nil 'agenda)
  2253. :style radio :selected (eq org-agenda-current-span 'fortnight)
  2254. :keys "v t"]
  2255. ["Month View" org-agenda-month-view
  2256. :active (org-agenda-check-type nil 'agenda)
  2257. :style radio :selected (eq org-agenda-current-span 'month)
  2258. :keys "v m"]
  2259. ["Year View" org-agenda-year-view
  2260. :active (org-agenda-check-type nil 'agenda)
  2261. :style radio :selected (eq org-agenda-current-span 'year)
  2262. :keys "v y"]
  2263. "--"
  2264. ["Include Diary" org-agenda-toggle-diary
  2265. :style toggle :selected org-agenda-include-diary
  2266. :active (org-agenda-check-type nil 'agenda)]
  2267. ["Include Deadlines" org-agenda-toggle-deadlines
  2268. :style toggle :selected org-agenda-include-deadlines
  2269. :active (org-agenda-check-type nil 'agenda)]
  2270. ["Use Time Grid" org-agenda-toggle-time-grid
  2271. :style toggle :selected org-agenda-use-time-grid
  2272. :active (org-agenda-check-type nil 'agenda)]
  2273. "--"
  2274. ["Show clock report" org-agenda-clockreport-mode
  2275. :style toggle :selected org-agenda-clockreport-mode
  2276. :active (org-agenda-check-type nil 'agenda)]
  2277. ["Show some entry text" org-agenda-entry-text-mode
  2278. :style toggle :selected org-agenda-entry-text-mode
  2279. :active t]
  2280. "--"
  2281. ["Show Logbook entries" org-agenda-log-mode
  2282. :style toggle :selected org-agenda-show-log
  2283. :active (org-agenda-check-type nil 'agenda)
  2284. :keys "v l (or just l)"]
  2285. ["Include archived trees" org-agenda-archives-mode
  2286. :style toggle :selected org-agenda-archives-mode :active t
  2287. :keys "v a"]
  2288. ["Include archive files" (org-agenda-archives-mode t)
  2289. :style toggle :selected (eq org-agenda-archives-mode t) :active t
  2290. :keys "v A"]
  2291. "--"
  2292. ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
  2293. ("Filter current view"
  2294. ["with generic interface" org-agenda-filter t]
  2295. "--"
  2296. ["by category at cursor" org-agenda-filter-by-category t]
  2297. ["by tag" org-agenda-filter-by-tag t]
  2298. ["by effort" org-agenda-filter-by-effort t]
  2299. ["by regexp" org-agenda-filter-by-regexp t]
  2300. ["by top-level headline" org-agenda-filter-by-top-headline t]
  2301. "--"
  2302. ["Remove all filtering" org-agenda-filter-remove-all t]
  2303. "--"
  2304. ["limit" org-agenda-limit-interactively t])
  2305. ["Rebuild buffer" org-agenda-redo t]
  2306. ["Write view to file" org-agenda-write t]
  2307. ["Save all Org buffers" org-save-all-org-buffers t]
  2308. "--"
  2309. ["Show original entry" org-agenda-show t]
  2310. ["Go To (other window)" org-agenda-goto t]
  2311. ["Go To (this window)" org-agenda-switch-to t]
  2312. ["Capture with cursor date" org-agenda-capture t]
  2313. ["Follow Mode" org-agenda-follow-mode
  2314. :style toggle :selected org-agenda-follow-mode :active t]
  2315. ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  2316. "--"
  2317. ("TODO"
  2318. ["Cycle TODO" org-agenda-todo t]
  2319. ["Next TODO set" org-agenda-todo-nextset t]
  2320. ["Previous TODO set" org-agenda-todo-previousset t]
  2321. ["Add note" org-agenda-add-note t])
  2322. ("Archive/Refile/Delete"
  2323. ["Archive default" org-agenda-archive-default t]
  2324. ["Archive default" org-agenda-archive-default-with-confirmation t]
  2325. ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
  2326. ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
  2327. ["Archive subtree" org-agenda-archive t]
  2328. "--"
  2329. ["Refile" org-agenda-refile t]
  2330. "--"
  2331. ["Delete subtree" org-agenda-kill t])
  2332. ("Bulk action"
  2333. ["Mark entry" org-agenda-bulk-mark t]
  2334. ["Mark all" org-agenda-bulk-mark-all t]
  2335. ["Unmark entry" org-agenda-bulk-unmark t]
  2336. ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
  2337. ["Toggle mark" org-agenda-bulk-toggle t]
  2338. ["Toggle all" org-agenda-bulk-toggle-all t]
  2339. ["Mark regexp" org-agenda-bulk-mark-regexp t])
  2340. ["Act on all marked" org-agenda-bulk-action t]
  2341. "--"
  2342. ("Tags and Properties"
  2343. ["Show all Tags" org-agenda-show-tags t]
  2344. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  2345. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  2346. "--"
  2347. ["Column View" org-columns t])
  2348. ("Deadline/Schedule"
  2349. ["Schedule" org-agenda-schedule t]
  2350. ["Set Deadline" org-agenda-deadline t]
  2351. "--"
  2352. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda)]
  2353. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda)]
  2354. ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u S-right"]
  2355. ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u S-left"]
  2356. ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-right"]
  2357. ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda) :keys "C-u C-u S-left"]
  2358. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda)])
  2359. ("Clock and Effort"
  2360. ["Clock in" org-agenda-clock-in t]
  2361. ["Clock out" org-agenda-clock-out t]
  2362. ["Clock cancel" org-agenda-clock-cancel t]
  2363. ["Goto running clock" org-clock-goto t]
  2364. "--"
  2365. ["Set Effort" org-agenda-set-effort t]
  2366. ["Change clocked effort" org-clock-modify-effort-estimate
  2367. (org-clock-is-active)])
  2368. ("Priority"
  2369. ["Set Priority" org-agenda-priority t]
  2370. ["Increase Priority" org-agenda-priority-up t]
  2371. ["Decrease Priority" org-agenda-priority-down t]
  2372. ["Show Priority" org-priority-show t])
  2373. ("Calendar/Diary"
  2374. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda)]
  2375. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda)]
  2376. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda)]
  2377. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda)]
  2378. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda)]
  2379. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda)]
  2380. "--"
  2381. ["Create iCalendar File" org-icalendar-combine-agenda-files t])
  2382. "--"
  2383. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  2384. "--"
  2385. ("MobileOrg"
  2386. ["Push Files and Views" org-mobile-push t]
  2387. ["Get Captured and Flagged" org-mobile-pull t]
  2388. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "\\[org-agenda] ?"]
  2389. ["Show note / unflag" org-agenda-show-the-flagging-note t]
  2390. "--"
  2391. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  2392. "--"
  2393. ["Quit" org-agenda-quit t]
  2394. ["Exit and Release Buffers" org-agenda-exit t]
  2395. ))
  2396. ;;; Agenda undo
  2397. (defvar org-agenda-allow-remote-undo t
  2398. "Non-nil means allow remote undo from the agenda buffer.")
  2399. (defvar org-agenda-undo-has-started-in nil
  2400. "Buffers that have already seen `undo-start' in the current undo sequence.")
  2401. (defun org-agenda-undo ()
  2402. "Undo a remote editing step in the agenda.
  2403. This undoes changes both in the agenda buffer and in the remote buffer
  2404. that have been changed along."
  2405. (interactive)
  2406. (or org-agenda-allow-remote-undo
  2407. (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
  2408. (when (not (eq this-command last-command))
  2409. (setq org-agenda-undo-has-started-in nil
  2410. org-agenda-pending-undo-list org-agenda-undo-list))
  2411. (when (not org-agenda-pending-undo-list)
  2412. (user-error "No further undo information"))
  2413. (let* ((entry (pop org-agenda-pending-undo-list))
  2414. buf line cmd rembuf)
  2415. (setq cmd (pop entry) line (pop entry))
  2416. (setq rembuf (nth 2 entry))
  2417. (org-with-remote-undo rembuf
  2418. (while (bufferp (setq buf (pop entry)))
  2419. (when (pop entry)
  2420. (with-current-buffer buf
  2421. (let (;; (last-undo-buffer buf)
  2422. (inhibit-read-only t))
  2423. (unless (memq buf org-agenda-undo-has-started-in)
  2424. (push buf org-agenda-undo-has-started-in)
  2425. (make-local-variable 'pending-undo-list)
  2426. (undo-start))
  2427. (while (and pending-undo-list
  2428. (listp pending-undo-list)
  2429. (not (car pending-undo-list)))
  2430. (pop pending-undo-list))
  2431. (undo-more 1))))))
  2432. (org-goto-line line)
  2433. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  2434. (defun org-verify-change-for-undo (l1 l2)
  2435. "Verify that a real change occurred between the undo lists L1 and L2."
  2436. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  2437. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  2438. (not (eq l1 l2)))
  2439. ;;; Agenda dispatch
  2440. (defvar org-agenda-restrict-begin (make-marker))
  2441. (defvar org-agenda-restrict-end (make-marker))
  2442. (defvar org-agenda-last-dispatch-buffer nil)
  2443. (defvar org-agenda-overriding-restriction nil)
  2444. (defcustom org-agenda-custom-commands-contexts nil
  2445. "Alist of custom agenda keys and contextual rules.
  2446. For example, if you have a custom agenda command \"p\" and you
  2447. want this command to be accessible only from plain text files,
  2448. use this:
  2449. (setq org-agenda-custom-commands-contexts
  2450. \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\")))))
  2451. Here are the available contexts definitions:
  2452. in-file: command displayed only in matching files
  2453. in-mode: command displayed only in matching modes
  2454. not-in-file: command not displayed in matching files
  2455. not-in-mode: command not displayed in matching modes
  2456. in-buffer: command displayed only in matching buffers
  2457. not-in-buffer: command not displayed in matching buffers
  2458. [function]: a custom function taking no argument
  2459. If you define several checks, the agenda command will be
  2460. accessible if there is at least one valid check.
  2461. You can also bind a key to another agenda custom command
  2462. depending on contextual rules.
  2463. (setq org-agenda-custom-commands-contexts
  2464. \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\")))))
  2465. Here it means: in .txt files, use \"p\" as the key for the
  2466. agenda command otherwise associated with \"q\". (The command
  2467. originally associated with \"q\" is not displayed to avoid
  2468. duplicates.)"
  2469. :version "24.3"
  2470. :group 'org-agenda-custom-commands
  2471. :type '(repeat (list :tag "Rule"
  2472. (string :tag " Agenda key")
  2473. (string :tag "Replace by command")
  2474. (repeat :tag "Available when"
  2475. (choice
  2476. (cons :tag "Condition"
  2477. (choice
  2478. (const :tag "In file" in-file)
  2479. (const :tag "Not in file" not-in-file)
  2480. (const :tag "In buffer" in-buffer)
  2481. (const :tag "Not in buffer" not-in-buffer)
  2482. (const :tag "In mode" in-mode)
  2483. (const :tag "Not in mode" not-in-mode))
  2484. (regexp))
  2485. (function :tag "Custom function"))))))
  2486. (defcustom org-agenda-max-entries nil
  2487. "Maximum number of entries to display in an agenda.
  2488. This can be nil (no limit) or an integer or an alist of agenda
  2489. types with an associated number of entries to display in this
  2490. type."
  2491. :version "24.4"
  2492. :package-version '(Org . "8.0")
  2493. :group 'org-agenda-custom-commands
  2494. :type '(choice (symbol :tag "No limit" nil)
  2495. (integer :tag "Max number of entries")
  2496. (repeat
  2497. (cons (choice :tag "Agenda type"
  2498. (const agenda)
  2499. (const todo)
  2500. (const tags)
  2501. (const search))
  2502. (integer :tag "Max number of entries")))))
  2503. (defcustom org-agenda-max-todos nil
  2504. "Maximum number of TODOs to display in an agenda.
  2505. This can be nil (no limit) or an integer or an alist of agenda
  2506. types with an associated number of entries to display in this
  2507. type."
  2508. :version "24.4"
  2509. :package-version '(Org . "8.0")
  2510. :group 'org-agenda-custom-commands
  2511. :type '(choice (symbol :tag "No limit" nil)
  2512. (integer :tag "Max number of TODOs")
  2513. (repeat
  2514. (cons (choice :tag "Agenda type"
  2515. (const agenda)
  2516. (const todo)
  2517. (const tags)
  2518. (const search))
  2519. (integer :tag "Max number of TODOs")))))
  2520. (defcustom org-agenda-max-tags nil
  2521. "Maximum number of tagged entries to display in an agenda.
  2522. This can be nil (no limit) or an integer or an alist of agenda
  2523. types with an associated number of entries to display in this
  2524. type."
  2525. :version "24.4"
  2526. :package-version '(Org . "8.0")
  2527. :group 'org-agenda-custom-commands
  2528. :type '(choice (symbol :tag "No limit" nil)
  2529. (integer :tag "Max number of tagged entries")
  2530. (repeat
  2531. (cons (choice :tag "Agenda type"
  2532. (const agenda)
  2533. (const todo)
  2534. (const tags)
  2535. (const search))
  2536. (integer :tag "Max number of tagged entries")))))
  2537. (defcustom org-agenda-max-effort nil
  2538. "Maximum cumulated effort duration for the agenda.
  2539. This can be nil (no limit) or a number of minutes (as an integer)
  2540. or an alist of agenda types with an associated number of minutes
  2541. to limit entries to in this type."
  2542. :version "24.4"
  2543. :package-version '(Org . "8.0")
  2544. :group 'org-agenda-custom-commands
  2545. :type '(choice (symbol :tag "No limit" nil)
  2546. (integer :tag "Max number of minutes")
  2547. (repeat
  2548. (cons (choice :tag "Agenda type"
  2549. (const agenda)
  2550. (const todo)
  2551. (const tags)
  2552. (const search))
  2553. (integer :tag "Max number of minutes")))))
  2554. (defvar org-agenda-keep-restricted-file-list nil)
  2555. (defvar org-keys nil)
  2556. (defvar org-match nil)
  2557. ;;;###autoload
  2558. (defun org-agenda (&optional arg keys restriction)
  2559. "Dispatch agenda commands to collect entries to the agenda buffer.
  2560. Prompts for a command to execute. Any prefix arg will be passed
  2561. on to the selected command. The default selections are:
  2562. a Call `org-agenda-list' to display the agenda for current day or week.
  2563. t Call `org-todo-list' to display the global todo list.
  2564. T Call `org-todo-list' to display the global todo list, select only
  2565. entries with a specific TODO keyword (the user gets a prompt).
  2566. m Call `org-tags-view' to display headlines with tags matching
  2567. a condition (the user is prompted for the condition).
  2568. M Like `m', but select only TODO entries, no ordinary headlines.
  2569. e Export views to associated files.
  2570. s Search entries for keywords.
  2571. S Search entries for keywords, only with TODO keywords.
  2572. / Multi occur across all agenda files and also files listed
  2573. in `org-agenda-text-search-extra-files'.
  2574. < Restrict agenda commands to buffer, subtree, or region.
  2575. Press several times to get the desired effect.
  2576. > Remove a previous restriction.
  2577. # List \"stuck\" projects.
  2578. ! Configure what \"stuck\" means.
  2579. C Configure custom agenda commands.
  2580. More commands can be added by configuring the variable
  2581. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  2582. searches can be pre-defined in this way.
  2583. If the current buffer is in Org mode and visiting a file, you can also
  2584. first press `<' once to indicate that the agenda should be temporarily
  2585. \(until the next use of `\\[org-agenda]') restricted to the current file.
  2586. Pressing `<' twice means to restrict to the current subtree or region
  2587. \(if active)."
  2588. (interactive "P")
  2589. (catch 'exit
  2590. (let* ((org-keys keys)
  2591. (prefix-descriptions nil)
  2592. (org-agenda-buffer-name org-agenda-buffer-name)
  2593. (org-agenda-window-setup (if (equal (buffer-name)
  2594. org-agenda-buffer-name)
  2595. 'current-window
  2596. org-agenda-window-setup))
  2597. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  2598. (org-agenda-custom-commands
  2599. ;; normalize different versions
  2600. (delq nil
  2601. (mapcar
  2602. (lambda (x)
  2603. (cond ((stringp (cdr x))
  2604. (push x prefix-descriptions)
  2605. nil)
  2606. ((stringp (nth 1 x)) x)
  2607. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  2608. (t (cons (car x) (cons "" (cdr x))))))
  2609. org-agenda-custom-commands)))
  2610. (org-agenda-custom-commands
  2611. (org-contextualize-keys
  2612. org-agenda-custom-commands org-agenda-custom-commands-contexts))
  2613. ;; (buf (current-buffer))
  2614. (bfn (buffer-file-name (buffer-base-buffer)))
  2615. entry type org-match lprops ans) ;; key
  2616. ;; Turn off restriction unless there is an overriding one,
  2617. (unless org-agenda-overriding-restriction
  2618. (unless org-agenda-keep-restricted-file-list
  2619. ;; There is a request to keep the file list in place
  2620. (put 'org-agenda-files 'org-restrict nil))
  2621. (setq org-agenda-restrict nil)
  2622. (move-marker org-agenda-restrict-begin nil)
  2623. (move-marker org-agenda-restrict-end nil))
  2624. ;; Remember where this call originated
  2625. (setq org-agenda-last-dispatch-buffer (current-buffer))
  2626. (unless org-keys
  2627. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  2628. org-keys (car ans)
  2629. restriction (cdr ans)))
  2630. ;; If we have sticky agenda buffers, set a name for the buffer,
  2631. ;; depending on the invoking keys. The user may still set this
  2632. ;; as a command option, which will overwrite what we do here.
  2633. (when org-agenda-sticky
  2634. (setq org-agenda-buffer-name
  2635. (format "*Org Agenda(%s)*" org-keys)))
  2636. ;; Establish the restriction, if any
  2637. (when (and (not org-agenda-overriding-restriction) restriction)
  2638. (put 'org-agenda-files 'org-restrict (list bfn))
  2639. (cond
  2640. ((eq restriction 'region)
  2641. (setq org-agenda-restrict (current-buffer))
  2642. (move-marker org-agenda-restrict-begin (region-beginning))
  2643. (move-marker org-agenda-restrict-end (region-end)))
  2644. ((eq restriction 'subtree)
  2645. (save-excursion
  2646. (setq org-agenda-restrict (current-buffer))
  2647. (org-back-to-heading t)
  2648. (move-marker org-agenda-restrict-begin (point))
  2649. (move-marker org-agenda-restrict-end
  2650. (progn (org-end-of-subtree t)))))
  2651. ((and (eq restriction 'buffer)
  2652. (or (< 1 (point-min))
  2653. (< (point-max) (1+ (buffer-size)))))
  2654. (setq org-agenda-restrict (current-buffer))
  2655. (move-marker org-agenda-restrict-begin (point-min))
  2656. (move-marker org-agenda-restrict-end (point-max)))))
  2657. ;; For example the todo list should not need it (but does...)
  2658. (cond
  2659. ((setq entry (assoc org-keys org-agenda-custom-commands))
  2660. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  2661. (progn
  2662. ;; FIXME: Is (nth 3 entry) supposed to have access (via dynvars)
  2663. ;; to some of the local variables? There's no doc about
  2664. ;; that for `org-agenda-custom-commands'.
  2665. (setq type (nth 2 entry) org-match (eval (nth 3 entry) t)
  2666. lprops (nth 4 entry))
  2667. (when org-agenda-sticky
  2668. (setq org-agenda-buffer-name
  2669. (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
  2670. (format "*Org Agenda(%s)*" org-keys))))
  2671. (cl-progv
  2672. (mapcar #'car lprops)
  2673. (mapcar (lambda (binding) (eval (cadr binding) t)) lprops)
  2674. (pcase type
  2675. (`agenda
  2676. (org-agenda-list arg))
  2677. (`agenda*
  2678. (org-agenda-list arg nil nil t))
  2679. (`alltodo
  2680. (org-todo-list arg))
  2681. (`search
  2682. (org-search-view arg org-match nil))
  2683. (`stuck
  2684. (org-agenda-list-stuck-projects arg))
  2685. (`tags
  2686. (org-tags-view arg org-match))
  2687. (`tags-todo
  2688. (org-tags-view '(4) org-match))
  2689. (`todo
  2690. (org-todo-list org-match))
  2691. (`tags-tree
  2692. (org-check-for-org-mode)
  2693. (org-match-sparse-tree arg org-match))
  2694. (`todo-tree
  2695. (org-check-for-org-mode)
  2696. (org-occur (concat "^" org-outline-regexp "[ \t]*"
  2697. (regexp-quote org-match) "\\>")))
  2698. (`occur-tree
  2699. (org-check-for-org-mode)
  2700. (org-occur org-match))
  2701. ((pred functionp)
  2702. (funcall type org-match))
  2703. ;; FIXME: Will signal an error since it's not `functionp'!
  2704. ((pred fboundp) (funcall type org-match))
  2705. (_ (user-error "Invalid custom agenda command type %s" type))))
  2706. (let ((inhibit-read-only t))
  2707. (add-text-properties (point-min) (point-max)
  2708. `(org-lprops ,lprops))))
  2709. (org-agenda-run-series (nth 1 entry) (cddr entry))))
  2710. ((equal org-keys "C")
  2711. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  2712. (customize-variable 'org-agenda-custom-commands))
  2713. ((equal org-keys "a") (call-interactively 'org-agenda-list))
  2714. ((equal org-keys "s") (call-interactively 'org-search-view))
  2715. ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
  2716. ((equal org-keys "t") (call-interactively 'org-todo-list))
  2717. ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  2718. ((equal org-keys "m") (call-interactively 'org-tags-view))
  2719. ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  2720. ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
  2721. ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
  2722. (add-hook
  2723. 'post-command-hook
  2724. (lambda ()
  2725. (unless (current-message)
  2726. (let* ((m (org-agenda-get-any-marker))
  2727. (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
  2728. (when note
  2729. (message "FLAGGING-NOTE ([?] for more info): %s"
  2730. (org-add-props
  2731. (replace-regexp-in-string
  2732. "\\\\n" "//"
  2733. (copy-sequence note))
  2734. nil 'face 'org-warning))))))
  2735. t t))
  2736. ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  2737. ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
  2738. ((equal org-keys "!") (customize-variable 'org-stuck-projects))
  2739. (t (user-error "Invalid agenda key"))))))
  2740. (defvar org-agenda-multi)
  2741. (defun org-agenda-append-agenda ()
  2742. "Append another agenda view to the current one.
  2743. This function allows interactive building of block agendas.
  2744. Agenda views are separated by `org-agenda-block-separator'."
  2745. (interactive)
  2746. (unless (derived-mode-p 'org-agenda-mode)
  2747. (user-error "Can only append from within agenda buffer"))
  2748. (let ((org-agenda-multi t))
  2749. (org-agenda)
  2750. (widen)
  2751. (org-agenda-finalize)
  2752. (setq buffer-read-only t)
  2753. (org-agenda-fit-window-to-buffer)))
  2754. (defun org-agenda-normalize-custom-commands (cmds)
  2755. "Normalize custom commands CMDS."
  2756. (delq nil
  2757. (mapcar
  2758. (lambda (x)
  2759. (cond ((stringp (cdr x)) nil)
  2760. ((stringp (nth 1 x)) x)
  2761. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  2762. (t (cons (car x) (cons "" (cdr x))))))
  2763. cmds)))
  2764. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  2765. "The user interface for selecting an agenda command."
  2766. (catch 'exit
  2767. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  2768. (restrict-ok (and bfn (derived-mode-p 'org-mode)))
  2769. (region-p (org-region-active-p))
  2770. (custom org-agenda-custom-commands)
  2771. (selstring "")
  2772. restriction second-time
  2773. c entry key type match prefixes rmheader header-end custom1 desc
  2774. line lines left right n n1)
  2775. (save-window-excursion
  2776. (delete-other-windows)
  2777. (org-switch-to-buffer-other-window " *Agenda Commands*")
  2778. (erase-buffer)
  2779. (insert (eval-when-compile
  2780. (let ((header
  2781. (copy-sequence
  2782. "Press key for an agenda command:
  2783. -------------------------------- < Buffer, subtree/region restriction
  2784. a Agenda for current week or day > Remove restriction
  2785. t List of all TODO entries e Export agenda views
  2786. m Match a TAGS/PROP/TODO query T Entries with special TODO kwd
  2787. s Search for keywords M Like m, but only TODO entries
  2788. / Multi-occur S Like s, but only TODO entries
  2789. ? Find :FLAGGED: entries C Configure custom agenda commands
  2790. * Toggle sticky agenda views # List stuck projects (!=configure)
  2791. "))
  2792. (start 0))
  2793. (while (string-match
  2794. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  2795. header start)
  2796. (setq start (match-end 0))
  2797. (add-text-properties (match-beginning 2) (match-end 2)
  2798. '(face bold) header))
  2799. header)))
  2800. (setq header-end (point-marker))
  2801. (while t
  2802. (setq custom1 custom)
  2803. (when (eq rmheader t)
  2804. (org-goto-line 1)
  2805. (re-search-forward ":" nil t)
  2806. (delete-region (match-end 0) (line-end-position))
  2807. (forward-char 1)
  2808. (looking-at "-+")
  2809. (delete-region (match-end 0) (line-end-position))
  2810. (move-marker header-end (match-end 0)))
  2811. (goto-char header-end)
  2812. (delete-region (point) (point-max))
  2813. ;; Produce all the lines that describe custom commands and prefixes
  2814. (setq lines nil)
  2815. (while (setq entry (pop custom1))
  2816. (setq key (car entry) desc (nth 1 entry)
  2817. type (nth 2 entry)
  2818. match (nth 3 entry))
  2819. (if (> (length key) 1)
  2820. (cl-pushnew (string-to-char key) prefixes :test #'equal)
  2821. (setq line
  2822. (format
  2823. "%-4s%-14s"
  2824. (org-add-props (copy-sequence key)
  2825. '(face bold))
  2826. (cond
  2827. ((string-match "\\S-" desc) desc)
  2828. ((eq type 'agenda) "Agenda for current week or day")
  2829. ((eq type 'agenda*) "Appointments for current week or day")
  2830. ((eq type 'alltodo) "List of all TODO entries")
  2831. ((eq type 'search) "Word search")
  2832. ((eq type 'stuck) "List of stuck projects")
  2833. ((eq type 'todo) "TODO keyword")
  2834. ((eq type 'tags) "Tags query")
  2835. ((eq type 'tags-todo) "Tags (TODO)")
  2836. ((eq type 'tags-tree) "Tags tree")
  2837. ((eq type 'todo-tree) "TODO kwd tree")
  2838. ((eq type 'occur-tree) "Occur tree")
  2839. ((functionp type) (if (symbolp type)
  2840. (symbol-name type)
  2841. "Lambda expression"))
  2842. (t "???"))))
  2843. (cond
  2844. ((not (org-string-nw-p match)) nil)
  2845. (org-agenda-menu-show-matcher
  2846. (setq line
  2847. (concat line ": "
  2848. (cond
  2849. ((stringp match)
  2850. (propertize match 'face 'org-warning))
  2851. ((listp type)
  2852. (format "set of %d commands" (length type)))))))
  2853. (t
  2854. (org-add-props line nil 'help-echo (concat "Matcher: " match))))
  2855. (push line lines)))
  2856. (setq lines (nreverse lines))
  2857. (when prefixes
  2858. (mapc (lambda (x)
  2859. (push
  2860. (format "%s %s"
  2861. (org-add-props (char-to-string x)
  2862. nil 'face 'bold)
  2863. (or (cdr (assoc (concat selstring
  2864. (char-to-string x))
  2865. prefix-descriptions))
  2866. "Prefix key"))
  2867. lines))
  2868. prefixes))
  2869. ;; Check if we should display in two columns
  2870. (if org-agenda-menu-two-columns
  2871. (progn
  2872. (setq n (length lines)
  2873. n1 (+ (/ n 2) (mod n 2))
  2874. right (nthcdr n1 lines)
  2875. left (copy-sequence lines))
  2876. (setcdr (nthcdr (1- n1) left) nil))
  2877. (setq left lines right nil))
  2878. (while left
  2879. (insert "\n" (pop left))
  2880. (when right
  2881. (if (< (current-column) 40)
  2882. (move-to-column 40 t)
  2883. (insert " "))
  2884. (insert (pop right))))
  2885. ;; Make the window the right size
  2886. (goto-char (point-min))
  2887. (if second-time
  2888. (when (not (pos-visible-in-window-p (point-max)))
  2889. (org-fit-window-to-buffer))
  2890. (setq second-time t)
  2891. (org-fit-window-to-buffer))
  2892. ;; Hint to navigation if window too small for all information
  2893. (setq header-line-format
  2894. (when (not (pos-visible-in-window-p (point-max)))
  2895. "Use C-v, M-v, C-n or C-p to navigate."))
  2896. ;; Ask for selection
  2897. (cl-loop
  2898. do (progn
  2899. (message "Press key for agenda command%s:"
  2900. (if (or restrict-ok org-agenda-overriding-restriction)
  2901. (if org-agenda-overriding-restriction
  2902. " (restriction lock active)"
  2903. (if restriction
  2904. (format " (restricted to %s)" restriction)
  2905. " (unrestricted)"))
  2906. ""))
  2907. (setq c (read-char-exclusive)))
  2908. until (not (memq c '(14 16 22 134217846)))
  2909. do (org-scroll c))
  2910. (message "")
  2911. (cond
  2912. ((assoc (char-to-string c) custom)
  2913. (setq selstring (concat selstring (char-to-string c)))
  2914. (throw 'exit (cons selstring restriction)))
  2915. ((memq c prefixes)
  2916. (setq selstring (concat selstring (char-to-string c))
  2917. prefixes nil
  2918. rmheader (or rmheader t)
  2919. custom (delq nil (mapcar
  2920. (lambda (x)
  2921. (if (or (= (length (car x)) 1)
  2922. (/= (string-to-char (car x)) c))
  2923. nil
  2924. (cons (substring (car x) 1) (cdr x))))
  2925. custom))))
  2926. ((eq c ?*)
  2927. (call-interactively 'org-toggle-sticky-agenda)
  2928. (sit-for 2))
  2929. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  2930. (message "Restriction is only possible in Org buffers")
  2931. (ding) (sit-for 1))
  2932. ((eq c ?1)
  2933. (org-agenda-remove-restriction-lock 'noupdate)
  2934. (setq restriction 'buffer))
  2935. ((eq c ?0)
  2936. (org-agenda-remove-restriction-lock 'noupdate)
  2937. (setq restriction (if region-p 'region 'subtree)))
  2938. ((eq c ?<)
  2939. (org-agenda-remove-restriction-lock 'noupdate)
  2940. (setq restriction
  2941. (cond
  2942. ((eq restriction 'buffer)
  2943. (if region-p 'region 'subtree))
  2944. ((memq restriction '(subtree region))
  2945. nil)
  2946. (t 'buffer))))
  2947. ((eq c ?>)
  2948. (org-agenda-remove-restriction-lock 'noupdate)
  2949. (setq restriction nil))
  2950. ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
  2951. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  2952. ((and (> (length selstring) 0) (eq c ?\d))
  2953. (delete-window)
  2954. (org-agenda-get-restriction-and-command prefix-descriptions))
  2955. ((equal c ?q) (user-error "Abort"))
  2956. (t (user-error "Invalid key %c" c))))))))
  2957. (defun org-agenda-fit-window-to-buffer ()
  2958. "Fit the window to the buffer size."
  2959. (and (memq org-agenda-window-setup '(reorganize-frame))
  2960. (fboundp 'fit-window-to-buffer)
  2961. (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
  2962. (= (car org-agenda-window-frame-fractions) 1.0))
  2963. (delete-other-windows)
  2964. (org-fit-window-to-buffer
  2965. nil
  2966. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  2967. (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
  2968. (defvar org-cmd nil)
  2969. (defvar org-agenda-overriding-cmd nil)
  2970. (defvar org-agenda-overriding-arguments nil)
  2971. (defvar org-agenda-overriding-cmd-arguments nil)
  2972. (defun org-let (list &rest body) ;FIXME: So many kittens are suffering here.
  2973. (declare (indent 1) (obsolete cl-progv "2021"))
  2974. (eval (cons 'let (cons list body))))
  2975. (defun org-let2 (list1 list2 &rest body) ;FIXME: Where did our karma go?
  2976. (declare (indent 2) (obsolete cl-progv "2021"))
  2977. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  2978. (defun org-agenda-run-series (name series)
  2979. "Run agenda NAME as a SERIES of agenda commands."
  2980. (let* ((gprops (nth 1 series))
  2981. (gvars (mapcar #'car gprops))
  2982. (gvals (mapcar (lambda (binding) (eval (cadr binding) t)) gprops)))
  2983. (cl-progv gvars gvals (org-agenda-prepare name))
  2984. ;; We need to reset agenda markers here, because when constructing a
  2985. ;; block agenda, the individual blocks do not do that.
  2986. (org-agenda-reset-markers)
  2987. (with-no-warnings
  2988. (defvar match)) ;Used via the `eval' below.
  2989. (let* ((org-agenda-multi t)
  2990. ;; FIXME: Redo should contain lists of (FUNS . ARGS) rather
  2991. ;; than expressions, so you don't need to `quote' the args
  2992. ;; and you just need to `apply' instead of `eval' when using it.
  2993. (redo (list 'org-agenda-run-series name (list 'quote series)))
  2994. (cmds (car series))
  2995. match
  2996. org-cmd type lprops)
  2997. (while (setq org-cmd (pop cmds))
  2998. (setq type (car org-cmd))
  2999. (setq match (eval (nth 1 org-cmd) t))
  3000. (setq lprops (nth 2 org-cmd))
  3001. (let ((org-agenda-overriding-arguments
  3002. (if (eq org-agenda-overriding-cmd org-cmd)
  3003. (or org-agenda-overriding-arguments
  3004. org-agenda-overriding-cmd-arguments)))
  3005. (lvars (mapcar #'car lprops))
  3006. (lvals (mapcar (lambda (binding) (eval (cadr binding) t)) lprops)))
  3007. (cl-progv (append gvars lvars) (append gvals lvals)
  3008. (pcase type
  3009. (`agenda
  3010. (call-interactively 'org-agenda-list))
  3011. (`agenda*
  3012. (funcall 'org-agenda-list nil nil t))
  3013. (`alltodo
  3014. (call-interactively 'org-todo-list))
  3015. (`search
  3016. (org-search-view current-prefix-arg match nil))
  3017. (`stuck
  3018. (call-interactively 'org-agenda-list-stuck-projects))
  3019. (`tags
  3020. (org-tags-view current-prefix-arg match))
  3021. (`tags-todo
  3022. (org-tags-view '(4) match))
  3023. (`todo
  3024. (org-todo-list match))
  3025. ((pred fboundp)
  3026. (funcall type match))
  3027. (_ (error "Invalid type in command series"))))))
  3028. (widen)
  3029. (let ((inhibit-read-only t))
  3030. (add-text-properties (point-min) (point-max)
  3031. `(org-series t org-series-redo-cmd ,redo)))
  3032. (setq org-agenda-redo-command redo)
  3033. (goto-char (point-min)))
  3034. (org-agenda-fit-window-to-buffer)
  3035. (cl-progv gvars gvals (org-agenda-finalize))))
  3036. (defun org-agenda--split-plist (plist)
  3037. ;; We could/should arguably use `map-keys' and `map-values'.
  3038. (let (keys vals)
  3039. (while plist
  3040. (push (pop plist) keys)
  3041. (push (pop plist) vals))
  3042. (cons (nreverse keys) (nreverse vals))))
  3043. ;;;###autoload
  3044. (defmacro org-batch-agenda (cmd-key &rest parameters)
  3045. "Run an agenda command in batch mode and send the result to STDOUT.
  3046. If CMD-KEY is a string of length 1, it is used as a key in
  3047. `org-agenda-custom-commands' and triggers this command. If it is a
  3048. longer string it is used as a tags/todo match string.
  3049. Parameters are alternating variable names and values that will be bound
  3050. before running the agenda command."
  3051. (pcase-let ((`(,vars . ,exps) (org-agenda--split-plist parameters)))
  3052. `(org--batch-agenda ,cmd-key ',vars (list ,@exps))))
  3053. (defun org--batch-agenda (cmd-key vars vals)
  3054. ;; `org-batch-agenda' is a macro because every other "parameter" is
  3055. ;; a variable name rather than an expression to evaluate. Yuck!
  3056. (cl-progv vars vals
  3057. (let (org-agenda-sticky)
  3058. (if (> (length cmd-key) 1)
  3059. (org-tags-view nil cmd-key)
  3060. (org-agenda nil cmd-key))))
  3061. (set-buffer org-agenda-buffer-name)
  3062. (princ (buffer-string)))
  3063. (defvar org-agenda-info nil)
  3064. ;;;###autoload
  3065. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  3066. "Run an agenda command in batch mode and send the result to STDOUT.
  3067. If CMD-KEY is a string of length 1, it is used as a key in
  3068. `org-agenda-custom-commands' and triggers this command. If it is a
  3069. longer string it is used as a tags/todo match string.
  3070. Parameters are alternating variable names and values that will be bound
  3071. before running the agenda command.
  3072. The output gives a line for each selected agenda item. Each
  3073. item is a list of comma-separated values, like this:
  3074. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  3075. category The category of the item
  3076. head The headline, without TODO kwd, TAGS and PRIORITY
  3077. type The type of the agenda entry, can be
  3078. todo selected in TODO match
  3079. tagsmatch selected in tags match
  3080. diary imported from diary
  3081. deadline a deadline on given date
  3082. scheduled scheduled on given date
  3083. timestamp entry has timestamp on given date
  3084. closed entry was closed on given date
  3085. upcoming-deadline warning about deadline
  3086. past-scheduled forwarded scheduled item
  3087. block entry has date block including g. date
  3088. todo The todo keyword, if any
  3089. tags All tags including inherited ones, separated by colons
  3090. date The relevant date, like 2007-2-14
  3091. time The time, like 15:00-16:50
  3092. extra String with extra planning info
  3093. priority-l The priority letter if any was given
  3094. priority-n The computed numerical priority
  3095. agenda-day The day in the agenda where this is listed"
  3096. (pcase-let ((`(,vars . ,exps) (org-agenda--split-plist parameters)))
  3097. `(org--batch-agenda-csv ,cmd-key ',vars (list ,@exps))))
  3098. (defun org--batch-agenda-csv (cmd-key vars vals)
  3099. ;; `org-batch-agenda-csv' is a macro because every other "parameter" is
  3100. ;; a variable name rather than an expression to evaluate. Yuck!
  3101. (let ((org-agenda-remove-tags t))
  3102. (cl-progv vars vals
  3103. ;; FIXME: Shouldn't this be 1 (see commit 10173ad6d610b)?
  3104. (if (> (length cmd-key) 2)
  3105. (org-tags-view nil cmd-key)
  3106. (org-agenda nil cmd-key))))
  3107. (set-buffer org-agenda-buffer-name)
  3108. (let ((lines (org-split-string (buffer-string) "\n")))
  3109. (dolist (line lines)
  3110. (when (get-text-property 0 'org-category line)
  3111. (setq org-agenda-info
  3112. (org-fix-agenda-info (text-properties-at 0 line)))
  3113. (princ
  3114. (mapconcat #'org-agenda-export-csv-mapper
  3115. '(org-category txt type todo tags date time extra
  3116. priority-letter priority agenda-day)
  3117. ","))
  3118. (princ "\n")))))
  3119. (defun org-fix-agenda-info (props)
  3120. "Make sure all properties on an agenda item have a canonical form.
  3121. This ensures the export commands can easily use it."
  3122. (let (tmp re)
  3123. (when (setq tmp (plist-get props 'tags))
  3124. (setq props (plist-put props 'tags (mapconcat #'identity tmp ":"))))
  3125. (when (setq tmp (plist-get props 'date))
  3126. (when (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  3127. (let ((calendar-date-display-form '(year "-" month "-" day)))
  3128. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  3129. (setq tmp (calendar-date-string tmp)))
  3130. (setq props (plist-put props 'date tmp)))
  3131. (when (setq tmp (plist-get props 'day))
  3132. (when (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  3133. (let ((calendar-date-display-form '(year "-" month "-" day)))
  3134. (setq tmp (calendar-date-string tmp)))
  3135. (setq props (plist-put props 'day tmp))
  3136. (setq props (plist-put props 'agenda-day tmp)))
  3137. (when (setq tmp (plist-get props 'txt))
  3138. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  3139. (plist-put props 'priority-letter (match-string 1 tmp))
  3140. (setq tmp (replace-match "" t t tmp)))
  3141. (when (and (setq re (plist-get props 'org-todo-regexp))
  3142. (setq re (concat "\\`\\.*" re " ?"))
  3143. (let ((case-fold-search nil)) (string-match re tmp)))
  3144. (plist-put props 'todo (match-string 1 tmp))
  3145. (setq tmp (replace-match "" t t tmp)))
  3146. (plist-put props 'txt tmp)))
  3147. props)
  3148. (defun org-agenda-export-csv-mapper (prop)
  3149. (let ((res (plist-get org-agenda-info prop)))
  3150. (setq res
  3151. (cond
  3152. ((not res) "")
  3153. ((stringp res) res)
  3154. (t (prin1-to-string res))))
  3155. (org-trim (replace-regexp-in-string "," ";" res nil t))))
  3156. ;;;###autoload
  3157. (defun org-store-agenda-views (&rest _parameters)
  3158. "Store agenda views."
  3159. (interactive)
  3160. (org--batch-store-agenda-views nil nil))
  3161. ;;;###autoload
  3162. (defmacro org-batch-store-agenda-views (&rest parameters)
  3163. "Run all custom agenda commands that have a file argument."
  3164. (pcase-let ((`(,vars . ,exps) (org-agenda--split-plist parameters)))
  3165. `(org--batch-store-agenda-views ',vars (list ,@exps))))
  3166. (defun org--batch-store-agenda-views (vars vals)
  3167. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  3168. (pop-up-frames nil)
  3169. (dir default-directory)
  3170. cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
  3171. (save-window-excursion
  3172. (while cmds
  3173. (setq cmd (pop cmds)
  3174. thiscmdkey (car cmd)
  3175. thiscmdcmd (cdr cmd)
  3176. match (nth 2 thiscmdcmd)
  3177. bufname (if org-agenda-sticky
  3178. (or (and (stringp match)
  3179. (format "*Org Agenda(%s:%s)*" thiscmdkey match))
  3180. (format "*Org Agenda(%s)*" thiscmdkey))
  3181. org-agenda-buffer-name)
  3182. cmd-or-set (nth 2 cmd)
  3183. opts (nth (if (listp cmd-or-set) 3 4) cmd)
  3184. files (nth (if (listp cmd-or-set) 4 5) cmd))
  3185. (if (stringp files) (setq files (list files)))
  3186. (when files
  3187. (let* ((opts (append org-agenda-exporter-settings opts))
  3188. (vars (append (mapcar #'car opts) vars))
  3189. (vals (append (mapcar (lambda (binding) (eval (cadr binding) t))
  3190. opts)
  3191. vals)))
  3192. (cl-progv vars vals
  3193. (org-agenda nil thiscmdkey))
  3194. (set-buffer bufname)
  3195. (while files
  3196. (cl-progv vars vals
  3197. (org-agenda-write (expand-file-name (pop files) dir)
  3198. nil t bufname))))
  3199. (and (get-buffer bufname)
  3200. (kill-buffer bufname)))))))
  3201. (defvar org-agenda-current-span nil
  3202. "The current span used in the agenda view.") ; local variable in the agenda buffer
  3203. (defun org-agenda-mark-header-line (pos)
  3204. "Mark the line at POS as an agenda structure header."
  3205. (save-excursion
  3206. (goto-char pos)
  3207. (put-text-property (line-beginning-position) (line-end-position)
  3208. 'org-agenda-structural-header t)
  3209. (when org-agenda-title-append
  3210. (put-text-property (line-beginning-position) (line-end-position)
  3211. 'org-agenda-title-append org-agenda-title-append))))
  3212. (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
  3213. (defvar org-agenda-write-buffer-name "Agenda View")
  3214. (defun org-agenda-write (file &optional open nosettings agenda-bufname)
  3215. "Write the current buffer (an agenda view) as a file.
  3216. Depending on the extension of the file name, plain text (.txt),
  3217. HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
  3218. If the extension is .ics, translate visible agenda into iCalendar
  3219. format. If the extension is .org, collect all subtrees
  3220. corresponding to the agenda entries and add them in an .org file.
  3221. With prefix argument OPEN, open the new file immediately. If
  3222. NOSETTINGS is given, do not scope the settings of
  3223. `org-agenda-exporter-settings' into the export commands. This is
  3224. used when the settings have already been scoped and we do not
  3225. wish to overrule other, higher priority settings. If
  3226. AGENDA-BUFFER-NAME is provided, use this as the buffer name for
  3227. the agenda to write."
  3228. (interactive "FWrite agenda to file: \nP")
  3229. (if (or (not (file-writable-p file))
  3230. (and (file-exists-p file)
  3231. (if (called-interactively-p 'any)
  3232. (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
  3233. (user-error "Cannot write agenda to file %s" file))
  3234. (cl-progv
  3235. (if nosettings nil (mapcar #'car org-agenda-exporter-settings))
  3236. (if nosettings nil (mapcar (lambda (binding) (eval (cadr binding) t))
  3237. org-agenda-exporter-settings))
  3238. (save-excursion
  3239. (save-window-excursion
  3240. (let ((bs (copy-sequence (buffer-string)))
  3241. (extension (file-name-extension file))
  3242. (default-directory (file-name-directory file))
  3243. ) ;; beg content
  3244. (with-temp-buffer
  3245. (rename-buffer org-agenda-write-buffer-name t)
  3246. (set-buffer-modified-p nil)
  3247. (insert bs)
  3248. (org-agenda-remove-marked-text 'invisible 'org-filtered)
  3249. (run-hooks 'org-agenda-before-write-hook)
  3250. (cond
  3251. ((bound-and-true-p org-mobile-creating-agendas)
  3252. (org-mobile-write-agenda-for-mobile file))
  3253. ((string= "org" extension)
  3254. (let (content p m message-log-max)
  3255. (goto-char (point-min))
  3256. (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
  3257. (goto-char p)
  3258. (setq m (get-text-property (point) 'org-hd-marker))
  3259. (when m
  3260. (push (with-current-buffer (marker-buffer m)
  3261. (goto-char m)
  3262. (org-copy-subtree 1 nil t t)
  3263. org-subtree-clip)
  3264. content)))
  3265. (find-file file)
  3266. (erase-buffer)
  3267. (dolist (s content) (org-paste-subtree 1 s))
  3268. (write-file file)
  3269. (kill-buffer (current-buffer))
  3270. (message "Org file written to %s" file)))
  3271. ((member extension '("html" "htm"))
  3272. (or (require 'htmlize nil t)
  3273. (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
  3274. (declare-function htmlize-buffer "htmlize" (&optional buffer))
  3275. (set-buffer (htmlize-buffer (current-buffer)))
  3276. (when org-agenda-export-html-style
  3277. ;; replace <style> section with org-agenda-export-html-style
  3278. (goto-char (point-min))
  3279. (kill-region (- (search-forward "<style") 6)
  3280. (search-forward "</style>"))
  3281. (insert org-agenda-export-html-style))
  3282. (write-file file)
  3283. (kill-buffer (current-buffer))
  3284. (message "HTML written to %s" file))
  3285. ((string= "ps" extension)
  3286. (require 'ps-print)
  3287. (ps-print-buffer-with-faces file)
  3288. (message "Postscript written to %s" file))
  3289. ((string= "pdf" extension)
  3290. (require 'ps-print)
  3291. (ps-print-buffer-with-faces
  3292. (concat (file-name-sans-extension file) ".ps"))
  3293. (call-process "ps2pdf" nil nil nil
  3294. (expand-file-name
  3295. (concat (file-name-sans-extension file) ".ps"))
  3296. (expand-file-name file))
  3297. (delete-file (concat (file-name-sans-extension file) ".ps"))
  3298. (message "PDF written to %s" file))
  3299. ((string= "ics" extension)
  3300. (require 'ox-icalendar)
  3301. (declare-function org-icalendar-export-current-agenda
  3302. "ox-icalendar" (file))
  3303. (org-icalendar-export-current-agenda (expand-file-name file)))
  3304. (t
  3305. (let ((bs (buffer-string)))
  3306. (find-file file)
  3307. (erase-buffer)
  3308. (insert bs)
  3309. (save-buffer 0)
  3310. (kill-buffer (current-buffer))
  3311. (message "Plain text written to %s" file))))))))
  3312. (set-buffer (or agenda-bufname
  3313. ;; FIXME: I'm pretty sure called-interactively-p
  3314. ;; doesn't do what we want here!
  3315. (and (called-interactively-p 'any) (buffer-name))
  3316. org-agenda-buffer-name)))
  3317. (when open (org-open-file file)))
  3318. (defun org-agenda-remove-marked-text (property &optional value)
  3319. "Delete all text marked with VALUE of PROPERTY.
  3320. VALUE defaults to t."
  3321. (let (beg)
  3322. (setq value (or value t))
  3323. (while (setq beg (text-property-any (point-min) (point-max)
  3324. property value))
  3325. (delete-region
  3326. beg (or (next-single-property-change beg property)
  3327. (point-max))))))
  3328. (defun org-agenda-add-entry-text ()
  3329. "Add entry text to agenda lines.
  3330. This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
  3331. entry text following headings shown in the agenda.
  3332. Drawers will be excluded, also the line with scheduling/deadline info."
  3333. (when (and (> org-agenda-add-entry-text-maxlines 0)
  3334. (not (bound-and-true-p org-mobile-creating-agendas)))
  3335. (let (m txt)
  3336. (goto-char (point-min))
  3337. (while (not (eobp))
  3338. (if (not (setq m (org-get-at-bol 'org-hd-marker)))
  3339. (beginning-of-line 2)
  3340. (setq txt (org-agenda-get-some-entry-text
  3341. m org-agenda-add-entry-text-maxlines " > "))
  3342. (end-of-line 1)
  3343. (if (string-match "\\S-" txt)
  3344. (insert "\n" txt)
  3345. (or (eobp) (forward-char 1))))))))
  3346. (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
  3347. &rest keep)
  3348. "Extract entry text from MARKER, at most N-LINES lines.
  3349. This will ignore drawers etc, just get the text.
  3350. If INDENT is given, prefix every line with this string. If KEEP is
  3351. given, it is a list of symbols, defining stuff that should not be
  3352. removed from the entry content. Currently only `planning' is allowed here."
  3353. (let (txt drawer-re kwd-time-re ind)
  3354. (save-excursion
  3355. (with-current-buffer (marker-buffer marker)
  3356. (if (not (derived-mode-p 'org-mode))
  3357. (setq txt "")
  3358. (org-with-wide-buffer
  3359. (goto-char marker)
  3360. (end-of-line 1)
  3361. (setq txt (buffer-substring
  3362. (min (1+ (point)) (point-max))
  3363. (progn (outline-next-heading) (point)))
  3364. drawer-re org-drawer-regexp
  3365. kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
  3366. ".*\n?"))
  3367. (with-temp-buffer
  3368. (insert txt)
  3369. (when org-agenda-add-entry-text-descriptive-links
  3370. (goto-char (point-min))
  3371. (while (org-activate-links (point-max))
  3372. (goto-char (match-end 0))))
  3373. (goto-char (point-min))
  3374. (while (re-search-forward org-link-bracket-re (point-max) t)
  3375. (set-text-properties (match-beginning 0) (match-end 0)
  3376. nil))
  3377. (goto-char (point-min))
  3378. (while (re-search-forward drawer-re nil t)
  3379. (delete-region
  3380. (match-beginning 0)
  3381. (progn (re-search-forward
  3382. "^[ \t]*:END:.*\n?" nil 'move)
  3383. (point))))
  3384. (unless (member 'planning keep)
  3385. (goto-char (point-min))
  3386. (while (re-search-forward kwd-time-re nil t)
  3387. (replace-match "")))
  3388. (goto-char (point-min))
  3389. (when org-agenda-entry-text-exclude-regexps
  3390. (let ((re-list org-agenda-entry-text-exclude-regexps) re)
  3391. (while (setq re (pop re-list))
  3392. (goto-char (point-min))
  3393. (while (re-search-forward re nil t)
  3394. (replace-match "")))))
  3395. (goto-char (point-max))
  3396. (skip-chars-backward " \t\n")
  3397. (when (looking-at "[ \t\n]+\\'") (replace-match ""))
  3398. ;; find and remove min common indentation
  3399. (goto-char (point-min))
  3400. (untabify (point-min) (point-max))
  3401. (setq ind (org-current-text-indentation))
  3402. (while (not (eobp))
  3403. (unless (looking-at "[ \t]*$")
  3404. (setq ind (min ind (org-current-text-indentation))))
  3405. (beginning-of-line 2))
  3406. (goto-char (point-min))
  3407. (while (not (eobp))
  3408. (unless (looking-at "[ \t]*$")
  3409. (move-to-column ind)
  3410. (delete-region (line-beginning-position) (point)))
  3411. (beginning-of-line 2))
  3412. (run-hooks 'org-agenda-entry-text-cleanup-hook)
  3413. (goto-char (point-min))
  3414. (when indent
  3415. (while (and (not (eobp)) (re-search-forward "^" nil t))
  3416. (replace-match indent t t)))
  3417. (goto-char (point-min))
  3418. (while (looking-at "[ \t]*\n") (replace-match ""))
  3419. (goto-char (point-max))
  3420. (when (> (org-current-line)
  3421. n-lines)
  3422. (org-goto-line (1+ n-lines))
  3423. (backward-char 1))
  3424. (setq txt (buffer-substring (point-min) (point))))))))
  3425. txt))
  3426. (defun org-check-for-org-mode ()
  3427. "Make sure current buffer is in Org mode. Error if not."
  3428. (or (derived-mode-p 'org-mode)
  3429. (error "Cannot execute Org agenda command on buffer in %s"
  3430. major-mode)))
  3431. ;;; Agenda prepare and finalize
  3432. (defvar org-agenda-multi nil) ; dynamically scoped
  3433. (defvar org-agenda-pre-window-conf nil)
  3434. (defvar org-agenda-columns-active nil)
  3435. (defvar org-agenda-name nil)
  3436. (defvar org-agenda-tag-filter nil)
  3437. (defvar org-agenda-category-filter nil)
  3438. (defvar org-agenda-regexp-filter nil)
  3439. (defvar org-agenda-effort-filter nil)
  3440. (defvar org-agenda-top-headline-filter nil)
  3441. (defvar org-agenda-represented-categories nil
  3442. "Cache for the list of all categories in the agenda.")
  3443. (defvar org-agenda-represented-tags nil
  3444. "Cache for the list of all categories in the agenda.")
  3445. (defvar org-agenda-tag-filter-preset nil
  3446. "A preset of the tags filter used for secondary agenda filtering.
  3447. This must be a list of strings, each string must be a single tag preceded
  3448. by \"+\" or \"-\".
  3449. This variable should not be set directly, but agenda custom commands can
  3450. bind it in the options section. The preset filter is a global property of
  3451. the entire agenda view. In a block agenda, it will not work reliably to
  3452. define a filter for one of the individual blocks. You need to set it in
  3453. the global options and expect it to be applied to the entire view.")
  3454. (defvar org-agenda-filters-preset nil
  3455. "Alist of filter types and associated preset of filters.
  3456. This variable is local in `org-agenda' buffers. See `org-agenda-local-vars'.")
  3457. (defconst org-agenda-filter-variables
  3458. '((category . org-agenda-category-filter)
  3459. (tag . org-agenda-tag-filter)
  3460. (effort . org-agenda-effort-filter)
  3461. (regexp . org-agenda-regexp-filter))
  3462. "Alist of filter types and associated variables.")
  3463. (defun org-agenda-filter-any ()
  3464. "Is any filter active?"
  3465. (cl-some (lambda (x)
  3466. (or (symbol-value (cdr x))
  3467. (assoc-default (car x) org-agenda-filters-preset)))
  3468. org-agenda-filter-variables))
  3469. (defvar org-agenda-category-filter-preset nil
  3470. "A preset of the category filter used for secondary agenda filtering.
  3471. This must be a list of strings, each string must be a single category
  3472. preceded by \"+\" or \"-\".
  3473. This variable should not be set directly, but agenda custom commands can
  3474. bind it in the options section. The preset filter is a global property of
  3475. the entire agenda view. In a block agenda, it will not work reliably to
  3476. define a filter for one of the individual blocks. You need to set it in
  3477. the global options and expect it to be applied to the entire view.")
  3478. (defvar org-agenda-regexp-filter-preset nil
  3479. "A preset of the regexp filter used for secondary agenda filtering.
  3480. This must be a list of strings, each string must be a single regexp
  3481. preceded by \"+\" or \"-\".
  3482. This variable should not be set directly, but agenda custom commands can
  3483. bind it in the options section. The preset filter is a global property of
  3484. the entire agenda view. In a block agenda, it will not work reliably to
  3485. define a filter for one of the individual blocks. You need to set it in
  3486. the global options and expect it to be applied to the entire view.")
  3487. (defvar org-agenda-effort-filter-preset nil
  3488. "A preset of the effort condition used for secondary agenda filtering.
  3489. This must be a list of strings, each string must be a single regexp
  3490. preceded by \"+\" or \"-\".
  3491. This variable should not be set directly, but agenda custom commands can
  3492. bind it in the options section. The preset filter is a global property of
  3493. the entire agenda view. In a block agenda, it will not work reliably to
  3494. define a filter for one of the individual blocks. You need to set it in
  3495. the global options and expect it to be applied to the entire view.")
  3496. (defun org-agenda-use-sticky-p ()
  3497. "Return non-nil if an agenda buffer named
  3498. `org-agenda-buffer-name' exists and should be shown instead of
  3499. generating a new one."
  3500. (and
  3501. ;; turned off by user
  3502. org-agenda-sticky
  3503. ;; For multi-agenda buffer already exists
  3504. (not org-agenda-multi)
  3505. ;; buffer found
  3506. (get-buffer org-agenda-buffer-name)
  3507. ;; C-u parameter is same as last call
  3508. (with-current-buffer (get-buffer org-agenda-buffer-name)
  3509. (and
  3510. (equal current-prefix-arg
  3511. org-agenda-last-prefix-arg)
  3512. ;; In case user turned stickiness on, while having existing
  3513. ;; Agenda buffer active, don't reuse that buffer, because it
  3514. ;; does not have org variables local
  3515. org-agenda-this-buffer-is-sticky))))
  3516. (defvar org-agenda-buffer-tmp-name nil)
  3517. (defun org-agenda--get-buffer-name (sticky-name)
  3518. (or org-agenda-buffer-tmp-name
  3519. (and org-agenda-doing-sticky-redo org-agenda-buffer-name)
  3520. sticky-name
  3521. "*Org Agenda*"))
  3522. (defun org-agenda-prepare-window (abuf filter-alist)
  3523. "Setup agenda buffer in the window.
  3524. ABUF is the buffer for the agenda window.
  3525. FILTER-ALIST is an alist of filters we need to apply when
  3526. `org-agenda-persistent-filter' is non-nil."
  3527. (let* ((awin (get-buffer-window abuf)) wconf)
  3528. (cond
  3529. ((equal (current-buffer) abuf) nil)
  3530. (awin (select-window awin))
  3531. ((not (setq wconf (current-window-configuration))))
  3532. ((eq org-agenda-window-setup 'current-window)
  3533. (pop-to-buffer-same-window abuf))
  3534. ((eq org-agenda-window-setup 'other-window)
  3535. (org-switch-to-buffer-other-window abuf))
  3536. ((eq org-agenda-window-setup 'other-frame)
  3537. (switch-to-buffer-other-frame abuf))
  3538. ((eq org-agenda-window-setup 'other-tab)
  3539. (if (fboundp 'switch-to-buffer-other-tab)
  3540. (switch-to-buffer-other-tab abuf)
  3541. (user-error "Your version of Emacs does not have tab bar support")))
  3542. ((eq org-agenda-window-setup 'only-window)
  3543. (delete-other-windows)
  3544. (pop-to-buffer-same-window abuf))
  3545. ((eq org-agenda-window-setup 'reorganize-frame)
  3546. (delete-other-windows)
  3547. (org-switch-to-buffer-other-window abuf)))
  3548. (setq org-agenda-tag-filter (cdr (assq 'tag filter-alist)))
  3549. (setq org-agenda-category-filter (cdr (assq 'cat filter-alist)))
  3550. (setq org-agenda-effort-filter (cdr (assq 'effort filter-alist)))
  3551. (setq org-agenda-regexp-filter (cdr (assq 're filter-alist)))
  3552. ;; Additional test in case agenda is invoked from within agenda
  3553. ;; buffer via elisp link.
  3554. (unless (equal (current-buffer) abuf)
  3555. (pop-to-buffer-same-window abuf))
  3556. (setq org-agenda-pre-window-conf
  3557. (or wconf org-agenda-pre-window-conf))))
  3558. (defun org-agenda-prepare (&optional name)
  3559. (let ((filter-alist (when org-agenda-persistent-filter
  3560. (with-current-buffer
  3561. (get-buffer-create org-agenda-buffer-name)
  3562. `((tag . ,org-agenda-tag-filter)
  3563. (re . ,org-agenda-regexp-filter)
  3564. (effort . ,org-agenda-effort-filter)
  3565. (cat . ,org-agenda-category-filter))))))
  3566. (if (org-agenda-use-sticky-p)
  3567. (progn
  3568. ;; Popup existing buffer
  3569. (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
  3570. filter-alist)
  3571. (message "Sticky Agenda buffer, use `r' to refresh")
  3572. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  3573. (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
  3574. (setq org-todo-keywords-for-agenda nil)
  3575. (if org-agenda-multi
  3576. (progn
  3577. (setq buffer-read-only nil)
  3578. (goto-char (point-max))
  3579. (unless (or (bobp) org-agenda-compact-blocks
  3580. (not org-agenda-block-separator))
  3581. (insert "\n"
  3582. (if (stringp org-agenda-block-separator)
  3583. org-agenda-block-separator
  3584. (make-string (window-max-chars-per-line) org-agenda-block-separator))
  3585. "\n"))
  3586. (narrow-to-region (point) (point-max)))
  3587. (setq org-done-keywords-for-agenda nil)
  3588. ;; Setting any org variables that are in org-agenda-local-vars
  3589. ;; list need to be done after the prepare call
  3590. (org-agenda-prepare-window
  3591. (get-buffer-create org-agenda-buffer-name) filter-alist)
  3592. (setq buffer-read-only nil)
  3593. (org-agenda-reset-markers)
  3594. (let ((inhibit-read-only t)) (erase-buffer))
  3595. (org-agenda-mode)
  3596. (setq org-agenda-buffer (current-buffer))
  3597. (setq org-agenda-contributing-files nil)
  3598. (setq org-agenda-columns-active nil)
  3599. (setq org-agenda-filters-preset
  3600. `((tag . ,org-agenda-tag-filter-preset)
  3601. (category . ,org-agenda-category-filter-preset)
  3602. (regexp . ,org-agenda-regexp-filter-preset)
  3603. (effort . ,org-agenda-effort-filter-preset)))
  3604. (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
  3605. (setq org-todo-keywords-for-agenda
  3606. (org-uniquify org-todo-keywords-for-agenda))
  3607. (setq org-done-keywords-for-agenda
  3608. (org-uniquify org-done-keywords-for-agenda))
  3609. (setq org-agenda-last-prefix-arg current-prefix-arg)
  3610. (setq org-agenda-this-buffer-name org-agenda-buffer-name)
  3611. (and name (not org-agenda-name)
  3612. (setq-local org-agenda-name name)))
  3613. (setq buffer-read-only nil))))
  3614. (defvar org-overriding-columns-format)
  3615. (defvar org-local-columns-format)
  3616. (defun org-agenda-finalize ()
  3617. "Finishing touch for the agenda buffer.
  3618. This function is called just before displaying the agenda. If
  3619. you want to add your own functions to the finalization of the
  3620. agenda display, configure `org-agenda-finalize-hook'."
  3621. (unless org-agenda-multi
  3622. (let ((inhibit-read-only t))
  3623. (save-excursion
  3624. (goto-char (point-min))
  3625. (save-excursion
  3626. (while (org-activate-links (point-max))
  3627. (goto-char (match-end 0))))
  3628. (unless (eq org-agenda-remove-tags t)
  3629. (org-agenda-align-tags))
  3630. (unless org-agenda-with-colors
  3631. (remove-text-properties (point-min) (point-max) '(face nil)))
  3632. (when (bound-and-true-p org-overriding-columns-format)
  3633. (setq-local org-local-columns-format
  3634. org-overriding-columns-format))
  3635. (when org-agenda-view-columns-initially
  3636. (org-agenda-columns))
  3637. (when org-agenda-fontify-priorities
  3638. (org-agenda-fontify-priorities))
  3639. (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
  3640. (org-agenda-dim-blocked-tasks))
  3641. (org-agenda-mark-clocking-task)
  3642. (when org-agenda-entry-text-mode
  3643. (org-agenda-entry-text-hide)
  3644. (org-agenda-entry-text-show))
  3645. (when (and (featurep 'org-habit)
  3646. (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
  3647. (org-habit-insert-consistency-graphs))
  3648. (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
  3649. (unless (or (eq org-agenda-show-inherited-tags 'always)
  3650. (and (listp org-agenda-show-inherited-tags)
  3651. (memq org-agenda-type org-agenda-show-inherited-tags))
  3652. (and (eq org-agenda-show-inherited-tags t)
  3653. (or (eq org-agenda-use-tag-inheritance t)
  3654. (and (listp org-agenda-use-tag-inheritance)
  3655. (not (memq org-agenda-type
  3656. org-agenda-use-tag-inheritance))))))
  3657. (let (mrk)
  3658. (save-excursion
  3659. (goto-char (point-min))
  3660. (while (equal (forward-line) 0)
  3661. (when (setq mrk (get-text-property (point) 'org-hd-marker))
  3662. (put-text-property (line-beginning-position) (line-end-position)
  3663. 'tags
  3664. (org-with-point-at mrk
  3665. (org-get-tags))))))))
  3666. (setq org-agenda-represented-tags nil
  3667. org-agenda-represented-categories nil)
  3668. (when org-agenda-top-headline-filter
  3669. (org-agenda-filter-top-headline-apply
  3670. org-agenda-top-headline-filter))
  3671. (when org-agenda-tag-filter
  3672. (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
  3673. (when (assoc-default 'tag org-agenda-filters-preset)
  3674. (org-agenda-filter-apply
  3675. (assoc-default 'tag org-agenda-filters-preset) 'tag t))
  3676. (when org-agenda-category-filter
  3677. (org-agenda-filter-apply org-agenda-category-filter 'category))
  3678. (when (assoc-default 'category org-agenda-filters-preset)
  3679. (org-agenda-filter-apply
  3680. (assoc-default 'category org-agenda-filters-preset) 'category))
  3681. (when org-agenda-regexp-filter
  3682. (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
  3683. (when (assoc-default 'regexp org-agenda-filters-preset)
  3684. (org-agenda-filter-apply
  3685. (assoc-default 'regexp org-agenda-filters-preset) 'regexp))
  3686. (when org-agenda-effort-filter
  3687. (org-agenda-filter-apply org-agenda-effort-filter 'effort))
  3688. (when (assoc-default 'effort org-agenda-filters-preset)
  3689. (org-agenda-filter-apply
  3690. (assoc-default 'effort org-agenda-filters-preset) 'effort))
  3691. (add-hook 'kill-buffer-hook #'org-agenda-reset-markers 'append 'local))
  3692. (run-hooks 'org-agenda-finalize-hook))))
  3693. (defun org-agenda-mark-clocking-task ()
  3694. "Mark the current clock entry in the agenda if it is present."
  3695. ;; We need to widen when `org-agenda-finalize' is called from
  3696. ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in').
  3697. (when (bound-and-true-p org-clock-current-task)
  3698. (save-restriction
  3699. (widen)
  3700. (org-agenda-unmark-clocking-task)
  3701. (when (marker-buffer org-clock-hd-marker)
  3702. (save-excursion
  3703. (goto-char (point-min))
  3704. (let (s ov)
  3705. (while (setq s (next-single-property-change (point) 'org-hd-marker))
  3706. (goto-char s)
  3707. (when (equal (org-get-at-bol 'org-hd-marker)
  3708. org-clock-hd-marker)
  3709. (setq ov (make-overlay (line-beginning-position)
  3710. (1+ (line-end-position))))
  3711. (overlay-put ov 'type 'org-agenda-clocking)
  3712. (overlay-put ov 'face 'org-agenda-clocking)
  3713. (overlay-put ov 'help-echo
  3714. "The clock is running in this item")))))))))
  3715. (defun org-agenda-unmark-clocking-task ()
  3716. "Unmark the current clocking task."
  3717. (mapc (lambda (o)
  3718. (when (eq (overlay-get o 'type) 'org-agenda-clocking)
  3719. (delete-overlay o)))
  3720. (overlays-in (point-min) (point-max))))
  3721. (defun org-agenda-fontify-priorities ()
  3722. "Make highest priority lines bold, and lowest italic."
  3723. (interactive)
  3724. (mapc (lambda (o) (when (eq (overlay-get o 'org-type) 'org-priority)
  3725. (delete-overlay o)))
  3726. (overlays-in (point-min) (point-max)))
  3727. (save-excursion
  3728. (let (b e p ov h l)
  3729. (goto-char (point-min))
  3730. (while (re-search-forward org-priority-regexp nil t)
  3731. (setq h (or (get-char-property (point) 'org-priority-highest)
  3732. org-priority-highest)
  3733. l (or (get-char-property (point) 'org-priority-lowest)
  3734. org-priority-lowest)
  3735. p (string-to-char (match-string 2))
  3736. b (match-beginning 1)
  3737. e (if (eq org-agenda-fontify-priorities 'cookies)
  3738. (1+ (match-end 2))
  3739. (line-end-position))
  3740. ov (make-overlay b e))
  3741. (overlay-put
  3742. ov 'face
  3743. (let ((special-face
  3744. (cond ((org-face-from-face-or-color
  3745. 'priority 'org-priority
  3746. (cdr (assoc p org-priority-faces))))
  3747. ((and (listp org-agenda-fontify-priorities)
  3748. (org-face-from-face-or-color
  3749. 'priority 'org-priority
  3750. (cdr (assoc p org-agenda-fontify-priorities)))))
  3751. ((equal p l) 'italic)
  3752. ((equal p h) 'bold))))
  3753. (if special-face (list special-face 'org-priority) 'org-priority)))
  3754. (overlay-put ov 'org-type 'org-priority)))))
  3755. (defvar org-depend-tag-blocked)
  3756. (defun org-agenda-dim-blocked-tasks (&optional _invisible)
  3757. "Dim currently blocked TODOs in the agenda display.
  3758. When INVISIBLE is non-nil, hide currently blocked TODO instead of
  3759. dimming them." ;FIXME: The arg isn't used, actually!
  3760. (interactive "P")
  3761. (when (called-interactively-p 'interactive)
  3762. (message "Dim or hide blocked tasks..."))
  3763. (dolist (o (overlays-in (point-min) (point-max)))
  3764. (when (eq (overlay-get o 'face) 'org-agenda-dimmed-todo-face)
  3765. (delete-overlay o)))
  3766. (save-excursion
  3767. (let ((inhibit-read-only t))
  3768. (goto-char (point-min))
  3769. (while (let ((pos (text-property-not-all
  3770. (point) (point-max) 'org-todo-blocked nil)))
  3771. (when pos (goto-char pos)))
  3772. (let* ((invisible
  3773. (eq (org-get-at-bol 'org-todo-blocked) 'invisible))
  3774. (todo-blocked
  3775. (eq (org-get-at-bol 'org-filter-type) 'todo-blocked))
  3776. (ov (make-overlay (if invisible
  3777. (line-end-position 0)
  3778. (line-beginning-position))
  3779. (line-end-position))))
  3780. (when todo-blocked
  3781. (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
  3782. (when invisible
  3783. (org-agenda-filter-hide-line 'todo-blocked)))
  3784. (if (= (point-max) (line-end-position))
  3785. (goto-char (point-max))
  3786. (move-beginning-of-line 2)))))
  3787. (when (called-interactively-p 'interactive)
  3788. (message "Dim or hide blocked tasks...done")))
  3789. (defun org-agenda--mark-blocked-entry (entry)
  3790. "If ENTRY is blocked, mark it for fontification or invisibility.
  3791. If the header at `org-hd-marker' is blocked according to
  3792. `org-entry-blocked-p', then if `org-agenda-dim-blocked-tasks' is
  3793. `invisible' and the header is not blocked by checkboxes, set the
  3794. text property `org-todo-blocked' to `invisible', otherwise set it
  3795. to t."
  3796. (when (get-text-property 0 'todo-state entry)
  3797. (let ((entry-marker (get-text-property 0 'org-hd-marker entry))
  3798. (org-blocked-by-checkboxes nil)
  3799. ;; Necessary so that `org-entry-blocked-p' does not change
  3800. ;; the buffer.
  3801. (org-depend-tag-blocked nil))
  3802. (when entry-marker
  3803. (let ((blocked
  3804. (with-current-buffer (marker-buffer entry-marker)
  3805. (save-excursion
  3806. (goto-char entry-marker)
  3807. (org-entry-blocked-p)))))
  3808. (when blocked
  3809. (let ((really-invisible
  3810. (and (not org-blocked-by-checkboxes)
  3811. (eq org-agenda-dim-blocked-tasks 'invisible))))
  3812. (put-text-property
  3813. 0 (length entry) 'org-todo-blocked
  3814. (if really-invisible 'invisible t)
  3815. entry)
  3816. (put-text-property
  3817. 0 (length entry) 'org-filter-type 'todo-blocked entry)))))))
  3818. entry)
  3819. (defvar org-agenda-skip-function nil
  3820. "Function to be called at each match during agenda construction.
  3821. If this function returns nil, the current match should not be skipped.
  3822. Otherwise, the function must return a position from where the search
  3823. should be continued.
  3824. This may also be a Lisp form that will be evaluated. Useful
  3825. forms include `org-agenda-skip-entry-if' and
  3826. `org-agenda-skip-subtree-if'. See the Info node `(org) Special
  3827. Agenda Views' for more details and examples.
  3828. Never set this variable using `setq' or similar, because then it
  3829. will apply to all future agenda commands. If you want a global
  3830. skipping condition, use the option `org-agenda-skip-function-global'
  3831. instead.
  3832. The correct way to use `org-agenda-skip-function' is to bind it with `let'
  3833. to scope it dynamically into the agenda-constructing command.
  3834. A good way to set it is through options in `org-agenda-custom-commands'.")
  3835. (defun org-agenda-skip (&optional element)
  3836. "Throw to `:skip' in places that should be skipped.
  3837. Also moves point to the end of the skipped region, so that search can
  3838. continue from there.
  3839. Optional argument ELEMENT contains element at point."
  3840. (when (or
  3841. (if element
  3842. (eq (org-element-type element) 'comment)
  3843. (save-excursion
  3844. (goto-char (line-beginning-position))
  3845. (looking-at comment-start-skip)))
  3846. (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
  3847. (or (and (save-match-data (org-in-archived-heading-p nil element))
  3848. (org-end-of-subtree t element))
  3849. (and (member org-archive-tag org-file-tags)
  3850. (goto-char (point-max)))))
  3851. (and org-agenda-skip-comment-trees
  3852. (org-in-commented-heading-p nil element)
  3853. (org-end-of-subtree t element))
  3854. (let ((to (or (org-agenda-skip-eval org-agenda-skip-function-global)
  3855. (org-agenda-skip-eval org-agenda-skip-function))))
  3856. (and to (goto-char to)))
  3857. (org-in-src-block-p t element))
  3858. (throw :skip t)))
  3859. (defun org-agenda-skip-eval (form)
  3860. "If FORM is a function or a list, call (or eval) it and return the result.
  3861. `save-excursion' and `save-match-data' are wrapped around the call, so point
  3862. and match data are returned to the previous state no matter what these
  3863. functions do."
  3864. (let (fp)
  3865. (and form
  3866. (or (setq fp (functionp form))
  3867. (consp form))
  3868. (save-excursion
  3869. (save-match-data
  3870. (if fp
  3871. (funcall form)
  3872. (eval form t)))))))
  3873. (defvar org-agenda-markers nil
  3874. "List of all currently active markers created by `org-agenda'.")
  3875. (defvar org-agenda-last-marker-time (float-time)
  3876. "Creation time of the last agenda marker.")
  3877. (defun org-agenda-new-marker (&optional pos)
  3878. "Return a new agenda marker.
  3879. Marker is at point, or at POS if non-nil. Org mode keeps a list
  3880. of these markers and resets them when they are no longer in use."
  3881. (let ((m (copy-marker (or pos (point)) t)))
  3882. (setq org-agenda-last-marker-time (float-time))
  3883. (if org-agenda-buffer
  3884. (with-current-buffer org-agenda-buffer
  3885. (push m org-agenda-markers))
  3886. (push m org-agenda-markers))
  3887. m))
  3888. (defun org-agenda-reset-markers ()
  3889. "Reset markers created by `org-agenda'."
  3890. (while org-agenda-markers
  3891. (move-marker (pop org-agenda-markers) nil)))
  3892. (defun org-agenda-save-markers-for-cut-and-paste (beg end)
  3893. "Save relative positions of markers in region.
  3894. This check for agenda markers in all agenda buffers currently active."
  3895. (dolist (buf (buffer-list))
  3896. (with-current-buffer buf
  3897. (when (eq major-mode 'org-agenda-mode)
  3898. (mapc (lambda (m) (org-check-and-save-marker m beg end))
  3899. org-agenda-markers)))))
  3900. ;;; Entry text mode
  3901. (defun org-agenda-entry-text-show-here ()
  3902. "Add some text from the entry as context to the current line."
  3903. (let (m txt o)
  3904. (setq m (org-get-at-bol 'org-hd-marker))
  3905. (unless (marker-buffer m)
  3906. (error "No marker points to an entry here"))
  3907. (setq txt (concat "\n" (org-no-properties
  3908. (org-agenda-get-some-entry-text
  3909. m org-agenda-entry-text-maxlines
  3910. org-agenda-entry-text-leaders))))
  3911. (when (string-match "\\S-" txt)
  3912. (setq o (make-overlay (line-beginning-position) (line-end-position)))
  3913. (overlay-put o 'evaporate t)
  3914. (overlay-put o 'org-overlay-type 'agenda-entry-content)
  3915. (overlay-put o 'after-string txt))))
  3916. (defun org-agenda-entry-text-show ()
  3917. "Add entry context for all agenda lines."
  3918. (interactive)
  3919. (save-excursion
  3920. (goto-char (point-max))
  3921. (beginning-of-line 1)
  3922. (while (not (bobp))
  3923. (when (org-get-at-bol 'org-hd-marker)
  3924. (org-agenda-entry-text-show-here))
  3925. (beginning-of-line 0))))
  3926. (defun org-agenda-entry-text-hide ()
  3927. "Remove any shown entry context."
  3928. (mapc (lambda (o)
  3929. (when (eq (overlay-get o 'org-overlay-type)
  3930. 'agenda-entry-content)
  3931. (delete-overlay o)))
  3932. (overlays-in (point-min) (point-max))))
  3933. (defun org-agenda-get-day-face (date)
  3934. "Return the face DATE should be displayed with."
  3935. (cond ((and (functionp org-agenda-day-face-function)
  3936. (funcall org-agenda-day-face-function date)))
  3937. ((and (org-agenda-today-p date)
  3938. (memq (calendar-day-of-week date) org-agenda-weekend-days))
  3939. 'org-agenda-date-weekend-today)
  3940. ((org-agenda-today-p date) 'org-agenda-date-today)
  3941. ((memq (calendar-day-of-week date) org-agenda-weekend-days)
  3942. 'org-agenda-date-weekend)
  3943. (t 'org-agenda-date)))
  3944. (defvar org-agenda-show-log-scoped)
  3945. ;;; Agenda Daily/Weekly
  3946. (defvar org-agenda-start-day nil ; dynamically scoped parameter
  3947. "Start day for the agenda view.
  3948. Custom commands can set this variable in the options section.
  3949. This is usually a string like \"2007-11-01\", \"+2d\" or any other
  3950. input allowed when reading a date through the Org calendar.
  3951. See the docstring of `org-read-date' for details.")
  3952. (defvar org-starting-day nil) ; local variable in the agenda buffer
  3953. (defvar org-arg-loc nil) ; local variable
  3954. ;;;###autoload
  3955. (defun org-agenda-list (&optional arg start-day span with-hour)
  3956. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  3957. The view will be for the current day or week, but from the overview buffer
  3958. you will be able to go to other days/weeks.
  3959. With a numeric prefix argument in an interactive call, the agenda will
  3960. span ARG days. Lisp programs should instead specify SPAN to change
  3961. the number of days. SPAN defaults to `org-agenda-span'.
  3962. START-DAY defaults to TODAY, or to the most recent match for the weekday
  3963. given in `org-agenda-start-on-weekday'.
  3964. When WITH-HOUR is non-nil, only include scheduled and deadline
  3965. items if they have an hour specification like [h]h:mm."
  3966. (interactive "P")
  3967. (when org-agenda-overriding-arguments
  3968. (setq arg (car org-agenda-overriding-arguments)
  3969. start-day (nth 1 org-agenda-overriding-arguments)
  3970. span (nth 2 org-agenda-overriding-arguments)))
  3971. (when (and (integerp arg) (> arg 0))
  3972. (setq span arg arg nil))
  3973. (when (numberp span)
  3974. (unless (< 0 span)
  3975. (user-error "Agenda creation impossible for this span(=%d days)" span)))
  3976. (catch 'exit
  3977. (setq org-agenda-buffer-name
  3978. (org-agenda--get-buffer-name
  3979. (and org-agenda-sticky
  3980. (cond ((and org-keys (stringp org-match))
  3981. (format "*Org Agenda(%s:%s)*" org-keys org-match))
  3982. (org-keys
  3983. (format "*Org Agenda(%s)*" org-keys))
  3984. (t "*Org Agenda(a)*")))))
  3985. (org-agenda-prepare "Day/Week")
  3986. (setq start-day (or start-day org-agenda-start-day))
  3987. (when (stringp start-day)
  3988. ;; Convert to an absolute day number
  3989. (setq start-day (time-to-days (org-read-date nil t start-day))))
  3990. (org-compile-prefix-format 'agenda)
  3991. (org-set-sorting-strategy 'agenda)
  3992. (let* ((span (org-agenda-ndays-to-span (or span org-agenda-span)))
  3993. (today (org-today))
  3994. (sd (or start-day today))
  3995. (ndays (org-agenda-span-to-ndays span sd))
  3996. (org-agenda-start-on-weekday
  3997. (and (or (eq ndays 7) (eq ndays 14))
  3998. org-agenda-start-on-weekday))
  3999. (thefiles (org-agenda-files nil 'ifmode))
  4000. (files thefiles)
  4001. (start (if (or (null org-agenda-start-on-weekday)
  4002. (< ndays 7))
  4003. sd
  4004. (let* ((nt (calendar-day-of-week
  4005. (calendar-gregorian-from-absolute sd)))
  4006. (n1 org-agenda-start-on-weekday)
  4007. (d (- nt n1)))
  4008. (- sd (+ (if (< d 0) 7 0) d)))))
  4009. (day-numbers (list start))
  4010. (day-cnt 0)
  4011. (inhibit-redisplay (not debug-on-error))
  4012. (org-agenda-show-log-scoped org-agenda-show-log)
  4013. s rtn rtnall file date d start-pos end-pos todayp ;; e
  4014. clocktable-start clocktable-end) ;; filter
  4015. (setq org-agenda-redo-command
  4016. (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
  4017. (dotimes (_ (1- ndays))
  4018. (push (1+ (car day-numbers)) day-numbers))
  4019. (setq day-numbers (nreverse day-numbers))
  4020. (setq clocktable-start (car day-numbers)
  4021. clocktable-end (1+ (or (org-last day-numbers) 0)))
  4022. (setq-local org-starting-day (car day-numbers))
  4023. (setq-local org-arg-loc arg)
  4024. (setq-local org-agenda-current-span (org-agenda-ndays-to-span span))
  4025. (unless org-agenda-compact-blocks
  4026. (let* ((d1 (car day-numbers))
  4027. (d2 (org-last day-numbers))
  4028. (w1 (org-days-to-iso-week d1))
  4029. (w2 (org-days-to-iso-week d2)))
  4030. (setq s (point))
  4031. (org-agenda--insert-overriding-header
  4032. (concat (org-agenda-span-name span)
  4033. "-agenda"
  4034. (cond ((<= 350 (- d2 d1)) "")
  4035. ((= w1 w2) (format " (W%02d)" w1))
  4036. (t (format " (W%02d-W%02d)" w1 w2)))
  4037. ":\n")))
  4038. ;; Add properties if we actually inserted a header.
  4039. (when (> (point) s)
  4040. (add-text-properties s (1- (point))
  4041. (list 'face 'org-agenda-structure
  4042. 'org-date-line t))
  4043. (org-agenda-mark-header-line s)))
  4044. (while (setq d (pop day-numbers))
  4045. (setq date (calendar-gregorian-from-absolute d)
  4046. s (point))
  4047. (if (or (setq todayp (= d today))
  4048. (and (not start-pos) (= d sd)))
  4049. (setq start-pos (point))
  4050. (when (and start-pos (not end-pos))
  4051. (setq end-pos (point))))
  4052. (setq files thefiles
  4053. rtnall nil)
  4054. (while (setq file (pop files))
  4055. (catch 'nextfile
  4056. (org-check-agenda-file file)
  4057. (let ((org-agenda-entry-types org-agenda-entry-types))
  4058. ;; Starred types override non-starred equivalents
  4059. (when (member :deadline* org-agenda-entry-types)
  4060. (setq org-agenda-entry-types
  4061. (delq :deadline org-agenda-entry-types)))
  4062. (when (member :scheduled* org-agenda-entry-types)
  4063. (setq org-agenda-entry-types
  4064. (delq :scheduled org-agenda-entry-types)))
  4065. ;; Honor with-hour
  4066. (when with-hour
  4067. (when (member :deadline org-agenda-entry-types)
  4068. (setq org-agenda-entry-types
  4069. (delq :deadline org-agenda-entry-types))
  4070. (push :deadline* org-agenda-entry-types))
  4071. (when (member :scheduled org-agenda-entry-types)
  4072. (setq org-agenda-entry-types
  4073. (delq :scheduled org-agenda-entry-types))
  4074. (push :scheduled* org-agenda-entry-types)))
  4075. (unless org-agenda-include-deadlines
  4076. (setq org-agenda-entry-types
  4077. (delq :deadline* (delq :deadline org-agenda-entry-types))))
  4078. (cond
  4079. ((memq org-agenda-show-log-scoped '(only clockcheck))
  4080. (setq rtn (org-agenda-get-day-entries
  4081. file date :closed)))
  4082. (org-agenda-show-log-scoped
  4083. (setq rtn (apply #'org-agenda-get-day-entries
  4084. file date
  4085. (append '(:closed) org-agenda-entry-types))))
  4086. (t
  4087. (setq rtn (apply #'org-agenda-get-day-entries
  4088. file date
  4089. org-agenda-entry-types)))))
  4090. (setq rtnall (append rtnall rtn)))) ;; all entries
  4091. (when org-agenda-include-diary
  4092. (let ((org-agenda-search-headline-for-time t))
  4093. (require 'diary-lib)
  4094. (setq rtn (org-get-entries-from-diary date))
  4095. (setq rtnall (append rtnall rtn))))
  4096. (when (or rtnall org-agenda-show-all-dates)
  4097. (setq day-cnt (1+ day-cnt))
  4098. (insert
  4099. (if (stringp org-agenda-format-date)
  4100. (format-time-string org-agenda-format-date
  4101. (org-time-from-absolute date))
  4102. (funcall org-agenda-format-date date))
  4103. "\n")
  4104. (put-text-property s (1- (point)) 'face
  4105. (org-agenda-get-day-face date))
  4106. (put-text-property s (1- (point)) 'org-date-line t)
  4107. (put-text-property s (1- (point)) 'org-agenda-date-header t)
  4108. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  4109. (when todayp
  4110. (put-text-property s (1- (point)) 'org-today t))
  4111. (setq rtnall
  4112. (org-agenda-add-time-grid-maybe rtnall ndays todayp))
  4113. (when rtnall (insert ;; all entries
  4114. (org-agenda-finalize-entries rtnall 'agenda)
  4115. "\n"))
  4116. (put-text-property s (1- (point)) 'day d)
  4117. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)))
  4118. (when (and org-agenda-clockreport-mode clocktable-start)
  4119. (let ((org-agenda-files (org-agenda-files nil 'ifmode))
  4120. ;; the above line is to ensure the restricted range!
  4121. (p (copy-sequence org-agenda-clockreport-parameter-plist))
  4122. tbl)
  4123. (setq p (org-plist-delete p :block))
  4124. (setq p (plist-put p :tstart clocktable-start))
  4125. (setq p (plist-put p :tend clocktable-end))
  4126. (setq p (plist-put p :scope 'agenda))
  4127. (setq tbl (apply #'org-clock-get-clocktable p))
  4128. (when org-agenda-clock-report-header
  4129. (insert (propertize org-agenda-clock-report-header 'face 'org-agenda-structure))
  4130. (unless (string-suffix-p "\n" org-agenda-clock-report-header)
  4131. (insert "\n")))
  4132. (insert tbl)))
  4133. (goto-char (point-min))
  4134. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  4135. (unless (or (not (get-buffer-window org-agenda-buffer-name))
  4136. (and (pos-visible-in-window-p (point-min))
  4137. (pos-visible-in-window-p (point-max))))
  4138. (goto-char (1- (point-max)))
  4139. (recenter -1)
  4140. (when (not (pos-visible-in-window-p (or start-pos 1)))
  4141. (goto-char (or start-pos 1))
  4142. (recenter 1)))
  4143. (goto-char (or start-pos 1))
  4144. (add-text-properties (point-min) (point-max)
  4145. `(org-agenda-type agenda
  4146. org-last-args (,arg ,start-day ,span)
  4147. org-redo-cmd ,org-agenda-redo-command
  4148. org-series-cmd ,org-cmd))
  4149. (when (eq org-agenda-show-log-scoped 'clockcheck)
  4150. (org-agenda-show-clocking-issues))
  4151. (org-agenda-finalize)
  4152. (setq buffer-read-only t)
  4153. (message ""))))
  4154. (defun org-agenda-ndays-to-span (n)
  4155. "Return a span symbol for a span of N days, or N if none matches."
  4156. (cond ((symbolp n) n)
  4157. ((= n 1) 'day)
  4158. ((= n 7) 'week)
  4159. ((= n 14) 'fortnight)
  4160. (t n)))
  4161. (defun org-agenda-span-to-ndays (span &optional start-day)
  4162. "Return ndays from SPAN, possibly starting at START-DAY.
  4163. START-DAY is an absolute time value."
  4164. (cond ((numberp span) span)
  4165. ((eq span 'day) 1)
  4166. ((eq span 'week) 7)
  4167. ((eq span 'fortnight) 14)
  4168. ((eq span 'month)
  4169. (let ((date (calendar-gregorian-from-absolute start-day)))
  4170. (calendar-last-day-of-month (car date) (cl-caddr date))))
  4171. ((eq span 'year)
  4172. (let ((date (calendar-gregorian-from-absolute start-day)))
  4173. (if (calendar-leap-year-p (cl-caddr date)) 366 365)))))
  4174. (defun org-agenda-span-name (span)
  4175. "Return a SPAN name."
  4176. (if (null span)
  4177. ""
  4178. (if (symbolp span)
  4179. (capitalize (symbol-name span))
  4180. (format "%d days" span))))
  4181. ;;; Agenda word search
  4182. (defvar org-agenda-search-history nil)
  4183. (defvar org-search-syntax-table nil
  4184. "Special syntax table for Org search.
  4185. In this table, we have single quotes not as word constituents, to
  4186. that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
  4187. (defvar org-mode-syntax-table) ; From org.el
  4188. (defun org-search-syntax-table ()
  4189. (unless org-search-syntax-table
  4190. (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
  4191. (modify-syntax-entry ?' "." org-search-syntax-table)
  4192. (modify-syntax-entry ?` "." org-search-syntax-table))
  4193. org-search-syntax-table)
  4194. (defvar org-agenda-last-search-view-search-was-boolean nil)
  4195. ;;;###autoload
  4196. (defun org-search-view (&optional todo-only string edit-at)
  4197. "Show all entries that contain a phrase or words or regular expressions.
  4198. With optional prefix argument TODO-ONLY, only consider entries that are
  4199. TODO entries. The argument STRING can be used to pass a default search
  4200. string into this function. If EDIT-AT is non-nil, it means that the
  4201. user should get a chance to edit this string, with cursor at position
  4202. EDIT-AT.
  4203. The search string can be viewed either as a phrase that should be found as
  4204. is, or it can be broken into a number of snippets, each of which must match
  4205. in a Boolean way to select an entry. The default depends on the variable
  4206. `org-agenda-search-view-always-boolean'.
  4207. Even if this is turned off (the default) you can always switch to
  4208. Boolean search dynamically by preceding the first word with \"+\" or \"-\".
  4209. The default is a direct search of the whole phrase, where each space in
  4210. the search string can expand to an arbitrary amount of whitespace,
  4211. including newlines.
  4212. If using a Boolean search, the search string is split on whitespace and
  4213. each snippet is searched separately, with logical AND to select an entry.
  4214. Words prefixed with a minus must *not* occur in the entry. Words without
  4215. a prefix or prefixed with a plus must occur in the entry. Matching is
  4216. case-insensitive. Words are enclosed by word delimiters (i.e. they must
  4217. match whole words, not parts of a word) if
  4218. `org-agenda-search-view-force-full-words' is set (default is nil).
  4219. Boolean search snippets enclosed by curly braces are interpreted as
  4220. regular expressions that must or (when preceded with \"-\") must not
  4221. match in the entry. Snippets enclosed into double quotes will be taken
  4222. as a whole, to include whitespace.
  4223. - If the search string starts with an asterisk, search only in headlines.
  4224. - If (possibly after the leading star) the search string starts with an
  4225. exclamation mark, this also means to look at TODO entries only, an effect
  4226. that can also be achieved with a prefix argument.
  4227. - If (possibly after star and exclamation mark) the search string starts
  4228. with a colon, this will mean that the (non-regexp) snippets of the
  4229. Boolean search must match as full words.
  4230. This command searches the agenda files, and in addition the files
  4231. listed in `org-agenda-text-search-extra-files' unless a restriction lock
  4232. is active."
  4233. (interactive "P")
  4234. (when org-agenda-overriding-arguments
  4235. (setq todo-only (car org-agenda-overriding-arguments)
  4236. string (nth 1 org-agenda-overriding-arguments)
  4237. edit-at (nth 2 org-agenda-overriding-arguments)))
  4238. (let* ((props (list 'face nil
  4239. 'done-face 'org-agenda-done
  4240. 'org-not-done-regexp org-not-done-regexp
  4241. 'org-todo-regexp org-todo-regexp
  4242. 'org-complex-heading-regexp org-complex-heading-regexp
  4243. 'mouse-face 'highlight
  4244. 'help-echo "mouse-2 or RET jump to location"))
  4245. (full-words org-agenda-search-view-force-full-words)
  4246. (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
  4247. regexp rtn rtnall files file pos inherited-tags
  4248. marker category level tags c neg re boolean
  4249. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  4250. (unless (and (not edit-at)
  4251. (stringp string)
  4252. (string-match "\\S-" string))
  4253. (setq string (read-string
  4254. (if org-agenda-search-view-always-boolean
  4255. "[+-]Word/{Regexp} ...: "
  4256. "Phrase or [+-]Word/{Regexp} ...: ")
  4257. (cond
  4258. ((integerp edit-at) (cons string edit-at))
  4259. (edit-at string))
  4260. 'org-agenda-search-history)))
  4261. (catch 'exit
  4262. (setq org-agenda-buffer-name
  4263. (org-agenda--get-buffer-name
  4264. (and org-agenda-sticky
  4265. (if (stringp string)
  4266. (format "*Org Agenda(%s:%s)*"
  4267. (or org-keys (or (and todo-only "S") "s"))
  4268. string)
  4269. (format "*Org Agenda(%s)*"
  4270. (or (and todo-only "S") "s"))))))
  4271. (org-agenda-prepare "SEARCH")
  4272. (org-compile-prefix-format 'search)
  4273. (org-set-sorting-strategy 'search)
  4274. (setq org-agenda-redo-command
  4275. (list 'org-search-view (if todo-only t nil)
  4276. (list 'if 'current-prefix-arg nil string)))
  4277. (setq org-agenda-query-string string)
  4278. (if (equal (string-to-char string) ?*)
  4279. (setq hdl-only t
  4280. words (substring string 1))
  4281. (setq words string))
  4282. (when (equal (string-to-char words) ?!)
  4283. (setq todo-only t
  4284. words (substring words 1)))
  4285. (when (equal (string-to-char words) ?:)
  4286. (setq full-words t
  4287. words (substring words 1)))
  4288. (when (or org-agenda-search-view-always-boolean
  4289. (member (string-to-char words) '(?- ?+ ?\{)))
  4290. (setq boolean t))
  4291. (setq words (split-string words))
  4292. (let (www w)
  4293. (while (setq w (pop words))
  4294. (while (and (string-match "\\\\\\'" w) words)
  4295. (setq w (concat (substring w 0 -1) " " (pop words))))
  4296. (push w www))
  4297. (setq words (nreverse www) www nil)
  4298. (while (setq w (pop words))
  4299. (when (and (string-match "\\`[-+]?{" w)
  4300. (not (string-match "}\\'" w)))
  4301. (while (and words (not (string-match "}\\'" (car words))))
  4302. (setq w (concat w " " (pop words))))
  4303. (setq w (concat w " " (pop words))))
  4304. (push w www))
  4305. (setq words (nreverse www)))
  4306. (setq org-agenda-last-search-view-search-was-boolean boolean)
  4307. (when boolean
  4308. (let (wds w)
  4309. (while (setq w (pop words))
  4310. (when (or (equal (substring w 0 1) "\"")
  4311. (and (> (length w) 1)
  4312. (member (substring w 0 1) '("+" "-"))
  4313. (equal (substring w 1 2) "\"")))
  4314. (while (and words (not (equal (substring w -1) "\"")))
  4315. (setq w (concat w " " (pop words)))))
  4316. (and (string-match "\\`\\([-+]?\\)\"" w)
  4317. (setq w (replace-match "\\1" nil nil w)))
  4318. (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
  4319. (push w wds))
  4320. (setq words (nreverse wds))))
  4321. (if boolean
  4322. (mapc (lambda (w)
  4323. (setq c (string-to-char w))
  4324. (if (equal c ?-)
  4325. (setq neg t w (substring w 1))
  4326. (if (equal c ?+)
  4327. (setq neg nil w (substring w 1))
  4328. (setq neg nil)))
  4329. (if (string-match "\\`{.*}\\'" w)
  4330. (setq re (substring w 1 -1))
  4331. (if full-words
  4332. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
  4333. (setq re (regexp-quote (downcase w)))))
  4334. (if neg (push re regexps-) (push re regexps+)))
  4335. words)
  4336. (push (mapconcat #'regexp-quote words "\\s-+")
  4337. regexps+))
  4338. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  4339. (if (not regexps+)
  4340. (setq regexp org-outline-regexp-bol)
  4341. (setq regexp (pop regexps+))
  4342. (when hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
  4343. regexp))))
  4344. (setq files (org-agenda-files nil 'ifmode))
  4345. ;; Add `org-agenda-text-search-extra-files' unless there is some
  4346. ;; restriction.
  4347. (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
  4348. (pop org-agenda-text-search-extra-files)
  4349. (unless (get 'org-agenda-files 'org-restrict)
  4350. (setq files (org-add-archive-files files))))
  4351. ;; Uniquify files. However, let `org-check-agenda-file' handle
  4352. ;; non-existent ones.
  4353. (setq files (cl-remove-duplicates
  4354. (append files org-agenda-text-search-extra-files)
  4355. :test (lambda (a b)
  4356. (and (file-exists-p a)
  4357. (file-exists-p b)
  4358. (file-equal-p a b))))
  4359. rtnall nil)
  4360. (while (setq file (pop files))
  4361. (setq ee nil)
  4362. (catch 'nextfile
  4363. (org-check-agenda-file file)
  4364. (setq buffer (if (file-exists-p file)
  4365. (org-get-agenda-file-buffer file)
  4366. (error "No such file %s" file)))
  4367. (unless buffer
  4368. ;; If file does not exist, make sure an error message is sent
  4369. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  4370. file))))
  4371. (with-current-buffer buffer
  4372. (with-syntax-table (org-search-syntax-table)
  4373. (unless (derived-mode-p 'org-mode)
  4374. (error "Agenda file %s is not in Org mode" file))
  4375. (let ((case-fold-search t))
  4376. (save-excursion
  4377. (save-restriction
  4378. (if (eq buffer org-agenda-restrict)
  4379. (narrow-to-region org-agenda-restrict-begin
  4380. org-agenda-restrict-end)
  4381. (widen))
  4382. (goto-char (point-min))
  4383. (unless (or (org-at-heading-p)
  4384. (outline-next-heading))
  4385. (throw 'nextfile t))
  4386. (goto-char (max (point-min) (1- (point))))
  4387. (while (re-search-forward regexp nil t)
  4388. (org-back-to-heading t)
  4389. (while (and (not (zerop org-agenda-search-view-max-outline-level))
  4390. (> (org-reduced-level (org-outline-level))
  4391. org-agenda-search-view-max-outline-level)
  4392. (forward-line -1)
  4393. (org-back-to-heading t)))
  4394. (skip-chars-forward "* ")
  4395. (setq beg (line-beginning-position)
  4396. beg1 (point)
  4397. end (progn
  4398. (outline-next-heading)
  4399. (while (and (not (zerop org-agenda-search-view-max-outline-level))
  4400. (> (org-reduced-level (org-outline-level))
  4401. org-agenda-search-view-max-outline-level)
  4402. (forward-line 1)
  4403. (outline-next-heading)))
  4404. (point)))
  4405. (catch :skip
  4406. (goto-char beg)
  4407. (org-agenda-skip)
  4408. (setq str (buffer-substring-no-properties
  4409. (line-beginning-position)
  4410. (if hdl-only (line-end-position) end)))
  4411. (mapc (lambda (wr) (when (string-match wr str)
  4412. (goto-char (1- end))
  4413. (throw :skip t)))
  4414. regexps-)
  4415. (mapc (lambda (wr) (unless (string-match wr str)
  4416. (goto-char (1- end))
  4417. (throw :skip t)))
  4418. (if todo-only
  4419. (cons (concat "^\\*+[ \t]+"
  4420. org-not-done-regexp)
  4421. regexps+)
  4422. regexps+))
  4423. (goto-char beg)
  4424. (setq marker (org-agenda-new-marker (point))
  4425. category (org-get-category)
  4426. level (make-string (org-reduced-level (org-outline-level)) ? )
  4427. inherited-tags
  4428. (or (eq org-agenda-show-inherited-tags 'always)
  4429. (and (listp org-agenda-show-inherited-tags)
  4430. (memq 'todo org-agenda-show-inherited-tags))
  4431. (and (eq org-agenda-show-inherited-tags t)
  4432. (or (eq org-agenda-use-tag-inheritance t)
  4433. (memq 'todo org-agenda-use-tag-inheritance))))
  4434. tags (org-get-tags nil (not inherited-tags))
  4435. txt (org-agenda-format-item
  4436. ""
  4437. (buffer-substring-no-properties
  4438. beg1 (line-end-position))
  4439. level category tags t))
  4440. (org-add-props txt props
  4441. 'org-marker marker 'org-hd-marker marker
  4442. 'org-todo-regexp org-todo-regexp
  4443. 'level level
  4444. 'org-complex-heading-regexp org-complex-heading-regexp
  4445. 'priority 1000
  4446. 'type "search")
  4447. (push txt ee)
  4448. (goto-char (1- end))))))))))
  4449. (setq rtn (nreverse ee))
  4450. (setq rtnall (append rtnall rtn)))
  4451. (org-agenda--insert-overriding-header
  4452. (with-temp-buffer
  4453. (insert "Search words: ")
  4454. (add-text-properties (point-min) (1- (point))
  4455. (list 'face 'org-agenda-structure))
  4456. (setq pos (point))
  4457. (insert string "\n")
  4458. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure-filter))
  4459. (setq pos (point))
  4460. (unless org-agenda-multi
  4461. (insert (substitute-command-keys "\\<org-agenda-mode-map>\
  4462. Press `\\[org-agenda-manipulate-query-add]', \
  4463. `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
  4464. `\\[org-agenda-manipulate-query-add-re]', \
  4465. `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
  4466. `\\[universal-argument] \\[org-agenda-redo]' for a fresh search\n"))
  4467. (add-text-properties pos (1- (point))
  4468. (list 'face 'org-agenda-structure-secondary)))
  4469. (buffer-string)))
  4470. (org-agenda-mark-header-line (point-min))
  4471. (when rtnall
  4472. (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
  4473. (goto-char (point-min))
  4474. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  4475. (add-text-properties (point-min) (point-max)
  4476. `(org-agenda-type search
  4477. org-last-args (,todo-only ,string ,edit-at)
  4478. org-redo-cmd ,org-agenda-redo-command
  4479. org-series-cmd ,org-cmd))
  4480. (org-agenda-finalize)
  4481. (setq buffer-read-only t))))
  4482. ;;; Agenda TODO list
  4483. (defun org-agenda-propertize-selected-todo-keywords (keywords)
  4484. "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
  4485. (concat
  4486. (if (or (equal keywords "ALL") (not keywords))
  4487. (propertize "ALL" 'face 'org-agenda-structure-filter)
  4488. (mapconcat
  4489. (lambda (kw)
  4490. (propertize kw 'face (list (org-get-todo-face kw) 'org-agenda-structure)))
  4491. (org-split-string keywords "|")
  4492. "|"))
  4493. "\n"))
  4494. (defvar org-select-this-todo-keyword nil)
  4495. (defvar org-last-arg nil)
  4496. (defvar crm-separator)
  4497. ;;;###autoload
  4498. (defun org-todo-list (&optional arg)
  4499. "Show all (not done) TODO entries from all agenda files in a single list.
  4500. The prefix arg can be used to select a specific TODO keyword and limit
  4501. the list to these. When using `\\[universal-argument]', you will be prompted
  4502. for a keyword. A numeric prefix directly selects the Nth keyword in
  4503. `org-todo-keywords-1'."
  4504. (interactive "P")
  4505. (when org-agenda-overriding-arguments
  4506. (setq arg org-agenda-overriding-arguments))
  4507. (when (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
  4508. (let* ((today (org-today))
  4509. (date (calendar-gregorian-from-absolute today))
  4510. (completion-ignore-case t)
  4511. kwds org-select-this-todo-keyword rtn rtnall files file pos)
  4512. (catch 'exit
  4513. (setq org-agenda-buffer-name
  4514. (org-agenda--get-buffer-name
  4515. (and org-agenda-sticky
  4516. (if (stringp org-select-this-todo-keyword)
  4517. (format "*Org Agenda(%s:%s)*" (or org-keys "t")
  4518. org-select-this-todo-keyword)
  4519. (format "*Org Agenda(%s)*" (or org-keys "t"))))))
  4520. (org-agenda-prepare "TODO")
  4521. (setq kwds org-todo-keywords-for-agenda
  4522. org-select-this-todo-keyword (if (stringp arg) arg
  4523. (and (integerp arg)
  4524. (> arg 0)
  4525. (nth (1- arg) kwds))))
  4526. (when (equal arg '(4))
  4527. (setq org-select-this-todo-keyword
  4528. (mapconcat #'identity
  4529. (let ((crm-separator "|"))
  4530. (completing-read-multiple
  4531. "Keyword (or KWD1|KWD2|...): "
  4532. (mapcar #'list kwds) nil nil))
  4533. "|")))
  4534. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  4535. (org-compile-prefix-format 'todo)
  4536. (org-set-sorting-strategy 'todo)
  4537. (setq org-agenda-redo-command
  4538. `(org-todo-list (or (and (numberp current-prefix-arg)
  4539. current-prefix-arg)
  4540. ,org-select-this-todo-keyword
  4541. current-prefix-arg ,arg)))
  4542. (setq files (org-agenda-files nil 'ifmode)
  4543. rtnall nil)
  4544. (while (setq file (pop files))
  4545. (catch 'nextfile
  4546. (org-check-agenda-file file)
  4547. (setq rtn (org-agenda-get-day-entries file date :todo))
  4548. (setq rtnall (append rtnall rtn))))
  4549. (org-agenda--insert-overriding-header
  4550. (with-temp-buffer
  4551. (insert "Global list of TODO items of type: ")
  4552. (add-text-properties (point-min) (1- (point))
  4553. (list 'face 'org-agenda-structure
  4554. 'short-heading
  4555. (concat "ToDo: "
  4556. (or org-select-this-todo-keyword "ALL"))))
  4557. (org-agenda-mark-header-line (point-min))
  4558. (insert (org-agenda-propertize-selected-todo-keywords
  4559. org-select-this-todo-keyword))
  4560. (setq pos (point))
  4561. (unless org-agenda-multi
  4562. (insert (substitute-command-keys "Press \
  4563. \\<org-agenda-mode-map>`N \\[org-agenda-redo]' (e.g. `0 \\[org-agenda-redo]') \
  4564. to search again: (0)[ALL]"))
  4565. (let ((n 0))
  4566. (dolist (k kwds)
  4567. (let ((s (format "(%d)%s" (cl-incf n) k)))
  4568. (when (> (+ (current-column) (string-width s) 1) (window-max-chars-per-line))
  4569. (insert "\n "))
  4570. (insert " " s))))
  4571. (insert "\n"))
  4572. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure-secondary))
  4573. (buffer-string)))
  4574. (org-agenda-mark-header-line (point-min))
  4575. (when rtnall
  4576. (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
  4577. (goto-char (point-min))
  4578. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  4579. (add-text-properties (point-min) (point-max)
  4580. `(org-agenda-type todo
  4581. org-last-args ,arg
  4582. org-redo-cmd ,org-agenda-redo-command
  4583. org-series-cmd ,org-cmd))
  4584. (org-agenda-finalize)
  4585. (setq buffer-read-only t))))
  4586. ;;; Agenda tags match
  4587. ;;;###autoload
  4588. (defun org-tags-view (&optional todo-only match)
  4589. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  4590. The prefix arg TODO-ONLY limits the search to TODO entries."
  4591. (interactive "P")
  4592. (when org-agenda-overriding-arguments
  4593. (setq todo-only (car org-agenda-overriding-arguments)
  4594. match (nth 1 org-agenda-overriding-arguments)))
  4595. (let* ((org-tags-match-list-sublevels
  4596. org-tags-match-list-sublevels)
  4597. (completion-ignore-case t)
  4598. (org--matcher-tags-todo-only todo-only)
  4599. rtn rtnall files file pos matcher
  4600. buffer)
  4601. (when (and (stringp match) (not (string-match "\\S-" match)))
  4602. (setq match nil))
  4603. (catch 'exit
  4604. (setq org-agenda-buffer-name
  4605. (org-agenda--get-buffer-name
  4606. (and org-agenda-sticky
  4607. (if (stringp match)
  4608. (format "*Org Agenda(%s:%s)*"
  4609. (or org-keys (or (and todo-only "M") "m"))
  4610. match)
  4611. (format "*Org Agenda(%s)*"
  4612. (or (and todo-only "M") "m"))))))
  4613. (setq matcher (org-make-tags-matcher match))
  4614. ;; Prepare agendas (and `org-tag-alist-for-agenda') before
  4615. ;; expanding tags within `org-make-tags-matcher'
  4616. (org-agenda-prepare (concat "TAGS " match))
  4617. (setq match (car matcher)
  4618. matcher (cdr matcher))
  4619. (org-compile-prefix-format 'tags)
  4620. (org-set-sorting-strategy 'tags)
  4621. (setq org-agenda-query-string match)
  4622. (setq org-agenda-redo-command
  4623. (list 'org-tags-view
  4624. `(quote ,org--matcher-tags-todo-only)
  4625. `(if current-prefix-arg nil ,org-agenda-query-string)))
  4626. (setq files (org-agenda-files nil 'ifmode)
  4627. rtnall nil)
  4628. (while (setq file (pop files))
  4629. (catch 'nextfile
  4630. (org-check-agenda-file file)
  4631. (setq buffer (if (file-exists-p file)
  4632. (org-get-agenda-file-buffer file)
  4633. (error "No such file %s" file)))
  4634. (if (not buffer)
  4635. ;; If file does not exist, error message to agenda
  4636. (setq rtn (list
  4637. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  4638. rtnall (append rtnall rtn))
  4639. (with-current-buffer buffer
  4640. (unless (derived-mode-p 'org-mode)
  4641. (error "Agenda file %s is not in Org mode" file))
  4642. (save-excursion
  4643. (save-restriction
  4644. (if (eq buffer org-agenda-restrict)
  4645. (narrow-to-region org-agenda-restrict-begin
  4646. org-agenda-restrict-end)
  4647. (widen))
  4648. (setq rtn (org-scan-tags 'agenda
  4649. matcher
  4650. org--matcher-tags-todo-only))
  4651. (setq rtnall (append rtnall rtn))))))))
  4652. (org-agenda--insert-overriding-header
  4653. (with-temp-buffer
  4654. (insert "Headlines with TAGS match: ")
  4655. (add-text-properties (point-min) (1- (point))
  4656. (list 'face 'org-agenda-structure
  4657. 'short-heading
  4658. (concat "Match: " match)))
  4659. (setq pos (point))
  4660. (insert match "\n")
  4661. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure-filter))
  4662. (setq pos (point))
  4663. (unless org-agenda-multi
  4664. (insert (substitute-command-keys
  4665. "Press \
  4666. \\<org-agenda-mode-map>`\\[universal-argument] \\[org-agenda-redo]' \
  4667. to search again\n")))
  4668. (add-text-properties pos (1- (point))
  4669. (list 'face 'org-agenda-structure-secondary))
  4670. (buffer-string)))
  4671. (org-agenda-mark-header-line (point-min))
  4672. (when rtnall
  4673. (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
  4674. (goto-char (point-min))
  4675. (or org-agenda-multi (org-agenda-fit-window-to-buffer))
  4676. (add-text-properties
  4677. (point-min) (point-max)
  4678. `(org-agenda-type tags
  4679. org-last-args (,org--matcher-tags-todo-only ,match)
  4680. org-redo-cmd ,org-agenda-redo-command
  4681. org-series-cmd ,org-cmd))
  4682. (org-agenda-finalize)
  4683. (setq buffer-read-only t))))
  4684. ;;; Agenda Finding stuck projects
  4685. (defvar org-agenda-skip-regexp nil
  4686. "Regular expression used in skipping subtrees for the agenda.
  4687. This is basically a temporary global variable that can be set and then
  4688. used by user-defined selections using `org-agenda-skip-function'.")
  4689. (defvar org-agenda-overriding-header nil
  4690. "When set during agenda, todo and tags searches it replaces the header.
  4691. If an empty string, no header will be inserted. If any other
  4692. string, it will be inserted as a header. If a function, insert
  4693. the string returned by the function as a header. If nil, a
  4694. header will be generated automatically according to the command.
  4695. This variable should not be set directly, but custom commands can
  4696. bind it in the options section.")
  4697. (defun org-agenda-skip-entry-if (&rest conditions)
  4698. "Skip entry if any of CONDITIONS is true.
  4699. See `org-agenda-skip-if' for details about CONDITIONS.
  4700. This function can be put into `org-agenda-skip-function' for the
  4701. duration of a command."
  4702. (org-agenda-skip-if nil conditions))
  4703. (defun org-agenda-skip-subtree-if (&rest conditions)
  4704. "Skip subtree if any of CONDITIONS is true.
  4705. See `org-agenda-skip-if' for details about CONDITIONS.
  4706. This function can be put into `org-agenda-skip-function' for the
  4707. duration of a command."
  4708. (org-agenda-skip-if t conditions))
  4709. (defun org-agenda-skip-if (subtree conditions)
  4710. "Check current entity for CONDITIONS.
  4711. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  4712. the entry (i.e. the text before the next heading) is checked.
  4713. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  4714. from different tests. Valid conditions are:
  4715. scheduled Check if there is a scheduled cookie
  4716. notscheduled Check if there is no scheduled cookie
  4717. deadline Check if there is a deadline
  4718. notdeadline Check if there is no deadline
  4719. timestamp Check if there is a timestamp (also deadline or scheduled)
  4720. nottimestamp Check if there is no timestamp (also deadline or scheduled)
  4721. regexp Check if regexp matches
  4722. notregexp Check if regexp does not match.
  4723. todo Check if TODO keyword matches
  4724. nottodo Check if TODO keyword does not match
  4725. The regexp is taken from the conditions list, and must come right
  4726. after the `regexp' or `notregexp' element.
  4727. `todo' and `nottodo' accept as an argument a list of todo
  4728. keywords, which may include \"*\" to match any todo keyword.
  4729. (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
  4730. would skip all entries with \"TODO\" or \"WAITING\" keywords.
  4731. Instead of a list, a keyword class may be given. For example:
  4732. (org-agenda-skip-entry-if \\='nottodo \\='done)
  4733. would skip entries that haven't been marked with any of \"DONE\"
  4734. keywords. Possible classes are: `todo', `done', `any'.
  4735. If any of these conditions is met, this function returns the end point of
  4736. the entity, causing the search to continue from there. This is a function
  4737. that can be put into `org-agenda-skip-function' for the duration of a command."
  4738. (org-back-to-heading t)
  4739. (let* (;; (beg (point))
  4740. (end (if subtree (save-excursion (org-end-of-subtree t) (point))
  4741. (org-entry-end-position)))
  4742. (planning-end (if subtree end (line-end-position 2)))
  4743. m)
  4744. (and
  4745. (or (and (memq 'scheduled conditions)
  4746. (re-search-forward org-scheduled-time-regexp planning-end t))
  4747. (and (memq 'notscheduled conditions)
  4748. (not
  4749. (save-excursion
  4750. (re-search-forward org-scheduled-time-regexp planning-end t))))
  4751. (and (memq 'deadline conditions)
  4752. (re-search-forward org-deadline-time-regexp planning-end t))
  4753. (and (memq 'notdeadline conditions)
  4754. (not
  4755. (save-excursion
  4756. (re-search-forward org-deadline-time-regexp planning-end t))))
  4757. (and (memq 'timestamp conditions)
  4758. (re-search-forward org-ts-regexp end t))
  4759. (and (memq 'nottimestamp conditions)
  4760. (not (save-excursion (re-search-forward org-ts-regexp end t))))
  4761. (and (setq m (memq 'regexp conditions))
  4762. (stringp (nth 1 m))
  4763. (re-search-forward (nth 1 m) end t))
  4764. (and (setq m (memq 'notregexp conditions))
  4765. (stringp (nth 1 m))
  4766. (not (save-excursion (re-search-forward (nth 1 m) end t))))
  4767. (and (or
  4768. (setq m (memq 'nottodo conditions))
  4769. (setq m (memq 'todo-unblocked conditions))
  4770. (setq m (memq 'nottodo-unblocked conditions))
  4771. (setq m (memq 'todo conditions)))
  4772. (org-agenda-skip-if-todo m end)))
  4773. end)))
  4774. (defun org-agenda-skip-if-todo (args end)
  4775. "Helper function for `org-agenda-skip-if', do not use it directly.
  4776. ARGS is a list with first element either `todo', `nottodo',
  4777. `todo-unblocked' or `nottodo-unblocked'. The remainder is either
  4778. a list of TODO keywords, or a state symbol `todo' or `done' or
  4779. `any'."
  4780. (let ((todo-re
  4781. (concat "^\\*+[ \t]+"
  4782. (regexp-opt
  4783. (pcase args
  4784. (`(,_ todo)
  4785. (org-delete-all org-done-keywords
  4786. (copy-sequence org-todo-keywords-1)))
  4787. (`(,_ done) org-done-keywords)
  4788. (`(,_ any) org-todo-keywords-1)
  4789. (`(,_ ,(pred atom))
  4790. (error "Invalid TODO class or type: %S" args))
  4791. (`(,_ ,(pred (member "*"))) org-todo-keywords-1)
  4792. (`(,_ ,todo-list) todo-list))
  4793. 'words))))
  4794. (pcase args
  4795. (`(todo . ,_)
  4796. (let (case-fold-search) (re-search-forward todo-re end t)))
  4797. (`(nottodo . ,_)
  4798. (not (let (case-fold-search) (re-search-forward todo-re end t))))
  4799. (`(todo-unblocked . ,_)
  4800. (catch :unblocked
  4801. (while (let (case-fold-search) (re-search-forward todo-re end t))
  4802. (when (org-entry-blocked-p) (throw :unblocked t)))
  4803. nil))
  4804. (`(nottodo-unblocked . ,_)
  4805. (catch :unblocked
  4806. (while (let (case-fold-search) (re-search-forward todo-re end t))
  4807. (when (org-entry-blocked-p) (throw :unblocked nil)))
  4808. t))
  4809. (`(,type . ,_) (error "Unknown TODO skip type: %S" type)))))
  4810. ;;;###autoload
  4811. (defun org-agenda-list-stuck-projects (&rest _ignore)
  4812. "Create agenda view for projects that are stuck.
  4813. Stuck projects are project that have no next actions. For the definitions
  4814. of what a project is and how to check if it stuck, customize the variable
  4815. `org-stuck-projects'."
  4816. (interactive)
  4817. (let* ((org-agenda-overriding-header
  4818. (or org-agenda-overriding-header "List of stuck projects: "))
  4819. (matcher (nth 0 org-stuck-projects))
  4820. (todo (nth 1 org-stuck-projects))
  4821. (tags (nth 2 org-stuck-projects))
  4822. (gen-re (org-string-nw-p (nth 3 org-stuck-projects)))
  4823. (todo-wds
  4824. (if (not (member "*" todo)) todo
  4825. (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
  4826. (org-delete-all org-done-keywords-for-agenda
  4827. (copy-sequence org-todo-keywords-for-agenda))))
  4828. (todo-re (and todo
  4829. (format "^\\*+[ \t]+\\(%s\\)\\>"
  4830. (mapconcat #'identity todo-wds "\\|"))))
  4831. (tags-re (cond ((null tags) nil)
  4832. ((member "*" tags) org-tag-line-re)
  4833. (tags
  4834. (let ((other-tags (format "\\(?:%s:\\)*" org-tag-re)))
  4835. (concat org-outline-regexp-bol
  4836. ".*?[ \t]:"
  4837. other-tags
  4838. (regexp-opt tags t)
  4839. ":" other-tags "[ \t]*$")))
  4840. (t nil)))
  4841. (re-list (delq nil (list todo-re tags-re gen-re)))
  4842. (skip-re
  4843. (if (null re-list)
  4844. (error "Missing information to identify unstuck projects")
  4845. (mapconcat #'identity re-list "\\|")))
  4846. (org-agenda-skip-function
  4847. ;; Skip entry if `org-agenda-skip-regexp' matches anywhere
  4848. ;; in the subtree.
  4849. (lambda ()
  4850. (and (save-excursion
  4851. (let ((case-fold-search nil))
  4852. (re-search-forward
  4853. skip-re (save-excursion (org-end-of-subtree t)) t)))
  4854. (progn (outline-next-heading) (point))))))
  4855. (org-tags-view nil matcher)
  4856. (setq org-agenda-buffer-name (buffer-name))
  4857. (with-current-buffer org-agenda-buffer-name
  4858. (setq org-agenda-redo-command
  4859. `(org-agenda-list-stuck-projects ,current-prefix-arg))
  4860. (let ((inhibit-read-only t))
  4861. (add-text-properties
  4862. (point-min) (point-max)
  4863. `(org-redo-cmd ,org-agenda-redo-command))))))
  4864. ;;; Diary integration
  4865. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  4866. (defvar diary-list-entries-hook)
  4867. (defvar diary-time-regexp)
  4868. (defvar diary-modify-entry-list-string-function)
  4869. (defvar diary-file-name-prefix)
  4870. (defvar diary-display-function)
  4871. (defun org-get-entries-from-diary (date)
  4872. "Get the (Emacs Calendar) diary entries for DATE."
  4873. (require 'diary-lib)
  4874. (declare-function diary-fancy-display "diary-lib" ())
  4875. (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
  4876. (diary-display-function #'diary-fancy-display)
  4877. (pop-up-frames nil)
  4878. (diary-list-entries-hook
  4879. (cons 'org-diary-default-entry diary-list-entries-hook))
  4880. (diary-file-name-prefix nil) ; turn this feature off
  4881. (diary-modify-entry-list-string-function
  4882. #'org-modify-diary-entry-string)
  4883. (diary-time-regexp (concat "^" diary-time-regexp))
  4884. entries
  4885. (org-disable-agenda-to-diary t))
  4886. (save-excursion
  4887. (save-window-excursion
  4888. (diary-list-entries date 1)))
  4889. (if (not (get-buffer diary-fancy-buffer))
  4890. (setq entries nil)
  4891. (with-current-buffer diary-fancy-buffer
  4892. (setq buffer-read-only nil)
  4893. (if (zerop (buffer-size))
  4894. ;; No entries
  4895. (setq entries nil)
  4896. ;; Omit the date and other unnecessary stuff
  4897. (org-agenda-cleanup-fancy-diary)
  4898. ;; Add prefix to each line and extend the text properties
  4899. (if (zerop (buffer-size))
  4900. (setq entries nil)
  4901. (setq entries (buffer-substring (point-min) (- (point-max) 1)))
  4902. (setq entries
  4903. (with-temp-buffer
  4904. (insert entries) (goto-char (point-min))
  4905. (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
  4906. (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
  4907. (replace-match (concat "; " (match-string 1)))))
  4908. (buffer-string)))))
  4909. (set-buffer-modified-p nil)
  4910. (kill-buffer diary-fancy-buffer)))
  4911. (when entries
  4912. (setq entries (org-split-string entries "\n"))
  4913. (setq entries
  4914. (mapcar
  4915. (lambda (x)
  4916. (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
  4917. ;; Extend the text properties to the beginning of the line
  4918. (org-add-props x (text-properties-at (1- (length x)) x)
  4919. 'type "diary" 'date date 'face 'org-agenda-diary))
  4920. entries)))))
  4921. (defvar org-agenda-cleanup-fancy-diary-hook nil
  4922. "Hook run when the fancy diary buffer is cleaned up.")
  4923. (defun org-agenda-cleanup-fancy-diary ()
  4924. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  4925. This gets rid of the date, the underline under the date, and the
  4926. dummy entry installed by Org mode to ensure non-empty diary for
  4927. each date. It also removes lines that contain only whitespace."
  4928. (goto-char (point-min))
  4929. (if (looking-at ".*?:[ \t]*")
  4930. (progn
  4931. (replace-match "")
  4932. (re-search-forward "\n=+$" nil t)
  4933. (replace-match "")
  4934. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  4935. (re-search-forward "\n=+$" nil t)
  4936. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  4937. (goto-char (point-min))
  4938. (while (re-search-forward "^ +\n" nil t)
  4939. (replace-match ""))
  4940. (goto-char (point-min))
  4941. (when (re-search-forward "^Org mode dummy\n?" nil t)
  4942. (replace-match ""))
  4943. (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
  4944. (defun org-modify-diary-entry-string (string)
  4945. "Add text properties to string, allowing Org to act on it."
  4946. (org-add-props string nil
  4947. 'mouse-face 'highlight
  4948. 'help-echo (if buffer-file-name
  4949. (format "mouse-2 or RET jump to diary file %s"
  4950. (abbreviate-file-name buffer-file-name))
  4951. "")
  4952. 'org-agenda-diary-link t
  4953. 'org-marker (org-agenda-new-marker (line-beginning-position))))
  4954. (defun org-diary-default-entry ()
  4955. "Add a dummy entry to the diary.
  4956. Needed to avoid empty dates which mess up holiday display."
  4957. ;; Catch the error if dealing with the new add-to-diary-alist
  4958. (when org-disable-agenda-to-diary
  4959. (diary-add-to-list original-date "Org mode dummy" "")))
  4960. (defvar org-diary-last-run-time nil)
  4961. ;;;###autoload
  4962. (defun org-diary (&rest args)
  4963. "Return diary information from org files.
  4964. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  4965. It accesses org files and extracts information from those files to be
  4966. listed in the diary. The function accepts arguments specifying what
  4967. items should be listed. For a list of arguments allowed here, see the
  4968. variable `org-agenda-entry-types'.
  4969. The call in the diary file should look like this:
  4970. &%%(org-diary) ~/path/to/some/orgfile.org
  4971. Use a separate line for each org file to check. Or, if you omit the file name,
  4972. all files listed in `org-agenda-files' will be checked automatically:
  4973. &%%(org-diary)
  4974. If you don't give any arguments (as in the example above), the default value
  4975. of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
  4976. So the example above may also be written as
  4977. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  4978. The function expects the lisp variables `entry' and `date' to be provided
  4979. by the caller, because this is how the calendar works. Don't use this
  4980. function from a program - use `org-agenda-get-day-entries' instead."
  4981. (with-no-warnings (defvar date) (defvar entry))
  4982. (when (> (- (float-time)
  4983. org-agenda-last-marker-time)
  4984. 5)
  4985. ;; I am not sure if this works with sticky agendas, because the marker
  4986. ;; list is then no longer a global variable.
  4987. (org-agenda-reset-markers))
  4988. (org-compile-prefix-format 'agenda)
  4989. (org-set-sorting-strategy 'agenda)
  4990. (setq args (or args org-agenda-entry-types))
  4991. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  4992. (list entry)
  4993. (org-agenda-files t)))
  4994. (time (float-time))
  4995. file rtn results)
  4996. (when (or (not org-diary-last-run-time)
  4997. (> (- time
  4998. org-diary-last-run-time)
  4999. 3))
  5000. (org-agenda-prepare-buffers files))
  5001. (setq org-diary-last-run-time time)
  5002. ;; If this is called during org-agenda, don't return any entries to
  5003. ;; the calendar. Org Agenda will list these entries itself.
  5004. (when org-disable-agenda-to-diary (setq files nil))
  5005. (while (setq file (pop files))
  5006. (setq rtn (apply #'org-agenda-get-day-entries file date args))
  5007. (setq results (append results rtn)))
  5008. (when results
  5009. (setq results
  5010. (mapcar (lambda (i) (replace-regexp-in-string
  5011. org-link-bracket-re "\\2" i))
  5012. results))
  5013. (concat (org-agenda-finalize-entries results) "\n"))))
  5014. ;;; Agenda entry finders
  5015. (defun org-agenda--timestamp-to-absolute (&rest args)
  5016. "Call `org-time-string-to-absolute' with ARGS.
  5017. However, throw `:skip' whenever an error is raised."
  5018. (condition-case e
  5019. (apply #'org-time-string-to-absolute args)
  5020. (org-diary-sexp-no-match (throw :skip nil))
  5021. (error
  5022. (message "%s; Skipping entry" (error-message-string e))
  5023. (throw :skip nil))))
  5024. (defun org-agenda-get-day-entries (file date &rest args)
  5025. "Does the work for `org-diary' and `org-agenda'.
  5026. FILE is the path to a file to be checked for entries. DATE is date like
  5027. the one returned by `calendar-current-date'. ARGS are symbols indicating
  5028. which kind of entries should be extracted. For details about these, see
  5029. the documentation of `org-diary'."
  5030. (let* ((org-startup-folded nil)
  5031. (org-startup-align-all-tables nil)
  5032. (buffer (if (file-exists-p file) (org-get-agenda-file-buffer file)
  5033. (error "No such file %s" file))))
  5034. (if (not buffer)
  5035. ;; If file does not exist, signal it in diary nonetheless.
  5036. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  5037. (with-current-buffer buffer
  5038. (unless (derived-mode-p 'org-mode)
  5039. (error "Agenda file %s is not in Org mode" file))
  5040. (setq org-agenda-buffer (or org-agenda-buffer buffer))
  5041. (setf org-agenda-current-date date)
  5042. (save-excursion
  5043. (save-restriction
  5044. (if (eq buffer org-agenda-restrict)
  5045. (narrow-to-region org-agenda-restrict-begin
  5046. org-agenda-restrict-end)
  5047. (widen))
  5048. ;; Rationalize ARGS. Also make sure `:deadline' comes
  5049. ;; first in order to populate DEADLINES before passing it.
  5050. ;;
  5051. ;; We use `delq' since `org-uniquify' duplicates ARGS,
  5052. ;; guarding us from modifying `org-agenda-entry-types'.
  5053. (setf args (org-uniquify (or args org-agenda-entry-types)))
  5054. (when (and (memq :scheduled args) (memq :scheduled* args))
  5055. (setf args (delq :scheduled* args)))
  5056. (cond
  5057. ((memq :deadline args)
  5058. (setf args (cons :deadline
  5059. (delq :deadline (delq :deadline* args)))))
  5060. ((memq :deadline* args)
  5061. (setf args (cons :deadline* (delq :deadline* args)))))
  5062. ;; Collect list of headlines. Return them flattened.
  5063. (let ((case-fold-search nil) results deadlines)
  5064. (org-dlet
  5065. ((date date))
  5066. (dolist (arg args (apply #'nconc (nreverse results)))
  5067. (pcase arg
  5068. ((and :todo (guard (org-agenda-today-p date)))
  5069. (push (org-agenda-get-todos) results))
  5070. (:timestamp
  5071. (push (org-agenda-get-blocks) results)
  5072. (push (org-agenda-get-timestamps deadlines) results))
  5073. (:sexp
  5074. (push (org-agenda-get-sexps) results))
  5075. (:scheduled
  5076. (push (org-agenda-get-scheduled deadlines) results))
  5077. (:scheduled*
  5078. (push (org-agenda-get-scheduled deadlines t) results))
  5079. (:closed
  5080. (push (org-agenda-get-progress) results))
  5081. (:deadline
  5082. (setf deadlines (org-agenda-get-deadlines))
  5083. (push deadlines results))
  5084. (:deadline*
  5085. (setf deadlines (org-agenda-get-deadlines t))
  5086. (push deadlines results))))))))))))
  5087. (defsubst org-em (x y list)
  5088. "Is X or Y a member of LIST?"
  5089. (or (memq x list) (memq y list)))
  5090. (defvar org-heading-keyword-regexp-format) ; defined in org.el
  5091. (defvar org-agenda-sorting-strategy-selected nil)
  5092. (defun org-agenda-entry-get-agenda-timestamp (pom)
  5093. "Retrieve timestamp information for sorting agenda views.
  5094. Given a point or marker POM, returns a cons cell of the timestamp
  5095. and the timestamp type relevant for the sorting strategy in
  5096. `org-agenda-sorting-strategy-selected'."
  5097. (let (ts ts-date-type)
  5098. (save-match-data
  5099. (cond ((org-em 'scheduled-up 'scheduled-down
  5100. org-agenda-sorting-strategy-selected)
  5101. (setq ts (org-entry-get pom "SCHEDULED")
  5102. ts-date-type " scheduled"))
  5103. ((org-em 'deadline-up 'deadline-down
  5104. org-agenda-sorting-strategy-selected)
  5105. (setq ts (org-entry-get pom "DEADLINE")
  5106. ts-date-type " deadline"))
  5107. ((org-em 'ts-up 'ts-down
  5108. org-agenda-sorting-strategy-selected)
  5109. (setq ts (org-entry-get pom "TIMESTAMP")
  5110. ts-date-type " timestamp"))
  5111. ((org-em 'tsia-up 'tsia-down
  5112. org-agenda-sorting-strategy-selected)
  5113. (setq ts (org-entry-get pom "TIMESTAMP_IA")
  5114. ts-date-type " timestamp_ia"))
  5115. ((org-em 'timestamp-up 'timestamp-down
  5116. org-agenda-sorting-strategy-selected)
  5117. (setq ts (or (org-entry-get pom "SCHEDULED")
  5118. (org-entry-get pom "DEADLINE")
  5119. (org-entry-get pom "TIMESTAMP")
  5120. (org-entry-get pom "TIMESTAMP_IA"))
  5121. ts-date-type ""))
  5122. (t (setq ts-date-type "")))
  5123. (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
  5124. ts-date-type))))
  5125. (defun org-agenda-get-todos ()
  5126. "Return the TODO information for agenda display."
  5127. (let* ((props (list 'face nil
  5128. 'done-face 'org-agenda-done
  5129. 'org-not-done-regexp org-not-done-regexp
  5130. 'org-todo-regexp org-todo-regexp
  5131. 'org-complex-heading-regexp org-complex-heading-regexp
  5132. 'mouse-face 'highlight
  5133. 'help-echo
  5134. (format "mouse-2 or RET jump to org file %s"
  5135. (abbreviate-file-name buffer-file-name))))
  5136. (case-fold-search nil)
  5137. (regexp (format org-heading-keyword-regexp-format
  5138. (cond
  5139. ((and org-select-this-todo-keyword
  5140. (equal org-select-this-todo-keyword "*"))
  5141. org-todo-regexp)
  5142. (org-select-this-todo-keyword
  5143. (concat "\\("
  5144. (mapconcat #'identity
  5145. (org-split-string
  5146. org-select-this-todo-keyword
  5147. "|")
  5148. "\\|")
  5149. "\\)"))
  5150. (t org-not-done-regexp))))
  5151. marker priority category level tags todo-state
  5152. ts-date ts-date-type ts-date-pair
  5153. ee txt beg end inherited-tags todo-state-end-pos
  5154. effort effort-minutes)
  5155. (goto-char (point-min))
  5156. (while (re-search-forward regexp nil t)
  5157. (catch :skip
  5158. (save-match-data
  5159. (beginning-of-line)
  5160. (org-agenda-skip)
  5161. (setq beg (point) end (save-excursion (outline-next-heading) (point)))
  5162. (unless (and (setq todo-state (org-get-todo-state))
  5163. (setq todo-state-end-pos (match-end 2)))
  5164. (goto-char end)
  5165. (throw :skip nil))
  5166. (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
  5167. (goto-char (1+ beg))
  5168. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  5169. (throw :skip nil)))
  5170. (goto-char (match-beginning 2))
  5171. (setq marker (org-agenda-new-marker (match-beginning 0))
  5172. category (org-get-category)
  5173. effort (save-match-data (or (get-text-property (point) 'effort)
  5174. (org-entry-get (point) org-effort-property)))
  5175. effort-minutes (when effort (save-match-data (org-duration-to-minutes effort)))
  5176. ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  5177. ts-date (car ts-date-pair)
  5178. ts-date-type (cdr ts-date-pair)
  5179. txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
  5180. inherited-tags
  5181. (or (eq org-agenda-show-inherited-tags 'always)
  5182. (and (listp org-agenda-show-inherited-tags)
  5183. (memq 'todo org-agenda-show-inherited-tags))
  5184. (and (eq org-agenda-show-inherited-tags t)
  5185. (or (eq org-agenda-use-tag-inheritance t)
  5186. (memq 'todo org-agenda-use-tag-inheritance))))
  5187. tags (org-get-tags nil (not inherited-tags))
  5188. level (make-string (org-reduced-level (org-outline-level)) ? )
  5189. txt (org-agenda-format-item ""
  5190. (org-add-props txt nil
  5191. 'effort effort
  5192. 'effort-minutes effort-minutes)
  5193. level category tags t)
  5194. priority (1+ (org-get-priority txt)))
  5195. (org-add-props txt props
  5196. 'org-marker marker 'org-hd-marker marker
  5197. 'priority priority
  5198. 'effort effort 'effort-minutes effort-minutes
  5199. 'level level
  5200. 'ts-date ts-date
  5201. 'type (concat "todo" ts-date-type) 'todo-state todo-state)
  5202. (push txt ee)
  5203. (if org-agenda-todo-list-sublevels
  5204. (goto-char todo-state-end-pos)
  5205. (org-end-of-subtree 'invisible))))
  5206. (nreverse ee)))
  5207. (defun org-agenda-todo-custom-ignore-p (time n)
  5208. "Check whether timestamp is farther away than n number of days.
  5209. This function is invoked if `org-agenda-todo-ignore-deadlines',
  5210. `org-agenda-todo-ignore-scheduled' or
  5211. `org-agenda-todo-ignore-timestamp' is set to an integer."
  5212. (let ((days (org-time-stamp-to-now
  5213. time org-agenda-todo-ignore-time-comparison-use-seconds)))
  5214. (if (>= n 0)
  5215. (>= days n)
  5216. (<= days n))))
  5217. ;;;###autoload
  5218. (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  5219. (&optional end)
  5220. "Do we have a reason to ignore this TODO entry because it has a time stamp?"
  5221. (when (or org-agenda-todo-ignore-with-date
  5222. org-agenda-todo-ignore-scheduled
  5223. org-agenda-todo-ignore-deadlines
  5224. org-agenda-todo-ignore-timestamp)
  5225. (setq end (or end (save-excursion (outline-next-heading) (point))))
  5226. (save-excursion
  5227. (or (and org-agenda-todo-ignore-with-date
  5228. (re-search-forward org-ts-regexp end t))
  5229. (and org-agenda-todo-ignore-scheduled
  5230. (re-search-forward org-scheduled-time-regexp end t)
  5231. (cond
  5232. ((eq org-agenda-todo-ignore-scheduled 'future)
  5233. (> (org-time-stamp-to-now
  5234. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds)
  5235. 0))
  5236. ((eq org-agenda-todo-ignore-scheduled 'past)
  5237. (<= (org-time-stamp-to-now
  5238. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds)
  5239. 0))
  5240. ((numberp org-agenda-todo-ignore-scheduled)
  5241. (org-agenda-todo-custom-ignore-p
  5242. (match-string 1) org-agenda-todo-ignore-scheduled))
  5243. (t)))
  5244. (and org-agenda-todo-ignore-deadlines
  5245. (re-search-forward org-deadline-time-regexp end t)
  5246. (cond
  5247. ((eq org-agenda-todo-ignore-deadlines 'all) t)
  5248. ((eq org-agenda-todo-ignore-deadlines 'far)
  5249. (not (org-deadline-close-p (match-string 1))))
  5250. ((eq org-agenda-todo-ignore-deadlines 'future)
  5251. (> (org-time-stamp-to-now
  5252. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds)
  5253. 0))
  5254. ((eq org-agenda-todo-ignore-deadlines 'past)
  5255. (<= (org-time-stamp-to-now
  5256. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds)
  5257. 0))
  5258. ((numberp org-agenda-todo-ignore-deadlines)
  5259. (org-agenda-todo-custom-ignore-p
  5260. (match-string 1) org-agenda-todo-ignore-deadlines))
  5261. (t (org-deadline-close-p (match-string 1)))))
  5262. (and org-agenda-todo-ignore-timestamp
  5263. (let ((buffer (current-buffer))
  5264. (regexp
  5265. (concat
  5266. org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
  5267. (start (point)))
  5268. ;; Copy current buffer into a temporary one
  5269. (with-temp-buffer
  5270. (insert-buffer-substring buffer start end)
  5271. (goto-char (point-min))
  5272. ;; Delete SCHEDULED and DEADLINE items
  5273. (while (re-search-forward regexp end t)
  5274. (delete-region (match-beginning 0) (match-end 0)))
  5275. (goto-char (point-min))
  5276. ;; No search for timestamp left
  5277. (when (re-search-forward org-ts-regexp nil t)
  5278. (cond
  5279. ((eq org-agenda-todo-ignore-timestamp 'future)
  5280. (> (org-time-stamp-to-now
  5281. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds)
  5282. 0))
  5283. ((eq org-agenda-todo-ignore-timestamp 'past)
  5284. (<= (org-time-stamp-to-now
  5285. (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds)
  5286. 0))
  5287. ((numberp org-agenda-todo-ignore-timestamp)
  5288. (org-agenda-todo-custom-ignore-p
  5289. (match-string 1) org-agenda-todo-ignore-timestamp))
  5290. (t))))))))))
  5291. (defun org-agenda-get-timestamps (&optional deadlines)
  5292. "Return the date stamp information for agenda display.
  5293. Optional argument DEADLINES is a list of deadline items to be
  5294. displayed in agenda view."
  5295. (with-no-warnings (defvar date))
  5296. (let* ((props (list 'face 'org-agenda-calendar-event
  5297. 'org-not-done-regexp org-not-done-regexp
  5298. 'org-todo-regexp org-todo-regexp
  5299. 'org-complex-heading-regexp org-complex-heading-regexp
  5300. 'mouse-face 'highlight
  5301. 'help-echo
  5302. (format "mouse-2 or RET jump to Org file %s"
  5303. (abbreviate-file-name buffer-file-name))))
  5304. (current (calendar-absolute-from-gregorian date))
  5305. (today (org-today))
  5306. (deadline-position-alist
  5307. (mapcar (lambda (d)
  5308. (let ((m (get-text-property 0 'org-hd-marker d)))
  5309. (and m (marker-position m))))
  5310. deadlines))
  5311. ;; Match time-stamps set to current date, time-stamps with
  5312. ;; a repeater, and S-exp time-stamps.
  5313. (regexp
  5314. (concat
  5315. (if org-agenda-include-inactive-timestamps "[[<]" "<")
  5316. (regexp-quote
  5317. (substring
  5318. (format-time-string
  5319. (car org-time-stamp-formats)
  5320. (org-encode-time ; DATE bound by calendar
  5321. 0 0 0 (nth 1 date) (car date) (nth 2 date)))
  5322. 1 11))
  5323. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
  5324. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  5325. timestamp-items)
  5326. (goto-char (point-min))
  5327. (while (re-search-forward regexp nil t)
  5328. ;; Skip date ranges, scheduled and deadlines, which are handled
  5329. ;; specially. Also skip time-stamps before first headline as
  5330. ;; there would be no entry to add to the agenda. Eventually,
  5331. ;; ignore clock entries.
  5332. (catch :skip
  5333. (save-match-data
  5334. (when (or (org-at-date-range-p)
  5335. (org-at-planning-p)
  5336. (org-before-first-heading-p)
  5337. (and org-agenda-include-inactive-timestamps
  5338. (org-at-clock-log-p))
  5339. (not (org-at-timestamp-p 'agenda)))
  5340. (throw :skip nil))
  5341. (org-agenda-skip (org-element-at-point)))
  5342. (let* ((pos (match-beginning 0))
  5343. (repeat (match-string 1))
  5344. (sexp-entry (match-string 3))
  5345. (time-stamp (if (or repeat sexp-entry) (match-string 0)
  5346. (save-excursion
  5347. (goto-char pos)
  5348. (looking-at org-ts-regexp-both)
  5349. (match-string 0))))
  5350. (todo-state (org-get-todo-state))
  5351. (warntime (get-text-property (point) 'org-appt-warntime))
  5352. (done? (member todo-state org-done-keywords)))
  5353. ;; Possibly skip done tasks.
  5354. (when (and done? org-agenda-skip-timestamp-if-done)
  5355. (throw :skip t))
  5356. ;; S-exp entry doesn't match current day: skip it.
  5357. (when (and sexp-entry (not (org-diary-sexp-entry sexp-entry "" date)))
  5358. (throw :skip nil))
  5359. (when repeat
  5360. (let* ((past
  5361. ;; A repeating time stamp is shown at its base
  5362. ;; date and every repeated date up to TODAY. If
  5363. ;; `org-agenda-prefer-last-repeat' is non-nil,
  5364. ;; however, only the last repeat before today
  5365. ;; (inclusive) is shown.
  5366. (org-agenda--timestamp-to-absolute
  5367. repeat
  5368. (if (or (> current today)
  5369. (eq org-agenda-prefer-last-repeat t)
  5370. (member todo-state org-agenda-prefer-last-repeat))
  5371. today
  5372. current)
  5373. 'past (current-buffer) pos))
  5374. (future
  5375. ;; Display every repeated date past TODAY
  5376. ;; (exclusive) unless
  5377. ;; `org-agenda-show-future-repeats' is nil. If
  5378. ;; this variable is set to `next', only display
  5379. ;; the first repeated date after TODAY
  5380. ;; (exclusive).
  5381. (cond
  5382. ((<= current today) past)
  5383. ((not org-agenda-show-future-repeats) past)
  5384. (t
  5385. (let ((base (if (eq org-agenda-show-future-repeats 'next)
  5386. (1+ today)
  5387. current)))
  5388. (org-agenda--timestamp-to-absolute
  5389. repeat base 'future (current-buffer) pos))))))
  5390. (when (and (/= current past) (/= current future))
  5391. (throw :skip nil))))
  5392. (save-excursion
  5393. (re-search-backward org-outline-regexp-bol nil t)
  5394. ;; Possibly skip time-stamp when a deadline is set.
  5395. (when (and org-agenda-skip-timestamp-if-deadline-is-shown
  5396. (assq (point) deadline-position-alist))
  5397. (throw :skip nil))
  5398. (let* ((category (org-get-category pos))
  5399. (effort (org-entry-get pos org-effort-property))
  5400. (effort-minutes (when effort (save-match-data (org-duration-to-minutes effort))))
  5401. (inherited-tags
  5402. (or (eq org-agenda-show-inherited-tags 'always)
  5403. (and (consp org-agenda-show-inherited-tags)
  5404. (memq 'agenda org-agenda-show-inherited-tags))
  5405. (and (eq org-agenda-show-inherited-tags t)
  5406. (or (eq org-agenda-use-tag-inheritance t)
  5407. (memq 'agenda
  5408. org-agenda-use-tag-inheritance)))))
  5409. (tags (org-get-tags nil (not inherited-tags)))
  5410. (level (make-string (org-reduced-level (org-outline-level))
  5411. ?\s))
  5412. (head (and (looking-at "\\*+[ \t]+\\(.*\\)")
  5413. (match-string 1)))
  5414. (inactive? (= (char-after pos) ?\[))
  5415. (habit? (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
  5416. (item
  5417. (org-agenda-format-item
  5418. (and inactive? org-agenda-inactive-leader)
  5419. (org-add-props head nil
  5420. 'effort effort
  5421. 'effort-minutes effort-minutes)
  5422. level category tags time-stamp org-ts-regexp habit?)))
  5423. (org-add-props item props
  5424. 'priority (if habit?
  5425. (org-habit-get-priority (org-habit-parse-todo))
  5426. (org-get-priority item))
  5427. 'org-marker (org-agenda-new-marker pos)
  5428. 'org-hd-marker (org-agenda-new-marker)
  5429. 'date date
  5430. 'level level
  5431. 'effort effort 'effort-minutes effort-minutes
  5432. 'ts-date (if repeat (org-agenda--timestamp-to-absolute repeat)
  5433. current)
  5434. 'todo-state todo-state
  5435. 'warntime warntime
  5436. 'type "timestamp")
  5437. (push item timestamp-items))))
  5438. (when org-agenda-skip-additional-timestamps-same-entry
  5439. (outline-next-heading))))
  5440. (nreverse timestamp-items)))
  5441. (defun org-agenda-get-sexps ()
  5442. "Return the sexp information for agenda display."
  5443. (require 'diary-lib)
  5444. (with-no-warnings (defvar date) (defvar entry))
  5445. (let* ((props (list 'face 'org-agenda-calendar-sexp
  5446. 'mouse-face 'highlight
  5447. 'help-echo
  5448. (format "mouse-2 or RET jump to org file %s"
  5449. (abbreviate-file-name buffer-file-name))))
  5450. (regexp "^&?%%(")
  5451. ;; FIXME: Is this `entry' binding intended to be dynamic,
  5452. ;; so as to "hide" any current binding for it?
  5453. marker category extra level ee txt tags entry
  5454. result beg b sexp sexp-entry todo-state warntime inherited-tags
  5455. effort effort-minutes)
  5456. (goto-char (point-min))
  5457. (while (re-search-forward regexp nil t)
  5458. (catch :skip
  5459. ;; We do not run `org-agenda-skip' righ away because every single sexp
  5460. ;; in the buffer is matched here, unlike day-specific search
  5461. ;; in ordinary timestamps. Most of the sexps will not match
  5462. ;; the agenda day and it is quicker to run `org-agenda-skip' only for
  5463. ;; matching sexps later on.
  5464. (setq beg (match-beginning 0))
  5465. (goto-char (1- (match-end 0)))
  5466. (setq b (point))
  5467. (forward-sexp 1)
  5468. (setq sexp (buffer-substring b (point)))
  5469. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  5470. (buffer-substring
  5471. (match-beginning 1)
  5472. (save-excursion
  5473. (goto-char (match-end 1))
  5474. (skip-chars-backward "[:blank:]")
  5475. (point)))
  5476. ""))
  5477. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  5478. (when result
  5479. ;; Only check if entry should be skipped on matching sexps.
  5480. (org-agenda-skip (org-element-at-point))
  5481. (setq marker (org-agenda-new-marker beg)
  5482. level (make-string (org-reduced-level (org-outline-level)) ? )
  5483. category (org-get-category beg)
  5484. effort (save-match-data (or (get-text-property (point) 'effort)
  5485. (org-entry-get (point) org-effort-property)))
  5486. inherited-tags
  5487. (or (eq org-agenda-show-inherited-tags 'always)
  5488. (and (listp org-agenda-show-inherited-tags)
  5489. (memq 'agenda org-agenda-show-inherited-tags))
  5490. (and (eq org-agenda-show-inherited-tags t)
  5491. (or (eq org-agenda-use-tag-inheritance t)
  5492. (memq 'agenda org-agenda-use-tag-inheritance))))
  5493. tags (org-get-tags nil (not inherited-tags))
  5494. todo-state (org-get-todo-state)
  5495. warntime (get-text-property (point) 'org-appt-warntime)
  5496. extra nil)
  5497. (setq effort-minutes (when effort (save-match-data (org-duration-to-minutes effort))))
  5498. (dolist (r (if (stringp result)
  5499. (list result)
  5500. result)) ;; we expect a list here
  5501. (when (and org-agenda-diary-sexp-prefix
  5502. (string-match org-agenda-diary-sexp-prefix r))
  5503. (setq extra (match-string 0 r)
  5504. r (replace-match "" nil nil r)))
  5505. (if (string-match "\\S-" r)
  5506. (setq txt r)
  5507. (setq txt "SEXP entry returned empty string"))
  5508. (setq txt (org-agenda-format-item extra
  5509. (org-add-props txt nil
  5510. 'effort effort
  5511. 'effort-minutes effort-minutes)
  5512. level category tags 'time))
  5513. (org-add-props txt props 'org-marker marker
  5514. 'date date 'todo-state todo-state
  5515. 'effort effort 'effort-minutes effort-minutes
  5516. 'level level 'type "sexp" 'warntime warntime)
  5517. (push txt ee)))))
  5518. (nreverse ee)))
  5519. ;; Calendar sanity: define some functions that are independent of
  5520. ;; `calendar-date-style'.
  5521. (defun org-anniversary (year month day &optional mark)
  5522. "Like `diary-anniversary', but with fixed (ISO) order of arguments."
  5523. (with-no-warnings
  5524. (let ((calendar-date-style 'iso))
  5525. (diary-anniversary year month day mark))))
  5526. (defun org-cyclic (N year month day &optional mark)
  5527. "Like `diary-cyclic', but with fixed (ISO) order of arguments."
  5528. (with-no-warnings
  5529. (let ((calendar-date-style 'iso))
  5530. (diary-cyclic N year month day mark))))
  5531. (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
  5532. "Like `diary-block', but with fixed (ISO) order of arguments."
  5533. (with-no-warnings
  5534. (let ((calendar-date-style 'iso))
  5535. (diary-block Y1 M1 D1 Y2 M2 D2 mark))))
  5536. (defun org-date (year month day &optional mark)
  5537. "Like `diary-date', but with fixed (ISO) order of arguments."
  5538. (with-no-warnings
  5539. (let ((calendar-date-style 'iso))
  5540. (diary-date year month day mark))))
  5541. ;; Define the `org-class' function
  5542. (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
  5543. "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
  5544. DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
  5545. SKIP-WEEKS is any number of ISO weeks in the block period for which the
  5546. item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
  5547. `holidays', then any date that is known by the Emacs calendar to be a
  5548. holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
  5549. then those holidays will be skipped."
  5550. (with-no-warnings (defvar date) (defvar entry))
  5551. (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
  5552. (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
  5553. (d (calendar-absolute-from-gregorian date))
  5554. (h (when skip-weeks (calendar-check-holidays date))))
  5555. (and
  5556. (<= date1 d)
  5557. (<= d date2)
  5558. (= (calendar-day-of-week date) dayname)
  5559. (or (not skip-weeks)
  5560. (progn
  5561. (require 'cal-iso)
  5562. (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
  5563. (not (or (and h (memq 'holidays skip-weeks))
  5564. (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
  5565. entry)))
  5566. (defalias 'org-get-closed #'org-agenda-get-progress)
  5567. (defun org-agenda-get-progress ()
  5568. "Return the logged TODO entries for agenda display."
  5569. (with-no-warnings (defvar date))
  5570. (let* ((props (list 'mouse-face 'highlight
  5571. 'org-not-done-regexp org-not-done-regexp
  5572. 'org-todo-regexp org-todo-regexp
  5573. 'org-complex-heading-regexp org-complex-heading-regexp
  5574. 'help-echo
  5575. (format "mouse-2 or RET jump to org file %s"
  5576. (abbreviate-file-name buffer-file-name))))
  5577. (items (if (consp org-agenda-show-log-scoped)
  5578. org-agenda-show-log-scoped
  5579. (if (eq org-agenda-show-log-scoped 'clockcheck)
  5580. '(clock)
  5581. org-agenda-log-mode-items)))
  5582. (parts
  5583. (delq nil
  5584. (list
  5585. (when (memq 'closed items) (concat "\\<" org-closed-string))
  5586. (when (memq 'clock items) (concat "\\<" org-clock-string))
  5587. (when (memq 'state items)
  5588. (format "- +State \"%s\".*?" org-todo-regexp)))))
  5589. (parts-re (if parts (mapconcat #'identity parts "\\|")
  5590. (error "`org-agenda-log-mode-items' is empty")))
  5591. (regexp (concat
  5592. "\\(" parts-re "\\)"
  5593. " *\\["
  5594. (regexp-quote
  5595. (substring
  5596. (format-time-string
  5597. (car org-time-stamp-formats)
  5598. (org-encode-time ; DATE bound by calendar
  5599. 0 0 0 (nth 1 date) (car date) (nth 2 date)))
  5600. 1 11))))
  5601. (org-agenda-search-headline-for-time nil)
  5602. marker hdmarker priority category level tags closedp type
  5603. statep clockp state ee txt extra timestr rest clocked inherited-tags
  5604. effort effort-minutes)
  5605. (goto-char (point-min))
  5606. (while (re-search-forward regexp nil t)
  5607. (catch :skip
  5608. (org-agenda-skip)
  5609. (setq marker (org-agenda-new-marker (match-beginning 0))
  5610. closedp (equal (match-string 1) org-closed-string)
  5611. statep (equal (string-to-char (match-string 1)) ?-)
  5612. clockp (not (or closedp statep))
  5613. state (and statep (match-string 2))
  5614. category (org-get-category (match-beginning 0))
  5615. timestr (buffer-substring (match-beginning 0) (line-end-position))
  5616. effort (save-match-data (or (get-text-property (point) 'effort)
  5617. (org-entry-get (point) org-effort-property))))
  5618. (setq effort-minutes (when effort (save-match-data (org-duration-to-minutes effort))))
  5619. (when (string-match "\\]" timestr)
  5620. ;; substring should only run to end of time stamp
  5621. (setq rest (substring timestr (match-end 0))
  5622. timestr (substring timestr 0 (match-end 0)))
  5623. (if (and (not closedp) (not statep)
  5624. (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
  5625. rest))
  5626. (progn (setq timestr (concat (substring timestr 0 -1)
  5627. "-" (match-string 1 rest) "]"))
  5628. (setq clocked (match-string 2 rest)))
  5629. (setq clocked "-")))
  5630. (save-excursion
  5631. (setq extra
  5632. (cond
  5633. ((not org-agenda-log-mode-add-notes) nil)
  5634. (statep
  5635. (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
  5636. (match-string 1)))
  5637. (clockp
  5638. (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
  5639. (match-string 1)))))
  5640. (if (not (re-search-backward org-outline-regexp-bol nil t))
  5641. (throw :skip nil)
  5642. (goto-char (match-beginning 0))
  5643. (setq hdmarker (org-agenda-new-marker)
  5644. inherited-tags
  5645. (or (eq org-agenda-show-inherited-tags 'always)
  5646. (and (listp org-agenda-show-inherited-tags)
  5647. (memq 'todo org-agenda-show-inherited-tags))
  5648. (and (eq org-agenda-show-inherited-tags t)
  5649. (or (eq org-agenda-use-tag-inheritance t)
  5650. (memq 'todo org-agenda-use-tag-inheritance))))
  5651. tags (org-get-tags nil (not inherited-tags))
  5652. level (make-string (org-reduced-level (org-outline-level)) ? ))
  5653. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  5654. (setq txt (match-string 1))
  5655. (when extra
  5656. (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
  5657. (setq txt (concat (substring txt 0 (match-beginning 1))
  5658. " - " extra " " (match-string 2 txt)))
  5659. (setq txt (concat txt " - " extra))))
  5660. (setq txt (org-agenda-format-item
  5661. (cond
  5662. (closedp "Closed: ")
  5663. (statep (concat "State: (" state ")"))
  5664. (t (concat "Clocked: (" clocked ")")))
  5665. (org-add-props txt nil
  5666. 'effort effort
  5667. 'effort-minutes effort-minutes)
  5668. level category tags timestr)))
  5669. (setq type (cond (closedp "closed")
  5670. (statep "state")
  5671. (t "clock")))
  5672. (setq priority 100000)
  5673. (org-add-props txt props
  5674. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
  5675. 'priority priority 'level level
  5676. 'effort effort 'effort-minutes effort-minutes
  5677. 'type type 'date date
  5678. 'undone-face 'org-warning 'done-face 'org-agenda-done)
  5679. (push txt ee))
  5680. (goto-char (line-end-position))))
  5681. (nreverse ee)))
  5682. (defun org-agenda-show-clocking-issues ()
  5683. "Add overlays, showing issues with clocking.
  5684. See also the user option `org-agenda-clock-consistency-checks'."
  5685. (interactive)
  5686. (let* ((pl org-agenda-clock-consistency-checks)
  5687. (re (concat "^[ \t]*"
  5688. org-clock-string
  5689. "[ \t]+"
  5690. "\\(\\[.*?\\]\\)" ; group 1 is first stamp
  5691. "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
  5692. (tlstart 0.)
  5693. (tlend 0.)
  5694. (maxtime (org-duration-to-minutes
  5695. (or (plist-get pl :max-duration) "24:00")))
  5696. (mintime (org-duration-to-minutes
  5697. (or (plist-get pl :min-duration) 0)))
  5698. (maxgap (org-duration-to-minutes
  5699. ;; default 30:00 means never complain
  5700. (or (plist-get pl :max-gap) "30:00")))
  5701. (gapok (mapcar #'org-duration-to-minutes
  5702. (plist-get pl :gap-ok-around)))
  5703. (def-face (or (plist-get pl :default-face)
  5704. '((:background "DarkRed") (:foreground "white"))))
  5705. issue face m te ts dt ov)
  5706. (goto-char (point-min))
  5707. (while (re-search-forward " Clocked: +(\\(?:-\\|\\([0-9]+:[0-9]+\\)\\))" nil t)
  5708. (setq issue nil face def-face)
  5709. (catch 'next
  5710. (setq m (org-get-at-bol 'org-marker)
  5711. te nil ts nil)
  5712. (unless (and m (markerp m))
  5713. (setq issue "No valid clock line") (throw 'next t))
  5714. (org-with-point-at m
  5715. (save-excursion
  5716. (goto-char (line-beginning-position))
  5717. (unless (looking-at re)
  5718. (error "No valid Clock line")
  5719. (throw 'next t))
  5720. (unless (match-end 3)
  5721. (setq issue
  5722. (format
  5723. "No end time: (%s)"
  5724. (org-duration-from-minutes
  5725. (floor
  5726. (- (float-time (org-current-time))
  5727. (float-time (org-time-string-to-time (match-string 1))))
  5728. 60)))
  5729. face (or (plist-get pl :no-end-time-face) face))
  5730. (throw 'next t))
  5731. (setq ts (match-string 1)
  5732. te (match-string 3)
  5733. ts (float-time (org-time-string-to-time ts))
  5734. te (float-time (org-time-string-to-time te))
  5735. dt (- te ts))))
  5736. (cond
  5737. ((> dt (* 60 maxtime))
  5738. ;; a very long clocking chunk
  5739. (setq issue (format "Clocking interval is very long: %s"
  5740. (org-duration-from-minutes (floor dt 60)))
  5741. face (or (plist-get pl :long-face) face)))
  5742. ((< dt (* 60 mintime))
  5743. ;; a very short clocking chunk
  5744. (setq issue (format "Clocking interval is very short: %s"
  5745. (org-duration-from-minutes (floor dt 60)))
  5746. face (or (plist-get pl :short-face) face)))
  5747. ((and (> tlend 0) (< ts tlend))
  5748. ;; Two clock entries are overlapping
  5749. (setq issue (format "Clocking overlap: %d minutes"
  5750. (/ (- tlend ts) 60))
  5751. face (or (plist-get pl :overlap-face) face)))
  5752. ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
  5753. ;; There is a gap, lets see if we need to report it
  5754. (unless (org-agenda-check-clock-gap tlend ts gapok)
  5755. (setq issue (format "Clocking gap: %d minutes"
  5756. (/ (- ts tlend) 60))
  5757. face (or (plist-get pl :gap-face) face))))
  5758. (t nil)))
  5759. (setq tlend (or te tlend) tlstart (or ts tlstart))
  5760. (when issue
  5761. ;; OK, there was some issue, add an overlay to show the issue
  5762. (setq ov (make-overlay (line-beginning-position) (line-end-position)))
  5763. (overlay-put ov 'before-string
  5764. (concat
  5765. (org-add-props
  5766. (format "%-43s" (concat " " issue))
  5767. nil
  5768. 'face face)
  5769. "\n"))
  5770. (overlay-put ov 'evaporate t)))))
  5771. (defun org-agenda-check-clock-gap (t1 t2 ok-list)
  5772. "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
  5773. (catch 'exit
  5774. (unless ok-list
  5775. ;; there are no OK times for gaps...
  5776. (throw 'exit nil))
  5777. (when (> (- (/ t2 36000) (/ t1 36000)) 24)
  5778. ;; This is more than 24 hours, so it is OK.
  5779. ;; because we have at least one OK time, that must be in the
  5780. ;; 24 hour interval.
  5781. (throw 'exit t))
  5782. ;; We have a shorter gap.
  5783. ;; Now we have to get the minute of the day when these times are
  5784. (let* ((t1dec (decode-time t1))
  5785. (t2dec (decode-time t2))
  5786. ;; compute the minute on the day
  5787. (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
  5788. (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
  5789. (when (< min2 min1)
  5790. ;; if min2 is smaller than min1, this means it is on the next day.
  5791. ;; Wrap it to after midnight.
  5792. (setq min2 (+ min2 1440)))
  5793. ;; Now check if any of the OK times is in the gap
  5794. (mapc (lambda (x)
  5795. ;; Wrap the time to after midnight if necessary
  5796. (when (< x min1) (setq x (+ x 1440)))
  5797. ;; Check if in interval
  5798. (and (<= min1 x) (>= min2 x) (throw 'exit t)))
  5799. ok-list)
  5800. ;; Nope, this gap is not OK
  5801. nil)))
  5802. (defun org-agenda-get-deadlines (&optional with-hour)
  5803. "Return the deadline information for agenda display.
  5804. When WITH-HOUR is non-nil, only return deadlines with an hour
  5805. specification like [h]h:mm."
  5806. (with-no-warnings (defvar date))
  5807. (let* ((props (list 'mouse-face 'highlight
  5808. 'org-not-done-regexp org-not-done-regexp
  5809. 'org-todo-regexp org-todo-regexp
  5810. 'org-complex-heading-regexp org-complex-heading-regexp
  5811. 'help-echo
  5812. (format "mouse-2 or RET jump to org file %s"
  5813. (abbreviate-file-name buffer-file-name))))
  5814. (regexp (if with-hour
  5815. org-deadline-time-hour-regexp
  5816. org-deadline-time-regexp))
  5817. (today (org-today))
  5818. (today? (org-agenda-today-p date)) ; DATE bound by calendar.
  5819. (current (calendar-absolute-from-gregorian date))
  5820. deadline-items)
  5821. (goto-char (point-min))
  5822. (if (org-element--cache-active-p)
  5823. (org-element-cache-map
  5824. (lambda (el)
  5825. (when (and (org-element-property :deadline el)
  5826. (or (not with-hour)
  5827. (org-element-property
  5828. :hour-start
  5829. (org-element-property :deadline el))
  5830. (org-element-property
  5831. :hour-end
  5832. (org-element-property :deadline el))))
  5833. (goto-char (org-element-property :contents-begin el))
  5834. (catch :skip
  5835. (org-agenda-skip el)
  5836. (let* ((s (substring (org-element-property
  5837. :raw-value
  5838. (org-element-property :deadline el))
  5839. 1 -1))
  5840. (pos (save-excursion
  5841. (goto-char (org-element-property :contents-begin el))
  5842. ;; We intentionally leave NOERROR
  5843. ;; argument in `re-search-forward' nil. If
  5844. ;; the search fails here, something went
  5845. ;; wrong and we are looking at
  5846. ;; non-matching headline.
  5847. (re-search-forward regexp (line-end-position))
  5848. (1- (match-beginning 1))))
  5849. (todo-state (org-element-property :todo-keyword el))
  5850. (done? (eq 'done (org-element-property :todo-type el)))
  5851. (sexp? (eq 'diary
  5852. (org-element-property
  5853. :type (org-element-property :deadline el))))
  5854. ;; DEADLINE is the deadline date for the entry. It is
  5855. ;; either the base date or the last repeat, according
  5856. ;; to `org-agenda-prefer-last-repeat'.
  5857. (deadline
  5858. (cond
  5859. (sexp? (org-agenda--timestamp-to-absolute s current))
  5860. ((or (eq org-agenda-prefer-last-repeat t)
  5861. (member todo-state org-agenda-prefer-last-repeat))
  5862. (org-agenda--timestamp-to-absolute
  5863. s today 'past (current-buffer) pos))
  5864. (t (org-agenda--timestamp-to-absolute s))))
  5865. ;; REPEAT is the future repeat closest from CURRENT,
  5866. ;; according to `org-agenda-show-future-repeats'. If
  5867. ;; the latter is nil, or if the time stamp has no
  5868. ;; repeat part, default to DEADLINE.
  5869. (repeat
  5870. (cond
  5871. (sexp? deadline)
  5872. ((<= current today) deadline)
  5873. ((not org-agenda-show-future-repeats) deadline)
  5874. (t
  5875. (let ((base (if (eq org-agenda-show-future-repeats 'next)
  5876. (1+ today)
  5877. current)))
  5878. (org-agenda--timestamp-to-absolute
  5879. s base 'future (current-buffer) pos)))))
  5880. (diff (- deadline current))
  5881. (suppress-prewarning
  5882. (let ((scheduled
  5883. (and org-agenda-skip-deadline-prewarning-if-scheduled
  5884. (org-element-property
  5885. :raw-value
  5886. (org-element-property :scheduled el)))))
  5887. (cond
  5888. ((not scheduled) nil)
  5889. ;; The current item has a scheduled date, so
  5890. ;; evaluate its prewarning lead time.
  5891. ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
  5892. ;; Use global prewarning-restart lead time.
  5893. org-agenda-skip-deadline-prewarning-if-scheduled)
  5894. ((eq org-agenda-skip-deadline-prewarning-if-scheduled
  5895. 'pre-scheduled)
  5896. ;; Set pre-warning to no earlier than SCHEDULED.
  5897. (min (- deadline
  5898. (org-agenda--timestamp-to-absolute scheduled))
  5899. org-deadline-warning-days))
  5900. ;; Set pre-warning to deadline.
  5901. (t 0))))
  5902. (wdays (or suppress-prewarning (org-get-wdays s))))
  5903. (cond
  5904. ;; Only display deadlines at their base date, at future
  5905. ;; repeat occurrences or in today agenda.
  5906. ((= current deadline) nil)
  5907. ((= current repeat) nil)
  5908. ((not today?) (throw :skip nil))
  5909. ;; Upcoming deadline: display within warning period WDAYS.
  5910. ((> deadline current) (when (> diff wdays) (throw :skip nil)))
  5911. ;; Overdue deadline: warn about it for
  5912. ;; `org-deadline-past-days' duration.
  5913. (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
  5914. ;; Possibly skip done tasks.
  5915. (when (and done?
  5916. (or org-agenda-skip-deadline-if-done
  5917. (/= deadline current)))
  5918. (throw :skip nil))
  5919. (save-excursion
  5920. (goto-char (org-element-property :begin el))
  5921. (let* ((category (org-get-category))
  5922. (effort (save-match-data (or (get-text-property (point) 'effort)
  5923. (org-element-property (intern (concat ":" (upcase org-effort-property))) el))))
  5924. (effort-minutes (when effort (save-match-data (org-duration-to-minutes effort))))
  5925. (level (make-string (org-element-property :level el)
  5926. ?\s))
  5927. (head (save-excursion
  5928. (goto-char (org-element-property :begin el))
  5929. (re-search-forward org-outline-regexp-bol)
  5930. (buffer-substring-no-properties (point) (line-end-position))))
  5931. (inherited-tags
  5932. (or (eq org-agenda-show-inherited-tags 'always)
  5933. (and (listp org-agenda-show-inherited-tags)
  5934. (memq 'agenda org-agenda-show-inherited-tags))
  5935. (and (eq org-agenda-show-inherited-tags t)
  5936. (or (eq org-agenda-use-tag-inheritance t)
  5937. (memq 'agenda
  5938. org-agenda-use-tag-inheritance)))))
  5939. (tags (org-get-tags el (not inherited-tags)))
  5940. (time
  5941. (cond
  5942. ;; No time of day designation if it is only
  5943. ;; a reminder.
  5944. ((and (/= current deadline) (/= current repeat)) nil)
  5945. ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  5946. (concat (substring s (match-beginning 1)) " "))
  5947. (t 'time)))
  5948. (item
  5949. (org-agenda-format-item
  5950. ;; Insert appropriate suffixes before deadlines.
  5951. ;; Those only apply to today agenda.
  5952. (pcase-let ((`(,now ,future ,past)
  5953. org-agenda-deadline-leaders))
  5954. (cond
  5955. ((and today? (< deadline today)) (format past (- diff)))
  5956. ((and today? (> deadline today)) (format future diff))
  5957. (t now)))
  5958. (org-add-props head nil
  5959. 'effort effort
  5960. 'effort-minutes effort-minutes)
  5961. level category tags time))
  5962. (face (org-agenda-deadline-face
  5963. (- 1 (/ (float diff) (max wdays 1)))))
  5964. (upcoming? (and today? (> deadline today)))
  5965. (warntime (get-text-property (point) 'org-appt-warntime)))
  5966. (org-add-props item props
  5967. 'org-marker (org-agenda-new-marker pos)
  5968. 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
  5969. 'warntime warntime
  5970. 'level level
  5971. 'effort effort 'effort-minutes effort-minutes
  5972. 'ts-date deadline
  5973. 'priority
  5974. ;; Adjust priority to today reminders about deadlines.
  5975. ;; Overdue deadlines get the highest priority
  5976. ;; increase, then imminent deadlines and eventually
  5977. ;; more distant deadlines.
  5978. (let ((adjust (if today? (- diff) 0)))
  5979. (+ adjust (org-get-priority item)))
  5980. 'todo-state todo-state
  5981. 'type (if upcoming? "upcoming-deadline" "deadline")
  5982. 'date (if upcoming? date deadline)
  5983. 'face (if done? 'org-agenda-done face)
  5984. 'undone-face face
  5985. 'done-face 'org-agenda-done)
  5986. (push item deadline-items)))))))
  5987. :next-re regexp
  5988. :fail-re regexp
  5989. :narrow t)
  5990. (while (re-search-forward regexp nil t)
  5991. (catch :skip
  5992. (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
  5993. (org-agenda-skip)
  5994. (let* ((s (match-string 1))
  5995. (pos (1- (match-beginning 1)))
  5996. (todo-state (save-match-data (org-get-todo-state)))
  5997. (done? (member todo-state org-done-keywords))
  5998. (sexp? (string-prefix-p "%%" s))
  5999. ;; DEADLINE is the deadline date for the entry. It is
  6000. ;; either the base date or the last repeat, according
  6001. ;; to `org-agenda-prefer-last-repeat'.
  6002. (deadline
  6003. (cond
  6004. (sexp? (org-agenda--timestamp-to-absolute s current))
  6005. ((or (eq org-agenda-prefer-last-repeat t)
  6006. (member todo-state org-agenda-prefer-last-repeat))
  6007. (org-agenda--timestamp-to-absolute
  6008. s today 'past (current-buffer) pos))
  6009. (t (org-agenda--timestamp-to-absolute s))))
  6010. ;; REPEAT is the future repeat closest from CURRENT,
  6011. ;; according to `org-agenda-show-future-repeats'. If
  6012. ;; the latter is nil, or if the time stamp has no
  6013. ;; repeat part, default to DEADLINE.
  6014. (repeat
  6015. (cond
  6016. (sexp? deadline)
  6017. ((<= current today) deadline)
  6018. ((not org-agenda-show-future-repeats) deadline)
  6019. (t
  6020. (let ((base (if (eq org-agenda-show-future-repeats 'next)
  6021. (1+ today)
  6022. current)))
  6023. (org-agenda--timestamp-to-absolute
  6024. s base 'future (current-buffer) pos)))))
  6025. (diff (- deadline current))
  6026. (suppress-prewarning
  6027. (let ((scheduled
  6028. (and org-agenda-skip-deadline-prewarning-if-scheduled
  6029. (org-entry-get nil "SCHEDULED"))))
  6030. (cond
  6031. ((not scheduled) nil)
  6032. ;; The current item has a scheduled date, so
  6033. ;; evaluate its prewarning lead time.
  6034. ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
  6035. ;; Use global prewarning-restart lead time.
  6036. org-agenda-skip-deadline-prewarning-if-scheduled)
  6037. ((eq org-agenda-skip-deadline-prewarning-if-scheduled
  6038. 'pre-scheduled)
  6039. ;; Set pre-warning to no earlier than SCHEDULED.
  6040. (min (- deadline
  6041. (org-agenda--timestamp-to-absolute scheduled))
  6042. org-deadline-warning-days))
  6043. ;; Set pre-warning to deadline.
  6044. (t 0))))
  6045. (wdays (or suppress-prewarning (org-get-wdays s))))
  6046. (cond
  6047. ;; Only display deadlines at their base date, at future
  6048. ;; repeat occurrences or in today agenda.
  6049. ((= current deadline) nil)
  6050. ((= current repeat) nil)
  6051. ((not today?) (throw :skip nil))
  6052. ;; Upcoming deadline: display within warning period WDAYS.
  6053. ((> deadline current) (when (> diff wdays) (throw :skip nil)))
  6054. ;; Overdue deadline: warn about it for
  6055. ;; `org-deadline-past-days' duration.
  6056. (t (when (< org-deadline-past-days (- diff)) (throw :skip nil))))
  6057. ;; Possibly skip done tasks.
  6058. (when (and done?
  6059. (or org-agenda-skip-deadline-if-done
  6060. (/= deadline current)))
  6061. (throw :skip nil))
  6062. (save-excursion
  6063. (re-search-backward "^\\*+[ \t]+" nil t)
  6064. (goto-char (match-end 0))
  6065. (let* ((category (org-get-category))
  6066. (effort (save-match-data (or (get-text-property (point) 'effort)
  6067. (org-entry-get (point) org-effort-property))))
  6068. (effort-minutes (when effort (save-match-data (org-duration-to-minutes effort))))
  6069. (level (make-string (org-reduced-level (org-outline-level))
  6070. ?\s))
  6071. (head (buffer-substring-no-properties
  6072. (point) (line-end-position)))
  6073. (inherited-tags
  6074. (or (eq org-agenda-show-inherited-tags 'always)
  6075. (and (listp org-agenda-show-inherited-tags)
  6076. (memq 'agenda org-agenda-show-inherited-tags))
  6077. (and (eq org-agenda-show-inherited-tags t)
  6078. (or (eq org-agenda-use-tag-inheritance t)
  6079. (memq 'agenda
  6080. org-agenda-use-tag-inheritance)))))
  6081. (tags (org-get-tags nil (not inherited-tags)))
  6082. (time
  6083. (cond
  6084. ;; No time of day designation if it is only
  6085. ;; a reminder.
  6086. ((and (/= current deadline) (/= current repeat)) nil)
  6087. ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  6088. (concat (substring s (match-beginning 1)) " "))
  6089. (t 'time)))
  6090. (item
  6091. (org-agenda-format-item
  6092. ;; Insert appropriate suffixes before deadlines.
  6093. ;; Those only apply to today agenda.
  6094. (pcase-let ((`(,now ,future ,past)
  6095. org-agenda-deadline-leaders))
  6096. (cond
  6097. ((and today? (< deadline today)) (format past (- diff)))
  6098. ((and today? (> deadline today)) (format future diff))
  6099. (t now)))
  6100. (org-add-props head nil
  6101. 'effort effort
  6102. 'effort-minutes effort-minutes)
  6103. level category tags time))
  6104. (face (org-agenda-deadline-face
  6105. (- 1 (/ (float diff) (max wdays 1)))))
  6106. (upcoming? (and today? (> deadline today)))
  6107. (warntime (get-text-property (point) 'org-appt-warntime)))
  6108. (org-add-props item props
  6109. 'org-marker (org-agenda-new-marker pos)
  6110. 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
  6111. 'warntime warntime
  6112. 'level level
  6113. 'effort effort 'effort-minutes effort-minutes
  6114. 'ts-date deadline
  6115. 'priority
  6116. ;; Adjust priority to today reminders about deadlines.
  6117. ;; Overdue deadlines get the highest priority
  6118. ;; increase, then imminent deadlines and eventually
  6119. ;; more distant deadlines.
  6120. (let ((adjust (if today? (- diff) 0)))
  6121. (+ adjust (org-get-priority item)))
  6122. 'todo-state todo-state
  6123. 'type (if upcoming? "upcoming-deadline" "deadline")
  6124. 'date (if upcoming? date deadline)
  6125. 'face (if done? 'org-agenda-done face)
  6126. 'undone-face face
  6127. 'done-face 'org-agenda-done)
  6128. (push item deadline-items)))))))
  6129. (nreverse deadline-items)))
  6130. (defun org-agenda-deadline-face (fraction)
  6131. "Return the face to displaying a deadline item.
  6132. FRACTION is what fraction of the head-warning time has passed."
  6133. (assoc-default fraction org-agenda-deadline-faces #'<=))
  6134. (defun org-agenda-get-scheduled (&optional deadlines with-hour)
  6135. "Return the scheduled information for agenda display.
  6136. Optional argument DEADLINES is a list of deadline items to be
  6137. displayed in agenda view. When WITH-HOUR is non-nil, only return
  6138. scheduled items with an hour specification like [h]h:mm."
  6139. (with-no-warnings (defvar date))
  6140. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  6141. 'org-todo-regexp org-todo-regexp
  6142. 'org-complex-heading-regexp org-complex-heading-regexp
  6143. 'done-face 'org-agenda-done
  6144. 'mouse-face 'highlight
  6145. 'help-echo
  6146. (format "mouse-2 or RET jump to Org file %s"
  6147. (abbreviate-file-name buffer-file-name))))
  6148. (regexp (if with-hour
  6149. org-scheduled-time-hour-regexp
  6150. org-scheduled-time-regexp))
  6151. (today (org-today))
  6152. (todayp (org-agenda-today-p date)) ; DATE bound by calendar.
  6153. (current (calendar-absolute-from-gregorian date))
  6154. (deadline-pos
  6155. (mapcar (lambda (d)
  6156. (let ((m (get-text-property 0 'org-hd-marker d)))
  6157. (and m (marker-position m))))
  6158. deadlines))
  6159. scheduled-items)
  6160. (goto-char (point-min))
  6161. (if (org-element--cache-active-p)
  6162. (org-element-cache-map
  6163. (lambda (el)
  6164. (when (and (org-element-property :scheduled el)
  6165. (or (not with-hour)
  6166. (org-element-property
  6167. :hour-start
  6168. (org-element-property :scheduled el))
  6169. (org-element-property
  6170. :hour-end
  6171. (org-element-property :scheduled el))))
  6172. (goto-char (org-element-property :contents-begin el))
  6173. (catch :skip
  6174. (org-agenda-skip el)
  6175. (let* ((s (substring (org-element-property
  6176. :raw-value
  6177. (org-element-property :scheduled el))
  6178. 1 -1))
  6179. (pos (save-excursion
  6180. (goto-char (org-element-property :contents-begin el))
  6181. ;; We intentionally leave NOERROR
  6182. ;; argument in `re-search-forward' nil. If
  6183. ;; the search fails here, something went
  6184. ;; wrong and we are looking at
  6185. ;; non-matching headline.
  6186. (re-search-forward regexp (line-end-position))
  6187. (1- (match-beginning 1))))
  6188. (todo-state (org-element-property :todo-keyword el))
  6189. (donep (eq 'done (org-element-property :todo-type el)))
  6190. (sexp? (eq 'diary
  6191. (org-element-property
  6192. :type (org-element-property :scheduled el))))
  6193. ;; SCHEDULE is the scheduled date for the entry. It is
  6194. ;; either the bare date or the last repeat, according
  6195. ;; to `org-agenda-prefer-last-repeat'.
  6196. (schedule
  6197. (cond
  6198. (sexp? (org-agenda--timestamp-to-absolute s current))
  6199. ((or (eq org-agenda-prefer-last-repeat t)
  6200. (member todo-state org-agenda-prefer-last-repeat))
  6201. (org-agenda--timestamp-to-absolute
  6202. s today 'past (current-buffer) pos))
  6203. (t (org-agenda--timestamp-to-absolute s))))
  6204. ;; REPEAT is the future repeat closest from CURRENT,
  6205. ;; according to `org-agenda-show-future-repeats'. If
  6206. ;; the latter is nil, or if the time stamp has no
  6207. ;; repeat part, default to SCHEDULE.
  6208. (repeat
  6209. (cond
  6210. (sexp? schedule)
  6211. ((<= current today) schedule)
  6212. ((not org-agenda-show-future-repeats) schedule)
  6213. (t
  6214. (let ((base (if (eq org-agenda-show-future-repeats 'next)
  6215. (1+ today)
  6216. current)))
  6217. (org-agenda--timestamp-to-absolute
  6218. s base 'future (current-buffer) pos)))))
  6219. (diff (- current schedule))
  6220. (warntime (get-text-property (point) 'org-appt-warntime))
  6221. (pastschedp (< schedule today))
  6222. (futureschedp (> schedule today))
  6223. (habitp (and (fboundp 'org-is-habit-p)
  6224. (string= "habit" (org-element-property :STYLE el))))
  6225. (suppress-delay
  6226. (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
  6227. (org-element-property
  6228. :raw-value
  6229. (org-element-property :deadline el)))))
  6230. (cond
  6231. ((not deadline) nil)
  6232. ;; The current item has a deadline date, so
  6233. ;; evaluate its delay time.
  6234. ((integerp org-agenda-skip-scheduled-delay-if-deadline)
  6235. ;; Use global delay time.
  6236. (- org-agenda-skip-scheduled-delay-if-deadline))
  6237. ((eq org-agenda-skip-scheduled-delay-if-deadline
  6238. 'post-deadline)
  6239. ;; Set delay to no later than DEADLINE.
  6240. (min (- schedule
  6241. (org-agenda--timestamp-to-absolute deadline))
  6242. org-scheduled-delay-days))
  6243. (t 0))))
  6244. (ddays
  6245. (cond
  6246. ;; Nullify delay when a repeater triggered already
  6247. ;; and the delay is of the form --Xd.
  6248. ((and (string-match-p "--[0-9]+[hdwmy]" s)
  6249. (> schedule (org-agenda--timestamp-to-absolute s)))
  6250. 0)
  6251. (suppress-delay
  6252. (let ((org-scheduled-delay-days suppress-delay))
  6253. (org-get-wdays s t t)))
  6254. (t (org-get-wdays s t)))))
  6255. ;; Display scheduled items at base date (SCHEDULE), today if
  6256. ;; scheduled before the current date, and at any repeat past
  6257. ;; today. However, skip delayed items and items that have
  6258. ;; been displayed for more than `org-scheduled-past-days'.
  6259. (unless (and todayp
  6260. habitp
  6261. (bound-and-true-p org-habit-show-all-today))
  6262. (when (or (and (> ddays 0) (< diff ddays))
  6263. (> diff (or (and habitp org-habit-scheduled-past-days)
  6264. org-scheduled-past-days))
  6265. (> schedule current)
  6266. (and (/= current schedule)
  6267. (/= current today)
  6268. (/= current repeat)))
  6269. (throw :skip nil)))
  6270. ;; Possibly skip done tasks.
  6271. (when (and donep
  6272. (or org-agenda-skip-scheduled-if-done
  6273. (/= schedule current)))
  6274. (throw :skip nil))
  6275. ;; Skip entry if it already appears as a deadline, per
  6276. ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
  6277. ;; doesn't apply to habits.
  6278. (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
  6279. ((guard
  6280. (or (not (memq (line-beginning-position 0) deadline-pos))
  6281. habitp))
  6282. nil)
  6283. (`repeated-after-deadline
  6284. (let ((deadline (time-to-days
  6285. (when (org-element-property :deadline el)
  6286. (org-time-string-to-time
  6287. (org-element-property :deadline el))))))
  6288. (and (<= schedule deadline) (> current deadline))))
  6289. (`not-today pastschedp)
  6290. (`t t)
  6291. (_ nil))
  6292. (throw :skip nil))
  6293. ;; Skip habits if `org-habit-show-habits' is nil, or if we
  6294. ;; only show them for today. Also skip done habits.
  6295. (when (and habitp
  6296. (or donep
  6297. (not (bound-and-true-p org-habit-show-habits))
  6298. (and (not todayp)
  6299. (bound-and-true-p
  6300. org-habit-show-habits-only-for-today))))
  6301. (throw :skip nil))
  6302. (save-excursion
  6303. (goto-char (org-element-property :begin el))
  6304. (let* ((category (org-get-category))
  6305. (effort (save-match-data
  6306. (or (get-text-property (point) 'effort)
  6307. (org-element-property (intern (concat ":" (upcase org-effort-property))) el))))
  6308. (effort-minutes (when effort (save-match-data (org-duration-to-minutes effort))))
  6309. (inherited-tags
  6310. (or (eq org-agenda-show-inherited-tags 'always)
  6311. (and (listp org-agenda-show-inherited-tags)
  6312. (memq 'agenda org-agenda-show-inherited-tags))
  6313. (and (eq org-agenda-show-inherited-tags t)
  6314. (or (eq org-agenda-use-tag-inheritance t)
  6315. (memq 'agenda
  6316. org-agenda-use-tag-inheritance)))))
  6317. (tags (org-get-tags el (not inherited-tags)))
  6318. (level (make-string (org-element-property :level el)
  6319. ?\s))
  6320. (head (save-excursion
  6321. (goto-char (org-element-property :begin el))
  6322. (re-search-forward org-outline-regexp-bol)
  6323. (buffer-substring (point) (line-end-position))))
  6324. (time
  6325. (cond
  6326. ;; No time of day designation if it is only a
  6327. ;; reminder, except for habits, which always show
  6328. ;; the time of day. Habits are an exception
  6329. ;; because if there is a time of day, that is
  6330. ;; interpreted to mean they should usually happen
  6331. ;; then, even if doing the habit was missed.
  6332. ((and
  6333. (not habitp)
  6334. (/= current schedule)
  6335. (/= current repeat))
  6336. nil)
  6337. ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  6338. (concat (substring s (match-beginning 1)) " "))
  6339. (t 'time)))
  6340. (item
  6341. (org-agenda-format-item
  6342. (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
  6343. ;; Show a reminder of a past scheduled today.
  6344. (if (and todayp pastschedp)
  6345. (format past diff)
  6346. first))
  6347. (org-add-props head nil
  6348. 'effort effort
  6349. 'effort-minutes effort-minutes)
  6350. level category tags time nil habitp))
  6351. (face (cond ((and (not habitp) pastschedp)
  6352. 'org-scheduled-previously)
  6353. ((and habitp futureschedp)
  6354. 'org-agenda-done)
  6355. (todayp 'org-scheduled-today)
  6356. (t 'org-scheduled)))
  6357. (habitp (and habitp (org-habit-parse-todo (org-element-property :begin el)))))
  6358. (org-add-props item props
  6359. 'undone-face face
  6360. 'face (if donep 'org-agenda-done face)
  6361. 'org-marker (org-agenda-new-marker pos)
  6362. 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
  6363. 'type (if pastschedp "past-scheduled" "scheduled")
  6364. 'date (if pastschedp schedule date)
  6365. 'ts-date schedule
  6366. 'warntime warntime
  6367. 'level level
  6368. 'effort effort 'effort-minutes effort-minutes
  6369. 'priority (if habitp (org-habit-get-priority habitp)
  6370. (+ 99 diff (org-get-priority item)))
  6371. 'org-habit-p habitp
  6372. 'todo-state todo-state)
  6373. (push item scheduled-items)))))))
  6374. :next-re regexp
  6375. :fail-re regexp
  6376. :narrow t)
  6377. (while (re-search-forward regexp nil t)
  6378. (catch :skip
  6379. (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
  6380. (org-agenda-skip)
  6381. (let* ((s (match-string 1))
  6382. (pos (1- (match-beginning 1)))
  6383. (todo-state (save-match-data (org-get-todo-state)))
  6384. (donep (member todo-state org-done-keywords))
  6385. (sexp? (string-prefix-p "%%" s))
  6386. ;; SCHEDULE is the scheduled date for the entry. It is
  6387. ;; either the bare date or the last repeat, according
  6388. ;; to `org-agenda-prefer-last-repeat'.
  6389. (schedule
  6390. (cond
  6391. (sexp? (org-agenda--timestamp-to-absolute s current))
  6392. ((or (eq org-agenda-prefer-last-repeat t)
  6393. (member todo-state org-agenda-prefer-last-repeat))
  6394. (org-agenda--timestamp-to-absolute
  6395. s today 'past (current-buffer) pos))
  6396. (t (org-agenda--timestamp-to-absolute s))))
  6397. ;; REPEAT is the future repeat closest from CURRENT,
  6398. ;; according to `org-agenda-show-future-repeats'. If
  6399. ;; the latter is nil, or if the time stamp has no
  6400. ;; repeat part, default to SCHEDULE.
  6401. (repeat
  6402. (cond
  6403. (sexp? schedule)
  6404. ((<= current today) schedule)
  6405. ((not org-agenda-show-future-repeats) schedule)
  6406. (t
  6407. (let ((base (if (eq org-agenda-show-future-repeats 'next)
  6408. (1+ today)
  6409. current)))
  6410. (org-agenda--timestamp-to-absolute
  6411. s base 'future (current-buffer) pos)))))
  6412. (diff (- current schedule))
  6413. (warntime (get-text-property (point) 'org-appt-warntime))
  6414. (pastschedp (< schedule today))
  6415. (futureschedp (> schedule today))
  6416. (habitp (and (fboundp 'org-is-habit-p) (org-is-habit-p)))
  6417. (suppress-delay
  6418. (let ((deadline (and org-agenda-skip-scheduled-delay-if-deadline
  6419. (org-entry-get nil "DEADLINE"))))
  6420. (cond
  6421. ((not deadline) nil)
  6422. ;; The current item has a deadline date, so
  6423. ;; evaluate its delay time.
  6424. ((integerp org-agenda-skip-scheduled-delay-if-deadline)
  6425. ;; Use global delay time.
  6426. (- org-agenda-skip-scheduled-delay-if-deadline))
  6427. ((eq org-agenda-skip-scheduled-delay-if-deadline
  6428. 'post-deadline)
  6429. ;; Set delay to no later than DEADLINE.
  6430. (min (- schedule
  6431. (org-agenda--timestamp-to-absolute deadline))
  6432. org-scheduled-delay-days))
  6433. (t 0))))
  6434. (ddays
  6435. (cond
  6436. ;; Nullify delay when a repeater triggered already
  6437. ;; and the delay is of the form --Xd.
  6438. ((and (string-match-p "--[0-9]+[hdwmy]" s)
  6439. (> schedule (org-agenda--timestamp-to-absolute s)))
  6440. 0)
  6441. (suppress-delay
  6442. (let ((org-scheduled-delay-days suppress-delay))
  6443. (org-get-wdays s t t)))
  6444. (t (org-get-wdays s t)))))
  6445. ;; Display scheduled items at base date (SCHEDULE), today if
  6446. ;; scheduled before the current date, and at any repeat past
  6447. ;; today. However, skip delayed items and items that have
  6448. ;; been displayed for more than `org-scheduled-past-days'.
  6449. (unless (and todayp
  6450. habitp
  6451. (bound-and-true-p org-habit-show-all-today))
  6452. (when (or (and (> ddays 0) (< diff ddays))
  6453. (> diff (or (and habitp org-habit-scheduled-past-days)
  6454. org-scheduled-past-days))
  6455. (> schedule current)
  6456. (and (/= current schedule)
  6457. (/= current today)
  6458. (/= current repeat)))
  6459. (throw :skip nil)))
  6460. ;; Possibly skip done tasks.
  6461. (when (and donep
  6462. (or org-agenda-skip-scheduled-if-done
  6463. (/= schedule current)))
  6464. (throw :skip nil))
  6465. ;; Skip entry if it already appears as a deadline, per
  6466. ;; `org-agenda-skip-scheduled-if-deadline-is-shown'. This
  6467. ;; doesn't apply to habits.
  6468. (when (pcase org-agenda-skip-scheduled-if-deadline-is-shown
  6469. ((guard
  6470. (or (not (memq (line-beginning-position 0) deadline-pos))
  6471. habitp))
  6472. nil)
  6473. (`repeated-after-deadline
  6474. (let ((deadline (time-to-days
  6475. (org-get-deadline-time (point)))))
  6476. (and (<= schedule deadline) (> current deadline))))
  6477. (`not-today pastschedp)
  6478. (`t t)
  6479. (_ nil))
  6480. (throw :skip nil))
  6481. ;; Skip habits if `org-habit-show-habits' is nil, or if we
  6482. ;; only show them for today. Also skip done habits.
  6483. (when (and habitp
  6484. (or donep
  6485. (not (bound-and-true-p org-habit-show-habits))
  6486. (and (not todayp)
  6487. (bound-and-true-p
  6488. org-habit-show-habits-only-for-today))))
  6489. (throw :skip nil))
  6490. (save-excursion
  6491. (re-search-backward "^\\*+[ \t]+" nil t)
  6492. (goto-char (match-end 0))
  6493. (let* ((category (org-get-category))
  6494. (effort (save-match-data (or (get-text-property (point) 'effort)
  6495. (org-entry-get (point) org-effort-property))))
  6496. (effort-minutes (when effort (save-match-data (org-duration-to-minutes effort))))
  6497. (inherited-tags
  6498. (or (eq org-agenda-show-inherited-tags 'always)
  6499. (and (listp org-agenda-show-inherited-tags)
  6500. (memq 'agenda org-agenda-show-inherited-tags))
  6501. (and (eq org-agenda-show-inherited-tags t)
  6502. (or (eq org-agenda-use-tag-inheritance t)
  6503. (memq 'agenda
  6504. org-agenda-use-tag-inheritance)))))
  6505. (tags (org-get-tags nil (not inherited-tags)))
  6506. (level (make-string (org-reduced-level (org-outline-level))
  6507. ?\s))
  6508. (head (buffer-substring (point) (line-end-position)))
  6509. (time
  6510. (cond
  6511. ;; No time of day designation if it is only a
  6512. ;; reminder, except for habits, which always show
  6513. ;; the time of day. Habits are an exception
  6514. ;; because if there is a time of day, that is
  6515. ;; interpreted to mean they should usually happen
  6516. ;; then, even if doing the habit was missed.
  6517. ((and
  6518. (not habitp)
  6519. (/= current schedule)
  6520. (/= current repeat))
  6521. nil)
  6522. ((string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  6523. (concat (substring s (match-beginning 1)) " "))
  6524. (t 'time)))
  6525. (item
  6526. (org-agenda-format-item
  6527. (pcase-let ((`(,first ,past) org-agenda-scheduled-leaders))
  6528. ;; Show a reminder of a past scheduled today.
  6529. (if (and todayp pastschedp)
  6530. (format past diff)
  6531. first))
  6532. (org-add-props head nil
  6533. 'effort effort
  6534. 'effort-minutes effort-minutes)
  6535. level category tags time nil habitp))
  6536. (face (cond ((and (not habitp) pastschedp)
  6537. 'org-scheduled-previously)
  6538. ((and habitp futureschedp)
  6539. 'org-agenda-done)
  6540. (todayp 'org-scheduled-today)
  6541. (t 'org-scheduled)))
  6542. (habitp (and habitp (org-habit-parse-todo))))
  6543. (org-add-props item props
  6544. 'undone-face face
  6545. 'face (if donep 'org-agenda-done face)
  6546. 'org-marker (org-agenda-new-marker pos)
  6547. 'org-hd-marker (org-agenda-new-marker (line-beginning-position))
  6548. 'type (if pastschedp "past-scheduled" "scheduled")
  6549. 'date (if pastschedp schedule date)
  6550. 'ts-date schedule
  6551. 'warntime warntime
  6552. 'level level
  6553. 'effort effort 'effort-minutes effort-minutes
  6554. 'priority (if habitp (org-habit-get-priority habitp)
  6555. (+ 99 diff (org-get-priority item)))
  6556. 'org-habit-p habitp
  6557. 'todo-state todo-state)
  6558. (push item scheduled-items)))))))
  6559. (nreverse scheduled-items)))
  6560. (defun org-agenda-get-blocks ()
  6561. "Return the date-range information for agenda display."
  6562. (with-no-warnings (defvar date))
  6563. (let* ((props (list 'face nil
  6564. 'org-not-done-regexp org-not-done-regexp
  6565. 'org-todo-regexp org-todo-regexp
  6566. 'org-complex-heading-regexp org-complex-heading-regexp
  6567. 'mouse-face 'highlight
  6568. 'help-echo
  6569. (format "mouse-2 or RET jump to org file %s"
  6570. (abbreviate-file-name buffer-file-name))))
  6571. (regexp org-tr-regexp)
  6572. (d0 (calendar-absolute-from-gregorian date))
  6573. marker hdmarker ee txt d1 d2 s1 s2 category
  6574. level todo-state tags pos head donep inherited-tags
  6575. effort effort-minutes)
  6576. (goto-char (point-min))
  6577. (while (re-search-forward regexp nil t)
  6578. (catch :skip
  6579. (org-agenda-skip)
  6580. (setq pos (point))
  6581. (let ((start-time (match-string 1))
  6582. (end-time (match-string 2)))
  6583. (setq s1 (match-string 1)
  6584. s2 (match-string 2)
  6585. d1 (time-to-days
  6586. (condition-case err
  6587. (org-time-string-to-time s1)
  6588. (error
  6589. (error
  6590. "Bad timestamp %S at %d in buffer %S\nError was: %s"
  6591. s1
  6592. pos
  6593. (current-buffer)
  6594. (error-message-string err)))))
  6595. d2 (time-to-days
  6596. (condition-case err
  6597. (org-time-string-to-time s2)
  6598. (error
  6599. (error
  6600. "Bad timestamp %S at %d in buffer %S\nError was: %s"
  6601. s2
  6602. pos
  6603. (current-buffer)
  6604. (error-message-string err))))))
  6605. (when (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  6606. ;; Only allow days between the limits, because the normal
  6607. ;; date stamps will catch the limits.
  6608. (save-excursion
  6609. (setq todo-state (org-get-todo-state))
  6610. (setq donep (member todo-state org-done-keywords))
  6611. (when (and donep org-agenda-skip-timestamp-if-done)
  6612. (throw :skip t))
  6613. (setq marker (org-agenda-new-marker (point))
  6614. category (org-get-category))
  6615. (setq effort (save-match-data (or (get-text-property (point) 'effort)
  6616. (org-entry-get (point) org-effort-property))))
  6617. (setq effort-minutes (when effort (save-match-data (org-duration-to-minutes effort))))
  6618. (if (not (re-search-backward org-outline-regexp-bol nil t))
  6619. (throw :skip nil)
  6620. (goto-char (match-beginning 0))
  6621. (setq hdmarker (org-agenda-new-marker (point))
  6622. inherited-tags
  6623. (or (eq org-agenda-show-inherited-tags 'always)
  6624. (and (listp org-agenda-show-inherited-tags)
  6625. (memq 'agenda org-agenda-show-inherited-tags))
  6626. (and (eq org-agenda-show-inherited-tags t)
  6627. (or (eq org-agenda-use-tag-inheritance t)
  6628. (memq 'agenda org-agenda-use-tag-inheritance))))
  6629. tags (org-get-tags nil (not inherited-tags)))
  6630. (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
  6631. (looking-at "\\*+[ \t]+\\(.*\\)")
  6632. (setq head (match-string 1))
  6633. (let ((remove-re
  6634. (if org-agenda-remove-timeranges-from-blocks
  6635. (concat
  6636. "<" (regexp-quote s1) ".*?>"
  6637. "--"
  6638. "<" (regexp-quote s2) ".*?>")
  6639. nil)))
  6640. (setq txt (org-agenda-format-item
  6641. (format
  6642. (nth (if (= d1 d2) 0 1)
  6643. org-agenda-timerange-leaders)
  6644. (1+ (- d0 d1)) (1+ (- d2 d1)))
  6645. (org-add-props head nil
  6646. 'effort effort
  6647. 'effort-minutes effort-minutes)
  6648. level category tags
  6649. (save-match-data
  6650. (let ((hhmm1 (and (string-match org-ts-regexp1 s1)
  6651. (match-string 6 s1)))
  6652. (hhmm2 (and (string-match org-ts-regexp1 s2)
  6653. (match-string 6 s2))))
  6654. (cond ((string= hhmm1 hhmm2)
  6655. (concat "<" start-time ">--<" end-time ">"))
  6656. ((and (= d1 d0) (= d2 d0))
  6657. (concat "<" start-time ">--<" end-time ">"))
  6658. ((= d1 d0)
  6659. (concat "<" start-time ">"))
  6660. ((= d2 d0)
  6661. (concat "<" end-time ">")))))
  6662. remove-re))))
  6663. (org-add-props txt props
  6664. 'org-marker marker 'org-hd-marker hdmarker
  6665. 'type "block" 'date date
  6666. 'level level
  6667. 'effort effort 'effort-minutes effort-minutes
  6668. 'todo-state todo-state
  6669. 'priority (org-get-priority txt))
  6670. (push txt ee))))
  6671. (goto-char pos)))
  6672. ;; Sort the entries by expiration date.
  6673. (nreverse ee)))
  6674. ;;; Agenda presentation and sorting
  6675. (defvar org-prefix-has-time nil
  6676. "A flag, set by `org-compile-prefix-format'.
  6677. The flag is set if the currently compiled format contains a `%t'.")
  6678. (defvar org-prefix-has-tag nil
  6679. "A flag, set by `org-compile-prefix-format'.
  6680. The flag is set if the currently compiled format contains a `%T'.")
  6681. (defvar org-prefix-has-effort nil
  6682. "A flag, set by `org-compile-prefix-format'.
  6683. The flag is set if the currently compiled format contains a `%e'.")
  6684. (defvar org-prefix-has-breadcrumbs nil
  6685. "A flag, set by `org-compile-prefix-format'.
  6686. The flag is set if the currently compiled format contains a `%b'.")
  6687. (defvar org-prefix-category-length nil
  6688. "Used by `org-compile-prefix-format' to remember the category field width.")
  6689. (defvar org-prefix-category-max-length nil
  6690. "Used by `org-compile-prefix-format' to remember the category field width.")
  6691. (defun org-agenda-get-category-icon (category)
  6692. "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
  6693. (cl-dolist (entry org-agenda-category-icon-alist)
  6694. (when (string-match-p (car entry) category)
  6695. (if (listp (cadr entry))
  6696. (cl-return (cadr entry))
  6697. (cl-return (apply #'create-image (cdr entry)))))))
  6698. (defun org-agenda-format-item (extra txt &optional with-level with-category tags dotime
  6699. remove-re habitp)
  6700. "Format TXT to be inserted into the agenda buffer.
  6701. In particular, add the prefix and corresponding text properties.
  6702. EXTRA must be a string to replace the `%s' specifier in the prefix format.
  6703. WITH-LEVEL may be a string to replace the `%l' specifier.
  6704. WITH-CATEGORY (a string, a symbol or nil) may be used to overrule the default
  6705. category taken from local variable or file name. It will replace the `%c'
  6706. specifier in the format.
  6707. DOTIME, when non-nil, indicates that a time-of-day should be extracted from
  6708. TXT for sorting of this entry, and for the `%t' specifier in the format.
  6709. When DOTIME is a string, this string is searched for a time before TXT is.
  6710. TAGS can be the tags of the headline.
  6711. Any match of REMOVE-RE will be removed from TXT."
  6712. ;; We keep the org-prefix-* variable values along with a compiled
  6713. ;; formatter, so that multiple agendas existing at the same time do
  6714. ;; not step on each other toes.
  6715. ;;
  6716. ;; It was inconvenient to make these variables buffer local in
  6717. ;; Agenda buffers, because this function expects to be called with
  6718. ;; the buffer where item comes from being current, and not agenda
  6719. ;; buffer
  6720. (let* ((bindings (car org-prefix-format-compiled))
  6721. (formatter (cadr org-prefix-format-compiled)))
  6722. (cl-loop for (var value) in bindings
  6723. do (set var value))
  6724. (save-match-data
  6725. ;; Diary entries sometimes have extra whitespace at the beginning
  6726. (setq txt (org-trim txt))
  6727. ;; Fix the tags part in txt
  6728. (setq txt (org-agenda-fix-displayed-tags
  6729. txt tags
  6730. org-agenda-show-inherited-tags
  6731. org-agenda-hide-tags-regexp))
  6732. (with-no-warnings
  6733. ;; `time', `tag', `effort' are needed for the eval of the prefix format.
  6734. ;; Based on what I see in `org-compile-prefix-format', I added
  6735. ;; a few more.
  6736. (defvar breadcrumbs) (defvar category) (defvar category-icon)
  6737. (defvar effort) (defvar extra)
  6738. (defvar level) (defvar tag) (defvar time))
  6739. (let* ((category (or with-category
  6740. (if buffer-file-name
  6741. (file-name-sans-extension
  6742. (file-name-nondirectory buffer-file-name))
  6743. "")))
  6744. (category-icon (org-agenda-get-category-icon category))
  6745. (category-icon (if category-icon
  6746. (propertize " " 'display category-icon)
  6747. ""))
  6748. (effort (and (not (string= txt ""))
  6749. (get-text-property 1 'effort txt)))
  6750. (tag (if tags (nth (1- (length tags)) tags) ""))
  6751. (time-grid-trailing-characters (nth 2 org-agenda-time-grid))
  6752. (extra (or (and (not habitp) extra) ""))
  6753. time
  6754. (ts (when dotime (concat
  6755. (if (stringp dotime) dotime "")
  6756. (and org-agenda-search-headline-for-time txt))))
  6757. (time-of-day (and dotime (org-get-time-of-day ts)))
  6758. stamp plain s0 s1 s2 rtn srp l
  6759. duration breadcrumbs)
  6760. (and (derived-mode-p 'org-mode) buffer-file-name
  6761. (add-to-list 'org-agenda-contributing-files buffer-file-name))
  6762. (when (and dotime time-of-day)
  6763. ;; Extract starting and ending time and move them to prefix
  6764. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  6765. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  6766. (setq s0 (match-string 0 ts)
  6767. srp (and stamp (match-end 3))
  6768. s1 (match-string (if plain 1 2) ts)
  6769. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  6770. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  6771. ;; them, we might want to remove them there to avoid duplication.
  6772. ;; The user can turn this off with a variable.
  6773. (when (and org-prefix-has-time
  6774. org-agenda-remove-times-when-in-prefix (or stamp plain)
  6775. (string-match (concat (regexp-quote s0) " *") txt)
  6776. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  6777. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  6778. (= (match-beginning 0) 0)
  6779. t))
  6780. (setq txt (replace-match "" nil nil txt))))
  6781. ;; Normalize the time(s) to 24 hour.
  6782. (when s1 (setq s1 (org-get-time-of-day s1 t)))
  6783. (when s2 (setq s2 (org-get-time-of-day s2 t)))
  6784. ;; Try to set s2 if s1 and
  6785. ;; `org-agenda-default-appointment-duration' are set
  6786. (when (and s1 (not s2) org-agenda-default-appointment-duration)
  6787. (setq s2
  6788. (org-duration-from-minutes
  6789. (+ (org-duration-to-minutes s1 t)
  6790. org-agenda-default-appointment-duration)
  6791. nil t)))
  6792. ;; Compute the duration
  6793. (when s2
  6794. (setq duration (- (org-duration-to-minutes s2)
  6795. (org-duration-to-minutes s1))))
  6796. ;; Format S1 and S2 for display.
  6797. (when s1 (setq s1 (format "%5s" (org-get-time-of-day s1 'overtime))))
  6798. (when s2 (setq s2 (org-get-time-of-day s2 'overtime))))
  6799. (when (string-match org-tag-group-re txt)
  6800. ;; Tags are in the string
  6801. (if (or (eq org-agenda-remove-tags t)
  6802. (and org-agenda-remove-tags
  6803. org-prefix-has-tag))
  6804. (setq txt (replace-match "" t t txt))
  6805. (setq txt (replace-match
  6806. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  6807. (match-string 1 txt))
  6808. t t txt))))
  6809. (when remove-re
  6810. (while (string-match remove-re txt)
  6811. (setq txt (replace-match "" t t txt))))
  6812. ;; Set org-heading property on `txt' to mark the start of the
  6813. ;; heading.
  6814. (add-text-properties 0 (length txt) '(org-heading t) txt)
  6815. ;; Prepare the variables needed in the eval of the compiled format
  6816. (when org-prefix-has-breadcrumbs
  6817. (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
  6818. (let ((s (org-format-outline-path (org-get-outline-path)
  6819. (1- (frame-width))
  6820. nil org-agenda-breadcrumbs-separator)))
  6821. (if (eq "" s) "" (concat s org-agenda-breadcrumbs-separator))))))
  6822. (setq time (cond (s2 (concat
  6823. (org-agenda-time-of-day-to-ampm-maybe s1)
  6824. "-" (org-agenda-time-of-day-to-ampm-maybe s2)
  6825. (when org-agenda-timegrid-use-ampm " ")))
  6826. (s1 (concat
  6827. (org-agenda-time-of-day-to-ampm-maybe s1)
  6828. (if org-agenda-timegrid-use-ampm
  6829. (concat time-grid-trailing-characters " ")
  6830. time-grid-trailing-characters)))
  6831. (t ""))
  6832. category (if (symbolp category) (symbol-name category) category)
  6833. level (or with-level ""))
  6834. (if (string-match org-link-bracket-re category)
  6835. (progn
  6836. (setq l (string-width (or (match-string 2) (match-string 1))))
  6837. (when (< l (or org-prefix-category-length 0))
  6838. (setq category (copy-sequence category))
  6839. (org-add-props category nil
  6840. 'extra-space (make-string
  6841. (- org-prefix-category-length l 1) ?\ ))))
  6842. (when (and org-prefix-category-max-length
  6843. (>= (length category) org-prefix-category-max-length))
  6844. (setq category (substring category 0 (1- org-prefix-category-max-length)))))
  6845. ;; Evaluate the compiled format
  6846. (setq rtn (concat (eval formatter t) txt))
  6847. ;; And finally add the text properties
  6848. (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
  6849. (org-add-props rtn nil
  6850. 'org-category category
  6851. 'tags tags
  6852. 'org-priority-highest org-priority-highest
  6853. 'org-priority-lowest org-priority-lowest
  6854. 'time-of-day time-of-day
  6855. 'duration duration
  6856. 'breadcrumbs breadcrumbs
  6857. 'txt txt
  6858. 'level level
  6859. 'time time
  6860. 'extra extra
  6861. 'format org-prefix-format-compiled
  6862. 'dotime dotime)))))
  6863. (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
  6864. "Remove tags string from TXT, and add a modified list of tags.
  6865. The modified list may contain inherited tags, and tags matched by
  6866. `org-agenda-hide-tags-regexp' will be removed."
  6867. (when (or add-inherited hide-re)
  6868. (when (string-match org-tag-group-re txt)
  6869. (setq txt (substring txt 0 (match-beginning 0))))
  6870. (setq tags
  6871. (delq nil
  6872. (mapcar (lambda (tg)
  6873. (if (or (and hide-re (string-match hide-re tg))
  6874. (and (not add-inherited)
  6875. (get-text-property 0 'inherited tg)))
  6876. nil
  6877. tg))
  6878. tags)))
  6879. (when tags
  6880. (let ((have-i (get-text-property 0 'inherited (car tags)))
  6881. i)
  6882. (setq txt (concat txt " :"
  6883. (mapconcat
  6884. (lambda (x)
  6885. (setq i (get-text-property 0 'inherited x))
  6886. (if (and have-i (not i))
  6887. (progn
  6888. (setq have-i nil)
  6889. (concat ":" x))
  6890. x))
  6891. tags ":")
  6892. (if have-i "::" ":"))))))
  6893. txt)
  6894. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  6895. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  6896. "Add a time-grid for agenda items which need it.
  6897. LIST is the list of agenda items formatted by `org-agenda-list'.
  6898. NDAYS is the span of the current agenda view.
  6899. TODAYP is t when the current agenda view is on today."
  6900. (catch 'exit
  6901. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  6902. ((and todayp (member 'today (car org-agenda-time-grid))))
  6903. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  6904. ((member 'weekly (car org-agenda-time-grid)))
  6905. (t (throw 'exit list)))
  6906. (let* ((have (delq nil (mapcar
  6907. (lambda (x) (get-text-property 1 'time-of-day x))
  6908. list)))
  6909. (string (nth 3 org-agenda-time-grid))
  6910. (gridtimes (nth 1 org-agenda-time-grid))
  6911. (req (car org-agenda-time-grid))
  6912. (remove (member 'remove-match req))
  6913. new time)
  6914. (when (and (member 'require-timed req) (not have))
  6915. ;; don't show empty grid
  6916. (throw 'exit list))
  6917. (while (setq time (pop gridtimes))
  6918. (unless (and remove (member time have))
  6919. (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
  6920. (push (org-agenda-format-item
  6921. nil string nil "" nil
  6922. (concat (substring time 0 -2) ":" (substring time -2)))
  6923. new)
  6924. (put-text-property
  6925. 2 (length (car new)) 'face 'org-time-grid (car new))))
  6926. (when (and todayp org-agenda-show-current-time-in-grid)
  6927. (push (org-agenda-format-item
  6928. nil org-agenda-current-time-string nil "" nil
  6929. (format-time-string "%H:%M "))
  6930. new)
  6931. (put-text-property
  6932. 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
  6933. (if (member 'time-up org-agenda-sorting-strategy-selected)
  6934. (append new list)
  6935. (append list new)))))
  6936. (defun org-compile-prefix-format (key)
  6937. "Compile the prefix format into a Lisp form that can be evaluated.
  6938. KEY is the agenda type (see `org-agenda-prefix-format').
  6939. The resulting form and associated variable bindings is returned
  6940. and stored in the variable `org-prefix-format-compiled'."
  6941. (setq org-prefix-has-time nil
  6942. org-prefix-has-tag nil
  6943. org-prefix-category-length nil
  6944. org-prefix-has-effort nil
  6945. org-prefix-has-breadcrumbs nil)
  6946. (let ((s (cond
  6947. ((stringp org-agenda-prefix-format)
  6948. org-agenda-prefix-format)
  6949. ((assq key org-agenda-prefix-format)
  6950. (cdr (assq key org-agenda-prefix-format)))
  6951. (t " %-12:c%?-12t% s")))
  6952. (start 0)
  6953. varform vars var c f opt) ;; e
  6954. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+?)\\)"
  6955. s start)
  6956. (setq var (or (cdr (assoc (match-string 4 s)
  6957. '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
  6958. ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
  6959. 'eval)
  6960. c (or (match-string 3 s) "")
  6961. opt (match-beginning 1)
  6962. start (1+ (match-beginning 0)))
  6963. (cl-case var
  6964. (time (setq org-prefix-has-time t))
  6965. (tag (setq org-prefix-has-tag t))
  6966. (effort (setq org-prefix-has-effort t))
  6967. (breadcrumbs (setq org-prefix-has-breadcrumbs t)))
  6968. (setq f (concat "%" (match-string 2 s) "s"))
  6969. (when (eq var 'category)
  6970. (setq org-prefix-category-length
  6971. (floor (abs (string-to-number (match-string 2 s)))))
  6972. (setq org-prefix-category-max-length
  6973. (let ((x (match-string 2 s)))
  6974. (save-match-data
  6975. (and (string-match "\\.[0-9]+" x)
  6976. (string-to-number (substring (match-string 0 x) 1)))))))
  6977. (if (eq var 'eval)
  6978. (setq varform `(format ,f (org-eval ,(read (substring s (match-beginning 4))))))
  6979. (if opt
  6980. (setq varform
  6981. `(if (member ,var '("" nil))
  6982. ""
  6983. (format ,f (concat ,var ,c))))
  6984. (setq varform
  6985. `(format ,f (if (member ,var '("" nil)) ""
  6986. (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
  6987. (if (eq var 'eval)
  6988. (setf (substring s (match-beginning 0)
  6989. (+ (match-beginning 4)
  6990. (length (format "%S" (read (substring s (match-beginning 4)))))))
  6991. "%s")
  6992. (setq s (replace-match "%s" t nil s)))
  6993. (push varform vars))
  6994. (setq vars (nreverse vars))
  6995. (with-current-buffer (or org-agenda-buffer (current-buffer))
  6996. (setq org-prefix-format-compiled
  6997. (list
  6998. `((org-prefix-has-time ,org-prefix-has-time)
  6999. (org-prefix-has-tag ,org-prefix-has-tag)
  7000. (org-prefix-category-length ,org-prefix-category-length)
  7001. (org-prefix-has-effort ,org-prefix-has-effort)
  7002. (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
  7003. `(format ,s ,@vars))))))
  7004. (defun org-set-sorting-strategy (key)
  7005. (setq org-agenda-sorting-strategy-selected
  7006. (if (symbolp (car org-agenda-sorting-strategy))
  7007. ;; the old format
  7008. org-agenda-sorting-strategy
  7009. (or (cdr (assq key org-agenda-sorting-strategy))
  7010. (cdr (assq 'agenda org-agenda-sorting-strategy))
  7011. '(time-up category-keep priority-down)))))
  7012. (defun org-get-time-of-day (s &optional string)
  7013. "Check string S for a time of day.
  7014. If found, return it as a military time number between 0 and 2400.
  7015. If not found, return nil.
  7016. The optional STRING argument forces conversion into a 5 character wide string
  7017. HH:MM. When it is `overtime', any time above 24:00 is turned into \"+H:MM\"
  7018. where H:MM is the duration above midnight."
  7019. (let ((case-fold-search t)
  7020. (time-regexp
  7021. (rx word-start
  7022. (group (opt (any "012")) digit) ;group 1: hours
  7023. (or (and ":" (group (any "012345") digit) ;group 2: minutes
  7024. (opt (group (or "am" "pm")))) ;group 3: am/pm
  7025. ;; Special "HHam/pm" case.
  7026. (group-n 3 (or "am" "pm")))
  7027. word-end)))
  7028. (save-match-data
  7029. (when (and (string-match time-regexp s)
  7030. (not (eq 'org-link (get-text-property 1 'face s))))
  7031. (let ((hours
  7032. (let* ((ampm (and (match-end 3) (downcase (match-string 3 s))))
  7033. (am-p (equal ampm "am")))
  7034. (pcase (string-to-number (match-string 1 s))
  7035. ((and (guard (not ampm)) h) h)
  7036. (12 (if am-p 0 12))
  7037. (h (+ h (if am-p 0 12))))))
  7038. (minutes
  7039. (if (match-end 2)
  7040. (string-to-number (match-string 2 s))
  7041. 0)))
  7042. (pcase string
  7043. (`nil (+ minutes (* hours 100)))
  7044. ((and `overtime
  7045. (guard (or (> hours 24)
  7046. (and (= hours 24)
  7047. (> minutes 0)))))
  7048. (format "+%d:%02d" (- hours 24) minutes))
  7049. ((guard org-agenda-time-leading-zero)
  7050. (format "%02d:%02d" hours minutes))
  7051. (_
  7052. (format "%d:%02d" hours minutes))))))))
  7053. (defvar org-agenda-before-sorting-filter-function nil
  7054. "Function to be applied to agenda items prior to sorting.
  7055. Prior to sorting also means just before they are inserted into the agenda.
  7056. To aid sorting, you may revisit the original entries and add more text
  7057. properties which will later be used by the sorting functions.
  7058. The function should take a string argument, an agenda line.
  7059. It has access to the text properties in that line, which contain among
  7060. other things, the property `org-hd-marker' that points to the entry
  7061. where the line comes from. Note that not all lines going into the agenda
  7062. have this property, only most.
  7063. The function should return the modified string. It is probably best
  7064. to ONLY change text properties.
  7065. You can also use this function as a filter, by returning nil for lines
  7066. you don't want to have in the agenda at all. For this application, you
  7067. could bind the variable in the options section of a custom command.")
  7068. (defun org-agenda-finalize-entries (list &optional type)
  7069. "Sort, limit and concatenate the LIST of agenda items.
  7070. The optional argument TYPE tells the agenda type."
  7071. (let ((max-effort (cond ((listp org-agenda-max-effort)
  7072. (cdr (assoc type org-agenda-max-effort)))
  7073. (t org-agenda-max-effort)))
  7074. (max-todo (cond ((listp org-agenda-max-todos)
  7075. (cdr (assoc type org-agenda-max-todos)))
  7076. (t org-agenda-max-todos)))
  7077. (max-tags (cond ((listp org-agenda-max-tags)
  7078. (cdr (assoc type org-agenda-max-tags)))
  7079. (t org-agenda-max-tags)))
  7080. (max-entries (cond ((listp org-agenda-max-entries)
  7081. (cdr (assoc type org-agenda-max-entries)))
  7082. (t org-agenda-max-entries))))
  7083. (when org-agenda-before-sorting-filter-function
  7084. (setq list
  7085. (delq nil
  7086. (mapcar
  7087. org-agenda-before-sorting-filter-function list))))
  7088. (setq list (mapcar #'org-agenda-highlight-todo list)
  7089. list (mapcar #'identity (sort list #'org-entries-lessp)))
  7090. (when max-effort
  7091. (setq list (org-agenda-limit-entries
  7092. list 'effort-minutes max-effort
  7093. (lambda (e) (or e (if org-agenda-sort-noeffort-is-high
  7094. 32767 -1))))))
  7095. (when max-todo
  7096. (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
  7097. (when max-tags
  7098. (setq list (org-agenda-limit-entries list 'tags max-tags)))
  7099. (when max-entries
  7100. (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
  7101. (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
  7102. (setq list (mapcar #'org-agenda--mark-blocked-entry list)))
  7103. (mapconcat #'identity list "\n")))
  7104. (defun org-agenda-limit-entries (list prop limit &optional fn)
  7105. "Limit the number of agenda entries."
  7106. (let ((include (and limit (< limit 0))))
  7107. (if limit
  7108. (let ((fun (or fn (lambda (p) (when p 1))))
  7109. (lim 0))
  7110. (delq nil
  7111. (mapcar
  7112. (lambda (e)
  7113. (let ((pval (funcall
  7114. fun (get-text-property (1- (length e))
  7115. prop e))))
  7116. (when pval (setq lim (+ lim pval)))
  7117. (cond ((and pval (<= lim (abs limit))) e)
  7118. ((and include (not pval)) e))))
  7119. list)))
  7120. list)))
  7121. (defun org-agenda-limit-interactively (remove)
  7122. "In agenda, interactively limit entries to various maximums."
  7123. (interactive "P")
  7124. (if remove
  7125. (progn (setq org-agenda-max-entries nil
  7126. org-agenda-max-todos nil
  7127. org-agenda-max-tags nil
  7128. org-agenda-max-effort nil)
  7129. (org-agenda-redo))
  7130. (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
  7131. (msg (cond ((= max ?E) "How many minutes? ")
  7132. ((= max ?e) "How many entries? ")
  7133. ((= max ?t) "How many TODO entries? ")
  7134. ((= max ?T) "How many tagged entries? ")
  7135. (t (user-error "Wrong input"))))
  7136. (num (string-to-number (read-from-minibuffer msg))))
  7137. (cond ((equal max ?e)
  7138. (let ((org-agenda-max-entries num)) (org-agenda-redo)))
  7139. ((equal max ?t)
  7140. (let ((org-agenda-max-todos num)) (org-agenda-redo)))
  7141. ((equal max ?T)
  7142. (let ((org-agenda-max-tags num)) (org-agenda-redo)))
  7143. ((equal max ?E)
  7144. (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
  7145. (org-agenda-fit-window-to-buffer))
  7146. (defun org-agenda-highlight-todo (x)
  7147. (let ((org-done-keywords org-done-keywords-for-agenda)
  7148. (case-fold-search nil)
  7149. re)
  7150. (if (eq x 'line)
  7151. (save-excursion
  7152. (beginning-of-line 1)
  7153. (setq re (org-get-at-bol 'org-todo-regexp))
  7154. (goto-char (or (text-property-any (line-beginning-position)
  7155. (line-end-position)
  7156. 'org-heading t)
  7157. (point)))
  7158. (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
  7159. (add-text-properties (match-beginning 0) (match-end 1)
  7160. (list 'face (org-get-todo-face 1)))
  7161. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  7162. (delete-region (match-beginning 1) (1- (match-end 0)))
  7163. (goto-char (match-beginning 1))
  7164. (insert (format org-agenda-todo-keyword-format s)))))
  7165. (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
  7166. (setq re (get-text-property 0 'org-todo-regexp x))
  7167. (when (and re
  7168. ;; Test `pl' because if there's no heading content,
  7169. ;; there's no point matching to highlight. Note
  7170. ;; that if we didn't test `pl' first, and there
  7171. ;; happened to be no keyword from `org-todo-regexp'
  7172. ;; on this heading line, then the `equal' comparison
  7173. ;; afterwards would spuriously succeed in the case
  7174. ;; where `pl' is nil -- causing an args-out-of-range
  7175. ;; error when we try to add text properties to text
  7176. ;; that isn't there.
  7177. pl
  7178. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  7179. x pl)
  7180. pl))
  7181. (add-text-properties
  7182. (or (match-end 1) (match-end 0)) (match-end 0)
  7183. (list 'face (org-get-todo-face (match-string 2 x)))
  7184. x)
  7185. (when (match-end 1)
  7186. (setq x
  7187. (concat
  7188. (substring x 0 (match-end 1))
  7189. (unless (string= org-agenda-todo-keyword-format "")
  7190. (format org-agenda-todo-keyword-format
  7191. (match-string 2 x)))
  7192. (unless (string= org-agenda-todo-keyword-format "")
  7193. ;; Remove `display' property as the icon could leak
  7194. ;; on the white space.
  7195. (org-add-props " " (org-plist-delete (text-properties-at 0 x)
  7196. 'display)))
  7197. (substring x (match-end 3)))))))
  7198. x)))
  7199. (defsubst org-cmp-values (a b property)
  7200. "Compare the numeric value of text PROPERTY for string A and B."
  7201. (let ((pa (or (get-text-property (1- (length a)) property a) 0))
  7202. (pb (or (get-text-property (1- (length b)) property b) 0)))
  7203. (cond ((> pa pb) +1)
  7204. ((< pa pb) -1))))
  7205. (defsubst org-cmp-effort (a b)
  7206. "Compare the effort values of string A and B."
  7207. (let* ((def (if org-agenda-sort-noeffort-is-high 32767 -1))
  7208. ;; `effort-minutes' property is not directly accessible from
  7209. ;; the strings, but is stored as a property in `txt'.
  7210. (ea (or (get-text-property
  7211. 0 'effort-minutes (get-text-property 0 'txt a))
  7212. def))
  7213. (eb (or (get-text-property
  7214. 0 'effort-minutes (get-text-property 0 'txt b))
  7215. def)))
  7216. (cond ((> ea eb) +1)
  7217. ((< ea eb) -1))))
  7218. (defsubst org-cmp-category (a b)
  7219. "Compare the string values of categories of strings A and B."
  7220. (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
  7221. (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
  7222. (cond ((string-lessp ca cb) -1)
  7223. ((string-lessp cb ca) +1))))
  7224. (defsubst org-cmp-todo-state (a b)
  7225. "Compare the todo states of strings A and B."
  7226. (let* ((ma (or (get-text-property 1 'org-marker a)
  7227. (get-text-property 1 'org-hd-marker a)))
  7228. (mb (or (get-text-property 1 'org-marker b)
  7229. (get-text-property 1 'org-hd-marker b)))
  7230. (fa (and ma (marker-buffer ma)))
  7231. (fb (and mb (marker-buffer mb)))
  7232. (todo-kwds
  7233. (or (and fa (with-current-buffer fa org-todo-keywords-1))
  7234. (and fb (with-current-buffer fb org-todo-keywords-1))))
  7235. (ta (or (get-text-property 1 'todo-state a) ""))
  7236. (tb (or (get-text-property 1 'todo-state b) ""))
  7237. (la (- (length (member ta todo-kwds))))
  7238. (lb (- (length (member tb todo-kwds))))
  7239. (donepa (member ta org-done-keywords-for-agenda))
  7240. (donepb (member tb org-done-keywords-for-agenda)))
  7241. (cond ((and donepa (not donepb)) -1)
  7242. ((and (not donepa) donepb) +1)
  7243. ((< la lb) -1)
  7244. ((< lb la) +1))))
  7245. (defsubst org-cmp-alpha (a b)
  7246. "Compare the headlines, alphabetically."
  7247. (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
  7248. (plb (text-property-any 0 (length b) 'org-heading t b))
  7249. (ta (and pla (substring a pla)))
  7250. (tb (and plb (substring b plb)))
  7251. (case-fold-search nil))
  7252. (when pla
  7253. (when (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
  7254. "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *")
  7255. ta)
  7256. (setq ta (substring ta (match-end 0))))
  7257. (setq ta (downcase ta)))
  7258. (when plb
  7259. (when (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
  7260. "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *")
  7261. tb)
  7262. (setq tb (substring tb (match-end 0))))
  7263. (setq tb (downcase tb)))
  7264. (cond ((not (or ta tb)) nil)
  7265. ((not ta) +1)
  7266. ((not tb) -1)
  7267. ((string-lessp ta tb) -1)
  7268. ((string-lessp tb ta) +1))))
  7269. (defsubst org-cmp-tag (a b)
  7270. "Compare the string values of the first tags of A and B."
  7271. (let ((ta (car (last (get-text-property 1 'tags a))))
  7272. (tb (car (last (get-text-property 1 'tags b)))))
  7273. (cond ((not (or ta tb)) nil)
  7274. ((not ta) +1)
  7275. ((not tb) -1)
  7276. ((string-lessp ta tb) -1)
  7277. ((string-lessp tb ta) +1))))
  7278. (defsubst org-cmp-time (a b)
  7279. "Compare the time-of-day values of strings A and B."
  7280. (let* ((def (if org-agenda-sort-notime-is-late 9901 -1))
  7281. (ta (or (get-text-property 1 'time-of-day a) def))
  7282. (tb (or (get-text-property 1 'time-of-day b) def)))
  7283. (cond ((< ta tb) -1)
  7284. ((< tb ta) +1))))
  7285. (defsubst org-cmp-ts (a b type)
  7286. "Compare the timestamps values of entries A and B.
  7287. When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
  7288. \"timestamp_ia\", compare within each of these type. When TYPE
  7289. is the empty string, compare all timestamps without respect of
  7290. their type."
  7291. (let* ((def (if org-agenda-sort-notime-is-late 99999999 -1))
  7292. (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
  7293. (get-text-property 1 'ts-date a))
  7294. def))
  7295. (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
  7296. (get-text-property 1 'ts-date b))
  7297. def)))
  7298. (cond ((if ta (and tb (< ta tb)) tb) -1)
  7299. ((if tb (and ta (< tb ta)) ta) +1))))
  7300. (defsubst org-cmp-habit-p (a b)
  7301. "Compare the todo states of strings A and B."
  7302. (let ((ha (get-text-property 1 'org-habit-p a))
  7303. (hb (get-text-property 1 'org-habit-p b)))
  7304. (cond ((and ha (not hb)) -1)
  7305. ((and (not ha) hb) +1))))
  7306. (defun org-entries-lessp (a b)
  7307. "Predicate for sorting agenda entries."
  7308. ;; The following variables will be used when the form is evaluated.
  7309. ;; So even though the compiler complains, keep them.
  7310. (let ((ss org-agenda-sorting-strategy-selected))
  7311. (org-dlet
  7312. ((timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
  7313. (org-cmp-ts a b "")))
  7314. (timestamp-down (if timestamp-up (- timestamp-up) nil))
  7315. (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
  7316. (org-cmp-ts a b "scheduled")))
  7317. (scheduled-down (if scheduled-up (- scheduled-up) nil))
  7318. (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
  7319. (org-cmp-ts a b "deadline")))
  7320. (deadline-down (if deadline-up (- deadline-up) nil))
  7321. (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
  7322. (org-cmp-ts a b "timestamp_ia")))
  7323. (tsia-down (if tsia-up (- tsia-up) nil))
  7324. (ts-up (and (org-em 'ts-up 'ts-down ss)
  7325. (org-cmp-ts a b "timestamp")))
  7326. (ts-down (if ts-up (- ts-up) nil))
  7327. (time-up (and (org-em 'time-up 'time-down ss)
  7328. (org-cmp-time a b)))
  7329. (time-down (if time-up (- time-up) nil))
  7330. (stats-up (and (org-em 'stats-up 'stats-down ss)
  7331. (org-cmp-values a b 'org-stats)))
  7332. (stats-down (if stats-up (- stats-up) nil))
  7333. (priority-up (and (org-em 'priority-up 'priority-down ss)
  7334. (org-cmp-values a b 'priority)))
  7335. (priority-down (if priority-up (- priority-up) nil))
  7336. (effort-up (and (org-em 'effort-up 'effort-down ss)
  7337. (org-cmp-effort a b)))
  7338. (effort-down (if effort-up (- effort-up) nil))
  7339. (category-up (and (or (org-em 'category-up 'category-down ss)
  7340. (memq 'category-keep ss))
  7341. (org-cmp-category a b)))
  7342. (category-down (if category-up (- category-up) nil))
  7343. (category-keep (if category-up +1 nil))
  7344. (tag-up (and (org-em 'tag-up 'tag-down ss)
  7345. (org-cmp-tag a b)))
  7346. (tag-down (if tag-up (- tag-up) nil))
  7347. (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
  7348. (org-cmp-todo-state a b)))
  7349. (todo-state-down (if todo-state-up (- todo-state-up) nil))
  7350. (habit-up (and (org-em 'habit-up 'habit-down ss)
  7351. (org-cmp-habit-p a b)))
  7352. (habit-down (if habit-up (- habit-up) nil))
  7353. (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
  7354. (org-cmp-alpha a b)))
  7355. (alpha-down (if alpha-up (- alpha-up) nil))
  7356. (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
  7357. user-defined-up user-defined-down)
  7358. (when (and need-user-cmp org-agenda-cmp-user-defined
  7359. (functionp org-agenda-cmp-user-defined))
  7360. (setq user-defined-up
  7361. (funcall org-agenda-cmp-user-defined a b)
  7362. user-defined-down (if user-defined-up (- user-defined-up) nil)))
  7363. (cdr (assoc
  7364. (eval (cons 'or org-agenda-sorting-strategy-selected) t)
  7365. '((-1 . t) (1 . nil) (nil . nil)))))))
  7366. ;;; Agenda restriction lock
  7367. (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
  7368. "Overlay to mark the headline to which agenda commands are restricted.")
  7369. (overlay-put org-agenda-restriction-lock-overlay
  7370. 'face 'org-agenda-restriction-lock)
  7371. (overlay-put org-agenda-restriction-lock-overlay
  7372. 'help-echo "Agendas are currently limited to this subtree.")
  7373. (delete-overlay org-agenda-restriction-lock-overlay)
  7374. (defun org-agenda-set-restriction-lock-from-agenda (arg)
  7375. "Set the restriction lock to the agenda item at point from within the agenda.
  7376. When called with a `\\[universal-argument]' prefix, restrict to
  7377. the file which contains the item.
  7378. Argument ARG is the prefix argument."
  7379. (interactive "P")
  7380. (unless (derived-mode-p 'org-agenda-mode)
  7381. (user-error "Not in an Org agenda buffer"))
  7382. (let* ((marker (or (org-get-at-bol 'org-marker)
  7383. (org-agenda-error)))
  7384. (buffer (marker-buffer marker))
  7385. (pos (marker-position marker)))
  7386. (with-current-buffer buffer
  7387. (goto-char pos)
  7388. (org-agenda-set-restriction-lock arg))))
  7389. ;;;###autoload
  7390. (defun org-agenda-set-restriction-lock (&optional type)
  7391. "Set restriction lock for agenda to current subtree or file.
  7392. When in a restricted subtree, remove it.
  7393. The restriction will span over the entire file if TYPE is `file',
  7394. or if TYPE is (4), or if the cursor is before the first headline
  7395. in the file. Otherwise, only apply the restriction to the current
  7396. subtree."
  7397. (interactive "P")
  7398. (if (and org-agenda-overriding-restriction
  7399. (member org-agenda-restriction-lock-overlay
  7400. (overlays-at (point)))
  7401. (equal (overlay-start org-agenda-restriction-lock-overlay)
  7402. (point)))
  7403. (org-agenda-remove-restriction-lock 'noupdate)
  7404. (org-agenda-remove-restriction-lock 'noupdate)
  7405. (and (equal type '(4)) (setq type 'file))
  7406. (setq type (cond
  7407. (type type)
  7408. ((org-at-heading-p) 'subtree)
  7409. ((condition-case nil (org-back-to-heading t) (error nil))
  7410. 'subtree)
  7411. (t 'file)))
  7412. (if (eq type 'subtree)
  7413. (progn
  7414. (setq org-agenda-restrict (current-buffer))
  7415. (setq org-agenda-overriding-restriction 'subtree)
  7416. (put 'org-agenda-files 'org-restrict
  7417. (list (buffer-file-name (buffer-base-buffer))))
  7418. (org-back-to-heading t)
  7419. (move-overlay org-agenda-restriction-lock-overlay
  7420. (point)
  7421. (if org-agenda-restriction-lock-highlight-subtree
  7422. (save-excursion (org-end-of-subtree t t) (point))
  7423. (line-end-position)))
  7424. (move-marker org-agenda-restrict-begin (point))
  7425. (move-marker org-agenda-restrict-end
  7426. (save-excursion (org-end-of-subtree t t)))
  7427. (message "Locking agenda restriction to subtree"))
  7428. (put 'org-agenda-files 'org-restrict
  7429. (list (buffer-file-name (buffer-base-buffer))))
  7430. (setq org-agenda-restrict nil)
  7431. (setq org-agenda-overriding-restriction 'file)
  7432. (move-marker org-agenda-restrict-begin nil)
  7433. (move-marker org-agenda-restrict-end nil)
  7434. (message "Locking agenda restriction to file"))
  7435. (setq current-prefix-arg nil))
  7436. (org-agenda-maybe-redo))
  7437. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  7438. "Remove agenda restriction lock."
  7439. (interactive "P")
  7440. (if (not (or org-agenda-restrict org-agenda-overriding-restriction))
  7441. (message "No agenda restriction to remove.")
  7442. (delete-overlay org-agenda-restriction-lock-overlay)
  7443. (delete-overlay org-speedbar-restriction-lock-overlay)
  7444. (setq org-agenda-overriding-restriction nil)
  7445. (unless org-agenda-keep-restricted-file-list
  7446. ;; There is a request to keep the file list in place
  7447. (put 'org-agenda-files 'org-restrict nil))
  7448. (setq org-agenda-restrict nil)
  7449. (put 'org-agenda-files 'org-restrict nil)
  7450. (move-marker org-agenda-restrict-begin nil)
  7451. (move-marker org-agenda-restrict-end nil)
  7452. (setq current-prefix-arg nil)
  7453. (message "Agenda restriction lock removed")
  7454. (or noupdate (org-agenda-maybe-redo))))
  7455. (defun org-agenda-maybe-redo ()
  7456. "If there is any window showing the agenda view, update it."
  7457. (let ((w (get-buffer-window (or org-agenda-this-buffer-name
  7458. org-agenda-buffer-name)
  7459. t))
  7460. (w0 (selected-window)))
  7461. (when w
  7462. (select-window w)
  7463. (org-agenda-redo)
  7464. (select-window w0)
  7465. (if org-agenda-overriding-restriction
  7466. (message "Agenda view shifted to new %s restriction"
  7467. org-agenda-overriding-restriction)
  7468. (message "Agenda restriction lock removed")))))
  7469. ;;; Agenda commands
  7470. (defun org-agenda-check-type (error &rest types)
  7471. "Check if agenda buffer or component is of allowed type.
  7472. If ERROR is non-nil, throw an error, otherwise just return nil.
  7473. Allowed types are `agenda' `todo' `tags' `search'."
  7474. (cond ((not org-agenda-type)
  7475. (error "No Org agenda currently displayed"))
  7476. ((memq org-agenda-type types) t)
  7477. (error
  7478. (error "Not allowed in '%s'-type agenda buffer or component" org-agenda-type))
  7479. (t nil)))
  7480. (defun org-agenda-Quit ()
  7481. "Exit the agenda, killing the agenda buffer.
  7482. Like `org-agenda-quit', but kill the buffer even when
  7483. `org-agenda-sticky' is non-nil."
  7484. (interactive)
  7485. (org-agenda--quit))
  7486. (defun org-agenda-quit ()
  7487. "Exit the agenda.
  7488. When `org-agenda-sticky' is non-nil, bury the agenda buffer
  7489. instead of killing it.
  7490. When `org-agenda-restore-windows-after-quit' is non-nil, restore
  7491. the pre-agenda window configuration.
  7492. When column view is active, exit column view instead of the
  7493. agenda."
  7494. (interactive)
  7495. (org-agenda--quit org-agenda-sticky))
  7496. (defun org-agenda--quit (&optional bury)
  7497. (if org-agenda-columns-active
  7498. (org-columns-quit)
  7499. (let ((wconf org-agenda-pre-window-conf)
  7500. (buf (current-buffer))
  7501. (org-agenda-last-indirect-window
  7502. (and (eq org-indirect-buffer-display 'other-window)
  7503. org-agenda-last-indirect-buffer
  7504. (get-buffer-window org-agenda-last-indirect-buffer))))
  7505. (cond
  7506. ((eq org-agenda-window-setup 'other-frame)
  7507. (delete-frame))
  7508. ((eq org-agenda-window-setup 'other-tab)
  7509. (if (fboundp 'tab-bar-close-tab)
  7510. (tab-bar-close-tab)
  7511. (user-error "Your version of Emacs does not have tab bar mode support")))
  7512. ((and org-agenda-restore-windows-after-quit
  7513. wconf)
  7514. ;; Maybe restore the pre-agenda window configuration. Reset
  7515. ;; `org-agenda-pre-window-conf' before running
  7516. ;; `set-window-configuration', which loses the current buffer.
  7517. (setq org-agenda-pre-window-conf nil)
  7518. (set-window-configuration wconf))
  7519. (t
  7520. (when org-agenda-last-indirect-window
  7521. (delete-window org-agenda-last-indirect-window))
  7522. (and (not (eq org-agenda-window-setup 'current-window))
  7523. (not (one-window-p))
  7524. (delete-window))))
  7525. (if bury
  7526. ;; Set the agenda buffer as the current buffer instead of
  7527. ;; passing it as an argument to `bury-buffer' so that
  7528. ;; `bury-buffer' removes it from the window.
  7529. (with-current-buffer buf
  7530. (bury-buffer))
  7531. (kill-buffer buf)
  7532. (setq org-agenda-archives-mode nil
  7533. org-agenda-buffer nil)))))
  7534. (defun org-agenda-exit ()
  7535. "Exit the agenda, killing Org buffers loaded by the agenda.
  7536. Like `org-agenda-Quit', but kill any buffers that were created by
  7537. the agenda. Org buffers visited directly by the user will not be
  7538. touched. Also, exit the agenda even if it is in column view."
  7539. (interactive)
  7540. (when org-agenda-columns-active
  7541. (org-columns-quit))
  7542. (org-release-buffers org-agenda-new-buffers)
  7543. (setq org-agenda-new-buffers nil)
  7544. (org-agenda-Quit))
  7545. (defun org-agenda-kill-all-agenda-buffers ()
  7546. "Kill all buffers in `org-agenda-mode'.
  7547. This is used when toggling sticky agendas."
  7548. (interactive)
  7549. (let (blist)
  7550. (dolist (buf (buffer-list))
  7551. (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
  7552. (push buf blist)))
  7553. (mapc #'kill-buffer blist)))
  7554. (defun org-agenda-execute (arg)
  7555. "Execute another agenda command, keeping same window.
  7556. So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
  7557. in the agenda."
  7558. (interactive "P")
  7559. (let ((org-agenda-window-setup 'current-window))
  7560. (org-agenda arg)))
  7561. (defun org-agenda-redo (&optional all)
  7562. "Rebuild possibly ALL agenda view(s) in the current buffer."
  7563. (interactive "P")
  7564. (defvar org-agenda-tag-filter-while-redo) ;FIXME: Where is this var used?
  7565. (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
  7566. (cpa (unless (eq all t) current-prefix-arg))
  7567. (org-agenda-doing-sticky-redo org-agenda-sticky)
  7568. (org-agenda-sticky nil)
  7569. (org-agenda-buffer-name (or org-agenda-this-buffer-name
  7570. org-agenda-buffer-name))
  7571. (org-agenda-keep-modes t)
  7572. (tag-filter org-agenda-tag-filter)
  7573. (tag-preset (assoc-default 'tag org-agenda-filters-preset))
  7574. (top-hl-filter org-agenda-top-headline-filter)
  7575. (cat-filter org-agenda-category-filter)
  7576. (cat-preset (assoc-default 'category org-agenda-filters-preset))
  7577. (re-filter org-agenda-regexp-filter)
  7578. (re-preset (assoc-default 'regexp org-agenda-filters-preset))
  7579. (effort-filter org-agenda-effort-filter)
  7580. (effort-preset (assoc-default 'effort org-agenda-filters-preset))
  7581. (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
  7582. (cols org-agenda-columns-active)
  7583. (line (org-current-line))
  7584. (window-line (- line (org-current-line (window-start))))
  7585. (lprops (get-text-property p 'org-lprops))
  7586. (redo-cmd (get-text-property p 'org-redo-cmd))
  7587. (last-args (get-text-property p 'org-last-args))
  7588. (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
  7589. (org-agenda-overriding-cmd-arguments
  7590. (unless (eq all t)
  7591. (cond ((listp last-args)
  7592. (cons (or cpa (car last-args)) (cdr last-args)))
  7593. ((stringp last-args)
  7594. last-args))))
  7595. (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
  7596. (and cols (org-columns-quit))
  7597. (message "Rebuilding agenda buffer...")
  7598. (if series-redo-cmd
  7599. (eval series-redo-cmd t)
  7600. (cl-progv
  7601. (mapcar #'car lprops)
  7602. (mapcar (lambda (binding) (eval (cadr binding) t)) lprops)
  7603. (eval redo-cmd t))
  7604. (let ((inhibit-read-only t))
  7605. (add-text-properties (point-min) (point-max) `(org-lprops ,lprops))))
  7606. (setq org-agenda-undo-list nil
  7607. org-agenda-pending-undo-list nil
  7608. org-agenda-tag-filter tag-filter
  7609. org-agenda-category-filter cat-filter
  7610. org-agenda-regexp-filter re-filter
  7611. org-agenda-effort-filter effort-filter
  7612. org-agenda-top-headline-filter top-hl-filter)
  7613. (message "Rebuilding agenda buffer...done")
  7614. (let ((tag (or tag-filter tag-preset))
  7615. (cat (or cat-filter cat-preset))
  7616. (effort (or effort-filter effort-preset))
  7617. (re (or re-filter re-preset)))
  7618. (when tag (org-agenda-filter-apply tag 'tag t))
  7619. (when cat (org-agenda-filter-apply cat 'category))
  7620. (when effort (org-agenda-filter-apply effort 'effort))
  7621. (when re (org-agenda-filter-apply re 'regexp)))
  7622. (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
  7623. (and cols (called-interactively-p 'any) (org-agenda-columns))
  7624. (org-goto-line line)
  7625. (when (called-interactively-p 'any) (recenter window-line))))
  7626. (defun org-agenda-redo-all (&optional exhaustive)
  7627. "Rebuild all agenda views in the current buffer.
  7628. With a prefix argument, do so in all agenda buffers."
  7629. (interactive "P")
  7630. (if exhaustive
  7631. (dolist (buffer (buffer-list))
  7632. (with-current-buffer buffer
  7633. (when (derived-mode-p 'org-agenda-mode)
  7634. (org-agenda-redo t))))
  7635. (org-agenda-redo t)))
  7636. (defvar org-global-tags-completion-table nil)
  7637. (defvar org-agenda-filter-form nil)
  7638. (defvar org-agenda-filtered-by-category nil)
  7639. (defsubst org-agenda-get-category ()
  7640. "Return the category of the agenda line."
  7641. (org-get-at-bol 'org-category))
  7642. (defun org-agenda-filter-by-category (strip)
  7643. "Filter lines in the agenda buffer that have a specific category.
  7644. The category is that of the current line.
  7645. With a `\\[universal-argument]' prefix argument, exclude the lines of that category.
  7646. When there is already a category filter in place, this command removes the
  7647. filter."
  7648. (interactive "P")
  7649. (if (and org-agenda-filtered-by-category
  7650. org-agenda-category-filter)
  7651. (org-agenda-filter-show-all-cat)
  7652. (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
  7653. (cond
  7654. ((and cat strip)
  7655. (org-agenda-filter-apply
  7656. (push (concat "-" cat) org-agenda-category-filter) 'category))
  7657. (cat
  7658. (org-agenda-filter-apply
  7659. (setq org-agenda-category-filter
  7660. (list (concat "+" cat)))
  7661. 'category))
  7662. (t (error "No category at point"))))))
  7663. (defun org-find-top-headline (&optional pos)
  7664. "Find the topmost parent headline and return it.
  7665. POS when non-nil is the marker or buffer position to start the
  7666. search from."
  7667. (save-excursion
  7668. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  7669. (when pos (goto-char pos))
  7670. ;; Skip up to the topmost parent.
  7671. (while (org-up-heading-safe))
  7672. (ignore-errors
  7673. (replace-regexp-in-string
  7674. "^\\[[0-9]+/[0-9]+\\] *\\|^\\[%[0-9]+\\] *" ""
  7675. (nth 4 (org-heading-components)))))))
  7676. (defvar org-agenda-filtered-by-top-headline nil)
  7677. (defun org-agenda-filter-by-top-headline (strip)
  7678. "Keep only those lines that are descendants from the same top headline.
  7679. The top headline is that of the current line. With prefix arg STRIP, hide
  7680. all lines of the category at point."
  7681. (interactive "P")
  7682. (if org-agenda-filtered-by-top-headline
  7683. (progn
  7684. (setq org-agenda-filtered-by-top-headline nil
  7685. org-agenda-top-headline-filter nil)
  7686. (org-agenda-filter-show-all-top-filter))
  7687. (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
  7688. (if toph (org-agenda-filter-top-headline-apply toph strip)
  7689. (error "No top-level headline at point")))))
  7690. (defvar org-agenda-regexp-filter nil)
  7691. (defun org-agenda-filter-by-regexp (strip-or-accumulate)
  7692. "Filter agenda entries by a regular expressions.
  7693. You will be prompted for the regular expression, and the agenda
  7694. view will only show entries that are matched by that expression.
  7695. With one `\\[universal-argument]' prefix argument, hide entries matching the regexp.
  7696. When there is already a regexp filter active, this command removed the
  7697. filter. However, with two `\\[universal-argument]' prefix arguments, add a new condition to
  7698. an already existing regexp filter."
  7699. (interactive "P")
  7700. (let* ((strip (equal strip-or-accumulate '(4)))
  7701. (accumulate (equal strip-or-accumulate '(16))))
  7702. (cond
  7703. ((and org-agenda-regexp-filter (not accumulate))
  7704. (org-agenda-filter-show-all-re)
  7705. (message "Regexp filter removed"))
  7706. (t (let ((flt (concat (if strip "-" "+")
  7707. (read-from-minibuffer
  7708. (if strip
  7709. "Hide entries matching regexp: "
  7710. "Narrow to entries matching regexp: ")))))
  7711. (push flt org-agenda-regexp-filter)
  7712. (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))))))
  7713. (defvar org-agenda-effort-filter nil)
  7714. (defun org-agenda-filter-by-effort (strip-or-accumulate)
  7715. "Filter agenda entries by effort.
  7716. With no `\\[universal-argument]' prefix argument, keep entries matching the effort condition.
  7717. With one `\\[universal-argument]' prefix argument, filter out entries matching the condition.
  7718. With two `\\[universal-argument]' prefix arguments, add a second condition to the existing filter.
  7719. This last option is in practice not very useful, but it is available for
  7720. consistency with the other filter commands."
  7721. (interactive "P")
  7722. (let* ((efforts (split-string
  7723. (or (cdr (assoc-string (concat org-effort-property "_ALL")
  7724. org-global-properties
  7725. t))
  7726. "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")))
  7727. ;; XXX: the following handles only up to 10 different
  7728. ;; effort values.
  7729. (allowed-keys (if (null efforts) nil
  7730. (mapcar (lambda (n) (mod n 10)) ;turn 10 into 0
  7731. (number-sequence 1 (length efforts)))))
  7732. (keep (equal strip-or-accumulate '(16)))
  7733. (negative (equal strip-or-accumulate '(4)))
  7734. (current org-agenda-effort-filter)
  7735. (op nil))
  7736. (while (not (memq op '(?< ?> ?= ?_)))
  7737. (setq op (read-char-exclusive
  7738. "Effort operator? (> = or <) or press `_' again to remove filter")))
  7739. ;; Select appropriate duration. Ignore non-digit characters.
  7740. (if (eq op ?_)
  7741. (progn
  7742. (org-agenda-filter-show-all-effort)
  7743. (message "Effort filter removed"))
  7744. (let ((prompt
  7745. (apply #'format
  7746. (concat "Effort %c "
  7747. (mapconcat (lambda (s) (concat "[%d]" s))
  7748. efforts
  7749. " "))
  7750. op allowed-keys))
  7751. (eff -1))
  7752. (while (not (memq eff allowed-keys))
  7753. (message prompt)
  7754. (setq eff (- (read-char-exclusive) 48)))
  7755. (org-agenda-filter-show-all-effort)
  7756. (setq org-agenda-effort-filter
  7757. (append
  7758. (list (concat (if negative "-" "+")
  7759. (char-to-string op)
  7760. ;; Numbering is 1 2 3 ... 9 0, but we want
  7761. ;; 0 1 2 ... 8 9.
  7762. (nth (mod (1- eff) 10) efforts)))
  7763. (if keep current nil)))
  7764. (org-agenda-filter-apply org-agenda-effort-filter 'effort)))))
  7765. (defun org-agenda-filter (&optional strip-or-accumulate)
  7766. "Prompt for a general filter string and apply it to the agenda.
  7767. The string may contain filter elements like
  7768. +category
  7769. +tag
  7770. +<effort > and = are also allowed as effort operators
  7771. +/regexp/
  7772. Instead of `+', `-' is allowed to strip the agenda of matching entries.
  7773. `+' is optional if it is not required to separate two string parts.
  7774. Multiple filter elements can be concatenated without spaces, for example
  7775. +work-John<0:10-/plot/
  7776. selects entries with category `work' and effort estimates below 10 minutes,
  7777. and deselects entries with tag `John' or matching the regexp `plot'.
  7778. During entry of the filter, completion for tags, categories and effort
  7779. values is offered. Since the syntax for categories and tags is identical
  7780. there should be no overlap between categories and tags. If there is, tags
  7781. get priority.
  7782. A single `\\[universal-argument]' prefix arg STRIP-OR-ACCUMULATE will negate the
  7783. entire filter, which can be useful in connection with the prompt history.
  7784. A double `\\[universal-argument] \\[universal-argument]' prefix arg will add the new filter elements to the
  7785. existing ones. A shortcut for this is to add an additional `+' at the
  7786. beginning of the string, like `+-John'.
  7787. With a triple prefix argument, execute the computed filtering defined in
  7788. the variable `org-agenda-auto-exclude-function'."
  7789. (interactive "P")
  7790. (if (equal strip-or-accumulate '(64))
  7791. ;; Execute the auto-exclude action
  7792. (if (not org-agenda-auto-exclude-function)
  7793. (user-error "`org-agenda-auto-exclude-function' is undefined")
  7794. (org-agenda-filter-show-all-tag)
  7795. (setq org-agenda-tag-filter nil)
  7796. (dolist (tag (org-agenda-get-represented-tags))
  7797. (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
  7798. (when modifier
  7799. (push modifier org-agenda-tag-filter))))
  7800. (unless (null org-agenda-tag-filter)
  7801. (org-agenda-filter-apply org-agenda-tag-filter 'tag 'expand)))
  7802. ;; Prompt for a filter and act
  7803. (let* ((tag-list (org-agenda-get-represented-tags))
  7804. (category-list (org-agenda-get-represented-categories))
  7805. (negate (equal strip-or-accumulate '(4)))
  7806. (cf (mapconcat #'identity org-agenda-category-filter ""))
  7807. (tf (mapconcat #'identity org-agenda-tag-filter ""))
  7808. ;; (rpl-fn (lambda (c) (replace-regexp-in-string "^\\+" "" (or (car c) ""))))
  7809. (ef (replace-regexp-in-string "^\\+" "" (or (car org-agenda-effort-filter) "")))
  7810. (rf (replace-regexp-in-string "^\\+" "" (or (car org-agenda-regexp-filter) "")))
  7811. (ff (concat cf tf ef (when (not (equal rf "")) (concat "/" rf "/"))))
  7812. (f-string (completing-read
  7813. (concat
  7814. (if negate "Negative filter" "Filter")
  7815. " [+cat-tag<0:10-/regexp/]: ")
  7816. #'org-agenda-filter-completion-function
  7817. nil nil ff))
  7818. (keep (or (if (string-match "^\\+[+-]" f-string)
  7819. (progn (setq f-string (substring f-string 1)) t))
  7820. (equal strip-or-accumulate '(16))))
  7821. (fc (if keep org-agenda-category-filter))
  7822. (ft (if keep org-agenda-tag-filter))
  7823. (fe (if keep org-agenda-effort-filter))
  7824. (fr (if keep org-agenda-regexp-filter))
  7825. pm s)
  7826. ;; If the filter contains a double-quoted string, replace a
  7827. ;; single hyphen by the arbitrary and temporary string "~~~"
  7828. ;; to disambiguate such hyphens from syntactic ones.
  7829. (setq f-string (replace-regexp-in-string
  7830. "\"\\([^\"]*\\)-\\([^\"]*\\)\"" "\"\\1~~~\\2\"" f-string))
  7831. (while (string-match "^[ \t]*\\([-+]\\)?\\(\\([^-+<>=/ \t]+\\)\\|\\([<>=][0-9:]+\\)\\|\\(/\\([^/]+\\)/?\\)\\)" f-string)
  7832. (setq pm (if (match-beginning 1) (match-string 1 f-string) "+"))
  7833. (when negate
  7834. (setq pm (if (equal pm "+") "-" "+")))
  7835. (cond
  7836. ((match-beginning 3)
  7837. ;; category or tag
  7838. (setq s (replace-regexp-in-string ; Remove the temporary special string.
  7839. "~~~" "-" (match-string 3 f-string)))
  7840. (cond
  7841. ((member s tag-list)
  7842. (org-pushnew-to-end (concat pm s) ft))
  7843. ((member s category-list)
  7844. (org-pushnew-to-end (concat pm ; Remove temporary double quotes.
  7845. (replace-regexp-in-string "\"\\(.*\\)\"" "\\1" s))
  7846. fc))
  7847. (t (message
  7848. "`%s%s' filter ignored because tag/category is not represented"
  7849. pm s))))
  7850. ((match-beginning 4)
  7851. ;; effort
  7852. (org-pushnew-to-end (concat pm (match-string 4 f-string)) fe))
  7853. ((match-beginning 5)
  7854. ;; regexp
  7855. (org-pushnew-to-end (concat pm (match-string 6 f-string)) fr)))
  7856. (setq f-string (substring f-string (match-end 0))))
  7857. (org-agenda-filter-remove-all)
  7858. (and fc (org-agenda-filter-apply
  7859. (setq org-agenda-category-filter fc) 'category))
  7860. (and ft (org-agenda-filter-apply
  7861. (setq org-agenda-tag-filter ft) 'tag 'expand))
  7862. (and fe (org-agenda-filter-apply
  7863. (setq org-agenda-effort-filter fe) 'effort))
  7864. (and fr (org-agenda-filter-apply
  7865. (setq org-agenda-regexp-filter fr) 'regexp))
  7866. (run-hooks 'org-agenda-filter-hook))))
  7867. (defun org-agenda-filter-completion-function (string _predicate &optional flag)
  7868. "Complete a complex filter string.
  7869. FLAG specifies the type of completion operation to perform. This
  7870. function is passed as a collection function to `completing-read',
  7871. which see."
  7872. (let ((completion-ignore-case t) ;tags are case-sensitive
  7873. (confirm (lambda (x) (stringp x)))
  7874. (prefix "")
  7875. (operator "")
  7876. table)
  7877. (when (string-match "^\\(.*\\([-+<>=]\\)\\)\\([^-+<>=]*\\)$" string)
  7878. (setq prefix (match-string 1 string)
  7879. operator (match-string 2 string)
  7880. string (match-string 3 string)))
  7881. (cond
  7882. ((member operator '("+" "-" "" nil))
  7883. (setq table (append (org-agenda-get-represented-categories)
  7884. (org-agenda-get-represented-tags))))
  7885. ((member operator '("<" ">" "="))
  7886. (setq table (split-string
  7887. (or (cdr (assoc-string (concat org-effort-property "_ALL")
  7888. org-global-properties
  7889. t))
  7890. "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00")
  7891. " +")))
  7892. (t (setq table nil)))
  7893. (pcase flag
  7894. (`t (all-completions string table confirm))
  7895. (`lambda (assoc string table)) ;exact match?
  7896. (`nil
  7897. (pcase (try-completion string table confirm)
  7898. ((and completion (pred stringp))
  7899. (concat prefix completion))
  7900. (completion completion)))
  7901. (_ nil))))
  7902. (defun org-agenda-filter-remove-all ()
  7903. "Remove all filters from the current agenda buffer."
  7904. (interactive)
  7905. (when org-agenda-tag-filter
  7906. (org-agenda-filter-show-all-tag))
  7907. (when org-agenda-category-filter
  7908. (org-agenda-filter-show-all-cat))
  7909. (when org-agenda-regexp-filter
  7910. (org-agenda-filter-show-all-re))
  7911. (when org-agenda-top-headline-filter
  7912. (org-agenda-filter-show-all-top-filter))
  7913. (when org-agenda-effort-filter
  7914. (org-agenda-filter-show-all-effort))
  7915. (org-agenda-finalize)
  7916. (when (called-interactively-p 'interactive)
  7917. (message "All agenda filters removed")))
  7918. (defun org-agenda-filter-by-tag (strip-or-accumulate &optional char exclude)
  7919. "Keep only those lines in the agenda buffer that have a specific tag.
  7920. The tag is selected with its fast selection letter, as configured.
  7921. With a `\\[universal-argument]' prefix, apply the filter negatively, stripping all matches.
  7922. With a `\\[universal-argument] \\[universal-argument]' prefix, add the new tag to the existing filter
  7923. instead of replacing it.
  7924. With a `\\[universal-argument] \\[universal-argument] \\[universal-argument]' prefix, filter the literal tag, \
  7925. i.e. don't
  7926. filter on all its group members.
  7927. A Lisp caller can specify CHAR. EXCLUDE means that the new tag
  7928. should be used to exclude the search - the interactive user can
  7929. also press `-' or `+' to switch between filtering and excluding."
  7930. (interactive "P")
  7931. (let* ((alist org-tag-alist-for-agenda)
  7932. (seen-chars nil)
  7933. (tag-chars (mapconcat
  7934. (lambda (x) (if (and (not (symbolp (car x)))
  7935. (cdr x)
  7936. (not (member (cdr x) seen-chars)))
  7937. (progn
  7938. (push (cdr x) seen-chars)
  7939. (char-to-string (cdr x)))
  7940. ""))
  7941. org-tag-alist-for-agenda ""))
  7942. (valid-char-list (append '(?\t ?\r ?\\ ?. ?\s ?q)
  7943. (string-to-list tag-chars)))
  7944. (exclude (or exclude (equal strip-or-accumulate '(4))))
  7945. (accumulate (equal strip-or-accumulate '(16)))
  7946. (expand (not (equal strip-or-accumulate '(64))))
  7947. (inhibit-read-only t)
  7948. (current org-agenda-tag-filter)
  7949. a tag) ;; n
  7950. (unless char
  7951. (while (not (memq char valid-char-list))
  7952. (org-unlogged-message
  7953. "%s by tag%s: [%s ]tag-char [TAB]tag %s[\\]off [q]uit"
  7954. (if exclude "Exclude[+]" "Filter[-]")
  7955. (if expand "" " (no grouptag expand)")
  7956. tag-chars
  7957. (if org-agenda-auto-exclude-function "[RET] " ""))
  7958. (setq char (read-char-exclusive))
  7959. ;; Excluding or filtering down
  7960. (cond ((eq char ?-) (setq exclude t))
  7961. ((eq char ?+) (setq exclude nil)))))
  7962. (when (eq char ?\t)
  7963. (unless (local-variable-p 'org-global-tags-completion-table)
  7964. (setq-local org-global-tags-completion-table
  7965. (org-global-tags-completion-table)))
  7966. (let ((completion-ignore-case t))
  7967. (setq tag (completing-read
  7968. "Tag: " org-global-tags-completion-table nil t))))
  7969. (cond
  7970. ((eq char ?\r)
  7971. (org-agenda-filter-show-all-tag)
  7972. (when org-agenda-auto-exclude-function
  7973. (setq org-agenda-tag-filter nil)
  7974. (dolist (tag (org-agenda-get-represented-tags))
  7975. (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
  7976. (when modifier
  7977. (push modifier org-agenda-tag-filter))))
  7978. (unless (null org-agenda-tag-filter)
  7979. (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
  7980. ((eq char ?\\)
  7981. (org-agenda-filter-show-all-tag)
  7982. (when (assoc-default 'tag org-agenda-filters-preset)
  7983. (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
  7984. ((eq char ?.)
  7985. (setq org-agenda-tag-filter
  7986. (mapcar (lambda(tag) (concat "+" tag))
  7987. (org-get-at-bol 'tags)))
  7988. (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
  7989. ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
  7990. ((or (eq char ?\s)
  7991. (setq a (rassoc char alist))
  7992. (and tag (setq a (cons tag nil))))
  7993. (org-agenda-filter-show-all-tag)
  7994. (setq tag (car a))
  7995. (setq org-agenda-tag-filter
  7996. (cons (concat (if exclude "-" "+") tag)
  7997. (if accumulate current nil)))
  7998. (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
  7999. (t (error "Invalid tag selection character %c" char)))))
  8000. (defun org-agenda-get-represented-categories ()
  8001. "Return a list of all categories used in this agenda buffer."
  8002. (or org-agenda-represented-categories
  8003. (when (derived-mode-p 'org-agenda-mode)
  8004. (let ((pos (point-min)) categories)
  8005. (while (and (< pos (point-max))
  8006. (setq pos (next-single-property-change
  8007. pos 'org-category nil (point-max))))
  8008. (push (get-text-property pos 'org-category) categories))
  8009. (setq org-agenda-represented-categories
  8010. ;; Enclose category names with a hyphen in double
  8011. ;; quotes to process them specially in `org-agenda-filter'.
  8012. (mapcar (lambda (s) (if (string-match-p "-" s) (format "\"%s\"" s) s))
  8013. (nreverse (org-uniquify (delq nil categories)))))))))
  8014. (defvar org-tag-groups-alist-for-agenda)
  8015. (defun org-agenda-get-represented-tags ()
  8016. "Return a list of all tags used in this agenda buffer.
  8017. These will be lower-case, for filtering."
  8018. (or org-agenda-represented-tags
  8019. (when (derived-mode-p 'org-agenda-mode)
  8020. (let ((pos (point-min)) tags-lists tt)
  8021. (while (and (< pos (point-max))
  8022. (setq pos (next-single-property-change
  8023. pos 'tags nil (point-max))))
  8024. (setq tt (get-text-property pos 'tags))
  8025. (if tt (push tt tags-lists)))
  8026. (setq tags-lists
  8027. (nreverse (org-uniquify
  8028. (delq nil (apply #'append tags-lists)))))
  8029. (dolist (tag tags-lists)
  8030. (mapc
  8031. (lambda (group)
  8032. (when (member tag group)
  8033. (push (car group) tags-lists)))
  8034. org-tag-groups-alist-for-agenda))
  8035. (setq org-agenda-represented-tags tags-lists)))))
  8036. (defun org-agenda-filter-make-matcher (filter type &optional expand)
  8037. "Create the form that tests a line for agenda filter.
  8038. Optional argument EXPAND can be used for the TYPE tag and will
  8039. expand the tags in the FILTER if any of the tags in FILTER are
  8040. grouptags."
  8041. (let ((multi-pos-cats
  8042. (and (eq type 'category)
  8043. (string-match-p "\\+.*\\+"
  8044. (mapconcat (lambda (cat) (substring cat 0 1))
  8045. filter ""))))
  8046. f f1)
  8047. (cond
  8048. ;; Tag filter
  8049. ((eq type 'tag)
  8050. (setq filter
  8051. (delete-dups
  8052. (append (assoc-default 'tag org-agenda-filters-preset)
  8053. filter)))
  8054. (dolist (x filter)
  8055. (let ((op (string-to-char x)))
  8056. (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
  8057. (setq x (list x)))
  8058. (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
  8059. (push f1 f))))
  8060. ;; Category filter
  8061. ((eq type 'category)
  8062. (setq filter
  8063. (delete-dups
  8064. (append (assoc-default 'category org-agenda-filters-preset)
  8065. filter)))
  8066. (dolist (x filter)
  8067. (if (equal "-" (substring x 0 1))
  8068. (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
  8069. (setq f1 (list 'equal (substring x 1) 'cat)))
  8070. (push f1 f)))
  8071. ;; Regexp filter
  8072. ((eq type 'regexp)
  8073. (setq filter
  8074. (delete-dups
  8075. (append (assoc-default 'regexp org-agenda-filters-preset)
  8076. filter)))
  8077. (dolist (x filter)
  8078. (if (equal "-" (substring x 0 1))
  8079. (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
  8080. (setq f1 (list 'string-match (substring x 1) 'txt)))
  8081. (push f1 f)))
  8082. ;; Effort filter
  8083. ((eq type 'effort)
  8084. (setq filter
  8085. (delete-dups
  8086. (append (assoc-default 'effort org-agenda-filters-preset)
  8087. filter)))
  8088. (dolist (x filter)
  8089. (push (org-agenda-filter-effort-form x) f))))
  8090. (cons (if multi-pos-cats 'or 'and) (nreverse f))))
  8091. (defun org-agenda-filter-make-matcher-tag-exp (tags op)
  8092. "Return a form associated to tag-expression TAGS.
  8093. Build a form testing a line for agenda filter for
  8094. tag-expressions. OP is an operator of type CHAR that allows the
  8095. function to set the right switches in the returned form."
  8096. (let (form)
  8097. ;; Any of the expressions can match if OP is +, all must match if
  8098. ;; the operator is -.
  8099. (dolist (x tags (cons (if (eq op ?-) 'and 'or) form))
  8100. (let* ((tag (substring x 1))
  8101. (f (cond
  8102. ((string= "" tag) 'tags)
  8103. ((and (string-match-p "\\`{" tag) (string-match-p "}\\'" tag))
  8104. ;; TAG is a regexp.
  8105. (list 'org-match-any-p (substring tag 1 -1) 'tags))
  8106. (t (list 'member tag 'tags)))))
  8107. (push (if (eq op ?-) (list 'not f) f) form)))))
  8108. (defun org-agenda-filter-effort-form (e)
  8109. "Return the form to compare the effort of the current line with what E says.
  8110. E looks like \"+<2:25\"."
  8111. (let (op)
  8112. (setq e (substring e 1))
  8113. (setq op (string-to-char e) e (substring e 1))
  8114. (setq op (cond ((equal op ?<) '<=)
  8115. ((equal op ?>) '>=)
  8116. ((equal op ??) op)
  8117. (t '=)))
  8118. (list 'org-agenda-compare-effort (list 'quote op)
  8119. (org-duration-to-minutes e))))
  8120. (defun org-agenda-compare-effort (op value)
  8121. "Compare the effort of the current line with VALUE, using OP.
  8122. If the line does not have an effort defined, return nil."
  8123. ;; `effort-minutes' property cannot be extracted directly from
  8124. ;; current line but is stored as a property in `txt'.
  8125. (let ((effort (get-text-property 0 'effort-minutes (org-get-at-bol 'txt))))
  8126. (funcall op
  8127. (or effort (if org-agenda-sort-noeffort-is-high 32767 -1))
  8128. value)))
  8129. (defun org-agenda-filter-expand-tags (filter &optional no-operator)
  8130. "Expand group tags in FILTER for the agenda.
  8131. When NO-OPERATOR is non-nil, do not add the + operator to
  8132. returned tags."
  8133. (if org-group-tags
  8134. (let (case-fold-search rtn)
  8135. (mapc
  8136. (lambda (f)
  8137. (let (f0 dir)
  8138. (if (string-match "^\\([+-]\\)\\(.+\\)" f)
  8139. (setq dir (match-string 1 f) f0 (match-string 2 f))
  8140. (setq dir (if no-operator "" "+") f0 f))
  8141. (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
  8142. (org-tags-expand f0 t))
  8143. rtn))))
  8144. filter)
  8145. (reverse rtn))
  8146. filter))
  8147. (defun org-agenda-filter-apply (filter type &optional expand)
  8148. "Set FILTER as the new agenda filter and apply it.
  8149. Optional argument EXPAND can be used for the TYPE tag and will
  8150. expand the tags in the FILTER if any of the tags in FILTER are
  8151. grouptags."
  8152. ;; Deactivate `org-agenda-entry-text-mode' when filtering
  8153. (when org-agenda-entry-text-mode (org-agenda-entry-text-mode))
  8154. (setq org-agenda-filter-form (org-agenda-filter-make-matcher
  8155. filter type expand))
  8156. ;; Only set `org-agenda-filtered-by-category' to t when a unique
  8157. ;; category is used as the filter:
  8158. (setq org-agenda-filtered-by-category
  8159. (and (eq type 'category)
  8160. (not (equal (substring (car filter) 0 1) "-"))))
  8161. (org-agenda-set-mode-name)
  8162. (save-excursion
  8163. (goto-char (point-min))
  8164. (while (not (eobp))
  8165. (when (or (org-get-at-bol 'org-hd-marker)
  8166. (org-get-at-bol 'org-marker))
  8167. (org-dlet
  8168. ((tags (org-get-at-bol 'tags))
  8169. (cat (org-agenda-get-category))
  8170. (txt (or (org-get-at-bol 'txt) "")))
  8171. (unless (eval org-agenda-filter-form t)
  8172. (org-agenda-filter-hide-line type))))
  8173. (beginning-of-line 2)))
  8174. (when (get-char-property (point) 'invisible)
  8175. (ignore-errors (org-agenda-previous-line))))
  8176. (defun org-agenda-filter-top-headline-apply (hl &optional negative)
  8177. "Filter by top headline HL."
  8178. (org-agenda-set-mode-name)
  8179. (save-excursion
  8180. (goto-char (point-min))
  8181. (while (not (eobp))
  8182. (let* ((pos (org-get-at-bol 'org-hd-marker))
  8183. (tophl (and pos (org-find-top-headline pos))))
  8184. (when (and tophl (funcall (if negative 'identity 'not)
  8185. (string= hl tophl)))
  8186. (org-agenda-filter-hide-line 'top-headline)))
  8187. (beginning-of-line 2)))
  8188. (when (get-char-property (point) 'invisible)
  8189. (org-agenda-previous-line))
  8190. (setq org-agenda-top-headline-filter hl
  8191. org-agenda-filtered-by-top-headline t))
  8192. (defun org-agenda-filter-hide-line (type)
  8193. "If current line is TYPE, hide it in the agenda buffer."
  8194. (let* (buffer-invisibility-spec
  8195. (beg (max (point-min) (1- (line-beginning-position))))
  8196. (end (line-end-position)))
  8197. (let ((inhibit-read-only t))
  8198. (add-text-properties
  8199. beg end `(invisible org-filtered org-filter-type ,type)))))
  8200. (defun org-agenda-remove-filter (type)
  8201. "Remove filter of type TYPE from the agenda buffer."
  8202. (interactive)
  8203. (save-excursion
  8204. (goto-char (point-min))
  8205. (let ((inhibit-read-only t) pos)
  8206. (while (setq pos (text-property-any (point) (point-max)
  8207. 'org-filter-type type))
  8208. (goto-char pos)
  8209. (remove-text-properties
  8210. (point) (next-single-property-change (point) 'org-filter-type)
  8211. `(invisible org-filtered org-filter-type ,type))))
  8212. (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
  8213. (setq org-agenda-filter-form nil)
  8214. (org-agenda-set-mode-name)
  8215. (org-agenda-finalize)))
  8216. (defun org-agenda-filter-show-all-tag nil
  8217. (org-agenda-remove-filter 'tag))
  8218. (defun org-agenda-filter-show-all-re nil
  8219. (org-agenda-remove-filter 'regexp))
  8220. (defun org-agenda-filter-show-all-effort nil
  8221. (org-agenda-remove-filter 'effort))
  8222. (defun org-agenda-filter-show-all-cat nil
  8223. (org-agenda-remove-filter 'category))
  8224. (defun org-agenda-filter-show-all-top-filter nil
  8225. (org-agenda-remove-filter 'top-headline))
  8226. (defun org-agenda-manipulate-query-add ()
  8227. "Manipulate the query by adding a search term with positive selection.
  8228. Positive selection means the term must be matched for selection of an entry."
  8229. (interactive)
  8230. (org-agenda-manipulate-query ?\[))
  8231. (defun org-agenda-manipulate-query-subtract ()
  8232. "Manipulate the query by adding a search term with negative selection.
  8233. Negative selection means term must not be matched for selection of an entry."
  8234. (interactive)
  8235. (org-agenda-manipulate-query ?\]))
  8236. (defun org-agenda-manipulate-query-add-re ()
  8237. "Manipulate the query by adding a search regexp with positive selection.
  8238. Positive selection means the regexp must match for selection of an entry."
  8239. (interactive)
  8240. (org-agenda-manipulate-query ?\{))
  8241. (defun org-agenda-manipulate-query-subtract-re ()
  8242. "Manipulate the query by adding a search regexp with negative selection.
  8243. Negative selection means regexp must not match for selection of an entry."
  8244. (interactive)
  8245. (org-agenda-manipulate-query ?\}))
  8246. (defun org-agenda-manipulate-query (char)
  8247. (cond
  8248. ((eq org-agenda-type 'agenda)
  8249. (let ((org-agenda-include-inactive-timestamps t))
  8250. (org-agenda-redo))
  8251. (message "Display now includes inactive timestamps as well"))
  8252. ((eq org-agenda-type 'search)
  8253. (org-add-to-string
  8254. 'org-agenda-query-string
  8255. (if org-agenda-last-search-view-search-was-boolean
  8256. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  8257. (?\{ . " +{}") (?\} . " -{}"))))
  8258. " "))
  8259. (setq org-agenda-redo-command
  8260. (list 'org-search-view
  8261. (car (get-text-property (min (1- (point-max)) (point))
  8262. 'org-last-args))
  8263. org-agenda-query-string
  8264. (+ (length org-agenda-query-string)
  8265. (if (member char '(?\{ ?\})) 0 1))))
  8266. (set-register org-agenda-query-register org-agenda-query-string)
  8267. (let ((org-agenda-overriding-arguments
  8268. (cdr org-agenda-redo-command)))
  8269. (org-agenda-redo)))
  8270. (t (error "Cannot manipulate query for %s-type agenda buffers"
  8271. org-agenda-type))))
  8272. (defun org-add-to-string (var string)
  8273. (set var (concat (symbol-value var) string)))
  8274. (defun org-agenda-goto-date (date)
  8275. "Jump to DATE in the agenda buffer.
  8276. When called interactively, prompt for the date.
  8277. When called from Lisp, DATE should be a date as returned by
  8278. `org-read-date'.
  8279. See also:
  8280. `org-agenda-earlier' (\\[org-agenda-earlier])
  8281. `org-agenda-later' (\\[org-agenda-later])
  8282. `org-agenda-goto-today' (\\[org-agenda-goto-today])"
  8283. (interactive
  8284. (list
  8285. (let ((org-read-date-prefer-future org-agenda-jump-prefer-future))
  8286. (org-read-date))))
  8287. (let* ((day (time-to-days (org-time-string-to-time date)))
  8288. (org-agenda-sticky-orig org-agenda-sticky)
  8289. (org-agenda-buffer-tmp-name (buffer-name))
  8290. (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
  8291. (0-arg (or current-prefix-arg (car args)))
  8292. (2-arg (nth 2 args))
  8293. (with-hour-p (nth 4 org-agenda-redo-command))
  8294. (newcmd (list 'org-agenda-list 0-arg date
  8295. (org-agenda-span-to-ndays
  8296. 2-arg (org-time-string-to-absolute date))
  8297. with-hour-p))
  8298. (newargs (cdr newcmd))
  8299. (inhibit-read-only t)
  8300. org-agenda-sticky)
  8301. (if (not (org-agenda-check-type t 'agenda))
  8302. (error "Not available in non-agenda views")
  8303. (add-text-properties (point-min) (point-max)
  8304. `(org-redo-cmd ,newcmd org-last-args ,newargs))
  8305. (org-agenda-redo)
  8306. (goto-char (point-min))
  8307. (while (not (or (= (or (get-text-property (point) 'day) 0) day)
  8308. (save-excursion (move-beginning-of-line 2) (eobp))))
  8309. (move-beginning-of-line 2))
  8310. (setq org-agenda-sticky org-agenda-sticky-orig
  8311. org-agenda-this-buffer-is-sticky org-agenda-sticky))))
  8312. (defun org-agenda-goto-today ()
  8313. "Go to today's date in the agenda buffer.
  8314. See also:
  8315. `org-agenda-later' (\\[org-agenda-later])
  8316. `org-agenda-earlier' (\\[org-agenda-earlier])
  8317. `org-agenda-goto-date' (\\[org-agenda-goto-date])"
  8318. (interactive)
  8319. (org-agenda-check-type t 'agenda)
  8320. (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
  8321. (curspan (nth 2 args))
  8322. (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  8323. (cond
  8324. (tdpos (goto-char tdpos))
  8325. ((eq org-agenda-type 'agenda)
  8326. (let* ((sd (org-agenda-compute-starting-span
  8327. (org-today) (or curspan org-agenda-span)))
  8328. (org-agenda-overriding-arguments args))
  8329. (setf (nth 1 org-agenda-overriding-arguments) sd)
  8330. (org-agenda-redo)
  8331. (org-agenda-find-same-or-today-or-agenda)))
  8332. (t (error "Cannot find today")))))
  8333. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  8334. (goto-char
  8335. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  8336. (text-property-any (point-min) (point-max) 'org-today t)
  8337. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  8338. (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
  8339. (org-agenda-backward-block))
  8340. (point-min))))
  8341. (defun org-agenda-backward-block ()
  8342. "Move backward by one agenda block."
  8343. (interactive)
  8344. (org-agenda-forward-block 'backward))
  8345. (defun org-agenda-forward-block (&optional backward)
  8346. "Move forward by one agenda block.
  8347. When optional argument BACKWARD is set, go backward."
  8348. (interactive)
  8349. (cond ((not (derived-mode-p 'org-agenda-mode))
  8350. (user-error
  8351. "Cannot execute this command outside of org-agenda-mode buffers"))
  8352. ((looking-at (if backward "\\`" "\\'"))
  8353. (message "Already at the %s block" (if backward "first" "last")))
  8354. (t (let ((_pos (prog1 (point)
  8355. (ignore-errors (if backward (backward-char 1)
  8356. (move-end-of-line 1)))))
  8357. (f (if backward
  8358. #'previous-single-property-change
  8359. #'next-single-property-change))
  8360. moved dest)
  8361. (while (and (setq dest (funcall
  8362. f (point) 'org-agenda-structural-header))
  8363. (not (get-text-property
  8364. (point) 'org-agenda-structural-header)))
  8365. (setq moved t)
  8366. (goto-char dest))
  8367. (if moved (move-beginning-of-line 1)
  8368. (goto-char (if backward (point-min) (point-max)))
  8369. (move-beginning-of-line 1)
  8370. (message "No %s block" (if backward "previous" "further")))))))
  8371. (defun org-agenda-later (arg)
  8372. "Go forward in time by the current span in the agenda buffer.
  8373. With prefix ARG, go forward that many times the current span.
  8374. See also:
  8375. `org-agenda-earlier' (\\[org-agenda-earlier])
  8376. `org-agenda-goto-today' (\\[org-agenda-goto-today])
  8377. `org-agenda-goto-date' (\\[org-agenda-goto-date])"
  8378. (interactive "p")
  8379. (org-agenda-check-type t 'agenda)
  8380. (let* ((wstart (window-start))
  8381. (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
  8382. (span (or (nth 2 args) org-agenda-current-span))
  8383. (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
  8384. (greg (calendar-gregorian-from-absolute sd))
  8385. (cnt (org-get-at-bol 'org-day-cnt))
  8386. greg2)
  8387. (cond
  8388. ((numberp span)
  8389. (setq sd (+ (* span arg) sd)))
  8390. ((eq span 'day)
  8391. (setq sd (+ arg sd)))
  8392. ((eq span 'week)
  8393. (setq sd (+ (* 7 arg) sd)))
  8394. ((eq span 'fortnight)
  8395. (setq sd (+ (* 14 arg) sd)))
  8396. ((eq span 'month)
  8397. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  8398. sd (calendar-absolute-from-gregorian greg2))
  8399. (setcar greg2 (1+ (car greg2))))
  8400. ((eq span 'year)
  8401. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  8402. sd (calendar-absolute-from-gregorian greg2))
  8403. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
  8404. (t
  8405. (setq sd (+ (* span arg) sd))))
  8406. (let ((org-agenda-overriding-cmd
  8407. ;; `cmd' may have been set by `org-agenda-run-series' which
  8408. ;; uses `org-agenda-overriding-cmd' to decide whether
  8409. ;; overriding is allowed for `cmd'
  8410. (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
  8411. (org-agenda-overriding-arguments
  8412. (list (car args) sd span)))
  8413. (org-agenda-redo)
  8414. (org-agenda-find-same-or-today-or-agenda cnt))
  8415. (set-window-start nil wstart)))
  8416. (defun org-agenda-earlier (arg)
  8417. "Go backward in time by the current span in the agenda buffer.
  8418. With prefix ARG, go backward that many times the current span.
  8419. See also:
  8420. `org-agenda-later' (\\[org-agenda-later])
  8421. `org-agenda-goto-today' (\\[org-agenda-goto-today])
  8422. `org-agenda-goto-date' (\\[org-agenda-goto-date])"
  8423. (interactive "p")
  8424. (org-agenda-later (- arg)))
  8425. (defun org-agenda-view-mode-dispatch ()
  8426. "Call one of the view mode commands."
  8427. (interactive)
  8428. (org-unlogged-message
  8429. "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
  8430. time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
  8431. [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
  8432. (pcase (read-char-exclusive)
  8433. (?\ (call-interactively 'org-agenda-reset-view))
  8434. (?d (call-interactively 'org-agenda-day-view))
  8435. (?w (call-interactively 'org-agenda-week-view))
  8436. (?t (call-interactively 'org-agenda-fortnight-view))
  8437. (?m (call-interactively 'org-agenda-month-view))
  8438. (?y (call-interactively 'org-agenda-year-view))
  8439. (?l (call-interactively 'org-agenda-log-mode))
  8440. (?L (org-agenda-log-mode '(4)))
  8441. (?c (org-agenda-log-mode 'clockcheck))
  8442. ((or ?F ?f) (call-interactively 'org-agenda-follow-mode))
  8443. (?a (call-interactively 'org-agenda-archives-mode))
  8444. (?A (org-agenda-archives-mode 'files))
  8445. ((or ?R ?r) (call-interactively 'org-agenda-clockreport-mode))
  8446. ((or ?E ?e) (call-interactively 'org-agenda-entry-text-mode))
  8447. (?G (call-interactively 'org-agenda-toggle-time-grid))
  8448. (?D (call-interactively 'org-agenda-toggle-diary))
  8449. (?\! (call-interactively 'org-agenda-toggle-deadlines))
  8450. (?\[ (let ((org-agenda-include-inactive-timestamps t))
  8451. (org-agenda-check-type t 'agenda)
  8452. (org-agenda-redo))
  8453. (message "Display now includes inactive timestamps as well"))
  8454. (?q (message "Abort"))
  8455. (key (user-error "Invalid key: %s" key))))
  8456. (defun org-agenda-reset-view ()
  8457. "Switch to default view for agenda."
  8458. (interactive)
  8459. (org-agenda-change-time-span org-agenda-span))
  8460. (defun org-agenda-day-view (&optional day-of-month)
  8461. "Switch to daily view for agenda.
  8462. With argument DAY-OF-MONTH, switch to that day of the month."
  8463. (interactive "P")
  8464. (org-agenda-change-time-span 'day day-of-month))
  8465. (defun org-agenda-week-view (&optional iso-week)
  8466. "Switch to weekly view for agenda.
  8467. With argument ISO-WEEK, switch to the corresponding ISO week.
  8468. If ISO-WEEK has more then 2 digits, only the last two encode
  8469. the week. Any digits before this encode a year. So 200712
  8470. means week 12 of year 2007. Years ranging from 70 years ago
  8471. to 30 years in the future can also be written as 2-digit years."
  8472. (interactive "P")
  8473. (org-agenda-change-time-span 'week iso-week))
  8474. (defun org-agenda-fortnight-view (&optional iso-week)
  8475. "Switch to fortnightly view for agenda.
  8476. With argument ISO-WEEK, switch to the corresponding ISO week.
  8477. If ISO-WEEK has more then 2 digits, only the last two encode
  8478. the week. Any digits before this encode a year. So 200712
  8479. means week 12 of year 2007. Years ranging from 70 years ago
  8480. to 30 years in the future can also be written as 2-digit years."
  8481. (interactive "P")
  8482. (org-agenda-change-time-span 'fortnight iso-week))
  8483. (defun org-agenda-month-view (&optional month)
  8484. "Switch to monthly view for agenda.
  8485. With argument MONTH, switch to that month. If MONTH has more
  8486. then 2 digits, only the last two encode the month. Any digits
  8487. before this encode a year. So 200712 means December year 2007.
  8488. Years ranging from 70 years ago to 30 years in the future can
  8489. also be written as 2-digit years."
  8490. (interactive "P")
  8491. (org-agenda-change-time-span 'month month))
  8492. (defun org-agenda-year-view (&optional year)
  8493. "Switch to yearly view for agenda.
  8494. With argument YEAR, switch to that year. Years ranging from 70
  8495. years ago to 30 years in the future can also be written as
  8496. 2-digit years."
  8497. (interactive "P")
  8498. (when year
  8499. (setq year (org-small-year-to-year year)))
  8500. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  8501. (org-agenda-change-time-span 'year year)
  8502. (error "Abort")))
  8503. (defun org-agenda-change-time-span (span &optional n)
  8504. "Change the agenda view to SPAN.
  8505. SPAN may be `day', `week', `fortnight', `month', `year'."
  8506. (org-agenda-check-type t 'agenda)
  8507. (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
  8508. (curspan (nth 2 args)))
  8509. (when (and (not n) (equal curspan span))
  8510. (error "Viewing span is already \"%s\"" span))
  8511. (let* ((sd (or (org-get-at-bol 'day)
  8512. (nth 1 args)
  8513. org-starting-day))
  8514. (sd (org-agenda-compute-starting-span sd span n))
  8515. (org-agenda-overriding-cmd
  8516. (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
  8517. (org-agenda-overriding-arguments
  8518. (list (car args) sd span)))
  8519. (org-agenda-redo)
  8520. (org-agenda-find-same-or-today-or-agenda))
  8521. (org-agenda-set-mode-name)
  8522. (message "Switched to %s view" span)))
  8523. (defun org-agenda-compute-starting-span (sd span &optional n)
  8524. "Compute starting date for agenda.
  8525. SPAN may be `day', `week', `fortnight', `month', `year'. The return value
  8526. is a cons cell with the starting date and the number of days,
  8527. so that the date SD will be in that range."
  8528. (let* ((greg (calendar-gregorian-from-absolute sd))
  8529. ;; (dg (nth 1 greg))
  8530. (mg (car greg))
  8531. (yg (nth 2 greg)))
  8532. (cond
  8533. ((eq span 'day)
  8534. (when n
  8535. (setq sd (+ (calendar-absolute-from-gregorian
  8536. (list mg 1 yg))
  8537. n -1))))
  8538. ((or (eq span 'week) (eq span 'fortnight))
  8539. (let* ((nt (calendar-day-of-week
  8540. (calendar-gregorian-from-absolute sd)))
  8541. (d (if org-agenda-start-on-weekday
  8542. (- nt org-agenda-start-on-weekday)
  8543. 0))
  8544. y1)
  8545. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  8546. (when n
  8547. (require 'cal-iso)
  8548. (when (> n 99)
  8549. (setq y1 (org-small-year-to-year (/ n 100))
  8550. n (mod n 100)))
  8551. (setq sd
  8552. (calendar-iso-to-absolute
  8553. (list n 1
  8554. (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
  8555. ((eq span 'month)
  8556. (let (y1)
  8557. (when (and n (> n 99))
  8558. (setq y1 (org-small-year-to-year (/ n 100))
  8559. n (mod n 100)))
  8560. (setq sd (calendar-absolute-from-gregorian
  8561. (list (or n mg) 1 (or y1 yg))))))
  8562. ((eq span 'year)
  8563. (setq sd (calendar-absolute-from-gregorian
  8564. (list 1 1 (or n yg))))))
  8565. sd))
  8566. (defun org-agenda-next-date-line (&optional arg)
  8567. "Jump to the next line indicating a date in agenda buffer."
  8568. (interactive "p")
  8569. (org-agenda-check-type t 'agenda)
  8570. (beginning-of-line 1)
  8571. ;; This does not work if user makes date format that starts with a blank
  8572. (when (looking-at-p "^\\S-") (forward-char 1))
  8573. (unless (re-search-forward "^\\S-" nil t arg)
  8574. (backward-char 1)
  8575. (error "No next date after this line in this buffer"))
  8576. (goto-char (match-beginning 0)))
  8577. (defun org-agenda-previous-date-line (&optional arg)
  8578. "Jump to the previous line indicating a date in agenda buffer."
  8579. (interactive "p")
  8580. (org-agenda-check-type t 'agenda)
  8581. (beginning-of-line 1)
  8582. (unless (re-search-backward "^\\S-" nil t arg)
  8583. (error "No previous date before this line in this buffer")))
  8584. ;; Initialize the highlight
  8585. (defvar org-hl (make-overlay 1 1))
  8586. (overlay-put org-hl 'face 'highlight)
  8587. (defun org-highlight (begin end &optional buffer)
  8588. "Highlight a region with overlay."
  8589. (move-overlay org-hl begin end (or buffer (current-buffer))))
  8590. (defun org-unhighlight ()
  8591. "Detach overlay INDEX."
  8592. (delete-overlay org-hl))
  8593. (defun org-unhighlight-once ()
  8594. "Remove the highlight from its position, and this function from the hook."
  8595. (remove-hook 'pre-command-hook #'org-unhighlight-once)
  8596. (org-unhighlight))
  8597. (defvar org-agenda-pre-follow-window-conf nil)
  8598. (defun org-agenda-follow-mode ()
  8599. "Toggle follow mode in an agenda buffer."
  8600. (interactive)
  8601. (unless org-agenda-follow-mode
  8602. (setq org-agenda-pre-follow-window-conf
  8603. (current-window-configuration)))
  8604. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  8605. (unless org-agenda-follow-mode
  8606. (set-window-configuration org-agenda-pre-follow-window-conf))
  8607. (org-agenda-set-mode-name)
  8608. (org-agenda-do-context-action)
  8609. (message "Follow mode is %s"
  8610. (if org-agenda-follow-mode "on" "off")))
  8611. (defun org-agenda-entry-text-mode (&optional arg)
  8612. "Toggle entry text mode in an agenda buffer."
  8613. (interactive "P")
  8614. (if (or org-agenda-tag-filter
  8615. org-agenda-category-filter
  8616. org-agenda-regexp-filter
  8617. org-agenda-top-headline-filter)
  8618. (user-error "Can't show entry text in filtered views")
  8619. (setq org-agenda-entry-text-mode (or (integerp arg)
  8620. (not org-agenda-entry-text-mode)))
  8621. (org-agenda-entry-text-hide)
  8622. (and org-agenda-entry-text-mode
  8623. (let ((org-agenda-entry-text-maxlines
  8624. (if (integerp arg) arg org-agenda-entry-text-maxlines)))
  8625. (org-agenda-entry-text-show)))
  8626. (org-agenda-set-mode-name)
  8627. (message "Entry text mode is %s%s"
  8628. (if org-agenda-entry-text-mode "on" "off")
  8629. (if (not org-agenda-entry-text-mode) ""
  8630. (format " (maximum number of lines is %d)"
  8631. (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
  8632. (defun org-agenda-clockreport-mode ()
  8633. "Toggle clocktable mode in an agenda buffer."
  8634. (interactive)
  8635. (org-agenda-check-type t 'agenda)
  8636. (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
  8637. (org-agenda-set-mode-name)
  8638. (org-agenda-redo)
  8639. (message "Clocktable mode is %s"
  8640. (if org-agenda-clockreport-mode "on" "off")))
  8641. (defun org-agenda-log-mode (&optional special)
  8642. "Toggle log mode in an agenda buffer.
  8643. With argument SPECIAL, show all possible log items, not only the ones
  8644. configured in `org-agenda-log-mode-items'.
  8645. With a `\\[universal-argument] \\[universal-argument]' prefix, show *only* \
  8646. log items, nothing else."
  8647. (interactive "P")
  8648. (org-agenda-check-type t 'agenda)
  8649. (setq org-agenda-show-log
  8650. (cond
  8651. ((equal special '(16)) 'only)
  8652. ((eq special 'clockcheck)
  8653. (if (eq org-agenda-show-log 'clockcheck)
  8654. nil 'clockcheck))
  8655. (special '(closed clock state))
  8656. (t (not org-agenda-show-log))))
  8657. (org-agenda-set-mode-name)
  8658. (org-agenda-redo)
  8659. (message "Log mode is %s" (if org-agenda-show-log "on" "off")))
  8660. (defun org-agenda-archives-mode (&optional with-files)
  8661. "Toggle inclusion of items in trees marked with :ARCHIVE:.
  8662. When called with a prefix argument, include all archive files as well."
  8663. (interactive "P")
  8664. (setq org-agenda-archives-mode
  8665. (cond ((and with-files (eq org-agenda-archives-mode t)) nil)
  8666. (with-files t)
  8667. (org-agenda-archives-mode nil)
  8668. (t 'trees)))
  8669. (org-agenda-set-mode-name)
  8670. (org-agenda-redo)
  8671. (message
  8672. "%s"
  8673. (cond
  8674. ((eq org-agenda-archives-mode nil)
  8675. "No archives are included")
  8676. ((eq org-agenda-archives-mode 'trees)
  8677. (format "Trees with :%s: tag are included" org-archive-tag))
  8678. ((eq org-agenda-archives-mode t)
  8679. (format "Trees with :%s: tag and all active archive files are included"
  8680. org-archive-tag)))))
  8681. (defun org-agenda-toggle-diary ()
  8682. "Toggle diary inclusion in an agenda buffer."
  8683. (interactive)
  8684. (org-agenda-check-type t 'agenda)
  8685. (setq org-agenda-include-diary (not org-agenda-include-diary))
  8686. (org-agenda-redo)
  8687. (org-agenda-set-mode-name)
  8688. (message "Diary inclusion turned %s"
  8689. (if org-agenda-include-diary "on" "off")))
  8690. (defun org-agenda-toggle-deadlines ()
  8691. "Toggle inclusion of entries with a deadline in an agenda buffer."
  8692. (interactive)
  8693. (org-agenda-check-type t 'agenda)
  8694. (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
  8695. (org-agenda-redo)
  8696. (org-agenda-set-mode-name)
  8697. (message "Deadlines inclusion turned %s"
  8698. (if org-agenda-include-deadlines "on" "off")))
  8699. (defun org-agenda-toggle-time-grid ()
  8700. "Toggle time grid in an agenda buffer."
  8701. (interactive)
  8702. (org-agenda-check-type t 'agenda)
  8703. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  8704. (org-agenda-redo)
  8705. (org-agenda-set-mode-name)
  8706. (message "Time-grid turned %s"
  8707. (if org-agenda-use-time-grid "on" "off")))
  8708. (defun org-agenda-set-mode-name ()
  8709. "Set the mode name to indicate all the small mode settings."
  8710. (setq mode-name
  8711. (list "Org-Agenda"
  8712. (if (get 'org-agenda-files 'org-restrict) " []" "")
  8713. " "
  8714. '(:eval (org-agenda-span-name org-agenda-current-span))
  8715. (if org-agenda-follow-mode " Follow" "")
  8716. (if org-agenda-entry-text-mode " ETxt" "")
  8717. (if org-agenda-include-diary " Diary" "")
  8718. (if org-agenda-include-deadlines " Ddl" "")
  8719. (if org-agenda-use-time-grid " Grid" "")
  8720. (if (and (boundp 'org-habit-show-habits)
  8721. org-habit-show-habits)
  8722. " Habit" "")
  8723. (cond
  8724. ((consp org-agenda-show-log) " LogAll")
  8725. ((eq org-agenda-show-log 'clockcheck) " ClkCk")
  8726. (org-agenda-show-log " Log")
  8727. (t ""))
  8728. (if (org-agenda-filter-any) " " "")
  8729. (if (or org-agenda-category-filter
  8730. (assoc-default 'category org-agenda-filters-preset))
  8731. '(:eval (propertize
  8732. (concat "["
  8733. (mapconcat
  8734. #'identity
  8735. (append
  8736. (assoc-default 'category org-agenda-filters-preset)
  8737. org-agenda-category-filter)
  8738. "")
  8739. "]")
  8740. 'face 'org-agenda-filter-category
  8741. 'help-echo "Category used in filtering"))
  8742. "")
  8743. (if (or org-agenda-tag-filter
  8744. (assoc-default 'tag org-agenda-filters-preset))
  8745. '(:eval (propertize
  8746. (concat (mapconcat
  8747. #'identity
  8748. (append
  8749. (assoc-default 'tag org-agenda-filters-preset)
  8750. org-agenda-tag-filter)
  8751. ""))
  8752. 'face 'org-agenda-filter-tags
  8753. 'help-echo "Tags used in filtering"))
  8754. "")
  8755. (if (or org-agenda-effort-filter
  8756. (assoc-default 'effort org-agenda-filters-preset))
  8757. '(:eval (propertize
  8758. (concat (mapconcat
  8759. #'identity
  8760. (append
  8761. (assoc-default 'effort org-agenda-filters-preset)
  8762. org-agenda-effort-filter)
  8763. ""))
  8764. 'face 'org-agenda-filter-effort
  8765. 'help-echo "Effort conditions used in filtering"))
  8766. "")
  8767. (if (or org-agenda-regexp-filter
  8768. (assoc-default 'regexp org-agenda-filters-preset))
  8769. '(:eval (propertize
  8770. (concat (mapconcat
  8771. (lambda (x) (concat (substring x 0 1) "/" (substring x 1) "/"))
  8772. (append
  8773. (assoc-default 'regexp org-agenda-filters-preset)
  8774. org-agenda-regexp-filter)
  8775. ""))
  8776. 'face 'org-agenda-filter-regexp
  8777. 'help-echo "Regexp used in filtering"))
  8778. "")
  8779. (if org-agenda-archives-mode
  8780. (if (eq org-agenda-archives-mode t)
  8781. " Archives"
  8782. (format " :%s:" org-archive-tag))
  8783. "")
  8784. (if org-agenda-clockreport-mode " Clock" "")))
  8785. (force-mode-line-update))
  8786. (defun org-agenda-update-agenda-type ()
  8787. "Update the agenda type after each command."
  8788. (setq org-agenda-type
  8789. (or (get-text-property (point) 'org-agenda-type)
  8790. (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
  8791. (defun org-agenda-next-line ()
  8792. "Move cursor to the next line, and show if follow mode is active."
  8793. (interactive)
  8794. (call-interactively 'next-line)
  8795. (org-agenda-do-context-action))
  8796. (defun org-agenda-previous-line ()
  8797. "Move cursor to the previous line, and show if follow-mode is active."
  8798. (interactive)
  8799. (call-interactively 'previous-line)
  8800. (org-agenda-do-context-action))
  8801. (defun org-agenda-next-item (n)
  8802. "Move cursor to next agenda item."
  8803. (interactive "p")
  8804. (let ((col (current-column)))
  8805. (dotimes (_ n)
  8806. (when (next-single-property-change (line-end-position) 'org-marker)
  8807. (move-end-of-line 1)
  8808. (goto-char (next-single-property-change (point) 'org-marker))))
  8809. (org-move-to-column col))
  8810. (org-agenda-do-context-action))
  8811. (defun org-agenda-previous-item (n)
  8812. "Move cursor to next agenda item."
  8813. (interactive "p")
  8814. (dotimes (_ n)
  8815. (let ((col (current-column))
  8816. (goto (save-excursion
  8817. (move-end-of-line 0)
  8818. (previous-single-property-change (point) 'org-marker))))
  8819. (when goto (goto-char goto))
  8820. (org-move-to-column col)))
  8821. (org-agenda-do-context-action))
  8822. (defun org-agenda-do-context-action ()
  8823. "Show outline path and, maybe, follow mode window."
  8824. (let ((m (org-get-at-bol 'org-marker)))
  8825. (when (and (markerp m) (marker-buffer m))
  8826. (and org-agenda-follow-mode
  8827. (if org-agenda-follow-indirect
  8828. (org-agenda-tree-to-indirect-buffer nil)
  8829. (org-agenda-show)))
  8830. (and org-agenda-show-outline-path
  8831. (org-with-point-at m (org-display-outline-path t))))))
  8832. (defun org-agenda-show-tags ()
  8833. "Show the tags applicable to the current item."
  8834. (interactive)
  8835. (let* ((tags (org-get-at-bol 'tags)))
  8836. (if tags
  8837. (message "Tags are :%s:"
  8838. (org-no-properties (mapconcat #'identity tags ":")))
  8839. (message "No tags associated with this line"))))
  8840. (defun org-agenda-goto (&optional highlight)
  8841. "Go to the entry at point in the corresponding Org file."
  8842. (interactive)
  8843. (let* ((marker (or (org-get-at-bol 'org-marker)
  8844. (org-agenda-error)))
  8845. (buffer (marker-buffer marker))
  8846. (pos (marker-position marker)))
  8847. ;; FIXME: use `org-switch-to-buffer-other-window'?
  8848. (switch-to-buffer-other-window buffer)
  8849. (widen)
  8850. (push-mark)
  8851. (goto-char pos)
  8852. (when (derived-mode-p 'org-mode)
  8853. (org-fold-show-context 'agenda)
  8854. (recenter (/ (window-height) 2))
  8855. (org-back-to-heading t)
  8856. (let ((case-fold-search nil))
  8857. (when (re-search-forward org-complex-heading-regexp nil t)
  8858. (goto-char (match-beginning 4)))))
  8859. (run-hooks 'org-agenda-after-show-hook)
  8860. (and highlight (org-highlight (line-beginning-position)
  8861. (line-end-position)))))
  8862. (defvar org-agenda-after-show-hook nil
  8863. "Normal hook run after an item has been shown from the agenda.
  8864. Point is in the buffer where the item originated.")
  8865. ;; Defined later in org-agenda.el
  8866. (defvar org-agenda-loop-over-headlines-in-active-region nil)
  8867. (defun org-agenda-do-in-region (beg end cmd &optional arg force-arg delete)
  8868. "Between region BEG and END, call agenda command CMD.
  8869. When optional argument ARG is non-nil or FORCE-ARG is t, pass
  8870. ARG to CMD. When optional argument DELETE is non-nil, assume CMD
  8871. deletes the agenda entry and don't move to the next entry."
  8872. (save-excursion
  8873. (goto-char beg)
  8874. (let ((mend (move-marker (make-marker) end))
  8875. (all (eq org-agenda-loop-over-headlines-in-active-region t))
  8876. (match (and (stringp org-agenda-loop-over-headlines-in-active-region)
  8877. org-agenda-loop-over-headlines-in-active-region))
  8878. (level (and (eq org-agenda-loop-over-headlines-in-active-region 'start-level)
  8879. (org-get-at-bol 'level))))
  8880. (while (< (point) mend)
  8881. (let ((ov (make-overlay (point) (line-end-position))))
  8882. (if (not (or all
  8883. (and match (looking-at-p match))
  8884. (eq level (org-get-at-bol 'level))))
  8885. (org-agenda-next-item 1)
  8886. (overlay-put ov 'face 'region)
  8887. (if (or arg force-arg) (funcall cmd arg) (funcall cmd))
  8888. (when (not delete) (org-agenda-next-item 1))
  8889. (delete-overlay ov)))))))
  8890. ;; org-agenda-[schedule,deadline,date-prompt,todo,[toggle]archive*,
  8891. ;; kill,set-property,set-effort] commands may loop over agenda
  8892. ;; entries. Commands `org-agenda-set-tags' and `org-agenda-bulk-mark'
  8893. ;; use their own mechanisms on active regions.
  8894. (defmacro org-agenda-maybe-loop (cmd arg force-arg delete &rest body)
  8895. "Maybe loop over agenda entries and perform CMD.
  8896. Pass ARG, FORCE-ARG, DELETE and BODY to `org-agenda-do-in-region'."
  8897. (declare (debug t))
  8898. `(if (and (called-interactively-p 'any)
  8899. org-agenda-loop-over-headlines-in-active-region
  8900. (org-region-active-p))
  8901. (org-agenda-do-in-region
  8902. (region-beginning) (region-end) ,cmd ,arg ,force-arg ,delete)
  8903. ,@body))
  8904. (defun org-agenda-kill ()
  8905. "Kill the entry or subtree belonging to the current agenda entry."
  8906. (interactive)
  8907. (or (eq major-mode 'org-agenda-mode) (user-error "Not in agenda"))
  8908. (org-agenda-maybe-loop
  8909. #'org-agenda-kill nil nil t
  8910. (let* ((bufname-orig (buffer-name))
  8911. (marker (or (org-get-at-bol 'org-marker)
  8912. (org-agenda-error)))
  8913. (buffer (marker-buffer marker))
  8914. (pos (marker-position marker))
  8915. (type (org-get-at-bol 'type))
  8916. dbeg dend (n 0))
  8917. (org-with-remote-undo buffer
  8918. (with-current-buffer buffer
  8919. (save-excursion
  8920. (goto-char pos)
  8921. (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
  8922. (setq dbeg (progn (org-back-to-heading t) (point))
  8923. dend (org-end-of-subtree t t))
  8924. (setq dbeg (line-beginning-position)
  8925. dend (min (point-max) (1+ (line-end-position)))))
  8926. (goto-char dbeg)
  8927. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  8928. (when (or (eq t org-agenda-confirm-kill)
  8929. (and (numberp org-agenda-confirm-kill)
  8930. (> n org-agenda-confirm-kill)))
  8931. (let ((win-conf (current-window-configuration)))
  8932. (unwind-protect
  8933. (and
  8934. (prog2
  8935. (org-agenda-tree-to-indirect-buffer nil)
  8936. (not (y-or-n-p
  8937. (format "Delete entry with %d lines in buffer \"%s\"? "
  8938. n (buffer-name buffer))))
  8939. (kill-buffer org-last-indirect-buffer))
  8940. (error "Abort"))
  8941. (set-window-configuration win-conf))))
  8942. (let ((org-agenda-buffer-name bufname-orig))
  8943. (org-remove-subtree-entries-from-agenda buffer dbeg dend))
  8944. (with-current-buffer buffer (delete-region dbeg dend))
  8945. (message "Agenda item and source killed")))))
  8946. (defvar org-archive-default-command) ; defined in org-archive.el
  8947. (defun org-agenda-archive-default ()
  8948. "Archive the entry or subtree belonging to the current agenda entry."
  8949. (interactive)
  8950. (require 'org-archive)
  8951. (funcall-interactively
  8952. #'org-agenda-archive-with org-archive-default-command))
  8953. (defun org-agenda-archive-default-with-confirmation ()
  8954. "Archive the entry or subtree belonging to the current agenda entry."
  8955. (interactive)
  8956. (require 'org-archive)
  8957. (funcall-interactively
  8958. #'org-agenda-archive-with org-archive-default-command 'confirm))
  8959. (defun org-agenda-archive ()
  8960. "Archive the entry or subtree belonging to the current agenda entry."
  8961. (interactive)
  8962. (funcall-interactively
  8963. #'org-agenda-archive-with 'org-archive-subtree))
  8964. (defun org-agenda-archive-to-archive-sibling ()
  8965. "Move the entry to the archive sibling."
  8966. (interactive)
  8967. (funcall-interactively
  8968. #'org-agenda-archive-with 'org-archive-to-archive-sibling))
  8969. (defvar org-archive-from-agenda)
  8970. (defun org-agenda-archive-with (cmd &optional confirm)
  8971. "Move the entry to the archive sibling."
  8972. (interactive)
  8973. (or (eq major-mode 'org-agenda-mode) (user-error "Not in agenda"))
  8974. (org-agenda-maybe-loop
  8975. #'org-agenda-archive-with cmd nil t
  8976. (let* ((bufname-orig (buffer-name))
  8977. (marker (or (org-get-at-bol 'org-marker)
  8978. (org-agenda-error)))
  8979. (buffer (marker-buffer marker))
  8980. (pos (marker-position marker)))
  8981. (org-with-remote-undo buffer
  8982. (with-current-buffer buffer
  8983. (if (derived-mode-p 'org-mode)
  8984. (if (and confirm
  8985. (not (y-or-n-p "Archive this subtree or entry? ")))
  8986. (error "Abort")
  8987. (save-window-excursion
  8988. (goto-char pos)
  8989. (let ((org-agenda-buffer-name bufname-orig))
  8990. (org-remove-subtree-entries-from-agenda))
  8991. (org-back-to-heading t)
  8992. (let ((org-archive-from-agenda t))
  8993. (funcall cmd))))
  8994. (error "Archiving works only in Org files")))))))
  8995. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  8996. "Remove all lines in the agenda that correspond to a given subtree.
  8997. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  8998. If this information is not given, the function uses the tree at point."
  8999. (let ((buf (or buf (current-buffer))) m p)
  9000. (save-excursion
  9001. (unless (and beg end)
  9002. (org-back-to-heading t)
  9003. (setq beg (point))
  9004. (org-end-of-subtree t)
  9005. (setq end (point)))
  9006. (set-buffer (get-buffer org-agenda-buffer-name))
  9007. (save-excursion
  9008. (goto-char (point-max))
  9009. (beginning-of-line 1)
  9010. (while (not (bobp))
  9011. (when (and (setq m (org-get-at-bol 'org-marker))
  9012. (equal buf (marker-buffer m))
  9013. (setq p (marker-position m))
  9014. (>= p beg)
  9015. (< p end))
  9016. (let ((inhibit-read-only t))
  9017. (delete-region (line-beginning-position)
  9018. (1+ (line-end-position)))))
  9019. (beginning-of-line 0))))))
  9020. (defun org-agenda-refile (&optional goto rfloc no-update)
  9021. "Refile the item at point.
  9022. When called with `\\[universal-argument] \\[universal-argument]', \
  9023. go to the location of the last
  9024. refiled item.
  9025. When called with `\\[universal-argument] \\[universal-argument] \
  9026. \\[universal-argument]' prefix or when GOTO is 0, clear
  9027. the refile cache.
  9028. RFLOC can be a refile location obtained in a different way.
  9029. When NO-UPDATE is non-nil, don't redo the agenda buffer."
  9030. (interactive "P")
  9031. (cond
  9032. ((member goto '(0 (64)))
  9033. (org-refile-cache-clear))
  9034. ((equal goto '(16))
  9035. (org-refile-goto-last-stored))
  9036. (t
  9037. (let* ((buffer-orig (buffer-name))
  9038. (marker (or (org-get-at-bol 'org-hd-marker)
  9039. (org-agenda-error)))
  9040. (buffer (marker-buffer marker))
  9041. ;; (pos (marker-position marker))
  9042. (rfloc (or rfloc
  9043. (org-refile-get-location
  9044. (if goto "Goto" "Refile to") buffer
  9045. org-refile-allow-creating-parent-nodes))))
  9046. (with-current-buffer buffer
  9047. (org-with-wide-buffer
  9048. (goto-char marker)
  9049. (let ((org-agenda-buffer-name buffer-orig))
  9050. (org-remove-subtree-entries-from-agenda))
  9051. (org-refile goto buffer rfloc))))
  9052. (unless no-update (org-agenda-redo)))))
  9053. (defun org-agenda-open-link (&optional arg)
  9054. "Open the link(s) in the current entry, if any.
  9055. This looks for a link in the displayed line in the agenda.
  9056. It also looks at the text of the entry itself."
  9057. (interactive "P")
  9058. (let* ((marker (or (org-get-at-bol 'org-hd-marker)
  9059. (org-get-at-bol 'org-marker)))
  9060. (buffer (and marker (marker-buffer marker)))
  9061. (prefix (buffer-substring (line-beginning-position)
  9062. (line-end-position)))
  9063. (lkall (and buffer (org-offer-links-in-entry
  9064. buffer marker arg prefix)))
  9065. (lk0 (car lkall))
  9066. (lk (if (stringp lk0) (list lk0) lk0))
  9067. (lkend (cdr lkall))
  9068. trg)
  9069. (cond
  9070. ((and buffer lk)
  9071. (mapcar (lambda(l)
  9072. (with-current-buffer buffer
  9073. (setq trg (and (string-match org-link-bracket-re l)
  9074. (match-string 1 l)))
  9075. (if (or (not trg) (string-match org-link-any-re trg))
  9076. ;; Don't use `org-with-wide-buffer' here as
  9077. ;; opening the link may result in moving the point
  9078. (save-restriction
  9079. (widen)
  9080. (goto-char marker)
  9081. (when (search-forward l nil lkend)
  9082. (goto-char (match-beginning 0))
  9083. (org-open-at-point)))
  9084. ;; This is an internal link, widen the buffer
  9085. ;; FIXME: use `org-switch-to-buffer-other-window'?
  9086. (switch-to-buffer-other-window buffer)
  9087. (widen)
  9088. (goto-char marker)
  9089. (when (search-forward l nil lkend)
  9090. (goto-char (match-beginning 0))
  9091. (org-open-at-point)))))
  9092. lk))
  9093. ((or (org-in-regexp (concat "\\(" org-link-bracket-re "\\)"))
  9094. (save-excursion
  9095. (beginning-of-line 1)
  9096. (looking-at (concat ".*?\\(" org-link-bracket-re "\\)"))))
  9097. (org-link-open-from-string (match-string 1)))
  9098. (t (message "No link to open here")))))
  9099. (defun org-agenda-copy-local-variable (var)
  9100. "Get a variable from a referenced buffer and install it here."
  9101. (let ((m (org-get-at-bol 'org-marker)))
  9102. (when (and m (buffer-live-p (marker-buffer m)))
  9103. (set (make-local-variable var)
  9104. (with-current-buffer (marker-buffer m)
  9105. (symbol-value var))))))
  9106. (defun org-agenda-switch-to (&optional delete-other-windows)
  9107. "Go to the Org mode file which contains the item at point.
  9108. When optional argument DELETE-OTHER-WINDOWS is non-nil, the
  9109. displayed Org file fills the frame."
  9110. (interactive)
  9111. (if (and org-return-follows-link
  9112. (not (org-get-at-bol 'org-marker))
  9113. (org-in-regexp org-link-bracket-re))
  9114. (org-link-open-from-string (match-string 0))
  9115. (let* ((marker (or (org-get-at-bol 'org-marker)
  9116. (org-agenda-error)))
  9117. (buffer (marker-buffer marker))
  9118. (pos (marker-position marker)))
  9119. (unless buffer (user-error "Trying to switch to non-existent buffer"))
  9120. (pop-to-buffer-same-window buffer)
  9121. (when delete-other-windows (delete-other-windows))
  9122. (widen)
  9123. (goto-char pos)
  9124. (when (derived-mode-p 'org-mode)
  9125. (org-fold-show-context 'agenda)
  9126. (run-hooks 'org-agenda-after-show-hook)))))
  9127. (defun org-agenda-goto-mouse (ev)
  9128. "Go to the Org file which contains the item at the mouse click."
  9129. (interactive "e")
  9130. (mouse-set-point ev)
  9131. (org-agenda-goto))
  9132. (defun org-agenda-show (&optional full-entry)
  9133. "Display the Org file which contains the item at point.
  9134. With prefix argument FULL-ENTRY, make the entire entry visible
  9135. if it was hidden in the outline."
  9136. (interactive "P")
  9137. (let ((win (selected-window)))
  9138. (org-agenda-goto t)
  9139. (when full-entry (org-fold-show-entry 'hide-drawers))
  9140. (select-window win)))
  9141. (defvar org-agenda-show-window nil)
  9142. (defun org-agenda-show-and-scroll-up (&optional arg)
  9143. "Display the Org file which contains the item at point.
  9144. When called repeatedly, scroll the window that is displaying the buffer.
  9145. With a `\\[universal-argument]' prefix argument, display the item, but \
  9146. fold drawers."
  9147. (interactive "P")
  9148. (let ((win (selected-window)))
  9149. (if (and (window-live-p org-agenda-show-window)
  9150. (eq this-command last-command))
  9151. (progn
  9152. (select-window org-agenda-show-window)
  9153. (ignore-errors (scroll-up)))
  9154. (org-agenda-goto t)
  9155. (org-fold-show-entry 'hide-drawers)
  9156. (if arg (org-cycle-hide-drawers 'children)
  9157. (org-with-wide-buffer
  9158. (narrow-to-region (org-entry-beginning-position)
  9159. (org-entry-end-position))
  9160. (org-fold-show-all '(drawers))))
  9161. (setq org-agenda-show-window (selected-window)))
  9162. (select-window win)))
  9163. (defun org-agenda-show-scroll-down ()
  9164. "Scroll down the window showing the agenda."
  9165. (interactive)
  9166. (let ((win (selected-window)))
  9167. (when (window-live-p org-agenda-show-window)
  9168. (select-window org-agenda-show-window)
  9169. (ignore-errors (scroll-down))
  9170. (select-window win))))
  9171. (defun org-agenda-show-1 (&optional more)
  9172. "Display the Org file which contains the item at point.
  9173. The prefix arg selects the amount of information to display:
  9174. 0 hide the subtree
  9175. 1 just show the entry according to defaults.
  9176. 2 show the children view
  9177. 3 show the subtree view
  9178. 4 show the entire subtree and any drawers
  9179. With prefix argument FULL-ENTRY, make the entire entry visible
  9180. if it was hidden in the outline."
  9181. (interactive "p")
  9182. (let ((win (selected-window)))
  9183. (org-agenda-goto t)
  9184. (org-back-to-heading)
  9185. (set-window-start (selected-window) (line-beginning-position))
  9186. (cond
  9187. ((= more 0)
  9188. (org-fold-subtree t)
  9189. (save-excursion
  9190. (org-back-to-heading)
  9191. (run-hook-with-args 'org-cycle-hook 'folded))
  9192. (message "Remote: FOLDED"))
  9193. ((and (called-interactively-p 'any) (= more 1))
  9194. (message "Remote: show with default settings"))
  9195. ((= more 2)
  9196. (org-fold-show-entry 'hide-drawers)
  9197. (org-fold-show-children)
  9198. (save-excursion
  9199. (org-back-to-heading)
  9200. (run-hook-with-args 'org-cycle-hook 'children))
  9201. (message "Remote: CHILDREN"))
  9202. ((= more 3)
  9203. (org-fold-show-subtree)
  9204. (save-excursion
  9205. (org-back-to-heading)
  9206. (run-hook-with-args 'org-cycle-hook 'subtree))
  9207. (message "Remote: SUBTREE"))
  9208. ((> more 3)
  9209. (org-fold-show-subtree)
  9210. (message "Remote: SUBTREE AND ALL DRAWERS")))
  9211. (select-window win)))
  9212. (defvar org-agenda-cycle-counter nil)
  9213. (defun org-agenda-cycle-show (&optional n)
  9214. "Show the current entry in another window, with default settings.
  9215. Default settings are taken from `org-show-context-detail'. When
  9216. use repeatedly in immediate succession, the remote entry will
  9217. cycle through visibility
  9218. children -> subtree -> folded
  9219. When called with a numeric prefix arg, that arg will be passed through to
  9220. `org-agenda-show-1'. For the interpretation of that argument, see the
  9221. docstring of `org-agenda-show-1'."
  9222. (interactive "P")
  9223. (if (integerp n)
  9224. (setq org-agenda-cycle-counter n)
  9225. (if (not (eq last-command this-command))
  9226. (setq org-agenda-cycle-counter 1)
  9227. (if (equal org-agenda-cycle-counter 0)
  9228. (setq org-agenda-cycle-counter 2)
  9229. (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
  9230. (when (> org-agenda-cycle-counter 3)
  9231. (setq org-agenda-cycle-counter 0)))))
  9232. (org-agenda-show-1 org-agenda-cycle-counter))
  9233. (defun org-agenda-recenter (arg)
  9234. "Display the Org file which contains the item at point and recenter."
  9235. (interactive "P")
  9236. (let ((win (selected-window)))
  9237. (org-agenda-goto t)
  9238. (recenter arg)
  9239. (select-window win)))
  9240. (defun org-agenda-show-mouse (ev)
  9241. "Display the Org file which contains the item at the mouse click."
  9242. (interactive "e")
  9243. (mouse-set-point ev)
  9244. (org-agenda-show))
  9245. (defun org-agenda-check-no-diary ()
  9246. "Check if the entry is a diary link and abort if yes."
  9247. (when (org-get-at-bol 'org-agenda-diary-link)
  9248. (org-agenda-error)))
  9249. (defun org-agenda-error ()
  9250. "Throw an error when a command is not allowed in the agenda."
  9251. (user-error "Command not allowed in this line"))
  9252. (defun org-agenda-tree-to-indirect-buffer (arg)
  9253. "Show the subtree corresponding to the current entry in an indirect buffer.
  9254. This calls the command `org-tree-to-indirect-buffer' from the original buffer.
  9255. With a numerical prefix ARG, go up to this level and then take that tree.
  9256. With a negative numeric ARG, go up by this number of levels.
  9257. With a `\\[universal-argument]' prefix, make a separate frame for this tree, \
  9258. i.e. don't use
  9259. the dedicated frame."
  9260. (interactive "P")
  9261. (if current-prefix-arg
  9262. (org-agenda-do-tree-to-indirect-buffer arg)
  9263. (let ((agenda-buffer (buffer-name))
  9264. (agenda-window (selected-window))
  9265. (indirect-window
  9266. (and org-last-indirect-buffer
  9267. (get-buffer-window org-last-indirect-buffer))))
  9268. (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
  9269. (unless (or (eq org-indirect-buffer-display 'new-frame)
  9270. (eq org-indirect-buffer-display 'dedicated-frame))
  9271. (unwind-protect
  9272. (unless (and indirect-window (window-live-p indirect-window))
  9273. (setq indirect-window (split-window agenda-window)))
  9274. (and indirect-window (select-window indirect-window))
  9275. (switch-to-buffer org-last-indirect-buffer :norecord)
  9276. (fit-window-to-buffer indirect-window)))
  9277. (select-window (get-buffer-window agenda-buffer))
  9278. (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
  9279. (defun org-agenda-do-tree-to-indirect-buffer (arg)
  9280. "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
  9281. (org-agenda-check-no-diary)
  9282. (let* ((marker (or (org-get-at-bol 'org-marker)
  9283. (org-agenda-error)))
  9284. (buffer (marker-buffer marker))
  9285. (pos (marker-position marker)))
  9286. (with-current-buffer buffer
  9287. (save-excursion
  9288. (goto-char pos)
  9289. (org-tree-to-indirect-buffer arg)))))
  9290. (defvar org-last-heading-marker (make-marker)
  9291. "Marker pointing to the headline that last changed its TODO state
  9292. by a remote command from the agenda.")
  9293. (defun org-agenda-todo-nextset ()
  9294. "Switch TODO entry to next sequence."
  9295. (interactive)
  9296. (org-agenda-todo 'nextset))
  9297. (defun org-agenda-todo-previousset ()
  9298. "Switch TODO entry to previous sequence."
  9299. (interactive)
  9300. (org-agenda-todo 'previousset))
  9301. (defvar org-agenda-headline-snapshot-before-repeat)
  9302. (defun org-agenda-todo (&optional arg)
  9303. "Cycle TODO state of line at point, also in Org file.
  9304. This changes the line at point, all other lines in the agenda referring to
  9305. the same tree node, and the headline of the tree node in the Org file."
  9306. (interactive "P")
  9307. (org-agenda-check-no-diary)
  9308. (org-agenda-maybe-loop
  9309. #'org-agenda-todo arg nil nil
  9310. (let* ((col (current-column))
  9311. (marker (or (org-get-at-bol 'org-marker)
  9312. (org-agenda-error)))
  9313. (buffer (marker-buffer marker))
  9314. (pos (marker-position marker))
  9315. (hdmarker (org-get-at-bol 'org-hd-marker))
  9316. (todayp (org-agenda-today-p (org-get-at-bol 'day)))
  9317. (inhibit-read-only t)
  9318. org-loop-over-headlines-in-active-region
  9319. org-agenda-headline-snapshot-before-repeat newhead just-one)
  9320. (org-with-remote-undo buffer
  9321. (with-current-buffer buffer
  9322. (widen)
  9323. (goto-char pos)
  9324. (org-fold-show-context 'agenda)
  9325. (let ((current-prefix-arg arg))
  9326. (call-interactively 'org-todo)
  9327. ;; Make sure that log is recorded in current undo.
  9328. (when (and org-log-setup
  9329. (not (eq org-log-note-how 'note)))
  9330. (org-add-log-note)))
  9331. (and (bolp) (forward-char 1))
  9332. (setq newhead (org-get-heading))
  9333. (when (and org-agenda-headline-snapshot-before-repeat
  9334. (not (equal org-agenda-headline-snapshot-before-repeat
  9335. newhead))
  9336. todayp)
  9337. (setq newhead org-agenda-headline-snapshot-before-repeat
  9338. just-one t))
  9339. (save-excursion
  9340. (org-back-to-heading)
  9341. (move-marker org-last-heading-marker (point))))
  9342. (beginning-of-line 1)
  9343. (save-window-excursion
  9344. (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
  9345. (when (bound-and-true-p org-clock-out-when-done)
  9346. (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
  9347. newhead)
  9348. (org-agenda-unmark-clocking-task))
  9349. (org-move-to-column col)
  9350. (org-agenda-mark-clocking-task)))))
  9351. (defun org-agenda-add-note (&optional _arg)
  9352. "Add a time-stamped note to the entry at point."
  9353. (interactive) ;; "P"
  9354. (org-agenda-check-no-diary)
  9355. (let* ((marker (or (org-get-at-bol 'org-marker)
  9356. (org-agenda-error)))
  9357. (buffer (marker-buffer marker))
  9358. (pos (marker-position marker))
  9359. (_hdmarker (org-get-at-bol 'org-hd-marker))
  9360. (inhibit-read-only t))
  9361. (with-current-buffer buffer
  9362. (widen)
  9363. (goto-char pos)
  9364. (org-fold-show-context 'agenda)
  9365. (org-add-note))))
  9366. (defun org-agenda-change-all-lines (newhead hdmarker
  9367. &optional fixface just-this)
  9368. "Change all lines in the agenda buffer which match HDMARKER.
  9369. The new content of the line will be NEWHEAD (as modified by
  9370. `org-agenda-format-item'). HDMARKER is checked with
  9371. `equal' against all `org-hd-marker' text properties in the file.
  9372. If FIXFACE is non-nil, the face of each item is modified according to
  9373. the new TODO state.
  9374. If JUST-THIS is non-nil, change just the current line, not all.
  9375. If FORCE-TAGS is non-nil, the car of it returns the new tags."
  9376. (let* ((inhibit-read-only t)
  9377. (line (org-current-line))
  9378. (org-agenda-buffer (current-buffer))
  9379. (thetags (with-current-buffer (marker-buffer hdmarker)
  9380. (org-get-tags hdmarker)))
  9381. props m undone-face done-face finish new dotime level cat tags
  9382. effort effort-minutes) ;; pl
  9383. (save-excursion
  9384. (goto-char (point-max))
  9385. (beginning-of-line 1)
  9386. (while (not finish)
  9387. (setq finish (bobp))
  9388. (when (and (setq m (org-get-at-bol 'org-hd-marker))
  9389. (or (not just-this) (= (org-current-line) line))
  9390. (equal m hdmarker))
  9391. (setq props (text-properties-at (point))
  9392. dotime (org-get-at-bol 'dotime)
  9393. cat (org-agenda-get-category)
  9394. level (org-get-at-bol 'level)
  9395. tags thetags
  9396. effort (org-get-at-bol 'effort)
  9397. effort-minutes (org-get-at-bol 'effort-minutes)
  9398. new
  9399. (let ((org-prefix-format-compiled
  9400. (or (get-text-property (min (1- (point-max)) (point)) 'format)
  9401. org-prefix-format-compiled))
  9402. (extra (org-get-at-bol 'extra)))
  9403. (with-current-buffer (marker-buffer hdmarker)
  9404. (org-with-wide-buffer
  9405. (org-agenda-format-item extra
  9406. (org-add-props newhead nil
  9407. 'effort effort
  9408. 'effort-minutes effort-minutes)
  9409. level cat tags dotime))))
  9410. ;; pl (text-property-any (line-beginning-position)
  9411. ;; (line-end-position) 'org-heading t)
  9412. undone-face (org-get-at-bol 'undone-face)
  9413. done-face (org-get-at-bol 'done-face))
  9414. (beginning-of-line 1)
  9415. (cond
  9416. ((equal new "") (delete-region (point) (line-beginning-position 2)))
  9417. ((looking-at ".*")
  9418. ;; When replacing the whole line, preserve bulk mark
  9419. ;; overlay, if any.
  9420. (let ((mark (catch :overlay
  9421. (dolist (o (overlays-in (point) (+ 2 (point))))
  9422. (when (eq (overlay-get o 'type)
  9423. 'org-marked-entry-overlay)
  9424. (throw :overlay o))))))
  9425. (replace-match new t t)
  9426. (beginning-of-line)
  9427. (when mark (move-overlay mark (point) (+ 2 (point)))))
  9428. (add-text-properties (line-beginning-position)
  9429. (line-end-position) props)
  9430. (when fixface
  9431. (add-text-properties
  9432. (line-beginning-position) (line-end-position)
  9433. (list 'face
  9434. (if org-last-todo-state-is-todo
  9435. undone-face done-face))))
  9436. (org-agenda-highlight-todo 'line)
  9437. (beginning-of-line 1))
  9438. (t (error "Line update did not work")))
  9439. (save-restriction
  9440. (narrow-to-region (line-beginning-position) (line-end-position))
  9441. (org-agenda-finalize)))
  9442. (beginning-of-line 0)))))
  9443. (defun org-agenda-align-tags (&optional line)
  9444. "Align all tags in agenda items to `org-agenda-tags-column'.
  9445. When optional argument LINE is non-nil, align tags only on the
  9446. current line."
  9447. (let ((inhibit-read-only t)
  9448. (org-agenda-tags-column (if (eq 'auto org-agenda-tags-column)
  9449. (- (window-max-chars-per-line))
  9450. org-agenda-tags-column))
  9451. (end (and line (line-end-position)))
  9452. l c)
  9453. (org-fold-core-ignore-modifications
  9454. (save-excursion
  9455. (goto-char (if line (line-beginning-position) (point-min)))
  9456. (while (re-search-forward org-tag-group-re end t)
  9457. (add-text-properties
  9458. (match-beginning 1) (match-end 1)
  9459. (list 'face (delq nil (let ((prop (get-text-property
  9460. (match-beginning 1) 'face)))
  9461. (or (listp prop) (setq prop (list prop)))
  9462. (if (memq 'org-tag prop)
  9463. prop
  9464. (cons 'org-tag prop))))))
  9465. (setq l (string-width (match-string 1))
  9466. c (if (< org-agenda-tags-column 0)
  9467. (- (abs org-agenda-tags-column) l)
  9468. org-agenda-tags-column))
  9469. (goto-char (match-beginning 1))
  9470. (delete-region (save-excursion (skip-chars-backward " \t") (point))
  9471. (point))
  9472. (insert (org-add-props
  9473. (make-string (max 1 (- c (current-column))) ?\s)
  9474. (plist-put (copy-sequence (text-properties-at (point)))
  9475. 'face nil))))
  9476. (goto-char (point-min))
  9477. (org-font-lock-add-tag-faces (point-max))))))
  9478. (defun org-agenda-priority-up ()
  9479. "Increase the priority of line at point, also in Org file."
  9480. (interactive)
  9481. (org-agenda-priority 'up))
  9482. (defun org-agenda-priority-down ()
  9483. "Decrease the priority of line at point, also in Org file."
  9484. (interactive)
  9485. (org-agenda-priority 'down))
  9486. (defun org-agenda-priority (&optional force-direction)
  9487. "Set the priority of line at point, also in Org file.
  9488. This changes the line at point, all other lines in the agenda
  9489. referring to the same tree node, and the headline of the tree
  9490. node in the Org file.
  9491. Called with one universal prefix arg, show the priority instead
  9492. of setting it.
  9493. When called programmatically, FORCE-DIRECTION can be `set', `up',
  9494. `down', or a character."
  9495. (interactive "P")
  9496. (unless org-priority-enable-commands
  9497. (user-error "Priority commands are disabled"))
  9498. (org-agenda-check-no-diary)
  9499. (let* ((col (current-column))
  9500. (hdmarker (org-get-at-bol 'org-hd-marker))
  9501. (buffer (marker-buffer hdmarker))
  9502. (pos (marker-position hdmarker))
  9503. (inhibit-read-only t)
  9504. newhead)
  9505. (org-with-remote-undo buffer
  9506. (with-current-buffer buffer
  9507. (widen)
  9508. (goto-char pos)
  9509. (org-fold-show-context 'agenda)
  9510. (org-priority force-direction)
  9511. (end-of-line 1)
  9512. (setq newhead (org-get-heading)))
  9513. (org-agenda-change-all-lines newhead hdmarker)
  9514. (org-move-to-column col))))
  9515. ;; FIXME: should fix the tags property of the agenda line.
  9516. (defun org-agenda-set-tags (&optional tag onoff)
  9517. "Set tags for the current headline."
  9518. (interactive)
  9519. (org-agenda-check-no-diary)
  9520. (if (and (org-region-active-p) (called-interactively-p 'any))
  9521. (call-interactively 'org-change-tag-in-region)
  9522. (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
  9523. (org-agenda-error)))
  9524. (buffer (marker-buffer hdmarker))
  9525. (pos (marker-position hdmarker))
  9526. (inhibit-read-only t)
  9527. newhead)
  9528. (org-with-remote-undo buffer
  9529. (with-current-buffer buffer
  9530. (widen)
  9531. (goto-char pos)
  9532. (org-fold-show-context 'agenda)
  9533. (if tag
  9534. (org-toggle-tag tag onoff)
  9535. (call-interactively #'org-set-tags-command))
  9536. (end-of-line 1)
  9537. (setq newhead (org-get-heading)))
  9538. (org-agenda-change-all-lines newhead hdmarker)
  9539. (beginning-of-line 1)))))
  9540. (defun org-agenda-set-property ()
  9541. "Set a property for the current headline."
  9542. (interactive)
  9543. (org-agenda-check-no-diary)
  9544. (org-agenda-maybe-loop
  9545. #'org-agenda-set-property nil nil nil
  9546. (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
  9547. (org-agenda-error)))
  9548. (buffer (marker-buffer hdmarker))
  9549. (pos (marker-position hdmarker))
  9550. (inhibit-read-only t)
  9551. ) ;; newhead
  9552. (org-with-remote-undo buffer
  9553. (with-current-buffer buffer
  9554. (widen)
  9555. (goto-char pos)
  9556. (org-fold-show-context 'agenda)
  9557. (call-interactively 'org-set-property))))))
  9558. (defun org-agenda-set-effort ()
  9559. "Set the effort property for the current headline."
  9560. (interactive)
  9561. (org-agenda-check-no-diary)
  9562. (org-agenda-maybe-loop
  9563. #'org-agenda-set-effort nil nil nil
  9564. (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
  9565. (org-agenda-error)))
  9566. (buffer (marker-buffer hdmarker))
  9567. (pos (marker-position hdmarker))
  9568. (inhibit-read-only t)
  9569. newhead)
  9570. (org-with-remote-undo buffer
  9571. (with-current-buffer buffer
  9572. (widen)
  9573. (goto-char pos)
  9574. (org-fold-show-context 'agenda)
  9575. (call-interactively 'org-set-effort)
  9576. (end-of-line 1)
  9577. (setq newhead (org-get-heading)))
  9578. (org-agenda-change-all-lines newhead hdmarker)))))
  9579. (defun org-agenda-toggle-archive-tag ()
  9580. "Toggle the archive tag for the current entry."
  9581. (interactive)
  9582. (org-agenda-check-no-diary)
  9583. (org-agenda-maybe-loop
  9584. #'org-agenda-toggle-archive-tag nil nil nil
  9585. (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
  9586. (org-agenda-error)))
  9587. (buffer (marker-buffer hdmarker))
  9588. (pos (marker-position hdmarker))
  9589. (inhibit-read-only t)
  9590. newhead)
  9591. (org-with-remote-undo buffer
  9592. (with-current-buffer buffer
  9593. (widen)
  9594. (goto-char pos)
  9595. (org-fold-show-context 'agenda)
  9596. (call-interactively 'org-toggle-archive-tag)
  9597. (end-of-line 1)
  9598. (setq newhead (org-get-heading)))
  9599. (org-agenda-change-all-lines newhead hdmarker)
  9600. (beginning-of-line 1)))))
  9601. (defun org-agenda-do-date-later (arg)
  9602. (interactive "P")
  9603. (cond
  9604. ((or (equal arg '(16))
  9605. (memq last-command
  9606. '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
  9607. (setq this-command 'org-agenda-date-later-minutes)
  9608. (org-agenda-date-later-minutes 1))
  9609. ((or (equal arg '(4))
  9610. (memq last-command
  9611. '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
  9612. (setq this-command 'org-agenda-date-later-hours)
  9613. (org-agenda-date-later-hours 1))
  9614. (t
  9615. (org-agenda-date-later (prefix-numeric-value arg)))))
  9616. (defun org-agenda-do-date-earlier (arg)
  9617. (interactive "P")
  9618. (cond
  9619. ((or (equal arg '(16))
  9620. (memq last-command
  9621. '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
  9622. (setq this-command 'org-agenda-date-earlier-minutes)
  9623. (org-agenda-date-earlier-minutes 1))
  9624. ((or (equal arg '(4))
  9625. (memq last-command
  9626. '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
  9627. (setq this-command 'org-agenda-date-earlier-hours)
  9628. (org-agenda-date-earlier-hours 1))
  9629. (t
  9630. (org-agenda-date-earlier (prefix-numeric-value arg)))))
  9631. (defun org-agenda-date-later (arg &optional what)
  9632. "Change the date of this item to ARG day(s) later."
  9633. (interactive "p")
  9634. (org-agenda-check-type t 'agenda)
  9635. (org-agenda-check-no-diary)
  9636. (let* ((marker (or (org-get-at-bol 'org-marker)
  9637. (org-agenda-error)))
  9638. (buffer (marker-buffer marker))
  9639. (pos (marker-position marker))
  9640. cdate today)
  9641. (org-with-remote-undo buffer
  9642. (with-current-buffer buffer
  9643. (widen)
  9644. (goto-char pos)
  9645. (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
  9646. (when (and org-agenda-move-date-from-past-immediately-to-today
  9647. (equal arg 1)
  9648. (or (not what) (eq what 'day))
  9649. (not (save-match-data (org-at-date-range-p))))
  9650. (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
  9651. cdate (calendar-absolute-from-gregorian
  9652. (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
  9653. today (org-today))
  9654. (when (> today cdate)
  9655. ;; immediately shift to today
  9656. (setq arg (- today cdate))))
  9657. (org-timestamp-change arg (or what 'day))
  9658. (when (and (org-at-date-range-p)
  9659. (re-search-backward org-tr-regexp-both
  9660. (line-beginning-position)))
  9661. (let ((end org-last-changed-timestamp))
  9662. (org-timestamp-change arg (or what 'day))
  9663. (setq org-last-changed-timestamp
  9664. (concat org-last-changed-timestamp "--" end)))))
  9665. (org-agenda-show-new-time marker org-last-changed-timestamp))
  9666. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  9667. (defun org-agenda-date-earlier (arg &optional what)
  9668. "Change the date of this item to ARG day(s) earlier."
  9669. (interactive "p")
  9670. (org-agenda-date-later (- arg) what))
  9671. (defun org-agenda-date-later-minutes (arg)
  9672. "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
  9673. (interactive "p")
  9674. (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
  9675. (org-agenda-date-later arg 'minute))
  9676. (defun org-agenda-date-earlier-minutes (arg)
  9677. "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
  9678. (interactive "p")
  9679. (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
  9680. (org-agenda-date-earlier arg 'minute))
  9681. (defun org-agenda-date-later-hours (arg)
  9682. "Change the time of this item, in hour steps."
  9683. (interactive "p")
  9684. (org-agenda-date-later arg 'hour))
  9685. (defun org-agenda-date-earlier-hours (arg)
  9686. "Change the time of this item, in hour steps."
  9687. (interactive "p")
  9688. (org-agenda-date-earlier arg 'hour))
  9689. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  9690. "Show new date stamp via text properties."
  9691. ;; We use text properties to make this undoable
  9692. (let ((inhibit-read-only t))
  9693. (setq stamp (concat prefix " => " stamp " "))
  9694. (save-excursion
  9695. (goto-char (point-max))
  9696. (while (not (bobp))
  9697. (when (equal marker (org-get-at-bol 'org-marker))
  9698. (remove-text-properties (line-beginning-position)
  9699. (line-end-position)
  9700. '(display nil))
  9701. (org-move-to-column
  9702. (- (window-max-chars-per-line)
  9703. (length stamp))
  9704. t)
  9705. (add-text-properties
  9706. (1- (point)) (line-end-position)
  9707. (list 'display (org-add-props stamp nil
  9708. 'face '(secondary-selection default))))
  9709. (beginning-of-line 1))
  9710. (beginning-of-line 0)))))
  9711. (defun org-agenda-date-prompt (arg)
  9712. "Change the date of this item. Date is prompted for, with default today.
  9713. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  9714. be used to request time specification in the time stamp."
  9715. (interactive "P")
  9716. (org-agenda-check-type t 'agenda)
  9717. (org-agenda-check-no-diary)
  9718. (org-agenda-maybe-loop
  9719. #'org-agenda-date-prompt arg t nil
  9720. (let* ((marker (or (org-get-at-bol 'org-marker)
  9721. (org-agenda-error)))
  9722. (buffer (marker-buffer marker))
  9723. (pos (marker-position marker)))
  9724. (org-with-remote-undo buffer
  9725. (with-current-buffer buffer
  9726. (widen)
  9727. (goto-char pos)
  9728. (unless (org-at-timestamp-p 'lax) (error "Cannot find time stamp"))
  9729. (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
  9730. (org-agenda-show-new-time marker org-last-changed-timestamp))
  9731. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  9732. (defun org-agenda-schedule (arg &optional time)
  9733. "Schedule the item at point.
  9734. ARG is passed through to `org-schedule'."
  9735. (interactive "P")
  9736. (org-agenda-check-type t 'agenda 'todo 'tags 'search)
  9737. (org-agenda-check-no-diary)
  9738. (org-agenda-maybe-loop
  9739. #'org-agenda-schedule arg t nil
  9740. (let* ((marker (or (org-get-at-bol 'org-marker)
  9741. (org-agenda-error)))
  9742. ;; (type (marker-insertion-type marker))
  9743. (buffer (marker-buffer marker))
  9744. (pos (marker-position marker))
  9745. ts)
  9746. (set-marker-insertion-type marker t)
  9747. (org-with-remote-undo buffer
  9748. (with-current-buffer buffer
  9749. (widen)
  9750. (goto-char pos)
  9751. (setq ts (org-schedule arg time)))
  9752. (org-agenda-show-new-time marker ts " S"))
  9753. (message "%s" ts))))
  9754. (defun org-agenda-deadline (arg &optional time)
  9755. "Schedule the item at point.
  9756. ARG is passed through to `org-deadline'."
  9757. (interactive "P")
  9758. (org-agenda-check-type t 'agenda 'todo 'tags 'search)
  9759. (org-agenda-check-no-diary)
  9760. (org-agenda-maybe-loop
  9761. #'org-agenda-deadline arg t nil
  9762. (let* ((marker (or (org-get-at-bol 'org-marker)
  9763. (org-agenda-error)))
  9764. (buffer (marker-buffer marker))
  9765. (pos (marker-position marker))
  9766. ts)
  9767. (org-with-remote-undo buffer
  9768. (with-current-buffer buffer
  9769. (widen)
  9770. (goto-char pos)
  9771. (setq ts (org-deadline arg time)))
  9772. (org-agenda-show-new-time marker ts " D"))
  9773. (message "%s" ts))))
  9774. (defun org-agenda-clock-in (&optional arg)
  9775. "Start the clock on the currently selected item."
  9776. (interactive "P")
  9777. (org-agenda-check-no-diary)
  9778. (if (equal arg '(4))
  9779. (org-clock-in arg)
  9780. (let* ((marker (or (org-get-at-bol 'org-marker)
  9781. (org-agenda-error)))
  9782. (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
  9783. (pos (marker-position marker))
  9784. (col (current-column))
  9785. newhead)
  9786. (org-with-remote-undo (marker-buffer marker)
  9787. (with-current-buffer (marker-buffer marker)
  9788. (widen)
  9789. (goto-char pos)
  9790. (org-fold-show-context 'agenda)
  9791. (org-clock-in arg)
  9792. (setq newhead (org-get-heading)))
  9793. (org-agenda-change-all-lines newhead hdmarker))
  9794. (org-move-to-column col))))
  9795. (defun org-agenda-clock-out ()
  9796. "Stop the currently running clock."
  9797. (interactive)
  9798. (unless (marker-buffer org-clock-marker)
  9799. (user-error "No running clock"))
  9800. (let ((marker (make-marker)) (col (current-column)) newhead)
  9801. (org-with-remote-undo (marker-buffer org-clock-marker)
  9802. (with-current-buffer (marker-buffer org-clock-marker)
  9803. (org-with-wide-buffer
  9804. (goto-char org-clock-marker)
  9805. (org-back-to-heading t)
  9806. (move-marker marker (point))
  9807. (org-clock-out)
  9808. (setq newhead (org-get-heading)))))
  9809. (org-agenda-change-all-lines newhead marker)
  9810. (move-marker marker nil)
  9811. (org-move-to-column col)
  9812. (org-agenda-unmark-clocking-task)))
  9813. (defun org-agenda-clock-cancel (&optional _arg)
  9814. "Cancel the currently running clock."
  9815. (interactive) ;; "P"
  9816. (unless (marker-buffer org-clock-marker)
  9817. (user-error "No running clock"))
  9818. (org-with-remote-undo (marker-buffer org-clock-marker)
  9819. (org-clock-cancel)))
  9820. (defun org-agenda-clock-goto ()
  9821. "Jump to the currently clocked in task within the agenda.
  9822. If the currently clocked in task is not listed in the agenda
  9823. buffer, display it in another window."
  9824. (interactive)
  9825. (let (pos)
  9826. (mapc (lambda (o)
  9827. (when (eq (overlay-get o 'type) 'org-agenda-clocking)
  9828. (setq pos (overlay-start o))))
  9829. (overlays-in (point-min) (point-max)))
  9830. (cond (pos (goto-char pos))
  9831. ;; If the currently clocked entry is not in the agenda
  9832. ;; buffer, we visit it in another window:
  9833. ((bound-and-true-p org-clock-current-task)
  9834. (org-switch-to-buffer-other-window (org-clock-goto)))
  9835. (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
  9836. (defun org-agenda-diary-entry-in-org-file ()
  9837. "Make a diary entry in the file `org-agenda-diary-file'."
  9838. (let (d1 d2 char (text "") dp1 dp2)
  9839. (if (equal (buffer-name) "*Calendar*")
  9840. (setq d1 (calendar-cursor-to-date t)
  9841. d2 (car calendar-mark-ring))
  9842. (setq dp1 (get-text-property (line-beginning-position) 'day))
  9843. (unless dp1 (user-error "No date defined in current line"))
  9844. (setq d1 (calendar-gregorian-from-absolute dp1)
  9845. d2 (and (ignore-errors (mark))
  9846. (save-excursion
  9847. (goto-char (mark))
  9848. (setq dp2 (get-text-property (line-beginning-position) 'day)))
  9849. (calendar-gregorian-from-absolute dp2))))
  9850. (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
  9851. (setq char (read-char-exclusive))
  9852. (cond
  9853. ((equal char ?d)
  9854. (setq text (read-string "Day entry: "))
  9855. (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
  9856. (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
  9857. ((equal char ?a)
  9858. (setq d1 (list (car d1) (nth 1 d1)
  9859. (read-number (format "Reference year [%d]: " (nth 2 d1))
  9860. (nth 2 d1))))
  9861. (setq text (read-string "Anniversary (use %d to show years): "))
  9862. (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
  9863. (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
  9864. ((equal char ?b)
  9865. (setq text (read-string "Block entry: "))
  9866. (unless (and d1 d2 (not (equal d1 d2)))
  9867. (user-error "No block of days selected"))
  9868. (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
  9869. (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
  9870. ((equal char ?j)
  9871. (org-switch-to-buffer-other-window
  9872. (find-file-noselect org-agenda-diary-file))
  9873. (require 'org-datetree)
  9874. (org-datetree-find-date-create d1)
  9875. (org-fold-reveal t))
  9876. (t (user-error "Invalid selection character `%c'" char)))))
  9877. (defcustom org-agenda-insert-diary-strategy 'date-tree
  9878. "Where in `org-agenda-diary-file' should new entries be added?
  9879. Valid values:
  9880. date-tree in the date tree, as first child of the date
  9881. date-tree-last in the date tree, as last child of the date
  9882. top-level as top-level entries at the end of the file."
  9883. :group 'org-agenda
  9884. :type '(choice
  9885. (const :tag "first in a date tree" date-tree)
  9886. (const :tag "last in a date tree" date-tree-last)
  9887. (const :tag "as top level at end of file" top-level)))
  9888. (defcustom org-agenda-insert-diary-extract-time nil
  9889. "Non-nil means extract any time specification from the diary entry."
  9890. :group 'org-agenda
  9891. :version "24.1"
  9892. :type 'boolean)
  9893. (defcustom org-agenda-bulk-mark-char ">"
  9894. "A single-character string to be used as the bulk mark."
  9895. :group 'org-agenda
  9896. :version "24.1"
  9897. :type 'string)
  9898. (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
  9899. "Add a diary entry with TYPE to `org-agenda-diary-file'.
  9900. If TEXT is not empty, it will become the headline of the new entry, and
  9901. the resulting entry will not be shown. When TEXT is empty, switch to
  9902. `org-agenda-diary-file' and let the user finish the entry there."
  9903. (let ((cw (current-window-configuration)))
  9904. (org-switch-to-buffer-other-window
  9905. (find-file-noselect org-agenda-diary-file))
  9906. (widen)
  9907. (goto-char (point-min))
  9908. (cl-case type
  9909. (anniversary
  9910. (or (re-search-forward "^\\*[ \t]+Anniversaries" nil t)
  9911. (progn
  9912. (or (org-at-heading-p)
  9913. (progn
  9914. (outline-next-heading)
  9915. (insert "* Anniversaries\n\n")
  9916. (beginning-of-line -1)))))
  9917. (outline-next-heading)
  9918. (org-back-over-empty-lines)
  9919. (backward-char 1)
  9920. (insert "\n")
  9921. (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
  9922. (nth 2 d1) (car d1) (nth 1 d1) text)))
  9923. (day
  9924. (let ((org-prefix-has-time t)
  9925. (org-agenda-time-leading-zero t)
  9926. fmt time time2)
  9927. (when org-agenda-insert-diary-extract-time
  9928. ;; Use org-agenda-format-item to parse text for a time-range and
  9929. ;; remove it. FIXME: This is a hack, we should refactor
  9930. ;; that function to make time extraction available separately
  9931. (setq fmt (org-agenda-format-item nil text nil nil nil t)
  9932. time (get-text-property 0 'time fmt)
  9933. time2 (if (> (length time) 0)
  9934. ;; split-string removes trailing ...... if
  9935. ;; no end time given. First space
  9936. ;; separates time from date.
  9937. (concat " " (car (split-string time "\\.")))
  9938. nil)
  9939. text (get-text-property 0 'txt fmt)))
  9940. (if (eq org-agenda-insert-diary-strategy 'top-level)
  9941. (org-agenda-insert-diary-as-top-level text)
  9942. (require 'org-datetree)
  9943. (org-datetree-find-date-create d1)
  9944. (org-agenda-insert-diary-make-new-entry text))
  9945. (org-insert-time-stamp (org-time-from-absolute
  9946. (calendar-absolute-from-gregorian d1))
  9947. nil nil nil nil time2))
  9948. (end-of-line 0))
  9949. ((block) ;; Wrap this in (strictly unnecessary) parens because
  9950. ;; otherwise the indentation gets confused by the
  9951. ;; special meaning of 'block
  9952. (when (> (calendar-absolute-from-gregorian d1)
  9953. (calendar-absolute-from-gregorian d2))
  9954. (setq d1 (prog1 d2 (setq d2 d1))))
  9955. (if (eq org-agenda-insert-diary-strategy 'top-level)
  9956. (org-agenda-insert-diary-as-top-level text)
  9957. (require 'org-datetree)
  9958. (org-datetree-find-date-create d1)
  9959. (org-agenda-insert-diary-make-new-entry text))
  9960. (org-insert-time-stamp (org-time-from-absolute
  9961. (calendar-absolute-from-gregorian d1)))
  9962. (insert "--")
  9963. (org-insert-time-stamp (org-time-from-absolute
  9964. (calendar-absolute-from-gregorian d2)))
  9965. (end-of-line 0)))
  9966. (if (string-match "\\S-" text)
  9967. (progn
  9968. (set-window-configuration cw)
  9969. (message "%s entry added to %s"
  9970. (capitalize (symbol-name type))
  9971. (abbreviate-file-name org-agenda-diary-file)))
  9972. (org-fold-reveal t)
  9973. (message "Please finish entry here"))))
  9974. (defun org-agenda-insert-diary-as-top-level (text)
  9975. "Make new entry as a top-level entry at the end of the file.
  9976. Add TEXT as headline, and position the cursor in the second line so that
  9977. a timestamp can be added there."
  9978. (widen)
  9979. (goto-char (point-max))
  9980. (unless (bolp) (insert "\n"))
  9981. (org-insert-heading nil t t)
  9982. (insert text)
  9983. (org-end-of-meta-data)
  9984. (unless (bolp) (insert "\n"))
  9985. (when org-adapt-indentation (indent-to-column 2)))
  9986. (defun org-agenda-insert-diary-make-new-entry (text)
  9987. "Make a new entry with TEXT as a child of the current subtree.
  9988. Position the point in the heading's first body line so that
  9989. a timestamp can be added there."
  9990. (cond
  9991. ((eq org-agenda-insert-diary-strategy 'date-tree-last)
  9992. (end-of-line)
  9993. (org-insert-heading '(4) t)
  9994. (org-do-demote))
  9995. (t
  9996. (outline-next-heading)
  9997. (org-back-over-empty-lines)
  9998. (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
  9999. (org-insert-heading nil t)
  10000. (org-do-demote)))
  10001. (let ((col (current-column)))
  10002. (insert text)
  10003. (org-end-of-meta-data)
  10004. ;; Ensure point is left on a blank line, at proper indentation.
  10005. (unless (bolp) (insert "\n"))
  10006. (unless (looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
  10007. (when org-adapt-indentation (indent-to-column col)))
  10008. (org-fold-show-set-visibility 'lineage))
  10009. (defun org-agenda-diary-entry ()
  10010. "Make a diary entry, like the `i' command from the calendar.
  10011. All the standard commands work: block, weekly etc.
  10012. When `org-agenda-diary-file' points to a file,
  10013. `org-agenda-diary-entry-in-org-file' is called instead to create
  10014. entries in that Org file."
  10015. (interactive)
  10016. (if (not (eq org-agenda-diary-file 'diary-file))
  10017. (org-agenda-diary-entry-in-org-file)
  10018. (require 'diary-lib)
  10019. (let* ((char (read-char-exclusive
  10020. "Diary entry: [d]ay [w]eekly [m]onthly [y]early\
  10021. [a]nniversary [b]lock [c]yclic"))
  10022. (cmd (cdr (assoc char
  10023. '((?d . diary-insert-entry)
  10024. (?w . diary-insert-weekly-entry)
  10025. (?m . diary-insert-monthly-entry)
  10026. (?y . diary-insert-yearly-entry)
  10027. (?a . diary-insert-anniversary-entry)
  10028. (?b . diary-insert-block-entry)
  10029. (?c . diary-insert-cyclic-entry)))))
  10030. (oldf (symbol-function 'calendar-cursor-to-date))
  10031. ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  10032. (point (point))
  10033. (mark (or (mark t) (point))))
  10034. (unless cmd
  10035. (user-error "No command associated with <%c>" char))
  10036. (unless (and (get-text-property point 'day)
  10037. (or (not (equal ?b char))
  10038. (get-text-property mark 'day)))
  10039. (user-error "Don't know which date to use for diary entry"))
  10040. ;; We implement this by hacking the `calendar-cursor-to-date' function
  10041. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  10042. (let ((calendar-mark-ring
  10043. (list (calendar-gregorian-from-absolute
  10044. (or (get-text-property mark 'day)
  10045. (get-text-property point 'day))))))
  10046. (unwind-protect
  10047. (progn
  10048. (fset 'calendar-cursor-to-date
  10049. (lambda (&optional _error _dummy)
  10050. (calendar-gregorian-from-absolute
  10051. (get-text-property point 'day))))
  10052. (call-interactively cmd))
  10053. (fset 'calendar-cursor-to-date oldf))))))
  10054. (defun org-agenda-execute-calendar-command (cmd)
  10055. "Execute a calendar command from the agenda with date from cursor."
  10056. (org-agenda-check-type t 'agenda)
  10057. (require 'diary-lib)
  10058. (unless (get-text-property (min (1- (point-max)) (point)) 'day)
  10059. (user-error "Don't know which date to use for the calendar command"))
  10060. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  10061. (point (point))
  10062. (date (calendar-gregorian-from-absolute
  10063. (get-text-property point 'day))))
  10064. ;; the following 2 vars are needed in the calendar
  10065. (org-dlet
  10066. ((displayed-month (car date))
  10067. (displayed-year (nth 2 date)))
  10068. (unwind-protect
  10069. (progn
  10070. (fset 'calendar-cursor-to-date
  10071. (lambda (&optional _error _dummy)
  10072. (calendar-gregorian-from-absolute
  10073. (get-text-property point 'day))))
  10074. (call-interactively cmd))
  10075. (fset 'calendar-cursor-to-date oldf)))))
  10076. (defun org-agenda-phases-of-moon ()
  10077. "Display the phases of the moon for the 3 months around the cursor date."
  10078. (interactive)
  10079. (org-agenda-execute-calendar-command 'calendar-lunar-phases))
  10080. (defun org-agenda-holidays ()
  10081. "Display the holidays for the 3 months around the cursor date."
  10082. (interactive)
  10083. (org-agenda-execute-calendar-command 'calendar-list-holidays))
  10084. (defvar calendar-longitude) ; defined in calendar.el
  10085. (defvar calendar-latitude) ; defined in calendar.el
  10086. (defvar calendar-location-name) ; defined in calendar.el
  10087. (defun org-agenda-sunrise-sunset (arg)
  10088. "Display sunrise and sunset for the cursor date.
  10089. Latitude and longitude can be specified with the variables
  10090. `calendar-latitude' and `calendar-longitude'. When called with prefix
  10091. argument, latitude and longitude will be prompted for."
  10092. (interactive "P")
  10093. (require 'solar)
  10094. (let ((calendar-longitude (if arg nil calendar-longitude))
  10095. (calendar-latitude (if arg nil calendar-latitude))
  10096. (calendar-location-name
  10097. (if arg "the given coordinates" calendar-location-name)))
  10098. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  10099. (defun org-agenda-goto-calendar ()
  10100. "Open the Emacs calendar with the date at the cursor."
  10101. (interactive)
  10102. (org-agenda-check-type t 'agenda)
  10103. (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
  10104. (user-error "Don't know which date to open in calendar")))
  10105. (date (calendar-gregorian-from-absolute day))
  10106. (calendar-move-hook nil)
  10107. (calendar-view-holidays-initially-flag nil)
  10108. (calendar-view-diary-initially-flag nil))
  10109. (calendar)
  10110. (calendar-goto-date date)))
  10111. ;;;###autoload
  10112. (defun org-calendar-goto-agenda ()
  10113. "Compute the Org agenda for the calendar date displayed at the cursor.
  10114. This is a command that has to be installed in `calendar-mode-map'."
  10115. (interactive)
  10116. ;; Temporarily disable sticky agenda since user clearly wants to
  10117. ;; refresh view anyway.
  10118. (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
  10119. (org-agenda-sticky nil))
  10120. (org-agenda-list nil (calendar-absolute-from-gregorian
  10121. (calendar-cursor-to-date))
  10122. nil)))
  10123. (defun org-agenda-convert-date ()
  10124. (interactive)
  10125. (org-agenda-check-type t 'agenda)
  10126. (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
  10127. date s)
  10128. (unless day
  10129. (user-error "Don't know which date to convert"))
  10130. (setq date (calendar-gregorian-from-absolute day))
  10131. (setq s (concat
  10132. "Gregorian: " (calendar-date-string date) "\n"
  10133. "ISO: " (calendar-iso-date-string date) "\n"
  10134. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  10135. "Julian: " (calendar-julian-date-string date) "\n"
  10136. "Astron. JD: " (calendar-astro-date-string date)
  10137. " (Julian date number at noon UTC)\n"
  10138. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  10139. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  10140. "French: " (calendar-french-date-string date) "\n"
  10141. "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
  10142. "Mayan: " (calendar-mayan-date-string date) "\n"
  10143. "Coptic: " (calendar-coptic-date-string date) "\n"
  10144. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  10145. "Persian: " (calendar-persian-date-string date) "\n"
  10146. "Chinese: " (calendar-chinese-date-string date) "\n"))
  10147. (with-output-to-temp-buffer "*Dates*"
  10148. (princ s))
  10149. (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
  10150. ;;; Bulk commands
  10151. (defun org-agenda-bulk-marked-p ()
  10152. "Non-nil when current entry is marked for bulk action."
  10153. (eq (get-char-property (line-beginning-position) 'type)
  10154. 'org-marked-entry-overlay))
  10155. (defun org-agenda-bulk-mark (&optional arg)
  10156. "Mark entries for future bulk action.
  10157. When ARG is nil or one and region is not active then mark the
  10158. entry at point.
  10159. When ARG is nil or one and region is active then mark the entries
  10160. in the region.
  10161. When ARG is greater than one mark ARG lines."
  10162. (interactive "p")
  10163. (when (and (or (not arg) (= arg 1)) (use-region-p))
  10164. (setq arg (count-lines (region-beginning) (region-end)))
  10165. (goto-char (region-beginning))
  10166. (deactivate-mark))
  10167. (dotimes (_ (or arg 1))
  10168. (unless (org-get-at-bol 'org-agenda-diary-link)
  10169. (let* ((m (org-get-at-bol 'org-hd-marker))
  10170. ov)
  10171. (unless (org-agenda-bulk-marked-p)
  10172. (unless m (user-error "Nothing to mark at point"))
  10173. (push m org-agenda-bulk-marked-entries)
  10174. (setq ov (make-overlay (line-beginning-position)
  10175. (+ 2 (line-beginning-position))))
  10176. (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
  10177. (org-get-todo-face "TODO")
  10178. 'evaporate)
  10179. (overlay-put ov 'type 'org-marked-entry-overlay))
  10180. (end-of-line 1)
  10181. (or (ignore-errors
  10182. (goto-char (next-single-property-change (point) 'org-hd-marker)))
  10183. (beginning-of-line 2))
  10184. (while (and (get-char-property (point) 'invisible) (not (eobp)))
  10185. (beginning-of-line 2)))))
  10186. (message "%d entries marked for bulk action"
  10187. (length org-agenda-bulk-marked-entries)))
  10188. (defun org-agenda-bulk-mark-all ()
  10189. "Mark all entries for future agenda bulk action."
  10190. (interactive)
  10191. (org-agenda-bulk-mark-regexp "."))
  10192. (defun org-agenda-bulk-mark-regexp (regexp)
  10193. "Mark entries matching REGEXP for future agenda bulk action."
  10194. (interactive "sMark entries matching regexp: ")
  10195. (let ((entries-marked 0) txt-at-point)
  10196. (save-excursion
  10197. (goto-char (point-min))
  10198. (goto-char (next-single-property-change (point) 'org-hd-marker))
  10199. (while (and (re-search-forward regexp nil t)
  10200. (setq txt-at-point
  10201. (get-text-property (match-beginning 0) 'txt)))
  10202. (if (get-char-property (point) 'invisible)
  10203. (beginning-of-line 2)
  10204. (when (string-match-p regexp txt-at-point)
  10205. (setq entries-marked (1+ entries-marked))
  10206. (call-interactively 'org-agenda-bulk-mark)))))
  10207. (unless entries-marked
  10208. (message "No entry matching this regexp."))))
  10209. (defun org-agenda-bulk-unmark (&optional arg)
  10210. "Unmark the entry at point for future bulk action."
  10211. (interactive "P")
  10212. (if arg
  10213. (org-agenda-bulk-unmark-all)
  10214. (cond ((org-agenda-bulk-marked-p)
  10215. (org-agenda-bulk-remove-overlays
  10216. (line-beginning-position) (+ 2 (line-beginning-position)))
  10217. (setq org-agenda-bulk-marked-entries
  10218. (delete (org-get-at-bol 'org-hd-marker)
  10219. org-agenda-bulk-marked-entries))
  10220. (end-of-line 1)
  10221. (or (ignore-errors
  10222. (goto-char (next-single-property-change (point) 'txt)))
  10223. (beginning-of-line 2))
  10224. (while (and (get-char-property (point) 'invisible) (not (eobp)))
  10225. (beginning-of-line 2))
  10226. (message "%d entries left marked for bulk action"
  10227. (length org-agenda-bulk-marked-entries)))
  10228. (t (message "No entry to unmark here")))))
  10229. (defun org-agenda-bulk-toggle-all ()
  10230. "Toggle all marks for bulk action."
  10231. (interactive)
  10232. (save-excursion
  10233. (goto-char (point-min))
  10234. (while (ignore-errors
  10235. (goto-char (next-single-property-change (point) 'org-hd-marker)))
  10236. (org-agenda-bulk-toggle))))
  10237. (defun org-agenda-bulk-toggle ()
  10238. "Toggle the mark at point for bulk action."
  10239. (interactive)
  10240. (if (org-agenda-bulk-marked-p)
  10241. (org-agenda-bulk-unmark)
  10242. (org-agenda-bulk-mark)))
  10243. (defun org-agenda-bulk-remove-overlays (&optional beg end)
  10244. "Remove the mark overlays between BEG and END in the agenda buffer.
  10245. BEG and END default to the buffer limits.
  10246. This only removes the overlays, it does not remove the markers
  10247. from the list in `org-agenda-bulk-marked-entries'."
  10248. (interactive)
  10249. (mapc (lambda (ov)
  10250. (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
  10251. (delete-overlay ov)))
  10252. (overlays-in (or beg (point-min)) (or end (point-max)))))
  10253. (defun org-agenda-bulk-unmark-all ()
  10254. "Remove all marks in the agenda buffer.
  10255. This will remove the markers and the overlays."
  10256. (interactive)
  10257. (if (null org-agenda-bulk-marked-entries)
  10258. (message "No entry to unmark")
  10259. (setq org-agenda-bulk-marked-entries nil)
  10260. (org-agenda-bulk-remove-overlays (point-min) (point-max))))
  10261. (defcustom org-agenda-persistent-marks nil
  10262. "Non-nil means marked items will stay marked after a bulk action.
  10263. You can toggle this interactively by typing `p' when prompted for a
  10264. bulk action."
  10265. :group 'org-agenda
  10266. :version "24.1"
  10267. :type 'boolean)
  10268. (defcustom org-agenda-loop-over-headlines-in-active-region t
  10269. "Shall some commands act upon headlines in the active region?
  10270. When set to t, some commands will be performed in all headlines
  10271. within the active region.
  10272. When set to `start-level', some commands will be performed in all
  10273. headlines within the active region, provided that these headlines
  10274. are of the same level than the first one.
  10275. When set to a regular expression, those commands will be
  10276. performed on the matching headlines within the active region.
  10277. The list of commands is: `org-agenda-schedule',
  10278. `org-agenda-deadline', `org-agenda-date-prompt',
  10279. `org-agenda-todo', `org-agenda-archive*', `org-agenda-kill'.
  10280. See `org-loop-over-headlines-in-active-region' for the equivalent
  10281. option for Org buffers."
  10282. :type '(choice (const :tag "Don't loop" nil)
  10283. (const :tag "All headlines in active region" t)
  10284. (const :tag "In active region, headlines at the same level than the first one" start-level)
  10285. (regexp :tag "Regular expression matcher"))
  10286. :version "27.1"
  10287. :package-version '(Org . "9.4")
  10288. :group 'org-agenda)
  10289. (defun org-agenda-bulk-action (&optional arg)
  10290. "Execute an remote-editing action on all marked entries.
  10291. The prefix arg is passed through to the command if possible."
  10292. (interactive "P")
  10293. ;; When there is no mark, act on the agenda entry at point.
  10294. (if (not org-agenda-bulk-marked-entries)
  10295. (save-excursion (org-agenda-bulk-mark)))
  10296. (dolist (m org-agenda-bulk-marked-entries)
  10297. (unless (and (markerp m)
  10298. (marker-buffer m)
  10299. (buffer-live-p (marker-buffer m))
  10300. (marker-position m))
  10301. (user-error "Marker %s for bulk command is invalid" m)))
  10302. ;; Prompt for the bulk command.
  10303. (org-unlogged-message
  10304. (concat "Bulk (" (if org-agenda-persistent-marks "" "don't ") "[p]ersist marks): "
  10305. "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
  10306. "[S]catter [f]unction "
  10307. (and org-agenda-bulk-custom-functions
  10308. (format " Custom: [%s]"
  10309. (mapconcat (lambda (f) (char-to-string (car f)))
  10310. org-agenda-bulk-custom-functions
  10311. "")))))
  10312. (catch 'exit
  10313. (let* ((org-log-refile (if org-log-refile 'time nil))
  10314. (entries (reverse org-agenda-bulk-marked-entries))
  10315. (org-overriding-default-time
  10316. (and (get-text-property (point) 'org-agenda-date-header)
  10317. (org-get-cursor-date)))
  10318. redo-at-end
  10319. cmd)
  10320. (pcase (read-char-exclusive)
  10321. (?p
  10322. (let ((org-agenda-persistent-marks
  10323. (not org-agenda-persistent-marks)))
  10324. (org-agenda-bulk-action)
  10325. (throw 'exit nil)))
  10326. (?$
  10327. (setq cmd #'org-agenda-archive))
  10328. (?A
  10329. (setq cmd #'org-agenda-archive-to-archive-sibling))
  10330. ((or ?r ?w)
  10331. (let ((refile-location
  10332. (org-refile-get-location
  10333. "Refile to"
  10334. (marker-buffer (car entries))
  10335. org-refile-allow-creating-parent-nodes)))
  10336. (when (nth 3 refile-location)
  10337. (setcar (nthcdr 3 refile-location)
  10338. (move-marker
  10339. (make-marker)
  10340. (nth 3 refile-location)
  10341. (or (get-file-buffer (nth 1 refile-location))
  10342. (find-buffer-visiting (nth 1 refile-location))
  10343. (error "This should not happen")))))
  10344. (setq cmd (lambda () (org-agenda-refile nil refile-location t)))
  10345. (setq redo-at-end t)))
  10346. (?t
  10347. (let ((state (completing-read
  10348. "Todo state: "
  10349. (with-current-buffer (marker-buffer (car entries))
  10350. (mapcar #'list org-todo-keywords-1)))))
  10351. (setq cmd (lambda ()
  10352. (let ((org-inhibit-blocking t)
  10353. (org-inhibit-logging 'note))
  10354. (org-agenda-todo state))))))
  10355. ((and (or ?- ?+) action)
  10356. (let ((tag (completing-read
  10357. (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
  10358. (with-current-buffer (marker-buffer (car entries))
  10359. (delq nil
  10360. (mapcar (lambda (x) (and (stringp (car x)) x))
  10361. org-current-tag-alist))))))
  10362. (setq cmd
  10363. (lambda ()
  10364. (org-agenda-set-tags tag
  10365. (if (eq action ?+) 'on 'off))))))
  10366. ((and (or ?s ?d) c)
  10367. (let* ((schedule? (eq c ?s))
  10368. (prompt (if schedule? "(Re)Schedule to" "(Re)Set Deadline to"))
  10369. (time
  10370. (and (not arg)
  10371. (let ((new (org-read-date
  10372. nil nil nil prompt org-overriding-default-time)))
  10373. ;; A "double plus" answer applies to every
  10374. ;; scheduled time. Do not turn it into
  10375. ;; a fixed date yet.
  10376. (if (string-match-p "\\`[ \t]*\\+\\+"
  10377. org-read-date-final-answer)
  10378. org-read-date-final-answer
  10379. new)))))
  10380. ;; Make sure to not prompt for a note when bulk
  10381. ;; rescheduling/resetting deadline as Org cannot cope with
  10382. ;; simultaneous notes. Besides, it could be annoying
  10383. ;; depending on the number of marked items.
  10384. (setq cmd
  10385. (if schedule?
  10386. (lambda ()
  10387. (let ((org-log-reschedule
  10388. (and org-log-reschedule 'time)))
  10389. (org-agenda-schedule arg time)))
  10390. (lambda ()
  10391. (let ((org-log-redeadline (and org-log-redeadline 'time)))
  10392. (org-agenda-deadline arg time)))))))
  10393. (?S
  10394. (unless (org-agenda-check-type nil 'agenda 'todo)
  10395. (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type))
  10396. (let ((days (read-number
  10397. (format "Scatter tasks across how many %sdays: "
  10398. (if arg "week" ""))
  10399. 7)))
  10400. (setq cmd
  10401. (lambda ()
  10402. (let ((distance (1+ (random days))))
  10403. (when arg
  10404. (let ((dist distance)
  10405. (day-of-week
  10406. (calendar-day-of-week
  10407. (calendar-gregorian-from-absolute (org-today)))))
  10408. (dotimes (_ (1+ dist))
  10409. (while (member day-of-week org-agenda-weekend-days)
  10410. (cl-incf distance)
  10411. (cl-incf day-of-week)
  10412. (when (= day-of-week 7)
  10413. (setq day-of-week 0)))
  10414. (cl-incf day-of-week)
  10415. (when (= day-of-week 7)
  10416. (setq day-of-week 0)))))
  10417. ;; Silently fail when try to replan a sexp entry.
  10418. (ignore-errors
  10419. (let* ((date (calendar-gregorian-from-absolute
  10420. (+ (org-today) distance)))
  10421. (time (org-encode-time
  10422. 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10423. (org-agenda-schedule nil time))))))))
  10424. (?f
  10425. (setq cmd
  10426. (intern
  10427. (completing-read "Function: " obarray #'fboundp t nil nil))))
  10428. (action
  10429. (setq cmd
  10430. (pcase (assoc action org-agenda-bulk-custom-functions)
  10431. (`(,_ ,fn)
  10432. fn)
  10433. (`(,_ ,fn ,arg-fn)
  10434. (apply #'apply-partially fn (funcall arg-fn)))
  10435. (_
  10436. (user-error "Invalid bulk action: %c" action))))
  10437. (setq redo-at-end t)))
  10438. ;; Sort the markers, to make sure that parents are handled
  10439. ;; before children.
  10440. (setq entries (sort entries
  10441. (lambda (a b)
  10442. (cond
  10443. ((eq (marker-buffer a) (marker-buffer b))
  10444. (< (marker-position a) (marker-position b)))
  10445. (t
  10446. (string< (buffer-name (marker-buffer a))
  10447. (buffer-name (marker-buffer b))))))))
  10448. ;; Now loop over all markers and apply CMD.
  10449. (let ((processed 0)
  10450. (skipped 0))
  10451. (dolist (e entries)
  10452. (let ((pos (text-property-any (point-min) (point-max) 'org-hd-marker e)))
  10453. (if (not pos)
  10454. (progn (message "Skipping removed entry at %s" e)
  10455. (cl-incf skipped))
  10456. (goto-char pos)
  10457. (let (org-loop-over-headlines-in-active-region) (funcall cmd))
  10458. ;; `post-command-hook' is not run yet. We make sure any
  10459. ;; pending log note is processed.
  10460. (when org-log-setup (org-add-log-note))
  10461. (cl-incf processed))))
  10462. (when redo-at-end (org-agenda-redo))
  10463. (unless org-agenda-persistent-marks (org-agenda-bulk-unmark-all))
  10464. (message "Acted on %d entries%s%s"
  10465. processed
  10466. (if (= skipped 0)
  10467. ""
  10468. (format ", skipped %d (disappeared before their turn)"
  10469. skipped))
  10470. (if (not org-agenda-persistent-marks) "" " (kept marked)"))))))
  10471. (defun org-agenda-capture (&optional with-time)
  10472. "Call `org-capture' with the date at point.
  10473. With a `C-1' prefix, use the HH:MM value at point (if any) or the
  10474. current HH:MM time."
  10475. (interactive "P")
  10476. (if (not (eq major-mode 'org-agenda-mode))
  10477. (user-error "You cannot do this outside of agenda buffers")
  10478. (let ((org-overriding-default-time
  10479. (org-get-cursor-date (equal with-time 1))))
  10480. (call-interactively 'org-capture))))
  10481. ;;; Dragging agenda lines forward/backward
  10482. (defun org-agenda-reapply-filters ()
  10483. "Re-apply all agenda filters."
  10484. (mapcar
  10485. (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
  10486. `((,org-agenda-tag-filter tag)
  10487. (,org-agenda-category-filter category)
  10488. (,org-agenda-regexp-filter regexp)
  10489. (,org-agenda-effort-filter effort)
  10490. (,(assoc-default 'tag org-agenda-filters-preset) tag)
  10491. (,(assoc-default 'category org-agenda-filters-preset) category)
  10492. (,(assoc-default 'effort org-agenda-filters-preset) effort)
  10493. (,(assoc-default 'regexp org-agenda-filters-preset) regexp))))
  10494. (defun org-agenda-drag-line-forward (arg &optional backward)
  10495. "Drag an agenda line forward by ARG lines.
  10496. When the optional argument `backward' is non-nil, move backward."
  10497. (interactive "p")
  10498. (let ((inhibit-read-only t) lst line)
  10499. (if (or (not (get-text-property (point) 'txt))
  10500. (save-excursion
  10501. (dotimes (_ arg)
  10502. (move-beginning-of-line (if backward 0 2))
  10503. (push (not (get-text-property (point) 'txt)) lst))
  10504. (delq nil lst)))
  10505. (message "Cannot move line forward")
  10506. (let ((end (save-excursion (move-beginning-of-line 2) (point))))
  10507. (move-beginning-of-line 1)
  10508. (setq line (buffer-substring (point) end))
  10509. (delete-region (point) end)
  10510. (move-beginning-of-line (funcall (if backward '1- '1+) arg))
  10511. (insert line)
  10512. (org-agenda-reapply-filters)
  10513. (org-agenda-mark-clocking-task)
  10514. (move-beginning-of-line 0)))))
  10515. (defun org-agenda-drag-line-backward (arg)
  10516. "Drag an agenda line backward by ARG lines."
  10517. (interactive "p")
  10518. (org-agenda-drag-line-forward arg t))
  10519. ;;; Flagging notes
  10520. (defun org-agenda-show-the-flagging-note ()
  10521. "Display the flagging note in the other window.
  10522. When called a second time in direct sequence, offer to remove the FLAGGING
  10523. tag and (if present) the flagging note."
  10524. (interactive)
  10525. (let ((hdmarker (org-get-at-bol 'org-hd-marker))
  10526. (win (selected-window))
  10527. note) ;; heading newhead
  10528. (unless hdmarker
  10529. (user-error "No linked entry at point"))
  10530. (if (and (eq this-command last-command)
  10531. (y-or-n-p "Unflag and remove any flagging note? "))
  10532. (progn
  10533. (org-agenda-remove-flag hdmarker)
  10534. (let ((win (get-buffer-window "*Flagging Note*")))
  10535. (and win (delete-window win)))
  10536. (message "Entry unflagged"))
  10537. (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
  10538. (unless note
  10539. (user-error "No flagging note"))
  10540. (org-kill-new note)
  10541. (org-switch-to-buffer-other-window "*Flagging Note*")
  10542. (erase-buffer)
  10543. (insert note)
  10544. (goto-char (point-min))
  10545. (while (re-search-forward "\\\\n" nil t)
  10546. (replace-match "\n" t t))
  10547. (goto-char (point-min))
  10548. (select-window win)
  10549. (message "%s" (substitute-command-keys "Flagging note pushed to \
  10550. kill ring. Press `\\[org-agenda-show-the-flagging-note]' again to remove \
  10551. tag and note")))))
  10552. (defun org-agenda-remove-flag (marker)
  10553. "Remove the FLAGGED tag and any flagging note in the entry."
  10554. (let ((newhead
  10555. (org-with-point-at marker
  10556. (org-toggle-tag "FLAGGED" 'off)
  10557. (org-entry-delete nil "THEFLAGGINGNOTE")
  10558. (org-get-heading))))
  10559. (org-agenda-change-all-lines newhead marker)
  10560. (message "Entry unflagged")))
  10561. (defun org-agenda-get-any-marker (&optional pos)
  10562. (or (get-text-property (or pos (line-beginning-position)) 'org-hd-marker)
  10563. (get-text-property (or pos (line-beginning-position)) 'org-marker)))
  10564. ;;; Appointment reminders
  10565. (defvar appt-time-msg-list) ; defined in appt.el
  10566. ;;;###autoload
  10567. (defun org-agenda-to-appt (&optional refresh filter &rest args)
  10568. "Activate appointments found in `org-agenda-files'.
  10569. With a `\\[universal-argument]' prefix, refresh the list of \
  10570. appointments.
  10571. If FILTER is t, interactively prompt the user for a regular
  10572. expression, and filter out entries that don't match it.
  10573. If FILTER is a string, use this string as a regular expression
  10574. for filtering entries out.
  10575. If FILTER is a function, filter out entries against which
  10576. calling the function returns nil. This function takes one
  10577. argument: an entry from `org-agenda-get-day-entries'.
  10578. FILTER can also be an alist with the car of each cell being
  10579. either `headline' or `category'. For example:
  10580. ((headline \"IMPORTANT\")
  10581. (category \"Work\"))
  10582. will only add headlines containing IMPORTANT or headlines
  10583. belonging to the \"Work\" category.
  10584. ARGS are symbols indicating what kind of entries to consider.
  10585. By default `org-agenda-to-appt' will use :deadline*, :scheduled*
  10586. \(i.e., deadlines and scheduled items with a hh:mm specification)
  10587. and :timestamp entries. See the docstring of `org-diary' for
  10588. details and examples.
  10589. If an entry has a APPT_WARNTIME property, its value will be used
  10590. to override `appt-message-warning-time'."
  10591. (interactive "P")
  10592. (when refresh (setq appt-time-msg-list nil))
  10593. (when (eq filter t)
  10594. (setq filter (read-from-minibuffer "Regexp filter: ")))
  10595. (let* ((cnt 0) ; count added events
  10596. (scope (or args '(:deadline* :scheduled* :timestamp)))
  10597. (org-agenda-new-buffers nil)
  10598. (org-deadline-warning-days 0)
  10599. ;; Do not use `org-today' here because appt only takes
  10600. ;; time and without date as argument, so it may pass wrong
  10601. ;; information otherwise
  10602. (today (org-date-to-gregorian
  10603. (time-to-days nil)))
  10604. (org-agenda-restrict nil)
  10605. (files (org-agenda-files 'unrestricted)) entries file
  10606. (org-agenda-buffer nil))
  10607. ;; Get all entries which may contain an appt
  10608. (org-agenda-prepare-buffers files)
  10609. (while (setq file (pop files))
  10610. (setq entries
  10611. (delq nil
  10612. (append entries
  10613. (apply #'org-agenda-get-day-entries
  10614. file today scope)))))
  10615. ;; Map through entries and find if we should filter them out
  10616. (mapc
  10617. (lambda (x)
  10618. (let* ((evt (org-trim
  10619. (replace-regexp-in-string
  10620. org-link-bracket-re "\\2"
  10621. (or (get-text-property 1 'txt x) ""))))
  10622. (cat (get-text-property (1- (length x)) 'org-category x))
  10623. (tod (get-text-property 1 'time-of-day x))
  10624. (ok (or (null filter)
  10625. (and (stringp filter) (string-match filter evt))
  10626. (and (functionp filter) (funcall filter x))
  10627. (and (listp filter)
  10628. (let ((cat-filter (cadr (assq 'category filter)))
  10629. (evt-filter (cadr (assq 'headline filter))))
  10630. (or (and (stringp cat-filter)
  10631. (string-match cat-filter cat))
  10632. (and (stringp evt-filter)
  10633. (string-match evt-filter evt)))))))
  10634. (wrn (get-text-property 1 'warntime x)))
  10635. ;; FIXME: Shall we remove text-properties for the appt text?
  10636. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  10637. (when (and ok tod (not (string-match "\\`DONE\\|CANCELLED" evt)))
  10638. (setq tod (concat "00" (number-to-string tod)))
  10639. (setq tod (when (string-match
  10640. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
  10641. (concat (match-string 1 tod) ":"
  10642. (match-string 2 tod))))
  10643. (when (appt-add tod evt wrn)
  10644. (setq cnt (1+ cnt))))))
  10645. entries)
  10646. (org-release-buffers org-agenda-new-buffers)
  10647. (if (eq cnt 0)
  10648. (message "No event to add")
  10649. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  10650. (defun org-agenda-today-p (date)
  10651. "Non-nil when DATE means today.
  10652. DATE is either a list of the form (month day year) or a number of
  10653. days as returned by `calendar-absolute-from-gregorian' or
  10654. `org-today'. This function considers `org-extend-today-until'
  10655. when defining today."
  10656. (eq (org-today)
  10657. (if (consp date) (calendar-absolute-from-gregorian date) date)))
  10658. (defun org-agenda-todo-yesterday (&optional arg)
  10659. "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
  10660. (interactive "P")
  10661. (let* ((org-use-effective-time t)
  10662. (hour (nth 2 (decode-time (org-current-time))))
  10663. (org-extend-today-until (1+ hour)))
  10664. (org-agenda-todo arg)))
  10665. (defun org-agenda-ctrl-c-ctrl-c ()
  10666. "Set tags in agenda buffer."
  10667. (interactive)
  10668. (org-agenda-set-tags))
  10669. (provide 'org-agenda)
  10670. ;;; org-agenda.el ends here