org-agenda.el 245 KB

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