org.el 1024 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.18a
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.17a"
  81. "The version number of the file org.el.")
  82. (defun org-version ()
  83. (interactive)
  84. (message "Org-mode version %s" org-version))
  85. ;;; Compatibility constants
  86. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  87. (defconst org-format-transports-properties-p
  88. (let ((x "a"))
  89. (add-text-properties 0 1 '(test t) x)
  90. (get-text-property 0 'test (format "%s" x)))
  91. "Does format transport text properties?")
  92. (defmacro org-bound-and-true-p (var)
  93. "Return the value of symbol VAR if it is bound, else nil."
  94. `(and (boundp (quote ,var)) ,var))
  95. (defmacro org-unmodified (&rest body)
  96. "Execute body without changing buffer-modified-p."
  97. `(set-buffer-modified-p
  98. (prog1 (buffer-modified-p) ,@body)))
  99. (defmacro org-re (s)
  100. "Replace posix classes in regular expression."
  101. (if (featurep 'xemacs)
  102. (let ((ss s))
  103. (save-match-data
  104. (while (string-match "\\[:alnum:\\]" ss)
  105. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  106. (while (string-match "\\[:alpha:\\]" ss)
  107. (setq ss (replace-match "a-zA-Z" t t ss)))
  108. ss))
  109. s))
  110. (defmacro org-preserve-lc (&rest body)
  111. `(let ((_line (org-current-line))
  112. (_col (current-column)))
  113. (unwind-protect
  114. (progn ,@body)
  115. (goto-line _line)
  116. (move-to-column _col))))
  117. (defmacro org-without-partial-completion (&rest body)
  118. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  119. partial-completion-mode)))
  120. (unwind-protect
  121. (progn
  122. (if pc-mode (partial-completion-mode -1))
  123. ,@body)
  124. (if pc-mode (partial-completion-mode 1)))))
  125. ;;; The custom variables
  126. (defgroup org nil
  127. "Outline-based notes management and organizer."
  128. :tag "Org"
  129. :group 'outlines
  130. :group 'hypermedia
  131. :group 'calendar)
  132. ;; FIXME: Needs a separate group...
  133. (defcustom org-completion-fallback-command 'hippie-expand
  134. "The expansion command called by \\[org-complete] in normal context.
  135. Normal means, no org-mode-specific context."
  136. :group 'org
  137. :type 'function)
  138. (defgroup org-startup nil
  139. "Options concerning startup of Org-mode."
  140. :tag "Org Startup"
  141. :group 'org)
  142. (defcustom org-startup-folded t
  143. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  144. This can also be configured on a per-file basis by adding one of
  145. the following lines anywhere in the buffer:
  146. #+STARTUP: fold
  147. #+STARTUP: nofold
  148. #+STARTUP: content"
  149. :group 'org-startup
  150. :type '(choice
  151. (const :tag "nofold: show all" nil)
  152. (const :tag "fold: overview" t)
  153. (const :tag "content: all headlines" content)))
  154. (defcustom org-startup-truncated t
  155. "Non-nil means, entering Org-mode will set `truncate-lines'.
  156. This is useful since some lines containing links can be very long and
  157. uninteresting. Also tables look terrible when wrapped."
  158. :group 'org-startup
  159. :type 'boolean)
  160. (defcustom org-startup-align-all-tables nil
  161. "Non-nil means, align all tables when visiting a file.
  162. This is useful when the column width in tables is forced with <N> cookies
  163. in table fields. Such tables will look correct only after the first re-align.
  164. This can also be configured on a per-file basis by adding one of
  165. the following lines anywhere in the buffer:
  166. #+STARTUP: align
  167. #+STARTUP: noalign"
  168. :group 'org-startup
  169. :type 'boolean)
  170. (defcustom org-insert-mode-line-in-empty-file nil
  171. "Non-nil means insert the first line setting Org-mode in empty files.
  172. When the function `org-mode' is called interactively in an empty file, this
  173. normally means that the file name does not automatically trigger Org-mode.
  174. To ensure that the file will always be in Org-mode in the future, a
  175. line enforcing Org-mode will be inserted into the buffer, if this option
  176. has been set."
  177. :group 'org-startup
  178. :type 'boolean)
  179. (defcustom org-replace-disputed-keys nil
  180. "Non-nil means use alternative key bindings for some keys.
  181. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  182. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  183. If you want to use Org-mode together with one of these other modes,
  184. or more generally if you would like to move some Org-mode commands to
  185. other keys, set this variable and configure the keys with the variable
  186. `org-disputed-keys'.
  187. This option is only relevant at load-time of Org-mode, and must be set
  188. *before* org.el is loaded. Changing it requires a restart of Emacs to
  189. become effective."
  190. :group 'org-startup
  191. :type 'boolean)
  192. (if (fboundp 'defvaralias)
  193. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  194. (defcustom org-disputed-keys
  195. '(([(shift up)] . [(meta p)])
  196. ([(shift down)] . [(meta n)])
  197. ([(shift left)] . [(meta -)])
  198. ([(shift right)] . [(meta +)])
  199. ([(control shift right)] . [(meta shift +)])
  200. ([(control shift left)] . [(meta shift -)]))
  201. "Keys for which Org-mode and other modes compete.
  202. This is an alist, cars are the default keys, second element specifies
  203. the alternative to use when `org-replace-disputed-keys' is t.
  204. Keys can be specified in any syntax supported by `define-key'.
  205. The value of this option takes effect only at Org-mode's startup,
  206. therefore you'll have to restart Emacs to apply it after changing."
  207. :group 'org-startup
  208. :type 'alist)
  209. (defun org-key (key)
  210. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  211. Or return the original if not disputed."
  212. (if org-replace-disputed-keys
  213. (let* ((nkey (key-description key))
  214. (x (org-find-if (lambda (x)
  215. (equal (key-description (car x)) nkey))
  216. org-disputed-keys)))
  217. (if x (cdr x) key))
  218. key))
  219. (defun org-find-if (predicate seq)
  220. (catch 'exit
  221. (while seq
  222. (if (funcall predicate (car seq))
  223. (throw 'exit (car seq))
  224. (pop seq)))))
  225. (defun org-defkey (keymap key def)
  226. "Define a key, possibly translated, as returned by `org-key'."
  227. (define-key keymap (org-key key) def))
  228. (defcustom org-ellipsis nil
  229. "The ellipsis to use in the Org-mode outline.
  230. When nil, just use the standard three dots. When a string, use that instead,
  231. When a face, use the standart 3 dots, but with the specified face.
  232. The change affects only Org-mode (which will then use its own display table).
  233. Changing this requires executing `M-x org-mode' in a buffer to become
  234. effective."
  235. :group 'org-startup
  236. :type '(choice (const :tag "Default" nil)
  237. (face :tag "Face" :value org-warning)
  238. (string :tag "String" :value "...#")))
  239. (defvar org-display-table nil
  240. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  241. (defgroup org-keywords nil
  242. "Keywords in Org-mode."
  243. :tag "Org Keywords"
  244. :group 'org)
  245. (defcustom org-deadline-string "DEADLINE:"
  246. "String to mark deadline entries.
  247. A deadline is this string, followed by a time stamp. Should be a word,
  248. terminated by a colon. You can insert a schedule keyword and
  249. a timestamp with \\[org-deadline].
  250. Changes become only effective after restarting Emacs."
  251. :group 'org-keywords
  252. :type 'string)
  253. (defcustom org-scheduled-string "SCHEDULED:"
  254. "String to mark scheduled TODO entries.
  255. A schedule is this string, followed by a time stamp. Should be a word,
  256. terminated by a colon. You can insert a schedule keyword and
  257. a timestamp with \\[org-schedule].
  258. Changes become only effective after restarting Emacs."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-closed-string "CLOSED:"
  262. "String used as the prefix for timestamps logging closing a TODO entry."
  263. :group 'org-keywords
  264. :type 'string)
  265. (defcustom org-clock-string "CLOCK:"
  266. "String used as prefix for timestamps clocking work hours on an item."
  267. :group 'org-keywords
  268. :type 'string)
  269. (defcustom org-comment-string "COMMENT"
  270. "Entries starting with this keyword will never be exported.
  271. An entry can be toggled between COMMENT and normal with
  272. \\[org-toggle-comment].
  273. Changes become only effective after restarting Emacs."
  274. :group 'org-keywords
  275. :type 'string)
  276. (defcustom org-quote-string "QUOTE"
  277. "Entries starting with this keyword will be exported in fixed-width font.
  278. Quoting applies only to the text in the entry following the headline, and does
  279. not extend beyond the next headline, even if that is lower level.
  280. An entry can be toggled between QUOTE and normal with
  281. \\[org-toggle-fixed-width-section]."
  282. :group 'org-keywords
  283. :type 'string)
  284. (defconst org-repeat-re
  285. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  286. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  287. "Regular expression for specifying repeated events.
  288. After a match, group 1 contains the repeat expression.")
  289. (defgroup org-structure nil
  290. "Options concerning the general structure of Org-mode files."
  291. :tag "Org Structure"
  292. :group 'org)
  293. (defgroup org-reveal-location nil
  294. "Options about how to make context of a location visible."
  295. :tag "Org Reveal Location"
  296. :group 'org-structure)
  297. (defconst org-context-choice
  298. '(choice
  299. (const :tag "Always" t)
  300. (const :tag "Never" nil)
  301. (repeat :greedy t :tag "Individual contexts"
  302. (cons
  303. (choice :tag "Context"
  304. (const agenda)
  305. (const org-goto)
  306. (const occur-tree)
  307. (const tags-tree)
  308. (const link-search)
  309. (const mark-goto)
  310. (const bookmark-jump)
  311. (const isearch)
  312. (const default))
  313. (boolean))))
  314. "Contexts for the reveal options.")
  315. (defcustom org-show-hierarchy-above '((default . t))
  316. "Non-nil means, show full hierarchy when revealing a location.
  317. Org-mode often shows locations in an org-mode file which might have
  318. been invisible before. When this is set, the hierarchy of headings
  319. above the exposed location is shown.
  320. Turning this off for example for sparse trees makes them very compact.
  321. Instead of t, this can also be an alist specifying this option for different
  322. contexts. Valid contexts are
  323. agenda when exposing an entry from the agenda
  324. org-goto when using the command `org-goto' on key C-c C-j
  325. occur-tree when using the command `org-occur' on key C-c /
  326. tags-tree when constructing a sparse tree based on tags matches
  327. link-search when exposing search matches associated with a link
  328. mark-goto when exposing the jump goal of a mark
  329. bookmark-jump when exposing a bookmark location
  330. isearch when exiting from an incremental search
  331. default default for all contexts not set explicitly"
  332. :group 'org-reveal-location
  333. :type org-context-choice)
  334. (defcustom org-show-following-heading '((default . nil))
  335. "Non-nil means, show following heading when revealing a location.
  336. Org-mode often shows locations in an org-mode file which might have
  337. been invisible before. When this is set, the heading following the
  338. match is shown.
  339. Turning this off for example for sparse trees makes them very compact,
  340. but makes it harder to edit the location of the match. In such a case,
  341. use the command \\[org-reveal] to show more context.
  342. Instead of t, this can also be an alist specifying this option for different
  343. contexts. See `org-show-hierarchy-above' for valid contexts."
  344. :group 'org-reveal-location
  345. :type org-context-choice)
  346. (defcustom org-show-siblings '((default . nil) (isearch t))
  347. "Non-nil means, show all sibling heading when revealing a location.
  348. Org-mode often shows locations in an org-mode file which might have
  349. been invisible before. When this is set, the sibling of the current entry
  350. heading are all made visible. If `org-show-hierarchy-above' is t,
  351. the same happens on each level of the hierarchy above the current entry.
  352. By default this is on for the isearch context, off for all other contexts.
  353. Turning this off for example for sparse trees makes them very compact,
  354. but makes it harder to edit the location of the match. In such a case,
  355. use the command \\[org-reveal] to show more context.
  356. Instead of t, this can also be an alist specifying this option for different
  357. contexts. See `org-show-hierarchy-above' for valid contexts."
  358. :group 'org-reveal-location
  359. :type org-context-choice)
  360. (defcustom org-show-entry-below '((default . nil))
  361. "Non-nil means, show the entry below a headline when revealing a location.
  362. Org-mode often shows locations in an org-mode file which might have
  363. been invisible before. When this is set, the text below the headline that is
  364. exposed is also shown.
  365. By default this is off for all contexts.
  366. Instead of t, this can also be an alist specifying this option for different
  367. contexts. See `org-show-hierarchy-above' for valid contexts."
  368. :group 'org-reveal-location
  369. :type org-context-choice)
  370. (defgroup org-cycle nil
  371. "Options concerning visibility cycling in Org-mode."
  372. :tag "Org Cycle"
  373. :group 'org-structure)
  374. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  375. "Names of drawers. Drawers are not opened by cycling on the headline above.
  376. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  377. this:
  378. :DRAWERNAME:
  379. .....
  380. :END:
  381. The drawer \"PROPERTIES\" is special for capturing properties through
  382. the property API.
  383. Drawers can be defined on the per-file basis with a line like:
  384. #+DRAWERS: HIDDEN STATE PROPERTIES"
  385. :group 'org-structure
  386. :type '(repeat (string :tag "Drawer Name")))
  387. (defcustom org-cycle-global-at-bob nil
  388. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  389. This makes it possible to do global cycling without having to use S-TAB or
  390. C-u TAB. For this special case to work, the first line of the buffer
  391. must not be a headline - it may be empty ot some other text. When used in
  392. this way, `org-cycle-hook' is disables temporarily, to make sure the
  393. cursor stays at the beginning of the buffer.
  394. When this option is nil, don't do anything special at the beginning
  395. of the buffer."
  396. :group 'org-cycle
  397. :type 'boolean)
  398. (defcustom org-cycle-emulate-tab t
  399. "Where should `org-cycle' emulate TAB.
  400. nil Never
  401. white Only in completely white lines
  402. whitestart Only at the beginning of lines, before the first non-white char.
  403. t Everywhere except in headlines
  404. exc-hl-bol Everywhere except at the start of a headline
  405. If TAB is used in a place where it does not emulate TAB, the current subtree
  406. visibility is cycled."
  407. :group 'org-cycle
  408. :type '(choice (const :tag "Never" nil)
  409. (const :tag "Only in completely white lines" white)
  410. (const :tag "Before first char in a line" whitestart)
  411. (const :tag "Everywhere except in headlines" t)
  412. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  413. ))
  414. (defcustom org-cycle-separator-lines 2
  415. "Number of empty lines needed to keep an empty line between collapsed trees.
  416. If you leave an empty line between the end of a subtree and the following
  417. headline, this empty line is hidden when the subtree is folded.
  418. Org-mode will leave (exactly) one empty line visible if the number of
  419. empty lines is equal or larger to the number given in this variable.
  420. So the default 2 means, at least 2 empty lines after the end of a subtree
  421. are needed to produce free space between a collapsed subtree and the
  422. following headline.
  423. Special case: when 0, never leave empty lines in collapsed view."
  424. :group 'org-cycle
  425. :type 'integer)
  426. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  427. org-cycle-hide-drawers
  428. org-cycle-show-empty-lines
  429. org-optimize-window-after-visibility-change)
  430. "Hook that is run after `org-cycle' has changed the buffer visibility.
  431. The function(s) in this hook must accept a single argument which indicates
  432. the new state that was set by the most recent `org-cycle' command. The
  433. argument is a symbol. After a global state change, it can have the values
  434. `overview', `content', or `all'. After a local state change, it can have
  435. the values `folded', `children', or `subtree'."
  436. :group 'org-cycle
  437. :type 'hook)
  438. (defgroup org-edit-structure nil
  439. "Options concerning structure editing in Org-mode."
  440. :tag "Org Edit Structure"
  441. :group 'org-structure)
  442. (defcustom org-special-ctrl-a/e nil
  443. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  444. When t, `C-a' will bring back the cursor to the beginning of the
  445. headline text, i.e. after the stars and after a possible TODO keyword.
  446. In an item, this will be the position after the bullet.
  447. When the cursor is already at that position, another `C-a' will bring
  448. it to the beginning of the line.
  449. `C-e' will jump to the end of the headline, ignoring the presence of tags
  450. in the headline. A second `C-e' will then jump to the true end of the
  451. line, after any tags.
  452. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  453. and only a directly following, identical keypress will bring the cursor
  454. to the special positions."
  455. :group 'org-edit-structure
  456. :type '(choice
  457. (const :tag "off" nil)
  458. (const :tag "after bullet first" t)
  459. (const :tag "border first" reversed)))
  460. (if (fboundp 'defvaralias)
  461. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  462. (defcustom org-odd-levels-only nil
  463. "Non-nil means, skip even levels and only use odd levels for the outline.
  464. This has the effect that two stars are being added/taken away in
  465. promotion/demotion commands. It also influences how levels are
  466. handled by the exporters.
  467. Changing it requires restart of `font-lock-mode' to become effective
  468. for fontification also in regions already fontified.
  469. You may also set this on a per-file basis by adding one of the following
  470. lines to the buffer:
  471. #+STARTUP: odd
  472. #+STARTUP: oddeven"
  473. :group 'org-edit-structure
  474. :group 'org-font-lock
  475. :type 'boolean)
  476. (defcustom org-adapt-indentation t
  477. "Non-nil means, adapt indentation when promoting and demoting.
  478. When this is set and the *entire* text in an entry is indented, the
  479. indentation is increased by one space in a demotion command, and
  480. decreased by one in a promotion command. If any line in the entry
  481. body starts at column 0, indentation is not changed at all."
  482. :group 'org-edit-structure
  483. :type 'boolean)
  484. (defcustom org-blank-before-new-entry '((heading . nil)
  485. (plain-list-item . nil))
  486. "Should `org-insert-heading' leave a blank line before new heading/item?
  487. The value is an alist, with `heading' and `plain-list-item' as car,
  488. and a boolean flag as cdr."
  489. :group 'org-edit-structure
  490. :type '(list
  491. (cons (const heading) (boolean))
  492. (cons (const plain-list-item) (boolean))))
  493. (defcustom org-insert-heading-hook nil
  494. "Hook being run after inserting a new heading."
  495. :group 'org-edit-structure
  496. :type 'hook)
  497. (defcustom org-enable-fixed-width-editor t
  498. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  499. This currently only means, they are never auto-wrapped.
  500. When nil, such lines will be treated like ordinary lines.
  501. See also the QUOTE keyword."
  502. :group 'org-edit-structure
  503. :type 'boolean)
  504. (defgroup org-sparse-trees nil
  505. "Options concerning sparse trees in Org-mode."
  506. :tag "Org Sparse Trees"
  507. :group 'org-structure)
  508. (defcustom org-highlight-sparse-tree-matches t
  509. "Non-nil means, highlight all matches that define a sparse tree.
  510. The highlights will automatically disappear the next time the buffer is
  511. changed by an edit command."
  512. :group 'org-sparse-trees
  513. :type 'boolean)
  514. (defcustom org-remove-highlights-with-change t
  515. "Non-nil means, any change to the buffer will remove temporary highlights.
  516. Such highlights are created by `org-occur' and `org-clock-display'.
  517. When nil, `C-c C-c needs to be used to get rid of the highlights.
  518. The highlights created by `org-preview-latex-fragment' always need
  519. `C-c C-c' to be removed."
  520. :group 'org-sparse-trees
  521. :group 'org-time
  522. :type 'boolean)
  523. (defcustom org-occur-hook '(org-first-headline-recenter)
  524. "Hook that is run after `org-occur' has constructed a sparse tree.
  525. This can be used to recenter the window to show as much of the structure
  526. as possible."
  527. :group 'org-sparse-trees
  528. :type 'hook)
  529. (defgroup org-plain-lists nil
  530. "Options concerning plain lists in Org-mode."
  531. :tag "Org Plain lists"
  532. :group 'org-structure)
  533. (defcustom org-cycle-include-plain-lists nil
  534. "Non-nil means, include plain lists into visibility cycling.
  535. This means that during cycling, plain list items will *temporarily* be
  536. interpreted as outline headlines with a level given by 1000+i where i is the
  537. indentation of the bullet. In all other operations, plain list items are
  538. not seen as headlines. For example, you cannot assign a TODO keyword to
  539. such an item."
  540. :group 'org-plain-lists
  541. :type 'boolean)
  542. (defcustom org-plain-list-ordered-item-terminator t
  543. "The character that makes a line with leading number an ordered list item.
  544. Valid values are ?. and ?\). To get both terminators, use t. While
  545. ?. may look nicer, it creates the danger that a line with leading
  546. number may be incorrectly interpreted as an item. ?\) therefore is
  547. the safe choice."
  548. :group 'org-plain-lists
  549. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  550. (const :tag "paren like in \"2)\"" ?\))
  551. (const :tab "both" t)))
  552. (defcustom org-auto-renumber-ordered-lists t
  553. "Non-nil means, automatically renumber ordered plain lists.
  554. Renumbering happens when the sequence have been changed with
  555. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  556. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  557. :group 'org-plain-lists
  558. :type 'boolean)
  559. (defcustom org-provide-checkbox-statistics t
  560. "Non-nil means, update checkbox statistics after insert and toggle.
  561. When this is set, checkbox statistics is updated each time you either insert
  562. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  563. with \\[org-ctrl-c-ctrl-c\\]."
  564. :group 'org-plain-lists
  565. :type 'boolean)
  566. (defgroup org-archive nil
  567. "Options concerning archiving in Org-mode."
  568. :tag "Org Archive"
  569. :group 'org-structure)
  570. (defcustom org-archive-tag "ARCHIVE"
  571. "The tag that marks a subtree as archived.
  572. An archived subtree does not open during visibility cycling, and does
  573. not contribute to the agenda listings.
  574. After changing this, font-lock must be restarted in the relevant buffers to
  575. get the proper fontification."
  576. :group 'org-archive
  577. :group 'org-keywords
  578. :type 'string)
  579. (defcustom org-agenda-skip-archived-trees t
  580. "Non-nil means, the agenda will skip any items located in archived trees.
  581. An archived tree is a tree marked with the tag ARCHIVE."
  582. :group 'org-archive
  583. :group 'org-agenda-skip
  584. :type 'boolean)
  585. (defcustom org-cycle-open-archived-trees nil
  586. "Non-nil means, `org-cycle' will open archived trees.
  587. An archived tree is a tree marked with the tag ARCHIVE.
  588. When nil, archived trees will stay folded. You can still open them with
  589. normal outline commands like `show-all', but not with the cycling commands."
  590. :group 'org-archive
  591. :group 'org-cycle
  592. :type 'boolean)
  593. (defcustom org-sparse-tree-open-archived-trees nil
  594. "Non-nil means sparse tree construction shows matches in archived trees.
  595. When nil, matches in these trees are highlighted, but the trees are kept in
  596. collapsed state."
  597. :group 'org-archive
  598. :group 'org-sparse-trees
  599. :type 'boolean)
  600. (defcustom org-archive-location "%s_archive::"
  601. "The location where subtrees should be archived.
  602. This string consists of two parts, separated by a double-colon.
  603. The first part is a file name - when omitted, archiving happens in the same
  604. file. %s will be replaced by the current file name (without directory part).
  605. Archiving to a different file is useful to keep archived entries from
  606. contributing to the Org-mode Agenda.
  607. The part after the double colon is a headline. The archived entries will be
  608. filed under that headline. When omitted, the subtrees are simply filed away
  609. at the end of the file, as top-level entries.
  610. Here are a few examples:
  611. \"%s_archive::\"
  612. If the current file is Projects.org, archive in file
  613. Projects.org_archive, as top-level trees. This is the default.
  614. \"::* Archived Tasks\"
  615. Archive in the current file, under the top-level headline
  616. \"* Archived Tasks\".
  617. \"~/org/archive.org::\"
  618. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  619. \"basement::** Finished Tasks\"
  620. Archive in file ./basement (relative path), as level 3 trees
  621. below the level 2 heading \"** Finished Tasks\".
  622. You may set this option on a per-file basis by adding to the buffer a
  623. line like
  624. #+ARCHIVE: basement::** Finished Tasks"
  625. :group 'org-archive
  626. :type 'string)
  627. (defcustom org-archive-mark-done t
  628. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  629. This can be a string to set the keyword to use. When t, Org-mode will
  630. use the first keyword in its list that means done."
  631. :group 'org-archive
  632. :type '(choice
  633. (const :tag "No" nil)
  634. (const :tag "Yes" t)
  635. (string :tag "Use this keyword")))
  636. (defcustom org-archive-stamp-time t
  637. "Non-nil means, add a time stamp to entries moved to an archive file.
  638. This variable is obsolete and has no effect anymore, instead add ot remove
  639. `time' from the variablle `org-archive-save-context-info'."
  640. :group 'org-archive
  641. :type 'boolean)
  642. (defcustom org-archive-save-context-info '(time file category todo itags)
  643. "Parts of context info that should be stored as properties when archiving.
  644. When a subtree is moved to an archive file, it looses information given by
  645. context, like inherited tags, the category, and possibly also the TODO
  646. state (depending on the variable `org-archive-mark-done').
  647. This variable can be a list of any of the following symbols:
  648. time The time of archiving.
  649. file The file where the entry originates.
  650. itags The local tags, in the headline of the subtree.
  651. ltags The tags the subtree inherits from further up the hierarchy.
  652. todo The pre-archive TODO state.
  653. category The category, taken from file name or #+CATEGORY lines.
  654. For each symbol present in the list, a property will be created in
  655. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  656. information."
  657. :group 'org-archive
  658. :type '(set :greedy t
  659. (const :tag "Time" time)
  660. (const :tag "File" file)
  661. (const :tag "Category" category)
  662. (const :tag "TODO state" todo)
  663. (const :tag "TODO state" priority)
  664. (const :tag "Inherited tags" itags)
  665. (const :tag "Local tags" ltags)))
  666. (defgroup org-imenu-and-speedbar nil
  667. "Options concerning imenu and speedbar in Org-mode."
  668. :tag "Org Imenu and Speedbar"
  669. :group 'org-structure)
  670. (defcustom org-imenu-depth 2
  671. "The maximum level for Imenu access to Org-mode headlines.
  672. This also applied for speedbar access."
  673. :group 'org-imenu-and-speedbar
  674. :type 'number)
  675. (defgroup org-table nil
  676. "Options concerning tables in Org-mode."
  677. :tag "Org Table"
  678. :group 'org)
  679. (defcustom org-enable-table-editor 'optimized
  680. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  681. When nil, such lines will be treated like ordinary lines.
  682. When equal to the symbol `optimized', the table editor will be optimized to
  683. do the following:
  684. - Automatic overwrite mode in front of whitespace in table fields.
  685. This makes the structure of the table stay in tact as long as the edited
  686. field does not exceed the column width.
  687. - Minimize the number of realigns. Normally, the table is aligned each time
  688. TAB or RET are pressed to move to another field. With optimization this
  689. happens only if changes to a field might have changed the column width.
  690. Optimization requires replacing the functions `self-insert-command',
  691. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  692. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  693. very good at guessing when a re-align will be necessary, but you can always
  694. force one with \\[org-ctrl-c-ctrl-c].
  695. If you would like to use the optimized version in Org-mode, but the
  696. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  697. This variable can be used to turn on and off the table editor during a session,
  698. but in order to toggle optimization, a restart is required.
  699. See also the variable `org-table-auto-blank-field'."
  700. :group 'org-table
  701. :type '(choice
  702. (const :tag "off" nil)
  703. (const :tag "on" t)
  704. (const :tag "on, optimized" optimized)))
  705. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  706. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  707. In the optimized version, the table editor takes over all simple keys that
  708. normally just insert a character. In tables, the characters are inserted
  709. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  710. for empty fields). Outside tables, the correct binding of the keys is
  711. restored.
  712. The default for this option is t if the optimized version is also used in
  713. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  714. this variable requires a restart of Emacs to become effective."
  715. :group 'org-table
  716. :type 'boolean)
  717. (defcustom orgtbl-radio-table-templates
  718. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  719. % END RECEIVE ORGTBL %n
  720. \\begin{comment}
  721. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  722. | | |
  723. \\end{comment}\n")
  724. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  725. @c END RECEIVE ORGTBL %n
  726. @ignore
  727. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  728. | | |
  729. @end ignore\n")
  730. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  731. <!-- END RECEIVE ORGTBL %n -->
  732. <!--
  733. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  734. | | |
  735. -->\n"))
  736. "Templates for radio tables in different major modes.
  737. All occurrences of %n in a template will be replaced with the name of the
  738. table, obtained by prompting the user."
  739. :group 'org-table
  740. :type '(repeat
  741. (list (symbol :tag "Major mode")
  742. (string :tag "Format"))))
  743. (defgroup org-table-settings nil
  744. "Settings for tables in Org-mode."
  745. :tag "Org Table Settings"
  746. :group 'org-table)
  747. (defcustom org-table-default-size "5x2"
  748. "The default size for newly created tables, Columns x Rows."
  749. :group 'org-table-settings
  750. :type 'string)
  751. (defcustom org-table-number-regexp
  752. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  753. "Regular expression for recognizing numbers in table columns.
  754. If a table column contains mostly numbers, it will be aligned to the
  755. right. If not, it will be aligned to the left.
  756. The default value of this option is a regular expression which allows
  757. anything which looks remotely like a number as used in scientific
  758. context. For example, all of the following will be considered a
  759. number:
  760. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  761. Other options offered by the customize interface are more restrictive."
  762. :group 'org-table-settings
  763. :type '(choice
  764. (const :tag "Positive Integers"
  765. "^[0-9]+$")
  766. (const :tag "Integers"
  767. "^[-+]?[0-9]+$")
  768. (const :tag "Floating Point Numbers"
  769. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  770. (const :tag "Floating Point Number or Integer"
  771. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  772. (const :tag "Exponential, Floating point, Integer"
  773. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  774. (const :tag "Very General Number-Like, including hex"
  775. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  776. (string :tag "Regexp:")))
  777. (defcustom org-table-number-fraction 0.5
  778. "Fraction of numbers in a column required to make the column align right.
  779. In a column all non-white fields are considered. If at least this
  780. fraction of fields is matched by `org-table-number-fraction',
  781. alignment to the right border applies."
  782. :group 'org-table-settings
  783. :type 'number)
  784. (defgroup org-table-editing nil
  785. "Behavior of tables during editing in Org-mode."
  786. :tag "Org Table Editing"
  787. :group 'org-table)
  788. (defcustom org-table-automatic-realign t
  789. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  790. When nil, aligning is only done with \\[org-table-align], or after column
  791. removal/insertion."
  792. :group 'org-table-editing
  793. :type 'boolean)
  794. (defcustom org-table-auto-blank-field t
  795. "Non-nil means, automatically blank table field when starting to type into it.
  796. This only happens when typing immediately after a field motion
  797. command (TAB, S-TAB or RET).
  798. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  799. :group 'org-table-editing
  800. :type 'boolean)
  801. (defcustom org-table-tab-jumps-over-hlines t
  802. "Non-nil means, tab in the last column of a table with jump over a hline.
  803. If a horizontal separator line is following the current line,
  804. `org-table-next-field' can either create a new row before that line, or jump
  805. over the line. When this option is nil, a new line will be created before
  806. this line."
  807. :group 'org-table-editing
  808. :type 'boolean)
  809. (defcustom org-table-tab-recognizes-table.el t
  810. "Non-nil means, TAB will automatically notice a table.el table.
  811. When it sees such a table, it moves point into it and - if necessary -
  812. calls `table-recognize-table'."
  813. :group 'org-table-editing
  814. :type 'boolean)
  815. (defgroup org-table-calculation nil
  816. "Options concerning tables in Org-mode."
  817. :tag "Org Table Calculation"
  818. :group 'org-table)
  819. (defcustom org-table-use-standard-references t
  820. "Should org-mode work with table refrences like B3 instead of @3$2?
  821. Possible values are:
  822. nil never use them
  823. from accept as input, do not present for editing
  824. t: accept as input and present for editing"
  825. :group 'org-table-calculation
  826. :type '(choice
  827. (const :tag "Never, don't even check unser input for them" nil)
  828. (const :tag "Always, both as user input, and when editing" t)
  829. (const :tag "Convert user input, don't offer during editing" 'from)))
  830. (defcustom org-table-copy-increment t
  831. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  832. :group 'org-table-calculation
  833. :type 'boolean)
  834. (defcustom org-calc-default-modes
  835. '(calc-internal-prec 12
  836. calc-float-format (float 5)
  837. calc-angle-mode deg
  838. calc-prefer-frac nil
  839. calc-symbolic-mode nil
  840. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  841. calc-display-working-message t
  842. )
  843. "List with Calc mode settings for use in calc-eval for table formulas.
  844. The list must contain alternating symbols (Calc modes variables and values).
  845. Don't remove any of the default settings, just change the values. Org-mode
  846. relies on the variables to be present in the list."
  847. :group 'org-table-calculation
  848. :type 'plist)
  849. (defcustom org-table-formula-evaluate-inline t
  850. "Non-nil means, TAB and RET evaluate a formula in current table field.
  851. If the current field starts with an equal sign, it is assumed to be a formula
  852. which should be evaluated as described in the manual and in the documentation
  853. string of the command `org-table-eval-formula'. This feature requires the
  854. Emacs calc package.
  855. When this variable is nil, formula calculation is only available through
  856. the command \\[org-table-eval-formula]."
  857. :group 'org-table-calculation
  858. :type 'boolean)
  859. (defcustom org-table-formula-use-constants t
  860. "Non-nil means, interpret constants in formulas in tables.
  861. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  862. by the value given in `org-table-formula-constants', or by a value obtained
  863. from the `constants.el' package."
  864. :group 'org-table-calculation
  865. :type 'boolean)
  866. (defcustom org-table-formula-constants nil
  867. "Alist with constant names and values, for use in table formulas.
  868. The car of each element is a name of a constant, without the `$' before it.
  869. The cdr is the value as a string. For example, if you'd like to use the
  870. speed of light in a formula, you would configure
  871. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  872. and then use it in an equation like `$1*$c'.
  873. Constants can also be defined on a per-file basis using a line like
  874. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  875. :group 'org-table-calculation
  876. :type '(repeat
  877. (cons (string :tag "name")
  878. (string :tag "value"))))
  879. (defvar org-table-formula-constants-local nil
  880. "Local version of `org-table-formula-constants'.")
  881. (make-variable-buffer-local 'org-table-formula-constants-local)
  882. (defcustom org-table-allow-automatic-line-recalculation t
  883. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  884. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  885. :group 'org-table-calculation
  886. :type 'boolean)
  887. (defgroup org-link nil
  888. "Options concerning links in Org-mode."
  889. :tag "Org Link"
  890. :group 'org)
  891. (defvar org-link-abbrev-alist-local nil
  892. "Buffer-local version of `org-link-abbrev-alist', which see.
  893. The value of this is taken from the #+LINK lines.")
  894. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  895. (defcustom org-link-abbrev-alist nil
  896. "Alist of link abbreviations.
  897. The car of each element is a string, to be replaced at the start of a link.
  898. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  899. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  900. [[linkkey:tag][description]]
  901. If REPLACE is a string, the tag will simply be appended to create the link.
  902. If the string contains \"%s\", the tag will be inserted there.
  903. REPLACE may also be a function that will be called with the tag as the
  904. only argument to create the link, which should be returned as a string.
  905. See the manual for examples."
  906. :group 'org-link
  907. :type 'alist)
  908. (defcustom org-descriptive-links t
  909. "Non-nil means, hide link part and only show description of bracket links.
  910. Bracket links are like [[link][descritpion]]. This variable sets the initial
  911. state in new org-mode buffers. The setting can then be toggled on a
  912. per-buffer basis from the Org->Hyperlinks menu."
  913. :group 'org-link
  914. :type 'boolean)
  915. (defcustom org-link-file-path-type 'adaptive
  916. "How the path name in file links should be stored.
  917. Valid values are:
  918. relative relative to the current directory, i.e. the directory of the file
  919. into which the link is being inserted.
  920. absolute absolute path, if possible with ~ for home directory.
  921. noabbrev absolute path, no abbreviation of home directory.
  922. adaptive Use relative path for files in the current directory and sub-
  923. directories of it. For other files, use an absolute path."
  924. :group 'org-link
  925. :type '(choice
  926. (const relative)
  927. (const absolute)
  928. (const noabbrev)
  929. (const adaptive)))
  930. (defcustom org-activate-links '(bracket angle plain radio tag date)
  931. "Types of links that should be activated in Org-mode files.
  932. This is a list of symbols, each leading to the activation of a certain link
  933. type. In principle, it does not hurt to turn on most link types - there may
  934. be a small gain when turning off unused link types. The types are:
  935. bracket The recommended [[link][description]] or [[link]] links with hiding.
  936. angular Links in angular brackes that may contain whitespace like
  937. <bbdb:Carsten Dominik>.
  938. plain Plain links in normal text, no whitespace, like http://google.com.
  939. radio Text that is matched by a radio target, see manual for details.
  940. tag Tag settings in a headline (link to tag search).
  941. date Time stamps (link to calendar).
  942. Changing this variable requires a restart of Emacs to become effective."
  943. :group 'org-link
  944. :type '(set (const :tag "Double bracket links (new style)" bracket)
  945. (const :tag "Angular bracket links (old style)" angular)
  946. (const :tag "plain text links" plain)
  947. (const :tag "Radio target matches" radio)
  948. (const :tag "Tags" tag)
  949. (const :tag "Tags" target)
  950. (const :tag "Timestamps" date)))
  951. (defgroup org-link-store nil
  952. "Options concerning storing links in Org-mode"
  953. :tag "Org Store Link"
  954. :group 'org-link)
  955. (defcustom org-email-link-description-format "Email %c: %.30s"
  956. "Format of the description part of a link to an email or usenet message.
  957. The following %-excapes will be replaced by corresponding information:
  958. %F full \"From\" field
  959. %f name, taken from \"From\" field, address if no name
  960. %T full \"To\" field
  961. %t first name in \"To\" field, address if no name
  962. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  963. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  964. %s subject
  965. %m message-id.
  966. You may use normal field width specification between the % and the letter.
  967. This is for example useful to limit the length of the subject.
  968. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  969. :group 'org-link-store
  970. :type 'string)
  971. (defcustom org-from-is-user-regexp
  972. (let (r1 r2)
  973. (when (and user-mail-address (not (string= user-mail-address "")))
  974. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  975. (when (and user-full-name (not (string= user-full-name "")))
  976. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  977. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  978. "Regexp mached against the \"From:\" header of an email or usenet message.
  979. It should match if the message is from the user him/herself."
  980. :group 'org-link-store
  981. :type 'regexp)
  982. (defcustom org-context-in-file-links t
  983. "Non-nil means, file links from `org-store-link' contain context.
  984. A search string will be added to the file name with :: as separator and
  985. used to find the context when the link is activated by the command
  986. `org-open-at-point'.
  987. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  988. negates this setting for the duration of the command."
  989. :group 'org-link-store
  990. :type 'boolean)
  991. (defcustom org-keep-stored-link-after-insertion nil
  992. "Non-nil means, keep link in list for entire session.
  993. The command `org-store-link' adds a link pointing to the current
  994. location to an internal list. These links accumulate during a session.
  995. The command `org-insert-link' can be used to insert links into any
  996. Org-mode file (offering completion for all stored links). When this
  997. option is nil, every link which has been inserted once using \\[org-insert-link]
  998. will be removed from the list, to make completing the unused links
  999. more efficient."
  1000. :group 'org-link-store
  1001. :type 'boolean)
  1002. (defcustom org-usenet-links-prefer-google nil
  1003. "Non-nil means, `org-store-link' will create web links to Google groups.
  1004. When nil, Gnus will be used for such links.
  1005. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1006. negates this setting for the duration of the command."
  1007. :group 'org-link-store
  1008. :type 'boolean)
  1009. (defgroup org-link-follow nil
  1010. "Options concerning following links in Org-mode"
  1011. :tag "Org Follow Link"
  1012. :group 'org-link)
  1013. (defcustom org-tab-follows-link nil
  1014. "Non-nil means, on links TAB will follow the link.
  1015. Needs to be set before org.el is loaded."
  1016. :group 'org-link-follow
  1017. :type 'boolean)
  1018. (defcustom org-return-follows-link nil
  1019. "Non-nil means, on links RET will follow the link.
  1020. Needs to be set before org.el is loaded."
  1021. :group 'org-link-follow
  1022. :type 'boolean)
  1023. (defcustom org-mouse-1-follows-link t
  1024. "Non-nil means, mouse-1 on a link will follow the link.
  1025. A longer mouse click will still set point. Does not wortk on XEmacs.
  1026. Needs to be set before org.el is loaded."
  1027. :group 'org-link-follow
  1028. :type 'boolean)
  1029. (defcustom org-mark-ring-length 4
  1030. "Number of different positions to be recorded in the ring
  1031. Changing this requires a restart of Emacs to work correctly."
  1032. :group 'org-link-follow
  1033. :type 'interger)
  1034. (defcustom org-link-frame-setup
  1035. '((vm . vm-visit-folder-other-frame)
  1036. (gnus . gnus-other-frame)
  1037. (file . find-file-other-window))
  1038. "Setup the frame configuration for following links.
  1039. When following a link with Emacs, it may often be useful to display
  1040. this link in another window or frame. This variable can be used to
  1041. set this up for the different types of links.
  1042. For VM, use any of
  1043. `vm-visit-folder'
  1044. `vm-visit-folder-other-frame'
  1045. For Gnus, use any of
  1046. `gnus'
  1047. `gnus-other-frame'
  1048. For FILE, use any of
  1049. `find-file'
  1050. `find-file-other-window'
  1051. `find-file-other-frame'
  1052. For the calendar, use the variable `calendar-setup'.
  1053. For BBDB, it is currently only possible to display the matches in
  1054. another window."
  1055. :group 'org-link-follow
  1056. :type '(list
  1057. (cons (const vm)
  1058. (choice
  1059. (const vm-visit-folder)
  1060. (const vm-visit-folder-other-window)
  1061. (const vm-visit-folder-other-frame)))
  1062. (cons (const gnus)
  1063. (choice
  1064. (const gnus)
  1065. (const gnus-other-frame)))
  1066. (cons (const file)
  1067. (choice
  1068. (const find-file)
  1069. (const find-file-other-window)
  1070. (const find-file-other-frame)))))
  1071. (defcustom org-display-internal-link-with-indirect-buffer nil
  1072. "Non-nil means, use indirect buffer to display infile links.
  1073. Activating internal links (from one location in a file to another location
  1074. in the same file) normally just jumps to the location. When the link is
  1075. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1076. another window. When this option is set, the other window actually displays
  1077. an indirect buffer clone of the current buffer, to avoid any visibility
  1078. changes to the current buffer."
  1079. :group 'org-link-follow
  1080. :type 'boolean)
  1081. (defcustom org-open-non-existing-files nil
  1082. "Non-nil means, `org-open-file' will open non-existing files.
  1083. When nil, an error will be generated."
  1084. :group 'org-link-follow
  1085. :type 'boolean)
  1086. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1087. "Function and arguments to call for following mailto links.
  1088. This is a list with the first element being a lisp function, and the
  1089. remaining elements being arguments to the function. In string arguments,
  1090. %a will be replaced by the address, and %s will be replaced by the subject
  1091. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1092. :group 'org-link-follow
  1093. :type '(choice
  1094. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1095. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1096. (const :tag "message-mail" (message-mail "%a" "%s"))
  1097. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1098. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1099. "Non-nil means, ask for confirmation before executing shell links.
  1100. Shell links can be dangerous: just think about a link
  1101. [[shell:rm -rf ~/*][Google Search]]
  1102. This link would show up in your Org-mode document as \"Google Search\",
  1103. but really it would remove your entire home directory.
  1104. Therefore we advise against setting this variable to nil.
  1105. Just change it to `y-or-n-p' of you want to confirm with a
  1106. single keystroke rather than having to type \"yes\"."
  1107. :group 'org-link-follow
  1108. :type '(choice
  1109. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1110. (const :tag "with y-or-n (faster)" y-or-n-p)
  1111. (const :tag "no confirmation (dangerous)" nil)))
  1112. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1113. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1114. Elisp links can be dangerous: just think about a link
  1115. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1116. This link would show up in your Org-mode document as \"Google Search\",
  1117. but really it would remove your entire home directory.
  1118. Therefore we advise against setting this variable to nil.
  1119. Just change it to `y-or-n-p' of you want to confirm with a
  1120. single keystroke rather than having to type \"yes\"."
  1121. :group 'org-link-follow
  1122. :type '(choice
  1123. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1124. (const :tag "with y-or-n (faster)" y-or-n-p)
  1125. (const :tag "no confirmation (dangerous)" nil)))
  1126. (defconst org-file-apps-defaults-gnu
  1127. '((remote . emacs)
  1128. (t . mailcap))
  1129. "Default file applications on a UNIX or GNU/Linux system.
  1130. See `org-file-apps'.")
  1131. (defconst org-file-apps-defaults-macosx
  1132. '((remote . emacs)
  1133. (t . "open %s")
  1134. ("ps" . "gv %s")
  1135. ("ps.gz" . "gv %s")
  1136. ("eps" . "gv %s")
  1137. ("eps.gz" . "gv %s")
  1138. ("dvi" . "xdvi %s")
  1139. ("fig" . "xfig %s"))
  1140. "Default file applications on a MacOS X system.
  1141. The system \"open\" is known as a default, but we use X11 applications
  1142. for some files for which the OS does not have a good default.
  1143. See `org-file-apps'.")
  1144. (defconst org-file-apps-defaults-windowsnt
  1145. (list
  1146. '(remote . emacs)
  1147. (cons t
  1148. (list (if (featurep 'xemacs)
  1149. 'mswindows-shell-execute
  1150. 'w32-shell-execute)
  1151. "open" 'file)))
  1152. "Default file applications on a Windows NT system.
  1153. The system \"open\" is used for most files.
  1154. See `org-file-apps'.")
  1155. (defcustom org-file-apps
  1156. '(
  1157. ("txt" . emacs)
  1158. ("tex" . emacs)
  1159. ("ltx" . emacs)
  1160. ("org" . emacs)
  1161. ("el" . emacs)
  1162. ("bib" . emacs)
  1163. )
  1164. "External applications for opening `file:path' items in a document.
  1165. Org-mode uses system defaults for different file types, but
  1166. you can use this variable to set the application for a given file
  1167. extension. The entries in this list are cons cells where the car identifies
  1168. files and the cdr the corresponding command. Possible values for the
  1169. file identifier are
  1170. \"ext\" A string identifying an extension
  1171. `directory' Matches a directory
  1172. `remote' Matches a remote file, accessible through tramp or efs.
  1173. Remote files most likely should be visited through Emacs
  1174. because external applications cannot handle such paths.
  1175. t Default for all remaining files
  1176. Possible values for the command are:
  1177. `emacs' The file will be visited by the current Emacs process.
  1178. `default' Use the default application for this file type.
  1179. string A command to be executed by a shell; %s will be replaced
  1180. by the path to the file.
  1181. sexp A Lisp form which will be evaluated. The file path will
  1182. be available in the Lisp variable `file'.
  1183. For more examples, see the system specific constants
  1184. `org-file-apps-defaults-macosx'
  1185. `org-file-apps-defaults-windowsnt'
  1186. `org-file-apps-defaults-gnu'."
  1187. :group 'org-link-follow
  1188. :type '(repeat
  1189. (cons (choice :value ""
  1190. (string :tag "Extension")
  1191. (const :tag "Default for unrecognized files" t)
  1192. (const :tag "Remote file" remote)
  1193. (const :tag "Links to a directory" directory))
  1194. (choice :value ""
  1195. (const :tag "Visit with Emacs" emacs)
  1196. (const :tag "Use system default" default)
  1197. (string :tag "Command")
  1198. (sexp :tag "Lisp form")))))
  1199. (defcustom org-mhe-search-all-folders nil
  1200. "Non-nil means, that the search for the mh-message will be extended to
  1201. all folders if the message cannot be found in the folder given in the link.
  1202. Searching all folders is very efficient with one of the search engines
  1203. supported by MH-E, but will be slow with pick."
  1204. :group 'org-link-follow
  1205. :type 'boolean)
  1206. (defgroup org-remember nil
  1207. "Options concerning interaction with remember.el."
  1208. :tag "Org Remember"
  1209. :group 'org)
  1210. (defcustom org-directory "~/org"
  1211. "Directory with org files.
  1212. This directory will be used as default to prompt for org files.
  1213. Used by the hooks for remember.el."
  1214. :group 'org-remember
  1215. :type 'directory)
  1216. (defcustom org-default-notes-file "~/.notes"
  1217. "Default target for storing notes.
  1218. Used by the hooks for remember.el. This can be a string, or nil to mean
  1219. the value of `remember-data-file'.
  1220. You can set this on a per-template basis with the variable
  1221. `org-remember-templates'."
  1222. :group 'org-remember
  1223. :type '(choice
  1224. (const :tag "Default from remember-data-file" nil)
  1225. file))
  1226. (defcustom org-remember-store-without-prompt t
  1227. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1228. In this case, you need `C-u C-c C-c' to get the prompts for
  1229. note file and headline.
  1230. When this variable is nil, `C-c C-c' give you the prompts, and
  1231. `C-u C-c C-c' trigger the fasttrack."
  1232. :group 'org-remember
  1233. :type 'boolean)
  1234. (defcustom org-remember-default-headline ""
  1235. "The headline that should be the default location in the notes file.
  1236. When filing remember notes, the cursor will start at that position.
  1237. You can set this on a per-template basis with the variable
  1238. `org-remember-templates'."
  1239. :group 'org-remember
  1240. :type 'string)
  1241. (defcustom org-remember-templates nil
  1242. "Templates for the creation of remember buffers.
  1243. When nil, just let remember make the buffer.
  1244. When not nil, this is a list of 5-element lists. In each entry, the first
  1245. element is a the name of the template, It should be a single short word.
  1246. The second element is a character, a unique key to select this template.
  1247. The third element is the template. The forth element is optional and can
  1248. specify a destination file for remember items created with this template.
  1249. The default file is given by `org-default-notes-file'. An optional fifth
  1250. element can specify the headline in that file that should be offered
  1251. first when the user is asked to file the entry. The default headline is
  1252. given in the variable `org-remember-default-headline'.
  1253. The template specifies the structure of the remember buffer. It should have
  1254. a first line starting with a star, to act as the org-mode headline.
  1255. Furthermore, the following %-escapes will be replaced with content:
  1256. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1257. A default value and a completion table ca be specified like this:
  1258. %^{prompt|default|completion2|completion3|...}
  1259. %t time stamp, date only
  1260. %T time stamp with date and time
  1261. %u, %U like the above, but inactive time stamps
  1262. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1263. You may define a prompt like %^{Please specify birthday}t
  1264. %n user name (taken from `user-full-name')
  1265. %a annotation, normally the link created with org-store-link
  1266. %i initial content, the region when remember is called with C-u.
  1267. If %i is indented, the entire inserted text will be indented
  1268. as well.
  1269. %c content of the clipboard, or current kill ring head
  1270. %^g prompt for tags, with completion on tags in target file
  1271. %^G prompt for tags, with completion all tags in all agenda files
  1272. %:keyword specific information for certain link types, see below
  1273. %[pathname] insert the contents of the file given by `pathname'
  1274. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1275. %! Store this note immediately after filling the template
  1276. %? After completing the template, position cursor here.
  1277. Apart from these general escapes, you can access information specific to the
  1278. link type that is created. For example, calling `remember' in emails or gnus
  1279. will record the author and the subject of the message, which you can access
  1280. with %:author and %:subject, respectively. Here is a complete list of what
  1281. is recorded for each link type.
  1282. Link type | Available information
  1283. -------------------+------------------------------------------------------
  1284. bbdb | %:type %:name %:company
  1285. vm, wl, mh, rmail | %:type %:subject %:message-id
  1286. | %:from %:fromname %:fromaddress
  1287. | %:to %:toname %:toaddress
  1288. | %:fromto (either \"to NAME\" or \"from NAME\")
  1289. gnus | %:group, for messages also all email fields
  1290. w3, w3m | %:type %:url
  1291. info | %:type %:file %:node
  1292. calendar | %:type %:date"
  1293. :group 'org-remember
  1294. :get (lambda (var) ; Make sure all entries have 5 elements
  1295. (mapcar (lambda (x)
  1296. (if (not (stringp (car x))) (setq x (cons "" x)))
  1297. (cond ((= (length x) 4) (append x '("")))
  1298. ((= (length x) 3) (append x '("" "")))
  1299. (t x)))
  1300. (default-value var)))
  1301. :type '(repeat
  1302. :tag "enabled"
  1303. (list :value ("" ?a "\n" nil nil)
  1304. (string :tag "Name")
  1305. (character :tag "Selection Key")
  1306. (string :tag "Template")
  1307. (choice
  1308. (file :tag "Destination file")
  1309. (const :tag "Prompt for file" nil))
  1310. (choice
  1311. (string :tag "Destination headline")
  1312. (const :tag "Selection interface for heading")))))
  1313. (defcustom org-reverse-note-order nil
  1314. "Non-nil means, store new notes at the beginning of a file or entry.
  1315. When nil, new notes will be filed to the end of a file or entry.
  1316. This can also be a list with cons cells of regular expressions that
  1317. are matched against file names, and values."
  1318. :group 'org-remember
  1319. :type '(choice
  1320. (const :tag "Reverse always" t)
  1321. (const :tag "Reverse never" nil)
  1322. (repeat :tag "By file name regexp"
  1323. (cons regexp boolean))))
  1324. (defcustom org-refile-targets '((nil . (:level . 1)))
  1325. "Targets for refiling entries with \\[org-refile].
  1326. This is list of cons cells. Each cell contains:
  1327. - a specification of the files to be considered, either a list of files,
  1328. or a symbol whose function or value fields will be used to retrieve
  1329. a file name or a list of file names. Nil means, refile to a different
  1330. heading in the current buffer.
  1331. - A specification of how to find candidate refile targets. This may be
  1332. any of
  1333. - a cons cell (:tag . \"TAG\") to identify refile targes by a tag.
  1334. This tag has to be present in all target headlines, inheritance will
  1335. not be considered.
  1336. - a cons cell (:todo . \"KEYWORD\" to identify refile targets by
  1337. todo keyword.
  1338. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1339. headlines that are refiling targets.
  1340. - a cons cell (:level . N). Any headline of level N is considered a target.
  1341. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1342. ;; FIXME: what if there are a var and func with same name???
  1343. :group 'org-remember
  1344. :type '(repeat
  1345. (cons
  1346. (choice :value org-agenda-files
  1347. (const :tag "All agenda files" org-agenda-files)
  1348. (const :tag "Current buffer" nil)
  1349. (function) (variable) (file))
  1350. (choice :tag "Identify target headline by"
  1351. (cons :tag "Specific tag" (const :tag) (string))
  1352. (cons :tag "TODO keyword" (const :todo) (string))
  1353. (cons :tag "Regular expression" (const :regexp) (regexp))
  1354. (cons :tag "Level number" (const :level) (integer))
  1355. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1356. (defcustom org-refile-use-outline-path nil
  1357. "Non-nil means, provide refile targets as paths.
  1358. So a level 3 headline will be available as level1/level2/level3.
  1359. When the value is `file', also include the file name (without directory)
  1360. into the path. When `full-file-path', include the full file path."
  1361. :group 'org-remember
  1362. :type '(choice
  1363. (const :tag "Not" nil)
  1364. (const :tag "Yes" t)
  1365. (const :tag "Start with file name" file)
  1366. (const :tag "Start with full file path" full-file-path)))
  1367. (defgroup org-todo nil
  1368. "Options concerning TODO items in Org-mode."
  1369. :tag "Org TODO"
  1370. :group 'org)
  1371. (defgroup org-progress nil
  1372. "Options concerning Progress logging in Org-mode."
  1373. :tag "Org Progress"
  1374. :group 'org-time)
  1375. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1376. "List of TODO entry keyword sequences and their interpretation.
  1377. \\<org-mode-map>This is a list of sequences.
  1378. Each sequence starts with a symbol, either `sequence' or `type',
  1379. indicating if the keywords should be interpreted as a sequence of
  1380. action steps, or as different types of TODO items. The first
  1381. keywords are states requiring action - these states will select a headline
  1382. for inclusion into the global TODO list Org-mode produces. If one of
  1383. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1384. signify that no further action is necessary. If \"|\" is not found,
  1385. the last keyword is treated as the only DONE state of the sequence.
  1386. The command \\[org-todo] cycles an entry through these states, and one
  1387. additional state where no keyword is present. For details about this
  1388. cycling, see the manual.
  1389. TODO keywords and interpretation can also be set on a per-file basis with
  1390. the special #+SEQ_TODO and #+TYP_TODO lines.
  1391. For backward compatibility, this variable may also be just a list
  1392. of keywords - in this case the interptetation (sequence or type) will be
  1393. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1394. :group 'org-todo
  1395. :group 'org-keywords
  1396. :type '(choice
  1397. (repeat :tag "Old syntax, just keywords"
  1398. (string :tag "Keyword"))
  1399. (repeat :tag "New syntax"
  1400. (cons
  1401. (choice
  1402. :tag "Interpretation"
  1403. (const :tag "Sequence (cycling hits every state)" sequence)
  1404. (const :tag "Type (cycling directly to DONE)" type))
  1405. (repeat
  1406. (string :tag "Keyword"))))))
  1407. (defvar org-todo-keywords-1 nil)
  1408. (make-variable-buffer-local 'org-todo-keywords-1)
  1409. (defvar org-todo-keywords-for-agenda nil)
  1410. (defvar org-done-keywords-for-agenda nil)
  1411. (defvar org-not-done-keywords nil)
  1412. (make-variable-buffer-local 'org-not-done-keywords)
  1413. (defvar org-done-keywords nil)
  1414. (make-variable-buffer-local 'org-done-keywords)
  1415. (defvar org-todo-heads nil)
  1416. (make-variable-buffer-local 'org-todo-heads)
  1417. (defvar org-todo-sets nil)
  1418. (make-variable-buffer-local 'org-todo-sets)
  1419. (defvar org-todo-log-states nil)
  1420. (make-variable-buffer-local 'org-todo-log-states)
  1421. (defvar org-todo-kwd-alist nil)
  1422. (make-variable-buffer-local 'org-todo-kwd-alist)
  1423. (defvar org-todo-key-alist nil)
  1424. (make-variable-buffer-local 'org-todo-key-alist)
  1425. (defvar org-todo-key-trigger nil)
  1426. (make-variable-buffer-local 'org-todo-key-trigger)
  1427. (defcustom org-todo-interpretation 'sequence
  1428. "Controls how TODO keywords are interpreted.
  1429. This variable is in principle obsolete and is only used for
  1430. backward compatibility, if the interpretation of todo keywords is
  1431. not given already in `org-todo-keywords'. See that variable for
  1432. more information."
  1433. :group 'org-todo
  1434. :group 'org-keywords
  1435. :type '(choice (const sequence)
  1436. (const type)))
  1437. (defcustom org-use-fast-todo-selection 'prefix
  1438. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1439. This variable describes if and under what circumstances the cycling
  1440. mechanism for TODO keywords will be replaced by a single-key, direct
  1441. selection scheme.
  1442. When nil, fast selection is never used.
  1443. When the symbol `prefix', it will be used when `org-todo' is called with
  1444. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1445. in an agenda buffer.
  1446. When t, fast selection is used by default. In this case, the prefix
  1447. argument forces cycling instead.
  1448. In all cases, the special interface is only used if access keys have actually
  1449. been assigned by the user, i.e. if keywords in the configuration are followed
  1450. by a letter in parenthesis, like TODO(t)."
  1451. :group 'org-todo
  1452. :type '(choice
  1453. (const :tag "Never" nil)
  1454. (const :tag "By default" t)
  1455. (const :tag "Only with C-u C-c C-t" prefix)))
  1456. (defcustom org-after-todo-state-change-hook nil
  1457. "Hook which is run after the state of a TODO item was changed.
  1458. The new state (a string with a TODO keyword, or nil) is available in the
  1459. Lisp variable `state'."
  1460. :group 'org-todo
  1461. :type 'hook)
  1462. (defcustom org-log-done nil
  1463. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1464. When the state of an entry is changed from nothing or a DONE state to
  1465. a not-done TODO state, remove a previous closing date.
  1466. This can also be a list of symbols indicating under which conditions
  1467. the time stamp recording the action should be annotated with a short note.
  1468. Valid members of this list are
  1469. done Offer to record a note when marking entries done
  1470. state Offer to record a note whenever changing the TODO state
  1471. of an item. This is only relevant if TODO keywords are
  1472. interpreted as sequence, see variable `org-todo-interpretation'.
  1473. When `state' is set, this includes tracking `done'.
  1474. clock-out Offer to record a note when clocking out of an item.
  1475. A separate window will then pop up and allow you to type a note.
  1476. After finishing with C-c C-c, the note will be added directly after the
  1477. timestamp, as a plain list item. See also the variable
  1478. `org-log-note-headings'.
  1479. Logging can also be configured on a per-file basis by adding one of
  1480. the following lines anywhere in the buffer:
  1481. #+STARTUP: logdone
  1482. #+STARTUP: nologging
  1483. #+STARTUP: lognotedone
  1484. #+STARTUP: lognotestate
  1485. #+STARTUP: lognoteclock-out
  1486. You can have local logging settings for a subtree by setting the LOGGING
  1487. property to one or more of these keywords."
  1488. :group 'org-todo
  1489. :group 'org-progress
  1490. :type '(choice
  1491. (const :tag "off" nil)
  1492. (const :tag "on" t)
  1493. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1494. (const :tag "when item is marked DONE" done)
  1495. (const :tag "when TODO state changes" state)
  1496. (const :tag "when clocking out" clock-out))))
  1497. (defcustom org-log-done-with-time t
  1498. "Non-nil means, the CLOSED time stamp will contain date and time.
  1499. When nil, only the date will be recorded."
  1500. :group 'org-progress
  1501. :type 'boolean)
  1502. (defcustom org-log-note-headings
  1503. '((done . "CLOSING NOTE %t")
  1504. (state . "State %-12s %t")
  1505. (clock-out . ""))
  1506. "Headings for notes added when clocking out or closing TODO items.
  1507. The value is an alist, with the car being a symbol indicating the note
  1508. context, and the cdr is the heading to be used. The heading may also be the
  1509. empty string.
  1510. %t in the heading will be replaced by a time stamp.
  1511. %s will be replaced by the new TODO state, in double quotes.
  1512. %u will be replaced by the user name.
  1513. %U will be replaced by the full user name."
  1514. :group 'org-todo
  1515. :group 'org-progress
  1516. :type '(list :greedy t
  1517. (cons (const :tag "Heading when closing an item" done) string)
  1518. (cons (const :tag
  1519. "Heading when changing todo state (todo sequence only)"
  1520. state) string)
  1521. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1522. (defcustom org-log-states-order-reversed t
  1523. "Non-nil means, the latest state change note will be directly after heading.
  1524. When nil, the notes will be orderer according to time."
  1525. :group 'org-todo
  1526. :group 'org-progress
  1527. :type 'boolean)
  1528. (defcustom org-log-repeat t
  1529. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1530. When nil, no note will be taken.
  1531. This option can also be set with on a per-file-basis with
  1532. #+STARTUP: logrepeat
  1533. #+STARTUP: nologrepeat
  1534. You can have local logging settings for a subtree by setting the LOGGING
  1535. property to one or more of these keywords."
  1536. :group 'org-todo
  1537. :group 'org-progress
  1538. :type 'boolean)
  1539. (defcustom org-clock-into-drawer 2
  1540. "Should clocking info be wrapped into a drawer?
  1541. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1542. If necessary, the drawer will be created.
  1543. When nil, the drawer will not be created, but used when present.
  1544. When an integer and the number of clocking entries in an item
  1545. reaches or exceeds this number, a drawer will be created."
  1546. :group 'org-todo
  1547. :group 'org-progress
  1548. :type '(choice
  1549. (const :tag "Always" t)
  1550. (const :tag "Only when drawer exists" nil)
  1551. (integer :tag "When at least N clock entries")))
  1552. (defcustom org-clock-out-when-done t
  1553. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1554. Nil means, clock will keep running until stopped explicitly with
  1555. `C-c C-x C-o', or until the clock is started in a different item."
  1556. :group 'org-progress
  1557. :type 'boolean)
  1558. (defcustom org-clock-in-switch-to-state nil
  1559. "Set task to a special todo state while clocking it.
  1560. The value should be the state to which the entry should be switched."
  1561. :group 'org-progress
  1562. :group 'org-todo
  1563. :type '(choice
  1564. (const :tag "Don't force a state" nil)
  1565. (string :tag "State")))
  1566. (defgroup org-priorities nil
  1567. "Priorities in Org-mode."
  1568. :tag "Org Priorities"
  1569. :group 'org-todo)
  1570. (defcustom org-highest-priority ?A
  1571. "The highest priority of TODO items. A character like ?A, ?B etc.
  1572. Must have a smaller ASCII number than `org-lowest-priority'."
  1573. :group 'org-priorities
  1574. :type 'character)
  1575. (defcustom org-lowest-priority ?C
  1576. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1577. Must have a larger ASCII number than `org-highest-priority'."
  1578. :group 'org-priorities
  1579. :type 'character)
  1580. (defcustom org-default-priority ?B
  1581. "The default priority of TODO items.
  1582. This is the priority an item get if no explicit priority is given."
  1583. :group 'org-priorities
  1584. :type 'character)
  1585. (defcustom org-priority-start-cycle-with-default t
  1586. "Non-nil means, start with default priority when starting to cycle.
  1587. When this is nil, the first step in the cycle will be (depending on the
  1588. command used) one higher or lower that the default priority."
  1589. :group 'org-priorities
  1590. :type 'boolean)
  1591. (defgroup org-time nil
  1592. "Options concerning time stamps and deadlines in Org-mode."
  1593. :tag "Org Time"
  1594. :group 'org)
  1595. (defcustom org-insert-labeled-timestamps-at-point nil
  1596. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1597. When nil, these labeled time stamps are forces into the second line of an
  1598. entry, just after the headline. When scheduling from the global TODO list,
  1599. the time stamp will always be forced into the second line."
  1600. :group 'org-time
  1601. :type 'boolean)
  1602. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1603. "Formats for `format-time-string' which are used for time stamps.
  1604. It is not recommended to change this constant.")
  1605. (defcustom org-time-stamp-rounding-minutes 0
  1606. "Number of minutes to round time stamps to upon insertion.
  1607. When zero, insert the time unmodified. Useful rounding numbers
  1608. should be factors of 60, so for example 5, 10, 15.
  1609. When this is not zero, you can still force an exact time-stamp by using
  1610. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1611. :group 'org-time
  1612. :type 'integer)
  1613. (defcustom org-display-custom-times nil
  1614. "Non-nil means, overlay custom formats over all time stamps.
  1615. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1616. To turn this on on a per-file basis, insert anywhere in the file:
  1617. #+STARTUP: customtime"
  1618. :group 'org-time
  1619. :set 'set-default
  1620. :type 'sexp)
  1621. (make-variable-buffer-local 'org-display-custom-times)
  1622. (defcustom org-time-stamp-custom-formats
  1623. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1624. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1625. These are overlayed over the default ISO format if the variable
  1626. `org-display-custom-times' is set. Time like %H:%M should be at the
  1627. end of the second format."
  1628. :group 'org-time
  1629. :type 'sexp)
  1630. (defun org-time-stamp-format (&optional long inactive)
  1631. "Get the right format for a time string."
  1632. (let ((f (if long (cdr org-time-stamp-formats)
  1633. (car org-time-stamp-formats))))
  1634. (if inactive
  1635. (concat "[" (substring f 1 -1) "]")
  1636. f)))
  1637. (defcustom org-read-date-prefer-future t
  1638. "Non-nil means, assume future for incomplete date input from user.
  1639. This affects the following situations:
  1640. 1. The user gives a day, but no month.
  1641. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1642. read this as the third of *next* month. However, if you enter \"17\",
  1643. it will be considered as *this* month.
  1644. 2. The user gives a month but not a year.
  1645. For example, if it is april and you enter \"feb 2\", this will be read
  1646. as feb 2, *next* year. \"May 5\", however, will be this year.
  1647. When this option is nil, the current month and year will always be used
  1648. as defaults."
  1649. :group 'org-time
  1650. :type 'boolean)
  1651. (defcustom org-read-date-display-live t
  1652. "Non-nil means, display current interpretation of date prompt live.
  1653. This display will be in an overlay, in the minibuffer."
  1654. :group 'org-time
  1655. :type 'boolean)
  1656. (defcustom org-read-date-popup-calendar t
  1657. "Non-nil means, pop up a calendar when prompting for a date.
  1658. In the calendar, the date can be selected with mouse-1. However, the
  1659. minibuffer will also be active, and you can simply enter the date as well.
  1660. When nil, only the minibuffer will be available."
  1661. :group 'org-time
  1662. :type 'boolean)
  1663. (if (fboundp 'defvaralias)
  1664. (defvaralias 'org-popup-calendar-for-date-prompt
  1665. 'org-read-date-popup-calendar))
  1666. (defcustom org-extend-today-until 0
  1667. "The hour when your day really ends.
  1668. This has influence for the following applications:
  1669. - When switching the agenda to \"today\". It it is still earlier than
  1670. the time given here, the day recognized as TODAY is actually yesterday.
  1671. - When a date is read from the user and it is still before the time given
  1672. here, the current date and time will be assumed to be yesterday, 23:59.
  1673. FIXME:
  1674. IMPORTANT: This is still a very experimental feature, it may disappear
  1675. again or it may be extended to mean more things."
  1676. :group 'org-time
  1677. :type 'number)
  1678. (defcustom org-edit-timestamp-down-means-later nil
  1679. "Non-nil means, S-down will increase the time in a time stamp.
  1680. When nil, S-up will increase."
  1681. :group 'org-time
  1682. :type 'boolean)
  1683. (defcustom org-calendar-follow-timestamp-change t
  1684. "Non-nil means, make the calendar window follow timestamp changes.
  1685. When a timestamp is modified and the calendar window is visible, it will be
  1686. moved to the new date."
  1687. :group 'org-time
  1688. :type 'boolean)
  1689. (defcustom org-clock-heading-function nil
  1690. "When non-nil, should be a function to create `org-clock-heading'.
  1691. This is the string shown in the mode line when a clock is running.
  1692. The function is called with point at the beginning of the headline."
  1693. :group 'org-time ; FIXME: Should we have a separate group????
  1694. :type 'function)
  1695. (defgroup org-tags nil
  1696. "Options concerning tags in Org-mode."
  1697. :tag "Org Tags"
  1698. :group 'org)
  1699. (defcustom org-tag-alist nil
  1700. "List of tags allowed in Org-mode files.
  1701. When this list is nil, Org-mode will base TAG input on what is already in the
  1702. buffer.
  1703. The value of this variable is an alist, the car of each entry must be a
  1704. keyword as a string, the cdr may be a character that is used to select
  1705. that tag through the fast-tag-selection interface.
  1706. See the manual for details."
  1707. :group 'org-tags
  1708. :type '(repeat
  1709. (choice
  1710. (cons (string :tag "Tag name")
  1711. (character :tag "Access char"))
  1712. (const :tag "Start radio group" (:startgroup))
  1713. (const :tag "End radio group" (:endgroup)))))
  1714. (defcustom org-use-fast-tag-selection 'auto
  1715. "Non-nil means, use fast tag selection scheme.
  1716. This is a special interface to select and deselect tags with single keys.
  1717. When nil, fast selection is never used.
  1718. When the symbol `auto', fast selection is used if and only if selection
  1719. characters for tags have been configured, either through the variable
  1720. `org-tag-alist' or through a #+TAGS line in the buffer.
  1721. When t, fast selection is always used and selection keys are assigned
  1722. automatically if necessary."
  1723. :group 'org-tags
  1724. :type '(choice
  1725. (const :tag "Always" t)
  1726. (const :tag "Never" nil)
  1727. (const :tag "When selection characters are configured" 'auto)))
  1728. (defcustom org-fast-tag-selection-single-key nil
  1729. "Non-nil means, fast tag selection exits after first change.
  1730. When nil, you have to press RET to exit it.
  1731. During fast tag selection, you can toggle this flag with `C-c'.
  1732. This variable can also have the value `expert'. In this case, the window
  1733. displaying the tags menu is not even shown, until you press C-c again."
  1734. :group 'org-tags
  1735. :type '(choice
  1736. (const :tag "No" nil)
  1737. (const :tag "Yes" t)
  1738. (const :tag "Expert" expert)))
  1739. (defvar org-fast-tag-selection-include-todo nil
  1740. "Non-nil means, fast tags selection interface will also offer TODO states.
  1741. This is an undocumented feature, you should not rely on it.")
  1742. (defcustom org-tags-column -80
  1743. "The column to which tags should be indented in a headline.
  1744. If this number is positive, it specifies the column. If it is negative,
  1745. it means that the tags should be flushright to that column. For example,
  1746. -80 works well for a normal 80 character screen."
  1747. :group 'org-tags
  1748. :type 'integer)
  1749. (defcustom org-auto-align-tags t
  1750. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1751. These operations change the length of a headline and therefore shift
  1752. the tags around. With this options turned on, after each such operation
  1753. the tags are again aligned to `org-tags-column'."
  1754. :group 'org-tags
  1755. :type 'boolean)
  1756. (defcustom org-use-tag-inheritance t
  1757. "Non-nil means, tags in levels apply also for sublevels.
  1758. When nil, only the tags directly given in a specific line apply there.
  1759. If you turn off this option, you very likely want to turn on the
  1760. companion option `org-tags-match-list-sublevels'."
  1761. :group 'org-tags
  1762. :type 'boolean)
  1763. (defcustom org-tags-match-list-sublevels nil
  1764. "Non-nil means list also sublevels of headlines matching tag search.
  1765. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1766. the sublevels of a headline matching a tag search often also match
  1767. the same search. Listing all of them can create very long lists.
  1768. Setting this variable to nil causes subtrees of a match to be skipped.
  1769. This option is off by default, because inheritance in on. If you turn
  1770. inheritance off, you very likely want to turn this option on.
  1771. As a special case, if the tag search is restricted to TODO items, the
  1772. value of this variable is ignored and sublevels are always checked, to
  1773. make sure all corresponding TODO items find their way into the list."
  1774. :group 'org-tags
  1775. :type 'boolean)
  1776. (defvar org-tags-history nil
  1777. "History of minibuffer reads for tags.")
  1778. (defvar org-last-tags-completion-table nil
  1779. "The last used completion table for tags.")
  1780. (defvar org-after-tags-change-hook nil
  1781. "Hook that is run after the tags in a line have changed.")
  1782. (defgroup org-properties nil
  1783. "Options concerning properties in Org-mode."
  1784. :tag "Org Properties"
  1785. :group 'org)
  1786. (defcustom org-property-format "%-10s %s"
  1787. "How property key/value pairs should be formatted by `indent-line'.
  1788. When `indent-line' hits a property definition, it will format the line
  1789. according to this format, mainly to make sure that the values are
  1790. lined-up with respect to each other."
  1791. :group 'org-properties
  1792. :type 'string)
  1793. (defcustom org-use-property-inheritance nil
  1794. "Non-nil means, properties apply also for sublevels.
  1795. This setting is only relevant during property searches, not when querying
  1796. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1797. you need to call `org-entry-get' with the inheritance flag.
  1798. Turning this on can cause significant overhead when doing a search, so
  1799. this is turned off by default.
  1800. When nil, only the properties directly given in the current entry count.
  1801. The value may also be a list of properties that shouldhave inheritance.
  1802. However, note that some special properties use inheritance under special
  1803. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1804. and the properties ending in \"_ALL\" when they are used as descriptor
  1805. for valid values of a property."
  1806. :group 'org-properties
  1807. :type '(choice
  1808. (const :tag "Not" nil)
  1809. (const :tag "Always" nil)
  1810. (repeat :tag "Specific properties" (string :tag "Property"))))
  1811. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1812. "The default column format, if no other format has been defined.
  1813. This variable can be set on the per-file basis by inserting a line
  1814. #+COLUMNS: %25ITEM ....."
  1815. :group 'org-properties
  1816. :type 'string)
  1817. (defcustom org-global-properties nil
  1818. "List of property/value pairs that can be inherited by any entry.
  1819. You can set buffer-local values for this by adding lines like
  1820. #+PROPERTY: NAME VALUE"
  1821. :group 'org-properties
  1822. :type '(repeat
  1823. (cons (string :tag "Property")
  1824. (string :tag "Value"))))
  1825. (defvar org-local-properties nil
  1826. "List of property/value pairs that can be inherited by any entry.
  1827. Valid for the current buffer.
  1828. This variable is populated from #+PROPERTY lines.")
  1829. (defgroup org-agenda nil
  1830. "Options concerning agenda views in Org-mode."
  1831. :tag "Org Agenda"
  1832. :group 'org)
  1833. (defvar org-category nil
  1834. "Variable used by org files to set a category for agenda display.
  1835. Such files should use a file variable to set it, for example
  1836. # -*- mode: org; org-category: \"ELisp\"
  1837. or contain a special line
  1838. #+CATEGORY: ELisp
  1839. If the file does not specify a category, then file's base name
  1840. is used instead.")
  1841. (make-variable-buffer-local 'org-category)
  1842. (defcustom org-agenda-files nil
  1843. "The files to be used for agenda display.
  1844. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1845. \\[org-remove-file]. You can also use customize to edit the list.
  1846. If an entry is a directory, all files in that directory that are matched by
  1847. `org-agenda-file-regexp' will be part of the file list.
  1848. If the value of the variable is not a list but a single file name, then
  1849. the list of agenda files is actually stored and maintained in that file, one
  1850. agenda file per line."
  1851. :group 'org-agenda
  1852. :type '(choice
  1853. (repeat :tag "List of files and directories" file)
  1854. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1855. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1856. "Regular expression to match files for `org-agenda-files'.
  1857. If any element in the list in that variable contains a directory instead
  1858. of a normal file, all files in that directory that are matched by this
  1859. regular expression will be included."
  1860. :group 'org-agenda
  1861. :type 'regexp)
  1862. (defcustom org-agenda-skip-unavailable-files nil
  1863. "t means to just skip non-reachable files in `org-agenda-files'.
  1864. Nil means to remove them, after a query, from the list."
  1865. :group 'org-agenda
  1866. :type 'boolean)
  1867. (defcustom org-agenda-multi-occur-extra-files nil
  1868. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1869. The files in `org-agenda-files' are always searched."
  1870. :group 'org-agenda
  1871. :type '(repeat file))
  1872. (defcustom org-agenda-confirm-kill 1
  1873. "When set, remote killing from the agenda buffer needs confirmation.
  1874. When t, a confirmation is always needed. When a number N, confirmation is
  1875. only needed when the text to be killed contains more than N non-white lines."
  1876. :group 'org-agenda
  1877. :type '(choice
  1878. (const :tag "Never" nil)
  1879. (const :tag "Always" t)
  1880. (number :tag "When more than N lines")))
  1881. (defcustom org-calendar-to-agenda-key [?c]
  1882. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1883. The command `org-calendar-goto-agenda' will be bound to this key. The
  1884. default is the character `c' because then `c' can be used to switch back and
  1885. forth between agenda and calendar."
  1886. :group 'org-agenda
  1887. :type 'sexp)
  1888. (defcustom org-agenda-compact-blocks nil
  1889. "Non-nil means, make the block agenda more compact.
  1890. This is done by leaving out unnecessary lines."
  1891. :group 'org-agenda
  1892. :type nil)
  1893. (defgroup org-agenda-export nil
  1894. "Options concerning exporting agenda views in Org-mode."
  1895. :tag "Org Agenda Export"
  1896. :group 'org-agenda)
  1897. (defcustom org-agenda-with-colors t
  1898. "Non-nil means, use colors in agenda views."
  1899. :group 'org-agenda-export
  1900. :type 'boolean)
  1901. (defcustom org-agenda-exporter-settings nil
  1902. "Alist of variable/value pairs that should be active during agenda export.
  1903. This is a good place to set uptions for ps-print and for htmlize."
  1904. :group 'org-agenda-export
  1905. :type '(repeat
  1906. (list
  1907. (variable)
  1908. (sexp :tag "Value"))))
  1909. (defcustom org-agenda-export-html-style ""
  1910. "The style specification for exported HTML Agenda files.
  1911. If this variable contains a string, it will replace the default <style>
  1912. section as produced by `htmlize'.
  1913. Since there are different ways of setting style information, this variable
  1914. needs to contain the full HTML structure to provide a style, including the
  1915. surrounding HTML tags. The style specifications should include definitions
  1916. the fonts used by the agenda, here is an example:
  1917. <style type=\"text/css\">
  1918. p { font-weight: normal; color: gray; }
  1919. .org-agenda-structure {
  1920. font-size: 110%;
  1921. color: #003399;
  1922. font-weight: 600;
  1923. }
  1924. .org-todo {
  1925. color: #cc6666;Week-agenda:
  1926. font-weight: bold;
  1927. }
  1928. .org-done {
  1929. color: #339933;
  1930. }
  1931. .title { text-align: center; }
  1932. .todo, .deadline { color: red; }
  1933. .done { color: green; }
  1934. </style>
  1935. or, if you want to keep the style in a file,
  1936. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1937. As the value of this option simply gets inserted into the HTML <head> header,
  1938. you can \"misuse\" it to also add other text to the header. However,
  1939. <style>...</style> is required, if not present the variable will be ignored."
  1940. :group 'org-agenda-export
  1941. :group 'org-export-html
  1942. :type 'string)
  1943. (defgroup org-agenda-custom-commands nil
  1944. "Options concerning agenda views in Org-mode."
  1945. :tag "Org Agenda Custom Commands"
  1946. :group 'org-agenda)
  1947. (defcustom org-agenda-custom-commands nil
  1948. "Custom commands for the agenda.
  1949. These commands will be offered on the splash screen displayed by the
  1950. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1951. (key desc type match options files)
  1952. key The key (one or more characters as a string) to be associated
  1953. with the command.
  1954. desc A description of the commend, when omitted or nil, a default
  1955. description is built using MATCH.
  1956. type The command type, any of the following symbols:
  1957. todo Entries with a specific TODO keyword, in all agenda files.
  1958. tags Tags match in all agenda files.
  1959. tags-todo Tags match in all agenda files, TODO entries only.
  1960. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1961. tags-tree Sparse tree with all tags matches in *current* file.
  1962. occur-tree Occur sparse tree for *current* file.
  1963. ... A user-defined function.
  1964. match What to search for:
  1965. - a single keyword for TODO keyword searches
  1966. - a tags match expression for tags searches
  1967. - a regular expression for occur searches
  1968. options A list of option settings, similar to that in a let form, so like
  1969. this: ((opt1 val1) (opt2 val2) ...)
  1970. files A list of files file to write the produced agenda buffer to
  1971. with the command `org-store-agenda-views'.
  1972. If a file name ends in \".html\", an HTML version of the buffer
  1973. is written out. If it ends in \".ps\", a postscript version is
  1974. produced. Otherwide, only the plain text is written to the file.
  1975. You can also define a set of commands, to create a composite agenda buffer.
  1976. In this case, an entry looks like this:
  1977. (key desc (cmd1 cmd2 ...) general-options file)
  1978. where
  1979. desc A description string to be displayed in the dispatcher menu.
  1980. cmd An agenda command, similar to the above. However, tree commands
  1981. are no allowed, but instead you can get agenda and global todo list.
  1982. So valid commands for a set are:
  1983. (agenda)
  1984. (alltodo)
  1985. (stuck)
  1986. (todo \"match\" options files)
  1987. (tags \"match\" options files)
  1988. (tags-todo \"match\" options files)
  1989. Each command can carry a list of options, and another set of options can be
  1990. given for the whole set of commands. Individual command options take
  1991. precedence over the general options.
  1992. When using several characters as key to a command, the first characters
  1993. are prefix commands. For the dispatcher to display useful information, you
  1994. should provide a description for the prefix, like
  1995. (setq org-agenda-custom-commands
  1996. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  1997. (\"hl\" tags \"+HOME+Lisa\")
  1998. (\"hp\" tags \"+HOME+Peter\")
  1999. (\"hk\" tags \"+HOME+Kim\")))"
  2000. :group 'org-agenda-custom-commands
  2001. :type '(repeat
  2002. (choice :value ("a" "" tags "" nil)
  2003. (list :tag "Single command"
  2004. (string :tag "Access Key(s) ")
  2005. (option (string :tag "Description"))
  2006. (choice
  2007. (const :tag "Agenda" agenda)
  2008. (const :tag "TODO list" alltodo)
  2009. (const :tag "Stuck projects" stuck)
  2010. (const :tag "Tags search (all agenda files)" tags)
  2011. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2012. (const :tag "TODO keyword search (all agenda files)" todo)
  2013. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2014. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2015. (const :tag "Occur tree (current buffer)" occur-tree)
  2016. (sexp :tag "Other, user-defined function"))
  2017. (string :tag "Match")
  2018. (repeat :tag "Local options"
  2019. (list (variable :tag "Option") (sexp :tag "Value")))
  2020. (option (repeat :tag "Export" (file :tag "Export to"))))
  2021. (list :tag "Command series, all agenda files"
  2022. (string :tag "Access Key(s)")
  2023. (string :tag "Description ")
  2024. (repeat
  2025. (choice
  2026. (const :tag "Agenda" (agenda))
  2027. (const :tag "TODO list" (alltodo))
  2028. (const :tag "Stuck projects" (stuck))
  2029. (list :tag "Tags search"
  2030. (const :format "" tags)
  2031. (string :tag "Match")
  2032. (repeat :tag "Local options"
  2033. (list (variable :tag "Option")
  2034. (sexp :tag "Value"))))
  2035. (list :tag "Tags search, TODO entries only"
  2036. (const :format "" tags-todo)
  2037. (string :tag "Match")
  2038. (repeat :tag "Local options"
  2039. (list (variable :tag "Option")
  2040. (sexp :tag "Value"))))
  2041. (list :tag "TODO keyword search"
  2042. (const :format "" todo)
  2043. (string :tag "Match")
  2044. (repeat :tag "Local options"
  2045. (list (variable :tag "Option")
  2046. (sexp :tag "Value"))))
  2047. (list :tag "Other, user-defined function"
  2048. (symbol :tag "function")
  2049. (string :tag "Match")
  2050. (repeat :tag "Local options"
  2051. (list (variable :tag "Option")
  2052. (sexp :tag "Value"))))))
  2053. (repeat :tag "General options"
  2054. (list (variable :tag "Option")
  2055. (sexp :tag "Value")))
  2056. (option (repeat :tag "Export" (file :tag "Export to"))))
  2057. (cons :tag "Prefix key documentation"
  2058. (string :tag "Access Key(s)")
  2059. (string :tag "Description ")))))
  2060. (defcustom org-stuck-projects
  2061. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2062. "How to identify stuck projects.
  2063. This is a list of four items:
  2064. 1. A tags/todo matcher string that is used to identify a project.
  2065. The entire tree below a headline matched by this is considered one project.
  2066. 2. A list of TODO keywords identifying non-stuck projects.
  2067. If the project subtree contains any headline with one of these todo
  2068. keywords, the project is considered to be not stuck. If you specify
  2069. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2070. 3. A list of tags identifying non-stuck projects.
  2071. If the project subtree contains any headline with one of these tags,
  2072. the project is considered to be not stuck. If you specify \"*\" as
  2073. a tag, any tag will mark the project unstuck.
  2074. 4. An arbitrary regular expression matching non-stuck projects.
  2075. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2076. or `C-c a #' to produce the list."
  2077. :group 'org-agenda-custom-commands
  2078. :type '(list
  2079. (string :tag "Tags/TODO match to identify a project")
  2080. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2081. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2082. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2083. (defgroup org-agenda-skip nil
  2084. "Options concerning skipping parts of agenda files."
  2085. :tag "Org Agenda Skip"
  2086. :group 'org-agenda)
  2087. (defcustom org-agenda-todo-list-sublevels t
  2088. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2089. When nil, the sublevels of a TODO entry are not checked, resulting in
  2090. potentially much shorter TODO lists."
  2091. :group 'org-agenda-skip
  2092. :group 'org-todo
  2093. :type 'boolean)
  2094. (defcustom org-agenda-todo-ignore-with-date nil
  2095. "Non-nil means, don't show entries with a date in the global todo list.
  2096. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2097. but don't want them to show up in the TODO list.
  2098. When this is set, it also covers deadlines and scheduled items, the settings
  2099. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2100. will be ignored."
  2101. :group 'org-agenda-skip
  2102. :group 'org-todo
  2103. :type 'boolean)
  2104. (defcustom org-agenda-todo-ignore-scheduled nil
  2105. "Non-nil means, don't show scheduled entries in the global todo list.
  2106. The idea behind this is that by scheduling it, you have already taken care
  2107. of this item.
  2108. See also `org-agenda-todo-ignore-with-date'."
  2109. :group 'org-agenda-skip
  2110. :group 'org-todo
  2111. :type 'boolean)
  2112. (defcustom org-agenda-todo-ignore-deadlines nil
  2113. "Non-nil means, don't show near deadline entries in the global todo list.
  2114. Near means closer than `org-deadline-warning-days' days.
  2115. The idea behind this is that such items will appear in the agenda anyway.
  2116. See also `org-agenda-todo-ignore-with-date'."
  2117. :group 'org-agenda-skip
  2118. :group 'org-todo
  2119. :type 'boolean)
  2120. (defcustom org-agenda-skip-scheduled-if-done nil
  2121. "Non-nil means don't show scheduled items in agenda when they are done.
  2122. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2123. it applies only to the actual date of the scheduling. Warnings about
  2124. an item with a past scheduling dates are always turned off when the item
  2125. is DONE."
  2126. :group 'org-agenda-skip
  2127. :type 'boolean)
  2128. (defcustom org-agenda-skip-deadline-if-done nil
  2129. "Non-nil means don't show deadines when the corresponding item is done.
  2130. When nil, the deadline is still shown and should give you a happy feeling.
  2131. This is relevant for the daily/weekly agenda. And it applied only to the
  2132. actualy date of the deadline. Warnings about approching and past-due
  2133. deadlines are always turned off when the item is DONE."
  2134. :group 'org-agenda-skip
  2135. :type 'boolean)
  2136. (defcustom org-agenda-skip-timestamp-if-done nil
  2137. "Non-nil means don't don't select item by timestamp or -range if it is DONE."
  2138. :group 'org-agenda-skip
  2139. :type 'boolean)
  2140. (defcustom org-timeline-show-empty-dates 3
  2141. "Non-nil means, `org-timeline' also shows dates without an entry.
  2142. When nil, only the days which actually have entries are shown.
  2143. When t, all days between the first and the last date are shown.
  2144. When an integer, show also empty dates, but if there is a gap of more than
  2145. N days, just insert a special line indicating the size of the gap."
  2146. :group 'org-agenda-skip
  2147. :type '(choice
  2148. (const :tag "None" nil)
  2149. (const :tag "All" t)
  2150. (number :tag "at most")))
  2151. (defgroup org-agenda-startup nil
  2152. "Options concerning initial settings in the Agenda in Org Mode."
  2153. :tag "Org Agenda Startup"
  2154. :group 'org-agenda)
  2155. (defcustom org-finalize-agenda-hook nil
  2156. "Hook run just before displaying an agenda buffer."
  2157. :group 'org-agenda-startup
  2158. :type 'hook)
  2159. (defcustom org-agenda-mouse-1-follows-link nil
  2160. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2161. A longer mouse click will still set point. Does not wortk on XEmacs.
  2162. Needs to be set before org.el is loaded."
  2163. :group 'org-agenda-startup
  2164. :type 'boolean)
  2165. (defcustom org-agenda-start-with-follow-mode nil
  2166. "The initial value of follow-mode in a newly created agenda window."
  2167. :group 'org-agenda-startup
  2168. :type 'boolean)
  2169. (defgroup org-agenda-windows nil
  2170. "Options concerning the windows used by the Agenda in Org Mode."
  2171. :tag "Org Agenda Windows"
  2172. :group 'org-agenda)
  2173. (defcustom org-agenda-window-setup 'reorganize-frame
  2174. "How the agenda buffer should be displayed.
  2175. Possible values for this option are:
  2176. current-window Show agenda in the current window, keeping all other windows.
  2177. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2178. other-window Use `switch-to-buffer-other-window' to display agenda.
  2179. reorganize-frame Show only two windows on the current frame, the current
  2180. window and the agenda.
  2181. See also the variable `org-agenda-restore-windows-after-quit'."
  2182. :group 'org-agenda-windows
  2183. :type '(choice
  2184. (const current-window)
  2185. (const other-frame)
  2186. (const other-window)
  2187. (const reorganize-frame)))
  2188. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2189. "The min and max height of the agenda window as a fraction of frame height.
  2190. The value of the variable is a cons cell with two numbers between 0 and 1.
  2191. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2192. :group 'org-agenda-windows
  2193. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2194. (defcustom org-agenda-restore-windows-after-quit nil
  2195. "Non-nil means, restore window configuration open exiting agenda.
  2196. Before the window configuration is changed for displaying the agenda,
  2197. the current status is recorded. When the agenda is exited with
  2198. `q' or `x' and this option is set, the old state is restored. If
  2199. `org-agenda-window-setup' is `other-frame', the value of this
  2200. option will be ignored.."
  2201. :group 'org-agenda-windows
  2202. :type 'boolean)
  2203. (defcustom org-indirect-buffer-display 'other-window
  2204. "How should indirect tree buffers be displayed?
  2205. This applies to indirect buffers created with the commands
  2206. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2207. Valid values are:
  2208. current-window Display in the current window
  2209. other-window Just display in another window.
  2210. dedicated-frame Create one new frame, and re-use it each time.
  2211. new-frame Make a new frame each time. Note that in this case
  2212. previously-made indirect buffers are kept, and you need to
  2213. kill these buffers yourself."
  2214. :group 'org-structure
  2215. :group 'org-agenda-windows
  2216. :type '(choice
  2217. (const :tag "In current window" current-window)
  2218. (const :tag "In current frame, other window" other-window)
  2219. (const :tag "Each time a new frame" new-frame)
  2220. (const :tag "One dedicated frame" dedicated-frame)))
  2221. (defgroup org-agenda-daily/weekly nil
  2222. "Options concerning the daily/weekly agenda."
  2223. :tag "Org Agenda Daily/Weekly"
  2224. :group 'org-agenda)
  2225. (defcustom org-agenda-ndays 7
  2226. "Number of days to include in overview display.
  2227. Should be 1 or 7."
  2228. :group 'org-agenda-daily/weekly
  2229. :type 'number)
  2230. (defcustom org-agenda-start-on-weekday 1
  2231. "Non-nil means, start the overview always on the specified weekday.
  2232. 0 denotes Sunday, 1 denotes Monday etc.
  2233. When nil, always start on the current day."
  2234. :group 'org-agenda-daily/weekly
  2235. :type '(choice (const :tag "Today" nil)
  2236. (number :tag "Weekday No.")))
  2237. (defcustom org-agenda-show-all-dates t
  2238. "Non-nil means, `org-agenda' shows every day in the selected range.
  2239. When nil, only the days which actually have entries are shown."
  2240. :group 'org-agenda-daily/weekly
  2241. :type 'boolean)
  2242. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2243. "Format string for displaying dates in the agenda.
  2244. Used by the daily/weekly agenda and by the timeline. This should be
  2245. a format string understood by `format-time-string', or a function returning
  2246. the formatted date as a string. The function must take a single argument,
  2247. a calendar-style date list like (month day year)."
  2248. :group 'org-agenda-daily/weekly
  2249. :type '(choice
  2250. (string :tag "Format string")
  2251. (function :tag "Function")))
  2252. (defun org-agenda-format-date-aligned (date)
  2253. "Format a date string for display in the daily/weekly agenda, or timeline.
  2254. This function makes sure that dates are aligned for easy reading."
  2255. (format "%-9s %2d %s %4d"
  2256. (calendar-day-name date)
  2257. (extract-calendar-day date)
  2258. (calendar-month-name (extract-calendar-month date))
  2259. (extract-calendar-year date)))
  2260. (defcustom org-agenda-include-diary nil
  2261. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2262. :group 'org-agenda-daily/weekly
  2263. :type 'boolean)
  2264. (defcustom org-agenda-include-all-todo nil
  2265. "Set means weekly/daily agenda will always contain all TODO entries.
  2266. The TODO entries will be listed at the top of the agenda, before
  2267. the entries for specific days."
  2268. :group 'org-agenda-daily/weekly
  2269. :type 'boolean)
  2270. (defcustom org-agenda-repeating-timestamp-show-all t
  2271. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2272. When nil, only one occurence is shown, either today or the
  2273. nearest into the future."
  2274. :group 'org-agenda-daily/weekly
  2275. :type 'boolean)
  2276. (defcustom org-deadline-warning-days 14
  2277. "No. of days before expiration during which a deadline becomes active.
  2278. This variable governs the display in sparse trees and in the agenda.
  2279. When negative, it means use this number (the absolute value of it)
  2280. even if a deadline has a different individual lead time specified."
  2281. :group 'org-time
  2282. :group 'org-agenda-daily/weekly
  2283. :type 'number)
  2284. (defcustom org-scheduled-past-days 10000
  2285. "No. of days to continue listing scheduled items that are not marked DONE.
  2286. When an item is scheduled on a date, it shows up in the agenda on this
  2287. day and will be listed until it is marked done for the number of days
  2288. given here."
  2289. :group 'org-agenda-daily/weekly
  2290. :type 'number)
  2291. (defgroup org-agenda-time-grid nil
  2292. "Options concerning the time grid in the Org-mode Agenda."
  2293. :tag "Org Agenda Time Grid"
  2294. :group 'org-agenda)
  2295. (defcustom org-agenda-use-time-grid t
  2296. "Non-nil means, show a time grid in the agenda schedule.
  2297. A time grid is a set of lines for specific times (like every two hours between
  2298. 8:00 and 20:00). The items scheduled for a day at specific times are
  2299. sorted in between these lines.
  2300. For details about when the grid will be shown, and what it will look like, see
  2301. the variable `org-agenda-time-grid'."
  2302. :group 'org-agenda-time-grid
  2303. :type 'boolean)
  2304. (defcustom org-agenda-time-grid
  2305. '((daily today require-timed)
  2306. "----------------"
  2307. (800 1000 1200 1400 1600 1800 2000))
  2308. "The settings for time grid for agenda display.
  2309. This is a list of three items. The first item is again a list. It contains
  2310. symbols specifying conditions when the grid should be displayed:
  2311. daily if the agenda shows a single day
  2312. weekly if the agenda shows an entire week
  2313. today show grid on current date, independent of daily/weekly display
  2314. require-timed show grid only if at least one item has a time specification
  2315. The second item is a string which will be places behing the grid time.
  2316. The third item is a list of integers, indicating the times that should have
  2317. a grid line."
  2318. :group 'org-agenda-time-grid
  2319. :type
  2320. '(list
  2321. (set :greedy t :tag "Grid Display Options"
  2322. (const :tag "Show grid in single day agenda display" daily)
  2323. (const :tag "Show grid in weekly agenda display" weekly)
  2324. (const :tag "Always show grid for today" today)
  2325. (const :tag "Show grid only if any timed entries are present"
  2326. require-timed)
  2327. (const :tag "Skip grid times already present in an entry"
  2328. remove-match))
  2329. (string :tag "Grid String")
  2330. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2331. (defgroup org-agenda-sorting nil
  2332. "Options concerning sorting in the Org-mode Agenda."
  2333. :tag "Org Agenda Sorting"
  2334. :group 'org-agenda)
  2335. (defconst org-sorting-choice
  2336. '(choice
  2337. (const time-up) (const time-down)
  2338. (const category-keep) (const category-up) (const category-down)
  2339. (const tag-down) (const tag-up)
  2340. (const priority-up) (const priority-down))
  2341. "Sorting choices.")
  2342. (defcustom org-agenda-sorting-strategy
  2343. '((agenda time-up category-keep priority-down)
  2344. (todo category-keep priority-down)
  2345. (tags category-keep priority-down))
  2346. "Sorting structure for the agenda items of a single day.
  2347. This is a list of symbols which will be used in sequence to determine
  2348. if an entry should be listed before another entry. The following
  2349. symbols are recognized:
  2350. time-up Put entries with time-of-day indications first, early first
  2351. time-down Put entries with time-of-day indications first, late first
  2352. category-keep Keep the default order of categories, corresponding to the
  2353. sequence in `org-agenda-files'.
  2354. category-up Sort alphabetically by category, A-Z.
  2355. category-down Sort alphabetically by category, Z-A.
  2356. tag-up Sort alphabetically by last tag, A-Z.
  2357. tag-down Sort alphabetically by last tag, Z-A.
  2358. priority-up Sort numerically by priority, high priority last.
  2359. priority-down Sort numerically by priority, high priority first.
  2360. The different possibilities will be tried in sequence, and testing stops
  2361. if one comparison returns a \"not-equal\". For example, the default
  2362. '(time-up category-keep priority-down)
  2363. means: Pull out all entries having a specified time of day and sort them,
  2364. in order to make a time schedule for the current day the first thing in the
  2365. agenda listing for the day. Of the entries without a time indication, keep
  2366. the grouped in categories, don't sort the categories, but keep them in
  2367. the sequence given in `org-agenda-files'. Within each category sort by
  2368. priority.
  2369. Leaving out `category-keep' would mean that items will be sorted across
  2370. categories by priority.
  2371. Instead of a single list, this can also be a set of list for specific
  2372. contents, with a context symbol in the car of the list, any of
  2373. `agenda', `todo', `tags' for the corresponding agenda views."
  2374. :group 'org-agenda-sorting
  2375. :type `(choice
  2376. (repeat :tag "General" ,org-sorting-choice)
  2377. (list :tag "Individually"
  2378. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2379. (repeat ,org-sorting-choice))
  2380. (cons (const :tag "Strategy for TODO lists" todo)
  2381. (repeat ,org-sorting-choice))
  2382. (cons (const :tag "Strategy for Tags matches" tags)
  2383. (repeat ,org-sorting-choice)))))
  2384. (defcustom org-sort-agenda-notime-is-late t
  2385. "Non-nil means, items without time are considered late.
  2386. This is only relevant for sorting. When t, items which have no explicit
  2387. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2388. do have a time. When nil, the default time is before 0:00. You can use this
  2389. option to decide if the schedule for today should come before or after timeless
  2390. agenda entries."
  2391. :group 'org-agenda-sorting
  2392. :type 'boolean)
  2393. (defgroup org-agenda-line-format nil
  2394. "Options concerning the entry prefix in the Org-mode agenda display."
  2395. :tag "Org Agenda Line Format"
  2396. :group 'org-agenda)
  2397. (defcustom org-agenda-prefix-format
  2398. '((agenda . " %-12:c%?-12t% s")
  2399. (timeline . " % s")
  2400. (todo . " %-12:c")
  2401. (tags . " %-12:c"))
  2402. "Format specifications for the prefix of items in the agenda views.
  2403. An alist with four entries, for the different agenda types. The keys to the
  2404. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2405. are format strings.
  2406. This format works similar to a printf format, with the following meaning:
  2407. %c the category of the item, \"Diary\" for entries from the diary, or
  2408. as given by the CATEGORY keyword or derived from the file name.
  2409. %T the *last* tag of the item. Last because inherited tags come
  2410. first in the list.
  2411. %t the time-of-day specification if one applies to the entry, in the
  2412. format HH:MM
  2413. %s Scheduling/Deadline information, a short string
  2414. All specifiers work basically like the standard `%s' of printf, but may
  2415. contain two additional characters: A question mark just after the `%' and
  2416. a whitespace/punctuation character just before the final letter.
  2417. If the first character after `%' is a question mark, the entire field
  2418. will only be included if the corresponding value applies to the
  2419. current entry. This is useful for fields which should have fixed
  2420. width when present, but zero width when absent. For example,
  2421. \"%?-12t\" will result in a 12 character time field if a time of the
  2422. day is specified, but will completely disappear in entries which do
  2423. not contain a time.
  2424. If there is punctuation or whitespace character just before the final
  2425. format letter, this character will be appended to the field value if
  2426. the value is not empty. For example, the format \"%-12:c\" leads to
  2427. \"Diary: \" if the category is \"Diary\". If the category were be
  2428. empty, no additional colon would be interted.
  2429. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2430. - Indent the line with two space characters
  2431. - Give the category in a 12 chars wide field, padded with whitespace on
  2432. the right (because of `-'). Append a colon if there is a category
  2433. (because of `:').
  2434. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2435. time, don't put in an empty field, just skip it (because of '?').
  2436. - Finally, put the scheduling information and append a whitespace.
  2437. As another example, if you don't want the time-of-day of entries in
  2438. the prefix, you could use:
  2439. (setq org-agenda-prefix-format \" %-11:c% s\")
  2440. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2441. `org-agenda-remove-tags'."
  2442. :type '(choice
  2443. (string :tag "General format")
  2444. (list :greedy t :tag "View dependent"
  2445. (cons (const agenda) (string :tag "Format"))
  2446. (cons (const timeline) (string :tag "Format"))
  2447. (cons (const todo) (string :tag "Format"))
  2448. (cons (const tags) (string :tag "Format"))))
  2449. :group 'org-agenda-line-format)
  2450. (defvar org-prefix-format-compiled nil
  2451. "The compiled version of the most recently used prefix format.
  2452. See the variable `org-agenda-prefix-format'.")
  2453. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2454. "Text preceeding scheduled items in the agenda view.
  2455. THis is a list with two strings. The first applies when the item is
  2456. scheduled on the current day. The second applies when it has been scheduled
  2457. previously, it may contain a %d to capture how many days ago the item was
  2458. scheduled."
  2459. :group 'org-agenda-line-format
  2460. :type '(list
  2461. (string :tag "Scheduled today ")
  2462. (string :tag "Scheduled previously")))
  2463. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2464. "Text preceeding deadline items in the agenda view.
  2465. This is a list with two strings. The first applies when the item has its
  2466. deadline on the current day. The second applies when it is in the past or
  2467. in the future, it may contain %d to capture how many days away the deadline
  2468. is (was)."
  2469. :group 'org-agenda-line-format
  2470. :type '(list
  2471. (string :tag "Deadline today ")
  2472. (string :tag "Deadline relative")))
  2473. (defcustom org-agenda-remove-times-when-in-prefix t
  2474. "Non-nil means, remove duplicate time specifications in agenda items.
  2475. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2476. time-of-day specification in a headline or diary entry is extracted and
  2477. placed into the prefix. If this option is non-nil, the original specification
  2478. \(a timestamp or -range, or just a plain time(range) specification like
  2479. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2480. cluttered.
  2481. The option can be t or nil. It may also be the symbol `beg', indicating
  2482. that the time should only be removed what it is located at the beginning of
  2483. the headline/diary entry."
  2484. :group 'org-agenda-line-format
  2485. :type '(choice
  2486. (const :tag "Always" t)
  2487. (const :tag "Never" nil)
  2488. (const :tag "When at beginning of entry" beg)))
  2489. (defcustom org-agenda-default-appointment-duration nil
  2490. "Default duration for appointments that only have a starting time.
  2491. When nil, no duration is specified in such cases.
  2492. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2493. :group 'org-agenda-line-format
  2494. :type '(choice
  2495. (integer :tag "Minutes")
  2496. (const :tag "No default duration")))
  2497. (defcustom org-agenda-remove-tags nil
  2498. "Non-nil means, remove the tags from the headline copy in the agenda.
  2499. When this is the symbol `prefix', only remove tags when
  2500. `org-agenda-prefix-format' contains a `%T' specifier."
  2501. :group 'org-agenda-line-format
  2502. :type '(choice
  2503. (const :tag "Always" t)
  2504. (const :tag "Never" nil)
  2505. (const :tag "When prefix format contains %T" prefix)))
  2506. (if (fboundp 'defvaralias)
  2507. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2508. 'org-agenda-remove-tags))
  2509. (defcustom org-agenda-tags-column -80
  2510. "Shift tags in agenda items to this column.
  2511. If this number is positive, it specifies the column. If it is negative,
  2512. it means that the tags should be flushright to that column. For example,
  2513. -80 works well for a normal 80 character screen."
  2514. :group 'org-agenda-line-format
  2515. :type 'integer)
  2516. (if (fboundp 'defvaralias)
  2517. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2518. (defcustom org-agenda-fontify-priorities t
  2519. "Non-nil means, highlight low and high priorities in agenda.
  2520. When t, the highest priority entries are bold, lowest priority italic.
  2521. This may also be an association list of priority faces. The face may be
  2522. a names face, or a list like `(:background \"Red\")'."
  2523. :group 'org-agenda-line-format
  2524. :type '(choice
  2525. (const :tag "Never" nil)
  2526. (const :tag "Defaults" t)
  2527. (repeat :tag "Specify"
  2528. (list (character :tag "Priority" :value ?A)
  2529. (sexp :tag "face")))))
  2530. (defgroup org-latex nil
  2531. "Options for embedding LaTeX code into Org-mode"
  2532. :tag "Org LaTeX"
  2533. :group 'org)
  2534. (defcustom org-format-latex-options
  2535. '(:foreground default :background default :scale 1.0
  2536. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2537. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2538. "Options for creating images from LaTeX fragments.
  2539. This is a property list with the following properties:
  2540. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2541. `default' means use the forground of the default face.
  2542. :background the background color, or \"Transparent\".
  2543. `default' means use the background of the default face.
  2544. :scale a scaling factor for the size of the images
  2545. :html-foreground, :html-background, :html-scale
  2546. The same numbers for HTML export.
  2547. :matchers a list indicating which matchers should be used to
  2548. find LaTeX fragments. Valid members of this list are:
  2549. \"begin\" find environments
  2550. \"$\" find math expressions surrounded by $...$
  2551. \"$$\" find math expressions surrounded by $$....$$
  2552. \"\\(\" find math expressions surrounded by \\(...\\)
  2553. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2554. :group 'org-latex
  2555. :type 'plist)
  2556. (defcustom org-format-latex-header "\\documentclass{article}
  2557. \\usepackage{fullpage} % do not remove
  2558. \\usepackage{amssymb}
  2559. \\usepackage[usenames]{color}
  2560. \\usepackage{amsmath}
  2561. \\usepackage{latexsym}
  2562. \\usepackage[mathscr]{eucal}
  2563. \\pagestyle{empty} % do not remove"
  2564. "The document header used for processing LaTeX fragments."
  2565. :group 'org-latex
  2566. :type 'string)
  2567. (defgroup org-export nil
  2568. "Options for exporting org-listings."
  2569. :tag "Org Export"
  2570. :group 'org)
  2571. (defgroup org-export-general nil
  2572. "General options for exporting Org-mode files."
  2573. :tag "Org Export General"
  2574. :group 'org-export)
  2575. ;; FIXME
  2576. (defvar org-export-publishing-directory nil)
  2577. (defcustom org-export-with-special-strings t
  2578. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2579. When this option is turned on, these strings will be exported as:
  2580. Org HTML LaTeX
  2581. -----+----------+--------
  2582. \\- &shy; \\-
  2583. -- &ndash; --
  2584. --- &mdash; ---
  2585. ... &hellip; \ldots
  2586. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2587. :group 'org-export-translation
  2588. :type 'boolean)
  2589. (defcustom org-export-language-setup
  2590. '(("en" "Author" "Date" "Table of Contents")
  2591. ("cs" "Autor" "Datum" "Obsah")
  2592. ("da" "Ophavsmand" "Dato" "Indhold")
  2593. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2594. ("es" "Autor" "Fecha" "\xcdndice")
  2595. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2596. ("it" "Autore" "Data" "Indice")
  2597. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2598. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2599. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2600. "Terms used in export text, translated to different languages.
  2601. Use the variable `org-export-default-language' to set the language,
  2602. or use the +OPTION lines for a per-file setting."
  2603. :group 'org-export-general
  2604. :type '(repeat
  2605. (list
  2606. (string :tag "HTML language tag")
  2607. (string :tag "Author")
  2608. (string :tag "Date")
  2609. (string :tag "Table of Contents"))))
  2610. (defcustom org-export-default-language "en"
  2611. "The default language of HTML export, as a string.
  2612. This should have an association in `org-export-language-setup'."
  2613. :group 'org-export-general
  2614. :type 'string)
  2615. (defcustom org-export-skip-text-before-1st-heading t
  2616. "Non-nil means, skip all text before the first headline when exporting.
  2617. When nil, that text is exported as well."
  2618. :group 'org-export-general
  2619. :type 'boolean)
  2620. (defcustom org-export-headline-levels 3
  2621. "The last level which is still exported as a headline.
  2622. Inferior levels will produce itemize lists when exported.
  2623. Note that a numeric prefix argument to an exporter function overrides
  2624. this setting.
  2625. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2626. :group 'org-export-general
  2627. :type 'number)
  2628. (defcustom org-export-with-section-numbers t
  2629. "Non-nil means, add section numbers to headlines when exporting.
  2630. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2631. :group 'org-export-general
  2632. :type 'boolean)
  2633. (defcustom org-export-with-toc t
  2634. "Non-nil means, create a table of contents in exported files.
  2635. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2636. When an integer, include levels up to N in the toc, this may then be
  2637. different from `org-export-headline-levels', but it will not be allowed
  2638. to be larger than the number of headline levels.
  2639. When nil, no table of contents is made.
  2640. Headlines which contain any TODO items will be marked with \"(*)\" in
  2641. ASCII export, and with red color in HTML output, if the option
  2642. `org-export-mark-todo-in-toc' is set.
  2643. In HTML output, the TOC will be clickable.
  2644. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2645. or \"toc:3\"."
  2646. :group 'org-export-general
  2647. :type '(choice
  2648. (const :tag "No Table of Contents" nil)
  2649. (const :tag "Full Table of Contents" t)
  2650. (integer :tag "TOC to level")))
  2651. (defcustom org-export-mark-todo-in-toc nil
  2652. "Non-nil means, mark TOC lines that contain any open TODO items."
  2653. :group 'org-export-general
  2654. :type 'boolean)
  2655. (defcustom org-export-preserve-breaks nil
  2656. "Non-nil means, preserve all line breaks when exporting.
  2657. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2658. export, line breaks will always be preserved, regardless of this variable.
  2659. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2660. :group 'org-export-general
  2661. :type 'boolean)
  2662. (defcustom org-export-with-archived-trees 'headline
  2663. "Whether subtrees with the ARCHIVE tag should be exported.
  2664. This can have three different values
  2665. nil Do not export, pretend this tree is not present
  2666. t Do export the entire tree
  2667. headline Only export the headline, but skip the tree below it."
  2668. :group 'org-export-general
  2669. :group 'org-archive
  2670. :type '(choice
  2671. (const :tag "not at all" nil)
  2672. (const :tag "headline only" 'headline)
  2673. (const :tag "entirely" t)))
  2674. (defcustom org-export-author-info t
  2675. "Non-nil means, insert author name and email into the exported file.
  2676. This option can also be set with the +OPTIONS line,
  2677. e.g. \"author-info:nil\"."
  2678. :group 'org-export-general
  2679. :type 'boolean)
  2680. (defcustom org-export-time-stamp-file t
  2681. "Non-nil means, insert a time stamp into the exported file.
  2682. The time stamp shows when the file was created.
  2683. This option can also be set with the +OPTIONS line,
  2684. e.g. \"timestamp:nil\"."
  2685. :group 'org-export-general
  2686. :type 'boolean)
  2687. (defcustom org-export-with-timestamps t
  2688. "If nil, do not export time stamps and associated keywords."
  2689. :group 'org-export-general
  2690. :type 'boolean)
  2691. (defcustom org-export-remove-timestamps-from-toc t
  2692. "If nil, remove timestamps from the table of contents entries."
  2693. :group 'org-export-general
  2694. :type 'boolean)
  2695. (defcustom org-export-with-tags 'not-in-toc
  2696. "If nil, do not export tags, just remove them from headlines.
  2697. If this is the symbol `not-in-toc', tags will be removed from table of
  2698. contents entries, but still be shown in the headlines of the document.
  2699. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2700. :group 'org-export-general
  2701. :type '(choice
  2702. (const :tag "Off" nil)
  2703. (const :tag "Not in TOC" not-in-toc)
  2704. (const :tag "On" t)))
  2705. (defcustom org-export-with-drawers nil
  2706. "Non-nil means, export with drawers like the property drawer.
  2707. When t, all drawers are exported. This may also be a list of
  2708. drawer names to export."
  2709. :group 'org-export-general
  2710. :type '(choice
  2711. (const :tag "All drawers" t)
  2712. (const :tag "None" nil)
  2713. (repeat :tag "Selected drawers"
  2714. (string :tag "Drawer name"))))
  2715. (defgroup org-export-translation nil
  2716. "Options for translating special ascii sequences for the export backends."
  2717. :tag "Org Export Translation"
  2718. :group 'org-export)
  2719. (defcustom org-export-with-emphasize t
  2720. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2721. If the export target supports emphasizing text, the word will be
  2722. typeset in bold, italic, or underlined, respectively. Works only for
  2723. single words, but you can say: I *really* *mean* *this*.
  2724. Not all export backends support this.
  2725. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2726. :group 'org-export-translation
  2727. :type 'boolean)
  2728. (defcustom org-export-with-footnotes t
  2729. "If nil, export [1] as a footnote marker.
  2730. Lines starting with [1] will be formatted as footnotes.
  2731. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2732. :group 'org-export-translation
  2733. :type 'boolean)
  2734. (defcustom org-export-with-sub-superscripts t
  2735. "Non-nil means, interpret \"_\" and \"^\" for export.
  2736. When this option is turned on, you can use TeX-like syntax for sub- and
  2737. superscripts. Several characters after \"_\" or \"^\" will be
  2738. considered as a single item - so grouping with {} is normally not
  2739. needed. For example, the following things will be parsed as single
  2740. sub- or superscripts.
  2741. 10^24 or 10^tau several digits will be considered 1 item.
  2742. 10^-12 or 10^-tau a leading sign with digits or a word
  2743. x^2-y^3 will be read as x^2 - y^3, because items are
  2744. terminated by almost any nonword/nondigit char.
  2745. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2746. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2747. sub/superscript. If you set this variable to the symbol `{}',
  2748. the braces are *required* in order to trigger interpretations as
  2749. sub/superscript. This can be helpful in documents that need \"_\"
  2750. frequently in plain text.
  2751. Not all export backends support this, but HTML does.
  2752. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2753. :group 'org-export-translation
  2754. :type '(choice
  2755. (const :tag "Always interpret" t)
  2756. (const :tag "Only with braces" {})
  2757. (const :tag "Never interpret" nil)))
  2758. (defcustom org-export-with-special-strings t
  2759. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2760. When this option is turned on, these strings will be exported as:
  2761. \\- : &shy;
  2762. -- : &ndash;
  2763. --- : &mdash;
  2764. Not all export backends support this, but HTML does.
  2765. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2766. :group 'org-export-translation
  2767. :type 'boolean)
  2768. (defcustom org-export-with-TeX-macros t
  2769. "Non-nil means, interpret simple TeX-like macros when exporting.
  2770. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2771. No only real TeX macros will work here, but the standard HTML entities
  2772. for math can be used as macro names as well. For a list of supported
  2773. names in HTML export, see the constant `org-html-entities'.
  2774. Not all export backends support this.
  2775. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2776. :group 'org-export-translation
  2777. :group 'org-export-latex
  2778. :type 'boolean)
  2779. (defcustom org-export-with-LaTeX-fragments nil
  2780. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2781. When set, the exporter will find LaTeX environments if the \\begin line is
  2782. the first non-white thing on a line. It will also find the math delimiters
  2783. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2784. display math.
  2785. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2786. :group 'org-export-translation
  2787. :group 'org-export-latex
  2788. :type 'boolean)
  2789. (defcustom org-export-with-fixed-width t
  2790. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2791. This can be used to have pre-formatted text, fragments of code etc. For
  2792. example:
  2793. : ;; Some Lisp examples
  2794. : (while (defc cnt)
  2795. : (ding))
  2796. will be looking just like this in also HTML. See also the QUOTE keyword.
  2797. Not all export backends support this.
  2798. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2799. :group 'org-export-translation
  2800. :type 'boolean)
  2801. (defcustom org-match-sexp-depth 3
  2802. "Number of stacked braces for sub/superscript matching.
  2803. This has to be set before loading org.el to be effective."
  2804. :group 'org-export-translation
  2805. :type 'integer)
  2806. (defgroup org-export-tables nil
  2807. "Options for exporting tables in Org-mode."
  2808. :tag "Org Export Tables"
  2809. :group 'org-export)
  2810. (defcustom org-export-with-tables t
  2811. "If non-nil, lines starting with \"|\" define a table.
  2812. For example:
  2813. | Name | Address | Birthday |
  2814. |-------------+----------+-----------|
  2815. | Arthur Dent | England | 29.2.2100 |
  2816. Not all export backends support this.
  2817. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2818. :group 'org-export-tables
  2819. :type 'boolean)
  2820. (defcustom org-export-highlight-first-table-line t
  2821. "Non-nil means, highlight the first table line.
  2822. In HTML export, this means use <th> instead of <td>.
  2823. In tables created with table.el, this applies to the first table line.
  2824. In Org-mode tables, all lines before the first horizontal separator
  2825. line will be formatted with <th> tags."
  2826. :group 'org-export-tables
  2827. :type 'boolean)
  2828. (defcustom org-export-table-remove-special-lines t
  2829. "Remove special lines and marking characters in calculating tables.
  2830. This removes the special marking character column from tables that are set
  2831. up for spreadsheet calculations. It also removes the entire lines
  2832. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2833. the values of constants may be useful to have."
  2834. :group 'org-export-tables
  2835. :type 'boolean)
  2836. (defcustom org-export-prefer-native-exporter-for-tables nil
  2837. "Non-nil means, always export tables created with table.el natively.
  2838. Natively means, use the HTML code generator in table.el.
  2839. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2840. the table does not use row- or column-spanning). This has the
  2841. advantage, that the automatic HTML conversions for math symbols and
  2842. sub/superscripts can be applied. Org-mode's HTML generator is also
  2843. much faster."
  2844. :group 'org-export-tables
  2845. :type 'boolean)
  2846. (defgroup org-export-ascii nil
  2847. "Options specific for ASCII export of Org-mode files."
  2848. :tag "Org Export ASCII"
  2849. :group 'org-export)
  2850. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2851. "Characters for underlining headings in ASCII export.
  2852. In the given sequence, these characters will be used for level 1, 2, ..."
  2853. :group 'org-export-ascii
  2854. :type '(repeat character))
  2855. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2856. "Bullet characters for headlines converted to lists in ASCII export.
  2857. The first character is is used for the first lest level generated in this
  2858. way, and so on. If there are more levels than characters given here,
  2859. the list will be repeated.
  2860. Note that plain lists will keep the same bullets as the have in the
  2861. Org-mode file."
  2862. :group 'org-export-ascii
  2863. :type '(repeat character))
  2864. (defgroup org-export-xml nil
  2865. "Options specific for XML export of Org-mode files."
  2866. :tag "Org Export XML"
  2867. :group 'org-export)
  2868. (defgroup org-export-html nil
  2869. "Options specific for HTML export of Org-mode files."
  2870. :tag "Org Export HTML"
  2871. :group 'org-export)
  2872. (defcustom org-export-html-coding-system nil
  2873. ""
  2874. :group 'org-export-html
  2875. :type 'coding-system)
  2876. (defcustom org-export-html-extension "html"
  2877. "The extension for exported HTML files."
  2878. :group 'org-export-html
  2879. :type 'string)
  2880. (defcustom org-export-html-style
  2881. "<style type=\"text/css\">
  2882. html {
  2883. font-family: Times, serif;
  2884. font-size: 12pt;
  2885. }
  2886. .title { text-align: center; }
  2887. .todo { color: red; }
  2888. .done { color: green; }
  2889. .timestamp { color: grey }
  2890. .timestamp-kwd { color: CadetBlue }
  2891. .tag { background-color:lightblue; font-weight:normal }
  2892. .target { background-color: lavender; }
  2893. pre {
  2894. border: 1pt solid #AEBDCC;
  2895. background-color: #F3F5F7;
  2896. padding: 5pt;
  2897. font-family: courier, monospace;
  2898. }
  2899. table { border-collapse: collapse; }
  2900. td, th {
  2901. vertical-align: top;
  2902. <!--border: 1pt solid #ADB9CC;-->
  2903. }
  2904. </style>"
  2905. "The default style specification for exported HTML files.
  2906. Since there are different ways of setting style information, this variable
  2907. needs to contain the full HTML structure to provide a style, including the
  2908. surrounding HTML tags. The style specifications should include definitions
  2909. for new classes todo, done, title, and deadline. For example, legal values
  2910. would be:
  2911. <style type=\"text/css\">
  2912. p { font-weight: normal; color: gray; }
  2913. h1 { color: black; }
  2914. .title { text-align: center; }
  2915. .todo, .deadline { color: red; }
  2916. .done { color: green; }
  2917. </style>
  2918. or, if you want to keep the style in a file,
  2919. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2920. As the value of this option simply gets inserted into the HTML <head> header,
  2921. you can \"misuse\" it to add arbitrary text to the header."
  2922. :group 'org-export-html
  2923. :type 'string)
  2924. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2925. "Format for typesetting the document title in HTML export."
  2926. :group 'org-export-html
  2927. :type 'string)
  2928. (defcustom org-export-html-toplevel-hlevel 2
  2929. "The <H> level for level 1 headings in HTML export."
  2930. :group 'org-export-html
  2931. :type 'string)
  2932. (defcustom org-export-html-link-org-files-as-html t
  2933. "Non-nil means, make file links to `file.org' point to `file.html'.
  2934. When org-mode is exporting an org-mode file to HTML, links to
  2935. non-html files are directly put into a href tag in HTML.
  2936. However, links to other Org-mode files (recognized by the
  2937. extension `.org.) should become links to the corresponding html
  2938. file, assuming that the linked org-mode file will also be
  2939. converted to HTML.
  2940. When nil, the links still point to the plain `.org' file."
  2941. :group 'org-export-html
  2942. :type 'boolean)
  2943. (defcustom org-export-html-inline-images 'maybe
  2944. "Non-nil means, inline images into exported HTML pages.
  2945. This is done using an <img> tag. When nil, an anchor with href is used to
  2946. link to the image. If this option is `maybe', then images in links with
  2947. an empty description will be inlined, while images with a description will
  2948. be linked only."
  2949. :group 'org-export-html
  2950. :type '(choice (const :tag "Never" nil)
  2951. (const :tag "Always" t)
  2952. (const :tag "When there is no description" maybe)))
  2953. ;; FIXME: rename
  2954. (defcustom org-export-html-expand t
  2955. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2956. When nil, these tags will be exported as plain text and therefore
  2957. not be interpreted by a browser.
  2958. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2959. :group 'org-export-html
  2960. :type 'boolean)
  2961. (defcustom org-export-html-table-tag
  2962. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2963. "The HTML tag that is used to start a table.
  2964. This must be a <table> tag, but you may change the options like
  2965. borders and spacing."
  2966. :group 'org-export-html
  2967. :type 'string)
  2968. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2969. "The opening tag for table header fields.
  2970. This is customizable so that alignment options can be specified."
  2971. :group 'org-export-tables
  2972. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2973. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2974. "The opening tag for table data fields.
  2975. This is customizable so that alignment options can be specified."
  2976. :group 'org-export-tables
  2977. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2978. (defcustom org-export-html-with-timestamp nil
  2979. "If non-nil, write `org-export-html-html-helper-timestamp'
  2980. into the exported HTML text. Otherwise, the buffer will just be saved
  2981. to a file."
  2982. :group 'org-export-html
  2983. :type 'boolean)
  2984. (defcustom org-export-html-html-helper-timestamp
  2985. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2986. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2987. :group 'org-export-html
  2988. :type 'string)
  2989. (defgroup org-export-icalendar nil
  2990. "Options specific for iCalendar export of Org-mode files."
  2991. :tag "Org Export iCalendar"
  2992. :group 'org-export)
  2993. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2994. "The file name for the iCalendar file covering all agenda files.
  2995. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2996. The file name should be absolute, the file will be overwritten without warning."
  2997. :group 'org-export-icalendar
  2998. :type 'file)
  2999. (defcustom org-icalendar-include-todo nil
  3000. "Non-nil means, export to iCalendar files should also cover TODO items."
  3001. :group 'org-export-icalendar
  3002. :type '(choice
  3003. (const :tag "None" nil)
  3004. (const :tag "Unfinished" t)
  3005. (const :tag "All" all)))
  3006. (defcustom org-icalendar-include-sexps t
  3007. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3008. These are entries like in the diary, but directly in an Org-mode file."
  3009. :group 'org-export-icalendar
  3010. :type 'boolean)
  3011. (defcustom org-icalendar-include-body 100
  3012. "Amount of text below headline to be included in iCalendar export.
  3013. This is a number of characters that should maximally be included.
  3014. Properties, scheduling and clocking lines will always be removed.
  3015. The text will be inserted into the DESCRIPTION field."
  3016. :group 'org-export-icalendar
  3017. :type '(choice
  3018. (const :tag "Nothing" nil)
  3019. (const :tag "Everything" t)
  3020. (integer :tag "Max characters")))
  3021. (defcustom org-icalendar-combined-name "OrgMode"
  3022. "Calendar name for the combined iCalendar representing all agenda files."
  3023. :group 'org-export-icalendar
  3024. :type 'string)
  3025. (defgroup org-font-lock nil
  3026. "Font-lock settings for highlighting in Org-mode."
  3027. :tag "Org Font Lock"
  3028. :group 'org)
  3029. (defcustom org-level-color-stars-only nil
  3030. "Non-nil means fontify only the stars in each headline.
  3031. When nil, the entire headline is fontified.
  3032. Changing it requires restart of `font-lock-mode' to become effective
  3033. also in regions already fontified."
  3034. :group 'org-font-lock
  3035. :type 'boolean)
  3036. (defcustom org-hide-leading-stars nil
  3037. "Non-nil means, hide the first N-1 stars in a headline.
  3038. This works by using the face `org-hide' for these stars. This
  3039. face is white for a light background, and black for a dark
  3040. background. You may have to customize the face `org-hide' to
  3041. make this work.
  3042. Changing it requires restart of `font-lock-mode' to become effective
  3043. also in regions already fontified.
  3044. You may also set this on a per-file basis by adding one of the following
  3045. lines to the buffer:
  3046. #+STARTUP: hidestars
  3047. #+STARTUP: showstars"
  3048. :group 'org-font-lock
  3049. :type 'boolean)
  3050. (defcustom org-fontify-done-headline nil
  3051. "Non-nil means, change the face of a headline if it is marked DONE.
  3052. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3053. When this is non-nil, the headline after the keyword is set to the
  3054. `org-headline-done' as an additional indication."
  3055. :group 'org-font-lock
  3056. :type 'boolean)
  3057. (defcustom org-fontify-emphasized-text t
  3058. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3059. Changing this variable requires a restart of Emacs to take effect."
  3060. :group 'org-font-lock
  3061. :type 'boolean)
  3062. (defcustom org-highlight-latex-fragments-and-specials nil
  3063. "Non-nil means, fontify what is treated specially by the exporters."
  3064. :group 'org-font-lock
  3065. :type 'boolean)
  3066. (defcustom org-hide-emphasis-markers nil
  3067. "Non-nil mean font-lock should hide the emphasis marker characters."
  3068. :group 'org-font-lock
  3069. :type 'boolean)
  3070. (defvar org-emph-re nil
  3071. "Regular expression for matching emphasis.")
  3072. (defvar org-verbatim-re nil
  3073. "Regular expression for matching verbatim text.")
  3074. (defvar org-emphasis-regexp-components) ; defined just below
  3075. (defvar org-emphasis-alist) ; defined just below
  3076. (defun org-set-emph-re (var val)
  3077. "Set variable and compute the emphasis regular expression."
  3078. (set var val)
  3079. (when (and (boundp 'org-emphasis-alist)
  3080. (boundp 'org-emphasis-regexp-components)
  3081. org-emphasis-alist org-emphasis-regexp-components)
  3082. (let* ((e org-emphasis-regexp-components)
  3083. (pre (car e))
  3084. (post (nth 1 e))
  3085. (border (nth 2 e))
  3086. (body (nth 3 e))
  3087. (nl (nth 4 e))
  3088. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3089. (body1 (concat body "*?"))
  3090. (markers (mapconcat 'car org-emphasis-alist ""))
  3091. (vmarkers (mapconcat
  3092. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3093. org-emphasis-alist "")))
  3094. ;; make sure special characters appear at the right position in the class
  3095. (if (string-match "\\^" markers)
  3096. (setq markers (concat (replace-match "" t t markers) "^")))
  3097. (if (string-match "-" markers)
  3098. (setq markers (concat (replace-match "" t t markers) "-")))
  3099. (if (string-match "\\^" vmarkers)
  3100. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3101. (if (string-match "-" vmarkers)
  3102. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3103. (if (> nl 0)
  3104. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3105. (int-to-string nl) "\\}")))
  3106. ;; Make the regexp
  3107. (setq org-emph-re
  3108. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3109. "\\("
  3110. "\\([" markers "]\\)"
  3111. "\\("
  3112. "[^" border "]\\|"
  3113. "[^" border (if (and nil stacked) markers) "]"
  3114. body1
  3115. "[^" border (if (and nil stacked) markers) "]"
  3116. "\\)"
  3117. "\\3\\)"
  3118. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3119. (setq org-verbatim-re
  3120. (concat "\\([" pre "]\\|^\\)"
  3121. "\\("
  3122. "\\([" vmarkers "]\\)"
  3123. "\\("
  3124. "[^" border "]\\|"
  3125. "[^" border "]"
  3126. body1
  3127. "[^" border "]"
  3128. "\\)"
  3129. "\\3\\)"
  3130. "\\([" post "]\\|$\\)")))))
  3131. (defcustom org-emphasis-regexp-components
  3132. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3133. "Components used to build the regular expression for emphasis.
  3134. This is a list with 6 entries. Terminology: In an emphasis string
  3135. like \" *strong word* \", we call the initial space PREMATCH, the final
  3136. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3137. and \"trong wor\" is the body. The different components in this variable
  3138. specify what is allowed/forbidden in each part:
  3139. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3140. post Chars allowed as postmatch. End of line will be allowed too.
  3141. border The chars *forbidden* as border characters.
  3142. body-regexp A regexp like \".\" to match a body character. Don't use
  3143. non-shy groups here, and don't allow newline here.
  3144. newline The maximum number of newlines allowed in an emphasis exp.
  3145. Use customize to modify this, or restart Emacs after changing it."
  3146. :group 'org-font-lock
  3147. :set 'org-set-emph-re
  3148. :type '(list
  3149. (sexp :tag "Allowed chars in pre ")
  3150. (sexp :tag "Allowed chars in post ")
  3151. (sexp :tag "Forbidden chars in border ")
  3152. (sexp :tag "Regexp for body ")
  3153. (integer :tag "number of newlines allowed")
  3154. (option (boolean :tag "Stacking (DISABLED) "))))
  3155. (defcustom org-emphasis-alist
  3156. '(("*" bold "<b>" "</b>")
  3157. ("/" italic "<i>" "</i>")
  3158. ("_" underline "<u>" "</u>")
  3159. ("=" org-code "<code>" "</code>" verbatim)
  3160. ("~" org-verbatim "" "" verbatim)
  3161. ("+" (:strike-through t) "<del>" "</del>")
  3162. )
  3163. "Special syntax for emphasized text.
  3164. Text starting and ending with a special character will be emphasized, for
  3165. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3166. characters, the face to be used by font-lock for highlighting in Org-mode
  3167. Emacs buffers, and the HTML tags to be used for this.
  3168. Use customize to modify this, or restart Emacs after changing it."
  3169. :group 'org-font-lock
  3170. :set 'org-set-emph-re
  3171. :type '(repeat
  3172. (list
  3173. (string :tag "Marker character")
  3174. (choice
  3175. (face :tag "Font-lock-face")
  3176. (plist :tag "Face property list"))
  3177. (string :tag "HTML start tag")
  3178. (string :tag "HTML end tag")
  3179. (option (const verbatim)))))
  3180. ;;; The faces
  3181. (defgroup org-faces nil
  3182. "Faces in Org-mode."
  3183. :tag "Org Faces"
  3184. :group 'org-font-lock)
  3185. (defun org-compatible-face (inherits specs)
  3186. "Make a compatible face specification.
  3187. If INHERITS is an existing face and if the Emacs version supports it,
  3188. just inherit the face. If not, use SPECS to define the face.
  3189. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3190. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3191. to the top of the list. The `min-colors' attribute will be removed from
  3192. any other entries, and any resulting duplicates will be removed entirely."
  3193. (cond
  3194. ((and inherits (facep inherits)
  3195. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3196. ;; In Emacs 23, we use inheritance where possible.
  3197. ;; We only do this in Emacs 23, because only there the outline
  3198. ;; faces have been changed to the original org-mode-level-faces.
  3199. (list (list t :inherit inherits)))
  3200. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3201. ;; These do not understand the `min-colors' attribute.
  3202. (let (r e a)
  3203. (while (setq e (pop specs))
  3204. (cond
  3205. ((memq (car e) '(t default)) (push e r))
  3206. ((setq a (member '(min-colors 8) (car e)))
  3207. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3208. (cdr e)))))
  3209. ((setq a (assq 'min-colors (car e)))
  3210. (setq e (cons (delq a (car e)) (cdr e)))
  3211. (or (assoc (car e) r) (push e r)))
  3212. (t (or (assoc (car e) r) (push e r)))))
  3213. (nreverse r)))
  3214. (t specs)))
  3215. (put 'org-compatible-face 'lisp-indent-function 1)
  3216. (defface org-hide
  3217. '((((background light)) (:foreground "white"))
  3218. (((background dark)) (:foreground "black")))
  3219. "Face used to hide leading stars in headlines.
  3220. The forground color of this face should be equal to the background
  3221. color of the frame."
  3222. :group 'org-faces)
  3223. (defface org-level-1 ;; font-lock-function-name-face
  3224. (org-compatible-face 'outline-1
  3225. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3226. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3227. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3228. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3229. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3230. (t (:bold t))))
  3231. "Face used for level 1 headlines."
  3232. :group 'org-faces)
  3233. (defface org-level-2 ;; font-lock-variable-name-face
  3234. (org-compatible-face 'outline-2
  3235. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3236. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3237. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3238. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3239. (t (:bold t))))
  3240. "Face used for level 2 headlines."
  3241. :group 'org-faces)
  3242. (defface org-level-3 ;; font-lock-keyword-face
  3243. (org-compatible-face 'outline-3
  3244. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3245. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3246. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3247. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3248. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3249. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3250. (t (:bold t))))
  3251. "Face used for level 3 headlines."
  3252. :group 'org-faces)
  3253. (defface org-level-4 ;; font-lock-comment-face
  3254. (org-compatible-face 'outline-4
  3255. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3256. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3257. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3258. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3259. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3260. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3261. (t (:bold t))))
  3262. "Face used for level 4 headlines."
  3263. :group 'org-faces)
  3264. (defface org-level-5 ;; font-lock-type-face
  3265. (org-compatible-face 'outline-5
  3266. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3267. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3268. (((class color) (min-colors 8)) (:foreground "green"))))
  3269. "Face used for level 5 headlines."
  3270. :group 'org-faces)
  3271. (defface org-level-6 ;; font-lock-constant-face
  3272. (org-compatible-face 'outline-6
  3273. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3274. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3275. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3276. "Face used for level 6 headlines."
  3277. :group 'org-faces)
  3278. (defface org-level-7 ;; font-lock-builtin-face
  3279. (org-compatible-face 'outline-7
  3280. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3281. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3282. (((class color) (min-colors 8)) (:foreground "blue"))))
  3283. "Face used for level 7 headlines."
  3284. :group 'org-faces)
  3285. (defface org-level-8 ;; font-lock-string-face
  3286. (org-compatible-face 'outline-8
  3287. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3288. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3289. (((class color) (min-colors 8)) (:foreground "green"))))
  3290. "Face used for level 8 headlines."
  3291. :group 'org-faces)
  3292. (defface org-special-keyword ;; font-lock-string-face
  3293. (org-compatible-face nil
  3294. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3295. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3296. (t (:italic t))))
  3297. "Face used for special keywords."
  3298. :group 'org-faces)
  3299. (defface org-drawer ;; font-lock-function-name-face
  3300. (org-compatible-face nil
  3301. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3302. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3303. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3304. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3305. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3306. (t (:bold t))))
  3307. "Face used for drawers."
  3308. :group 'org-faces)
  3309. (defface org-property-value nil
  3310. "Face used for the value of a property."
  3311. :group 'org-faces)
  3312. (defface org-column
  3313. (org-compatible-face nil
  3314. '((((class color) (min-colors 16) (background light))
  3315. (:background "grey90"))
  3316. (((class color) (min-colors 16) (background dark))
  3317. (:background "grey30"))
  3318. (((class color) (min-colors 8))
  3319. (:background "cyan" :foreground "black"))
  3320. (t (:inverse-video t))))
  3321. "Face for column display of entry properties."
  3322. :group 'org-faces)
  3323. (when (fboundp 'set-face-attribute)
  3324. ;; Make sure that a fixed-width face is used when we have a column table.
  3325. (set-face-attribute 'org-column nil
  3326. :height (face-attribute 'default :height)
  3327. :family (face-attribute 'default :family)))
  3328. (defface org-warning
  3329. (org-compatible-face 'font-lock-warning-face
  3330. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3331. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3332. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3333. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3334. (t (:bold t))))
  3335. "Face for deadlines and TODO keywords."
  3336. :group 'org-faces)
  3337. (defface org-archived ; similar to shadow
  3338. (org-compatible-face 'shadow
  3339. '((((class color grayscale) (min-colors 88) (background light))
  3340. (:foreground "grey50"))
  3341. (((class color grayscale) (min-colors 88) (background dark))
  3342. (:foreground "grey70"))
  3343. (((class color) (min-colors 8) (background light))
  3344. (:foreground "green"))
  3345. (((class color) (min-colors 8) (background dark))
  3346. (:foreground "yellow"))))
  3347. "Face for headline with the ARCHIVE tag."
  3348. :group 'org-faces)
  3349. (defface org-link
  3350. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3351. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3352. (t (:underline t)))
  3353. "Face for links."
  3354. :group 'org-faces)
  3355. (defface org-ellipsis
  3356. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3357. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3358. (t (:strike-through t)))
  3359. "Face for the ellipsis in folded text."
  3360. :group 'org-faces)
  3361. (defface org-target
  3362. '((((class color) (background light)) (:underline t))
  3363. (((class color) (background dark)) (:underline t))
  3364. (t (:underline t)))
  3365. "Face for links."
  3366. :group 'org-faces)
  3367. (defface org-date
  3368. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3369. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3370. (t (:underline t)))
  3371. "Face for links."
  3372. :group 'org-faces)
  3373. (defface org-sexp-date
  3374. '((((class color) (background light)) (:foreground "Purple"))
  3375. (((class color) (background dark)) (:foreground "Cyan"))
  3376. (t (:underline t)))
  3377. "Face for links."
  3378. :group 'org-faces)
  3379. (defface org-tag
  3380. '((t (:bold t)))
  3381. "Face for tags."
  3382. :group 'org-faces)
  3383. (defface org-todo ; font-lock-warning-face
  3384. (org-compatible-face nil
  3385. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3386. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3387. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3388. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3389. (t (:inverse-video t :bold t))))
  3390. "Face for TODO keywords."
  3391. :group 'org-faces)
  3392. (defface org-done ;; font-lock-type-face
  3393. (org-compatible-face nil
  3394. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3395. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3396. (((class color) (min-colors 8)) (:foreground "green"))
  3397. (t (:bold t))))
  3398. "Face used for todo keywords that indicate DONE items."
  3399. :group 'org-faces)
  3400. (defface org-headline-done ;; font-lock-string-face
  3401. (org-compatible-face nil
  3402. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3403. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3404. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3405. "Face used to indicate that a headline is DONE.
  3406. This face is only used if `org-fontify-done-headline' is set. If applies
  3407. to the part of the headline after the DONE keyword."
  3408. :group 'org-faces)
  3409. (defcustom org-todo-keyword-faces nil
  3410. "Faces for specific TODO keywords.
  3411. This is a list of cons cells, with TODO keywords in the car
  3412. and faces in the cdr. The face can be a symbol, or a property
  3413. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3414. :group 'org-faces
  3415. :group 'org-todo
  3416. :type '(repeat
  3417. (cons
  3418. (string :tag "keyword")
  3419. (sexp :tag "face"))))
  3420. (defface org-table ;; font-lock-function-name-face
  3421. (org-compatible-face nil
  3422. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3423. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3424. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3425. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3426. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3427. (((class color) (min-colors 8) (background dark)))))
  3428. "Face used for tables."
  3429. :group 'org-faces)
  3430. (defface org-formula
  3431. (org-compatible-face nil
  3432. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3433. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3434. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3435. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3436. (t (:bold t :italic t))))
  3437. "Face for formulas."
  3438. :group 'org-faces)
  3439. (defface org-code
  3440. (org-compatible-face nil
  3441. '((((class color grayscale) (min-colors 88) (background light))
  3442. (:foreground "grey50"))
  3443. (((class color grayscale) (min-colors 88) (background dark))
  3444. (:foreground "grey70"))
  3445. (((class color) (min-colors 8) (background light))
  3446. (:foreground "green"))
  3447. (((class color) (min-colors 8) (background dark))
  3448. (:foreground "yellow"))))
  3449. "Face for fixed-with text like code snippets."
  3450. :group 'org-faces
  3451. :version "22.1")
  3452. (defface org-verbatim
  3453. (org-compatible-face nil
  3454. '((((class color grayscale) (min-colors 88) (background light))
  3455. (:foreground "grey50" :underline t))
  3456. (((class color grayscale) (min-colors 88) (background dark))
  3457. (:foreground "grey70" :underline t))
  3458. (((class color) (min-colors 8) (background light))
  3459. (:foreground "green" :underline t))
  3460. (((class color) (min-colors 8) (background dark))
  3461. (:foreground "yellow" :underline t))))
  3462. "Face for fixed-with text like code snippets."
  3463. :group 'org-faces
  3464. :version "22.1")
  3465. (defface org-agenda-structure ;; font-lock-function-name-face
  3466. (org-compatible-face nil
  3467. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3468. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3469. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3470. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3471. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3472. (t (:bold t))))
  3473. "Face used in agenda for captions and dates."
  3474. :group 'org-faces)
  3475. (defface org-scheduled-today
  3476. (org-compatible-face nil
  3477. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3478. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3479. (((class color) (min-colors 8)) (:foreground "green"))
  3480. (t (:bold t :italic t))))
  3481. "Face for items scheduled for a certain day."
  3482. :group 'org-faces)
  3483. (defface org-scheduled-previously
  3484. (org-compatible-face nil
  3485. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3486. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3487. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3488. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3489. (t (:bold t))))
  3490. "Face for items scheduled previously, and not yet done."
  3491. :group 'org-faces)
  3492. (defface org-upcoming-deadline
  3493. (org-compatible-face nil
  3494. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3495. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3496. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3497. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3498. (t (:bold t))))
  3499. "Face for items scheduled previously, and not yet done."
  3500. :group 'org-faces)
  3501. (defcustom org-agenda-deadline-faces
  3502. '((1.0 . org-warning)
  3503. (0.5 . org-upcoming-deadline)
  3504. (0.0 . default))
  3505. "Faces for showing deadlines in the agenda.
  3506. This is a list of cons cells. The cdr of each cess is a face to be used,
  3507. and it can also just be a like like '(:foreground \"yellow\").
  3508. Each car is a fraction of the head-warning time that must have passed for
  3509. this the face in the cdr to be used for display. The numbers must be
  3510. given in descending order. The head-warning time is normally taken
  3511. from `org-deadline-warning-days', but can also be specified in the deadline
  3512. timestamp itself, like this:
  3513. DEADLINE: <2007-08-13 Mon -8d>
  3514. You may use d for days, w for weeks, m for months and y for years. Months
  3515. and years will only be treated in an approximate fashion (30.4 days for a
  3516. month and 365.24 days for a year)."
  3517. :group 'org-faces
  3518. :group 'org-agenda-daily/weekly
  3519. :type '(repeat
  3520. (cons
  3521. (number :tag "Fraction of head-warning time passed")
  3522. (sexp :tag "Face"))))
  3523. ;; FIXME: this is not a good face yet.
  3524. (defface org-agenda-restriction-lock
  3525. (org-compatible-face nil
  3526. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3527. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3528. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3529. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3530. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3531. (t (:inverse-video t))))
  3532. "Face for showing the agenda restriction lock."
  3533. :group 'org-faces)
  3534. (defface org-time-grid ;; font-lock-variable-name-face
  3535. (org-compatible-face nil
  3536. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3537. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3538. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3539. "Face used for time grids."
  3540. :group 'org-faces)
  3541. (defconst org-level-faces
  3542. '(org-level-1 org-level-2 org-level-3 org-level-4
  3543. org-level-5 org-level-6 org-level-7 org-level-8
  3544. ))
  3545. (defcustom org-n-level-faces (length org-level-faces)
  3546. "The number different faces to be used for headlines.
  3547. Org-mode defines 8 different headline faces, so this can be at most 8.
  3548. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3549. :type 'number
  3550. :group 'org-faces)
  3551. ;;; Functions and variables from ther packages
  3552. ;; Declared here to avoid compiler warnings
  3553. (eval-and-compile
  3554. (unless (fboundp 'declare-function)
  3555. (defmacro declare-function (fn file &optional arglist fileonly))))
  3556. ;; XEmacs only
  3557. (defvar outline-mode-menu-heading)
  3558. (defvar outline-mode-menu-show)
  3559. (defvar outline-mode-menu-hide)
  3560. (defvar zmacs-regions) ; XEmacs regions
  3561. ;; Emacs only
  3562. (defvar mark-active)
  3563. ;; Various packages
  3564. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3565. (declare-function add-to-diary-list "diary-lib"
  3566. (date string specifier &optional marker globcolor literal))
  3567. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3568. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3569. (declare-function Info-goto-node "info" (nodename &optional fork))
  3570. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3571. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3572. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3573. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3574. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3575. (declare-function bbdb-record-name "ext:bbdb" (record))
  3576. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3577. (declare-function bibtex-generate-autokey "bibtex" ())
  3578. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3579. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3580. (defvar calc-embedded-close-formula)
  3581. (defvar calc-embedded-open-formula)
  3582. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3583. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3584. (declare-function calendar-check-holidays "holidays" (date))
  3585. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3586. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3587. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3588. (declare-function calendar-forward-day "cal-move" (arg))
  3589. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3590. (declare-function calendar-goto-date "cal-move" (date))
  3591. (declare-function calendar-goto-today "cal-move" ())
  3592. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3593. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3594. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3595. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3596. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3597. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3598. (defvar calendar-mode-map)
  3599. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3600. (declare-function cdlatex-tab "ext:cdlatex" ())
  3601. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3602. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3603. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3604. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3605. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3606. (defvar font-lock-unfontify-region-function)
  3607. (declare-function gnus-article-show-summary "gnus-art" ())
  3608. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3609. (defvar gnus-other-frame-object)
  3610. (defvar gnus-group-name)
  3611. (defvar gnus-article-current)
  3612. (defvar Info-current-file)
  3613. (defvar Info-current-node)
  3614. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3615. (declare-function mh-find-path "mh-utils" ())
  3616. (declare-function mh-get-header-field "mh-utils" (field))
  3617. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3618. (declare-function mh-header-display "mh-show" ())
  3619. (declare-function mh-index-previous-folder "mh-search" ())
  3620. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3621. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3622. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3623. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3624. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3625. (declare-function mh-show-header-display "mh-show" t t)
  3626. (declare-function mh-show-msg "mh-show" (msg))
  3627. (declare-function mh-show-show "mh-show" t t)
  3628. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3629. (defvar mh-progs)
  3630. (defvar mh-current-folder)
  3631. (defvar mh-show-folder-buffer)
  3632. (defvar mh-index-folder)
  3633. (defvar mh-searcher)
  3634. (declare-function org-export-latex-cleaned-string "org-export-latex" (&optional commentsp))
  3635. (declare-function parse-time-string "parse-time" (string))
  3636. (declare-function remember "remember" (&optional initial))
  3637. (declare-function remember-buffer-desc "remember" ())
  3638. (defvar remember-save-after-remembering)
  3639. (defvar remember-data-file)
  3640. (defvar remember-register)
  3641. (defvar remember-buffer)
  3642. (defvar remember-handler-functions)
  3643. (defvar remember-annotation-functions)
  3644. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3645. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3646. (declare-function rmail-what-message "rmail" ())
  3647. (defvar texmathp-why)
  3648. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3649. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3650. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3651. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3652. (declare-function vm-isearch-update "ext:vm-search" ())
  3653. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3654. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3655. (declare-function vm-su-subject "ext:vm-summary" (m))
  3656. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3657. (defvar vm-message-pointer)
  3658. (defvar vm-folder-directory)
  3659. (defvar w3m-current-url)
  3660. (defvar w3m-current-title)
  3661. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3662. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3663. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3664. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3665. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3666. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3667. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3668. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3669. (defvar wl-summary-buffer-elmo-folder)
  3670. (defvar wl-summary-buffer-folder-name)
  3671. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3672. (defvar org-latex-regexps)
  3673. (defvar constants-unit-system)
  3674. ;;; Variables for pre-computed regular expressions, all buffer local
  3675. (defvar org-drawer-regexp nil
  3676. "Matches first line of a hidden block.")
  3677. (make-variable-buffer-local 'org-drawer-regexp)
  3678. (defvar org-todo-regexp nil
  3679. "Matches any of the TODO state keywords.")
  3680. (make-variable-buffer-local 'org-todo-regexp)
  3681. (defvar org-not-done-regexp nil
  3682. "Matches any of the TODO state keywords except the last one.")
  3683. (make-variable-buffer-local 'org-not-done-regexp)
  3684. (defvar org-todo-line-regexp nil
  3685. "Matches a headline and puts TODO state into group 2 if present.")
  3686. (make-variable-buffer-local 'org-todo-line-regexp)
  3687. (defvar org-complex-heading-regexp nil
  3688. "Matches a headline and puts everything into groups:
  3689. group 1: the stars
  3690. group 2: The todo keyword, maybe
  3691. group 3: Priority cookie
  3692. group 4: True headline
  3693. group 5: Tags")
  3694. (make-variable-buffer-local 'org-complex-heading-regexp)
  3695. (defvar org-todo-line-tags-regexp nil
  3696. "Matches a headline and puts TODO state into group 2 if present.
  3697. Also put tags into group 4 if tags are present.")
  3698. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3699. (defvar org-nl-done-regexp nil
  3700. "Matches newline followed by a headline with the DONE keyword.")
  3701. (make-variable-buffer-local 'org-nl-done-regexp)
  3702. (defvar org-looking-at-done-regexp nil
  3703. "Matches the DONE keyword a point.")
  3704. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3705. (defvar org-ds-keyword-length 12
  3706. "Maximum length of the Deadline and SCHEDULED keywords.")
  3707. (make-variable-buffer-local 'org-ds-keyword-length)
  3708. (defvar org-deadline-regexp nil
  3709. "Matches the DEADLINE keyword.")
  3710. (make-variable-buffer-local 'org-deadline-regexp)
  3711. (defvar org-deadline-time-regexp nil
  3712. "Matches the DEADLINE keyword together with a time stamp.")
  3713. (make-variable-buffer-local 'org-deadline-time-regexp)
  3714. (defvar org-deadline-line-regexp nil
  3715. "Matches the DEADLINE keyword and the rest of the line.")
  3716. (make-variable-buffer-local 'org-deadline-line-regexp)
  3717. (defvar org-scheduled-regexp nil
  3718. "Matches the SCHEDULED keyword.")
  3719. (make-variable-buffer-local 'org-scheduled-regexp)
  3720. (defvar org-scheduled-time-regexp nil
  3721. "Matches the SCHEDULED keyword together with a time stamp.")
  3722. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3723. (defvar org-closed-time-regexp nil
  3724. "Matches the CLOSED keyword together with a time stamp.")
  3725. (make-variable-buffer-local 'org-closed-time-regexp)
  3726. (defvar org-keyword-time-regexp nil
  3727. "Matches any of the 4 keywords, together with the time stamp.")
  3728. (make-variable-buffer-local 'org-keyword-time-regexp)
  3729. (defvar org-keyword-time-not-clock-regexp nil
  3730. "Matches any of the 3 keywords, together with the time stamp.")
  3731. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3732. (defvar org-maybe-keyword-time-regexp nil
  3733. "Matches a timestamp, possibly preceeded by a keyword.")
  3734. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3735. (defvar org-planning-or-clock-line-re nil
  3736. "Matches a line with planning or clock info.")
  3737. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3738. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3739. rear-nonsticky t mouse-map t fontified t)
  3740. "Properties to remove when a string without properties is wanted.")
  3741. (defsubst org-match-string-no-properties (num &optional string)
  3742. (if (featurep 'xemacs)
  3743. (let ((s (match-string num string)))
  3744. (remove-text-properties 0 (length s) org-rm-props s)
  3745. s)
  3746. (match-string-no-properties num string)))
  3747. (defsubst org-no-properties (s)
  3748. (if (fboundp 'set-text-properties)
  3749. (set-text-properties 0 (length s) nil s)
  3750. (remove-text-properties 0 (length s) org-rm-props s))
  3751. s)
  3752. (defsubst org-get-alist-option (option key)
  3753. (cond ((eq key t) t)
  3754. ((eq option t) t)
  3755. ((assoc key option) (cdr (assoc key option)))
  3756. (t (cdr (assq 'default option)))))
  3757. (defsubst org-inhibit-invisibility ()
  3758. "Modified `buffer-invisibility-spec' for Emacs 21.
  3759. Some ops with invisible text do not work correctly on Emacs 21. For these
  3760. we turn off invisibility temporarily. Use this in a `let' form."
  3761. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3762. (defsubst org-set-local (var value)
  3763. "Make VAR local in current buffer and set it to VALUE."
  3764. (set (make-variable-buffer-local var) value))
  3765. (defsubst org-mode-p ()
  3766. "Check if the current buffer is in Org-mode."
  3767. (eq major-mode 'org-mode))
  3768. (defsubst org-last (list)
  3769. "Return the last element of LIST."
  3770. (car (last list)))
  3771. (defun org-let (list &rest body)
  3772. (eval (cons 'let (cons list body))))
  3773. (put 'org-let 'lisp-indent-function 1)
  3774. (defun org-let2 (list1 list2 &rest body)
  3775. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3776. (put 'org-let2 'lisp-indent-function 2)
  3777. (defconst org-startup-options
  3778. '(("fold" org-startup-folded t)
  3779. ("overview" org-startup-folded t)
  3780. ("nofold" org-startup-folded nil)
  3781. ("showall" org-startup-folded nil)
  3782. ("content" org-startup-folded content)
  3783. ("hidestars" org-hide-leading-stars t)
  3784. ("showstars" org-hide-leading-stars nil)
  3785. ("odd" org-odd-levels-only t)
  3786. ("oddeven" org-odd-levels-only nil)
  3787. ("align" org-startup-align-all-tables t)
  3788. ("noalign" org-startup-align-all-tables nil)
  3789. ("customtime" org-display-custom-times t)
  3790. ("logging" org-log-done t)
  3791. ("logdone" org-log-done t)
  3792. ("nologging" org-log-done nil)
  3793. ("lognotedone" org-log-done done push)
  3794. ("lognotestate" org-log-done state push)
  3795. ("lognoteclock-out" org-log-done clock-out push)
  3796. ("logrepeat" org-log-repeat t)
  3797. ("nologrepeat" org-log-repeat nil)
  3798. ("constcgs" constants-unit-system cgs)
  3799. ("constSI" constants-unit-system SI))
  3800. "Variable associated with STARTUP options for org-mode.
  3801. Each element is a list of three items: The startup options as written
  3802. in the #+STARTUP line, the corresponding variable, and the value to
  3803. set this variable to if the option is found. An optional forth element PUSH
  3804. means to push this value onto the list in the variable.")
  3805. (defun org-set-regexps-and-options ()
  3806. "Precompute regular expressions for current buffer."
  3807. (when (org-mode-p)
  3808. (org-set-local 'org-todo-kwd-alist nil)
  3809. (org-set-local 'org-todo-key-alist nil)
  3810. (org-set-local 'org-todo-key-trigger nil)
  3811. (org-set-local 'org-todo-keywords-1 nil)
  3812. (org-set-local 'org-done-keywords nil)
  3813. (org-set-local 'org-todo-heads nil)
  3814. (org-set-local 'org-todo-sets nil)
  3815. (org-set-local 'org-todo-log-states nil)
  3816. (let ((re (org-make-options-regexp
  3817. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3818. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3819. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3820. (splitre "[ \t]+")
  3821. kwds kws0 kwsa key value cat arch tags const links hw dws
  3822. tail sep kws1 prio props drawers
  3823. ex log)
  3824. (save-excursion
  3825. (save-restriction
  3826. (widen)
  3827. (goto-char (point-min))
  3828. (while (re-search-forward re nil t)
  3829. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3830. (cond
  3831. ((equal key "CATEGORY")
  3832. (if (string-match "[ \t]+$" value)
  3833. (setq value (replace-match "" t t value)))
  3834. (setq cat value))
  3835. ((member key '("SEQ_TODO" "TODO"))
  3836. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3837. ((equal key "TYP_TODO")
  3838. (push (cons 'type (org-split-string value splitre)) kwds))
  3839. ((equal key "TAGS")
  3840. (setq tags (append tags (org-split-string value splitre))))
  3841. ((equal key "COLUMNS")
  3842. (org-set-local 'org-columns-default-format value))
  3843. ((equal key "LINK")
  3844. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3845. (push (cons (match-string 1 value)
  3846. (org-trim (match-string 2 value)))
  3847. links)))
  3848. ((equal key "PRIORITIES")
  3849. (setq prio (org-split-string value " +")))
  3850. ((equal key "PROPERTY")
  3851. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3852. (push (cons (match-string 1 value) (match-string 2 value))
  3853. props)))
  3854. ((equal key "DRAWERS")
  3855. (setq drawers (org-split-string value splitre)))
  3856. ((equal key "CONSTANTS")
  3857. (setq const (append const (org-split-string value splitre))))
  3858. ((equal key "STARTUP")
  3859. (let ((opts (org-split-string value splitre))
  3860. l var val)
  3861. (while (setq l (pop opts))
  3862. (when (setq l (assoc l org-startup-options))
  3863. (setq var (nth 1 l) val (nth 2 l))
  3864. (if (not (nth 3 l))
  3865. (set (make-local-variable var) val)
  3866. (if (not (listp (symbol-value var)))
  3867. (set (make-local-variable var) nil))
  3868. (set (make-local-variable var) (symbol-value var))
  3869. (add-to-list var val))))))
  3870. ((equal key "ARCHIVE")
  3871. (string-match " *$" value)
  3872. (setq arch (replace-match "" t t value))
  3873. (remove-text-properties 0 (length arch)
  3874. '(face t fontified t) arch)))
  3875. )))
  3876. (when cat
  3877. (org-set-local 'org-category (intern cat))
  3878. (push (cons "CATEGORY" cat) props))
  3879. (when prio
  3880. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3881. (setq prio (mapcar 'string-to-char prio))
  3882. (org-set-local 'org-highest-priority (nth 0 prio))
  3883. (org-set-local 'org-lowest-priority (nth 1 prio))
  3884. (org-set-local 'org-default-priority (nth 2 prio)))
  3885. (and props (org-set-local 'org-local-properties (nreverse props)))
  3886. (and drawers (org-set-local 'org-drawers drawers))
  3887. (and arch (org-set-local 'org-archive-location arch))
  3888. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3889. ;; Process the TODO keywords
  3890. (unless kwds
  3891. ;; Use the global values as if they had been given locally.
  3892. (setq kwds (default-value 'org-todo-keywords))
  3893. (if (stringp (car kwds))
  3894. (setq kwds (list (cons org-todo-interpretation
  3895. (default-value 'org-todo-keywords)))))
  3896. (setq kwds (reverse kwds)))
  3897. (setq kwds (nreverse kwds))
  3898. (let (inter kws kw)
  3899. (while (setq kws (pop kwds))
  3900. (setq inter (pop kws) sep (member "|" kws)
  3901. kws0 (delete "|" (copy-sequence kws))
  3902. kwsa nil
  3903. kws1 (mapcar
  3904. (lambda (x)
  3905. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3906. (progn
  3907. (setq kw (match-string 1 x)
  3908. ex (and (match-end 2) (match-string 2 x))
  3909. log (and ex (string-match "@" ex))
  3910. key (and ex (substring ex 0 1)))
  3911. (if (equal key "@") (setq key nil))
  3912. (push (cons kw (and key (string-to-char key))) kwsa)
  3913. (and log (push kw org-todo-log-states))
  3914. kw)
  3915. (error "Invalid TODO keyword %s" x)))
  3916. kws0)
  3917. kwsa (if kwsa (append '((:startgroup))
  3918. (nreverse kwsa)
  3919. '((:endgroup))))
  3920. hw (car kws1)
  3921. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3922. tail (list inter hw (car dws) (org-last dws)))
  3923. (add-to-list 'org-todo-heads hw 'append)
  3924. (push kws1 org-todo-sets)
  3925. (setq org-done-keywords (append org-done-keywords dws nil))
  3926. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3927. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3928. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3929. (setq org-todo-sets (nreverse org-todo-sets)
  3930. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3931. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3932. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3933. ;; Process the constants
  3934. (when const
  3935. (let (e cst)
  3936. (while (setq e (pop const))
  3937. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3938. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3939. (setq org-table-formula-constants-local cst)))
  3940. ;; Process the tags.
  3941. (when tags
  3942. (let (e tgs)
  3943. (while (setq e (pop tags))
  3944. (cond
  3945. ((equal e "{") (push '(:startgroup) tgs))
  3946. ((equal e "}") (push '(:endgroup) tgs))
  3947. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3948. (push (cons (match-string 1 e)
  3949. (string-to-char (match-string 2 e)))
  3950. tgs))
  3951. (t (push (list e) tgs))))
  3952. (org-set-local 'org-tag-alist nil)
  3953. (while (setq e (pop tgs))
  3954. (or (and (stringp (car e))
  3955. (assoc (car e) org-tag-alist))
  3956. (push e org-tag-alist))))))
  3957. ;; Compute the regular expressions and other local variables
  3958. (if (not org-done-keywords)
  3959. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3960. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3961. (length org-scheduled-string)))
  3962. org-drawer-regexp
  3963. (concat "^[ \t]*:\\("
  3964. (mapconcat 'regexp-quote org-drawers "\\|")
  3965. "\\):[ \t]*$")
  3966. org-not-done-keywords
  3967. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3968. org-todo-regexp
  3969. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3970. "\\|") "\\)\\>")
  3971. org-not-done-regexp
  3972. (concat "\\<\\("
  3973. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3974. "\\)\\>")
  3975. org-todo-line-regexp
  3976. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3977. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3978. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3979. org-complex-heading-regexp
  3980. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  3981. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3982. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3983. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3984. org-nl-done-regexp
  3985. (concat "\n\\*+[ \t]+"
  3986. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3987. "\\)" "\\>")
  3988. org-todo-line-tags-regexp
  3989. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3990. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3991. (org-re
  3992. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3993. org-looking-at-done-regexp
  3994. (concat "^" "\\(?:"
  3995. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3996. "\\>")
  3997. org-deadline-regexp (concat "\\<" org-deadline-string)
  3998. org-deadline-time-regexp
  3999. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4000. org-deadline-line-regexp
  4001. (concat "\\<\\(" org-deadline-string "\\).*")
  4002. org-scheduled-regexp
  4003. (concat "\\<" org-scheduled-string)
  4004. org-scheduled-time-regexp
  4005. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4006. org-closed-time-regexp
  4007. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4008. org-keyword-time-regexp
  4009. (concat "\\<\\(" org-scheduled-string
  4010. "\\|" org-deadline-string
  4011. "\\|" org-closed-string
  4012. "\\|" org-clock-string "\\)"
  4013. " *[[<]\\([^]>]+\\)[]>]")
  4014. org-keyword-time-not-clock-regexp
  4015. (concat "\\<\\(" org-scheduled-string
  4016. "\\|" org-deadline-string
  4017. "\\|" org-closed-string
  4018. "\\)"
  4019. " *[[<]\\([^]>]+\\)[]>]")
  4020. org-maybe-keyword-time-regexp
  4021. (concat "\\(\\<\\(" org-scheduled-string
  4022. "\\|" org-deadline-string
  4023. "\\|" org-closed-string
  4024. "\\|" org-clock-string "\\)\\)?"
  4025. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4026. org-planning-or-clock-line-re
  4027. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4028. "\\|" org-deadline-string
  4029. "\\|" org-closed-string "\\|" org-clock-string
  4030. "\\)\\>\\)")
  4031. )
  4032. (org-compute-latex-and-specials-regexp)
  4033. (org-set-font-lock-defaults)))
  4034. (defun org-remove-keyword-keys (list)
  4035. (mapcar (lambda (x)
  4036. (if (string-match "(..?)$" x)
  4037. (substring x 0 (match-beginning 0))
  4038. x))
  4039. list))
  4040. ;; FIXME: this could be done much better, using second characters etc.
  4041. (defun org-assign-fast-keys (alist)
  4042. "Assign fast keys to a keyword-key alist.
  4043. Respect keys that are already there."
  4044. (let (new e k c c1 c2 (char ?a))
  4045. (while (setq e (pop alist))
  4046. (cond
  4047. ((equal e '(:startgroup)) (push e new))
  4048. ((equal e '(:endgroup)) (push e new))
  4049. (t
  4050. (setq k (car e) c2 nil)
  4051. (if (cdr e)
  4052. (setq c (cdr e))
  4053. ;; automatically assign a character.
  4054. (setq c1 (string-to-char
  4055. (downcase (substring
  4056. k (if (= (string-to-char k) ?@) 1 0)))))
  4057. (if (or (rassoc c1 new) (rassoc c1 alist))
  4058. (while (or (rassoc char new) (rassoc char alist))
  4059. (setq char (1+ char)))
  4060. (setq c2 c1))
  4061. (setq c (or c2 char)))
  4062. (push (cons k c) new))))
  4063. (nreverse new)))
  4064. ;;; Some variables ujsed in various places
  4065. (defvar org-window-configuration nil
  4066. "Used in various places to store a window configuration.")
  4067. (defvar org-finish-function nil
  4068. "Function to be called when `C-c C-c' is used.
  4069. This is for getting out of special buffers like remember.")
  4070. ;; FIXME: Occasionally check by commenting these, to make sure
  4071. ;; no other functions uses these, forgetting to let-bind them.
  4072. (defvar entry)
  4073. (defvar state)
  4074. (defvar last-state)
  4075. (defvar date)
  4076. (defvar description)
  4077. ;; Defined somewhere in this file, but used before definition.
  4078. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4079. (defvar org-agenda-buffer-name)
  4080. (defvar org-agenda-undo-list)
  4081. (defvar org-agenda-pending-undo-list)
  4082. (defvar org-agenda-overriding-header)
  4083. (defvar orgtbl-mode)
  4084. (defvar org-html-entities)
  4085. (defvar org-struct-menu)
  4086. (defvar org-org-menu)
  4087. (defvar org-tbl-menu)
  4088. (defvar org-agenda-keymap)
  4089. ;;;; Emacs/XEmacs compatibility
  4090. ;; Overlay compatibility functions
  4091. (defun org-make-overlay (beg end &optional buffer)
  4092. (if (featurep 'xemacs)
  4093. (make-extent beg end buffer)
  4094. (make-overlay beg end buffer)))
  4095. (defun org-delete-overlay (ovl)
  4096. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4097. (defun org-detach-overlay (ovl)
  4098. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4099. (defun org-move-overlay (ovl beg end &optional buffer)
  4100. (if (featurep 'xemacs)
  4101. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4102. (move-overlay ovl beg end buffer)))
  4103. (defun org-overlay-put (ovl prop value)
  4104. (if (featurep 'xemacs)
  4105. (set-extent-property ovl prop value)
  4106. (overlay-put ovl prop value)))
  4107. (defun org-overlay-display (ovl text &optional face evap)
  4108. "Make overlay OVL display TEXT with face FACE."
  4109. (if (featurep 'xemacs)
  4110. (let ((gl (make-glyph text)))
  4111. (and face (set-glyph-face gl face))
  4112. (set-extent-property ovl 'invisible t)
  4113. (set-extent-property ovl 'end-glyph gl))
  4114. (overlay-put ovl 'display text)
  4115. (if face (overlay-put ovl 'face face))
  4116. (if evap (overlay-put ovl 'evaporate t))))
  4117. (defun org-overlay-before-string (ovl text &optional face evap)
  4118. "Make overlay OVL display TEXT with face FACE."
  4119. (if (featurep 'xemacs)
  4120. (let ((gl (make-glyph text)))
  4121. (and face (set-glyph-face gl face))
  4122. (set-extent-property ovl 'begin-glyph gl))
  4123. (if face (org-add-props text nil 'face face))
  4124. (overlay-put ovl 'before-string text)
  4125. (if evap (overlay-put ovl 'evaporate t))))
  4126. (defun org-overlay-get (ovl prop)
  4127. (if (featurep 'xemacs)
  4128. (extent-property ovl prop)
  4129. (overlay-get ovl prop)))
  4130. (defun org-overlays-at (pos)
  4131. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4132. (defun org-overlays-in (&optional start end)
  4133. (if (featurep 'xemacs)
  4134. (extent-list nil start end)
  4135. (overlays-in start end)))
  4136. (defun org-overlay-start (o)
  4137. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4138. (defun org-overlay-end (o)
  4139. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4140. (defun org-find-overlays (prop &optional pos delete)
  4141. "Find all overlays specifying PROP at POS or point.
  4142. If DELETE is non-nil, delete all those overlays."
  4143. (let ((overlays (org-overlays-at (or pos (point))))
  4144. ov found)
  4145. (while (setq ov (pop overlays))
  4146. (if (org-overlay-get ov prop)
  4147. (if delete (org-delete-overlay ov) (push ov found))))
  4148. found))
  4149. ;; Region compatibility
  4150. (defun org-add-hook (hook function &optional append local)
  4151. "Add-hook, compatible with both Emacsen."
  4152. (if (and local (featurep 'xemacs))
  4153. (add-local-hook hook function append)
  4154. (add-hook hook function append local)))
  4155. (defvar org-ignore-region nil
  4156. "To temporarily disable the active region.")
  4157. (defun org-region-active-p ()
  4158. "Is `transient-mark-mode' on and the region active?
  4159. Works on both Emacs and XEmacs."
  4160. (if org-ignore-region
  4161. nil
  4162. (if (featurep 'xemacs)
  4163. (and zmacs-regions (region-active-p))
  4164. (if (fboundp 'use-region-p)
  4165. (use-region-p)
  4166. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4167. ;; Invisibility compatibility
  4168. (defun org-add-to-invisibility-spec (arg)
  4169. "Add elements to `buffer-invisibility-spec'.
  4170. See documentation for `buffer-invisibility-spec' for the kind of elements
  4171. that can be added."
  4172. (cond
  4173. ((fboundp 'add-to-invisibility-spec)
  4174. (add-to-invisibility-spec arg))
  4175. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4176. (setq buffer-invisibility-spec (list arg)))
  4177. (t
  4178. (setq buffer-invisibility-spec
  4179. (cons arg buffer-invisibility-spec)))))
  4180. (defun org-remove-from-invisibility-spec (arg)
  4181. "Remove elements from `buffer-invisibility-spec'."
  4182. (if (fboundp 'remove-from-invisibility-spec)
  4183. (remove-from-invisibility-spec arg)
  4184. (if (consp buffer-invisibility-spec)
  4185. (setq buffer-invisibility-spec
  4186. (delete arg buffer-invisibility-spec)))))
  4187. (defun org-in-invisibility-spec-p (arg)
  4188. "Is ARG a member of `buffer-invisibility-spec'?"
  4189. (if (consp buffer-invisibility-spec)
  4190. (member arg buffer-invisibility-spec)
  4191. nil))
  4192. ;;;; Define the Org-mode
  4193. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4194. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  4195. ;; We use a before-change function to check if a table might need
  4196. ;; an update.
  4197. (defvar org-table-may-need-update t
  4198. "Indicates that a table might need an update.
  4199. This variable is set by `org-before-change-function'.
  4200. `org-table-align' sets it back to nil.")
  4201. (defvar org-mode-map)
  4202. (defvar org-mode-hook nil)
  4203. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4204. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4205. (defvar org-table-buffer-is-an nil)
  4206. (defconst org-outline-regexp "\\*+ ")
  4207. ;;;###autoload
  4208. (define-derived-mode org-mode outline-mode "Org"
  4209. "Outline-based notes management and organizer, alias
  4210. \"Carsten's outline-mode for keeping track of everything.\"
  4211. Org-mode develops organizational tasks around a NOTES file which
  4212. contains information about projects as plain text. Org-mode is
  4213. implemented on top of outline-mode, which is ideal to keep the content
  4214. of large files well structured. It supports ToDo items, deadlines and
  4215. time stamps, which magically appear in the diary listing of the Emacs
  4216. calendar. Tables are easily created with a built-in table editor.
  4217. Plain text URL-like links connect to websites, emails (VM), Usenet
  4218. messages (Gnus), BBDB entries, and any files related to the project.
  4219. For printing and sharing of notes, an Org-mode file (or a part of it)
  4220. can be exported as a structured ASCII or HTML file.
  4221. The following commands are available:
  4222. \\{org-mode-map}"
  4223. ;; Get rid of Outline menus, they are not needed
  4224. ;; Need to do this here because define-derived-mode sets up
  4225. ;; the keymap so late. Still, it is a waste to call this each time
  4226. ;; we switch another buffer into org-mode.
  4227. (if (featurep 'xemacs)
  4228. (when (boundp 'outline-mode-menu-heading)
  4229. ;; Assume this is Greg's port, it used easymenu
  4230. (easy-menu-remove outline-mode-menu-heading)
  4231. (easy-menu-remove outline-mode-menu-show)
  4232. (easy-menu-remove outline-mode-menu-hide))
  4233. (define-key org-mode-map [menu-bar headings] 'undefined)
  4234. (define-key org-mode-map [menu-bar hide] 'undefined)
  4235. (define-key org-mode-map [menu-bar show] 'undefined))
  4236. (easy-menu-add org-org-menu)
  4237. (easy-menu-add org-tbl-menu)
  4238. (org-install-agenda-files-menu)
  4239. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4240. (org-add-to-invisibility-spec '(org-cwidth))
  4241. (when (featurep 'xemacs)
  4242. (org-set-local 'line-move-ignore-invisible t))
  4243. (org-set-local 'outline-regexp org-outline-regexp)
  4244. (org-set-local 'outline-level 'org-outline-level)
  4245. (when (and org-ellipsis
  4246. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4247. (fboundp 'make-glyph-code))
  4248. (unless org-display-table
  4249. (setq org-display-table (make-display-table)))
  4250. (set-display-table-slot
  4251. org-display-table 4
  4252. (vconcat (mapcar
  4253. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4254. org-ellipsis)))
  4255. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4256. (setq buffer-display-table org-display-table))
  4257. (org-set-regexps-and-options)
  4258. ;; Calc embedded
  4259. (org-set-local 'calc-embedded-open-mode "# ")
  4260. (modify-syntax-entry ?# "<")
  4261. (modify-syntax-entry ?@ "w")
  4262. (if org-startup-truncated (setq truncate-lines t))
  4263. (org-set-local 'font-lock-unfontify-region-function
  4264. 'org-unfontify-region)
  4265. ;; Activate before-change-function
  4266. (org-set-local 'org-table-may-need-update t)
  4267. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4268. 'local)
  4269. ;; Check for running clock before killing a buffer
  4270. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4271. ;; Paragraphs and auto-filling
  4272. (org-set-autofill-regexps)
  4273. (setq indent-line-function 'org-indent-line-function)
  4274. (org-update-radio-target-regexp)
  4275. ;; Comment characters
  4276. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4277. (org-set-local 'comment-padding " ")
  4278. ;; Imenu
  4279. (org-set-local 'imenu-create-index-function
  4280. 'org-imenu-get-tree)
  4281. ;; Make isearch reveal context
  4282. (if (or (featurep 'xemacs)
  4283. (not (boundp 'outline-isearch-open-invisible-function)))
  4284. ;; Emacs 21 and XEmacs make use of the hook
  4285. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4286. ;; Emacs 22 deals with this through a special variable
  4287. (org-set-local 'outline-isearch-open-invisible-function
  4288. (lambda (&rest ignore) (org-show-context 'isearch))))
  4289. ;; If empty file that did not turn on org-mode automatically, make it to.
  4290. (if (and org-insert-mode-line-in-empty-file
  4291. (interactive-p)
  4292. (= (point-min) (point-max)))
  4293. (insert "# -*- mode: org -*-\n\n"))
  4294. (unless org-inhibit-startup
  4295. (when org-startup-align-all-tables
  4296. (let ((bmp (buffer-modified-p)))
  4297. (org-table-map-tables 'org-table-align)
  4298. (set-buffer-modified-p bmp)))
  4299. (org-cycle-hide-drawers 'all)
  4300. (cond
  4301. ((eq org-startup-folded t)
  4302. (org-cycle '(4)))
  4303. ((eq org-startup-folded 'content)
  4304. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4305. (org-cycle '(4)) (org-cycle '(4)))))))
  4306. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4307. (defsubst org-call-with-arg (command arg)
  4308. "Call COMMAND interactively, but pretend prefix are was ARG."
  4309. (let ((current-prefix-arg arg)) (call-interactively command)))
  4310. (defsubst org-current-line (&optional pos)
  4311. (save-excursion
  4312. (and pos (goto-char pos))
  4313. ;; works also in narrowed buffer, because we start at 1, not point-min
  4314. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4315. (defun org-current-time ()
  4316. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4317. (if (> org-time-stamp-rounding-minutes 0)
  4318. (let ((r org-time-stamp-rounding-minutes)
  4319. (time (decode-time)))
  4320. (apply 'encode-time
  4321. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4322. (nthcdr 2 time))))
  4323. (current-time)))
  4324. (defun org-add-props (string plist &rest props)
  4325. "Add text properties to entire string, from beginning to end.
  4326. PLIST may be a list of properties, PROPS are individual properties and values
  4327. that will be added to PLIST. Returns the string that was modified."
  4328. (add-text-properties
  4329. 0 (length string) (if props (append plist props) plist) string)
  4330. string)
  4331. (put 'org-add-props 'lisp-indent-function 2)
  4332. ;;;; Font-Lock stuff, including the activators
  4333. (defvar org-mouse-map (make-sparse-keymap))
  4334. (org-defkey org-mouse-map
  4335. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4336. (org-defkey org-mouse-map
  4337. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4338. (when org-mouse-1-follows-link
  4339. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4340. (when org-tab-follows-link
  4341. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4342. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4343. (when org-return-follows-link
  4344. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4345. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4346. (require 'font-lock)
  4347. (defconst org-non-link-chars "]\t\n\r<>")
  4348. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4349. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  4350. (defvar org-link-re-with-space nil
  4351. "Matches a link with spaces, optional angular brackets around it.")
  4352. (defvar org-link-re-with-space2 nil
  4353. "Matches a link with spaces, optional angular brackets around it.")
  4354. (defvar org-angle-link-re nil
  4355. "Matches link with angular brackets, spaces are allowed.")
  4356. (defvar org-plain-link-re nil
  4357. "Matches plain link, without spaces.")
  4358. (defvar org-bracket-link-regexp nil
  4359. "Matches a link in double brackets.")
  4360. (defvar org-bracket-link-analytic-regexp nil
  4361. "Regular expression used to analyze links.
  4362. Here is what the match groups contain after a match:
  4363. 1: http:
  4364. 2: http
  4365. 3: path
  4366. 4: [desc]
  4367. 5: desc")
  4368. (defvar org-any-link-re nil
  4369. "Regular expression matching any link.")
  4370. (defun org-make-link-regexps ()
  4371. "Update the link regular expressions.
  4372. This should be called after the variable `org-link-types' has changed."
  4373. (setq org-link-re-with-space
  4374. (concat
  4375. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4376. "\\([^" org-non-link-chars " ]"
  4377. "[^" org-non-link-chars "]*"
  4378. "[^" org-non-link-chars " ]\\)>?")
  4379. org-link-re-with-space2
  4380. (concat
  4381. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4382. "\\([^" org-non-link-chars " ]"
  4383. "[^]\t\n\r]*"
  4384. "[^" org-non-link-chars " ]\\)>?")
  4385. org-angle-link-re
  4386. (concat
  4387. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4388. "\\([^" org-non-link-chars " ]"
  4389. "[^" org-non-link-chars "]*"
  4390. "\\)>")
  4391. org-plain-link-re
  4392. (concat
  4393. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4394. "\\([^]\t\n\r<>,;() ]+\\)")
  4395. org-bracket-link-regexp
  4396. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4397. org-bracket-link-analytic-regexp
  4398. (concat
  4399. "\\[\\["
  4400. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4401. "\\([^]]+\\)"
  4402. "\\]"
  4403. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4404. "\\]")
  4405. org-any-link-re
  4406. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4407. org-angle-link-re "\\)\\|\\("
  4408. org-plain-link-re "\\)")))
  4409. (org-make-link-regexps)
  4410. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4411. "Regular expression for fast time stamp matching.")
  4412. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4413. "Regular expression for fast time stamp matching.")
  4414. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4415. "Regular expression matching time strings for analysis.
  4416. This one does not require the space after the date.")
  4417. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4418. "Regular expression matching time strings for analysis.")
  4419. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4420. "Regular expression matching time stamps, with groups.")
  4421. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4422. "Regular expression matching time stamps (also [..]), with groups.")
  4423. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4424. "Regular expression matching a time stamp range.")
  4425. (defconst org-tr-regexp-both
  4426. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4427. "Regular expression matching a time stamp range.")
  4428. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4429. org-ts-regexp "\\)?")
  4430. "Regular expression matching a time stamp or time stamp range.")
  4431. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4432. org-ts-regexp-both "\\)?")
  4433. "Regular expression matching a time stamp or time stamp range.
  4434. The time stamps may be either active or inactive.")
  4435. (defvar org-emph-face nil)
  4436. (defun org-do-emphasis-faces (limit)
  4437. "Run through the buffer and add overlays to links."
  4438. (let (rtn)
  4439. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4440. (if (not (= (char-after (match-beginning 3))
  4441. (char-after (match-beginning 4))))
  4442. (progn
  4443. (setq rtn t)
  4444. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4445. 'face
  4446. (nth 1 (assoc (match-string 3)
  4447. org-emphasis-alist)))
  4448. (add-text-properties (match-beginning 2) (match-end 2)
  4449. '(font-lock-multiline t))
  4450. (when org-hide-emphasis-markers
  4451. (add-text-properties (match-end 4) (match-beginning 5)
  4452. '(invisible org-link))
  4453. (add-text-properties (match-beginning 3) (match-end 3)
  4454. '(invisible org-link)))))
  4455. (backward-char 1))
  4456. rtn))
  4457. (defun org-emphasize (&optional char)
  4458. "Insert or change an emphasis, i.e. a font like bold or italic.
  4459. If there is an active region, change that region to a new emphasis.
  4460. If there is no region, just insert the marker characters and position
  4461. the cursor between them.
  4462. CHAR should be either the marker character, or the first character of the
  4463. HTML tag associated with that emphasis. If CHAR is a space, the means
  4464. to remove the emphasis of the selected region.
  4465. If char is not given (for example in an interactive call) it
  4466. will be prompted for."
  4467. (interactive)
  4468. (let ((eal org-emphasis-alist) e det
  4469. (erc org-emphasis-regexp-components)
  4470. (prompt "")
  4471. (string "") beg end move tag c s)
  4472. (if (org-region-active-p)
  4473. (setq beg (region-beginning) end (region-end)
  4474. string (buffer-substring beg end))
  4475. (setq move t))
  4476. (while (setq e (pop eal))
  4477. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4478. c (aref tag 0))
  4479. (push (cons c (string-to-char (car e))) det)
  4480. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4481. (substring tag 1)))))
  4482. (unless char
  4483. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4484. (setq char (read-char-exclusive)))
  4485. (setq char (or (cdr (assoc char det)) char))
  4486. (if (equal char ?\ )
  4487. (setq s "" move nil)
  4488. (unless (assoc (char-to-string char) org-emphasis-alist)
  4489. (error "No such emphasis marker: \"%c\"" char))
  4490. (setq s (char-to-string char)))
  4491. (while (and (> (length string) 1)
  4492. (equal (substring string 0 1) (substring string -1))
  4493. (assoc (substring string 0 1) org-emphasis-alist))
  4494. (setq string (substring string 1 -1)))
  4495. (setq string (concat s string s))
  4496. (if beg (delete-region beg end))
  4497. (unless (or (bolp)
  4498. (string-match (concat "[" (nth 0 erc) "\n]")
  4499. (char-to-string (char-before (point)))))
  4500. (insert " "))
  4501. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4502. (char-to-string (char-after (point))))
  4503. (insert " ") (backward-char 1))
  4504. (insert string)
  4505. (and move (backward-char 1))))
  4506. (defconst org-nonsticky-props
  4507. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4508. (defun org-activate-plain-links (limit)
  4509. "Run through the buffer and add overlays to links."
  4510. (catch 'exit
  4511. (let (f)
  4512. (while (re-search-forward org-plain-link-re limit t)
  4513. (setq f (get-text-property (match-beginning 0) 'face))
  4514. (if (or (eq f 'org-tag)
  4515. (and (listp f) (memq 'org-tag f)))
  4516. nil
  4517. (add-text-properties (match-beginning 0) (match-end 0)
  4518. (list 'mouse-face 'highlight
  4519. 'rear-nonsticky org-nonsticky-props
  4520. 'keymap org-mouse-map
  4521. ))
  4522. (throw 'exit t))))))
  4523. (defun org-activate-code (limit)
  4524. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4525. (unless (get-text-property (match-beginning 1) 'face)
  4526. (remove-text-properties (match-beginning 0) (match-end 0)
  4527. '(display t invisible t intangible t))
  4528. t)))
  4529. (defun org-activate-angle-links (limit)
  4530. "Run through the buffer and add overlays to links."
  4531. (if (re-search-forward org-angle-link-re limit t)
  4532. (progn
  4533. (add-text-properties (match-beginning 0) (match-end 0)
  4534. (list 'mouse-face 'highlight
  4535. 'rear-nonsticky org-nonsticky-props
  4536. 'keymap org-mouse-map
  4537. ))
  4538. t)))
  4539. (defmacro org-maybe-intangible (props)
  4540. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4541. In emacs 21, invisible text is not avoided by the command loop, so the
  4542. intangible property is needed to make sure point skips this text.
  4543. In Emacs 22, this is not necessary. The intangible text property has
  4544. led to problems with flyspell. These problems are fixed in flyspell.el,
  4545. but we still avoid setting the property in Emacs 22 and later.
  4546. We use a macro so that the test can happen at compilation time."
  4547. (if (< emacs-major-version 22)
  4548. `(append '(intangible t) ,props)
  4549. props))
  4550. (defun org-activate-bracket-links (limit)
  4551. "Run through the buffer and add overlays to bracketed links."
  4552. (if (re-search-forward org-bracket-link-regexp limit t)
  4553. (let* ((help (concat "LINK: "
  4554. (org-match-string-no-properties 1)))
  4555. ;; FIXME: above we should remove the escapes.
  4556. ;; but that requires another match, protecting match data,
  4557. ;; a lot of overhead for font-lock.
  4558. (ip (org-maybe-intangible
  4559. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4560. 'keymap org-mouse-map 'mouse-face 'highlight
  4561. 'font-lock-multiline t 'help-echo help)))
  4562. (vp (list 'rear-nonsticky org-nonsticky-props
  4563. 'keymap org-mouse-map 'mouse-face 'highlight
  4564. ' font-lock-multiline t 'help-echo help)))
  4565. ;; We need to remove the invisible property here. Table narrowing
  4566. ;; may have made some of this invisible.
  4567. (remove-text-properties (match-beginning 0) (match-end 0)
  4568. '(invisible nil))
  4569. (if (match-end 3)
  4570. (progn
  4571. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4572. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4573. (add-text-properties (match-end 3) (match-end 0) ip))
  4574. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4575. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4576. (add-text-properties (match-end 1) (match-end 0) ip))
  4577. t)))
  4578. (defun org-activate-dates (limit)
  4579. "Run through the buffer and add overlays to dates."
  4580. (if (re-search-forward org-tsr-regexp-both limit t)
  4581. (progn
  4582. (add-text-properties (match-beginning 0) (match-end 0)
  4583. (list 'mouse-face 'highlight
  4584. 'rear-nonsticky org-nonsticky-props
  4585. 'keymap org-mouse-map))
  4586. (when org-display-custom-times
  4587. (if (match-end 3)
  4588. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4589. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4590. t)))
  4591. (defvar org-target-link-regexp nil
  4592. "Regular expression matching radio targets in plain text.")
  4593. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4594. "Regular expression matching a link target.")
  4595. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4596. "Regular expression matching a radio target.")
  4597. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4598. "Regular expression matching any target.")
  4599. (defun org-activate-target-links (limit)
  4600. "Run through the buffer and add overlays to target matches."
  4601. (when org-target-link-regexp
  4602. (let ((case-fold-search t))
  4603. (if (re-search-forward org-target-link-regexp limit t)
  4604. (progn
  4605. (add-text-properties (match-beginning 0) (match-end 0)
  4606. (list 'mouse-face 'highlight
  4607. 'rear-nonsticky org-nonsticky-props
  4608. 'keymap org-mouse-map
  4609. 'help-echo "Radio target link"
  4610. 'org-linked-text t))
  4611. t)))))
  4612. (defun org-update-radio-target-regexp ()
  4613. "Find all radio targets in this file and update the regular expression."
  4614. (interactive)
  4615. (when (memq 'radio org-activate-links)
  4616. (setq org-target-link-regexp
  4617. (org-make-target-link-regexp (org-all-targets 'radio)))
  4618. (org-restart-font-lock)))
  4619. (defun org-hide-wide-columns (limit)
  4620. (let (s e)
  4621. (setq s (text-property-any (point) (or limit (point-max))
  4622. 'org-cwidth t))
  4623. (when s
  4624. (setq e (next-single-property-change s 'org-cwidth))
  4625. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4626. (goto-char e)
  4627. t)))
  4628. (defvar org-latex-and-specials-regexp nil
  4629. "Regular expression for highlighting export special stuff.")
  4630. (defvar org-match-substring-regexp)
  4631. (defvar org-match-substring-with-braces-regexp)
  4632. (defvar org-export-html-special-string-regexps)
  4633. (defun org-compute-latex-and-specials-regexp ()
  4634. "Compute regular expression for stuff treated specially by exporters."
  4635. (if (not org-highlight-latex-fragments-and-specials)
  4636. (org-set-local 'org-latex-and-specials-regexp nil)
  4637. (let*
  4638. ((matchers (plist-get org-format-latex-options :matchers))
  4639. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4640. org-latex-regexps)))
  4641. (options (org-combine-plists (org-default-export-plist)
  4642. (org-infile-export-plist)))
  4643. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4644. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4645. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4646. (org-export-html-expand (plist-get options :expand-quoted-html))
  4647. (org-export-with-special-strings (plist-get options :special-strings))
  4648. (re-sub
  4649. (cond
  4650. ((equal org-export-with-sub-superscripts '{})
  4651. (list org-match-substring-with-braces-regexp))
  4652. (org-export-with-sub-superscripts
  4653. (list org-match-substring-regexp))
  4654. (t nil)))
  4655. (re-latex
  4656. (if org-export-with-LaTeX-fragments
  4657. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4658. (re-macros
  4659. (if org-export-with-TeX-macros
  4660. (list (concat "\\\\"
  4661. (regexp-opt
  4662. (append (mapcar 'car org-html-entities)
  4663. (if (boundp 'org-latex-entities)
  4664. org-latex-entities nil))
  4665. 'words))) ; FIXME
  4666. ))
  4667. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4668. (re-special (if org-export-with-special-strings
  4669. (mapcar (lambda (x) (car x))
  4670. org-export-html-special-string-regexps)))
  4671. (re-rest
  4672. (delq nil
  4673. (list
  4674. (if org-export-html-expand "@<[^>\n]+>")
  4675. ))))
  4676. (org-set-local
  4677. 'org-latex-and-specials-regexp
  4678. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4679. re-rest) "\\|")))))
  4680. (defface org-latex-and-export-specials
  4681. (let ((font (cond ((assq :inherit custom-face-attributes)
  4682. '(:inherit underline))
  4683. (t '(:underline t)))))
  4684. `((((class grayscale) (background light))
  4685. (:foreground "DimGray" ,@font))
  4686. (((class grayscale) (background dark))
  4687. (:foreground "LightGray" ,@font))
  4688. (((class color) (background light))
  4689. (:foreground "SaddleBrown"))
  4690. (((class color) (background dark))
  4691. (:foreground "burlywood"))
  4692. (t (,@font))))
  4693. "Face used to highlight math latex and other special exporter stuff."
  4694. :group 'org-faces)
  4695. (defun org-do-latex-and-special-faces (limit)
  4696. "Run through the buffer and add overlays to links."
  4697. (when org-latex-and-specials-regexp
  4698. (let (rtn d)
  4699. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4700. limit t))
  4701. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4702. 'face))
  4703. '(org-code org-verbatim underline)))
  4704. (progn
  4705. (setq rtn t
  4706. d (cond ((member (char-after (1+ (match-beginning 0)))
  4707. '(?_ ?^)) 1)
  4708. (t 0)))
  4709. (font-lock-prepend-text-property
  4710. (+ d (match-beginning 0)) (match-end 0)
  4711. 'face 'org-latex-and-export-specials)
  4712. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4713. '(font-lock-multiline t)))))
  4714. rtn)))
  4715. (defun org-restart-font-lock ()
  4716. "Restart font-lock-mode, to force refontification."
  4717. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4718. (font-lock-mode -1)
  4719. (font-lock-mode 1)))
  4720. (defun org-all-targets (&optional radio)
  4721. "Return a list of all targets in this file.
  4722. With optional argument RADIO, only find radio targets."
  4723. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4724. rtn)
  4725. (save-excursion
  4726. (goto-char (point-min))
  4727. (while (re-search-forward re nil t)
  4728. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4729. rtn)))
  4730. (defun org-make-target-link-regexp (targets)
  4731. "Make regular expression matching all strings in TARGETS.
  4732. The regular expression finds the targets also if there is a line break
  4733. between words."
  4734. (and targets
  4735. (concat
  4736. "\\<\\("
  4737. (mapconcat
  4738. (lambda (x)
  4739. (while (string-match " +" x)
  4740. (setq x (replace-match "\\s-+" t t x)))
  4741. x)
  4742. targets
  4743. "\\|")
  4744. "\\)\\>")))
  4745. (defun org-activate-tags (limit)
  4746. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4747. (progn
  4748. (add-text-properties (match-beginning 1) (match-end 1)
  4749. (list 'mouse-face 'highlight
  4750. 'rear-nonsticky org-nonsticky-props
  4751. 'keymap org-mouse-map))
  4752. t)))
  4753. (defun org-outline-level ()
  4754. (save-excursion
  4755. (looking-at outline-regexp)
  4756. (if (match-beginning 1)
  4757. (+ (org-get-string-indentation (match-string 1)) 1000)
  4758. (1- (- (match-end 0) (match-beginning 0))))))
  4759. (defvar org-font-lock-keywords nil)
  4760. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4761. "Regular expression matching a property line.")
  4762. (defun org-set-font-lock-defaults ()
  4763. (let* ((em org-fontify-emphasized-text)
  4764. (lk org-activate-links)
  4765. (org-font-lock-extra-keywords
  4766. (list
  4767. ;; Headlines
  4768. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4769. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4770. ;; Table lines
  4771. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4772. (1 'org-table t))
  4773. ;; Table internals
  4774. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4775. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4776. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4777. ;; Drawers
  4778. (list org-drawer-regexp '(0 'org-special-keyword t))
  4779. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4780. ;; Properties
  4781. (list org-property-re
  4782. '(1 'org-special-keyword t)
  4783. '(3 'org-property-value t))
  4784. (if org-format-transports-properties-p
  4785. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4786. ;; Links
  4787. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4788. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4789. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4790. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4791. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4792. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4793. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4794. '(org-hide-wide-columns (0 nil append))
  4795. ;; TODO lines
  4796. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4797. '(1 (org-get-todo-face 1) t))
  4798. ;; DONE
  4799. (if org-fontify-done-headline
  4800. (list (concat "^[*]+ +\\<\\("
  4801. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4802. "\\)\\(.*\\)")
  4803. '(2 'org-headline-done t))
  4804. nil)
  4805. ;; Priorities
  4806. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4807. ;; Special keywords
  4808. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4809. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4810. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4811. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4812. ;; Emphasis
  4813. (if em
  4814. (if (featurep 'xemacs)
  4815. '(org-do-emphasis-faces (0 nil append))
  4816. '(org-do-emphasis-faces)))
  4817. ;; Checkboxes
  4818. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4819. 2 'bold prepend)
  4820. (if org-provide-checkbox-statistics
  4821. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4822. (0 (org-get-checkbox-statistics-face) t)))
  4823. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4824. '(1 'org-archived prepend))
  4825. ;; Specials
  4826. '(org-do-latex-and-special-faces)
  4827. ;; Code
  4828. '(org-activate-code (1 'org-code t))
  4829. ;; COMMENT
  4830. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4831. "\\|" org-quote-string "\\)\\>")
  4832. '(1 'org-special-keyword t))
  4833. '("^#.*" (0 'font-lock-comment-face t))
  4834. )))
  4835. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4836. ;; Now set the full font-lock-keywords
  4837. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4838. (org-set-local 'font-lock-defaults
  4839. '(org-font-lock-keywords t nil nil backward-paragraph))
  4840. (kill-local-variable 'font-lock-keywords) nil))
  4841. (defvar org-m nil)
  4842. (defvar org-l nil)
  4843. (defvar org-f nil)
  4844. (defun org-get-level-face (n)
  4845. "Get the right face for match N in font-lock matching of healdines."
  4846. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4847. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4848. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4849. (cond
  4850. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4851. ((eq n 2) org-f)
  4852. (t (if org-level-color-stars-only nil org-f))))
  4853. (defun org-get-todo-face (kwd)
  4854. "Get the right face for a TODO keyword KWD.
  4855. If KWD is a number, get the corresponding match group."
  4856. (if (numberp kwd) (setq kwd (match-string kwd)))
  4857. (or (cdr (assoc kwd org-todo-keyword-faces))
  4858. (and (member kwd org-done-keywords) 'org-done)
  4859. 'org-todo))
  4860. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4861. "Remove fontification and activation overlays from links."
  4862. (font-lock-default-unfontify-region beg end)
  4863. (let* ((buffer-undo-list t)
  4864. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4865. (inhibit-modification-hooks t)
  4866. deactivate-mark buffer-file-name buffer-file-truename)
  4867. (remove-text-properties beg end
  4868. '(mouse-face t keymap t org-linked-text t
  4869. invisible t intangible t))))
  4870. ;;;; Visibility cycling, including org-goto and indirect buffer
  4871. ;;; Cycling
  4872. (defvar org-cycle-global-status nil)
  4873. (make-variable-buffer-local 'org-cycle-global-status)
  4874. (defvar org-cycle-subtree-status nil)
  4875. (make-variable-buffer-local 'org-cycle-subtree-status)
  4876. ;;;###autoload
  4877. (defun org-cycle (&optional arg)
  4878. "Visibility cycling for Org-mode.
  4879. - When this function is called with a prefix argument, rotate the entire
  4880. buffer through 3 states (global cycling)
  4881. 1. OVERVIEW: Show only top-level headlines.
  4882. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4883. 3. SHOW ALL: Show everything.
  4884. - When point is at the beginning of a headline, rotate the subtree started
  4885. by this line through 3 different states (local cycling)
  4886. 1. FOLDED: Only the main headline is shown.
  4887. 2. CHILDREN: The main headline and the direct children are shown.
  4888. From this state, you can move to one of the children
  4889. and zoom in further.
  4890. 3. SUBTREE: Show the entire subtree, including body text.
  4891. - When there is a numeric prefix, go up to a heading with level ARG, do
  4892. a `show-subtree' and return to the previous cursor position. If ARG
  4893. is negative, go up that many levels.
  4894. - When point is not at the beginning of a headline, execute
  4895. `indent-relative', like TAB normally does. See the option
  4896. `org-cycle-emulate-tab' for details.
  4897. - Special case: if point is at the beginning of the buffer and there is
  4898. no headline in line 1, this function will act as if called with prefix arg.
  4899. But only if also the variable `org-cycle-global-at-bob' is t."
  4900. (interactive "P")
  4901. (let* ((outline-regexp
  4902. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4903. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4904. outline-regexp))
  4905. (bob-special (and org-cycle-global-at-bob (bobp)
  4906. (not (looking-at outline-regexp))))
  4907. (org-cycle-hook
  4908. (if bob-special
  4909. (delq 'org-optimize-window-after-visibility-change
  4910. (copy-sequence org-cycle-hook))
  4911. org-cycle-hook))
  4912. (pos (point)))
  4913. (if (or bob-special (equal arg '(4)))
  4914. ;; special case: use global cycling
  4915. (setq arg t))
  4916. (cond
  4917. ((org-at-table-p 'any)
  4918. ;; Enter the table or move to the next field in the table
  4919. (or (org-table-recognize-table.el)
  4920. (progn
  4921. (if arg (org-table-edit-field t)
  4922. (org-table-justify-field-maybe)
  4923. (call-interactively 'org-table-next-field)))))
  4924. ((eq arg t) ;; Global cycling
  4925. (cond
  4926. ((and (eq last-command this-command)
  4927. (eq org-cycle-global-status 'overview))
  4928. ;; We just created the overview - now do table of contents
  4929. ;; This can be slow in very large buffers, so indicate action
  4930. (message "CONTENTS...")
  4931. (org-content)
  4932. (message "CONTENTS...done")
  4933. (setq org-cycle-global-status 'contents)
  4934. (run-hook-with-args 'org-cycle-hook 'contents))
  4935. ((and (eq last-command this-command)
  4936. (eq org-cycle-global-status 'contents))
  4937. ;; We just showed the table of contents - now show everything
  4938. (show-all)
  4939. (message "SHOW ALL")
  4940. (setq org-cycle-global-status 'all)
  4941. (run-hook-with-args 'org-cycle-hook 'all))
  4942. (t
  4943. ;; Default action: go to overview
  4944. (org-overview)
  4945. (message "OVERVIEW")
  4946. (setq org-cycle-global-status 'overview)
  4947. (run-hook-with-args 'org-cycle-hook 'overview))))
  4948. ((and org-drawers org-drawer-regexp
  4949. (save-excursion
  4950. (beginning-of-line 1)
  4951. (looking-at org-drawer-regexp)))
  4952. ;; Toggle block visibility
  4953. (org-flag-drawer
  4954. (not (get-char-property (match-end 0) 'invisible))))
  4955. ((integerp arg)
  4956. ;; Show-subtree, ARG levels up from here.
  4957. (save-excursion
  4958. (org-back-to-heading)
  4959. (outline-up-heading (if (< arg 0) (- arg)
  4960. (- (funcall outline-level) arg)))
  4961. (org-show-subtree)))
  4962. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4963. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4964. ;; At a heading: rotate between three different views
  4965. (org-back-to-heading)
  4966. (let ((goal-column 0) eoh eol eos)
  4967. ;; First, some boundaries
  4968. (save-excursion
  4969. (org-back-to-heading)
  4970. (save-excursion
  4971. (beginning-of-line 2)
  4972. (while (and (not (eobp)) ;; this is like `next-line'
  4973. (get-char-property (1- (point)) 'invisible))
  4974. (beginning-of-line 2)) (setq eol (point)))
  4975. (outline-end-of-heading) (setq eoh (point))
  4976. (org-end-of-subtree t)
  4977. (unless (eobp)
  4978. (skip-chars-forward " \t\n")
  4979. (beginning-of-line 1) ; in case this is an item
  4980. )
  4981. (setq eos (1- (point))))
  4982. ;; Find out what to do next and set `this-command'
  4983. (cond
  4984. ((= eos eoh)
  4985. ;; Nothing is hidden behind this heading
  4986. (message "EMPTY ENTRY")
  4987. (setq org-cycle-subtree-status nil)
  4988. (save-excursion
  4989. (goto-char eos)
  4990. (outline-next-heading)
  4991. (if (org-invisible-p) (org-flag-heading nil))))
  4992. ((or (>= eol eos)
  4993. (not (string-match "\\S-" (buffer-substring eol eos))))
  4994. ;; Entire subtree is hidden in one line: open it
  4995. (org-show-entry)
  4996. (show-children)
  4997. (message "CHILDREN")
  4998. (save-excursion
  4999. (goto-char eos)
  5000. (outline-next-heading)
  5001. (if (org-invisible-p) (org-flag-heading nil)))
  5002. (setq org-cycle-subtree-status 'children)
  5003. (run-hook-with-args 'org-cycle-hook 'children))
  5004. ((and (eq last-command this-command)
  5005. (eq org-cycle-subtree-status 'children))
  5006. ;; We just showed the children, now show everything.
  5007. (org-show-subtree)
  5008. (message "SUBTREE")
  5009. (setq org-cycle-subtree-status 'subtree)
  5010. (run-hook-with-args 'org-cycle-hook 'subtree))
  5011. (t
  5012. ;; Default action: hide the subtree.
  5013. (hide-subtree)
  5014. (message "FOLDED")
  5015. (setq org-cycle-subtree-status 'folded)
  5016. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5017. ;; TAB emulation
  5018. (buffer-read-only (org-back-to-heading))
  5019. ((org-try-cdlatex-tab))
  5020. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5021. (or (not (bolp))
  5022. (not (looking-at outline-regexp))))
  5023. (call-interactively (global-key-binding "\t")))
  5024. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5025. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5026. (or (and (eq org-cycle-emulate-tab 'white)
  5027. (= (match-end 0) (point-at-eol)))
  5028. (and (eq org-cycle-emulate-tab 'whitestart)
  5029. (>= (match-end 0) pos))))
  5030. t
  5031. (eq org-cycle-emulate-tab t))
  5032. ; (if (and (looking-at "[ \n\r\t]")
  5033. ; (string-match "^[ \t]*$" (buffer-substring
  5034. ; (point-at-bol) (point))))
  5035. ; (progn
  5036. ; (beginning-of-line 1)
  5037. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5038. (call-interactively (global-key-binding "\t")))
  5039. (t (save-excursion
  5040. (org-back-to-heading)
  5041. (org-cycle))))))
  5042. ;;;###autoload
  5043. (defun org-global-cycle (&optional arg)
  5044. "Cycle the global visibility. For details see `org-cycle'."
  5045. (interactive "P")
  5046. (let ((org-cycle-include-plain-lists
  5047. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5048. (if (integerp arg)
  5049. (progn
  5050. (show-all)
  5051. (hide-sublevels arg)
  5052. (setq org-cycle-global-status 'contents))
  5053. (org-cycle '(4)))))
  5054. (defun org-overview ()
  5055. "Switch to overview mode, shoing only top-level headlines.
  5056. Really, this shows all headlines with level equal or greater than the level
  5057. of the first headline in the buffer. This is important, because if the
  5058. first headline is not level one, then (hide-sublevels 1) gives confusing
  5059. results."
  5060. (interactive)
  5061. (let ((level (save-excursion
  5062. (goto-char (point-min))
  5063. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5064. (progn
  5065. (goto-char (match-beginning 0))
  5066. (funcall outline-level))))))
  5067. (and level (hide-sublevels level))))
  5068. (defun org-content (&optional arg)
  5069. "Show all headlines in the buffer, like a table of contents.
  5070. With numerical argument N, show content up to level N."
  5071. (interactive "P")
  5072. (save-excursion
  5073. ;; Visit all headings and show their offspring
  5074. (and (integerp arg) (org-overview))
  5075. (goto-char (point-max))
  5076. (catch 'exit
  5077. (while (and (progn (condition-case nil
  5078. (outline-previous-visible-heading 1)
  5079. (error (goto-char (point-min))))
  5080. t)
  5081. (looking-at outline-regexp))
  5082. (if (integerp arg)
  5083. (show-children (1- arg))
  5084. (show-branches))
  5085. (if (bobp) (throw 'exit nil))))))
  5086. (defun org-optimize-window-after-visibility-change (state)
  5087. "Adjust the window after a change in outline visibility.
  5088. This function is the default value of the hook `org-cycle-hook'."
  5089. (when (get-buffer-window (current-buffer))
  5090. (cond
  5091. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5092. ; ((eq state 'overview) (org-beginning-of-line))
  5093. ((eq state 'content) nil)
  5094. ((eq state 'all) nil)
  5095. ((eq state 'folded) nil)
  5096. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5097. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5098. (defun org-compact-display-after-subtree-move ()
  5099. (let (beg end)
  5100. (save-excursion
  5101. (if (org-up-heading-safe)
  5102. (progn
  5103. (hide-subtree)
  5104. (show-entry)
  5105. (show-children)
  5106. (org-cycle-show-empty-lines 'children)
  5107. (org-cycle-hide-drawers 'children))
  5108. (org-overview)))))
  5109. (defun org-cycle-show-empty-lines (state)
  5110. "Show empty lines above all visible headlines.
  5111. The region to be covered depends on STATE when called through
  5112. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5113. entire buffer covered. Note that an empty line is only shown if there
  5114. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5115. (when (> org-cycle-separator-lines 0)
  5116. (save-excursion
  5117. (let* ((n org-cycle-separator-lines)
  5118. (re (cond
  5119. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5120. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5121. (t (let ((ns (number-to-string (- n 2))))
  5122. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5123. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5124. beg end)
  5125. (cond
  5126. ((memq state '(overview contents t))
  5127. (setq beg (point-min) end (point-max)))
  5128. ((memq state '(children folded))
  5129. (setq beg (point) end (progn (org-end-of-subtree t t)
  5130. (beginning-of-line 2)
  5131. (point)))))
  5132. (when beg
  5133. (goto-char beg)
  5134. (while (re-search-forward re end t)
  5135. (if (not (get-char-property (match-end 1) 'invisible))
  5136. (outline-flag-region
  5137. (match-beginning 1) (match-end 1) nil)))))))
  5138. ;; Never hide empty lines at the end of the file.
  5139. (save-excursion
  5140. (goto-char (point-max))
  5141. (outline-previous-heading)
  5142. (outline-end-of-heading)
  5143. (if (and (looking-at "[ \t\n]+")
  5144. (= (match-end 0) (point-max)))
  5145. (outline-flag-region (point) (match-end 0) nil))))
  5146. (defun org-subtree-end-visible-p ()
  5147. "Is the end of the current subtree visible?"
  5148. (pos-visible-in-window-p
  5149. (save-excursion (org-end-of-subtree t) (point))))
  5150. (defun org-first-headline-recenter (&optional N)
  5151. "Move cursor to the first headline and recenter the headline.
  5152. Optional argument N means, put the headline into the Nth line of the window."
  5153. (goto-char (point-min))
  5154. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5155. (beginning-of-line)
  5156. (recenter (prefix-numeric-value N))))
  5157. ;;; Org-goto
  5158. (defvar org-goto-window-configuration nil)
  5159. (defvar org-goto-marker nil)
  5160. (defvar org-goto-map
  5161. (let ((map (make-sparse-keymap)))
  5162. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5163. (while (setq cmd (pop cmds))
  5164. (substitute-key-definition cmd cmd map global-map)))
  5165. (suppress-keymap map)
  5166. (org-defkey map "\C-m" 'org-goto-ret)
  5167. (org-defkey map [(left)] 'org-goto-left)
  5168. (org-defkey map [(right)] 'org-goto-right)
  5169. (org-defkey map [(?q)] 'org-goto-quit)
  5170. (org-defkey map [(control ?g)] 'org-goto-quit)
  5171. (org-defkey map "\C-i" 'org-cycle)
  5172. (org-defkey map [(tab)] 'org-cycle)
  5173. (org-defkey map [(down)] 'outline-next-visible-heading)
  5174. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5175. (org-defkey map "n" 'outline-next-visible-heading)
  5176. (org-defkey map "p" 'outline-previous-visible-heading)
  5177. (org-defkey map "f" 'outline-forward-same-level)
  5178. (org-defkey map "b" 'outline-backward-same-level)
  5179. (org-defkey map "u" 'outline-up-heading)
  5180. (org-defkey map "/" 'org-occur)
  5181. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5182. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5183. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5184. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5185. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5186. map))
  5187. (defconst org-goto-help
  5188. "Browse copy of buffer to find location or copy text.
  5189. RET=jump to location [Q]uit and return to previous location
  5190. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur"
  5191. )
  5192. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5193. (defun org-goto ()
  5194. "Look up a different location in the current file, keeping current visibility.
  5195. When you want look-up or go to a different location in a document, the
  5196. fastest way is often to fold the entire buffer and then dive into the tree.
  5197. This method has the disadvantage, that the previous location will be folded,
  5198. which may not be what you want.
  5199. This command works around this by showing a copy of the current buffer
  5200. in an indirect buffer, in overview mode. You can dive into the tree in
  5201. that copy, use org-occur and incremental search to find a location.
  5202. When pressing RET or `Q', the command returns to the original buffer in
  5203. which the visibility is still unchanged. After RET is will also jump to
  5204. the location selected in the indirect buffer and expose the
  5205. the headline hierarchy above."
  5206. (interactive)
  5207. (let* ((org-goto-start-pos (point))
  5208. (selected-point
  5209. (car (org-get-location (current-buffer) org-goto-help))))
  5210. (if selected-point
  5211. (progn
  5212. (org-mark-ring-push org-goto-start-pos)
  5213. (goto-char selected-point)
  5214. (if (or (org-invisible-p) (org-invisible-p2))
  5215. (org-show-context 'org-goto)))
  5216. (message "Quit"))))
  5217. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5218. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5219. (defun org-get-location (buf help)
  5220. "Let the user select a location in the Org-mode buffer BUF.
  5221. This function uses a recursive edit. It returns the selected position
  5222. or nil."
  5223. (let (org-goto-selected-point org-goto-exit-command)
  5224. (save-excursion
  5225. (save-window-excursion
  5226. (delete-other-windows)
  5227. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5228. (switch-to-buffer
  5229. (condition-case nil
  5230. (make-indirect-buffer (current-buffer) "*org-goto*")
  5231. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5232. (with-output-to-temp-buffer "*Help*"
  5233. (princ help))
  5234. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5235. (setq buffer-read-only nil)
  5236. (let ((org-startup-truncated t)
  5237. (org-startup-folded nil)
  5238. (org-startup-align-all-tables nil))
  5239. (org-mode)
  5240. (org-overview))
  5241. (setq buffer-read-only t)
  5242. (if (and (boundp 'org-goto-start-pos)
  5243. (integer-or-marker-p org-goto-start-pos))
  5244. (let ((org-show-hierarchy-above t)
  5245. (org-show-siblings t)
  5246. (org-show-following-heading t))
  5247. (goto-char org-goto-start-pos)
  5248. (and (org-invisible-p) (org-show-context)))
  5249. (goto-char (point-min)))
  5250. (org-beginning-of-line)
  5251. (message "Select location and press RET")
  5252. ;; now we make sure that during selection, ony very few keys work
  5253. ;; and that it is impossible to switch to another window.
  5254. ; (let ((gm (current-global-map))
  5255. ; (overriding-local-map org-goto-map))
  5256. ; (unwind-protect
  5257. ; (progn
  5258. ; (use-global-map org-goto-map)
  5259. ; (recursive-edit))
  5260. ; (use-global-map gm)))
  5261. (use-local-map org-goto-map)
  5262. (recursive-edit)
  5263. ))
  5264. (kill-buffer "*org-goto*")
  5265. (cons org-goto-selected-point org-goto-exit-command)))
  5266. (defun org-goto-ret (&optional arg)
  5267. "Finish `org-goto' by going to the new location."
  5268. (interactive "P")
  5269. (setq org-goto-selected-point (point)
  5270. org-goto-exit-command 'return)
  5271. (throw 'exit nil))
  5272. (defun org-goto-left ()
  5273. "Finish `org-goto' by going to the new location."
  5274. (interactive)
  5275. (if (org-on-heading-p)
  5276. (progn
  5277. (beginning-of-line 1)
  5278. (setq org-goto-selected-point (point)
  5279. org-goto-exit-command 'left)
  5280. (throw 'exit nil))
  5281. (error "Not on a heading")))
  5282. (defun org-goto-right ()
  5283. "Finish `org-goto' by going to the new location."
  5284. (interactive)
  5285. (if (org-on-heading-p)
  5286. (progn
  5287. (setq org-goto-selected-point (point)
  5288. org-goto-exit-command 'right)
  5289. (throw 'exit nil))
  5290. (error "Not on a heading")))
  5291. (defun org-goto-quit ()
  5292. "Finish `org-goto' without cursor motion."
  5293. (interactive)
  5294. (setq org-goto-selected-point nil)
  5295. (setq org-goto-exit-command 'quit)
  5296. (throw 'exit nil))
  5297. ;;; Indirect buffer display of subtrees
  5298. (defvar org-indirect-dedicated-frame nil
  5299. "This is the frame being used for indirect tree display.")
  5300. (defvar org-last-indirect-buffer nil)
  5301. (defun org-tree-to-indirect-buffer (&optional arg)
  5302. "Create indirect buffer and narrow it to current subtree.
  5303. With numerical prefix ARG, go up to this level and then take that tree.
  5304. If ARG is negative, go up that many levels.
  5305. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5306. indirect buffer previously made with this command, to avoid proliferation of
  5307. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5308. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5309. is kept so that you can work with several indirect buffers at the same time.
  5310. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5311. requests that a new frame be made for the new buffer, so that the dedicated
  5312. frame is not changed."
  5313. (interactive "P")
  5314. (let ((cbuf (current-buffer))
  5315. (cwin (selected-window))
  5316. (pos (point))
  5317. beg end level heading ibuf)
  5318. (save-excursion
  5319. (org-back-to-heading t)
  5320. (when (numberp arg)
  5321. (setq level (org-outline-level))
  5322. (if (< arg 0) (setq arg (+ level arg)))
  5323. (while (> (setq level (org-outline-level)) arg)
  5324. (outline-up-heading 1 t)))
  5325. (setq beg (point)
  5326. heading (org-get-heading))
  5327. (org-end-of-subtree t) (setq end (point)))
  5328. (if (and (buffer-live-p org-last-indirect-buffer)
  5329. (not (eq org-indirect-buffer-display 'new-frame))
  5330. (not arg))
  5331. (kill-buffer org-last-indirect-buffer))
  5332. (setq ibuf (org-get-indirect-buffer cbuf)
  5333. org-last-indirect-buffer ibuf)
  5334. (cond
  5335. ((or (eq org-indirect-buffer-display 'new-frame)
  5336. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5337. (select-frame (make-frame))
  5338. (delete-other-windows)
  5339. (switch-to-buffer ibuf)
  5340. (org-set-frame-title heading))
  5341. ((eq org-indirect-buffer-display 'dedicated-frame)
  5342. (raise-frame
  5343. (select-frame (or (and org-indirect-dedicated-frame
  5344. (frame-live-p org-indirect-dedicated-frame)
  5345. org-indirect-dedicated-frame)
  5346. (setq org-indirect-dedicated-frame (make-frame)))))
  5347. (delete-other-windows)
  5348. (switch-to-buffer ibuf)
  5349. (org-set-frame-title (concat "Indirect: " heading)))
  5350. ((eq org-indirect-buffer-display 'current-window)
  5351. (switch-to-buffer ibuf))
  5352. ((eq org-indirect-buffer-display 'other-window)
  5353. (pop-to-buffer ibuf))
  5354. (t (error "Invalid value.")))
  5355. (if (featurep 'xemacs)
  5356. (save-excursion (org-mode) (turn-on-font-lock)))
  5357. (narrow-to-region beg end)
  5358. (show-all)
  5359. (goto-char pos)
  5360. (and (window-live-p cwin) (select-window cwin))))
  5361. (defun org-get-indirect-buffer (&optional buffer)
  5362. (setq buffer (or buffer (current-buffer)))
  5363. (let ((n 1) (base (buffer-name buffer)) bname)
  5364. (while (buffer-live-p
  5365. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5366. (setq n (1+ n)))
  5367. (condition-case nil
  5368. (make-indirect-buffer buffer bname 'clone)
  5369. (error (make-indirect-buffer buffer bname)))))
  5370. (defun org-set-frame-title (title)
  5371. "Set the title of the current frame to the string TITLE."
  5372. ;; FIXME: how to name a single frame in XEmacs???
  5373. (unless (featurep 'xemacs)
  5374. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5375. ;;;; Structure editing
  5376. ;;; Inserting headlines
  5377. (defun org-insert-heading (&optional force-heading)
  5378. "Insert a new heading or item with same depth at point.
  5379. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5380. If point is at the beginning of a headline, insert a sibling before the
  5381. current headline. If point is in the middle of a headline, split the headline
  5382. at that position and make the rest of the headline part of the sibling below
  5383. the current headline."
  5384. (interactive "P")
  5385. (if (= (buffer-size) 0)
  5386. (insert "\n* ")
  5387. (when (or force-heading (not (org-insert-item)))
  5388. (let* ((head (save-excursion
  5389. (condition-case nil
  5390. (progn
  5391. (org-back-to-heading)
  5392. (match-string 0))
  5393. (error "*"))))
  5394. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5395. pos)
  5396. (cond
  5397. ((and (org-on-heading-p) (bolp)
  5398. (or (bobp)
  5399. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5400. (open-line (if blank 2 1)))
  5401. ((and (bolp)
  5402. (or (bobp)
  5403. (save-excursion
  5404. (backward-char 1) (not (org-invisible-p)))))
  5405. nil)
  5406. (t (newline (if blank 2 1))))
  5407. (insert head) (just-one-space)
  5408. (setq pos (point))
  5409. (end-of-line 1)
  5410. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5411. (run-hooks 'org-insert-heading-hook)))))
  5412. (defun org-insert-heading-after-current ()
  5413. "Insert a new heading with same level as current, after current subtree."
  5414. (interactive)
  5415. (org-back-to-heading)
  5416. (org-insert-heading)
  5417. (org-move-subtree-down)
  5418. (end-of-line 1))
  5419. (defun org-insert-todo-heading (arg)
  5420. "Insert a new heading with the same level and TODO state as current heading.
  5421. If the heading has no TODO state, or if the state is DONE, use the first
  5422. state (TODO by default). Also with prefix arg, force first state."
  5423. (interactive "P")
  5424. (when (not (org-insert-item 'checkbox))
  5425. (org-insert-heading)
  5426. (save-excursion
  5427. (org-back-to-heading)
  5428. (outline-previous-heading)
  5429. (looking-at org-todo-line-regexp))
  5430. (if (or arg
  5431. (not (match-beginning 2))
  5432. (member (match-string 2) org-done-keywords))
  5433. (insert (car org-todo-keywords-1) " ")
  5434. (insert (match-string 2) " "))))
  5435. (defun org-insert-subheading (arg)
  5436. "Insert a new subheading and demote it.
  5437. Works for outline headings and for plain lists alike."
  5438. (interactive "P")
  5439. (org-insert-heading arg)
  5440. (cond
  5441. ((org-on-heading-p) (org-do-demote))
  5442. ((org-at-item-p) (org-indent-item 1))))
  5443. (defun org-insert-todo-subheading (arg)
  5444. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5445. Works for outline headings and for plain lists alike."
  5446. (interactive "P")
  5447. (org-insert-todo-heading arg)
  5448. (cond
  5449. ((org-on-heading-p) (org-do-demote))
  5450. ((org-at-item-p) (org-indent-item 1))))
  5451. ;;; Promotion and Demotion
  5452. (defun org-promote-subtree ()
  5453. "Promote the entire subtree.
  5454. See also `org-promote'."
  5455. (interactive)
  5456. (save-excursion
  5457. (org-map-tree 'org-promote))
  5458. (org-fix-position-after-promote))
  5459. (defun org-demote-subtree ()
  5460. "Demote the entire subtree. See `org-demote'.
  5461. See also `org-promote'."
  5462. (interactive)
  5463. (save-excursion
  5464. (org-map-tree 'org-demote))
  5465. (org-fix-position-after-promote))
  5466. (defun org-do-promote ()
  5467. "Promote the current heading higher up the tree.
  5468. If the region is active in `transient-mark-mode', promote all headings
  5469. in the region."
  5470. (interactive)
  5471. (save-excursion
  5472. (if (org-region-active-p)
  5473. (org-map-region 'org-promote (region-beginning) (region-end))
  5474. (org-promote)))
  5475. (org-fix-position-after-promote))
  5476. (defun org-do-demote ()
  5477. "Demote the current heading lower down the tree.
  5478. If the region is active in `transient-mark-mode', demote all headings
  5479. in the region."
  5480. (interactive)
  5481. (save-excursion
  5482. (if (org-region-active-p)
  5483. (org-map-region 'org-demote (region-beginning) (region-end))
  5484. (org-demote)))
  5485. (org-fix-position-after-promote))
  5486. (defun org-fix-position-after-promote ()
  5487. "Make sure that after pro/demotion cursor position is right."
  5488. (let ((pos (point)))
  5489. (when (save-excursion
  5490. (beginning-of-line 1)
  5491. (looking-at org-todo-line-regexp)
  5492. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5493. (cond ((eobp) (insert " "))
  5494. ((eolp) (insert " "))
  5495. ((equal (char-after) ?\ ) (forward-char 1))))))
  5496. (defun org-reduced-level (l)
  5497. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5498. (defun org-get-legal-level (level &optional change)
  5499. "Rectify a level change under the influence of `org-odd-levels-only'
  5500. LEVEL is a current level, CHANGE is by how much the level should be
  5501. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5502. even level numbers will become the next higher odd number."
  5503. (if org-odd-levels-only
  5504. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5505. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5506. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5507. (max 1 (+ level change))))
  5508. (defun org-promote ()
  5509. "Promote the current heading higher up the tree.
  5510. If the region is active in `transient-mark-mode', promote all headings
  5511. in the region."
  5512. (org-back-to-heading t)
  5513. (let* ((level (save-match-data (funcall outline-level)))
  5514. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5515. (diff (abs (- level (length up-head) -1))))
  5516. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5517. (replace-match up-head nil t)
  5518. ;; Fixup tag positioning
  5519. (and org-auto-align-tags (org-set-tags nil t))
  5520. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5521. (defun org-demote ()
  5522. "Demote the current heading lower down the tree.
  5523. If the region is active in `transient-mark-mode', demote all headings
  5524. in the region."
  5525. (org-back-to-heading t)
  5526. (let* ((level (save-match-data (funcall outline-level)))
  5527. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5528. (diff (abs (- level (length down-head) -1))))
  5529. (replace-match down-head nil t)
  5530. ;; Fixup tag positioning
  5531. (and org-auto-align-tags (org-set-tags nil t))
  5532. (if org-adapt-indentation (org-fixup-indentation diff))))
  5533. (defun org-map-tree (fun)
  5534. "Call FUN for every heading underneath the current one."
  5535. (org-back-to-heading)
  5536. (let ((level (funcall outline-level)))
  5537. (save-excursion
  5538. (funcall fun)
  5539. (while (and (progn
  5540. (outline-next-heading)
  5541. (> (funcall outline-level) level))
  5542. (not (eobp)))
  5543. (funcall fun)))))
  5544. (defun org-map-region (fun beg end)
  5545. "Call FUN for every heading between BEG and END."
  5546. (let ((org-ignore-region t))
  5547. (save-excursion
  5548. (setq end (copy-marker end))
  5549. (goto-char beg)
  5550. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5551. (< (point) end))
  5552. (funcall fun))
  5553. (while (and (progn
  5554. (outline-next-heading)
  5555. (< (point) end))
  5556. (not (eobp)))
  5557. (funcall fun)))))
  5558. (defun org-fixup-indentation (diff)
  5559. "Change the indentation in the current entry by DIFF
  5560. However, if any line in the current entry has no indentation, or if it
  5561. would end up with no indentation after the change, nothing at all is done."
  5562. (save-excursion
  5563. (let ((end (save-excursion (outline-next-heading)
  5564. (point-marker)))
  5565. (prohibit (if (> diff 0)
  5566. "^\\S-"
  5567. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5568. col)
  5569. (unless (save-excursion (end-of-line 1)
  5570. (re-search-forward prohibit end t))
  5571. (while (and (< (point) end)
  5572. (re-search-forward "^[ \t]+" end t))
  5573. (goto-char (match-end 0))
  5574. (setq col (current-column))
  5575. (if (< diff 0) (replace-match ""))
  5576. (indent-to (+ diff col))))
  5577. (move-marker end nil))))
  5578. (defun org-convert-to-odd-levels ()
  5579. "Convert an org-mode file with all levels allowed to one with odd levels.
  5580. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5581. level 5 etc."
  5582. (interactive)
  5583. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5584. (let ((org-odd-levels-only nil) n)
  5585. (save-excursion
  5586. (goto-char (point-min))
  5587. (while (re-search-forward "^\\*\\*+ " nil t)
  5588. (setq n (- (length (match-string 0)) 2))
  5589. (while (>= (setq n (1- n)) 0)
  5590. (org-demote))
  5591. (end-of-line 1))))))
  5592. (defun org-convert-to-oddeven-levels ()
  5593. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5594. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5595. section with an even level, conversion would destroy the structure of the file. An error
  5596. is signaled in this case."
  5597. (interactive)
  5598. (goto-char (point-min))
  5599. ;; First check if there are no even levels
  5600. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5601. (org-show-context t)
  5602. (error "Not all levels are odd in this file. Conversion not possible."))
  5603. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5604. (let ((org-odd-levels-only nil) n)
  5605. (save-excursion
  5606. (goto-char (point-min))
  5607. (while (re-search-forward "^\\*\\*+ " nil t)
  5608. (setq n (/ (1- (length (match-string 0))) 2))
  5609. (while (>= (setq n (1- n)) 0)
  5610. (org-promote))
  5611. (end-of-line 1))))))
  5612. (defun org-tr-level (n)
  5613. "Make N odd if required."
  5614. (if org-odd-levels-only (1+ (/ n 2)) n))
  5615. ;;; Vertical tree motion, cutting and pasting of subtrees
  5616. (defun org-move-subtree-up (&optional arg)
  5617. "Move the current subtree up past ARG headlines of the same level."
  5618. (interactive "p")
  5619. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5620. (defun org-move-subtree-down (&optional arg)
  5621. "Move the current subtree down past ARG headlines of the same level."
  5622. (interactive "p")
  5623. (setq arg (prefix-numeric-value arg))
  5624. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5625. 'outline-get-last-sibling))
  5626. (ins-point (make-marker))
  5627. (cnt (abs arg))
  5628. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5629. ;; Select the tree
  5630. (org-back-to-heading)
  5631. (setq beg0 (point))
  5632. (save-excursion
  5633. (setq ne-beg (org-back-over-empty-lines))
  5634. (setq beg (point)))
  5635. (save-match-data
  5636. (save-excursion (outline-end-of-heading)
  5637. (setq folded (org-invisible-p)))
  5638. (outline-end-of-subtree))
  5639. (outline-next-heading)
  5640. (setq ne-end (org-back-over-empty-lines))
  5641. (setq end (point))
  5642. (goto-char beg0)
  5643. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5644. ;; include less whitespace
  5645. (save-excursion
  5646. (goto-char beg)
  5647. (forward-line (- ne-beg ne-end))
  5648. (setq beg (point))))
  5649. ;; Find insertion point, with error handling
  5650. (while (> cnt 0)
  5651. (or (and (funcall movfunc) (looking-at outline-regexp))
  5652. (progn (goto-char beg0)
  5653. (error "Cannot move past superior level or buffer limit")))
  5654. (setq cnt (1- cnt)))
  5655. (if (> arg 0)
  5656. ;; Moving forward - still need to move over subtree
  5657. (progn (org-end-of-subtree t t)
  5658. (save-excursion
  5659. (org-back-over-empty-lines)
  5660. (or (bolp) (newline)))))
  5661. (setq ne-ins (org-back-over-empty-lines))
  5662. (move-marker ins-point (point))
  5663. (setq txt (buffer-substring beg end))
  5664. (delete-region beg end)
  5665. (outline-flag-region (1- beg) beg nil)
  5666. (outline-flag-region (1- (point)) (point) nil)
  5667. (insert txt)
  5668. (or (bolp) (insert "\n"))
  5669. (setq ins-end (point))
  5670. (goto-char ins-point)
  5671. (org-skip-whitespace)
  5672. (when (and (< arg 0)
  5673. (org-first-sibling-p)
  5674. (> ne-ins ne-beg))
  5675. ;; Move whitespace back to beginning
  5676. (save-excursion
  5677. (goto-char ins-end)
  5678. (let ((kill-whole-line t))
  5679. (kill-line (- ne-ins ne-beg)) (point)))
  5680. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5681. (move-marker ins-point nil)
  5682. (org-compact-display-after-subtree-move)
  5683. (unless folded
  5684. (org-show-entry)
  5685. (show-children)
  5686. (org-cycle-hide-drawers 'children))))
  5687. (defvar org-subtree-clip ""
  5688. "Clipboard for cut and paste of subtrees.
  5689. This is actually only a copy of the kill, because we use the normal kill
  5690. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5691. (defvar org-subtree-clip-folded nil
  5692. "Was the last copied subtree folded?
  5693. This is used to fold the tree back after pasting.")
  5694. (defun org-cut-subtree (&optional n)
  5695. "Cut the current subtree into the clipboard.
  5696. With prefix arg N, cut this many sequential subtrees.
  5697. This is a short-hand for marking the subtree and then cutting it."
  5698. (interactive "p")
  5699. (org-copy-subtree n 'cut))
  5700. (defun org-copy-subtree (&optional n cut)
  5701. "Cut the current subtree into the clipboard.
  5702. With prefix arg N, cut this many sequential subtrees.
  5703. This is a short-hand for marking the subtree and then copying it.
  5704. If CUT is non-nil, actually cut the subtree."
  5705. (interactive "p")
  5706. (let (beg end folded (beg0 (point)))
  5707. (if (interactive-p)
  5708. (org-back-to-heading nil) ; take what looks like a subtree
  5709. (org-back-to-heading t)) ; take what is really there
  5710. (org-back-over-empty-lines)
  5711. (setq beg (point))
  5712. (skip-chars-forward " \t\r\n")
  5713. (save-match-data
  5714. (save-excursion (outline-end-of-heading)
  5715. (setq folded (org-invisible-p)))
  5716. (condition-case nil
  5717. (outline-forward-same-level (1- n))
  5718. (error nil))
  5719. (org-end-of-subtree t t))
  5720. (org-back-over-empty-lines)
  5721. (setq end (point))
  5722. (goto-char beg0)
  5723. (when (> end beg)
  5724. (setq org-subtree-clip-folded folded)
  5725. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5726. (setq org-subtree-clip (current-kill 0))
  5727. (message "%s: Subtree(s) with %d characters"
  5728. (if cut "Cut" "Copied")
  5729. (length org-subtree-clip)))))
  5730. (defun org-paste-subtree (&optional level tree)
  5731. "Paste the clipboard as a subtree, with modification of headline level.
  5732. The entire subtree is promoted or demoted in order to match a new headline
  5733. level. By default, the new level is derived from the visible headings
  5734. before and after the insertion point, and taken to be the inferior headline
  5735. level of the two. So if the previous visible heading is level 3 and the
  5736. next is level 4 (or vice versa), level 4 will be used for insertion.
  5737. This makes sure that the subtree remains an independent subtree and does
  5738. not swallow low level entries.
  5739. You can also force a different level, either by using a numeric prefix
  5740. argument, or by inserting the heading marker by hand. For example, if the
  5741. cursor is after \"*****\", then the tree will be shifted to level 5.
  5742. If you want to insert the tree as is, just use \\[yank].
  5743. If optional TREE is given, use this text instead of the kill ring."
  5744. (interactive "P")
  5745. (unless (org-kill-is-subtree-p tree)
  5746. (error "%s"
  5747. (substitute-command-keys
  5748. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5749. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5750. (^re (concat "^\\(" outline-regexp "\\)"))
  5751. (re (concat "\\(" outline-regexp "\\)"))
  5752. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5753. (old-level (if (string-match ^re txt)
  5754. (- (match-end 0) (match-beginning 0) 1)
  5755. -1))
  5756. (force-level (cond (level (prefix-numeric-value level))
  5757. ((string-match
  5758. ^re_ (buffer-substring (point-at-bol) (point)))
  5759. (- (match-end 1) (match-beginning 1)))
  5760. (t nil)))
  5761. (previous-level (save-excursion
  5762. (condition-case nil
  5763. (progn
  5764. (outline-previous-visible-heading 1)
  5765. (if (looking-at re)
  5766. (- (match-end 0) (match-beginning 0) 1)
  5767. 1))
  5768. (error 1))))
  5769. (next-level (save-excursion
  5770. (condition-case nil
  5771. (progn
  5772. (or (looking-at outline-regexp)
  5773. (outline-next-visible-heading 1))
  5774. (if (looking-at re)
  5775. (- (match-end 0) (match-beginning 0) 1)
  5776. 1))
  5777. (error 1))))
  5778. (new-level (or force-level (max previous-level next-level)))
  5779. (shift (if (or (= old-level -1)
  5780. (= new-level -1)
  5781. (= old-level new-level))
  5782. 0
  5783. (- new-level old-level)))
  5784. (delta (if (> shift 0) -1 1))
  5785. (func (if (> shift 0) 'org-demote 'org-promote))
  5786. (org-odd-levels-only nil)
  5787. beg end)
  5788. ;; Remove the forced level indicator
  5789. (if force-level
  5790. (delete-region (point-at-bol) (point)))
  5791. ;; Paste
  5792. (beginning-of-line 1)
  5793. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5794. (setq beg (point))
  5795. (insert-before-markers txt) ;; FIXME: correct fix????
  5796. (unless (string-match "\n\\'" txt) (insert "\n"))
  5797. (setq end (point))
  5798. (goto-char beg)
  5799. (skip-chars-forward " \t\n\r")
  5800. (setq beg (point))
  5801. ;; Shift if necessary
  5802. (unless (= shift 0)
  5803. (save-restriction
  5804. (narrow-to-region beg end)
  5805. (while (not (= shift 0))
  5806. (org-map-region func (point-min) (point-max))
  5807. (setq shift (+ delta shift)))
  5808. (goto-char (point-min))))
  5809. (when (interactive-p)
  5810. (message "Clipboard pasted as level %d subtree" new-level))
  5811. (if (and kill-ring
  5812. (eq org-subtree-clip (current-kill 0))
  5813. org-subtree-clip-folded)
  5814. ;; The tree was folded before it was killed/copied
  5815. (hide-subtree))))
  5816. (defun org-kill-is-subtree-p (&optional txt)
  5817. "Check if the current kill is an outline subtree, or a set of trees.
  5818. Returns nil if kill does not start with a headline, or if the first
  5819. headline level is not the largest headline level in the tree.
  5820. So this will actually accept several entries of equal levels as well,
  5821. which is OK for `org-paste-subtree'.
  5822. If optional TXT is given, check this string instead of the current kill."
  5823. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5824. (start-level (and kill
  5825. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5826. org-outline-regexp "\\)")
  5827. kill)
  5828. (- (match-end 2) (match-beginning 2) 1)))
  5829. (re (concat "^" org-outline-regexp))
  5830. (start (1+ (match-beginning 2))))
  5831. (if (not start-level)
  5832. (progn
  5833. nil) ;; does not even start with a heading
  5834. (catch 'exit
  5835. (while (setq start (string-match re kill (1+ start)))
  5836. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5837. (throw 'exit nil)))
  5838. t))))
  5839. (defun org-narrow-to-subtree ()
  5840. "Narrow buffer to the current subtree."
  5841. (interactive)
  5842. (save-excursion
  5843. (narrow-to-region
  5844. (progn (org-back-to-heading) (point))
  5845. (progn (org-end-of-subtree t t) (point)))))
  5846. ;;; Outline Sorting
  5847. (defun org-sort (with-case)
  5848. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5849. Optional argument WITH-CASE means sort case-sensitively."
  5850. (interactive "P")
  5851. (if (org-at-table-p)
  5852. (org-call-with-arg 'org-table-sort-lines with-case)
  5853. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5854. (defvar org-priority-regexp) ; defined later in the file
  5855. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5856. "Sort entries on a certain level of an outline tree.
  5857. If there is an active region, the entries in the region are sorted.
  5858. Else, if the cursor is before the first entry, sort the top-level items.
  5859. Else, the children of the entry at point are sorted.
  5860. Sorting can be alphabetically, numerically, and by date/time as given by
  5861. the first time stamp in the entry. The command prompts for the sorting
  5862. type unless it has been given to the function through the SORTING-TYPE
  5863. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5864. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5865. called with point at the beginning of the record. It must return either
  5866. a string or a number that should serve as the sorting key for that record.
  5867. Comparing entries ignores case by default. However, with an optional argument
  5868. WITH-CASE, the sorting considers case as well."
  5869. (interactive "P")
  5870. (let ((case-func (if with-case 'identity 'downcase))
  5871. start beg end stars re re2
  5872. txt what tmp plain-list-p)
  5873. ;; Find beginning and end of region to sort
  5874. (cond
  5875. ((org-region-active-p)
  5876. ;; we will sort the region
  5877. (setq end (region-end)
  5878. what "region")
  5879. (goto-char (region-beginning))
  5880. (if (not (org-on-heading-p)) (outline-next-heading))
  5881. (setq start (point)))
  5882. ((org-at-item-p)
  5883. ;; we will sort this plain list
  5884. (org-beginning-of-item-list) (setq start (point))
  5885. (org-end-of-item-list) (setq end (point))
  5886. (goto-char start)
  5887. (setq plain-list-p t
  5888. what "plain list"))
  5889. ((or (org-on-heading-p)
  5890. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5891. ;; we will sort the children of the current headline
  5892. (org-back-to-heading)
  5893. (setq start (point)
  5894. end (progn (org-end-of-subtree t t)
  5895. (org-back-over-empty-lines)
  5896. (point))
  5897. what "children")
  5898. (goto-char start)
  5899. (show-subtree)
  5900. (outline-next-heading))
  5901. (t
  5902. ;; we will sort the top-level entries in this file
  5903. (goto-char (point-min))
  5904. (or (org-on-heading-p) (outline-next-heading))
  5905. (setq start (point) end (point-max) what "top-level")
  5906. (goto-char start)
  5907. (show-all)))
  5908. (setq beg (point))
  5909. (if (>= beg end) (error "Nothing to sort"))
  5910. (unless plain-list-p
  5911. (looking-at "\\(\\*+\\)")
  5912. (setq stars (match-string 1)
  5913. re (concat "^" (regexp-quote stars) " +")
  5914. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5915. txt (buffer-substring beg end))
  5916. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5917. (if (and (not (equal stars "*")) (string-match re2 txt))
  5918. (error "Region to sort contains a level above the first entry")))
  5919. (unless sorting-type
  5920. (message
  5921. (if plain-list-p
  5922. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5923. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  5924. what)
  5925. (setq sorting-type (read-char-exclusive))
  5926. (and (= (downcase sorting-type) ?f)
  5927. (setq getkey-func
  5928. (completing-read "Sort using function: "
  5929. obarray 'fboundp t nil nil))
  5930. (setq getkey-func (intern getkey-func)))
  5931. (and (= (downcase sorting-type) ?r)
  5932. (setq property
  5933. (completing-read "Property: "
  5934. (mapcar 'list (org-buffer-property-keys t))
  5935. nil t))))
  5936. (message "Sorting entries...")
  5937. (save-restriction
  5938. (narrow-to-region start end)
  5939. (let ((dcst (downcase sorting-type))
  5940. (now (current-time)))
  5941. (sort-subr
  5942. (/= dcst sorting-type)
  5943. ;; This function moves to the beginning character of the "record" to
  5944. ;; be sorted.
  5945. (if plain-list-p
  5946. (lambda nil
  5947. (if (org-at-item-p) t (goto-char (point-max))))
  5948. (lambda nil
  5949. (if (re-search-forward re nil t)
  5950. (goto-char (match-beginning 0))
  5951. (goto-char (point-max)))))
  5952. ;; This function moves to the last character of the "record" being
  5953. ;; sorted.
  5954. (if plain-list-p
  5955. 'org-end-of-item
  5956. (lambda nil
  5957. (save-match-data
  5958. (condition-case nil
  5959. (outline-forward-same-level 1)
  5960. (error
  5961. (goto-char (point-max)))))))
  5962. ;; This function returns the value that gets sorted against.
  5963. (if plain-list-p
  5964. (lambda nil
  5965. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5966. (cond
  5967. ((= dcst ?n)
  5968. (string-to-number (buffer-substring (match-end 0)
  5969. (point-at-eol))))
  5970. ((= dcst ?a)
  5971. (buffer-substring (match-end 0) (point-at-eol)))
  5972. ((= dcst ?t)
  5973. (if (re-search-forward org-ts-regexp
  5974. (point-at-eol) t)
  5975. (org-time-string-to-time (match-string 0))
  5976. now))
  5977. ((= dcst ?f)
  5978. (if getkey-func
  5979. (progn
  5980. (setq tmp (funcall getkey-func))
  5981. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5982. tmp)
  5983. (error "Invalid key function `%s'" getkey-func)))
  5984. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5985. (lambda nil
  5986. (cond
  5987. ((= dcst ?n)
  5988. (if (looking-at outline-regexp)
  5989. (string-to-number (buffer-substring (match-end 0)
  5990. (point-at-eol)))
  5991. nil))
  5992. ((= dcst ?a)
  5993. (funcall case-func (buffer-substring (point-at-bol)
  5994. (point-at-eol))))
  5995. ((= dcst ?t)
  5996. (if (re-search-forward org-ts-regexp
  5997. (save-excursion
  5998. (forward-line 2)
  5999. (point)) t)
  6000. (org-time-string-to-time (match-string 0))
  6001. now))
  6002. ((= dcst ?p)
  6003. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6004. (string-to-char (match-string 2))
  6005. org-default-priority))
  6006. ((= dcst ?r)
  6007. (or (org-entry-get nil property) ""))
  6008. ((= dcst ?f)
  6009. (if getkey-func
  6010. (progn
  6011. (setq tmp (funcall getkey-func))
  6012. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6013. tmp)
  6014. (error "Invalid key function `%s'" getkey-func)))
  6015. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6016. nil
  6017. (cond
  6018. ((= dcst ?a) 'string<)
  6019. ((= dcst ?t) 'time-less-p)
  6020. (t nil)))))
  6021. (message "Sorting entries...done")))
  6022. (defun org-do-sort (table what &optional with-case sorting-type)
  6023. "Sort TABLE of WHAT according to SORTING-TYPE.
  6024. The user will be prompted for the SORTING-TYPE if the call to this
  6025. function does not specify it. WHAT is only for the prompt, to indicate
  6026. what is being sorted. The sorting key will be extracted from
  6027. the car of the elements of the table.
  6028. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6029. (unless sorting-type
  6030. (message
  6031. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6032. what)
  6033. (setq sorting-type (read-char-exclusive)))
  6034. (let ((dcst (downcase sorting-type))
  6035. extractfun comparefun)
  6036. ;; Define the appropriate functions
  6037. (cond
  6038. ((= dcst ?n)
  6039. (setq extractfun 'string-to-number
  6040. comparefun (if (= dcst sorting-type) '< '>)))
  6041. ((= dcst ?a)
  6042. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6043. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6044. comparefun (if (= dcst sorting-type)
  6045. 'string<
  6046. (lambda (a b) (and (not (string< a b))
  6047. (not (string= a b)))))))
  6048. ((= dcst ?t)
  6049. (setq extractfun
  6050. (lambda (x)
  6051. (if (string-match org-ts-regexp x)
  6052. (time-to-seconds
  6053. (org-time-string-to-time (match-string 0 x)))
  6054. 0))
  6055. comparefun (if (= dcst sorting-type) '< '>)))
  6056. (t (error "Invalid sorting type `%c'" sorting-type)))
  6057. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6058. table)
  6059. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6060. ;;;; Plain list items, including checkboxes
  6061. ;;; Plain list items
  6062. (defun org-at-item-p ()
  6063. "Is point in a line starting a hand-formatted item?"
  6064. (let ((llt org-plain-list-ordered-item-terminator))
  6065. (save-excursion
  6066. (goto-char (point-at-bol))
  6067. (looking-at
  6068. (cond
  6069. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6070. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6071. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6072. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6073. (defun org-in-item-p ()
  6074. "It the cursor inside a plain list item.
  6075. Does not have to be the first line."
  6076. (save-excursion
  6077. (condition-case nil
  6078. (progn
  6079. (org-beginning-of-item)
  6080. (org-at-item-p)
  6081. t)
  6082. (error nil))))
  6083. (defun org-insert-item (&optional checkbox)
  6084. "Insert a new item at the current level.
  6085. Return t when things worked, nil when we are not in an item."
  6086. (when (save-excursion
  6087. (condition-case nil
  6088. (progn
  6089. (org-beginning-of-item)
  6090. (org-at-item-p)
  6091. (if (org-invisible-p) (error "Invisible item"))
  6092. t)
  6093. (error nil)))
  6094. (let* ((bul (match-string 0))
  6095. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6096. (match-end 0)))
  6097. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6098. pos)
  6099. (cond
  6100. ((and (org-at-item-p) (<= (point) eow))
  6101. ;; before the bullet
  6102. (beginning-of-line 1)
  6103. (open-line (if blank 2 1)))
  6104. ((<= (point) eow)
  6105. (beginning-of-line 1))
  6106. (t (newline (if blank 2 1))))
  6107. (insert bul (if checkbox "[ ]" ""))
  6108. (just-one-space)
  6109. (setq pos (point))
  6110. (end-of-line 1)
  6111. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6112. (org-maybe-renumber-ordered-list)
  6113. (and checkbox (org-update-checkbox-count-maybe))
  6114. t))
  6115. ;;; Checkboxes
  6116. (defun org-at-item-checkbox-p ()
  6117. "Is point at a line starting a plain-list item with a checklet?"
  6118. (and (org-at-item-p)
  6119. (save-excursion
  6120. (goto-char (match-end 0))
  6121. (skip-chars-forward " \t")
  6122. (looking-at "\\[[- X]\\]"))))
  6123. (defun org-toggle-checkbox (&optional arg)
  6124. "Toggle the checkbox in the current line."
  6125. (interactive "P")
  6126. (catch 'exit
  6127. (let (beg end status (firstnew 'unknown))
  6128. (cond
  6129. ((org-region-active-p)
  6130. (setq beg (region-beginning) end (region-end)))
  6131. ((org-on-heading-p)
  6132. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6133. ((org-at-item-checkbox-p)
  6134. (let ((pos (point)))
  6135. (replace-match
  6136. (cond (arg "[-]")
  6137. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6138. (t "[ ]"))
  6139. t t)
  6140. (goto-char pos))
  6141. (throw 'exit t))
  6142. (t (error "Not at a checkbox or heading, and no active region")))
  6143. (save-excursion
  6144. (goto-char beg)
  6145. (while (< (point) end)
  6146. (when (org-at-item-checkbox-p)
  6147. (setq status (equal (match-string 0) "[X]"))
  6148. (when (eq firstnew 'unknown)
  6149. (setq firstnew (not status)))
  6150. (replace-match
  6151. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6152. (beginning-of-line 2)))))
  6153. (org-update-checkbox-count-maybe))
  6154. (defun org-update-checkbox-count-maybe ()
  6155. "Update checkbox statistics unless turned off by user."
  6156. (when org-provide-checkbox-statistics
  6157. (org-update-checkbox-count)))
  6158. (defun org-update-checkbox-count (&optional all)
  6159. "Update the checkbox statistics in the current section.
  6160. This will find all statistic cookies like [57%] and [6/12] and update them
  6161. with the current numbers. With optional prefix argument ALL, do this for
  6162. the whole buffer."
  6163. (interactive "P")
  6164. (save-excursion
  6165. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6166. (beg (condition-case nil
  6167. (progn (outline-back-to-heading) (point))
  6168. (error (point-min))))
  6169. (end (move-marker (make-marker)
  6170. (progn (outline-next-heading) (point))))
  6171. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  6172. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6173. b1 e1 f1 c-on c-off lim (cstat 0))
  6174. (when all
  6175. (goto-char (point-min))
  6176. (outline-next-heading)
  6177. (setq beg (point) end (point-max)))
  6178. (goto-char beg)
  6179. (while (re-search-forward re end t)
  6180. (setq cstat (1+ cstat)
  6181. b1 (match-beginning 0)
  6182. e1 (match-end 0)
  6183. f1 (match-beginning 1)
  6184. lim (cond
  6185. ((org-on-heading-p) (outline-next-heading) (point))
  6186. ((org-at-item-p) (org-end-of-item) (point))
  6187. (t nil))
  6188. c-on 0 c-off 0)
  6189. (goto-char e1)
  6190. (when lim
  6191. (while (re-search-forward re-box lim t)
  6192. (if (member (match-string 2) '("[ ]" "[-]"))
  6193. (setq c-off (1+ c-off))
  6194. (setq c-on (1+ c-on))))
  6195. ; (delete-region b1 e1)
  6196. (goto-char b1)
  6197. (insert (if f1
  6198. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6199. (format "[%d/%d]" c-on (+ c-on c-off))))
  6200. (and (looking-at "\\[.*?\\]")
  6201. (replace-match ""))))
  6202. (when (interactive-p)
  6203. (message "Checkbox satistics updated %s (%d places)"
  6204. (if all "in entire file" "in current outline entry") cstat)))))
  6205. (defun org-get-checkbox-statistics-face ()
  6206. "Select the face for checkbox statistics.
  6207. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6208. it will be `org-todo'."
  6209. (if (match-end 1)
  6210. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6211. (if (and (> (match-end 2) (match-beginning 2))
  6212. (equal (match-string 2) (match-string 3)))
  6213. 'org-done
  6214. 'org-todo)))
  6215. (defun org-get-indentation (&optional line)
  6216. "Get the indentation of the current line, interpreting tabs.
  6217. When LINE is given, assume it represents a line and compute its indentation."
  6218. (if line
  6219. (if (string-match "^ *" (org-remove-tabs line))
  6220. (match-end 0))
  6221. (save-excursion
  6222. (beginning-of-line 1)
  6223. (skip-chars-forward " \t")
  6224. (current-column))))
  6225. (defun org-remove-tabs (s &optional width)
  6226. "Replace tabulators in S with spaces.
  6227. Assumes that s is a single line, starting in column 0."
  6228. (setq width (or width tab-width))
  6229. (while (string-match "\t" s)
  6230. (setq s (replace-match
  6231. (make-string
  6232. (- (* width (/ (+ (match-beginning 0) width) width))
  6233. (match-beginning 0)) ?\ )
  6234. t t s)))
  6235. s)
  6236. (defun org-fix-indentation (line ind)
  6237. "Fix indentation in LINE.
  6238. IND is a cons cell with target and minimum indentation.
  6239. If the current indenation in LINE is smaller than the minimum,
  6240. leave it alone. If it is larger than ind, set it to the target."
  6241. (let* ((l (org-remove-tabs line))
  6242. (i (org-get-indentation l))
  6243. (i1 (car ind)) (i2 (cdr ind)))
  6244. (if (>= i i2) (setq l (substring line i2)))
  6245. (if (> i1 0)
  6246. (concat (make-string i1 ?\ ) l)
  6247. l)))
  6248. (defcustom org-empty-line-terminates-plain-lists nil
  6249. "Non-nil means, an empty line ends all plain list levels.
  6250. When nil, empty lines are part of the preceeding item."
  6251. :group 'org-plain-lists
  6252. :type 'boolean)
  6253. (defun org-beginning-of-item ()
  6254. "Go to the beginning of the current hand-formatted item.
  6255. If the cursor is not in an item, throw an error."
  6256. (interactive)
  6257. (let ((pos (point))
  6258. (limit (save-excursion
  6259. (condition-case nil
  6260. (progn
  6261. (org-back-to-heading)
  6262. (beginning-of-line 2) (point))
  6263. (error (point-min)))))
  6264. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6265. ind ind1)
  6266. (if (org-at-item-p)
  6267. (beginning-of-line 1)
  6268. (beginning-of-line 1)
  6269. (skip-chars-forward " \t")
  6270. (setq ind (current-column))
  6271. (if (catch 'exit
  6272. (while t
  6273. (beginning-of-line 0)
  6274. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6275. (if (looking-at "[ \t]*$")
  6276. (setq ind1 ind-empty)
  6277. (skip-chars-forward " \t")
  6278. (setq ind1 (current-column)))
  6279. (if (< ind1 ind)
  6280. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6281. nil
  6282. (goto-char pos)
  6283. (error "Not in an item")))))
  6284. (defun org-end-of-item ()
  6285. "Go to the end of the current hand-formatted item.
  6286. If the cursor is not in an item, throw an error."
  6287. (interactive)
  6288. (let* ((pos (point))
  6289. ind1
  6290. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6291. (limit (save-excursion (outline-next-heading) (point)))
  6292. (ind (save-excursion
  6293. (org-beginning-of-item)
  6294. (skip-chars-forward " \t")
  6295. (current-column)))
  6296. (end (catch 'exit
  6297. (while t
  6298. (beginning-of-line 2)
  6299. (if (eobp) (throw 'exit (point)))
  6300. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6301. (if (looking-at "[ \t]*$")
  6302. (setq ind1 ind-empty)
  6303. (skip-chars-forward " \t")
  6304. (setq ind1 (current-column)))
  6305. (if (<= ind1 ind)
  6306. (throw 'exit (point-at-bol)))))))
  6307. (if end
  6308. (goto-char end)
  6309. (goto-char pos)
  6310. (error "Not in an item"))))
  6311. (defun org-next-item ()
  6312. "Move to the beginning of the next item in the current plain list.
  6313. Error if not at a plain list, or if this is the last item in the list."
  6314. (interactive)
  6315. (let (ind ind1 (pos (point)))
  6316. (org-beginning-of-item)
  6317. (setq ind (org-get-indentation))
  6318. (org-end-of-item)
  6319. (setq ind1 (org-get-indentation))
  6320. (unless (and (org-at-item-p) (= ind ind1))
  6321. (goto-char pos)
  6322. (error "On last item"))))
  6323. (defun org-previous-item ()
  6324. "Move to the beginning of the previous item in the current plain list.
  6325. Error if not at a plain list, or if this is the first item in the list."
  6326. (interactive)
  6327. (let (beg ind ind1 (pos (point)))
  6328. (org-beginning-of-item)
  6329. (setq beg (point))
  6330. (setq ind (org-get-indentation))
  6331. (goto-char beg)
  6332. (catch 'exit
  6333. (while t
  6334. (beginning-of-line 0)
  6335. (if (looking-at "[ \t]*$")
  6336. nil
  6337. (if (<= (setq ind1 (org-get-indentation)) ind)
  6338. (throw 'exit t)))))
  6339. (condition-case nil
  6340. (if (or (not (org-at-item-p))
  6341. (< ind1 (1- ind)))
  6342. (error "")
  6343. (org-beginning-of-item))
  6344. (error (goto-char pos)
  6345. (error "On first item")))))
  6346. (defun org-first-list-item-p ()
  6347. "Is this heading the item in a plain list?"
  6348. (unless (org-at-item-p)
  6349. (error "Not at a plain list item"))
  6350. (org-beginning-of-item)
  6351. (= (point) (save-excursion (org-beginning-of-item-list))))
  6352. (defun org-move-item-down ()
  6353. "Move the plain list item at point down, i.e. swap with following item.
  6354. Subitems (items with larger indentation) are considered part of the item,
  6355. so this really moves item trees."
  6356. (interactive)
  6357. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6358. (org-beginning-of-item)
  6359. (setq beg0 (point))
  6360. (save-excursion
  6361. (setq ne-beg (org-back-over-empty-lines))
  6362. (setq beg (point)))
  6363. (goto-char beg0)
  6364. (setq ind (org-get-indentation))
  6365. (org-end-of-item)
  6366. (setq end0 (point))
  6367. (setq ind1 (org-get-indentation))
  6368. (setq ne-end (org-back-over-empty-lines))
  6369. (setq end (point))
  6370. (goto-char beg0)
  6371. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6372. ;; include less whitespace
  6373. (save-excursion
  6374. (goto-char beg)
  6375. (forward-line (- ne-beg ne-end))
  6376. (setq beg (point))))
  6377. (goto-char end0)
  6378. (if (and (org-at-item-p) (= ind ind1))
  6379. (progn
  6380. (org-end-of-item)
  6381. (org-back-over-empty-lines)
  6382. (setq txt (buffer-substring beg end))
  6383. (save-excursion
  6384. (delete-region beg end))
  6385. (setq pos (point))
  6386. (insert txt)
  6387. (goto-char pos) (org-skip-whitespace)
  6388. (org-maybe-renumber-ordered-list))
  6389. (goto-char pos)
  6390. (error "Cannot move this item further down"))))
  6391. (defun org-move-item-up (arg)
  6392. "Move the plain list item at point up, i.e. swap with previous item.
  6393. Subitems (items with larger indentation) are considered part of the item,
  6394. so this really moves item trees."
  6395. (interactive "p")
  6396. (let (beg beg0 end end0 ind ind1 (pos (point)) txt
  6397. ne-beg ne-end ne-ins ins-end)
  6398. (org-beginning-of-item)
  6399. (setq beg0 (point))
  6400. (setq ind (org-get-indentation))
  6401. (save-excursion
  6402. (setq ne-beg (org-back-over-empty-lines))
  6403. (setq beg (point)))
  6404. (goto-char beg0)
  6405. (org-end-of-item)
  6406. (setq ne-end (org-back-over-empty-lines))
  6407. (setq end (point))
  6408. (goto-char beg0)
  6409. (catch 'exit
  6410. (while t
  6411. (beginning-of-line 0)
  6412. (if (looking-at "[ \t]*$")
  6413. (if org-empty-line-terminates-plain-lists
  6414. (progn
  6415. (goto-char pos)
  6416. (error "Cannot move this item further up"))
  6417. nil)
  6418. (if (<= (setq ind1 (org-get-indentation)) ind)
  6419. (throw 'exit t)))))
  6420. (condition-case nil
  6421. (org-beginning-of-item)
  6422. (error (goto-char beg)
  6423. (error "Cannot move this item further up")))
  6424. (setq ind1 (org-get-indentation))
  6425. (if (and (org-at-item-p) (= ind ind1))
  6426. (progn
  6427. (setq ne-ins (org-back-over-empty-lines))
  6428. (setq txt (buffer-substring beg end))
  6429. (save-excursion
  6430. (delete-region beg end))
  6431. (setq pos (point))
  6432. (insert txt)
  6433. (setq ins-end (point))
  6434. (goto-char pos) (org-skip-whitespace)
  6435. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6436. ;; Move whitespace back to beginning
  6437. (save-excursion
  6438. (goto-char ins-end)
  6439. (let ((kill-whole-line t))
  6440. (kill-line (- ne-ins ne-beg)) (point)))
  6441. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6442. (org-maybe-renumber-ordered-list))
  6443. (goto-char pos)
  6444. (error "Cannot move this item further up"))))
  6445. (defun org-maybe-renumber-ordered-list ()
  6446. "Renumber the ordered list at point if setup allows it.
  6447. This tests the user option `org-auto-renumber-ordered-lists' before
  6448. doing the renumbering."
  6449. (interactive)
  6450. (when (and org-auto-renumber-ordered-lists
  6451. (org-at-item-p))
  6452. (if (match-beginning 3)
  6453. (org-renumber-ordered-list 1)
  6454. (org-fix-bullet-type))))
  6455. (defun org-maybe-renumber-ordered-list-safe ()
  6456. (condition-case nil
  6457. (save-excursion
  6458. (org-maybe-renumber-ordered-list))
  6459. (error nil)))
  6460. (defun org-cycle-list-bullet (&optional which)
  6461. "Cycle through the different itemize/enumerate bullets.
  6462. This cycle the entire list level through the sequence:
  6463. `-' -> `+' -> `*' -> `1.' -> `1)'
  6464. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6465. 0 meand `-', 1 means `+' etc."
  6466. (interactive "P")
  6467. (org-preserve-lc
  6468. (org-beginning-of-item-list)
  6469. (org-at-item-p)
  6470. (beginning-of-line 1)
  6471. (let ((current (match-string 0))
  6472. (prevp (eq which 'previous))
  6473. new)
  6474. (setq new (cond
  6475. ((and (numberp which)
  6476. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6477. ((string-match "-" current) (if prevp "1)" "+"))
  6478. ((string-match "\\+" current)
  6479. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6480. ((string-match "\\*" current) (if prevp "+" "1."))
  6481. ((string-match "\\." current) (if prevp "*" "1)"))
  6482. ((string-match ")" current) (if prevp "1." "-"))
  6483. (t (error "This should not happen"))))
  6484. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6485. (org-fix-bullet-type)
  6486. (org-maybe-renumber-ordered-list))))
  6487. (defun org-get-string-indentation (s)
  6488. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6489. (let ((n -1) (i 0) (w tab-width) c)
  6490. (catch 'exit
  6491. (while (< (setq n (1+ n)) (length s))
  6492. (setq c (aref s n))
  6493. (cond ((= c ?\ ) (setq i (1+ i)))
  6494. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6495. (t (throw 'exit t)))))
  6496. i))
  6497. (defun org-renumber-ordered-list (arg)
  6498. "Renumber an ordered plain list.
  6499. Cursor needs to be in the first line of an item, the line that starts
  6500. with something like \"1.\" or \"2)\"."
  6501. (interactive "p")
  6502. (unless (and (org-at-item-p)
  6503. (match-beginning 3))
  6504. (error "This is not an ordered list"))
  6505. (let ((line (org-current-line))
  6506. (col (current-column))
  6507. (ind (org-get-string-indentation
  6508. (buffer-substring (point-at-bol) (match-beginning 3))))
  6509. ;; (term (substring (match-string 3) -1))
  6510. ind1 (n (1- arg))
  6511. fmt)
  6512. ;; find where this list begins
  6513. (org-beginning-of-item-list)
  6514. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6515. (setq fmt (concat "%d" (match-string 1)))
  6516. (beginning-of-line 0)
  6517. ;; walk forward and replace these numbers
  6518. (catch 'exit
  6519. (while t
  6520. (catch 'next
  6521. (beginning-of-line 2)
  6522. (if (eobp) (throw 'exit nil))
  6523. (if (looking-at "[ \t]*$") (throw 'next nil))
  6524. (skip-chars-forward " \t") (setq ind1 (current-column))
  6525. (if (> ind1 ind) (throw 'next t))
  6526. (if (< ind1 ind) (throw 'exit t))
  6527. (if (not (org-at-item-p)) (throw 'exit nil))
  6528. (delete-region (match-beginning 2) (match-end 2))
  6529. (goto-char (match-beginning 2))
  6530. (insert (format fmt (setq n (1+ n)))))))
  6531. (goto-line line)
  6532. (move-to-column col)))
  6533. (defun org-fix-bullet-type ()
  6534. "Make sure all items in this list have the same bullet as the firsst item."
  6535. (interactive)
  6536. (unless (org-at-item-p) (error "This is not a list"))
  6537. (let ((line (org-current-line))
  6538. (col (current-column))
  6539. (ind (current-indentation))
  6540. ind1 bullet)
  6541. ;; find where this list begins
  6542. (org-beginning-of-item-list)
  6543. (beginning-of-line 1)
  6544. ;; find out what the bullet type is
  6545. (looking-at "[ \t]*\\(\\S-+\\)")
  6546. (setq bullet (match-string 1))
  6547. ;; walk forward and replace these numbers
  6548. (beginning-of-line 0)
  6549. (catch 'exit
  6550. (while t
  6551. (catch 'next
  6552. (beginning-of-line 2)
  6553. (if (eobp) (throw 'exit nil))
  6554. (if (looking-at "[ \t]*$") (throw 'next nil))
  6555. (skip-chars-forward " \t") (setq ind1 (current-column))
  6556. (if (> ind1 ind) (throw 'next t))
  6557. (if (< ind1 ind) (throw 'exit t))
  6558. (if (not (org-at-item-p)) (throw 'exit nil))
  6559. (skip-chars-forward " \t")
  6560. (looking-at "\\S-+")
  6561. (replace-match bullet))))
  6562. (goto-line line)
  6563. (move-to-column col)
  6564. (if (string-match "[0-9]" bullet)
  6565. (org-renumber-ordered-list 1))))
  6566. (defun org-beginning-of-item-list ()
  6567. "Go to the beginning of the current item list.
  6568. I.e. to the first item in this list."
  6569. (interactive)
  6570. (org-beginning-of-item)
  6571. (let ((pos (point-at-bol))
  6572. (ind (org-get-indentation))
  6573. ind1)
  6574. ;; find where this list begins
  6575. (catch 'exit
  6576. (while t
  6577. (catch 'next
  6578. (beginning-of-line 0)
  6579. (if (looking-at "[ \t]*$")
  6580. (throw (if (bobp) 'exit 'next) t))
  6581. (skip-chars-forward " \t") (setq ind1 (current-column))
  6582. (if (or (< ind1 ind)
  6583. (and (= ind1 ind)
  6584. (not (org-at-item-p)))
  6585. (bobp))
  6586. (throw 'exit t)
  6587. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6588. (goto-char pos)))
  6589. (defun org-end-of-item-list ()
  6590. "Go to the end of the current item list.
  6591. I.e. to the text after the last item."
  6592. (interactive)
  6593. (org-beginning-of-item)
  6594. (let ((pos (point-at-bol))
  6595. (ind (org-get-indentation))
  6596. ind1)
  6597. ;; find where this list begins
  6598. (catch 'exit
  6599. (while t
  6600. (catch 'next
  6601. (beginning-of-line 2)
  6602. (if (looking-at "[ \t]*$")
  6603. (throw (if (eobp) 'exit 'next) t))
  6604. (skip-chars-forward " \t") (setq ind1 (current-column))
  6605. (if (or (< ind1 ind)
  6606. (and (= ind1 ind)
  6607. (not (org-at-item-p)))
  6608. (eobp))
  6609. (progn
  6610. (setq pos (point-at-bol))
  6611. (throw 'exit t))))))
  6612. (goto-char pos)))
  6613. (defvar org-last-indent-begin-marker (make-marker))
  6614. (defvar org-last-indent-end-marker (make-marker))
  6615. (defun org-outdent-item (arg)
  6616. "Outdent a local list item."
  6617. (interactive "p")
  6618. (org-indent-item (- arg)))
  6619. (defun org-indent-item (arg)
  6620. "Indent a local list item."
  6621. (interactive "p")
  6622. (unless (org-at-item-p)
  6623. (error "Not on an item"))
  6624. (save-excursion
  6625. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6626. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6627. (setq beg org-last-indent-begin-marker
  6628. end org-last-indent-end-marker)
  6629. (org-beginning-of-item)
  6630. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6631. (org-end-of-item)
  6632. (setq end (move-marker org-last-indent-end-marker (point))))
  6633. (goto-char beg)
  6634. (setq tmp (org-item-indent-positions)
  6635. ind (car tmp)
  6636. ind-down (nth 2 tmp)
  6637. ind-up (nth 1 tmp)
  6638. delta (if (> arg 0)
  6639. (if ind-down (- ind-down ind) 2)
  6640. (if ind-up (- ind-up ind) -2)))
  6641. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6642. (while (< (point) end)
  6643. (beginning-of-line 1)
  6644. (skip-chars-forward " \t") (setq ind1 (current-column))
  6645. (delete-region (point-at-bol) (point))
  6646. (or (eolp) (indent-to-column (+ ind1 delta)))
  6647. (beginning-of-line 2))))
  6648. (org-fix-bullet-type)
  6649. (org-maybe-renumber-ordered-list-safe)
  6650. (save-excursion
  6651. (beginning-of-line 0)
  6652. (condition-case nil (org-beginning-of-item) (error nil))
  6653. (org-maybe-renumber-ordered-list-safe)))
  6654. (defun org-item-indent-positions ()
  6655. "Return indentation for plain list items.
  6656. This returns a list with three values: The current indentation, the
  6657. parent indentation and the indentation a child should habe.
  6658. Assumes cursor in item line."
  6659. (let* ((bolpos (point-at-bol))
  6660. (ind (org-get-indentation))
  6661. ind-down ind-up pos)
  6662. (save-excursion
  6663. (org-beginning-of-item-list)
  6664. (skip-chars-backward "\n\r \t")
  6665. (when (org-in-item-p)
  6666. (org-beginning-of-item)
  6667. (setq ind-up (org-get-indentation))))
  6668. (setq pos (point))
  6669. (save-excursion
  6670. (cond
  6671. ((and (condition-case nil (progn (org-previous-item) t)
  6672. (error nil))
  6673. (or (forward-char 1) t)
  6674. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6675. (setq ind-down (org-get-indentation)))
  6676. ((and (goto-char pos)
  6677. (org-at-item-p))
  6678. (goto-char (match-end 0))
  6679. (skip-chars-forward " \t")
  6680. (setq ind-down (current-column)))))
  6681. (list ind ind-up ind-down)))
  6682. ;;; The orgstruct minor mode
  6683. ;; Define a minor mode which can be used in other modes in order to
  6684. ;; integrate the org-mode structure editing commands.
  6685. ;; This is really a hack, because the org-mode structure commands use
  6686. ;; keys which normally belong to the major mode. Here is how it
  6687. ;; works: The minor mode defines all the keys necessary to operate the
  6688. ;; structure commands, but wraps the commands into a function which
  6689. ;; tests if the cursor is currently at a headline or a plain list
  6690. ;; item. If that is the case, the structure command is used,
  6691. ;; temporarily setting many Org-mode variables like regular
  6692. ;; expressions for filling etc. However, when any of those keys is
  6693. ;; used at a different location, function uses `key-binding' to look
  6694. ;; up if the key has an associated command in another currently active
  6695. ;; keymap (minor modes, major mode, global), and executes that
  6696. ;; command. There might be problems if any of the keys is otherwise
  6697. ;; used as a prefix key.
  6698. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6699. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6700. ;; addresses this by checking explicitly for both bindings.
  6701. (defvar orgstruct-mode-map (make-sparse-keymap)
  6702. "Keymap for the minor `orgstruct-mode'.")
  6703. (defvar org-local-vars nil
  6704. "List of local variables, for use by `orgstruct-mode'")
  6705. ;;;###autoload
  6706. (define-minor-mode orgstruct-mode
  6707. "Toggle the minor more `orgstruct-mode'.
  6708. This mode is for using Org-mode structure commands in other modes.
  6709. The following key behave as if Org-mode was active, if the cursor
  6710. is on a headline, or on a plain list item (both in the definition
  6711. of Org-mode).
  6712. M-up Move entry/item up
  6713. M-down Move entry/item down
  6714. M-left Promote
  6715. M-right Demote
  6716. M-S-up Move entry/item up
  6717. M-S-down Move entry/item down
  6718. M-S-left Promote subtree
  6719. M-S-right Demote subtree
  6720. M-q Fill paragraph and items like in Org-mode
  6721. C-c ^ Sort entries
  6722. C-c - Cycle list bullet
  6723. TAB Cycle item visibility
  6724. M-RET Insert new heading/item
  6725. S-M-RET Insert new TODO heading / Chekbox item
  6726. C-c C-c Set tags / toggle checkbox"
  6727. nil " OrgStruct" nil
  6728. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6729. ;;;###autoload
  6730. (defun turn-on-orgstruct ()
  6731. "Unconditionally turn on `orgstruct-mode'."
  6732. (orgstruct-mode 1))
  6733. ;;;###autoload
  6734. (defun turn-on-orgstruct++ ()
  6735. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6736. In addition to setting orgstruct-mode, this also exports all indentation and
  6737. autofilling variables from org-mode into the buffer. Note that turning
  6738. off orgstruct-mode will *not* remove these additonal settings."
  6739. (orgstruct-mode 1)
  6740. (let (var val)
  6741. (mapc
  6742. (lambda (x)
  6743. (when (string-match
  6744. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6745. (symbol-name (car x)))
  6746. (setq var (car x) val (nth 1 x))
  6747. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6748. org-local-vars)))
  6749. (defun orgstruct-error ()
  6750. "Error when there is no default binding for a structure key."
  6751. (interactive)
  6752. (error "This key has no function outside structure elements"))
  6753. (defun orgstruct-setup ()
  6754. "Setup orgstruct keymaps."
  6755. (let ((nfunc 0)
  6756. (bindings
  6757. (list
  6758. '([(meta up)] org-metaup)
  6759. '([(meta down)] org-metadown)
  6760. '([(meta left)] org-metaleft)
  6761. '([(meta right)] org-metaright)
  6762. '([(meta shift up)] org-shiftmetaup)
  6763. '([(meta shift down)] org-shiftmetadown)
  6764. '([(meta shift left)] org-shiftmetaleft)
  6765. '([(meta shift right)] org-shiftmetaright)
  6766. '([(shift up)] org-shiftup)
  6767. '([(shift down)] org-shiftdown)
  6768. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6769. '("\M-q" fill-paragraph)
  6770. '("\C-c^" org-sort)
  6771. '("\C-c-" org-cycle-list-bullet)))
  6772. elt key fun cmd)
  6773. (while (setq elt (pop bindings))
  6774. (setq nfunc (1+ nfunc))
  6775. (setq key (org-key (car elt))
  6776. fun (nth 1 elt)
  6777. cmd (orgstruct-make-binding fun nfunc key))
  6778. (org-defkey orgstruct-mode-map key cmd))
  6779. ;; Special treatment needed for TAB and RET
  6780. (org-defkey orgstruct-mode-map [(tab)]
  6781. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6782. (org-defkey orgstruct-mode-map "\C-i"
  6783. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6784. (org-defkey orgstruct-mode-map "\M-\C-m"
  6785. (orgstruct-make-binding 'org-insert-heading 105
  6786. "\M-\C-m" [(meta return)]))
  6787. (org-defkey orgstruct-mode-map [(meta return)]
  6788. (orgstruct-make-binding 'org-insert-heading 106
  6789. [(meta return)] "\M-\C-m"))
  6790. (org-defkey orgstruct-mode-map [(shift meta return)]
  6791. (orgstruct-make-binding 'org-insert-todo-heading 107
  6792. [(meta return)] "\M-\C-m"))
  6793. (unless org-local-vars
  6794. (setq org-local-vars (org-get-local-variables)))
  6795. t))
  6796. (defun orgstruct-make-binding (fun n &rest keys)
  6797. "Create a function for binding in the structure minor mode.
  6798. FUN is the command to call inside a table. N is used to create a unique
  6799. command name. KEYS are keys that should be checked in for a command
  6800. to execute outside of tables."
  6801. (eval
  6802. (list 'defun
  6803. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6804. '(arg)
  6805. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6806. "Outside of structure, run the binding of `"
  6807. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6808. "'.")
  6809. '(interactive "p")
  6810. (list 'if
  6811. '(org-context-p 'headline 'item)
  6812. (list 'org-run-like-in-org-mode (list 'quote fun))
  6813. (list 'let '(orgstruct-mode)
  6814. (list 'call-interactively
  6815. (append '(or)
  6816. (mapcar (lambda (k)
  6817. (list 'key-binding k))
  6818. keys)
  6819. '('orgstruct-error))))))))
  6820. (defun org-context-p (&rest contexts)
  6821. "Check if local context is and of CONTEXTS.
  6822. Possible values in the list of contexts are `table', `headline', and `item'."
  6823. (let ((pos (point)))
  6824. (goto-char (point-at-bol))
  6825. (prog1 (or (and (memq 'table contexts)
  6826. (looking-at "[ \t]*|"))
  6827. (and (memq 'headline contexts)
  6828. (looking-at "\\*+"))
  6829. (and (memq 'item contexts)
  6830. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6831. (goto-char pos))))
  6832. (defun org-get-local-variables ()
  6833. "Return a list of all local variables in an org-mode buffer."
  6834. (let (varlist)
  6835. (with-current-buffer (get-buffer-create "*Org tmp*")
  6836. (erase-buffer)
  6837. (org-mode)
  6838. (setq varlist (buffer-local-variables)))
  6839. (kill-buffer "*Org tmp*")
  6840. (delq nil
  6841. (mapcar
  6842. (lambda (x)
  6843. (setq x
  6844. (if (symbolp x)
  6845. (list x)
  6846. (list (car x) (list 'quote (cdr x)))))
  6847. (if (string-match
  6848. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6849. (symbol-name (car x)))
  6850. x nil))
  6851. varlist))))
  6852. ;;;###autoload
  6853. (defun org-run-like-in-org-mode (cmd)
  6854. (unless org-local-vars
  6855. (setq org-local-vars (org-get-local-variables)))
  6856. (eval (list 'let org-local-vars
  6857. (list 'call-interactively (list 'quote cmd)))))
  6858. ;;;; Archiving
  6859. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6860. (defun org-archive-subtree (&optional find-done)
  6861. "Move the current subtree to the archive.
  6862. The archive can be a certain top-level heading in the current file, or in
  6863. a different file. The tree will be moved to that location, the subtree
  6864. heading be marked DONE, and the current time will be added.
  6865. When called with prefix argument FIND-DONE, find whole trees without any
  6866. open TODO items and archive them (after getting confirmation from the user).
  6867. If the cursor is not at a headline when this comand is called, try all level
  6868. 1 trees. If the cursor is on a headline, only try the direct children of
  6869. this heading."
  6870. (interactive "P")
  6871. (if find-done
  6872. (org-archive-all-done)
  6873. ;; Save all relevant TODO keyword-relatex variables
  6874. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6875. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6876. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6877. (tr-org-done-keywords org-done-keywords)
  6878. (tr-org-todo-regexp org-todo-regexp)
  6879. (tr-org-todo-line-regexp org-todo-line-regexp)
  6880. (tr-org-odd-levels-only org-odd-levels-only)
  6881. (this-buffer (current-buffer))
  6882. (org-archive-location org-archive-location)
  6883. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6884. ;; start of variables that will be used for saving context
  6885. ;; The compiler complains about them - keep them anyway!
  6886. (file (abbreviate-file-name (buffer-file-name)))
  6887. (time (format-time-string
  6888. (substring (cdr org-time-stamp-formats) 1 -1)
  6889. (current-time)))
  6890. afile heading buffer level newfile-p
  6891. category todo priority
  6892. ;; start of variables that will be used for savind context
  6893. ltags itags prop)
  6894. ;; Try to find a local archive location
  6895. (save-excursion
  6896. (save-restriction
  6897. (widen)
  6898. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6899. (if (and prop (string-match "\\S-" prop))
  6900. (setq org-archive-location prop)
  6901. (if (or (re-search-backward re nil t)
  6902. (re-search-forward re nil t))
  6903. (setq org-archive-location (match-string 1))))))
  6904. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6905. (progn
  6906. (setq afile (format (match-string 1 org-archive-location)
  6907. (file-name-nondirectory buffer-file-name))
  6908. heading (match-string 2 org-archive-location)))
  6909. (error "Invalid `org-archive-location'"))
  6910. (if (> (length afile) 0)
  6911. (setq newfile-p (not (file-exists-p afile))
  6912. buffer (find-file-noselect afile))
  6913. (setq buffer (current-buffer)))
  6914. (unless buffer
  6915. (error "Cannot access file \"%s\"" afile))
  6916. (if (and (> (length heading) 0)
  6917. (string-match "^\\*+" heading))
  6918. (setq level (match-end 0))
  6919. (setq heading nil level 0))
  6920. (save-excursion
  6921. (org-back-to-heading t)
  6922. ;; Get context information that will be lost by moving the tree
  6923. (org-refresh-category-properties)
  6924. (setq category (org-get-category)
  6925. todo (and (looking-at org-todo-line-regexp)
  6926. (match-string 2))
  6927. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  6928. ltags (org-get-tags)
  6929. itags (org-delete-all ltags (org-get-tags-at)))
  6930. (setq ltags (mapconcat 'identity ltags " ")
  6931. itags (mapconcat 'identity itags " "))
  6932. ;; We first only copy, in case something goes wrong
  6933. ;; we need to protect this-command, to avoid kill-region sets it,
  6934. ;; which would lead to duplication of subtrees
  6935. (let (this-command) (org-copy-subtree))
  6936. (set-buffer buffer)
  6937. ;; Enforce org-mode for the archive buffer
  6938. (if (not (org-mode-p))
  6939. ;; Force the mode for future visits.
  6940. (let ((org-insert-mode-line-in-empty-file t)
  6941. (org-inhibit-startup t))
  6942. (call-interactively 'org-mode)))
  6943. (when newfile-p
  6944. (goto-char (point-max))
  6945. (insert (format "\nArchived entries from file %s\n\n"
  6946. (buffer-file-name this-buffer))))
  6947. ;; Force the TODO keywords of the original buffer
  6948. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  6949. (org-todo-keywords-1 tr-org-todo-keywords-1)
  6950. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  6951. (org-done-keywords tr-org-done-keywords)
  6952. (org-todo-regexp tr-org-todo-regexp)
  6953. (org-todo-line-regexp tr-org-todo-line-regexp)
  6954. (org-odd-levels-only
  6955. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  6956. org-odd-levels-only
  6957. tr-org-odd-levels-only)))
  6958. (goto-char (point-min))
  6959. (if heading
  6960. (progn
  6961. (if (re-search-forward
  6962. (concat "^" (regexp-quote heading)
  6963. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  6964. nil t)
  6965. (goto-char (match-end 0))
  6966. ;; Heading not found, just insert it at the end
  6967. (goto-char (point-max))
  6968. (or (bolp) (insert "\n"))
  6969. (insert "\n" heading "\n")
  6970. (end-of-line 0))
  6971. ;; Make the subtree visible
  6972. (show-subtree)
  6973. (org-end-of-subtree t)
  6974. (skip-chars-backward " \t\r\n")
  6975. (and (looking-at "[ \t\r\n]*")
  6976. (replace-match "\n\n")))
  6977. ;; No specific heading, just go to end of file.
  6978. (goto-char (point-max)) (insert "\n"))
  6979. ;; Paste
  6980. (org-paste-subtree (org-get-legal-level level 1))
  6981. ;; Mark the entry as done
  6982. (when (and org-archive-mark-done
  6983. (looking-at org-todo-line-regexp)
  6984. (or (not (match-end 2))
  6985. (not (member (match-string 2) org-done-keywords))))
  6986. (let (org-log-done)
  6987. (org-todo
  6988. (car (or (member org-archive-mark-done org-done-keywords)
  6989. org-done-keywords)))))
  6990. ;; Add the context info
  6991. (when org-archive-save-context-info
  6992. (let ((l org-archive-save-context-info) e n v)
  6993. (while (setq e (pop l))
  6994. (when (and (setq v (symbol-value e))
  6995. (stringp v) (string-match "\\S-" v))
  6996. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  6997. (org-entry-put (point) n v)))))
  6998. ;; Save the buffer, if it is not the same buffer.
  6999. (if (not (eq this-buffer buffer)) (save-buffer))))
  7000. ;; Here we are back in the original buffer. Everything seems to have
  7001. ;; worked. So now cut the tree and finish up.
  7002. (let (this-command) (org-cut-subtree))
  7003. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7004. (message "Subtree archived %s"
  7005. (if (eq this-buffer buffer)
  7006. (concat "under heading: " heading)
  7007. (concat "in file: " (abbreviate-file-name afile)))))))
  7008. (defun org-refresh-category-properties ()
  7009. "Refresh category text properties in teh buffer."
  7010. (let ((def-cat (cond
  7011. ((null org-category)
  7012. (if buffer-file-name
  7013. (file-name-sans-extension
  7014. (file-name-nondirectory buffer-file-name))
  7015. "???"))
  7016. ((symbolp org-category) (symbol-name org-category))
  7017. (t org-category)))
  7018. beg end cat pos optionp)
  7019. (org-unmodified
  7020. (save-excursion
  7021. (save-restriction
  7022. (widen)
  7023. (goto-char (point-min))
  7024. (put-text-property (point) (point-max) 'org-category def-cat)
  7025. (while (re-search-forward
  7026. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7027. (setq pos (match-end 0)
  7028. optionp (equal (char-after (match-beginning 0)) ?#)
  7029. cat (org-trim (match-string 2)))
  7030. (if optionp
  7031. (setq beg (point-at-bol) end (point-max))
  7032. (org-back-to-heading t)
  7033. (setq beg (point) end (org-end-of-subtree t t)))
  7034. (put-text-property beg end 'org-category cat)
  7035. (goto-char pos)))))))
  7036. (defun org-archive-all-done (&optional tag)
  7037. "Archive sublevels of the current tree without open TODO items.
  7038. If the cursor is not on a headline, try all level 1 trees. If
  7039. it is on a headline, try all direct children.
  7040. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7041. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7042. (rea (concat ".*:" org-archive-tag ":"))
  7043. (begm (make-marker))
  7044. (endm (make-marker))
  7045. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7046. "Move subtree to archive (no open TODO items)? "))
  7047. beg end (cntarch 0))
  7048. (if (org-on-heading-p)
  7049. (progn
  7050. (setq re1 (concat "^" (regexp-quote
  7051. (make-string
  7052. (1+ (- (match-end 0) (match-beginning 0)))
  7053. ?*))
  7054. " "))
  7055. (move-marker begm (point))
  7056. (move-marker endm (org-end-of-subtree t)))
  7057. (setq re1 "^* ")
  7058. (move-marker begm (point-min))
  7059. (move-marker endm (point-max)))
  7060. (save-excursion
  7061. (goto-char begm)
  7062. (while (re-search-forward re1 endm t)
  7063. (setq beg (match-beginning 0)
  7064. end (save-excursion (org-end-of-subtree t) (point)))
  7065. (goto-char beg)
  7066. (if (re-search-forward re end t)
  7067. (goto-char end)
  7068. (goto-char beg)
  7069. (if (and (or (not tag) (not (looking-at rea)))
  7070. (y-or-n-p question))
  7071. (progn
  7072. (if tag
  7073. (org-toggle-tag org-archive-tag 'on)
  7074. (org-archive-subtree))
  7075. (setq cntarch (1+ cntarch)))
  7076. (goto-char end)))))
  7077. (message "%d trees archived" cntarch)))
  7078. (defun org-cycle-hide-drawers (state)
  7079. "Re-hide all drawers after a visibility state change."
  7080. (when (and (org-mode-p)
  7081. (not (memq state '(overview folded))))
  7082. (save-excursion
  7083. (let* ((globalp (memq state '(contents all)))
  7084. (beg (if globalp (point-min) (point)))
  7085. (end (if globalp (point-max) (org-end-of-subtree t))))
  7086. (goto-char beg)
  7087. (while (re-search-forward org-drawer-regexp end t)
  7088. (org-flag-drawer t))))))
  7089. (defun org-flag-drawer (flag)
  7090. (save-excursion
  7091. (beginning-of-line 1)
  7092. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7093. (let ((b (match-end 0)))
  7094. (if (re-search-forward
  7095. "^[ \t]*:END:"
  7096. (save-excursion (outline-next-heading) (point)) t)
  7097. (outline-flag-region b (point-at-eol) flag)
  7098. (error ":END: line missing"))))))
  7099. (defun org-cycle-hide-archived-subtrees (state)
  7100. "Re-hide all archived subtrees after a visibility state change."
  7101. (when (and (not org-cycle-open-archived-trees)
  7102. (not (memq state '(overview folded))))
  7103. (save-excursion
  7104. (let* ((globalp (memq state '(contents all)))
  7105. (beg (if globalp (point-min) (point)))
  7106. (end (if globalp (point-max) (org-end-of-subtree t))))
  7107. (org-hide-archived-subtrees beg end)
  7108. (goto-char beg)
  7109. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7110. (message "%s" (substitute-command-keys
  7111. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7112. (defun org-force-cycle-archived ()
  7113. "Cycle subtree even if it is archived."
  7114. (interactive)
  7115. (setq this-command 'org-cycle)
  7116. (let ((org-cycle-open-archived-trees t))
  7117. (call-interactively 'org-cycle)))
  7118. (defun org-hide-archived-subtrees (beg end)
  7119. "Re-hide all archived subtrees after a visibility state change."
  7120. (save-excursion
  7121. (let* ((re (concat ":" org-archive-tag ":")))
  7122. (goto-char beg)
  7123. (while (re-search-forward re end t)
  7124. (and (org-on-heading-p) (hide-subtree))
  7125. (org-end-of-subtree t)))))
  7126. (defun org-toggle-tag (tag &optional onoff)
  7127. "Toggle the tag TAG for the current line.
  7128. If ONOFF is `on' or `off', don't toggle but set to this state."
  7129. (unless (org-on-heading-p t) (error "Not on headling"))
  7130. (let (res current)
  7131. (save-excursion
  7132. (beginning-of-line)
  7133. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7134. (point-at-eol) t)
  7135. (progn
  7136. (setq current (match-string 1))
  7137. (replace-match ""))
  7138. (setq current ""))
  7139. (setq current (nreverse (org-split-string current ":")))
  7140. (cond
  7141. ((eq onoff 'on)
  7142. (setq res t)
  7143. (or (member tag current) (push tag current)))
  7144. ((eq onoff 'off)
  7145. (or (not (member tag current)) (setq current (delete tag current))))
  7146. (t (if (member tag current)
  7147. (setq current (delete tag current))
  7148. (setq res t)
  7149. (push tag current))))
  7150. (end-of-line 1)
  7151. (if current
  7152. (progn
  7153. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7154. (org-set-tags nil t))
  7155. (delete-horizontal-space))
  7156. (run-hooks 'org-after-tags-change-hook))
  7157. res))
  7158. (defun org-toggle-archive-tag (&optional arg)
  7159. "Toggle the archive tag for the current headline.
  7160. With prefix ARG, check all children of current headline and offer tagging
  7161. the children that do not contain any open TODO items."
  7162. (interactive "P")
  7163. (if arg
  7164. (org-archive-all-done 'tag)
  7165. (let (set)
  7166. (save-excursion
  7167. (org-back-to-heading t)
  7168. (setq set (org-toggle-tag org-archive-tag))
  7169. (when set (hide-subtree)))
  7170. (and set (beginning-of-line 1))
  7171. (message "Subtree %s" (if set "archived" "unarchived")))))
  7172. ;;;; Tables
  7173. ;;; The table editor
  7174. ;; Watch out: Here we are talking about two different kind of tables.
  7175. ;; Most of the code is for the tables created with the Org-mode table editor.
  7176. ;; Sometimes, we talk about tables created and edited with the table.el
  7177. ;; Emacs package. We call the former org-type tables, and the latter
  7178. ;; table.el-type tables.
  7179. (defun org-before-change-function (beg end)
  7180. "Every change indicates that a table might need an update."
  7181. (setq org-table-may-need-update t))
  7182. (defconst org-table-line-regexp "^[ \t]*|"
  7183. "Detects an org-type table line.")
  7184. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7185. "Detects an org-type table line.")
  7186. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7187. "Detects a table line marked for automatic recalculation.")
  7188. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7189. "Detects a table line marked for automatic recalculation.")
  7190. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7191. "Detects a table line marked for automatic recalculation.")
  7192. (defconst org-table-hline-regexp "^[ \t]*|-"
  7193. "Detects an org-type table hline.")
  7194. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7195. "Detects a table-type table hline.")
  7196. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7197. "Detects an org-type or table-type table.")
  7198. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7199. "Searching from within a table (any type) this finds the first line
  7200. outside the table.")
  7201. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7202. "Searching from within a table (any type) this finds the first line
  7203. outside the table.")
  7204. (defvar org-table-last-highlighted-reference nil)
  7205. (defvar org-table-formula-history nil)
  7206. (defvar org-table-column-names nil
  7207. "Alist with column names, derived from the `!' line.")
  7208. (defvar org-table-column-name-regexp nil
  7209. "Regular expression matching the current column names.")
  7210. (defvar org-table-local-parameters nil
  7211. "Alist with parameter names, derived from the `$' line.")
  7212. (defvar org-table-named-field-locations nil
  7213. "Alist with locations of named fields.")
  7214. (defvar org-table-current-line-types nil
  7215. "Table row types, non-nil only for the duration of a comand.")
  7216. (defvar org-table-current-begin-line nil
  7217. "Table begin line, non-nil only for the duration of a comand.")
  7218. (defvar org-table-current-begin-pos nil
  7219. "Table begin position, non-nil only for the duration of a comand.")
  7220. (defvar org-table-dlines nil
  7221. "Vector of data line line numbers in the current table.")
  7222. (defvar org-table-hlines nil
  7223. "Vector of hline line numbers in the current table.")
  7224. (defconst org-table-range-regexp
  7225. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7226. ;; 1 2 3 4 5
  7227. "Regular expression for matching ranges in formulas.")
  7228. (defconst org-table-range-regexp2
  7229. (concat
  7230. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7231. "\\.\\."
  7232. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7233. "Match a range for reference display.")
  7234. (defconst org-table-translate-regexp
  7235. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7236. "Match a reference that needs translation, for reference display.")
  7237. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7238. (defun org-table-create-with-table.el ()
  7239. "Use the table.el package to insert a new table.
  7240. If there is already a table at point, convert between Org-mode tables
  7241. and table.el tables."
  7242. (interactive)
  7243. (require 'table)
  7244. (cond
  7245. ((org-at-table.el-p)
  7246. (if (y-or-n-p "Convert table to Org-mode table? ")
  7247. (org-table-convert)))
  7248. ((org-at-table-p)
  7249. (if (y-or-n-p "Convert table to table.el table? ")
  7250. (org-table-convert)))
  7251. (t (call-interactively 'table-insert))))
  7252. (defun org-table-create-or-convert-from-region (arg)
  7253. "Convert region to table, or create an empty table.
  7254. If there is an active region, convert it to a table, using the function
  7255. `org-table-convert-region'. See the documentation of that function
  7256. to learn how the prefix argument is interpreted to determine the field
  7257. separator.
  7258. If there is no such region, create an empty table with `org-table-create'."
  7259. (interactive "P")
  7260. (if (org-region-active-p)
  7261. (org-table-convert-region (region-beginning) (region-end) arg)
  7262. (org-table-create arg)))
  7263. (defun org-table-create (&optional size)
  7264. "Query for a size and insert a table skeleton.
  7265. SIZE is a string Columns x Rows like for example \"3x2\"."
  7266. (interactive "P")
  7267. (unless size
  7268. (setq size (read-string
  7269. (concat "Table size Columns x Rows [e.g. "
  7270. org-table-default-size "]: ")
  7271. "" nil org-table-default-size)))
  7272. (let* ((pos (point))
  7273. (indent (make-string (current-column) ?\ ))
  7274. (split (org-split-string size " *x *"))
  7275. (rows (string-to-number (nth 1 split)))
  7276. (columns (string-to-number (car split)))
  7277. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7278. "\n")))
  7279. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7280. (point-at-bol) (point)))
  7281. (beginning-of-line 1)
  7282. (newline))
  7283. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7284. (dotimes (i rows) (insert line))
  7285. (goto-char pos)
  7286. (if (> rows 1)
  7287. ;; Insert a hline after the first row.
  7288. (progn
  7289. (end-of-line 1)
  7290. (insert "\n|-")
  7291. (goto-char pos)))
  7292. (org-table-align)))
  7293. (defun org-table-convert-region (beg0 end0 &optional separator)
  7294. "Convert region to a table.
  7295. The region goes from BEG0 to END0, but these borders will be moved
  7296. slightly, to make sure a beginning of line in the first line is included.
  7297. SEPARATOR specifies the field separator in the lines. It can have the
  7298. following values:
  7299. '(4) Use the comma as a field separator
  7300. '(16) Use a TAB as field separator
  7301. integer When a number, use that many spaces as field separator
  7302. nil When nil, the command tries to be smart and figure out the
  7303. separator in the following way:
  7304. - when each line contains a TAB, assume TAB-separated material
  7305. - when each line contains a comme, assume CSV material
  7306. - else, assume one or more SPACE charcters as separator."
  7307. (interactive "rP")
  7308. (let* ((beg (min beg0 end0))
  7309. (end (max beg0 end0))
  7310. re)
  7311. (goto-char beg)
  7312. (beginning-of-line 1)
  7313. (setq beg (move-marker (make-marker) (point)))
  7314. (goto-char end)
  7315. (if (bolp) (backward-char 1) (end-of-line 1))
  7316. (setq end (move-marker (make-marker) (point)))
  7317. ;; Get the right field separator
  7318. (unless separator
  7319. (goto-char beg)
  7320. (setq separator
  7321. (cond
  7322. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7323. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7324. (t 1))))
  7325. (setq re (cond
  7326. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7327. ((equal separator '(16)) "^\\|\t")
  7328. ((integerp separator)
  7329. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7330. (t (error "This should not happen"))))
  7331. (goto-char beg)
  7332. (while (re-search-forward re end t)
  7333. (replace-match "| " t t))
  7334. (goto-char beg)
  7335. (insert " ")
  7336. (org-table-align)))
  7337. (defun org-table-import (file arg)
  7338. "Import FILE as a table.
  7339. The file is assumed to be tab-separated. Such files can be produced by most
  7340. spreadsheet and database applications. If no tabs (at least one per line)
  7341. are found, lines will be split on whitespace into fields."
  7342. (interactive "f\nP")
  7343. (or (bolp) (newline))
  7344. (let ((beg (point))
  7345. (pm (point-max)))
  7346. (insert-file-contents file)
  7347. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7348. (defun org-table-export ()
  7349. "Export table as a tab-separated file.
  7350. Such a file can be imported into a spreadsheet program like Excel."
  7351. (interactive)
  7352. (let* ((beg (org-table-begin))
  7353. (end (org-table-end))
  7354. (table (buffer-substring beg end))
  7355. (file (read-file-name "Export table to: "))
  7356. buf)
  7357. (unless (or (not (file-exists-p file))
  7358. (y-or-n-p (format "Overwrite file %s? " file)))
  7359. (error "Abort"))
  7360. (with-current-buffer (find-file-noselect file)
  7361. (setq buf (current-buffer))
  7362. (erase-buffer)
  7363. (fundamental-mode)
  7364. (insert table)
  7365. (goto-char (point-min))
  7366. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7367. (replace-match "" t t)
  7368. (end-of-line 1))
  7369. (goto-char (point-min))
  7370. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7371. (replace-match "" t t)
  7372. (goto-char (min (1+ (point)) (point-max))))
  7373. (goto-char (point-min))
  7374. (while (re-search-forward "^-[-+]*$" nil t)
  7375. (replace-match "")
  7376. (if (looking-at "\n")
  7377. (delete-char 1)))
  7378. (goto-char (point-min))
  7379. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7380. (replace-match "\t" t t))
  7381. (save-buffer))
  7382. (kill-buffer buf)))
  7383. (defvar org-table-aligned-begin-marker (make-marker)
  7384. "Marker at the beginning of the table last aligned.
  7385. Used to check if cursor still is in that table, to minimize realignment.")
  7386. (defvar org-table-aligned-end-marker (make-marker)
  7387. "Marker at the end of the table last aligned.
  7388. Used to check if cursor still is in that table, to minimize realignment.")
  7389. (defvar org-table-last-alignment nil
  7390. "List of flags for flushright alignment, from the last re-alignment.
  7391. This is being used to correctly align a single field after TAB or RET.")
  7392. (defvar org-table-last-column-widths nil
  7393. "List of max width of fields in each column.
  7394. This is being used to correctly align a single field after TAB or RET.")
  7395. (defvar org-table-overlay-coordinates nil
  7396. "Overlay coordinates after each align of a table.")
  7397. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7398. (defvar org-last-recalc-line nil)
  7399. (defconst org-narrow-column-arrow "=>"
  7400. "Used as display property in narrowed table columns.")
  7401. (defun org-table-align ()
  7402. "Align the table at point by aligning all vertical bars."
  7403. (interactive)
  7404. (let* (
  7405. ;; Limits of table
  7406. (beg (org-table-begin))
  7407. (end (org-table-end))
  7408. ;; Current cursor position
  7409. (linepos (org-current-line))
  7410. (colpos (org-table-current-column))
  7411. (winstart (window-start))
  7412. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7413. lines (new "") lengths l typenums ty fields maxfields i
  7414. column
  7415. (indent "") cnt frac
  7416. rfmt hfmt
  7417. (spaces '(1 . 1))
  7418. (sp1 (car spaces))
  7419. (sp2 (cdr spaces))
  7420. (rfmt1 (concat
  7421. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7422. (hfmt1 (concat
  7423. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7424. emptystrings links dates emph narrow fmax f1 len c e)
  7425. (untabify beg end)
  7426. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7427. ;; Check if we have links or dates
  7428. (goto-char beg)
  7429. (setq links (re-search-forward org-bracket-link-regexp end t))
  7430. (goto-char beg)
  7431. (setq emph (and org-hide-emphasis-markers
  7432. (re-search-forward org-emph-re end t)))
  7433. (goto-char beg)
  7434. (setq dates (and org-display-custom-times
  7435. (re-search-forward org-ts-regexp-both end t)))
  7436. ;; Make sure the link properties are right
  7437. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7438. ;; Make sure the date properties are right
  7439. (when dates (goto-char beg) (while (org-activate-dates end)))
  7440. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7441. ;; Check if we are narrowing any columns
  7442. (goto-char beg)
  7443. (setq narrow (and org-format-transports-properties-p
  7444. (re-search-forward "<[0-9]+>" end t)))
  7445. ;; Get the rows
  7446. (setq lines (org-split-string
  7447. (buffer-substring beg end) "\n"))
  7448. ;; Store the indentation of the first line
  7449. (if (string-match "^ *" (car lines))
  7450. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7451. ;; Mark the hlines by setting the corresponding element to nil
  7452. ;; At the same time, we remove trailing space.
  7453. (setq lines (mapcar (lambda (l)
  7454. (if (string-match "^ *|-" l)
  7455. nil
  7456. (if (string-match "[ \t]+$" l)
  7457. (substring l 0 (match-beginning 0))
  7458. l)))
  7459. lines))
  7460. ;; Get the data fields by splitting the lines.
  7461. (setq fields (mapcar
  7462. (lambda (l)
  7463. (org-split-string l " *| *"))
  7464. (delq nil (copy-sequence lines))))
  7465. ;; How many fields in the longest line?
  7466. (condition-case nil
  7467. (setq maxfields (apply 'max (mapcar 'length fields)))
  7468. (error
  7469. (kill-region beg end)
  7470. (org-table-create org-table-default-size)
  7471. (error "Empty table - created default table")))
  7472. ;; A list of empty strings to fill any short rows on output
  7473. (setq emptystrings (make-list maxfields ""))
  7474. ;; Check for special formatting.
  7475. (setq i -1)
  7476. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7477. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7478. ;; Check if there is an explicit width specified
  7479. (when narrow
  7480. (setq c column fmax nil)
  7481. (while c
  7482. (setq e (pop c))
  7483. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7484. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7485. ;; Find fields that are wider than fmax, and shorten them
  7486. (when fmax
  7487. (loop for xx in column do
  7488. (when (and (stringp xx)
  7489. (> (org-string-width xx) fmax))
  7490. (org-add-props xx nil
  7491. 'help-echo
  7492. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7493. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7494. (unless (> f1 1)
  7495. (error "Cannot narrow field starting with wide link \"%s\""
  7496. (match-string 0 xx)))
  7497. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7498. (add-text-properties (- f1 2) f1
  7499. (list 'display org-narrow-column-arrow)
  7500. xx)))))
  7501. ;; Get the maximum width for each column
  7502. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7503. ;; Get the fraction of numbers, to decide about alignment of the column
  7504. (setq cnt 0 frac 0.0)
  7505. (loop for x in column do
  7506. (if (equal x "")
  7507. nil
  7508. (setq frac ( / (+ (* frac cnt)
  7509. (if (string-match org-table-number-regexp x) 1 0))
  7510. (setq cnt (1+ cnt))))))
  7511. (push (>= frac org-table-number-fraction) typenums))
  7512. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7513. ;; Store the alignment of this table, for later editing of single fields
  7514. (setq org-table-last-alignment typenums
  7515. org-table-last-column-widths lengths)
  7516. ;; With invisible characters, `format' does not get the field width right
  7517. ;; So we need to make these fields wide by hand.
  7518. (when (or links emph)
  7519. (loop for i from 0 upto (1- maxfields) do
  7520. (setq len (nth i lengths))
  7521. (loop for j from 0 upto (1- (length fields)) do
  7522. (setq c (nthcdr i (car (nthcdr j fields))))
  7523. (if (and (stringp (car c))
  7524. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7525. ; (string-match org-bracket-link-regexp (car c))
  7526. (< (org-string-width (car c)) len))
  7527. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7528. ;; Compute the formats needed for output of the table
  7529. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7530. (while (setq l (pop lengths))
  7531. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7532. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7533. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7534. (setq rfmt (concat rfmt "\n")
  7535. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7536. (setq new (mapconcat
  7537. (lambda (l)
  7538. (if l (apply 'format rfmt
  7539. (append (pop fields) emptystrings))
  7540. hfmt))
  7541. lines ""))
  7542. ;; Replace the old one
  7543. (delete-region beg end)
  7544. (move-marker end nil)
  7545. (move-marker org-table-aligned-begin-marker (point))
  7546. (insert new)
  7547. (move-marker org-table-aligned-end-marker (point))
  7548. (when (and orgtbl-mode (not (org-mode-p)))
  7549. (goto-char org-table-aligned-begin-marker)
  7550. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7551. ;; Try to move to the old location
  7552. (goto-line winstartline)
  7553. (setq winstart (point-at-bol))
  7554. (goto-line linepos)
  7555. (set-window-start (selected-window) winstart 'noforce)
  7556. (org-table-goto-column colpos)
  7557. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7558. (setq org-table-may-need-update nil)
  7559. ))
  7560. (defun org-string-width (s)
  7561. "Compute width of string, ignoring invisible characters.
  7562. This ignores character with invisibility property `org-link', and also
  7563. characters with property `org-cwidth', because these will become invisible
  7564. upon the next fontification round."
  7565. (let (b l)
  7566. (when (or (eq t buffer-invisibility-spec)
  7567. (assq 'org-link buffer-invisibility-spec))
  7568. (while (setq b (text-property-any 0 (length s)
  7569. 'invisible 'org-link s))
  7570. (setq s (concat (substring s 0 b)
  7571. (substring s (or (next-single-property-change
  7572. b 'invisible s) (length s)))))))
  7573. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7574. (setq s (concat (substring s 0 b)
  7575. (substring s (or (next-single-property-change
  7576. b 'org-cwidth s) (length s))))))
  7577. (setq l (string-width s) b -1)
  7578. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7579. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7580. l))
  7581. (defun org-table-begin (&optional table-type)
  7582. "Find the beginning of the table and return its position.
  7583. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7584. (save-excursion
  7585. (if (not (re-search-backward
  7586. (if table-type org-table-any-border-regexp
  7587. org-table-border-regexp)
  7588. nil t))
  7589. (progn (goto-char (point-min)) (point))
  7590. (goto-char (match-beginning 0))
  7591. (beginning-of-line 2)
  7592. (point))))
  7593. (defun org-table-end (&optional table-type)
  7594. "Find the end of the table and return its position.
  7595. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7596. (save-excursion
  7597. (if (not (re-search-forward
  7598. (if table-type org-table-any-border-regexp
  7599. org-table-border-regexp)
  7600. nil t))
  7601. (goto-char (point-max))
  7602. (goto-char (match-beginning 0)))
  7603. (point-marker)))
  7604. (defun org-table-justify-field-maybe (&optional new)
  7605. "Justify the current field, text to left, number to right.
  7606. Optional argument NEW may specify text to replace the current field content."
  7607. (cond
  7608. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7609. ((org-at-table-hline-p))
  7610. ((and (not new)
  7611. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7612. (current-buffer)))
  7613. (< (point) org-table-aligned-begin-marker)
  7614. (>= (point) org-table-aligned-end-marker)))
  7615. ;; This is not the same table, force a full re-align
  7616. (setq org-table-may-need-update t))
  7617. (t ;; realign the current field, based on previous full realign
  7618. (let* ((pos (point)) s
  7619. (col (org-table-current-column))
  7620. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7621. l f n o e)
  7622. (when (> col 0)
  7623. (skip-chars-backward "^|\n")
  7624. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7625. (progn
  7626. (setq s (match-string 1)
  7627. o (match-string 0)
  7628. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7629. e (not (= (match-beginning 2) (match-end 2))))
  7630. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7631. l (if e "|" (setq org-table-may-need-update t) ""))
  7632. n (format f s))
  7633. (if new
  7634. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7635. (setq n (format f new))
  7636. (setq n (concat new "|") org-table-may-need-update t)))
  7637. (or (equal n o)
  7638. (let (org-table-may-need-update)
  7639. (replace-match n t t))))
  7640. (setq org-table-may-need-update t))
  7641. (goto-char pos))))))
  7642. (defun org-table-next-field ()
  7643. "Go to the next field in the current table, creating new lines as needed.
  7644. Before doing so, re-align the table if necessary."
  7645. (interactive)
  7646. (org-table-maybe-eval-formula)
  7647. (org-table-maybe-recalculate-line)
  7648. (if (and org-table-automatic-realign
  7649. org-table-may-need-update)
  7650. (org-table-align))
  7651. (let ((end (org-table-end)))
  7652. (if (org-at-table-hline-p)
  7653. (end-of-line 1))
  7654. (condition-case nil
  7655. (progn
  7656. (re-search-forward "|" end)
  7657. (if (looking-at "[ \t]*$")
  7658. (re-search-forward "|" end))
  7659. (if (and (looking-at "-")
  7660. org-table-tab-jumps-over-hlines
  7661. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7662. (goto-char (match-beginning 1)))
  7663. (if (looking-at "-")
  7664. (progn
  7665. (beginning-of-line 0)
  7666. (org-table-insert-row 'below))
  7667. (if (looking-at " ") (forward-char 1))))
  7668. (error
  7669. (org-table-insert-row 'below)))))
  7670. (defun org-table-previous-field ()
  7671. "Go to the previous field in the table.
  7672. Before doing so, re-align the table if necessary."
  7673. (interactive)
  7674. (org-table-justify-field-maybe)
  7675. (org-table-maybe-recalculate-line)
  7676. (if (and org-table-automatic-realign
  7677. org-table-may-need-update)
  7678. (org-table-align))
  7679. (if (org-at-table-hline-p)
  7680. (end-of-line 1))
  7681. (re-search-backward "|" (org-table-begin))
  7682. (re-search-backward "|" (org-table-begin))
  7683. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7684. (re-search-backward "|" (org-table-begin)))
  7685. (if (looking-at "| ?")
  7686. (goto-char (match-end 0))))
  7687. (defun org-table-next-row ()
  7688. "Go to the next row (same column) in the current table.
  7689. Before doing so, re-align the table if necessary."
  7690. (interactive)
  7691. (org-table-maybe-eval-formula)
  7692. (org-table-maybe-recalculate-line)
  7693. (if (or (looking-at "[ \t]*$")
  7694. (save-excursion (skip-chars-backward " \t") (bolp)))
  7695. (newline)
  7696. (if (and org-table-automatic-realign
  7697. org-table-may-need-update)
  7698. (org-table-align))
  7699. (let ((col (org-table-current-column)))
  7700. (beginning-of-line 2)
  7701. (if (or (not (org-at-table-p))
  7702. (org-at-table-hline-p))
  7703. (progn
  7704. (beginning-of-line 0)
  7705. (org-table-insert-row 'below)))
  7706. (org-table-goto-column col)
  7707. (skip-chars-backward "^|\n\r")
  7708. (if (looking-at " ") (forward-char 1)))))
  7709. (defun org-table-copy-down (n)
  7710. "Copy a field down in the current column.
  7711. If the field at the cursor is empty, copy into it the content of the nearest
  7712. non-empty field above. With argument N, use the Nth non-empty field.
  7713. If the current field is not empty, it is copied down to the next row, and
  7714. the cursor is moved with it. Therefore, repeating this command causes the
  7715. column to be filled row-by-row.
  7716. If the variable `org-table-copy-increment' is non-nil and the field is an
  7717. integer or a timestamp, it will be incremented while copying. In the case of
  7718. a timestamp, if the cursor is on the year, change the year. If it is on the
  7719. month or the day, change that. Point will stay on the current date field
  7720. in order to easily repeat the interval."
  7721. (interactive "p")
  7722. (let* ((colpos (org-table-current-column))
  7723. (col (current-column))
  7724. (field (org-table-get-field))
  7725. (non-empty (string-match "[^ \t]" field))
  7726. (beg (org-table-begin))
  7727. txt)
  7728. (org-table-check-inside-data-field)
  7729. (if non-empty
  7730. (progn
  7731. (setq txt (org-trim field))
  7732. (org-table-next-row)
  7733. (org-table-blank-field))
  7734. (save-excursion
  7735. (setq txt
  7736. (catch 'exit
  7737. (while (progn (beginning-of-line 1)
  7738. (re-search-backward org-table-dataline-regexp
  7739. beg t))
  7740. (org-table-goto-column colpos t)
  7741. (if (and (looking-at
  7742. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7743. (= (setq n (1- n)) 0))
  7744. (throw 'exit (match-string 1))))))))
  7745. (if txt
  7746. (progn
  7747. (if (and org-table-copy-increment
  7748. (string-match "^[0-9]+$" txt))
  7749. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7750. (insert txt)
  7751. (move-to-column col)
  7752. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7753. (org-timestamp-up 1)
  7754. (org-table-maybe-recalculate-line))
  7755. (org-table-align)
  7756. (move-to-column col))
  7757. (error "No non-empty field found"))))
  7758. (defun org-table-check-inside-data-field ()
  7759. "Is point inside a table data field?
  7760. I.e. not on a hline or before the first or after the last column?
  7761. This actually throws an error, so it aborts the current command."
  7762. (if (or (not (org-at-table-p))
  7763. (= (org-table-current-column) 0)
  7764. (org-at-table-hline-p)
  7765. (looking-at "[ \t]*$"))
  7766. (error "Not in table data field")))
  7767. (defvar org-table-clip nil
  7768. "Clipboard for table regions.")
  7769. (defun org-table-blank-field ()
  7770. "Blank the current table field or active region."
  7771. (interactive)
  7772. (org-table-check-inside-data-field)
  7773. (if (and (interactive-p) (org-region-active-p))
  7774. (let (org-table-clip)
  7775. (org-table-cut-region (region-beginning) (region-end)))
  7776. (skip-chars-backward "^|")
  7777. (backward-char 1)
  7778. (if (looking-at "|[^|\n]+")
  7779. (let* ((pos (match-beginning 0))
  7780. (match (match-string 0))
  7781. (len (org-string-width match)))
  7782. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7783. (goto-char (+ 2 pos))
  7784. (substring match 1)))))
  7785. (defun org-table-get-field (&optional n replace)
  7786. "Return the value of the field in column N of current row.
  7787. N defaults to current field.
  7788. If REPLACE is a string, replace field with this value. The return value
  7789. is always the old value."
  7790. (and n (org-table-goto-column n))
  7791. (skip-chars-backward "^|\n")
  7792. (backward-char 1)
  7793. (if (looking-at "|[^|\r\n]*")
  7794. (let* ((pos (match-beginning 0))
  7795. (val (buffer-substring (1+ pos) (match-end 0))))
  7796. (if replace
  7797. (replace-match (concat "|" replace) t t))
  7798. (goto-char (min (point-at-eol) (+ 2 pos)))
  7799. val)
  7800. (forward-char 1) ""))
  7801. (defun org-table-field-info (arg)
  7802. "Show info about the current field, and highlight any reference at point."
  7803. (interactive "P")
  7804. (org-table-get-specials)
  7805. (save-excursion
  7806. (let* ((pos (point))
  7807. (col (org-table-current-column))
  7808. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7809. (name (car (rassoc (list (org-current-line) col)
  7810. org-table-named-field-locations)))
  7811. (eql (org-table-get-stored-formulas))
  7812. (dline (org-table-current-dline))
  7813. (ref (format "@%d$%d" dline col))
  7814. (ref1 (org-table-convert-refs-to-an ref))
  7815. (fequation (or (assoc name eql) (assoc ref eql)))
  7816. (cequation (assoc (int-to-string col) eql))
  7817. (eqn (or fequation cequation)))
  7818. (goto-char pos)
  7819. (condition-case nil
  7820. (org-table-show-reference 'local)
  7821. (error nil))
  7822. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7823. dline col
  7824. (if cname (concat " or $" cname) "")
  7825. dline col ref1
  7826. (if name (concat " or $" name) "")
  7827. ;; FIXME: formula info not correct if special table line
  7828. (if eqn
  7829. (concat ", formula: "
  7830. (org-table-formula-to-user
  7831. (concat
  7832. (if (string-match "^[$@]"(car eqn)) "" "$")
  7833. (car eqn) "=" (cdr eqn))))
  7834. "")))))
  7835. (defun org-table-current-column ()
  7836. "Find out which column we are in.
  7837. When called interactively, column is also displayed in echo area."
  7838. (interactive)
  7839. (if (interactive-p) (org-table-check-inside-data-field))
  7840. (save-excursion
  7841. (let ((cnt 0) (pos (point)))
  7842. (beginning-of-line 1)
  7843. (while (search-forward "|" pos t)
  7844. (setq cnt (1+ cnt)))
  7845. (if (interactive-p) (message "This is table column %d" cnt))
  7846. cnt)))
  7847. (defun org-table-current-dline ()
  7848. "Find out what table data line we are in.
  7849. Only datalins count for this."
  7850. (interactive)
  7851. (if (interactive-p) (org-table-check-inside-data-field))
  7852. (save-excursion
  7853. (let ((cnt 0) (pos (point)))
  7854. (goto-char (org-table-begin))
  7855. (while (<= (point) pos)
  7856. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7857. (beginning-of-line 2))
  7858. (if (interactive-p) (message "This is table line %d" cnt))
  7859. cnt)))
  7860. (defun org-table-goto-column (n &optional on-delim force)
  7861. "Move the cursor to the Nth column in the current table line.
  7862. With optional argument ON-DELIM, stop with point before the left delimiter
  7863. of the field.
  7864. If there are less than N fields, just go to after the last delimiter.
  7865. However, when FORCE is non-nil, create new columns if necessary."
  7866. (interactive "p")
  7867. (let ((pos (point-at-eol)))
  7868. (beginning-of-line 1)
  7869. (when (> n 0)
  7870. (while (and (> (setq n (1- n)) -1)
  7871. (or (search-forward "|" pos t)
  7872. (and force
  7873. (progn (end-of-line 1)
  7874. (skip-chars-backward "^|")
  7875. (insert " | "))))))
  7876. ; (backward-char 2) t)))))
  7877. (when (and force (not (looking-at ".*|")))
  7878. (save-excursion (end-of-line 1) (insert " | ")))
  7879. (if on-delim
  7880. (backward-char 1)
  7881. (if (looking-at " ") (forward-char 1))))))
  7882. (defun org-at-table-p (&optional table-type)
  7883. "Return t if the cursor is inside an org-type table.
  7884. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7885. (if org-enable-table-editor
  7886. (save-excursion
  7887. (beginning-of-line 1)
  7888. (looking-at (if table-type org-table-any-line-regexp
  7889. org-table-line-regexp)))
  7890. nil))
  7891. (defun org-at-table.el-p ()
  7892. "Return t if and only if we are at a table.el table."
  7893. (and (org-at-table-p 'any)
  7894. (save-excursion
  7895. (goto-char (org-table-begin 'any))
  7896. (looking-at org-table1-hline-regexp))))
  7897. (defun org-table-recognize-table.el ()
  7898. "If there is a table.el table nearby, recognize it and move into it."
  7899. (if org-table-tab-recognizes-table.el
  7900. (if (org-at-table.el-p)
  7901. (progn
  7902. (beginning-of-line 1)
  7903. (if (looking-at org-table-dataline-regexp)
  7904. nil
  7905. (if (looking-at org-table1-hline-regexp)
  7906. (progn
  7907. (beginning-of-line 2)
  7908. (if (looking-at org-table-any-border-regexp)
  7909. (beginning-of-line -1)))))
  7910. (if (re-search-forward "|" (org-table-end t) t)
  7911. (progn
  7912. (require 'table)
  7913. (if (table--at-cell-p (point))
  7914. t
  7915. (message "recognizing table.el table...")
  7916. (table-recognize-table)
  7917. (message "recognizing table.el table...done")))
  7918. (error "This should not happen..."))
  7919. t)
  7920. nil)
  7921. nil))
  7922. (defun org-at-table-hline-p ()
  7923. "Return t if the cursor is inside a hline in a table."
  7924. (if org-enable-table-editor
  7925. (save-excursion
  7926. (beginning-of-line 1)
  7927. (looking-at org-table-hline-regexp))
  7928. nil))
  7929. (defun org-table-insert-column ()
  7930. "Insert a new column into the table."
  7931. (interactive)
  7932. (if (not (org-at-table-p))
  7933. (error "Not at a table"))
  7934. (org-table-find-dataline)
  7935. (let* ((col (max 1 (org-table-current-column)))
  7936. (beg (org-table-begin))
  7937. (end (org-table-end))
  7938. ;; Current cursor position
  7939. (linepos (org-current-line))
  7940. (colpos col))
  7941. (goto-char beg)
  7942. (while (< (point) end)
  7943. (if (org-at-table-hline-p)
  7944. nil
  7945. (org-table-goto-column col t)
  7946. (insert "| "))
  7947. (beginning-of-line 2))
  7948. (move-marker end nil)
  7949. (goto-line linepos)
  7950. (org-table-goto-column colpos)
  7951. (org-table-align)
  7952. (org-table-fix-formulas "$" nil (1- col) 1)))
  7953. (defun org-table-find-dataline ()
  7954. "Find a dataline in the current table, which is needed for column commands."
  7955. (if (and (org-at-table-p)
  7956. (not (org-at-table-hline-p)))
  7957. t
  7958. (let ((col (current-column))
  7959. (end (org-table-end)))
  7960. (move-to-column col)
  7961. (while (and (< (point) end)
  7962. (or (not (= (current-column) col))
  7963. (org-at-table-hline-p)))
  7964. (beginning-of-line 2)
  7965. (move-to-column col))
  7966. (if (and (org-at-table-p)
  7967. (not (org-at-table-hline-p)))
  7968. t
  7969. (error
  7970. "Please position cursor in a data line for column operations")))))
  7971. (defun org-table-delete-column ()
  7972. "Delete a column from the table."
  7973. (interactive)
  7974. (if (not (org-at-table-p))
  7975. (error "Not at a table"))
  7976. (org-table-find-dataline)
  7977. (org-table-check-inside-data-field)
  7978. (let* ((col (org-table-current-column))
  7979. (beg (org-table-begin))
  7980. (end (org-table-end))
  7981. ;; Current cursor position
  7982. (linepos (org-current-line))
  7983. (colpos col))
  7984. (goto-char beg)
  7985. (while (< (point) end)
  7986. (if (org-at-table-hline-p)
  7987. nil
  7988. (org-table-goto-column col t)
  7989. (and (looking-at "|[^|\n]+|")
  7990. (replace-match "|")))
  7991. (beginning-of-line 2))
  7992. (move-marker end nil)
  7993. (goto-line linepos)
  7994. (org-table-goto-column colpos)
  7995. (org-table-align)
  7996. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  7997. col -1 col)))
  7998. (defun org-table-move-column-right ()
  7999. "Move column to the right."
  8000. (interactive)
  8001. (org-table-move-column nil))
  8002. (defun org-table-move-column-left ()
  8003. "Move column to the left."
  8004. (interactive)
  8005. (org-table-move-column 'left))
  8006. (defun org-table-move-column (&optional left)
  8007. "Move the current column to the right. With arg LEFT, move to the left."
  8008. (interactive "P")
  8009. (if (not (org-at-table-p))
  8010. (error "Not at a table"))
  8011. (org-table-find-dataline)
  8012. (org-table-check-inside-data-field)
  8013. (let* ((col (org-table-current-column))
  8014. (col1 (if left (1- col) col))
  8015. (beg (org-table-begin))
  8016. (end (org-table-end))
  8017. ;; Current cursor position
  8018. (linepos (org-current-line))
  8019. (colpos (if left (1- col) (1+ col))))
  8020. (if (and left (= col 1))
  8021. (error "Cannot move column further left"))
  8022. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8023. (error "Cannot move column further right"))
  8024. (goto-char beg)
  8025. (while (< (point) end)
  8026. (if (org-at-table-hline-p)
  8027. nil
  8028. (org-table-goto-column col1 t)
  8029. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8030. (replace-match "|\\2|\\1|")))
  8031. (beginning-of-line 2))
  8032. (move-marker end nil)
  8033. (goto-line linepos)
  8034. (org-table-goto-column colpos)
  8035. (org-table-align)
  8036. (org-table-fix-formulas
  8037. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8038. (cons (number-to-string colpos) (number-to-string col))))))
  8039. (defun org-table-move-row-down ()
  8040. "Move table row down."
  8041. (interactive)
  8042. (org-table-move-row nil))
  8043. (defun org-table-move-row-up ()
  8044. "Move table row up."
  8045. (interactive)
  8046. (org-table-move-row 'up))
  8047. (defun org-table-move-row (&optional up)
  8048. "Move the current table line down. With arg UP, move it up."
  8049. (interactive "P")
  8050. (let* ((col (current-column))
  8051. (pos (point))
  8052. (hline1p (save-excursion (beginning-of-line 1)
  8053. (looking-at org-table-hline-regexp)))
  8054. (dline1 (org-table-current-dline))
  8055. (dline2 (+ dline1 (if up -1 1)))
  8056. (tonew (if up 0 2))
  8057. txt hline2p)
  8058. (beginning-of-line tonew)
  8059. (unless (org-at-table-p)
  8060. (goto-char pos)
  8061. (error "Cannot move row further"))
  8062. (setq hline2p (looking-at org-table-hline-regexp))
  8063. (goto-char pos)
  8064. (beginning-of-line 1)
  8065. (setq pos (point))
  8066. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8067. (delete-region (point) (1+ (point-at-eol)))
  8068. (beginning-of-line tonew)
  8069. (insert txt)
  8070. (beginning-of-line 0)
  8071. (move-to-column col)
  8072. (unless (or hline1p hline2p)
  8073. (org-table-fix-formulas
  8074. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8075. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8076. (defun org-table-insert-row (&optional arg)
  8077. "Insert a new row above the current line into the table.
  8078. With prefix ARG, insert below the current line."
  8079. (interactive "P")
  8080. (if (not (org-at-table-p))
  8081. (error "Not at a table"))
  8082. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8083. (new (org-table-clean-line line)))
  8084. ;; Fix the first field if necessary
  8085. (if (string-match "^[ \t]*| *[#$] *|" line)
  8086. (setq new (replace-match (match-string 0 line) t t new)))
  8087. (beginning-of-line (if arg 2 1))
  8088. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8089. (beginning-of-line 0)
  8090. (re-search-forward "| ?" (point-at-eol) t)
  8091. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8092. (org-table-align))
  8093. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8094. (defun org-table-insert-hline (&optional above)
  8095. "Insert a horizontal-line below the current line into the table.
  8096. With prefix ABOVE, insert above the current line."
  8097. (interactive "P")
  8098. (if (not (org-at-table-p))
  8099. (error "Not at a table"))
  8100. (let ((line (org-table-clean-line
  8101. (buffer-substring (point-at-bol) (point-at-eol))))
  8102. (col (current-column)))
  8103. (while (string-match "|\\( +\\)|" line)
  8104. (setq line (replace-match
  8105. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8106. ?-) "|") t t line)))
  8107. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8108. (beginning-of-line (if above 1 2))
  8109. (insert line "\n")
  8110. (beginning-of-line (if above 1 -1))
  8111. (move-to-column col)
  8112. (and org-table-overlay-coordinates (org-table-align))))
  8113. (defun org-table-hline-and-move (&optional same-column)
  8114. "Insert a hline and move to the row below that line."
  8115. (interactive "P")
  8116. (let ((col (org-table-current-column)))
  8117. (org-table-maybe-eval-formula)
  8118. (org-table-maybe-recalculate-line)
  8119. (org-table-insert-hline)
  8120. (end-of-line 2)
  8121. (if (looking-at "\n[ \t]*|-")
  8122. (progn (insert "\n|") (org-table-align))
  8123. (org-table-next-field))
  8124. (if same-column (org-table-goto-column col))))
  8125. (defun org-table-clean-line (s)
  8126. "Convert a table line S into a string with only \"|\" and space.
  8127. In particular, this does handle wide and invisible characters."
  8128. (if (string-match "^[ \t]*|-" s)
  8129. ;; It's a hline, just map the characters
  8130. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8131. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8132. (setq s (replace-match
  8133. (concat "|" (make-string (org-string-width (match-string 1 s))
  8134. ?\ ) "|")
  8135. t t s)))
  8136. s))
  8137. (defun org-table-kill-row ()
  8138. "Delete the current row or horizontal line from the table."
  8139. (interactive)
  8140. (if (not (org-at-table-p))
  8141. (error "Not at a table"))
  8142. (let ((col (current-column))
  8143. (dline (org-table-current-dline)))
  8144. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8145. (if (not (org-at-table-p)) (beginning-of-line 0))
  8146. (move-to-column col)
  8147. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8148. dline -1 dline)))
  8149. (defun org-table-sort-lines (with-case &optional sorting-type)
  8150. "Sort table lines according to the column at point.
  8151. The position of point indicates the column to be used for
  8152. sorting, and the range of lines is the range between the nearest
  8153. horizontal separator lines, or the entire table of no such lines
  8154. exist. If point is before the first column, you will be prompted
  8155. for the sorting column. If there is an active region, the mark
  8156. specifies the first line and the sorting column, while point
  8157. should be in the last line to be included into the sorting.
  8158. The command then prompts for the sorting type which can be
  8159. alphabetically, numerically, or by time (as given in a time stamp
  8160. in the field). Sorting in reverse order is also possible.
  8161. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8162. If SORTING-TYPE is specified when this function is called from a Lisp
  8163. program, no prompting will take place. SORTING-TYPE must be a character,
  8164. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8165. should be done in reverse order."
  8166. (interactive "P")
  8167. (let* ((thisline (org-current-line))
  8168. (thiscol (org-table-current-column))
  8169. beg end bcol ecol tend tbeg column lns pos)
  8170. (when (equal thiscol 0)
  8171. (if (interactive-p)
  8172. (setq thiscol
  8173. (string-to-number
  8174. (read-string "Use column N for sorting: ")))
  8175. (setq thiscol 1))
  8176. (org-table-goto-column thiscol))
  8177. (org-table-check-inside-data-field)
  8178. (if (org-region-active-p)
  8179. (progn
  8180. (setq beg (region-beginning) end (region-end))
  8181. (goto-char beg)
  8182. (setq column (org-table-current-column)
  8183. beg (point-at-bol))
  8184. (goto-char end)
  8185. (setq end (point-at-bol 2)))
  8186. (setq column (org-table-current-column)
  8187. pos (point)
  8188. tbeg (org-table-begin)
  8189. tend (org-table-end))
  8190. (if (re-search-backward org-table-hline-regexp tbeg t)
  8191. (setq beg (point-at-bol 2))
  8192. (goto-char tbeg)
  8193. (setq beg (point-at-bol 1)))
  8194. (goto-char pos)
  8195. (if (re-search-forward org-table-hline-regexp tend t)
  8196. (setq end (point-at-bol 1))
  8197. (goto-char tend)
  8198. (setq end (point-at-bol))))
  8199. (setq beg (move-marker (make-marker) beg)
  8200. end (move-marker (make-marker) end))
  8201. (untabify beg end)
  8202. (goto-char beg)
  8203. (org-table-goto-column column)
  8204. (skip-chars-backward "^|")
  8205. (setq bcol (current-column))
  8206. (org-table-goto-column (1+ column))
  8207. (skip-chars-backward "^|")
  8208. (setq ecol (1- (current-column)))
  8209. (org-table-goto-column column)
  8210. (setq lns (mapcar (lambda(x) (cons
  8211. (org-sort-remove-invisible
  8212. (nth (1- column)
  8213. (org-split-string x "[ \t]*|[ \t]*")))
  8214. x))
  8215. (org-split-string (buffer-substring beg end) "\n")))
  8216. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8217. (delete-region beg end)
  8218. (move-marker beg nil)
  8219. (move-marker end nil)
  8220. (insert (mapconcat 'cdr lns "\n") "\n")
  8221. (goto-line thisline)
  8222. (org-table-goto-column thiscol)
  8223. (message "%d lines sorted, based on column %d" (length lns) column)))
  8224. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8225. (defun org-sort-remove-invisible (s)
  8226. (remove-text-properties 0 (length s) org-rm-props s)
  8227. (while (string-match org-bracket-link-regexp s)
  8228. (setq s (replace-match (if (match-end 2)
  8229. (match-string 3 s)
  8230. (match-string 1 s)) t t s)))
  8231. s)
  8232. (defun org-table-cut-region (beg end)
  8233. "Copy region in table to the clipboard and blank all relevant fields."
  8234. (interactive "r")
  8235. (org-table-copy-region beg end 'cut))
  8236. (defun org-table-copy-region (beg end &optional cut)
  8237. "Copy rectangular region in table to clipboard.
  8238. A special clipboard is used which can only be accessed
  8239. with `org-table-paste-rectangle'."
  8240. (interactive "rP")
  8241. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8242. region cols
  8243. (rpl (if cut " " nil)))
  8244. (goto-char beg)
  8245. (org-table-check-inside-data-field)
  8246. (setq l01 (org-current-line)
  8247. c01 (org-table-current-column))
  8248. (goto-char end)
  8249. (org-table-check-inside-data-field)
  8250. (setq l02 (org-current-line)
  8251. c02 (org-table-current-column))
  8252. (setq l1 (min l01 l02) l2 (max l01 l02)
  8253. c1 (min c01 c02) c2 (max c01 c02))
  8254. (catch 'exit
  8255. (while t
  8256. (catch 'nextline
  8257. (if (> l1 l2) (throw 'exit t))
  8258. (goto-line l1)
  8259. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8260. (setq cols nil ic1 c1 ic2 c2)
  8261. (while (< ic1 (1+ ic2))
  8262. (push (org-table-get-field ic1 rpl) cols)
  8263. (setq ic1 (1+ ic1)))
  8264. (push (nreverse cols) region)
  8265. (setq l1 (1+ l1)))))
  8266. (setq org-table-clip (nreverse region))
  8267. (if cut (org-table-align))
  8268. org-table-clip))
  8269. (defun org-table-paste-rectangle ()
  8270. "Paste a rectangular region into a table.
  8271. The upper right corner ends up in the current field. All involved fields
  8272. will be overwritten. If the rectangle does not fit into the present table,
  8273. the table is enlarged as needed. The process ignores horizontal separator
  8274. lines."
  8275. (interactive)
  8276. (unless (and org-table-clip (listp org-table-clip))
  8277. (error "First cut/copy a region to paste!"))
  8278. (org-table-check-inside-data-field)
  8279. (let* ((clip org-table-clip)
  8280. (line (org-current-line))
  8281. (col (org-table-current-column))
  8282. (org-enable-table-editor t)
  8283. (org-table-automatic-realign nil)
  8284. c cols field)
  8285. (while (setq cols (pop clip))
  8286. (while (org-at-table-hline-p) (beginning-of-line 2))
  8287. (if (not (org-at-table-p))
  8288. (progn (end-of-line 0) (org-table-next-field)))
  8289. (setq c col)
  8290. (while (setq field (pop cols))
  8291. (org-table-goto-column c nil 'force)
  8292. (org-table-get-field nil field)
  8293. (setq c (1+ c)))
  8294. (beginning-of-line 2))
  8295. (goto-line line)
  8296. (org-table-goto-column col)
  8297. (org-table-align)))
  8298. (defun org-table-convert ()
  8299. "Convert from `org-mode' table to table.el and back.
  8300. Obviously, this only works within limits. When an Org-mode table is
  8301. converted to table.el, all horizontal separator lines get lost, because
  8302. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8303. A table.el table can be converted to an Org-mode table only if it does not
  8304. do row or column spanning. Multiline cells will become multiple cells.
  8305. Beware, Org-mode does not test if the table can be successfully converted - it
  8306. blindly applies a recipe that works for simple tables."
  8307. (interactive)
  8308. (require 'table)
  8309. (if (org-at-table.el-p)
  8310. ;; convert to Org-mode table
  8311. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8312. (end (move-marker (make-marker) (org-table-end t))))
  8313. (table-unrecognize-region beg end)
  8314. (goto-char beg)
  8315. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8316. (replace-match ""))
  8317. (goto-char beg))
  8318. (if (org-at-table-p)
  8319. ;; convert to table.el table
  8320. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8321. (end (move-marker (make-marker) (org-table-end))))
  8322. ;; first, get rid of all horizontal lines
  8323. (goto-char beg)
  8324. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8325. (replace-match ""))
  8326. ;; insert a hline before first
  8327. (goto-char beg)
  8328. (org-table-insert-hline 'above)
  8329. (beginning-of-line -1)
  8330. ;; insert a hline after each line
  8331. (while (progn (beginning-of-line 3) (< (point) end))
  8332. (org-table-insert-hline))
  8333. (goto-char beg)
  8334. (setq end (move-marker end (org-table-end)))
  8335. ;; replace "+" at beginning and ending of hlines
  8336. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8337. (replace-match "\\1+-"))
  8338. (goto-char beg)
  8339. (while (re-search-forward "-|[ \t]*$" end t)
  8340. (replace-match "-+"))
  8341. (goto-char beg)))))
  8342. (defun org-table-wrap-region (arg)
  8343. "Wrap several fields in a column like a paragraph.
  8344. This is useful if you'd like to spread the contents of a field over several
  8345. lines, in order to keep the table compact.
  8346. If there is an active region, and both point and mark are in the same column,
  8347. the text in the column is wrapped to minimum width for the given number of
  8348. lines. Generally, this makes the table more compact. A prefix ARG may be
  8349. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8350. formats the selected text to two lines. If the region was longer than two
  8351. lines, the remaining lines remain empty. A negative prefix argument reduces
  8352. the current number of lines by that amount. The wrapped text is pasted back
  8353. into the table. If you formatted it to more lines than it was before, fields
  8354. further down in the table get overwritten - so you might need to make space in
  8355. the table first.
  8356. If there is no region, the current field is split at the cursor position and
  8357. the text fragment to the right of the cursor is prepended to the field one
  8358. line down.
  8359. If there is no region, but you specify a prefix ARG, the current field gets
  8360. blank, and the content is appended to the field above."
  8361. (interactive "P")
  8362. (org-table-check-inside-data-field)
  8363. (if (org-region-active-p)
  8364. ;; There is a region: fill as a paragraph
  8365. (let* ((beg (region-beginning))
  8366. (cline (save-excursion (goto-char beg) (org-current-line)))
  8367. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8368. nlines)
  8369. (org-table-cut-region (region-beginning) (region-end))
  8370. (if (> (length (car org-table-clip)) 1)
  8371. (error "Region must be limited to single column"))
  8372. (setq nlines (if arg
  8373. (if (< arg 1)
  8374. (+ (length org-table-clip) arg)
  8375. arg)
  8376. (length org-table-clip)))
  8377. (setq org-table-clip
  8378. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8379. nil nlines)))
  8380. (goto-line cline)
  8381. (org-table-goto-column ccol)
  8382. (org-table-paste-rectangle))
  8383. ;; No region, split the current field at point
  8384. (if arg
  8385. ;; combine with field above
  8386. (let ((s (org-table-blank-field))
  8387. (col (org-table-current-column)))
  8388. (beginning-of-line 0)
  8389. (while (org-at-table-hline-p) (beginning-of-line 0))
  8390. (org-table-goto-column col)
  8391. (skip-chars-forward "^|")
  8392. (skip-chars-backward " ")
  8393. (insert " " (org-trim s))
  8394. (org-table-align))
  8395. ;; split field
  8396. (when (looking-at "\\([^|]+\\)+|")
  8397. (let ((s (match-string 1)))
  8398. (replace-match " |")
  8399. (goto-char (match-beginning 0))
  8400. (org-table-next-row)
  8401. (insert (org-trim s) " ")
  8402. (org-table-align))))))
  8403. (defvar org-field-marker nil)
  8404. (defun org-table-edit-field (arg)
  8405. "Edit table field in a different window.
  8406. This is mainly useful for fields that contain hidden parts.
  8407. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8408. it can be edited in place."
  8409. (interactive "P")
  8410. (if arg
  8411. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8412. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8413. (remove-text-properties b e '(org-cwidth t invisible t
  8414. display t intangible t))
  8415. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8416. (font-lock-fontify-block)))
  8417. (let ((pos (move-marker (make-marker) (point)))
  8418. (field (org-table-get-field))
  8419. (cw (current-window-configuration))
  8420. p)
  8421. (org-switch-to-buffer-other-window "*Org tmp*")
  8422. (erase-buffer)
  8423. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8424. (let ((org-inhibit-startup t)) (org-mode))
  8425. (goto-char (setq p (point-max)))
  8426. (insert (org-trim field))
  8427. (remove-text-properties p (point-max)
  8428. '(invisible t org-cwidth t display t
  8429. intangible t))
  8430. (goto-char p)
  8431. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8432. (org-set-local 'org-window-configuration cw)
  8433. (org-set-local 'org-field-marker pos)
  8434. (message "Edit and finish with C-c C-c"))))
  8435. (defun org-table-finish-edit-field ()
  8436. "Finish editing a table data field.
  8437. Remove all newline characters, insert the result into the table, realign
  8438. the table and kill the editing buffer."
  8439. (let ((pos org-field-marker)
  8440. (cw org-window-configuration)
  8441. (cb (current-buffer))
  8442. text)
  8443. (goto-char (point-min))
  8444. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8445. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8446. (replace-match " "))
  8447. (setq text (org-trim (buffer-string)))
  8448. (set-window-configuration cw)
  8449. (kill-buffer cb)
  8450. (select-window (get-buffer-window (marker-buffer pos)))
  8451. (goto-char pos)
  8452. (move-marker pos nil)
  8453. (org-table-check-inside-data-field)
  8454. (org-table-get-field nil text)
  8455. (org-table-align)
  8456. (message "New field value inserted")))
  8457. (defun org-trim (s)
  8458. "Remove whitespace at beginning and end of string."
  8459. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8460. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8461. s)
  8462. (defun org-wrap (string &optional width lines)
  8463. "Wrap string to either a number of lines, or a width in characters.
  8464. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8465. that costs. If there is a word longer than WIDTH, the text is actually
  8466. wrapped to the length of that word.
  8467. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8468. many lines, whatever width that takes.
  8469. The return value is a list of lines, without newlines at the end."
  8470. (let* ((words (org-split-string string "[ \t\n]+"))
  8471. (maxword (apply 'max (mapcar 'org-string-width words)))
  8472. w ll)
  8473. (cond (width
  8474. (org-do-wrap words (max maxword width)))
  8475. (lines
  8476. (setq w maxword)
  8477. (setq ll (org-do-wrap words maxword))
  8478. (if (<= (length ll) lines)
  8479. ll
  8480. (setq ll words)
  8481. (while (> (length ll) lines)
  8482. (setq w (1+ w))
  8483. (setq ll (org-do-wrap words w)))
  8484. ll))
  8485. (t (error "Cannot wrap this")))))
  8486. (defun org-do-wrap (words width)
  8487. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8488. (let (lines line)
  8489. (while words
  8490. (setq line (pop words))
  8491. (while (and words (< (+ (length line) (length (car words))) width))
  8492. (setq line (concat line " " (pop words))))
  8493. (setq lines (push line lines)))
  8494. (nreverse lines)))
  8495. (defun org-split-string (string &optional separators)
  8496. "Splits STRING into substrings at SEPARATORS.
  8497. No empty strings are returned if there are matches at the beginning
  8498. and end of string."
  8499. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8500. (start 0)
  8501. notfirst
  8502. (list nil))
  8503. (while (and (string-match rexp string
  8504. (if (and notfirst
  8505. (= start (match-beginning 0))
  8506. (< start (length string)))
  8507. (1+ start) start))
  8508. (< (match-beginning 0) (length string)))
  8509. (setq notfirst t)
  8510. (or (eq (match-beginning 0) 0)
  8511. (and (eq (match-beginning 0) (match-end 0))
  8512. (eq (match-beginning 0) start))
  8513. (setq list
  8514. (cons (substring string start (match-beginning 0))
  8515. list)))
  8516. (setq start (match-end 0)))
  8517. (or (eq start (length string))
  8518. (setq list
  8519. (cons (substring string start)
  8520. list)))
  8521. (nreverse list)))
  8522. (defun org-table-map-tables (function)
  8523. "Apply FUNCTION to the start of all tables in the buffer."
  8524. (save-excursion
  8525. (save-restriction
  8526. (widen)
  8527. (goto-char (point-min))
  8528. (while (re-search-forward org-table-any-line-regexp nil t)
  8529. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8530. (beginning-of-line 1)
  8531. (if (looking-at org-table-line-regexp)
  8532. (save-excursion (funcall function)))
  8533. (re-search-forward org-table-any-border-regexp nil 1))))
  8534. (message "Mapping tables: done"))
  8535. (defvar org-timecnt) ; dynamically scoped parameter
  8536. (defun org-table-sum (&optional beg end nlast)
  8537. "Sum numbers in region of current table column.
  8538. The result will be displayed in the echo area, and will be available
  8539. as kill to be inserted with \\[yank].
  8540. If there is an active region, it is interpreted as a rectangle and all
  8541. numbers in that rectangle will be summed. If there is no active
  8542. region and point is located in a table column, sum all numbers in that
  8543. column.
  8544. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8545. numbers are assumed to be times as well (in decimal hours) and the
  8546. numbers are added as such.
  8547. If NLAST is a number, only the NLAST fields will actually be summed."
  8548. (interactive)
  8549. (save-excursion
  8550. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8551. (cond
  8552. ((and beg end)) ; beg and end given explicitly
  8553. ((org-region-active-p)
  8554. (setq beg (region-beginning) end (region-end)))
  8555. (t
  8556. (setq col (org-table-current-column))
  8557. (goto-char (org-table-begin))
  8558. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8559. (error "No table data"))
  8560. (org-table-goto-column col)
  8561. (setq beg (point))
  8562. (goto-char (org-table-end))
  8563. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8564. (error "No table data"))
  8565. (org-table-goto-column col)
  8566. (setq end (point))))
  8567. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8568. (items1 (cond ((not nlast) items)
  8569. ((>= nlast (length items)) items)
  8570. (t (setq items (reverse items))
  8571. (setcdr (nthcdr (1- nlast) items) nil)
  8572. (nreverse items))))
  8573. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8574. items1)))
  8575. (res (apply '+ numbers))
  8576. (sres (if (= org-timecnt 0)
  8577. (format "%g" res)
  8578. (setq diff (* 3600 res)
  8579. h (floor (/ diff 3600)) diff (mod diff 3600)
  8580. m (floor (/ diff 60)) diff (mod diff 60)
  8581. s diff)
  8582. (format "%d:%02d:%02d" h m s))))
  8583. (kill-new sres)
  8584. (if (interactive-p)
  8585. (message "%s"
  8586. (substitute-command-keys
  8587. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8588. (length numbers) sres))))
  8589. sres))))
  8590. (defun org-table-get-number-for-summing (s)
  8591. (let (n)
  8592. (if (string-match "^ *|? *" s)
  8593. (setq s (replace-match "" nil nil s)))
  8594. (if (string-match " *|? *$" s)
  8595. (setq s (replace-match "" nil nil s)))
  8596. (setq n (string-to-number s))
  8597. (cond
  8598. ((and (string-match "0" s)
  8599. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8600. ((string-match "\\`[ \t]+\\'" s) nil)
  8601. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8602. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8603. (m (string-to-number (or (match-string 2 s) "0")))
  8604. (s (string-to-number (or (match-string 4 s) "0"))))
  8605. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8606. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8607. ((equal n 0) nil)
  8608. (t n))))
  8609. (defun org-table-current-field-formula (&optional key noerror)
  8610. "Return the formula active for the current field.
  8611. Assumes that specials are in place.
  8612. If KEY is given, return the key to this formula.
  8613. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8614. (let* ((name (car (rassoc (list (org-current-line)
  8615. (org-table-current-column))
  8616. org-table-named-field-locations)))
  8617. (col (org-table-current-column))
  8618. (scol (int-to-string col))
  8619. (ref (format "@%d$%d" (org-table-current-dline) col))
  8620. (stored-list (org-table-get-stored-formulas noerror))
  8621. (ass (or (assoc name stored-list)
  8622. (assoc ref stored-list)
  8623. (assoc scol stored-list))))
  8624. (if key
  8625. (car ass)
  8626. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8627. (cdr ass))))))
  8628. (defun org-table-get-formula (&optional equation named)
  8629. "Read a formula from the minibuffer, offer stored formula as default.
  8630. When NAMED is non-nil, look for a named equation."
  8631. (let* ((stored-list (org-table-get-stored-formulas))
  8632. (name (car (rassoc (list (org-current-line)
  8633. (org-table-current-column))
  8634. org-table-named-field-locations)))
  8635. (ref (format "@%d$%d" (org-table-current-dline)
  8636. (org-table-current-column)))
  8637. (refass (assoc ref stored-list))
  8638. (scol (if named
  8639. (if name name ref)
  8640. (int-to-string (org-table-current-column))))
  8641. (dummy (and (or name refass) (not named)
  8642. (not (y-or-n-p "Replace field formula with column formula? " ))
  8643. (error "Abort")))
  8644. (name (or name ref))
  8645. (org-table-may-need-update nil)
  8646. (stored (cdr (assoc scol stored-list)))
  8647. (eq (cond
  8648. ((and stored equation (string-match "^ *=? *$" equation))
  8649. stored)
  8650. ((stringp equation)
  8651. equation)
  8652. (t (org-table-formula-from-user
  8653. (read-string
  8654. (org-table-formula-to-user
  8655. (format "%s formula %s%s="
  8656. (if named "Field" "Column")
  8657. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8658. scol))
  8659. (if stored (org-table-formula-to-user stored) "")
  8660. 'org-table-formula-history
  8661. )))))
  8662. mustsave)
  8663. (when (not (string-match "\\S-" eq))
  8664. ;; remove formula
  8665. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8666. (org-table-store-formulas stored-list)
  8667. (error "Formula removed"))
  8668. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8669. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8670. (if (and name (not named))
  8671. ;; We set the column equation, delete the named one.
  8672. (setq stored-list (delq (assoc name stored-list) stored-list)
  8673. mustsave t))
  8674. (if stored
  8675. (setcdr (assoc scol stored-list) eq)
  8676. (setq stored-list (cons (cons scol eq) stored-list)))
  8677. (if (or mustsave (not (equal stored eq)))
  8678. (org-table-store-formulas stored-list))
  8679. eq))
  8680. (defun org-table-store-formulas (alist)
  8681. "Store the list of formulas below the current table."
  8682. (setq alist (sort alist 'org-table-formula-less-p))
  8683. (save-excursion
  8684. (goto-char (org-table-end))
  8685. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8686. (progn
  8687. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8688. (goto-char (match-beginning 2))
  8689. (delete-region (match-beginning 2) (match-end 0)))
  8690. (insert "#+TBLFM:"))
  8691. (insert " "
  8692. (mapconcat (lambda (x)
  8693. (concat
  8694. (if (equal (string-to-char (car x)) ?@) "" "$")
  8695. (car x) "=" (cdr x)))
  8696. alist "::")
  8697. "\n")))
  8698. (defsubst org-table-formula-make-cmp-string (a)
  8699. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8700. (concat
  8701. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8702. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8703. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8704. (defun org-table-formula-less-p (a b)
  8705. "Compare two formulas for sorting."
  8706. (let ((as (org-table-formula-make-cmp-string (car a)))
  8707. (bs (org-table-formula-make-cmp-string (car b))))
  8708. (and as bs (string< as bs))))
  8709. (defun org-table-get-stored-formulas (&optional noerror)
  8710. "Return an alist with the stored formulas directly after current table."
  8711. (interactive)
  8712. (let (scol eq eq-alist strings string seen)
  8713. (save-excursion
  8714. (goto-char (org-table-end))
  8715. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8716. (setq strings (org-split-string (match-string 2) " *:: *"))
  8717. (while (setq string (pop strings))
  8718. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8719. (setq scol (if (match-end 2)
  8720. (match-string 2 string)
  8721. (match-string 1 string))
  8722. eq (match-string 3 string)
  8723. eq-alist (cons (cons scol eq) eq-alist))
  8724. (if (member scol seen)
  8725. (if noerror
  8726. (progn
  8727. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8728. (ding)
  8729. (sit-for 2))
  8730. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8731. (push scol seen))))))
  8732. (nreverse eq-alist)))
  8733. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8734. "Modify the equations after the table structure has been edited.
  8735. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8736. For all numbers larger than LIMIT, shift them by DELTA."
  8737. (save-excursion
  8738. (goto-char (org-table-end))
  8739. (when (looking-at "#\\+TBLFM:")
  8740. (let ((re (concat key "\\([0-9]+\\)"))
  8741. (re2
  8742. (when remove
  8743. (if (equal key "$")
  8744. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8745. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8746. s n a)
  8747. (when remove
  8748. (while (re-search-forward re2 (point-at-eol) t)
  8749. (replace-match "")))
  8750. (while (re-search-forward re (point-at-eol) t)
  8751. (setq s (match-string 1) n (string-to-number s))
  8752. (cond
  8753. ((setq a (assoc s replace))
  8754. (replace-match (concat key (cdr a)) t t))
  8755. ((and limit (> n limit))
  8756. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8757. (defun org-table-get-specials ()
  8758. "Get the column names and local parameters for this table."
  8759. (save-excursion
  8760. (let ((beg (org-table-begin)) (end (org-table-end))
  8761. names name fields fields1 field cnt
  8762. c v l line col types dlines hlines)
  8763. (setq org-table-column-names nil
  8764. org-table-local-parameters nil
  8765. org-table-named-field-locations nil
  8766. org-table-current-begin-line nil
  8767. org-table-current-begin-pos nil
  8768. org-table-current-line-types nil)
  8769. (goto-char beg)
  8770. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8771. (setq names (org-split-string (match-string 1) " *| *")
  8772. cnt 1)
  8773. (while (setq name (pop names))
  8774. (setq cnt (1+ cnt))
  8775. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8776. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8777. (setq org-table-column-names (nreverse org-table-column-names))
  8778. (setq org-table-column-name-regexp
  8779. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8780. (goto-char beg)
  8781. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8782. (setq fields (org-split-string (match-string 1) " *| *"))
  8783. (while (setq field (pop fields))
  8784. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8785. (push (cons (match-string 1 field) (match-string 2 field))
  8786. org-table-local-parameters))))
  8787. (goto-char beg)
  8788. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8789. (setq c (match-string 1)
  8790. fields (org-split-string (match-string 2) " *| *"))
  8791. (save-excursion
  8792. (beginning-of-line (if (equal c "_") 2 0))
  8793. (setq line (org-current-line) col 1)
  8794. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8795. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8796. (while (and fields1 (setq field (pop fields)))
  8797. (setq v (pop fields1) col (1+ col))
  8798. (when (and (stringp field) (stringp v)
  8799. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8800. (push (cons field v) org-table-local-parameters)
  8801. (push (list field line col) org-table-named-field-locations))))
  8802. ;; Analyse the line types
  8803. (goto-char beg)
  8804. (setq org-table-current-begin-line (org-current-line)
  8805. org-table-current-begin-pos (point)
  8806. l org-table-current-begin-line)
  8807. (while (looking-at "[ \t]*|\\(-\\)?")
  8808. (push (if (match-end 1) 'hline 'dline) types)
  8809. (if (match-end 1) (push l hlines) (push l dlines))
  8810. (beginning-of-line 2)
  8811. (setq l (1+ l)))
  8812. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8813. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8814. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8815. (defun org-table-maybe-eval-formula ()
  8816. "Check if the current field starts with \"=\" or \":=\".
  8817. If yes, store the formula and apply it."
  8818. ;; We already know we are in a table. Get field will only return a formula
  8819. ;; when appropriate. It might return a separator line, but no problem.
  8820. (when org-table-formula-evaluate-inline
  8821. (let* ((field (org-trim (or (org-table-get-field) "")))
  8822. named eq)
  8823. (when (string-match "^:?=\\(.*\\)" field)
  8824. (setq named (equal (string-to-char field) ?:)
  8825. eq (match-string 1 field))
  8826. (if (or (fboundp 'calc-eval)
  8827. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8828. (org-table-eval-formula (if named '(4) nil)
  8829. (org-table-formula-from-user eq))
  8830. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8831. (defvar org-recalc-commands nil
  8832. "List of commands triggering the recalculation of a line.
  8833. Will be filled automatically during use.")
  8834. (defvar org-recalc-marks
  8835. '((" " . "Unmarked: no special line, no automatic recalculation")
  8836. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8837. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8838. ("!" . "Column name definition line. Reference in formula as $name.")
  8839. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8840. ("_" . "Names for values in row below this one.")
  8841. ("^" . "Names for values in row above this one.")))
  8842. (defun org-table-rotate-recalc-marks (&optional newchar)
  8843. "Rotate the recalculation mark in the first column.
  8844. If in any row, the first field is not consistent with a mark,
  8845. insert a new column for the markers.
  8846. When there is an active region, change all the lines in the region,
  8847. after prompting for the marking character.
  8848. After each change, a message will be displayed indicating the meaning
  8849. of the new mark."
  8850. (interactive)
  8851. (unless (org-at-table-p) (error "Not at a table"))
  8852. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8853. (beg (org-table-begin))
  8854. (end (org-table-end))
  8855. (l (org-current-line))
  8856. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8857. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8858. (have-col
  8859. (save-excursion
  8860. (goto-char beg)
  8861. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8862. (col (org-table-current-column))
  8863. (forcenew (car (assoc newchar org-recalc-marks)))
  8864. epos new)
  8865. (when l1
  8866. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8867. (setq newchar (char-to-string (read-char-exclusive))
  8868. forcenew (car (assoc newchar org-recalc-marks))))
  8869. (if (and newchar (not forcenew))
  8870. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8871. newchar))
  8872. (if l1 (goto-line l1))
  8873. (save-excursion
  8874. (beginning-of-line 1)
  8875. (unless (looking-at org-table-dataline-regexp)
  8876. (error "Not at a table data line")))
  8877. (unless have-col
  8878. (org-table-goto-column 1)
  8879. (org-table-insert-column)
  8880. (org-table-goto-column (1+ col)))
  8881. (setq epos (point-at-eol))
  8882. (save-excursion
  8883. (beginning-of-line 1)
  8884. (org-table-get-field
  8885. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8886. (concat " "
  8887. (setq new (or forcenew
  8888. (cadr (member (match-string 1) marks))))
  8889. " ")
  8890. " # ")))
  8891. (if (and l1 l2)
  8892. (progn
  8893. (goto-line l1)
  8894. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8895. (and (looking-at org-table-dataline-regexp)
  8896. (org-table-get-field 1 (concat " " new " "))))
  8897. (goto-line l1)))
  8898. (if (not (= epos (point-at-eol))) (org-table-align))
  8899. (goto-line l)
  8900. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  8901. (defun org-table-maybe-recalculate-line ()
  8902. "Recompute the current line if marked for it, and if we haven't just done it."
  8903. (interactive)
  8904. (and org-table-allow-automatic-line-recalculation
  8905. (not (and (memq last-command org-recalc-commands)
  8906. (equal org-last-recalc-line (org-current-line))))
  8907. (save-excursion (beginning-of-line 1)
  8908. (looking-at org-table-auto-recalculate-regexp))
  8909. (org-table-recalculate) t))
  8910. (defvar org-table-formula-debug nil
  8911. "Non-nil means, debug table formulas.
  8912. When nil, simply write \"#ERROR\" in corrupted fields.")
  8913. (make-variable-buffer-local 'org-table-formula-debug)
  8914. (defvar modes)
  8915. (defsubst org-set-calc-mode (var &optional value)
  8916. (if (stringp var)
  8917. (setq var (assoc var '(("D" calc-angle-mode deg)
  8918. ("R" calc-angle-mode rad)
  8919. ("F" calc-prefer-frac t)
  8920. ("S" calc-symbolic-mode t)))
  8921. value (nth 2 var) var (nth 1 var)))
  8922. (if (memq var modes)
  8923. (setcar (cdr (memq var modes)) value)
  8924. (cons var (cons value modes)))
  8925. modes)
  8926. (defun org-table-eval-formula (&optional arg equation
  8927. suppress-align suppress-const
  8928. suppress-store suppress-analysis)
  8929. "Replace the table field value at the cursor by the result of a calculation.
  8930. This function makes use of Dave Gillespie's Calc package, in my view the
  8931. most exciting program ever written for GNU Emacs. So you need to have Calc
  8932. installed in order to use this function.
  8933. In a table, this command replaces the value in the current field with the
  8934. result of a formula. It also installs the formula as the \"current\" column
  8935. formula, by storing it in a special line below the table. When called
  8936. with a `C-u' prefix, the current field must ba a named field, and the
  8937. formula is installed as valid in only this specific field.
  8938. When called with two `C-u' prefixes, insert the active equation
  8939. for the field back into the current field, so that it can be
  8940. edited there. This is useful in order to use \\[org-table-show-reference]
  8941. to check the referenced fields.
  8942. When called, the command first prompts for a formula, which is read in
  8943. the minibuffer. Previously entered formulas are available through the
  8944. history list, and the last used formula is offered as a default.
  8945. These stored formulas are adapted correctly when moving, inserting, or
  8946. deleting columns with the corresponding commands.
  8947. The formula can be any algebraic expression understood by the Calc package.
  8948. For details, see the Org-mode manual.
  8949. This function can also be called from Lisp programs and offers
  8950. additional arguments: EQUATION can be the formula to apply. If this
  8951. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  8952. used to speed-up recursive calls by by-passing unnecessary aligns.
  8953. SUPPRESS-CONST suppresses the interpretation of constants in the
  8954. formula, assuming that this has been done already outside the function.
  8955. SUPPRESS-STORE means the formula should not be stored, either because
  8956. it is already stored, or because it is a modified equation that should
  8957. not overwrite the stored one."
  8958. (interactive "P")
  8959. (org-table-check-inside-data-field)
  8960. (or suppress-analysis (org-table-get-specials))
  8961. (if (equal arg '(16))
  8962. (let ((eq (org-table-current-field-formula)))
  8963. (or eq (error "No equation active for current field"))
  8964. (org-table-get-field nil eq)
  8965. (org-table-align)
  8966. (setq org-table-may-need-update t))
  8967. (let* (fields
  8968. (ndown (if (integerp arg) arg 1))
  8969. (org-table-automatic-realign nil)
  8970. (case-fold-search nil)
  8971. (down (> ndown 1))
  8972. (formula (if (and equation suppress-store)
  8973. equation
  8974. (org-table-get-formula equation (equal arg '(4)))))
  8975. (n0 (org-table-current-column))
  8976. (modes (copy-sequence org-calc-default-modes))
  8977. (numbers nil) ; was a variable, now fixed default
  8978. (keep-empty nil)
  8979. n form form0 bw fmt x ev orig c lispp literal)
  8980. ;; Parse the format string. Since we have a lot of modes, this is
  8981. ;; a lot of work. However, I think calc still uses most of the time.
  8982. (if (string-match ";" formula)
  8983. (let ((tmp (org-split-string formula ";")))
  8984. (setq formula (car tmp)
  8985. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  8986. (nth 1 tmp)))
  8987. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  8988. (setq c (string-to-char (match-string 1 fmt))
  8989. n (string-to-number (match-string 2 fmt)))
  8990. (if (= c ?p)
  8991. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  8992. (setq modes (org-set-calc-mode
  8993. 'calc-float-format
  8994. (list (cdr (assoc c '((?n . float) (?f . fix)
  8995. (?s . sci) (?e . eng))))
  8996. n))))
  8997. (setq fmt (replace-match "" t t fmt)))
  8998. (if (string-match "[NT]" fmt)
  8999. (setq numbers (equal (match-string 0 fmt) "N")
  9000. fmt (replace-match "" t t fmt)))
  9001. (if (string-match "L" fmt)
  9002. (setq literal t
  9003. fmt (replace-match "" t t fmt)))
  9004. (if (string-match "E" fmt)
  9005. (setq keep-empty t
  9006. fmt (replace-match "" t t fmt)))
  9007. (while (string-match "[DRFS]" fmt)
  9008. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9009. (setq fmt (replace-match "" t t fmt)))
  9010. (unless (string-match "\\S-" fmt)
  9011. (setq fmt nil))))
  9012. (if (and (not suppress-const) org-table-formula-use-constants)
  9013. (setq formula (org-table-formula-substitute-names formula)))
  9014. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9015. (while (> ndown 0)
  9016. (setq fields (org-split-string
  9017. (org-no-properties
  9018. (buffer-substring (point-at-bol) (point-at-eol)))
  9019. " *| *"))
  9020. (if (eq numbers t)
  9021. (setq fields (mapcar
  9022. (lambda (x) (number-to-string (string-to-number x)))
  9023. fields)))
  9024. (setq ndown (1- ndown))
  9025. (setq form (copy-sequence formula)
  9026. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9027. (if (and lispp literal) (setq lispp 'literal))
  9028. ;; Check for old vertical references
  9029. (setq form (org-rewrite-old-row-references form))
  9030. ;; Insert complex ranges
  9031. (while (string-match org-table-range-regexp form)
  9032. (setq form
  9033. (replace-match
  9034. (save-match-data
  9035. (org-table-make-reference
  9036. (org-table-get-range (match-string 0 form) nil n0)
  9037. keep-empty numbers lispp))
  9038. t t form)))
  9039. ;; Insert simple ranges
  9040. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9041. (setq form
  9042. (replace-match
  9043. (save-match-data
  9044. (org-table-make-reference
  9045. (org-sublist
  9046. fields (string-to-number (match-string 1 form))
  9047. (string-to-number (match-string 2 form)))
  9048. keep-empty numbers lispp))
  9049. t t form)))
  9050. (setq form0 form)
  9051. ;; Insert the references to fields in same row
  9052. (while (string-match "\\$\\([0-9]+\\)" form)
  9053. (setq n (string-to-number (match-string 1 form))
  9054. x (nth (1- (if (= n 0) n0 n)) fields))
  9055. (unless x (error "Invalid field specifier \"%s\""
  9056. (match-string 0 form)))
  9057. (setq form (replace-match
  9058. (save-match-data
  9059. (org-table-make-reference x nil numbers lispp))
  9060. t t form)))
  9061. (if lispp
  9062. (setq ev (condition-case nil
  9063. (eval (eval (read form)))
  9064. (error "#ERROR"))
  9065. ev (if (numberp ev) (number-to-string ev) ev))
  9066. (or (fboundp 'calc-eval)
  9067. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9068. (setq ev (calc-eval (cons form modes)
  9069. (if numbers 'num))))
  9070. (when org-table-formula-debug
  9071. (with-output-to-temp-buffer "*Substitution History*"
  9072. (princ (format "Substitution history of formula
  9073. Orig: %s
  9074. $xyz-> %s
  9075. @r$c-> %s
  9076. $1-> %s\n" orig formula form0 form))
  9077. (if (listp ev)
  9078. (princ (format " %s^\nError: %s"
  9079. (make-string (car ev) ?\-) (nth 1 ev)))
  9080. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9081. ev (or fmt "NONE")
  9082. (if fmt (format fmt (string-to-number ev)) ev)))))
  9083. (setq bw (get-buffer-window "*Substitution History*"))
  9084. (shrink-window-if-larger-than-buffer bw)
  9085. (unless (and (interactive-p) (not ndown))
  9086. (unless (let (inhibit-redisplay)
  9087. (y-or-n-p "Debugging Formula. Continue to next? "))
  9088. (org-table-align)
  9089. (error "Abort"))
  9090. (delete-window bw)
  9091. (message "")))
  9092. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9093. (org-table-justify-field-maybe
  9094. (if fmt (format fmt (string-to-number ev)) ev))
  9095. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9096. (call-interactively 'org-return)
  9097. (setq ndown 0)))
  9098. (and down (org-table-maybe-recalculate-line))
  9099. (or suppress-align (and org-table-may-need-update
  9100. (org-table-align))))))
  9101. (defun org-table-put-field-property (prop value)
  9102. (save-excursion
  9103. (put-text-property (progn (skip-chars-backward "^|") (point))
  9104. (progn (skip-chars-forward "^|") (point))
  9105. prop value)))
  9106. (defun org-table-get-range (desc &optional tbeg col highlight)
  9107. "Get a calc vector from a column, accorting to descriptor DESC.
  9108. Optional arguments TBEG and COL can give the beginning of the table and
  9109. the current column, to avoid unnecessary parsing.
  9110. HIGHLIGHT means, just highlight the range."
  9111. (if (not (equal (string-to-char desc) ?@))
  9112. (setq desc (concat "@" desc)))
  9113. (save-excursion
  9114. (or tbeg (setq tbeg (org-table-begin)))
  9115. (or col (setq col (org-table-current-column)))
  9116. (let ((thisline (org-current-line))
  9117. beg end c1 c2 r1 r2 rangep tmp)
  9118. (unless (string-match org-table-range-regexp desc)
  9119. (error "Invalid table range specifier `%s'" desc))
  9120. (setq rangep (match-end 3)
  9121. r1 (and (match-end 1) (match-string 1 desc))
  9122. r2 (and (match-end 4) (match-string 4 desc))
  9123. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9124. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9125. (and c1 (setq c1 (+ (string-to-number c1)
  9126. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9127. (and c2 (setq c2 (+ (string-to-number c2)
  9128. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9129. (if (equal r1 "") (setq r1 nil))
  9130. (if (equal r2 "") (setq r2 nil))
  9131. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9132. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9133. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9134. (if (not r1) (setq r1 thisline))
  9135. (if (not r2) (setq r2 thisline))
  9136. (if (not c1) (setq c1 col))
  9137. (if (not c2) (setq c2 col))
  9138. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9139. ;; just one field
  9140. (progn
  9141. (goto-line r1)
  9142. (while (not (looking-at org-table-dataline-regexp))
  9143. (beginning-of-line 2))
  9144. (prog1 (org-trim (org-table-get-field c1))
  9145. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9146. ;; A range, return a vector
  9147. ;; First sort the numbers to get a regular ractangle
  9148. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9149. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9150. (goto-line r1)
  9151. (while (not (looking-at org-table-dataline-regexp))
  9152. (beginning-of-line 2))
  9153. (org-table-goto-column c1)
  9154. (setq beg (point))
  9155. (goto-line r2)
  9156. (while (not (looking-at org-table-dataline-regexp))
  9157. (beginning-of-line 0))
  9158. (org-table-goto-column c2)
  9159. (setq end (point))
  9160. (if highlight
  9161. (org-table-highlight-rectangle
  9162. beg (progn (skip-chars-forward "^|\n") (point))))
  9163. ;; return string representation of calc vector
  9164. (mapcar 'org-trim
  9165. (apply 'append (org-table-copy-region beg end)))))))
  9166. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9167. "Analyze descriptor DESC and retrieve the corresponding line number.
  9168. The cursor is currently in line CLINE, the table begins in line BLINE,
  9169. and TABLE is a vector with line types."
  9170. (if (string-match "^[0-9]+$" desc)
  9171. (aref org-table-dlines (string-to-number desc))
  9172. (setq cline (or cline (org-current-line))
  9173. bline (or bline org-table-current-begin-line)
  9174. table (or table org-table-current-line-types))
  9175. (if (or
  9176. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9177. ;; 1 2 3 4 5 6
  9178. (and (not (match-end 3)) (not (match-end 6)))
  9179. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9180. (error "invalid row descriptor `%s'" desc))
  9181. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9182. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9183. (odir (and (match-end 5) (match-string 5 desc)))
  9184. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9185. (i (- cline bline))
  9186. (rel (and (match-end 6)
  9187. (or (and (match-end 1) (not (match-end 3)))
  9188. (match-end 5)))))
  9189. (if (and hn (not hdir))
  9190. (progn
  9191. (setq i 0 hdir "+")
  9192. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9193. (if (and (not hn) on (not odir))
  9194. (error "should never happen");;(aref org-table-dlines on)
  9195. (if (and hn (> hn 0))
  9196. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9197. (if on
  9198. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9199. (+ bline i)))))
  9200. (defun org-find-row-type (table i type backwards relative n)
  9201. (let ((l (length table)))
  9202. (while (> n 0)
  9203. (while (and (setq i (+ i (if backwards -1 1)))
  9204. (>= i 0) (< i l)
  9205. (not (eq (aref table i) type))
  9206. (if (and relative (eq (aref table i) 'hline))
  9207. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9208. t)))
  9209. (setq n (1- n)))
  9210. (if (or (< i 0) (>= i l))
  9211. (error "Row descriptior leads outside table")
  9212. i)))
  9213. (defun org-rewrite-old-row-references (s)
  9214. (if (string-match "&[-+0-9I]" s)
  9215. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9216. s))
  9217. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9218. "Convert list ELEMENTS to something appropriate to insert into formula.
  9219. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9220. NUMBERS indicates that everything should be converted to numbers.
  9221. LISPP means to return something appropriate for a Lisp list."
  9222. (if (stringp elements) ; just a single val
  9223. (if lispp
  9224. (if (eq lispp 'literal)
  9225. elements
  9226. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9227. (if (equal elements "") (setq elements "0"))
  9228. (if numbers (number-to-string (string-to-number elements)) elements))
  9229. (unless keep-empty
  9230. (setq elements
  9231. (delq nil
  9232. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9233. elements))))
  9234. (setq elements (or elements '("0")))
  9235. (if lispp
  9236. (mapconcat
  9237. (lambda (x)
  9238. (if (eq lispp 'literal)
  9239. x
  9240. (prin1-to-string (if numbers (string-to-number x) x))))
  9241. elements " ")
  9242. (concat "[" (mapconcat
  9243. (lambda (x)
  9244. (if numbers (number-to-string (string-to-number x)) x))
  9245. elements
  9246. ",") "]"))))
  9247. (defun org-table-recalculate (&optional all noalign)
  9248. "Recalculate the current table line by applying all stored formulas.
  9249. With prefix arg ALL, do this for all lines in the table."
  9250. (interactive "P")
  9251. (or (memq this-command org-recalc-commands)
  9252. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9253. (unless (org-at-table-p) (error "Not at a table"))
  9254. (if (equal all '(16))
  9255. (org-table-iterate)
  9256. (org-table-get-specials)
  9257. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9258. (lambda (a b) (string< (car a) (car b)))))
  9259. (inhibit-redisplay (not debug-on-error))
  9260. (line-re org-table-dataline-regexp)
  9261. (thisline (org-current-line))
  9262. (thiscol (org-table-current-column))
  9263. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9264. ;; Insert constants in all formulas
  9265. (setq eqlist
  9266. (mapcar (lambda (x)
  9267. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9268. x)
  9269. eqlist))
  9270. ;; Split the equation list
  9271. (while (setq eq (pop eqlist))
  9272. (if (<= (string-to-char (car eq)) ?9)
  9273. (push eq eqlnum)
  9274. (push eq eqlname)))
  9275. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9276. (if all
  9277. (progn
  9278. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9279. (goto-char (setq beg (org-table-begin)))
  9280. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9281. ;; This is a table with marked lines, compute selected lines
  9282. (setq line-re org-table-recalculate-regexp)
  9283. ;; Move forward to the first non-header line
  9284. (if (and (re-search-forward org-table-dataline-regexp end t)
  9285. (re-search-forward org-table-hline-regexp end t)
  9286. (re-search-forward org-table-dataline-regexp end t))
  9287. (setq beg (match-beginning 0))
  9288. nil))) ;; just leave beg where it is
  9289. (setq beg (point-at-bol)
  9290. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9291. (goto-char beg)
  9292. (and all (message "Re-applying formulas to full table..."))
  9293. ;; First find the named fields, and mark them untouchanble
  9294. (remove-text-properties beg end '(org-untouchable t))
  9295. (while (setq eq (pop eqlname))
  9296. (setq name (car eq)
  9297. a (assoc name org-table-named-field-locations))
  9298. (and (not a)
  9299. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9300. (setq a (list name
  9301. (aref org-table-dlines
  9302. (string-to-number (match-string 1 name)))
  9303. (string-to-number (match-string 2 name)))))
  9304. (when (and a (or all (equal (nth 1 a) thisline)))
  9305. (message "Re-applying formula to field: %s" name)
  9306. (goto-line (nth 1 a))
  9307. (org-table-goto-column (nth 2 a))
  9308. (push (append a (list (cdr eq))) eqlname1)
  9309. (org-table-put-field-property :org-untouchable t)))
  9310. ;; Now evauluate the column formulas, but skip fields covered by
  9311. ;; field formulas
  9312. (goto-char beg)
  9313. (while (re-search-forward line-re end t)
  9314. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9315. ;; Unprotected line, recalculate
  9316. (and all (message "Re-applying formulas to full table...(line %d)"
  9317. (setq cnt (1+ cnt))))
  9318. (setq org-last-recalc-line (org-current-line))
  9319. (setq eql eqlnum)
  9320. (while (setq entry (pop eql))
  9321. (goto-line org-last-recalc-line)
  9322. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9323. (unless (get-text-property (point) :org-untouchable)
  9324. (org-table-eval-formula nil (cdr entry)
  9325. 'noalign 'nocst 'nostore 'noanalysis)))))
  9326. ;; Now evaluate the field formulas
  9327. (while (setq eq (pop eqlname1))
  9328. (message "Re-applying formula to field: %s" (car eq))
  9329. (goto-line (nth 1 eq))
  9330. (org-table-goto-column (nth 2 eq))
  9331. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9332. 'nostore 'noanalysis))
  9333. (goto-line thisline)
  9334. (org-table-goto-column thiscol)
  9335. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9336. (or noalign (and org-table-may-need-update (org-table-align))
  9337. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9338. ;; back to initial position
  9339. (message "Re-applying formulas...done")
  9340. (goto-line thisline)
  9341. (org-table-goto-column thiscol)
  9342. (or noalign (and org-table-may-need-update (org-table-align))
  9343. (and all (message "Re-applying formulas...done"))))))
  9344. (defun org-table-iterate (&optional arg)
  9345. "Recalculate the table until it does not change anymore."
  9346. (interactive "P")
  9347. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9348. (i 0)
  9349. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9350. thistbl)
  9351. (catch 'exit
  9352. (while (< i imax)
  9353. (setq i (1+ i))
  9354. (org-table-recalculate 'all)
  9355. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9356. (if (not (string= lasttbl thistbl))
  9357. (setq lasttbl thistbl)
  9358. (if (> i 1)
  9359. (message "Convergence after %d iterations" i)
  9360. (message "Table was already stable"))
  9361. (throw 'exit t)))
  9362. (error "No convergence after %d iterations" i))))
  9363. (defun org-table-formula-substitute-names (f)
  9364. "Replace $const with values in string F."
  9365. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9366. ;; First, check for column names
  9367. (while (setq start (string-match org-table-column-name-regexp f start))
  9368. (setq start (1+ start))
  9369. (setq a (assoc (match-string 1 f) org-table-column-names))
  9370. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9371. ;; Parameters and constants
  9372. (setq start 0)
  9373. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9374. (setq start (1+ start))
  9375. (if (setq a (save-match-data
  9376. (org-table-get-constant (match-string 1 f))))
  9377. (setq f (replace-match
  9378. (concat (if pp "(") a (if pp ")")) t t f))))
  9379. (if org-table-formula-debug
  9380. (put-text-property 0 (length f) :orig-formula f1 f))
  9381. f))
  9382. (defun org-table-get-constant (const)
  9383. "Find the value for a parameter or constant in a formula.
  9384. Parameters get priority."
  9385. (or (cdr (assoc const org-table-local-parameters))
  9386. (cdr (assoc const org-table-formula-constants-local))
  9387. (cdr (assoc const org-table-formula-constants))
  9388. (and (fboundp 'constants-get) (constants-get const))
  9389. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9390. (org-entry-get nil (substring const 5) 'inherit))
  9391. "#UNDEFINED_NAME"))
  9392. (defvar org-table-fedit-map
  9393. (let ((map (make-sparse-keymap)))
  9394. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9395. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9396. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9397. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9398. (org-defkey map "\C-c?" 'org-table-show-reference)
  9399. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9400. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9401. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9402. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9403. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9404. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9405. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9406. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9407. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9408. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9409. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9410. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9411. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9412. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9413. map))
  9414. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9415. '("Edit-Formulas"
  9416. ["Finish and Install" org-table-fedit-finish t]
  9417. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9418. ["Abort" org-table-fedit-abort t]
  9419. "--"
  9420. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9421. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9422. "--"
  9423. "Shift Reference at Point"
  9424. ["Up" org-table-fedit-ref-up t]
  9425. ["Down" org-table-fedit-ref-down t]
  9426. ["Left" org-table-fedit-ref-left t]
  9427. ["Right" org-table-fedit-ref-right t]
  9428. "-"
  9429. "Change Test Row for Column Formulas"
  9430. ["Up" org-table-fedit-line-up t]
  9431. ["Down" org-table-fedit-line-down t]
  9432. "--"
  9433. ["Scroll Table Window" org-table-fedit-scroll t]
  9434. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9435. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9436. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9437. org-table-overlay-coordinates)]
  9438. "--"
  9439. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9440. :style toggle :selected org-table-buffer-is-an]))
  9441. (defvar org-pos)
  9442. (defun org-table-edit-formulas ()
  9443. "Edit the formulas of the current table in a separate buffer."
  9444. (interactive)
  9445. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9446. (beginning-of-line 0))
  9447. (unless (org-at-table-p) (error "Not at a table"))
  9448. (org-table-get-specials)
  9449. (let ((key (org-table-current-field-formula 'key 'noerror))
  9450. (eql (sort (org-table-get-stored-formulas 'noerror)
  9451. 'org-table-formula-less-p))
  9452. (pos (move-marker (make-marker) (point)))
  9453. (startline 1)
  9454. (wc (current-window-configuration))
  9455. (titles '((column . "# Column Formulas\n")
  9456. (field . "# Field Formulas\n")
  9457. (named . "# Named Field Formulas\n")))
  9458. entry s type title)
  9459. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9460. (erase-buffer)
  9461. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9462. (let ((font-lock-global-modes '(not fundamental-mode)))
  9463. (fundamental-mode))
  9464. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9465. (org-set-local 'org-pos pos)
  9466. (org-set-local 'org-window-configuration wc)
  9467. (use-local-map org-table-fedit-map)
  9468. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9469. (easy-menu-add org-table-fedit-menu)
  9470. (setq startline (org-current-line))
  9471. (while (setq entry (pop eql))
  9472. (setq type (cond
  9473. ((equal (string-to-char (car entry)) ?@) 'field)
  9474. ((string-match "^[0-9]" (car entry)) 'column)
  9475. (t 'named)))
  9476. (when (setq title (assq type titles))
  9477. (or (bobp) (insert "\n"))
  9478. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9479. (setq titles (delq title titles)))
  9480. (if (equal key (car entry)) (setq startline (org-current-line)))
  9481. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9482. (car entry) " = " (cdr entry) "\n"))
  9483. (remove-text-properties 0 (length s) '(face nil) s)
  9484. (insert s))
  9485. (if (eq org-table-use-standard-references t)
  9486. (org-table-fedit-toggle-ref-type))
  9487. (goto-line startline)
  9488. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9489. (defun org-table-fedit-post-command ()
  9490. (when (not (memq this-command '(lisp-complete-symbol)))
  9491. (let ((win (selected-window)))
  9492. (save-excursion
  9493. (condition-case nil
  9494. (org-table-show-reference)
  9495. (error nil))
  9496. (select-window win)))))
  9497. (defun org-table-formula-to-user (s)
  9498. "Convert a formula from internal to user representation."
  9499. (if (eq org-table-use-standard-references t)
  9500. (org-table-convert-refs-to-an s)
  9501. s))
  9502. (defun org-table-formula-from-user (s)
  9503. "Convert a formula from user to internal representation."
  9504. (if org-table-use-standard-references
  9505. (org-table-convert-refs-to-rc s)
  9506. s))
  9507. (defun org-table-convert-refs-to-rc (s)
  9508. "Convert spreadsheet references from AB7 to @7$28.
  9509. Works for single references, but also for entire formulas and even the
  9510. full TBLFM line."
  9511. (let ((start 0))
  9512. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9513. (cond
  9514. ((match-end 3)
  9515. ;; format match, just advance
  9516. (setq start (match-end 0)))
  9517. ((and (> (match-beginning 0) 0)
  9518. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9519. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9520. ;; 3.e5 or something like this.
  9521. (setq start (match-end 0)))
  9522. (t
  9523. (setq start (match-beginning 0)
  9524. s (replace-match
  9525. (if (equal (match-string 2 s) "&")
  9526. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9527. (format "@%d$%d"
  9528. (string-to-number (match-string 2 s))
  9529. (org-letters-to-number (match-string 1 s))))
  9530. t t s)))))
  9531. s))
  9532. (defun org-table-convert-refs-to-an (s)
  9533. "Convert spreadsheet references from to @7$28 to AB7.
  9534. Works for single references, but also for entire formulas and even the
  9535. full TBLFM line."
  9536. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9537. (setq s (replace-match
  9538. (format "%s%d"
  9539. (org-number-to-letters
  9540. (string-to-number (match-string 2 s)))
  9541. (string-to-number (match-string 1 s)))
  9542. t t s)))
  9543. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9544. (setq s (replace-match (concat "\\1"
  9545. (org-number-to-letters
  9546. (string-to-number (match-string 2 s))) "&")
  9547. t nil s)))
  9548. s)
  9549. (defun org-letters-to-number (s)
  9550. "Convert a base 26 number represented by letters into an integer.
  9551. For example: AB -> 28."
  9552. (let ((n 0))
  9553. (setq s (upcase s))
  9554. (while (> (length s) 0)
  9555. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9556. s (substring s 1)))
  9557. n))
  9558. (defun org-number-to-letters (n)
  9559. "Convert an integer into a base 26 number represented by letters.
  9560. For example: 28 -> AB."
  9561. (let ((s ""))
  9562. (while (> n 0)
  9563. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9564. n (/ (1- n) 26)))
  9565. s))
  9566. (defun org-table-fedit-convert-buffer (function)
  9567. "Convert all references in this buffer, using FUNTION."
  9568. (let ((line (org-current-line)))
  9569. (goto-char (point-min))
  9570. (while (not (eobp))
  9571. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9572. (delete-region (point) (point-at-eol))
  9573. (or (eobp) (forward-char 1)))
  9574. (goto-line line)))
  9575. (defun org-table-fedit-toggle-ref-type ()
  9576. "Convert all references in the buffer from B3 to @3$2 and back."
  9577. (interactive)
  9578. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9579. (org-table-fedit-convert-buffer
  9580. (if org-table-buffer-is-an
  9581. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9582. (message "Reference type switched to %s"
  9583. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9584. (defun org-table-fedit-ref-up ()
  9585. "Shift the reference at point one row/hline up."
  9586. (interactive)
  9587. (org-table-fedit-shift-reference 'up))
  9588. (defun org-table-fedit-ref-down ()
  9589. "Shift the reference at point one row/hline down."
  9590. (interactive)
  9591. (org-table-fedit-shift-reference 'down))
  9592. (defun org-table-fedit-ref-left ()
  9593. "Shift the reference at point one field to the left."
  9594. (interactive)
  9595. (org-table-fedit-shift-reference 'left))
  9596. (defun org-table-fedit-ref-right ()
  9597. "Shift the reference at point one field to the right."
  9598. (interactive)
  9599. (org-table-fedit-shift-reference 'right))
  9600. (defun org-table-fedit-shift-reference (dir)
  9601. (cond
  9602. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9603. (if (memq dir '(left right))
  9604. (org-rematch-and-replace 1 (eq dir 'left))
  9605. (error "Cannot shift reference in this direction")))
  9606. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9607. ;; A B3-like reference
  9608. (if (memq dir '(up down))
  9609. (org-rematch-and-replace 2 (eq dir 'up))
  9610. (org-rematch-and-replace 1 (eq dir 'left))))
  9611. ((org-at-regexp-p
  9612. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9613. ;; An internal reference
  9614. (if (memq dir '(up down))
  9615. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9616. (org-rematch-and-replace 5 (eq dir 'left))))))
  9617. (defun org-rematch-and-replace (n &optional decr hline)
  9618. "Re-match the group N, and replace it with the shifted refrence."
  9619. (or (match-end n) (error "Cannot shift reference in this direction"))
  9620. (goto-char (match-beginning n))
  9621. (and (looking-at (regexp-quote (match-string n)))
  9622. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9623. t t)))
  9624. (defun org-shift-refpart (ref &optional decr hline)
  9625. "Shift a refrence part REF.
  9626. If DECR is set, decrease the references row/column, else increase.
  9627. If HLINE is set, this may be a hline reference, it certainly is not
  9628. a translation reference."
  9629. (save-match-data
  9630. (let* ((sign (string-match "^[-+]" ref)) n)
  9631. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9632. (cond
  9633. ((and hline (string-match "^I+" ref))
  9634. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9635. (setq n (+ n (if decr -1 1)))
  9636. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9637. (if sign
  9638. (setq sign (if (< n 0) "-" "+") n (abs n))
  9639. (setq n (max 1 n)))
  9640. (concat sign (make-string n ?I)))
  9641. ((string-match "^[0-9]+" ref)
  9642. (setq n (string-to-number (concat sign ref)))
  9643. (setq n (+ n (if decr -1 1)))
  9644. (if sign
  9645. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9646. (number-to-string (max 1 n))))
  9647. ((string-match "^[a-zA-Z]+" ref)
  9648. (org-number-to-letters
  9649. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9650. (t (error "Cannot shift reference"))))))
  9651. (defun org-table-fedit-toggle-coordinates ()
  9652. "Toggle the display of coordinates in the refrenced table."
  9653. (interactive)
  9654. (let ((pos (marker-position org-pos)))
  9655. (with-current-buffer (marker-buffer org-pos)
  9656. (save-excursion
  9657. (goto-char pos)
  9658. (org-table-toggle-coordinate-overlays)))))
  9659. (defun org-table-fedit-finish (&optional arg)
  9660. "Parse the buffer for formula definitions and install them.
  9661. With prefix ARG, apply the new formulas to the table."
  9662. (interactive "P")
  9663. (org-table-remove-rectangle-highlight)
  9664. (if org-table-use-standard-references
  9665. (progn
  9666. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9667. (setq org-table-buffer-is-an nil)))
  9668. (let ((pos org-pos) eql var form)
  9669. (goto-char (point-min))
  9670. (while (re-search-forward
  9671. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9672. nil t)
  9673. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9674. form (match-string 3))
  9675. (setq form (org-trim form))
  9676. (when (not (equal form ""))
  9677. (while (string-match "[ \t]*\n[ \t]*" form)
  9678. (setq form (replace-match " " t t form)))
  9679. (when (assoc var eql)
  9680. (error "Double formulas for %s" var))
  9681. (push (cons var form) eql)))
  9682. (setq org-pos nil)
  9683. (set-window-configuration org-window-configuration)
  9684. (select-window (get-buffer-window (marker-buffer pos)))
  9685. (goto-char pos)
  9686. (unless (org-at-table-p)
  9687. (error "Lost table position - cannot install formulae"))
  9688. (org-table-store-formulas eql)
  9689. (move-marker pos nil)
  9690. (kill-buffer "*Edit Formulas*")
  9691. (if arg
  9692. (org-table-recalculate 'all)
  9693. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9694. (defun org-table-fedit-abort ()
  9695. "Abort editing formulas, without installing the changes."
  9696. (interactive)
  9697. (org-table-remove-rectangle-highlight)
  9698. (let ((pos org-pos))
  9699. (set-window-configuration org-window-configuration)
  9700. (select-window (get-buffer-window (marker-buffer pos)))
  9701. (goto-char pos)
  9702. (move-marker pos nil)
  9703. (message "Formula editing aborted without installing changes")))
  9704. (defun org-table-fedit-lisp-indent ()
  9705. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9706. (interactive)
  9707. (let ((pos (point)) beg end ind)
  9708. (beginning-of-line 1)
  9709. (cond
  9710. ((looking-at "[ \t]")
  9711. (goto-char pos)
  9712. (call-interactively 'lisp-indent-line))
  9713. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9714. ((not (fboundp 'pp-buffer))
  9715. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9716. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9717. (goto-char (- (match-end 0) 2))
  9718. (setq beg (point))
  9719. (setq ind (make-string (current-column) ?\ ))
  9720. (condition-case nil (forward-sexp 1)
  9721. (error
  9722. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9723. (setq end (point))
  9724. (save-restriction
  9725. (narrow-to-region beg end)
  9726. (if (eq last-command this-command)
  9727. (progn
  9728. (goto-char (point-min))
  9729. (setq this-command nil)
  9730. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9731. (replace-match " ")))
  9732. (pp-buffer)
  9733. (untabify (point-min) (point-max))
  9734. (goto-char (1+ (point-min)))
  9735. (while (re-search-forward "^." nil t)
  9736. (beginning-of-line 1)
  9737. (insert ind))
  9738. (goto-char (point-max))
  9739. (backward-delete-char 1)))
  9740. (goto-char beg))
  9741. (t nil))))
  9742. (defvar org-show-positions nil)
  9743. (defun org-table-show-reference (&optional local)
  9744. "Show the location/value of the $ expression at point."
  9745. (interactive)
  9746. (org-table-remove-rectangle-highlight)
  9747. (catch 'exit
  9748. (let ((pos (if local (point) org-pos))
  9749. (face2 'highlight)
  9750. (org-inhibit-highlight-removal t)
  9751. (win (selected-window))
  9752. (org-show-positions nil)
  9753. var name e what match dest)
  9754. (if local (org-table-get-specials))
  9755. (setq what (cond
  9756. ((or (org-at-regexp-p org-table-range-regexp2)
  9757. (org-at-regexp-p org-table-translate-regexp)
  9758. (org-at-regexp-p org-table-range-regexp))
  9759. (setq match
  9760. (save-match-data
  9761. (org-table-convert-refs-to-rc (match-string 0))))
  9762. 'range)
  9763. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9764. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9765. ((not local) nil)
  9766. (t (error "No reference at point")))
  9767. match (and what (or match (match-string 0))))
  9768. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9769. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9770. 'secondary-selection))
  9771. (org-add-hook 'before-change-functions
  9772. 'org-table-remove-rectangle-highlight)
  9773. (if (eq what 'name) (setq var (substring match 1)))
  9774. (when (eq what 'range)
  9775. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9776. (setq match (org-table-formula-substitute-names match)))
  9777. (unless local
  9778. (save-excursion
  9779. (end-of-line 1)
  9780. (re-search-backward "^\\S-" nil t)
  9781. (beginning-of-line 1)
  9782. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9783. (setq dest
  9784. (save-match-data
  9785. (org-table-convert-refs-to-rc (match-string 1))))
  9786. (org-table-add-rectangle-overlay
  9787. (match-beginning 1) (match-end 1) face2))))
  9788. (if (and (markerp pos) (marker-buffer pos))
  9789. (if (get-buffer-window (marker-buffer pos))
  9790. (select-window (get-buffer-window (marker-buffer pos)))
  9791. (org-switch-to-buffer-other-window (get-buffer-window
  9792. (marker-buffer pos)))))
  9793. (goto-char pos)
  9794. (org-table-force-dataline)
  9795. (when dest
  9796. (setq name (substring dest 1))
  9797. (cond
  9798. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9799. (setq e (assoc name org-table-named-field-locations))
  9800. (goto-line (nth 1 e))
  9801. (org-table-goto-column (nth 2 e)))
  9802. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9803. (let ((l (string-to-number (match-string 1 dest)))
  9804. (c (string-to-number (match-string 2 dest))))
  9805. (goto-line (aref org-table-dlines l))
  9806. (org-table-goto-column c)))
  9807. (t (org-table-goto-column (string-to-number name))))
  9808. (move-marker pos (point))
  9809. (org-table-highlight-rectangle nil nil face2))
  9810. (cond
  9811. ((equal dest match))
  9812. ((not match))
  9813. ((eq what 'range)
  9814. (condition-case nil
  9815. (save-excursion
  9816. (org-table-get-range match nil nil 'highlight))
  9817. (error nil)))
  9818. ((setq e (assoc var org-table-named-field-locations))
  9819. (goto-line (nth 1 e))
  9820. (org-table-goto-column (nth 2 e))
  9821. (org-table-highlight-rectangle (point) (point))
  9822. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9823. ((setq e (assoc var org-table-column-names))
  9824. (org-table-goto-column (string-to-number (cdr e)))
  9825. (org-table-highlight-rectangle (point) (point))
  9826. (goto-char (org-table-begin))
  9827. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9828. (org-table-end) t)
  9829. (progn
  9830. (goto-char (match-beginning 1))
  9831. (org-table-highlight-rectangle)
  9832. (message "Named column (column %s)" (cdr e)))
  9833. (error "Column name not found")))
  9834. ((eq what 'column)
  9835. ;; column number
  9836. (org-table-goto-column (string-to-number (substring match 1)))
  9837. (org-table-highlight-rectangle (point) (point))
  9838. (message "Column %s" (substring match 1)))
  9839. ((setq e (assoc var org-table-local-parameters))
  9840. (goto-char (org-table-begin))
  9841. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9842. (progn
  9843. (goto-char (match-beginning 1))
  9844. (org-table-highlight-rectangle)
  9845. (message "Local parameter."))
  9846. (error "Parameter not found")))
  9847. (t
  9848. (cond
  9849. ((not var) (error "No reference at point"))
  9850. ((setq e (assoc var org-table-formula-constants-local))
  9851. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9852. var (cdr e)))
  9853. ((setq e (assoc var org-table-formula-constants))
  9854. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9855. var (cdr e)))
  9856. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9857. (message "Constant: $%s=%s, from `constants.el'%s."
  9858. var e (format " (%s units)" constants-unit-system)))
  9859. (t (error "Undefined name $%s" var)))))
  9860. (goto-char pos)
  9861. (when (and org-show-positions
  9862. (not (memq this-command '(org-table-fedit-scroll
  9863. org-table-fedit-scroll-down))))
  9864. (push pos org-show-positions)
  9865. (push org-table-current-begin-pos org-show-positions)
  9866. (let ((min (apply 'min org-show-positions))
  9867. (max (apply 'max org-show-positions)))
  9868. (goto-char min) (recenter 0)
  9869. (goto-char max)
  9870. (or (pos-visible-in-window-p max) (recenter -1))))
  9871. (select-window win))))
  9872. (defun org-table-force-dataline ()
  9873. "Make sure the cursor is in a dataline in a table."
  9874. (unless (save-excursion
  9875. (beginning-of-line 1)
  9876. (looking-at org-table-dataline-regexp))
  9877. (let* ((re org-table-dataline-regexp)
  9878. (p1 (save-excursion (re-search-forward re nil 'move)))
  9879. (p2 (save-excursion (re-search-backward re nil 'move))))
  9880. (cond ((and p1 p2)
  9881. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9882. p1 p2)))
  9883. ((or p1 p2) (goto-char (or p1 p2)))
  9884. (t (error "No table dataline around here"))))))
  9885. (defun org-table-fedit-line-up ()
  9886. "Move cursor one line up in the window showing the table."
  9887. (interactive)
  9888. (org-table-fedit-move 'previous-line))
  9889. (defun org-table-fedit-line-down ()
  9890. "Move cursor one line down in the window showing the table."
  9891. (interactive)
  9892. (org-table-fedit-move 'next-line))
  9893. (defun org-table-fedit-move (command)
  9894. "Move the cursor in the window shoinw the table.
  9895. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9896. (let ((org-table-allow-automatic-line-recalculation nil)
  9897. (pos org-pos) (win (selected-window)) p)
  9898. (select-window (get-buffer-window (marker-buffer org-pos)))
  9899. (setq p (point))
  9900. (call-interactively command)
  9901. (while (and (org-at-table-p)
  9902. (org-at-table-hline-p))
  9903. (call-interactively command))
  9904. (or (org-at-table-p) (goto-char p))
  9905. (move-marker pos (point))
  9906. (select-window win)))
  9907. (defun org-table-fedit-scroll (N)
  9908. (interactive "p")
  9909. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9910. (scroll-other-window N)))
  9911. (defun org-table-fedit-scroll-down (N)
  9912. (interactive "p")
  9913. (org-table-fedit-scroll (- N)))
  9914. (defvar org-table-rectangle-overlays nil)
  9915. (defun org-table-add-rectangle-overlay (beg end &optional face)
  9916. "Add a new overlay."
  9917. (let ((ov (org-make-overlay beg end)))
  9918. (org-overlay-put ov 'face (or face 'secondary-selection))
  9919. (push ov org-table-rectangle-overlays)))
  9920. (defun org-table-highlight-rectangle (&optional beg end face)
  9921. "Highlight rectangular region in a table."
  9922. (setq beg (or beg (point)) end (or end (point)))
  9923. (let ((b (min beg end))
  9924. (e (max beg end))
  9925. l1 c1 l2 c2 tmp)
  9926. (and (boundp 'org-show-positions)
  9927. (setq org-show-positions (cons b (cons e org-show-positions))))
  9928. (goto-char (min beg end))
  9929. (setq l1 (org-current-line)
  9930. c1 (org-table-current-column))
  9931. (goto-char (max beg end))
  9932. (setq l2 (org-current-line)
  9933. c2 (org-table-current-column))
  9934. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  9935. (goto-line l1)
  9936. (beginning-of-line 1)
  9937. (loop for line from l1 to l2 do
  9938. (when (looking-at org-table-dataline-regexp)
  9939. (org-table-goto-column c1)
  9940. (skip-chars-backward "^|\n") (setq beg (point))
  9941. (org-table-goto-column c2)
  9942. (skip-chars-forward "^|\n") (setq end (point))
  9943. (org-table-add-rectangle-overlay beg end face))
  9944. (beginning-of-line 2))
  9945. (goto-char b))
  9946. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  9947. (defun org-table-remove-rectangle-highlight (&rest ignore)
  9948. "Remove the rectangle overlays."
  9949. (unless org-inhibit-highlight-removal
  9950. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  9951. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  9952. (setq org-table-rectangle-overlays nil)))
  9953. (defvar org-table-coordinate-overlays nil
  9954. "Collects the cooordinate grid overlays, so that they can be removed.")
  9955. (make-variable-buffer-local 'org-table-coordinate-overlays)
  9956. (defun org-table-overlay-coordinates ()
  9957. "Add overlays to the table at point, to show row/column coordinates."
  9958. (interactive)
  9959. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9960. (setq org-table-coordinate-overlays nil)
  9961. (save-excursion
  9962. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  9963. (goto-char (org-table-begin))
  9964. (while (org-at-table-p)
  9965. (setq eol (point-at-eol))
  9966. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  9967. (push ov org-table-coordinate-overlays)
  9968. (setq hline (looking-at org-table-hline-regexp))
  9969. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  9970. (format "%4d" (setq id (1+ id)))))
  9971. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  9972. (when hline
  9973. (setq ic 0)
  9974. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  9975. (setq beg (1+ (match-beginning 0))
  9976. ic (1+ ic)
  9977. s1 (concat "$" (int-to-string ic))
  9978. s2 (org-number-to-letters ic)
  9979. str (if (eq org-table-use-standard-references t) s2 s1))
  9980. (setq ov (org-make-overlay beg (+ beg (length str))))
  9981. (push ov org-table-coordinate-overlays)
  9982. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  9983. (beginning-of-line 2)))))
  9984. (defun org-table-toggle-coordinate-overlays ()
  9985. "Toggle the display of Row/Column numbers in tables."
  9986. (interactive)
  9987. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  9988. (message "Row/Column number display turned %s"
  9989. (if org-table-overlay-coordinates "on" "off"))
  9990. (if (and (org-at-table-p) org-table-overlay-coordinates)
  9991. (org-table-align))
  9992. (unless org-table-overlay-coordinates
  9993. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9994. (setq org-table-coordinate-overlays nil)))
  9995. (defun org-table-toggle-formula-debugger ()
  9996. "Toggle the formula debugger in tables."
  9997. (interactive)
  9998. (setq org-table-formula-debug (not org-table-formula-debug))
  9999. (message "Formula debugging has been turned %s"
  10000. (if org-table-formula-debug "on" "off")))
  10001. ;;; The orgtbl minor mode
  10002. ;; Define a minor mode which can be used in other modes in order to
  10003. ;; integrate the org-mode table editor.
  10004. ;; This is really a hack, because the org-mode table editor uses several
  10005. ;; keys which normally belong to the major mode, for example the TAB and
  10006. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10007. ;; necessary to operate the table editor, but wraps the commands into a
  10008. ;; function which tests if the cursor is currently inside a table. If that
  10009. ;; is the case, the table editor command is executed. However, when any of
  10010. ;; those keys is used outside a table, the function uses `key-binding' to
  10011. ;; look up if the key has an associated command in another currently active
  10012. ;; keymap (minor modes, major mode, global), and executes that command.
  10013. ;; There might be problems if any of the keys used by the table editor is
  10014. ;; otherwise used as a prefix key.
  10015. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10016. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10017. ;; addresses this by checking explicitly for both bindings.
  10018. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10019. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10020. ;; Some modes bind other commands to simple characters, for example
  10021. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10022. ;; active, this binding is ignored inside tables and replaced with a
  10023. ;; modified self-insert.
  10024. (defvar orgtbl-mode nil
  10025. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10026. table editor in arbitrary modes.")
  10027. (make-variable-buffer-local 'orgtbl-mode)
  10028. (defvar orgtbl-mode-map (make-keymap)
  10029. "Keymap for `orgtbl-mode'.")
  10030. ;;;###autoload
  10031. (defun turn-on-orgtbl ()
  10032. "Unconditionally turn on `orgtbl-mode'."
  10033. (orgtbl-mode 1))
  10034. (defvar org-old-auto-fill-inhibit-regexp nil
  10035. "Local variable used by `orgtbl-mode'")
  10036. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10037. "Matches a line belonging to an orgtbl.")
  10038. (defconst orgtbl-extra-font-lock-keywords
  10039. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10040. 0 (quote 'org-table) 'prepend))
  10041. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10042. ;;;###autoload
  10043. (defun orgtbl-mode (&optional arg)
  10044. "The `org-mode' table editor as a minor mode for use in other modes."
  10045. (interactive)
  10046. (if (org-mode-p)
  10047. ;; Exit without error, in case some hook functions calls this
  10048. ;; by accident in org-mode.
  10049. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10050. (setq orgtbl-mode
  10051. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10052. (if orgtbl-mode
  10053. (progn
  10054. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10055. ;; Make sure we are first in minor-mode-map-alist
  10056. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10057. (and c (setq minor-mode-map-alist
  10058. (cons c (delq c minor-mode-map-alist)))))
  10059. (org-set-local (quote org-table-may-need-update) t)
  10060. (org-add-hook 'before-change-functions 'org-before-change-function
  10061. nil 'local)
  10062. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10063. auto-fill-inhibit-regexp)
  10064. (org-set-local 'auto-fill-inhibit-regexp
  10065. (if auto-fill-inhibit-regexp
  10066. (concat orgtbl-line-start-regexp "\\|"
  10067. auto-fill-inhibit-regexp)
  10068. orgtbl-line-start-regexp))
  10069. (org-add-to-invisibility-spec '(org-cwidth))
  10070. (when (fboundp 'font-lock-add-keywords)
  10071. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10072. (org-restart-font-lock))
  10073. (easy-menu-add orgtbl-mode-menu)
  10074. (run-hooks 'orgtbl-mode-hook))
  10075. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10076. (org-cleanup-narrow-column-properties)
  10077. (org-remove-from-invisibility-spec '(org-cwidth))
  10078. (remove-hook 'before-change-functions 'org-before-change-function t)
  10079. (when (fboundp 'font-lock-remove-keywords)
  10080. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10081. (org-restart-font-lock))
  10082. (easy-menu-remove orgtbl-mode-menu)
  10083. (force-mode-line-update 'all))))
  10084. (defun org-cleanup-narrow-column-properties ()
  10085. "Remove all properties related to narrow-column invisibility."
  10086. (let ((s 1))
  10087. (while (setq s (text-property-any s (point-max)
  10088. 'display org-narrow-column-arrow))
  10089. (remove-text-properties s (1+ s) '(display t)))
  10090. (setq s 1)
  10091. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10092. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10093. (setq s 1)
  10094. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10095. (remove-text-properties s (1+ s) '(invisible t)))))
  10096. ;; Install it as a minor mode.
  10097. (put 'orgtbl-mode :included t)
  10098. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10099. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10100. (defun orgtbl-make-binding (fun n &rest keys)
  10101. "Create a function for binding in the table minor mode.
  10102. FUN is the command to call inside a table. N is used to create a unique
  10103. command name. KEYS are keys that should be checked in for a command
  10104. to execute outside of tables."
  10105. (eval
  10106. (list 'defun
  10107. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10108. '(arg)
  10109. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10110. "Outside of tables, run the binding of `"
  10111. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10112. "'.")
  10113. '(interactive "p")
  10114. (list 'if
  10115. '(org-at-table-p)
  10116. (list 'call-interactively (list 'quote fun))
  10117. (list 'let '(orgtbl-mode)
  10118. (list 'call-interactively
  10119. (append '(or)
  10120. (mapcar (lambda (k)
  10121. (list 'key-binding k))
  10122. keys)
  10123. '('orgtbl-error))))))))
  10124. (defun orgtbl-error ()
  10125. "Error when there is no default binding for a table key."
  10126. (interactive)
  10127. (error "This key has no function outside tables"))
  10128. (defun orgtbl-setup ()
  10129. "Setup orgtbl keymaps."
  10130. (let ((nfunc 0)
  10131. (bindings
  10132. (list
  10133. '([(meta shift left)] org-table-delete-column)
  10134. '([(meta left)] org-table-move-column-left)
  10135. '([(meta right)] org-table-move-column-right)
  10136. '([(meta shift right)] org-table-insert-column)
  10137. '([(meta shift up)] org-table-kill-row)
  10138. '([(meta shift down)] org-table-insert-row)
  10139. '([(meta up)] org-table-move-row-up)
  10140. '([(meta down)] org-table-move-row-down)
  10141. '("\C-c\C-w" org-table-cut-region)
  10142. '("\C-c\M-w" org-table-copy-region)
  10143. '("\C-c\C-y" org-table-paste-rectangle)
  10144. '("\C-c-" org-table-insert-hline)
  10145. '("\C-c}" org-table-toggle-coordinate-overlays)
  10146. '("\C-c{" org-table-toggle-formula-debugger)
  10147. '("\C-m" org-table-next-row)
  10148. '([(shift return)] org-table-copy-down)
  10149. '("\C-c\C-q" org-table-wrap-region)
  10150. '("\C-c?" org-table-field-info)
  10151. '("\C-c " org-table-blank-field)
  10152. '("\C-c+" org-table-sum)
  10153. '("\C-c=" org-table-eval-formula)
  10154. '("\C-c'" org-table-edit-formulas)
  10155. '("\C-c`" org-table-edit-field)
  10156. '("\C-c*" org-table-recalculate)
  10157. '("\C-c|" org-table-create-or-convert-from-region)
  10158. '("\C-c^" org-table-sort-lines)
  10159. '([(control ?#)] org-table-rotate-recalc-marks)))
  10160. elt key fun cmd)
  10161. (while (setq elt (pop bindings))
  10162. (setq nfunc (1+ nfunc))
  10163. (setq key (org-key (car elt))
  10164. fun (nth 1 elt)
  10165. cmd (orgtbl-make-binding fun nfunc key))
  10166. (org-defkey orgtbl-mode-map key cmd))
  10167. ;; Special treatment needed for TAB and RET
  10168. (org-defkey orgtbl-mode-map [(return)]
  10169. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10170. (org-defkey orgtbl-mode-map "\C-m"
  10171. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10172. (org-defkey orgtbl-mode-map [(tab)]
  10173. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10174. (org-defkey orgtbl-mode-map "\C-i"
  10175. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10176. (org-defkey orgtbl-mode-map [(shift tab)]
  10177. (orgtbl-make-binding 'org-table-previous-field 104
  10178. [(shift tab)] [(tab)] "\C-i"))
  10179. (org-defkey orgtbl-mode-map "\M-\C-m"
  10180. (orgtbl-make-binding 'org-table-wrap-region 105
  10181. "\M-\C-m" [(meta return)]))
  10182. (org-defkey orgtbl-mode-map [(meta return)]
  10183. (orgtbl-make-binding 'org-table-wrap-region 106
  10184. [(meta return)] "\M-\C-m"))
  10185. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10186. (when orgtbl-optimized
  10187. ;; If the user wants maximum table support, we need to hijack
  10188. ;; some standard editing functions
  10189. (org-remap orgtbl-mode-map
  10190. 'self-insert-command 'orgtbl-self-insert-command
  10191. 'delete-char 'org-delete-char
  10192. 'delete-backward-char 'org-delete-backward-char)
  10193. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10194. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10195. '("OrgTbl"
  10196. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10197. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10198. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10199. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10200. "--"
  10201. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10202. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10203. ["Copy Field from Above"
  10204. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10205. "--"
  10206. ("Column"
  10207. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10208. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10209. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10210. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10211. ("Row"
  10212. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10213. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10214. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10215. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10216. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10217. "--"
  10218. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10219. ("Rectangle"
  10220. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10221. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10222. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10223. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10224. "--"
  10225. ("Radio tables"
  10226. ["Insert table template" orgtbl-insert-radio-table
  10227. (assq major-mode orgtbl-radio-table-templates)]
  10228. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10229. "--"
  10230. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10231. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10232. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10233. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10234. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10235. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10236. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10237. ["Sum Column/Rectangle" org-table-sum
  10238. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10239. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10240. ["Debug Formulas"
  10241. org-table-toggle-formula-debugger :active (org-at-table-p)
  10242. :keys "C-c {"
  10243. :style toggle :selected org-table-formula-debug]
  10244. ["Show Col/Row Numbers"
  10245. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10246. :keys "C-c }"
  10247. :style toggle :selected org-table-overlay-coordinates]
  10248. ))
  10249. t))
  10250. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10251. "If the cursor is inside a table, realign the table.
  10252. It it is a table to be sent away to a receiver, do it.
  10253. With prefix arg, also recompute table."
  10254. (interactive "P")
  10255. (let ((pos (point)) action)
  10256. (save-excursion
  10257. (beginning-of-line 1)
  10258. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10259. ((looking-at "[ \t]*|") pos)
  10260. ((looking-at "#\\+TBLFM:") 'recalc))))
  10261. (cond
  10262. ((integerp action)
  10263. (goto-char action)
  10264. (org-table-maybe-eval-formula)
  10265. (if arg
  10266. (call-interactively 'org-table-recalculate)
  10267. (org-table-maybe-recalculate-line))
  10268. (call-interactively 'org-table-align)
  10269. (orgtbl-send-table 'maybe))
  10270. ((eq action 'recalc)
  10271. (save-excursion
  10272. (beginning-of-line 1)
  10273. (skip-chars-backward " \r\n\t")
  10274. (if (org-at-table-p)
  10275. (org-call-with-arg 'org-table-recalculate t))))
  10276. (t (let (orgtbl-mode)
  10277. (call-interactively (key-binding "\C-c\C-c")))))))
  10278. (defun orgtbl-tab (arg)
  10279. "Justification and field motion for `orgtbl-mode'."
  10280. (interactive "P")
  10281. (if arg (org-table-edit-field t)
  10282. (org-table-justify-field-maybe)
  10283. (org-table-next-field)))
  10284. (defun orgtbl-ret ()
  10285. "Justification and field motion for `orgtbl-mode'."
  10286. (interactive)
  10287. (org-table-justify-field-maybe)
  10288. (org-table-next-row))
  10289. (defun orgtbl-self-insert-command (N)
  10290. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10291. If the cursor is in a table looking at whitespace, the whitespace is
  10292. overwritten, and the table is not marked as requiring realignment."
  10293. (interactive "p")
  10294. (if (and (org-at-table-p)
  10295. (or
  10296. (and org-table-auto-blank-field
  10297. (member last-command
  10298. '(orgtbl-hijacker-command-100
  10299. orgtbl-hijacker-command-101
  10300. orgtbl-hijacker-command-102
  10301. orgtbl-hijacker-command-103
  10302. orgtbl-hijacker-command-104
  10303. orgtbl-hijacker-command-105))
  10304. (org-table-blank-field))
  10305. t)
  10306. (eq N 1)
  10307. (looking-at "[^|\n]* +|"))
  10308. (let (org-table-may-need-update)
  10309. (goto-char (1- (match-end 0)))
  10310. (delete-backward-char 1)
  10311. (goto-char (match-beginning 0))
  10312. (self-insert-command N))
  10313. (setq org-table-may-need-update t)
  10314. (let (orgtbl-mode)
  10315. (call-interactively (key-binding (vector last-input-event))))))
  10316. (defun org-force-self-insert (N)
  10317. "Needed to enforce self-insert under remapping."
  10318. (interactive "p")
  10319. (self-insert-command N))
  10320. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10321. "Regula expression matching exponentials as produced by calc.")
  10322. (defvar org-table-clean-did-remove-column nil)
  10323. (defun orgtbl-export (table target)
  10324. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10325. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10326. org-table-last-alignment org-table-last-column-widths
  10327. maxcol column)
  10328. (if (not (fboundp func))
  10329. (error "Cannot export orgtbl table to %s" target))
  10330. (setq lines (org-table-clean-before-export lines))
  10331. (setq table
  10332. (mapcar
  10333. (lambda (x)
  10334. (if (string-match org-table-hline-regexp x)
  10335. 'hline
  10336. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10337. lines))
  10338. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10339. table)))
  10340. (loop for i from (1- maxcol) downto 0 do
  10341. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10342. (setq column (delq nil column))
  10343. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10344. (push (> (/ (apply '+ (mapcar (lambda (x) (if (string-match org-table-number-regexp x) 1 0)) column)) maxcol) org-table-number-fraction) org-table-last-alignment))
  10345. (funcall func table nil)))
  10346. (defun orgtbl-send-table (&optional maybe)
  10347. "Send a tranformed version of this table to the receiver position.
  10348. With argument MAYBE, fail quietly if no transformation is defined for
  10349. this table."
  10350. (interactive)
  10351. (catch 'exit
  10352. (unless (org-at-table-p) (error "Not at a table"))
  10353. ;; when non-interactive, we assume align has just happened.
  10354. (when (interactive-p) (org-table-align))
  10355. (save-excursion
  10356. (goto-char (org-table-begin))
  10357. (beginning-of-line 0)
  10358. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10359. (if maybe
  10360. (throw 'exit nil)
  10361. (error "Don't know how to transform this table."))))
  10362. (let* ((name (match-string 1))
  10363. beg
  10364. (transform (intern (match-string 2)))
  10365. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10366. (skip (plist-get params :skip))
  10367. (skipcols (plist-get params :skipcols))
  10368. (txt (buffer-substring-no-properties
  10369. (org-table-begin) (org-table-end)))
  10370. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10371. (lines (org-table-clean-before-export lines))
  10372. (i0 (if org-table-clean-did-remove-column 2 1))
  10373. (table (mapcar
  10374. (lambda (x)
  10375. (if (string-match org-table-hline-regexp x)
  10376. 'hline
  10377. (org-remove-by-index
  10378. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10379. skipcols i0)))
  10380. lines))
  10381. (fun (if (= i0 2) 'cdr 'identity))
  10382. (org-table-last-alignment
  10383. (org-remove-by-index (funcall fun org-table-last-alignment)
  10384. skipcols i0))
  10385. (org-table-last-column-widths
  10386. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10387. skipcols i0)))
  10388. (unless (fboundp transform)
  10389. (error "No such transformation function %s" transform))
  10390. (setq txt (funcall transform table params))
  10391. ;; Find the insertion place
  10392. (save-excursion
  10393. (goto-char (point-min))
  10394. (unless (re-search-forward
  10395. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10396. (error "Don't know where to insert translated table"))
  10397. (goto-char (match-beginning 0))
  10398. (beginning-of-line 2)
  10399. (setq beg (point))
  10400. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10401. (error "Cannot find end of insertion region"))
  10402. (beginning-of-line 1)
  10403. (delete-region beg (point))
  10404. (goto-char beg)
  10405. (insert txt "\n"))
  10406. (message "Table converted and installed at receiver location"))))
  10407. (defun org-remove-by-index (list indices &optional i0)
  10408. "Remove the elements in LIST with indices in INDICES.
  10409. First element has index 0, or I0 if given."
  10410. (if (not indices)
  10411. list
  10412. (if (integerp indices) (setq indices (list indices)))
  10413. (setq i0 (1- (or i0 0)))
  10414. (delq :rm (mapcar (lambda (x)
  10415. (setq i0 (1+ i0))
  10416. (if (memq i0 indices) :rm x))
  10417. list))))
  10418. (defun orgtbl-toggle-comment ()
  10419. "Comment or uncomment the orgtbl at point."
  10420. (interactive)
  10421. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10422. (re2 (concat "^" orgtbl-line-start-regexp))
  10423. (commented (save-excursion (beginning-of-line 1)
  10424. (cond ((looking-at re1) t)
  10425. ((looking-at re2) nil)
  10426. (t (error "Not at an org table")))))
  10427. (re (if commented re1 re2))
  10428. beg end)
  10429. (save-excursion
  10430. (beginning-of-line 1)
  10431. (while (looking-at re) (beginning-of-line 0))
  10432. (beginning-of-line 2)
  10433. (setq beg (point))
  10434. (while (looking-at re) (beginning-of-line 2))
  10435. (setq end (point)))
  10436. (comment-region beg end (if commented '(4) nil))))
  10437. (defun orgtbl-insert-radio-table ()
  10438. "Insert a radio table template appropriate for this major mode."
  10439. (interactive)
  10440. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10441. (txt (nth 1 e))
  10442. name pos)
  10443. (unless e (error "No radio table setup defined for %s" major-mode))
  10444. (setq name (read-string "Table name: "))
  10445. (while (string-match "%n" txt)
  10446. (setq txt (replace-match name t t txt)))
  10447. (or (bolp) (insert "\n"))
  10448. (setq pos (point))
  10449. (insert txt)
  10450. (goto-char pos)))
  10451. (defun org-get-param (params header i sym &optional hsym)
  10452. "Get parameter value for symbol SYM.
  10453. If this is a header line, actually get the value for the symbol with an
  10454. additional \"h\" inserted after the colon.
  10455. If the value is a protperty list, get the element for the current column.
  10456. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10457. (let ((val (plist-get params sym)))
  10458. (and hsym header (setq val (or (plist-get params hsym) val)))
  10459. (if (consp val) (plist-get val i) val)))
  10460. (defun orgtbl-to-generic (table params)
  10461. "Convert the orgtbl-mode TABLE to some other format.
  10462. This generic routine can be used for many standard cases.
  10463. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10464. separator line, or a list of fields for that line.
  10465. PARAMS is a property list of parameters that can influence the conversion.
  10466. For the generic converter, some parameters are obligatory: You need to
  10467. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10468. :splice, you must have :tstart and :tend.
  10469. Valid parameters are
  10470. :tstart String to start the table. Ignored when :splice is t.
  10471. :tend String to end the table. Ignored when :splice is t.
  10472. :splice When set to t, return only table body lines, don't wrap
  10473. them into :tstart and :tend. Default is nil.
  10474. :hline String to be inserted on horizontal separation lines.
  10475. May be nil to ignore hlines.
  10476. :lstart String to start a new table line.
  10477. :lend String to end a table line
  10478. :sep Separator between two fields
  10479. :lfmt Format for entire line, with enough %s to capture all fields.
  10480. If this is present, :lstart, :lend, and :sep are ignored.
  10481. :fmt A format to be used to wrap the field, should contain
  10482. %s for the original field value. For example, to wrap
  10483. everything in dollars, you could use :fmt \"$%s$\".
  10484. This may also be a property list with column numbers and
  10485. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10486. :hlstart :hlend :hlsep :hlfmt :hfmt
  10487. Same as above, specific for the header lines in the table.
  10488. All lines before the first hline are treated as header.
  10489. If any of these is not present, the data line value is used.
  10490. :efmt Use this format to print numbers with exponentials.
  10491. The format should have %s twice for inserting mantissa
  10492. and exponent, for example \"%s\\\\times10^{%s}\". This
  10493. may also be a property list with column numbers and
  10494. formats. :fmt will still be applied after :efmt.
  10495. In addition to this, the parameters :skip and :skipcols are always handled
  10496. directly by `orgtbl-send-table'. See manual."
  10497. (interactive)
  10498. (let* ((p params)
  10499. (splicep (plist-get p :splice))
  10500. (hline (plist-get p :hline))
  10501. rtn line i fm efm lfmt h)
  10502. ;; Do we have a header?
  10503. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10504. (setq h t))
  10505. ;; Put header
  10506. (unless splicep
  10507. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10508. ;; Now loop over all lines
  10509. (while (setq line (pop table))
  10510. (if (eq line 'hline)
  10511. ;; A horizontal separator line
  10512. (progn (if hline (push hline rtn))
  10513. (setq h nil)) ; no longer in header
  10514. ;; A normal line. Convert the fields, push line onto the result list
  10515. (setq i 0)
  10516. (setq line
  10517. (mapcar
  10518. (lambda (f)
  10519. (setq i (1+ i)
  10520. fm (org-get-param p h i :fmt :hfmt)
  10521. efm (org-get-param p h i :efmt))
  10522. (if (and efm (string-match orgtbl-exp-regexp f))
  10523. (setq f (format
  10524. efm (match-string 1 f) (match-string 2 f))))
  10525. (if fm (setq f (format fm f)))
  10526. f)
  10527. line))
  10528. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10529. (push (apply 'format lfmt line) rtn)
  10530. (push (concat
  10531. (org-get-param p h i :lstart :hlstart)
  10532. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10533. (org-get-param p h i :lend :hlend))
  10534. rtn))))
  10535. (unless splicep
  10536. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10537. (mapconcat 'identity (nreverse rtn) "\n")))
  10538. (defun orgtbl-to-latex (table params)
  10539. "Convert the orgtbl-mode TABLE to LaTeX.
  10540. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10541. separator line, or a list of fields for that line.
  10542. PARAMS is a property list of parameters that can influence the conversion.
  10543. Supports all parameters from `orgtbl-to-generic'. Most important for
  10544. LaTeX are:
  10545. :splice When set to t, return only table body lines, don't wrap
  10546. them into a tabular environment. Default is nil.
  10547. :fmt A format to be used to wrap the field, should contain %s for the
  10548. original field value. For example, to wrap everything in dollars,
  10549. use :fmt \"$%s$\". This may also be a property list with column
  10550. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10551. :efmt Format for transforming numbers with exponentials. The format
  10552. should have %s twice for inserting mantissa and exponent, for
  10553. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10554. This may also be a property list with column numbers and formats.
  10555. The general parameters :skip and :skipcols have already been applied when
  10556. this function is called."
  10557. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10558. org-table-last-alignment ""))
  10559. (params2
  10560. (list
  10561. :tstart (concat "\\begin{tabular}{" alignment "}")
  10562. :tend "\\end{tabular}"
  10563. :lstart "" :lend " \\\\" :sep " & "
  10564. :efmt "%s\\,(%s)" :hline "\\hline")))
  10565. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10566. (defun orgtbl-to-html (table params)
  10567. "Convert the orgtbl-mode TABLE to LaTeX.
  10568. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10569. separator line, or a list of fields for that line.
  10570. PARAMS is a property list of parameters that can influence the conversion.
  10571. Currently this function recognizes the following parameters:
  10572. :splice When set to t, return only table body lines, don't wrap
  10573. them into a <table> environment. Default is nil.
  10574. The general parameters :skip and :skipcols have already been applied when
  10575. this function is called. The function does *not* use `orgtbl-to-generic',
  10576. so you cannot specify parameters for it."
  10577. (let* ((splicep (plist-get params :splice))
  10578. html)
  10579. ;; Just call the formatter we already have
  10580. ;; We need to make text lines for it, so put the fields back together.
  10581. (setq html (org-format-org-table-html
  10582. (mapcar
  10583. (lambda (x)
  10584. (if (eq x 'hline)
  10585. "|----+----|"
  10586. (concat "| " (mapconcat 'identity x " | ") " |")))
  10587. table)
  10588. splicep))
  10589. (if (string-match "\n+\\'" html)
  10590. (setq html (replace-match "" t t html)))
  10591. html))
  10592. (defun orgtbl-to-texinfo (table params)
  10593. "Convert the orgtbl-mode TABLE to TeXInfo.
  10594. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10595. separator line, or a list of fields for that line.
  10596. PARAMS is a property list of parameters that can influence the conversion.
  10597. Supports all parameters from `orgtbl-to-generic'. Most important for
  10598. TeXInfo are:
  10599. :splice nil/t When set to t, return only table body lines, don't wrap
  10600. them into a multitable environment. Default is nil.
  10601. :fmt fmt A format to be used to wrap the field, should contain
  10602. %s for the original field value. For example, to wrap
  10603. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10604. This may also be a property list with column numbers and
  10605. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10606. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  10607. are computed automatically from the width of the columns
  10608. under org-mode.
  10609. The general parameters :skip and :skipcols have already been applied when
  10610. this function is called."
  10611. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10612. (colfrac (or (plist-get params :cf)
  10613. (mapconcat
  10614. (lambda (x) (format "%.3f" (/ (float x) total)))
  10615. org-table-last-column-widths " ")))
  10616. (params2
  10617. (list
  10618. :tstart (concat "@multitable @columnfractions " colfrac)
  10619. :tend "@end multitable"
  10620. :lstart "@item " :lend "" :sep " @tab "
  10621. :hlstart "@headitem ")))
  10622. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10623. ;;;; Link Stuff
  10624. ;;; Link abbreviations
  10625. (defun org-link-expand-abbrev (link)
  10626. "Apply replacements as defined in `org-link-abbrev-alist."
  10627. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10628. (let* ((key (match-string 1 link))
  10629. (as (or (assoc key org-link-abbrev-alist-local)
  10630. (assoc key org-link-abbrev-alist)))
  10631. (tag (and (match-end 2) (match-string 3 link)))
  10632. rpl)
  10633. (if (not as)
  10634. link
  10635. (setq rpl (cdr as))
  10636. (cond
  10637. ((symbolp rpl) (funcall rpl tag))
  10638. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10639. (t (concat rpl tag)))))
  10640. link))
  10641. ;;; Storing and inserting links
  10642. (defvar org-insert-link-history nil
  10643. "Minibuffer history for links inserted with `org-insert-link'.")
  10644. (defvar org-stored-links nil
  10645. "Contains the links stored with `org-store-link'.")
  10646. (defvar org-store-link-plist nil
  10647. "Plist with info about the most recently link created with `org-store-link'.")
  10648. (defvar org-link-protocols nil
  10649. "Link protocols added to Org-mode using `org-add-link-type'.")
  10650. (defvar org-store-link-functions nil
  10651. "List of functions that are called to create and store a link.
  10652. Each function will be called in turn until one returns a non-nil
  10653. value. Each function should check if it is responsible for creating
  10654. this link (for example by looking at the major mode).
  10655. If not, it must exit and return nil.
  10656. If yes, it should return a non-nil value after a calling
  10657. `org-store-link-props' with a list of properties and values.
  10658. Special properties are:
  10659. :type The link prefix. like \"http\". This must be given.
  10660. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10661. This is obligatory as well.
  10662. :description Optional default description for the second pair
  10663. of brackets in an Org-mode link. The user can still change
  10664. this when inserting this link into an Org-mode buffer.
  10665. In addition to these, any additional properties can be specified
  10666. and then used in remember templates.")
  10667. (defun org-add-link-type (type &optional follow publish)
  10668. "Add TYPE to the list of `org-link-types'.
  10669. Re-compute all regular expressions depending on `org-link-types'
  10670. FOLLOW and PUBLISH are two functions. Both take the link path as
  10671. an argument.
  10672. FOLLOW should do whatever is necessary to follow the link, for example
  10673. to find a file or display a mail message.
  10674. PUBLISH takes the path and retuns the string that should be used when
  10675. this document is published. FIMXE: This is actually not yet implemented."
  10676. (add-to-list 'org-link-types type t)
  10677. (org-make-link-regexps)
  10678. (add-to-list 'org-link-protocols
  10679. (list type follow publish)))
  10680. (defun org-add-agenda-custom-command (entry)
  10681. "Replace or add a command in `org-agenda-custom-commands'.
  10682. This is mostly for hacking and trying a new command - once the command
  10683. works you probably want to add it to `org-agenda-custom-commands' for good."
  10684. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10685. (if ass
  10686. (setcdr ass (cdr entry))
  10687. (push entry org-agenda-custom-commands))))
  10688. ;;;###autoload
  10689. (defun org-store-link (arg)
  10690. "\\<org-mode-map>Store an org-link to the current location.
  10691. This link can later be inserted into an org-buffer with
  10692. \\[org-insert-link].
  10693. For some link types, a prefix arg is interpreted:
  10694. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10695. For file links, arg negates `org-context-in-file-links'."
  10696. (interactive "P")
  10697. (setq org-store-link-plist nil) ; reset
  10698. (let (link cpltxt desc description search txt)
  10699. (cond
  10700. ((run-hook-with-args-until-success 'org-store-link-functions)
  10701. (setq link (plist-get org-store-link-plist :link)
  10702. desc (or (plist-get org-store-link-plist :description) link)))
  10703. ((eq major-mode 'bbdb-mode)
  10704. (let ((name (bbdb-record-name (bbdb-current-record)))
  10705. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10706. (setq cpltxt (concat "bbdb:" (or name company))
  10707. link (org-make-link cpltxt))
  10708. (org-store-link-props :type "bbdb" :name name :company company)))
  10709. ((eq major-mode 'Info-mode)
  10710. (setq link (org-make-link "info:"
  10711. (file-name-nondirectory Info-current-file)
  10712. ":" Info-current-node))
  10713. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10714. ":" Info-current-node))
  10715. (org-store-link-props :type "info" :file Info-current-file
  10716. :node Info-current-node))
  10717. ((eq major-mode 'calendar-mode)
  10718. (let ((cd (calendar-cursor-to-date)))
  10719. (setq link
  10720. (format-time-string
  10721. (car org-time-stamp-formats)
  10722. (apply 'encode-time
  10723. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10724. nil nil nil))))
  10725. (org-store-link-props :type "calendar" :date cd)))
  10726. ((or (eq major-mode 'vm-summary-mode)
  10727. (eq major-mode 'vm-presentation-mode))
  10728. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10729. (vm-follow-summary-cursor)
  10730. (save-excursion
  10731. (vm-select-folder-buffer)
  10732. (let* ((message (car vm-message-pointer))
  10733. (folder buffer-file-name)
  10734. (subject (vm-su-subject message))
  10735. (to (vm-get-header-contents message "To"))
  10736. (from (vm-get-header-contents message "From"))
  10737. (message-id (vm-su-message-id message)))
  10738. (org-store-link-props :type "vm" :from from :to to :subject subject
  10739. :message-id message-id)
  10740. (setq message-id (org-remove-angle-brackets message-id))
  10741. (setq folder (abbreviate-file-name folder))
  10742. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10743. folder)
  10744. (setq folder (replace-match "" t t folder)))
  10745. (setq cpltxt (org-email-link-description))
  10746. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10747. ((eq major-mode 'wl-summary-mode)
  10748. (let* ((msgnum (wl-summary-message-number))
  10749. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10750. msgnum 'message-id))
  10751. (wl-message-entity
  10752. (if (fboundp 'elmo-message-entity)
  10753. (elmo-message-entity
  10754. wl-summary-buffer-elmo-folder msgnum)
  10755. (elmo-msgdb-overview-get-entity
  10756. msgnum (wl-summary-buffer-msgdb))))
  10757. (from (wl-summary-line-from))
  10758. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10759. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10760. (wl-summary-line-subject))))
  10761. (org-store-link-props :type "wl" :from from :to to
  10762. :subject subject :message-id message-id)
  10763. (setq message-id (org-remove-angle-brackets message-id))
  10764. (setq cpltxt (org-email-link-description))
  10765. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10766. "#" message-id))))
  10767. ((or (equal major-mode 'mh-folder-mode)
  10768. (equal major-mode 'mh-show-mode))
  10769. (let ((from (org-mhe-get-header "From:"))
  10770. (to (org-mhe-get-header "To:"))
  10771. (message-id (org-mhe-get-header "Message-Id:"))
  10772. (subject (org-mhe-get-header "Subject:")))
  10773. (org-store-link-props :type "mh" :from from :to to
  10774. :subject subject :message-id message-id)
  10775. (setq cpltxt (org-email-link-description))
  10776. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10777. (org-remove-angle-brackets message-id)))))
  10778. ((eq major-mode 'rmail-mode)
  10779. (save-excursion
  10780. (save-restriction
  10781. (rmail-narrow-to-non-pruned-header)
  10782. (let ((folder buffer-file-name)
  10783. (message-id (mail-fetch-field "message-id"))
  10784. (from (mail-fetch-field "from"))
  10785. (to (mail-fetch-field "to"))
  10786. (subject (mail-fetch-field "subject")))
  10787. (org-store-link-props
  10788. :type "rmail" :from from :to to
  10789. :subject subject :message-id message-id)
  10790. (setq message-id (org-remove-angle-brackets message-id))
  10791. (setq cpltxt (org-email-link-description))
  10792. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10793. ((eq major-mode 'gnus-group-mode)
  10794. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10795. (gnus-group-group-name)) ; version
  10796. ((fboundp 'gnus-group-name)
  10797. (gnus-group-name))
  10798. (t "???"))))
  10799. (unless group (error "Not on a group"))
  10800. (org-store-link-props :type "gnus" :group group)
  10801. (setq cpltxt (concat
  10802. (if (org-xor arg org-usenet-links-prefer-google)
  10803. "http://groups.google.com/groups?group="
  10804. "gnus:")
  10805. group)
  10806. link (org-make-link cpltxt))))
  10807. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10808. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10809. (let* ((group gnus-newsgroup-name)
  10810. (article (gnus-summary-article-number))
  10811. (header (gnus-summary-article-header article))
  10812. (from (mail-header-from header))
  10813. (message-id (mail-header-id header))
  10814. (date (mail-header-date header))
  10815. (subject (gnus-summary-subject-string)))
  10816. (org-store-link-props :type "gnus" :from from :subject subject
  10817. :message-id message-id :group group)
  10818. (setq cpltxt (org-email-link-description))
  10819. (if (org-xor arg org-usenet-links-prefer-google)
  10820. (setq link
  10821. (concat
  10822. cpltxt "\n "
  10823. (format "http://groups.google.com/groups?as_umsgid=%s"
  10824. (org-fixup-message-id-for-http message-id))))
  10825. (setq link (org-make-link "gnus:" group
  10826. "#" (number-to-string article))))))
  10827. ((eq major-mode 'w3-mode)
  10828. (setq cpltxt (url-view-url t)
  10829. link (org-make-link cpltxt))
  10830. (org-store-link-props :type "w3" :url (url-view-url t)))
  10831. ((eq major-mode 'w3m-mode)
  10832. (setq cpltxt (or w3m-current-title w3m-current-url)
  10833. link (org-make-link w3m-current-url))
  10834. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10835. ((setq search (run-hook-with-args-until-success
  10836. 'org-create-file-search-functions))
  10837. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10838. "::" search))
  10839. (setq cpltxt (or description link)))
  10840. ((eq major-mode 'image-mode)
  10841. (setq cpltxt (concat "file:"
  10842. (abbreviate-file-name buffer-file-name))
  10843. link (org-make-link cpltxt))
  10844. (org-store-link-props :type "image" :file buffer-file-name))
  10845. ((eq major-mode 'dired-mode)
  10846. ;; link to the file in the current line
  10847. (setq cpltxt (concat "file:"
  10848. (abbreviate-file-name
  10849. (expand-file-name
  10850. (dired-get-filename nil t))))
  10851. link (org-make-link cpltxt)))
  10852. ((and buffer-file-name (org-mode-p))
  10853. ;; Just link to current headline
  10854. (setq cpltxt (concat "file:"
  10855. (abbreviate-file-name buffer-file-name)))
  10856. ;; Add a context search string
  10857. (when (org-xor org-context-in-file-links arg)
  10858. ;; Check if we are on a target
  10859. (if (org-in-regexp "<<\\(.*?\\)>>")
  10860. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10861. (setq txt (cond
  10862. ((org-on-heading-p) nil)
  10863. ((org-region-active-p)
  10864. (buffer-substring (region-beginning) (region-end)))
  10865. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10866. (when (or (null txt) (string-match "\\S-" txt))
  10867. (setq cpltxt
  10868. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10869. desc "NONE"))))
  10870. (if (string-match "::\\'" cpltxt)
  10871. (setq cpltxt (substring cpltxt 0 -2)))
  10872. (setq link (org-make-link cpltxt)))
  10873. ((buffer-file-name (buffer-base-buffer))
  10874. ;; Just link to this file here.
  10875. (setq cpltxt (concat "file:"
  10876. (abbreviate-file-name
  10877. (buffer-file-name (buffer-base-buffer)))))
  10878. ;; Add a context string
  10879. (when (org-xor org-context-in-file-links arg)
  10880. (setq txt (if (org-region-active-p)
  10881. (buffer-substring (region-beginning) (region-end))
  10882. (buffer-substring (point-at-bol) (point-at-eol))))
  10883. ;; Only use search option if there is some text.
  10884. (when (string-match "\\S-" txt)
  10885. (setq cpltxt
  10886. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10887. desc "NONE")))
  10888. (setq link (org-make-link cpltxt)))
  10889. ((interactive-p)
  10890. (error "Cannot link to a buffer which is not visiting a file"))
  10891. (t (setq link nil)))
  10892. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10893. (setq link (or link cpltxt)
  10894. desc (or desc cpltxt))
  10895. (if (equal desc "NONE") (setq desc nil))
  10896. (if (and (interactive-p) link)
  10897. (progn
  10898. (setq org-stored-links
  10899. (cons (list link desc) org-stored-links))
  10900. (message "Stored: %s" (or desc link)))
  10901. (and link (org-make-link-string link desc)))))
  10902. (defun org-store-link-props (&rest plist)
  10903. "Store link properties, extract names and addresses."
  10904. (let (x adr)
  10905. (when (setq x (plist-get plist :from))
  10906. (setq adr (mail-extract-address-components x))
  10907. (plist-put plist :fromname (car adr))
  10908. (plist-put plist :fromaddress (nth 1 adr)))
  10909. (when (setq x (plist-get plist :to))
  10910. (setq adr (mail-extract-address-components x))
  10911. (plist-put plist :toname (car adr))
  10912. (plist-put plist :toaddress (nth 1 adr))))
  10913. (let ((from (plist-get plist :from))
  10914. (to (plist-get plist :to)))
  10915. (when (and from to org-from-is-user-regexp)
  10916. (plist-put plist :fromto
  10917. (if (string-match org-from-is-user-regexp from)
  10918. (concat "to %t")
  10919. (concat "from %f")))))
  10920. (setq org-store-link-plist plist))
  10921. (defun org-email-link-description (&optional fmt)
  10922. "Return the description part of an email link.
  10923. This takes information from `org-store-link-plist' and formats it
  10924. according to FMT (default from `org-email-link-description-format')."
  10925. (setq fmt (or fmt org-email-link-description-format))
  10926. (let* ((p org-store-link-plist)
  10927. (to (plist-get p :toaddress))
  10928. (from (plist-get p :fromaddress))
  10929. (table
  10930. (list
  10931. (cons "%c" (plist-get p :fromto))
  10932. (cons "%F" (plist-get p :from))
  10933. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  10934. (cons "%T" (plist-get p :to))
  10935. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  10936. (cons "%s" (plist-get p :subject))
  10937. (cons "%m" (plist-get p :message-id)))))
  10938. (when (string-match "%c" fmt)
  10939. ;; Check if the user wrote this message
  10940. (if (and org-from-is-user-regexp from to
  10941. (save-match-data (string-match org-from-is-user-regexp from)))
  10942. (setq fmt (replace-match "to %t" t t fmt))
  10943. (setq fmt (replace-match "from %f" t t fmt))))
  10944. (org-replace-escapes fmt table)))
  10945. (defun org-make-org-heading-search-string (&optional string heading)
  10946. "Make search string for STRING or current headline."
  10947. (interactive)
  10948. (let ((s (or string (org-get-heading))))
  10949. (unless (and string (not heading))
  10950. ;; We are using a headline, clean up garbage in there.
  10951. (if (string-match org-todo-regexp s)
  10952. (setq s (replace-match "" t t s)))
  10953. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  10954. (setq s (replace-match "" t t s)))
  10955. (setq s (org-trim s))
  10956. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10957. org-comment-string "\\)") s)
  10958. (setq s (replace-match "" t t s)))
  10959. (while (string-match org-ts-regexp s)
  10960. (setq s (replace-match "" t t s))))
  10961. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10962. (setq s (replace-match " " t t s)))
  10963. (or string (setq s (concat "*" s))) ; Add * for headlines
  10964. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10965. (defun org-make-link (&rest strings)
  10966. "Concatenate STRINGS."
  10967. (apply 'concat strings))
  10968. (defun org-make-link-string (link &optional description)
  10969. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10970. (unless (string-match "\\S-" link)
  10971. (error "Empty link"))
  10972. (when (stringp description)
  10973. ;; Remove brackets from the description, they are fatal.
  10974. (while (string-match "\\[" description)
  10975. (setq description (replace-match "{" t t description)))
  10976. (while (string-match "\\]" description)
  10977. (setq description (replace-match "}" t t description))))
  10978. (when (equal (org-link-escape link) description)
  10979. ;; No description needed, it is identical
  10980. (setq description nil))
  10981. (when (and (not description)
  10982. (not (equal link (org-link-escape link))))
  10983. (setq description link))
  10984. (concat "[[" (org-link-escape link) "]"
  10985. (if description (concat "[" description "]") "")
  10986. "]"))
  10987. (defconst org-link-escape-chars
  10988. '((?\ . "%20")
  10989. (?\[ . "%5B")
  10990. (?\] . "%5D")
  10991. (?\340 . "%E0") ; `a
  10992. (?\342 . "%E2") ; ^a
  10993. (?\347 . "%E7") ; ,c
  10994. (?\350 . "%E8") ; `e
  10995. (?\351 . "%E9") ; 'e
  10996. (?\352 . "%EA") ; ^e
  10997. (?\356 . "%EE") ; ^i
  10998. (?\364 . "%F4") ; ^o
  10999. (?\371 . "%F9") ; `u
  11000. (?\373 . "%FB") ; ^u
  11001. (?\; . "%3B")
  11002. (?? . "%3F")
  11003. (?= . "%3D")
  11004. (?+ . "%2B")
  11005. )
  11006. "Association list of escapes for some characters problematic in links.
  11007. This is the list that is used for internal purposes.")
  11008. (defconst org-link-escape-chars-browser
  11009. '((?\ . "%20")) ; 32 for the SPC char
  11010. "Association list of escapes for some characters problematic in links.
  11011. This is the list that is used before handing over to the browser.")
  11012. (defun org-link-escape (text &optional table)
  11013. "Escape charaters in TEXT that are problematic for links."
  11014. (setq table (or table org-link-escape-chars))
  11015. (when text
  11016. (let ((re (mapconcat (lambda (x) (regexp-quote
  11017. (char-to-string (car x))))
  11018. table "\\|")))
  11019. (while (string-match re text)
  11020. (setq text
  11021. (replace-match
  11022. (cdr (assoc (string-to-char (match-string 0 text))
  11023. table))
  11024. t t text)))
  11025. text)))
  11026. (defun org-link-unescape (text &optional table)
  11027. "Reverse the action of `org-link-escape'."
  11028. (setq table (or table org-link-escape-chars))
  11029. (when text
  11030. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11031. table "\\|")))
  11032. (while (string-match re text)
  11033. (setq text
  11034. (replace-match
  11035. (char-to-string (car (rassoc (match-string 0 text) table)))
  11036. t t text)))
  11037. text)))
  11038. (defun org-xor (a b)
  11039. "Exclusive or."
  11040. (if a (not b) b))
  11041. (defun org-get-header (header)
  11042. "Find a header field in the current buffer."
  11043. (save-excursion
  11044. (goto-char (point-min))
  11045. (let ((case-fold-search t) s)
  11046. (cond
  11047. ((eq header 'from)
  11048. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11049. (setq s (match-string 1)))
  11050. (while (string-match "\"" s)
  11051. (setq s (replace-match "" t t s)))
  11052. (if (string-match "[<(].*" s)
  11053. (setq s (replace-match "" t t s))))
  11054. ((eq header 'message-id)
  11055. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11056. (setq s (match-string 1))))
  11057. ((eq header 'subject)
  11058. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11059. (setq s (match-string 1)))))
  11060. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11061. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11062. s)))
  11063. (defun org-fixup-message-id-for-http (s)
  11064. "Replace special characters in a message id, so it can be used in an http query."
  11065. (while (string-match "<" s)
  11066. (setq s (replace-match "%3C" t t s)))
  11067. (while (string-match ">" s)
  11068. (setq s (replace-match "%3E" t t s)))
  11069. (while (string-match "@" s)
  11070. (setq s (replace-match "%40" t t s)))
  11071. s)
  11072. ;;;###autoload
  11073. (defun org-insert-link-global ()
  11074. "Insert a link like Org-mode does.
  11075. This command can be called in any mode to insert a link in Org-mode syntax."
  11076. (interactive)
  11077. (org-run-like-in-org-mode 'org-insert-link))
  11078. (defun org-insert-link (&optional complete-file)
  11079. "Insert a link. At the prompt, enter the link.
  11080. Completion can be used to select a link previously stored with
  11081. `org-store-link'. When the empty string is entered (i.e. if you just
  11082. press RET at the prompt), the link defaults to the most recently
  11083. stored link. As SPC triggers completion in the minibuffer, you need to
  11084. use M-SPC or C-q SPC to force the insertion of a space character.
  11085. You will also be prompted for a description, and if one is given, it will
  11086. be displayed in the buffer instead of the link.
  11087. If there is already a link at point, this command will allow you to edit link
  11088. and description parts.
  11089. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11090. selected using completion. The path to the file will be relative to
  11091. the current directory if the file is in the current directory or a
  11092. subdirectory. Otherwise, the link will be the absolute path as
  11093. completed in the minibuffer (i.e. normally ~/path/to/file).
  11094. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11095. is in the current directory or below.
  11096. With three \\[universal-argument] prefixes, negate the meaning of
  11097. `org-keep-stored-link-after-insertion'."
  11098. (interactive "P")
  11099. (let* ((wcf (current-window-configuration))
  11100. (region (if (org-region-active-p)
  11101. (buffer-substring (region-beginning) (region-end))))
  11102. (remove (and region (list (region-beginning) (region-end))))
  11103. (desc region)
  11104. tmphist ; byte-compile incorrectly complains about this
  11105. link entry file)
  11106. (cond
  11107. ((org-in-regexp org-bracket-link-regexp 1)
  11108. ;; We do have a link at point, and we are going to edit it.
  11109. (setq remove (list (match-beginning 0) (match-end 0)))
  11110. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11111. (setq link (read-string "Link: "
  11112. (org-link-unescape
  11113. (org-match-string-no-properties 1)))))
  11114. ((or (org-in-regexp org-angle-link-re)
  11115. (org-in-regexp org-plain-link-re))
  11116. ;; Convert to bracket link
  11117. (setq remove (list (match-beginning 0) (match-end 0))
  11118. link (read-string "Link: "
  11119. (org-remove-angle-brackets (match-string 0)))))
  11120. ((equal complete-file '(4))
  11121. ;; Completing read for file names.
  11122. (setq file (read-file-name "File: "))
  11123. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11124. (pwd1 (file-name-as-directory (abbreviate-file-name
  11125. (expand-file-name ".")))))
  11126. (cond
  11127. ((equal complete-file '(16))
  11128. (setq link (org-make-link
  11129. "file:"
  11130. (abbreviate-file-name (expand-file-name file)))))
  11131. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11132. (setq link (org-make-link "file:" (match-string 1 file))))
  11133. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11134. (expand-file-name file))
  11135. (setq link (org-make-link
  11136. "file:" (match-string 1 (expand-file-name file)))))
  11137. (t (setq link (org-make-link "file:" file))))))
  11138. (t
  11139. ;; Read link, with completion for stored links.
  11140. (with-output-to-temp-buffer "*Org Links*"
  11141. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11142. (when org-stored-links
  11143. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11144. (princ (mapconcat
  11145. (lambda (x)
  11146. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11147. (reverse org-stored-links) "\n"))))
  11148. (let ((cw (selected-window)))
  11149. (select-window (get-buffer-window "*Org Links*"))
  11150. (shrink-window-if-larger-than-buffer)
  11151. (setq truncate-lines t)
  11152. (select-window cw))
  11153. ;; Fake a link history, containing the stored links.
  11154. (setq tmphist (append (mapcar 'car org-stored-links)
  11155. org-insert-link-history))
  11156. (unwind-protect
  11157. (setq link (org-completing-read
  11158. "Link: "
  11159. (append
  11160. (mapcar (lambda (x) (list (concat (car x) ":")))
  11161. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11162. (mapcar (lambda (x) (list (concat x ":")))
  11163. org-link-types))
  11164. nil nil nil
  11165. 'tmphist
  11166. (or (car (car org-stored-links)))))
  11167. (set-window-configuration wcf)
  11168. (kill-buffer "*Org Links*"))
  11169. (setq entry (assoc link org-stored-links))
  11170. (or entry (push link org-insert-link-history))
  11171. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11172. (not org-keep-stored-link-after-insertion))
  11173. (setq org-stored-links (delq (assoc link org-stored-links)
  11174. org-stored-links)))
  11175. (setq desc (or desc (nth 1 entry)))))
  11176. (if (string-match org-plain-link-re link)
  11177. ;; URL-like link, normalize the use of angular brackets.
  11178. (setq link (org-make-link (org-remove-angle-brackets link))))
  11179. ;; Check if we are linking to the current file with a search option
  11180. ;; If yes, simplify the link by using only the search option.
  11181. (when (and buffer-file-name
  11182. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11183. (let* ((path (match-string 1 link))
  11184. (case-fold-search nil)
  11185. (search (match-string 2 link)))
  11186. (save-match-data
  11187. (if (equal (file-truename buffer-file-name) (file-truename path))
  11188. ;; We are linking to this same file, with a search option
  11189. (setq link search)))))
  11190. ;; Check if we can/should use a relative path. If yes, simplify the link
  11191. (when (string-match "\\<file:\\(.*\\)" link)
  11192. (let* ((path (match-string 1 link))
  11193. (origpath path)
  11194. (desc-is-link (equal link desc))
  11195. (case-fold-search nil))
  11196. (cond
  11197. ((eq org-link-file-path-type 'absolute)
  11198. (setq path (abbreviate-file-name (expand-file-name path))))
  11199. ((eq org-link-file-path-type 'noabbrev)
  11200. (setq path (expand-file-name path)))
  11201. ((eq org-link-file-path-type 'relative)
  11202. (setq path (file-relative-name path)))
  11203. (t
  11204. (save-match-data
  11205. (if (string-match (concat "^" (regexp-quote
  11206. (file-name-as-directory
  11207. (expand-file-name "."))))
  11208. (expand-file-name path))
  11209. ;; We are linking a file with relative path name.
  11210. (setq path (substring (expand-file-name path)
  11211. (match-end 0)))))))
  11212. (setq link (concat "file:" path))
  11213. (if (equal desc origpath)
  11214. (setq desc path))))
  11215. (setq desc (read-string "Description: " desc))
  11216. (unless (string-match "\\S-" desc) (setq desc nil))
  11217. (if remove (apply 'delete-region remove))
  11218. (insert (org-make-link-string link desc))))
  11219. (defun org-completing-read (&rest args)
  11220. (let ((minibuffer-local-completion-map
  11221. (copy-keymap minibuffer-local-completion-map)))
  11222. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11223. (apply 'completing-read args)))
  11224. ;;; Opening/following a link
  11225. (defvar org-link-search-failed nil)
  11226. (defun org-next-link ()
  11227. "Move forward to the next link.
  11228. If the link is in hidden text, expose it."
  11229. (interactive)
  11230. (when (and org-link-search-failed (eq this-command last-command))
  11231. (goto-char (point-min))
  11232. (message "Link search wrapped back to beginning of buffer"))
  11233. (setq org-link-search-failed nil)
  11234. (let* ((pos (point))
  11235. (ct (org-context))
  11236. (a (assoc :link ct)))
  11237. (if a (goto-char (nth 2 a)))
  11238. (if (re-search-forward org-any-link-re nil t)
  11239. (progn
  11240. (goto-char (match-beginning 0))
  11241. (if (org-invisible-p) (org-show-context)))
  11242. (goto-char pos)
  11243. (setq org-link-search-failed t)
  11244. (error "No further link found"))))
  11245. (defun org-previous-link ()
  11246. "Move backward to the previous link.
  11247. If the link is in hidden text, expose it."
  11248. (interactive)
  11249. (when (and org-link-search-failed (eq this-command last-command))
  11250. (goto-char (point-max))
  11251. (message "Link search wrapped back to end of buffer"))
  11252. (setq org-link-search-failed nil)
  11253. (let* ((pos (point))
  11254. (ct (org-context))
  11255. (a (assoc :link ct)))
  11256. (if a (goto-char (nth 1 a)))
  11257. (if (re-search-backward org-any-link-re nil t)
  11258. (progn
  11259. (goto-char (match-beginning 0))
  11260. (if (org-invisible-p) (org-show-context)))
  11261. (goto-char pos)
  11262. (setq org-link-search-failed t)
  11263. (error "No further link found"))))
  11264. (defun org-find-file-at-mouse (ev)
  11265. "Open file link or URL at mouse."
  11266. (interactive "e")
  11267. (mouse-set-point ev)
  11268. (org-open-at-point 'in-emacs))
  11269. (defun org-open-at-mouse (ev)
  11270. "Open file link or URL at mouse."
  11271. (interactive "e")
  11272. (mouse-set-point ev)
  11273. (org-open-at-point))
  11274. (defvar org-window-config-before-follow-link nil
  11275. "The window configuration before following a link.
  11276. This is saved in case the need arises to restore it.")
  11277. (defvar org-open-link-marker (make-marker)
  11278. "Marker pointing to the location where `org-open-at-point; was called.")
  11279. ;;;###autoload
  11280. (defun org-open-at-point-global ()
  11281. "Follow a link like Org-mode does.
  11282. This command can be called in any mode to follow a link that has
  11283. Org-mode syntax."
  11284. (interactive)
  11285. (org-run-like-in-org-mode 'org-open-at-point))
  11286. (defun org-open-at-point (&optional in-emacs)
  11287. "Open link at or after point.
  11288. If there is no link at point, this function will search forward up to
  11289. the end of the current subtree.
  11290. Normally, files will be opened by an appropriate application. If the
  11291. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11292. (interactive "P")
  11293. (catch 'abort
  11294. (move-marker org-open-link-marker (point))
  11295. (setq org-window-config-before-follow-link (current-window-configuration))
  11296. (org-remove-occur-highlights nil nil t)
  11297. (if (org-at-timestamp-p t)
  11298. (org-follow-timestamp-link)
  11299. (let (type path link line search (pos (point)))
  11300. (catch 'match
  11301. (save-excursion
  11302. (skip-chars-forward "^]\n\r")
  11303. (when (org-in-regexp org-bracket-link-regexp)
  11304. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11305. (while (string-match " *\n *" link)
  11306. (setq link (replace-match " " t t link)))
  11307. (setq link (org-link-expand-abbrev link))
  11308. (if (string-match org-link-re-with-space2 link)
  11309. (setq type (match-string 1 link) path (match-string 2 link))
  11310. (setq type "thisfile" path link))
  11311. (throw 'match t)))
  11312. (when (get-text-property (point) 'org-linked-text)
  11313. (setq type "thisfile"
  11314. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11315. (1+ (point)) (point))
  11316. path (buffer-substring
  11317. (previous-single-property-change pos 'org-linked-text)
  11318. (next-single-property-change pos 'org-linked-text)))
  11319. (throw 'match t))
  11320. (save-excursion
  11321. (when (or (org-in-regexp org-angle-link-re)
  11322. (org-in-regexp org-plain-link-re))
  11323. (setq type (match-string 1) path (match-string 2))
  11324. (throw 'match t)))
  11325. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11326. (setq type "tree-match"
  11327. path (match-string 1))
  11328. (throw 'match t))
  11329. (save-excursion
  11330. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11331. (setq type "tags"
  11332. path (match-string 1))
  11333. (while (string-match ":" path)
  11334. (setq path (replace-match "+" t t path)))
  11335. (throw 'match t))))
  11336. (unless path
  11337. (error "No link found"))
  11338. ;; Remove any trailing spaces in path
  11339. (if (string-match " +\\'" path)
  11340. (setq path (replace-match "" t t path)))
  11341. (cond
  11342. ((assoc type org-link-protocols)
  11343. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11344. ((equal type "mailto")
  11345. (let ((cmd (car org-link-mailto-program))
  11346. (args (cdr org-link-mailto-program)) args1
  11347. (address path) (subject "") a)
  11348. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11349. (setq address (match-string 1 path)
  11350. subject (org-link-escape (match-string 2 path))))
  11351. (while args
  11352. (cond
  11353. ((not (stringp (car args))) (push (pop args) args1))
  11354. (t (setq a (pop args))
  11355. (if (string-match "%a" a)
  11356. (setq a (replace-match address t t a)))
  11357. (if (string-match "%s" a)
  11358. (setq a (replace-match subject t t a)))
  11359. (push a args1))))
  11360. (apply cmd (nreverse args1))))
  11361. ((member type '("http" "https" "ftp" "news"))
  11362. (browse-url (concat type ":" (org-link-escape
  11363. path org-link-escape-chars-browser))))
  11364. ((string= type "tags")
  11365. (org-tags-view in-emacs path))
  11366. ((string= type "thisfile")
  11367. (if in-emacs
  11368. (switch-to-buffer-other-window
  11369. (org-get-buffer-for-internal-link (current-buffer)))
  11370. (org-mark-ring-push))
  11371. (let ((cmd `(org-link-search
  11372. ,path
  11373. ,(cond ((equal in-emacs '(4)) 'occur)
  11374. ((equal in-emacs '(16)) 'org-occur)
  11375. (t nil))
  11376. ,pos)))
  11377. (condition-case nil (eval cmd)
  11378. (error (progn (widen) (eval cmd))))))
  11379. ((string= type "tree-match")
  11380. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11381. ((string= type "file")
  11382. (if (string-match "::\\([0-9]+\\)\\'" path)
  11383. (setq line (string-to-number (match-string 1 path))
  11384. path (substring path 0 (match-beginning 0)))
  11385. (if (string-match "::\\(.+\\)\\'" path)
  11386. (setq search (match-string 1 path)
  11387. path (substring path 0 (match-beginning 0)))))
  11388. (if (string-match "[*?{]" (file-name-nondirectory path))
  11389. (dired path)
  11390. (org-open-file path in-emacs line search)))
  11391. ((string= type "news")
  11392. (org-follow-gnus-link path))
  11393. ((string= type "bbdb")
  11394. (org-follow-bbdb-link path))
  11395. ((string= type "info")
  11396. (org-follow-info-link path))
  11397. ((string= type "gnus")
  11398. (let (group article)
  11399. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11400. (error "Error in Gnus link"))
  11401. (setq group (match-string 1 path)
  11402. article (match-string 3 path))
  11403. (org-follow-gnus-link group article)))
  11404. ((string= type "vm")
  11405. (let (folder article)
  11406. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11407. (error "Error in VM link"))
  11408. (setq folder (match-string 1 path)
  11409. article (match-string 3 path))
  11410. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11411. (org-follow-vm-link folder article in-emacs)))
  11412. ((string= type "wl")
  11413. (let (folder article)
  11414. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11415. (error "Error in Wanderlust link"))
  11416. (setq folder (match-string 1 path)
  11417. article (match-string 3 path))
  11418. (org-follow-wl-link folder article)))
  11419. ((string= type "mhe")
  11420. (let (folder article)
  11421. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11422. (error "Error in MHE link"))
  11423. (setq folder (match-string 1 path)
  11424. article (match-string 3 path))
  11425. (org-follow-mhe-link folder article)))
  11426. ((string= type "rmail")
  11427. (let (folder article)
  11428. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11429. (error "Error in RMAIL link"))
  11430. (setq folder (match-string 1 path)
  11431. article (match-string 3 path))
  11432. (org-follow-rmail-link folder article)))
  11433. ((string= type "shell")
  11434. (let ((cmd path))
  11435. (if (or (not org-confirm-shell-link-function)
  11436. (funcall org-confirm-shell-link-function
  11437. (format "Execute \"%s\" in shell? "
  11438. (org-add-props cmd nil
  11439. 'face 'org-warning))))
  11440. (progn
  11441. (message "Executing %s" cmd)
  11442. (shell-command cmd))
  11443. (error "Abort"))))
  11444. ((string= type "elisp")
  11445. (let ((cmd path))
  11446. (if (or (not org-confirm-elisp-link-function)
  11447. (funcall org-confirm-elisp-link-function
  11448. (format "Execute \"%s\" as elisp? "
  11449. (org-add-props cmd nil
  11450. 'face 'org-warning))))
  11451. (message "%s => %s" cmd (eval (read cmd)))
  11452. (error "Abort"))))
  11453. (t
  11454. (browse-url-at-point)))))
  11455. (move-marker org-open-link-marker nil)))
  11456. ;;; File search
  11457. (defvar org-create-file-search-functions nil
  11458. "List of functions to construct the right search string for a file link.
  11459. These functions are called in turn with point at the location to
  11460. which the link should point.
  11461. A function in the hook should first test if it would like to
  11462. handle this file type, for example by checking the major-mode or
  11463. the file extension. If it decides not to handle this file, it
  11464. should just return nil to give other functions a chance. If it
  11465. does handle the file, it must return the search string to be used
  11466. when following the link. The search string will be part of the
  11467. file link, given after a double colon, and `org-open-at-point'
  11468. will automatically search for it. If special measures must be
  11469. taken to make the search successful, another function should be
  11470. added to the companion hook `org-execute-file-search-functions',
  11471. which see.
  11472. A function in this hook may also use `setq' to set the variable
  11473. `description' to provide a suggestion for the descriptive text to
  11474. be used for this link when it gets inserted into an Org-mode
  11475. buffer with \\[org-insert-link].")
  11476. (defvar org-execute-file-search-functions nil
  11477. "List of functions to execute a file search triggered by a link.
  11478. Functions added to this hook must accept a single argument, the
  11479. search string that was part of the file link, the part after the
  11480. double colon. The function must first check if it would like to
  11481. handle this search, for example by checking the major-mode or the
  11482. file extension. If it decides not to handle this search, it
  11483. should just return nil to give other functions a chance. If it
  11484. does handle the search, it must return a non-nil value to keep
  11485. other functions from trying.
  11486. Each function can access the current prefix argument through the
  11487. variable `current-prefix-argument'. Note that a single prefix is
  11488. used to force opening a link in Emacs, so it may be good to only
  11489. use a numeric or double prefix to guide the search function.
  11490. In case this is needed, a function in this hook can also restore
  11491. the window configuration before `org-open-at-point' was called using:
  11492. (set-window-configuration org-window-config-before-follow-link)")
  11493. (defun org-link-search (s &optional type avoid-pos)
  11494. "Search for a link search option.
  11495. If S is surrounded by forward slashes, it is interpreted as a
  11496. regular expression. In org-mode files, this will create an `org-occur'
  11497. sparse tree. In ordinary files, `occur' will be used to list matches.
  11498. If the current buffer is in `dired-mode', grep will be used to search
  11499. in all files. If AVOID-POS is given, ignore matches near that position."
  11500. (let ((case-fold-search t)
  11501. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11502. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11503. (append '(("") (" ") ("\t") ("\n"))
  11504. org-emphasis-alist)
  11505. "\\|") "\\)"))
  11506. (pos (point))
  11507. (pre "") (post "")
  11508. words re0 re1 re2 re3 re4 re5 re2a reall)
  11509. (cond
  11510. ;; First check if there are any special
  11511. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11512. ;; Now try the builtin stuff
  11513. ((save-excursion
  11514. (goto-char (point-min))
  11515. (and
  11516. (re-search-forward
  11517. (concat "<<" (regexp-quote s0) ">>") nil t)
  11518. (setq pos (match-beginning 0))))
  11519. ;; There is an exact target for this
  11520. (goto-char pos))
  11521. ((string-match "^/\\(.*\\)/$" s)
  11522. ;; A regular expression
  11523. (cond
  11524. ((org-mode-p)
  11525. (org-occur (match-string 1 s)))
  11526. ;;((eq major-mode 'dired-mode)
  11527. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11528. (t (org-do-occur (match-string 1 s)))))
  11529. (t
  11530. ;; A normal search strings
  11531. (when (equal (string-to-char s) ?*)
  11532. ;; Anchor on headlines, post may include tags.
  11533. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11534. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11535. s (substring s 1)))
  11536. (remove-text-properties
  11537. 0 (length s)
  11538. '(face nil mouse-face nil keymap nil fontified nil) s)
  11539. ;; Make a series of regular expressions to find a match
  11540. (setq words (org-split-string s "[ \n\r\t]+")
  11541. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11542. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11543. "\\)" markers)
  11544. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11545. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11546. re1 (concat pre re2 post)
  11547. re3 (concat pre re4 post)
  11548. re5 (concat pre ".*" re4)
  11549. re2 (concat pre re2)
  11550. re2a (concat pre re2a)
  11551. re4 (concat pre re4)
  11552. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11553. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11554. re5 "\\)"
  11555. ))
  11556. (cond
  11557. ((eq type 'org-occur) (org-occur reall))
  11558. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11559. (t (goto-char (point-min))
  11560. (if (or (org-search-not-self 1 re0 nil t)
  11561. (org-search-not-self 1 re1 nil t)
  11562. (org-search-not-self 1 re2 nil t)
  11563. (org-search-not-self 1 re2a nil t)
  11564. (org-search-not-self 1 re3 nil t)
  11565. (org-search-not-self 1 re4 nil t)
  11566. (org-search-not-self 1 re5 nil t)
  11567. )
  11568. (goto-char (match-beginning 1))
  11569. (goto-char pos)
  11570. (error "No match")))))
  11571. (t
  11572. ;; Normal string-search
  11573. (goto-char (point-min))
  11574. (if (search-forward s nil t)
  11575. (goto-char (match-beginning 0))
  11576. (error "No match"))))
  11577. (and (org-mode-p) (org-show-context 'link-search))))
  11578. (defun org-search-not-self (group &rest args)
  11579. "Execute `re-search-forward', but only accept matches that do not
  11580. enclose the position of `org-open-link-marker'."
  11581. (let ((m org-open-link-marker))
  11582. (catch 'exit
  11583. (while (apply 're-search-forward args)
  11584. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11585. (goto-char (match-end group))
  11586. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11587. (> (match-beginning 0) (marker-position m))
  11588. (< (match-end 0) (marker-position m)))
  11589. (save-match-data
  11590. (or (not (org-in-regexp
  11591. org-bracket-link-analytic-regexp 1))
  11592. (not (match-end 4)) ; no description
  11593. (and (<= (match-beginning 4) (point))
  11594. (>= (match-end 4) (point))))))
  11595. (throw 'exit (point))))))))
  11596. (defun org-get-buffer-for-internal-link (buffer)
  11597. "Return a buffer to be used for displaying the link target of internal links."
  11598. (cond
  11599. ((not org-display-internal-link-with-indirect-buffer)
  11600. buffer)
  11601. ((string-match "(Clone)$" (buffer-name buffer))
  11602. (message "Buffer is already a clone, not making another one")
  11603. ;; we also do not modify visibility in this case
  11604. buffer)
  11605. (t ; make a new indirect buffer for displaying the link
  11606. (let* ((bn (buffer-name buffer))
  11607. (ibn (concat bn "(Clone)"))
  11608. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11609. (with-current-buffer ib (org-overview))
  11610. ib))))
  11611. (defun org-do-occur (regexp &optional cleanup)
  11612. "Call the Emacs command `occur'.
  11613. If CLEANUP is non-nil, remove the printout of the regular expression
  11614. in the *Occur* buffer. This is useful if the regex is long and not useful
  11615. to read."
  11616. (occur regexp)
  11617. (when cleanup
  11618. (let ((cwin (selected-window)) win beg end)
  11619. (when (setq win (get-buffer-window "*Occur*"))
  11620. (select-window win))
  11621. (goto-char (point-min))
  11622. (when (re-search-forward "match[a-z]+" nil t)
  11623. (setq beg (match-end 0))
  11624. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11625. (setq end (1- (match-beginning 0)))))
  11626. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11627. (goto-char (point-min))
  11628. (select-window cwin))))
  11629. ;;; The mark ring for links jumps
  11630. (defvar org-mark-ring nil
  11631. "Mark ring for positions before jumps in Org-mode.")
  11632. (defvar org-mark-ring-last-goto nil
  11633. "Last position in the mark ring used to go back.")
  11634. ;; Fill and close the ring
  11635. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11636. (loop for i from 1 to org-mark-ring-length do
  11637. (push (make-marker) org-mark-ring))
  11638. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11639. org-mark-ring)
  11640. (defun org-mark-ring-push (&optional pos buffer)
  11641. "Put the current position or POS into the mark ring and rotate it."
  11642. (interactive)
  11643. (setq pos (or pos (point)))
  11644. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11645. (move-marker (car org-mark-ring)
  11646. (or pos (point))
  11647. (or buffer (current-buffer)))
  11648. (message "%s"
  11649. (substitute-command-keys
  11650. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11651. (defun org-mark-ring-goto (&optional n)
  11652. "Jump to the previous position in the mark ring.
  11653. With prefix arg N, jump back that many stored positions. When
  11654. called several times in succession, walk through the entire ring.
  11655. Org-mode commands jumping to a different position in the current file,
  11656. or to another Org-mode file, automatically push the old position
  11657. onto the ring."
  11658. (interactive "p")
  11659. (let (p m)
  11660. (if (eq last-command this-command)
  11661. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11662. (setq p org-mark-ring))
  11663. (setq org-mark-ring-last-goto p)
  11664. (setq m (car p))
  11665. (switch-to-buffer (marker-buffer m))
  11666. (goto-char m)
  11667. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11668. (defun org-remove-angle-brackets (s)
  11669. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11670. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11671. s)
  11672. (defun org-add-angle-brackets (s)
  11673. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11674. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11675. s)
  11676. ;;; Following specific links
  11677. (defun org-follow-timestamp-link ()
  11678. (cond
  11679. ((org-at-date-range-p t)
  11680. (let ((org-agenda-start-on-weekday)
  11681. (t1 (match-string 1))
  11682. (t2 (match-string 2)))
  11683. (setq t1 (time-to-days (org-time-string-to-time t1))
  11684. t2 (time-to-days (org-time-string-to-time t2)))
  11685. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11686. ((org-at-timestamp-p t)
  11687. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11688. (substring (match-string 1) 0 10)))
  11689. 1))
  11690. (t (error "This should not happen"))))
  11691. (defun org-follow-bbdb-link (name)
  11692. "Follow a BBDB link to NAME."
  11693. (require 'bbdb)
  11694. (let ((inhibit-redisplay (not debug-on-error))
  11695. (bbdb-electric-p nil))
  11696. (catch 'exit
  11697. ;; Exact match on name
  11698. (bbdb-name (concat "\\`" name "\\'") nil)
  11699. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11700. ;; Exact match on name
  11701. (bbdb-company (concat "\\`" name "\\'") nil)
  11702. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11703. ;; Partial match on name
  11704. (bbdb-name name nil)
  11705. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11706. ;; Partial match on company
  11707. (bbdb-company name nil)
  11708. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11709. ;; General match including network address and notes
  11710. (bbdb name nil)
  11711. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11712. (delete-window (get-buffer-window "*BBDB*"))
  11713. (error "No matching BBDB record")))))
  11714. (defun org-follow-info-link (name)
  11715. "Follow an info file & node link to NAME."
  11716. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11717. (string-match "\\(.*\\)" name))
  11718. (progn
  11719. (require 'info)
  11720. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11721. (Info-find-node (match-string 1 name) (match-string 2 name))
  11722. (Info-find-node (match-string 1 name) "Top")))
  11723. (message "Could not open: %s" name)))
  11724. (defun org-follow-gnus-link (&optional group article)
  11725. "Follow a Gnus link to GROUP and ARTICLE."
  11726. (require 'gnus)
  11727. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11728. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11729. (cond ((and group article)
  11730. (gnus-group-read-group 1 nil group)
  11731. (gnus-summary-goto-article (string-to-number article) nil t))
  11732. (group (gnus-group-jump-to-group group))))
  11733. (defun org-follow-vm-link (&optional folder article readonly)
  11734. "Follow a VM link to FOLDER and ARTICLE."
  11735. (require 'vm)
  11736. (setq article (org-add-angle-brackets article))
  11737. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11738. ;; ange-ftp or efs or tramp access
  11739. (let ((user (or (match-string 1 folder) (user-login-name)))
  11740. (host (match-string 2 folder))
  11741. (file (match-string 3 folder)))
  11742. (cond
  11743. ((featurep 'tramp)
  11744. ;; use tramp to access the file
  11745. (if (featurep 'xemacs)
  11746. (setq folder (format "[%s@%s]%s" user host file))
  11747. (setq folder (format "/%s@%s:%s" user host file))))
  11748. (t
  11749. ;; use ange-ftp or efs
  11750. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11751. (setq folder (format "/%s@%s:%s" user host file))))))
  11752. (when folder
  11753. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11754. (sit-for 0.1)
  11755. (when article
  11756. (vm-select-folder-buffer)
  11757. (widen)
  11758. (let ((case-fold-search t))
  11759. (goto-char (point-min))
  11760. (if (not (re-search-forward
  11761. (concat "^" "message-id: *" (regexp-quote article))))
  11762. (error "Could not find the specified message in this folder"))
  11763. (vm-isearch-update)
  11764. (vm-isearch-narrow)
  11765. (vm-beginning-of-message)
  11766. (vm-summarize)))))
  11767. (defun org-follow-wl-link (folder article)
  11768. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11769. (if (and (string= folder "%")
  11770. article
  11771. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11772. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11773. ;; Thus, we recompose folder and article ids.
  11774. (setq folder (format "%s#%s" folder (match-string 1 article))
  11775. article (match-string 3 article)))
  11776. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11777. (error "No such folder: %s" folder))
  11778. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11779. (and article
  11780. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11781. (wl-summary-redisplay)))
  11782. (defun org-follow-rmail-link (folder article)
  11783. "Follow an RMAIL link to FOLDER and ARTICLE."
  11784. (setq article (org-add-angle-brackets article))
  11785. (let (message-number)
  11786. (save-excursion
  11787. (save-window-excursion
  11788. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11789. (setq message-number
  11790. (save-restriction
  11791. (widen)
  11792. (goto-char (point-max))
  11793. (if (re-search-backward
  11794. (concat "^Message-ID:\\s-+" (regexp-quote
  11795. (or article "")))
  11796. nil t)
  11797. (rmail-what-message))))))
  11798. (if message-number
  11799. (progn
  11800. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11801. (rmail-show-message message-number)
  11802. message-number)
  11803. (error "Message not found"))))
  11804. ;;; mh-e integration based on planner-mode
  11805. (defun org-mhe-get-message-real-folder ()
  11806. "Return the name of the current message real folder, so if you use
  11807. sequences, it will now work."
  11808. (save-excursion
  11809. (let* ((folder
  11810. (if (equal major-mode 'mh-folder-mode)
  11811. mh-current-folder
  11812. ;; Refer to the show buffer
  11813. mh-show-folder-buffer))
  11814. (end-index
  11815. (if (boundp 'mh-index-folder)
  11816. (min (length mh-index-folder) (length folder))))
  11817. )
  11818. ;; a simple test on mh-index-data does not work, because
  11819. ;; mh-index-data is always nil in a show buffer.
  11820. (if (and (boundp 'mh-index-folder)
  11821. (string= mh-index-folder (substring folder 0 end-index)))
  11822. (if (equal major-mode 'mh-show-mode)
  11823. (save-window-excursion
  11824. (let (pop-up-frames)
  11825. (when (buffer-live-p (get-buffer folder))
  11826. (progn
  11827. (pop-to-buffer folder)
  11828. (org-mhe-get-message-folder-from-index)
  11829. )
  11830. )))
  11831. (org-mhe-get-message-folder-from-index)
  11832. )
  11833. folder
  11834. )
  11835. )))
  11836. (defun org-mhe-get-message-folder-from-index ()
  11837. "Returns the name of the message folder in a index folder buffer."
  11838. (save-excursion
  11839. (mh-index-previous-folder)
  11840. (re-search-forward "^\\(+.*\\)$" nil t)
  11841. (message "%s" (match-string 1))))
  11842. (defun org-mhe-get-message-folder ()
  11843. "Return the name of the current message folder. Be careful if you
  11844. use sequences."
  11845. (save-excursion
  11846. (if (equal major-mode 'mh-folder-mode)
  11847. mh-current-folder
  11848. ;; Refer to the show buffer
  11849. mh-show-folder-buffer)))
  11850. (defun org-mhe-get-message-num ()
  11851. "Return the number of the current message. Be careful if you
  11852. use sequences."
  11853. (save-excursion
  11854. (if (equal major-mode 'mh-folder-mode)
  11855. (mh-get-msg-num nil)
  11856. ;; Refer to the show buffer
  11857. (mh-show-buffer-message-number))))
  11858. (defun org-mhe-get-header (header)
  11859. "Return a header of the message in folder mode. This will create a
  11860. show buffer for the corresponding message. If you have a more clever
  11861. idea..."
  11862. (let* ((folder (org-mhe-get-message-folder))
  11863. (num (org-mhe-get-message-num))
  11864. (buffer (get-buffer-create (concat "show-" folder)))
  11865. (header-field))
  11866. (with-current-buffer buffer
  11867. (mh-display-msg num folder)
  11868. (if (equal major-mode 'mh-folder-mode)
  11869. (mh-header-display)
  11870. (mh-show-header-display))
  11871. (set-buffer buffer)
  11872. (setq header-field (mh-get-header-field header))
  11873. (if (equal major-mode 'mh-folder-mode)
  11874. (mh-show)
  11875. (mh-show-show))
  11876. header-field)))
  11877. (defun org-follow-mhe-link (folder article)
  11878. "Follow an MHE link to FOLDER and ARTICLE.
  11879. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11880. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11881. ARTICLE is searched in all folders. Indexed searches (swish++,
  11882. namazu, and others supported by MH-E) will always search in all
  11883. folders."
  11884. (require 'mh-e)
  11885. (require 'mh-search)
  11886. (require 'mh-utils)
  11887. (mh-find-path)
  11888. (if (not article)
  11889. (mh-visit-folder (mh-normalize-folder-name folder))
  11890. (setq article (org-add-angle-brackets article))
  11891. (mh-search-choose)
  11892. (if (equal mh-searcher 'pick)
  11893. (progn
  11894. (mh-search folder (list "--message-id" article))
  11895. (when (and org-mhe-search-all-folders
  11896. (not (org-mhe-get-message-real-folder)))
  11897. (kill-this-buffer)
  11898. (mh-search "+" (list "--message-id" article))))
  11899. (mh-search "+" article))
  11900. (if (org-mhe-get-message-real-folder)
  11901. (mh-show-msg 1)
  11902. (kill-this-buffer)
  11903. (error "Message not found"))))
  11904. ;;; BibTeX links
  11905. ;; Use the custom search meachnism to construct and use search strings for
  11906. ;; file links to BibTeX database entries.
  11907. (defun org-create-file-search-in-bibtex ()
  11908. "Create the search string and description for a BibTeX database entry."
  11909. (when (eq major-mode 'bibtex-mode)
  11910. ;; yes, we want to construct this search string.
  11911. ;; Make a good description for this entry, using names, year and the title
  11912. ;; Put it into the `description' variable which is dynamically scoped.
  11913. (let ((bibtex-autokey-names 1)
  11914. (bibtex-autokey-names-stretch 1)
  11915. (bibtex-autokey-name-case-convert-function 'identity)
  11916. (bibtex-autokey-name-separator " & ")
  11917. (bibtex-autokey-additional-names " et al.")
  11918. (bibtex-autokey-year-length 4)
  11919. (bibtex-autokey-name-year-separator " ")
  11920. (bibtex-autokey-titlewords 3)
  11921. (bibtex-autokey-titleword-separator " ")
  11922. (bibtex-autokey-titleword-case-convert-function 'identity)
  11923. (bibtex-autokey-titleword-length 'infty)
  11924. (bibtex-autokey-year-title-separator ": "))
  11925. (setq description (bibtex-generate-autokey)))
  11926. ;; Now parse the entry, get the key and return it.
  11927. (save-excursion
  11928. (bibtex-beginning-of-entry)
  11929. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11930. (defun org-execute-file-search-in-bibtex (s)
  11931. "Find the link search string S as a key for a database entry."
  11932. (when (eq major-mode 'bibtex-mode)
  11933. ;; Yes, we want to do the search in this file.
  11934. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11935. (goto-char (point-min))
  11936. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11937. (regexp-quote s) "[ \t\n]*,") nil t)
  11938. (goto-char (match-beginning 0)))
  11939. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11940. ;; Use double prefix to indicate that any web link should be browsed
  11941. (let ((b (current-buffer)) (p (point)))
  11942. ;; Restore the window configuration because we just use the web link
  11943. (set-window-configuration org-window-config-before-follow-link)
  11944. (save-excursion (set-buffer b) (goto-char p)
  11945. (bibtex-url)))
  11946. (recenter 0)) ; Move entry start to beginning of window
  11947. ;; return t to indicate that the search is done.
  11948. t))
  11949. ;; Finally add the functions to the right hooks.
  11950. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11951. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11952. ;; end of Bibtex link setup
  11953. ;;; Following file links
  11954. (defun org-open-file (path &optional in-emacs line search)
  11955. "Open the file at PATH.
  11956. First, this expands any special file name abbreviations. Then the
  11957. configuration variable `org-file-apps' is checked if it contains an
  11958. entry for this file type, and if yes, the corresponding command is launched.
  11959. If no application is found, Emacs simply visits the file.
  11960. With optional argument IN-EMACS, Emacs will visit the file.
  11961. Optional LINE specifies a line to go to, optional SEARCH a string to
  11962. search for. If LINE or SEARCH is given, the file will always be
  11963. opened in Emacs.
  11964. If the file does not exist, an error is thrown."
  11965. (setq in-emacs (or in-emacs line search))
  11966. (let* ((file (if (equal path "")
  11967. buffer-file-name
  11968. (substitute-in-file-name (expand-file-name path))))
  11969. (apps (append org-file-apps (org-default-apps)))
  11970. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  11971. (dirp (if remp nil (file-directory-p file)))
  11972. (dfile (downcase file))
  11973. (old-buffer (current-buffer))
  11974. (old-pos (point))
  11975. (old-mode major-mode)
  11976. ext cmd)
  11977. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  11978. (setq ext (match-string 1 dfile))
  11979. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  11980. (setq ext (match-string 1 dfile))))
  11981. (if in-emacs
  11982. (setq cmd 'emacs)
  11983. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  11984. (and dirp (cdr (assoc 'directory apps)))
  11985. (cdr (assoc ext apps))
  11986. (cdr (assoc t apps)))))
  11987. (when (eq cmd 'mailcap)
  11988. (require 'mailcap)
  11989. (mailcap-parse-mailcaps)
  11990. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  11991. (command (mailcap-mime-info mime-type)))
  11992. (if (stringp command)
  11993. (setq cmd command)
  11994. (setq cmd 'emacs))))
  11995. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  11996. (not (file-exists-p file))
  11997. (not org-open-non-existing-files))
  11998. (error "No such file: %s" file))
  11999. (cond
  12000. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12001. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12002. (while (string-match "['\"]%s['\"]" cmd)
  12003. (setq cmd (replace-match "%s" t t cmd)))
  12004. (while (string-match "%s" cmd)
  12005. (setq cmd (replace-match (shell-quote-argument file) t t cmd)))
  12006. (save-window-excursion
  12007. (start-process-shell-command cmd nil cmd)))
  12008. ((or (stringp cmd)
  12009. (eq cmd 'emacs))
  12010. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12011. (widen)
  12012. (if line (goto-line line)
  12013. (if search (org-link-search search))))
  12014. ((consp cmd)
  12015. (eval cmd))
  12016. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12017. (and (org-mode-p) (eq old-mode 'org-mode)
  12018. (or (not (equal old-buffer (current-buffer)))
  12019. (not (equal old-pos (point))))
  12020. (org-mark-ring-push old-pos old-buffer))))
  12021. (defun org-default-apps ()
  12022. "Return the default applications for this operating system."
  12023. (cond
  12024. ((eq system-type 'darwin)
  12025. org-file-apps-defaults-macosx)
  12026. ((eq system-type 'windows-nt)
  12027. org-file-apps-defaults-windowsnt)
  12028. (t org-file-apps-defaults-gnu)))
  12029. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12030. (defun org-file-remote-p (file)
  12031. "Test whether FILE specifies a location on a remote system.
  12032. Return non-nil if the location is indeed remote.
  12033. For example, the filename \"/user@host:/foo\" specifies a location
  12034. on the system \"/user@host:\"."
  12035. (cond ((fboundp 'file-remote-p)
  12036. (file-remote-p file))
  12037. ((fboundp 'tramp-handle-file-remote-p)
  12038. (tramp-handle-file-remote-p file))
  12039. ((and (boundp 'ange-ftp-name-format)
  12040. (string-match (car ange-ftp-name-format) file))
  12041. t)
  12042. (t nil)))
  12043. ;;;; Hooks for remember.el, and refiling
  12044. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12045. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12046. ;;;###autoload
  12047. (defun org-remember-insinuate ()
  12048. "Setup remember.el for use wiht Org-mode."
  12049. (require 'remember)
  12050. (setq remember-annotation-functions '(org-remember-annotation))
  12051. (setq remember-handler-functions '(org-remember-handler))
  12052. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12053. ;;;###autoload
  12054. (defun org-remember-annotation ()
  12055. "Return a link to the current location as an annotation for remember.el.
  12056. If you are using Org-mode files as target for data storage with
  12057. remember.el, then the annotations should include a link compatible with the
  12058. conventions in Org-mode. This function returns such a link."
  12059. (org-store-link nil))
  12060. (defconst org-remember-help
  12061. "Select a destination location for the note.
  12062. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12063. RET on headline -> Store as sublevel entry to current headline
  12064. RET at beg-of-buf -> Append to file as level 2 headline
  12065. <left>/<right> -> before/after current headline, same headings level")
  12066. (defvar org-remember-previous-location nil)
  12067. (defvar org-force-remember-template-char) ;; dynamically scoped
  12068. (defun org-select-remember-template (&optional use-char)
  12069. (when org-remember-templates
  12070. (let* ((templates (mapcar (lambda (x)
  12071. (if (stringp (car x))
  12072. (append (list (nth 1 x) (car x)) (cddr x))
  12073. (append (list (car x) "") (cdr x))))
  12074. org-remember-templates))
  12075. (char (or use-char
  12076. (cond
  12077. ((= (length templates) 1)
  12078. (caar templates))
  12079. ((and (boundp 'org-force-remember-template-char)
  12080. org-force-remember-template-char)
  12081. (if (stringp org-force-remember-template-char)
  12082. (string-to-char org-force-remember-template-char)
  12083. org-force-remember-template-char))
  12084. (t
  12085. (message "Select template: %s"
  12086. (mapconcat
  12087. (lambda (x)
  12088. (cond
  12089. ((not (string-match "\\S-" (nth 1 x)))
  12090. (format "[%c]" (car x)))
  12091. ((equal (downcase (car x))
  12092. (downcase (aref (nth 1 x) 0)))
  12093. (format "[%c]%s" (car x)
  12094. (substring (nth 1 x) 1)))
  12095. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12096. templates " "))
  12097. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12098. (when (equal char0 ?\C-g)
  12099. (jump-to-register remember-register)
  12100. (kill-buffer remember-buffer))
  12101. char0))))))
  12102. (cddr (assoc char templates)))))
  12103. (defvar x-last-selected-text)
  12104. (defvar x-last-selected-text-primary)
  12105. ;;;###autoload
  12106. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12107. "Initialize *remember* buffer with template, invoke `org-mode'.
  12108. This function should be placed into `remember-mode-hook' and in fact requires
  12109. to be run from that hook to function properly."
  12110. (unless (fboundp 'remember-finalize)
  12111. (defalias 'remember-finalize 'remember-buffer))
  12112. (if org-remember-templates
  12113. (let* ((entry (org-select-remember-template use-char))
  12114. (tpl (car entry))
  12115. (plist-p (if org-store-link-plist t nil))
  12116. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12117. (string-match "\\S-" (nth 1 entry)))
  12118. (nth 1 entry)
  12119. org-default-notes-file))
  12120. (headline (nth 2 entry))
  12121. (v-c (or (and (eq window-system 'x)
  12122. (fboundp 'x-cut-buffer-or-selection-value)
  12123. (x-cut-buffer-or-selection-value))
  12124. (org-bound-and-true-p x-last-selected-text)
  12125. (org-bound-and-true-p x-last-selected-text-primary)
  12126. (and (> (length kill-ring) 0) (current-kill 0))))
  12127. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12128. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12129. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12130. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12131. ;; `initial' and `annotation' are bound in `remember'
  12132. (v-i (if (boundp 'initial) initial))
  12133. (v-a (if (and (boundp 'annotation) annotation)
  12134. (if (equal annotation "[[]]") "" annotation)
  12135. ""))
  12136. (v-A (if (and v-a
  12137. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12138. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12139. v-a))
  12140. (v-n user-full-name)
  12141. (org-startup-folded nil)
  12142. org-time-was-given org-end-time-was-given x
  12143. prompt completions char time pos default histvar)
  12144. (setq org-store-link-plist
  12145. (append (list :annotation v-a :initial v-i)
  12146. org-store-link-plist))
  12147. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12148. (erase-buffer)
  12149. (insert (substitute-command-keys
  12150. (format
  12151. "## Filing location: Select interactively, default, or last used:
  12152. ## %s to select file and header location interactively.
  12153. ## %s \"%s\" -> \"* %s\"
  12154. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12155. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12156. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12157. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12158. (abbreviate-file-name (or file org-default-notes-file))
  12159. (or headline "")
  12160. (or (car org-remember-previous-location) "???")
  12161. (or (cdr org-remember-previous-location) "???"))))
  12162. (insert tpl) (goto-char (point-min))
  12163. ;; Simple %-escapes
  12164. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12165. (when (and initial (equal (match-string 0) "%i"))
  12166. (save-match-data
  12167. (let* ((lead (buffer-substring
  12168. (point-at-bol) (match-beginning 0))))
  12169. (setq v-i (mapconcat 'identity
  12170. (org-split-string initial "\n")
  12171. (concat "\n" lead))))))
  12172. (replace-match
  12173. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12174. t t))
  12175. ;; %[] Insert contents of a file.
  12176. (goto-char (point-min))
  12177. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12178. (let ((start (match-beginning 0))
  12179. (end (match-end 0))
  12180. (filename (expand-file-name (match-string 1))))
  12181. (goto-char start)
  12182. (delete-region start end)
  12183. (condition-case error
  12184. (insert-file-contents filename)
  12185. (error (insert (format "%%![Couldn't insert %s: %s]"
  12186. filename error))))))
  12187. ;; %() embedded elisp
  12188. (goto-char (point-min))
  12189. (while (re-search-forward "%\\((.+)\\)" nil t)
  12190. (goto-char (match-beginning 0))
  12191. (let ((template-start (point)))
  12192. (forward-char 1)
  12193. (let ((result
  12194. (condition-case error
  12195. (eval (read (current-buffer)))
  12196. (error (format "%%![Error: %s]" error)))))
  12197. (delete-region template-start (point))
  12198. (insert result))))
  12199. ;; From the property list
  12200. (when plist-p
  12201. (goto-char (point-min))
  12202. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12203. (and (setq x (or (plist-get org-store-link-plist
  12204. (intern (match-string 1))) ""))
  12205. (replace-match x t t))))
  12206. ;; Turn on org-mode in the remember buffer, set local variables
  12207. (org-mode)
  12208. (org-set-local 'org-finish-function 'remember-finalize)
  12209. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12210. (org-set-local 'org-default-notes-file file))
  12211. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12212. (org-set-local 'org-remember-default-headline headline))
  12213. ;; Interactive template entries
  12214. (goto-char (point-min))
  12215. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  12216. (setq char (if (match-end 3) (match-string 3))
  12217. prompt (if (match-end 2) (match-string 2)))
  12218. (goto-char (match-beginning 0))
  12219. (replace-match "")
  12220. (setq completions nil default nil)
  12221. (when prompt
  12222. (setq completions (org-split-string prompt "|")
  12223. prompt (pop completions)
  12224. default (car completions)
  12225. histvar (intern (concat
  12226. "org-remember-template-prompt-history::"
  12227. (or prompt "")))
  12228. completions (mapcar 'list completions)))
  12229. (cond
  12230. ((member char '("G" "g"))
  12231. (let* ((org-last-tags-completion-table
  12232. (org-global-tags-completion-table
  12233. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12234. (org-add-colon-after-tag-completion t)
  12235. (ins (completing-read
  12236. (if prompt (concat prompt ": ") "Tags: ")
  12237. 'org-tags-completion-function nil nil nil
  12238. 'org-tags-history)))
  12239. (setq ins (mapconcat 'identity
  12240. (org-split-string ins (org-re "[^[:alnum:]]+"))
  12241. ":"))
  12242. (when (string-match "\\S-" ins)
  12243. (or (equal (char-before) ?:) (insert ":"))
  12244. (insert ins)
  12245. (or (equal (char-after) ?:) (insert ":")))))
  12246. (char
  12247. (setq org-time-was-given (equal (upcase char) char))
  12248. (setq time (org-read-date (equal (upcase char) "U") t nil
  12249. prompt))
  12250. (org-insert-time-stamp time org-time-was-given
  12251. (member char '("u" "U"))
  12252. nil nil (list org-end-time-was-given)))
  12253. (t
  12254. (insert (org-completing-read
  12255. (concat (if prompt prompt "Enter string")
  12256. (if default (concat " [" default "]"))
  12257. ": ")
  12258. completions nil nil nil histvar default)))))
  12259. (goto-char (point-min))
  12260. (if (re-search-forward "%\\?" nil t)
  12261. (replace-match "")
  12262. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12263. (org-mode)
  12264. (org-set-local 'org-finish-function 'remember-finalize))
  12265. (when (save-excursion
  12266. (goto-char (point-min))
  12267. (re-search-forward "%!" nil t))
  12268. (replace-match "")
  12269. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12270. (defun org-remember-finish-immediately ()
  12271. "File remember note immediately.
  12272. This should be run in `post-command-hook' and will remove itself
  12273. from that hook."
  12274. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12275. (when org-finish-function
  12276. (funcall org-finish-function)))
  12277. ;;;###autoload
  12278. (defun org-remember (&optional goto org-force-remember-template-char)
  12279. "Call `remember'. If this is already a remember buffer, re-apply template.
  12280. If there is an active region, make sure remember uses it as initial content
  12281. of the remember buffer.
  12282. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12283. anything, just go to the file/headline where the selected templated usually
  12284. stores its notes. With a double prefix arg `C-u C-u', got to the last
  12285. note stored by remember.
  12286. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12287. associated with a template in `org-remember-tempates'."
  12288. (interactive "P")
  12289. (cond
  12290. ((equal goto '(4)) (org-go-to-remember-target))
  12291. ((equal goto '(16)) (org-remember-goto-last-stored))
  12292. (t
  12293. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12294. (progn
  12295. (when (< (length org-remember-templates) 2)
  12296. (error "No other template available"))
  12297. (erase-buffer)
  12298. (let ((annotation (plist-get org-store-link-plist :annotation))
  12299. (initial (plist-get org-store-link-plist :initial)))
  12300. (org-remember-apply-template))
  12301. (message "Press C-c C-c to remember data"))
  12302. (if (org-region-active-p)
  12303. (remember (buffer-substring (point) (mark)))
  12304. (call-interactively 'remember))))))
  12305. (defun org-remember-goto-last-stored ()
  12306. "Go to the location where the last remember note was stored."
  12307. (interactive)
  12308. (bookmark-jump "org-remember-last-stored")
  12309. (message "This is the last note stored by remember"))
  12310. (defun org-go-to-remember-target (&optional template-key)
  12311. "Go to the target location of a remember template.
  12312. The user is queried for the template."
  12313. (interactive)
  12314. (let* ((entry (org-select-remember-template template-key))
  12315. (file (nth 1 entry))
  12316. (heading (nth 2 entry))
  12317. visiting)
  12318. (unless (and file (stringp file) (string-match "\\S-" file))
  12319. (setq file org-default-notes-file))
  12320. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12321. (setq heading org-remember-default-headline))
  12322. (setq visiting (org-find-base-buffer-visiting file))
  12323. (if (not visiting) (find-file-noselect file))
  12324. (switch-to-buffer (or visiting (get-file-buffer file)))
  12325. (widen)
  12326. (goto-char (point-min))
  12327. (if (re-search-forward
  12328. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12329. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12330. nil t)
  12331. (goto-char (match-beginning 0))
  12332. (error "Target headline not found: %s" heading))))
  12333. (defvar org-note-abort nil) ; dynamically scoped
  12334. ;;;###autoload
  12335. (defun org-remember-handler ()
  12336. "Store stuff from remember.el into an org file.
  12337. First prompts for an org file. If the user just presses return, the value
  12338. of `org-default-notes-file' is used.
  12339. Then the command offers the headings tree of the selected file in order to
  12340. file the text at a specific location.
  12341. You can either immediately press RET to get the note appended to the
  12342. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12343. find a better place. Then press RET or <left> or <right> in insert the note.
  12344. Key Cursor position Note gets inserted
  12345. -----------------------------------------------------------------------------
  12346. RET buffer-start as level 1 heading at end of file
  12347. RET on headline as sublevel of the heading at cursor
  12348. RET no heading at cursor position, level taken from context.
  12349. Or use prefix arg to specify level manually.
  12350. <left> on headline as same level, before current heading
  12351. <right> on headline as same level, after current heading
  12352. So the fastest way to store the note is to press RET RET to append it to
  12353. the default file. This way your current train of thought is not
  12354. interrupted, in accordance with the principles of remember.el.
  12355. You can also get the fast execution without prompting by using
  12356. C-u C-c C-c to exit the remember buffer. See also the variable
  12357. `org-remember-store-without-prompt'.
  12358. Before being stored away, the function ensures that the text has a
  12359. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12360. is constructed from the current date and some additional data.
  12361. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12362. also indented so that it starts in the same column as the headline
  12363. \(i.e. after the stars).
  12364. See also the variable `org-reverse-note-order'."
  12365. (goto-char (point-min))
  12366. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12367. (replace-match ""))
  12368. (goto-char (point-max))
  12369. (catch 'quit
  12370. (if org-note-abort (throw 'quit nil))
  12371. (let* ((txt (buffer-substring (point-min) (point-max)))
  12372. (fastp (org-xor (equal current-prefix-arg '(4))
  12373. org-remember-store-without-prompt))
  12374. (file (if fastp org-default-notes-file (org-get-org-file)))
  12375. (heading org-remember-default-headline)
  12376. (visiting (org-find-base-buffer-visiting file))
  12377. (org-startup-folded nil)
  12378. (org-startup-align-all-tables nil)
  12379. (org-goto-start-pos 1)
  12380. spos exitcmd level indent reversed)
  12381. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12382. (setq file (car org-remember-previous-location)
  12383. heading (cdr org-remember-previous-location)))
  12384. (setq current-prefix-arg nil)
  12385. (if (string-match "[ \t\n]+\\'" txt)
  12386. (setq txt (replace-match "" t t txt)))
  12387. ;; Modify text so that it becomes a nice subtree which can be inserted
  12388. ;; into an org tree.
  12389. (let* ((lines (split-string txt "\n"))
  12390. first)
  12391. (setq first (car lines) lines (cdr lines))
  12392. (if (string-match "^\\*+ " first)
  12393. ;; Is already a headline
  12394. (setq indent nil)
  12395. ;; We need to add a headline: Use time and first buffer line
  12396. (setq lines (cons first lines)
  12397. first (concat "* " (current-time-string)
  12398. " (" (remember-buffer-desc) ")")
  12399. indent " "))
  12400. (if (and org-adapt-indentation indent)
  12401. (setq lines (mapcar
  12402. (lambda (x)
  12403. (if (string-match "\\S-" x)
  12404. (concat indent x) x))
  12405. lines)))
  12406. (setq txt (concat first "\n"
  12407. (mapconcat 'identity lines "\n"))))
  12408. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12409. (setq txt (replace-match "\n\n" t t txt))
  12410. (if (string-match "[ \t\n]*\\'" txt)
  12411. (setq txt (replace-match "\n" t t txt))))
  12412. ;; Find the file
  12413. (if (not visiting) (find-file-noselect file))
  12414. (with-current-buffer (or visiting (get-file-buffer file))
  12415. (unless (org-mode-p)
  12416. (error "Target files for remember notes must be in Org-mode"))
  12417. (save-excursion
  12418. (save-restriction
  12419. (widen)
  12420. (and (goto-char (point-min))
  12421. (not (re-search-forward "^\\* " nil t))
  12422. (insert "\n* " (or heading "Notes") "\n"))
  12423. (setq reversed (org-notes-order-reversed-p))
  12424. ;; Find the default location
  12425. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12426. (goto-char (point-min))
  12427. (if (re-search-forward
  12428. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12429. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12430. nil t)
  12431. (setq org-goto-start-pos (match-beginning 0))
  12432. (when fastp
  12433. (goto-char (point-max))
  12434. (unless (bolp) (newline))
  12435. (insert "* " heading "\n")
  12436. (setq org-goto-start-pos (point-at-bol 0)))))
  12437. ;; Ask the User for a location
  12438. (if fastp
  12439. (setq spos org-goto-start-pos
  12440. exitcmd 'return)
  12441. (setq spos (org-get-location (current-buffer) org-remember-help)
  12442. exitcmd (cdr spos)
  12443. spos (car spos)))
  12444. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12445. ; not handle this note
  12446. (goto-char spos)
  12447. (cond ((org-on-heading-p t)
  12448. (org-back-to-heading t)
  12449. (setq level (funcall outline-level))
  12450. (cond
  12451. ((eq exitcmd 'return)
  12452. ;; sublevel of current
  12453. (setq org-remember-previous-location
  12454. (cons (abbreviate-file-name file)
  12455. (org-get-heading 'notags)))
  12456. (if reversed
  12457. (outline-next-heading)
  12458. (org-end-of-subtree)
  12459. (if (not (bolp))
  12460. (if (looking-at "[ \t]*\n")
  12461. (beginning-of-line 2)
  12462. (end-of-line 1)
  12463. (insert "\n"))))
  12464. (bookmark-set "org-remember-last-stored")
  12465. (org-paste-subtree (org-get-legal-level level 1) txt))
  12466. ((eq exitcmd 'left)
  12467. ;; before current
  12468. (bookmark-set "org-remember-last-stored")
  12469. (org-paste-subtree level txt))
  12470. ((eq exitcmd 'right)
  12471. ;; after current
  12472. (org-end-of-subtree t)
  12473. (bookmark-set "org-remember-last-stored")
  12474. (org-paste-subtree level txt))
  12475. (t (error "This should not happen"))))
  12476. ((and (bobp) (not reversed))
  12477. ;; Put it at the end, one level below level 1
  12478. (save-restriction
  12479. (widen)
  12480. (goto-char (point-max))
  12481. (if (not (bolp)) (newline))
  12482. (bookmark-set "org-remember-last-stored")
  12483. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12484. ((and (bobp) reversed)
  12485. ;; Put it at the start, as level 1
  12486. (save-restriction
  12487. (widen)
  12488. (goto-char (point-min))
  12489. (re-search-forward "^\\*+ " nil t)
  12490. (beginning-of-line 1)
  12491. (bookmark-set "org-remember-last-stored")
  12492. (org-paste-subtree 1 txt)))
  12493. (t
  12494. ;; Put it right there, with automatic level determined by
  12495. ;; org-paste-subtree or from prefix arg
  12496. (bookmark-set "org-remember-last-stored")
  12497. (org-paste-subtree
  12498. (if (numberp current-prefix-arg) current-prefix-arg)
  12499. txt)))
  12500. (when remember-save-after-remembering
  12501. (save-buffer)
  12502. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12503. t) ;; return t to indicate that we took care of this note.
  12504. (defun org-get-org-file ()
  12505. "Read a filename, with default directory `org-directory'."
  12506. (let ((default (or org-default-notes-file remember-data-file)))
  12507. (read-file-name (format "File name [%s]: " default)
  12508. (file-name-as-directory org-directory)
  12509. default)))
  12510. (defun org-notes-order-reversed-p ()
  12511. "Check if the current file should receive notes in reversed order."
  12512. (cond
  12513. ((not org-reverse-note-order) nil)
  12514. ((eq t org-reverse-note-order) t)
  12515. ((not (listp org-reverse-note-order)) nil)
  12516. (t (catch 'exit
  12517. (let ((all org-reverse-note-order)
  12518. entry)
  12519. (while (setq entry (pop all))
  12520. (if (string-match (car entry) buffer-file-name)
  12521. (throw 'exit (cdr entry))))
  12522. nil)))))
  12523. ;;; Refiling
  12524. (defvar org-refile-target-table nil
  12525. "The list of refile targets, created by `org-refile'.")
  12526. (defvar org-agenda-new-buffers nil
  12527. "Buffers created to visit agenda files.")
  12528. (defun org-get-refile-targets ()
  12529. "Produce a table with refile targets."
  12530. (let ((entries org-refile-targets)
  12531. org-agenda-new-files targets txt re files f desc descre)
  12532. (while (setq entry (pop entries))
  12533. (setq files (car entry) desc (cdr entry))
  12534. (cond
  12535. ((null files) (setq files (list (current-buffer))))
  12536. ((eq files 'org-agenda-files)
  12537. (setq files (org-agenda-files 'unrestricted)))
  12538. ((and (symbolp files) (fboundp files))
  12539. (setq files (funcall files)))
  12540. ((and (symbolp files) (boundp files))
  12541. (setq files (symbol-value files))))
  12542. (if (stringp files) (setq files (list files)))
  12543. (cond
  12544. ((eq (car desc) :tag)
  12545. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12546. ((eq (car desc) :todo)
  12547. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12548. ((eq (car desc) :regexp)
  12549. (setq descre (cdr desc)))
  12550. ((eq (car desc) :level)
  12551. (setq descre (concat "^\\*\\{" (number-to-string
  12552. (if org-odd-levels-only
  12553. (1- (* 2 (cdr desc)))
  12554. (cdr desc)))
  12555. "\\}[ \t]")))
  12556. ((eq (car desc) :maxlevel)
  12557. (setq descre (concat "^\\*\\{1," (number-to-string
  12558. (if org-odd-levels-only
  12559. (1- (* 2 (cdr desc)))
  12560. (cdr desc)))
  12561. "\\}[ \t]")))
  12562. (t (error "Bad refiling target description %s" desc)))
  12563. (while (setq f (pop files))
  12564. (save-excursion
  12565. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12566. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12567. (save-excursion
  12568. (save-restriction
  12569. (widen)
  12570. (goto-char (point-min))
  12571. (while (re-search-forward descre nil t)
  12572. (goto-char (point-at-bol))
  12573. (when (looking-at org-complex-heading-regexp)
  12574. (setq txt (match-string 4)
  12575. re (concat "^" (regexp-quote
  12576. (buffer-substring (match-beginning 1)
  12577. (match-end 4)))))
  12578. (if (match-end 5) (setq re (concat re "[ \t]+"
  12579. (regexp-quote
  12580. (match-string 5)))))
  12581. (setq re (concat re "[ \t]*$"))
  12582. (when org-refile-use-outline-path
  12583. (setq txt (mapconcat 'identity
  12584. (append
  12585. (if (eq org-refile-use-outline-path 'file)
  12586. (list (file-name-nondirectory
  12587. (buffer-file-name (buffer-base-buffer))))
  12588. (if (eq org-refile-use-outline-path 'full-file-path)
  12589. (list (buffer-file-name (buffer-base-buffer)))))
  12590. (org-get-outline-path)
  12591. (list txt))
  12592. "/")))
  12593. (push (list txt f re (point)) targets))
  12594. (goto-char (point-at-eol))))))))
  12595. (org-release-buffers org-agenda-new-buffers)
  12596. (nreverse targets)))
  12597. (defun org-get-outline-path ()
  12598. (let (rtn)
  12599. (save-excursion
  12600. (while (org-up-heading-safe)
  12601. (when (looking-at org-complex-heading-regexp)
  12602. (push (org-match-string-no-properties 4) rtn)))
  12603. rtn)))
  12604. (defun org-refile (&optional reversed-or-update)
  12605. "Move the entry at point to another heading.
  12606. The list of target headings is compiled using the information in
  12607. `org-refile-targets', which see. This list is created upon first use, and
  12608. you can update it by calling this command with a double prefix (`C-u C-u').
  12609. At the target location, the entry is filed as a subitem of the target heading.
  12610. Depending on `org-reverse-note-order', the new subitem will either be the
  12611. first of the last subitem. A single C-u prefix will toggle the value of this
  12612. variable for the duration of the command."
  12613. (interactive "P")
  12614. (if (equal reversed-or-update '(16))
  12615. (progn
  12616. (setq org-refile-target-table (org-get-refile-targets))
  12617. (message "Refile targets updated (%d targets)"
  12618. (length org-refile-target-table)))
  12619. (when (or (not org-refile-target-table)
  12620. (and (= (length org-refile-targets) 1)
  12621. (not (caar org-refile-targets))))
  12622. (setq org-refile-target-table (org-get-refile-targets)))
  12623. (unless org-refile-target-table
  12624. (error "No refile targets"))
  12625. (let* ((cbuf (current-buffer))
  12626. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12627. (fname (and filename (file-truename filename)))
  12628. (tbl (mapcar
  12629. (lambda (x)
  12630. (if (not (equal fname (file-truename (nth 1 x))))
  12631. (cons (concat (car x) " (" (file-name-nondirectory
  12632. (nth 1 x)) ")")
  12633. (cdr x))
  12634. x))
  12635. org-refile-target-table))
  12636. (completion-ignore-case t)
  12637. pos it nbuf file re level reversed)
  12638. (when (setq it (completing-read "Refile to: " tbl
  12639. nil t nil 'org-refile-history))
  12640. (setq it (assoc it tbl)
  12641. file (nth 1 it)
  12642. re (nth 2 it))
  12643. (org-copy-special)
  12644. (save-excursion
  12645. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12646. (find-file-noselect file))))
  12647. (setq reversed (org-notes-order-reversed-p))
  12648. (if (equal reversed-or-update '(16)) (setq reversed (not reversed)))
  12649. (save-excursion
  12650. (save-restriction
  12651. (widen)
  12652. (goto-char (point-min))
  12653. (unless (re-search-forward re nil t)
  12654. (error "Cannot find target location - try again with `C-u' prefix."))
  12655. (goto-char (match-beginning 0))
  12656. (looking-at outline-regexp)
  12657. (setq level (org-get-legal-level (funcall outline-level) 1))
  12658. (goto-char (or (save-excursion
  12659. (if reversed
  12660. (outline-next-heading)
  12661. (outline-get-next-sibling)))
  12662. (point-max)))
  12663. (org-paste-subtree level))))
  12664. (org-cut-special)
  12665. (message "Entry refiled to \"%s\"" (car it))))))
  12666. ;;;; Dynamic blocks
  12667. (defun org-find-dblock (name)
  12668. "Find the first dynamic block with name NAME in the buffer.
  12669. If not found, stay at current position and return nil."
  12670. (let (pos)
  12671. (save-excursion
  12672. (goto-char (point-min))
  12673. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12674. nil t)
  12675. (match-beginning 0))))
  12676. (if pos (goto-char pos))
  12677. pos))
  12678. (defconst org-dblock-start-re
  12679. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12680. "Matches the startline of a dynamic block, with parameters.")
  12681. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12682. "Matches the end of a dyhamic block.")
  12683. (defun org-create-dblock (plist)
  12684. "Create a dynamic block section, with parameters taken from PLIST.
  12685. PLIST must containe a :name entry which is used as name of the block."
  12686. (unless (bolp) (newline))
  12687. (let ((name (plist-get plist :name)))
  12688. (insert "#+BEGIN: " name)
  12689. (while plist
  12690. (if (eq (car plist) :name)
  12691. (setq plist (cddr plist))
  12692. (insert " " (prin1-to-string (pop plist)))))
  12693. (insert "\n\n#+END:\n")
  12694. (beginning-of-line -2)))
  12695. (defun org-prepare-dblock ()
  12696. "Prepare dynamic block for refresh.
  12697. This empties the block, puts the cursor at the insert position and returns
  12698. the property list including an extra property :name with the block name."
  12699. (unless (looking-at org-dblock-start-re)
  12700. (error "Not at a dynamic block"))
  12701. (let* ((begdel (1+ (match-end 0)))
  12702. (name (org-no-properties (match-string 1)))
  12703. (params (append (list :name name)
  12704. (read (concat "(" (match-string 3) ")")))))
  12705. (unless (re-search-forward org-dblock-end-re nil t)
  12706. (error "Dynamic block not terminated"))
  12707. (delete-region begdel (match-beginning 0))
  12708. (goto-char begdel)
  12709. (open-line 1)
  12710. params))
  12711. (defun org-map-dblocks (&optional command)
  12712. "Apply COMMAND to all dynamic blocks in the current buffer.
  12713. If COMMAND is not given, use `org-update-dblock'."
  12714. (let ((cmd (or command 'org-update-dblock))
  12715. pos)
  12716. (save-excursion
  12717. (goto-char (point-min))
  12718. (while (re-search-forward org-dblock-start-re nil t)
  12719. (goto-char (setq pos (match-beginning 0)))
  12720. (condition-case nil
  12721. (funcall cmd)
  12722. (error (message "Error during update of dynamic block")))
  12723. (goto-char pos)
  12724. (unless (re-search-forward org-dblock-end-re nil t)
  12725. (error "Dynamic block not terminated"))))))
  12726. (defun org-dblock-update (&optional arg)
  12727. "User command for updating dynamic blocks.
  12728. Update the dynamic block at point. With prefix ARG, update all dynamic
  12729. blocks in the buffer."
  12730. (interactive "P")
  12731. (if arg
  12732. (org-update-all-dblocks)
  12733. (or (looking-at org-dblock-start-re)
  12734. (org-beginning-of-dblock))
  12735. (org-update-dblock)))
  12736. (defun org-update-dblock ()
  12737. "Update the dynamic block at point
  12738. This means to empty the block, parse for parameters and then call
  12739. the correct writing function."
  12740. (save-window-excursion
  12741. (let* ((pos (point))
  12742. (line (org-current-line))
  12743. (params (org-prepare-dblock))
  12744. (name (plist-get params :name))
  12745. (cmd (intern (concat "org-dblock-write:" name))))
  12746. (message "Updating dynamic block `%s' at line %d..." name line)
  12747. (funcall cmd params)
  12748. (message "Updating dynamic block `%s' at line %d...done" name line)
  12749. (goto-char pos))))
  12750. (defun org-beginning-of-dblock ()
  12751. "Find the beginning of the dynamic block at point.
  12752. Error if there is no scuh block at point."
  12753. (let ((pos (point))
  12754. beg)
  12755. (end-of-line 1)
  12756. (if (and (re-search-backward org-dblock-start-re nil t)
  12757. (setq beg (match-beginning 0))
  12758. (re-search-forward org-dblock-end-re nil t)
  12759. (> (match-end 0) pos))
  12760. (goto-char beg)
  12761. (goto-char pos)
  12762. (error "Not in a dynamic block"))))
  12763. (defun org-update-all-dblocks ()
  12764. "Update all dynamic blocks in the buffer.
  12765. This function can be used in a hook."
  12766. (when (org-mode-p)
  12767. (org-map-dblocks 'org-update-dblock)))
  12768. ;;;; Completion
  12769. (defconst org-additional-option-like-keywords
  12770. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12771. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12772. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12773. (defun org-complete (&optional arg)
  12774. "Perform completion on word at point.
  12775. At the beginning of a headline, this completes TODO keywords as given in
  12776. `org-todo-keywords'.
  12777. If the current word is preceded by a backslash, completes the TeX symbols
  12778. that are supported for HTML support.
  12779. If the current word is preceded by \"#+\", completes special words for
  12780. setting file options.
  12781. In the line after \"#+STARTUP:, complete valid keywords.\"
  12782. At all other locations, this simply calls the value of
  12783. `org-completion-fallback-command'."
  12784. (interactive "P")
  12785. (org-without-partial-completion
  12786. (catch 'exit
  12787. (let* ((end (point))
  12788. (beg1 (save-excursion
  12789. (skip-chars-backward (org-re "[:alnum:]_@"))
  12790. (point)))
  12791. (beg (save-excursion
  12792. (skip-chars-backward "a-zA-Z0-9_:$")
  12793. (point)))
  12794. (confirm (lambda (x) (stringp (car x))))
  12795. (searchhead (equal (char-before beg) ?*))
  12796. (tag (and (equal (char-before beg1) ?:)
  12797. (equal (char-after (point-at-bol)) ?*)))
  12798. (prop (and (equal (char-before beg1) ?:)
  12799. (not (equal (char-after (point-at-bol)) ?*))))
  12800. (texp (equal (char-before beg) ?\\))
  12801. (link (equal (char-before beg) ?\[))
  12802. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12803. beg)
  12804. "#+"))
  12805. (startup (string-match "^#\\+STARTUP:.*"
  12806. (buffer-substring (point-at-bol) (point))))
  12807. (completion-ignore-case opt)
  12808. (type nil)
  12809. (tbl nil)
  12810. (table (cond
  12811. (opt
  12812. (setq type :opt)
  12813. (append
  12814. (mapcar
  12815. (lambda (x)
  12816. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12817. (cons (match-string 2 x) (match-string 1 x)))
  12818. (org-split-string (org-get-current-options) "\n"))
  12819. (mapcar 'list org-additional-option-like-keywords)))
  12820. (startup
  12821. (setq type :startup)
  12822. org-startup-options)
  12823. (link (append org-link-abbrev-alist-local
  12824. org-link-abbrev-alist))
  12825. (texp
  12826. (setq type :tex)
  12827. org-html-entities)
  12828. ((string-match "\\`\\*+[ \t]+\\'"
  12829. (buffer-substring (point-at-bol) beg))
  12830. (setq type :todo)
  12831. (mapcar 'list org-todo-keywords-1))
  12832. (searchhead
  12833. (setq type :searchhead)
  12834. (save-excursion
  12835. (goto-char (point-min))
  12836. (while (re-search-forward org-todo-line-regexp nil t)
  12837. (push (list
  12838. (org-make-org-heading-search-string
  12839. (match-string 3) t))
  12840. tbl)))
  12841. tbl)
  12842. (tag (setq type :tag beg beg1)
  12843. (or org-tag-alist (org-get-buffer-tags)))
  12844. (prop (setq type :prop beg beg1)
  12845. (mapcar 'list (org-buffer-property-keys)))
  12846. (t (progn
  12847. (call-interactively org-completion-fallback-command)
  12848. (throw 'exit nil)))))
  12849. (pattern (buffer-substring-no-properties beg end))
  12850. (completion (try-completion pattern table confirm)))
  12851. (cond ((eq completion t)
  12852. (if (not (assoc (upcase pattern) table))
  12853. (message "Already complete")
  12854. (if (equal type :opt)
  12855. (insert (substring (cdr (assoc (upcase pattern) table))
  12856. (length pattern)))
  12857. (if (memq type '(:tag :prop)) (insert ":")))))
  12858. ((null completion)
  12859. (message "Can't find completion for \"%s\"" pattern)
  12860. (ding))
  12861. ((not (string= pattern completion))
  12862. (delete-region beg end)
  12863. (if (string-match " +$" completion)
  12864. (setq completion (replace-match "" t t completion)))
  12865. (insert completion)
  12866. (if (get-buffer-window "*Completions*")
  12867. (delete-window (get-buffer-window "*Completions*")))
  12868. (if (assoc completion table)
  12869. (if (eq type :todo) (insert " ")
  12870. (if (memq type '(:tag :prop)) (insert ":"))))
  12871. (if (and (equal type :opt) (assoc completion table))
  12872. (message "%s" (substitute-command-keys
  12873. "Press \\[org-complete] again to insert example settings"))))
  12874. (t
  12875. (message "Making completion list...")
  12876. (let ((list (sort (all-completions pattern table confirm)
  12877. 'string<)))
  12878. (with-output-to-temp-buffer "*Completions*"
  12879. (condition-case nil
  12880. ;; Protection needed for XEmacs and emacs 21
  12881. (display-completion-list list pattern)
  12882. (error (display-completion-list list)))))
  12883. (message "Making completion list...%s" "done")))))))
  12884. ;;;; TODO, DEADLINE, Comments
  12885. (defun org-toggle-comment ()
  12886. "Change the COMMENT state of an entry."
  12887. (interactive)
  12888. (save-excursion
  12889. (org-back-to-heading)
  12890. (let (case-fold-search)
  12891. (if (looking-at (concat outline-regexp
  12892. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  12893. (replace-match "" t t nil 1)
  12894. (if (looking-at outline-regexp)
  12895. (progn
  12896. (goto-char (match-end 0))
  12897. (insert org-comment-string " ")))))))
  12898. (defvar org-last-todo-state-is-todo nil
  12899. "This is non-nil when the last TODO state change led to a TODO state.
  12900. If the last change removed the TODO tag or switched to DONE, then
  12901. this is nil.")
  12902. (defvar org-setting-tags nil) ; dynamically skiped
  12903. ;; FIXME: better place
  12904. (defun org-property-or-variable-value (var &optional inherit)
  12905. "Check if there is a property fixing the value of VAR.
  12906. If yes, return this value. If not, return the current value of the variable."
  12907. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12908. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12909. (read prop)
  12910. (symbol-value var))))
  12911. (defun org-parse-local-options (string var)
  12912. "Parse STRING for startup setting relevant for variable VAR."
  12913. (let ((rtn (symbol-value var))
  12914. e opts)
  12915. (save-match-data
  12916. (if (or (not string) (not (string-match "\\S-" string)))
  12917. rtn
  12918. (setq opts (delq nil (mapcar (lambda (x)
  12919. (setq e (assoc x org-startup-options))
  12920. (if (eq (nth 1 e) var) e nil))
  12921. (org-split-string string "[ \t]+"))))
  12922. (if (not opts)
  12923. rtn
  12924. (setq rtn nil)
  12925. (while (setq e (pop opts))
  12926. (if (not (nth 3 e))
  12927. (setq rtn (nth 2 e))
  12928. (if (not (listp rtn)) (setq rtn nil))
  12929. (push (nth 2 e) rtn)))
  12930. rtn)))))
  12931. (defvar org-blocker-hook nil
  12932. "Hook for functions that are allowed to block a state change.
  12933. Each function gets as its single argument a property list, see
  12934. `org-trigger-hook' for more information about this list.
  12935. If any of the functions in this hook returns nil, the state change
  12936. is blocked.")
  12937. (defvar org-trigger-hook nil
  12938. "Hook for functions that are triggered by a state change.
  12939. Each function gets as its single argument a property list with at least
  12940. the following elements:
  12941. (:type type-of-change :position pos-at-entry-start
  12942. :from old-state :to new-state)
  12943. Depending on the type, more properties may be present.
  12944. This mechanism is currently implemented for:
  12945. TODO state changes
  12946. ------------------
  12947. :type todo-state-change
  12948. :from previous state (keyword as a string), or nil
  12949. :to new state (keyword as a string), or nil")
  12950. (defun org-todo (&optional arg)
  12951. "Change the TODO state of an item.
  12952. The state of an item is given by a keyword at the start of the heading,
  12953. like
  12954. *** TODO Write paper
  12955. *** DONE Call mom
  12956. The different keywords are specified in the variable `org-todo-keywords'.
  12957. By default the available states are \"TODO\" and \"DONE\".
  12958. So for this example: when the item starts with TODO, it is changed to DONE.
  12959. When it starts with DONE, the DONE is removed. And when neither TODO nor
  12960. DONE are present, add TODO at the beginning of the heading.
  12961. With C-u prefix arg, use completion to determine the new state.
  12962. With numeric prefix arg, switch to that state.
  12963. For calling through lisp, arg is also interpreted in the following way:
  12964. 'none -> empty state
  12965. \"\"(empty string) -> switch to empty state
  12966. 'done -> switch to DONE
  12967. 'nextset -> switch to the next set of keywords
  12968. 'previousset -> switch to the previous set of keywords
  12969. \"WAITING\" -> switch to the specified keyword, but only if it
  12970. really is a member of `org-todo-keywords'."
  12971. (interactive "P")
  12972. (save-excursion
  12973. (catch 'exit
  12974. (org-back-to-heading)
  12975. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  12976. (or (looking-at (concat " +" org-todo-regexp " *"))
  12977. (looking-at " *"))
  12978. (let* ((match-data (match-data))
  12979. (startpos (point-at-bol))
  12980. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  12981. (org-log-done (org-parse-local-options logging 'org-log-done))
  12982. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  12983. (this (match-string 1))
  12984. (hl-pos (match-beginning 0))
  12985. (head (org-get-todo-sequence-head this))
  12986. (ass (assoc head org-todo-kwd-alist))
  12987. (interpret (nth 1 ass))
  12988. (done-word (nth 3 ass))
  12989. (final-done-word (nth 4 ass))
  12990. (last-state (or this ""))
  12991. (completion-ignore-case t)
  12992. (member (member this org-todo-keywords-1))
  12993. (tail (cdr member))
  12994. (state (cond
  12995. ((and org-todo-key-trigger
  12996. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  12997. (and (not arg) org-use-fast-todo-selection
  12998. (not (eq org-use-fast-todo-selection 'prefix)))))
  12999. ;; Use fast selection
  13000. (org-fast-todo-selection))
  13001. ((and (equal arg '(4))
  13002. (or (not org-use-fast-todo-selection)
  13003. (not org-todo-key-trigger)))
  13004. ;; Read a state with completion
  13005. (completing-read "State: " (mapcar (lambda(x) (list x))
  13006. org-todo-keywords-1)
  13007. nil t))
  13008. ((eq arg 'right)
  13009. (if this
  13010. (if tail (car tail) nil)
  13011. (car org-todo-keywords-1)))
  13012. ((eq arg 'left)
  13013. (if (equal member org-todo-keywords-1)
  13014. nil
  13015. (if this
  13016. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13017. org-todo-keywords-1)
  13018. (org-last org-todo-keywords-1))))
  13019. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13020. (setq arg nil))) ; hack to fall back to cycling
  13021. (arg
  13022. ;; user or caller requests a specific state
  13023. (cond
  13024. ((equal arg "") nil)
  13025. ((eq arg 'none) nil)
  13026. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13027. ((eq arg 'nextset)
  13028. (or (car (cdr (member head org-todo-heads)))
  13029. (car org-todo-heads)))
  13030. ((eq arg 'previousset)
  13031. (let ((org-todo-heads (reverse org-todo-heads)))
  13032. (or (car (cdr (member head org-todo-heads)))
  13033. (car org-todo-heads))))
  13034. ((car (member arg org-todo-keywords-1)))
  13035. ((nth (1- (prefix-numeric-value arg))
  13036. org-todo-keywords-1))))
  13037. ((null member) (or head (car org-todo-keywords-1)))
  13038. ((equal this final-done-word) nil) ;; -> make empty
  13039. ((null tail) nil) ;; -> first entry
  13040. ((eq interpret 'sequence)
  13041. (car tail))
  13042. ((memq interpret '(type priority))
  13043. (if (eq this-command last-command)
  13044. (car tail)
  13045. (if (> (length tail) 0)
  13046. (or done-word (car org-done-keywords))
  13047. nil)))
  13048. (t nil)))
  13049. (next (if state (concat " " state " ") " "))
  13050. (change-plist (list :type 'todo-state-change :from this :to state
  13051. :position startpos))
  13052. dostates)
  13053. (when org-blocker-hook
  13054. (unless (save-excursion
  13055. (save-match-data
  13056. (run-hook-with-args-until-failure
  13057. 'org-blocker-hook change-plist)))
  13058. (if (interactive-p)
  13059. (error "TODO state change from %s to %s blocked" this state)
  13060. ;; fail silently
  13061. (message "TODO state change from %s to %s blocked" this state)
  13062. (throw 'exit nil))))
  13063. (store-match-data match-data)
  13064. (replace-match next t t)
  13065. (unless (pos-visible-in-window-p hl-pos)
  13066. (message "TODO state changed to %s" (org-trim next)))
  13067. (unless head
  13068. (setq head (org-get-todo-sequence-head state)
  13069. ass (assoc head org-todo-kwd-alist)
  13070. interpret (nth 1 ass)
  13071. done-word (nth 3 ass)
  13072. final-done-word (nth 4 ass)))
  13073. (when (memq arg '(nextset previousset))
  13074. (message "Keyword-Set %d/%d: %s"
  13075. (- (length org-todo-sets) -1
  13076. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13077. (length org-todo-sets)
  13078. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13079. (setq org-last-todo-state-is-todo
  13080. (not (member state org-done-keywords)))
  13081. (when (and org-log-done (not (memq arg '(nextset previousset))))
  13082. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  13083. (or (not org-todo-log-states)
  13084. (member state org-todo-log-states))))
  13085. (cond
  13086. ((and state (member state org-not-done-keywords)
  13087. (not (member this org-not-done-keywords)))
  13088. ;; This is now a todo state and was not one before
  13089. ;; Remove any CLOSED timestamp, and possibly log the state change
  13090. (org-add-planning-info nil nil 'closed)
  13091. (and dostates (org-add-log-maybe 'state state 'findpos)))
  13092. ((and state dostates)
  13093. ;; This is a non-nil state, and we need to log it
  13094. (org-add-log-maybe 'state state 'findpos))
  13095. ((and (member state org-done-keywords)
  13096. (not (member this org-done-keywords)))
  13097. ;; It is now done, and it was not done before
  13098. (org-add-planning-info 'closed (org-current-time))
  13099. (org-add-log-maybe 'done state 'findpos))))
  13100. ;; Fixup tag positioning
  13101. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13102. (run-hooks 'org-after-todo-state-change-hook)
  13103. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  13104. (if (and arg (not (member state org-done-keywords)))
  13105. (setq head (org-get-todo-sequence-head state)))
  13106. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13107. ;; Fixup cursor location if close to the keyword
  13108. (if (and (outline-on-heading-p)
  13109. (not (bolp))
  13110. (save-excursion (beginning-of-line 1)
  13111. (looking-at org-todo-line-regexp))
  13112. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13113. (progn
  13114. (goto-char (or (match-end 2) (match-end 1)))
  13115. (just-one-space)))
  13116. (when org-trigger-hook
  13117. (save-excursion
  13118. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13119. (defun org-get-todo-sequence-head (kwd)
  13120. "Return the head of the TODO sequence to which KWD belongs.
  13121. If KWD is not set, check if there is a text property remembering the
  13122. right sequence."
  13123. (let (p)
  13124. (cond
  13125. ((not kwd)
  13126. (or (get-text-property (point-at-bol) 'org-todo-head)
  13127. (progn
  13128. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13129. nil (point-at-eol)))
  13130. (get-text-property p 'org-todo-head))))
  13131. ((not (member kwd org-todo-keywords-1))
  13132. (car org-todo-keywords-1))
  13133. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13134. (defun org-fast-todo-selection ()
  13135. "Fast TODO keyword selection with single keys.
  13136. Returns the new TODO keyword, or nil if no state change should occur."
  13137. (let* ((fulltable org-todo-key-alist)
  13138. (done-keywords org-done-keywords) ;; needed for the faces.
  13139. (maxlen (apply 'max (mapcar
  13140. (lambda (x)
  13141. (if (stringp (car x)) (string-width (car x)) 0))
  13142. fulltable)))
  13143. (expert nil)
  13144. (fwidth (+ maxlen 3 1 3))
  13145. (ncol (/ (- (window-width) 4) fwidth))
  13146. tg cnt e c tbl
  13147. groups ingroup)
  13148. (save-window-excursion
  13149. (if expert
  13150. (set-buffer (get-buffer-create " *Org todo*"))
  13151. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13152. (erase-buffer)
  13153. (org-set-local 'org-done-keywords done-keywords)
  13154. (setq tbl fulltable cnt 0)
  13155. (while (setq e (pop tbl))
  13156. (cond
  13157. ((equal e '(:startgroup))
  13158. (push '() groups) (setq ingroup t)
  13159. (when (not (= cnt 0))
  13160. (setq cnt 0)
  13161. (insert "\n"))
  13162. (insert "{ "))
  13163. ((equal e '(:endgroup))
  13164. (setq ingroup nil cnt 0)
  13165. (insert "}\n"))
  13166. (t
  13167. (setq tg (car e) c (cdr e))
  13168. (if ingroup (push tg (car groups)))
  13169. (setq tg (org-add-props tg nil 'face
  13170. (org-get-todo-face tg)))
  13171. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13172. (insert "[" c "] " tg (make-string
  13173. (- fwidth 4 (length tg)) ?\ ))
  13174. (when (= (setq cnt (1+ cnt)) ncol)
  13175. (insert "\n")
  13176. (if ingroup (insert " "))
  13177. (setq cnt 0)))))
  13178. (insert "\n")
  13179. (goto-char (point-min))
  13180. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13181. (fit-window-to-buffer))
  13182. (message "[a-z..]:Set [SPC]:clear")
  13183. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13184. (cond
  13185. ((or (= c ?\C-g)
  13186. (and (= c ?q) (not (rassoc c fulltable))))
  13187. (setq quit-flag t))
  13188. ((= c ?\ ) nil)
  13189. ((setq e (rassoc c fulltable) tg (car e))
  13190. tg)
  13191. (t (setq quit-flag t))))))
  13192. (defun org-get-repeat ()
  13193. "Check if tere is a deadline/schedule with repeater in this entry."
  13194. (save-match-data
  13195. (save-excursion
  13196. (org-back-to-heading t)
  13197. (if (re-search-forward
  13198. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13199. (match-string 1)))))
  13200. (defvar org-last-changed-timestamp)
  13201. (defvar org-log-post-message)
  13202. (defun org-auto-repeat-maybe ()
  13203. "Check if the current headline contains a repeated deadline/schedule.
  13204. If yes, set TODO state back to what it was and change the base date
  13205. of repeating deadline/scheduled time stamps to new date.
  13206. This function should be run in the `org-after-todo-state-change-hook'."
  13207. ;; last-state is dynamically scoped into this function
  13208. (let* ((repeat (org-get-repeat))
  13209. (aa (assoc last-state org-todo-kwd-alist))
  13210. (interpret (nth 1 aa))
  13211. (head (nth 2 aa))
  13212. (done-word (nth 3 aa))
  13213. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13214. (msg "Entry repeats: ")
  13215. (org-log-done)
  13216. re type n what ts)
  13217. (when repeat
  13218. (org-todo (if (eq interpret 'type) last-state head))
  13219. (when (and org-log-repeat
  13220. (not (memq 'org-add-log-note
  13221. (default-value 'post-command-hook))))
  13222. ;; Make sure a note is taken
  13223. (let ((org-log-done '(done)))
  13224. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  13225. 'findpos)))
  13226. (org-back-to-heading t)
  13227. (org-add-planning-info nil nil 'closed)
  13228. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13229. org-deadline-time-regexp "\\)"))
  13230. (while (re-search-forward
  13231. re (save-excursion (outline-next-heading) (point)) t)
  13232. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  13233. ts (match-string (if (match-end 2) 2 4)))
  13234. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13235. (setq n (string-to-number (match-string 1 ts))
  13236. what (match-string 2 ts))
  13237. (if (equal what "w") (setq n (* n 7) what "d"))
  13238. (org-timestamp-change n (cdr (assoc what whata))))
  13239. (setq msg (concat msg type org-last-changed-timestamp " ")))
  13240. (setq org-log-post-message msg)
  13241. (message "%s" msg))))
  13242. (defun org-show-todo-tree (arg)
  13243. "Make a compact tree which shows all headlines marked with TODO.
  13244. The tree will show the lines where the regexp matches, and all higher
  13245. headlines above the match.
  13246. With \\[universal-argument] prefix, also show the DONE entries.
  13247. With a numeric prefix N, construct a sparse tree for the Nth element
  13248. of `org-todo-keywords-1'."
  13249. (interactive "P")
  13250. (let ((case-fold-search nil)
  13251. (kwd-re
  13252. (cond ((null arg) org-not-done-regexp)
  13253. ((equal arg '(4))
  13254. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13255. (mapcar 'list org-todo-keywords-1))))
  13256. (concat "\\("
  13257. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13258. "\\)\\>")))
  13259. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13260. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13261. org-todo-keywords-1)))
  13262. (t (error "Invalid prefix argument: %s" arg)))))
  13263. (message "%d TODO entries found"
  13264. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13265. (defun org-deadline (&optional remove)
  13266. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13267. With argument REMOVE, remove any deadline from the item."
  13268. (interactive "P")
  13269. (if remove
  13270. (progn
  13271. (org-add-planning-info nil nil 'deadline)
  13272. (message "Item no longer has a deadline."))
  13273. (org-add-planning-info 'deadline nil 'closed)))
  13274. (defun org-schedule (&optional remove)
  13275. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13276. With argument REMOVE, remove any scheduling date from the item."
  13277. (interactive "P")
  13278. (if remove
  13279. (progn
  13280. (org-add-planning-info nil nil 'scheduled)
  13281. (message "Item is no longer scheduled."))
  13282. (org-add-planning-info 'scheduled nil 'closed)))
  13283. (defun org-add-planning-info (what &optional time &rest remove)
  13284. "Insert new timestamp with keyword in the line directly after the headline.
  13285. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13286. If non is given, the user is prompted for a date.
  13287. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13288. be removed."
  13289. (interactive)
  13290. (let (org-time-was-given org-end-time-was-given)
  13291. (when what (setq time (or time (org-read-date nil 'to-time))))
  13292. (when (and org-insert-labeled-timestamps-at-point
  13293. (member what '(scheduled deadline)))
  13294. (insert
  13295. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13296. (org-insert-time-stamp time org-time-was-given
  13297. nil nil nil (list org-end-time-was-given))
  13298. (setq what nil))
  13299. (save-excursion
  13300. (save-restriction
  13301. (let (col list elt ts buffer-invisibility-spec)
  13302. (org-back-to-heading t)
  13303. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13304. (goto-char (match-end 1))
  13305. (setq col (current-column))
  13306. (goto-char (match-end 0))
  13307. (if (eobp) (insert "\n") (forward-char 1))
  13308. (if (and (not (looking-at outline-regexp))
  13309. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13310. "[^\r\n]*"))
  13311. (not (equal (match-string 1) org-clock-string)))
  13312. (narrow-to-region (match-beginning 0) (match-end 0))
  13313. (insert-before-markers "\n")
  13314. (backward-char 1)
  13315. (narrow-to-region (point) (point))
  13316. (indent-to-column col))
  13317. ;; Check if we have to remove something.
  13318. (setq list (cons what remove))
  13319. (while list
  13320. (setq elt (pop list))
  13321. (goto-char (point-min))
  13322. (when (or (and (eq elt 'scheduled)
  13323. (re-search-forward org-scheduled-time-regexp nil t))
  13324. (and (eq elt 'deadline)
  13325. (re-search-forward org-deadline-time-regexp nil t))
  13326. (and (eq elt 'closed)
  13327. (re-search-forward org-closed-time-regexp nil t)))
  13328. (replace-match "")
  13329. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13330. (if (looking-at " +") (replace-match ""))))
  13331. (goto-char (point-max))
  13332. (when what
  13333. (insert
  13334. (if (not (equal (char-before) ?\ )) " " "")
  13335. (cond ((eq what 'scheduled) org-scheduled-string)
  13336. ((eq what 'deadline) org-deadline-string)
  13337. ((eq what 'closed) org-closed-string))
  13338. " ")
  13339. (setq ts (org-insert-time-stamp
  13340. time
  13341. (or org-time-was-given
  13342. (and (eq what 'closed) org-log-done-with-time))
  13343. (eq what 'closed)
  13344. nil nil (list org-end-time-was-given)))
  13345. (end-of-line 1))
  13346. (goto-char (point-min))
  13347. (widen)
  13348. (if (looking-at "[ \t]+\r?\n")
  13349. (replace-match ""))
  13350. ts)))))
  13351. (defvar org-log-note-marker (make-marker))
  13352. (defvar org-log-note-purpose nil)
  13353. (defvar org-log-note-state nil)
  13354. (defvar org-log-note-window-configuration nil)
  13355. (defvar org-log-note-return-to (make-marker))
  13356. (defvar org-log-post-message nil
  13357. "Message to be displayed after a log note has been stored.
  13358. The auto-repeater uses this.")
  13359. (defun org-add-log-maybe (&optional purpose state findpos)
  13360. "Set up the post command hook to take a note."
  13361. (save-excursion
  13362. (when (and (listp org-log-done)
  13363. (memq purpose org-log-done))
  13364. (when findpos
  13365. (org-back-to-heading t)
  13366. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13367. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13368. "[^\r\n]*\\)?"))
  13369. (goto-char (match-end 0))
  13370. (unless org-log-states-order-reversed
  13371. (and (= (char-after) ?\n) (forward-char 1))
  13372. (org-skip-over-state-notes)
  13373. (skip-chars-backward " \t\n\r")))
  13374. (move-marker org-log-note-marker (point))
  13375. (setq org-log-note-purpose purpose)
  13376. (setq org-log-note-state state)
  13377. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  13378. (defun org-skip-over-state-notes ()
  13379. "Skip past the list of State notes in an entry."
  13380. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13381. (while (looking-at "[ \t]*- State")
  13382. (condition-case nil
  13383. (org-next-item)
  13384. (error (org-end-of-item)))))
  13385. (defun org-add-log-note (&optional purpose)
  13386. "Pop up a window for taking a note, and add this note later at point."
  13387. (remove-hook 'post-command-hook 'org-add-log-note)
  13388. (setq org-log-note-window-configuration (current-window-configuration))
  13389. (delete-other-windows)
  13390. (move-marker org-log-note-return-to (point))
  13391. (switch-to-buffer (marker-buffer org-log-note-marker))
  13392. (goto-char org-log-note-marker)
  13393. (org-switch-to-buffer-other-window "*Org Note*")
  13394. (erase-buffer)
  13395. (let ((org-inhibit-startup t)) (org-mode))
  13396. (insert (format "# Insert note for %s.
  13397. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13398. (cond
  13399. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13400. ((eq org-log-note-purpose 'done) "closed todo item")
  13401. ((eq org-log-note-purpose 'state)
  13402. (format "state change to \"%s\"" org-log-note-state))
  13403. (t (error "This should not happen")))))
  13404. (org-set-local 'org-finish-function 'org-store-log-note))
  13405. (defun org-store-log-note ()
  13406. "Finish taking a log note, and insert it to where it belongs."
  13407. (let ((txt (buffer-string))
  13408. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13409. lines ind)
  13410. (kill-buffer (current-buffer))
  13411. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13412. (setq txt (replace-match "" t t txt)))
  13413. (if (string-match "\\s-+\\'" txt)
  13414. (setq txt (replace-match "" t t txt)))
  13415. (setq lines (org-split-string txt "\n"))
  13416. (when (and note (string-match "\\S-" note))
  13417. (setq note
  13418. (org-replace-escapes
  13419. note
  13420. (list (cons "%u" (user-login-name))
  13421. (cons "%U" user-full-name)
  13422. (cons "%t" (format-time-string
  13423. (org-time-stamp-format 'long 'inactive)
  13424. (current-time)))
  13425. (cons "%s" (if org-log-note-state
  13426. (concat "\"" org-log-note-state "\"")
  13427. "")))))
  13428. (if lines (setq note (concat note " \\\\")))
  13429. (push note lines))
  13430. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13431. (when lines
  13432. (save-excursion
  13433. (set-buffer (marker-buffer org-log-note-marker))
  13434. (save-excursion
  13435. (goto-char org-log-note-marker)
  13436. (move-marker org-log-note-marker nil)
  13437. (end-of-line 1)
  13438. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13439. (indent-relative nil)
  13440. (insert "- " (pop lines))
  13441. (org-indent-line-function)
  13442. (beginning-of-line 1)
  13443. (looking-at "[ \t]*")
  13444. (setq ind (concat (match-string 0) " "))
  13445. (end-of-line 1)
  13446. (while lines (insert "\n" ind (pop lines)))))))
  13447. (set-window-configuration org-log-note-window-configuration)
  13448. (with-current-buffer (marker-buffer org-log-note-return-to)
  13449. (goto-char org-log-note-return-to))
  13450. (move-marker org-log-note-return-to nil)
  13451. (and org-log-post-message (message "%s" org-log-post-message)))
  13452. ;; FIXME: what else would be useful?
  13453. ;; - priority
  13454. ;; - date
  13455. (defun org-sparse-tree (&optional arg)
  13456. "Create a sparse tree, prompt for the details.
  13457. This command can create sparse trees. You first need to select the type
  13458. of match used to create the tree:
  13459. t Show entries with a specific TODO keyword.
  13460. T Show entries selected by a tags match.
  13461. p Enter a property name and its value (both with completion on existing
  13462. names/values) and show entries with that property.
  13463. r Show entries matching a regular expression
  13464. d Show deadlines due within `org-deadline-warning-days'."
  13465. (interactive "P")
  13466. (let (ans kwd value)
  13467. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13468. (setq ans (read-char-exclusive))
  13469. (cond
  13470. ((equal ans ?d)
  13471. (call-interactively 'org-check-deadlines))
  13472. ((equal ans ?b)
  13473. (call-interactively 'org-check-before-date))
  13474. ((equal ans ?t)
  13475. (org-show-todo-tree '(4)))
  13476. ((equal ans ?T)
  13477. (call-interactively 'org-tags-sparse-tree))
  13478. ((member ans '(?p ?P))
  13479. (setq kwd (completing-read "Property: "
  13480. (mapcar 'list (org-buffer-property-keys))))
  13481. (setq value (completing-read "Value: "
  13482. (mapcar 'list (org-property-values kwd))))
  13483. (unless (string-match "\\`{.*}\\'" value)
  13484. (setq value (concat "\"" value "\"")))
  13485. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13486. ((member ans '(?r ?R ?/))
  13487. (call-interactively 'org-occur))
  13488. (t (error "No such sparse tree command \"%c\"" ans)))))
  13489. (defvar org-occur-highlights nil)
  13490. (make-variable-buffer-local 'org-occur-highlights)
  13491. (defun org-occur (regexp &optional keep-previous callback)
  13492. "Make a compact tree which shows all matches of REGEXP.
  13493. The tree will show the lines where the regexp matches, and all higher
  13494. headlines above the match. It will also show the heading after the match,
  13495. to make sure editing the matching entry is easy.
  13496. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13497. call to `org-occur' will be kept, to allow stacking of calls to this
  13498. command.
  13499. If CALLBACK is non-nil, it is a function which is called to confirm
  13500. that the match should indeed be shown."
  13501. (interactive "sRegexp: \nP")
  13502. (or keep-previous (org-remove-occur-highlights nil nil t))
  13503. (let ((cnt 0))
  13504. (save-excursion
  13505. (goto-char (point-min))
  13506. (if (or (not keep-previous) ; do not want to keep
  13507. (not org-occur-highlights)) ; no previous matches
  13508. ;; hide everything
  13509. (org-overview))
  13510. (while (re-search-forward regexp nil t)
  13511. (when (or (not callback)
  13512. (save-match-data (funcall callback)))
  13513. (setq cnt (1+ cnt))
  13514. (when org-highlight-sparse-tree-matches
  13515. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13516. (org-show-context 'occur-tree))))
  13517. (when org-remove-highlights-with-change
  13518. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13519. nil 'local))
  13520. (unless org-sparse-tree-open-archived-trees
  13521. (org-hide-archived-subtrees (point-min) (point-max)))
  13522. (run-hooks 'org-occur-hook)
  13523. (if (interactive-p)
  13524. (message "%d match(es) for regexp %s" cnt regexp))
  13525. cnt))
  13526. (defun org-show-context (&optional key)
  13527. "Make sure point and context and visible.
  13528. How much context is shown depends upon the variables
  13529. `org-show-hierarchy-above', `org-show-following-heading'. and
  13530. `org-show-siblings'."
  13531. (let ((heading-p (org-on-heading-p t))
  13532. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13533. (following-p (org-get-alist-option org-show-following-heading key))
  13534. (entry-p (org-get-alist-option org-show-entry-below key))
  13535. (siblings-p (org-get-alist-option org-show-siblings key)))
  13536. (catch 'exit
  13537. ;; Show heading or entry text
  13538. (if (and heading-p (not entry-p))
  13539. (org-flag-heading nil) ; only show the heading
  13540. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13541. (org-show-hidden-entry))) ; show entire entry
  13542. (when following-p
  13543. ;; Show next sibling, or heading below text
  13544. (save-excursion
  13545. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13546. (org-flag-heading nil))))
  13547. (when siblings-p (org-show-siblings))
  13548. (when hierarchy-p
  13549. ;; show all higher headings, possibly with siblings
  13550. (save-excursion
  13551. (while (and (condition-case nil
  13552. (progn (org-up-heading-all 1) t)
  13553. (error nil))
  13554. (not (bobp)))
  13555. (org-flag-heading nil)
  13556. (when siblings-p (org-show-siblings))))))))
  13557. (defun org-reveal (&optional siblings)
  13558. "Show current entry, hierarchy above it, and the following headline.
  13559. This can be used to show a consistent set of context around locations
  13560. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13561. not t for the search context.
  13562. With optional argument SIBLINGS, on each level of the hierarchy all
  13563. siblings are shown. This repairs the tree structure to what it would
  13564. look like when opened with hierarchical calls to `org-cycle'."
  13565. (interactive "P")
  13566. (let ((org-show-hierarchy-above t)
  13567. (org-show-following-heading t)
  13568. (org-show-siblings (if siblings t org-show-siblings)))
  13569. (org-show-context nil)))
  13570. (defun org-highlight-new-match (beg end)
  13571. "Highlight from BEG to END and mark the highlight is an occur headline."
  13572. (let ((ov (org-make-overlay beg end)))
  13573. (org-overlay-put ov 'face 'secondary-selection)
  13574. (push ov org-occur-highlights)))
  13575. (defun org-remove-occur-highlights (&optional beg end noremove)
  13576. "Remove the occur highlights from the buffer.
  13577. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13578. from the `before-change-functions' in the current buffer."
  13579. (interactive)
  13580. (unless org-inhibit-highlight-removal
  13581. (mapc 'org-delete-overlay org-occur-highlights)
  13582. (setq org-occur-highlights nil)
  13583. (unless noremove
  13584. (remove-hook 'before-change-functions
  13585. 'org-remove-occur-highlights 'local))))
  13586. ;;;; Priorities
  13587. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13588. "Regular expression matching the priority indicator.")
  13589. (defvar org-remove-priority-next-time nil)
  13590. (defun org-priority-up ()
  13591. "Increase the priority of the current item."
  13592. (interactive)
  13593. (org-priority 'up))
  13594. (defun org-priority-down ()
  13595. "Decrease the priority of the current item."
  13596. (interactive)
  13597. (org-priority 'down))
  13598. (defun org-priority (&optional action)
  13599. "Change the priority of an item by ARG.
  13600. ACTION can be `set', `up', `down', or a character."
  13601. (interactive)
  13602. (setq action (or action 'set))
  13603. (let (current new news have remove)
  13604. (save-excursion
  13605. (org-back-to-heading)
  13606. (if (looking-at org-priority-regexp)
  13607. (setq current (string-to-char (match-string 2))
  13608. have t)
  13609. (setq current org-default-priority))
  13610. (cond
  13611. ((or (eq action 'set) (integerp action))
  13612. (if (integerp action)
  13613. (setq new action)
  13614. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13615. (setq new (read-char-exclusive)))
  13616. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13617. (= (upcase org-lowest-priority) org-lowest-priority))
  13618. (setq new (upcase new)))
  13619. (cond ((equal new ?\ ) (setq remove t))
  13620. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13621. (error "Priority must be between `%c' and `%c'"
  13622. org-highest-priority org-lowest-priority))))
  13623. ((eq action 'up)
  13624. (if (and (not have) (eq last-command this-command))
  13625. (setq new org-lowest-priority)
  13626. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13627. org-default-priority (1- current)))))
  13628. ((eq action 'down)
  13629. (if (and (not have) (eq last-command this-command))
  13630. (setq new org-highest-priority)
  13631. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13632. org-default-priority (1+ current)))))
  13633. (t (error "Invalid action")))
  13634. (if (or (< (upcase new) org-highest-priority)
  13635. (> (upcase new) org-lowest-priority))
  13636. (setq remove t))
  13637. (setq news (format "%c" new))
  13638. (if have
  13639. (if remove
  13640. (replace-match "" t t nil 1)
  13641. (replace-match news t t nil 2))
  13642. (if remove
  13643. (error "No priority cookie found in line")
  13644. (looking-at org-todo-line-regexp)
  13645. (if (match-end 2)
  13646. (progn
  13647. (goto-char (match-end 2))
  13648. (insert " [#" news "]"))
  13649. (goto-char (match-beginning 3))
  13650. (insert "[#" news "] ")))))
  13651. (org-preserve-lc (org-set-tags nil 'align))
  13652. (if remove
  13653. (message "Priority removed")
  13654. (message "Priority of current item set to %s" news))))
  13655. (defun org-get-priority (s)
  13656. "Find priority cookie and return priority."
  13657. (save-match-data
  13658. (if (not (string-match org-priority-regexp s))
  13659. (* 1000 (- org-lowest-priority org-default-priority))
  13660. (* 1000 (- org-lowest-priority
  13661. (string-to-char (match-string 2 s)))))))
  13662. ;;;; Tags
  13663. (defun org-scan-tags (action matcher &optional todo-only)
  13664. "Scan headline tags with inheritance and produce output ACTION.
  13665. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13666. evaluated, testing if a given set of tags qualifies a headline for
  13667. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13668. are included in the output."
  13669. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13670. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13671. (org-re
  13672. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13673. (props (list 'face nil
  13674. 'done-face 'org-done
  13675. 'undone-face nil
  13676. 'mouse-face 'highlight
  13677. 'org-not-done-regexp org-not-done-regexp
  13678. 'org-todo-regexp org-todo-regexp
  13679. 'keymap org-agenda-keymap
  13680. 'help-echo
  13681. (format "mouse-2 or RET jump to org file %s"
  13682. (abbreviate-file-name
  13683. (or (buffer-file-name (buffer-base-buffer))
  13684. (buffer-name (buffer-base-buffer)))))))
  13685. (case-fold-search nil)
  13686. lspos
  13687. tags tags-list tags-alist (llast 0) rtn level category i txt
  13688. todo marker entry priority)
  13689. (save-excursion
  13690. (goto-char (point-min))
  13691. (when (eq action 'sparse-tree)
  13692. (org-overview)
  13693. (org-remove-occur-highlights))
  13694. (while (re-search-forward re nil t)
  13695. (catch :skip
  13696. (setq todo (if (match-end 1) (match-string 2))
  13697. tags (if (match-end 4) (match-string 4)))
  13698. (goto-char (setq lspos (1+ (match-beginning 0))))
  13699. (setq level (org-reduced-level (funcall outline-level))
  13700. category (org-get-category))
  13701. (setq i llast llast level)
  13702. ;; remove tag lists from same and sublevels
  13703. (while (>= i level)
  13704. (when (setq entry (assoc i tags-alist))
  13705. (setq tags-alist (delete entry tags-alist)))
  13706. (setq i (1- i)))
  13707. ;; add the nex tags
  13708. (when tags
  13709. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13710. tags-alist
  13711. (cons (cons level tags) tags-alist)))
  13712. ;; compile tags for current headline
  13713. (setq tags-list
  13714. (if org-use-tag-inheritance
  13715. (apply 'append (mapcar 'cdr tags-alist))
  13716. tags))
  13717. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13718. (eval matcher)
  13719. (or (not org-agenda-skip-archived-trees)
  13720. (not (member org-archive-tag tags-list))))
  13721. (and (eq action 'agenda) (org-agenda-skip))
  13722. ;; list this headline
  13723. (if (eq action 'sparse-tree)
  13724. (progn
  13725. (and org-highlight-sparse-tree-matches
  13726. (org-get-heading) (match-end 0)
  13727. (org-highlight-new-match
  13728. (match-beginning 0) (match-beginning 1)))
  13729. (org-show-context 'tags-tree))
  13730. (setq txt (org-format-agenda-item
  13731. ""
  13732. (concat
  13733. (if org-tags-match-list-sublevels
  13734. (make-string (1- level) ?.) "")
  13735. (org-get-heading))
  13736. category tags-list)
  13737. priority (org-get-priority txt))
  13738. (goto-char lspos)
  13739. (setq marker (org-agenda-new-marker))
  13740. (org-add-props txt props
  13741. 'org-marker marker 'org-hd-marker marker 'org-category category
  13742. 'priority priority 'type "tagsmatch")
  13743. (push txt rtn))
  13744. ;; if we are to skip sublevels, jump to end of subtree
  13745. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13746. (when (and (eq action 'sparse-tree)
  13747. (not org-sparse-tree-open-archived-trees))
  13748. (org-hide-archived-subtrees (point-min) (point-max)))
  13749. (nreverse rtn)))
  13750. (defvar todo-only) ;; dynamically scoped
  13751. (defun org-tags-sparse-tree (&optional todo-only match)
  13752. "Create a sparse tree according to tags string MATCH.
  13753. MATCH can contain positive and negative selection of tags, like
  13754. \"+WORK+URGENT-WITHBOSS\".
  13755. If optional argument TODO_ONLY is non-nil, only select lines that are
  13756. also TODO lines."
  13757. (interactive "P")
  13758. (org-prepare-agenda-buffers (list (current-buffer)))
  13759. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13760. (defvar org-cached-props nil)
  13761. (defun org-cached-entry-get (pom property)
  13762. (if (or (eq t org-use-property-inheritance)
  13763. (member property org-use-property-inheritance))
  13764. ;; Caching is not possible, check it directly
  13765. (org-entry-get pom property 'inherit)
  13766. ;; Get all properties, so that we can do complicated checks easily
  13767. (cdr (assoc property (or org-cached-props
  13768. (setq org-cached-props
  13769. (org-entry-properties pom)))))))
  13770. (defun org-global-tags-completion-table (&optional files)
  13771. "Return the list of all tags in all agenda buffer/files."
  13772. (save-excursion
  13773. (org-uniquify
  13774. (apply 'append
  13775. (mapcar
  13776. (lambda (file)
  13777. (set-buffer (find-file-noselect file))
  13778. (org-get-buffer-tags))
  13779. (if (and files (car files))
  13780. files
  13781. (org-agenda-files)))))))
  13782. (defun org-make-tags-matcher (match)
  13783. "Create the TAGS//TODO matcher form for the selection string MATCH."
  13784. ;; todo-only is scoped dynamically into this function, and the function
  13785. ;; may change it it the matcher asksk for it.
  13786. (unless match
  13787. ;; Get a new match request, with completion
  13788. (let ((org-last-tags-completion-table
  13789. (org-global-tags-completion-table)))
  13790. (setq match (completing-read
  13791. "Match: " 'org-tags-completion-function nil nil nil
  13792. 'org-tags-history))))
  13793. ;; Parse the string and create a lisp form
  13794. (let ((match0 match)
  13795. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  13796. minus tag mm
  13797. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13798. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  13799. (if (string-match "/+" match)
  13800. ;; match contains also a todo-matching request
  13801. (progn
  13802. (setq tagsmatch (substring match 0 (match-beginning 0))
  13803. todomatch (substring match (match-end 0)))
  13804. (if (string-match "^!" todomatch)
  13805. (setq todo-only t todomatch (substring todomatch 1)))
  13806. (if (string-match "^\\s-*$" todomatch)
  13807. (setq todomatch nil)))
  13808. ;; only matching tags
  13809. (setq tagsmatch match todomatch nil))
  13810. ;; Make the tags matcher
  13811. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13812. (setq tagsmatcher t)
  13813. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13814. (while (setq term (pop orterms))
  13815. (while (and (equal (substring term -1) "\\") orterms)
  13816. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13817. (while (string-match re term)
  13818. (setq minus (and (match-end 1)
  13819. (equal (match-string 1 term) "-"))
  13820. tag (match-string 2 term)
  13821. re-p (equal (string-to-char tag) ?{)
  13822. level-p (match-end 3)
  13823. prop-p (match-end 4)
  13824. mm (cond
  13825. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13826. (level-p `(= level ,(string-to-number
  13827. (match-string 3 term))))
  13828. (prop-p
  13829. (setq pn (match-string 4 term)
  13830. pv (match-string 5 term)
  13831. cat-p (equal pn "CATEGORY")
  13832. re-p (equal (string-to-char pv) ?{)
  13833. pv (substring pv 1 -1))
  13834. (if (equal pn "CATEGORY")
  13835. (setq gv '(get-text-property (point) 'org-category))
  13836. (setq gv `(org-cached-entry-get nil ,pn)))
  13837. (if re-p
  13838. `(string-match ,pv (or ,gv ""))
  13839. `(equal ,pv ,gv)))
  13840. (t `(member ,(downcase tag) tags-list)))
  13841. mm (if minus (list 'not mm) mm)
  13842. term (substring term (match-end 0)))
  13843. (push mm tagsmatcher))
  13844. (push (if (> (length tagsmatcher) 1)
  13845. (cons 'and tagsmatcher)
  13846. (car tagsmatcher))
  13847. orlist)
  13848. (setq tagsmatcher nil))
  13849. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13850. (setq tagsmatcher
  13851. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13852. ;; Make the todo matcher
  13853. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13854. (setq todomatcher t)
  13855. (setq orterms (org-split-string todomatch "|") orlist nil)
  13856. (while (setq term (pop orterms))
  13857. (while (string-match re term)
  13858. (setq minus (and (match-end 1)
  13859. (equal (match-string 1 term) "-"))
  13860. kwd (match-string 2 term)
  13861. re-p (equal (string-to-char kwd) ?{)
  13862. term (substring term (match-end 0))
  13863. mm (if re-p
  13864. `(string-match ,(substring kwd 1 -1) todo)
  13865. (list 'equal 'todo kwd))
  13866. mm (if minus (list 'not mm) mm))
  13867. (push mm todomatcher))
  13868. (push (if (> (length todomatcher) 1)
  13869. (cons 'and todomatcher)
  13870. (car todomatcher))
  13871. orlist)
  13872. (setq todomatcher nil))
  13873. (setq todomatcher (if (> (length orlist) 1)
  13874. (cons 'or orlist) (car orlist))))
  13875. ;; Return the string and lisp forms of the matcher
  13876. (setq matcher (if todomatcher
  13877. (list 'and tagsmatcher todomatcher)
  13878. tagsmatcher))
  13879. (cons match0 matcher)))
  13880. (defun org-match-any-p (re list)
  13881. "Does re match any element of list?"
  13882. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13883. (delq nil list))
  13884. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  13885. (defvar org-tags-overlay (org-make-overlay 1 1))
  13886. (org-detach-overlay org-tags-overlay)
  13887. (defun org-align-tags-here (to-col)
  13888. ;; Assumes that this is a headline
  13889. (let ((pos (point)) (col (current-column)) tags)
  13890. (beginning-of-line 1)
  13891. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13892. (< pos (match-beginning 2)))
  13893. (progn
  13894. (setq tags (match-string 2))
  13895. (goto-char (match-beginning 1))
  13896. (insert " ")
  13897. (delete-region (point) (1+ (match-end 0)))
  13898. (backward-char 1)
  13899. (move-to-column
  13900. (max (1+ (current-column))
  13901. (1+ col)
  13902. (if (> to-col 0)
  13903. to-col
  13904. (- (abs to-col) (length tags))))
  13905. t)
  13906. (insert tags)
  13907. (move-to-column (min (current-column) col) t))
  13908. (goto-char pos))))
  13909. (defun org-set-tags (&optional arg just-align)
  13910. "Set the tags for the current headline.
  13911. With prefix ARG, realign all tags in headings in the current buffer."
  13912. (interactive "P")
  13913. (let* ((re (concat "^" outline-regexp))
  13914. (current (org-get-tags-string))
  13915. (col (current-column))
  13916. (org-setting-tags t)
  13917. table current-tags inherited-tags ; computed below when needed
  13918. tags p0 c0 c1 rpl)
  13919. (if arg
  13920. (save-excursion
  13921. (goto-char (point-min))
  13922. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13923. (while (re-search-forward re nil t)
  13924. (org-set-tags nil t)
  13925. (end-of-line 1)))
  13926. (message "All tags realigned to column %d" org-tags-column))
  13927. (if just-align
  13928. (setq tags current)
  13929. ;; Get a new set of tags from the user
  13930. (save-excursion
  13931. (setq table (or org-tag-alist (org-get-buffer-tags))
  13932. org-last-tags-completion-table table
  13933. current-tags (org-split-string current ":")
  13934. inherited-tags (nreverse
  13935. (nthcdr (length current-tags)
  13936. (nreverse (org-get-tags-at))))
  13937. tags
  13938. (if (or (eq t org-use-fast-tag-selection)
  13939. (and org-use-fast-tag-selection
  13940. (delq nil (mapcar 'cdr table))))
  13941. (org-fast-tag-selection
  13942. current-tags inherited-tags table
  13943. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  13944. (let ((org-add-colon-after-tag-completion t))
  13945. (org-trim
  13946. (org-without-partial-completion
  13947. (completing-read "Tags: " 'org-tags-completion-function
  13948. nil nil current 'org-tags-history)))))))
  13949. (while (string-match "[-+&]+" tags)
  13950. ;; No boolean logic, just a list
  13951. (setq tags (replace-match ":" t t tags))))
  13952. (if (string-match "\\`[\t ]*\\'" tags)
  13953. (setq tags "")
  13954. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13955. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13956. ;; Insert new tags at the correct column
  13957. (beginning-of-line 1)
  13958. (cond
  13959. ((and (equal current "") (equal tags "")))
  13960. ((re-search-forward
  13961. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13962. (point-at-eol) t)
  13963. (if (equal tags "")
  13964. (setq rpl "")
  13965. (goto-char (match-beginning 0))
  13966. (setq c0 (current-column) p0 (point)
  13967. c1 (max (1+ c0) (if (> org-tags-column 0)
  13968. org-tags-column
  13969. (- (- org-tags-column) (length tags))))
  13970. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13971. (replace-match rpl t t)
  13972. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  13973. tags)
  13974. (t (error "Tags alignment failed")))
  13975. (move-to-column col)
  13976. (unless just-align
  13977. (run-hooks 'org-after-tags-change-hook)))))
  13978. (defun org-change-tag-in-region (beg end tag off)
  13979. "Add or remove TAG for each entry in the region.
  13980. This works in the agenda, and also in an org-mode buffer."
  13981. (interactive
  13982. (list (region-beginning) (region-end)
  13983. (let ((org-last-tags-completion-table
  13984. (if (org-mode-p)
  13985. (org-get-buffer-tags)
  13986. (org-global-tags-completion-table))))
  13987. (completing-read
  13988. "Tag: " 'org-tags-completion-function nil nil nil
  13989. 'org-tags-history))
  13990. (progn
  13991. (message "[s]et or [r]emove? ")
  13992. (equal (read-char-exclusive) ?r))))
  13993. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13994. (let ((agendap (equal major-mode 'org-agenda-mode))
  13995. l1 l2 m buf pos newhead (cnt 0))
  13996. (goto-char end)
  13997. (setq l2 (1- (org-current-line)))
  13998. (goto-char beg)
  13999. (setq l1 (org-current-line))
  14000. (loop for l from l1 to l2 do
  14001. (goto-line l)
  14002. (setq m (get-text-property (point) 'org-hd-marker))
  14003. (when (or (and (org-mode-p) (org-on-heading-p))
  14004. (and agendap m))
  14005. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14006. pos (if agendap m (point)))
  14007. (with-current-buffer buf
  14008. (save-excursion
  14009. (save-restriction
  14010. (goto-char pos)
  14011. (setq cnt (1+ cnt))
  14012. (org-toggle-tag tag (if off 'off 'on))
  14013. (setq newhead (org-get-heading)))))
  14014. (and agendap (org-agenda-change-all-lines newhead m))))
  14015. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14016. (defun org-tags-completion-function (string predicate &optional flag)
  14017. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14018. (confirm (lambda (x) (stringp (car x)))))
  14019. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14020. (setq s1 (match-string 1 string)
  14021. s2 (match-string 2 string))
  14022. (setq s1 "" s2 string))
  14023. (cond
  14024. ((eq flag nil)
  14025. ;; try completion
  14026. (setq rtn (try-completion s2 ctable confirm))
  14027. (if (stringp rtn)
  14028. (setq rtn
  14029. (concat s1 s2 (substring rtn (length s2))
  14030. (if (and org-add-colon-after-tag-completion
  14031. (assoc rtn ctable))
  14032. ":" ""))))
  14033. rtn)
  14034. ((eq flag t)
  14035. ;; all-completions
  14036. (all-completions s2 ctable confirm)
  14037. )
  14038. ((eq flag 'lambda)
  14039. ;; exact match?
  14040. (assoc s2 ctable)))
  14041. ))
  14042. (defun org-fast-tag-insert (kwd tags face &optional end)
  14043. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14044. (insert (format "%-12s" (concat kwd ":"))
  14045. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14046. (or end "")))
  14047. (defun org-fast-tag-show-exit (flag)
  14048. (save-excursion
  14049. (goto-line 3)
  14050. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14051. (replace-match ""))
  14052. (when flag
  14053. (end-of-line 1)
  14054. (move-to-column (- (window-width) 19) t)
  14055. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14056. (defun org-set-current-tags-overlay (current prefix)
  14057. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14058. (if (featurep 'xemacs)
  14059. (org-overlay-display org-tags-overlay (concat prefix s)
  14060. 'secondary-selection)
  14061. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14062. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14063. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14064. "Fast tag selection with single keys.
  14065. CURRENT is the current list of tags in the headline, INHERITED is the
  14066. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14067. possibly with grouping information. TODO-TABLE is a similar table with
  14068. TODO keywords, should these have keys assigned to them.
  14069. If the keys are nil, a-z are automatically assigned.
  14070. Returns the new tags string, or nil to not change the current settings."
  14071. (let* ((fulltable (append table todo-table))
  14072. (maxlen (apply 'max (mapcar
  14073. (lambda (x)
  14074. (if (stringp (car x)) (string-width (car x)) 0))
  14075. fulltable)))
  14076. (buf (current-buffer))
  14077. (expert (eq org-fast-tag-selection-single-key 'expert))
  14078. (buffer-tags nil)
  14079. (fwidth (+ maxlen 3 1 3))
  14080. (ncol (/ (- (window-width) 4) fwidth))
  14081. (i-face 'org-done)
  14082. (c-face 'org-todo)
  14083. tg cnt e c char c1 c2 ntable tbl rtn
  14084. ov-start ov-end ov-prefix
  14085. (exit-after-next org-fast-tag-selection-single-key)
  14086. (done-keywords org-done-keywords)
  14087. groups ingroup)
  14088. (save-excursion
  14089. (beginning-of-line 1)
  14090. (if (looking-at
  14091. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14092. (setq ov-start (match-beginning 1)
  14093. ov-end (match-end 1)
  14094. ov-prefix "")
  14095. (setq ov-start (1- (point-at-eol))
  14096. ov-end (1+ ov-start))
  14097. (skip-chars-forward "^\n\r")
  14098. (setq ov-prefix
  14099. (concat
  14100. (buffer-substring (1- (point)) (point))
  14101. (if (> (current-column) org-tags-column)
  14102. " "
  14103. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14104. (org-move-overlay org-tags-overlay ov-start ov-end)
  14105. (save-window-excursion
  14106. (if expert
  14107. (set-buffer (get-buffer-create " *Org tags*"))
  14108. (delete-other-windows)
  14109. (split-window-vertically)
  14110. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14111. (erase-buffer)
  14112. (org-set-local 'org-done-keywords done-keywords)
  14113. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14114. (org-fast-tag-insert "Current" current c-face "\n\n")
  14115. (org-fast-tag-show-exit exit-after-next)
  14116. (org-set-current-tags-overlay current ov-prefix)
  14117. (setq tbl fulltable char ?a cnt 0)
  14118. (while (setq e (pop tbl))
  14119. (cond
  14120. ((equal e '(:startgroup))
  14121. (push '() groups) (setq ingroup t)
  14122. (when (not (= cnt 0))
  14123. (setq cnt 0)
  14124. (insert "\n"))
  14125. (insert "{ "))
  14126. ((equal e '(:endgroup))
  14127. (setq ingroup nil cnt 0)
  14128. (insert "}\n"))
  14129. (t
  14130. (setq tg (car e) c2 nil)
  14131. (if (cdr e)
  14132. (setq c (cdr e))
  14133. ;; automatically assign a character.
  14134. (setq c1 (string-to-char
  14135. (downcase (substring
  14136. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14137. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14138. (while (or (rassoc char ntable) (rassoc char table))
  14139. (setq char (1+ char)))
  14140. (setq c2 c1))
  14141. (setq c (or c2 char)))
  14142. (if ingroup (push tg (car groups)))
  14143. (setq tg (org-add-props tg nil 'face
  14144. (cond
  14145. ((not (assoc tg table))
  14146. (org-get-todo-face tg))
  14147. ((member tg current) c-face)
  14148. ((member tg inherited) i-face)
  14149. (t nil))))
  14150. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14151. (insert "[" c "] " tg (make-string
  14152. (- fwidth 4 (length tg)) ?\ ))
  14153. (push (cons tg c) ntable)
  14154. (when (= (setq cnt (1+ cnt)) ncol)
  14155. (insert "\n")
  14156. (if ingroup (insert " "))
  14157. (setq cnt 0)))))
  14158. (setq ntable (nreverse ntable))
  14159. (insert "\n")
  14160. (goto-char (point-min))
  14161. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14162. (fit-window-to-buffer))
  14163. (setq rtn
  14164. (catch 'exit
  14165. (while t
  14166. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14167. (if groups " [!] no groups" " [!]groups")
  14168. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14169. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14170. (cond
  14171. ((= c ?\r) (throw 'exit t))
  14172. ((= c ?!)
  14173. (setq groups (not groups))
  14174. (goto-char (point-min))
  14175. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14176. ((= c ?\C-c)
  14177. (if (not expert)
  14178. (org-fast-tag-show-exit
  14179. (setq exit-after-next (not exit-after-next)))
  14180. (setq expert nil)
  14181. (delete-other-windows)
  14182. (split-window-vertically)
  14183. (org-switch-to-buffer-other-window " *Org tags*")
  14184. (and (fboundp 'fit-window-to-buffer)
  14185. (fit-window-to-buffer))))
  14186. ((or (= c ?\C-g)
  14187. (and (= c ?q) (not (rassoc c ntable))))
  14188. (org-detach-overlay org-tags-overlay)
  14189. (setq quit-flag t))
  14190. ((= c ?\ )
  14191. (setq current nil)
  14192. (if exit-after-next (setq exit-after-next 'now)))
  14193. ((= c ?\t)
  14194. (condition-case nil
  14195. (setq tg (completing-read
  14196. "Tag: "
  14197. (or buffer-tags
  14198. (with-current-buffer buf
  14199. (org-get-buffer-tags)))))
  14200. (quit (setq tg "")))
  14201. (when (string-match "\\S-" tg)
  14202. (add-to-list 'buffer-tags (list tg))
  14203. (if (member tg current)
  14204. (setq current (delete tg current))
  14205. (push tg current)))
  14206. (if exit-after-next (setq exit-after-next 'now)))
  14207. ((setq e (rassoc c todo-table) tg (car e))
  14208. (with-current-buffer buf
  14209. (save-excursion (org-todo tg)))
  14210. (if exit-after-next (setq exit-after-next 'now)))
  14211. ((setq e (rassoc c ntable) tg (car e))
  14212. (if (member tg current)
  14213. (setq current (delete tg current))
  14214. (loop for g in groups do
  14215. (if (member tg g)
  14216. (mapc (lambda (x)
  14217. (setq current (delete x current)))
  14218. g)))
  14219. (push tg current))
  14220. (if exit-after-next (setq exit-after-next 'now))))
  14221. ;; Create a sorted list
  14222. (setq current
  14223. (sort current
  14224. (lambda (a b)
  14225. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14226. (if (eq exit-after-next 'now) (throw 'exit t))
  14227. (goto-char (point-min))
  14228. (beginning-of-line 2)
  14229. (delete-region (point) (point-at-eol))
  14230. (org-fast-tag-insert "Current" current c-face)
  14231. (org-set-current-tags-overlay current ov-prefix)
  14232. (while (re-search-forward
  14233. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14234. (setq tg (match-string 1))
  14235. (add-text-properties
  14236. (match-beginning 1) (match-end 1)
  14237. (list 'face
  14238. (cond
  14239. ((member tg current) c-face)
  14240. ((member tg inherited) i-face)
  14241. (t (get-text-property (match-beginning 1) 'face))))))
  14242. (goto-char (point-min)))))
  14243. (org-detach-overlay org-tags-overlay)
  14244. (if rtn
  14245. (mapconcat 'identity current ":")
  14246. nil))))
  14247. (defun org-get-tags-string ()
  14248. "Get the TAGS string in the current headline."
  14249. (unless (org-on-heading-p t)
  14250. (error "Not on a heading"))
  14251. (save-excursion
  14252. (beginning-of-line 1)
  14253. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14254. (org-match-string-no-properties 1)
  14255. "")))
  14256. (defun org-get-tags ()
  14257. "Get the list of tags specified in the current headline."
  14258. (org-split-string (org-get-tags-string) ":"))
  14259. (defun org-get-buffer-tags ()
  14260. "Get a table of all tags used in the buffer, for completion."
  14261. (let (tags)
  14262. (save-excursion
  14263. (goto-char (point-min))
  14264. (while (re-search-forward
  14265. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14266. (when (equal (char-after (point-at-bol 0)) ?*)
  14267. (mapc (lambda (x) (add-to-list 'tags x))
  14268. (org-split-string (org-match-string-no-properties 1) ":")))))
  14269. (mapcar 'list tags)))
  14270. ;;;; Properties
  14271. ;;; Setting and retrieving properties
  14272. (defconst org-special-properties
  14273. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14274. "TIMESTAMP" "TIMESTAMP_IA")
  14275. "The special properties valid in Org-mode.
  14276. These are properties that are not defined in the property drawer,
  14277. but in some other way.")
  14278. (defconst org-default-properties
  14279. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14280. "LOCATION" "LOGGING" "COLUMNS")
  14281. "Some properties that are used by Org-mode for various purposes.
  14282. Being in this list makes sure that they are offered for completion.")
  14283. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14284. "Regular expression matching the first line of a property drawer.")
  14285. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14286. "Regular expression matching the first line of a property drawer.")
  14287. (defun org-property-action ()
  14288. "Do an action on properties."
  14289. (interactive)
  14290. (let (c)
  14291. (org-at-property-p)
  14292. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14293. (setq c (read-char-exclusive))
  14294. (cond
  14295. ((equal c ?s)
  14296. (call-interactively 'org-set-property))
  14297. ((equal c ?d)
  14298. (call-interactively 'org-delete-property))
  14299. ((equal c ?D)
  14300. (call-interactively 'org-delete-property-globally))
  14301. ((equal c ?c)
  14302. (call-interactively 'org-compute-property-at-point))
  14303. (t (error "No such property action %c" c)))))
  14304. (defun org-at-property-p ()
  14305. "Is the cursor in a property line?"
  14306. ;; FIXME: Does not check if we are actually in the drawer.
  14307. ;; FIXME: also returns true on any drawers.....
  14308. ;; This is used by C-c C-c for property action.
  14309. (save-excursion
  14310. (beginning-of-line 1)
  14311. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14312. (defmacro org-with-point-at (pom &rest body)
  14313. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14314. (declare (indent 1) (debug t))
  14315. `(save-excursion
  14316. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14317. (save-excursion
  14318. (goto-char (or pom (point)))
  14319. ,@body)))
  14320. (defun org-get-property-block (&optional beg end force)
  14321. "Return the (beg . end) range of the body of the property drawer.
  14322. BEG and END can be beginning and end of subtree, if not given
  14323. they will be found.
  14324. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14325. (catch 'exit
  14326. (save-excursion
  14327. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14328. (end (or end (progn (outline-next-heading) (point)))))
  14329. (goto-char beg)
  14330. (if (re-search-forward org-property-start-re end t)
  14331. (setq beg (1+ (match-end 0)))
  14332. (if force
  14333. (save-excursion
  14334. (org-insert-property-drawer)
  14335. (setq end (progn (outline-next-heading) (point))))
  14336. (throw 'exit nil))
  14337. (goto-char beg)
  14338. (if (re-search-forward org-property-start-re end t)
  14339. (setq beg (1+ (match-end 0)))))
  14340. (if (re-search-forward org-property-end-re end t)
  14341. (setq end (match-beginning 0))
  14342. (or force (throw 'exit nil))
  14343. (goto-char beg)
  14344. (setq end beg)
  14345. (org-indent-line-function)
  14346. (insert ":END:\n"))
  14347. (cons beg end)))))
  14348. (defun org-entry-properties (&optional pom which)
  14349. "Get all properties of the entry at point-or-marker POM.
  14350. This includes the TODO keyword, the tags, time strings for deadline,
  14351. scheduled, and clocking, and any additional properties defined in the
  14352. entry. The return value is an alist, keys may occur multiple times
  14353. if the property key was used several times.
  14354. POM may also be nil, in which case the current entry is used.
  14355. If WHICH is nil or `all', get all properties. If WHICH is
  14356. `special' or `standard', only get that subclass."
  14357. (setq which (or which 'all))
  14358. (org-with-point-at pom
  14359. (let ((clockstr (substring org-clock-string 0 -1))
  14360. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14361. beg end range props sum-props key value string)
  14362. (save-excursion
  14363. (when (condition-case nil (org-back-to-heading t) (error nil))
  14364. (setq beg (point))
  14365. (setq sum-props (get-text-property (point) 'org-summaries))
  14366. (outline-next-heading)
  14367. (setq end (point))
  14368. (when (memq which '(all special))
  14369. ;; Get the special properties, like TODO and tags
  14370. (goto-char beg)
  14371. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14372. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14373. (when (looking-at org-priority-regexp)
  14374. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14375. (when (and (setq value (org-get-tags-string))
  14376. (string-match "\\S-" value))
  14377. (push (cons "TAGS" value) props))
  14378. (when (setq value (org-get-tags-at))
  14379. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14380. props))
  14381. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14382. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14383. string (if (equal key clockstr)
  14384. (org-no-properties
  14385. (org-trim
  14386. (buffer-substring
  14387. (match-beginning 3) (goto-char (point-at-eol)))))
  14388. (substring (org-match-string-no-properties 3) 1 -1)))
  14389. (unless key
  14390. (if (= (char-after (match-beginning 3)) ?\[)
  14391. (setq key "TIMESTAMP_IA")
  14392. (setq key "TIMESTAMP")))
  14393. (when (or (equal key clockstr) (not (assoc key props)))
  14394. (push (cons key string) props)))
  14395. )
  14396. (when (memq which '(all standard))
  14397. ;; Get the standard properties, like :PORP: ...
  14398. (setq range (org-get-property-block beg end))
  14399. (when range
  14400. (goto-char (car range))
  14401. (while (re-search-forward
  14402. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14403. (cdr range) t)
  14404. (setq key (org-match-string-no-properties 1)
  14405. value (org-trim (or (org-match-string-no-properties 2) "")))
  14406. (unless (member key excluded)
  14407. (push (cons key (or value "")) props)))))
  14408. (append sum-props (nreverse props)))))))
  14409. (defun org-entry-get (pom property &optional inherit)
  14410. "Get value of PROPERTY for entry at point-or-marker POM.
  14411. If INHERIT is non-nil and the entry does not have the property,
  14412. then also check higher levels of the hierarchy.
  14413. If the property is present but empty, the return value is the empty string.
  14414. If the property is not present at all, nil is returned."
  14415. (org-with-point-at pom
  14416. (if inherit
  14417. (org-entry-get-with-inheritance property)
  14418. (if (member property org-special-properties)
  14419. ;; We need a special property. Use brute force, get all properties.
  14420. (cdr (assoc property (org-entry-properties nil 'special)))
  14421. (let ((range (org-get-property-block)))
  14422. (if (and range
  14423. (goto-char (car range))
  14424. (re-search-forward
  14425. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14426. (cdr range) t))
  14427. ;; Found the property, return it.
  14428. (if (match-end 1)
  14429. (org-match-string-no-properties 1)
  14430. "")))))))
  14431. (defun org-entry-delete (pom property)
  14432. "Delete the property PROPERTY from entry at point-or-marker POM."
  14433. (org-with-point-at pom
  14434. (if (member property org-special-properties)
  14435. nil ; cannot delete these properties.
  14436. (let ((range (org-get-property-block)))
  14437. (if (and range
  14438. (goto-char (car range))
  14439. (re-search-forward
  14440. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14441. (cdr range) t))
  14442. (progn
  14443. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14444. t)
  14445. nil)))))
  14446. ;; Multi-values properties are properties that contain multiple values
  14447. ;; These values are assumed to be single words, separated by whitespace.
  14448. (defun org-entry-add-to-multivalued-property (pom property value)
  14449. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14450. (let* ((old (org-entry-get pom property))
  14451. (values (and old (org-split-string old "[ \t]"))))
  14452. (unless (member value values)
  14453. (setq values (cons value values))
  14454. (org-entry-put pom property
  14455. (mapconcat 'identity values " ")))))
  14456. (defun org-entry-remove-from-multivalued-property (pom property value)
  14457. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14458. (let* ((old (org-entry-get pom property))
  14459. (values (and old (org-split-string old "[ \t]"))))
  14460. (when (member value values)
  14461. (setq values (delete value values))
  14462. (org-entry-put pom property
  14463. (mapconcat 'identity values " ")))))
  14464. (defun org-entry-member-in-multivalued-property (pom property value)
  14465. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14466. (let* ((old (org-entry-get pom property))
  14467. (values (and old (org-split-string old "[ \t]"))))
  14468. (member value values)))
  14469. (defvar org-entry-property-inherited-from (make-marker))
  14470. (defun org-entry-get-with-inheritance (property)
  14471. "Get entry property, and search higher levels if not present."
  14472. (let (tmp)
  14473. (save-excursion
  14474. (save-restriction
  14475. (widen)
  14476. (catch 'ex
  14477. (while t
  14478. (when (setq tmp (org-entry-get nil property))
  14479. (org-back-to-heading t)
  14480. (move-marker org-entry-property-inherited-from (point))
  14481. (throw 'ex tmp))
  14482. (or (org-up-heading-safe) (throw 'ex nil)))))
  14483. (or tmp (cdr (assoc property org-local-properties))
  14484. (cdr (assoc property org-global-properties))))))
  14485. (defun org-entry-put (pom property value)
  14486. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14487. (org-with-point-at pom
  14488. (org-back-to-heading t)
  14489. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14490. range)
  14491. (cond
  14492. ((equal property "TODO")
  14493. (when (and (stringp value) (string-match "\\S-" value)
  14494. (not (member value org-todo-keywords-1)))
  14495. (error "\"%s\" is not a valid TODO state" value))
  14496. (if (or (not value)
  14497. (not (string-match "\\S-" value)))
  14498. (setq value 'none))
  14499. (org-todo value)
  14500. (org-set-tags nil 'align))
  14501. ((equal property "PRIORITY")
  14502. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14503. (string-to-char value) ?\ ))
  14504. (org-set-tags nil 'align))
  14505. ((equal property "SCHEDULED")
  14506. (if (re-search-forward org-scheduled-time-regexp end t)
  14507. (cond
  14508. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14509. ((eq value 'later) (org-timestamp-change 1 'day))
  14510. (t (call-interactively 'org-schedule)))
  14511. (call-interactively 'org-schedule)))
  14512. ((equal property "DEADLINE")
  14513. (if (re-search-forward org-deadline-time-regexp end t)
  14514. (cond
  14515. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14516. ((eq value 'later) (org-timestamp-change 1 'day))
  14517. (t (call-interactively 'org-deadline)))
  14518. (call-interactively 'org-deadline)))
  14519. ((member property org-special-properties)
  14520. (error "The %s property can not yet be set with `org-entry-put'"
  14521. property))
  14522. (t ; a non-special property
  14523. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14524. (setq range (org-get-property-block beg end 'force))
  14525. (goto-char (car range))
  14526. (if (re-search-forward
  14527. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14528. (progn
  14529. (delete-region (match-beginning 1) (match-end 1))
  14530. (goto-char (match-beginning 1)))
  14531. (goto-char (cdr range))
  14532. (insert "\n")
  14533. (backward-char 1)
  14534. (org-indent-line-function)
  14535. (insert ":" property ":"))
  14536. (and value (insert " " value))
  14537. (org-indent-line-function)))))))
  14538. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  14539. "Get all property keys in the current buffer.
  14540. With INCLUDE-SPECIALS, also list the special properties that relect things
  14541. like tags and TODO state.
  14542. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14543. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  14544. (let (rtn range)
  14545. (save-excursion
  14546. (save-restriction
  14547. (widen)
  14548. (goto-char (point-min))
  14549. (while (re-search-forward org-property-start-re nil t)
  14550. (setq range (org-get-property-block))
  14551. (goto-char (car range))
  14552. (while (re-search-forward
  14553. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  14554. (cdr range) t)
  14555. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14556. (outline-next-heading))))
  14557. (when include-specials
  14558. (setq rtn (append org-special-properties rtn)))
  14559. (when include-defaults
  14560. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14561. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14562. (defun org-property-values (key)
  14563. "Return a list of all values of property KEY."
  14564. (save-excursion
  14565. (save-restriction
  14566. (widen)
  14567. (goto-char (point-min))
  14568. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14569. values)
  14570. (while (re-search-forward re nil t)
  14571. (add-to-list 'values (org-trim (match-string 1))))
  14572. (delete "" values)))))
  14573. (defun org-insert-property-drawer ()
  14574. "Insert a property drawer into the current entry."
  14575. (interactive)
  14576. (org-back-to-heading t)
  14577. (looking-at outline-regexp)
  14578. (let ((indent (- (match-end 0)(match-beginning 0)))
  14579. (beg (point))
  14580. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14581. end hiddenp)
  14582. (outline-next-heading)
  14583. (setq end (point))
  14584. (goto-char beg)
  14585. (while (re-search-forward re end t))
  14586. (setq hiddenp (org-invisible-p))
  14587. (end-of-line 1)
  14588. (and (equal (char-after) ?\n) (forward-char 1))
  14589. (org-skip-over-state-notes)
  14590. (skip-chars-backward " \t\n\r")
  14591. (if (eq (char-before) ?*) (forward-char 1))
  14592. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14593. (beginning-of-line 0)
  14594. (indent-to-column indent)
  14595. (beginning-of-line 2)
  14596. (indent-to-column indent)
  14597. (beginning-of-line 0)
  14598. (if hiddenp
  14599. (save-excursion
  14600. (org-back-to-heading t)
  14601. (hide-entry))
  14602. (org-flag-drawer t))))
  14603. (defun org-set-property (property value)
  14604. "In the current entry, set PROPERTY to VALUE.
  14605. When called interactively, this will prompt for a property name, offering
  14606. completion on existing and default properties. And then it will prompt
  14607. for a value, offering competion either on allowed values (via an inherited
  14608. xxx_ALL property) or on existing values in other instances of this property
  14609. in the current file."
  14610. (interactive
  14611. (let* ((prop (completing-read
  14612. "Property: " (mapcar 'list (org-buffer-property-keys nil t))))
  14613. (cur (org-entry-get nil prop))
  14614. (allowed (org-property-get-allowed-values nil prop 'table))
  14615. (existing (mapcar 'list (org-property-values prop)))
  14616. (val (if allowed
  14617. (completing-read "Value: " allowed nil 'req-match)
  14618. (completing-read
  14619. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14620. (concat "[" cur "]") "")
  14621. ": ")
  14622. existing nil nil "" nil cur))))
  14623. (list prop (if (equal val "") cur val))))
  14624. (unless (equal (org-entry-get nil property) value)
  14625. (org-entry-put nil property value)))
  14626. (defun org-delete-property (property)
  14627. "In the current entry, delete PROPERTY."
  14628. (interactive
  14629. (let* ((prop (completing-read
  14630. "Property: " (org-entry-properties nil 'standard))))
  14631. (list prop)))
  14632. (message "Property %s %s" property
  14633. (if (org-entry-delete nil property)
  14634. "deleted"
  14635. "was not present in the entry")))
  14636. (defun org-delete-property-globally (property)
  14637. "Remove PROPERTY globally, from all entries."
  14638. (interactive
  14639. (let* ((prop (completing-read
  14640. "Globally remove property: "
  14641. (mapcar 'list (org-buffer-property-keys)))))
  14642. (list prop)))
  14643. (save-excursion
  14644. (save-restriction
  14645. (widen)
  14646. (goto-char (point-min))
  14647. (let ((cnt 0))
  14648. (while (re-search-forward
  14649. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14650. nil t)
  14651. (setq cnt (1+ cnt))
  14652. (replace-match ""))
  14653. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14654. (defvar org-columns-current-fmt-compiled) ; defined below
  14655. (defun org-compute-property-at-point ()
  14656. "Compute the property at point.
  14657. This looks for an enclosing column format, extracts the operator and
  14658. then applies it to the proerty in the column format's scope."
  14659. (interactive)
  14660. (unless (org-at-property-p)
  14661. (error "Not at a property"))
  14662. (let ((prop (org-match-string-no-properties 2)))
  14663. (org-columns-get-format-and-top-level)
  14664. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14665. (error "No operator defined for property %s" prop))
  14666. (org-columns-compute prop)))
  14667. (defun org-property-get-allowed-values (pom property &optional table)
  14668. "Get allowed values for the property PROPERTY.
  14669. When TABLE is non-nil, return an alist that can directly be used for
  14670. completion."
  14671. (let (vals)
  14672. (cond
  14673. ((equal property "TODO")
  14674. (setq vals (org-with-point-at pom
  14675. (append org-todo-keywords-1 '("")))))
  14676. ((equal property "PRIORITY")
  14677. (let ((n org-lowest-priority))
  14678. (while (>= n org-highest-priority)
  14679. (push (char-to-string n) vals)
  14680. (setq n (1- n)))))
  14681. ((member property org-special-properties))
  14682. (t
  14683. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14684. (when (and vals (string-match "\\S-" vals))
  14685. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14686. (setq vals (mapcar (lambda (x)
  14687. (cond ((stringp x) x)
  14688. ((numberp x) (number-to-string x))
  14689. ((symbolp x) (symbol-name x))
  14690. (t "???")))
  14691. vals)))))
  14692. (if table (mapcar 'list vals) vals)))
  14693. (defun org-property-previous-allowed-value (&optional previous)
  14694. "Switch to the next allowed value for this property."
  14695. (interactive)
  14696. (org-property-next-allowed-value t))
  14697. (defun org-property-next-allowed-value (&optional previous)
  14698. "Switch to the next allowed value for this property."
  14699. (interactive)
  14700. (unless (org-at-property-p)
  14701. (error "Not at a property"))
  14702. (let* ((key (match-string 2))
  14703. (value (match-string 3))
  14704. (allowed (or (org-property-get-allowed-values (point) key)
  14705. (and (member value '("[ ]" "[-]" "[X]"))
  14706. '("[ ]" "[X]"))))
  14707. nval)
  14708. (unless allowed
  14709. (error "Allowed values for this property have not been defined"))
  14710. (if previous (setq allowed (reverse allowed)))
  14711. (if (member value allowed)
  14712. (setq nval (car (cdr (member value allowed)))))
  14713. (setq nval (or nval (car allowed)))
  14714. (if (equal nval value)
  14715. (error "Only one allowed value for this property"))
  14716. (org-at-property-p)
  14717. (replace-match (concat " :" key ": " nval) t t)
  14718. (org-indent-line-function)
  14719. (beginning-of-line 1)
  14720. (skip-chars-forward " \t")))
  14721. (defun org-find-entry-with-id (ident)
  14722. "Locate the entry that contains the ID property with exact value IDENT.
  14723. IDENT can be a string, a symbol or a number, this function will search for
  14724. the string representation of it.
  14725. Return the position where this entry starts, or nil if there is no such entry."
  14726. (let ((id (cond
  14727. ((stringp ident) ident)
  14728. ((symbol-name ident) (symbol-name ident))
  14729. ((numberp ident) (number-to-string ident))
  14730. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14731. (case-fold-search nil))
  14732. (save-excursion
  14733. (save-restriction
  14734. (goto-char (point-min))
  14735. (when (re-search-forward
  14736. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14737. nil t)
  14738. (org-back-to-heading)
  14739. (point))))))
  14740. ;;; Column View
  14741. (defvar org-columns-overlays nil
  14742. "Holds the list of current column overlays.")
  14743. (defvar org-columns-current-fmt nil
  14744. "Local variable, holds the currently active column format.")
  14745. (defvar org-columns-current-fmt-compiled nil
  14746. "Local variable, holds the currently active column format.
  14747. This is the compiled version of the format.")
  14748. (defvar org-columns-current-widths nil
  14749. "Loval variable, holds the currently widths of fields.")
  14750. (defvar org-columns-current-maxwidths nil
  14751. "Loval variable, holds the currently active maximum column widths.")
  14752. (defvar org-columns-begin-marker (make-marker)
  14753. "Points to the position where last a column creation command was called.")
  14754. (defvar org-columns-top-level-marker (make-marker)
  14755. "Points to the position where current columns region starts.")
  14756. (defvar org-columns-map (make-sparse-keymap)
  14757. "The keymap valid in column display.")
  14758. (defun org-columns-content ()
  14759. "Switch to contents view while in columns view."
  14760. (interactive)
  14761. (org-overview)
  14762. (org-content))
  14763. (org-defkey org-columns-map "c" 'org-columns-content)
  14764. (org-defkey org-columns-map "o" 'org-overview)
  14765. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  14766. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  14767. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  14768. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  14769. (org-defkey org-columns-map "v" 'org-columns-show-value)
  14770. (org-defkey org-columns-map "q" 'org-columns-quit)
  14771. (org-defkey org-columns-map "r" 'org-columns-redo)
  14772. (org-defkey org-columns-map [left] 'backward-char)
  14773. (org-defkey org-columns-map "\M-b" 'backward-char)
  14774. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  14775. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  14776. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  14777. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  14778. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  14779. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  14780. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  14781. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  14782. (org-defkey org-columns-map "<" 'org-columns-narrow)
  14783. (org-defkey org-columns-map ">" 'org-columns-widen)
  14784. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  14785. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  14786. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  14787. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  14788. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  14789. '("Column"
  14790. ["Edit property" org-columns-edit-value t]
  14791. ["Next allowed value" org-columns-next-allowed-value t]
  14792. ["Previous allowed value" org-columns-previous-allowed-value t]
  14793. ["Show full value" org-columns-show-value t]
  14794. ["Edit allowed values" org-columns-edit-allowed t]
  14795. "--"
  14796. ["Edit column attributes" org-columns-edit-attributes t]
  14797. ["Increase column width" org-columns-widen t]
  14798. ["Decrease column width" org-columns-narrow t]
  14799. "--"
  14800. ["Move column right" org-columns-move-right t]
  14801. ["Move column left" org-columns-move-left t]
  14802. ["Add column" org-columns-new t]
  14803. ["Delete column" org-columns-delete t]
  14804. "--"
  14805. ["CONTENTS" org-columns-content t]
  14806. ["OVERVIEW" org-overview t]
  14807. ["Refresh columns display" org-columns-redo t]
  14808. "--"
  14809. ["Open link" org-columns-open-link t]
  14810. "--"
  14811. ["Quit" org-columns-quit t]))
  14812. (defun org-columns-new-overlay (beg end &optional string face)
  14813. "Create a new column overlay and add it to the list."
  14814. (let ((ov (org-make-overlay beg end)))
  14815. (org-overlay-put ov 'face (or face 'secondary-selection))
  14816. (org-overlay-display ov string face)
  14817. (push ov org-columns-overlays)
  14818. ov))
  14819. (defun org-columns-display-here (&optional props)
  14820. "Overlay the current line with column display."
  14821. (interactive)
  14822. (let* ((fmt org-columns-current-fmt-compiled)
  14823. (beg (point-at-bol))
  14824. (level-face (save-excursion
  14825. (beginning-of-line 1)
  14826. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  14827. (org-get-level-face 2))))
  14828. (color (list :foreground
  14829. (face-attribute (or level-face 'default) :foreground)))
  14830. props pom property ass width f string ov column val modval)
  14831. ;; Check if the entry is in another buffer.
  14832. (unless props
  14833. (if (eq major-mode 'org-agenda-mode)
  14834. (setq pom (or (get-text-property (point) 'org-hd-marker)
  14835. (get-text-property (point) 'org-marker))
  14836. props (if pom (org-entry-properties pom) nil))
  14837. (setq props (org-entry-properties nil))))
  14838. ;; Walk the format
  14839. (while (setq column (pop fmt))
  14840. (setq property (car column)
  14841. ass (if (equal property "ITEM")
  14842. (cons "ITEM"
  14843. (save-match-data
  14844. (org-no-properties
  14845. (org-remove-tabs
  14846. (buffer-substring-no-properties
  14847. (point-at-bol) (point-at-eol))))))
  14848. (assoc property props))
  14849. width (or (cdr (assoc property org-columns-current-maxwidths))
  14850. (nth 2 column)
  14851. (length property))
  14852. f (format "%%-%d.%ds | " width width)
  14853. val (or (cdr ass) "")
  14854. modval (if (equal property "ITEM")
  14855. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  14856. string (format f (or modval val)))
  14857. ;; Create the overlay
  14858. (org-unmodified
  14859. (setq ov (org-columns-new-overlay
  14860. beg (setq beg (1+ beg)) string
  14861. (list color 'org-column)))
  14862. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  14863. (org-overlay-put ov 'keymap org-columns-map)
  14864. (org-overlay-put ov 'org-columns-key property)
  14865. (org-overlay-put ov 'org-columns-value (cdr ass))
  14866. (org-overlay-put ov 'org-columns-value-modified modval)
  14867. (org-overlay-put ov 'org-columns-pom pom)
  14868. (org-overlay-put ov 'org-columns-format f))
  14869. (if (or (not (char-after beg))
  14870. (equal (char-after beg) ?\n))
  14871. (let ((inhibit-read-only t))
  14872. (save-excursion
  14873. (goto-char beg)
  14874. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  14875. ;; Make the rest of the line disappear.
  14876. (org-unmodified
  14877. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  14878. (org-overlay-put ov 'invisible t)
  14879. (org-overlay-put ov 'keymap org-columns-map)
  14880. (org-overlay-put ov 'intangible t)
  14881. (push ov org-columns-overlays)
  14882. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  14883. (org-overlay-put ov 'keymap org-columns-map)
  14884. (push ov org-columns-overlays)
  14885. (let ((inhibit-read-only t))
  14886. (put-text-property (max (point-min) (1- (point-at-bol)))
  14887. (min (point-max) (1+ (point-at-eol)))
  14888. 'read-only "Type `e' to edit property")))))
  14889. (defvar org-previous-header-line-format nil
  14890. "The header line format before column view was turned on.")
  14891. (defvar org-columns-inhibit-recalculation nil
  14892. "Inhibit recomputing of columns on column view startup.")
  14893. (defvar header-line-format)
  14894. (defun org-columns-display-here-title ()
  14895. "Overlay the newline before the current line with the table title."
  14896. (interactive)
  14897. (let ((fmt org-columns-current-fmt-compiled)
  14898. string (title "")
  14899. property width f column str widths)
  14900. (while (setq column (pop fmt))
  14901. (setq property (car column)
  14902. str (or (nth 1 column) property)
  14903. width (or (cdr (assoc property org-columns-current-maxwidths))
  14904. (nth 2 column)
  14905. (length str))
  14906. widths (push width widths)
  14907. f (format "%%-%d.%ds | " width width)
  14908. string (format f str)
  14909. title (concat title string)))
  14910. (setq title (concat
  14911. (org-add-props " " nil 'display '(space :align-to 0))
  14912. (org-add-props title nil 'face '(:weight bold :underline t))))
  14913. (org-set-local 'org-previous-header-line-format header-line-format)
  14914. (org-set-local 'org-columns-current-widths (nreverse widths))
  14915. (setq header-line-format title)))
  14916. (defun org-columns-remove-overlays ()
  14917. "Remove all currently active column overlays."
  14918. (interactive)
  14919. (when (marker-buffer org-columns-begin-marker)
  14920. (with-current-buffer (marker-buffer org-columns-begin-marker)
  14921. (when (local-variable-p 'org-previous-header-line-format)
  14922. (setq header-line-format org-previous-header-line-format)
  14923. (kill-local-variable 'org-previous-header-line-format))
  14924. (move-marker org-columns-begin-marker nil)
  14925. (move-marker org-columns-top-level-marker nil)
  14926. (org-unmodified
  14927. (mapc 'org-delete-overlay org-columns-overlays)
  14928. (setq org-columns-overlays nil)
  14929. (let ((inhibit-read-only t))
  14930. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  14931. (defun org-columns-cleanup-item (item fmt)
  14932. "Remove from ITEM what is a column in the format FMT."
  14933. (if (not org-complex-heading-regexp)
  14934. item
  14935. (when (string-match org-complex-heading-regexp item)
  14936. (concat
  14937. (org-add-props (concat (match-string 1 item) " ") nil
  14938. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  14939. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  14940. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  14941. " " (match-string 4 item)
  14942. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  14943. (defun org-columns-show-value ()
  14944. "Show the full value of the property."
  14945. (interactive)
  14946. (let ((value (get-char-property (point) 'org-columns-value)))
  14947. (message "Value is: %s" (or value ""))))
  14948. (defun org-columns-quit ()
  14949. "Remove the column overlays and in this way exit column editing."
  14950. (interactive)
  14951. (org-unmodified
  14952. (org-columns-remove-overlays)
  14953. (let ((inhibit-read-only t))
  14954. (remove-text-properties (point-min) (point-max) '(read-only t))))
  14955. (when (eq major-mode 'org-agenda-mode)
  14956. (message
  14957. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  14958. (defun org-columns-check-computed ()
  14959. "Check if this column value is computed.
  14960. If yes, throw an error indicating that changing it does not make sense."
  14961. (let ((val (get-char-property (point) 'org-columns-value)))
  14962. (when (and (stringp val)
  14963. (get-char-property 0 'org-computed val))
  14964. (error "This value is computed from the entry's children"))))
  14965. (defun org-columns-todo (&optional arg)
  14966. "Change the TODO state during column view."
  14967. (interactive "P")
  14968. (org-columns-edit-value "TODO"))
  14969. (defun org-columns-set-tags-or-toggle (&optional arg)
  14970. "Toggle checkbox at point, or set tags for current headline."
  14971. (interactive "P")
  14972. (if (string-match "\\`\\[[ xX-]\\]\\'"
  14973. (get-char-property (point) 'org-columns-value))
  14974. (org-columns-next-allowed-value)
  14975. (org-columns-edit-value "TAGS")))
  14976. (defun org-columns-edit-value (&optional key)
  14977. "Edit the value of the property at point in column view.
  14978. Where possible, use the standard interface for changing this line."
  14979. (interactive)
  14980. (org-columns-check-computed)
  14981. (let* ((external-key key)
  14982. (col (current-column))
  14983. (key (or key (get-char-property (point) 'org-columns-key)))
  14984. (value (get-char-property (point) 'org-columns-value))
  14985. (bol (point-at-bol)) (eol (point-at-eol))
  14986. (pom (or (get-text-property bol 'org-hd-marker)
  14987. (point))) ; keep despite of compiler waring
  14988. (line-overlays
  14989. (delq nil (mapcar (lambda (x)
  14990. (and (eq (overlay-buffer x) (current-buffer))
  14991. (>= (overlay-start x) bol)
  14992. (<= (overlay-start x) eol)
  14993. x))
  14994. org-columns-overlays)))
  14995. nval eval allowed)
  14996. (cond
  14997. ((equal key "ITEM")
  14998. (setq eval '(org-with-point-at pom
  14999. (org-edit-headline))))
  15000. ((equal key "TODO")
  15001. (setq eval '(org-with-point-at pom
  15002. (let ((current-prefix-arg
  15003. (if external-key current-prefix-arg '(4))))
  15004. (call-interactively 'org-todo)))))
  15005. ((equal key "PRIORITY")
  15006. (setq eval '(org-with-point-at pom
  15007. (call-interactively 'org-priority))))
  15008. ((equal key "TAGS")
  15009. (setq eval '(org-with-point-at pom
  15010. (let ((org-fast-tag-selection-single-key
  15011. (if (eq org-fast-tag-selection-single-key 'expert)
  15012. t org-fast-tag-selection-single-key)))
  15013. (call-interactively 'org-set-tags)))))
  15014. ((equal key "DEADLINE")
  15015. (setq eval '(org-with-point-at pom
  15016. (call-interactively 'org-deadline))))
  15017. ((equal key "SCHEDULED")
  15018. (setq eval '(org-with-point-at pom
  15019. (call-interactively 'org-schedule))))
  15020. (t
  15021. (setq allowed (org-property-get-allowed-values pom key 'table))
  15022. (if allowed
  15023. (setq nval (completing-read "Value: " allowed nil t))
  15024. (setq nval (read-string "Edit: " value)))
  15025. (setq nval (org-trim nval))
  15026. (when (not (equal nval value))
  15027. (setq eval '(org-entry-put pom key nval)))))
  15028. (when eval
  15029. (let ((inhibit-read-only t))
  15030. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15031. (unwind-protect
  15032. (progn
  15033. (setq org-columns-overlays
  15034. (org-delete-all line-overlays org-columns-overlays))
  15035. (mapc 'org-delete-overlay line-overlays)
  15036. (org-columns-eval eval))
  15037. (org-columns-display-here))))
  15038. (move-to-column col)
  15039. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  15040. (org-columns-update key))))
  15041. (defun org-edit-headline () ; FIXME: this is not columns specific
  15042. "Edit the current headline, the part without TODO keyword, TAGS."
  15043. (org-back-to-heading)
  15044. (when (looking-at org-todo-line-regexp)
  15045. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15046. (txt (match-string 3))
  15047. (post "")
  15048. txt2)
  15049. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15050. (setq post (match-string 0 txt)
  15051. txt (substring txt 0 (match-beginning 0))))
  15052. (setq txt2 (read-string "Edit: " txt))
  15053. (when (not (equal txt txt2))
  15054. (beginning-of-line 1)
  15055. (insert pre txt2 post)
  15056. (delete-region (point) (point-at-eol))
  15057. (org-set-tags nil t)))))
  15058. (defun org-columns-edit-allowed ()
  15059. "Edit the list of allowed values for the current property."
  15060. (interactive)
  15061. (let* ((key (get-char-property (point) 'org-columns-key))
  15062. (key1 (concat key "_ALL"))
  15063. (allowed (org-entry-get (point) key1 t))
  15064. nval)
  15065. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15066. (setq nval (read-string "Allowed: " allowed))
  15067. (org-entry-put
  15068. (cond ((marker-position org-entry-property-inherited-from)
  15069. org-entry-property-inherited-from)
  15070. ((marker-position org-columns-top-level-marker)
  15071. org-columns-top-level-marker))
  15072. key1 nval)))
  15073. (defmacro org-no-warnings (&rest body)
  15074. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15075. (defun org-columns-eval (form)
  15076. (let (hidep)
  15077. (save-excursion
  15078. (beginning-of-line 1)
  15079. ;; `next-line' is needed here, because it skips invisible line.
  15080. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15081. (setq hidep (org-on-heading-p 1)))
  15082. (eval form)
  15083. (and hidep (hide-entry))))
  15084. (defun org-columns-previous-allowed-value ()
  15085. "Switch to the previous allowed value for this column."
  15086. (interactive)
  15087. (org-columns-next-allowed-value t))
  15088. (defun org-columns-next-allowed-value (&optional previous)
  15089. "Switch to the next allowed value for this column."
  15090. (interactive)
  15091. (org-columns-check-computed)
  15092. (let* ((col (current-column))
  15093. (key (get-char-property (point) 'org-columns-key))
  15094. (value (get-char-property (point) 'org-columns-value))
  15095. (bol (point-at-bol)) (eol (point-at-eol))
  15096. (pom (or (get-text-property bol 'org-hd-marker)
  15097. (point))) ; keep despite of compiler waring
  15098. (line-overlays
  15099. (delq nil (mapcar (lambda (x)
  15100. (and (eq (overlay-buffer x) (current-buffer))
  15101. (>= (overlay-start x) bol)
  15102. (<= (overlay-start x) eol)
  15103. x))
  15104. org-columns-overlays)))
  15105. (allowed (or (org-property-get-allowed-values pom key)
  15106. (and (equal
  15107. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15108. 'checkbox) '("[ ]" "[X]"))))
  15109. nval)
  15110. (when (equal key "ITEM")
  15111. (error "Cannot edit item headline from here"))
  15112. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15113. (error "Allowed values for this property have not been defined"))
  15114. (if (member key '("SCHEDULED" "DEADLINE"))
  15115. (setq nval (if previous 'earlier 'later))
  15116. (if previous (setq allowed (reverse allowed)))
  15117. (if (member value allowed)
  15118. (setq nval (car (cdr (member value allowed)))))
  15119. (setq nval (or nval (car allowed)))
  15120. (if (equal nval value)
  15121. (error "Only one allowed value for this property")))
  15122. (let ((inhibit-read-only t))
  15123. (remove-text-properties (1- bol) eol '(read-only t))
  15124. (unwind-protect
  15125. (progn
  15126. (setq org-columns-overlays
  15127. (org-delete-all line-overlays org-columns-overlays))
  15128. (mapc 'org-delete-overlay line-overlays)
  15129. (org-columns-eval '(org-entry-put pom key nval)))
  15130. (org-columns-display-here)))
  15131. (move-to-column col)
  15132. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  15133. (org-columns-update key))))
  15134. (defun org-verify-version (task)
  15135. (cond
  15136. ((eq task 'columns)
  15137. (if (or (featurep 'xemacs)
  15138. (< emacs-major-version 22))
  15139. (error "Emacs 22 is required for the columns feature")))))
  15140. (defun org-columns-open-link (&optional arg)
  15141. (interactive "P")
  15142. (let ((key (get-char-property (point) 'org-columns-key))
  15143. (value (get-char-property (point) 'org-columns-value)))
  15144. (org-open-link-from-string arg)))
  15145. (defun org-open-link-from-string (s &optional arg)
  15146. "Open a link in the string S, as if it was in Org-mode."
  15147. (interactive)
  15148. (with-temp-buffer
  15149. (let ((org-inhibit-startup t))
  15150. (org-mode)
  15151. (insert s)
  15152. (goto-char (point-min))
  15153. (org-open-at-point arg))))
  15154. (defun org-columns-get-format-and-top-level ()
  15155. (let (fmt)
  15156. (when (condition-case nil (org-back-to-heading) (error nil))
  15157. (move-marker org-entry-property-inherited-from nil)
  15158. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15159. (setq fmt (or fmt org-columns-default-format))
  15160. (org-set-local 'org-columns-current-fmt fmt)
  15161. (org-columns-compile-format fmt)
  15162. (if (marker-position org-entry-property-inherited-from)
  15163. (move-marker org-columns-top-level-marker
  15164. org-entry-property-inherited-from)
  15165. (move-marker org-columns-top-level-marker (point)))
  15166. fmt))
  15167. (defun org-columns ()
  15168. "Turn on column view on an org-mode file."
  15169. (interactive)
  15170. (org-verify-version 'columns)
  15171. (org-columns-remove-overlays)
  15172. (move-marker org-columns-begin-marker (point))
  15173. (let (beg end fmt cache maxwidths)
  15174. (setq fmt (org-columns-get-format-and-top-level))
  15175. (save-excursion
  15176. (goto-char org-columns-top-level-marker)
  15177. (setq beg (point))
  15178. (unless org-columns-inhibit-recalculation
  15179. (org-columns-compute-all))
  15180. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15181. (point-max)))
  15182. (goto-char beg)
  15183. ;; Get and cache the properties
  15184. (while (re-search-forward (concat "^" outline-regexp) end t)
  15185. (push (cons (org-current-line) (org-entry-properties)) cache))
  15186. (when cache
  15187. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15188. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15189. (org-columns-display-here-title)
  15190. (mapc (lambda (x)
  15191. (goto-line (car x))
  15192. (org-columns-display-here (cdr x)))
  15193. cache)))))
  15194. (defun org-columns-new (&optional prop title width op fmt)
  15195. "Insert a new column, to the leeft o the current column."
  15196. (interactive)
  15197. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15198. cell)
  15199. (setq prop (completing-read
  15200. "Property: " (mapcar 'list (org-buffer-property-keys t))
  15201. nil nil prop))
  15202. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15203. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15204. (if (string-match "\\S-" width)
  15205. (setq width (string-to-number width))
  15206. (setq width nil))
  15207. (setq fmt (completing-read "Summary [none]: "
  15208. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox"))
  15209. nil t))
  15210. (if (string-match "\\S-" fmt)
  15211. (setq fmt (intern fmt))
  15212. (setq fmt nil))
  15213. (if (eq fmt 'none) (setq fmt nil))
  15214. (if editp
  15215. (progn
  15216. (setcar editp prop)
  15217. (setcdr editp (list title width nil fmt)))
  15218. (setq cell (nthcdr (1- (current-column))
  15219. org-columns-current-fmt-compiled))
  15220. (setcdr cell (cons (list prop title width nil fmt)
  15221. (cdr cell))))
  15222. (org-columns-store-format)
  15223. (org-columns-redo)))
  15224. (defun org-columns-delete ()
  15225. "Delete the column at point from columns view."
  15226. (interactive)
  15227. (let* ((n (current-column))
  15228. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15229. (when (y-or-n-p
  15230. (format "Are you sure you want to remove column \"%s\"? " title))
  15231. (setq org-columns-current-fmt-compiled
  15232. (delq (nth n org-columns-current-fmt-compiled)
  15233. org-columns-current-fmt-compiled))
  15234. (org-columns-store-format)
  15235. (org-columns-redo)
  15236. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15237. (backward-char 1)))))
  15238. (defun org-columns-edit-attributes ()
  15239. "Edit the attributes of the current column."
  15240. (interactive)
  15241. (let* ((n (current-column))
  15242. (info (nth n org-columns-current-fmt-compiled)))
  15243. (apply 'org-columns-new info)))
  15244. (defun org-columns-widen (arg)
  15245. "Make the column wider by ARG characters."
  15246. (interactive "p")
  15247. (let* ((n (current-column))
  15248. (entry (nth n org-columns-current-fmt-compiled))
  15249. (width (or (nth 2 entry)
  15250. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15251. (setq width (max 1 (+ width arg)))
  15252. (setcar (nthcdr 2 entry) width)
  15253. (org-columns-store-format)
  15254. (org-columns-redo)))
  15255. (defun org-columns-narrow (arg)
  15256. "Make the column nrrower by ARG characters."
  15257. (interactive "p")
  15258. (org-columns-widen (- arg)))
  15259. (defun org-columns-move-right ()
  15260. "Swap this column with the one to the right."
  15261. (interactive)
  15262. (let* ((n (current-column))
  15263. (cell (nthcdr n org-columns-current-fmt-compiled))
  15264. e)
  15265. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15266. (error "Cannot shift this column further to the right"))
  15267. (setq e (car cell))
  15268. (setcar cell (car (cdr cell)))
  15269. (setcdr cell (cons e (cdr (cdr cell))))
  15270. (org-columns-store-format)
  15271. (org-columns-redo)
  15272. (forward-char 1)))
  15273. (defun org-columns-move-left ()
  15274. "Swap this column with the one to the left."
  15275. (interactive)
  15276. (let* ((n (current-column)))
  15277. (when (= n 0)
  15278. (error "Cannot shift this column further to the left"))
  15279. (backward-char 1)
  15280. (org-columns-move-right)
  15281. (backward-char 1)))
  15282. (defun org-columns-store-format ()
  15283. "Store the text version of the current columns format in appropriate place.
  15284. This is either in the COLUMNS property of the node starting the current column
  15285. display, or in the #+COLUMNS line of the current buffer."
  15286. (let (fmt (cnt 0))
  15287. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15288. (org-set-local 'org-columns-current-fmt fmt)
  15289. (if (marker-position org-columns-top-level-marker)
  15290. (save-excursion
  15291. (goto-char org-columns-top-level-marker)
  15292. (if (and (org-at-heading-p)
  15293. (org-entry-get nil "COLUMNS"))
  15294. (org-entry-put nil "COLUMNS" fmt)
  15295. (goto-char (point-min))
  15296. ;; Overwrite all #+COLUMNS lines....
  15297. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15298. (setq cnt (1+ cnt))
  15299. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15300. (unless (> cnt 0)
  15301. (goto-char (point-min))
  15302. (or (org-on-heading-p t) (outline-next-heading))
  15303. (let ((inhibit-read-only t))
  15304. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15305. (org-set-local 'org-columns-default-format fmt))))))
  15306. (defvar org-overriding-columns-format nil
  15307. "When set, overrides any other definition.")
  15308. (defvar org-agenda-view-columns-initially nil
  15309. "When set, switch to columns view immediately after creating the agenda.")
  15310. (defun org-agenda-columns ()
  15311. "Turn on column view in the agenda."
  15312. (interactive)
  15313. (org-verify-version 'columns)
  15314. (org-columns-remove-overlays)
  15315. (move-marker org-columns-begin-marker (point))
  15316. (let (fmt cache maxwidths m)
  15317. (cond
  15318. ((and (local-variable-p 'org-overriding-columns-format)
  15319. org-overriding-columns-format)
  15320. (setq fmt org-overriding-columns-format))
  15321. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15322. (setq fmt (org-entry-get m "COLUMNS" t)))
  15323. ((and (boundp 'org-columns-current-fmt)
  15324. (local-variable-p 'org-columns-current-fmt)
  15325. org-columns-current-fmt)
  15326. (setq fmt org-columns-current-fmt))
  15327. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15328. (setq m (get-text-property m 'org-hd-marker))
  15329. (setq fmt (org-entry-get m "COLUMNS" t))))
  15330. (setq fmt (or fmt org-columns-default-format))
  15331. (org-set-local 'org-columns-current-fmt fmt)
  15332. (org-columns-compile-format fmt)
  15333. (save-excursion
  15334. ;; Get and cache the properties
  15335. (goto-char (point-min))
  15336. (while (not (eobp))
  15337. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15338. (get-text-property (point) 'org-marker)))
  15339. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15340. (beginning-of-line 2))
  15341. (when cache
  15342. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15343. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15344. (org-columns-display-here-title)
  15345. (mapc (lambda (x)
  15346. (goto-line (car x))
  15347. (org-columns-display-here (cdr x)))
  15348. cache)))))
  15349. (defun org-columns-get-autowidth-alist (s cache)
  15350. "Derive the maximum column widths from the format and the cache."
  15351. (let ((start 0) rtn)
  15352. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  15353. (push (cons (match-string 1 s) 1) rtn)
  15354. (setq start (match-end 0)))
  15355. (mapc (lambda (x)
  15356. (setcdr x (apply 'max
  15357. (mapcar
  15358. (lambda (y)
  15359. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15360. cache))))
  15361. rtn)
  15362. rtn))
  15363. (defun org-columns-compute-all ()
  15364. "Compute all columns that have operators defined."
  15365. (org-unmodified
  15366. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15367. (let ((columns org-columns-current-fmt-compiled) col)
  15368. (while (setq col (pop columns))
  15369. (when (nth 3 col)
  15370. (save-excursion
  15371. (org-columns-compute (car col)))))))
  15372. (defun org-columns-update (property)
  15373. "Recompute PROPERTY, and update the columns display for it."
  15374. (org-columns-compute property)
  15375. (let (fmt val pos)
  15376. (save-excursion
  15377. (mapc (lambda (ov)
  15378. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15379. (setq pos (org-overlay-start ov))
  15380. (goto-char pos)
  15381. (when (setq val (cdr (assoc property
  15382. (get-text-property
  15383. (point-at-bol) 'org-summaries))))
  15384. (setq fmt (org-overlay-get ov 'org-columns-format))
  15385. (org-overlay-put ov 'org-columns-value val)
  15386. (org-overlay-put ov 'display (format fmt val)))))
  15387. org-columns-overlays))))
  15388. (defun org-columns-compute (property)
  15389. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15390. (interactive)
  15391. (let* ((re (concat "^" outline-regexp))
  15392. (lmax 30) ; Does anyone use deeper levels???
  15393. (lsum (make-vector lmax 0))
  15394. (lflag (make-vector lmax nil))
  15395. (level 0)
  15396. (ass (assoc property org-columns-current-fmt-compiled))
  15397. (format (nth 4 ass))
  15398. (printf (nth 5 ass))
  15399. (beg org-columns-top-level-marker)
  15400. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15401. (save-excursion
  15402. ;; Find the region to compute
  15403. (goto-char beg)
  15404. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15405. (goto-char end)
  15406. ;; Walk the tree from the back and do the computations
  15407. (while (re-search-backward re beg t)
  15408. (setq sumpos (match-beginning 0)
  15409. last-level level
  15410. level (org-outline-level)
  15411. val (org-entry-get nil property)
  15412. valflag (and val (string-match "\\S-" val)))
  15413. (cond
  15414. ((< level last-level)
  15415. ;; put the sum of lower levels here as a property
  15416. (setq sum (aref lsum last-level) ; current sum
  15417. flag (aref lflag last-level) ; any valid entries from children?
  15418. str (org-column-number-to-string sum format printf)
  15419. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15420. useval (if flag str1 (if valflag val ""))
  15421. sum-alist (get-text-property sumpos 'org-summaries))
  15422. (if (assoc property sum-alist)
  15423. (setcdr (assoc property sum-alist) useval)
  15424. (push (cons property useval) sum-alist)
  15425. (org-unmodified
  15426. (add-text-properties sumpos (1+ sumpos)
  15427. (list 'org-summaries sum-alist))))
  15428. (when val
  15429. (org-entry-put nil property (if flag str val)))
  15430. ;; add current to current level accumulator
  15431. (when (or flag valflag)
  15432. (aset lsum level (+ (aref lsum level)
  15433. (if flag sum (org-column-string-to-number
  15434. (if flag str val) format))))
  15435. (aset lflag level t))
  15436. ;; clear accumulators for deeper levels
  15437. (loop for l from (1+ level) to (1- lmax) do
  15438. (aset lsum l 0)
  15439. (aset lflag l nil)))
  15440. ((>= level last-level)
  15441. ;; add what we have here to the accumulator for this level
  15442. (aset lsum level (+ (aref lsum level)
  15443. (org-column-string-to-number (or val "0") format)))
  15444. (and valflag (aset lflag level t)))
  15445. (t (error "This should not happen")))))))
  15446. (defun org-columns-redo ()
  15447. "Construct the column display again."
  15448. (interactive)
  15449. (message "Recomputing columns...")
  15450. (save-excursion
  15451. (if (marker-position org-columns-begin-marker)
  15452. (goto-char org-columns-begin-marker))
  15453. (org-columns-remove-overlays)
  15454. (if (org-mode-p)
  15455. (call-interactively 'org-columns)
  15456. (call-interactively 'org-agenda-columns)))
  15457. (message "Recomputing columns...done"))
  15458. (defun org-columns-not-in-agenda ()
  15459. (if (eq major-mode 'org-agenda-mode)
  15460. (error "This command is only allowed in Org-mode buffers")))
  15461. (defun org-string-to-number (s)
  15462. "Convert string to number, and interpret hh:mm:ss."
  15463. (if (not (string-match ":" s))
  15464. (string-to-number s)
  15465. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15466. (while l
  15467. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15468. sum)))
  15469. (defun org-column-number-to-string (n fmt printf)
  15470. "Convert a computed column number to a string value, according to FMT."
  15471. (cond
  15472. ((eq fmt 'add_times)
  15473. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15474. (format "%d:%02d" h m)))
  15475. ((eq fmt 'checkbox)
  15476. (cond ((= n (floor n)) "[X]")
  15477. ((> n 1.) "[-]")
  15478. (t "[ ]")))
  15479. (printf (format printf n))
  15480. ((eq fmt 'currency)
  15481. (format "%.2f" n))
  15482. (t (number-to-string n))))
  15483. (defun org-column-string-to-number (s fmt)
  15484. "Convert a column value to a number that can be used for column computing."
  15485. (cond
  15486. ((string-match ":" s)
  15487. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15488. (while l
  15489. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15490. sum))
  15491. ((eq fmt 'checkbox)
  15492. (if (equal s "[X]") 1. 0.000001))
  15493. (t (string-to-number s))))
  15494. (defun org-columns-uncompile-format (cfmt)
  15495. "Turn the compiled columns format back into a string representation."
  15496. (let ((rtn "") e s prop title op width fmt printf)
  15497. (while (setq e (pop cfmt))
  15498. (setq prop (car e)
  15499. title (nth 1 e)
  15500. width (nth 2 e)
  15501. op (nth 3 e)
  15502. fmt (nth 4 e)
  15503. printf (nth 5 e))
  15504. (cond
  15505. ((eq fmt 'add_times) (setq op ":"))
  15506. ((eq fmt 'checkbox) (setq op "X"))
  15507. ((eq fmt 'add_numbers) (setq op "+"))
  15508. ((eq fmt 'currency) (setq op "$")))
  15509. (if (and op printf) (setq op (concat op ";" printf)))
  15510. (if (equal title prop) (setq title nil))
  15511. (setq s (concat "%" (if width (number-to-string width))
  15512. prop
  15513. (if title (concat "(" title ")"))
  15514. (if op (concat "{" op "}"))))
  15515. (setq rtn (concat rtn " " s)))
  15516. (org-trim rtn)))
  15517. (defun org-columns-compile-format (fmt)
  15518. "Turn a column format string into an alist of specifications.
  15519. The alist has one entry for each column in the format. The elements of
  15520. that list are:
  15521. property the property
  15522. title the title field for the columns
  15523. width the column width in characters, can be nil for automatic
  15524. operator the operator if any
  15525. format the output format for computed results, derived from operator
  15526. printf a printf format for computed values"
  15527. (let ((start 0) width prop title op f printf)
  15528. (setq org-columns-current-fmt-compiled nil)
  15529. (while (string-match
  15530. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15531. fmt start)
  15532. (setq start (match-end 0)
  15533. width (match-string 1 fmt)
  15534. prop (match-string 2 fmt)
  15535. title (or (match-string 3 fmt) prop)
  15536. op (match-string 4 fmt)
  15537. f nil
  15538. printf nil)
  15539. (if width (setq width (string-to-number width)))
  15540. (when (and op (string-match ";" op))
  15541. (setq printf (substring op (match-end 0))
  15542. op (substring op 0 (match-beginning 0))))
  15543. (cond
  15544. ((equal op "+") (setq f 'add_numbers))
  15545. ((equal op "$") (setq f 'currency))
  15546. ((equal op ":") (setq f 'add_times))
  15547. ((equal op "X") (setq f 'checkbox)))
  15548. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15549. (setq org-columns-current-fmt-compiled
  15550. (nreverse org-columns-current-fmt-compiled))))
  15551. ;;; Dynamic block for Column view
  15552. (defun org-columns-capture-view ()
  15553. "Get the column view of the current buffer and return it as a list.
  15554. The list will contains the title row and all other rows. Each row is
  15555. a list of fields."
  15556. (save-excursion
  15557. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15558. (n (length title)) row tbl)
  15559. (goto-char (point-min))
  15560. (while (re-search-forward "^\\*+ " nil t)
  15561. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15562. (setq row nil)
  15563. (loop for i from 0 to (1- n) do
  15564. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15565. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15566. "")
  15567. row))
  15568. (setq row (nreverse row))
  15569. (push row tbl)))
  15570. (append (list title 'hline) (nreverse tbl)))))
  15571. (defun org-dblock-write:columnview (params)
  15572. "Write the column view table.
  15573. PARAMS is a property list of parameters:
  15574. :width enforce same column widths with <N> specifiers.
  15575. :id the :ID: property of the entry where the columns view
  15576. should be built, as a string. When `local', call locally.
  15577. When `global' call column view with the cursor at the beginning
  15578. of the buffer (usually this means that the whole buffer switches
  15579. to column view).
  15580. :hlines When t, insert a hline before each item. When a number, insert
  15581. a hline before each level <= that number.
  15582. :vlines When t, make each column a colgroup to enforce vertical lines."
  15583. (let ((pos (move-marker (make-marker) (point)))
  15584. (hlines (plist-get params :hlines))
  15585. (vlines (plist-get params :vlines))
  15586. tbl id idpos nfields tmp)
  15587. (save-excursion
  15588. (save-restriction
  15589. (when (setq id (plist-get params :id))
  15590. (cond ((not id) nil)
  15591. ((eq id 'global) (goto-char (point-min)))
  15592. ((eq id 'local) nil)
  15593. ((setq idpos (org-find-entry-with-id id))
  15594. (goto-char idpos))
  15595. (t (error "Cannot find entry with :ID: %s" id))))
  15596. (org-columns)
  15597. (setq tbl (org-columns-capture-view))
  15598. (setq nfields (length (car tbl)))
  15599. (org-columns-quit)))
  15600. (goto-char pos)
  15601. (move-marker pos nil)
  15602. (when tbl
  15603. (when (plist-get params :hlines)
  15604. (setq tmp nil)
  15605. (while tbl
  15606. (if (eq (car tbl) 'hline)
  15607. (push (pop tbl) tmp)
  15608. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15609. (if (and (not (eq (car tmp) 'hline))
  15610. (or (eq hlines t)
  15611. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15612. (push 'hline tmp)))
  15613. (push (pop tbl) tmp)))
  15614. (setq tbl (nreverse tmp)))
  15615. (when vlines
  15616. (setq tbl (mapcar (lambda (x)
  15617. (if (eq 'hline x) x (cons "" x)))
  15618. tbl))
  15619. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15620. (setq pos (point))
  15621. (insert (org-listtable-to-string tbl))
  15622. (when (plist-get params :width)
  15623. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15624. org-columns-current-widths "|")))
  15625. (goto-char pos)
  15626. (org-table-align))))
  15627. (defun org-listtable-to-string (tbl)
  15628. "Convert a listtable TBL to a string that contains the Org-mode table.
  15629. The table still need to be alligned. The resulting string has no leading
  15630. and tailing newline characters."
  15631. (mapconcat
  15632. (lambda (x)
  15633. (cond
  15634. ((listp x)
  15635. (concat "|" (mapconcat 'identity x "|") "|"))
  15636. ((eq x 'hline) "|-|")
  15637. (t (error "Garbage in listtable: %s" x))))
  15638. tbl "\n"))
  15639. (defun org-insert-columns-dblock ()
  15640. "Create a dynamic block capturing a column view table."
  15641. (interactive)
  15642. (let ((defaults '(:name "columnview" :hlines 1))
  15643. (id (completing-read
  15644. "Capture columns (local, global, entry with :ID: property) [local]: "
  15645. (append '(("global") ("local"))
  15646. (mapcar 'list (org-property-values "ID"))))))
  15647. (if (equal id "") (setq id 'local))
  15648. (if (equal id "global") (setq id 'global))
  15649. (setq defaults (append defaults (list :id id)))
  15650. (org-create-dblock defaults)
  15651. (org-update-dblock)))
  15652. ;;;; Timestamps
  15653. (defvar org-last-changed-timestamp nil)
  15654. (defvar org-time-was-given) ; dynamically scoped parameter
  15655. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15656. (defvar org-ts-what) ; dynamically scoped parameter
  15657. (defun org-time-stamp (arg)
  15658. "Prompt for a date/time and insert a time stamp.
  15659. If the user specifies a time like HH:MM, or if this command is called
  15660. with a prefix argument, the time stamp will contain date and time.
  15661. Otherwise, only the date will be included. All parts of a date not
  15662. specified by the user will be filled in from the current date/time.
  15663. So if you press just return without typing anything, the time stamp
  15664. will represent the current date/time. If there is already a timestamp
  15665. at the cursor, it will be modified."
  15666. (interactive "P")
  15667. (let* ((ts nil)
  15668. (default-time
  15669. ;; Default time is either today, or, when entering a range,
  15670. ;; the range start.
  15671. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15672. (save-excursion
  15673. (re-search-backward
  15674. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15675. (- (point) 20) t)))
  15676. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15677. (current-time)))
  15678. (default-input (and ts (org-get-compact-tod ts)))
  15679. org-time-was-given org-end-time-was-given time)
  15680. (cond
  15681. ((and (org-at-timestamp-p)
  15682. (eq last-command 'org-time-stamp)
  15683. (eq this-command 'org-time-stamp))
  15684. (insert "--")
  15685. (setq time (let ((this-command this-command))
  15686. (org-read-date arg 'totime nil nil default-time default-input)))
  15687. (org-insert-time-stamp time (or org-time-was-given arg)))
  15688. ((org-at-timestamp-p)
  15689. (setq time (let ((this-command this-command))
  15690. (org-read-date arg 'totime nil nil default-time default-input)))
  15691. (when (org-at-timestamp-p) ; just to get the match data
  15692. (replace-match "")
  15693. (setq org-last-changed-timestamp
  15694. (org-insert-time-stamp
  15695. time (or org-time-was-given arg)
  15696. nil nil nil (list org-end-time-was-given))))
  15697. (message "Timestamp updated"))
  15698. (t
  15699. (setq time (let ((this-command this-command))
  15700. (org-read-date arg 'totime nil nil default-time default-input)))
  15701. (org-insert-time-stamp time (or org-time-was-given arg)
  15702. nil nil nil (list org-end-time-was-given))))))
  15703. ;; FIXME: can we use this for something else????
  15704. ;; like computing time differences?????
  15705. (defun org-get-compact-tod (s)
  15706. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15707. (let* ((t1 (match-string 1 s))
  15708. (h1 (string-to-number (match-string 2 s)))
  15709. (m1 (string-to-number (match-string 3 s)))
  15710. (t2 (and (match-end 4) (match-string 5 s)))
  15711. (h2 (and t2 (string-to-number (match-string 6 s))))
  15712. (m2 (and t2 (string-to-number (match-string 7 s))))
  15713. dh dm)
  15714. (if (not t2)
  15715. t1
  15716. (setq dh (- h2 h1) dm (- m2 m1))
  15717. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15718. (concat t1 "+" (number-to-string dh)
  15719. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  15720. (defun org-time-stamp-inactive (&optional arg)
  15721. "Insert an inactive time stamp.
  15722. An inactive time stamp is enclosed in square brackets instead of angle
  15723. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15724. does not link to the calendar and cannot be changed with the S-cursor keys.
  15725. So these are more for recording a certain time/date."
  15726. (interactive "P")
  15727. (let (org-time-was-given org-end-time-was-given time)
  15728. (setq time (org-read-date arg 'totime))
  15729. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  15730. nil nil (list org-end-time-was-given))))
  15731. (defvar org-date-ovl (org-make-overlay 1 1))
  15732. (org-overlay-put org-date-ovl 'face 'org-warning)
  15733. (org-detach-overlay org-date-ovl)
  15734. (defvar org-ans1) ; dynamically scoped parameter
  15735. (defvar org-ans2) ; dynamically scoped parameter
  15736. (defvar org-plain-time-of-day-regexp) ; defined below
  15737. (defvar org-read-date-overlay nil)
  15738. (defvar org-dcst nil) ; dynamically scoped
  15739. (defun org-read-date (&optional with-time to-time from-string prompt
  15740. default-time default-input)
  15741. "Read a date, possibly a time, and make things smooth for the user.
  15742. The prompt will suggest to enter an ISO date, but you can also enter anything
  15743. which will at least partially be understood by `parse-time-string'.
  15744. Unrecognized parts of the date will default to the current day, month, year,
  15745. hour and minute. If this command is called to replace a timestamp at point,
  15746. of to enter the second timestamp of a range, the default time is taken from the
  15747. existing stamp. For example,
  15748. 3-2-5 --> 2003-02-05
  15749. feb 15 --> currentyear-02-15
  15750. sep 12 9 --> 2009-09-12
  15751. 12:45 --> today 12:45
  15752. 22 sept 0:34 --> currentyear-09-22 0:34
  15753. 12 --> currentyear-currentmonth-12
  15754. Fri --> nearest Friday (today or later)
  15755. etc.
  15756. Furthermore you can specify a relative date by giving, as the *first* thing
  15757. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  15758. change in days weeks, months, years.
  15759. With a single plus or minus, the date is relative to today. With a double
  15760. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15761. +4d --> four days from today
  15762. +4 --> same as above
  15763. +2w --> two weeks from today
  15764. ++5 --> five days from default date
  15765. The function understands only English month and weekday abbreviations,
  15766. but this can be configured with the variables `parse-time-months' and
  15767. `parse-time-weekdays'.
  15768. While prompting, a calendar is popped up - you can also select the
  15769. date with the mouse (button 1). The calendar shows a period of three
  15770. months. To scroll it to other months, use the keys `>' and `<'.
  15771. If you don't like the calendar, turn it off with
  15772. \(setq org-read-date-popup-calendar nil)
  15773. With optional argument TO-TIME, the date will immediately be converted
  15774. to an internal time.
  15775. With an optional argument WITH-TIME, the prompt will suggest to also
  15776. insert a time. Note that when WITH-TIME is not set, you can still
  15777. enter a time, and this function will inform the calling routine about
  15778. this change. The calling routine may then choose to change the format
  15779. used to insert the time stamp into the buffer to include the time.
  15780. With optional argument FROM-STRING, read from this string instead from
  15781. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15782. the time/date that is used for everything that is not specified by the
  15783. user."
  15784. (require 'parse-time)
  15785. (let* ((org-time-stamp-rounding-minutes
  15786. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  15787. (org-dcst org-display-custom-times)
  15788. (ct (org-current-time))
  15789. (def (or default-time ct))
  15790. (defdecode (decode-time def))
  15791. (dummy (progn
  15792. (when (< (nth 2 defdecode) org-extend-today-until)
  15793. (setcar (nthcdr 2 defdecode) -1)
  15794. (setcar (nthcdr 1 defdecode) 59)
  15795. (setq def (apply 'encode-time defdecode)
  15796. defdecode (decode-time def)))))
  15797. (calendar-move-hook nil)
  15798. (view-diary-entries-initially nil)
  15799. (view-calendar-holidays-initially nil)
  15800. (timestr (format-time-string
  15801. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  15802. (prompt (concat (if prompt (concat prompt " ") "")
  15803. (format "Date+time [%s]: " timestr)))
  15804. ans (org-ans0 "") org-ans1 org-ans2 final)
  15805. (cond
  15806. (from-string (setq ans from-string))
  15807. (org-read-date-popup-calendar
  15808. (save-excursion
  15809. (save-window-excursion
  15810. (calendar)
  15811. (calendar-forward-day (- (time-to-days def)
  15812. (calendar-absolute-from-gregorian
  15813. (calendar-current-date))))
  15814. (org-eval-in-calendar nil t)
  15815. (let* ((old-map (current-local-map))
  15816. (map (copy-keymap calendar-mode-map))
  15817. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  15818. (org-defkey map (kbd "RET") 'org-calendar-select)
  15819. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  15820. 'org-calendar-select-mouse)
  15821. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  15822. 'org-calendar-select-mouse)
  15823. (org-defkey minibuffer-local-map [(meta shift left)]
  15824. (lambda () (interactive)
  15825. (org-eval-in-calendar '(calendar-backward-month 1))))
  15826. (org-defkey minibuffer-local-map [(meta shift right)]
  15827. (lambda () (interactive)
  15828. (org-eval-in-calendar '(calendar-forward-month 1))))
  15829. (org-defkey minibuffer-local-map [(meta shift up)]
  15830. (lambda () (interactive)
  15831. (org-eval-in-calendar '(calendar-backward-year 1))))
  15832. (org-defkey minibuffer-local-map [(meta shift down)]
  15833. (lambda () (interactive)
  15834. (org-eval-in-calendar '(calendar-forward-year 1))))
  15835. (org-defkey minibuffer-local-map [(shift up)]
  15836. (lambda () (interactive)
  15837. (org-eval-in-calendar '(calendar-backward-week 1))))
  15838. (org-defkey minibuffer-local-map [(shift down)]
  15839. (lambda () (interactive)
  15840. (org-eval-in-calendar '(calendar-forward-week 1))))
  15841. (org-defkey minibuffer-local-map [(shift left)]
  15842. (lambda () (interactive)
  15843. (org-eval-in-calendar '(calendar-backward-day 1))))
  15844. (org-defkey minibuffer-local-map [(shift right)]
  15845. (lambda () (interactive)
  15846. (org-eval-in-calendar '(calendar-forward-day 1))))
  15847. (org-defkey minibuffer-local-map ">"
  15848. (lambda () (interactive)
  15849. (org-eval-in-calendar '(scroll-calendar-left 1))))
  15850. (org-defkey minibuffer-local-map "<"
  15851. (lambda () (interactive)
  15852. (org-eval-in-calendar '(scroll-calendar-right 1))))
  15853. (unwind-protect
  15854. (progn
  15855. (use-local-map map)
  15856. (add-hook 'post-command-hook 'org-read-date-display)
  15857. (setq org-ans0 (read-string prompt default-input nil nil))
  15858. ;; org-ans0: from prompt
  15859. ;; org-ans1: from mouse click
  15860. ;; org-ans2: from calendar motion
  15861. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15862. (remove-hook 'post-command-hook 'org-read-date-display)
  15863. (use-local-map old-map)
  15864. (when org-read-date-overlay
  15865. (org-delete-overlay org-read-date-overlay)
  15866. (setq org-read-date-overlay nil)))))))
  15867. (t ; Naked prompt only
  15868. (unwind-protect
  15869. (setq ans (read-string prompt default-input nil timestr))
  15870. (when org-read-date-overlay
  15871. (org-delete-overlay org-read-date-overlay)
  15872. (setq org-read-date-overlay nil)))))
  15873. (setq final (org-read-date-analyze ans def defdecode))
  15874. (if to-time
  15875. (apply 'encode-time final)
  15876. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15877. (format "%04d-%02d-%02d %02d:%02d"
  15878. (nth 5 final) (nth 4 final) (nth 3 final)
  15879. (nth 2 final) (nth 1 final))
  15880. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15881. (defvar def)
  15882. (defvar defdecode)
  15883. (defvar with-time)
  15884. (defun org-read-date-display ()
  15885. "Display the currrent date prompt interpretation in the minibuffer."
  15886. (when org-read-date-display-live
  15887. (when org-read-date-overlay
  15888. (org-delete-overlay org-read-date-overlay))
  15889. (let ((p (point)))
  15890. (end-of-line 1)
  15891. (while (not (equal (buffer-substring
  15892. (max (point-min) (- (point) 4)) (point))
  15893. " "))
  15894. (insert " "))
  15895. (goto-char p))
  15896. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15897. " " (or org-ans1 org-ans2)))
  15898. (org-end-time-was-given nil)
  15899. (f (org-read-date-analyze ans def defdecode))
  15900. (fmts (if org-dcst
  15901. org-time-stamp-custom-formats
  15902. org-time-stamp-formats))
  15903. (fmt (if (or with-time
  15904. (and (boundp 'org-time-was-given) org-time-was-given))
  15905. (cdr fmts)
  15906. (car fmts)))
  15907. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  15908. (when (and org-end-time-was-given
  15909. (string-match org-plain-time-of-day-regexp txt))
  15910. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15911. org-end-time-was-given
  15912. (substring txt (match-end 0)))))
  15913. (setq org-read-date-overlay
  15914. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15915. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  15916. (defun org-read-date-analyze (ans def defdecode)
  15917. "Analyze the combined answer of the date prompt."
  15918. ;; FIXME: cleanup and comment
  15919. (let (delta deltan deltaw deltadef year month day
  15920. hour minute second wday pm h2 m2 tl wday1)
  15921. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  15922. (setq ans (replace-match "" t t ans)
  15923. deltan (car delta)
  15924. deltaw (nth 1 delta)
  15925. deltadef (nth 2 delta)))
  15926. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  15927. (when (string-match
  15928. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15929. (setq year (if (match-end 2)
  15930. (string-to-number (match-string 2 ans))
  15931. (string-to-number (format-time-string "%Y")))
  15932. month (string-to-number (match-string 3 ans))
  15933. day (string-to-number (match-string 4 ans)))
  15934. (if (< year 100) (setq year (+ 2000 year)))
  15935. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15936. t nil ans)))
  15937. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15938. ;; If there is a time with am/pm, and *no* time without it, we convert
  15939. ;; so that matching will be successful.
  15940. (loop for i from 1 to 2 do ; twice, for end time as well
  15941. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15942. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15943. (setq hour (string-to-number (match-string 1 ans))
  15944. minute (if (match-end 3)
  15945. (string-to-number (match-string 3 ans))
  15946. 0)
  15947. pm (equal ?p
  15948. (string-to-char (downcase (match-string 4 ans)))))
  15949. (if (and (= hour 12) (not pm))
  15950. (setq hour 0)
  15951. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15952. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15953. t t ans))))
  15954. ;; Check if a time range is given as a duration
  15955. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15956. (setq hour (string-to-number (match-string 1 ans))
  15957. h2 (+ hour (string-to-number (match-string 3 ans)))
  15958. minute (string-to-number (match-string 2 ans))
  15959. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  15960. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15961. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  15962. ;; Check if there is a time range
  15963. (when (boundp 'org-end-time-was-given)
  15964. (setq org-time-was-given nil)
  15965. (when (and (string-match org-plain-time-of-day-regexp ans)
  15966. (match-end 8))
  15967. (setq org-end-time-was-given (match-string 8 ans))
  15968. (setq ans (concat (substring ans 0 (match-beginning 7))
  15969. (substring ans (match-end 7))))))
  15970. (setq tl (parse-time-string ans)
  15971. day (or (nth 3 tl) (nth 3 defdecode))
  15972. month (or (nth 4 tl)
  15973. (if (and org-read-date-prefer-future
  15974. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  15975. (1+ (nth 4 defdecode))
  15976. (nth 4 defdecode)))
  15977. year (or (nth 5 tl)
  15978. (if (and org-read-date-prefer-future
  15979. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  15980. (1+ (nth 5 defdecode))
  15981. (nth 5 defdecode)))
  15982. hour (or (nth 2 tl) (nth 2 defdecode))
  15983. minute (or (nth 1 tl) (nth 1 defdecode))
  15984. second (or (nth 0 tl) 0)
  15985. wday (nth 6 tl))
  15986. (when deltan
  15987. (unless deltadef
  15988. (let ((now (decode-time (current-time))))
  15989. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15990. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15991. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15992. ((equal deltaw "m") (setq month (+ month deltan)))
  15993. ((equal deltaw "y") (setq year (+ year deltan)))))
  15994. (when (and wday (not (nth 3 tl)))
  15995. ;; Weekday was given, but no day, so pick that day in the week
  15996. ;; on or after the derived date.
  15997. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15998. (unless (equal wday wday1)
  15999. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16000. (if (and (boundp 'org-time-was-given)
  16001. (nth 2 tl))
  16002. (setq org-time-was-given t))
  16003. (if (< year 100) (setq year (+ 2000 year)))
  16004. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16005. (list second minute hour day month year)))
  16006. (defvar parse-time-weekdays)
  16007. (defun org-read-date-get-relative (s today default)
  16008. "Check string S for special relative date string.
  16009. TODAY and DEFAULT are internal times, for today and for a default.
  16010. Return shift list (N what def-flag)
  16011. WHAT is \"d\", \"w\", \"m\", or \"y\" for day. week, month, year.
  16012. N is the number if WHATs to shift
  16013. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16014. the DEFAULT date rather than TODAY."
  16015. (when (string-match
  16016. (concat
  16017. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16018. "\\([0-9]+\\)?"
  16019. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16020. "\\([ \t]\\|$\\)") s)
  16021. (let* ((dir (if (match-end 1)
  16022. (string-to-char (substring (match-string 1 s) -1))
  16023. ?+))
  16024. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16025. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16026. (what (if (match-end 3) (match-string 3 s) "d"))
  16027. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16028. (date (if rel default today))
  16029. (wday (nth 6 (decode-time date)))
  16030. delta)
  16031. (if wday1
  16032. (progn
  16033. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16034. (if (= dir ?-) (setq delta (- delta 7)))
  16035. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16036. (list delta "d" rel))
  16037. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16038. (defun org-eval-in-calendar (form &optional keepdate)
  16039. "Eval FORM in the calendar window and return to current window.
  16040. Also, store the cursor date in variable org-ans2."
  16041. (let ((sw (selected-window)))
  16042. (select-window (get-buffer-window "*Calendar*"))
  16043. (eval form)
  16044. (when (and (not keepdate) (calendar-cursor-to-date))
  16045. (let* ((date (calendar-cursor-to-date))
  16046. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16047. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16048. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16049. (select-window sw)))
  16050. ; ;; Update the prompt to show new default date
  16051. ; (save-excursion
  16052. ; (goto-char (point-min))
  16053. ; (when (and org-ans2
  16054. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16055. ; (get-text-property (match-end 0) 'field))
  16056. ; (let ((inhibit-read-only t))
  16057. ; (replace-match (concat "[" org-ans2 "]") t t)
  16058. ; (add-text-properties (point-min) (1+ (match-end 0))
  16059. ; (text-properties-at (1+ (point-min)))))))))
  16060. (defun org-calendar-select ()
  16061. "Return to `org-read-date' with the date currently selected.
  16062. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16063. (interactive)
  16064. (when (calendar-cursor-to-date)
  16065. (let* ((date (calendar-cursor-to-date))
  16066. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16067. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16068. (if (active-minibuffer-window) (exit-minibuffer))))
  16069. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16070. "Insert a date stamp for the date given by the internal TIME.
  16071. WITH-HM means, use the stamp format that includes the time of the day.
  16072. INACTIVE means use square brackets instead of angular ones, so that the
  16073. stamp will not contribute to the agenda.
  16074. PRE and POST are optional strings to be inserted before and after the
  16075. stamp.
  16076. The command returns the inserted time stamp."
  16077. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16078. stamp)
  16079. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16080. (insert-before-markers (or pre ""))
  16081. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16082. (when (listp extra)
  16083. (setq extra (car extra))
  16084. (if (and (stringp extra)
  16085. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16086. (setq extra (format "-%02d:%02d"
  16087. (string-to-number (match-string 1 extra))
  16088. (string-to-number (match-string 2 extra))))
  16089. (setq extra nil)))
  16090. (when extra
  16091. (backward-char 1)
  16092. (insert-before-markers extra)
  16093. (forward-char 1))
  16094. (insert-before-markers (or post ""))
  16095. stamp))
  16096. (defun org-toggle-time-stamp-overlays ()
  16097. "Toggle the use of custom time stamp formats."
  16098. (interactive)
  16099. (setq org-display-custom-times (not org-display-custom-times))
  16100. (unless org-display-custom-times
  16101. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16102. (while (setq p (next-single-property-change p 'display))
  16103. (if (and (get-text-property p 'display)
  16104. (eq (get-text-property p 'face) 'org-date))
  16105. (remove-text-properties
  16106. p (setq p (next-single-property-change p 'display))
  16107. '(display t))))
  16108. (set-buffer-modified-p bmp)))
  16109. (if (featurep 'xemacs)
  16110. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16111. (org-restart-font-lock)
  16112. (setq org-table-may-need-update t)
  16113. (if org-display-custom-times
  16114. (message "Time stamps are overlayed with custom format")
  16115. (message "Time stamp overlays removed")))
  16116. (defun org-display-custom-time (beg end)
  16117. "Overlay modified time stamp format over timestamp between BED and END."
  16118. (let* ((ts (buffer-substring beg end))
  16119. t1 w1 with-hm tf time str w2 (off 0))
  16120. (save-match-data
  16121. (setq t1 (org-parse-time-string ts t))
  16122. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16123. (setq off (- (match-end 0) (match-beginning 0)))))
  16124. (setq end (- end off))
  16125. (setq w1 (- end beg)
  16126. with-hm (and (nth 1 t1) (nth 2 t1))
  16127. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16128. time (org-fix-decoded-time t1)
  16129. str (org-add-props
  16130. (format-time-string
  16131. (substring tf 1 -1) (apply 'encode-time time))
  16132. nil 'mouse-face 'highlight)
  16133. w2 (length str))
  16134. (if (not (= w2 w1))
  16135. (add-text-properties (1+ beg) (+ 2 beg)
  16136. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16137. (if (featurep 'xemacs)
  16138. (progn
  16139. (put-text-property beg end 'invisible t)
  16140. (put-text-property beg end 'end-glyph (make-glyph str)))
  16141. (put-text-property beg end 'display str))))
  16142. (defun org-translate-time (string)
  16143. "Translate all timestamps in STRING to custom format.
  16144. But do this only if the variable `org-display-custom-times' is set."
  16145. (when org-display-custom-times
  16146. (save-match-data
  16147. (let* ((start 0)
  16148. (re org-ts-regexp-both)
  16149. t1 with-hm inactive tf time str beg end)
  16150. (while (setq start (string-match re string start))
  16151. (setq beg (match-beginning 0)
  16152. end (match-end 0)
  16153. t1 (save-match-data
  16154. (org-parse-time-string (substring string beg end) t))
  16155. with-hm (and (nth 1 t1) (nth 2 t1))
  16156. inactive (equal (substring string beg (1+ beg)) "[")
  16157. tf (funcall (if with-hm 'cdr 'car)
  16158. org-time-stamp-custom-formats)
  16159. time (org-fix-decoded-time t1)
  16160. str (format-time-string
  16161. (concat
  16162. (if inactive "[" "<") (substring tf 1 -1)
  16163. (if inactive "]" ">"))
  16164. (apply 'encode-time time))
  16165. string (replace-match str t t string)
  16166. start (+ start (length str)))))))
  16167. string)
  16168. (defun org-fix-decoded-time (time)
  16169. "Set 0 instead of nil for the first 6 elements of time.
  16170. Don't touch the rest."
  16171. (let ((n 0))
  16172. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16173. (defun org-days-to-time (timestamp-string)
  16174. "Difference between TIMESTAMP-STRING and now in days."
  16175. (- (time-to-days (org-time-string-to-time timestamp-string))
  16176. (time-to-days (current-time))))
  16177. (defun org-deadline-close (timestamp-string &optional ndays)
  16178. "Is the time in TIMESTAMP-STRING close to the current date?"
  16179. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16180. (and (< (org-days-to-time timestamp-string) ndays)
  16181. (not (org-entry-is-done-p))))
  16182. (defun org-get-wdays (ts)
  16183. "Get the deadline lead time appropriate for timestring TS."
  16184. (cond
  16185. ((<= org-deadline-warning-days 0)
  16186. ;; 0 or negative, enforce this value no matter what
  16187. (- org-deadline-warning-days))
  16188. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16189. ;; lead time is specified.
  16190. (floor (* (string-to-number (match-string 1 ts))
  16191. (cdr (assoc (match-string 2 ts)
  16192. '(("d" . 1) ("w" . 7)
  16193. ("m" . 30.4) ("y" . 365.25)))))))
  16194. ;; go for the default.
  16195. (t org-deadline-warning-days)))
  16196. (defun org-calendar-select-mouse (ev)
  16197. "Return to `org-read-date' with the date currently selected.
  16198. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16199. (interactive "e")
  16200. (mouse-set-point ev)
  16201. (when (calendar-cursor-to-date)
  16202. (let* ((date (calendar-cursor-to-date))
  16203. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16204. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16205. (if (active-minibuffer-window) (exit-minibuffer))))
  16206. (defun org-check-deadlines (ndays)
  16207. "Check if there are any deadlines due or past due.
  16208. A deadline is considered due if it happens within `org-deadline-warning-days'
  16209. days from today's date. If the deadline appears in an entry marked DONE,
  16210. it is not shown. The prefix arg NDAYS can be used to test that many
  16211. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16212. (interactive "P")
  16213. (let* ((org-warn-days
  16214. (cond
  16215. ((equal ndays '(4)) 100000)
  16216. (ndays (prefix-numeric-value ndays))
  16217. (t (abs org-deadline-warning-days))))
  16218. (case-fold-search nil)
  16219. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16220. (callback
  16221. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16222. (message "%d deadlines past-due or due within %d days"
  16223. (org-occur regexp nil callback)
  16224. org-warn-days)))
  16225. (defun org-check-before-date (date)
  16226. "Check if there are deadlines or scheduled entries before DATE."
  16227. (interactive (list (org-read-date)))
  16228. (let ((case-fold-search nil)
  16229. (regexp (concat "\\<\\(" org-deadline-string
  16230. "\\|" org-scheduled-string
  16231. "\\) *<\\([^>]+\\)>"))
  16232. (callback
  16233. (lambda () (time-less-p
  16234. (org-time-string-to-time (match-string 2))
  16235. (org-time-string-to-time date)))))
  16236. (message "%d entries before %s"
  16237. (org-occur regexp nil callback) date)))
  16238. (defun org-evaluate-time-range (&optional to-buffer)
  16239. "Evaluate a time range by computing the difference between start and end.
  16240. Normally the result is just printed in the echo area, but with prefix arg
  16241. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16242. If the time range is actually in a table, the result is inserted into the
  16243. next column.
  16244. For time difference computation, a year is assumed to be exactly 365
  16245. days in order to avoid rounding problems."
  16246. (interactive "P")
  16247. (or
  16248. (org-clock-update-time-maybe)
  16249. (save-excursion
  16250. (unless (org-at-date-range-p t)
  16251. (goto-char (point-at-bol))
  16252. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16253. (if (not (org-at-date-range-p t))
  16254. (error "Not at a time-stamp range, and none found in current line")))
  16255. (let* ((ts1 (match-string 1))
  16256. (ts2 (match-string 2))
  16257. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16258. (match-end (match-end 0))
  16259. (time1 (org-time-string-to-time ts1))
  16260. (time2 (org-time-string-to-time ts2))
  16261. (t1 (time-to-seconds time1))
  16262. (t2 (time-to-seconds time2))
  16263. (diff (abs (- t2 t1)))
  16264. (negative (< (- t2 t1) 0))
  16265. ;; (ys (floor (* 365 24 60 60)))
  16266. (ds (* 24 60 60))
  16267. (hs (* 60 60))
  16268. (fy "%dy %dd %02d:%02d")
  16269. (fy1 "%dy %dd")
  16270. (fd "%dd %02d:%02d")
  16271. (fd1 "%dd")
  16272. (fh "%02d:%02d")
  16273. y d h m align)
  16274. (if havetime
  16275. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16276. y 0
  16277. d (floor (/ diff ds)) diff (mod diff ds)
  16278. h (floor (/ diff hs)) diff (mod diff hs)
  16279. m (floor (/ diff 60)))
  16280. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16281. y 0
  16282. d (floor (+ (/ diff ds) 0.5))
  16283. h 0 m 0))
  16284. (if (not to-buffer)
  16285. (message "%s" (org-make-tdiff-string y d h m))
  16286. (if (org-at-table-p)
  16287. (progn
  16288. (goto-char match-end)
  16289. (setq align t)
  16290. (and (looking-at " *|") (goto-char (match-end 0))))
  16291. (goto-char match-end))
  16292. (if (looking-at
  16293. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16294. (replace-match ""))
  16295. (if negative (insert " -"))
  16296. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16297. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16298. (insert " " (format fh h m))))
  16299. (if align (org-table-align))
  16300. (message "Time difference inserted")))))
  16301. (defun org-make-tdiff-string (y d h m)
  16302. (let ((fmt "")
  16303. (l nil))
  16304. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16305. l (push y l)))
  16306. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16307. l (push d l)))
  16308. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16309. l (push h l)))
  16310. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16311. l (push m l)))
  16312. (apply 'format fmt (nreverse l))))
  16313. (defun org-time-string-to-time (s)
  16314. (apply 'encode-time (org-parse-time-string s)))
  16315. (defun org-time-string-to-absolute (s &optional daynr)
  16316. "Convert a time stamp to an absolute day number.
  16317. If there is a specifyer for a cyclic time stamp, get the closest date to
  16318. DAYNR."
  16319. (cond
  16320. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16321. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16322. daynr
  16323. (+ daynr 1000)))
  16324. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16325. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16326. (time-to-days (current-time))) (match-string 0 s)))
  16327. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16328. (defun org-time-from-absolute (d)
  16329. "Return the time corresponding to date D.
  16330. D may be an absolute day number, or a calendar-type list (month day year)."
  16331. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16332. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16333. (defun org-calendar-holiday ()
  16334. "List of holidays, for Diary display in Org-mode."
  16335. (require 'holidays)
  16336. (let ((hl (funcall
  16337. (if (fboundp 'calendar-check-holidays)
  16338. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16339. (if hl (mapconcat 'identity hl "; "))))
  16340. (defun org-diary-sexp-entry (sexp entry date)
  16341. "Process a SEXP diary ENTRY for DATE."
  16342. (require 'diary-lib)
  16343. (let ((result (if calendar-debug-sexp
  16344. (let ((stack-trace-on-error t))
  16345. (eval (car (read-from-string sexp))))
  16346. (condition-case nil
  16347. (eval (car (read-from-string sexp)))
  16348. (error
  16349. (beep)
  16350. (message "Bad sexp at line %d in %s: %s"
  16351. (org-current-line)
  16352. (buffer-file-name) sexp)
  16353. (sleep-for 2))))))
  16354. (cond ((stringp result) result)
  16355. ((and (consp result)
  16356. (stringp (cdr result))) (cdr result))
  16357. (result entry)
  16358. (t nil))))
  16359. (defun org-diary-to-ical-string (frombuf)
  16360. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16361. This uses the icalendar.el library."
  16362. (let* ((tmpdir (if (featurep 'xemacs)
  16363. (temp-directory)
  16364. temporary-file-directory))
  16365. (tmpfile (make-temp-name
  16366. (expand-file-name "orgics" tmpdir)))
  16367. buf rtn b e)
  16368. (save-excursion
  16369. (set-buffer frombuf)
  16370. (icalendar-export-region (point-min) (point-max) tmpfile)
  16371. (setq buf (find-buffer-visiting tmpfile))
  16372. (set-buffer buf)
  16373. (goto-char (point-min))
  16374. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16375. (setq b (match-beginning 0)))
  16376. (goto-char (point-max))
  16377. (if (re-search-backward "^END:VEVENT" nil t)
  16378. (setq e (match-end 0)))
  16379. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16380. (kill-buffer buf)
  16381. (kill-buffer frombuf)
  16382. (delete-file tmpfile)
  16383. rtn))
  16384. (defun org-closest-date (start current change)
  16385. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  16386. ;; Make the proper lists from the dates
  16387. (catch 'exit
  16388. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16389. dn dw sday cday n1 n2
  16390. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16391. (setq start (org-date-to-gregorian start)
  16392. current (org-date-to-gregorian
  16393. (if org-agenda-repeating-timestamp-show-all
  16394. current
  16395. (time-to-days (current-time))))
  16396. sday (calendar-absolute-from-gregorian start)
  16397. cday (calendar-absolute-from-gregorian current))
  16398. (if (<= cday sday) (throw 'exit sday))
  16399. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16400. (setq dn (string-to-number (match-string 1 change))
  16401. dw (cdr (assoc (match-string 2 change) a1)))
  16402. (error "Invalid change specifyer: %s" change))
  16403. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16404. (cond
  16405. ((eq dw 'day)
  16406. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16407. n2 (+ n1 dn)))
  16408. ((eq dw 'year)
  16409. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16410. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16411. (setq date1 (list m d y1)
  16412. n1 (calendar-absolute-from-gregorian date1)
  16413. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16414. n2 (calendar-absolute-from-gregorian date2)))
  16415. ((eq dw 'month)
  16416. ;; approx number of month between the tow dates
  16417. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16418. ;; How often does dn fit in there?
  16419. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16420. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16421. m (+ m nm)
  16422. ny (floor (/ m 12))
  16423. y (+ y ny)
  16424. m (- m (* ny 12)))
  16425. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16426. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16427. (setq m2 (+ m dn) y2 y)
  16428. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16429. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16430. (while (< n2 cday)
  16431. (setq n1 n2 m m2 y y2)
  16432. (setq m2 (+ m dn) y2 y)
  16433. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16434. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16435. (if org-agenda-repeating-timestamp-show-all
  16436. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  16437. (if (= cday n1) n1 n2)))))
  16438. (defun org-date-to-gregorian (date)
  16439. "Turn any specification of DATE into a gregorian date for the calendar."
  16440. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16441. ((and (listp date) (= (length date) 3)) date)
  16442. ((stringp date)
  16443. (setq date (org-parse-time-string date))
  16444. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16445. ((listp date)
  16446. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16447. (defun org-parse-time-string (s &optional nodefault)
  16448. "Parse the standard Org-mode time string.
  16449. This should be a lot faster than the normal `parse-time-string'.
  16450. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16451. hour and minute fields will be nil if not given."
  16452. (if (string-match org-ts-regexp0 s)
  16453. (list 0
  16454. (if (or (match-beginning 8) (not nodefault))
  16455. (string-to-number (or (match-string 8 s) "0")))
  16456. (if (or (match-beginning 7) (not nodefault))
  16457. (string-to-number (or (match-string 7 s) "0")))
  16458. (string-to-number (match-string 4 s))
  16459. (string-to-number (match-string 3 s))
  16460. (string-to-number (match-string 2 s))
  16461. nil nil nil)
  16462. (make-list 9 0)))
  16463. (defun org-timestamp-up (&optional arg)
  16464. "Increase the date item at the cursor by one.
  16465. If the cursor is on the year, change the year. If it is on the month or
  16466. the day, change that.
  16467. With prefix ARG, change by that many units."
  16468. (interactive "p")
  16469. (org-timestamp-change (prefix-numeric-value arg)))
  16470. (defun org-timestamp-down (&optional arg)
  16471. "Decrease the date item at the cursor by one.
  16472. If the cursor is on the year, change the year. If it is on the month or
  16473. the day, change that.
  16474. With prefix ARG, change by that many units."
  16475. (interactive "p")
  16476. (org-timestamp-change (- (prefix-numeric-value arg))))
  16477. (defun org-timestamp-up-day (&optional arg)
  16478. "Increase the date in the time stamp by one day.
  16479. With prefix ARG, change that many days."
  16480. (interactive "p")
  16481. (if (and (not (org-at-timestamp-p t))
  16482. (org-on-heading-p))
  16483. (org-todo 'up)
  16484. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16485. (defun org-timestamp-down-day (&optional arg)
  16486. "Decrease the date in the time stamp by one day.
  16487. With prefix ARG, change that many days."
  16488. (interactive "p")
  16489. (if (and (not (org-at-timestamp-p t))
  16490. (org-on-heading-p))
  16491. (org-todo 'down)
  16492. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16493. (defsubst org-pos-in-match-range (pos n)
  16494. (and (match-beginning n)
  16495. (<= (match-beginning n) pos)
  16496. (>= (match-end n) pos)))
  16497. (defun org-at-timestamp-p (&optional inactive-ok)
  16498. "Determine if the cursor is in or at a timestamp."
  16499. (interactive)
  16500. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16501. (pos (point))
  16502. (ans (or (looking-at tsr)
  16503. (save-excursion
  16504. (skip-chars-backward "^[<\n\r\t")
  16505. (if (> (point) (point-min)) (backward-char 1))
  16506. (and (looking-at tsr)
  16507. (> (- (match-end 0) pos) -1))))))
  16508. (and ans
  16509. (boundp 'org-ts-what)
  16510. (setq org-ts-what
  16511. (cond
  16512. ((= pos (match-beginning 0)) 'bracket)
  16513. ((= pos (1- (match-end 0))) 'bracket)
  16514. ((org-pos-in-match-range pos 2) 'year)
  16515. ((org-pos-in-match-range pos 3) 'month)
  16516. ((org-pos-in-match-range pos 7) 'hour)
  16517. ((org-pos-in-match-range pos 8) 'minute)
  16518. ((or (org-pos-in-match-range pos 4)
  16519. (org-pos-in-match-range pos 5)) 'day)
  16520. ((and (> pos (or (match-end 8) (match-end 5)))
  16521. (< pos (match-end 0)))
  16522. (- pos (or (match-end 8) (match-end 5))))
  16523. (t 'day))))
  16524. ans))
  16525. (defun org-toggle-timestamp-type ()
  16526. ""
  16527. (interactive)
  16528. (when (org-at-timestamp-p t)
  16529. (save-excursion
  16530. (goto-char (match-beginning 0))
  16531. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16532. (goto-char (1- (match-end 0)))
  16533. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16534. (message "Timestamp is now %sactive"
  16535. (if (equal (char-before) ?>) "in" ""))))
  16536. (defun org-timestamp-change (n &optional what)
  16537. "Change the date in the time stamp at point.
  16538. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16539. `year', `minute', `second'. If WHAT is not given, the cursor position
  16540. in the timestamp determines what will be changed."
  16541. (let ((pos (point))
  16542. with-hm inactive
  16543. org-ts-what
  16544. extra
  16545. ts time time0)
  16546. (if (not (org-at-timestamp-p t))
  16547. (error "Not at a timestamp"))
  16548. (if (and (not what) (eq org-ts-what 'bracket))
  16549. (org-toggle-timestamp-type)
  16550. (if (and (not what) (not (eq org-ts-what 'day))
  16551. org-display-custom-times
  16552. (get-text-property (point) 'display)
  16553. (not (get-text-property (1- (point)) 'display)))
  16554. (setq org-ts-what 'day))
  16555. (setq org-ts-what (or what org-ts-what)
  16556. inactive (= (char-after (match-beginning 0)) ?\[)
  16557. ts (match-string 0))
  16558. (replace-match "")
  16559. (if (string-match
  16560. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16561. ts)
  16562. (setq extra (match-string 1 ts)))
  16563. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16564. (setq with-hm t))
  16565. (setq time0 (org-parse-time-string ts))
  16566. (setq time
  16567. (encode-time (or (car time0) 0)
  16568. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16569. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16570. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16571. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16572. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16573. (nthcdr 6 time0)))
  16574. (when (integerp org-ts-what)
  16575. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16576. (if (eq what 'calendar)
  16577. (let ((cal-date (org-get-date-from-calendar)))
  16578. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16579. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16580. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16581. (setcar time0 (or (car time0) 0))
  16582. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16583. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16584. (setq time (apply 'encode-time time0))))
  16585. (setq org-last-changed-timestamp
  16586. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16587. (org-clock-update-time-maybe)
  16588. (goto-char pos)
  16589. ;; Try to recenter the calendar window, if any
  16590. (if (and org-calendar-follow-timestamp-change
  16591. (get-buffer-window "*Calendar*" t)
  16592. (memq org-ts-what '(day month year)))
  16593. (org-recenter-calendar (time-to-days time))))))
  16594. ;; FIXME: does not yet work for lead times
  16595. (defun org-modify-ts-extra (s pos n)
  16596. "Change the different parts of the lead-time and repeat fields in timestamp."
  16597. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16598. ng h m new)
  16599. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16600. (cond
  16601. ((or (org-pos-in-match-range pos 2)
  16602. (org-pos-in-match-range pos 3))
  16603. (setq m (string-to-number (match-string 3 s))
  16604. h (string-to-number (match-string 2 s)))
  16605. (if (org-pos-in-match-range pos 2)
  16606. (setq h (+ h n))
  16607. (setq m (+ m n)))
  16608. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16609. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16610. (setq h (min 24 (max 0 h)))
  16611. (setq ng 1 new (format "-%02d:%02d" h m)))
  16612. ((org-pos-in-match-range pos 6)
  16613. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16614. ((org-pos-in-match-range pos 5)
  16615. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16616. (when ng
  16617. (setq s (concat
  16618. (substring s 0 (match-beginning ng))
  16619. new
  16620. (substring s (match-end ng))))))
  16621. s))
  16622. (defun org-recenter-calendar (date)
  16623. "If the calendar is visible, recenter it to DATE."
  16624. (let* ((win (selected-window))
  16625. (cwin (get-buffer-window "*Calendar*" t))
  16626. (calendar-move-hook nil))
  16627. (when cwin
  16628. (select-window cwin)
  16629. (calendar-goto-date (if (listp date) date
  16630. (calendar-gregorian-from-absolute date)))
  16631. (select-window win))))
  16632. (defun org-goto-calendar (&optional arg)
  16633. "Go to the Emacs calendar at the current date.
  16634. If there is a time stamp in the current line, go to that date.
  16635. A prefix ARG can be used to force the current date."
  16636. (interactive "P")
  16637. (let ((tsr org-ts-regexp) diff
  16638. (calendar-move-hook nil)
  16639. (view-calendar-holidays-initially nil)
  16640. (view-diary-entries-initially nil))
  16641. (if (or (org-at-timestamp-p)
  16642. (save-excursion
  16643. (beginning-of-line 1)
  16644. (looking-at (concat ".*" tsr))))
  16645. (let ((d1 (time-to-days (current-time)))
  16646. (d2 (time-to-days
  16647. (org-time-string-to-time (match-string 1)))))
  16648. (setq diff (- d2 d1))))
  16649. (calendar)
  16650. (calendar-goto-today)
  16651. (if (and diff (not arg)) (calendar-forward-day diff))))
  16652. (defun org-get-date-from-calendar ()
  16653. "Return a list (month day year) of date at point in calendar."
  16654. (with-current-buffer "*Calendar*"
  16655. (save-match-data
  16656. (calendar-cursor-to-date))))
  16657. (defun org-date-from-calendar ()
  16658. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16659. If there is already a time stamp at the cursor position, update it."
  16660. (interactive)
  16661. (if (org-at-timestamp-p t)
  16662. (org-timestamp-change 0 'calendar)
  16663. (let ((cal-date (org-get-date-from-calendar)))
  16664. (org-insert-time-stamp
  16665. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16666. ;; Make appt aware of appointments from the agenda
  16667. ;;;###autoload
  16668. (defun org-agenda-to-appt (&optional filter)
  16669. "Activate appointments found in `org-agenda-files'.
  16670. When prefixed, prompt for a regular expression and use it as a
  16671. filter: only add entries if they match this regular expression.
  16672. FILTER can be a string. In this case, use this string as a
  16673. regular expression to filter results.
  16674. FILTER can also be an alist, with the car of each cell being
  16675. either 'headline or 'category. For example:
  16676. '((headline \"IMPORTANT\")
  16677. (category \"Work\"))
  16678. will only add headlines containing IMPORTANT or headlines
  16679. belonging to the category \"Work\"."
  16680. (interactive "P")
  16681. (require 'calendar)
  16682. (if (equal filter '(4))
  16683. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16684. (let* ((cnt 0) ; count added events
  16685. (org-agenda-new-buffers nil)
  16686. (today (org-date-to-gregorian
  16687. (time-to-days (current-time))))
  16688. (files (org-agenda-files)) entries file)
  16689. ;; Get all entries which may contain an appt
  16690. (while (setq file (pop files))
  16691. (setq entries
  16692. (append entries
  16693. (org-agenda-get-day-entries
  16694. file today
  16695. :timestamp :scheduled :deadline))))
  16696. (setq entries (delq nil entries))
  16697. ;; Map thru entries and find if they pass thru the filter
  16698. (mapc
  16699. (lambda(x)
  16700. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  16701. (cat (get-text-property 1 'org-category x))
  16702. (tod (get-text-property 1 'time-of-day x))
  16703. (ok (or (null filter)
  16704. (and (stringp filter) (string-match filter evt))
  16705. (and (listp filter)
  16706. (or (string-match
  16707. (cadr (assoc 'category filter)) cat)
  16708. (string-match
  16709. (cadr (assoc 'headline filter)) evt))))))
  16710. ;; FIXME: Shall we remove text-properties for the appt text?
  16711. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  16712. (when (and ok tod)
  16713. (setq tod (number-to-string tod)
  16714. tod (when (string-match
  16715. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  16716. (concat (match-string 1 tod) ":"
  16717. (match-string 2 tod))))
  16718. (appt-add tod evt)
  16719. (setq cnt (1+ cnt))))) entries)
  16720. (org-release-buffers org-agenda-new-buffers)
  16721. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  16722. ;;; The clock for measuring work time.
  16723. (defvar org-mode-line-string "")
  16724. (put 'org-mode-line-string 'risky-local-variable t)
  16725. (defvar org-mode-line-timer nil)
  16726. (defvar org-clock-heading "")
  16727. (defvar org-clock-start-time "")
  16728. (defun org-update-mode-line ()
  16729. (let* ((delta (- (time-to-seconds (current-time))
  16730. (time-to-seconds org-clock-start-time)))
  16731. (h (floor delta 3600))
  16732. (m (floor (- delta (* 3600 h)) 60)))
  16733. (setq org-mode-line-string
  16734. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  16735. 'help-echo "Org-mode clock is running"))
  16736. (force-mode-line-update)))
  16737. (defvar org-clock-marker (make-marker)
  16738. "Marker recording the last clock-in.")
  16739. (defvar org-clock-mode-line-entry nil
  16740. "Information for the modeline about the running clock.")
  16741. (defun org-clock-in ()
  16742. "Start the clock on the current item.
  16743. If necessary, clock-out of the currently active clock."
  16744. (interactive)
  16745. (org-clock-out t)
  16746. (let (ts)
  16747. (save-excursion
  16748. (org-back-to-heading t)
  16749. (when (and org-clock-in-switch-to-state
  16750. (not (looking-at (concat outline-regexp "[ \t]*"
  16751. org-clock-in-switch-to-state
  16752. "\\>"))))
  16753. (org-todo org-clock-in-switch-to-state))
  16754. (if (and org-clock-heading-function
  16755. (functionp org-clock-heading-function))
  16756. (setq org-clock-heading (funcall org-clock-heading-function))
  16757. (if (looking-at org-complex-heading-regexp)
  16758. (setq org-clock-heading (match-string 4))
  16759. (setq org-clock-heading "???")))
  16760. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  16761. (org-clock-find-position)
  16762. (insert "\n") (backward-char 1)
  16763. (indent-relative)
  16764. (insert org-clock-string " ")
  16765. (setq org-clock-start-time (current-time))
  16766. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16767. (move-marker org-clock-marker (point) (buffer-base-buffer))
  16768. (or global-mode-string (setq global-mode-string '("")))
  16769. (or (memq 'org-mode-line-string global-mode-string)
  16770. (setq global-mode-string
  16771. (append global-mode-string '(org-mode-line-string))))
  16772. (org-update-mode-line)
  16773. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  16774. (message "Clock started at %s" ts))))
  16775. (defun org-clock-find-position ()
  16776. "Find the location where the next clock line should be inserted."
  16777. (org-back-to-heading t)
  16778. (catch 'exit
  16779. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  16780. (re (concat "^[ \t]*" org-clock-string))
  16781. (cnt 0)
  16782. first last)
  16783. (goto-char beg)
  16784. (when (eobp) (newline) (setq end (max (point) end)))
  16785. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  16786. ;; we seem to have a CLOCK drawer, so go there.
  16787. (beginning-of-line 2)
  16788. (throw 'exit t))
  16789. ;; Lets count the CLOCK lines
  16790. (goto-char beg)
  16791. (while (re-search-forward re end t)
  16792. (setq first (or first (match-beginning 0))
  16793. last (match-beginning 0)
  16794. cnt (1+ cnt)))
  16795. (when (and (integerp org-clock-into-drawer)
  16796. (>= (1+ cnt) org-clock-into-drawer))
  16797. ;; Wrap current entries into a new drawer
  16798. (goto-char last)
  16799. (beginning-of-line 2)
  16800. (if (org-at-item-p) (org-end-of-item))
  16801. (insert ":END:\n")
  16802. (beginning-of-line 0)
  16803. (org-indent-line-function)
  16804. (goto-char first)
  16805. (insert ":CLOCK:\n")
  16806. (beginning-of-line 0)
  16807. (org-indent-line-function)
  16808. (org-flag-drawer t)
  16809. (beginning-of-line 2)
  16810. (throw 'exit nil))
  16811. (goto-char beg)
  16812. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16813. (not (equal (match-string 1) org-clock-string)))
  16814. ;; Planning info, skip to after it
  16815. (beginning-of-line 2)
  16816. (or (bolp) (newline)))
  16817. (when (eq t org-clock-into-drawer)
  16818. (insert ":CLOCK:\n:END:\n")
  16819. (beginning-of-line -1)
  16820. (org-indent-line-function)
  16821. (org-flag-drawer t)
  16822. (beginning-of-line 2)
  16823. (org-indent-line-function)))))
  16824. (defun org-clock-out (&optional fail-quietly)
  16825. "Stop the currently running clock.
  16826. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  16827. (interactive)
  16828. (catch 'exit
  16829. (if (not (marker-buffer org-clock-marker))
  16830. (if fail-quietly (throw 'exit t) (error "No active clock")))
  16831. (let (ts te s h m)
  16832. (save-excursion
  16833. (set-buffer (marker-buffer org-clock-marker))
  16834. (goto-char org-clock-marker)
  16835. (beginning-of-line 1)
  16836. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16837. (equal (match-string 1) org-clock-string))
  16838. (setq ts (match-string 2))
  16839. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  16840. (goto-char (match-end 0))
  16841. (delete-region (point) (point-at-eol))
  16842. (insert "--")
  16843. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16844. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  16845. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  16846. h (floor (/ s 3600))
  16847. s (- s (* 3600 h))
  16848. m (floor (/ s 60))
  16849. s (- s (* 60 s)))
  16850. (insert " => " (format "%2d:%02d" h m))
  16851. (move-marker org-clock-marker nil)
  16852. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  16853. (org-log-done (org-parse-local-options logging 'org-log-done))
  16854. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  16855. (org-add-log-maybe 'clock-out))
  16856. (when org-mode-line-timer
  16857. (cancel-timer org-mode-line-timer)
  16858. (setq org-mode-line-timer nil))
  16859. (setq global-mode-string
  16860. (delq 'org-mode-line-string global-mode-string))
  16861. (force-mode-line-update)
  16862. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  16863. (defun org-clock-cancel ()
  16864. "Cancel the running clock be removing the start timestamp."
  16865. (interactive)
  16866. (if (not (marker-buffer org-clock-marker))
  16867. (error "No active clock"))
  16868. (save-excursion
  16869. (set-buffer (marker-buffer org-clock-marker))
  16870. (goto-char org-clock-marker)
  16871. (delete-region (1- (point-at-bol)) (point-at-eol)))
  16872. (setq global-mode-string
  16873. (delq 'org-mode-line-string global-mode-string))
  16874. (force-mode-line-update)
  16875. (message "Clock canceled"))
  16876. (defun org-clock-goto (&optional delete-windows)
  16877. "Go to the currently clocked-in entry."
  16878. (interactive "P")
  16879. (if (not (marker-buffer org-clock-marker))
  16880. (error "No active clock"))
  16881. (switch-to-buffer-other-window
  16882. (marker-buffer org-clock-marker))
  16883. (if delete-windows (delete-other-windows))
  16884. (goto-char org-clock-marker)
  16885. (org-show-entry)
  16886. (org-back-to-heading)
  16887. (recenter))
  16888. (defvar org-clock-file-total-minutes nil
  16889. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  16890. (make-variable-buffer-local 'org-clock-file-total-minutes)
  16891. (defun org-clock-sum (&optional tstart tend)
  16892. "Sum the times for each subtree.
  16893. Puts the resulting times in minutes as a text property on each headline."
  16894. (interactive)
  16895. (let* ((bmp (buffer-modified-p))
  16896. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  16897. org-clock-string
  16898. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  16899. (lmax 30)
  16900. (ltimes (make-vector lmax 0))
  16901. (t1 0)
  16902. (level 0)
  16903. ts te dt
  16904. time)
  16905. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  16906. (save-excursion
  16907. (goto-char (point-max))
  16908. (while (re-search-backward re nil t)
  16909. (cond
  16910. ((match-end 2)
  16911. ;; Two time stamps
  16912. (setq ts (match-string 2)
  16913. te (match-string 3)
  16914. ts (time-to-seconds
  16915. (apply 'encode-time (org-parse-time-string ts)))
  16916. te (time-to-seconds
  16917. (apply 'encode-time (org-parse-time-string te)))
  16918. ts (if tstart (max ts tstart) ts)
  16919. te (if tend (min te tend) te)
  16920. dt (- te ts)
  16921. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  16922. ((match-end 4)
  16923. ;; A naket time
  16924. (setq t1 (+ t1 (string-to-number (match-string 5))
  16925. (* 60 (string-to-number (match-string 4))))))
  16926. (t ;; A headline
  16927. (setq level (- (match-end 1) (match-beginning 1)))
  16928. (when (or (> t1 0) (> (aref ltimes level) 0))
  16929. (loop for l from 0 to level do
  16930. (aset ltimes l (+ (aref ltimes l) t1)))
  16931. (setq t1 0 time (aref ltimes level))
  16932. (loop for l from level to (1- lmax) do
  16933. (aset ltimes l 0))
  16934. (goto-char (match-beginning 0))
  16935. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  16936. (setq org-clock-file-total-minutes (aref ltimes 0)))
  16937. (set-buffer-modified-p bmp)))
  16938. (defun org-clock-display (&optional total-only)
  16939. "Show subtree times in the entire buffer.
  16940. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  16941. in the echo area."
  16942. (interactive)
  16943. (org-remove-clock-overlays)
  16944. (let (time h m p)
  16945. (org-clock-sum)
  16946. (unless total-only
  16947. (save-excursion
  16948. (goto-char (point-min))
  16949. (while (or (and (equal (setq p (point)) (point-min))
  16950. (get-text-property p :org-clock-minutes))
  16951. (setq p (next-single-property-change
  16952. (point) :org-clock-minutes)))
  16953. (goto-char p)
  16954. (when (setq time (get-text-property p :org-clock-minutes))
  16955. (org-put-clock-overlay time (funcall outline-level))))
  16956. (setq h (/ org-clock-file-total-minutes 60)
  16957. m (- org-clock-file-total-minutes (* 60 h)))
  16958. ;; Arrange to remove the overlays upon next change.
  16959. (when org-remove-highlights-with-change
  16960. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  16961. nil 'local))))
  16962. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  16963. (defvar org-clock-overlays nil)
  16964. (make-variable-buffer-local 'org-clock-overlays)
  16965. (defun org-put-clock-overlay (time &optional level)
  16966. "Put an overlays on the current line, displaying TIME.
  16967. If LEVEL is given, prefix time with a corresponding number of stars.
  16968. This creates a new overlay and stores it in `org-clock-overlays', so that it
  16969. will be easy to remove."
  16970. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  16971. (l (if level (org-get-legal-level level 0) 0))
  16972. (off 0)
  16973. ov tx)
  16974. (move-to-column c)
  16975. (unless (eolp) (skip-chars-backward "^ \t"))
  16976. (skip-chars-backward " \t")
  16977. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  16978. tx (concat (buffer-substring (1- (point)) (point))
  16979. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  16980. (org-add-props (format "%s %2d:%02d%s"
  16981. (make-string l ?*) h m
  16982. (make-string (- 10 l) ?\ ))
  16983. '(face secondary-selection))
  16984. ""))
  16985. (if (not (featurep 'xemacs))
  16986. (org-overlay-put ov 'display tx)
  16987. (org-overlay-put ov 'invisible t)
  16988. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  16989. (push ov org-clock-overlays)))
  16990. (defun org-remove-clock-overlays (&optional beg end noremove)
  16991. "Remove the occur highlights from the buffer.
  16992. BEG and END are ignored. If NOREMOVE is nil, remove this function
  16993. from the `before-change-functions' in the current buffer."
  16994. (interactive)
  16995. (unless org-inhibit-highlight-removal
  16996. (mapc 'org-delete-overlay org-clock-overlays)
  16997. (setq org-clock-overlays nil)
  16998. (unless noremove
  16999. (remove-hook 'before-change-functions
  17000. 'org-remove-clock-overlays 'local))))
  17001. (defun org-clock-out-if-current ()
  17002. "Clock out if the current entry contains the running clock.
  17003. This is used to stop the clock after a TODO entry is marked DONE,
  17004. and is only done if the variable `org-clock-out-when-done' is not nil."
  17005. (when (and org-clock-out-when-done
  17006. (member state org-done-keywords)
  17007. (equal (marker-buffer org-clock-marker) (current-buffer))
  17008. (< (point) org-clock-marker)
  17009. (> (save-excursion (outline-next-heading) (point))
  17010. org-clock-marker))
  17011. ;; Clock out, but don't accept a logging message for this.
  17012. (let ((org-log-done (if (and (listp org-log-done)
  17013. (member 'clock-out org-log-done))
  17014. '(done)
  17015. org-log-done)))
  17016. (org-clock-out))))
  17017. (add-hook 'org-after-todo-state-change-hook
  17018. 'org-clock-out-if-current)
  17019. (defun org-check-running-clock ()
  17020. "Check if the current buffer contains the running clock.
  17021. If yes, offer to stop it and to save the buffer with the changes."
  17022. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17023. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17024. (buffer-name))))
  17025. (org-clock-out)
  17026. (when (y-or-n-p "Save changed buffer?")
  17027. (save-buffer))))
  17028. (defun org-clock-report (&optional arg)
  17029. "Create a table containing a report about clocked time.
  17030. If the cursor is inside an existing clocktable block, then the table
  17031. will be updated. If not, a new clocktable will be inserted.
  17032. When called with a prefix argument, move to the first clock table in the
  17033. buffer and update it."
  17034. (interactive "P")
  17035. (org-remove-clock-overlays)
  17036. (when arg (org-find-dblock "clocktable"))
  17037. (if (org-in-clocktable-p)
  17038. (goto-char (org-in-clocktable-p))
  17039. (org-create-dblock (list :name "clocktable"
  17040. :maxlevel 2 :scope 'file)))
  17041. (org-update-dblock))
  17042. (defun org-in-clocktable-p ()
  17043. "Check if the cursor is in a clocktable."
  17044. (let ((pos (point)) start)
  17045. (save-excursion
  17046. (end-of-line 1)
  17047. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17048. (setq start (match-beginning 0))
  17049. (re-search-forward "^#\\+END:.*" nil t)
  17050. (>= (match-end 0) pos)
  17051. start))))
  17052. (defun org-clock-update-time-maybe ()
  17053. "If this is a CLOCK line, update it and return t.
  17054. Otherwise, return nil."
  17055. (interactive)
  17056. (save-excursion
  17057. (beginning-of-line 1)
  17058. (skip-chars-forward " \t")
  17059. (when (looking-at org-clock-string)
  17060. (let ((re (concat "[ \t]*" org-clock-string
  17061. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17062. "\\([ \t]*=>.*\\)?"))
  17063. ts te h m s)
  17064. (if (not (looking-at re))
  17065. nil
  17066. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17067. (end-of-line 1)
  17068. (setq ts (match-string 1)
  17069. te (match-string 2))
  17070. (setq s (- (time-to-seconds
  17071. (apply 'encode-time (org-parse-time-string te)))
  17072. (time-to-seconds
  17073. (apply 'encode-time (org-parse-time-string ts))))
  17074. h (floor (/ s 3600))
  17075. s (- s (* 3600 h))
  17076. m (floor (/ s 60))
  17077. s (- s (* 60 s)))
  17078. (insert " => " (format "%2d:%02d" h m))
  17079. t)))))
  17080. (defun org-clock-special-range (key &optional time as-strings)
  17081. "Return two times bordering a special time range.
  17082. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17083. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17084. A week starts Monday 0:00 and ends Sunday 24:00.
  17085. The range is determined relative to TIME. TIME defaults to the current time.
  17086. The return value is a cons cell with two internal times like the ones
  17087. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17088. the returned times will be formatted strings."
  17089. (let* ((tm (decode-time (or time (current-time))))
  17090. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17091. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17092. (dow (nth 6 tm))
  17093. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17094. (cond
  17095. ((eq key 'today)
  17096. (setq h 0 m 0 h1 24 m1 0))
  17097. ((eq key 'yesterday)
  17098. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17099. ((eq key 'thisweek)
  17100. (setq diff (if (= dow 0) 6 (1- dow))
  17101. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17102. ((eq key 'lastweek)
  17103. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17104. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17105. ((eq key 'thismonth)
  17106. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17107. ((eq key 'lastmonth)
  17108. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17109. ((eq key 'thisyear)
  17110. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17111. ((eq key 'lastyear)
  17112. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17113. (t (error "No such time block %s" key)))
  17114. (setq ts (encode-time s m h d month y)
  17115. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17116. (or d1 d) (or month1 month) (or y1 y)))
  17117. (setq fm (cdr org-time-stamp-formats))
  17118. (if as-strings
  17119. (cons (format-time-string fm ts) (format-time-string fm te))
  17120. (cons ts te))))
  17121. (defun org-dblock-write:clocktable (params)
  17122. "Write the standard clocktable."
  17123. (let ((hlchars '((1 . "*") (2 . "/")))
  17124. (emph nil)
  17125. (ins (make-marker))
  17126. (total-time nil)
  17127. ipos time h m p level hlc hdl maxlevel
  17128. ts te cc block beg end pos scope tbl tostring multifile)
  17129. (setq scope (plist-get params :scope)
  17130. tostring (plist-get params :tostring)
  17131. multifile (plist-get params :multifile)
  17132. maxlevel (or (plist-get params :maxlevel) 3)
  17133. emph (plist-get params :emphasize)
  17134. ts (plist-get params :tstart)
  17135. te (plist-get params :tend)
  17136. block (plist-get params :block))
  17137. (when block
  17138. (setq cc (org-clock-special-range block nil t)
  17139. ts (car cc) te (cdr cc)))
  17140. (if ts (setq ts (time-to-seconds
  17141. (apply 'encode-time (org-parse-time-string ts)))))
  17142. (if te (setq te (time-to-seconds
  17143. (apply 'encode-time (org-parse-time-string te)))))
  17144. (move-marker ins (point))
  17145. (setq ipos (point))
  17146. ;; Get the right scope
  17147. (setq pos (point))
  17148. (save-restriction
  17149. (cond
  17150. ((not scope))
  17151. ((eq scope 'file) (widen))
  17152. ((eq scope 'subtree) (org-narrow-to-subtree))
  17153. ((eq scope 'tree)
  17154. (while (org-up-heading-safe))
  17155. (org-narrow-to-subtree))
  17156. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17157. (symbol-name scope)))
  17158. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17159. (catch 'exit
  17160. (while (org-up-heading-safe)
  17161. (looking-at outline-regexp)
  17162. (if (<= (org-reduced-level (funcall outline-level)) level)
  17163. (throw 'exit nil))))
  17164. (org-narrow-to-subtree))
  17165. ((or (listp scope) (eq scope 'agenda))
  17166. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17167. (scope 'agenda)
  17168. (p1 (copy-sequence params))
  17169. file)
  17170. (plist-put p1 :tostring t)
  17171. (plist-put p1 :multifile t)
  17172. (plist-put p1 :scope 'file)
  17173. (org-prepare-agenda-buffers files)
  17174. (while (setq file (pop files))
  17175. (with-current-buffer (find-buffer-visiting file)
  17176. (push (org-clocktable-add-file
  17177. file (org-dblock-write:clocktable p1)) tbl)
  17178. (setq total-time (+ (or total-time 0)
  17179. org-clock-file-total-minutes)))))))
  17180. (goto-char pos)
  17181. (unless (eq scope 'agenda)
  17182. (org-clock-sum ts te)
  17183. (goto-char (point-min))
  17184. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17185. (goto-char p)
  17186. (when (setq time (get-text-property p :org-clock-minutes))
  17187. (save-excursion
  17188. (beginning-of-line 1)
  17189. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17190. (setq level (org-reduced-level
  17191. (- (match-end 1) (match-beginning 1))))
  17192. (<= level maxlevel))
  17193. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17194. hdl (match-string 2)
  17195. h (/ time 60)
  17196. m (- time (* 60 h)))
  17197. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17198. (push (concat
  17199. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17200. (make-string (1- level) ?|)
  17201. hlc (format "%d:%02d" h m) hlc
  17202. " |") tbl))))))
  17203. (setq tbl (nreverse tbl))
  17204. (if tostring
  17205. (if tbl (mapconcat 'identity tbl "\n") nil)
  17206. (goto-char ins)
  17207. (insert-before-markers
  17208. "Clock summary at ["
  17209. (substring
  17210. (format-time-string (cdr org-time-stamp-formats))
  17211. 1 -1)
  17212. "]."
  17213. (if block
  17214. (format " Considered range is /%s/." block)
  17215. "")
  17216. "\n\n"
  17217. (if (eq scope 'agenda) "|File" "")
  17218. "|L|Headline|Time|\n")
  17219. (setq total-time (or total-time org-clock-file-total-minutes)
  17220. h (/ total-time 60)
  17221. m (- total-time (* 60 h)))
  17222. (insert-before-markers
  17223. "|-\n|"
  17224. (if (eq scope 'agenda) "|" "")
  17225. "|"
  17226. "*Total time*| "
  17227. (format "*%d:%02d*" h m)
  17228. "|\n|-\n")
  17229. (setq tbl (delq nil tbl))
  17230. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17231. (equal (substring (car tbl) 0 2) "|-"))
  17232. (pop tbl))
  17233. (insert-before-markers (mapconcat
  17234. 'identity (delq nil tbl)
  17235. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17236. (backward-delete-char 1)
  17237. (goto-char ipos)
  17238. (skip-chars-forward "^|")
  17239. (org-table-align)))))
  17240. (defun org-clocktable-add-file (file table)
  17241. (if table
  17242. (let ((lines (org-split-string table "\n"))
  17243. (ff (file-name-nondirectory file)))
  17244. (mapconcat 'identity
  17245. (mapcar (lambda (x)
  17246. (if (string-match org-table-dataline-regexp x)
  17247. (concat "|" ff x)
  17248. x))
  17249. lines)
  17250. "\n"))))
  17251. ;; FIXME: I don't think anybody uses this, ask David
  17252. (defun org-collect-clock-time-entries ()
  17253. "Return an internal list with clocking information.
  17254. This list has one entry for each CLOCK interval.
  17255. FIXME: describe the elements."
  17256. (interactive)
  17257. (let ((re (concat "^[ \t]*" org-clock-string
  17258. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17259. rtn beg end next cont level title total closedp leafp
  17260. clockpos titlepos h m donep)
  17261. (save-excursion
  17262. (org-clock-sum)
  17263. (goto-char (point-min))
  17264. (while (re-search-forward re nil t)
  17265. (setq clockpos (match-beginning 0)
  17266. beg (match-string 1) end (match-string 2)
  17267. cont (match-end 0))
  17268. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17269. end (apply 'encode-time (org-parse-time-string end)))
  17270. (org-back-to-heading t)
  17271. (setq donep (org-entry-is-done-p))
  17272. (setq titlepos (point)
  17273. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17274. h (/ total 60) m (- total (* 60 h))
  17275. total (cons h m))
  17276. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17277. (setq level (- (match-end 1) (match-beginning 1))
  17278. title (org-match-string-no-properties 2))
  17279. (save-excursion (outline-next-heading) (setq next (point)))
  17280. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17281. (goto-char next)
  17282. (setq leafp (and (looking-at "^\\*+ ")
  17283. (<= (- (match-end 0) (point)) level)))
  17284. (push (list beg end clockpos closedp donep
  17285. total title titlepos level leafp)
  17286. rtn)
  17287. (goto-char cont)))
  17288. (nreverse rtn)))
  17289. ;;;; Agenda, and Diary Integration
  17290. ;;; Define the Org-agenda-mode
  17291. (defvar org-agenda-mode-map (make-sparse-keymap)
  17292. "Keymap for `org-agenda-mode'.")
  17293. (defvar org-agenda-menu) ; defined later in this file.
  17294. (defvar org-agenda-follow-mode nil)
  17295. (defvar org-agenda-show-log nil)
  17296. (defvar org-agenda-redo-command nil)
  17297. (defvar org-agenda-mode-hook nil)
  17298. (defvar org-agenda-type nil)
  17299. (defvar org-agenda-force-single-file nil)
  17300. (defun org-agenda-mode ()
  17301. "Mode for time-sorted view on action items in Org-mode files.
  17302. The following commands are available:
  17303. \\{org-agenda-mode-map}"
  17304. (interactive)
  17305. (kill-all-local-variables)
  17306. (setq org-agenda-undo-list nil
  17307. org-agenda-pending-undo-list nil)
  17308. (setq major-mode 'org-agenda-mode)
  17309. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17310. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17311. (setq mode-name "Org-Agenda")
  17312. (use-local-map org-agenda-mode-map)
  17313. (easy-menu-add org-agenda-menu)
  17314. (if org-startup-truncated (setq truncate-lines t))
  17315. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17316. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17317. ;; Make sure properties are removed when copying text
  17318. (when (boundp 'buffer-substring-filters)
  17319. (org-set-local 'buffer-substring-filters
  17320. (cons (lambda (x)
  17321. (set-text-properties 0 (length x) nil x) x)
  17322. buffer-substring-filters)))
  17323. (unless org-agenda-keep-modes
  17324. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17325. org-agenda-show-log nil))
  17326. (easy-menu-change
  17327. '("Agenda") "Agenda Files"
  17328. (append
  17329. (list
  17330. (vector
  17331. (if (get 'org-agenda-files 'org-restrict)
  17332. "Restricted to single file"
  17333. "Edit File List")
  17334. '(org-edit-agenda-file-list)
  17335. (not (get 'org-agenda-files 'org-restrict)))
  17336. "--")
  17337. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17338. (org-agenda-set-mode-name)
  17339. (apply
  17340. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17341. (list 'org-agenda-mode-hook)))
  17342. (substitute-key-definition 'undo 'org-agenda-undo
  17343. org-agenda-mode-map global-map)
  17344. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17345. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17346. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17347. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17348. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17349. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17350. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17351. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17352. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17353. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17354. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17355. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17356. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17357. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17358. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17359. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17360. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17361. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17362. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17363. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17364. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17365. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17366. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17367. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17368. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17369. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17370. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17371. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17372. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17373. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17374. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17375. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17376. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17377. (while l (org-defkey org-agenda-mode-map
  17378. (int-to-string (pop l)) 'digit-argument)))
  17379. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17380. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17381. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17382. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17383. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17384. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17385. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17386. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17387. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17388. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17389. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17390. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17391. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17392. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17393. (org-defkey org-agenda-mode-map "n" 'next-line)
  17394. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17395. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17396. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17397. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17398. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17399. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17400. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17401. (eval-after-load "calendar"
  17402. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17403. 'org-calendar-goto-agenda))
  17404. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17405. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17406. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17407. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17408. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17409. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17410. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17411. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17412. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17413. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17414. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17415. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17416. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17417. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17418. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17419. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17420. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17421. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17422. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17423. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17424. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17425. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17426. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17427. "Local keymap for agenda entries from Org-mode.")
  17428. (org-defkey org-agenda-keymap
  17429. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17430. (org-defkey org-agenda-keymap
  17431. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17432. (when org-agenda-mouse-1-follows-link
  17433. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17434. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17435. '("Agenda"
  17436. ("Agenda Files")
  17437. "--"
  17438. ["Show" org-agenda-show t]
  17439. ["Go To (other window)" org-agenda-goto t]
  17440. ["Go To (this window)" org-agenda-switch-to t]
  17441. ["Follow Mode" org-agenda-follow-mode
  17442. :style toggle :selected org-agenda-follow-mode :active t]
  17443. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17444. "--"
  17445. ["Cycle TODO" org-agenda-todo t]
  17446. ["Archive subtree" org-agenda-archive t]
  17447. ["Delete subtree" org-agenda-kill t]
  17448. "--"
  17449. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17450. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17451. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17452. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17453. "--"
  17454. ("Tags and Properties"
  17455. ["Show all Tags" org-agenda-show-tags t]
  17456. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17457. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17458. "--"
  17459. ["Column View" org-columns t])
  17460. ("Date/Schedule"
  17461. ["Schedule" org-agenda-schedule t]
  17462. ["Set Deadline" org-agenda-deadline t]
  17463. "--"
  17464. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17465. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17466. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17467. ("Clock"
  17468. ["Clock in" org-agenda-clock-in t]
  17469. ["Clock out" org-agenda-clock-out t]
  17470. ["Clock cancel" org-agenda-clock-cancel t]
  17471. ["Goto running clock" org-clock-goto t])
  17472. ("Priority"
  17473. ["Set Priority" org-agenda-priority t]
  17474. ["Increase Priority" org-agenda-priority-up t]
  17475. ["Decrease Priority" org-agenda-priority-down t]
  17476. ["Show Priority" org-agenda-show-priority t])
  17477. ("Calendar/Diary"
  17478. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17479. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17480. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17481. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17482. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17483. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17484. "--"
  17485. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17486. "--"
  17487. ("View"
  17488. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17489. :style radio :selected (equal org-agenda-ndays 1)]
  17490. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17491. :style radio :selected (equal org-agenda-ndays 7)]
  17492. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17493. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17494. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17495. :style radio :selected (member org-agenda-ndays '(365 366))]
  17496. "--"
  17497. ["Show Logbook entries" org-agenda-log-mode
  17498. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17499. ["Include Diary" org-agenda-toggle-diary
  17500. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17501. ["Use Time Grid" org-agenda-toggle-time-grid
  17502. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17503. ["Write view to file" org-write-agenda t]
  17504. ["Rebuild buffer" org-agenda-redo t]
  17505. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17506. "--"
  17507. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17508. "--"
  17509. ["Quit" org-agenda-quit t]
  17510. ["Exit and Release Buffers" org-agenda-exit t]
  17511. ))
  17512. ;;; Agenda undo
  17513. (defvar org-agenda-allow-remote-undo t
  17514. "Non-nil means, allow remote undo from the agenda buffer.")
  17515. (defvar org-agenda-undo-list nil
  17516. "List of undoable operations in the agenda since last refresh.")
  17517. (defvar org-agenda-undo-has-started-in nil
  17518. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17519. (defvar org-agenda-pending-undo-list nil
  17520. "In a series of undo commands, this is the list of remaning undo items.")
  17521. (defmacro org-if-unprotected (&rest body)
  17522. "Execute BODY if there is no `org-protected' text property at point."
  17523. (declare (debug t))
  17524. `(unless (get-text-property (point) 'org-protected)
  17525. ,@body))
  17526. (defmacro org-with-remote-undo (_buffer &rest _body)
  17527. "Execute BODY while recording undo information in two buffers."
  17528. (declare (indent 1) (debug t))
  17529. `(let ((_cline (org-current-line))
  17530. (_cmd this-command)
  17531. (_buf1 (current-buffer))
  17532. (_buf2 ,_buffer)
  17533. (_undo1 buffer-undo-list)
  17534. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17535. _c1 _c2)
  17536. ,@_body
  17537. (when org-agenda-allow-remote-undo
  17538. (setq _c1 (org-verify-change-for-undo
  17539. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17540. _c2 (org-verify-change-for-undo
  17541. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17542. (when (or _c1 _c2)
  17543. ;; make sure there are undo boundaries
  17544. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17545. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17546. ;; remember which buffer to undo
  17547. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17548. org-agenda-undo-list)))))
  17549. (defun org-agenda-undo ()
  17550. "Undo a remote editing step in the agenda.
  17551. This undoes changes both in the agenda buffer and in the remote buffer
  17552. that have been changed along."
  17553. (interactive)
  17554. (or org-agenda-allow-remote-undo
  17555. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17556. (if (not (eq this-command last-command))
  17557. (setq org-agenda-undo-has-started-in nil
  17558. org-agenda-pending-undo-list org-agenda-undo-list))
  17559. (if (not org-agenda-pending-undo-list)
  17560. (error "No further undo information"))
  17561. (let* ((entry (pop org-agenda-pending-undo-list))
  17562. buf line cmd rembuf)
  17563. (setq cmd (pop entry) line (pop entry))
  17564. (setq rembuf (nth 2 entry))
  17565. (org-with-remote-undo rembuf
  17566. (while (bufferp (setq buf (pop entry)))
  17567. (if (pop entry)
  17568. (with-current-buffer buf
  17569. (let ((last-undo-buffer buf)
  17570. (inhibit-read-only t))
  17571. (unless (memq buf org-agenda-undo-has-started-in)
  17572. (push buf org-agenda-undo-has-started-in)
  17573. (make-local-variable 'pending-undo-list)
  17574. (undo-start))
  17575. (while (and pending-undo-list
  17576. (listp pending-undo-list)
  17577. (not (car pending-undo-list)))
  17578. (pop pending-undo-list))
  17579. (undo-more 1))))))
  17580. (goto-line line)
  17581. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17582. (defun org-verify-change-for-undo (l1 l2)
  17583. "Verify that a real change occurred between the undo lists L1 and L2."
  17584. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17585. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17586. (not (eq l1 l2)))
  17587. ;;; Agenda dispatch
  17588. (defvar org-agenda-restrict nil)
  17589. (defvar org-agenda-restrict-begin (make-marker))
  17590. (defvar org-agenda-restrict-end (make-marker))
  17591. (defvar org-agenda-last-dispatch-buffer nil)
  17592. (defvar org-agenda-overriding-restriction nil)
  17593. ;;;###autoload
  17594. (defun org-agenda (arg &optional keys restriction)
  17595. "Dispatch agenda commands to collect entries to the agenda buffer.
  17596. Prompts for a command to execute. Any prefix arg will be passed
  17597. on to the selected command. The default selections are:
  17598. a Call `org-agenda-list' to display the agenda for current day or week.
  17599. t Call `org-todo-list' to display the global todo list.
  17600. T Call `org-todo-list' to display the global todo list, select only
  17601. entries with a specific TODO keyword (the user gets a prompt).
  17602. m Call `org-tags-view' to display headlines with tags matching
  17603. a condition (the user is prompted for the condition).
  17604. M Like `m', but select only TODO entries, no ordinary headlines.
  17605. L Create a timeline for the current buffer.
  17606. e Export views to associated files.
  17607. More commands can be added by configuring the variable
  17608. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17609. searches can be pre-defined in this way.
  17610. If the current buffer is in Org-mode and visiting a file, you can also
  17611. first press `<' once to indicate that the agenda should be temporarily
  17612. \(until the next use of \\[org-agenda]) restricted to the current file.
  17613. Pressing `<' twice means to restrict to the current subtree or region
  17614. \(if active)."
  17615. (interactive "P")
  17616. (catch 'exit
  17617. (let* ((prefix-descriptions nil)
  17618. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17619. (org-agenda-custom-commands
  17620. ;; normalize different versions
  17621. (delq nil
  17622. (mapcar
  17623. (lambda (x)
  17624. (cond ((stringp (cdr x))
  17625. (push x prefix-descriptions)
  17626. nil)
  17627. ((stringp (nth 1 x)) x)
  17628. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17629. (t (cons (car x) (cons "" (cdr x))))))
  17630. org-agenda-custom-commands)))
  17631. (buf (current-buffer))
  17632. (bfn (buffer-file-name (buffer-base-buffer)))
  17633. entry key type match lprops ans)
  17634. ;; Turn off restriction unless there is an overriding one
  17635. (unless org-agenda-overriding-restriction
  17636. (put 'org-agenda-files 'org-restrict nil)
  17637. (setq org-agenda-restrict nil)
  17638. (move-marker org-agenda-restrict-begin nil)
  17639. (move-marker org-agenda-restrict-end nil))
  17640. ;; Delete old local properties
  17641. (put 'org-agenda-redo-command 'org-lprops nil)
  17642. ;; Remember where this call originated
  17643. (setq org-agenda-last-dispatch-buffer (current-buffer))
  17644. (unless keys
  17645. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  17646. keys (car ans)
  17647. restriction (cdr ans)))
  17648. ;; Estabish the restriction, if any
  17649. (when (and (not org-agenda-overriding-restriction) restriction)
  17650. (put 'org-agenda-files 'org-restrict (list bfn))
  17651. (cond
  17652. ((eq restriction 'region)
  17653. (setq org-agenda-restrict t)
  17654. (move-marker org-agenda-restrict-begin (region-beginning))
  17655. (move-marker org-agenda-restrict-end (region-end)))
  17656. ((eq restriction 'subtree)
  17657. (save-excursion
  17658. (setq org-agenda-restrict t)
  17659. (org-back-to-heading t)
  17660. (move-marker org-agenda-restrict-begin (point))
  17661. (move-marker org-agenda-restrict-end
  17662. (progn (org-end-of-subtree t)))))))
  17663. (require 'calendar) ; FIXME: can we avoid this for some commands?
  17664. ;; For example the todo list should not need it (but does...)
  17665. (cond
  17666. ((setq entry (assoc keys org-agenda-custom-commands))
  17667. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  17668. (progn
  17669. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  17670. (put 'org-agenda-redo-command 'org-lprops lprops)
  17671. (cond
  17672. ((eq type 'agenda)
  17673. (org-let lprops '(org-agenda-list current-prefix-arg)))
  17674. ((eq type 'alltodo)
  17675. (org-let lprops '(org-todo-list current-prefix-arg)))
  17676. ((eq type 'stuck)
  17677. (org-let lprops '(org-agenda-list-stuck-projects
  17678. current-prefix-arg)))
  17679. ((eq type 'tags)
  17680. (org-let lprops '(org-tags-view current-prefix-arg match)))
  17681. ((eq type 'tags-todo)
  17682. (org-let lprops '(org-tags-view '(4) match)))
  17683. ((eq type 'todo)
  17684. (org-let lprops '(org-todo-list match)))
  17685. ((eq type 'tags-tree)
  17686. (org-check-for-org-mode)
  17687. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  17688. ((eq type 'todo-tree)
  17689. (org-check-for-org-mode)
  17690. (org-let lprops
  17691. '(org-occur (concat "^" outline-regexp "[ \t]*"
  17692. (regexp-quote match) "\\>"))))
  17693. ((eq type 'occur-tree)
  17694. (org-check-for-org-mode)
  17695. (org-let lprops '(org-occur match)))
  17696. ((functionp type)
  17697. (org-let lprops '(funcall type match)))
  17698. ((fboundp type)
  17699. (org-let lprops '(funcall type match)))
  17700. (t (error "Invalid custom agenda command type %s" type))))
  17701. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  17702. ((equal keys "C")
  17703. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  17704. (customize-variable 'org-agenda-custom-commands))
  17705. ((equal keys "a") (call-interactively 'org-agenda-list))
  17706. ((equal keys "t") (call-interactively 'org-todo-list))
  17707. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  17708. ((equal keys "m") (call-interactively 'org-tags-view))
  17709. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  17710. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  17711. ((equal keys "L")
  17712. (unless (org-mode-p)
  17713. (error "This is not an Org-mode file"))
  17714. (unless restriction
  17715. (put 'org-agenda-files 'org-restrict (list bfn))
  17716. (org-call-with-arg 'org-timeline arg)))
  17717. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  17718. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  17719. ((equal keys "!") (customize-variable 'org-stuck-projects))
  17720. (t (error "Invalid agenda key"))))))
  17721. (defun org-agenda-normalize-custom-commands (cmds)
  17722. (delq nil
  17723. (mapcar
  17724. (lambda (x)
  17725. (cond ((stringp (cdr x)) nil)
  17726. ((stringp (nth 1 x)) x)
  17727. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17728. (t (cons (car x) (cons "" (cdr x))))))
  17729. cmds)))
  17730. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  17731. "The user interface for selecting an agenda command."
  17732. (catch 'exit
  17733. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  17734. (restrict-ok (and bfn (org-mode-p)))
  17735. (region-p (org-region-active-p))
  17736. (custom org-agenda-custom-commands)
  17737. (selstring "")
  17738. restriction second-time
  17739. c entry key type match prefixes rmheader header-end custom1 desc)
  17740. (save-window-excursion
  17741. (delete-other-windows)
  17742. (org-switch-to-buffer-other-window " *Agenda Commands*")
  17743. (erase-buffer)
  17744. (insert (eval-when-compile
  17745. (let ((header
  17746. "
  17747. Press key for an agenda command: < Buffer,subtree/region restriction
  17748. -------------------------------- > Remove restriction
  17749. a Agenda for current week or day e Export agenda views
  17750. t List of all TODO entries T Entries with special TODO kwd
  17751. m Match a TAGS query M Like m, but only TODO entries
  17752. L Timeline for current buffer # List stuck projects (!=configure)
  17753. / Multi-occur C Configure custom agenda commands
  17754. ")
  17755. (start 0))
  17756. (while (string-match
  17757. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  17758. header start)
  17759. (setq start (match-end 0))
  17760. (add-text-properties (match-beginning 2) (match-end 2)
  17761. '(face bold) header))
  17762. header)))
  17763. (setq header-end (move-marker (make-marker) (point)))
  17764. (while t
  17765. (setq custom1 custom)
  17766. (when (eq rmheader t)
  17767. (goto-line 1)
  17768. (re-search-forward ":" nil t)
  17769. (delete-region (match-end 0) (point-at-eol))
  17770. (forward-char 1)
  17771. (looking-at "-+")
  17772. (delete-region (match-end 0) (point-at-eol))
  17773. (move-marker header-end (match-end 0)))
  17774. (goto-char header-end)
  17775. (delete-region (point) (point-max))
  17776. (while (setq entry (pop custom1))
  17777. (setq key (car entry) desc (nth 1 entry)
  17778. type (nth 2 entry) match (nth 3 entry))
  17779. (if (> (length key) 1)
  17780. (add-to-list 'prefixes (string-to-char key))
  17781. (insert
  17782. (format
  17783. "\n%-4s%-14s: %s"
  17784. (org-add-props (copy-sequence key)
  17785. '(face bold))
  17786. (cond
  17787. ((string-match "\\S-" desc) desc)
  17788. ((eq type 'agenda) "Agenda for current week or day")
  17789. ((eq type 'alltodo) "List of all TODO entries")
  17790. ((eq type 'stuck) "List of stuck projects")
  17791. ((eq type 'todo) "TODO keyword")
  17792. ((eq type 'tags) "Tags query")
  17793. ((eq type 'tags-todo) "Tags (TODO)")
  17794. ((eq type 'tags-tree) "Tags tree")
  17795. ((eq type 'todo-tree) "TODO kwd tree")
  17796. ((eq type 'occur-tree) "Occur tree")
  17797. ((functionp type) (if (symbolp type)
  17798. (symbol-name type)
  17799. "Lambda expression"))
  17800. (t "???"))
  17801. (cond
  17802. ((stringp match)
  17803. (org-add-props match nil 'face 'org-warning))
  17804. (match
  17805. (format "set of %d commands" (length match)))
  17806. (t ""))))))
  17807. (when prefixes
  17808. (mapc (lambda (x)
  17809. (insert
  17810. (format "\n%s %s"
  17811. (org-add-props (char-to-string x)
  17812. nil 'face 'bold)
  17813. (or (cdr (assoc (concat selstring (char-to-string x))
  17814. prefix-descriptions))
  17815. "Prefix key"))))
  17816. prefixes))
  17817. (goto-char (point-min))
  17818. (when (fboundp 'fit-window-to-buffer)
  17819. (if second-time
  17820. (if (not (pos-visible-in-window-p (point-max)))
  17821. (fit-window-to-buffer))
  17822. (setq second-time t)
  17823. (fit-window-to-buffer)))
  17824. (message "Press key for agenda command%s:"
  17825. (if (or restrict-ok org-agenda-overriding-restriction)
  17826. (if org-agenda-overriding-restriction
  17827. " (restriction lock active)"
  17828. (if restriction
  17829. (format " (restricted to %s)" restriction)
  17830. " (unrestricted)"))
  17831. ""))
  17832. (setq c (read-char-exclusive))
  17833. (message "")
  17834. (cond
  17835. ((assoc (char-to-string c) custom)
  17836. (setq selstring (concat selstring (char-to-string c)))
  17837. (throw 'exit (cons selstring restriction)))
  17838. ((memq c prefixes)
  17839. (setq selstring (concat selstring (char-to-string c))
  17840. prefixes nil
  17841. rmheader (or rmheader t)
  17842. custom (delq nil (mapcar
  17843. (lambda (x)
  17844. (if (or (= (length (car x)) 1)
  17845. (/= (string-to-char (car x)) c))
  17846. nil
  17847. (cons (substring (car x) 1) (cdr x))))
  17848. custom))))
  17849. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  17850. (message "Restriction is only possible in Org-mode buffers")
  17851. (ding) (sit-for 1))
  17852. ((eq c ?1)
  17853. (org-agenda-remove-restriction-lock 'noupdate)
  17854. (setq restriction 'buffer))
  17855. ((eq c ?0)
  17856. (org-agenda-remove-restriction-lock 'noupdate)
  17857. (setq restriction (if region-p 'region 'subtree)))
  17858. ((eq c ?<)
  17859. (org-agenda-remove-restriction-lock 'noupdate)
  17860. (setq restriction
  17861. (cond
  17862. ((eq restriction 'buffer)
  17863. (if region-p 'region 'subtree))
  17864. ((memq restriction '(subtree region))
  17865. nil)
  17866. (t 'buffer))))
  17867. ((eq c ?>)
  17868. (org-agenda-remove-restriction-lock 'noupdate)
  17869. (setq restriction nil))
  17870. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  17871. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  17872. ((and (> (length selstring) 0) (eq c ?\d))
  17873. (delete-window)
  17874. (org-agenda-get-restriction-and-command prefix-descriptions))
  17875. ((equal c ?q) (error "Abort"))
  17876. (t (error "Invalid key %c" c))))))))
  17877. (defun org-run-agenda-series (name series)
  17878. (org-prepare-agenda name)
  17879. (let* ((org-agenda-multi t)
  17880. (redo (list 'org-run-agenda-series name (list 'quote series)))
  17881. (cmds (car series))
  17882. (gprops (nth 1 series))
  17883. match ;; The byte compiler incorrectly complains about this. Keep it!
  17884. cmd type lprops)
  17885. (while (setq cmd (pop cmds))
  17886. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  17887. (cond
  17888. ((eq type 'agenda)
  17889. (org-let2 gprops lprops
  17890. '(call-interactively 'org-agenda-list)))
  17891. ((eq type 'alltodo)
  17892. (org-let2 gprops lprops
  17893. '(call-interactively 'org-todo-list)))
  17894. ((eq type 'stuck)
  17895. (org-let2 gprops lprops
  17896. '(call-interactively 'org-agenda-list-stuck-projects)))
  17897. ((eq type 'tags)
  17898. (org-let2 gprops lprops
  17899. '(org-tags-view current-prefix-arg match)))
  17900. ((eq type 'tags-todo)
  17901. (org-let2 gprops lprops
  17902. '(org-tags-view '(4) match)))
  17903. ((eq type 'todo)
  17904. (org-let2 gprops lprops
  17905. '(org-todo-list match)))
  17906. ((fboundp type)
  17907. (org-let2 gprops lprops
  17908. '(funcall type match)))
  17909. (t (error "Invalid type in command series"))))
  17910. (widen)
  17911. (setq org-agenda-redo-command redo)
  17912. (goto-char (point-min)))
  17913. (org-finalize-agenda))
  17914. ;;;###autoload
  17915. (defmacro org-batch-agenda (cmd-key &rest parameters)
  17916. "Run an agenda command in batch mode and send the result to STDOUT.
  17917. If CMD-KEY is a string of length 1, it is used as a key in
  17918. `org-agenda-custom-commands' and triggers this command. If it is a
  17919. longer string is is used as a tags/todo match string.
  17920. Paramters are alternating variable names and values that will be bound
  17921. before running the agenda command."
  17922. (let (pars)
  17923. (while parameters
  17924. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17925. (if (> (length cmd-key) 2)
  17926. (eval (list 'let (nreverse pars)
  17927. (list 'org-tags-view nil cmd-key)))
  17928. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17929. (set-buffer org-agenda-buffer-name)
  17930. (princ (org-encode-for-stdout (buffer-string)))))
  17931. (defun org-encode-for-stdout (string)
  17932. (if (fboundp 'encode-coding-string)
  17933. (encode-coding-string string buffer-file-coding-system)
  17934. string))
  17935. (defvar org-agenda-info nil)
  17936. ;;;###autoload
  17937. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  17938. "Run an agenda command in batch mode and send the result to STDOUT.
  17939. If CMD-KEY is a string of length 1, it is used as a key in
  17940. `org-agenda-custom-commands' and triggers this command. If it is a
  17941. longer string is is used as a tags/todo match string.
  17942. Paramters are alternating variable names and values that will be bound
  17943. before running the agenda command.
  17944. The output gives a line for each selected agenda item. Each
  17945. item is a list of comma-separated values, like this:
  17946. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  17947. category The category of the item
  17948. head The headline, without TODO kwd, TAGS and PRIORITY
  17949. type The type of the agenda entry, can be
  17950. todo selected in TODO match
  17951. tagsmatch selected in tags match
  17952. diary imported from diary
  17953. deadline a deadline on given date
  17954. scheduled scheduled on given date
  17955. timestamp entry has timestamp on given date
  17956. closed entry was closed on given date
  17957. upcoming-deadline warning about deadline
  17958. past-scheduled forwarded scheduled item
  17959. block entry has date block including g. date
  17960. todo The todo keyword, if any
  17961. tags All tags including inherited ones, separated by colons
  17962. date The relevant date, like 2007-2-14
  17963. time The time, like 15:00-16:50
  17964. extra Sting with extra planning info
  17965. priority-l The priority letter if any was given
  17966. priority-n The computed numerical priority
  17967. agenda-day The day in the agenda where this is listed"
  17968. (let (pars)
  17969. (while parameters
  17970. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17971. (push (list 'org-agenda-remove-tags t) pars)
  17972. (if (> (length cmd-key) 2)
  17973. (eval (list 'let (nreverse pars)
  17974. (list 'org-tags-view nil cmd-key)))
  17975. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17976. (set-buffer org-agenda-buffer-name)
  17977. (let* ((lines (org-split-string (buffer-string) "\n"))
  17978. line)
  17979. (while (setq line (pop lines))
  17980. (catch 'next
  17981. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  17982. (setq org-agenda-info
  17983. (org-fix-agenda-info (text-properties-at 0 line)))
  17984. (princ
  17985. (org-encode-for-stdout
  17986. (mapconcat 'org-agenda-export-csv-mapper
  17987. '(org-category txt type todo tags date time-of-day extra
  17988. priority-letter priority agenda-day)
  17989. ",")))
  17990. (princ "\n"))))))
  17991. (defun org-fix-agenda-info (props)
  17992. "Make sure all properties on an agenda item have a canonical form,
  17993. so the the export commands caneasily use it."
  17994. (let (tmp re)
  17995. (when (setq tmp (plist-get props 'tags))
  17996. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  17997. (when (setq tmp (plist-get props 'date))
  17998. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  17999. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18000. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18001. (setq tmp (calendar-date-string tmp)))
  18002. (setq props (plist-put props 'date tmp)))
  18003. (when (setq tmp (plist-get props 'day))
  18004. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18005. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18006. (setq tmp (calendar-date-string tmp)))
  18007. (setq props (plist-put props 'day tmp))
  18008. (setq props (plist-put props 'agenda-day tmp)))
  18009. (when (setq tmp (plist-get props 'txt))
  18010. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18011. (plist-put props 'priority-letter (match-string 1 tmp))
  18012. (setq tmp (replace-match "" t t tmp)))
  18013. (when (and (setq re (plist-get props 'org-todo-regexp))
  18014. (setq re (concat "\\`\\.*" re " ?"))
  18015. (string-match re tmp))
  18016. (plist-put props 'todo (match-string 1 tmp))
  18017. (setq tmp (replace-match "" t t tmp)))
  18018. (plist-put props 'txt tmp)))
  18019. props)
  18020. (defun org-agenda-export-csv-mapper (prop)
  18021. (let ((res (plist-get org-agenda-info prop)))
  18022. (setq res
  18023. (cond
  18024. ((not res) "")
  18025. ((stringp res) res)
  18026. (t (prin1-to-string res))))
  18027. (while (string-match "," res)
  18028. (setq res (replace-match ";" t t res)))
  18029. (org-trim res)))
  18030. ;;;###autoload
  18031. (defun org-store-agenda-views (&rest parameters)
  18032. (interactive)
  18033. (eval (list 'org-batch-store-agenda-views)))
  18034. ;; FIXME, why is this a macro?????
  18035. ;;;###autoload
  18036. (defmacro org-batch-store-agenda-views (&rest parameters)
  18037. "Run all custom agenda commands that have a file argument."
  18038. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18039. (pop-up-frames nil)
  18040. (dir default-directory)
  18041. pars cmd thiscmdkey files opts)
  18042. (while parameters
  18043. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18044. (setq pars (reverse pars))
  18045. (save-window-excursion
  18046. (while cmds
  18047. (setq cmd (pop cmds)
  18048. thiscmdkey (car cmd)
  18049. opts (nth 4 cmd)
  18050. files (nth 5 cmd))
  18051. (if (stringp files) (setq files (list files)))
  18052. (when files
  18053. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18054. (list 'org-agenda nil thiscmdkey)))
  18055. (set-buffer org-agenda-buffer-name)
  18056. (while files
  18057. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18058. (list 'org-write-agenda
  18059. (expand-file-name (pop files) dir) t))))
  18060. (and (get-buffer org-agenda-buffer-name)
  18061. (kill-buffer org-agenda-buffer-name)))))))
  18062. (defun org-write-agenda (file &optional nosettings)
  18063. "Write the current buffer (an agenda view) as a file.
  18064. Depending on the extension of the file name, plain text (.txt),
  18065. HTML (.html or .htm) or Postscript (.ps) is produced.
  18066. If NOSETTINGS is given, do not scope the settings of
  18067. `org-agenda-exporter-settings' into the export commands. This is used when
  18068. the settings have already been scoped and we do not wish to overrule other,
  18069. higher priority settings."
  18070. (interactive "FWrite agenda to file: ")
  18071. (if (not (file-writable-p file))
  18072. (error "Cannot write agenda to file %s" file))
  18073. (cond
  18074. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18075. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18076. (org-let (if nosettings nil org-agenda-exporter-settings)
  18077. '(save-excursion
  18078. (save-window-excursion
  18079. (cond
  18080. ((string-match "\\.html?\\'" file)
  18081. (set-buffer (htmlize-buffer (current-buffer)))
  18082. (when (and org-agenda-export-html-style
  18083. (string-match "<style>" org-agenda-export-html-style))
  18084. ;; replace <style> section with org-agenda-export-html-style
  18085. (goto-char (point-min))
  18086. (kill-region (- (search-forward "<style") 6)
  18087. (search-forward "</style>"))
  18088. (insert org-agenda-export-html-style))
  18089. (write-file file)
  18090. (kill-buffer (current-buffer))
  18091. (message "HTML written to %s" file))
  18092. ((string-match "\\.ps\\'" file)
  18093. (ps-print-buffer-with-faces file)
  18094. (message "Postscript written to %s" file))
  18095. (t
  18096. (let ((bs (buffer-string)))
  18097. (find-file file)
  18098. (insert bs)
  18099. (save-buffer 0)
  18100. (kill-buffer (current-buffer))
  18101. (message "Plain text written to %s" file))))))
  18102. (set-buffer org-agenda-buffer-name)))
  18103. (defmacro org-no-read-only (&rest body)
  18104. "Inhibit read-only for BODY."
  18105. `(let ((inhibit-read-only t)) ,@body))
  18106. (defun org-check-for-org-mode ()
  18107. "Make sure current buffer is in org-mode. Error if not."
  18108. (or (org-mode-p)
  18109. (error "Cannot execute org-mode agenda command on buffer in %s."
  18110. major-mode)))
  18111. (defun org-fit-agenda-window ()
  18112. "Fit the window to the buffer size."
  18113. (and (memq org-agenda-window-setup '(reorganize-frame))
  18114. (fboundp 'fit-window-to-buffer)
  18115. (fit-window-to-buffer
  18116. nil
  18117. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18118. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18119. ;;; Agenda file list
  18120. (defun org-agenda-files (&optional unrestricted)
  18121. "Get the list of agenda files.
  18122. Optional UNRESTRICTED means return the full list even if a restriction
  18123. is currently in place."
  18124. (let ((files
  18125. (cond
  18126. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18127. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18128. ((listp org-agenda-files) org-agenda-files)
  18129. (t (error "Invalid value of `org-agenda-files'")))))
  18130. (setq files (apply 'append
  18131. (mapcar (lambda (f)
  18132. (if (file-directory-p f)
  18133. (directory-files f t
  18134. org-agenda-file-regexp)
  18135. (list f)))
  18136. files)))
  18137. (if org-agenda-skip-unavailable-files
  18138. (delq nil
  18139. (mapcar (function
  18140. (lambda (file)
  18141. (and (file-readable-p file) file)))
  18142. files))
  18143. files))) ; `org-check-agenda-file' will remove them from the list
  18144. (defun org-edit-agenda-file-list ()
  18145. "Edit the list of agenda files.
  18146. Depending on setup, this either uses customize to edit the variable
  18147. `org-agenda-files', or it visits the file that is holding the list. In the
  18148. latter case, the buffer is set up in a way that saving it automatically kills
  18149. the buffer and restores the previous window configuration."
  18150. (interactive)
  18151. (if (stringp org-agenda-files)
  18152. (let ((cw (current-window-configuration)))
  18153. (find-file org-agenda-files)
  18154. (org-set-local 'org-window-configuration cw)
  18155. (org-add-hook 'after-save-hook
  18156. (lambda ()
  18157. (set-window-configuration
  18158. (prog1 org-window-configuration
  18159. (kill-buffer (current-buffer))))
  18160. (org-install-agenda-files-menu)
  18161. (message "New agenda file list installed"))
  18162. nil 'local)
  18163. (message "%s" (substitute-command-keys
  18164. "Edit list and finish with \\[save-buffer]")))
  18165. (customize-variable 'org-agenda-files)))
  18166. (defun org-store-new-agenda-file-list (list)
  18167. "Set new value for the agenda file list and save it correcly."
  18168. (if (stringp org-agenda-files)
  18169. (let ((f org-agenda-files) b)
  18170. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18171. (with-temp-file f
  18172. (insert (mapconcat 'identity list "\n") "\n")))
  18173. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18174. (setq org-agenda-files list)
  18175. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18176. (defun org-read-agenda-file-list ()
  18177. "Read the list of agenda files from a file."
  18178. (when (stringp org-agenda-files)
  18179. (with-temp-buffer
  18180. (insert-file-contents org-agenda-files)
  18181. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18182. ;;;###autoload
  18183. (defun org-cycle-agenda-files ()
  18184. "Cycle through the files in `org-agenda-files'.
  18185. If the current buffer visits an agenda file, find the next one in the list.
  18186. If the current buffer does not, find the first agenda file."
  18187. (interactive)
  18188. (let* ((fs (org-agenda-files t))
  18189. (files (append fs (list (car fs))))
  18190. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18191. file)
  18192. (unless files (error "No agenda files"))
  18193. (catch 'exit
  18194. (while (setq file (pop files))
  18195. (if (equal (file-truename file) tcf)
  18196. (when (car files)
  18197. (find-file (car files))
  18198. (throw 'exit t))))
  18199. (find-file (car fs)))
  18200. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18201. (defun org-agenda-file-to-front (&optional to-end)
  18202. "Move/add the current file to the top of the agenda file list.
  18203. If the file is not present in the list, it is added to the front. If it is
  18204. present, it is moved there. With optional argument TO-END, add/move to the
  18205. end of the list."
  18206. (interactive "P")
  18207. (let ((org-agenda-skip-unavailable-files nil)
  18208. (file-alist (mapcar (lambda (x)
  18209. (cons (file-truename x) x))
  18210. (org-agenda-files t)))
  18211. (ctf (file-truename buffer-file-name))
  18212. x had)
  18213. (setq x (assoc ctf file-alist) had x)
  18214. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18215. (if to-end
  18216. (setq file-alist (append (delq x file-alist) (list x)))
  18217. (setq file-alist (cons x (delq x file-alist))))
  18218. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18219. (org-install-agenda-files-menu)
  18220. (message "File %s to %s of agenda file list"
  18221. (if had "moved" "added") (if to-end "end" "front"))))
  18222. (defun org-remove-file (&optional file)
  18223. "Remove current file from the list of files in variable `org-agenda-files'.
  18224. These are the files which are being checked for agenda entries.
  18225. Optional argument FILE means, use this file instead of the current."
  18226. (interactive)
  18227. (let* ((org-agenda-skip-unavailable-files nil)
  18228. (file (or file buffer-file-name))
  18229. (true-file (file-truename file))
  18230. (afile (abbreviate-file-name file))
  18231. (files (delq nil (mapcar
  18232. (lambda (x)
  18233. (if (equal true-file
  18234. (file-truename x))
  18235. nil x))
  18236. (org-agenda-files t)))))
  18237. (if (not (= (length files) (length (org-agenda-files t))))
  18238. (progn
  18239. (org-store-new-agenda-file-list files)
  18240. (org-install-agenda-files-menu)
  18241. (message "Removed file: %s" afile))
  18242. (message "File was not in list: %s (not removed)" afile))))
  18243. (defun org-file-menu-entry (file)
  18244. (vector file (list 'find-file file) t))
  18245. (defun org-check-agenda-file (file)
  18246. "Make sure FILE exists. If not, ask user what to do."
  18247. (when (not (file-exists-p file))
  18248. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18249. (abbreviate-file-name file))
  18250. (let ((r (downcase (read-char-exclusive))))
  18251. (cond
  18252. ((equal r ?r)
  18253. (org-remove-file file)
  18254. (throw 'nextfile t))
  18255. (t (error "Abort"))))))
  18256. ;;; Agenda prepare and finalize
  18257. (defvar org-agenda-multi nil) ; dynammically scoped
  18258. (defvar org-agenda-buffer-name "*Org Agenda*")
  18259. (defvar org-pre-agenda-window-conf nil)
  18260. (defvar org-agenda-name nil)
  18261. (defun org-prepare-agenda (&optional name)
  18262. (setq org-todo-keywords-for-agenda nil)
  18263. (setq org-done-keywords-for-agenda nil)
  18264. (if org-agenda-multi
  18265. (progn
  18266. (setq buffer-read-only nil)
  18267. (goto-char (point-max))
  18268. (unless (or (bobp) org-agenda-compact-blocks)
  18269. (insert "\n" (make-string (window-width) ?=) "\n"))
  18270. (narrow-to-region (point) (point-max)))
  18271. (org-agenda-maybe-reset-markers 'force)
  18272. (org-prepare-agenda-buffers (org-agenda-files))
  18273. (setq org-todo-keywords-for-agenda
  18274. (org-uniquify org-todo-keywords-for-agenda))
  18275. (setq org-done-keywords-for-agenda
  18276. (org-uniquify org-done-keywords-for-agenda))
  18277. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18278. (awin (get-buffer-window abuf)))
  18279. (cond
  18280. ((equal (current-buffer) abuf) nil)
  18281. (awin (select-window awin))
  18282. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18283. ((equal org-agenda-window-setup 'current-window)
  18284. (switch-to-buffer abuf))
  18285. ((equal org-agenda-window-setup 'other-window)
  18286. (org-switch-to-buffer-other-window abuf))
  18287. ((equal org-agenda-window-setup 'other-frame)
  18288. (switch-to-buffer-other-frame abuf))
  18289. ((equal org-agenda-window-setup 'reorganize-frame)
  18290. (delete-other-windows)
  18291. (org-switch-to-buffer-other-window abuf))))
  18292. (setq buffer-read-only nil)
  18293. (erase-buffer)
  18294. (org-agenda-mode)
  18295. (and name (not org-agenda-name)
  18296. (org-set-local 'org-agenda-name name)))
  18297. (setq buffer-read-only nil))
  18298. (defun org-finalize-agenda ()
  18299. "Finishing touch for the agenda buffer, called just before displaying it."
  18300. (unless org-agenda-multi
  18301. (save-excursion
  18302. (let ((inhibit-read-only t))
  18303. (goto-char (point-min))
  18304. (while (org-activate-bracket-links (point-max))
  18305. (add-text-properties (match-beginning 0) (match-end 0)
  18306. '(face org-link)))
  18307. (org-agenda-align-tags)
  18308. (unless org-agenda-with-colors
  18309. (remove-text-properties (point-min) (point-max) '(face nil))))
  18310. (if (and (boundp 'org-overriding-columns-format)
  18311. org-overriding-columns-format)
  18312. (org-set-local 'org-overriding-columns-format
  18313. org-overriding-columns-format))
  18314. (if (and (boundp 'org-agenda-view-columns-initially)
  18315. org-agenda-view-columns-initially)
  18316. (org-agenda-columns))
  18317. (when org-agenda-fontify-priorities
  18318. (org-fontify-priorities))
  18319. (run-hooks 'org-finalize-agenda-hook))))
  18320. (defun org-fontify-priorities ()
  18321. "Make highest priority lines bold, and lowest italic."
  18322. (interactive)
  18323. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18324. (org-delete-overlay o)))
  18325. (org-overlays-in (point-min) (point-max)))
  18326. (save-excursion
  18327. (let ((inhibit-read-only t)
  18328. b e p ov h l)
  18329. (goto-char (point-min))
  18330. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18331. (setq h (or (get-char-property (point) 'org-highest-priority)
  18332. org-highest-priority)
  18333. l (or (get-char-property (point) 'org-lowest-priority)
  18334. org-lowest-priority)
  18335. p (string-to-char (match-string 1))
  18336. b (match-beginning 0) e (point-at-eol)
  18337. ov (org-make-overlay b e))
  18338. (org-overlay-put
  18339. ov 'face
  18340. (cond ((listp org-agenda-fontify-priorities)
  18341. (cdr (assoc p org-agenda-fontify-priorities)))
  18342. ((equal p l) 'italic)
  18343. ((equal p h) 'bold)))
  18344. (org-overlay-put ov 'org-type 'org-priority)))))
  18345. (defun org-prepare-agenda-buffers (files)
  18346. "Create buffers for all agenda files, protect archived trees and comments."
  18347. (interactive)
  18348. (let ((pa '(:org-archived t))
  18349. (pc '(:org-comment t))
  18350. (pall '(:org-archived t :org-comment t))
  18351. (inhibit-read-only t)
  18352. (rea (concat ":" org-archive-tag ":"))
  18353. bmp file re)
  18354. (save-excursion
  18355. (save-restriction
  18356. (while (setq file (pop files))
  18357. (if (bufferp file)
  18358. (set-buffer file)
  18359. (org-check-agenda-file file)
  18360. (set-buffer (org-get-agenda-file-buffer file)))
  18361. (widen)
  18362. (setq bmp (buffer-modified-p))
  18363. (org-refresh-category-properties)
  18364. (setq org-todo-keywords-for-agenda
  18365. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18366. (setq org-done-keywords-for-agenda
  18367. (append org-done-keywords-for-agenda org-done-keywords))
  18368. (save-excursion
  18369. (remove-text-properties (point-min) (point-max) pall)
  18370. (when org-agenda-skip-archived-trees
  18371. (goto-char (point-min))
  18372. (while (re-search-forward rea nil t)
  18373. (if (org-on-heading-p t)
  18374. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18375. (goto-char (point-min))
  18376. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18377. (while (re-search-forward re nil t)
  18378. (add-text-properties
  18379. (match-beginning 0) (org-end-of-subtree t) pc)))
  18380. (set-buffer-modified-p bmp))))))
  18381. (defvar org-agenda-skip-function nil
  18382. "Function to be called at each match during agenda construction.
  18383. If this function returns nil, the current match should not be skipped.
  18384. Otherwise, the function must return a position from where the search
  18385. should be continued.
  18386. This may also be a Lisp form, it will be evaluated.
  18387. Never set this variable using `setq' or so, because then it will apply
  18388. to all future agenda commands. Instead, bind it with `let' to scope
  18389. it dynamically into the agenda-constructing command. A good way to set
  18390. it is through options in org-agenda-custom-commands.")
  18391. (defun org-agenda-skip ()
  18392. "Throw to `:skip' in places that should be skipped.
  18393. Also moves point to the end of the skipped region, so that search can
  18394. continue from there."
  18395. (let ((p (point-at-bol)) to fp)
  18396. (and org-agenda-skip-archived-trees
  18397. (get-text-property p :org-archived)
  18398. (org-end-of-subtree t)
  18399. (throw :skip t))
  18400. (and (get-text-property p :org-comment)
  18401. (org-end-of-subtree t)
  18402. (throw :skip t))
  18403. (if (equal (char-after p) ?#) (throw :skip t))
  18404. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18405. (consp org-agenda-skip-function))
  18406. (setq to (save-excursion
  18407. (save-match-data
  18408. (if fp
  18409. (funcall org-agenda-skip-function)
  18410. (eval org-agenda-skip-function))))))
  18411. (goto-char to)
  18412. (throw :skip t))))
  18413. (defvar org-agenda-markers nil
  18414. "List of all currently active markers created by `org-agenda'.")
  18415. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18416. "Creation time of the last agenda marker.")
  18417. (defun org-agenda-new-marker (&optional pos)
  18418. "Return a new agenda marker.
  18419. Org-mode keeps a list of these markers and resets them when they are
  18420. no longer in use."
  18421. (let ((m (copy-marker (or pos (point)))))
  18422. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18423. (push m org-agenda-markers)
  18424. m))
  18425. (defun org-agenda-maybe-reset-markers (&optional force)
  18426. "Reset markers created by `org-agenda'. But only if they are old enough."
  18427. (if (or (and force (not org-agenda-multi))
  18428. (> (- (time-to-seconds (current-time))
  18429. org-agenda-last-marker-time)
  18430. 5))
  18431. (while org-agenda-markers
  18432. (move-marker (pop org-agenda-markers) nil))))
  18433. (defun org-get-agenda-file-buffer (file)
  18434. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18435. it to the list of buffers which might be released later."
  18436. (let ((buf (org-find-base-buffer-visiting file)))
  18437. (if buf
  18438. buf ; just return it
  18439. ;; Make a new buffer and remember it
  18440. (setq buf (find-file-noselect file))
  18441. (if buf (push buf org-agenda-new-buffers))
  18442. buf)))
  18443. (defun org-release-buffers (blist)
  18444. "Release all buffers in list, asking the user for confirmation when needed.
  18445. When a buffer is unmodified, it is just killed. When modified, it is saved
  18446. \(if the user agrees) and then killed."
  18447. (let (buf file)
  18448. (while (setq buf (pop blist))
  18449. (setq file (buffer-file-name buf))
  18450. (when (and (buffer-modified-p buf)
  18451. file
  18452. (y-or-n-p (format "Save file %s? " file)))
  18453. (with-current-buffer buf (save-buffer)))
  18454. (kill-buffer buf))))
  18455. (defun org-get-category (&optional pos)
  18456. "Get the category applying to position POS."
  18457. (get-text-property (or pos (point)) 'org-category))
  18458. ;;; Agenda timeline
  18459. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18460. (defun org-timeline (&optional include-all)
  18461. "Show a time-sorted view of the entries in the current org file.
  18462. Only entries with a time stamp of today or later will be listed. With
  18463. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18464. under the current date.
  18465. If the buffer contains an active region, only check the region for
  18466. dates."
  18467. (interactive "P")
  18468. (require 'calendar)
  18469. (org-compile-prefix-format 'timeline)
  18470. (org-set-sorting-strategy 'timeline)
  18471. (let* ((dopast t)
  18472. (dotodo include-all)
  18473. (doclosed org-agenda-show-log)
  18474. (entry buffer-file-name)
  18475. (date (calendar-current-date))
  18476. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18477. (end (if (org-region-active-p) (region-end) (point-max)))
  18478. (day-numbers (org-get-all-dates beg end 'no-ranges
  18479. t doclosed ; always include today
  18480. org-timeline-show-empty-dates))
  18481. (org-deadline-warning-days 0)
  18482. (org-agenda-only-exact-dates t)
  18483. (today (time-to-days (current-time)))
  18484. (past t)
  18485. args
  18486. s e rtn d emptyp)
  18487. (setq org-agenda-redo-command
  18488. (list 'progn
  18489. (list 'org-switch-to-buffer-other-window (current-buffer))
  18490. (list 'org-timeline (list 'quote include-all))))
  18491. (if (not dopast)
  18492. ;; Remove past dates from the list of dates.
  18493. (setq day-numbers (delq nil (mapcar (lambda(x)
  18494. (if (>= x today) x nil))
  18495. day-numbers))))
  18496. (org-prepare-agenda (concat "Timeline "
  18497. (file-name-nondirectory buffer-file-name)))
  18498. (if doclosed (push :closed args))
  18499. (push :timestamp args)
  18500. (push :deadline args)
  18501. (push :scheduled args)
  18502. (push :sexp args)
  18503. (if dotodo (push :todo args))
  18504. (while (setq d (pop day-numbers))
  18505. (if (and (listp d) (eq (car d) :omitted))
  18506. (progn
  18507. (setq s (point))
  18508. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18509. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18510. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18511. (if (and (>= d today)
  18512. dopast
  18513. past)
  18514. (progn
  18515. (setq past nil)
  18516. (insert (make-string 79 ?-) "\n")))
  18517. (setq date (calendar-gregorian-from-absolute d))
  18518. (setq s (point))
  18519. (setq rtn (and (not emptyp)
  18520. (apply 'org-agenda-get-day-entries entry
  18521. date args)))
  18522. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18523. (progn
  18524. (insert
  18525. (if (stringp org-agenda-format-date)
  18526. (format-time-string org-agenda-format-date
  18527. (org-time-from-absolute date))
  18528. (funcall org-agenda-format-date date))
  18529. "\n")
  18530. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18531. (put-text-property s (1- (point)) 'org-date-line t)
  18532. (if (equal d today)
  18533. (put-text-property s (1- (point)) 'org-today t))
  18534. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18535. (put-text-property s (1- (point)) 'day d)))))
  18536. (goto-char (point-min))
  18537. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18538. (point-min)))
  18539. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18540. (org-finalize-agenda)
  18541. (setq buffer-read-only t)))
  18542. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  18543. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18544. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18545. not every single day in the range. If FORCE-TODAY is non-nil, make
  18546. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18547. inactive time stamps (those in square brackets) are included.
  18548. When EMPTY is non-nil, also include days without any entries."
  18549. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  18550. dates dates1 date day day1 day2 ts1 ts2)
  18551. (if force-today
  18552. (setq dates (list (time-to-days (current-time)))))
  18553. (save-excursion
  18554. (goto-char beg)
  18555. (while (re-search-forward re end t)
  18556. (setq day (time-to-days (org-time-string-to-time
  18557. (substring (match-string 1) 0 10))))
  18558. (or (memq day dates) (push day dates)))
  18559. (unless no-ranges
  18560. (goto-char beg)
  18561. (while (re-search-forward org-tr-regexp end t)
  18562. (setq ts1 (substring (match-string 1) 0 10)
  18563. ts2 (substring (match-string 2) 0 10)
  18564. day1 (time-to-days (org-time-string-to-time ts1))
  18565. day2 (time-to-days (org-time-string-to-time ts2)))
  18566. (while (< (setq day1 (1+ day1)) day2)
  18567. (or (memq day1 dates) (push day1 dates)))))
  18568. (setq dates (sort dates '<))
  18569. (when empty
  18570. (while (setq day (pop dates))
  18571. (setq day2 (car dates))
  18572. (push day dates1)
  18573. (when (and day2 empty)
  18574. (if (or (eq empty t)
  18575. (and (numberp empty) (<= (- day2 day) empty)))
  18576. (while (< (setq day (1+ day)) day2)
  18577. (push (list day) dates1))
  18578. (push (cons :omitted (- day2 day)) dates1))))
  18579. (setq dates (nreverse dates1)))
  18580. dates)))
  18581. ;;; Agenda Daily/Weekly
  18582. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18583. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18584. (defvar org-agenda-last-arguments nil
  18585. "The arguments of the previous call to org-agenda")
  18586. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18587. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18588. (defvar org-include-all-loc nil) ; local variable
  18589. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18590. ;;;###autoload
  18591. (defun org-agenda-list (&optional include-all start-day ndays)
  18592. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18593. The view will be for the current day or week, but from the overview buffer
  18594. you will be able to go to other days/weeks.
  18595. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18596. all unfinished TODO items will also be shown, before the agenda.
  18597. This feature is considered obsolete, please use the TODO list or a block
  18598. agenda instead.
  18599. With a numeric prefix argument in an interactive call, the agenda will
  18600. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18601. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18602. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18603. given in `org-agenda-start-on-weekday'."
  18604. (interactive "P")
  18605. (if (and (integerp include-all) (> include-all 0))
  18606. (setq ndays include-all include-all nil))
  18607. (setq ndays (or ndays org-agenda-ndays)
  18608. start-day (or start-day org-agenda-start-day))
  18609. (if org-agenda-overriding-arguments
  18610. (setq include-all (car org-agenda-overriding-arguments)
  18611. start-day (nth 1 org-agenda-overriding-arguments)
  18612. ndays (nth 2 org-agenda-overriding-arguments)))
  18613. (if (stringp start-day)
  18614. ;; Convert to an absolute day number
  18615. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18616. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18617. (org-compile-prefix-format 'agenda)
  18618. (org-set-sorting-strategy 'agenda)
  18619. (require 'calendar)
  18620. (let* ((org-agenda-start-on-weekday
  18621. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18622. org-agenda-start-on-weekday nil))
  18623. (thefiles (org-agenda-files))
  18624. (files thefiles)
  18625. (today (time-to-days
  18626. (time-subtract (current-time)
  18627. (list 0 (* 3600 org-extend-today-until) 0))))
  18628. (sd (or start-day today))
  18629. (start (if (or (null org-agenda-start-on-weekday)
  18630. (< org-agenda-ndays 7))
  18631. sd
  18632. (let* ((nt (calendar-day-of-week
  18633. (calendar-gregorian-from-absolute sd)))
  18634. (n1 org-agenda-start-on-weekday)
  18635. (d (- nt n1)))
  18636. (- sd (+ (if (< d 0) 7 0) d)))))
  18637. (day-numbers (list start))
  18638. (day-cnt 0)
  18639. (inhibit-redisplay (not debug-on-error))
  18640. s e rtn rtnall file date d start-pos end-pos todayp nd)
  18641. (setq org-agenda-redo-command
  18642. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  18643. ;; Make the list of days
  18644. (setq ndays (or ndays org-agenda-ndays)
  18645. nd ndays)
  18646. (while (> ndays 1)
  18647. (push (1+ (car day-numbers)) day-numbers)
  18648. (setq ndays (1- ndays)))
  18649. (setq day-numbers (nreverse day-numbers))
  18650. (org-prepare-agenda "Day/Week")
  18651. (org-set-local 'org-starting-day (car day-numbers))
  18652. (org-set-local 'org-include-all-loc include-all)
  18653. (org-set-local 'org-agenda-span
  18654. (org-agenda-ndays-to-span nd))
  18655. (when (and (or include-all org-agenda-include-all-todo)
  18656. (member today day-numbers))
  18657. (setq files thefiles
  18658. rtnall nil)
  18659. (while (setq file (pop files))
  18660. (catch 'nextfile
  18661. (org-check-agenda-file file)
  18662. (setq date (calendar-gregorian-from-absolute today)
  18663. rtn (org-agenda-get-day-entries
  18664. file date :todo))
  18665. (setq rtnall (append rtnall rtn))))
  18666. (when rtnall
  18667. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  18668. (add-text-properties (point-min) (1- (point))
  18669. (list 'face 'org-agenda-structure))
  18670. (insert (org-finalize-agenda-entries rtnall) "\n")))
  18671. (unless org-agenda-compact-blocks
  18672. (setq s (point))
  18673. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  18674. "-agenda:\n")
  18675. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  18676. 'org-date-line t)))
  18677. (while (setq d (pop day-numbers))
  18678. (setq date (calendar-gregorian-from-absolute d)
  18679. s (point))
  18680. (if (or (setq todayp (= d today))
  18681. (and (not start-pos) (= d sd)))
  18682. (setq start-pos (point))
  18683. (if (and start-pos (not end-pos))
  18684. (setq end-pos (point))))
  18685. (setq files thefiles
  18686. rtnall nil)
  18687. (while (setq file (pop files))
  18688. (catch 'nextfile
  18689. (org-check-agenda-file file)
  18690. (if org-agenda-show-log
  18691. (setq rtn (org-agenda-get-day-entries
  18692. file date
  18693. :deadline :scheduled :timestamp :sexp :closed))
  18694. (setq rtn (org-agenda-get-day-entries
  18695. file date
  18696. :deadline :scheduled :sexp :timestamp)))
  18697. (setq rtnall (append rtnall rtn))))
  18698. (if org-agenda-include-diary
  18699. (progn
  18700. (require 'diary-lib)
  18701. (setq rtn (org-get-entries-from-diary date))
  18702. (setq rtnall (append rtnall rtn))))
  18703. (if (or rtnall org-agenda-show-all-dates)
  18704. (progn
  18705. (setq day-cnt (1+ day-cnt))
  18706. (insert
  18707. (if (stringp org-agenda-format-date)
  18708. (format-time-string org-agenda-format-date
  18709. (org-time-from-absolute date))
  18710. (funcall org-agenda-format-date date))
  18711. "\n")
  18712. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18713. (put-text-property s (1- (point)) 'org-date-line t)
  18714. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  18715. (if todayp (put-text-property s (1- (point)) 'org-today t))
  18716. (if rtnall (insert
  18717. (org-finalize-agenda-entries
  18718. (org-agenda-add-time-grid-maybe
  18719. rtnall nd todayp))
  18720. "\n"))
  18721. (put-text-property s (1- (point)) 'day d)
  18722. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  18723. (goto-char (point-min))
  18724. (org-fit-agenda-window)
  18725. (unless (and (pos-visible-in-window-p (point-min))
  18726. (pos-visible-in-window-p (point-max)))
  18727. (goto-char (1- (point-max)))
  18728. (recenter -1)
  18729. (if (not (pos-visible-in-window-p (or start-pos 1)))
  18730. (progn
  18731. (goto-char (or start-pos 1))
  18732. (recenter 1))))
  18733. (goto-char (or start-pos 1))
  18734. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  18735. (org-finalize-agenda)
  18736. (setq buffer-read-only t)
  18737. (message "")))
  18738. (defun org-agenda-ndays-to-span (n)
  18739. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  18740. ;;; Agenda TODO list
  18741. (defvar org-select-this-todo-keyword nil)
  18742. (defvar org-last-arg nil)
  18743. ;;;###autoload
  18744. (defun org-todo-list (arg)
  18745. "Show all TODO entries from all agenda file in a single list.
  18746. The prefix arg can be used to select a specific TODO keyword and limit
  18747. the list to these. When using \\[universal-argument], you will be prompted
  18748. for a keyword. A numeric prefix directly selects the Nth keyword in
  18749. `org-todo-keywords-1'."
  18750. (interactive "P")
  18751. (require 'calendar)
  18752. (org-compile-prefix-format 'todo)
  18753. (org-set-sorting-strategy 'todo)
  18754. (org-prepare-agenda "TODO")
  18755. (let* ((today (time-to-days (current-time)))
  18756. (date (calendar-gregorian-from-absolute today))
  18757. (kwds org-todo-keywords-for-agenda)
  18758. (completion-ignore-case t)
  18759. (org-select-this-todo-keyword
  18760. (if (stringp arg) arg
  18761. (and arg (integerp arg) (> arg 0)
  18762. (nth (1- arg) kwds))))
  18763. rtn rtnall files file pos)
  18764. (when (equal arg '(4))
  18765. (setq org-select-this-todo-keyword
  18766. (completing-read "Keyword (or KWD1|K2D2|...): "
  18767. (mapcar 'list kwds) nil nil)))
  18768. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  18769. (org-set-local 'org-last-arg arg)
  18770. (setq org-agenda-redo-command
  18771. '(org-todo-list (or current-prefix-arg org-last-arg)))
  18772. (setq files (org-agenda-files)
  18773. rtnall nil)
  18774. (while (setq file (pop files))
  18775. (catch 'nextfile
  18776. (org-check-agenda-file file)
  18777. (setq rtn (org-agenda-get-day-entries file date :todo))
  18778. (setq rtnall (append rtnall rtn))))
  18779. (if org-agenda-overriding-header
  18780. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18781. nil 'face 'org-agenda-structure) "\n")
  18782. (insert "Global list of TODO items of type: ")
  18783. (add-text-properties (point-min) (1- (point))
  18784. (list 'face 'org-agenda-structure))
  18785. (setq pos (point))
  18786. (insert (or org-select-this-todo-keyword "ALL") "\n")
  18787. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  18788. (setq pos (point))
  18789. (unless org-agenda-multi
  18790. (insert "Available with `N r': (0)ALL")
  18791. (let ((n 0) s)
  18792. (mapc (lambda (x)
  18793. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  18794. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  18795. (insert "\n "))
  18796. (insert " " s))
  18797. kwds))
  18798. (insert "\n"))
  18799. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  18800. (when rtnall
  18801. (insert (org-finalize-agenda-entries rtnall) "\n"))
  18802. (goto-char (point-min))
  18803. (org-fit-agenda-window)
  18804. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  18805. (org-finalize-agenda)
  18806. (setq buffer-read-only t)))
  18807. ;;; Agenda tags match
  18808. ;;;###autoload
  18809. (defun org-tags-view (&optional todo-only match)
  18810. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  18811. The prefix arg TODO-ONLY limits the search to TODO entries."
  18812. (interactive "P")
  18813. (org-compile-prefix-format 'tags)
  18814. (org-set-sorting-strategy 'tags)
  18815. (let* ((org-tags-match-list-sublevels
  18816. (if todo-only t org-tags-match-list-sublevels))
  18817. (completion-ignore-case t)
  18818. rtn rtnall files file pos matcher
  18819. buffer)
  18820. (setq matcher (org-make-tags-matcher match)
  18821. match (car matcher) matcher (cdr matcher))
  18822. (org-prepare-agenda (concat "TAGS " match))
  18823. (setq org-agenda-redo-command
  18824. (list 'org-tags-view (list 'quote todo-only)
  18825. (list 'if 'current-prefix-arg nil match)))
  18826. (setq files (org-agenda-files)
  18827. rtnall nil)
  18828. (while (setq file (pop files))
  18829. (catch 'nextfile
  18830. (org-check-agenda-file file)
  18831. (setq buffer (if (file-exists-p file)
  18832. (org-get-agenda-file-buffer file)
  18833. (error "No such file %s" file)))
  18834. (if (not buffer)
  18835. ;; If file does not exist, merror message to agenda
  18836. (setq rtn (list
  18837. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  18838. rtnall (append rtnall rtn))
  18839. (with-current-buffer buffer
  18840. (unless (org-mode-p)
  18841. (error "Agenda file %s is not in `org-mode'" file))
  18842. (save-excursion
  18843. (save-restriction
  18844. (if org-agenda-restrict
  18845. (narrow-to-region org-agenda-restrict-begin
  18846. org-agenda-restrict-end)
  18847. (widen))
  18848. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  18849. (setq rtnall (append rtnall rtn))))))))
  18850. (if org-agenda-overriding-header
  18851. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18852. nil 'face 'org-agenda-structure) "\n")
  18853. (insert "Headlines with TAGS match: ")
  18854. (add-text-properties (point-min) (1- (point))
  18855. (list 'face 'org-agenda-structure))
  18856. (setq pos (point))
  18857. (insert match "\n")
  18858. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  18859. (setq pos (point))
  18860. (unless org-agenda-multi
  18861. (insert "Press `C-u r' to search again with new search string\n"))
  18862. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  18863. (when rtnall
  18864. (insert (org-finalize-agenda-entries rtnall) "\n"))
  18865. (goto-char (point-min))
  18866. (org-fit-agenda-window)
  18867. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  18868. (org-finalize-agenda)
  18869. (setq buffer-read-only t)))
  18870. ;;; Agenda Finding stuck projects
  18871. (defvar org-agenda-skip-regexp nil
  18872. "Regular expression used in skipping subtrees for the agenda.
  18873. This is basically a temporary global variable that can be set and then
  18874. used by user-defined selections using `org-agenda-skip-function'.")
  18875. (defvar org-agenda-overriding-header nil
  18876. "When this is set during todo and tags searches, will replace header.")
  18877. (defun org-agenda-skip-subtree-when-regexp-matches ()
  18878. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  18879. If yes, it returns the end position of this tree, causing agenda commands
  18880. to skip this subtree. This is a function that can be put into
  18881. `org-agenda-skip-function' for the duration of a command."
  18882. (let ((end (save-excursion (org-end-of-subtree t)))
  18883. skip)
  18884. (save-excursion
  18885. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  18886. (and skip end)))
  18887. (defun org-agenda-skip-entry-if (&rest conditions)
  18888. "Skip entry if any of CONDITIONS is true.
  18889. See `org-agenda-skip-if' for details."
  18890. (org-agenda-skip-if nil conditions))
  18891. (defun org-agenda-skip-subtree-if (&rest conditions)
  18892. "Skip entry if any of CONDITIONS is true.
  18893. See `org-agenda-skip-if' for details."
  18894. (org-agenda-skip-if t conditions))
  18895. (defun org-agenda-skip-if (subtree conditions)
  18896. "Checks current entity for CONDITIONS.
  18897. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  18898. the entry, i.e. the text before the next heading is checked.
  18899. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  18900. from different tests. Valid conditions are:
  18901. scheduled Check if there is a scheduled cookie
  18902. notscheduled Check if there is no scheduled cookie
  18903. deadline Check if there is a deadline
  18904. notdeadline Check if there is no deadline
  18905. regexp Check if regexp matches
  18906. notregexp Check if regexp does not match.
  18907. The regexp is taken from the conditions list, it must come right after
  18908. the `regexp' or `notregexp' element.
  18909. If any of these conditions is met, this function returns the end point of
  18910. the entity, causing the search to continue from there. This is a function
  18911. that can be put into `org-agenda-skip-function' for the duration of a command."
  18912. (let (beg end m)
  18913. (org-back-to-heading t)
  18914. (setq beg (point)
  18915. end (if subtree
  18916. (progn (org-end-of-subtree t) (point))
  18917. (progn (outline-next-heading) (1- (point)))))
  18918. (goto-char beg)
  18919. (and
  18920. (or
  18921. (and (memq 'scheduled conditions)
  18922. (re-search-forward org-scheduled-time-regexp end t))
  18923. (and (memq 'notscheduled conditions)
  18924. (not (re-search-forward org-scheduled-time-regexp end t)))
  18925. (and (memq 'deadline conditions)
  18926. (re-search-forward org-deadline-time-regexp end t))
  18927. (and (memq 'notdeadline conditions)
  18928. (not (re-search-forward org-deadline-time-regexp end t)))
  18929. (and (setq m (memq 'regexp conditions))
  18930. (stringp (nth 1 m))
  18931. (re-search-forward (nth 1 m) end t))
  18932. (and (setq m (memq 'notregexp conditions))
  18933. (stringp (nth 1 m))
  18934. (not (re-search-forward (nth 1 m) end t))))
  18935. end)))
  18936. ;;;###autoload
  18937. (defun org-agenda-list-stuck-projects (&rest ignore)
  18938. "Create agenda view for projects that are stuck.
  18939. Stuck projects are project that have no next actions. For the definitions
  18940. of what a project is and how to check if it stuck, customize the variable
  18941. `org-stuck-projects'.
  18942. MATCH is being ignored."
  18943. (interactive)
  18944. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  18945. ;; FIXME: we could have used org-agenda-skip-if here.
  18946. (org-agenda-overriding-header "List of stuck projects: ")
  18947. (matcher (nth 0 org-stuck-projects))
  18948. (todo (nth 1 org-stuck-projects))
  18949. (todo-wds (if (member "*" todo)
  18950. (progn
  18951. (org-prepare-agenda-buffers (org-agenda-files))
  18952. (org-delete-all
  18953. org-done-keywords-for-agenda
  18954. (copy-sequence org-todo-keywords-for-agenda)))
  18955. todo))
  18956. (todo-re (concat "^\\*+[ \t]+\\("
  18957. (mapconcat 'identity todo-wds "\\|")
  18958. "\\)\\>"))
  18959. (tags (nth 2 org-stuck-projects))
  18960. (tags-re (if (member "*" tags)
  18961. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  18962. (concat "^\\*+ .*:\\("
  18963. (mapconcat 'identity tags "\\|")
  18964. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  18965. (gen-re (nth 3 org-stuck-projects))
  18966. (re-list
  18967. (delq nil
  18968. (list
  18969. (if todo todo-re)
  18970. (if tags tags-re)
  18971. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  18972. gen-re)))))
  18973. (setq org-agenda-skip-regexp
  18974. (if re-list
  18975. (mapconcat 'identity re-list "\\|")
  18976. (error "No information how to identify unstuck projects")))
  18977. (org-tags-view nil matcher)
  18978. (with-current-buffer org-agenda-buffer-name
  18979. (setq org-agenda-redo-command
  18980. '(org-agenda-list-stuck-projects
  18981. (or current-prefix-arg org-last-arg))))))
  18982. ;;; Diary integration
  18983. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  18984. (defun org-get-entries-from-diary (date)
  18985. "Get the (Emacs Calendar) diary entries for DATE."
  18986. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  18987. (diary-display-hook '(fancy-diary-display))
  18988. (pop-up-frames nil)
  18989. (list-diary-entries-hook
  18990. (cons 'org-diary-default-entry list-diary-entries-hook))
  18991. (diary-file-name-prefix-function nil) ; turn this feature off
  18992. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  18993. entries
  18994. (org-disable-agenda-to-diary t))
  18995. (save-excursion
  18996. (save-window-excursion
  18997. (funcall (if (fboundp 'diary-list-entries)
  18998. 'diary-list-entries 'list-diary-entries)
  18999. date 1)))
  19000. (if (not (get-buffer fancy-diary-buffer))
  19001. (setq entries nil)
  19002. (with-current-buffer fancy-diary-buffer
  19003. (setq buffer-read-only nil)
  19004. (if (zerop (buffer-size))
  19005. ;; No entries
  19006. (setq entries nil)
  19007. ;; Omit the date and other unnecessary stuff
  19008. (org-agenda-cleanup-fancy-diary)
  19009. ;; Add prefix to each line and extend the text properties
  19010. (if (zerop (buffer-size))
  19011. (setq entries nil)
  19012. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19013. (set-buffer-modified-p nil)
  19014. (kill-buffer fancy-diary-buffer)))
  19015. (when entries
  19016. (setq entries (org-split-string entries "\n"))
  19017. (setq entries
  19018. (mapcar
  19019. (lambda (x)
  19020. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19021. ;; Extend the text properties to the beginning of the line
  19022. (org-add-props x (text-properties-at (1- (length x)) x)
  19023. 'type "diary" 'date date))
  19024. entries)))))
  19025. (defun org-agenda-cleanup-fancy-diary ()
  19026. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19027. This gets rid of the date, the underline under the date, and
  19028. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19029. date. It also removes lines that contain only whitespace."
  19030. (goto-char (point-min))
  19031. (if (looking-at ".*?:[ \t]*")
  19032. (progn
  19033. (replace-match "")
  19034. (re-search-forward "\n=+$" nil t)
  19035. (replace-match "")
  19036. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19037. (re-search-forward "\n=+$" nil t)
  19038. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19039. (goto-char (point-min))
  19040. (while (re-search-forward "^ +\n" nil t)
  19041. (replace-match ""))
  19042. (goto-char (point-min))
  19043. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19044. (replace-match "")))
  19045. ;; Make sure entries from the diary have the right text properties.
  19046. (eval-after-load "diary-lib"
  19047. '(if (boundp 'diary-modify-entry-list-string-function)
  19048. ;; We can rely on the hook, nothing to do
  19049. nil
  19050. ;; Hook not avaiable, must use advice to make this work
  19051. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19052. "Make the position visible."
  19053. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19054. (stringp string)
  19055. buffer-file-name)
  19056. (setq string (org-modify-diary-entry-string string))))))
  19057. (defun org-modify-diary-entry-string (string)
  19058. "Add text properties to string, allowing org-mode to act on it."
  19059. (org-add-props string nil
  19060. 'mouse-face 'highlight
  19061. 'keymap org-agenda-keymap
  19062. 'help-echo (if buffer-file-name
  19063. (format "mouse-2 or RET jump to diary file %s"
  19064. (abbreviate-file-name buffer-file-name))
  19065. "")
  19066. 'org-agenda-diary-link t
  19067. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19068. (defun org-diary-default-entry ()
  19069. "Add a dummy entry to the diary.
  19070. Needed to avoid empty dates which mess up holiday display."
  19071. ;; Catch the error if dealing with the new add-to-diary-alist
  19072. (when org-disable-agenda-to-diary
  19073. (condition-case nil
  19074. (add-to-diary-list original-date "Org-mode dummy" "")
  19075. (error
  19076. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19077. ;;;###autoload
  19078. (defun org-diary (&rest args)
  19079. "Return diary information from org-files.
  19080. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19081. It accesses org files and extracts information from those files to be
  19082. listed in the diary. The function accepts arguments specifying what
  19083. items should be listed. The following arguments are allowed:
  19084. :timestamp List the headlines of items containing a date stamp or
  19085. date range matching the selected date. Deadlines will
  19086. also be listed, on the expiration day.
  19087. :sexp List entries resulting from diary-like sexps.
  19088. :deadline List any deadlines past due, or due within
  19089. `org-deadline-warning-days'. The listing occurs only
  19090. in the diary for *today*, not at any other date. If
  19091. an entry is marked DONE, it is no longer listed.
  19092. :scheduled List all items which are scheduled for the given date.
  19093. The diary for *today* also contains items which were
  19094. scheduled earlier and are not yet marked DONE.
  19095. :todo List all TODO items from the org-file. This may be a
  19096. long list - so this is not turned on by default.
  19097. Like deadlines, these entries only show up in the
  19098. diary for *today*, not at any other date.
  19099. The call in the diary file should look like this:
  19100. &%%(org-diary) ~/path/to/some/orgfile.org
  19101. Use a separate line for each org file to check. Or, if you omit the file name,
  19102. all files listed in `org-agenda-files' will be checked automatically:
  19103. &%%(org-diary)
  19104. If you don't give any arguments (as in the example above), the default
  19105. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19106. So the example above may also be written as
  19107. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19108. The function expects the lisp variables `entry' and `date' to be provided
  19109. by the caller, because this is how the calendar works. Don't use this
  19110. function from a program - use `org-agenda-get-day-entries' instead."
  19111. (org-agenda-maybe-reset-markers)
  19112. (org-compile-prefix-format 'agenda)
  19113. (org-set-sorting-strategy 'agenda)
  19114. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19115. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19116. (list entry)
  19117. (org-agenda-files t)))
  19118. file rtn results)
  19119. (org-prepare-agenda-buffers files)
  19120. ;; If this is called during org-agenda, don't return any entries to
  19121. ;; the calendar. Org Agenda will list these entries itself.
  19122. (if org-disable-agenda-to-diary (setq files nil))
  19123. (while (setq file (pop files))
  19124. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19125. (setq results (append results rtn)))
  19126. (if results
  19127. (concat (org-finalize-agenda-entries results) "\n"))))
  19128. ;;; Agenda entry finders
  19129. (defun org-agenda-get-day-entries (file date &rest args)
  19130. "Does the work for `org-diary' and `org-agenda'.
  19131. FILE is the path to a file to be checked for entries. DATE is date like
  19132. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19133. which kind of entries should be extracted. For details about these, see
  19134. the documentation of `org-diary'."
  19135. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19136. (let* ((org-startup-folded nil)
  19137. (org-startup-align-all-tables nil)
  19138. (buffer (if (file-exists-p file)
  19139. (org-get-agenda-file-buffer file)
  19140. (error "No such file %s" file)))
  19141. arg results rtn)
  19142. (if (not buffer)
  19143. ;; If file does not exist, make sure an error message ends up in diary
  19144. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19145. (with-current-buffer buffer
  19146. (unless (org-mode-p)
  19147. (error "Agenda file %s is not in `org-mode'" file))
  19148. (let ((case-fold-search nil))
  19149. (save-excursion
  19150. (save-restriction
  19151. (if org-agenda-restrict
  19152. (narrow-to-region org-agenda-restrict-begin
  19153. org-agenda-restrict-end)
  19154. (widen))
  19155. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19156. (while (setq arg (pop args))
  19157. (cond
  19158. ((and (eq arg :todo)
  19159. (equal date (calendar-current-date)))
  19160. (setq rtn (org-agenda-get-todos))
  19161. (setq results (append results rtn)))
  19162. ((eq arg :timestamp)
  19163. (setq rtn (org-agenda-get-blocks))
  19164. (setq results (append results rtn))
  19165. (setq rtn (org-agenda-get-timestamps))
  19166. (setq results (append results rtn)))
  19167. ((eq arg :sexp)
  19168. (setq rtn (org-agenda-get-sexps))
  19169. (setq results (append results rtn)))
  19170. ((eq arg :scheduled)
  19171. (setq rtn (org-agenda-get-scheduled))
  19172. (setq results (append results rtn)))
  19173. ((eq arg :closed)
  19174. (setq rtn (org-agenda-get-closed))
  19175. (setq results (append results rtn)))
  19176. ((eq arg :deadline)
  19177. (setq rtn (org-agenda-get-deadlines))
  19178. (setq results (append results rtn))))))))
  19179. results))))
  19180. (defun org-entry-is-todo-p ()
  19181. (member (org-get-todo-state) org-not-done-keywords))
  19182. (defun org-entry-is-done-p ()
  19183. (member (org-get-todo-state) org-done-keywords))
  19184. (defun org-get-todo-state ()
  19185. (save-excursion
  19186. (org-back-to-heading t)
  19187. (and (looking-at org-todo-line-regexp)
  19188. (match-end 2)
  19189. (match-string 2))))
  19190. (defun org-at-date-range-p (&optional inactive-ok)
  19191. "Is the cursor inside a date range?"
  19192. (interactive)
  19193. (save-excursion
  19194. (catch 'exit
  19195. (let ((pos (point)))
  19196. (skip-chars-backward "^[<\r\n")
  19197. (skip-chars-backward "<[")
  19198. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19199. (>= (match-end 0) pos)
  19200. (throw 'exit t))
  19201. (skip-chars-backward "^<[\r\n")
  19202. (skip-chars-backward "<[")
  19203. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19204. (>= (match-end 0) pos)
  19205. (throw 'exit t)))
  19206. nil)))
  19207. (defun org-agenda-get-todos ()
  19208. "Return the TODO information for agenda display."
  19209. (let* ((props (list 'face nil
  19210. 'done-face 'org-done
  19211. 'org-not-done-regexp org-not-done-regexp
  19212. 'org-todo-regexp org-todo-regexp
  19213. 'mouse-face 'highlight
  19214. 'keymap org-agenda-keymap
  19215. 'help-echo
  19216. (format "mouse-2 or RET jump to org file %s"
  19217. (abbreviate-file-name buffer-file-name))))
  19218. ;; FIXME: get rid of the \n at some point but watch out
  19219. (regexp (concat "^\\*+[ \t]+\\("
  19220. (if org-select-this-todo-keyword
  19221. (if (equal org-select-this-todo-keyword "*")
  19222. org-todo-regexp
  19223. (concat "\\<\\("
  19224. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19225. "\\)\\>"))
  19226. org-not-done-regexp)
  19227. "[^\n\r]*\\)"))
  19228. marker priority category tags
  19229. ee txt beg end)
  19230. (goto-char (point-min))
  19231. (while (re-search-forward regexp nil t)
  19232. (catch :skip
  19233. (save-match-data
  19234. (beginning-of-line)
  19235. (setq beg (point) end (progn (outline-next-heading) (point)))
  19236. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19237. (re-search-forward org-ts-regexp end t))
  19238. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19239. (re-search-forward org-scheduled-time-regexp end t))
  19240. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19241. (re-search-forward org-deadline-time-regexp end t)
  19242. (org-deadline-close (match-string 1))))
  19243. (goto-char (1+ beg))
  19244. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19245. (throw :skip nil)))
  19246. (goto-char beg)
  19247. (org-agenda-skip)
  19248. (goto-char (match-beginning 1))
  19249. (setq marker (org-agenda-new-marker (match-beginning 0))
  19250. category (org-get-category)
  19251. tags (org-get-tags-at (point))
  19252. txt (org-format-agenda-item "" (match-string 1) category tags)
  19253. priority (1+ (org-get-priority txt)))
  19254. (org-add-props txt props
  19255. 'org-marker marker 'org-hd-marker marker
  19256. 'priority priority 'org-category category
  19257. 'type "todo")
  19258. (push txt ee)
  19259. (if org-agenda-todo-list-sublevels
  19260. (goto-char (match-end 1))
  19261. (org-end-of-subtree 'invisible))))
  19262. (nreverse ee)))
  19263. (defconst org-agenda-no-heading-message
  19264. "No heading for this item in buffer or region.")
  19265. (defun org-agenda-get-timestamps ()
  19266. "Return the date stamp information for agenda display."
  19267. (let* ((props (list 'face nil
  19268. 'org-not-done-regexp org-not-done-regexp
  19269. 'org-todo-regexp org-todo-regexp
  19270. 'mouse-face 'highlight
  19271. 'keymap org-agenda-keymap
  19272. 'help-echo
  19273. (format "mouse-2 or RET jump to org file %s"
  19274. (abbreviate-file-name buffer-file-name))))
  19275. (d1 (calendar-absolute-from-gregorian date))
  19276. (remove-re
  19277. (concat
  19278. (regexp-quote
  19279. (format-time-string
  19280. "<%Y-%m-%d"
  19281. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19282. ".*?>"))
  19283. (regexp
  19284. (concat
  19285. (regexp-quote
  19286. (substring
  19287. (format-time-string
  19288. (car org-time-stamp-formats)
  19289. (apply 'encode-time ; DATE bound by calendar
  19290. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19291. 0 11))
  19292. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19293. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19294. marker hdmarker deadlinep scheduledp donep tmp priority category
  19295. ee txt timestr tags b0 b3 e3 head)
  19296. (goto-char (point-min))
  19297. (while (re-search-forward regexp nil t)
  19298. (setq b0 (match-beginning 0)
  19299. b3 (match-beginning 3) e3 (match-end 3))
  19300. (catch :skip
  19301. (and (org-at-date-range-p) (throw :skip nil))
  19302. (org-agenda-skip)
  19303. (if (and (match-end 1)
  19304. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19305. (throw :skip nil))
  19306. (if (and e3
  19307. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19308. (throw :skip nil))
  19309. (setq marker (org-agenda-new-marker b0)
  19310. category (org-get-category b0)
  19311. tmp (buffer-substring (max (point-min)
  19312. (- b0 org-ds-keyword-length))
  19313. b0)
  19314. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19315. deadlinep (string-match org-deadline-regexp tmp)
  19316. scheduledp (string-match org-scheduled-regexp tmp)
  19317. donep (org-entry-is-done-p))
  19318. (if (or scheduledp deadlinep) (throw :skip t))
  19319. (if (string-match ">" timestr)
  19320. ;; substring should only run to end of time stamp
  19321. (setq timestr (substring timestr 0 (match-end 0))))
  19322. (save-excursion
  19323. (if (re-search-backward "^\\*+ " nil t)
  19324. (progn
  19325. (goto-char (match-beginning 0))
  19326. (setq hdmarker (org-agenda-new-marker)
  19327. tags (org-get-tags-at))
  19328. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19329. (setq head (match-string 1))
  19330. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19331. (setq txt (org-format-agenda-item
  19332. nil head category tags timestr nil
  19333. remove-re)))
  19334. (setq txt org-agenda-no-heading-message))
  19335. (setq priority (org-get-priority txt))
  19336. (org-add-props txt props
  19337. 'org-marker marker 'org-hd-marker hdmarker)
  19338. (org-add-props txt nil 'priority priority
  19339. 'org-category category 'date date
  19340. 'type "timestamp")
  19341. (push txt ee))
  19342. (outline-next-heading)))
  19343. (nreverse ee)))
  19344. (defun org-agenda-get-sexps ()
  19345. "Return the sexp information for agenda display."
  19346. (require 'diary-lib)
  19347. (let* ((props (list 'face nil
  19348. 'mouse-face 'highlight
  19349. 'keymap org-agenda-keymap
  19350. 'help-echo
  19351. (format "mouse-2 or RET jump to org file %s"
  19352. (abbreviate-file-name buffer-file-name))))
  19353. (regexp "^&?%%(")
  19354. marker category ee txt tags entry result beg b sexp sexp-entry)
  19355. (goto-char (point-min))
  19356. (while (re-search-forward regexp nil t)
  19357. (catch :skip
  19358. (org-agenda-skip)
  19359. (setq beg (match-beginning 0))
  19360. (goto-char (1- (match-end 0)))
  19361. (setq b (point))
  19362. (forward-sexp 1)
  19363. (setq sexp (buffer-substring b (point)))
  19364. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19365. (org-trim (match-string 1))
  19366. ""))
  19367. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19368. (when result
  19369. (setq marker (org-agenda-new-marker beg)
  19370. category (org-get-category beg))
  19371. (if (string-match "\\S-" result)
  19372. (setq txt result)
  19373. (setq txt "SEXP entry returned empty string"))
  19374. (setq txt (org-format-agenda-item
  19375. "" txt category tags 'time))
  19376. (org-add-props txt props 'org-marker marker)
  19377. (org-add-props txt nil
  19378. 'org-category category 'date date
  19379. 'type "sexp")
  19380. (push txt ee))))
  19381. (nreverse ee)))
  19382. (defun org-agenda-get-closed ()
  19383. "Return the logged TODO entries for agenda display."
  19384. (let* ((props (list 'mouse-face 'highlight
  19385. 'org-not-done-regexp org-not-done-regexp
  19386. 'org-todo-regexp org-todo-regexp
  19387. 'keymap org-agenda-keymap
  19388. 'help-echo
  19389. (format "mouse-2 or RET jump to org file %s"
  19390. (abbreviate-file-name buffer-file-name))))
  19391. (regexp (concat
  19392. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19393. (regexp-quote
  19394. (substring
  19395. (format-time-string
  19396. (car org-time-stamp-formats)
  19397. (apply 'encode-time ; DATE bound by calendar
  19398. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19399. 1 11))))
  19400. marker hdmarker priority category tags closedp
  19401. ee txt timestr)
  19402. (goto-char (point-min))
  19403. (while (re-search-forward regexp nil t)
  19404. (catch :skip
  19405. (org-agenda-skip)
  19406. (setq marker (org-agenda-new-marker (match-beginning 0))
  19407. closedp (equal (match-string 1) org-closed-string)
  19408. category (org-get-category (match-beginning 0))
  19409. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19410. ;; donep (org-entry-is-done-p)
  19411. )
  19412. (if (string-match "\\]" timestr)
  19413. ;; substring should only run to end of time stamp
  19414. (setq timestr (substring timestr 0 (match-end 0))))
  19415. (save-excursion
  19416. (if (re-search-backward "^\\*+ " nil t)
  19417. (progn
  19418. (goto-char (match-beginning 0))
  19419. (setq hdmarker (org-agenda-new-marker)
  19420. tags (org-get-tags-at))
  19421. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19422. (setq txt (org-format-agenda-item
  19423. (if closedp "Closed: " "Clocked: ")
  19424. (match-string 1) category tags timestr)))
  19425. (setq txt org-agenda-no-heading-message))
  19426. (setq priority 100000)
  19427. (org-add-props txt props
  19428. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19429. 'priority priority 'org-category category
  19430. 'type "closed" 'date date
  19431. 'undone-face 'org-warning 'done-face 'org-done)
  19432. (push txt ee))
  19433. (outline-next-heading)))
  19434. (nreverse ee)))
  19435. (defun org-agenda-get-deadlines ()
  19436. "Return the deadline information for agenda display."
  19437. (let* ((props (list 'mouse-face 'highlight
  19438. 'org-not-done-regexp org-not-done-regexp
  19439. 'org-todo-regexp org-todo-regexp
  19440. 'keymap org-agenda-keymap
  19441. 'help-echo
  19442. (format "mouse-2 or RET jump to org file %s"
  19443. (abbreviate-file-name buffer-file-name))))
  19444. (regexp org-deadline-time-regexp)
  19445. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19446. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19447. d2 diff dfrac wdays pos pos1 category tags
  19448. ee txt head face s upcomingp donep timestr)
  19449. (goto-char (point-min))
  19450. (while (re-search-forward regexp nil t)
  19451. (catch :skip
  19452. (org-agenda-skip)
  19453. (setq s (match-string 1)
  19454. pos (1- (match-beginning 1))
  19455. d2 (org-time-string-to-absolute (match-string 1) d1)
  19456. diff (- d2 d1)
  19457. wdays (org-get-wdays s)
  19458. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  19459. upcomingp (and todayp (> diff 0)))
  19460. ;; When to show a deadline in the calendar:
  19461. ;; If the expiration is within wdays warning time.
  19462. ;; Past-due deadlines are only shown on the current date
  19463. (if (or (and (<= diff wdays)
  19464. (and todayp (not org-agenda-only-exact-dates)))
  19465. (= diff 0))
  19466. (save-excursion
  19467. (setq category (org-get-category))
  19468. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19469. (progn
  19470. (goto-char (match-end 0))
  19471. (setq pos1 (match-beginning 0))
  19472. (setq tags (org-get-tags-at pos1))
  19473. (setq head (buffer-substring-no-properties
  19474. (point)
  19475. (progn (skip-chars-forward "^\r\n")
  19476. (point))))
  19477. (setq donep (string-match org-looking-at-done-regexp head))
  19478. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19479. (setq timestr
  19480. (concat (substring s (match-beginning 1)) " "))
  19481. (setq timestr 'time))
  19482. (if (and donep
  19483. (or org-agenda-skip-deadline-if-done
  19484. (not (= diff 0))))
  19485. (setq txt nil)
  19486. (setq txt (org-format-agenda-item
  19487. (if (= diff 0)
  19488. (car org-agenda-deadline-leaders)
  19489. (format (nth 1 org-agenda-deadline-leaders)
  19490. diff))
  19491. head category tags timestr))))
  19492. (setq txt org-agenda-no-heading-message))
  19493. (when txt
  19494. (setq face (org-agenda-deadline-face dfrac))
  19495. (org-add-props txt props
  19496. 'org-marker (org-agenda-new-marker pos)
  19497. 'org-hd-marker (org-agenda-new-marker pos1)
  19498. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  19499. (org-get-priority txt))
  19500. 'org-category category
  19501. 'type (if upcomingp "upcoming-deadline" "deadline")
  19502. 'date (if upcomingp date d2)
  19503. 'face (if donep 'org-done face)
  19504. 'undone-face face 'done-face 'org-done)
  19505. (push txt ee))))))
  19506. (nreverse ee)))
  19507. (defun org-agenda-deadline-face (fraction)
  19508. "Return the face to displaying a deadline item.
  19509. FRACTION is what fraction of the head-warning time has passed."
  19510. (let ((faces org-agenda-deadline-faces) f)
  19511. (catch 'exit
  19512. (while (setq f (pop faces))
  19513. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19514. (defun org-agenda-get-scheduled ()
  19515. "Return the scheduled information for agenda display."
  19516. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19517. 'org-todo-regexp org-todo-regexp
  19518. 'done-face 'org-done
  19519. 'mouse-face 'highlight
  19520. 'keymap org-agenda-keymap
  19521. 'help-echo
  19522. (format "mouse-2 or RET jump to org file %s"
  19523. (abbreviate-file-name buffer-file-name))))
  19524. (regexp org-scheduled-time-regexp)
  19525. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19526. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19527. d2 diff pos pos1 category tags
  19528. ee txt head pastschedp donep face timestr s)
  19529. (goto-char (point-min))
  19530. (while (re-search-forward regexp nil t)
  19531. (catch :skip
  19532. (org-agenda-skip)
  19533. (setq s (match-string 1)
  19534. pos (1- (match-beginning 1))
  19535. d2 (org-time-string-to-absolute (match-string 1) d1)
  19536. diff (- d2 d1))
  19537. (setq pastschedp (and todayp (< diff 0)))
  19538. ;; When to show a scheduled item in the calendar:
  19539. ;; If it is on or past the date.
  19540. (if (or (and (< diff 0)
  19541. (and todayp (not org-agenda-only-exact-dates)))
  19542. (= diff 0))
  19543. (save-excursion
  19544. (setq category (org-get-category))
  19545. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19546. (progn
  19547. (goto-char (match-end 0))
  19548. (setq pos1 (match-beginning 0))
  19549. (setq tags (org-get-tags-at))
  19550. (setq head (buffer-substring-no-properties
  19551. (point)
  19552. (progn (skip-chars-forward "^\r\n") (point))))
  19553. (setq donep (string-match org-looking-at-done-regexp head))
  19554. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19555. (setq timestr
  19556. (concat (substring s (match-beginning 1)) " "))
  19557. (setq timestr 'time))
  19558. (if (and donep
  19559. (or org-agenda-skip-scheduled-if-done
  19560. (not (= diff 0))))
  19561. (setq txt nil)
  19562. (setq txt (org-format-agenda-item
  19563. (if (= diff 0)
  19564. (car org-agenda-scheduled-leaders)
  19565. (format (nth 1 org-agenda-scheduled-leaders)
  19566. (- 1 diff)))
  19567. head category tags timestr))))
  19568. (setq txt org-agenda-no-heading-message))
  19569. (when txt
  19570. (setq face (if pastschedp
  19571. 'org-scheduled-previously
  19572. 'org-scheduled-today))
  19573. (org-add-props txt props
  19574. 'undone-face face
  19575. 'face (if donep 'org-done face)
  19576. 'org-marker (org-agenda-new-marker pos)
  19577. 'org-hd-marker (org-agenda-new-marker pos1)
  19578. 'type (if pastschedp "past-scheduled" "scheduled")
  19579. 'date (if pastschedp d2 date)
  19580. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19581. 'org-category category)
  19582. (push txt ee))))))
  19583. (nreverse ee)))
  19584. (defun org-agenda-get-blocks ()
  19585. "Return the date-range information for agenda display."
  19586. (let* ((props (list 'face nil
  19587. 'org-not-done-regexp org-not-done-regexp
  19588. 'org-todo-regexp org-todo-regexp
  19589. 'mouse-face 'highlight
  19590. 'keymap org-agenda-keymap
  19591. 'help-echo
  19592. (format "mouse-2 or RET jump to org file %s"
  19593. (abbreviate-file-name buffer-file-name))))
  19594. (regexp org-tr-regexp)
  19595. (d0 (calendar-absolute-from-gregorian date))
  19596. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19597. donep head)
  19598. (goto-char (point-min))
  19599. (while (re-search-forward regexp nil t)
  19600. (catch :skip
  19601. (org-agenda-skip)
  19602. (setq pos (point))
  19603. (setq timestr (match-string 0)
  19604. s1 (match-string 1)
  19605. s2 (match-string 2)
  19606. d1 (time-to-days (org-time-string-to-time s1))
  19607. d2 (time-to-days (org-time-string-to-time s2)))
  19608. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19609. ;; Only allow days between the limits, because the normal
  19610. ;; date stamps will catch the limits.
  19611. (save-excursion
  19612. (setq marker (org-agenda-new-marker (point)))
  19613. (setq category (org-get-category))
  19614. (if (re-search-backward "^\\*+ " nil t)
  19615. (progn
  19616. (goto-char (match-beginning 0))
  19617. (setq hdmarker (org-agenda-new-marker (point)))
  19618. (setq tags (org-get-tags-at))
  19619. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19620. (setq head (match-string 1))
  19621. (and org-agenda-skip-timestamp-if-done
  19622. (org-entry-is-done-p)
  19623. (throw :skip t))
  19624. (setq txt (org-format-agenda-item
  19625. (format (if (= d1 d2) "" "(%d/%d): ")
  19626. (1+ (- d0 d1)) (1+ (- d2 d1)))
  19627. head category tags
  19628. (if (= d0 d1) timestr))))
  19629. (setq txt org-agenda-no-heading-message))
  19630. (org-add-props txt props
  19631. 'org-marker marker 'org-hd-marker hdmarker
  19632. 'type "block" 'date date
  19633. 'priority (org-get-priority txt) 'org-category category)
  19634. (push txt ee)))
  19635. (goto-char pos)))
  19636. ;; Sort the entries by expiration date.
  19637. (nreverse ee)))
  19638. ;;; Agenda presentation and sorting
  19639. (defconst org-plain-time-of-day-regexp
  19640. (concat
  19641. "\\(\\<[012]?[0-9]"
  19642. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19643. "\\(--?"
  19644. "\\(\\<[012]?[0-9]"
  19645. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19646. "\\)?")
  19647. "Regular expression to match a plain time or time range.
  19648. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19649. groups carry important information:
  19650. 0 the full match
  19651. 1 the first time, range or not
  19652. 8 the second time, if it is a range.")
  19653. (defconst org-plain-time-extension-regexp
  19654. (concat
  19655. "\\(\\<[012]?[0-9]"
  19656. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19657. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  19658. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  19659. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19660. groups carry important information:
  19661. 0 the full match
  19662. 7 hours of duration
  19663. 9 minutes of duration")
  19664. (defconst org-stamp-time-of-day-regexp
  19665. (concat
  19666. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  19667. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  19668. "\\(--?"
  19669. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  19670. "Regular expression to match a timestamp time or time range.
  19671. After a match, the following groups carry important information:
  19672. 0 the full match
  19673. 1 date plus weekday, for backreferencing to make sure both times on same day
  19674. 2 the first time, range or not
  19675. 4 the second time, if it is a range.")
  19676. (defvar org-prefix-has-time nil
  19677. "A flag, set by `org-compile-prefix-format'.
  19678. The flag is set if the currently compiled format contains a `%t'.")
  19679. (defvar org-prefix-has-tag nil
  19680. "A flag, set by `org-compile-prefix-format'.
  19681. The flag is set if the currently compiled format contains a `%T'.")
  19682. (defun org-format-agenda-item (extra txt &optional category tags dotime
  19683. noprefix remove-re)
  19684. "Format TXT to be inserted into the agenda buffer.
  19685. In particular, it adds the prefix and corresponding text properties. EXTRA
  19686. must be a string and replaces the `%s' specifier in the prefix format.
  19687. CATEGORY (string, symbol or nil) may be used to overrule the default
  19688. category taken from local variable or file name. It will replace the `%c'
  19689. specifier in the format. DOTIME, when non-nil, indicates that a
  19690. time-of-day should be extracted from TXT for sorting of this entry, and for
  19691. the `%t' specifier in the format. When DOTIME is a string, this string is
  19692. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  19693. only the correctly processes TXT should be returned - this is used by
  19694. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  19695. Any match of REMOVE-RE will be removed from TXT."
  19696. (save-match-data
  19697. ;; Diary entries sometimes have extra whitespace at the beginning
  19698. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  19699. (let* ((category (or category
  19700. org-category
  19701. (if buffer-file-name
  19702. (file-name-sans-extension
  19703. (file-name-nondirectory buffer-file-name))
  19704. "")))
  19705. (tag (if tags (nth (1- (length tags)) tags) ""))
  19706. time ; time and tag are needed for the eval of the prefix format
  19707. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  19708. (time-of-day (and dotime (org-get-time-of-day ts)))
  19709. stamp plain s0 s1 s2 rtn srp)
  19710. (when (and dotime time-of-day org-prefix-has-time)
  19711. ;; Extract starting and ending time and move them to prefix
  19712. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  19713. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  19714. (setq s0 (match-string 0 ts)
  19715. srp (and stamp (match-end 3))
  19716. s1 (match-string (if plain 1 2) ts)
  19717. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  19718. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  19719. ;; them, we might want to remove them there to avoid duplication.
  19720. ;; The user can turn this off with a variable.
  19721. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  19722. (string-match (concat (regexp-quote s0) " *") txt)
  19723. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  19724. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  19725. (= (match-beginning 0) 0)
  19726. t))
  19727. (setq txt (replace-match "" nil nil txt))))
  19728. ;; Normalize the time(s) to 24 hour
  19729. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  19730. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  19731. (when (and s1 (not s2) org-agenda-default-appointment-duration
  19732. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  19733. (let ((m (+ (string-to-number (match-string 2 s1))
  19734. (* 60 (string-to-number (match-string 1 s1)))
  19735. org-agenda-default-appointment-duration))
  19736. h)
  19737. (setq h (/ m 60) m (- m (* h 60)))
  19738. (setq s2 (format "%02d:%02d" h m))))
  19739. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  19740. txt)
  19741. ;; Tags are in the string
  19742. (if (or (eq org-agenda-remove-tags t)
  19743. (and org-agenda-remove-tags
  19744. org-prefix-has-tag))
  19745. (setq txt (replace-match "" t t txt))
  19746. (setq txt (replace-match
  19747. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  19748. (match-string 2 txt))
  19749. t t txt))))
  19750. (when remove-re
  19751. (while (string-match remove-re txt)
  19752. (setq txt (replace-match "" t t txt))))
  19753. ;; Create the final string
  19754. (if noprefix
  19755. (setq rtn txt)
  19756. ;; Prepare the variables needed in the eval of the compiled format
  19757. (setq time (cond (s2 (concat s1 "-" s2))
  19758. (s1 (concat s1 "......"))
  19759. (t ""))
  19760. extra (or extra "")
  19761. category (if (symbolp category) (symbol-name category) category))
  19762. ;; Evaluate the compiled format
  19763. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  19764. ;; And finally add the text properties
  19765. (org-add-props rtn nil
  19766. 'org-category (downcase category) 'tags tags
  19767. 'org-highest-priority org-highest-priority
  19768. 'org-lowest-priority org-lowest-priority
  19769. 'prefix-length (- (length rtn) (length txt))
  19770. 'time-of-day time-of-day
  19771. 'txt txt
  19772. 'time time
  19773. 'extra extra
  19774. 'dotime dotime))))
  19775. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  19776. (defvar org-agenda-sorting-strategy-selected nil)
  19777. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  19778. (catch 'exit
  19779. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  19780. ((and todayp (member 'today (car org-agenda-time-grid))))
  19781. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  19782. ((member 'weekly (car org-agenda-time-grid)))
  19783. (t (throw 'exit list)))
  19784. (let* ((have (delq nil (mapcar
  19785. (lambda (x) (get-text-property 1 'time-of-day x))
  19786. list)))
  19787. (string (nth 1 org-agenda-time-grid))
  19788. (gridtimes (nth 2 org-agenda-time-grid))
  19789. (req (car org-agenda-time-grid))
  19790. (remove (member 'remove-match req))
  19791. new time)
  19792. (if (and (member 'require-timed req) (not have))
  19793. ;; don't show empty grid
  19794. (throw 'exit list))
  19795. (while (setq time (pop gridtimes))
  19796. (unless (and remove (member time have))
  19797. (setq time (int-to-string time))
  19798. (push (org-format-agenda-item
  19799. nil string "" nil
  19800. (concat (substring time 0 -2) ":" (substring time -2)))
  19801. new)
  19802. (put-text-property
  19803. 1 (length (car new)) 'face 'org-time-grid (car new))))
  19804. (if (member 'time-up org-agenda-sorting-strategy-selected)
  19805. (append new list)
  19806. (append list new)))))
  19807. (defun org-compile-prefix-format (key)
  19808. "Compile the prefix format into a Lisp form that can be evaluated.
  19809. The resulting form is returned and stored in the variable
  19810. `org-prefix-format-compiled'."
  19811. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  19812. (let ((s (cond
  19813. ((stringp org-agenda-prefix-format)
  19814. org-agenda-prefix-format)
  19815. ((assq key org-agenda-prefix-format)
  19816. (cdr (assq key org-agenda-prefix-format)))
  19817. (t " %-12:c%?-12t% s")))
  19818. (start 0)
  19819. varform vars var e c f opt)
  19820. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  19821. s start)
  19822. (setq var (cdr (assoc (match-string 4 s)
  19823. '(("c" . category) ("t" . time) ("s" . extra)
  19824. ("T" . tag))))
  19825. c (or (match-string 3 s) "")
  19826. opt (match-beginning 1)
  19827. start (1+ (match-beginning 0)))
  19828. (if (equal var 'time) (setq org-prefix-has-time t))
  19829. (if (equal var 'tag) (setq org-prefix-has-tag t))
  19830. (setq f (concat "%" (match-string 2 s) "s"))
  19831. (if opt
  19832. (setq varform
  19833. `(if (equal "" ,var)
  19834. ""
  19835. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  19836. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  19837. (setq s (replace-match "%s" t nil s))
  19838. (push varform vars))
  19839. (setq vars (nreverse vars))
  19840. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  19841. (defun org-set-sorting-strategy (key)
  19842. (if (symbolp (car org-agenda-sorting-strategy))
  19843. ;; the old format
  19844. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  19845. (setq org-agenda-sorting-strategy-selected
  19846. (or (cdr (assq key org-agenda-sorting-strategy))
  19847. (cdr (assq 'agenda org-agenda-sorting-strategy))
  19848. '(time-up category-keep priority-down)))))
  19849. (defun org-get-time-of-day (s &optional string mod24)
  19850. "Check string S for a time of day.
  19851. If found, return it as a military time number between 0 and 2400.
  19852. If not found, return nil.
  19853. The optional STRING argument forces conversion into a 5 character wide string
  19854. HH:MM."
  19855. (save-match-data
  19856. (when
  19857. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  19858. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  19859. (let* ((h (string-to-number (match-string 1 s)))
  19860. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  19861. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  19862. (am-p (equal ampm "am"))
  19863. (h1 (cond ((not ampm) h)
  19864. ((= h 12) (if am-p 0 12))
  19865. (t (+ h (if am-p 0 12)))))
  19866. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  19867. (mod h1 24) h1))
  19868. (t0 (+ (* 100 h2) m))
  19869. (t1 (concat (if (>= h1 24) "+" " ")
  19870. (if (< t0 100) "0" "")
  19871. (if (< t0 10) "0" "")
  19872. (int-to-string t0))))
  19873. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  19874. (defun org-finalize-agenda-entries (list &optional nosort)
  19875. "Sort and concatenate the agenda items."
  19876. (setq list (mapcar 'org-agenda-highlight-todo list))
  19877. (if nosort
  19878. list
  19879. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  19880. (defun org-agenda-highlight-todo (x)
  19881. (let (re pl)
  19882. (if (eq x 'line)
  19883. (save-excursion
  19884. (beginning-of-line 1)
  19885. (setq re (get-text-property (point) 'org-todo-regexp))
  19886. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  19887. (and (looking-at (concat "[ \t]*\\.*" re))
  19888. (add-text-properties (match-beginning 0) (match-end 0)
  19889. (list 'face (org-get-todo-face 0)))))
  19890. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  19891. pl (get-text-property 0 'prefix-length x))
  19892. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  19893. (add-text-properties
  19894. (or (match-end 1) (match-end 0)) (match-end 0)
  19895. (list 'face (org-get-todo-face (match-string 2 x)))
  19896. x))
  19897. x)))
  19898. (defsubst org-cmp-priority (a b)
  19899. "Compare the priorities of string A and B."
  19900. (let ((pa (or (get-text-property 1 'priority a) 0))
  19901. (pb (or (get-text-property 1 'priority b) 0)))
  19902. (cond ((> pa pb) +1)
  19903. ((< pa pb) -1)
  19904. (t nil))))
  19905. (defsubst org-cmp-category (a b)
  19906. "Compare the string values of categories of strings A and B."
  19907. (let ((ca (or (get-text-property 1 'org-category a) ""))
  19908. (cb (or (get-text-property 1 'org-category b) "")))
  19909. (cond ((string-lessp ca cb) -1)
  19910. ((string-lessp cb ca) +1)
  19911. (t nil))))
  19912. (defsubst org-cmp-tag (a b)
  19913. "Compare the string values of categories of strings A and B."
  19914. (let ((ta (car (last (get-text-property 1 'tags a))))
  19915. (tb (car (last (get-text-property 1 'tags b)))))
  19916. (cond ((not ta) +1)
  19917. ((not tb) -1)
  19918. ((string-lessp ta tb) -1)
  19919. ((string-lessp tb ta) +1)
  19920. (t nil))))
  19921. (defsubst org-cmp-time (a b)
  19922. "Compare the time-of-day values of strings A and B."
  19923. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  19924. (ta (or (get-text-property 1 'time-of-day a) def))
  19925. (tb (or (get-text-property 1 'time-of-day b) def)))
  19926. (cond ((< ta tb) -1)
  19927. ((< tb ta) +1)
  19928. (t nil))))
  19929. (defun org-entries-lessp (a b)
  19930. "Predicate for sorting agenda entries."
  19931. ;; The following variables will be used when the form is evaluated.
  19932. ;; So even though the compiler complains, keep them.
  19933. (let* ((time-up (org-cmp-time a b))
  19934. (time-down (if time-up (- time-up) nil))
  19935. (priority-up (org-cmp-priority a b))
  19936. (priority-down (if priority-up (- priority-up) nil))
  19937. (category-up (org-cmp-category a b))
  19938. (category-down (if category-up (- category-up) nil))
  19939. (category-keep (if category-up +1 nil))
  19940. (tag-up (org-cmp-tag a b))
  19941. (tag-down (if tag-up (- tag-up) nil)))
  19942. (cdr (assoc
  19943. (eval (cons 'or org-agenda-sorting-strategy-selected))
  19944. '((-1 . t) (1 . nil) (nil . nil))))))
  19945. ;;; Agenda restriction lock
  19946. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  19947. "Overlay to mark the headline to which arenda commands are restricted.")
  19948. (org-overlay-put org-agenda-restriction-lock-overlay
  19949. 'face 'org-agenda-restriction-lock)
  19950. (org-overlay-put org-agenda-restriction-lock-overlay
  19951. 'help-echo "Agendas are currently limited to this subtree.")
  19952. (org-detach-overlay org-agenda-restriction-lock-overlay)
  19953. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  19954. "Overlay marking the agenda restriction line in speedbar.")
  19955. (org-overlay-put org-speedbar-restriction-lock-overlay
  19956. 'face 'org-agenda-restriction-lock)
  19957. (org-overlay-put org-speedbar-restriction-lock-overlay
  19958. 'help-echo "Agendas are currently limited to this item.")
  19959. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19960. (defun org-agenda-set-restriction-lock (&optional type)
  19961. "Set restriction lock for agenda, to current subtree or file.
  19962. Restriction will be the file if TYPE is `file', or if type is the
  19963. universal prefix '(4), or if the cursor is before the first headline
  19964. in the file. Otherwise, restriction will be to the current subtree."
  19965. (interactive "P")
  19966. (and (equal type '(4)) (setq type 'file))
  19967. (setq type (cond
  19968. (type type)
  19969. ((org-at-heading-p) 'subtree)
  19970. ((condition-case nil (org-back-to-heading t) (error nil))
  19971. 'subtree)
  19972. (t 'file)))
  19973. (if (eq type 'subtree)
  19974. (progn
  19975. (setq org-agenda-restrict t)
  19976. (setq org-agenda-overriding-restriction 'subtree)
  19977. (put 'org-agenda-files 'org-restrict
  19978. (list (buffer-file-name (buffer-base-buffer))))
  19979. (org-back-to-heading t)
  19980. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  19981. (move-marker org-agenda-restrict-begin (point))
  19982. (move-marker org-agenda-restrict-end
  19983. (save-excursion (org-end-of-subtree t)))
  19984. (message "Locking agenda restriction to subtree"))
  19985. (put 'org-agenda-files 'org-restrict
  19986. (list (buffer-file-name (buffer-base-buffer))))
  19987. (setq org-agenda-restrict nil)
  19988. (setq org-agenda-overriding-restriction 'file)
  19989. (move-marker org-agenda-restrict-begin nil)
  19990. (move-marker org-agenda-restrict-end nil)
  19991. (message "Locking agenda restriction to file"))
  19992. (setq current-prefix-arg nil)
  19993. (org-agenda-maybe-redo))
  19994. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  19995. "Remove the agenda restriction lock."
  19996. (interactive "P")
  19997. (org-detach-overlay org-agenda-restriction-lock-overlay)
  19998. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19999. (setq org-agenda-overriding-restriction nil)
  20000. (setq org-agenda-restrict nil)
  20001. (put 'org-agenda-files 'org-restrict nil)
  20002. (move-marker org-agenda-restrict-begin nil)
  20003. (move-marker org-agenda-restrict-end nil)
  20004. (setq current-prefix-arg nil)
  20005. (message "Agenda restriction lock removed")
  20006. (or noupdate (org-agenda-maybe-redo)))
  20007. (defun org-agenda-maybe-redo ()
  20008. "If there is any window showing the agenda view, update it."
  20009. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20010. (w0 (selected-window)))
  20011. (when w
  20012. (select-window w)
  20013. (org-agenda-redo)
  20014. (select-window w0)
  20015. (if org-agenda-overriding-restriction
  20016. (message "Agenda view shifted to new %s restriction"
  20017. org-agenda-overriding-restriction)
  20018. (message "Agenda restriction lock removed")))))
  20019. ;;; Agenda commands
  20020. (defun org-agenda-check-type (error &rest types)
  20021. "Check if agenda buffer is of allowed type.
  20022. If ERROR is non-nil, throw an error, otherwise just return nil."
  20023. (if (memq org-agenda-type types)
  20024. t
  20025. (if error
  20026. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20027. nil)))
  20028. (defun org-agenda-quit ()
  20029. "Exit agenda by removing the window or the buffer."
  20030. (interactive)
  20031. (let ((buf (current-buffer)))
  20032. (if (not (one-window-p)) (delete-window))
  20033. (kill-buffer buf)
  20034. (org-agenda-maybe-reset-markers 'force)
  20035. (org-columns-remove-overlays))
  20036. ;; Maybe restore the pre-agenda window configuration.
  20037. (and org-agenda-restore-windows-after-quit
  20038. (not (eq org-agenda-window-setup 'other-frame))
  20039. org-pre-agenda-window-conf
  20040. (set-window-configuration org-pre-agenda-window-conf)))
  20041. (defun org-agenda-exit ()
  20042. "Exit agenda by removing the window or the buffer.
  20043. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20044. Org-mode buffers visited directly by the user will not be touched."
  20045. (interactive)
  20046. (org-release-buffers org-agenda-new-buffers)
  20047. (setq org-agenda-new-buffers nil)
  20048. (org-agenda-quit))
  20049. (defun org-agenda-execute (arg)
  20050. "Execute another agenda command, keeping same window.\\<global-map>
  20051. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20052. (interactive "P")
  20053. (let ((org-agenda-window-setup 'current-window))
  20054. (org-agenda arg)))
  20055. (defun org-save-all-org-buffers ()
  20056. "Save all Org-mode buffers without user confirmation."
  20057. (interactive)
  20058. (message "Saving all Org-mode buffers...")
  20059. (save-some-buffers t 'org-mode-p)
  20060. (message "Saving all Org-mode buffers... done"))
  20061. (defun org-agenda-redo ()
  20062. "Rebuild Agenda.
  20063. When this is the global TODO list, a prefix argument will be interpreted."
  20064. (interactive)
  20065. (let* ((org-agenda-keep-modes t)
  20066. (line (org-current-line))
  20067. (window-line (- line (org-current-line (window-start))))
  20068. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20069. (message "Rebuilding agenda buffer...")
  20070. (org-let lprops '(eval org-agenda-redo-command))
  20071. (setq org-agenda-undo-list nil
  20072. org-agenda-pending-undo-list nil)
  20073. (message "Rebuilding agenda buffer...done")
  20074. (goto-line line)
  20075. (recenter window-line)))
  20076. (defun org-agenda-goto-date (date)
  20077. "Jump to DATE in agenda."
  20078. (interactive (list (org-read-date)))
  20079. (org-agenda-list nil date))
  20080. (defun org-agenda-goto-today ()
  20081. "Go to today."
  20082. (interactive)
  20083. (org-agenda-check-type t 'timeline 'agenda)
  20084. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20085. (cond
  20086. (tdpos (goto-char tdpos))
  20087. ((eq org-agenda-type 'agenda)
  20088. (let* ((sd (time-to-days
  20089. (time-subtract (current-time)
  20090. (list 0 (* 3600 org-extend-today-until) 0))))
  20091. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20092. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20093. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20094. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20095. (org-agenda-redo)
  20096. (org-agenda-find-same-or-today-or-agenda)))
  20097. (t (error "Cannot find today")))))
  20098. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20099. (goto-char
  20100. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20101. (text-property-any (point-min) (point-max) 'org-today t)
  20102. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20103. (point-min))))
  20104. (defun org-agenda-later (arg)
  20105. "Go forward in time by thee current span.
  20106. With prefix ARG, go forward that many times the current span."
  20107. (interactive "p")
  20108. (org-agenda-check-type t 'agenda)
  20109. (let* ((span org-agenda-span)
  20110. (sd org-starting-day)
  20111. (greg (calendar-gregorian-from-absolute sd))
  20112. (cnt (get-text-property (point) 'org-day-cnt))
  20113. greg2 nd)
  20114. (cond
  20115. ((eq span 'day)
  20116. (setq sd (+ arg sd) nd 1))
  20117. ((eq span 'week)
  20118. (setq sd (+ (* 7 arg) sd) nd 7))
  20119. ((eq span 'month)
  20120. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20121. sd (calendar-absolute-from-gregorian greg2))
  20122. (setcar greg2 (1+ (car greg2)))
  20123. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20124. ((eq span 'year)
  20125. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20126. sd (calendar-absolute-from-gregorian greg2))
  20127. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20128. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20129. (let ((org-agenda-overriding-arguments
  20130. (list (car org-agenda-last-arguments) sd nd t)))
  20131. (org-agenda-redo)
  20132. (org-agenda-find-same-or-today-or-agenda cnt))))
  20133. (defun org-agenda-earlier (arg)
  20134. "Go backward in time by the current span.
  20135. With prefix ARG, go backward that many times the current span."
  20136. (interactive "p")
  20137. (org-agenda-later (- arg)))
  20138. (defun org-agenda-day-view ()
  20139. "Switch to daily view for agenda."
  20140. (interactive)
  20141. (setq org-agenda-ndays 1)
  20142. (org-agenda-change-time-span 'day))
  20143. (defun org-agenda-week-view ()
  20144. "Switch to daily view for agenda."
  20145. (interactive)
  20146. (setq org-agenda-ndays 7)
  20147. (org-agenda-change-time-span 'week))
  20148. (defun org-agenda-month-view ()
  20149. "Switch to daily view for agenda."
  20150. (interactive)
  20151. (org-agenda-change-time-span 'month))
  20152. (defun org-agenda-year-view ()
  20153. "Switch to daily view for agenda."
  20154. (interactive)
  20155. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20156. (org-agenda-change-time-span 'year)
  20157. (error "Abort")))
  20158. (defun org-agenda-change-time-span (span)
  20159. "Change the agenda view to SPAN.
  20160. SPAN may be `day', `week', `month', `year'."
  20161. (org-agenda-check-type t 'agenda)
  20162. (if (equal org-agenda-span span)
  20163. (error "Viewing span is already \"%s\"" span))
  20164. (let* ((sd (or (get-text-property (point) 'day)
  20165. org-starting-day))
  20166. (computed (org-agenda-compute-time-span sd span))
  20167. (org-agenda-overriding-arguments
  20168. (list (car org-agenda-last-arguments)
  20169. (car computed) (cdr computed) t)))
  20170. (org-agenda-redo)
  20171. (org-agenda-find-same-or-today-or-agenda))
  20172. (org-agenda-set-mode-name)
  20173. (message "Switched to %s view" span))
  20174. (defun org-agenda-compute-time-span (sd span)
  20175. "Compute starting date and number of days for agenda.
  20176. SPAN may be `day', `week', `month', `year'. The return value
  20177. is a cons cell with the starting date and the number of days,
  20178. so that the date SD will be in that range."
  20179. (let* ((greg (calendar-gregorian-from-absolute sd))
  20180. nd)
  20181. (cond
  20182. ((eq span 'day)
  20183. (setq nd 1))
  20184. ((eq span 'week)
  20185. (let* ((nt (calendar-day-of-week
  20186. (calendar-gregorian-from-absolute sd)))
  20187. (d (if org-agenda-start-on-weekday
  20188. (- nt org-agenda-start-on-weekday)
  20189. 0)))
  20190. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20191. (setq nd 7)))
  20192. ((eq span 'month)
  20193. (setq sd (calendar-absolute-from-gregorian
  20194. (list (car greg) 1 (nth 2 greg)))
  20195. nd (- (calendar-absolute-from-gregorian
  20196. (list (1+ (car greg)) 1 (nth 2 greg)))
  20197. sd)))
  20198. ((eq span 'year)
  20199. (setq sd (calendar-absolute-from-gregorian
  20200. (list 1 1 (nth 2 greg)))
  20201. nd (- (calendar-absolute-from-gregorian
  20202. (list 1 1 (1+ (nth 2 greg))))
  20203. sd))))
  20204. (cons sd nd)))
  20205. ;; FIXME: does not work if user makes date format that starts with a blank
  20206. (defun org-agenda-next-date-line (&optional arg)
  20207. "Jump to the next line indicating a date in agenda buffer."
  20208. (interactive "p")
  20209. (org-agenda-check-type t 'agenda 'timeline)
  20210. (beginning-of-line 1)
  20211. (if (looking-at "^\\S-") (forward-char 1))
  20212. (if (not (re-search-forward "^\\S-" nil t arg))
  20213. (progn
  20214. (backward-char 1)
  20215. (error "No next date after this line in this buffer")))
  20216. (goto-char (match-beginning 0)))
  20217. (defun org-agenda-previous-date-line (&optional arg)
  20218. "Jump to the previous line indicating a date in agenda buffer."
  20219. (interactive "p")
  20220. (org-agenda-check-type t 'agenda 'timeline)
  20221. (beginning-of-line 1)
  20222. (if (not (re-search-backward "^\\S-" nil t arg))
  20223. (error "No previous date before this line in this buffer")))
  20224. ;; Initialize the highlight
  20225. (defvar org-hl (org-make-overlay 1 1))
  20226. (org-overlay-put org-hl 'face 'highlight)
  20227. (defun org-highlight (begin end &optional buffer)
  20228. "Highlight a region with overlay."
  20229. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20230. org-hl begin end (or buffer (current-buffer))))
  20231. (defun org-unhighlight ()
  20232. "Detach overlay INDEX."
  20233. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20234. ;; FIXME this is currently not used.
  20235. (defun org-highlight-until-next-command (beg end &optional buffer)
  20236. (org-highlight beg end buffer)
  20237. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20238. (defun org-unhighlight-once ()
  20239. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20240. (org-unhighlight))
  20241. (defun org-agenda-follow-mode ()
  20242. "Toggle follow mode in an agenda buffer."
  20243. (interactive)
  20244. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20245. (org-agenda-set-mode-name)
  20246. (message "Follow mode is %s"
  20247. (if org-agenda-follow-mode "on" "off")))
  20248. (defun org-agenda-log-mode ()
  20249. "Toggle log mode in an agenda buffer."
  20250. (interactive)
  20251. (org-agenda-check-type t 'agenda 'timeline)
  20252. (setq org-agenda-show-log (not org-agenda-show-log))
  20253. (org-agenda-set-mode-name)
  20254. (org-agenda-redo)
  20255. (message "Log mode is %s"
  20256. (if org-agenda-show-log "on" "off")))
  20257. (defun org-agenda-toggle-diary ()
  20258. "Toggle diary inclusion in an agenda buffer."
  20259. (interactive)
  20260. (org-agenda-check-type t 'agenda)
  20261. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20262. (org-agenda-redo)
  20263. (org-agenda-set-mode-name)
  20264. (message "Diary inclusion turned %s"
  20265. (if org-agenda-include-diary "on" "off")))
  20266. (defun org-agenda-toggle-time-grid ()
  20267. "Toggle time grid in an agenda buffer."
  20268. (interactive)
  20269. (org-agenda-check-type t 'agenda)
  20270. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20271. (org-agenda-redo)
  20272. (org-agenda-set-mode-name)
  20273. (message "Time-grid turned %s"
  20274. (if org-agenda-use-time-grid "on" "off")))
  20275. (defun org-agenda-set-mode-name ()
  20276. "Set the mode name to indicate all the small mode settings."
  20277. (setq mode-name
  20278. (concat "Org-Agenda"
  20279. (if (equal org-agenda-ndays 1) " Day" "")
  20280. (if (equal org-agenda-ndays 7) " Week" "")
  20281. (if org-agenda-follow-mode " Follow" "")
  20282. (if org-agenda-include-diary " Diary" "")
  20283. (if org-agenda-use-time-grid " Grid" "")
  20284. (if org-agenda-show-log " Log" "")))
  20285. (force-mode-line-update))
  20286. (defun org-agenda-post-command-hook ()
  20287. (and (eolp) (not (bolp)) (backward-char 1))
  20288. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20289. (if (and org-agenda-follow-mode
  20290. (get-text-property (point) 'org-marker))
  20291. (org-agenda-show)))
  20292. (defun org-agenda-show-priority ()
  20293. "Show the priority of the current item.
  20294. This priority is composed of the main priority given with the [#A] cookies,
  20295. and by additional input from the age of a schedules or deadline entry."
  20296. (interactive)
  20297. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20298. (message "Priority is %d" (if pri pri -1000))))
  20299. (defun org-agenda-show-tags ()
  20300. "Show the tags applicable to the current item."
  20301. (interactive)
  20302. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20303. (if tags
  20304. (message "Tags are :%s:"
  20305. (org-no-properties (mapconcat 'identity tags ":")))
  20306. (message "No tags associated with this line"))))
  20307. (defun org-agenda-goto (&optional highlight)
  20308. "Go to the Org-mode file which contains the item at point."
  20309. (interactive)
  20310. (let* ((marker (or (get-text-property (point) 'org-marker)
  20311. (org-agenda-error)))
  20312. (buffer (marker-buffer marker))
  20313. (pos (marker-position marker)))
  20314. (switch-to-buffer-other-window buffer)
  20315. (widen)
  20316. (goto-char pos)
  20317. (when (org-mode-p)
  20318. (org-show-context 'agenda)
  20319. (save-excursion
  20320. (and (outline-next-heading)
  20321. (org-flag-heading nil)))) ; show the next heading
  20322. (run-hooks 'org-agenda-after-show-hook)
  20323. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20324. (defvar org-agenda-after-show-hook nil
  20325. "Normal hook run after an item has been shown from the agenda.
  20326. Point is in the buffer where the item originated.")
  20327. (defun org-agenda-kill ()
  20328. "Kill the entry or subtree belonging to the current agenda entry."
  20329. (interactive)
  20330. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20331. (let* ((marker (or (get-text-property (point) 'org-marker)
  20332. (org-agenda-error)))
  20333. (buffer (marker-buffer marker))
  20334. (pos (marker-position marker))
  20335. (type (get-text-property (point) 'type))
  20336. dbeg dend (n 0) conf)
  20337. (org-with-remote-undo buffer
  20338. (with-current-buffer buffer
  20339. (save-excursion
  20340. (goto-char pos)
  20341. (if (and (org-mode-p) (not (member type '("sexp"))))
  20342. (setq dbeg (progn (org-back-to-heading t) (point))
  20343. dend (org-end-of-subtree t t))
  20344. (setq dbeg (point-at-bol)
  20345. dend (min (point-max) (1+ (point-at-eol)))))
  20346. (goto-char dbeg)
  20347. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20348. (setq conf (or (eq t org-agenda-confirm-kill)
  20349. (and (numberp org-agenda-confirm-kill)
  20350. (> n org-agenda-confirm-kill))))
  20351. (and conf
  20352. (not (y-or-n-p
  20353. (format "Delete entry with %d lines in buffer \"%s\"? "
  20354. n (buffer-name buffer))))
  20355. (error "Abort"))
  20356. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20357. (with-current-buffer buffer (delete-region dbeg dend))
  20358. (message "Agenda item and source killed"))))
  20359. (defun org-agenda-archive ()
  20360. "Kill the entry or subtree belonging to the current agenda entry."
  20361. (interactive)
  20362. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20363. (let* ((marker (or (get-text-property (point) 'org-marker)
  20364. (org-agenda-error)))
  20365. (buffer (marker-buffer marker))
  20366. (pos (marker-position marker)))
  20367. (org-with-remote-undo buffer
  20368. (with-current-buffer buffer
  20369. (if (org-mode-p)
  20370. (save-excursion
  20371. (goto-char pos)
  20372. (org-remove-subtree-entries-from-agenda)
  20373. (org-back-to-heading t)
  20374. (org-archive-subtree))
  20375. (error "Archiving works only in Org-mode files"))))))
  20376. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20377. "Remove all lines in the agenda that correspond to a given subtree.
  20378. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20379. If this information is not given, the function uses the tree at point."
  20380. (let ((buf (or buf (current-buffer))) m p)
  20381. (save-excursion
  20382. (unless (and beg end)
  20383. (org-back-to-heading t)
  20384. (setq beg (point))
  20385. (org-end-of-subtree t)
  20386. (setq end (point)))
  20387. (set-buffer (get-buffer org-agenda-buffer-name))
  20388. (save-excursion
  20389. (goto-char (point-max))
  20390. (beginning-of-line 1)
  20391. (while (not (bobp))
  20392. (when (and (setq m (get-text-property (point) 'org-marker))
  20393. (equal buf (marker-buffer m))
  20394. (setq p (marker-position m))
  20395. (>= p beg)
  20396. (<= p end))
  20397. (let ((inhibit-read-only t))
  20398. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20399. (beginning-of-line 0))))))
  20400. (defun org-agenda-open-link ()
  20401. "Follow the link in the current line, if any."
  20402. (interactive)
  20403. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20404. (save-excursion
  20405. (save-restriction
  20406. (narrow-to-region (point-at-bol) (point-at-eol))
  20407. (org-open-at-point))))
  20408. (defun org-agenda-copy-local-variable (var)
  20409. "Get a variable from a referenced buffer and install it here."
  20410. (let ((m (get-text-property (point) 'org-marker)))
  20411. (when (and m (buffer-live-p (marker-buffer m)))
  20412. (org-set-local var (with-current-buffer (marker-buffer m)
  20413. (symbol-value var))))))
  20414. (defun org-agenda-switch-to (&optional delete-other-windows)
  20415. "Go to the Org-mode file which contains the item at point."
  20416. (interactive)
  20417. (let* ((marker (or (get-text-property (point) 'org-marker)
  20418. (org-agenda-error)))
  20419. (buffer (marker-buffer marker))
  20420. (pos (marker-position marker)))
  20421. (switch-to-buffer buffer)
  20422. (and delete-other-windows (delete-other-windows))
  20423. (widen)
  20424. (goto-char pos)
  20425. (when (org-mode-p)
  20426. (org-show-context 'agenda)
  20427. (save-excursion
  20428. (and (outline-next-heading)
  20429. (org-flag-heading nil)))))) ; show the next heading
  20430. (defun org-agenda-goto-mouse (ev)
  20431. "Go to the Org-mode file which contains the item at the mouse click."
  20432. (interactive "e")
  20433. (mouse-set-point ev)
  20434. (org-agenda-goto))
  20435. (defun org-agenda-show ()
  20436. "Display the Org-mode file which contains the item at point."
  20437. (interactive)
  20438. (let ((win (selected-window)))
  20439. (org-agenda-goto t)
  20440. (select-window win)))
  20441. (defun org-agenda-recenter (arg)
  20442. "Display the Org-mode file which contains the item at point and recenter."
  20443. (interactive "P")
  20444. (let ((win (selected-window)))
  20445. (org-agenda-goto t)
  20446. (recenter arg)
  20447. (select-window win)))
  20448. (defun org-agenda-show-mouse (ev)
  20449. "Display the Org-mode file which contains the item at the mouse click."
  20450. (interactive "e")
  20451. (mouse-set-point ev)
  20452. (org-agenda-show))
  20453. (defun org-agenda-check-no-diary ()
  20454. "Check if the entry is a diary link and abort if yes."
  20455. (if (get-text-property (point) 'org-agenda-diary-link)
  20456. (org-agenda-error)))
  20457. (defun org-agenda-error ()
  20458. (error "Command not allowed in this line"))
  20459. (defun org-agenda-tree-to-indirect-buffer ()
  20460. "Show the subtree corresponding to the current entry in an indirect buffer.
  20461. This calls the command `org-tree-to-indirect-buffer' from the original
  20462. Org-mode buffer.
  20463. With numerical prefix arg ARG, go up to this level and then take that tree.
  20464. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20465. dedicated frame)."
  20466. (interactive)
  20467. (org-agenda-check-no-diary)
  20468. (let* ((marker (or (get-text-property (point) 'org-marker)
  20469. (org-agenda-error)))
  20470. (buffer (marker-buffer marker))
  20471. (pos (marker-position marker)))
  20472. (with-current-buffer buffer
  20473. (save-excursion
  20474. (goto-char pos)
  20475. (call-interactively 'org-tree-to-indirect-buffer)))))
  20476. (defvar org-last-heading-marker (make-marker)
  20477. "Marker pointing to the headline that last changed its TODO state
  20478. by a remote command from the agenda.")
  20479. (defun org-agenda-todo-nextset ()
  20480. "Switch TODO entry to next sequence."
  20481. (interactive)
  20482. (org-agenda-todo 'nextset))
  20483. (defun org-agenda-todo-previousset ()
  20484. "Switch TODO entry to previous sequence."
  20485. (interactive)
  20486. (org-agenda-todo 'previousset))
  20487. (defun org-agenda-todo (&optional arg)
  20488. "Cycle TODO state of line at point, also in Org-mode file.
  20489. This changes the line at point, all other lines in the agenda referring to
  20490. the same tree node, and the headline of the tree node in the Org-mode file."
  20491. (interactive "P")
  20492. (org-agenda-check-no-diary)
  20493. (let* ((col (current-column))
  20494. (marker (or (get-text-property (point) 'org-marker)
  20495. (org-agenda-error)))
  20496. (buffer (marker-buffer marker))
  20497. (pos (marker-position marker))
  20498. (hdmarker (get-text-property (point) 'org-hd-marker))
  20499. (inhibit-read-only t)
  20500. newhead)
  20501. (org-with-remote-undo buffer
  20502. (with-current-buffer buffer
  20503. (widen)
  20504. (goto-char pos)
  20505. (org-show-context 'agenda)
  20506. (save-excursion
  20507. (and (outline-next-heading)
  20508. (org-flag-heading nil))) ; show the next heading
  20509. (org-todo arg)
  20510. (and (bolp) (forward-char 1))
  20511. (setq newhead (org-get-heading))
  20512. (save-excursion
  20513. (org-back-to-heading)
  20514. (move-marker org-last-heading-marker (point))))
  20515. (beginning-of-line 1)
  20516. (save-excursion
  20517. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20518. (move-to-column col))))
  20519. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20520. "Change all lines in the agenda buffer which match HDMARKER.
  20521. The new content of the line will be NEWHEAD (as modified by
  20522. `org-format-agenda-item'). HDMARKER is checked with
  20523. `equal' against all `org-hd-marker' text properties in the file.
  20524. If FIXFACE is non-nil, the face of each item is modified acording to
  20525. the new TODO state."
  20526. (let* ((inhibit-read-only t)
  20527. props m pl undone-face done-face finish new dotime cat tags)
  20528. (save-excursion
  20529. (goto-char (point-max))
  20530. (beginning-of-line 1)
  20531. (while (not finish)
  20532. (setq finish (bobp))
  20533. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20534. (equal m hdmarker))
  20535. (setq props (text-properties-at (point))
  20536. dotime (get-text-property (point) 'dotime)
  20537. cat (get-text-property (point) 'org-category)
  20538. tags (get-text-property (point) 'tags)
  20539. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20540. pl (get-text-property (point) 'prefix-length)
  20541. undone-face (get-text-property (point) 'undone-face)
  20542. done-face (get-text-property (point) 'done-face))
  20543. (move-to-column pl)
  20544. (cond
  20545. ((equal new "")
  20546. (beginning-of-line 1)
  20547. (and (looking-at ".*\n?") (replace-match "")))
  20548. ((looking-at ".*")
  20549. (replace-match new t t)
  20550. (beginning-of-line 1)
  20551. (add-text-properties (point-at-bol) (point-at-eol) props)
  20552. (when fixface
  20553. (add-text-properties
  20554. (point-at-bol) (point-at-eol)
  20555. (list 'face
  20556. (if org-last-todo-state-is-todo
  20557. undone-face done-face))))
  20558. (org-agenda-highlight-todo 'line)
  20559. (beginning-of-line 1))
  20560. (t (error "Line update did not work"))))
  20561. (beginning-of-line 0)))
  20562. (org-finalize-agenda)))
  20563. (defun org-agenda-align-tags (&optional line)
  20564. "Align all tags in agenda items to `org-agenda-tags-column'."
  20565. (let ((inhibit-read-only t) l c)
  20566. (save-excursion
  20567. (goto-char (if line (point-at-bol) (point-min)))
  20568. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20569. (if line (point-at-eol) nil) t)
  20570. (add-text-properties
  20571. (match-beginning 2) (match-end 2)
  20572. (list 'face (list 'org-tag (get-text-property
  20573. (match-beginning 2) 'face))))
  20574. (setq l (- (match-end 2) (match-beginning 2))
  20575. c (if (< org-agenda-tags-column 0)
  20576. (- (abs org-agenda-tags-column) l)
  20577. org-agenda-tags-column))
  20578. (delete-region (match-beginning 1) (match-end 1))
  20579. (goto-char (match-beginning 1))
  20580. (insert (org-add-props
  20581. (make-string (max 1 (- c (current-column))) ?\ )
  20582. (text-properties-at (point))))))))
  20583. (defun org-agenda-priority-up ()
  20584. "Increase the priority of line at point, also in Org-mode file."
  20585. (interactive)
  20586. (org-agenda-priority 'up))
  20587. (defun org-agenda-priority-down ()
  20588. "Decrease the priority of line at point, also in Org-mode file."
  20589. (interactive)
  20590. (org-agenda-priority 'down))
  20591. (defun org-agenda-priority (&optional force-direction)
  20592. "Set the priority of line at point, also in Org-mode file.
  20593. This changes the line at point, all other lines in the agenda referring to
  20594. the same tree node, and the headline of the tree node in the Org-mode file."
  20595. (interactive)
  20596. (org-agenda-check-no-diary)
  20597. (let* ((marker (or (get-text-property (point) 'org-marker)
  20598. (org-agenda-error)))
  20599. (hdmarker (get-text-property (point) 'org-hd-marker))
  20600. (buffer (marker-buffer hdmarker))
  20601. (pos (marker-position hdmarker))
  20602. (inhibit-read-only t)
  20603. newhead)
  20604. (org-with-remote-undo buffer
  20605. (with-current-buffer buffer
  20606. (widen)
  20607. (goto-char pos)
  20608. (org-show-context 'agenda)
  20609. (save-excursion
  20610. (and (outline-next-heading)
  20611. (org-flag-heading nil))) ; show the next heading
  20612. (funcall 'org-priority force-direction)
  20613. (end-of-line 1)
  20614. (setq newhead (org-get-heading)))
  20615. (org-agenda-change-all-lines newhead hdmarker)
  20616. (beginning-of-line 1))))
  20617. (defun org-get-tags-at (&optional pos)
  20618. "Get a list of all headline tags applicable at POS.
  20619. POS defaults to point. If tags are inherited, the list contains
  20620. the targets in the same sequence as the headlines appear, i.e.
  20621. the tags of the current headline come last."
  20622. (interactive)
  20623. (let (tags lastpos)
  20624. (save-excursion
  20625. (save-restriction
  20626. (widen)
  20627. (goto-char (or pos (point)))
  20628. (save-match-data
  20629. (org-back-to-heading t)
  20630. (condition-case nil
  20631. (while (not (equal lastpos (point)))
  20632. (setq lastpos (point))
  20633. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  20634. (setq tags (append (org-split-string
  20635. (org-match-string-no-properties 1) ":")
  20636. tags)))
  20637. (or org-use-tag-inheritance (error ""))
  20638. (org-up-heading-all 1))
  20639. (error nil))))
  20640. tags)))
  20641. ;; FIXME: should fix the tags property of the agenda line.
  20642. (defun org-agenda-set-tags ()
  20643. "Set tags for the current headline."
  20644. (interactive)
  20645. (org-agenda-check-no-diary)
  20646. (if (and (org-region-active-p) (interactive-p))
  20647. (call-interactively 'org-change-tag-in-region)
  20648. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20649. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20650. (org-agenda-error)))
  20651. (buffer (marker-buffer hdmarker))
  20652. (pos (marker-position hdmarker))
  20653. (inhibit-read-only t)
  20654. newhead)
  20655. (org-with-remote-undo buffer
  20656. (with-current-buffer buffer
  20657. (widen)
  20658. (goto-char pos)
  20659. (save-excursion
  20660. (org-show-context 'agenda))
  20661. (save-excursion
  20662. (and (outline-next-heading)
  20663. (org-flag-heading nil))) ; show the next heading
  20664. (goto-char pos)
  20665. (call-interactively 'org-set-tags)
  20666. (end-of-line 1)
  20667. (setq newhead (org-get-heading)))
  20668. (org-agenda-change-all-lines newhead hdmarker)
  20669. (beginning-of-line 1)))))
  20670. (defun org-agenda-toggle-archive-tag ()
  20671. "Toggle the archive tag for the current entry."
  20672. (interactive)
  20673. (org-agenda-check-no-diary)
  20674. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20675. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20676. (org-agenda-error)))
  20677. (buffer (marker-buffer hdmarker))
  20678. (pos (marker-position hdmarker))
  20679. (inhibit-read-only t)
  20680. newhead)
  20681. (org-with-remote-undo buffer
  20682. (with-current-buffer buffer
  20683. (widen)
  20684. (goto-char pos)
  20685. (org-show-context 'agenda)
  20686. (save-excursion
  20687. (and (outline-next-heading)
  20688. (org-flag-heading nil))) ; show the next heading
  20689. (call-interactively 'org-toggle-archive-tag)
  20690. (end-of-line 1)
  20691. (setq newhead (org-get-heading)))
  20692. (org-agenda-change-all-lines newhead hdmarker)
  20693. (beginning-of-line 1))))
  20694. (defun org-agenda-date-later (arg &optional what)
  20695. "Change the date of this item to one day later."
  20696. (interactive "p")
  20697. (org-agenda-check-type t 'agenda 'timeline)
  20698. (org-agenda-check-no-diary)
  20699. (let* ((marker (or (get-text-property (point) 'org-marker)
  20700. (org-agenda-error)))
  20701. (buffer (marker-buffer marker))
  20702. (pos (marker-position marker)))
  20703. (org-with-remote-undo buffer
  20704. (with-current-buffer buffer
  20705. (widen)
  20706. (goto-char pos)
  20707. (if (not (org-at-timestamp-p))
  20708. (error "Cannot find time stamp"))
  20709. (org-timestamp-change arg (or what 'day)))
  20710. (org-agenda-show-new-time marker org-last-changed-timestamp))
  20711. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  20712. (defun org-agenda-date-earlier (arg &optional what)
  20713. "Change the date of this item to one day earlier."
  20714. (interactive "p")
  20715. (org-agenda-date-later (- arg) what))
  20716. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  20717. "Show new date stamp via text properties."
  20718. ;; We use text properties to make this undoable
  20719. (let ((inhibit-read-only t))
  20720. (setq stamp (concat " " prefix " => " stamp))
  20721. (save-excursion
  20722. (goto-char (point-max))
  20723. (while (not (bobp))
  20724. (when (equal marker (get-text-property (point) 'org-marker))
  20725. (move-to-column (- (window-width) (length stamp)) t)
  20726. (if (featurep 'xemacs)
  20727. ;; Use `duplicable' property to trigger undo recording
  20728. (let ((ex (make-extent nil nil))
  20729. (gl (make-glyph stamp)))
  20730. (set-glyph-face gl 'secondary-selection)
  20731. (set-extent-properties
  20732. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  20733. (insert-extent ex (1- (point)) (point-at-eol)))
  20734. (add-text-properties
  20735. (1- (point)) (point-at-eol)
  20736. (list 'display (org-add-props stamp nil
  20737. 'face 'secondary-selection))))
  20738. (beginning-of-line 1))
  20739. (beginning-of-line 0)))))
  20740. (defun org-agenda-date-prompt (arg)
  20741. "Change the date of this item. Date is prompted for, with default today.
  20742. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  20743. be used to request time specification in the time stamp."
  20744. (interactive "P")
  20745. (org-agenda-check-type t 'agenda 'timeline)
  20746. (org-agenda-check-no-diary)
  20747. (let* ((marker (or (get-text-property (point) 'org-marker)
  20748. (org-agenda-error)))
  20749. (buffer (marker-buffer marker))
  20750. (pos (marker-position marker)))
  20751. (org-with-remote-undo buffer
  20752. (with-current-buffer buffer
  20753. (widen)
  20754. (goto-char pos)
  20755. (if (not (org-at-timestamp-p))
  20756. (error "Cannot find time stamp"))
  20757. (org-time-stamp arg)
  20758. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  20759. (defun org-agenda-schedule (arg)
  20760. "Schedule the item at point."
  20761. (interactive "P")
  20762. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20763. (org-agenda-check-no-diary)
  20764. (let* ((marker (or (get-text-property (point) 'org-marker)
  20765. (org-agenda-error)))
  20766. (buffer (marker-buffer marker))
  20767. (pos (marker-position marker))
  20768. (org-insert-labeled-timestamps-at-point nil)
  20769. ts)
  20770. (org-with-remote-undo buffer
  20771. (with-current-buffer buffer
  20772. (widen)
  20773. (goto-char pos)
  20774. (setq ts (org-schedule arg)))
  20775. (org-agenda-show-new-time marker ts "S"))
  20776. (message "Item scheduled for %s" ts)))
  20777. (defun org-agenda-deadline (arg)
  20778. "Schedule the item at point."
  20779. (interactive "P")
  20780. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20781. (org-agenda-check-no-diary)
  20782. (let* ((marker (or (get-text-property (point) 'org-marker)
  20783. (org-agenda-error)))
  20784. (buffer (marker-buffer marker))
  20785. (pos (marker-position marker))
  20786. (org-insert-labeled-timestamps-at-point nil)
  20787. ts)
  20788. (org-with-remote-undo buffer
  20789. (with-current-buffer buffer
  20790. (widen)
  20791. (goto-char pos)
  20792. (setq ts (org-deadline arg)))
  20793. (org-agenda-show-new-time marker ts "S"))
  20794. (message "Deadline for this item set to %s" ts)))
  20795. (defun org-get-heading (&optional no-tags)
  20796. "Return the heading of the current entry, without the stars."
  20797. (save-excursion
  20798. (org-back-to-heading t)
  20799. (if (looking-at
  20800. (if no-tags
  20801. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  20802. "\\*+[ \t]+\\([^\r\n]*\\)"))
  20803. (match-string 1) "")))
  20804. (defun org-agenda-clock-in (&optional arg)
  20805. "Start the clock on the currently selected item."
  20806. (interactive "P")
  20807. (org-agenda-check-no-diary)
  20808. (let* ((marker (or (get-text-property (point) 'org-marker)
  20809. (org-agenda-error)))
  20810. (pos (marker-position marker)))
  20811. (org-with-remote-undo (marker-buffer marker)
  20812. (with-current-buffer (marker-buffer marker)
  20813. (widen)
  20814. (goto-char pos)
  20815. (org-clock-in)))))
  20816. (defun org-agenda-clock-out (&optional arg)
  20817. "Stop the currently running clock."
  20818. (interactive "P")
  20819. (unless (marker-buffer org-clock-marker)
  20820. (error "No running clock"))
  20821. (org-with-remote-undo (marker-buffer org-clock-marker)
  20822. (org-clock-out)))
  20823. (defun org-agenda-clock-cancel (&optional arg)
  20824. "Cancel the currently running clock."
  20825. (interactive "P")
  20826. (unless (marker-buffer org-clock-marker)
  20827. (error "No running clock"))
  20828. (org-with-remote-undo (marker-buffer org-clock-marker)
  20829. (org-clock-cancel)))
  20830. (defun org-agenda-diary-entry ()
  20831. "Make a diary entry, like the `i' command from the calendar.
  20832. All the standard commands work: block, weekly etc."
  20833. (interactive)
  20834. (org-agenda-check-type t 'agenda 'timeline)
  20835. (require 'diary-lib)
  20836. (let* ((char (progn
  20837. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  20838. (read-char-exclusive)))
  20839. (cmd (cdr (assoc char
  20840. '((?d . insert-diary-entry)
  20841. (?w . insert-weekly-diary-entry)
  20842. (?m . insert-monthly-diary-entry)
  20843. (?y . insert-yearly-diary-entry)
  20844. (?a . insert-anniversary-diary-entry)
  20845. (?b . insert-block-diary-entry)
  20846. (?c . insert-cyclic-diary-entry)))))
  20847. (oldf (symbol-function 'calendar-cursor-to-date))
  20848. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  20849. (point (point))
  20850. (mark (or (mark t) (point))))
  20851. (unless cmd
  20852. (error "No command associated with <%c>" char))
  20853. (unless (and (get-text-property point 'day)
  20854. (or (not (equal ?b char))
  20855. (get-text-property mark 'day)))
  20856. (error "Don't know which date to use for diary entry"))
  20857. ;; We implement this by hacking the `calendar-cursor-to-date' function
  20858. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  20859. (let ((calendar-mark-ring
  20860. (list (calendar-gregorian-from-absolute
  20861. (or (get-text-property mark 'day)
  20862. (get-text-property point 'day))))))
  20863. (unwind-protect
  20864. (progn
  20865. (fset 'calendar-cursor-to-date
  20866. (lambda (&optional error)
  20867. (calendar-gregorian-from-absolute
  20868. (get-text-property point 'day))))
  20869. (call-interactively cmd))
  20870. (fset 'calendar-cursor-to-date oldf)))))
  20871. (defun org-agenda-execute-calendar-command (cmd)
  20872. "Execute a calendar command from the agenda, with the date associated to
  20873. the cursor position."
  20874. (org-agenda-check-type t 'agenda 'timeline)
  20875. (require 'diary-lib)
  20876. (unless (get-text-property (point) 'day)
  20877. (error "Don't know which date to use for calendar command"))
  20878. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  20879. (point (point))
  20880. (date (calendar-gregorian-from-absolute
  20881. (get-text-property point 'day)))
  20882. ;; the following 3 vars are needed in the calendar
  20883. (displayed-day (extract-calendar-day date))
  20884. (displayed-month (extract-calendar-month date))
  20885. (displayed-year (extract-calendar-year date)))
  20886. (unwind-protect
  20887. (progn
  20888. (fset 'calendar-cursor-to-date
  20889. (lambda (&optional error)
  20890. (calendar-gregorian-from-absolute
  20891. (get-text-property point 'day))))
  20892. (call-interactively cmd))
  20893. (fset 'calendar-cursor-to-date oldf))))
  20894. (defun org-agenda-phases-of-moon ()
  20895. "Display the phases of the moon for the 3 months around the cursor date."
  20896. (interactive)
  20897. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  20898. (defun org-agenda-holidays ()
  20899. "Display the holidays for the 3 months around the cursor date."
  20900. (interactive)
  20901. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  20902. (defun org-agenda-sunrise-sunset (arg)
  20903. "Display sunrise and sunset for the cursor date.
  20904. Latitude and longitude can be specified with the variables
  20905. `calendar-latitude' and `calendar-longitude'. When called with prefix
  20906. argument, latitude and longitude will be prompted for."
  20907. (interactive "P")
  20908. (let ((calendar-longitude (if arg nil calendar-longitude))
  20909. (calendar-latitude (if arg nil calendar-latitude))
  20910. (calendar-location-name
  20911. (if arg "the given coordinates" calendar-location-name)))
  20912. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  20913. (defun org-agenda-goto-calendar ()
  20914. "Open the Emacs calendar with the date at the cursor."
  20915. (interactive)
  20916. (org-agenda-check-type t 'agenda 'timeline)
  20917. (let* ((day (or (get-text-property (point) 'day)
  20918. (error "Don't know which date to open in calendar")))
  20919. (date (calendar-gregorian-from-absolute day))
  20920. (calendar-move-hook nil)
  20921. (view-calendar-holidays-initially nil)
  20922. (view-diary-entries-initially nil))
  20923. (calendar)
  20924. (calendar-goto-date date)))
  20925. (defun org-calendar-goto-agenda ()
  20926. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  20927. This is a command that has to be installed in `calendar-mode-map'."
  20928. (interactive)
  20929. (org-agenda-list nil (calendar-absolute-from-gregorian
  20930. (calendar-cursor-to-date))
  20931. nil))
  20932. (defun org-agenda-convert-date ()
  20933. (interactive)
  20934. (org-agenda-check-type t 'agenda 'timeline)
  20935. (let ((day (get-text-property (point) 'day))
  20936. date s)
  20937. (unless day
  20938. (error "Don't know which date to convert"))
  20939. (setq date (calendar-gregorian-from-absolute day))
  20940. (setq s (concat
  20941. "Gregorian: " (calendar-date-string date) "\n"
  20942. "ISO: " (calendar-iso-date-string date) "\n"
  20943. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  20944. "Julian: " (calendar-julian-date-string date) "\n"
  20945. "Astron. JD: " (calendar-astro-date-string date)
  20946. " (Julian date number at noon UTC)\n"
  20947. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  20948. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  20949. "French: " (calendar-french-date-string date) "\n"
  20950. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  20951. "Mayan: " (calendar-mayan-date-string date) "\n"
  20952. "Coptic: " (calendar-coptic-date-string date) "\n"
  20953. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  20954. "Persian: " (calendar-persian-date-string date) "\n"
  20955. "Chinese: " (calendar-chinese-date-string date) "\n"))
  20956. (with-output-to-temp-buffer "*Dates*"
  20957. (princ s))
  20958. (if (fboundp 'fit-window-to-buffer)
  20959. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  20960. ;;;; Embedded LaTeX
  20961. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  20962. "Keymap for the minor `org-cdlatex-mode'.")
  20963. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  20964. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  20965. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  20966. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  20967. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  20968. (defvar org-cdlatex-texmathp-advice-is-done nil
  20969. "Flag remembering if we have applied the advice to texmathp already.")
  20970. (define-minor-mode org-cdlatex-mode
  20971. "Toggle the minor `org-cdlatex-mode'.
  20972. This mode supports entering LaTeX environment and math in LaTeX fragments
  20973. in Org-mode.
  20974. \\{org-cdlatex-mode-map}"
  20975. nil " OCDL" nil
  20976. (when org-cdlatex-mode (require 'cdlatex))
  20977. (unless org-cdlatex-texmathp-advice-is-done
  20978. (setq org-cdlatex-texmathp-advice-is-done t)
  20979. (defadvice texmathp (around org-math-always-on activate)
  20980. "Always return t in org-mode buffers.
  20981. This is because we want to insert math symbols without dollars even outside
  20982. the LaTeX math segments. If Orgmode thinks that point is actually inside
  20983. en embedded LaTeX fragement, let texmathp do its job.
  20984. \\[org-cdlatex-mode-map]"
  20985. (interactive)
  20986. (let (p)
  20987. (cond
  20988. ((not (org-mode-p)) ad-do-it)
  20989. ((eq this-command 'cdlatex-math-symbol)
  20990. (setq ad-return-value t
  20991. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  20992. (t
  20993. (let ((p (org-inside-LaTeX-fragment-p)))
  20994. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  20995. (setq ad-return-value t
  20996. texmathp-why '("Org-mode embedded math" . 0))
  20997. (if p ad-do-it)))))))))
  20998. (defun turn-on-org-cdlatex ()
  20999. "Unconditionally turn on `org-cdlatex-mode'."
  21000. (org-cdlatex-mode 1))
  21001. (defun org-inside-LaTeX-fragment-p ()
  21002. "Test if point is inside a LaTeX fragment.
  21003. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21004. sequence appearing also before point.
  21005. Even though the matchers for math are configurable, this function assumes
  21006. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21007. delimiters are skipped when they have been removed by customization.
  21008. The return value is nil, or a cons cell with the delimiter and
  21009. and the position of this delimiter.
  21010. This function does a reasonably good job, but can locally be fooled by
  21011. for example currency specifications. For example it will assume being in
  21012. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21013. fragments that are properly closed, but during editing, we have to live
  21014. with the uncertainty caused by missing closing delimiters. This function
  21015. looks only before point, not after."
  21016. (catch 'exit
  21017. (let ((pos (point))
  21018. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21019. (lim (progn
  21020. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21021. (point)))
  21022. dd-on str (start 0) m re)
  21023. (goto-char pos)
  21024. (when dodollar
  21025. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21026. re (nth 1 (assoc "$" org-latex-regexps)))
  21027. (while (string-match re str start)
  21028. (cond
  21029. ((= (match-end 0) (length str))
  21030. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21031. ((= (match-end 0) (- (length str) 5))
  21032. (throw 'exit nil))
  21033. (t (setq start (match-end 0))))))
  21034. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21035. (goto-char pos)
  21036. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21037. (and (match-beginning 2) (throw 'exit nil))
  21038. ;; count $$
  21039. (while (re-search-backward "\\$\\$" lim t)
  21040. (setq dd-on (not dd-on)))
  21041. (goto-char pos)
  21042. (if dd-on (cons "$$" m))))))
  21043. (defun org-try-cdlatex-tab ()
  21044. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21045. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21046. - inside a LaTeX fragment, or
  21047. - after the first word in a line, where an abbreviation expansion could
  21048. insert a LaTeX environment."
  21049. (when org-cdlatex-mode
  21050. (cond
  21051. ((save-excursion
  21052. (skip-chars-backward "a-zA-Z0-9*")
  21053. (skip-chars-backward " \t")
  21054. (bolp))
  21055. (cdlatex-tab) t)
  21056. ((org-inside-LaTeX-fragment-p)
  21057. (cdlatex-tab) t)
  21058. (t nil))))
  21059. (defun org-cdlatex-underscore-caret (&optional arg)
  21060. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21061. Revert to the normal definition outside of these fragments."
  21062. (interactive "P")
  21063. (if (org-inside-LaTeX-fragment-p)
  21064. (call-interactively 'cdlatex-sub-superscript)
  21065. (let (org-cdlatex-mode)
  21066. (call-interactively (key-binding (vector last-input-event))))))
  21067. (defun org-cdlatex-math-modify (&optional arg)
  21068. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21069. Revert to the normal definition outside of these fragments."
  21070. (interactive "P")
  21071. (if (org-inside-LaTeX-fragment-p)
  21072. (call-interactively 'cdlatex-math-modify)
  21073. (let (org-cdlatex-mode)
  21074. (call-interactively (key-binding (vector last-input-event))))))
  21075. (defvar org-latex-fragment-image-overlays nil
  21076. "List of overlays carrying the images of latex fragments.")
  21077. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21078. (defun org-remove-latex-fragment-image-overlays ()
  21079. "Remove all overlays with LaTeX fragment images in current buffer."
  21080. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21081. (setq org-latex-fragment-image-overlays nil))
  21082. (defun org-preview-latex-fragment (&optional subtree)
  21083. "Preview the LaTeX fragment at point, or all locally or globally.
  21084. If the cursor is in a LaTeX fragment, create the image and overlay
  21085. it over the source code. If there is no fragment at point, display
  21086. all fragments in the current text, from one headline to the next. With
  21087. prefix SUBTREE, display all fragments in the current subtree. With a
  21088. double prefix `C-u C-u', or when the cursor is before the first headline,
  21089. display all fragments in the buffer.
  21090. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21091. (interactive "P")
  21092. (org-remove-latex-fragment-image-overlays)
  21093. (save-excursion
  21094. (save-restriction
  21095. (let (beg end at msg)
  21096. (cond
  21097. ((or (equal subtree '(16))
  21098. (not (save-excursion
  21099. (re-search-backward (concat "^" outline-regexp) nil t))))
  21100. (setq beg (point-min) end (point-max)
  21101. msg "Creating images for buffer...%s"))
  21102. ((equal subtree '(4))
  21103. (org-back-to-heading)
  21104. (setq beg (point) end (org-end-of-subtree t)
  21105. msg "Creating images for subtree...%s"))
  21106. (t
  21107. (if (setq at (org-inside-LaTeX-fragment-p))
  21108. (goto-char (max (point-min) (- (cdr at) 2)))
  21109. (org-back-to-heading))
  21110. (setq beg (point) end (progn (outline-next-heading) (point))
  21111. msg (if at "Creating image...%s"
  21112. "Creating images for entry...%s"))))
  21113. (message msg "")
  21114. (narrow-to-region beg end)
  21115. (goto-char beg)
  21116. (org-format-latex
  21117. (concat "ltxpng/" (file-name-sans-extension
  21118. (file-name-nondirectory
  21119. buffer-file-name)))
  21120. default-directory 'overlays msg at 'forbuffer)
  21121. (message msg "done. Use `C-c C-c' to remove images.")))))
  21122. (defvar org-latex-regexps
  21123. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21124. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21125. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21126. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21127. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21128. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21129. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21130. "Regular expressions for matching embedded LaTeX.")
  21131. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21132. "Replace LaTeX fragments with links to an image, and produce images."
  21133. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21134. (let* ((prefixnodir (file-name-nondirectory prefix))
  21135. (absprefix (expand-file-name prefix dir))
  21136. (todir (file-name-directory absprefix))
  21137. (opt org-format-latex-options)
  21138. (matchers (plist-get opt :matchers))
  21139. (re-list org-latex-regexps)
  21140. (cnt 0) txt link beg end re e checkdir
  21141. m n block linkfile movefile ov)
  21142. ;; Check if there are old images files with this prefix, and remove them
  21143. (when (file-directory-p todir)
  21144. (mapc 'delete-file
  21145. (directory-files
  21146. todir 'full
  21147. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21148. ;; Check the different regular expressions
  21149. (while (setq e (pop re-list))
  21150. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21151. block (if (nth 3 e) "\n\n" ""))
  21152. (when (member m matchers)
  21153. (goto-char (point-min))
  21154. (while (re-search-forward re nil t)
  21155. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21156. (setq txt (match-string n)
  21157. beg (match-beginning n) end (match-end n)
  21158. cnt (1+ cnt)
  21159. linkfile (format "%s_%04d.png" prefix cnt)
  21160. movefile (format "%s_%04d.png" absprefix cnt)
  21161. link (concat block "[[file:" linkfile "]]" block))
  21162. (if msg (message msg cnt))
  21163. (goto-char beg)
  21164. (unless checkdir ; make sure the directory exists
  21165. (setq checkdir t)
  21166. (or (file-directory-p todir) (make-directory todir)))
  21167. (org-create-formula-image
  21168. txt movefile opt forbuffer)
  21169. (if overlays
  21170. (progn
  21171. (setq ov (org-make-overlay beg end))
  21172. (if (featurep 'xemacs)
  21173. (progn
  21174. (org-overlay-put ov 'invisible t)
  21175. (org-overlay-put
  21176. ov 'end-glyph
  21177. (make-glyph (vector 'png :file movefile))))
  21178. (org-overlay-put
  21179. ov 'display
  21180. (list 'image :type 'png :file movefile :ascent 'center)))
  21181. (push ov org-latex-fragment-image-overlays)
  21182. (goto-char end))
  21183. (delete-region beg end)
  21184. (insert link))))))))
  21185. ;; This function borrows from Ganesh Swami's latex2png.el
  21186. (defun org-create-formula-image (string tofile options buffer)
  21187. (let* ((tmpdir (if (featurep 'xemacs)
  21188. (temp-directory)
  21189. temporary-file-directory))
  21190. (texfilebase (make-temp-name
  21191. (expand-file-name "orgtex" tmpdir)))
  21192. (texfile (concat texfilebase ".tex"))
  21193. (dvifile (concat texfilebase ".dvi"))
  21194. (pngfile (concat texfilebase ".png"))
  21195. (fnh (face-attribute 'default :height nil))
  21196. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21197. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21198. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21199. "Black"))
  21200. (bg (or (plist-get options (if buffer :background :html-background))
  21201. "Transparent")))
  21202. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21203. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21204. (with-temp-file texfile
  21205. (insert org-format-latex-header
  21206. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21207. (let ((dir default-directory))
  21208. (condition-case nil
  21209. (progn
  21210. (cd tmpdir)
  21211. (call-process "latex" nil nil nil texfile))
  21212. (error nil))
  21213. (cd dir))
  21214. (if (not (file-exists-p dvifile))
  21215. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21216. (call-process "dvipng" nil nil nil
  21217. "-E" "-fg" fg "-bg" bg
  21218. "-D" dpi
  21219. ;;"-x" scale "-y" scale
  21220. "-T" "tight"
  21221. "-o" pngfile
  21222. dvifile)
  21223. (if (not (file-exists-p pngfile))
  21224. (progn (message "Failed to create png file from %s" texfile) nil)
  21225. ;; Use the requested file name and clean up
  21226. (copy-file pngfile tofile 'replace)
  21227. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21228. (delete-file (concat texfilebase e)))
  21229. pngfile))))
  21230. (defun org-dvipng-color (attr)
  21231. "Return an rgb color specification for dvipng."
  21232. (apply 'format "rgb %s %s %s"
  21233. (mapcar 'org-normalize-color
  21234. (color-values (face-attribute 'default attr nil)))))
  21235. (defun org-normalize-color (value)
  21236. "Return string to be used as color value for an RGB component."
  21237. (format "%g" (/ value 65535.0)))
  21238. ;;;; Exporting
  21239. ;;; Variables, constants, and parameter plists
  21240. (defconst org-level-max 20)
  21241. (defvar org-export-html-preamble nil
  21242. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21243. (defvar org-export-html-postamble nil
  21244. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21245. (defvar org-export-html-auto-preamble t
  21246. "Should default preamble be inserted? Set by publishing functions.")
  21247. (defvar org-export-html-auto-postamble t
  21248. "Should default postamble be inserted? Set by publishing functions.")
  21249. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21250. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21251. (defconst org-export-plist-vars
  21252. '((:language . org-export-default-language)
  21253. (:customtime . org-display-custom-times)
  21254. (:headline-levels . org-export-headline-levels)
  21255. (:section-numbers . org-export-with-section-numbers)
  21256. (:table-of-contents . org-export-with-toc)
  21257. (:preserve-breaks . org-export-preserve-breaks)
  21258. (:archived-trees . org-export-with-archived-trees)
  21259. (:emphasize . org-export-with-emphasize)
  21260. (:sub-superscript . org-export-with-sub-superscripts)
  21261. (:special-strings . org-export-with-special-strings)
  21262. (:footnotes . org-export-with-footnotes)
  21263. (:drawers . org-export-with-drawers)
  21264. (:tags . org-export-with-tags)
  21265. (:TeX-macros . org-export-with-TeX-macros)
  21266. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21267. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21268. (:fixed-width . org-export-with-fixed-width)
  21269. (:timestamps . org-export-with-timestamps)
  21270. (:author-info . org-export-author-info)
  21271. (:time-stamp-file . org-export-time-stamp-file)
  21272. (:tables . org-export-with-tables)
  21273. (:table-auto-headline . org-export-highlight-first-table-line)
  21274. (:style . org-export-html-style)
  21275. (:agenda-style . org-agenda-export-html-style)
  21276. (:convert-org-links . org-export-html-link-org-files-as-html)
  21277. (:inline-images . org-export-html-inline-images)
  21278. (:html-extension . org-export-html-extension)
  21279. (:html-table-tag . org-export-html-table-tag)
  21280. (:expand-quoted-html . org-export-html-expand)
  21281. (:timestamp . org-export-html-with-timestamp)
  21282. (:publishing-directory . org-export-publishing-directory)
  21283. (:preamble . org-export-html-preamble)
  21284. (:postamble . org-export-html-postamble)
  21285. (:auto-preamble . org-export-html-auto-preamble)
  21286. (:auto-postamble . org-export-html-auto-postamble)
  21287. (:author . user-full-name)
  21288. (:email . user-mail-address)))
  21289. (defun org-default-export-plist ()
  21290. "Return the property list with default settings for the export variables."
  21291. (let ((l org-export-plist-vars) rtn e)
  21292. (while (setq e (pop l))
  21293. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21294. rtn))
  21295. (defun org-infile-export-plist ()
  21296. "Return the property list with file-local settings for export."
  21297. (save-excursion
  21298. (goto-char 0)
  21299. (let ((re (org-make-options-regexp
  21300. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21301. p key val text options)
  21302. (while (re-search-forward re nil t)
  21303. (setq key (org-match-string-no-properties 1)
  21304. val (org-match-string-no-properties 2))
  21305. (cond
  21306. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21307. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21308. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21309. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21310. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21311. ((string-equal key "TEXT")
  21312. (setq text (if text (concat text "\n" val) val)))
  21313. ((string-equal key "OPTIONS") (setq options val))))
  21314. (setq p (plist-put p :text text))
  21315. (when options
  21316. (let ((op '(("H" . :headline-levels)
  21317. ("num" . :section-numbers)
  21318. ("toc" . :table-of-contents)
  21319. ("\\n" . :preserve-breaks)
  21320. ("@" . :expand-quoted-html)
  21321. (":" . :fixed-width)
  21322. ("|" . :tables)
  21323. ("^" . :sub-superscript)
  21324. ("-" . :special-strings)
  21325. ("f" . :footnotes)
  21326. ("d" . :drawers)
  21327. ("tags" . :tags)
  21328. ("*" . :emphasize)
  21329. ("TeX" . :TeX-macros)
  21330. ("LaTeX" . :LaTeX-fragments)
  21331. ("skip" . :skip-before-1st-heading)
  21332. ("author" . :author-info)
  21333. ("timestamp" . :time-stamp-file)))
  21334. o)
  21335. (while (setq o (pop op))
  21336. (if (string-match (concat (regexp-quote (car o))
  21337. ":\\([^ \t\n\r;,.]*\\)")
  21338. options)
  21339. (setq p (plist-put p (cdr o)
  21340. (car (read-from-string
  21341. (match-string 1 options)))))))))
  21342. p)))
  21343. (defun org-export-directory (type plist)
  21344. (let* ((val (plist-get plist :publishing-directory))
  21345. (dir (if (listp val)
  21346. (or (cdr (assoc type val)) ".")
  21347. val)))
  21348. dir))
  21349. (defun org-skip-comments (lines)
  21350. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21351. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21352. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21353. (case-fold-search nil)
  21354. rtn line level)
  21355. (while (setq line (pop lines))
  21356. (cond
  21357. ((and (string-match re1 line)
  21358. (setq level (- (match-end 1) (match-beginning 1))))
  21359. ;; Beginning of a COMMENT subtree. Skip it.
  21360. (while (and (setq line (pop lines))
  21361. (or (not (string-match re2 line))
  21362. (> (- (match-end 1) (match-beginning 1)) level))))
  21363. (setq lines (cons line lines)))
  21364. ((string-match "^#" line)
  21365. ;; an ordinary comment line
  21366. )
  21367. ((and org-export-table-remove-special-lines
  21368. (string-match "^[ \t]*|" line)
  21369. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21370. (and (string-match "| *<[0-9]+> *|" line)
  21371. (not (string-match "| *[^ <|]" line)))))
  21372. ;; a special table line that should be removed
  21373. )
  21374. (t (setq rtn (cons line rtn)))))
  21375. (nreverse rtn)))
  21376. (defun org-export (&optional arg)
  21377. (interactive)
  21378. (let ((help "[t] insert the export option template
  21379. \[v] limit export to visible part of outline tree
  21380. \[a] export as ASCII
  21381. \[h] export as HTML
  21382. \[H] export as HTML to temporary buffer
  21383. \[R] export region as HTML
  21384. \[b] export as HTML and browse immediately
  21385. \[x] export as XOXO
  21386. \[l] export as LaTeX
  21387. \[L] export as LaTeX to temporary buffer
  21388. \[i] export current file as iCalendar file
  21389. \[I] export all agenda files as iCalendar files
  21390. \[c] export agenda files into combined iCalendar file
  21391. \[F] publish current file
  21392. \[P] publish current project
  21393. \[X] publish... (project will be prompted for)
  21394. \[A] publish all projects")
  21395. (cmds
  21396. '((?t . org-insert-export-options-template)
  21397. (?v . org-export-visible)
  21398. (?a . org-export-as-ascii)
  21399. (?h . org-export-as-html)
  21400. (?b . org-export-as-html-and-open)
  21401. (?H . org-export-as-html-to-buffer)
  21402. (?R . org-export-region-as-html)
  21403. (?x . org-export-as-xoxo)
  21404. (?l . org-export-as-latex)
  21405. (?L . org-export-as-latex-to-buffer)
  21406. (?i . org-export-icalendar-this-file)
  21407. (?I . org-export-icalendar-all-agenda-files)
  21408. (?c . org-export-icalendar-combine-agenda-files)
  21409. (?F . org-publish-current-file)
  21410. (?P . org-publish-current-project)
  21411. (?X . org-publish)
  21412. (?A . org-publish-all)))
  21413. r1 r2 ass)
  21414. (save-window-excursion
  21415. (delete-other-windows)
  21416. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21417. (princ help))
  21418. (message "Select command: ")
  21419. (setq r1 (read-char-exclusive)))
  21420. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21421. (if (setq ass (assq r2 cmds))
  21422. (call-interactively (cdr ass))
  21423. (error "No command associated with key %c" r1))))
  21424. (defconst org-html-entities
  21425. '(("nbsp")
  21426. ("iexcl")
  21427. ("cent")
  21428. ("pound")
  21429. ("curren")
  21430. ("yen")
  21431. ("brvbar")
  21432. ("vert" . "&#124;")
  21433. ("sect")
  21434. ("uml")
  21435. ("copy")
  21436. ("ordf")
  21437. ("laquo")
  21438. ("not")
  21439. ("shy")
  21440. ("reg")
  21441. ("macr")
  21442. ("deg")
  21443. ("plusmn")
  21444. ("sup2")
  21445. ("sup3")
  21446. ("acute")
  21447. ("micro")
  21448. ("para")
  21449. ("middot")
  21450. ("odot"."o")
  21451. ("star"."*")
  21452. ("cedil")
  21453. ("sup1")
  21454. ("ordm")
  21455. ("raquo")
  21456. ("frac14")
  21457. ("frac12")
  21458. ("frac34")
  21459. ("iquest")
  21460. ("Agrave")
  21461. ("Aacute")
  21462. ("Acirc")
  21463. ("Atilde")
  21464. ("Auml")
  21465. ("Aring") ("AA"."&Aring;")
  21466. ("AElig")
  21467. ("Ccedil")
  21468. ("Egrave")
  21469. ("Eacute")
  21470. ("Ecirc")
  21471. ("Euml")
  21472. ("Igrave")
  21473. ("Iacute")
  21474. ("Icirc")
  21475. ("Iuml")
  21476. ("ETH")
  21477. ("Ntilde")
  21478. ("Ograve")
  21479. ("Oacute")
  21480. ("Ocirc")
  21481. ("Otilde")
  21482. ("Ouml")
  21483. ("times")
  21484. ("Oslash")
  21485. ("Ugrave")
  21486. ("Uacute")
  21487. ("Ucirc")
  21488. ("Uuml")
  21489. ("Yacute")
  21490. ("THORN")
  21491. ("szlig")
  21492. ("agrave")
  21493. ("aacute")
  21494. ("acirc")
  21495. ("atilde")
  21496. ("auml")
  21497. ("aring")
  21498. ("aelig")
  21499. ("ccedil")
  21500. ("egrave")
  21501. ("eacute")
  21502. ("ecirc")
  21503. ("euml")
  21504. ("igrave")
  21505. ("iacute")
  21506. ("icirc")
  21507. ("iuml")
  21508. ("eth")
  21509. ("ntilde")
  21510. ("ograve")
  21511. ("oacute")
  21512. ("ocirc")
  21513. ("otilde")
  21514. ("ouml")
  21515. ("divide")
  21516. ("oslash")
  21517. ("ugrave")
  21518. ("uacute")
  21519. ("ucirc")
  21520. ("uuml")
  21521. ("yacute")
  21522. ("thorn")
  21523. ("yuml")
  21524. ("fnof")
  21525. ("Alpha")
  21526. ("Beta")
  21527. ("Gamma")
  21528. ("Delta")
  21529. ("Epsilon")
  21530. ("Zeta")
  21531. ("Eta")
  21532. ("Theta")
  21533. ("Iota")
  21534. ("Kappa")
  21535. ("Lambda")
  21536. ("Mu")
  21537. ("Nu")
  21538. ("Xi")
  21539. ("Omicron")
  21540. ("Pi")
  21541. ("Rho")
  21542. ("Sigma")
  21543. ("Tau")
  21544. ("Upsilon")
  21545. ("Phi")
  21546. ("Chi")
  21547. ("Psi")
  21548. ("Omega")
  21549. ("alpha")
  21550. ("beta")
  21551. ("gamma")
  21552. ("delta")
  21553. ("epsilon")
  21554. ("varepsilon"."&epsilon;")
  21555. ("zeta")
  21556. ("eta")
  21557. ("theta")
  21558. ("iota")
  21559. ("kappa")
  21560. ("lambda")
  21561. ("mu")
  21562. ("nu")
  21563. ("xi")
  21564. ("omicron")
  21565. ("pi")
  21566. ("rho")
  21567. ("sigmaf") ("varsigma"."&sigmaf;")
  21568. ("sigma")
  21569. ("tau")
  21570. ("upsilon")
  21571. ("phi")
  21572. ("chi")
  21573. ("psi")
  21574. ("omega")
  21575. ("thetasym") ("vartheta"."&thetasym;")
  21576. ("upsih")
  21577. ("piv")
  21578. ("bull") ("bullet"."&bull;")
  21579. ("hellip") ("dots"."&hellip;")
  21580. ("prime")
  21581. ("Prime")
  21582. ("oline")
  21583. ("frasl")
  21584. ("weierp")
  21585. ("image")
  21586. ("real")
  21587. ("trade")
  21588. ("alefsym")
  21589. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21590. ("uarr") ("uparrow"."&uarr;")
  21591. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21592. ("darr")("downarrow"."&darr;")
  21593. ("harr") ("leftrightarrow"."&harr;")
  21594. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21595. ("lArr") ("Leftarrow"."&lArr;")
  21596. ("uArr") ("Uparrow"."&uArr;")
  21597. ("rArr") ("Rightarrow"."&rArr;")
  21598. ("dArr") ("Downarrow"."&dArr;")
  21599. ("hArr") ("Leftrightarrow"."&hArr;")
  21600. ("forall")
  21601. ("part") ("partial"."&part;")
  21602. ("exist") ("exists"."&exist;")
  21603. ("empty") ("emptyset"."&empty;")
  21604. ("nabla")
  21605. ("isin") ("in"."&isin;")
  21606. ("notin")
  21607. ("ni")
  21608. ("prod")
  21609. ("sum")
  21610. ("minus")
  21611. ("lowast") ("ast"."&lowast;")
  21612. ("radic")
  21613. ("prop") ("proptp"."&prop;")
  21614. ("infin") ("infty"."&infin;")
  21615. ("ang") ("angle"."&ang;")
  21616. ("and") ("wedge"."&and;")
  21617. ("or") ("vee"."&or;")
  21618. ("cap")
  21619. ("cup")
  21620. ("int")
  21621. ("there4")
  21622. ("sim")
  21623. ("cong") ("simeq"."&cong;")
  21624. ("asymp")("approx"."&asymp;")
  21625. ("ne") ("neq"."&ne;")
  21626. ("equiv")
  21627. ("le")
  21628. ("ge")
  21629. ("sub") ("subset"."&sub;")
  21630. ("sup") ("supset"."&sup;")
  21631. ("nsub")
  21632. ("sube")
  21633. ("supe")
  21634. ("oplus")
  21635. ("otimes")
  21636. ("perp")
  21637. ("sdot") ("cdot"."&sdot;")
  21638. ("lceil")
  21639. ("rceil")
  21640. ("lfloor")
  21641. ("rfloor")
  21642. ("lang")
  21643. ("rang")
  21644. ("loz") ("Diamond"."&loz;")
  21645. ("spades") ("spadesuit"."&spades;")
  21646. ("clubs") ("clubsuit"."&clubs;")
  21647. ("hearts") ("diamondsuit"."&hearts;")
  21648. ("diams") ("diamondsuit"."&diams;")
  21649. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  21650. ("quot")
  21651. ("amp")
  21652. ("lt")
  21653. ("gt")
  21654. ("OElig")
  21655. ("oelig")
  21656. ("Scaron")
  21657. ("scaron")
  21658. ("Yuml")
  21659. ("circ")
  21660. ("tilde")
  21661. ("ensp")
  21662. ("emsp")
  21663. ("thinsp")
  21664. ("zwnj")
  21665. ("zwj")
  21666. ("lrm")
  21667. ("rlm")
  21668. ("ndash")
  21669. ("mdash")
  21670. ("lsquo")
  21671. ("rsquo")
  21672. ("sbquo")
  21673. ("ldquo")
  21674. ("rdquo")
  21675. ("bdquo")
  21676. ("dagger")
  21677. ("Dagger")
  21678. ("permil")
  21679. ("lsaquo")
  21680. ("rsaquo")
  21681. ("euro")
  21682. ("arccos"."arccos")
  21683. ("arcsin"."arcsin")
  21684. ("arctan"."arctan")
  21685. ("arg"."arg")
  21686. ("cos"."cos")
  21687. ("cosh"."cosh")
  21688. ("cot"."cot")
  21689. ("coth"."coth")
  21690. ("csc"."csc")
  21691. ("deg"."deg")
  21692. ("det"."det")
  21693. ("dim"."dim")
  21694. ("exp"."exp")
  21695. ("gcd"."gcd")
  21696. ("hom"."hom")
  21697. ("inf"."inf")
  21698. ("ker"."ker")
  21699. ("lg"."lg")
  21700. ("lim"."lim")
  21701. ("liminf"."liminf")
  21702. ("limsup"."limsup")
  21703. ("ln"."ln")
  21704. ("log"."log")
  21705. ("max"."max")
  21706. ("min"."min")
  21707. ("Pr"."Pr")
  21708. ("sec"."sec")
  21709. ("sin"."sin")
  21710. ("sinh"."sinh")
  21711. ("sup"."sup")
  21712. ("tan"."tan")
  21713. ("tanh"."tanh")
  21714. )
  21715. "Entities for TeX->HTML translation.
  21716. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  21717. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  21718. In that case, \"\\ent\" will be translated to \"&other;\".
  21719. The list contains HTML entities for Latin-1, Greek and other symbols.
  21720. It is supplemented by a number of commonly used TeX macros with appropriate
  21721. translations. There is currently no way for users to extend this.")
  21722. ;;; General functions for all backends
  21723. (defun org-cleaned-string-for-export (string &rest parameters)
  21724. "Cleanup a buffer STRING so that links can be created safely."
  21725. (interactive)
  21726. (let* ((re-radio (and org-target-link-regexp
  21727. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  21728. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  21729. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  21730. (re-archive (concat ":" org-archive-tag ":"))
  21731. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  21732. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  21733. (htmlp (plist-get parameters :for-html))
  21734. (asciip (plist-get parameters :for-ascii))
  21735. (latexp (plist-get parameters :for-LaTeX))
  21736. (commentsp (plist-get parameters :comments))
  21737. (archived-trees (plist-get parameters :archived-trees))
  21738. (inhibit-read-only t)
  21739. (drawers org-drawers)
  21740. (exp-drawers (plist-get parameters :drawers))
  21741. (outline-regexp "\\*+ ")
  21742. a b xx
  21743. rtn p)
  21744. (with-current-buffer (get-buffer-create " org-mode-tmp")
  21745. (erase-buffer)
  21746. (insert string)
  21747. ;; Remove license-to-kill stuff
  21748. (while (setq p (text-property-any (point-min) (point-max)
  21749. :org-license-to-kill t))
  21750. (delete-region p (next-single-property-change p :org-license-to-kill)))
  21751. (let ((org-inhibit-startup t)) (org-mode))
  21752. (untabify (point-min) (point-max))
  21753. ;; Get the correct stuff before the first headline
  21754. (when (plist-get parameters :skip-before-1st-heading)
  21755. (goto-char (point-min))
  21756. (when (re-search-forward "^\\*+[ \t]" nil t)
  21757. (delete-region (point-min) (match-beginning 0))
  21758. (goto-char (point-min))
  21759. (insert "\n")))
  21760. (when (plist-get parameters :add-text)
  21761. (goto-char (point-min))
  21762. (insert (plist-get parameters :add-text) "\n"))
  21763. ;; Get rid of archived trees
  21764. (when (not (eq archived-trees t))
  21765. (goto-char (point-min))
  21766. (while (re-search-forward re-archive nil t)
  21767. (if (not (org-on-heading-p t))
  21768. (org-end-of-subtree t)
  21769. (beginning-of-line 1)
  21770. (setq a (if archived-trees
  21771. (1+ (point-at-eol)) (point))
  21772. b (org-end-of-subtree t))
  21773. (if (> b a) (delete-region a b)))))
  21774. ;; Get rid of drawers
  21775. (unless (eq t exp-drawers)
  21776. (goto-char (point-min))
  21777. (let ((re (concat "^[ \t]*:\\("
  21778. (mapconcat
  21779. 'identity
  21780. (org-delete-all exp-drawers
  21781. (copy-sequence drawers))
  21782. "\\|")
  21783. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  21784. (while (re-search-forward re nil t)
  21785. (replace-match ""))))
  21786. ;; Find targets in comments and move them out of comments,
  21787. ;; but mark them as targets that should be invisible
  21788. (goto-char (point-min))
  21789. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  21790. (replace-match "\\1(INVISIBLE)"))
  21791. ;; Protect backend specific stuff, throw away the others.
  21792. (let ((formatters
  21793. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  21794. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  21795. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  21796. fmt)
  21797. (goto-char (point-min))
  21798. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  21799. (goto-char (match-end 0))
  21800. (while (not (looking-at "#\\+END_EXAMPLE"))
  21801. (insert ": ")
  21802. (beginning-of-line 2)))
  21803. (goto-char (point-min))
  21804. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  21805. (add-text-properties (match-beginning 0) (match-end 0)
  21806. '(org-protected t)))
  21807. (while formatters
  21808. (setq fmt (pop formatters))
  21809. (when (car fmt)
  21810. (goto-char (point-min))
  21811. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  21812. ":[ \t]*\\(.*\\)") nil t)
  21813. (replace-match "\\1" t)
  21814. (add-text-properties
  21815. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  21816. '(org-protected t))))
  21817. (goto-char (point-min))
  21818. (while (re-search-forward
  21819. (concat "^#\\+"
  21820. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  21821. (cadddr fmt) "\\>.*\n?") nil t)
  21822. (if (car fmt)
  21823. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  21824. '(org-protected t))
  21825. (delete-region (match-beginning 0) (match-end 0))))))
  21826. ;; Protect quoted subtrees
  21827. (goto-char (point-min))
  21828. (while (re-search-forward re-quote nil t)
  21829. (goto-char (match-beginning 0))
  21830. (end-of-line 1)
  21831. (add-text-properties (point) (org-end-of-subtree t)
  21832. '(org-protected t)))
  21833. ;; Protect verbatim elements
  21834. (goto-char (point-min))
  21835. (while (re-search-forward org-verbatim-re nil t)
  21836. (add-text-properties (match-beginning 4) (match-end 4)
  21837. '(org-protected t))
  21838. (goto-char (1+ (match-end 4))))
  21839. ;; Remove subtrees that are commented
  21840. (goto-char (point-min))
  21841. (while (re-search-forward re-commented nil t)
  21842. (goto-char (match-beginning 0))
  21843. (delete-region (point) (org-end-of-subtree t)))
  21844. ;; Remove special table lines
  21845. (when org-export-table-remove-special-lines
  21846. (goto-char (point-min))
  21847. (while (re-search-forward "^[ \t]*|" nil t)
  21848. (beginning-of-line 1)
  21849. (if (or (looking-at "[ \t]*| *[!_^] *|")
  21850. (and (looking-at ".*?| *<[0-9]+> *|")
  21851. (not (looking-at ".*?| *[^ <|]"))))
  21852. (delete-region (max (point-min) (1- (point-at-bol)))
  21853. (point-at-eol))
  21854. (end-of-line 1))))
  21855. ;; Specific LaTeX stuff
  21856. (when latexp
  21857. (require 'org-export-latex nil)
  21858. (org-export-latex-cleaned-string))
  21859. (when asciip
  21860. (org-export-ascii-clean-string))
  21861. ;; Specific HTML stuff
  21862. (when htmlp
  21863. ;; Convert LaTeX fragments to images
  21864. (when (plist-get parameters :LaTeX-fragments)
  21865. (org-format-latex
  21866. (concat "ltxpng/" (file-name-sans-extension
  21867. (file-name-nondirectory
  21868. org-current-export-file)))
  21869. org-current-export-dir nil "Creating LaTeX image %s"))
  21870. (message "Exporting..."))
  21871. ;; Remove or replace comments
  21872. (goto-char (point-min))
  21873. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  21874. (if commentsp
  21875. (progn (add-text-properties
  21876. (match-beginning 0) (match-end 0) '(org-protected t))
  21877. (replace-match (format commentsp (match-string 1)) t t))
  21878. (replace-match "")))
  21879. ;; Find matches for radio targets and turn them into internal links
  21880. (goto-char (point-min))
  21881. (when re-radio
  21882. (while (re-search-forward re-radio nil t)
  21883. (org-if-unprotected
  21884. (replace-match "\\1[[\\2]]"))))
  21885. ;; Find all links that contain a newline and put them into a single line
  21886. (goto-char (point-min))
  21887. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  21888. (org-if-unprotected
  21889. (replace-match "\\1 \\3")
  21890. (goto-char (match-beginning 0))))
  21891. ;; Normalize links: Convert angle and plain links into bracket links
  21892. ;; Expand link abbreviations
  21893. (goto-char (point-min))
  21894. (while (re-search-forward re-plain-link nil t)
  21895. (goto-char (1- (match-end 0)))
  21896. (org-if-unprotected
  21897. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  21898. ":" (match-string 3) "]]")))
  21899. ;; added 'org-link face to links
  21900. (put-text-property 0 (length s) 'face 'org-link s)
  21901. (replace-match s t t))))
  21902. (goto-char (point-min))
  21903. (while (re-search-forward re-angle-link nil t)
  21904. (goto-char (1- (match-end 0)))
  21905. (org-if-unprotected
  21906. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  21907. ":" (match-string 3) "]]")))
  21908. (put-text-property 0 (length s) 'face 'org-link s)
  21909. (replace-match s t t))))
  21910. (goto-char (point-min))
  21911. (while (re-search-forward org-bracket-link-regexp nil t)
  21912. (org-if-unprotected
  21913. (let* ((s (concat "[[" (setq xx (save-match-data
  21914. (org-link-expand-abbrev (match-string 1))))
  21915. "]"
  21916. (if (match-end 3)
  21917. (match-string 2)
  21918. (concat "[" xx "]"))
  21919. "]")))
  21920. (put-text-property 0 (length s) 'face 'org-link s)
  21921. (replace-match s t t))))
  21922. ;; Find multiline emphasis and put them into single line
  21923. (when (plist-get parameters :emph-multiline)
  21924. (goto-char (point-min))
  21925. (while (re-search-forward org-emph-re nil t)
  21926. (if (not (= (char-after (match-beginning 3))
  21927. (char-after (match-beginning 4))))
  21928. (org-if-unprotected
  21929. (subst-char-in-region (match-beginning 0) (match-end 0)
  21930. ?\n ?\ t)
  21931. (goto-char (1- (match-end 0))))
  21932. (goto-char (1+ (match-beginning 0))))))
  21933. (setq rtn (buffer-string)))
  21934. (kill-buffer " org-mode-tmp")
  21935. rtn))
  21936. (defun org-export-grab-title-from-buffer ()
  21937. "Get a title for the current document, from looking at the buffer."
  21938. (let ((inhibit-read-only t))
  21939. (save-excursion
  21940. (goto-char (point-min))
  21941. (let ((end (save-excursion (outline-next-heading) (point))))
  21942. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  21943. ;; Mark the line so that it will not be exported as normal text.
  21944. (org-unmodified
  21945. (add-text-properties (match-beginning 0) (match-end 0)
  21946. (list :org-license-to-kill t)))
  21947. ;; Return the title string
  21948. (org-trim (match-string 0)))))))
  21949. (defun org-export-get-title-from-subtree ()
  21950. "Return subtree title and exclude it from export."
  21951. (let (title (m (mark)))
  21952. (save-excursion
  21953. (goto-char (region-beginning))
  21954. (when (and (org-at-heading-p)
  21955. (>= (org-end-of-subtree t t) (region-end)))
  21956. ;; This is a subtree, we take the title from the first heading
  21957. (goto-char (region-beginning))
  21958. (looking-at org-todo-line-regexp)
  21959. (setq title (match-string 3))
  21960. (org-unmodified
  21961. (add-text-properties (point) (1+ (point-at-eol))
  21962. (list :org-license-to-kill t)))))
  21963. title))
  21964. (defun org-solidify-link-text (s &optional alist)
  21965. "Take link text and make a safe target out of it."
  21966. (save-match-data
  21967. (let* ((rtn
  21968. (mapconcat
  21969. 'identity
  21970. (org-split-string s "[ \t\r\n]+") "--"))
  21971. (a (assoc rtn alist)))
  21972. (or (cdr a) rtn))))
  21973. (defun org-get-min-level (lines)
  21974. "Get the minimum level in LINES."
  21975. (let ((re "^\\(\\*+\\) ") l min)
  21976. (catch 'exit
  21977. (while (setq l (pop lines))
  21978. (if (string-match re l)
  21979. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  21980. 1)))
  21981. ;; Variable holding the vector with section numbers
  21982. (defvar org-section-numbers (make-vector org-level-max 0))
  21983. (defun org-init-section-numbers ()
  21984. "Initialize the vector for the section numbers."
  21985. (let* ((level -1)
  21986. (numbers (nreverse (org-split-string "" "\\.")))
  21987. (depth (1- (length org-section-numbers)))
  21988. (i depth) number-string)
  21989. (while (>= i 0)
  21990. (if (> i level)
  21991. (aset org-section-numbers i 0)
  21992. (setq number-string (or (car numbers) "0"))
  21993. (if (string-match "\\`[A-Z]\\'" number-string)
  21994. (aset org-section-numbers i
  21995. (- (string-to-char number-string) ?A -1))
  21996. (aset org-section-numbers i (string-to-number number-string)))
  21997. (pop numbers))
  21998. (setq i (1- i)))))
  21999. (defun org-section-number (&optional level)
  22000. "Return a string with the current section number.
  22001. When LEVEL is non-nil, increase section numbers on that level."
  22002. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22003. (when level
  22004. (when (> level -1)
  22005. (aset org-section-numbers
  22006. level (1+ (aref org-section-numbers level))))
  22007. (setq idx (1+ level))
  22008. (while (<= idx depth)
  22009. (if (not (= idx 1))
  22010. (aset org-section-numbers idx 0))
  22011. (setq idx (1+ idx))))
  22012. (setq idx 0)
  22013. (while (<= idx depth)
  22014. (setq n (aref org-section-numbers idx))
  22015. (setq string (concat string (if (not (string= string "")) "." "")
  22016. (int-to-string n)))
  22017. (setq idx (1+ idx)))
  22018. (save-match-data
  22019. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22020. (setq string (replace-match "" t nil string)))
  22021. (if (string-match "\\(\\.0\\)+\\'" string)
  22022. (setq string (replace-match "" t nil string))))
  22023. string))
  22024. ;;; ASCII export
  22025. (defvar org-last-level nil) ; dynamically scoped variable
  22026. (defvar org-min-level nil) ; dynamically scoped variable
  22027. (defvar org-levels-open nil) ; dynamically scoped parameter
  22028. (defvar org-ascii-current-indentation nil) ; For communication
  22029. (defun org-export-as-ascii (arg)
  22030. "Export the outline as a pretty ASCII file.
  22031. If there is an active region, export only the region.
  22032. The prefix ARG specifies how many levels of the outline should become
  22033. underlined headlines. The default is 3."
  22034. (interactive "P")
  22035. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22036. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22037. (org-infile-export-plist)))
  22038. (region-p (org-region-active-p))
  22039. (subtree-p
  22040. (when region-p
  22041. (save-excursion
  22042. (goto-char (region-beginning))
  22043. (and (org-at-heading-p)
  22044. (>= (org-end-of-subtree t t) (region-end))))))
  22045. (custom-times org-display-custom-times)
  22046. (org-ascii-current-indentation '(0 . 0))
  22047. (level 0) line txt
  22048. (umax nil)
  22049. (umax-toc nil)
  22050. (case-fold-search nil)
  22051. (filename (concat (file-name-as-directory
  22052. (org-export-directory :ascii opt-plist))
  22053. (file-name-sans-extension
  22054. (or (and subtree-p
  22055. (org-entry-get (region-beginning)
  22056. "EXPORT_FILE_NAME" t))
  22057. (file-name-nondirectory buffer-file-name)))
  22058. ".txt"))
  22059. (filename (if (equal (file-truename filename)
  22060. (file-truename buffer-file-name))
  22061. (concat filename ".txt")
  22062. filename))
  22063. (buffer (find-file-noselect filename))
  22064. (org-levels-open (make-vector org-level-max nil))
  22065. (odd org-odd-levels-only)
  22066. (date (plist-get opt-plist :date))
  22067. (author (plist-get opt-plist :author))
  22068. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22069. (plist-get opt-plist :title)
  22070. (and (not
  22071. (plist-get opt-plist :skip-before-1st-heading))
  22072. (org-export-grab-title-from-buffer))
  22073. (file-name-sans-extension
  22074. (file-name-nondirectory buffer-file-name))))
  22075. (email (plist-get opt-plist :email))
  22076. (language (plist-get opt-plist :language))
  22077. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22078. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22079. (todo nil)
  22080. (lang-words nil)
  22081. (region
  22082. (buffer-substring
  22083. (if (org-region-active-p) (region-beginning) (point-min))
  22084. (if (org-region-active-p) (region-end) (point-max))))
  22085. (lines (org-split-string
  22086. (org-cleaned-string-for-export
  22087. region
  22088. :for-ascii t
  22089. :skip-before-1st-heading
  22090. (plist-get opt-plist :skip-before-1st-heading)
  22091. :drawers (plist-get opt-plist :drawers)
  22092. :verbatim-multiline t
  22093. :archived-trees
  22094. (plist-get opt-plist :archived-trees)
  22095. :add-text (plist-get opt-plist :text))
  22096. "\n"))
  22097. thetoc have-headings first-heading-pos
  22098. table-open table-buffer)
  22099. (let ((inhibit-read-only t))
  22100. (org-unmodified
  22101. (remove-text-properties (point-min) (point-max)
  22102. '(:org-license-to-kill t))))
  22103. (setq org-min-level (org-get-min-level lines))
  22104. (setq org-last-level org-min-level)
  22105. (org-init-section-numbers)
  22106. (find-file-noselect filename)
  22107. (setq lang-words (or (assoc language org-export-language-setup)
  22108. (assoc "en" org-export-language-setup)))
  22109. (switch-to-buffer-other-window buffer)
  22110. (erase-buffer)
  22111. (fundamental-mode)
  22112. ;; create local variables for all options, to make sure all called
  22113. ;; functions get the correct information
  22114. (mapc (lambda (x)
  22115. (set (make-local-variable (cdr x))
  22116. (plist-get opt-plist (car x))))
  22117. org-export-plist-vars)
  22118. (org-set-local 'org-odd-levels-only odd)
  22119. (setq umax (if arg (prefix-numeric-value arg)
  22120. org-export-headline-levels))
  22121. (setq umax-toc (if (integerp org-export-with-toc)
  22122. (min org-export-with-toc umax)
  22123. umax))
  22124. ;; File header
  22125. (if title (org-insert-centered title ?=))
  22126. (insert "\n")
  22127. (if (and (or author email)
  22128. org-export-author-info)
  22129. (insert (concat (nth 1 lang-words) ": " (or author "")
  22130. (if email (concat " <" email ">") "")
  22131. "\n")))
  22132. (cond
  22133. ((and date (string-match "%" date))
  22134. (setq date (format-time-string date (current-time))))
  22135. (date)
  22136. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22137. (if (and date org-export-time-stamp-file)
  22138. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22139. (insert "\n\n")
  22140. (if org-export-with-toc
  22141. (progn
  22142. (push (concat (nth 3 lang-words) "\n") thetoc)
  22143. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22144. (mapc '(lambda (line)
  22145. (if (string-match org-todo-line-regexp
  22146. line)
  22147. ;; This is a headline
  22148. (progn
  22149. (setq have-headings t)
  22150. (setq level (- (match-end 1) (match-beginning 1))
  22151. level (org-tr-level level)
  22152. txt (match-string 3 line)
  22153. todo
  22154. (or (and org-export-mark-todo-in-toc
  22155. (match-beginning 2)
  22156. (not (member (match-string 2 line)
  22157. org-done-keywords)))
  22158. ; TODO, not DONE
  22159. (and org-export-mark-todo-in-toc
  22160. (= level umax-toc)
  22161. (org-search-todo-below
  22162. line lines level))))
  22163. (setq txt (org-html-expand-for-ascii txt))
  22164. (while (string-match org-bracket-link-regexp txt)
  22165. (setq txt
  22166. (replace-match
  22167. (match-string (if (match-end 2) 3 1) txt)
  22168. t t txt)))
  22169. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22170. (string-match
  22171. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22172. txt))
  22173. (setq txt (replace-match "" t t txt)))
  22174. (if (string-match quote-re0 txt)
  22175. (setq txt (replace-match "" t t txt)))
  22176. (if org-export-with-section-numbers
  22177. (setq txt (concat (org-section-number level)
  22178. " " txt)))
  22179. (if (<= level umax-toc)
  22180. (progn
  22181. (push
  22182. (concat
  22183. (make-string
  22184. (* (max 0 (- level org-min-level)) 4) ?\ )
  22185. (format (if todo "%s (*)\n" "%s\n") txt))
  22186. thetoc)
  22187. (setq org-last-level level))
  22188. ))))
  22189. lines)
  22190. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22191. (org-init-section-numbers)
  22192. (while (setq line (pop lines))
  22193. ;; Remove the quoted HTML tags.
  22194. (setq line (org-html-expand-for-ascii line))
  22195. ;; Remove targets
  22196. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22197. (setq line (replace-match "" t t line)))
  22198. ;; Replace internal links
  22199. (while (string-match org-bracket-link-regexp line)
  22200. (setq line (replace-match
  22201. (if (match-end 3) "[\\3]" "[\\1]")
  22202. t nil line)))
  22203. (when custom-times
  22204. (setq line (org-translate-time line)))
  22205. (cond
  22206. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22207. ;; a Headline
  22208. (setq first-heading-pos (or first-heading-pos (point)))
  22209. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22210. txt (match-string 2 line))
  22211. (org-ascii-level-start level txt umax lines))
  22212. ((and org-export-with-tables
  22213. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22214. (if (not table-open)
  22215. ;; New table starts
  22216. (setq table-open t table-buffer nil))
  22217. ;; Accumulate lines
  22218. (setq table-buffer (cons line table-buffer))
  22219. (when (or (not lines)
  22220. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22221. (car lines))))
  22222. (setq table-open nil
  22223. table-buffer (nreverse table-buffer))
  22224. (insert (mapconcat
  22225. (lambda (x)
  22226. (org-fix-indentation x org-ascii-current-indentation))
  22227. (org-format-table-ascii table-buffer)
  22228. "\n") "\n")))
  22229. (t
  22230. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22231. (if (and org-export-with-fixed-width
  22232. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22233. (setq line (replace-match "\\1" nil nil line)))
  22234. (insert line "\n"))))
  22235. (normal-mode)
  22236. ;; insert the table of contents
  22237. (when thetoc
  22238. (goto-char (point-min))
  22239. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22240. (progn
  22241. (goto-char (match-beginning 0))
  22242. (replace-match ""))
  22243. (goto-char first-heading-pos))
  22244. (mapc 'insert thetoc)
  22245. (or (looking-at "[ \t]*\n[ \t]*\n")
  22246. (insert "\n\n")))
  22247. ;; Convert whitespace place holders
  22248. (goto-char (point-min))
  22249. (let (beg end)
  22250. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22251. (setq end (next-single-property-change beg 'org-whitespace))
  22252. (goto-char beg)
  22253. (delete-region beg end)
  22254. (insert (make-string (- end beg) ?\ ))))
  22255. (save-buffer)
  22256. ;; remove display and invisible chars
  22257. (let (beg end)
  22258. (goto-char (point-min))
  22259. (while (setq beg (next-single-property-change (point) 'display))
  22260. (setq end (next-single-property-change beg 'display))
  22261. (delete-region beg end)
  22262. (goto-char beg)
  22263. (insert "=>"))
  22264. (goto-char (point-min))
  22265. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22266. (setq end (next-single-property-change beg 'org-cwidth))
  22267. (delete-region beg end)
  22268. (goto-char beg)))
  22269. (goto-char (point-min))))
  22270. (defun org-export-ascii-clean-string ()
  22271. "Do extra work for ASCII export"
  22272. (goto-char (point-min))
  22273. (while (re-search-forward org-verbatim-re nil t)
  22274. (goto-char (match-end 2))
  22275. (backward-delete-char 1) (insert "'")
  22276. (goto-char (match-beginning 2))
  22277. (delete-char 1) (insert "`")
  22278. (goto-char (match-end 2))))
  22279. (defun org-search-todo-below (line lines level)
  22280. "Search the subtree below LINE for any TODO entries."
  22281. (let ((rest (cdr (memq line lines)))
  22282. (re org-todo-line-regexp)
  22283. line lv todo)
  22284. (catch 'exit
  22285. (while (setq line (pop rest))
  22286. (if (string-match re line)
  22287. (progn
  22288. (setq lv (- (match-end 1) (match-beginning 1))
  22289. todo (and (match-beginning 2)
  22290. (not (member (match-string 2 line)
  22291. org-done-keywords))))
  22292. ; TODO, not DONE
  22293. (if (<= lv level) (throw 'exit nil))
  22294. (if todo (throw 'exit t))))))))
  22295. (defun org-html-expand-for-ascii (line)
  22296. "Handle quoted HTML for ASCII export."
  22297. (if org-export-html-expand
  22298. (while (string-match "@<[^<>\n]*>" line)
  22299. ;; We just remove the tags for now.
  22300. (setq line (replace-match "" nil nil line))))
  22301. line)
  22302. (defun org-insert-centered (s &optional underline)
  22303. "Insert the string S centered and underline it with character UNDERLINE."
  22304. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22305. (insert (make-string ind ?\ ) s "\n")
  22306. (if underline
  22307. (insert (make-string ind ?\ )
  22308. (make-string (string-width s) underline)
  22309. "\n"))))
  22310. (defun org-ascii-level-start (level title umax &optional lines)
  22311. "Insert a new level in ASCII export."
  22312. (let (char (n (- level umax 1)) (ind 0))
  22313. (if (> level umax)
  22314. (progn
  22315. (insert (make-string (* 2 n) ?\ )
  22316. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22317. org-export-ascii-bullets))
  22318. " " title "\n")
  22319. ;; find the indentation of the next non-empty line
  22320. (catch 'stop
  22321. (while lines
  22322. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22323. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22324. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22325. (pop lines)))
  22326. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22327. (if (or (not (equal (char-before) ?\n))
  22328. (not (equal (char-before (1- (point))) ?\n)))
  22329. (insert "\n"))
  22330. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22331. (unless org-export-with-tags
  22332. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22333. (setq title (replace-match "" t t title))))
  22334. (if org-export-with-section-numbers
  22335. (setq title (concat (org-section-number level) " " title)))
  22336. (insert title "\n" (make-string (string-width title) char) "\n")
  22337. (setq org-ascii-current-indentation '(0 . 0)))))
  22338. (defun org-export-visible (type arg)
  22339. "Create a copy of the visible part of the current buffer, and export it.
  22340. The copy is created in a temporary buffer and removed after use.
  22341. TYPE is the final key (as a string) that also select the export command in
  22342. the `C-c C-e' export dispatcher.
  22343. As a special case, if the you type SPC at the prompt, the temporary
  22344. org-mode file will not be removed but presented to you so that you can
  22345. continue to use it. The prefix arg ARG is passed through to the exporting
  22346. command."
  22347. (interactive
  22348. (list (progn
  22349. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22350. (read-char-exclusive))
  22351. current-prefix-arg))
  22352. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22353. (error "Invalid export key"))
  22354. (let* ((binding (cdr (assoc type
  22355. '((?a . org-export-as-ascii)
  22356. (?\C-a . org-export-as-ascii)
  22357. (?b . org-export-as-html-and-open)
  22358. (?\C-b . org-export-as-html-and-open)
  22359. (?h . org-export-as-html)
  22360. (?H . org-export-as-html-to-buffer)
  22361. (?R . org-export-region-as-html)
  22362. (?x . org-export-as-xoxo)))))
  22363. (keepp (equal type ?\ ))
  22364. (file buffer-file-name)
  22365. (buffer (get-buffer-create "*Org Export Visible*"))
  22366. s e)
  22367. ;; Need to hack the drawers here.
  22368. (save-excursion
  22369. (goto-char (point-min))
  22370. (while (re-search-forward org-drawer-regexp nil t)
  22371. (goto-char (match-beginning 1))
  22372. (or (org-invisible-p) (org-flag-drawer nil))))
  22373. (with-current-buffer buffer (erase-buffer))
  22374. (save-excursion
  22375. (setq s (goto-char (point-min)))
  22376. (while (not (= (point) (point-max)))
  22377. (goto-char (org-find-invisible))
  22378. (append-to-buffer buffer s (point))
  22379. (setq s (goto-char (org-find-visible))))
  22380. (org-cycle-hide-drawers 'all)
  22381. (goto-char (point-min))
  22382. (unless keepp
  22383. ;; Copy all comment lines to the end, to make sure #+ settings are
  22384. ;; still available for the second export step. Kind of a hack, but
  22385. ;; does do the trick.
  22386. (if (looking-at "#[^\r\n]*")
  22387. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22388. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22389. (append-to-buffer buffer (1+ (match-beginning 0))
  22390. (min (point-max) (1+ (match-end 0))))))
  22391. (set-buffer buffer)
  22392. (let ((buffer-file-name file)
  22393. (org-inhibit-startup t))
  22394. (org-mode)
  22395. (show-all)
  22396. (unless keepp (funcall binding arg))))
  22397. (if (not keepp)
  22398. (kill-buffer buffer)
  22399. (switch-to-buffer-other-window buffer)
  22400. (goto-char (point-min)))))
  22401. (defun org-find-visible ()
  22402. (let ((s (point)))
  22403. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22404. (get-char-property s 'invisible)))
  22405. s))
  22406. (defun org-find-invisible ()
  22407. (let ((s (point)))
  22408. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22409. (not (get-char-property s 'invisible))))
  22410. s))
  22411. ;;; HTML export
  22412. (defun org-get-current-options ()
  22413. "Return a string with current options as keyword options.
  22414. Does include HTML export options as well as TODO and CATEGORY stuff."
  22415. (format
  22416. "#+TITLE: %s
  22417. #+AUTHOR: %s
  22418. #+EMAIL: %s
  22419. #+LANGUAGE: %s
  22420. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22421. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s d:%s tags:%s
  22422. #+CATEGORY: %s
  22423. #+SEQ_TODO: %s
  22424. #+TYP_TODO: %s
  22425. #+PRIORITIES: %c %c %c
  22426. #+DRAWERS: %s
  22427. #+STARTUP: %s %s %s %s %s
  22428. #+TAGS: %s
  22429. #+ARCHIVE: %s
  22430. #+LINK: %s
  22431. "
  22432. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22433. org-export-headline-levels
  22434. org-export-with-section-numbers
  22435. org-export-with-toc
  22436. org-export-preserve-breaks
  22437. org-export-html-expand
  22438. org-export-with-fixed-width
  22439. org-export-with-tables
  22440. org-export-with-sub-superscripts
  22441. org-export-with-special-strings
  22442. org-export-with-footnotes
  22443. org-export-with-emphasize
  22444. org-export-with-TeX-macros
  22445. org-export-with-LaTeX-fragments
  22446. org-export-skip-text-before-1st-heading
  22447. org-export-with-drawers
  22448. org-export-with-tags
  22449. (file-name-nondirectory buffer-file-name)
  22450. "TODO FEEDBACK VERIFY DONE"
  22451. "Me Jason Marie DONE"
  22452. org-highest-priority org-lowest-priority org-default-priority
  22453. (mapconcat 'identity org-drawers " ")
  22454. (cdr (assoc org-startup-folded
  22455. '((nil . "showall") (t . "overview") (content . "content"))))
  22456. (if org-odd-levels-only "odd" "oddeven")
  22457. (if org-hide-leading-stars "hidestars" "showstars")
  22458. (if org-startup-align-all-tables "align" "noalign")
  22459. (cond ((eq t org-log-done) "logdone")
  22460. ((not org-log-done) "nologging")
  22461. ((listp org-log-done)
  22462. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  22463. org-log-done " ")))
  22464. (or (mapconcat (lambda (x)
  22465. (cond
  22466. ((equal '(:startgroup) x) "{")
  22467. ((equal '(:endgroup) x) "}")
  22468. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22469. (t (car x))))
  22470. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22471. org-archive-location
  22472. "org file:~/org/%s.org"
  22473. ))
  22474. (defun org-insert-export-options-template ()
  22475. "Insert into the buffer a template with information for exporting."
  22476. (interactive)
  22477. (if (not (bolp)) (newline))
  22478. (let ((s (org-get-current-options)))
  22479. (and (string-match "#\\+CATEGORY" s)
  22480. (setq s (substring s 0 (match-beginning 0))))
  22481. (insert s)))
  22482. (defun org-toggle-fixed-width-section (arg)
  22483. "Toggle the fixed-width export.
  22484. If there is no active region, the QUOTE keyword at the current headline is
  22485. inserted or removed. When present, it causes the text between this headline
  22486. and the next to be exported as fixed-width text, and unmodified.
  22487. If there is an active region, this command adds or removes a colon as the
  22488. first character of this line. If the first character of a line is a colon,
  22489. this line is also exported in fixed-width font."
  22490. (interactive "P")
  22491. (let* ((cc 0)
  22492. (regionp (org-region-active-p))
  22493. (beg (if regionp (region-beginning) (point)))
  22494. (end (if regionp (region-end)))
  22495. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22496. (case-fold-search nil)
  22497. (re "[ \t]*\\(:\\)")
  22498. off)
  22499. (if regionp
  22500. (save-excursion
  22501. (goto-char beg)
  22502. (setq cc (current-column))
  22503. (beginning-of-line 1)
  22504. (setq off (looking-at re))
  22505. (while (> nlines 0)
  22506. (setq nlines (1- nlines))
  22507. (beginning-of-line 1)
  22508. (cond
  22509. (arg
  22510. (move-to-column cc t)
  22511. (insert ":\n")
  22512. (forward-line -1))
  22513. ((and off (looking-at re))
  22514. (replace-match "" t t nil 1))
  22515. ((not off) (move-to-column cc t) (insert ":")))
  22516. (forward-line 1)))
  22517. (save-excursion
  22518. (org-back-to-heading)
  22519. (if (looking-at (concat outline-regexp
  22520. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22521. (replace-match "" t t nil 1)
  22522. (if (looking-at outline-regexp)
  22523. (progn
  22524. (goto-char (match-end 0))
  22525. (insert org-quote-string " "))))))))
  22526. (defun org-export-as-html-and-open (arg)
  22527. "Export the outline as HTML and immediately open it with a browser.
  22528. If there is an active region, export only the region.
  22529. The prefix ARG specifies how many levels of the outline should become
  22530. headlines. The default is 3. Lower levels will become bulleted lists."
  22531. (interactive "P")
  22532. (org-export-as-html arg 'hidden)
  22533. (org-open-file buffer-file-name))
  22534. (defun org-export-as-html-batch ()
  22535. "Call `org-export-as-html', may be used in batch processing as
  22536. emacs --batch
  22537. --load=$HOME/lib/emacs/org.el
  22538. --eval \"(setq org-export-headline-levels 2)\"
  22539. --visit=MyFile --funcall org-export-as-html-batch"
  22540. (org-export-as-html org-export-headline-levels 'hidden))
  22541. (defun org-export-as-html-to-buffer (arg)
  22542. "Call `org-exort-as-html` with output to a temporary buffer.
  22543. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22544. (interactive "P")
  22545. (org-export-as-html arg nil nil "*Org HTML Export*")
  22546. (switch-to-buffer-other-window "*Org HTML Export*"))
  22547. (defun org-replace-region-by-html (beg end)
  22548. "Assume the current region has org-mode syntax, and convert it to HTML.
  22549. This can be used in any buffer. For example, you could write an
  22550. itemized list in org-mode syntax in an HTML buffer and then use this
  22551. command to convert it."
  22552. (interactive "r")
  22553. (let (reg html buf pop-up-frames)
  22554. (save-window-excursion
  22555. (if (org-mode-p)
  22556. (setq html (org-export-region-as-html
  22557. beg end t 'string))
  22558. (setq reg (buffer-substring beg end)
  22559. buf (get-buffer-create "*Org tmp*"))
  22560. (with-current-buffer buf
  22561. (erase-buffer)
  22562. (insert reg)
  22563. (org-mode)
  22564. (setq html (org-export-region-as-html
  22565. (point-min) (point-max) t 'string)))
  22566. (kill-buffer buf)))
  22567. (delete-region beg end)
  22568. (insert html)))
  22569. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22570. "Convert region from BEG to END in org-mode buffer to HTML.
  22571. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22572. contents, and only produce the region of converted text, useful for
  22573. cut-and-paste operations.
  22574. If BUFFER is a buffer or a string, use/create that buffer as a target
  22575. of the converted HTML. If BUFFER is the symbol `string', return the
  22576. produced HTML as a string and leave not buffer behind. For example,
  22577. a Lisp program could call this function in the following way:
  22578. (setq html (org-export-region-as-html beg end t 'string))
  22579. When called interactively, the output buffer is selected, and shown
  22580. in a window. A non-interactive call will only retunr the buffer."
  22581. (interactive "r\nP")
  22582. (when (interactive-p)
  22583. (setq buffer "*Org HTML Export*"))
  22584. (let ((transient-mark-mode t) (zmacs-regions t)
  22585. rtn)
  22586. (goto-char end)
  22587. (set-mark (point)) ;; to activate the region
  22588. (goto-char beg)
  22589. (setq rtn (org-export-as-html
  22590. nil nil nil
  22591. buffer body-only))
  22592. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22593. (if (and (interactive-p) (bufferp rtn))
  22594. (switch-to-buffer-other-window rtn)
  22595. rtn)))
  22596. (defvar html-table-tag nil) ; dynamically scoped into this.
  22597. (defun org-export-as-html (arg &optional hidden ext-plist
  22598. to-buffer body-only)
  22599. "Export the outline as a pretty HTML file.
  22600. If there is an active region, export only the region. The prefix
  22601. ARG specifies how many levels of the outline should become
  22602. headlines. The default is 3. Lower levels will become bulleted
  22603. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22604. EXT-PLIST is a property list with external parameters overriding
  22605. org-mode's default settings, but still inferior to file-local
  22606. settings. When TO-BUFFER is non-nil, create a buffer with that
  22607. name and export to that buffer. If TO-BUFFER is the symbol `string',
  22608. don't leave any buffer behind but just return the resulting HTML as
  22609. a string. When BODY-ONLY is set, don't produce the file header and footer,
  22610. simply return the content of <body>...</body>, without even
  22611. the body tags themselves."
  22612. (interactive "P")
  22613. ;; Make sure we have a file name when we need it.
  22614. (when (and (not (or to-buffer body-only))
  22615. (not buffer-file-name))
  22616. (if (buffer-base-buffer)
  22617. (org-set-local 'buffer-file-name
  22618. (with-current-buffer (buffer-base-buffer)
  22619. buffer-file-name))
  22620. (error "Need a file name to be able to export.")))
  22621. (message "Exporting...")
  22622. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22623. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  22624. (setq-default org-done-keywords org-done-keywords)
  22625. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  22626. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22627. ext-plist
  22628. (org-infile-export-plist)))
  22629. (style (plist-get opt-plist :style))
  22630. (link-validate (plist-get opt-plist :link-validation-function))
  22631. valid thetoc have-headings first-heading-pos
  22632. (odd org-odd-levels-only)
  22633. (region-p (org-region-active-p))
  22634. (subtree-p
  22635. (when region-p
  22636. (save-excursion
  22637. (goto-char (region-beginning))
  22638. (and (org-at-heading-p)
  22639. (>= (org-end-of-subtree t t) (region-end))))))
  22640. ;; The following two are dynamically scoped into other
  22641. ;; routines below.
  22642. (org-current-export-dir (org-export-directory :html opt-plist))
  22643. (org-current-export-file buffer-file-name)
  22644. (level 0) (line "") (origline "") txt todo
  22645. (umax nil)
  22646. (umax-toc nil)
  22647. (filename (if to-buffer nil
  22648. (expand-file-name
  22649. (concat
  22650. (file-name-sans-extension
  22651. (or (and subtree-p
  22652. (org-entry-get (region-beginning)
  22653. "EXPORT_FILE_NAME" t))
  22654. (file-name-nondirectory buffer-file-name)))
  22655. "." org-export-html-extension)
  22656. (file-name-as-directory
  22657. (org-export-directory :html opt-plist)))))
  22658. (current-dir (if buffer-file-name
  22659. (file-name-directory buffer-file-name)
  22660. default-directory))
  22661. (buffer (if to-buffer
  22662. (cond
  22663. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  22664. (t (get-buffer-create to-buffer)))
  22665. (find-file-noselect filename)))
  22666. (org-levels-open (make-vector org-level-max nil))
  22667. (date (plist-get opt-plist :date))
  22668. (author (plist-get opt-plist :author))
  22669. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22670. (plist-get opt-plist :title)
  22671. (and (not
  22672. (plist-get opt-plist :skip-before-1st-heading))
  22673. (org-export-grab-title-from-buffer))
  22674. (and buffer-file-name
  22675. (file-name-sans-extension
  22676. (file-name-nondirectory buffer-file-name)))
  22677. "UNTITLED"))
  22678. (html-table-tag (plist-get opt-plist :html-table-tag))
  22679. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22680. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  22681. (inquote nil)
  22682. (infixed nil)
  22683. (in-local-list nil)
  22684. (local-list-num nil)
  22685. (local-list-indent nil)
  22686. (llt org-plain-list-ordered-item-terminator)
  22687. (email (plist-get opt-plist :email))
  22688. (language (plist-get opt-plist :language))
  22689. (lang-words nil)
  22690. (target-alist nil) tg
  22691. (head-count 0) cnt
  22692. (start 0)
  22693. (coding-system (and (boundp 'buffer-file-coding-system)
  22694. buffer-file-coding-system))
  22695. (coding-system-for-write (or org-export-html-coding-system
  22696. coding-system))
  22697. (save-buffer-coding-system (or org-export-html-coding-system
  22698. coding-system))
  22699. (charset (and coding-system-for-write
  22700. (fboundp 'coding-system-get)
  22701. (coding-system-get coding-system-for-write
  22702. 'mime-charset)))
  22703. (region
  22704. (buffer-substring
  22705. (if region-p (region-beginning) (point-min))
  22706. (if region-p (region-end) (point-max))))
  22707. (lines
  22708. (org-split-string
  22709. (org-cleaned-string-for-export
  22710. region
  22711. :emph-multiline t
  22712. :for-html t
  22713. :skip-before-1st-heading
  22714. (plist-get opt-plist :skip-before-1st-heading)
  22715. :drawers (plist-get opt-plist :drawers)
  22716. :archived-trees
  22717. (plist-get opt-plist :archived-trees)
  22718. :add-text
  22719. (plist-get opt-plist :text)
  22720. :LaTeX-fragments
  22721. (plist-get opt-plist :LaTeX-fragments))
  22722. "[\r\n]"))
  22723. table-open type
  22724. table-buffer table-orig-buffer
  22725. ind start-is-num starter didclose
  22726. rpl path desc descp desc1 desc2 link
  22727. )
  22728. (let ((inhibit-read-only t))
  22729. (org-unmodified
  22730. (remove-text-properties (point-min) (point-max)
  22731. '(:org-license-to-kill t))))
  22732. (message "Exporting...")
  22733. (setq org-min-level (org-get-min-level lines))
  22734. (setq org-last-level org-min-level)
  22735. (org-init-section-numbers)
  22736. (cond
  22737. ((and date (string-match "%" date))
  22738. (setq date (format-time-string date (current-time))))
  22739. (date)
  22740. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22741. ;; Get the language-dependent settings
  22742. (setq lang-words (or (assoc language org-export-language-setup)
  22743. (assoc "en" org-export-language-setup)))
  22744. ;; Switch to the output buffer
  22745. (set-buffer buffer)
  22746. (erase-buffer)
  22747. (fundamental-mode)
  22748. (and (fboundp 'set-buffer-file-coding-system)
  22749. (set-buffer-file-coding-system coding-system-for-write))
  22750. (let ((case-fold-search nil)
  22751. (org-odd-levels-only odd))
  22752. ;; create local variables for all options, to make sure all called
  22753. ;; functions get the correct information
  22754. (mapc (lambda (x)
  22755. (set (make-local-variable (cdr x))
  22756. (plist-get opt-plist (car x))))
  22757. org-export-plist-vars)
  22758. (setq umax (if arg (prefix-numeric-value arg)
  22759. org-export-headline-levels))
  22760. (setq umax-toc (if (integerp org-export-with-toc)
  22761. (min org-export-with-toc umax)
  22762. umax))
  22763. (unless body-only
  22764. ;; File header
  22765. (insert (format
  22766. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  22767. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  22768. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  22769. lang=\"%s\" xml:lang=\"%s\">
  22770. <head>
  22771. <title>%s</title>
  22772. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  22773. <meta name=\"generator\" content=\"Org-mode\"/>
  22774. <meta name=\"generated\" content=\"%s\"/>
  22775. <meta name=\"author\" content=\"%s\"/>
  22776. %s
  22777. </head><body>
  22778. "
  22779. language language (org-html-expand title)
  22780. (or charset "iso-8859-1") date author style))
  22781. (insert (or (plist-get opt-plist :preamble) ""))
  22782. (when (plist-get opt-plist :auto-preamble)
  22783. (if title (insert (format org-export-html-title-format
  22784. (org-html-expand title))))))
  22785. (if (and org-export-with-toc (not body-only))
  22786. (progn
  22787. (push (format "<h%d>%s</h%d>\n"
  22788. org-export-html-toplevel-hlevel
  22789. (nth 3 lang-words)
  22790. org-export-html-toplevel-hlevel)
  22791. thetoc)
  22792. (push "<ul>\n<li>" thetoc)
  22793. (setq lines
  22794. (mapcar '(lambda (line)
  22795. (if (string-match org-todo-line-regexp line)
  22796. ;; This is a headline
  22797. (progn
  22798. (setq have-headings t)
  22799. (setq level (- (match-end 1) (match-beginning 1))
  22800. level (org-tr-level level)
  22801. txt (save-match-data
  22802. (org-html-expand
  22803. (org-export-cleanup-toc-line
  22804. (match-string 3 line))))
  22805. todo
  22806. (or (and org-export-mark-todo-in-toc
  22807. (match-beginning 2)
  22808. (not (member (match-string 2 line)
  22809. org-done-keywords)))
  22810. ; TODO, not DONE
  22811. (and org-export-mark-todo-in-toc
  22812. (= level umax-toc)
  22813. (org-search-todo-below
  22814. line lines level))))
  22815. (if (string-match
  22816. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  22817. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  22818. (if (string-match quote-re0 txt)
  22819. (setq txt (replace-match "" t t txt)))
  22820. (if org-export-with-section-numbers
  22821. (setq txt (concat (org-section-number level)
  22822. " " txt)))
  22823. (if (<= level (max umax umax-toc))
  22824. (setq head-count (+ head-count 1)))
  22825. (if (<= level umax-toc)
  22826. (progn
  22827. (if (> level org-last-level)
  22828. (progn
  22829. (setq cnt (- level org-last-level))
  22830. (while (>= (setq cnt (1- cnt)) 0)
  22831. (push "\n<ul>\n<li>" thetoc))
  22832. (push "\n" thetoc)))
  22833. (if (< level org-last-level)
  22834. (progn
  22835. (setq cnt (- org-last-level level))
  22836. (while (>= (setq cnt (1- cnt)) 0)
  22837. (push "</li>\n</ul>" thetoc))
  22838. (push "\n" thetoc)))
  22839. ;; Check for targets
  22840. (while (string-match org-target-regexp line)
  22841. (setq tg (match-string 1 line)
  22842. line (replace-match
  22843. (concat "@<span class=\"target\">" tg "@</span> ")
  22844. t t line))
  22845. (push (cons (org-solidify-link-text tg)
  22846. (format "sec-%d" head-count))
  22847. target-alist))
  22848. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  22849. (setq txt (replace-match "" t t txt)))
  22850. (push
  22851. (format
  22852. (if todo
  22853. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  22854. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  22855. head-count txt) thetoc)
  22856. (setq org-last-level level))
  22857. )))
  22858. line)
  22859. lines))
  22860. (while (> org-last-level (1- org-min-level))
  22861. (setq org-last-level (1- org-last-level))
  22862. (push "</li>\n</ul>\n" thetoc))
  22863. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22864. (setq head-count 0)
  22865. (org-init-section-numbers)
  22866. (while (setq line (pop lines) origline line)
  22867. (catch 'nextline
  22868. ;; end of quote section?
  22869. (when (and inquote (string-match "^\\*+ " line))
  22870. (insert "</pre>\n")
  22871. (setq inquote nil))
  22872. ;; inside a quote section?
  22873. (when inquote
  22874. (insert (org-html-protect line) "\n")
  22875. (throw 'nextline nil))
  22876. ;; verbatim lines
  22877. (when (and org-export-with-fixed-width
  22878. (string-match "^[ \t]*:\\(.*\\)" line))
  22879. (when (not infixed)
  22880. (setq infixed t)
  22881. (insert "<pre>\n"))
  22882. (insert (org-html-protect (match-string 1 line)) "\n")
  22883. (when (and lines
  22884. (not (string-match "^[ \t]*\\(:.*\\)"
  22885. (car lines))))
  22886. (setq infixed nil)
  22887. (insert "</pre>\n"))
  22888. (throw 'nextline nil))
  22889. ;; Protected HTML
  22890. (when (get-text-property 0 'org-protected line)
  22891. (let (par)
  22892. (when (re-search-backward
  22893. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  22894. (setq par (match-string 1))
  22895. (replace-match "\\2\n"))
  22896. (insert line "\n")
  22897. (while (and lines
  22898. (or (= (length (car lines)) 0)
  22899. (get-text-property 0 'org-protected (car lines))))
  22900. (insert (pop lines) "\n"))
  22901. (and par (insert "<p>\n")))
  22902. (throw 'nextline nil))
  22903. ;; Horizontal line
  22904. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  22905. (insert "\n<hr/>\n")
  22906. (throw 'nextline nil))
  22907. ;; make targets to anchors
  22908. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  22909. (cond
  22910. ((match-end 2)
  22911. (setq line (replace-match
  22912. (concat "@<a name=\""
  22913. (org-solidify-link-text (match-string 1 line))
  22914. "\">\\nbsp@</a>")
  22915. t t line)))
  22916. ((and org-export-with-toc (equal (string-to-char line) ?*))
  22917. (setq line (replace-match
  22918. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  22919. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  22920. t t line)))
  22921. (t
  22922. (setq line (replace-match
  22923. (concat "@<a name=\""
  22924. (org-solidify-link-text (match-string 1 line))
  22925. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  22926. t t line)))))
  22927. (setq line (org-html-handle-time-stamps line))
  22928. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  22929. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  22930. ;; Also handle sub_superscripts and checkboxes
  22931. (or (string-match org-table-hline-regexp line)
  22932. (setq line (org-html-expand line)))
  22933. ;; Format the links
  22934. (setq start 0)
  22935. (while (string-match org-bracket-link-analytic-regexp line start)
  22936. (setq start (match-beginning 0))
  22937. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  22938. (setq path (match-string 3 line))
  22939. (setq desc1 (if (match-end 5) (match-string 5 line))
  22940. desc2 (if (match-end 2) (concat type ":" path) path)
  22941. descp (and desc1 (not (equal desc1 desc2)))
  22942. desc (or desc1 desc2))
  22943. ;; Make an image out of the description if that is so wanted
  22944. (when (and descp (org-file-image-p desc))
  22945. (save-match-data
  22946. (if (string-match "^file:" desc)
  22947. (setq desc (substring desc (match-end 0)))))
  22948. (setq desc (concat "<img src=\"" desc "\"/>")))
  22949. ;; FIXME: do we need to unescape here somewhere?
  22950. (cond
  22951. ((equal type "internal")
  22952. (setq rpl
  22953. (concat
  22954. "<a href=\"#"
  22955. (org-solidify-link-text
  22956. (save-match-data (org-link-unescape path)) target-alist)
  22957. "\">" desc "</a>")))
  22958. ((member type '("http" "https"))
  22959. ;; standard URL, just check if we need to inline an image
  22960. (if (and (or (eq t org-export-html-inline-images)
  22961. (and org-export-html-inline-images (not descp)))
  22962. (org-file-image-p path))
  22963. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  22964. (setq link (concat type ":" path))
  22965. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  22966. ((member type '("ftp" "mailto" "news"))
  22967. ;; standard URL
  22968. (setq link (concat type ":" path))
  22969. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  22970. ((string= type "file")
  22971. ;; FILE link
  22972. (let* ((filename path)
  22973. (abs-p (file-name-absolute-p filename))
  22974. thefile file-is-image-p search)
  22975. (save-match-data
  22976. (if (string-match "::\\(.*\\)" filename)
  22977. (setq search (match-string 1 filename)
  22978. filename (replace-match "" t nil filename)))
  22979. (setq valid
  22980. (if (functionp link-validate)
  22981. (funcall link-validate filename current-dir)
  22982. t))
  22983. (setq file-is-image-p (org-file-image-p filename))
  22984. (setq thefile (if abs-p (expand-file-name filename) filename))
  22985. (when (and org-export-html-link-org-files-as-html
  22986. (string-match "\\.org$" thefile))
  22987. (setq thefile (concat (substring thefile 0
  22988. (match-beginning 0))
  22989. "." org-export-html-extension))
  22990. (if (and search
  22991. ;; make sure this is can be used as target search
  22992. (not (string-match "^[0-9]*$" search))
  22993. (not (string-match "^\\*" search))
  22994. (not (string-match "^/.*/$" search)))
  22995. (setq thefile (concat thefile "#"
  22996. (org-solidify-link-text
  22997. (org-link-unescape search)))))
  22998. (when (string-match "^file:" desc)
  22999. (setq desc (replace-match "" t t desc))
  23000. (if (string-match "\\.org$" desc)
  23001. (setq desc (replace-match "" t t desc))))))
  23002. (setq rpl (if (and file-is-image-p
  23003. (or (eq t org-export-html-inline-images)
  23004. (and org-export-html-inline-images
  23005. (not descp))))
  23006. (concat "<img src=\"" thefile "\"/>")
  23007. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23008. (if (not valid) (setq rpl desc))))
  23009. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23010. (setq rpl (concat "<i>&lt;" type ":"
  23011. (save-match-data (org-link-unescape path))
  23012. "&gt;</i>"))))
  23013. (setq line (replace-match rpl t t line)
  23014. start (+ start (length rpl))))
  23015. ;; TODO items
  23016. (if (and (string-match org-todo-line-regexp line)
  23017. (match-beginning 2))
  23018. (setq line
  23019. (concat (substring line 0 (match-beginning 2))
  23020. "<span class=\""
  23021. (if (member (match-string 2 line)
  23022. org-done-keywords)
  23023. "done" "todo")
  23024. "\">" (match-string 2 line)
  23025. "</span>" (substring line (match-end 2)))))
  23026. ;; Does this contain a reference to a footnote?
  23027. (when org-export-with-footnotes
  23028. (setq start 0)
  23029. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23030. (if (get-text-property (match-beginning 2) 'org-protected line)
  23031. (setq start (match-end 2))
  23032. (let ((n (match-string 2 line)))
  23033. (setq line
  23034. (replace-match
  23035. (format
  23036. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23037. (match-string 1 line) n n n)
  23038. t t line))))))
  23039. (cond
  23040. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23041. ;; This is a headline
  23042. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23043. txt (match-string 2 line))
  23044. (if (string-match quote-re0 txt)
  23045. (setq txt (replace-match "" t t txt)))
  23046. (if (<= level (max umax umax-toc))
  23047. (setq head-count (+ head-count 1)))
  23048. (when in-local-list
  23049. ;; Close any local lists before inserting a new header line
  23050. (while local-list-num
  23051. (org-close-li)
  23052. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23053. (pop local-list-num))
  23054. (setq local-list-indent nil
  23055. in-local-list nil))
  23056. (setq first-heading-pos (or first-heading-pos (point)))
  23057. (org-html-level-start level txt umax
  23058. (and org-export-with-toc (<= level umax))
  23059. head-count)
  23060. ;; QUOTES
  23061. (when (string-match quote-re line)
  23062. (insert "<pre>")
  23063. (setq inquote t)))
  23064. ((and org-export-with-tables
  23065. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23066. (if (not table-open)
  23067. ;; New table starts
  23068. (setq table-open t table-buffer nil table-orig-buffer nil))
  23069. ;; Accumulate lines
  23070. (setq table-buffer (cons line table-buffer)
  23071. table-orig-buffer (cons origline table-orig-buffer))
  23072. (when (or (not lines)
  23073. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23074. (car lines))))
  23075. (setq table-open nil
  23076. table-buffer (nreverse table-buffer)
  23077. table-orig-buffer (nreverse table-orig-buffer))
  23078. (org-close-par-maybe)
  23079. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23080. (t
  23081. ;; Normal lines
  23082. (when (string-match
  23083. (cond
  23084. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23085. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23086. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23087. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23088. line)
  23089. (setq ind (org-get-string-indentation line)
  23090. start-is-num (match-beginning 4)
  23091. starter (if (match-beginning 2)
  23092. (substring (match-string 2 line) 0 -1))
  23093. line (substring line (match-beginning 5)))
  23094. (unless (string-match "[^ \t]" line)
  23095. ;; empty line. Pretend indentation is large.
  23096. (setq ind (if org-empty-line-terminates-plain-lists
  23097. 0
  23098. (1+ (or (car local-list-indent) 1)))))
  23099. (setq didclose nil)
  23100. (while (and in-local-list
  23101. (or (and (= ind (car local-list-indent))
  23102. (not starter))
  23103. (< ind (car local-list-indent))))
  23104. (setq didclose t)
  23105. (org-close-li)
  23106. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23107. (pop local-list-num) (pop local-list-indent)
  23108. (setq in-local-list local-list-indent))
  23109. (cond
  23110. ((and starter
  23111. (or (not in-local-list)
  23112. (> ind (car local-list-indent))))
  23113. ;; Start new (level of) list
  23114. (org-close-par-maybe)
  23115. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23116. (push start-is-num local-list-num)
  23117. (push ind local-list-indent)
  23118. (setq in-local-list t))
  23119. (starter
  23120. ;; continue current list
  23121. (org-close-li)
  23122. (insert "<li>\n"))
  23123. (didclose
  23124. ;; we did close a list, normal text follows: need <p>
  23125. (org-open-par)))
  23126. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23127. (setq line
  23128. (replace-match
  23129. (if (equal (match-string 1 line) "X")
  23130. "<b>[X]</b>"
  23131. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23132. t t line))))
  23133. ;; Empty lines start a new paragraph. If hand-formatted lists
  23134. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23135. ;; also start a new paragraph.
  23136. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23137. ;; Is this the start of a footnote?
  23138. (when org-export-with-footnotes
  23139. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23140. (org-close-par-maybe)
  23141. (let ((n (match-string 1 line)))
  23142. (setq line (replace-match
  23143. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23144. ;; Check if the line break needs to be conserved
  23145. (cond
  23146. ((string-match "\\\\\\\\[ \t]*$" line)
  23147. (setq line (replace-match "<br/>" t t line)))
  23148. (org-export-preserve-breaks
  23149. (setq line (concat line "<br/>"))))
  23150. (insert line "\n")))))
  23151. ;; Properly close all local lists and other lists
  23152. (when inquote (insert "</pre>\n"))
  23153. (when in-local-list
  23154. ;; Close any local lists before inserting a new header line
  23155. (while local-list-num
  23156. (org-close-li)
  23157. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23158. (pop local-list-num))
  23159. (setq local-list-indent nil
  23160. in-local-list nil))
  23161. (org-html-level-start 1 nil umax
  23162. (and org-export-with-toc (<= level umax))
  23163. head-count)
  23164. (unless body-only
  23165. (when (plist-get opt-plist :auto-postamble)
  23166. (insert "<div id=\"postamble\">")
  23167. (when (and org-export-author-info author)
  23168. (insert "<p class=\"author\"> "
  23169. (nth 1 lang-words) ": " author "\n")
  23170. (when email
  23171. (if (listp (split-string email ",+ *"))
  23172. (mapc (lambda(e)
  23173. (insert "<a href=\"mailto:" e "\">&lt;"
  23174. e "&gt;</a>\n"))
  23175. (split-string email ",+ *"))
  23176. (insert "<a href=\"mailto:" email "\">&lt;"
  23177. email "&gt;</a>\n")))
  23178. (insert "</p>\n"))
  23179. (when (and date org-export-time-stamp-file)
  23180. (insert "<p class=\"date\"> "
  23181. (nth 2 lang-words) ": "
  23182. date "</p>\n"))
  23183. (insert "</div>"))
  23184. (if org-export-html-with-timestamp
  23185. (insert org-export-html-html-helper-timestamp))
  23186. (insert (or (plist-get opt-plist :postamble) ""))
  23187. (insert "</body>\n</html>\n"))
  23188. (normal-mode)
  23189. (if (eq major-mode default-major-mode) (html-mode))
  23190. ;; insert the table of contents
  23191. (goto-char (point-min))
  23192. (when thetoc
  23193. (if (or (re-search-forward
  23194. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23195. (re-search-forward
  23196. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23197. (progn
  23198. (goto-char (match-beginning 0))
  23199. (replace-match ""))
  23200. (goto-char first-heading-pos)
  23201. (when (looking-at "\\s-*</p>")
  23202. (goto-char (match-end 0))
  23203. (insert "\n")))
  23204. (insert "<div id=\"table-of-contents\">\n")
  23205. (mapc 'insert thetoc)
  23206. (insert "</div>\n"))
  23207. ;; remove empty paragraphs and lists
  23208. (goto-char (point-min))
  23209. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23210. (replace-match ""))
  23211. (goto-char (point-min))
  23212. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23213. (replace-match ""))
  23214. ;; Convert whitespace place holders
  23215. (goto-char (point-min))
  23216. (let (beg end n)
  23217. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23218. (setq n (get-text-property beg 'org-whitespace)
  23219. end (next-single-property-change beg 'org-whitespace))
  23220. (goto-char beg)
  23221. (delete-region beg end)
  23222. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23223. (make-string n ?x)))))
  23224. (or to-buffer (save-buffer))
  23225. (goto-char (point-min))
  23226. (message "Exporting... done")
  23227. (if (eq to-buffer 'string)
  23228. (prog1 (buffer-substring (point-min) (point-max))
  23229. (kill-buffer (current-buffer)))
  23230. (current-buffer)))))
  23231. (defvar org-table-colgroup-info nil)
  23232. (defun org-format-table-ascii (lines)
  23233. "Format a table for ascii export."
  23234. (if (stringp lines)
  23235. (setq lines (org-split-string lines "\n")))
  23236. (if (not (string-match "^[ \t]*|" (car lines)))
  23237. ;; Table made by table.el - test for spanning
  23238. lines
  23239. ;; A normal org table
  23240. ;; Get rid of hlines at beginning and end
  23241. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23242. (setq lines (nreverse lines))
  23243. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23244. (setq lines (nreverse lines))
  23245. (when org-export-table-remove-special-lines
  23246. ;; Check if the table has a marking column. If yes remove the
  23247. ;; column and the special lines
  23248. (setq lines (org-table-clean-before-export lines)))
  23249. ;; Get rid of the vertical lines except for grouping
  23250. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23251. rtn line vl1 start)
  23252. (while (setq line (pop lines))
  23253. (if (string-match org-table-hline-regexp line)
  23254. (and (string-match "|\\(.*\\)|" line)
  23255. (setq line (replace-match " \\1" t nil line)))
  23256. (setq start 0 vl1 vl)
  23257. (while (string-match "|" line start)
  23258. (setq start (match-end 0))
  23259. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23260. (push line rtn))
  23261. (nreverse rtn))))
  23262. (defun org-colgroup-info-to-vline-list (info)
  23263. (let (vl new last)
  23264. (while info
  23265. (setq last new new (pop info))
  23266. (if (or (memq last '(:end :startend))
  23267. (memq new '(:start :startend)))
  23268. (push t vl)
  23269. (push nil vl)))
  23270. (setq vl (nreverse vl))
  23271. (and vl (setcar vl nil))
  23272. vl))
  23273. (defun org-format-table-html (lines olines)
  23274. "Find out which HTML converter to use and return the HTML code."
  23275. (if (stringp lines)
  23276. (setq lines (org-split-string lines "\n")))
  23277. (if (string-match "^[ \t]*|" (car lines))
  23278. ;; A normal org table
  23279. (org-format-org-table-html lines)
  23280. ;; Table made by table.el - test for spanning
  23281. (let* ((hlines (delq nil (mapcar
  23282. (lambda (x)
  23283. (if (string-match "^[ \t]*\\+-" x) x
  23284. nil))
  23285. lines)))
  23286. (first (car hlines))
  23287. (ll (and (string-match "\\S-+" first)
  23288. (match-string 0 first)))
  23289. (re (concat "^[ \t]*" (regexp-quote ll)))
  23290. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23291. hlines))))
  23292. (if (and (not spanning)
  23293. (not org-export-prefer-native-exporter-for-tables))
  23294. ;; We can use my own converter with HTML conversions
  23295. (org-format-table-table-html lines)
  23296. ;; Need to use the code generator in table.el, with the original text.
  23297. (org-format-table-table-html-using-table-generate-source olines)))))
  23298. (defun org-format-org-table-html (lines &optional splice)
  23299. "Format a table into HTML."
  23300. ;; Get rid of hlines at beginning and end
  23301. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23302. (setq lines (nreverse lines))
  23303. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23304. (setq lines (nreverse lines))
  23305. (when org-export-table-remove-special-lines
  23306. ;; Check if the table has a marking column. If yes remove the
  23307. ;; column and the special lines
  23308. (setq lines (org-table-clean-before-export lines)))
  23309. (let ((head (and org-export-highlight-first-table-line
  23310. (delq nil (mapcar
  23311. (lambda (x) (string-match "^[ \t]*|-" x))
  23312. (cdr lines)))))
  23313. (nlines 0) fnum i
  23314. tbopen line fields html gr colgropen)
  23315. (if splice (setq head nil))
  23316. (unless splice (push (if head "<thead>" "<tbody>") html))
  23317. (setq tbopen t)
  23318. (while (setq line (pop lines))
  23319. (catch 'next-line
  23320. (if (string-match "^[ \t]*|-" line)
  23321. (progn
  23322. (unless splice
  23323. (push (if head "</thead>" "</tbody>") html)
  23324. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23325. (setq head nil) ;; head ends here, first time around
  23326. ;; ignore this line
  23327. (throw 'next-line t)))
  23328. ;; Break the line into fields
  23329. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23330. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23331. (setq nlines (1+ nlines) i -1)
  23332. (push (concat "<tr>"
  23333. (mapconcat
  23334. (lambda (x)
  23335. (setq i (1+ i))
  23336. (if (and (< i nlines)
  23337. (string-match org-table-number-regexp x))
  23338. (incf (aref fnum i)))
  23339. (if head
  23340. (concat (car org-export-table-header-tags) x
  23341. (cdr org-export-table-header-tags))
  23342. (concat (car org-export-table-data-tags) x
  23343. (cdr org-export-table-data-tags))))
  23344. fields "")
  23345. "</tr>")
  23346. html)))
  23347. (unless splice (if tbopen (push "</tbody>" html)))
  23348. (unless splice (push "</table>\n" html))
  23349. (setq html (nreverse html))
  23350. (unless splice
  23351. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23352. (push (mapconcat
  23353. (lambda (x)
  23354. (setq gr (pop org-table-colgroup-info))
  23355. (format "%s<col align=\"%s\"></col>%s"
  23356. (if (memq gr '(:start :startend))
  23357. (prog1
  23358. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23359. (setq colgropen t))
  23360. "")
  23361. (if (> (/ (float x) nlines) org-table-number-fraction)
  23362. "right" "left")
  23363. (if (memq gr '(:end :startend))
  23364. (progn (setq colgropen nil) "</colgroup>")
  23365. "")))
  23366. fnum "")
  23367. html)
  23368. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23369. (push html-table-tag html))
  23370. (concat (mapconcat 'identity html "\n") "\n")))
  23371. (defun org-table-clean-before-export (lines)
  23372. "Check if the table has a marking column.
  23373. If yes remove the column and the special lines."
  23374. (setq org-table-colgroup-info nil)
  23375. (if (memq nil
  23376. (mapcar
  23377. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23378. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23379. lines))
  23380. (progn
  23381. (setq org-table-clean-did-remove-column nil)
  23382. (delq nil
  23383. (mapcar
  23384. (lambda (x)
  23385. (cond
  23386. ((string-match "^[ \t]*| */ *|" x)
  23387. (setq org-table-colgroup-info
  23388. (mapcar (lambda (x)
  23389. (cond ((member x '("<" "&lt;")) :start)
  23390. ((member x '(">" "&gt;")) :end)
  23391. ((member x '("<>" "&lt;&gt;")) :startend)
  23392. (t nil)))
  23393. (org-split-string x "[ \t]*|[ \t]*")))
  23394. nil)
  23395. (t x)))
  23396. lines)))
  23397. (setq org-table-clean-did-remove-column t)
  23398. (delq nil
  23399. (mapcar
  23400. (lambda (x)
  23401. (cond
  23402. ((string-match "^[ \t]*| */ *|" x)
  23403. (setq org-table-colgroup-info
  23404. (mapcar (lambda (x)
  23405. (cond ((member x '("<" "&lt;")) :start)
  23406. ((member x '(">" "&gt;")) :end)
  23407. ((member x '("<>" "&lt;&gt;")) :startend)
  23408. (t nil)))
  23409. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23410. nil)
  23411. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23412. nil) ; ignore this line
  23413. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23414. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23415. ;; remove the first column
  23416. (replace-match "\\1|" t nil x))))
  23417. lines))))
  23418. (defun org-format-table-table-html (lines)
  23419. "Format a table generated by table.el into HTML.
  23420. This conversion does *not* use `table-generate-source' from table.el.
  23421. This has the advantage that Org-mode's HTML conversions can be used.
  23422. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23423. (let (line field-buffer
  23424. (head org-export-highlight-first-table-line)
  23425. fields html empty)
  23426. (setq html (concat html-table-tag "\n"))
  23427. (while (setq line (pop lines))
  23428. (setq empty "&nbsp;")
  23429. (catch 'next-line
  23430. (if (string-match "^[ \t]*\\+-" line)
  23431. (progn
  23432. (if field-buffer
  23433. (progn
  23434. (setq
  23435. html
  23436. (concat
  23437. html
  23438. "<tr>"
  23439. (mapconcat
  23440. (lambda (x)
  23441. (if (equal x "") (setq x empty))
  23442. (if head
  23443. (concat (car org-export-table-header-tags) x
  23444. (cdr org-export-table-header-tags))
  23445. (concat (car org-export-table-data-tags) x
  23446. (cdr org-export-table-data-tags))))
  23447. field-buffer "\n")
  23448. "</tr>\n"))
  23449. (setq head nil)
  23450. (setq field-buffer nil)))
  23451. ;; Ignore this line
  23452. (throw 'next-line t)))
  23453. ;; Break the line into fields and store the fields
  23454. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23455. (if field-buffer
  23456. (setq field-buffer (mapcar
  23457. (lambda (x)
  23458. (concat x "<br/>" (pop fields)))
  23459. field-buffer))
  23460. (setq field-buffer fields))))
  23461. (setq html (concat html "</table>\n"))
  23462. html))
  23463. (defun org-format-table-table-html-using-table-generate-source (lines)
  23464. "Format a table into html, using `table-generate-source' from table.el.
  23465. This has the advantage that cell- or row-spanning is allowed.
  23466. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23467. (require 'table)
  23468. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23469. (erase-buffer)
  23470. (insert (mapconcat 'identity lines "\n"))
  23471. (goto-char (point-min))
  23472. (if (not (re-search-forward "|[^+]" nil t))
  23473. (error "Error processing table"))
  23474. (table-recognize-table)
  23475. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23476. (table-generate-source 'html " org-tmp2 ")
  23477. (set-buffer " org-tmp2 ")
  23478. (buffer-substring (point-min) (point-max))))
  23479. (defun org-html-handle-time-stamps (s)
  23480. "Format time stamps in string S, or remove them."
  23481. (catch 'exit
  23482. (let (r b)
  23483. (while (string-match org-maybe-keyword-time-regexp s)
  23484. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23485. ;; never export CLOCK
  23486. (throw 'exit ""))
  23487. (or b (setq b (substring s 0 (match-beginning 0))))
  23488. (if (not org-export-with-timestamps)
  23489. (setq r (concat r (substring s 0 (match-beginning 0)))
  23490. s (substring s (match-end 0)))
  23491. (setq r (concat
  23492. r (substring s 0 (match-beginning 0))
  23493. (if (match-end 1)
  23494. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23495. (match-string 1 s)))
  23496. (format " @<span class=\"timestamp\">%s@</span>"
  23497. (substring
  23498. (org-translate-time (match-string 3 s)) 1 -1)))
  23499. s (substring s (match-end 0)))))
  23500. ;; Line break if line started and ended with time stamp stuff
  23501. (if (not r)
  23502. s
  23503. (setq r (concat r s))
  23504. (unless (string-match "\\S-" (concat b s))
  23505. (setq r (concat r "@<br/>")))
  23506. r))))
  23507. (defun org-html-protect (s)
  23508. ;; convert & to &amp;, < to &lt; and > to &gt;
  23509. (let ((start 0))
  23510. (while (string-match "&" s start)
  23511. (setq s (replace-match "&amp;" t t s)
  23512. start (1+ (match-beginning 0))))
  23513. (while (string-match "<" s)
  23514. (setq s (replace-match "&lt;" t t s)))
  23515. (while (string-match ">" s)
  23516. (setq s (replace-match "&gt;" t t s))))
  23517. s)
  23518. (defun org-export-cleanup-toc-line (s)
  23519. "Remove tags and time staps from lines going into the toc."
  23520. (when (memq org-export-with-tags '(not-in-toc nil))
  23521. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23522. (setq s (replace-match "" t t s))))
  23523. (when org-export-remove-timestamps-from-toc
  23524. (while (string-match org-maybe-keyword-time-regexp s)
  23525. (setq s (replace-match "" t t s))))
  23526. (while (string-match org-bracket-link-regexp s)
  23527. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23528. t t s)))
  23529. s)
  23530. (defun org-html-expand (string)
  23531. "Prepare STRING for HTML export. Applies all active conversions.
  23532. If there are links in the string, don't modify these."
  23533. (let* ((re (concat org-bracket-link-regexp "\\|"
  23534. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23535. m s l res)
  23536. (while (setq m (string-match re string))
  23537. (setq s (substring string 0 m)
  23538. l (match-string 0 string)
  23539. string (substring string (match-end 0)))
  23540. (push (org-html-do-expand s) res)
  23541. (push l res))
  23542. (push (org-html-do-expand string) res)
  23543. (apply 'concat (nreverse res))))
  23544. (defun org-html-do-expand (s)
  23545. "Apply all active conversions to translate special ASCII to HTML."
  23546. (setq s (org-html-protect s))
  23547. (if org-export-html-expand
  23548. (let ((start 0))
  23549. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23550. (setq s (replace-match "<\\1>" t nil s)))))
  23551. (if org-export-with-emphasize
  23552. (setq s (org-export-html-convert-emphasize s)))
  23553. (if org-export-with-special-strings
  23554. (setq s (org-export-html-convert-special-strings s)))
  23555. (if org-export-with-sub-superscripts
  23556. (setq s (org-export-html-convert-sub-super s)))
  23557. (if org-export-with-TeX-macros
  23558. (let ((start 0) wd ass)
  23559. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23560. (if (get-text-property (match-beginning 0) 'org-protected s)
  23561. (setq start (match-end 0))
  23562. (setq wd (match-string 1 s))
  23563. (if (setq ass (assoc wd org-html-entities))
  23564. (setq s (replace-match (or (cdr ass)
  23565. (concat "&" (car ass) ";"))
  23566. t t s))
  23567. (setq start (+ start (length wd))))))))
  23568. s)
  23569. (defun org-create-multibrace-regexp (left right n)
  23570. "Create a regular expression which will match a balanced sexp.
  23571. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23572. as single character strings.
  23573. The regexp returned will match the entire expression including the
  23574. delimiters. It will also define a single group which contains the
  23575. match except for the outermost delimiters. The maximum depth of
  23576. stacked delimiters is N. Escaping delimiters is not possible."
  23577. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23578. (or "\\|")
  23579. (re nothing)
  23580. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23581. (while (> n 1)
  23582. (setq n (1- n)
  23583. re (concat re or next)
  23584. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23585. (concat left "\\(" re "\\)" right)))
  23586. (defvar org-match-substring-regexp
  23587. (concat
  23588. "\\([^\\]\\)\\([_^]\\)\\("
  23589. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23590. "\\|"
  23591. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23592. "\\|"
  23593. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23594. "The regular expression matching a sub- or superscript.")
  23595. (defvar org-match-substring-with-braces-regexp
  23596. (concat
  23597. "\\([^\\]\\)\\([_^]\\)\\("
  23598. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23599. "\\)")
  23600. "The regular expression matching a sub- or superscript, forcing braces.")
  23601. (defconst org-export-html-special-string-regexps
  23602. '(("\\\\-" . "&shy;")
  23603. ("---\\([^-]\\)" . "&mdash;\\1")
  23604. ("--\\([^-]\\)" . "&ndash;\\1")
  23605. ("\\.\\.\\." . "&hellip;"))
  23606. "Regular expressions for special string conversion.")
  23607. (defun org-export-html-convert-special-strings (string)
  23608. "Convert special characters in STRING to HTML."
  23609. (let ((all org-export-html-special-string-regexps)
  23610. e a re rpl start)
  23611. (while (setq a (pop all))
  23612. (setq re (car a) rpl (cdr a) start 0)
  23613. (while (string-match re string start)
  23614. (if (get-text-property (match-beginning 0) 'org-protected string)
  23615. (setq start (match-end 0))
  23616. (setq string (replace-match rpl t nil string)))))
  23617. string))
  23618. (defun org-export-html-convert-sub-super (string)
  23619. "Convert sub- and superscripts in STRING to HTML."
  23620. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  23621. (while (string-match org-match-substring-regexp string s)
  23622. (cond
  23623. ((and requireb (match-end 8)) (setq s (match-end 2)))
  23624. ((get-text-property (match-beginning 2) 'org-protected string)
  23625. (setq s (match-end 2)))
  23626. (t
  23627. (setq s (match-end 1)
  23628. key (if (string= (match-string 2 string) "_") "sub" "sup")
  23629. c (or (match-string 8 string)
  23630. (match-string 6 string)
  23631. (match-string 5 string))
  23632. string (replace-match
  23633. (concat (match-string 1 string)
  23634. "<" key ">" c "</" key ">")
  23635. t t string)))))
  23636. (while (string-match "\\\\\\([_^]\\)" string)
  23637. (setq string (replace-match (match-string 1 string) t t string)))
  23638. string))
  23639. (defun org-export-html-convert-emphasize (string)
  23640. "Apply emphasis."
  23641. (let ((s 0) rpl)
  23642. (while (string-match org-emph-re string s)
  23643. (if (not (equal
  23644. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  23645. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  23646. (setq s (match-beginning 0)
  23647. rpl
  23648. (concat
  23649. (match-string 1 string)
  23650. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  23651. (match-string 4 string)
  23652. (nth 3 (assoc (match-string 3 string)
  23653. org-emphasis-alist))
  23654. (match-string 5 string))
  23655. string (replace-match rpl t t string)
  23656. s (+ s (- (length rpl) 2)))
  23657. (setq s (1+ s))))
  23658. string))
  23659. (defvar org-par-open nil)
  23660. (defun org-open-par ()
  23661. "Insert <p>, but first close previous paragraph if any."
  23662. (org-close-par-maybe)
  23663. (insert "\n<p>")
  23664. (setq org-par-open t))
  23665. (defun org-close-par-maybe ()
  23666. "Close paragraph if there is one open."
  23667. (when org-par-open
  23668. (insert "</p>")
  23669. (setq org-par-open nil)))
  23670. (defun org-close-li ()
  23671. "Close <li> if necessary."
  23672. (org-close-par-maybe)
  23673. (insert "</li>\n"))
  23674. (defvar body-only) ; dynamically scoped into this.
  23675. (defun org-html-level-start (level title umax with-toc head-count)
  23676. "Insert a new level in HTML export.
  23677. When TITLE is nil, just close all open levels."
  23678. (org-close-par-maybe)
  23679. (let ((l org-level-max))
  23680. (while (>= l level)
  23681. (if (aref org-levels-open (1- l))
  23682. (progn
  23683. (org-html-level-close l umax)
  23684. (aset org-levels-open (1- l) nil)))
  23685. (setq l (1- l)))
  23686. (when title
  23687. ;; If title is nil, this means this function is called to close
  23688. ;; all levels, so the rest is done only if title is given
  23689. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23690. (setq title (replace-match
  23691. (if org-export-with-tags
  23692. (save-match-data
  23693. (concat
  23694. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  23695. (mapconcat 'identity (org-split-string
  23696. (match-string 1 title) ":")
  23697. "&nbsp;")
  23698. "</span>"))
  23699. "")
  23700. t t title)))
  23701. (if (> level umax)
  23702. (progn
  23703. (if (aref org-levels-open (1- level))
  23704. (progn
  23705. (org-close-li)
  23706. (insert "<li>" title "<br/>\n"))
  23707. (aset org-levels-open (1- level) t)
  23708. (org-close-par-maybe)
  23709. (insert "<ul>\n<li>" title "<br/>\n")))
  23710. (aset org-levels-open (1- level) t)
  23711. (if (and org-export-with-section-numbers (not body-only))
  23712. (setq title (concat (org-section-number level) " " title)))
  23713. (setq level (+ level org-export-html-toplevel-hlevel -1))
  23714. (if with-toc
  23715. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  23716. level level head-count title level))
  23717. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  23718. (org-open-par)))))
  23719. (defun org-html-level-close (level max-outline-level)
  23720. "Terminate one level in HTML export."
  23721. (if (<= level max-outline-level)
  23722. (insert "</div>\n")
  23723. (org-close-li)
  23724. (insert "</ul>\n")))
  23725. ;;; iCalendar export
  23726. ;;;###autoload
  23727. (defun org-export-icalendar-this-file ()
  23728. "Export current file as an iCalendar file.
  23729. The iCalendar file will be located in the same directory as the Org-mode
  23730. file, but with extension `.ics'."
  23731. (interactive)
  23732. (org-export-icalendar nil buffer-file-name))
  23733. ;;;###autoload
  23734. (defun org-export-icalendar-all-agenda-files ()
  23735. "Export all files in `org-agenda-files' to iCalendar .ics files.
  23736. Each iCalendar file will be located in the same directory as the Org-mode
  23737. file, but with extension `.ics'."
  23738. (interactive)
  23739. (apply 'org-export-icalendar nil (org-agenda-files t)))
  23740. ;;;###autoload
  23741. (defun org-export-icalendar-combine-agenda-files ()
  23742. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  23743. The file is stored under the name `org-combined-agenda-icalendar-file'."
  23744. (interactive)
  23745. (apply 'org-export-icalendar t (org-agenda-files t)))
  23746. (defun org-export-icalendar (combine &rest files)
  23747. "Create iCalendar files for all elements of FILES.
  23748. If COMBINE is non-nil, combine all calendar entries into a single large
  23749. file and store it under the name `org-combined-agenda-icalendar-file'."
  23750. (save-excursion
  23751. (org-prepare-agenda-buffers files)
  23752. (let* ((dir (org-export-directory
  23753. :ical (list :publishing-directory
  23754. org-export-publishing-directory)))
  23755. file ical-file ical-buffer category started org-agenda-new-buffers)
  23756. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  23757. (when combine
  23758. (setq ical-file
  23759. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  23760. org-combined-agenda-icalendar-file
  23761. (expand-file-name org-combined-agenda-icalendar-file dir))
  23762. ical-buffer (org-get-agenda-file-buffer ical-file))
  23763. (set-buffer ical-buffer) (erase-buffer))
  23764. (while (setq file (pop files))
  23765. (catch 'nextfile
  23766. (org-check-agenda-file file)
  23767. (set-buffer (org-get-agenda-file-buffer file))
  23768. (unless combine
  23769. (setq ical-file (concat (file-name-as-directory dir)
  23770. (file-name-sans-extension
  23771. (file-name-nondirectory buffer-file-name))
  23772. ".ics"))
  23773. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  23774. (with-current-buffer ical-buffer (erase-buffer)))
  23775. (setq category (or org-category
  23776. (file-name-sans-extension
  23777. (file-name-nondirectory buffer-file-name))))
  23778. (if (symbolp category) (setq category (symbol-name category)))
  23779. (let ((standard-output ical-buffer))
  23780. (if combine
  23781. (and (not started) (setq started t)
  23782. (org-start-icalendar-file org-icalendar-combined-name))
  23783. (org-start-icalendar-file category))
  23784. (org-print-icalendar-entries combine)
  23785. (when (or (and combine (not files)) (not combine))
  23786. (org-finish-icalendar-file)
  23787. (set-buffer ical-buffer)
  23788. (save-buffer)
  23789. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  23790. (org-release-buffers org-agenda-new-buffers))))
  23791. (defvar org-after-save-iCalendar-file-hook nil
  23792. "Hook run after an iCalendar file has been saved.
  23793. The iCalendar buffer is still current when this hook is run.
  23794. A good way to use this is to tell a desktop calenndar application to re-read
  23795. the iCalendar file.")
  23796. (defun org-print-icalendar-entries (&optional combine)
  23797. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  23798. When COMBINE is non nil, add the category to each line."
  23799. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  23800. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  23801. (dts (org-ical-ts-to-string
  23802. (format-time-string (cdr org-time-stamp-formats) (current-time))
  23803. "DTSTART"))
  23804. hd ts ts2 state status (inc t) pos b sexp rrule
  23805. scheduledp deadlinep tmp pri category entry location summary desc
  23806. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  23807. (org-refresh-category-properties)
  23808. (save-excursion
  23809. (goto-char (point-min))
  23810. (while (re-search-forward re1 nil t)
  23811. (catch :skip
  23812. (org-agenda-skip)
  23813. (setq pos (match-beginning 0)
  23814. ts (match-string 0)
  23815. inc t
  23816. hd (org-get-heading)
  23817. summary (org-icalendar-cleanup-string
  23818. (org-entry-get nil "SUMMARY"))
  23819. desc (org-icalendar-cleanup-string
  23820. (or (org-entry-get nil "DESCRIPTION")
  23821. (and org-icalendar-include-body (org-get-entry)))
  23822. t org-icalendar-include-body)
  23823. location (org-icalendar-cleanup-string
  23824. (org-entry-get nil "LOCATION"))
  23825. category (org-get-category))
  23826. (if (looking-at re2)
  23827. (progn
  23828. (goto-char (match-end 0))
  23829. (setq ts2 (match-string 1) inc nil))
  23830. (setq tmp (buffer-substring (max (point-min)
  23831. (- pos org-ds-keyword-length))
  23832. pos)
  23833. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  23834. (progn
  23835. (setq inc nil)
  23836. (replace-match "\\1" t nil ts))
  23837. ts)
  23838. deadlinep (string-match org-deadline-regexp tmp)
  23839. scheduledp (string-match org-scheduled-regexp tmp)
  23840. ;; donep (org-entry-is-done-p)
  23841. ))
  23842. (if (or (string-match org-tr-regexp hd)
  23843. (string-match org-ts-regexp hd))
  23844. (setq hd (replace-match "" t t hd)))
  23845. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  23846. (setq rrule
  23847. (concat "\nRRULE:FREQ="
  23848. (cdr (assoc
  23849. (match-string 2 ts)
  23850. '(("d" . "DAILY")("w" . "WEEKLY")
  23851. ("m" . "MONTHLY")("y" . "YEARLY"))))
  23852. ";INTERVAL=" (match-string 1 ts)))
  23853. (setq rrule ""))
  23854. (setq summary (or summary hd))
  23855. (if (string-match org-bracket-link-regexp summary)
  23856. (setq summary
  23857. (replace-match (if (match-end 3)
  23858. (match-string 3 summary)
  23859. (match-string 1 summary))
  23860. t t summary)))
  23861. (if deadlinep (setq summary (concat "DL: " summary)))
  23862. (if scheduledp (setq summary (concat "S: " summary)))
  23863. (if (string-match "\\`<%%" ts)
  23864. (with-current-buffer sexp-buffer
  23865. (insert (substring ts 1 -1) " " summary "\n"))
  23866. (princ (format "BEGIN:VEVENT
  23867. %s
  23868. %s%s
  23869. SUMMARY:%s%s%s
  23870. CATEGORIES:%s
  23871. END:VEVENT\n"
  23872. (org-ical-ts-to-string ts "DTSTART")
  23873. (org-ical-ts-to-string ts2 "DTEND" inc)
  23874. rrule summary
  23875. (if (and desc (string-match "\\S-" desc))
  23876. (concat "\nDESCRIPTION: " desc) "")
  23877. (if (and location (string-match "\\S-" location))
  23878. (concat "\nLOCATION: " location) "")
  23879. category)))))
  23880. (when (and org-icalendar-include-sexps
  23881. (condition-case nil (require 'icalendar) (error nil))
  23882. (fboundp 'icalendar-export-region))
  23883. ;; Get all the literal sexps
  23884. (goto-char (point-min))
  23885. (while (re-search-forward "^&?%%(" nil t)
  23886. (catch :skip
  23887. (org-agenda-skip)
  23888. (setq b (match-beginning 0))
  23889. (goto-char (1- (match-end 0)))
  23890. (forward-sexp 1)
  23891. (end-of-line 1)
  23892. (setq sexp (buffer-substring b (point)))
  23893. (with-current-buffer sexp-buffer
  23894. (insert sexp "\n"))
  23895. (princ (org-diary-to-ical-string sexp-buffer)))))
  23896. (when org-icalendar-include-todo
  23897. (goto-char (point-min))
  23898. (while (re-search-forward org-todo-line-regexp nil t)
  23899. (catch :skip
  23900. (org-agenda-skip)
  23901. (setq state (match-string 2))
  23902. (setq status (if (member state org-done-keywords)
  23903. "COMPLETED" "NEEDS-ACTION"))
  23904. (when (and state
  23905. (or (not (member state org-done-keywords))
  23906. (eq org-icalendar-include-todo 'all))
  23907. (not (member org-archive-tag (org-get-tags-at)))
  23908. )
  23909. (setq hd (match-string 3)
  23910. summary (org-icalendar-cleanup-string
  23911. (org-entry-get nil "SUMMARY"))
  23912. desc (org-icalendar-cleanup-string
  23913. (or (org-entry-get nil "DESCRIPTION")
  23914. (and org-icalendar-include-body (org-get-entry)))
  23915. t org-icalendar-include-body)
  23916. location (org-icalendar-cleanup-string
  23917. (org-entry-get nil "LOCATION")))
  23918. (if (string-match org-bracket-link-regexp hd)
  23919. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  23920. (match-string 1 hd))
  23921. t t hd)))
  23922. (if (string-match org-priority-regexp hd)
  23923. (setq pri (string-to-char (match-string 2 hd))
  23924. hd (concat (substring hd 0 (match-beginning 1))
  23925. (substring hd (match-end 1))))
  23926. (setq pri org-default-priority))
  23927. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  23928. (- org-lowest-priority org-highest-priority))))))
  23929. (princ (format "BEGIN:VTODO
  23930. %s
  23931. SUMMARY:%s%s%s
  23932. CATEGORIES:%s
  23933. SEQUENCE:1
  23934. PRIORITY:%d
  23935. STATUS:%s
  23936. END:VTODO\n"
  23937. dts
  23938. (or summary hd)
  23939. (if (and location (string-match "\\S-" location))
  23940. (concat "\nLOCATION: " location) "")
  23941. (if (and desc (string-match "\\S-" desc))
  23942. (concat "\nDESCRIPTION: " desc) "")
  23943. category pri status)))))))))
  23944. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  23945. "Take out stuff and quote what needs to be quoted.
  23946. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  23947. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  23948. characters."
  23949. (if (not s)
  23950. nil
  23951. (when is-body
  23952. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  23953. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  23954. (while (string-match re s) (setq s (replace-match "" t t s)))
  23955. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  23956. (let ((start 0))
  23957. (while (string-match "\\([,;\\]\\)" s start)
  23958. (setq start (+ (match-beginning 0) 2)
  23959. s (replace-match "\\\\\\1" nil nil s))))
  23960. (when is-body
  23961. (while (string-match "[ \t]*\n[ \t]*" s)
  23962. (setq s (replace-match "\\n" t t s))))
  23963. (setq s (org-trim s))
  23964. (if is-body
  23965. (if maxlength
  23966. (if (and (numberp maxlength)
  23967. (> (length s) maxlength))
  23968. (setq s (substring s 0 maxlength)))))
  23969. s))
  23970. (defun org-get-entry ()
  23971. "Clean-up description string."
  23972. (save-excursion
  23973. (org-back-to-heading t)
  23974. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  23975. (defun org-start-icalendar-file (name)
  23976. "Start an iCalendar file by inserting the header."
  23977. (let ((user user-full-name)
  23978. (name (or name "unknown"))
  23979. (timezone (cadr (current-time-zone))))
  23980. (princ
  23981. (format "BEGIN:VCALENDAR
  23982. VERSION:2.0
  23983. X-WR-CALNAME:%s
  23984. PRODID:-//%s//Emacs with Org-mode//EN
  23985. X-WR-TIMEZONE:%s
  23986. CALSCALE:GREGORIAN\n" name user timezone))))
  23987. (defun org-finish-icalendar-file ()
  23988. "Finish an iCalendar file by inserting the END statement."
  23989. (princ "END:VCALENDAR\n"))
  23990. (defun org-ical-ts-to-string (s keyword &optional inc)
  23991. "Take a time string S and convert it to iCalendar format.
  23992. KEYWORD is added in front, to make a complete line like DTSTART....
  23993. When INC is non-nil, increase the hour by two (if time string contains
  23994. a time), or the day by one (if it does not contain a time)."
  23995. (let ((t1 (org-parse-time-string s 'nodefault))
  23996. t2 fmt have-time time)
  23997. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  23998. (setq t2 t1 have-time t)
  23999. (setq t2 (org-parse-time-string s)))
  24000. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24001. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24002. (when inc
  24003. (if have-time
  24004. (if org-agenda-default-appointment-duration
  24005. (setq mi (+ org-agenda-default-appointment-duration mi))
  24006. (setq h (+ 2 h)))
  24007. (setq d (1+ d))))
  24008. (setq time (encode-time s mi h d m y)))
  24009. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24010. (concat keyword (format-time-string fmt time))))
  24011. ;;; XOXO export
  24012. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24013. (with-current-buffer buffer
  24014. (apply 'insert output)))
  24015. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24016. (defun org-export-as-xoxo (&optional buffer)
  24017. "Export the org buffer as XOXO.
  24018. The XOXO buffer is named *xoxo-<source buffer name>*"
  24019. (interactive (list (current-buffer)))
  24020. ;; A quickie abstraction
  24021. ;; Output everything as XOXO
  24022. (with-current-buffer (get-buffer buffer)
  24023. (let* ((pos (point))
  24024. (opt-plist (org-combine-plists (org-default-export-plist)
  24025. (org-infile-export-plist)))
  24026. (filename (concat (file-name-as-directory
  24027. (org-export-directory :xoxo opt-plist))
  24028. (file-name-sans-extension
  24029. (file-name-nondirectory buffer-file-name))
  24030. ".html"))
  24031. (out (find-file-noselect filename))
  24032. (last-level 1)
  24033. (hanging-li nil))
  24034. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24035. ;; Check the output buffer is empty.
  24036. (with-current-buffer out (erase-buffer))
  24037. ;; Kick off the output
  24038. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24039. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24040. (let* ((hd (match-string-no-properties 1))
  24041. (level (length hd))
  24042. (text (concat
  24043. (match-string-no-properties 2)
  24044. (save-excursion
  24045. (goto-char (match-end 0))
  24046. (let ((str ""))
  24047. (catch 'loop
  24048. (while 't
  24049. (forward-line)
  24050. (if (looking-at "^[ \t]\\(.*\\)")
  24051. (setq str (concat str (match-string-no-properties 1)))
  24052. (throw 'loop str)))))))))
  24053. ;; Handle level rendering
  24054. (cond
  24055. ((> level last-level)
  24056. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24057. ((< level last-level)
  24058. (dotimes (- (- last-level level) 1)
  24059. (if hanging-li
  24060. (org-export-as-xoxo-insert-into out "</li>\n"))
  24061. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24062. (when hanging-li
  24063. (org-export-as-xoxo-insert-into out "</li>\n")
  24064. (setq hanging-li nil)))
  24065. ((equal level last-level)
  24066. (if hanging-li
  24067. (org-export-as-xoxo-insert-into out "</li>\n")))
  24068. )
  24069. (setq last-level level)
  24070. ;; And output the new li
  24071. (setq hanging-li 't)
  24072. (if (equal ?+ (elt text 0))
  24073. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24074. (org-export-as-xoxo-insert-into out "<li>" text))))
  24075. ;; Finally finish off the ol
  24076. (dotimes (- last-level 1)
  24077. (if hanging-li
  24078. (org-export-as-xoxo-insert-into out "</li>\n"))
  24079. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24080. (goto-char pos)
  24081. ;; Finish the buffer off and clean it up.
  24082. (switch-to-buffer-other-window out)
  24083. (indent-region (point-min) (point-max) nil)
  24084. (save-buffer)
  24085. (goto-char (point-min))
  24086. )))
  24087. ;;;; Key bindings
  24088. ;; Make `C-c C-x' a prefix key
  24089. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24090. ;; TAB key with modifiers
  24091. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24092. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24093. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24094. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24095. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24096. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24097. ;; The following line is necessary under Suse GNU/Linux
  24098. (unless (featurep 'xemacs)
  24099. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24100. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24101. (define-key org-mode-map [backtab] 'org-shifttab)
  24102. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24103. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24104. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24105. ;; Cursor keys with modifiers
  24106. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24107. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24108. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24109. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24110. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24111. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24112. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24113. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24114. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24115. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24116. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24117. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24118. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24119. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24120. ;;; Extra keys for tty access.
  24121. ;; We only set them when really needed because otherwise the
  24122. ;; menus don't show the simple keys
  24123. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24124. (not window-system))
  24125. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24126. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24127. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24128. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24129. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24130. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24131. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24132. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24133. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24134. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24135. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24136. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24137. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24138. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24139. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24140. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24141. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24142. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24143. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24144. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24145. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24146. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24147. ;; All the other keys
  24148. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24149. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24150. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24151. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24152. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24153. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24154. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24155. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24156. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24157. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24158. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24159. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24160. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24161. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24162. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24163. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24164. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24165. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24166. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24167. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24168. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24169. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24170. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24171. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24172. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24173. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24174. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24175. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24176. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24177. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24178. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24179. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24180. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24181. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24182. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24183. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24184. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24185. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24186. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24187. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24188. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24189. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24190. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24191. (org-defkey org-mode-map "\C-m" 'org-return)
  24192. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24193. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24194. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24195. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24196. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24197. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24198. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24199. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24200. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24201. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24202. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24203. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24204. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24205. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24206. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24207. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24208. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24209. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24210. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24211. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24212. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24213. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24214. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24215. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24216. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24217. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24218. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24219. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24220. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24221. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24222. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24223. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24224. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24225. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24226. (when (featurep 'xemacs)
  24227. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24228. (defsubst org-table-p () (org-at-table-p))
  24229. (defun org-self-insert-command (N)
  24230. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24231. If the cursor is in a table looking at whitespace, the whitespace is
  24232. overwritten, and the table is not marked as requiring realignment."
  24233. (interactive "p")
  24234. (if (and (org-table-p)
  24235. (progn
  24236. ;; check if we blank the field, and if that triggers align
  24237. (and org-table-auto-blank-field
  24238. (member last-command
  24239. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24240. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24241. ;; got extra space, this field does not determine column width
  24242. (let (org-table-may-need-update) (org-table-blank-field))
  24243. ;; no extra space, this field may determine column width
  24244. (org-table-blank-field)))
  24245. t)
  24246. (eq N 1)
  24247. (looking-at "[^|\n]* |"))
  24248. (let (org-table-may-need-update)
  24249. (goto-char (1- (match-end 0)))
  24250. (delete-backward-char 1)
  24251. (goto-char (match-beginning 0))
  24252. (self-insert-command N))
  24253. (setq org-table-may-need-update t)
  24254. (self-insert-command N)
  24255. (org-fix-tags-on-the-fly)))
  24256. (defun org-fix-tags-on-the-fly ()
  24257. (when (and (equal (char-after (point-at-bol)) ?*)
  24258. (org-on-heading-p))
  24259. (org-align-tags-here org-tags-column)))
  24260. (defun org-delete-backward-char (N)
  24261. "Like `delete-backward-char', insert whitespace at field end in tables.
  24262. When deleting backwards, in tables this function will insert whitespace in
  24263. front of the next \"|\" separator, to keep the table aligned. The table will
  24264. still be marked for re-alignment if the field did fill the entire column,
  24265. because, in this case the deletion might narrow the column."
  24266. (interactive "p")
  24267. (if (and (org-table-p)
  24268. (eq N 1)
  24269. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24270. (looking-at ".*?|"))
  24271. (let ((pos (point))
  24272. (noalign (looking-at "[^|\n\r]* |"))
  24273. (c org-table-may-need-update))
  24274. (backward-delete-char N)
  24275. (skip-chars-forward "^|")
  24276. (insert " ")
  24277. (goto-char (1- pos))
  24278. ;; noalign: if there were two spaces at the end, this field
  24279. ;; does not determine the width of the column.
  24280. (if noalign (setq org-table-may-need-update c)))
  24281. (backward-delete-char N)
  24282. (org-fix-tags-on-the-fly)))
  24283. (defun org-delete-char (N)
  24284. "Like `delete-char', but insert whitespace at field end in tables.
  24285. When deleting characters, in tables this function will insert whitespace in
  24286. front of the next \"|\" separator, to keep the table aligned. The table will
  24287. still be marked for re-alignment if the field did fill the entire column,
  24288. because, in this case the deletion might narrow the column."
  24289. (interactive "p")
  24290. (if (and (org-table-p)
  24291. (not (bolp))
  24292. (not (= (char-after) ?|))
  24293. (eq N 1))
  24294. (if (looking-at ".*?|")
  24295. (let ((pos (point))
  24296. (noalign (looking-at "[^|\n\r]* |"))
  24297. (c org-table-may-need-update))
  24298. (replace-match (concat
  24299. (substring (match-string 0) 1 -1)
  24300. " |"))
  24301. (goto-char pos)
  24302. ;; noalign: if there were two spaces at the end, this field
  24303. ;; does not determine the width of the column.
  24304. (if noalign (setq org-table-may-need-update c)))
  24305. (delete-char N))
  24306. (delete-char N)
  24307. (org-fix-tags-on-the-fly)))
  24308. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24309. (put 'org-self-insert-command 'delete-selection t)
  24310. (put 'orgtbl-self-insert-command 'delete-selection t)
  24311. (put 'org-delete-char 'delete-selection 'supersede)
  24312. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24313. ;; Make `flyspell-mode' delay after some commands
  24314. (put 'org-self-insert-command 'flyspell-delayed t)
  24315. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24316. (put 'org-delete-char 'flyspell-delayed t)
  24317. (put 'org-delete-backward-char 'flyspell-delayed t)
  24318. ;; Make pabbrev-mode expand after org-mode commands
  24319. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24320. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24321. ;; How to do this: Measure non-white length of current string
  24322. ;; If equal to column width, we should realign.
  24323. (defun org-remap (map &rest commands)
  24324. "In MAP, remap the functions given in COMMANDS.
  24325. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24326. (let (new old)
  24327. (while commands
  24328. (setq old (pop commands) new (pop commands))
  24329. (if (fboundp 'command-remapping)
  24330. (org-defkey map (vector 'remap old) new)
  24331. (substitute-key-definition old new map global-map)))))
  24332. (when (eq org-enable-table-editor 'optimized)
  24333. ;; If the user wants maximum table support, we need to hijack
  24334. ;; some standard editing functions
  24335. (org-remap org-mode-map
  24336. 'self-insert-command 'org-self-insert-command
  24337. 'delete-char 'org-delete-char
  24338. 'delete-backward-char 'org-delete-backward-char)
  24339. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24340. (defun org-shiftcursor-error ()
  24341. "Throw an error because Shift-Cursor command was applied in wrong context."
  24342. (error "This command is active in special context like tables, headlines or timestamps"))
  24343. (defun org-shifttab (&optional arg)
  24344. "Global visibility cycling or move to previous table field.
  24345. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24346. on context.
  24347. See the individual commands for more information."
  24348. (interactive "P")
  24349. (cond
  24350. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24351. (arg (message "Content view to level: ")
  24352. (org-content (prefix-numeric-value arg))
  24353. (setq org-cycle-global-status 'overview))
  24354. (t (call-interactively 'org-global-cycle))))
  24355. (defun org-shiftmetaleft ()
  24356. "Promote subtree or delete table column.
  24357. Calls `org-promote-subtree', `org-outdent-item',
  24358. or `org-table-delete-column', depending on context.
  24359. See the individual commands for more information."
  24360. (interactive)
  24361. (cond
  24362. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24363. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24364. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24365. (t (org-shiftcursor-error))))
  24366. (defun org-shiftmetaright ()
  24367. "Demote subtree or insert table column.
  24368. Calls `org-demote-subtree', `org-indent-item',
  24369. or `org-table-insert-column', depending on context.
  24370. See the individual commands for more information."
  24371. (interactive)
  24372. (cond
  24373. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24374. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24375. ((org-at-item-p) (call-interactively 'org-indent-item))
  24376. (t (org-shiftcursor-error))))
  24377. (defun org-shiftmetaup (&optional arg)
  24378. "Move subtree up or kill table row.
  24379. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24380. `org-move-item-up' depending on context. See the individual commands
  24381. for more information."
  24382. (interactive "P")
  24383. (cond
  24384. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24385. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24386. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24387. (t (org-shiftcursor-error))))
  24388. (defun org-shiftmetadown (&optional arg)
  24389. "Move subtree down or insert table row.
  24390. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24391. `org-move-item-down', depending on context. See the individual
  24392. commands for more information."
  24393. (interactive "P")
  24394. (cond
  24395. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24396. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24397. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24398. (t (org-shiftcursor-error))))
  24399. (defun org-metaleft (&optional arg)
  24400. "Promote heading or move table column to left.
  24401. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24402. With no specific context, calls the Emacs default `backward-word'.
  24403. See the individual commands for more information."
  24404. (interactive "P")
  24405. (cond
  24406. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24407. ((or (org-on-heading-p) (org-region-active-p))
  24408. (call-interactively 'org-do-promote))
  24409. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24410. (t (call-interactively 'backward-word))))
  24411. (defun org-metaright (&optional arg)
  24412. "Demote subtree or move table column to right.
  24413. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24414. With no specific context, calls the Emacs default `forward-word'.
  24415. See the individual commands for more information."
  24416. (interactive "P")
  24417. (cond
  24418. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24419. ((or (org-on-heading-p) (org-region-active-p))
  24420. (call-interactively 'org-do-demote))
  24421. ((org-at-item-p) (call-interactively 'org-indent-item))
  24422. (t (call-interactively 'forward-word))))
  24423. (defun org-metaup (&optional arg)
  24424. "Move subtree up or move table row up.
  24425. Calls `org-move-subtree-up' or `org-table-move-row' or
  24426. `org-move-item-up', depending on context. See the individual commands
  24427. for more information."
  24428. (interactive "P")
  24429. (cond
  24430. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24431. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24432. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24433. (t (transpose-lines 1) (beginning-of-line -1))))
  24434. (defun org-metadown (&optional arg)
  24435. "Move subtree down or move table row down.
  24436. Calls `org-move-subtree-down' or `org-table-move-row' or
  24437. `org-move-item-down', depending on context. See the individual
  24438. commands for more information."
  24439. (interactive "P")
  24440. (cond
  24441. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24442. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24443. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24444. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24445. (defun org-shiftup (&optional arg)
  24446. "Increase item in timestamp or increase priority of current headline.
  24447. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24448. depending on context. See the individual commands for more information."
  24449. (interactive "P")
  24450. (cond
  24451. ((org-at-timestamp-p t)
  24452. (call-interactively (if org-edit-timestamp-down-means-later
  24453. 'org-timestamp-down 'org-timestamp-up)))
  24454. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24455. ((org-at-item-p) (call-interactively 'org-previous-item))
  24456. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24457. (defun org-shiftdown (&optional arg)
  24458. "Decrease item in timestamp or decrease priority of current headline.
  24459. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24460. depending on context. See the individual commands for more information."
  24461. (interactive "P")
  24462. (cond
  24463. ((org-at-timestamp-p t)
  24464. (call-interactively (if org-edit-timestamp-down-means-later
  24465. 'org-timestamp-up 'org-timestamp-down)))
  24466. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24467. (t (call-interactively 'org-next-item))))
  24468. (defun org-shiftright ()
  24469. "Next TODO keyword or timestamp one day later, depending on context."
  24470. (interactive)
  24471. (cond
  24472. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24473. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24474. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24475. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24476. (t (org-shiftcursor-error))))
  24477. (defun org-shiftleft ()
  24478. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24479. (interactive)
  24480. (cond
  24481. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24482. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24483. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24484. ((org-at-property-p)
  24485. (call-interactively 'org-property-previous-allowed-value))
  24486. (t (org-shiftcursor-error))))
  24487. (defun org-shiftcontrolright ()
  24488. "Switch to next TODO set."
  24489. (interactive)
  24490. (cond
  24491. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24492. (t (org-shiftcursor-error))))
  24493. (defun org-shiftcontrolleft ()
  24494. "Switch to previous TODO set."
  24495. (interactive)
  24496. (cond
  24497. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24498. (t (org-shiftcursor-error))))
  24499. (defun org-ctrl-c-ret ()
  24500. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24501. (interactive)
  24502. (cond
  24503. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24504. (t (call-interactively 'org-insert-heading))))
  24505. (defun org-copy-special ()
  24506. "Copy region in table or copy current subtree.
  24507. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24508. See the individual commands for more information."
  24509. (interactive)
  24510. (call-interactively
  24511. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24512. (defun org-cut-special ()
  24513. "Cut region in table or cut current subtree.
  24514. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24515. See the individual commands for more information."
  24516. (interactive)
  24517. (call-interactively
  24518. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24519. (defun org-paste-special (arg)
  24520. "Paste rectangular region into table, or past subtree relative to level.
  24521. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24522. See the individual commands for more information."
  24523. (interactive "P")
  24524. (if (org-at-table-p)
  24525. (org-table-paste-rectangle)
  24526. (org-paste-subtree arg)))
  24527. (defun org-ctrl-c-ctrl-c (&optional arg)
  24528. "Set tags in headline, or update according to changed information at point.
  24529. This command does many different things, depending on context:
  24530. - If the cursor is in a headline, prompt for tags and insert them
  24531. into the current line, aligned to `org-tags-column'. When called
  24532. with prefix arg, realign all tags in the current buffer.
  24533. - If the cursor is in one of the special #+KEYWORD lines, this
  24534. triggers scanning the buffer for these lines and updating the
  24535. information.
  24536. - If the cursor is inside a table, realign the table. This command
  24537. works even if the automatic table editor has been turned off.
  24538. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24539. the entire table.
  24540. - If the cursor is a the beginning of a dynamic block, update it.
  24541. - If the cursor is inside a table created by the table.el package,
  24542. activate that table.
  24543. - If the current buffer is a remember buffer, close note and file it.
  24544. with a prefix argument, file it without further interaction to the default
  24545. location.
  24546. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24547. links in this buffer.
  24548. - If the cursor is on a numbered item in a plain list, renumber the
  24549. ordered list.
  24550. - If the cursor is on a checkbox, toggle it."
  24551. (interactive "P")
  24552. (let ((org-enable-table-editor t))
  24553. (cond
  24554. ((or org-clock-overlays
  24555. org-occur-highlights
  24556. org-latex-fragment-image-overlays)
  24557. (org-remove-clock-overlays)
  24558. (org-remove-occur-highlights)
  24559. (org-remove-latex-fragment-image-overlays)
  24560. (message "Temporary highlights/overlays removed from current buffer"))
  24561. ((and (local-variable-p 'org-finish-function (current-buffer))
  24562. (fboundp org-finish-function))
  24563. (funcall org-finish-function))
  24564. ((org-at-property-p)
  24565. (call-interactively 'org-property-action))
  24566. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24567. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24568. ((org-at-table.el-p)
  24569. (require 'table)
  24570. (beginning-of-line 1)
  24571. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24572. (call-interactively 'table-recognize-table))
  24573. ((org-at-table-p)
  24574. (org-table-maybe-eval-formula)
  24575. (if arg
  24576. (call-interactively 'org-table-recalculate)
  24577. (org-table-maybe-recalculate-line))
  24578. (call-interactively 'org-table-align))
  24579. ((org-at-item-checkbox-p)
  24580. (call-interactively 'org-toggle-checkbox))
  24581. ((org-at-item-p)
  24582. (call-interactively 'org-maybe-renumber-ordered-list))
  24583. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24584. ;; Dynamic block
  24585. (beginning-of-line 1)
  24586. (org-update-dblock))
  24587. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24588. (cond
  24589. ((equal (match-string 1) "TBLFM")
  24590. ;; Recalculate the table before this line
  24591. (save-excursion
  24592. (beginning-of-line 1)
  24593. (skip-chars-backward " \r\n\t")
  24594. (if (org-at-table-p)
  24595. (org-call-with-arg 'org-table-recalculate t))))
  24596. (t
  24597. (call-interactively 'org-mode-restart))))
  24598. (t (error "C-c C-c can do nothing useful at this location.")))))
  24599. (defun org-mode-restart ()
  24600. "Restart Org-mode, to scan again for special lines.
  24601. Also updates the keyword regular expressions."
  24602. (interactive)
  24603. (let ((org-inhibit-startup t)) (org-mode))
  24604. (message "Org-mode restarted to refresh keyword and special line setup"))
  24605. (defun org-kill-note-or-show-branches ()
  24606. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  24607. (interactive)
  24608. (if (not org-finish-function)
  24609. (call-interactively 'show-branches)
  24610. (let ((org-note-abort t))
  24611. (funcall org-finish-function))))
  24612. (defun org-return (&optional indent)
  24613. "Goto next table row or insert a newline.
  24614. Calls `org-table-next-row' or `newline', depending on context.
  24615. See the individual commands for more information."
  24616. (interactive)
  24617. (cond
  24618. ((bobp) (if indent (newline-and-indent) (newline)))
  24619. ((org-at-table-p)
  24620. (org-table-justify-field-maybe)
  24621. (call-interactively 'org-table-next-row))
  24622. (t (if indent (newline-and-indent) (newline)))))
  24623. (defun org-return-indent ()
  24624. (interactive)
  24625. "Goto next table row or insert a newline and indent.
  24626. Calls `org-table-next-row' or `newline-and-indent', depending on
  24627. context. See the individual commands for more information."
  24628. (org-return t))
  24629. (defun org-ctrl-c-minus ()
  24630. "Insert separator line in table or modify bullet type in list.
  24631. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  24632. depending on context."
  24633. (interactive)
  24634. (cond
  24635. ((org-at-table-p)
  24636. (call-interactively 'org-table-insert-hline))
  24637. ((org-on-heading-p)
  24638. ;; Convert to item
  24639. (save-excursion
  24640. (beginning-of-line 1)
  24641. (if (looking-at "\\*+ ")
  24642. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  24643. ((org-in-item-p)
  24644. (call-interactively 'org-cycle-list-bullet))
  24645. (t (error "`C-c -' does have no function here."))))
  24646. (defun org-meta-return (&optional arg)
  24647. "Insert a new heading or wrap a region in a table.
  24648. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  24649. See the individual commands for more information."
  24650. (interactive "P")
  24651. (cond
  24652. ((org-at-table-p)
  24653. (call-interactively 'org-table-wrap-region))
  24654. (t (call-interactively 'org-insert-heading))))
  24655. ;;; Menu entries
  24656. ;; Define the Org-mode menus
  24657. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  24658. '("Tbl"
  24659. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  24660. ["Next Field" org-cycle (org-at-table-p)]
  24661. ["Previous Field" org-shifttab (org-at-table-p)]
  24662. ["Next Row" org-return (org-at-table-p)]
  24663. "--"
  24664. ["Blank Field" org-table-blank-field (org-at-table-p)]
  24665. ["Edit Field" org-table-edit-field (org-at-table-p)]
  24666. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  24667. "--"
  24668. ("Column"
  24669. ["Move Column Left" org-metaleft (org-at-table-p)]
  24670. ["Move Column Right" org-metaright (org-at-table-p)]
  24671. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  24672. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  24673. ("Row"
  24674. ["Move Row Up" org-metaup (org-at-table-p)]
  24675. ["Move Row Down" org-metadown (org-at-table-p)]
  24676. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  24677. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  24678. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  24679. "--"
  24680. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  24681. ("Rectangle"
  24682. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  24683. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  24684. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  24685. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  24686. "--"
  24687. ("Calculate"
  24688. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  24689. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  24690. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  24691. "--"
  24692. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  24693. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  24694. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  24695. "--"
  24696. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  24697. "--"
  24698. ["Sum Column/Rectangle" org-table-sum
  24699. (or (org-at-table-p) (org-region-active-p))]
  24700. ["Which Column?" org-table-current-column (org-at-table-p)])
  24701. ["Debug Formulas"
  24702. org-table-toggle-formula-debugger
  24703. :style toggle :selected org-table-formula-debug]
  24704. ["Show Col/Row Numbers"
  24705. org-table-toggle-coordinate-overlays
  24706. :style toggle :selected org-table-overlay-coordinates]
  24707. "--"
  24708. ["Create" org-table-create (and (not (org-at-table-p))
  24709. org-enable-table-editor)]
  24710. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  24711. ["Import from File" org-table-import (not (org-at-table-p))]
  24712. ["Export to File" org-table-export (org-at-table-p)]
  24713. "--"
  24714. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  24715. (easy-menu-define org-org-menu org-mode-map "Org menu"
  24716. '("Org"
  24717. ("Show/Hide"
  24718. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  24719. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  24720. ["Sparse Tree" org-occur t]
  24721. ["Reveal Context" org-reveal t]
  24722. ["Show All" show-all t]
  24723. "--"
  24724. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  24725. "--"
  24726. ["New Heading" org-insert-heading t]
  24727. ("Navigate Headings"
  24728. ["Up" outline-up-heading t]
  24729. ["Next" outline-next-visible-heading t]
  24730. ["Previous" outline-previous-visible-heading t]
  24731. ["Next Same Level" outline-forward-same-level t]
  24732. ["Previous Same Level" outline-backward-same-level t]
  24733. "--"
  24734. ["Jump" org-goto t])
  24735. ("Edit Structure"
  24736. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  24737. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  24738. "--"
  24739. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  24740. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  24741. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  24742. "--"
  24743. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  24744. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  24745. ["Demote Heading" org-metaright (not (org-at-table-p))]
  24746. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  24747. "--"
  24748. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  24749. "--"
  24750. ["Convert to odd levels" org-convert-to-odd-levels t]
  24751. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  24752. ("Editing"
  24753. ["Emphasis..." org-emphasize t])
  24754. ("Archive"
  24755. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  24756. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  24757. ; :active t :keys "C-u C-c C-x C-a"]
  24758. ["Sparse trees open ARCHIVE trees"
  24759. (setq org-sparse-tree-open-archived-trees
  24760. (not org-sparse-tree-open-archived-trees))
  24761. :style toggle :selected org-sparse-tree-open-archived-trees]
  24762. ["Cycling opens ARCHIVE trees"
  24763. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  24764. :style toggle :selected org-cycle-open-archived-trees]
  24765. ["Agenda includes ARCHIVE trees"
  24766. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  24767. :style toggle :selected (not org-agenda-skip-archived-trees)]
  24768. "--"
  24769. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  24770. ; ["Check and Move Children" (org-archive-subtree '(4))
  24771. ; :active t :keys "C-u C-c C-x C-s"]
  24772. )
  24773. "--"
  24774. ("TODO Lists"
  24775. ["TODO/DONE/-" org-todo t]
  24776. ("Select keyword"
  24777. ["Next keyword" org-shiftright (org-on-heading-p)]
  24778. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  24779. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  24780. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  24781. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  24782. ["Show TODO Tree" org-show-todo-tree t]
  24783. ["Global TODO list" org-todo-list t]
  24784. "--"
  24785. ["Set Priority" org-priority t]
  24786. ["Priority Up" org-shiftup t]
  24787. ["Priority Down" org-shiftdown t])
  24788. ("TAGS and Properties"
  24789. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  24790. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  24791. "--"
  24792. ["Set property" 'org-set-property t]
  24793. ["Column view of properties" org-columns t]
  24794. ["Insert Column View DBlock" org-insert-columns-dblock t])
  24795. ("Dates and Scheduling"
  24796. ["Timestamp" org-time-stamp t]
  24797. ["Timestamp (inactive)" org-time-stamp-inactive t]
  24798. ("Change Date"
  24799. ["1 Day Later" org-shiftright t]
  24800. ["1 Day Earlier" org-shiftleft t]
  24801. ["1 ... Later" org-shiftup t]
  24802. ["1 ... Earlier" org-shiftdown t])
  24803. ["Compute Time Range" org-evaluate-time-range t]
  24804. ["Schedule Item" org-schedule t]
  24805. ["Deadline" org-deadline t]
  24806. "--"
  24807. ["Custom time format" org-toggle-time-stamp-overlays
  24808. :style radio :selected org-display-custom-times]
  24809. "--"
  24810. ["Goto Calendar" org-goto-calendar t]
  24811. ["Date from Calendar" org-date-from-calendar t])
  24812. ("Logging work"
  24813. ["Clock in" org-clock-in t]
  24814. ["Clock out" org-clock-out t]
  24815. ["Clock cancel" org-clock-cancel t]
  24816. ["Goto running clock" org-clock-goto t]
  24817. ["Display times" org-clock-display t]
  24818. ["Create clock table" org-clock-report t]
  24819. "--"
  24820. ["Record DONE time"
  24821. (progn (setq org-log-done (not org-log-done))
  24822. (message "Switching to %s will %s record a timestamp"
  24823. (car org-done-keywords)
  24824. (if org-log-done "automatically" "not")))
  24825. :style toggle :selected org-log-done])
  24826. "--"
  24827. ["Agenda Command..." org-agenda t]
  24828. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  24829. ("File List for Agenda")
  24830. ("Special views current file"
  24831. ["TODO Tree" org-show-todo-tree t]
  24832. ["Check Deadlines" org-check-deadlines t]
  24833. ["Timeline" org-timeline t]
  24834. ["Tags Tree" org-tags-sparse-tree t])
  24835. "--"
  24836. ("Hyperlinks"
  24837. ["Store Link (Global)" org-store-link t]
  24838. ["Insert Link" org-insert-link t]
  24839. ["Follow Link" org-open-at-point t]
  24840. "--"
  24841. ["Next link" org-next-link t]
  24842. ["Previous link" org-previous-link t]
  24843. "--"
  24844. ["Descriptive Links"
  24845. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  24846. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  24847. ["Literal Links"
  24848. (progn
  24849. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  24850. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  24851. "--"
  24852. ["Export/Publish..." org-export t]
  24853. ("LaTeX"
  24854. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  24855. :selected org-cdlatex-mode]
  24856. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  24857. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  24858. ["Modify math symbol" org-cdlatex-math-modify
  24859. (org-inside-LaTeX-fragment-p)]
  24860. ["Export LaTeX fragments as images"
  24861. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  24862. :style toggle :selected org-export-with-LaTeX-fragments])
  24863. "--"
  24864. ("Documentation"
  24865. ["Show Version" org-version t]
  24866. ["Info Documentation" org-info t])
  24867. ("Customize"
  24868. ["Browse Org Group" org-customize t]
  24869. "--"
  24870. ["Expand This Menu" org-create-customize-menu
  24871. (fboundp 'customize-menu-create)])
  24872. "--"
  24873. ["Refresh setup" org-mode-restart t]
  24874. ))
  24875. (defun org-info (&optional node)
  24876. "Read documentation for Org-mode in the info system.
  24877. With optional NODE, go directly to that node."
  24878. (interactive)
  24879. (require 'info)
  24880. (Info-goto-node (format "(org)%s" (or node ""))))
  24881. (defun org-install-agenda-files-menu ()
  24882. (let ((bl (buffer-list)))
  24883. (save-excursion
  24884. (while bl
  24885. (set-buffer (pop bl))
  24886. (if (org-mode-p) (setq bl nil)))
  24887. (when (org-mode-p)
  24888. (easy-menu-change
  24889. '("Org") "File List for Agenda"
  24890. (append
  24891. (list
  24892. ["Edit File List" (org-edit-agenda-file-list) t]
  24893. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  24894. ["Remove Current File from List" org-remove-file t]
  24895. ["Cycle through agenda files" org-cycle-agenda-files t]
  24896. ["Occur in all agenda files" org-occur-in-agenda-files t]
  24897. "--")
  24898. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  24899. ;;;; Documentation
  24900. (defun org-customize ()
  24901. "Call the customize function with org as argument."
  24902. (interactive)
  24903. (customize-browse 'org))
  24904. (defun org-create-customize-menu ()
  24905. "Create a full customization menu for Org-mode, insert it into the menu."
  24906. (interactive)
  24907. (if (fboundp 'customize-menu-create)
  24908. (progn
  24909. (easy-menu-change
  24910. '("Org") "Customize"
  24911. `(["Browse Org group" org-customize t]
  24912. "--"
  24913. ,(customize-menu-create 'org)
  24914. ["Set" Custom-set t]
  24915. ["Save" Custom-save t]
  24916. ["Reset to Current" Custom-reset-current t]
  24917. ["Reset to Saved" Custom-reset-saved t]
  24918. ["Reset to Standard Settings" Custom-reset-standard t]))
  24919. (message "\"Org\"-menu now contains full customization menu"))
  24920. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  24921. ;;;; Miscellaneous stuff
  24922. ;;; Generally useful functions
  24923. (defun org-context ()
  24924. "Return a list of contexts of the current cursor position.
  24925. If several contexts apply, all are returned.
  24926. Each context entry is a list with a symbol naming the context, and
  24927. two positions indicating start and end of the context. Possible
  24928. contexts are:
  24929. :headline anywhere in a headline
  24930. :headline-stars on the leading stars in a headline
  24931. :todo-keyword on a TODO keyword (including DONE) in a headline
  24932. :tags on the TAGS in a headline
  24933. :priority on the priority cookie in a headline
  24934. :item on the first line of a plain list item
  24935. :item-bullet on the bullet/number of a plain list item
  24936. :checkbox on the checkbox in a plain list item
  24937. :table in an org-mode table
  24938. :table-special on a special filed in a table
  24939. :table-table in a table.el table
  24940. :link on a hyperlink
  24941. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  24942. :target on a <<target>>
  24943. :radio-target on a <<<radio-target>>>
  24944. :latex-fragment on a LaTeX fragment
  24945. :latex-preview on a LaTeX fragment with overlayed preview image
  24946. This function expects the position to be visible because it uses font-lock
  24947. faces as a help to recognize the following contexts: :table-special, :link,
  24948. and :keyword."
  24949. (let* ((f (get-text-property (point) 'face))
  24950. (faces (if (listp f) f (list f)))
  24951. (p (point)) clist o)
  24952. ;; First the large context
  24953. (cond
  24954. ((org-on-heading-p t)
  24955. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  24956. (when (progn
  24957. (beginning-of-line 1)
  24958. (looking-at org-todo-line-tags-regexp))
  24959. (push (org-point-in-group p 1 :headline-stars) clist)
  24960. (push (org-point-in-group p 2 :todo-keyword) clist)
  24961. (push (org-point-in-group p 4 :tags) clist))
  24962. (goto-char p)
  24963. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  24964. (if (looking-at "\\[#[A-Z0-9]\\]")
  24965. (push (org-point-in-group p 0 :priority) clist)))
  24966. ((org-at-item-p)
  24967. (push (org-point-in-group p 2 :item-bullet) clist)
  24968. (push (list :item (point-at-bol)
  24969. (save-excursion (org-end-of-item) (point)))
  24970. clist)
  24971. (and (org-at-item-checkbox-p)
  24972. (push (org-point-in-group p 0 :checkbox) clist)))
  24973. ((org-at-table-p)
  24974. (push (list :table (org-table-begin) (org-table-end)) clist)
  24975. (if (memq 'org-formula faces)
  24976. (push (list :table-special
  24977. (previous-single-property-change p 'face)
  24978. (next-single-property-change p 'face)) clist)))
  24979. ((org-at-table-p 'any)
  24980. (push (list :table-table) clist)))
  24981. (goto-char p)
  24982. ;; Now the small context
  24983. (cond
  24984. ((org-at-timestamp-p)
  24985. (push (org-point-in-group p 0 :timestamp) clist))
  24986. ((memq 'org-link faces)
  24987. (push (list :link
  24988. (previous-single-property-change p 'face)
  24989. (next-single-property-change p 'face)) clist))
  24990. ((memq 'org-special-keyword faces)
  24991. (push (list :keyword
  24992. (previous-single-property-change p 'face)
  24993. (next-single-property-change p 'face)) clist))
  24994. ((org-on-target-p)
  24995. (push (org-point-in-group p 0 :target) clist)
  24996. (goto-char (1- (match-beginning 0)))
  24997. (if (looking-at org-radio-target-regexp)
  24998. (push (org-point-in-group p 0 :radio-target) clist))
  24999. (goto-char p))
  25000. ((setq o (car (delq nil
  25001. (mapcar
  25002. (lambda (x)
  25003. (if (memq x org-latex-fragment-image-overlays) x))
  25004. (org-overlays-at (point))))))
  25005. (push (list :latex-fragment
  25006. (org-overlay-start o) (org-overlay-end o)) clist)
  25007. (push (list :latex-preview
  25008. (org-overlay-start o) (org-overlay-end o)) clist))
  25009. ((org-inside-LaTeX-fragment-p)
  25010. ;; FIXME: positions wrong.
  25011. (push (list :latex-fragment (point) (point)) clist)))
  25012. (setq clist (nreverse (delq nil clist)))
  25013. clist))
  25014. ;; FIXME: Compare with at-regexp-p Do we need both?
  25015. (defun org-in-regexp (re &optional nlines visually)
  25016. "Check if point is inside a match of regexp.
  25017. Normally only the current line is checked, but you can include NLINES extra
  25018. lines both before and after point into the search.
  25019. If VISUALLY is set, require that the cursor is not after the match but
  25020. really on, so that the block visually is on the match."
  25021. (catch 'exit
  25022. (let ((pos (point))
  25023. (eol (point-at-eol (+ 1 (or nlines 0))))
  25024. (inc (if visually 1 0)))
  25025. (save-excursion
  25026. (beginning-of-line (- 1 (or nlines 0)))
  25027. (while (re-search-forward re eol t)
  25028. (if (and (<= (match-beginning 0) pos)
  25029. (>= (+ inc (match-end 0)) pos))
  25030. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25031. (defun org-at-regexp-p (regexp)
  25032. "Is point inside a match of REGEXP in the current line?"
  25033. (catch 'exit
  25034. (save-excursion
  25035. (let ((pos (point)) (end (point-at-eol)))
  25036. (beginning-of-line 1)
  25037. (while (re-search-forward regexp end t)
  25038. (if (and (<= (match-beginning 0) pos)
  25039. (>= (match-end 0) pos))
  25040. (throw 'exit t)))
  25041. nil))))
  25042. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25043. "Call `multi-occur' with buffers for all agenda files."
  25044. (interactive "sOrg-files matching: \np")
  25045. (let* ((files (org-agenda-files))
  25046. (tnames (mapcar 'file-truename files))
  25047. (extra org-agenda-multi-occur-extra-files)
  25048. f)
  25049. (while (setq f (pop extra))
  25050. (unless (member (file-truename f) tnames)
  25051. (add-to-list 'files f 'append)
  25052. (add-to-list 'tnames (file-truename f) 'append)))
  25053. (multi-occur
  25054. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25055. regexp)))
  25056. (defun org-uniquify (list)
  25057. "Remove duplicate elements from LIST."
  25058. (let (res)
  25059. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25060. res))
  25061. (defun org-delete-all (elts list)
  25062. "Remove all elements in ELTS from LIST."
  25063. (while elts
  25064. (setq list (delete (pop elts) list)))
  25065. list)
  25066. (defun org-back-over-empty-lines ()
  25067. "Move backwards over witespace, to the beginning of the first empty line.
  25068. Returns the number o empty lines passed."
  25069. (let ((pos (point)))
  25070. (skip-chars-backward " \t\n\r")
  25071. (beginning-of-line 2)
  25072. (count-lines (point) pos)))
  25073. (defun org-skip-whitespace ()
  25074. (skip-chars-forward " \t\n\r"))
  25075. (defun org-point-in-group (point group &optional context)
  25076. "Check if POINT is in match-group GROUP.
  25077. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25078. match. If the match group does ot exist or point is not inside it,
  25079. return nil."
  25080. (and (match-beginning group)
  25081. (>= point (match-beginning group))
  25082. (<= point (match-end group))
  25083. (if context
  25084. (list context (match-beginning group) (match-end group))
  25085. t)))
  25086. (defun org-switch-to-buffer-other-window (&rest args)
  25087. "Switch to buffer in a second window on the current frame.
  25088. In particular, do not allow pop-up frames."
  25089. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25090. special-display-function)
  25091. (apply 'switch-to-buffer-other-window args)))
  25092. (defun org-combine-plists (&rest plists)
  25093. "Create a single property list from all plists in PLISTS.
  25094. The process starts by copying the first list, and then setting properties
  25095. from the other lists. Settings in the last list are the most significant
  25096. ones and overrule settings in the other lists."
  25097. (let ((rtn (copy-sequence (pop plists)))
  25098. p v ls)
  25099. (while plists
  25100. (setq ls (pop plists))
  25101. (while ls
  25102. (setq p (pop ls) v (pop ls))
  25103. (setq rtn (plist-put rtn p v))))
  25104. rtn))
  25105. (defun org-move-line-down (arg)
  25106. "Move the current line down. With prefix argument, move it past ARG lines."
  25107. (interactive "p")
  25108. (let ((col (current-column))
  25109. beg end pos)
  25110. (beginning-of-line 1) (setq beg (point))
  25111. (beginning-of-line 2) (setq end (point))
  25112. (beginning-of-line (+ 1 arg))
  25113. (setq pos (move-marker (make-marker) (point)))
  25114. (insert (delete-and-extract-region beg end))
  25115. (goto-char pos)
  25116. (move-to-column col)))
  25117. (defun org-move-line-up (arg)
  25118. "Move the current line up. With prefix argument, move it past ARG lines."
  25119. (interactive "p")
  25120. (let ((col (current-column))
  25121. beg end pos)
  25122. (beginning-of-line 1) (setq beg (point))
  25123. (beginning-of-line 2) (setq end (point))
  25124. (beginning-of-line (- arg))
  25125. (setq pos (move-marker (make-marker) (point)))
  25126. (insert (delete-and-extract-region beg end))
  25127. (goto-char pos)
  25128. (move-to-column col)))
  25129. (defun org-replace-escapes (string table)
  25130. "Replace %-escapes in STRING with values in TABLE.
  25131. TABLE is an association list with keys like \"%a\" and string values.
  25132. The sequences in STRING may contain normal field width and padding information,
  25133. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25134. so values can contain further %-escapes if they are define later in TABLE."
  25135. (let ((case-fold-search nil)
  25136. e re rpl)
  25137. (while (setq e (pop table))
  25138. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25139. (while (string-match re string)
  25140. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25141. (cdr e)))
  25142. (setq string (replace-match rpl t t string))))
  25143. string))
  25144. (defun org-sublist (list start end)
  25145. "Return a section of LIST, from START to END.
  25146. Counting starts at 1."
  25147. (let (rtn (c start))
  25148. (setq list (nthcdr (1- start) list))
  25149. (while (and list (<= c end))
  25150. (push (pop list) rtn)
  25151. (setq c (1+ c)))
  25152. (nreverse rtn)))
  25153. (defun org-find-base-buffer-visiting (file)
  25154. "Like `find-buffer-visiting' but alway return the base buffer and
  25155. not an indirect buffer"
  25156. (let ((buf (find-buffer-visiting file)))
  25157. (if buf
  25158. (or (buffer-base-buffer buf) buf)
  25159. nil)))
  25160. (defun org-image-file-name-regexp ()
  25161. "Return regexp matching the file names of images."
  25162. (if (fboundp 'image-file-name-regexp)
  25163. (image-file-name-regexp)
  25164. (let ((image-file-name-extensions
  25165. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25166. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25167. (concat "\\."
  25168. (regexp-opt (nconc (mapcar 'upcase
  25169. image-file-name-extensions)
  25170. image-file-name-extensions)
  25171. t)
  25172. "\\'"))))
  25173. (defun org-file-image-p (file)
  25174. "Return non-nil if FILE is an image."
  25175. (save-match-data
  25176. (string-match (org-image-file-name-regexp) file)))
  25177. ;;; Paragraph filling stuff.
  25178. ;; We want this to be just right, so use the full arsenal.
  25179. (defun org-indent-line-function ()
  25180. "Indent line like previous, but further if previous was headline or item."
  25181. (interactive)
  25182. (let* ((pos (point))
  25183. (itemp (org-at-item-p))
  25184. column bpos bcol tpos tcol bullet btype bullet-type)
  25185. ;; Find the previous relevant line
  25186. (beginning-of-line 1)
  25187. (cond
  25188. ((looking-at "#") (setq column 0))
  25189. ((looking-at "\\*+ ") (setq column 0))
  25190. (t
  25191. (beginning-of-line 0)
  25192. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25193. (beginning-of-line 0))
  25194. (cond
  25195. ((looking-at "\\*+[ \t]+")
  25196. (goto-char (match-end 0))
  25197. (setq column (current-column)))
  25198. ((org-in-item-p)
  25199. (org-beginning-of-item)
  25200. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25201. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25202. (setq bpos (match-beginning 1) tpos (match-end 0)
  25203. bcol (progn (goto-char bpos) (current-column))
  25204. tcol (progn (goto-char tpos) (current-column))
  25205. bullet (match-string 1)
  25206. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25207. (if (not itemp)
  25208. (setq column tcol)
  25209. (goto-char pos)
  25210. (beginning-of-line 1)
  25211. (if (looking-at "\\S-")
  25212. (progn
  25213. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25214. (setq bullet (match-string 1)
  25215. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25216. (setq column (if (equal btype bullet-type) bcol tcol)))
  25217. (setq column (org-get-indentation)))))
  25218. (t (setq column (org-get-indentation))))))
  25219. (goto-char pos)
  25220. (if (<= (current-column) (current-indentation))
  25221. (indent-line-to column)
  25222. (save-excursion (indent-line-to column)))
  25223. (setq column (current-column))
  25224. (beginning-of-line 1)
  25225. (if (looking-at
  25226. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25227. (replace-match (concat "\\1" (format org-property-format
  25228. (match-string 2) (match-string 3)))
  25229. t nil))
  25230. (move-to-column column)))
  25231. (defun org-set-autofill-regexps ()
  25232. (interactive)
  25233. ;; In the paragraph separator we include headlines, because filling
  25234. ;; text in a line directly attached to a headline would otherwise
  25235. ;; fill the headline as well.
  25236. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25237. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25238. ;; The paragraph starter includes hand-formatted lists.
  25239. (org-set-local 'paragraph-start
  25240. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25241. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25242. ;; But only if the user has not turned off tables or fixed-width regions
  25243. (org-set-local
  25244. 'auto-fill-inhibit-regexp
  25245. (concat "\\*+ \\|#\\+"
  25246. "\\|[ \t]*" org-keyword-time-regexp
  25247. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25248. (concat
  25249. "\\|[ \t]*["
  25250. (if org-enable-table-editor "|" "")
  25251. (if org-enable-fixed-width-editor ":" "")
  25252. "]"))))
  25253. ;; We use our own fill-paragraph function, to make sure that tables
  25254. ;; and fixed-width regions are not wrapped. That function will pass
  25255. ;; through to `fill-paragraph' when appropriate.
  25256. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25257. ; Adaptive filling: To get full control, first make sure that
  25258. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25259. (org-set-local 'adaptive-fill-regexp "\000")
  25260. (org-set-local 'adaptive-fill-function
  25261. 'org-adaptive-fill-function))
  25262. (defun org-fill-paragraph (&optional justify)
  25263. "Re-align a table, pass through to fill-paragraph if no table."
  25264. (let ((table-p (org-at-table-p))
  25265. (table.el-p (org-at-table.el-p)))
  25266. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25267. (save-excursion (goto-char (point-at-bol))
  25268. (looking-at outline-regexp)))
  25269. t) ; skip headlines
  25270. (table.el-p t) ; skip table.el tables
  25271. (table-p (org-table-align) t) ; align org-mode tables
  25272. (t nil)))) ; call paragraph-fill
  25273. ;; For reference, this is the default value of adaptive-fill-regexp
  25274. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25275. (defun org-adaptive-fill-function ()
  25276. "Return a fill prefix for org-mode files.
  25277. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25278. work correctly."
  25279. (cond ((looking-at "#[ \t]+")
  25280. (match-string 0))
  25281. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25282. (save-excursion
  25283. (goto-char (match-end 0))
  25284. (make-string (current-column) ?\ )))
  25285. (t nil)))
  25286. ;;;; Functions extending outline functionality
  25287. (defun org-beginning-of-line (&optional arg)
  25288. "Go to the beginning of the current line. If that is invisible, continue
  25289. to a visible line beginning. This makes the function of C-a more intuitive.
  25290. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25291. first attempt, and only move to after the tags when the cursor is already
  25292. beyond the end of the headline."
  25293. (interactive "P")
  25294. (let ((pos (point)))
  25295. (beginning-of-line 1)
  25296. (if (bobp)
  25297. nil
  25298. (backward-char 1)
  25299. (if (org-invisible-p)
  25300. (while (and (not (bobp)) (org-invisible-p))
  25301. (backward-char 1)
  25302. (beginning-of-line 1))
  25303. (forward-char 1)))
  25304. (when org-special-ctrl-a/e
  25305. (cond
  25306. ((and (looking-at org-todo-line-regexp)
  25307. (= (char-after (match-end 1)) ?\ ))
  25308. (goto-char
  25309. (if (eq org-special-ctrl-a/e t)
  25310. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25311. ((= pos (point)) (match-beginning 3))
  25312. (t (point)))
  25313. (cond ((> pos (point)) (point))
  25314. ((not (eq last-command this-command)) (point))
  25315. (t (match-beginning 3))))))
  25316. ((org-at-item-p)
  25317. (goto-char
  25318. (if (eq org-special-ctrl-a/e t)
  25319. (cond ((> pos (match-end 4)) (match-end 4))
  25320. ((= pos (point)) (match-end 4))
  25321. (t (point)))
  25322. (cond ((> pos (point)) (point))
  25323. ((not (eq last-command this-command)) (point))
  25324. (t (match-end 4))))))))))
  25325. (defun org-end-of-line (&optional arg)
  25326. "Go to the end of the line.
  25327. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25328. first attempt, and only move to after the tags when the cursor is already
  25329. beyond the end of the headline."
  25330. (interactive "P")
  25331. (if (or (not org-special-ctrl-a/e)
  25332. (not (org-on-heading-p)))
  25333. (end-of-line arg)
  25334. (let ((pos (point)))
  25335. (beginning-of-line 1)
  25336. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25337. (if (eq org-special-ctrl-a/e t)
  25338. (if (or (< pos (match-beginning 1))
  25339. (= pos (match-end 0)))
  25340. (goto-char (match-beginning 1))
  25341. (goto-char (match-end 0)))
  25342. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25343. (goto-char (match-end 0))
  25344. (goto-char (match-beginning 1))))
  25345. (end-of-line arg)))))
  25346. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25347. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25348. (defun org-invisible-p ()
  25349. "Check if point is at a character currently not visible."
  25350. ;; Early versions of noutline don't have `outline-invisible-p'.
  25351. (if (fboundp 'outline-invisible-p)
  25352. (outline-invisible-p)
  25353. (get-char-property (point) 'invisible)))
  25354. (defun org-invisible-p2 ()
  25355. "Check if point is at a character currently not visible."
  25356. (save-excursion
  25357. (if (and (eolp) (not (bobp))) (backward-char 1))
  25358. ;; Early versions of noutline don't have `outline-invisible-p'.
  25359. (if (fboundp 'outline-invisible-p)
  25360. (outline-invisible-p)
  25361. (get-char-property (point) 'invisible))))
  25362. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25363. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25364. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25365. (defun org-at-heading-or-item-p ()
  25366. (or (org-on-heading-p) (org-at-item-p)))
  25367. (defun org-on-target-p ()
  25368. (or (org-in-regexp org-radio-target-regexp)
  25369. (org-in-regexp org-target-regexp)))
  25370. (defun org-up-heading-all (arg)
  25371. "Move to the heading line of which the present line is a subheading.
  25372. This function considers both visible and invisible heading lines.
  25373. With argument, move up ARG levels."
  25374. (if (fboundp 'outline-up-heading-all)
  25375. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25376. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25377. (defun org-up-heading-safe ()
  25378. "Move to the heading line of which the present line is a subheading.
  25379. This version will not throw an error. It will return the level of the
  25380. headline found, or nil if no higher level is found."
  25381. (let ((pos (point)) start-level level
  25382. (re (concat "^" outline-regexp)))
  25383. (catch 'exit
  25384. (outline-back-to-heading t)
  25385. (setq start-level (funcall outline-level))
  25386. (if (equal start-level 1) (throw 'exit nil))
  25387. (while (re-search-backward re nil t)
  25388. (setq level (funcall outline-level))
  25389. (if (< level start-level) (throw 'exit level)))
  25390. nil)))
  25391. (defun org-first-sibling-p ()
  25392. "Is this heading the first child of its parents?"
  25393. (interactive)
  25394. (let ((re (concat "^" outline-regexp))
  25395. level l)
  25396. (unless (org-at-heading-p t)
  25397. (error "Not at a heading"))
  25398. (setq level (funcall outline-level))
  25399. (save-excursion
  25400. (if (not (re-search-backward re nil t))
  25401. t
  25402. (setq l (funcall outline-level))
  25403. (< l level)))))
  25404. (defun org-goto-sibling (&optional previous)
  25405. "Goto the next sibling, even if it is invisible.
  25406. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25407. when a sibling was found. When none is found, return nil and don't
  25408. move point."
  25409. (let ((fun (if previous 're-search-backward 're-search-forward))
  25410. (pos (point))
  25411. (re (concat "^" outline-regexp))
  25412. level l)
  25413. (when (condition-case nil (org-back-to-heading t) (error nil))
  25414. (setq level (funcall outline-level))
  25415. (catch 'exit
  25416. (or previous (forward-char 1))
  25417. (while (funcall fun re nil t)
  25418. (setq l (funcall outline-level))
  25419. (when (< l level) (goto-char pos) (throw 'exit nil))
  25420. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25421. (goto-char pos)
  25422. nil))))
  25423. (defun org-show-siblings ()
  25424. "Show all siblings of the current headline."
  25425. (save-excursion
  25426. (while (org-goto-sibling) (org-flag-heading nil)))
  25427. (save-excursion
  25428. (while (org-goto-sibling 'previous)
  25429. (org-flag-heading nil))))
  25430. (defun org-show-hidden-entry ()
  25431. "Show an entry where even the heading is hidden."
  25432. (save-excursion
  25433. (org-show-entry)))
  25434. (defun org-flag-heading (flag &optional entry)
  25435. "Flag the current heading. FLAG non-nil means make invisible.
  25436. When ENTRY is non-nil, show the entire entry."
  25437. (save-excursion
  25438. (org-back-to-heading t)
  25439. ;; Check if we should show the entire entry
  25440. (if entry
  25441. (progn
  25442. (org-show-entry)
  25443. (save-excursion
  25444. (and (outline-next-heading)
  25445. (org-flag-heading nil))))
  25446. (outline-flag-region (max (point-min) (1- (point)))
  25447. (save-excursion (outline-end-of-heading) (point))
  25448. flag))))
  25449. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25450. ;; This is an exact copy of the original function, but it uses
  25451. ;; `org-back-to-heading', to make it work also in invisible
  25452. ;; trees. And is uses an invisible-OK argument.
  25453. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25454. (org-back-to-heading invisible-OK)
  25455. (let ((first t)
  25456. (level (funcall outline-level)))
  25457. (while (and (not (eobp))
  25458. (or first (> (funcall outline-level) level)))
  25459. (setq first nil)
  25460. (outline-next-heading))
  25461. (unless to-heading
  25462. (if (memq (preceding-char) '(?\n ?\^M))
  25463. (progn
  25464. ;; Go to end of line before heading
  25465. (forward-char -1)
  25466. (if (memq (preceding-char) '(?\n ?\^M))
  25467. ;; leave blank line before heading
  25468. (forward-char -1))))))
  25469. (point))
  25470. (defun org-show-subtree ()
  25471. "Show everything after this heading at deeper levels."
  25472. (outline-flag-region
  25473. (point)
  25474. (save-excursion
  25475. (outline-end-of-subtree) (outline-next-heading) (point))
  25476. nil))
  25477. (defun org-show-entry ()
  25478. "Show the body directly following this heading.
  25479. Show the heading too, if it is currently invisible."
  25480. (interactive)
  25481. (save-excursion
  25482. (condition-case nil
  25483. (progn
  25484. (org-back-to-heading t)
  25485. (outline-flag-region
  25486. (max (point-min) (1- (point)))
  25487. (save-excursion
  25488. (re-search-forward
  25489. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25490. (or (match-beginning 1) (point-max)))
  25491. nil))
  25492. (error nil))))
  25493. (defun org-make-options-regexp (kwds)
  25494. "Make a regular expression for keyword lines."
  25495. (concat
  25496. "^"
  25497. "#?[ \t]*\\+\\("
  25498. (mapconcat 'regexp-quote kwds "\\|")
  25499. "\\):[ \t]*"
  25500. "\\(.+\\)"))
  25501. ;; Make isearch reveal the necessary context
  25502. (defun org-isearch-end ()
  25503. "Reveal context after isearch exits."
  25504. (when isearch-success ; only if search was successful
  25505. (if (featurep 'xemacs)
  25506. ;; Under XEmacs, the hook is run in the correct place,
  25507. ;; we directly show the context.
  25508. (org-show-context 'isearch)
  25509. ;; In Emacs the hook runs *before* restoring the overlays.
  25510. ;; So we have to use a one-time post-command-hook to do this.
  25511. ;; (Emacs 22 has a special variable, see function `org-mode')
  25512. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25513. isearch-mode-end-hook-quit)
  25514. ;; Only when the isearch was not quitted.
  25515. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25516. 'append 'local)))))
  25517. (defun org-isearch-post-command ()
  25518. "Remove self from hook, and show context."
  25519. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25520. (org-show-context 'isearch))
  25521. ;;;; Integration with and fixes for other packages
  25522. ;;; Imenu support
  25523. (defvar org-imenu-markers nil
  25524. "All markers currently used by Imenu.")
  25525. (make-variable-buffer-local 'org-imenu-markers)
  25526. (defun org-imenu-new-marker (&optional pos)
  25527. "Return a new marker for use by Imenu, and remember the marker."
  25528. (let ((m (make-marker)))
  25529. (move-marker m (or pos (point)))
  25530. (push m org-imenu-markers)
  25531. m))
  25532. (defun org-imenu-get-tree ()
  25533. "Produce the index for Imenu."
  25534. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25535. (setq org-imenu-markers nil)
  25536. (let* ((n org-imenu-depth)
  25537. (re (concat "^" outline-regexp))
  25538. (subs (make-vector (1+ n) nil))
  25539. (last-level 0)
  25540. m tree level head)
  25541. (save-excursion
  25542. (save-restriction
  25543. (widen)
  25544. (goto-char (point-max))
  25545. (while (re-search-backward re nil t)
  25546. (setq level (org-reduced-level (funcall outline-level)))
  25547. (when (<= level n)
  25548. (looking-at org-complex-heading-regexp)
  25549. (setq head (org-match-string-no-properties 4)
  25550. m (org-imenu-new-marker))
  25551. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25552. (if (>= level last-level)
  25553. (push (cons head m) (aref subs level))
  25554. (push (cons head (aref subs (1+ level))) (aref subs level))
  25555. (loop for i from (1+ level) to n do (aset subs i nil)))
  25556. (setq last-level level)))))
  25557. (aref subs 1)))
  25558. (eval-after-load "imenu"
  25559. '(progn
  25560. (add-hook 'imenu-after-jump-hook
  25561. (lambda () (org-show-context 'org-goto)))))
  25562. ;; Speedbar support
  25563. (defun org-speedbar-set-agenda-restriction ()
  25564. "Restrict future agenda commands to the location at point in speedbar.
  25565. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  25566. (interactive)
  25567. (let (p m tp np dir txt w)
  25568. (cond
  25569. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25570. 'org-imenu t))
  25571. (setq m (get-text-property p 'org-imenu-marker))
  25572. (save-excursion
  25573. (save-restriction
  25574. (set-buffer (marker-buffer m))
  25575. (goto-char m)
  25576. (org-agenda-set-restriction-lock 'subtree))))
  25577. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25578. 'speedbar-function 'speedbar-find-file))
  25579. (setq tp (previous-single-property-change
  25580. (1+ p) 'speedbar-function)
  25581. np (next-single-property-change
  25582. tp 'speedbar-function)
  25583. dir (speedbar-line-directory)
  25584. txt (buffer-substring-no-properties (or tp (point-min))
  25585. (or np (point-max))))
  25586. (save-excursion
  25587. (save-restriction
  25588. (set-buffer (find-file-noselect
  25589. (let ((default-directory dir))
  25590. (expand-file-name txt))))
  25591. (unless (org-mode-p)
  25592. (error "Cannot restrict to non-Org-mode file"))
  25593. (org-agenda-set-restriction-lock 'file))))
  25594. (t (error "Don't know how to restrict Org-mode's agenda")))
  25595. (org-move-overlay org-speedbar-restriction-lock-overlay
  25596. (point-at-bol) (point-at-eol))
  25597. (setq current-prefix-arg nil)
  25598. (org-agenda-maybe-redo)))
  25599. (eval-after-load "speedbar"
  25600. '(progn
  25601. (speedbar-add-supported-extension ".org")
  25602. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  25603. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  25604. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  25605. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  25606. (add-hook 'speedbar-visiting-tag-hook
  25607. (lambda () (org-show-context 'org-goto)))))
  25608. ;;; Fixes and Hacks
  25609. ;; Make flyspell not check words in links, to not mess up our keymap
  25610. (defun org-mode-flyspell-verify ()
  25611. "Don't let flyspell put overlays at active buttons."
  25612. (not (get-text-property (point) 'keymap)))
  25613. ;; Make `bookmark-jump' show the jump location if it was hidden.
  25614. (eval-after-load "bookmark"
  25615. '(if (boundp 'bookmark-after-jump-hook)
  25616. ;; We can use the hook
  25617. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  25618. ;; Hook not available, use advice
  25619. (defadvice bookmark-jump (after org-make-visible activate)
  25620. "Make the position visible."
  25621. (org-bookmark-jump-unhide))))
  25622. (defun org-bookmark-jump-unhide ()
  25623. "Unhide the current position, to show the bookmark location."
  25624. (and (org-mode-p)
  25625. (or (org-invisible-p)
  25626. (save-excursion (goto-char (max (point-min) (1- (point))))
  25627. (org-invisible-p)))
  25628. (org-show-context 'bookmark-jump)))
  25629. ;; Fix a bug in htmlize where there are text properties (face nil)
  25630. (eval-after-load "htmlize"
  25631. '(progn
  25632. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  25633. "Make sure there are no nil faces"
  25634. (setq ad-return-value (delq nil ad-return-value)))))
  25635. ;; Make session.el ignore our circular variable
  25636. (eval-after-load "session"
  25637. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  25638. ;;;; Experimental code
  25639. (defun org-closed-in-range ()
  25640. "Sparse tree of items closed in a certain time range.
  25641. Still experimental, may disappear in the future."
  25642. (interactive)
  25643. ;; Get the time interval from the user.
  25644. (let* ((time1 (time-to-seconds
  25645. (org-read-date nil 'to-time nil "Starting date: ")))
  25646. (time2 (time-to-seconds
  25647. (org-read-date nil 'to-time nil "End date:")))
  25648. ;; callback function
  25649. (callback (lambda ()
  25650. (let ((time
  25651. (time-to-seconds
  25652. (apply 'encode-time
  25653. (org-parse-time-string
  25654. (match-string 1))))))
  25655. ;; check if time in interval
  25656. (and (>= time time1) (<= time time2))))))
  25657. ;; make tree, check each match with the callback
  25658. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  25659. ;;;; Finish up
  25660. (provide 'org)
  25661. (run-hooks 'org-load-hook)
  25662. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  25663. ;;; org.el ends here