org-agenda.el 247 KB

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