org-agenda.el 221 KB

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