org-agenda.el 285 KB

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