org-agenda.el 236 KB

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