org-agenda.el 239 KB

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