org.el 1021 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694
  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 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.17
  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.17"
  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. %? After completing the template, position cursor here.
  1276. Apart from these general escapes, you can access information specific to the
  1277. link type that is created. For example, calling `remember' in emails or gnus
  1278. will record the author and the subject of the message, which you can access
  1279. with %:author and %:subject, respectively. Here is a complete list of what
  1280. is recorded for each link type.
  1281. Link type | Available information
  1282. -------------------+------------------------------------------------------
  1283. bbdb | %:type %:name %:company
  1284. vm, wl, mh, rmail | %:type %:subject %:message-id
  1285. | %:from %:fromname %:fromaddress
  1286. | %:to %:toname %:toaddress
  1287. | %:fromto (either \"to NAME\" or \"from NAME\")
  1288. gnus | %:group, for messages also all email fields
  1289. w3, w3m | %:type %:url
  1290. info | %:type %:file %:node
  1291. calendar | %:type %:date"
  1292. :group 'org-remember
  1293. :get (lambda (var) ; Make sure all entries have 5 elements
  1294. (mapcar (lambda (x)
  1295. (if (not (stringp (car x))) (setq x (cons "" x)))
  1296. (cond ((= (length x) 4) (append x '("")))
  1297. ((= (length x) 3) (append x '("" "")))
  1298. (t x)))
  1299. (default-value var)))
  1300. :type '(repeat
  1301. :tag "enabled"
  1302. (list :value ("" ?a "\n" nil nil)
  1303. (string :tag "Name")
  1304. (character :tag "Selection Key")
  1305. (string :tag "Template")
  1306. (choice
  1307. (file :tag "Destination file")
  1308. (const :tag "Prompt for file" nil))
  1309. (choice
  1310. (string :tag "Destination headline")
  1311. (const :tag "Selection interface for heading")))))
  1312. (defcustom org-reverse-note-order nil
  1313. "Non-nil means, store new notes at the beginning of a file or entry.
  1314. When nil, new notes will be filed to the end of a file or entry.
  1315. This can also be a list with cons cells of regular expressions that
  1316. are matched against file names, and values."
  1317. :group 'org-remember
  1318. :type '(choice
  1319. (const :tag "Reverse always" t)
  1320. (const :tag "Reverse never" nil)
  1321. (repeat :tag "By file name regexp"
  1322. (cons regexp boolean))))
  1323. (defcustom org-refile-targets '((nil . (:level . 1)))
  1324. "Targets for refiling entries with \\[org-refile].
  1325. This is list of cons cells. Each cell contains:
  1326. - a specification of the files to be considered, either a list of files,
  1327. or a symbol whose function or value fields will be used to retrieve
  1328. a file name or a list of file names. Nil means, refile to a different
  1329. heading in the current buffer.
  1330. - A specification of how to find candidate refile targets. This may be
  1331. any of
  1332. - a cons cell (:tag . \"TAG\") to identify refile targes by a tag.
  1333. This tag has to be present in all target headlines, inheritance will
  1334. not be considered.
  1335. - a cons cell (:todo . \"KEYWORD\" to identify refile targets by
  1336. todo keyword.
  1337. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1338. headlines that are refiling targets.
  1339. - a cons cell (:level . N). Any headline of level N is considered a target.
  1340. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1341. ;; FIXME: what if there are a var and func with same name???
  1342. :group 'org-remember
  1343. :type '(repeat
  1344. (cons
  1345. (choice :value org-agenda-files
  1346. (const :tag "All agenda files" org-agenda-files)
  1347. (const :tag "Current buffer" nil)
  1348. (function) (variable) (file))
  1349. (choice :tag "Identify target headline by"
  1350. (cons :tag "Specific tag" (const :tag) (string))
  1351. (cons :tag "TODO keyword" (const :todo) (string))
  1352. (cons :tag "Regular expression" (const :regexp) (regexp))
  1353. (cons :tag "Level number" (const :level) (integer))
  1354. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1355. (defcustom org-refile-use-outline-path nil
  1356. "Non-nil means, provide refile targets as paths.
  1357. So a level 3 headline will be available as level1/level2/level3."
  1358. :group 'org-remember
  1359. :type 'boolean)
  1360. (defgroup org-todo nil
  1361. "Options concerning TODO items in Org-mode."
  1362. :tag "Org TODO"
  1363. :group 'org)
  1364. (defgroup org-progress nil
  1365. "Options concerning Progress logging in Org-mode."
  1366. :tag "Org Progress"
  1367. :group 'org-time)
  1368. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1369. "List of TODO entry keyword sequences and their interpretation.
  1370. \\<org-mode-map>This is a list of sequences.
  1371. Each sequence starts with a symbol, either `sequence' or `type',
  1372. indicating if the keywords should be interpreted as a sequence of
  1373. action steps, or as different types of TODO items. The first
  1374. keywords are states requiring action - these states will select a headline
  1375. for inclusion into the global TODO list Org-mode produces. If one of
  1376. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1377. signify that no further action is necessary. If \"|\" is not found,
  1378. the last keyword is treated as the only DONE state of the sequence.
  1379. The command \\[org-todo] cycles an entry through these states, and one
  1380. additional state where no keyword is present. For details about this
  1381. cycling, see the manual.
  1382. TODO keywords and interpretation can also be set on a per-file basis with
  1383. the special #+SEQ_TODO and #+TYP_TODO lines.
  1384. For backward compatibility, this variable may also be just a list
  1385. of keywords - in this case the interptetation (sequence or type) will be
  1386. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1387. :group 'org-todo
  1388. :group 'org-keywords
  1389. :type '(choice
  1390. (repeat :tag "Old syntax, just keywords"
  1391. (string :tag "Keyword"))
  1392. (repeat :tag "New syntax"
  1393. (cons
  1394. (choice
  1395. :tag "Interpretation"
  1396. (const :tag "Sequence (cycling hits every state)" sequence)
  1397. (const :tag "Type (cycling directly to DONE)" type))
  1398. (repeat
  1399. (string :tag "Keyword"))))))
  1400. (defvar org-todo-keywords-1 nil)
  1401. (make-variable-buffer-local 'org-todo-keywords-1)
  1402. (defvar org-todo-keywords-for-agenda nil)
  1403. (defvar org-done-keywords-for-agenda nil)
  1404. (defvar org-not-done-keywords nil)
  1405. (make-variable-buffer-local 'org-not-done-keywords)
  1406. (defvar org-done-keywords nil)
  1407. (make-variable-buffer-local 'org-done-keywords)
  1408. (defvar org-todo-heads nil)
  1409. (make-variable-buffer-local 'org-todo-heads)
  1410. (defvar org-todo-sets nil)
  1411. (make-variable-buffer-local 'org-todo-sets)
  1412. (defvar org-todo-log-states nil)
  1413. (make-variable-buffer-local 'org-todo-log-states)
  1414. (defvar org-todo-kwd-alist nil)
  1415. (make-variable-buffer-local 'org-todo-kwd-alist)
  1416. (defvar org-todo-key-alist nil)
  1417. (make-variable-buffer-local 'org-todo-key-alist)
  1418. (defvar org-todo-key-trigger nil)
  1419. (make-variable-buffer-local 'org-todo-key-trigger)
  1420. (defcustom org-todo-interpretation 'sequence
  1421. "Controls how TODO keywords are interpreted.
  1422. This variable is in principle obsolete and is only used for
  1423. backward compatibility, if the interpretation of todo keywords is
  1424. not given already in `org-todo-keywords'. See that variable for
  1425. more information."
  1426. :group 'org-todo
  1427. :group 'org-keywords
  1428. :type '(choice (const sequence)
  1429. (const type)))
  1430. (defcustom org-use-fast-todo-selection 'prefix
  1431. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1432. This variable describes if and under what circumstances the cycling
  1433. mechanism for TODO keywords will be replaced by a single-key, direct
  1434. selection scheme.
  1435. When nil, fast selection is never used.
  1436. When the symbol `prefix', it will be used when `org-todo' is called with
  1437. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1438. in an agenda buffer.
  1439. When t, fast selection is used by default. In this case, the prefix
  1440. argument forces cycling instead.
  1441. In all cases, the special interface is only used if access keys have actually
  1442. been assigned by the user, i.e. if keywords in the configuration are followed
  1443. by a letter in parenthesis, like TODO(t)."
  1444. :group 'org-todo
  1445. :type '(choice
  1446. (const :tag "Never" nil)
  1447. (const :tag "By default" t)
  1448. (const :tag "Only with C-u C-c C-t" prefix)))
  1449. (defcustom org-after-todo-state-change-hook nil
  1450. "Hook which is run after the state of a TODO item was changed.
  1451. The new state (a string with a TODO keyword, or nil) is available in the
  1452. Lisp variable `state'."
  1453. :group 'org-todo
  1454. :type 'hook)
  1455. (defcustom org-log-done nil
  1456. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1457. When the state of an entry is changed from nothing or a DONE state to
  1458. a not-done TODO state, remove a previous closing date.
  1459. This can also be a list of symbols indicating under which conditions
  1460. the time stamp recording the action should be annotated with a short note.
  1461. Valid members of this list are
  1462. done Offer to record a note when marking entries done
  1463. state Offer to record a note whenever changing the TODO state
  1464. of an item. This is only relevant if TODO keywords are
  1465. interpreted as sequence, see variable `org-todo-interpretation'.
  1466. When `state' is set, this includes tracking `done'.
  1467. clock-out Offer to record a note when clocking out of an item.
  1468. A separate window will then pop up and allow you to type a note.
  1469. After finishing with C-c C-c, the note will be added directly after the
  1470. timestamp, as a plain list item. See also the variable
  1471. `org-log-note-headings'.
  1472. Logging can also be configured on a per-file basis by adding one of
  1473. the following lines anywhere in the buffer:
  1474. #+STARTUP: logdone
  1475. #+STARTUP: nologging
  1476. #+STARTUP: lognotedone
  1477. #+STARTUP: lognotestate
  1478. #+STARTUP: lognoteclock-out
  1479. You can have local logging settings for a subtree by setting the LOGGING
  1480. property to one or more of these keywords."
  1481. :group 'org-todo
  1482. :group 'org-progress
  1483. :type '(choice
  1484. (const :tag "off" nil)
  1485. (const :tag "on" t)
  1486. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1487. (const :tag "when item is marked DONE" done)
  1488. (const :tag "when TODO state changes" state)
  1489. (const :tag "when clocking out" clock-out))))
  1490. (defcustom org-log-done-with-time t
  1491. "Non-nil means, the CLOSED time stamp will contain date and time.
  1492. When nil, only the date will be recorded."
  1493. :group 'org-progress
  1494. :type 'boolean)
  1495. (defcustom org-log-note-headings
  1496. '((done . "CLOSING NOTE %t")
  1497. (state . "State %-12s %t")
  1498. (clock-out . ""))
  1499. "Headings for notes added when clocking out or closing TODO items.
  1500. The value is an alist, with the car being a symbol indicating the note
  1501. context, and the cdr is the heading to be used. The heading may also be the
  1502. empty string.
  1503. %t in the heading will be replaced by a time stamp.
  1504. %s will be replaced by the new TODO state, in double quotes.
  1505. %u will be replaced by the user name.
  1506. %U will be replaced by the full user name."
  1507. :group 'org-todo
  1508. :group 'org-progress
  1509. :type '(list :greedy t
  1510. (cons (const :tag "Heading when closing an item" done) string)
  1511. (cons (const :tag
  1512. "Heading when changing todo state (todo sequence only)"
  1513. state) string)
  1514. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1515. (defcustom org-log-states-order-reversed t
  1516. "Non-nil means, the latest state change note will be directly after heading.
  1517. When nil, the notes will be orderer according to time."
  1518. :group 'org-todo
  1519. :group 'org-progress
  1520. :type 'boolean)
  1521. (defcustom org-log-repeat t
  1522. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1523. When nil, no note will be taken.
  1524. This option can also be set with on a per-file-basis with
  1525. #+STARTUP: logrepeat
  1526. #+STARTUP: nologrepeat
  1527. You can have local logging settings for a subtree by setting the LOGGING
  1528. property to one or more of these keywords."
  1529. :group 'org-todo
  1530. :group 'org-progress
  1531. :type 'boolean)
  1532. (defcustom org-clock-into-drawer 2
  1533. "Should clocking info be wrapped into a drawer?
  1534. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1535. If necessary, the drawer will be created.
  1536. When nil, the drawer will not be created, but used when present.
  1537. When an integer and the number of clocking entries in an item
  1538. reaches or exceeds this number, a drawer will be created."
  1539. :group 'org-todo
  1540. :group 'org-progress
  1541. :type '(choice
  1542. (const :tag "Always" t)
  1543. (const :tag "Only when drawer exists" nil)
  1544. (integer :tag "When at least N clock entries")))
  1545. (defcustom org-clock-out-when-done t
  1546. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1547. Nil means, clock will keep running until stopped explicitly with
  1548. `C-c C-x C-o', or until the clock is started in a different item."
  1549. :group 'org-progress
  1550. :type 'boolean)
  1551. (defgroup org-priorities nil
  1552. "Priorities in Org-mode."
  1553. :tag "Org Priorities"
  1554. :group 'org-todo)
  1555. (defcustom org-highest-priority ?A
  1556. "The highest priority of TODO items. A character like ?A, ?B etc.
  1557. Must have a smaller ASCII number than `org-lowest-priority'."
  1558. :group 'org-priorities
  1559. :type 'character)
  1560. (defcustom org-lowest-priority ?C
  1561. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1562. Must have a larger ASCII number than `org-highest-priority'."
  1563. :group 'org-priorities
  1564. :type 'character)
  1565. (defcustom org-default-priority ?B
  1566. "The default priority of TODO items.
  1567. This is the priority an item get if no explicit priority is given."
  1568. :group 'org-priorities
  1569. :type 'character)
  1570. (defcustom org-priority-start-cycle-with-default t
  1571. "Non-nil means, start with default priority when starting to cycle.
  1572. When this is nil, the first step in the cycle will be (depending on the
  1573. command used) one higher or lower that the default priority."
  1574. :group 'org-priorities
  1575. :type 'boolean)
  1576. (defgroup org-time nil
  1577. "Options concerning time stamps and deadlines in Org-mode."
  1578. :tag "Org Time"
  1579. :group 'org)
  1580. (defcustom org-insert-labeled-timestamps-at-point nil
  1581. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1582. When nil, these labeled time stamps are forces into the second line of an
  1583. entry, just after the headline. When scheduling from the global TODO list,
  1584. the time stamp will always be forced into the second line."
  1585. :group 'org-time
  1586. :type 'boolean)
  1587. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1588. "Formats for `format-time-string' which are used for time stamps.
  1589. It is not recommended to change this constant.")
  1590. (defcustom org-time-stamp-rounding-minutes 0
  1591. "Number of minutes to round time stamps to upon insertion.
  1592. When zero, insert the time unmodified. Useful rounding numbers
  1593. should be factors of 60, so for example 5, 10, 15.
  1594. When this is not zero, you can still force an exact time-stamp by using
  1595. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1596. :group 'org-time
  1597. :type 'integer)
  1598. (defcustom org-display-custom-times nil
  1599. "Non-nil means, overlay custom formats over all time stamps.
  1600. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1601. To turn this on on a per-file basis, insert anywhere in the file:
  1602. #+STARTUP: customtime"
  1603. :group 'org-time
  1604. :set 'set-default
  1605. :type 'sexp)
  1606. (make-variable-buffer-local 'org-display-custom-times)
  1607. (defcustom org-time-stamp-custom-formats
  1608. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1609. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1610. These are overlayed over the default ISO format if the variable
  1611. `org-display-custom-times' is set. Time like %H:%M should be at the
  1612. end of the second format."
  1613. :group 'org-time
  1614. :type 'sexp)
  1615. (defun org-time-stamp-format (&optional long inactive)
  1616. "Get the right format for a time string."
  1617. (let ((f (if long (cdr org-time-stamp-formats)
  1618. (car org-time-stamp-formats))))
  1619. (if inactive
  1620. (concat "[" (substring f 1 -1) "]")
  1621. f)))
  1622. (defcustom org-read-date-prefer-future t
  1623. "Non-nil means, assume future for incomplete date input from user.
  1624. This affects the following situations:
  1625. 1. The user gives a day, but no month.
  1626. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1627. read this as the third of *next* month. However, if you enter \"17\",
  1628. it will be considered as *this* month.
  1629. 2. The user gives a month but not a year.
  1630. For example, if it is april and you enter \"feb 2\", this will be read
  1631. as feb 2, *next* year. \"May 5\", however, will be this year.
  1632. When this option is nil, the current month and year will always be used
  1633. as defaults."
  1634. :group 'org-time
  1635. :type 'boolean)
  1636. (defcustom org-read-date-display-live t
  1637. "Non-nil means, display current interpretation of date prompt live.
  1638. This display will be in an overlay, in the minibuffer."
  1639. :group 'org-time
  1640. :type 'boolean)
  1641. (defcustom org-read-date-popup-calendar t
  1642. "Non-nil means, pop up a calendar when prompting for a date.
  1643. In the calendar, the date can be selected with mouse-1. However, the
  1644. minibuffer will also be active, and you can simply enter the date as well.
  1645. When nil, only the minibuffer will be available."
  1646. :group 'org-time
  1647. :type 'boolean)
  1648. (if (fboundp 'defvaralias)
  1649. (defvaralias 'org-popup-calendar-for-date-prompt
  1650. 'org-read-date-popup-calendar))
  1651. (defcustom org-extend-today-until 0
  1652. "The hour when your day really ends.
  1653. This has influence for the following applications:
  1654. - When switching the agenda to \"today\". It it is still earlier than
  1655. the time given here, the day recognized as TODAY is actually yesterday.
  1656. - When a date is read from the user and it is still before the time given
  1657. here, the current date and time will be assumed to be yesterday, 23:59.
  1658. FIXME:
  1659. IMPORTANT: This is still a very experimental feature, it may disappear
  1660. again or it may be extended to mean more things."
  1661. :group 'org-time
  1662. :type 'number)
  1663. (defcustom org-edit-timestamp-down-means-later nil
  1664. "Non-nil means, S-down will increase the time in a time stamp.
  1665. When nil, S-up will increase."
  1666. :group 'org-time
  1667. :type 'boolean)
  1668. (defcustom org-calendar-follow-timestamp-change t
  1669. "Non-nil means, make the calendar window follow timestamp changes.
  1670. When a timestamp is modified and the calendar window is visible, it will be
  1671. moved to the new date."
  1672. :group 'org-time
  1673. :type 'boolean)
  1674. (defcustom org-clock-heading-function nil
  1675. "When non-nil, should be a function to create `org-clock-heading'.
  1676. This is the string shown in the mode line when a clock is running.
  1677. The function is called with point at the beginning of the headline."
  1678. :group 'org-time ; FIXME: Should we have a separate group????
  1679. :type 'function)
  1680. (defgroup org-tags nil
  1681. "Options concerning tags in Org-mode."
  1682. :tag "Org Tags"
  1683. :group 'org)
  1684. (defcustom org-tag-alist nil
  1685. "List of tags allowed in Org-mode files.
  1686. When this list is nil, Org-mode will base TAG input on what is already in the
  1687. buffer.
  1688. The value of this variable is an alist, the car of each entry must be a
  1689. keyword as a string, the cdr may be a character that is used to select
  1690. that tag through the fast-tag-selection interface.
  1691. See the manual for details."
  1692. :group 'org-tags
  1693. :type '(repeat
  1694. (choice
  1695. (cons (string :tag "Tag name")
  1696. (character :tag "Access char"))
  1697. (const :tag "Start radio group" (:startgroup))
  1698. (const :tag "End radio group" (:endgroup)))))
  1699. (defcustom org-use-fast-tag-selection 'auto
  1700. "Non-nil means, use fast tag selection scheme.
  1701. This is a special interface to select and deselect tags with single keys.
  1702. When nil, fast selection is never used.
  1703. When the symbol `auto', fast selection is used if and only if selection
  1704. characters for tags have been configured, either through the variable
  1705. `org-tag-alist' or through a #+TAGS line in the buffer.
  1706. When t, fast selection is always used and selection keys are assigned
  1707. automatically if necessary."
  1708. :group 'org-tags
  1709. :type '(choice
  1710. (const :tag "Always" t)
  1711. (const :tag "Never" nil)
  1712. (const :tag "When selection characters are configured" 'auto)))
  1713. (defcustom org-fast-tag-selection-single-key nil
  1714. "Non-nil means, fast tag selection exits after first change.
  1715. When nil, you have to press RET to exit it.
  1716. During fast tag selection, you can toggle this flag with `C-c'.
  1717. This variable can also have the value `expert'. In this case, the window
  1718. displaying the tags menu is not even shown, until you press C-c again."
  1719. :group 'org-tags
  1720. :type '(choice
  1721. (const :tag "No" nil)
  1722. (const :tag "Yes" t)
  1723. (const :tag "Expert" expert)))
  1724. (defvar org-fast-tag-selection-include-todo nil
  1725. "Non-nil means, fast tags selection interface will also offer TODO states.
  1726. This is an undocumented feature, you should not rely on it.")
  1727. (defcustom org-tags-column -80
  1728. "The column to which tags should be indented in a headline.
  1729. If this number is positive, it specifies the column. If it is negative,
  1730. it means that the tags should be flushright to that column. For example,
  1731. -80 works well for a normal 80 character screen."
  1732. :group 'org-tags
  1733. :type 'integer)
  1734. (defcustom org-auto-align-tags t
  1735. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1736. These operations change the length of a headline and therefore shift
  1737. the tags around. With this options turned on, after each such operation
  1738. the tags are again aligned to `org-tags-column'."
  1739. :group 'org-tags
  1740. :type 'boolean)
  1741. (defcustom org-use-tag-inheritance t
  1742. "Non-nil means, tags in levels apply also for sublevels.
  1743. When nil, only the tags directly given in a specific line apply there.
  1744. If you turn off this option, you very likely want to turn on the
  1745. companion option `org-tags-match-list-sublevels'."
  1746. :group 'org-tags
  1747. :type 'boolean)
  1748. (defcustom org-tags-match-list-sublevels nil
  1749. "Non-nil means list also sublevels of headlines matching tag search.
  1750. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1751. the sublevels of a headline matching a tag search often also match
  1752. the same search. Listing all of them can create very long lists.
  1753. Setting this variable to nil causes subtrees of a match to be skipped.
  1754. This option is off by default, because inheritance in on. If you turn
  1755. inheritance off, you very likely want to turn this option on.
  1756. As a special case, if the tag search is restricted to TODO items, the
  1757. value of this variable is ignored and sublevels are always checked, to
  1758. make sure all corresponding TODO items find their way into the list."
  1759. :group 'org-tags
  1760. :type 'boolean)
  1761. (defvar org-tags-history nil
  1762. "History of minibuffer reads for tags.")
  1763. (defvar org-last-tags-completion-table nil
  1764. "The last used completion table for tags.")
  1765. (defvar org-after-tags-change-hook nil
  1766. "Hook that is run after the tags in a line have changed.")
  1767. (defgroup org-properties nil
  1768. "Options concerning properties in Org-mode."
  1769. :tag "Org Properties"
  1770. :group 'org)
  1771. (defcustom org-property-format "%-10s %s"
  1772. "How property key/value pairs should be formatted by `indent-line'.
  1773. When `indent-line' hits a property definition, it will format the line
  1774. according to this format, mainly to make sure that the values are
  1775. lined-up with respect to each other."
  1776. :group 'org-properties
  1777. :type 'string)
  1778. (defcustom org-use-property-inheritance nil
  1779. "Non-nil means, properties apply also for sublevels.
  1780. This setting is only relevant during property searches, not when querying
  1781. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1782. you need to call `org-entry-get' with the inheritance flag.
  1783. Turning this on can cause significant overhead when doing a search, so
  1784. this is turned off by default.
  1785. When nil, only the properties directly given in the current entry count.
  1786. The value may also be a list of properties that shouldhave inheritance.
  1787. However, note that some special properties use inheritance under special
  1788. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1789. and the properties ending in \"_ALL\" when they are used as descriptor
  1790. for valid values of a property."
  1791. :group 'org-properties
  1792. :type '(choice
  1793. (const :tag "Not" nil)
  1794. (const :tag "Always" nil)
  1795. (repeat :tag "Specific properties")))
  1796. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1797. "The default column format, if no other format has been defined.
  1798. This variable can be set on the per-file basis by inserting a line
  1799. #+COLUMNS: %25ITEM ....."
  1800. :group 'org-properties
  1801. :type 'string)
  1802. (defcustom org-global-properties nil
  1803. "List of property/value pairs that can be inherited by any entry.
  1804. You can set buffer-local values for this by adding lines like
  1805. #+PROPERTY: NAME VALUE"
  1806. :group 'org-properties
  1807. :type '(repeat
  1808. (cons (string :tag "Property")
  1809. (string :tag "Value"))))
  1810. (defvar org-local-properties nil
  1811. "List of property/value pairs that can be inherited by any entry.
  1812. Valid for the current buffer.
  1813. This variable is populated from #+PROPERTY lines.")
  1814. (defgroup org-agenda nil
  1815. "Options concerning agenda views in Org-mode."
  1816. :tag "Org Agenda"
  1817. :group 'org)
  1818. (defvar org-category nil
  1819. "Variable used by org files to set a category for agenda display.
  1820. Such files should use a file variable to set it, for example
  1821. # -*- mode: org; org-category: \"ELisp\"
  1822. or contain a special line
  1823. #+CATEGORY: ELisp
  1824. If the file does not specify a category, then file's base name
  1825. is used instead.")
  1826. (make-variable-buffer-local 'org-category)
  1827. (defcustom org-agenda-files nil
  1828. "The files to be used for agenda display.
  1829. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1830. \\[org-remove-file]. You can also use customize to edit the list.
  1831. If an entry is a directory, all files in that directory that are matched by
  1832. `org-agenda-file-regexp' will be part of the file list.
  1833. If the value of the variable is not a list but a single file name, then
  1834. the list of agenda files is actually stored and maintained in that file, one
  1835. agenda file per line."
  1836. :group 'org-agenda
  1837. :type '(choice
  1838. (repeat :tag "List of files and directories" file)
  1839. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1840. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1841. "Regular expression to match files for `org-agenda-files'.
  1842. If any element in the list in that variable contains a directory instead
  1843. of a normal file, all files in that directory that are matched by this
  1844. regular expression will be included."
  1845. :group 'org-agenda
  1846. :type 'regexp)
  1847. (defcustom org-agenda-skip-unavailable-files nil
  1848. "t means to just skip non-reachable files in `org-agenda-files'.
  1849. Nil means to remove them, after a query, from the list."
  1850. :group 'org-agenda
  1851. :type 'boolean)
  1852. (defcustom org-agenda-multi-occur-extra-files nil
  1853. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1854. The files in `org-agenda-files' are always searched."
  1855. :group 'org-agenda
  1856. :type '(repeat file))
  1857. (defcustom org-agenda-confirm-kill 1
  1858. "When set, remote killing from the agenda buffer needs confirmation.
  1859. When t, a confirmation is always needed. When a number N, confirmation is
  1860. only needed when the text to be killed contains more than N non-white lines."
  1861. :group 'org-agenda
  1862. :type '(choice
  1863. (const :tag "Never" nil)
  1864. (const :tag "Always" t)
  1865. (number :tag "When more than N lines")))
  1866. (defcustom org-calendar-to-agenda-key [?c]
  1867. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1868. The command `org-calendar-goto-agenda' will be bound to this key. The
  1869. default is the character `c' because then `c' can be used to switch back and
  1870. forth between agenda and calendar."
  1871. :group 'org-agenda
  1872. :type 'sexp)
  1873. (defcustom org-agenda-compact-blocks nil
  1874. "Non-nil means, make the block agenda more compact.
  1875. This is done by leaving out unnecessary lines."
  1876. :group 'org-agenda
  1877. :type nil)
  1878. (defgroup org-agenda-export nil
  1879. "Options concerning exporting agenda views in Org-mode."
  1880. :tag "Org Agenda Export"
  1881. :group 'org-agenda)
  1882. (defcustom org-agenda-with-colors t
  1883. "Non-nil means, use colors in agenda views."
  1884. :group 'org-agenda-export
  1885. :type 'boolean)
  1886. (defcustom org-agenda-exporter-settings nil
  1887. "Alist of variable/value pairs that should be active during agenda export.
  1888. This is a good place to set uptions for ps-print and for htmlize."
  1889. :group 'org-agenda-export
  1890. :type '(repeat
  1891. (list
  1892. (variable)
  1893. (sexp :tag "Value"))))
  1894. (defcustom org-agenda-export-html-style ""
  1895. "The style specification for exported HTML Agenda files.
  1896. If this variable contains a string, it will replace the default <style>
  1897. section as produced by `htmlize'.
  1898. Since there are different ways of setting style information, this variable
  1899. needs to contain the full HTML structure to provide a style, including the
  1900. surrounding HTML tags. The style specifications should include definitions
  1901. the fonts used by the agenda, here is an example:
  1902. <style type=\"text/css\">
  1903. p { font-weight: normal; color: gray; }
  1904. .org-agenda-structure {
  1905. font-size: 110%;
  1906. color: #003399;
  1907. font-weight: 600;
  1908. }
  1909. .org-todo {
  1910. color: #cc6666;Week-agenda:
  1911. font-weight: bold;
  1912. }
  1913. .org-done {
  1914. color: #339933;
  1915. }
  1916. .title { text-align: center; }
  1917. .todo, .deadline { color: red; }
  1918. .done { color: green; }
  1919. </style>
  1920. or, if you want to keep the style in a file,
  1921. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1922. As the value of this option simply gets inserted into the HTML <head> header,
  1923. you can \"misuse\" it to also add other text to the header. However,
  1924. <style>...</style> is required, if not present the variable will be ignored."
  1925. :group 'org-agenda-export
  1926. :group 'org-export-html
  1927. :type 'string)
  1928. (defgroup org-agenda-custom-commands nil
  1929. "Options concerning agenda views in Org-mode."
  1930. :tag "Org Agenda Custom Commands"
  1931. :group 'org-agenda)
  1932. (defcustom org-agenda-custom-commands nil
  1933. "Custom commands for the agenda.
  1934. These commands will be offered on the splash screen displayed by the
  1935. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1936. (key desc type match options files)
  1937. key The key (one or more characters as a string) to be associated
  1938. with the command.
  1939. desc A description of the commend, when omitted or nil, a default
  1940. description is built using MATCH.
  1941. type The command type, any of the following symbols:
  1942. todo Entries with a specific TODO keyword, in all agenda files.
  1943. tags Tags match in all agenda files.
  1944. tags-todo Tags match in all agenda files, TODO entries only.
  1945. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1946. tags-tree Sparse tree with all tags matches in *current* file.
  1947. occur-tree Occur sparse tree for *current* file.
  1948. ... A user-defined function.
  1949. match What to search for:
  1950. - a single keyword for TODO keyword searches
  1951. - a tags match expression for tags searches
  1952. - a regular expression for occur searches
  1953. options A list of option settings, similar to that in a let form, so like
  1954. this: ((opt1 val1) (opt2 val2) ...)
  1955. files A list of files file to write the produced agenda buffer to
  1956. with the command `org-store-agenda-views'.
  1957. If a file name ends in \".html\", an HTML version of the buffer
  1958. is written out. If it ends in \".ps\", a postscript version is
  1959. produced. Otherwide, only the plain text is written to the file.
  1960. You can also define a set of commands, to create a composite agenda buffer.
  1961. In this case, an entry looks like this:
  1962. (key desc (cmd1 cmd2 ...) general-options file)
  1963. where
  1964. desc A description string to be displayed in the dispatcher menu.
  1965. cmd An agenda command, similar to the above. However, tree commands
  1966. are no allowed, but instead you can get agenda and global todo list.
  1967. So valid commands for a set are:
  1968. (agenda)
  1969. (alltodo)
  1970. (stuck)
  1971. (todo \"match\" options files)
  1972. (tags \"match\" options files)
  1973. (tags-todo \"match\" options files)
  1974. Each command can carry a list of options, and another set of options can be
  1975. given for the whole set of commands. Individual command options take
  1976. precedence over the general options.
  1977. When using several characters as key to a command, the first characters
  1978. are prefix commands. For the dispatcher to display useful information, you
  1979. should provide a description for the prefix, like
  1980. (setq org-agenda-custom-commands
  1981. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  1982. (\"hl\" tags \"+HOME+Lisa\")
  1983. (\"hp\" tags \"+HOME+Peter\")
  1984. (\"hk\" tags \"+HOME+Kim\")))"
  1985. :group 'org-agenda-custom-commands
  1986. :type '(repeat
  1987. (choice :value ("a" "" tags "" nil)
  1988. (list :tag "Single command"
  1989. (string :tag "Access Key(s) ")
  1990. (option (string :tag "Description"))
  1991. (choice
  1992. (const :tag "Agenda" agenda)
  1993. (const :tag "TODO list" alltodo)
  1994. (const :tag "Stuck projects" stuck)
  1995. (const :tag "Tags search (all agenda files)" tags)
  1996. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1997. (const :tag "TODO keyword search (all agenda files)" todo)
  1998. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1999. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2000. (const :tag "Occur tree (current buffer)" occur-tree)
  2001. (sexp :tag "Other, user-defined function"))
  2002. (string :tag "Match")
  2003. (repeat :tag "Local options"
  2004. (list (variable :tag "Option") (sexp :tag "Value")))
  2005. (option (repeat :tag "Export" (file :tag "Export to"))))
  2006. (list :tag "Command series, all agenda files"
  2007. (string :tag "Access Key(s)")
  2008. (string :tag "Description ")
  2009. (repeat
  2010. (choice
  2011. (const :tag "Agenda" (agenda))
  2012. (const :tag "TODO list" (alltodo))
  2013. (const :tag "Stuck projects" (stuck))
  2014. (list :tag "Tags search"
  2015. (const :format "" tags)
  2016. (string :tag "Match")
  2017. (repeat :tag "Local options"
  2018. (list (variable :tag "Option")
  2019. (sexp :tag "Value"))))
  2020. (list :tag "Tags search, TODO entries only"
  2021. (const :format "" tags-todo)
  2022. (string :tag "Match")
  2023. (repeat :tag "Local options"
  2024. (list (variable :tag "Option")
  2025. (sexp :tag "Value"))))
  2026. (list :tag "TODO keyword search"
  2027. (const :format "" todo)
  2028. (string :tag "Match")
  2029. (repeat :tag "Local options"
  2030. (list (variable :tag "Option")
  2031. (sexp :tag "Value"))))
  2032. (list :tag "Other, user-defined function"
  2033. (symbol :tag "function")
  2034. (string :tag "Match")
  2035. (repeat :tag "Local options"
  2036. (list (variable :tag "Option")
  2037. (sexp :tag "Value"))))))
  2038. (repeat :tag "General options"
  2039. (list (variable :tag "Option")
  2040. (sexp :tag "Value")))
  2041. (option (repeat :tag "Export" (file :tag "Export to"))))
  2042. (cons :tag "Prefix key documentation"
  2043. (string :tag "Access Key(s)")
  2044. (string :tag "Description ")))))
  2045. (defcustom org-stuck-projects
  2046. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2047. "How to identify stuck projects.
  2048. This is a list of four items:
  2049. 1. A tags/todo matcher string that is used to identify a project.
  2050. The entire tree below a headline matched by this is considered one project.
  2051. 2. A list of TODO keywords identifying non-stuck projects.
  2052. If the project subtree contains any headline with one of these todo
  2053. keywords, the project is considered to be not stuck. If you specify
  2054. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2055. 3. A list of tags identifying non-stuck projects.
  2056. If the project subtree contains any headline with one of these tags,
  2057. the project is considered to be not stuck. If you specify \"*\" as
  2058. a tag, any tag will mark the project unstuck.
  2059. 4. An arbitrary regular expression matching non-stuck projects.
  2060. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2061. or `C-c a #' to produce the list."
  2062. :group 'org-agenda-custom-commands
  2063. :type '(list
  2064. (string :tag "Tags/TODO match to identify a project")
  2065. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2066. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2067. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2068. (defgroup org-agenda-skip nil
  2069. "Options concerning skipping parts of agenda files."
  2070. :tag "Org Agenda Skip"
  2071. :group 'org-agenda)
  2072. (defcustom org-agenda-todo-list-sublevels t
  2073. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2074. When nil, the sublevels of a TODO entry are not checked, resulting in
  2075. potentially much shorter TODO lists."
  2076. :group 'org-agenda-skip
  2077. :group 'org-todo
  2078. :type 'boolean)
  2079. (defcustom org-agenda-todo-ignore-with-date nil
  2080. "Non-nil means, don't show entries with a date in the global todo list.
  2081. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2082. but don't want them to show up in the TODO list.
  2083. When this is set, it also covers deadlines and scheduled items, the settings
  2084. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2085. will be ignored."
  2086. :group 'org-agenda-skip
  2087. :group 'org-todo
  2088. :type 'boolean)
  2089. (defcustom org-agenda-todo-ignore-scheduled nil
  2090. "Non-nil means, don't show scheduled entries in the global todo list.
  2091. The idea behind this is that by scheduling it, you have already taken care
  2092. of this item.
  2093. See also `org-agenda-todo-ignore-with-date'."
  2094. :group 'org-agenda-skip
  2095. :group 'org-todo
  2096. :type 'boolean)
  2097. (defcustom org-agenda-todo-ignore-deadlines nil
  2098. "Non-nil means, don't show near deadline entries in the global todo list.
  2099. Near means closer than `org-deadline-warning-days' days.
  2100. The idea behind this is that such items will appear in the agenda anyway.
  2101. See also `org-agenda-todo-ignore-with-date'."
  2102. :group 'org-agenda-skip
  2103. :group 'org-todo
  2104. :type 'boolean)
  2105. (defcustom org-agenda-skip-scheduled-if-done nil
  2106. "Non-nil means don't show scheduled items in agenda when they are done.
  2107. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2108. it applies only to the actual date of the scheduling. Warnings about
  2109. an item with a past scheduling dates are always turned off when the item
  2110. is DONE."
  2111. :group 'org-agenda-skip
  2112. :type 'boolean)
  2113. (defcustom org-agenda-skip-deadline-if-done nil
  2114. "Non-nil means don't show deadines when the corresponding item is done.
  2115. When nil, the deadline is still shown and should give you a happy feeling.
  2116. This is relevant for the daily/weekly agenda. And it applied only to the
  2117. actualy date of the deadline. Warnings about approching and past-due
  2118. deadlines are always turned off when the item is DONE."
  2119. :group 'org-agenda-skip
  2120. :type 'boolean)
  2121. (defcustom org-agenda-skip-timestamp-if-done nil
  2122. "Non-nil means don't don't select item by timestamp or -range if it is DONE."
  2123. :group 'org-agenda-skip
  2124. :type 'boolean)
  2125. (defcustom org-timeline-show-empty-dates 3
  2126. "Non-nil means, `org-timeline' also shows dates without an entry.
  2127. When nil, only the days which actually have entries are shown.
  2128. When t, all days between the first and the last date are shown.
  2129. When an integer, show also empty dates, but if there is a gap of more than
  2130. N days, just insert a special line indicating the size of the gap."
  2131. :group 'org-agenda-skip
  2132. :type '(choice
  2133. (const :tag "None" nil)
  2134. (const :tag "All" t)
  2135. (number :tag "at most")))
  2136. (defgroup org-agenda-startup nil
  2137. "Options concerning initial settings in the Agenda in Org Mode."
  2138. :tag "Org Agenda Startup"
  2139. :group 'org-agenda)
  2140. (defcustom org-finalize-agenda-hook nil
  2141. "Hook run just before displaying an agenda buffer."
  2142. :group 'org-agenda-startup
  2143. :type 'hook)
  2144. (defcustom org-agenda-mouse-1-follows-link nil
  2145. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2146. A longer mouse click will still set point. Does not wortk on XEmacs.
  2147. Needs to be set before org.el is loaded."
  2148. :group 'org-agenda-startup
  2149. :type 'boolean)
  2150. (defcustom org-agenda-start-with-follow-mode nil
  2151. "The initial value of follow-mode in a newly created agenda window."
  2152. :group 'org-agenda-startup
  2153. :type 'boolean)
  2154. (defgroup org-agenda-windows nil
  2155. "Options concerning the windows used by the Agenda in Org Mode."
  2156. :tag "Org Agenda Windows"
  2157. :group 'org-agenda)
  2158. (defcustom org-agenda-window-setup 'reorganize-frame
  2159. "How the agenda buffer should be displayed.
  2160. Possible values for this option are:
  2161. current-window Show agenda in the current window, keeping all other windows.
  2162. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2163. other-window Use `switch-to-buffer-other-window' to display agenda.
  2164. reorganize-frame Show only two windows on the current frame, the current
  2165. window and the agenda.
  2166. See also the variable `org-agenda-restore-windows-after-quit'."
  2167. :group 'org-agenda-windows
  2168. :type '(choice
  2169. (const current-window)
  2170. (const other-frame)
  2171. (const other-window)
  2172. (const reorganize-frame)))
  2173. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2174. "The min and max height of the agenda window as a fraction of frame height.
  2175. The value of the variable is a cons cell with two numbers between 0 and 1.
  2176. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2177. :group 'org-agenda-windows
  2178. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2179. (defcustom org-agenda-restore-windows-after-quit nil
  2180. "Non-nil means, restore window configuration open exiting agenda.
  2181. Before the window configuration is changed for displaying the agenda,
  2182. the current status is recorded. When the agenda is exited with
  2183. `q' or `x' and this option is set, the old state is restored. If
  2184. `org-agenda-window-setup' is `other-frame', the value of this
  2185. option will be ignored.."
  2186. :group 'org-agenda-windows
  2187. :type 'boolean)
  2188. (defcustom org-indirect-buffer-display 'other-window
  2189. "How should indirect tree buffers be displayed?
  2190. This applies to indirect buffers created with the commands
  2191. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2192. Valid values are:
  2193. current-window Display in the current window
  2194. other-window Just display in another window.
  2195. dedicated-frame Create one new frame, and re-use it each time.
  2196. new-frame Make a new frame each time. Note that in this case
  2197. previously-made indirect buffers are kept, and you need to
  2198. kill these buffers yourself."
  2199. :group 'org-structure
  2200. :group 'org-agenda-windows
  2201. :type '(choice
  2202. (const :tag "In current window" current-window)
  2203. (const :tag "In current frame, other window" other-window)
  2204. (const :tag "Each time a new frame" new-frame)
  2205. (const :tag "One dedicated frame" dedicated-frame)))
  2206. (defgroup org-agenda-daily/weekly nil
  2207. "Options concerning the daily/weekly agenda."
  2208. :tag "Org Agenda Daily/Weekly"
  2209. :group 'org-agenda)
  2210. (defcustom org-agenda-ndays 7
  2211. "Number of days to include in overview display.
  2212. Should be 1 or 7."
  2213. :group 'org-agenda-daily/weekly
  2214. :type 'number)
  2215. (defcustom org-agenda-start-on-weekday 1
  2216. "Non-nil means, start the overview always on the specified weekday.
  2217. 0 denotes Sunday, 1 denotes Monday etc.
  2218. When nil, always start on the current day."
  2219. :group 'org-agenda-daily/weekly
  2220. :type '(choice (const :tag "Today" nil)
  2221. (number :tag "Weekday No.")))
  2222. (defcustom org-agenda-show-all-dates t
  2223. "Non-nil means, `org-agenda' shows every day in the selected range.
  2224. When nil, only the days which actually have entries are shown."
  2225. :group 'org-agenda-daily/weekly
  2226. :type 'boolean)
  2227. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2228. "Format string for displaying dates in the agenda.
  2229. Used by the daily/weekly agenda and by the timeline. This should be
  2230. a format string understood by `format-time-string', or a function returning
  2231. the formatted date as a string. The function must take a single argument,
  2232. a calendar-style date list like (month day year)."
  2233. :group 'org-agenda-daily/weekly
  2234. :type '(choice
  2235. (string :tag "Format string")
  2236. (function :tag "Function")))
  2237. (defun org-agenda-format-date-aligned (date)
  2238. "Format a date string for display in the daily/weekly agenda, or timeline.
  2239. This function makes sure that dates are aligned for easy reading."
  2240. (format "%-9s %2d %s %4d"
  2241. (calendar-day-name date)
  2242. (extract-calendar-day date)
  2243. (calendar-month-name (extract-calendar-month date))
  2244. (extract-calendar-year date)))
  2245. (defcustom org-agenda-include-diary nil
  2246. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2247. :group 'org-agenda-daily/weekly
  2248. :type 'boolean)
  2249. (defcustom org-agenda-include-all-todo nil
  2250. "Set means weekly/daily agenda will always contain all TODO entries.
  2251. The TODO entries will be listed at the top of the agenda, before
  2252. the entries for specific days."
  2253. :group 'org-agenda-daily/weekly
  2254. :type 'boolean)
  2255. (defcustom org-agenda-repeating-timestamp-show-all t
  2256. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2257. When nil, only one occurence is shown, either today or the
  2258. nearest into the future."
  2259. :group 'org-agenda-daily/weekly
  2260. :type 'boolean)
  2261. (defcustom org-deadline-warning-days 14
  2262. "No. of days before expiration during which a deadline becomes active.
  2263. This variable governs the display in sparse trees and in the agenda.
  2264. When negative, it means use this number (the absolute value of it)
  2265. even if a deadline has a different individual lead time specified."
  2266. :group 'org-time
  2267. :group 'org-agenda-daily/weekly
  2268. :type 'number)
  2269. (defcustom org-scheduled-past-days 10000
  2270. "No. of days to continue listing scheduled items that are not marked DONE.
  2271. When an item is scheduled on a date, it shows up in the agenda on this
  2272. day and will be listed until it is marked done for the number of days
  2273. given here."
  2274. :group 'org-agenda-daily/weekly
  2275. :type 'number)
  2276. (defgroup org-agenda-time-grid nil
  2277. "Options concerning the time grid in the Org-mode Agenda."
  2278. :tag "Org Agenda Time Grid"
  2279. :group 'org-agenda)
  2280. (defcustom org-agenda-use-time-grid t
  2281. "Non-nil means, show a time grid in the agenda schedule.
  2282. A time grid is a set of lines for specific times (like every two hours between
  2283. 8:00 and 20:00). The items scheduled for a day at specific times are
  2284. sorted in between these lines.
  2285. For details about when the grid will be shown, and what it will look like, see
  2286. the variable `org-agenda-time-grid'."
  2287. :group 'org-agenda-time-grid
  2288. :type 'boolean)
  2289. (defcustom org-agenda-time-grid
  2290. '((daily today require-timed)
  2291. "----------------"
  2292. (800 1000 1200 1400 1600 1800 2000))
  2293. "The settings for time grid for agenda display.
  2294. This is a list of three items. The first item is again a list. It contains
  2295. symbols specifying conditions when the grid should be displayed:
  2296. daily if the agenda shows a single day
  2297. weekly if the agenda shows an entire week
  2298. today show grid on current date, independent of daily/weekly display
  2299. require-timed show grid only if at least one item has a time specification
  2300. The second item is a string which will be places behing the grid time.
  2301. The third item is a list of integers, indicating the times that should have
  2302. a grid line."
  2303. :group 'org-agenda-time-grid
  2304. :type
  2305. '(list
  2306. (set :greedy t :tag "Grid Display Options"
  2307. (const :tag "Show grid in single day agenda display" daily)
  2308. (const :tag "Show grid in weekly agenda display" weekly)
  2309. (const :tag "Always show grid for today" today)
  2310. (const :tag "Show grid only if any timed entries are present"
  2311. require-timed)
  2312. (const :tag "Skip grid times already present in an entry"
  2313. remove-match))
  2314. (string :tag "Grid String")
  2315. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2316. (defgroup org-agenda-sorting nil
  2317. "Options concerning sorting in the Org-mode Agenda."
  2318. :tag "Org Agenda Sorting"
  2319. :group 'org-agenda)
  2320. (defconst org-sorting-choice
  2321. '(choice
  2322. (const time-up) (const time-down)
  2323. (const category-keep) (const category-up) (const category-down)
  2324. (const tag-down) (const tag-up)
  2325. (const priority-up) (const priority-down))
  2326. "Sorting choices.")
  2327. (defcustom org-agenda-sorting-strategy
  2328. '((agenda time-up category-keep priority-down)
  2329. (todo category-keep priority-down)
  2330. (tags category-keep priority-down))
  2331. "Sorting structure for the agenda items of a single day.
  2332. This is a list of symbols which will be used in sequence to determine
  2333. if an entry should be listed before another entry. The following
  2334. symbols are recognized:
  2335. time-up Put entries with time-of-day indications first, early first
  2336. time-down Put entries with time-of-day indications first, late first
  2337. category-keep Keep the default order of categories, corresponding to the
  2338. sequence in `org-agenda-files'.
  2339. category-up Sort alphabetically by category, A-Z.
  2340. category-down Sort alphabetically by category, Z-A.
  2341. tag-up Sort alphabetically by last tag, A-Z.
  2342. tag-down Sort alphabetically by last tag, Z-A.
  2343. priority-up Sort numerically by priority, high priority last.
  2344. priority-down Sort numerically by priority, high priority first.
  2345. The different possibilities will be tried in sequence, and testing stops
  2346. if one comparison returns a \"not-equal\". For example, the default
  2347. '(time-up category-keep priority-down)
  2348. means: Pull out all entries having a specified time of day and sort them,
  2349. in order to make a time schedule for the current day the first thing in the
  2350. agenda listing for the day. Of the entries without a time indication, keep
  2351. the grouped in categories, don't sort the categories, but keep them in
  2352. the sequence given in `org-agenda-files'. Within each category sort by
  2353. priority.
  2354. Leaving out `category-keep' would mean that items will be sorted across
  2355. categories by priority.
  2356. Instead of a single list, this can also be a set of list for specific
  2357. contents, with a context symbol in the car of the list, any of
  2358. `agenda', `todo', `tags' for the corresponding agenda views."
  2359. :group 'org-agenda-sorting
  2360. :type `(choice
  2361. (repeat :tag "General" org-sorting-choice)
  2362. (list :tag "Individually"
  2363. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2364. (repeat ,org-sorting-choice))
  2365. (cons (const :tag "Strategy for TODO lists" todo)
  2366. (repeat ,org-sorting-choice))
  2367. (cons (const :tag "Strategy for Tags matches" tags)
  2368. (repeat ,org-sorting-choice)))))
  2369. (defcustom org-sort-agenda-notime-is-late t
  2370. "Non-nil means, items without time are considered late.
  2371. This is only relevant for sorting. When t, items which have no explicit
  2372. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2373. do have a time. When nil, the default time is before 0:00. You can use this
  2374. option to decide if the schedule for today should come before or after timeless
  2375. agenda entries."
  2376. :group 'org-agenda-sorting
  2377. :type 'boolean)
  2378. (defgroup org-agenda-line-format nil
  2379. "Options concerning the entry prefix in the Org-mode agenda display."
  2380. :tag "Org Agenda Line Format"
  2381. :group 'org-agenda)
  2382. (defcustom org-agenda-prefix-format
  2383. '((agenda . " %-12:c%?-12t% s")
  2384. (timeline . " % s")
  2385. (todo . " %-12:c")
  2386. (tags . " %-12:c"))
  2387. "Format specifications for the prefix of items in the agenda views.
  2388. An alist with four entries, for the different agenda types. The keys to the
  2389. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2390. are format strings.
  2391. This format works similar to a printf format, with the following meaning:
  2392. %c the category of the item, \"Diary\" for entries from the diary, or
  2393. as given by the CATEGORY keyword or derived from the file name.
  2394. %T the *last* tag of the item. Last because inherited tags come
  2395. first in the list.
  2396. %t the time-of-day specification if one applies to the entry, in the
  2397. format HH:MM
  2398. %s Scheduling/Deadline information, a short string
  2399. All specifiers work basically like the standard `%s' of printf, but may
  2400. contain two additional characters: A question mark just after the `%' and
  2401. a whitespace/punctuation character just before the final letter.
  2402. If the first character after `%' is a question mark, the entire field
  2403. will only be included if the corresponding value applies to the
  2404. current entry. This is useful for fields which should have fixed
  2405. width when present, but zero width when absent. For example,
  2406. \"%?-12t\" will result in a 12 character time field if a time of the
  2407. day is specified, but will completely disappear in entries which do
  2408. not contain a time.
  2409. If there is punctuation or whitespace character just before the final
  2410. format letter, this character will be appended to the field value if
  2411. the value is not empty. For example, the format \"%-12:c\" leads to
  2412. \"Diary: \" if the category is \"Diary\". If the category were be
  2413. empty, no additional colon would be interted.
  2414. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2415. - Indent the line with two space characters
  2416. - Give the category in a 12 chars wide field, padded with whitespace on
  2417. the right (because of `-'). Append a colon if there is a category
  2418. (because of `:').
  2419. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2420. time, don't put in an empty field, just skip it (because of '?').
  2421. - Finally, put the scheduling information and append a whitespace.
  2422. As another example, if you don't want the time-of-day of entries in
  2423. the prefix, you could use:
  2424. (setq org-agenda-prefix-format \" %-11:c% s\")
  2425. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2426. `org-agenda-remove-tags'."
  2427. :type '(choice
  2428. (string :tag "General format")
  2429. (list :greedy t :tag "View dependent"
  2430. (cons (const agenda) (string :tag "Format"))
  2431. (cons (const timeline) (string :tag "Format"))
  2432. (cons (const todo) (string :tag "Format"))
  2433. (cons (const tags) (string :tag "Format"))))
  2434. :group 'org-agenda-line-format)
  2435. (defvar org-prefix-format-compiled nil
  2436. "The compiled version of the most recently used prefix format.
  2437. See the variable `org-agenda-prefix-format'.")
  2438. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2439. "Text preceeding scheduled items in the agenda view.
  2440. THis is a list with two strings. The first applies when the item is
  2441. scheduled on the current day. The second applies when it has been scheduled
  2442. previously, it may contain a %d to capture how many days ago the item was
  2443. scheduled."
  2444. :group 'org-agenda-line-format
  2445. :type '(list
  2446. (string :tag "Scheduled today ")
  2447. (string :tag "Scheduled previously")))
  2448. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2449. "Text preceeding deadline items in the agenda view.
  2450. This is a list with two strings. The first applies when the item has its
  2451. deadline on the current day. The second applies when it is in the past or
  2452. in the future, it may contain %d to capture how many days away the deadline
  2453. is (was)."
  2454. :group 'org-agenda-line-format
  2455. :type '(list
  2456. (string :tag "Deadline today ")
  2457. (string :tag "Deadline relative")))
  2458. (defcustom org-agenda-remove-times-when-in-prefix t
  2459. "Non-nil means, remove duplicate time specifications in agenda items.
  2460. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2461. time-of-day specification in a headline or diary entry is extracted and
  2462. placed into the prefix. If this option is non-nil, the original specification
  2463. \(a timestamp or -range, or just a plain time(range) specification like
  2464. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2465. cluttered.
  2466. The option can be t or nil. It may also be the symbol `beg', indicating
  2467. that the time should only be removed what it is located at the beginning of
  2468. the headline/diary entry."
  2469. :group 'org-agenda-line-format
  2470. :type '(choice
  2471. (const :tag "Always" t)
  2472. (const :tag "Never" nil)
  2473. (const :tag "When at beginning of entry" beg)))
  2474. (defcustom org-agenda-default-appointment-duration nil
  2475. "Default duration for appointments that only have a starting time.
  2476. When nil, no duration is specified in such cases.
  2477. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2478. :group 'org-agenda-line-format
  2479. :type '(choice
  2480. (integer :tag "Minutes")
  2481. (const :tag "No default duration")))
  2482. (defcustom org-agenda-remove-tags nil
  2483. "Non-nil means, remove the tags from the headline copy in the agenda.
  2484. When this is the symbol `prefix', only remove tags when
  2485. `org-agenda-prefix-format' contains a `%T' specifier."
  2486. :group 'org-agenda-line-format
  2487. :type '(choice
  2488. (const :tag "Always" t)
  2489. (const :tag "Never" nil)
  2490. (const :tag "When prefix format contains %T" prefix)))
  2491. (if (fboundp 'defvaralias)
  2492. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2493. 'org-agenda-remove-tags))
  2494. (defcustom org-agenda-tags-column -80
  2495. "Shift tags in agenda items to this column.
  2496. If this number is positive, it specifies the column. If it is negative,
  2497. it means that the tags should be flushright to that column. For example,
  2498. -80 works well for a normal 80 character screen."
  2499. :group 'org-agenda-line-format
  2500. :type 'integer)
  2501. (if (fboundp 'defvaralias)
  2502. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2503. (defcustom org-agenda-fontify-priorities t
  2504. "Non-nil means, highlight low and high priorities in agenda.
  2505. When t, the highest priority entries are bold, lowest priority italic.
  2506. This may also be an association list of priority faces. The face may be
  2507. a names face, or a list like `(:background \"Red\")'."
  2508. :group 'org-agenda-line-format
  2509. :type '(choice
  2510. (const :tag "Never" nil)
  2511. (const :tag "Defaults" t)
  2512. (repeat :tag "Specify"
  2513. (list (character :tag "Priority" :value ?A)
  2514. (sexp :tag "face")))))
  2515. (defgroup org-latex nil
  2516. "Options for embedding LaTeX code into Org-mode"
  2517. :tag "Org LaTeX"
  2518. :group 'org)
  2519. (defcustom org-format-latex-options
  2520. '(:foreground default :background default :scale 1.0
  2521. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2522. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2523. "Options for creating images from LaTeX fragments.
  2524. This is a property list with the following properties:
  2525. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2526. `default' means use the forground of the default face.
  2527. :background the background color, or \"Transparent\".
  2528. `default' means use the background of the default face.
  2529. :scale a scaling factor for the size of the images
  2530. :html-foreground, :html-background, :html-scale
  2531. The same numbers for HTML export.
  2532. :matchers a list indicating which matchers should be used to
  2533. find LaTeX fragments. Valid members of this list are:
  2534. \"begin\" find environments
  2535. \"$\" find math expressions surrounded by $...$
  2536. \"$$\" find math expressions surrounded by $$....$$
  2537. \"\\(\" find math expressions surrounded by \\(...\\)
  2538. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2539. :group 'org-latex
  2540. :type 'plist)
  2541. (defcustom org-format-latex-header "\\documentclass{article}
  2542. \\usepackage{fullpage} % do not remove
  2543. \\usepackage{amssymb}
  2544. \\usepackage[usenames]{color}
  2545. \\usepackage{amsmath}
  2546. \\usepackage{latexsym}
  2547. \\usepackage[mathscr]{eucal}
  2548. \\pagestyle{empty} % do not remove"
  2549. "The document header used for processing LaTeX fragments."
  2550. :group 'org-latex
  2551. :type 'string)
  2552. (defgroup org-export nil
  2553. "Options for exporting org-listings."
  2554. :tag "Org Export"
  2555. :group 'org)
  2556. (defgroup org-export-general nil
  2557. "General options for exporting Org-mode files."
  2558. :tag "Org Export General"
  2559. :group 'org-export)
  2560. ;; FIXME
  2561. (defvar org-export-publishing-directory nil)
  2562. (defcustom org-export-with-special-strings t
  2563. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2564. When this option is turned on, these strings will be exported as:
  2565. Org HTML LaTeX
  2566. -----+----------+--------
  2567. \\- &shy; \\-
  2568. -- &ndash; --
  2569. --- &mdash; ---
  2570. ... &hellip; \ldots
  2571. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2572. :group 'org-export-translation
  2573. :type 'boolean)
  2574. (defcustom org-export-language-setup
  2575. '(("en" "Author" "Date" "Table of Contents")
  2576. ("cs" "Autor" "Datum" "Obsah")
  2577. ("da" "Ophavsmand" "Dato" "Indhold")
  2578. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2579. ("es" "Autor" "Fecha" "\xcdndice")
  2580. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2581. ("it" "Autore" "Data" "Indice")
  2582. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2583. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2584. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2585. "Terms used in export text, translated to different languages.
  2586. Use the variable `org-export-default-language' to set the language,
  2587. or use the +OPTION lines for a per-file setting."
  2588. :group 'org-export-general
  2589. :type '(repeat
  2590. (list
  2591. (string :tag "HTML language tag")
  2592. (string :tag "Author")
  2593. (string :tag "Date")
  2594. (string :tag "Table of Contents"))))
  2595. (defcustom org-export-default-language "en"
  2596. "The default language of HTML export, as a string.
  2597. This should have an association in `org-export-language-setup'."
  2598. :group 'org-export-general
  2599. :type 'string)
  2600. (defcustom org-export-skip-text-before-1st-heading t
  2601. "Non-nil means, skip all text before the first headline when exporting.
  2602. When nil, that text is exported as well."
  2603. :group 'org-export-general
  2604. :type 'boolean)
  2605. (defcustom org-export-headline-levels 3
  2606. "The last level which is still exported as a headline.
  2607. Inferior levels will produce itemize lists when exported.
  2608. Note that a numeric prefix argument to an exporter function overrides
  2609. this setting.
  2610. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2611. :group 'org-export-general
  2612. :type 'number)
  2613. (defcustom org-export-with-section-numbers t
  2614. "Non-nil means, add section numbers to headlines when exporting.
  2615. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2616. :group 'org-export-general
  2617. :type 'boolean)
  2618. (defcustom org-export-with-toc t
  2619. "Non-nil means, create a table of contents in exported files.
  2620. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2621. When an integer, include levels up to N in the toc, this may then be
  2622. different from `org-export-headline-levels', but it will not be allowed
  2623. to be larger than the number of headline levels.
  2624. When nil, no table of contents is made.
  2625. Headlines which contain any TODO items will be marked with \"(*)\" in
  2626. ASCII export, and with red color in HTML output, if the option
  2627. `org-export-mark-todo-in-toc' is set.
  2628. In HTML output, the TOC will be clickable.
  2629. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2630. or \"toc:3\"."
  2631. :group 'org-export-general
  2632. :type '(choice
  2633. (const :tag "No Table of Contents" nil)
  2634. (const :tag "Full Table of Contents" t)
  2635. (integer :tag "TOC to level")))
  2636. (defcustom org-export-mark-todo-in-toc nil
  2637. "Non-nil means, mark TOC lines that contain any open TODO items."
  2638. :group 'org-export-general
  2639. :type 'boolean)
  2640. (defcustom org-export-preserve-breaks nil
  2641. "Non-nil means, preserve all line breaks when exporting.
  2642. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2643. export, line breaks will always be preserved, regardless of this variable.
  2644. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2645. :group 'org-export-general
  2646. :type 'boolean)
  2647. (defcustom org-export-with-archived-trees 'headline
  2648. "Whether subtrees with the ARCHIVE tag should be exported.
  2649. This can have three different values
  2650. nil Do not export, pretend this tree is not present
  2651. t Do export the entire tree
  2652. headline Only export the headline, but skip the tree below it."
  2653. :group 'org-export-general
  2654. :group 'org-archive
  2655. :type '(choice
  2656. (const :tag "not at all" nil)
  2657. (const :tag "headline only" 'headline)
  2658. (const :tag "entirely" t)))
  2659. (defcustom org-export-author-info t
  2660. "Non-nil means, insert author name and email into the exported file.
  2661. This option can also be set with the +OPTIONS line,
  2662. e.g. \"author-info:nil\"."
  2663. :group 'org-export-general
  2664. :type 'boolean)
  2665. (defcustom org-export-time-stamp-file t
  2666. "Non-nil means, insert a time stamp into the exported file.
  2667. The time stamp shows when the file was created.
  2668. This option can also be set with the +OPTIONS line,
  2669. e.g. \"timestamp:nil\"."
  2670. :group 'org-export-general
  2671. :type 'boolean)
  2672. (defcustom org-export-with-timestamps t
  2673. "If nil, do not export time stamps and associated keywords."
  2674. :group 'org-export-general
  2675. :type 'boolean)
  2676. (defcustom org-export-remove-timestamps-from-toc t
  2677. "If nil, remove timestamps from the table of contents entries."
  2678. :group 'org-export-general
  2679. :type 'boolean)
  2680. (defcustom org-export-with-tags 'not-in-toc
  2681. "If nil, do not export tags, just remove them from headlines.
  2682. If this is the symbol `not-in-toc', tags will be removed from table of
  2683. contents entries, but still be shown in the headlines of the document.
  2684. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2685. :group 'org-export-general
  2686. :type '(choice
  2687. (const :tag "Off" nil)
  2688. (const :tag "Not in TOC" not-in-toc)
  2689. (const :tag "On" t)))
  2690. (defcustom org-export-with-drawers nil
  2691. "Non-nil means, export with drawers like the property drawer.
  2692. When t, all drawers are exported. This may also be a list of
  2693. drawer names to export."
  2694. :group 'org-export-general
  2695. :type '(choice
  2696. (const :tag "All drawers" t)
  2697. (const :tag "None" nil)
  2698. (repeat :tag "Selected drawers"
  2699. (string :tag "Drawer name"))))
  2700. (defgroup org-export-translation nil
  2701. "Options for translating special ascii sequences for the export backends."
  2702. :tag "Org Export Translation"
  2703. :group 'org-export)
  2704. (defcustom org-export-with-emphasize t
  2705. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2706. If the export target supports emphasizing text, the word will be
  2707. typeset in bold, italic, or underlined, respectively. Works only for
  2708. single words, but you can say: I *really* *mean* *this*.
  2709. Not all export backends support this.
  2710. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2711. :group 'org-export-translation
  2712. :type 'boolean)
  2713. (defcustom org-export-with-footnotes t
  2714. "If nil, export [1] as a footnote marker.
  2715. Lines starting with [1] will be formatted as footnotes.
  2716. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2717. :group 'org-export-translation
  2718. :type 'boolean)
  2719. (defcustom org-export-with-sub-superscripts t
  2720. "Non-nil means, interpret \"_\" and \"^\" for export.
  2721. When this option is turned on, you can use TeX-like syntax for sub- and
  2722. superscripts. Several characters after \"_\" or \"^\" will be
  2723. considered as a single item - so grouping with {} is normally not
  2724. needed. For example, the following things will be parsed as single
  2725. sub- or superscripts.
  2726. 10^24 or 10^tau several digits will be considered 1 item.
  2727. 10^-12 or 10^-tau a leading sign with digits or a word
  2728. x^2-y^3 will be read as x^2 - y^3, because items are
  2729. terminated by almost any nonword/nondigit char.
  2730. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2731. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2732. sub/superscript. If you set this variable to the symbol `{}',
  2733. the braces are *required* in order to trigger interpretations as
  2734. sub/superscript. This can be helpful in documents that need \"_\"
  2735. frequently in plain text.
  2736. Not all export backends support this, but HTML does.
  2737. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2738. :group 'org-export-translation
  2739. :type '(choice
  2740. (const :tag "Always interpret" t)
  2741. (const :tag "Only with braces" {})
  2742. (const :tag "Never interpret" nil)))
  2743. (defcustom org-export-with-special-strings t
  2744. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2745. When this option is turned on, these strings will be exported as:
  2746. \\- : &shy;
  2747. -- : &ndash;
  2748. --- : &mdash;
  2749. Not all export backends support this, but HTML does.
  2750. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2751. :group 'org-export-translation
  2752. :type 'boolean)
  2753. (defcustom org-export-with-TeX-macros t
  2754. "Non-nil means, interpret simple TeX-like macros when exporting.
  2755. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2756. No only real TeX macros will work here, but the standard HTML entities
  2757. for math can be used as macro names as well. For a list of supported
  2758. names in HTML export, see the constant `org-html-entities'.
  2759. Not all export backends support this.
  2760. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2761. :group 'org-export-translation
  2762. :group 'org-export-latex
  2763. :type 'boolean)
  2764. (defcustom org-export-with-LaTeX-fragments nil
  2765. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2766. When set, the exporter will find LaTeX environments if the \\begin line is
  2767. the first non-white thing on a line. It will also find the math delimiters
  2768. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2769. display math.
  2770. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2771. :group 'org-export-translation
  2772. :group 'org-export-latex
  2773. :type 'boolean)
  2774. (defcustom org-export-with-fixed-width t
  2775. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2776. This can be used to have pre-formatted text, fragments of code etc. For
  2777. example:
  2778. : ;; Some Lisp examples
  2779. : (while (defc cnt)
  2780. : (ding))
  2781. will be looking just like this in also HTML. See also the QUOTE keyword.
  2782. Not all export backends support this.
  2783. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2784. :group 'org-export-translation
  2785. :type 'boolean)
  2786. (defcustom org-match-sexp-depth 3
  2787. "Number of stacked braces for sub/superscript matching.
  2788. This has to be set before loading org.el to be effective."
  2789. :group 'org-export-translation
  2790. :type 'integer)
  2791. (defgroup org-export-tables nil
  2792. "Options for exporting tables in Org-mode."
  2793. :tag "Org Export Tables"
  2794. :group 'org-export)
  2795. (defcustom org-export-with-tables t
  2796. "If non-nil, lines starting with \"|\" define a table.
  2797. For example:
  2798. | Name | Address | Birthday |
  2799. |-------------+----------+-----------|
  2800. | Arthur Dent | England | 29.2.2100 |
  2801. Not all export backends support this.
  2802. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2803. :group 'org-export-tables
  2804. :type 'boolean)
  2805. (defcustom org-export-highlight-first-table-line t
  2806. "Non-nil means, highlight the first table line.
  2807. In HTML export, this means use <th> instead of <td>.
  2808. In tables created with table.el, this applies to the first table line.
  2809. In Org-mode tables, all lines before the first horizontal separator
  2810. line will be formatted with <th> tags."
  2811. :group 'org-export-tables
  2812. :type 'boolean)
  2813. (defcustom org-export-table-remove-special-lines t
  2814. "Remove special lines and marking characters in calculating tables.
  2815. This removes the special marking character column from tables that are set
  2816. up for spreadsheet calculations. It also removes the entire lines
  2817. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2818. the values of constants may be useful to have."
  2819. :group 'org-export-tables
  2820. :type 'boolean)
  2821. (defcustom org-export-prefer-native-exporter-for-tables nil
  2822. "Non-nil means, always export tables created with table.el natively.
  2823. Natively means, use the HTML code generator in table.el.
  2824. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2825. the table does not use row- or column-spanning). This has the
  2826. advantage, that the automatic HTML conversions for math symbols and
  2827. sub/superscripts can be applied. Org-mode's HTML generator is also
  2828. much faster."
  2829. :group 'org-export-tables
  2830. :type 'boolean)
  2831. (defgroup org-export-ascii nil
  2832. "Options specific for ASCII export of Org-mode files."
  2833. :tag "Org Export ASCII"
  2834. :group 'org-export)
  2835. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2836. "Characters for underlining headings in ASCII export.
  2837. In the given sequence, these characters will be used for level 1, 2, ..."
  2838. :group 'org-export-ascii
  2839. :type '(repeat character))
  2840. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2841. "Bullet characters for headlines converted to lists in ASCII export.
  2842. The first character is is used for the first lest level generated in this
  2843. way, and so on. If there are more levels than characters given here,
  2844. the list will be repeated.
  2845. Note that plain lists will keep the same bullets as the have in the
  2846. Org-mode file."
  2847. :group 'org-export-ascii
  2848. :type '(repeat character))
  2849. (defgroup org-export-xml nil
  2850. "Options specific for XML export of Org-mode files."
  2851. :tag "Org Export XML"
  2852. :group 'org-export)
  2853. (defgroup org-export-html nil
  2854. "Options specific for HTML export of Org-mode files."
  2855. :tag "Org Export HTML"
  2856. :group 'org-export)
  2857. (defcustom org-export-html-coding-system nil
  2858. ""
  2859. :group 'org-export-html
  2860. :type 'coding-system)
  2861. (defcustom org-export-html-extension "html"
  2862. "The extension for exported HTML files."
  2863. :group 'org-export-html
  2864. :type 'string)
  2865. (defcustom org-export-html-style
  2866. "<style type=\"text/css\">
  2867. html {
  2868. font-family: Times, serif;
  2869. font-size: 12pt;
  2870. }
  2871. .title { text-align: center; }
  2872. .todo { color: red; }
  2873. .done { color: green; }
  2874. .timestamp { color: grey }
  2875. .timestamp-kwd { color: CadetBlue }
  2876. .tag { background-color:lightblue; font-weight:normal }
  2877. .target { background-color: lavender; }
  2878. pre {
  2879. border: 1pt solid #AEBDCC;
  2880. background-color: #F3F5F7;
  2881. padding: 5pt;
  2882. font-family: courier, monospace;
  2883. }
  2884. table { border-collapse: collapse; }
  2885. td, th {
  2886. vertical-align: top;
  2887. <!--border: 1pt solid #ADB9CC;-->
  2888. }
  2889. </style>"
  2890. "The default style specification for exported HTML files.
  2891. Since there are different ways of setting style information, this variable
  2892. needs to contain the full HTML structure to provide a style, including the
  2893. surrounding HTML tags. The style specifications should include definitions
  2894. for new classes todo, done, title, and deadline. For example, legal values
  2895. would be:
  2896. <style type=\"text/css\">
  2897. p { font-weight: normal; color: gray; }
  2898. h1 { color: black; }
  2899. .title { text-align: center; }
  2900. .todo, .deadline { color: red; }
  2901. .done { color: green; }
  2902. </style>
  2903. or, if you want to keep the style in a file,
  2904. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2905. As the value of this option simply gets inserted into the HTML <head> header,
  2906. you can \"misuse\" it to add arbitrary text to the header."
  2907. :group 'org-export-html
  2908. :type 'string)
  2909. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2910. "Format for typesetting the document title in HTML export."
  2911. :group 'org-export-html
  2912. :type 'string)
  2913. (defcustom org-export-html-toplevel-hlevel 2
  2914. "The <H> level for level 1 headings in HTML export."
  2915. :group 'org-export-html
  2916. :type 'string)
  2917. (defcustom org-export-html-link-org-files-as-html t
  2918. "Non-nil means, make file links to `file.org' point to `file.html'.
  2919. When org-mode is exporting an org-mode file to HTML, links to
  2920. non-html files are directly put into a href tag in HTML.
  2921. However, links to other Org-mode files (recognized by the
  2922. extension `.org.) should become links to the corresponding html
  2923. file, assuming that the linked org-mode file will also be
  2924. converted to HTML.
  2925. When nil, the links still point to the plain `.org' file."
  2926. :group 'org-export-html
  2927. :type 'boolean)
  2928. (defcustom org-export-html-inline-images 'maybe
  2929. "Non-nil means, inline images into exported HTML pages.
  2930. This is done using an <img> tag. When nil, an anchor with href is used to
  2931. link to the image. If this option is `maybe', then images in links with
  2932. an empty description will be inlined, while images with a description will
  2933. be linked only."
  2934. :group 'org-export-html
  2935. :type '(choice (const :tag "Never" nil)
  2936. (const :tag "Always" t)
  2937. (const :tag "When there is no description" maybe)))
  2938. ;; FIXME: rename
  2939. (defcustom org-export-html-expand t
  2940. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2941. When nil, these tags will be exported as plain text and therefore
  2942. not be interpreted by a browser.
  2943. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2944. :group 'org-export-html
  2945. :type 'boolean)
  2946. (defcustom org-export-html-table-tag
  2947. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2948. "The HTML tag that is used to start a table.
  2949. This must be a <table> tag, but you may change the options like
  2950. borders and spacing."
  2951. :group 'org-export-html
  2952. :type 'string)
  2953. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2954. "The opening tag for table header fields.
  2955. This is customizable so that alignment options can be specified."
  2956. :group 'org-export-tables
  2957. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2958. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2959. "The opening tag for table data fields.
  2960. This is customizable so that alignment options can be specified."
  2961. :group 'org-export-tables
  2962. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2963. (defcustom org-export-html-with-timestamp nil
  2964. "If non-nil, write `org-export-html-html-helper-timestamp'
  2965. into the exported HTML text. Otherwise, the buffer will just be saved
  2966. to a file."
  2967. :group 'org-export-html
  2968. :type 'boolean)
  2969. (defcustom org-export-html-html-helper-timestamp
  2970. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2971. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2972. :group 'org-export-html
  2973. :type 'string)
  2974. (defgroup org-export-icalendar nil
  2975. "Options specific for iCalendar export of Org-mode files."
  2976. :tag "Org Export iCalendar"
  2977. :group 'org-export)
  2978. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2979. "The file name for the iCalendar file covering all agenda files.
  2980. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2981. The file name should be absolute, the file will be overwritten without warning."
  2982. :group 'org-export-icalendar
  2983. :type 'file)
  2984. (defcustom org-icalendar-include-todo nil
  2985. "Non-nil means, export to iCalendar files should also cover TODO items."
  2986. :group 'org-export-icalendar
  2987. :type '(choice
  2988. (const :tag "None" nil)
  2989. (const :tag "Unfinished" t)
  2990. (const :tag "All" all)))
  2991. (defcustom org-icalendar-include-sexps t
  2992. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2993. These are entries like in the diary, but directly in an Org-mode file."
  2994. :group 'org-export-icalendar
  2995. :type 'boolean)
  2996. (defcustom org-icalendar-include-body 100
  2997. "Amount of text below headline to be included in iCalendar export.
  2998. This is a number of characters that should maximally be included.
  2999. Properties, scheduling and clocking lines will always be removed.
  3000. The text will be inserted into the DESCRIPTION field."
  3001. :group 'org-export-icalendar
  3002. :type '(choice
  3003. (const :tag "Nothing" nil)
  3004. (const :tag "Everything" t)
  3005. (integer :tag "Max characters")))
  3006. (defcustom org-icalendar-combined-name "OrgMode"
  3007. "Calendar name for the combined iCalendar representing all agenda files."
  3008. :group 'org-export-icalendar
  3009. :type 'string)
  3010. (defgroup org-font-lock nil
  3011. "Font-lock settings for highlighting in Org-mode."
  3012. :tag "Org Font Lock"
  3013. :group 'org)
  3014. (defcustom org-level-color-stars-only nil
  3015. "Non-nil means fontify only the stars in each headline.
  3016. When nil, the entire headline is fontified.
  3017. Changing it requires restart of `font-lock-mode' to become effective
  3018. also in regions already fontified."
  3019. :group 'org-font-lock
  3020. :type 'boolean)
  3021. (defcustom org-hide-leading-stars nil
  3022. "Non-nil means, hide the first N-1 stars in a headline.
  3023. This works by using the face `org-hide' for these stars. This
  3024. face is white for a light background, and black for a dark
  3025. background. You may have to customize the face `org-hide' to
  3026. make this work.
  3027. Changing it requires restart of `font-lock-mode' to become effective
  3028. also in regions already fontified.
  3029. You may also set this on a per-file basis by adding one of the following
  3030. lines to the buffer:
  3031. #+STARTUP: hidestars
  3032. #+STARTUP: showstars"
  3033. :group 'org-font-lock
  3034. :type 'boolean)
  3035. (defcustom org-fontify-done-headline nil
  3036. "Non-nil means, change the face of a headline if it is marked DONE.
  3037. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3038. When this is non-nil, the headline after the keyword is set to the
  3039. `org-headline-done' as an additional indication."
  3040. :group 'org-font-lock
  3041. :type 'boolean)
  3042. (defcustom org-fontify-emphasized-text t
  3043. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3044. Changing this variable requires a restart of Emacs to take effect."
  3045. :group 'org-font-lock
  3046. :type 'boolean)
  3047. (defcustom org-highlight-latex-fragments-and-specials nil
  3048. "Non-nil means, fontify what is treated specially by the exporters."
  3049. :group 'org-font-lock
  3050. :type 'boolean)
  3051. (defcustom org-hide-emphasis-markers nil
  3052. "Non-nil mean font-lock should hide the emphasis marker characters."
  3053. :group 'org-font-lock
  3054. :type 'boolean)
  3055. (defvar org-emph-re nil
  3056. "Regular expression for matching emphasis.")
  3057. (defvar org-verbatim-re nil
  3058. "Regular expression for matching verbatim text.")
  3059. (defvar org-emphasis-regexp-components) ; defined just below
  3060. (defvar org-emphasis-alist) ; defined just below
  3061. (defun org-set-emph-re (var val)
  3062. "Set variable and compute the emphasis regular expression."
  3063. (set var val)
  3064. (when (and (boundp 'org-emphasis-alist)
  3065. (boundp 'org-emphasis-regexp-components)
  3066. org-emphasis-alist org-emphasis-regexp-components)
  3067. (let* ((e org-emphasis-regexp-components)
  3068. (pre (car e))
  3069. (post (nth 1 e))
  3070. (border (nth 2 e))
  3071. (body (nth 3 e))
  3072. (nl (nth 4 e))
  3073. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3074. (body1 (concat body "*?"))
  3075. (markers (mapconcat 'car org-emphasis-alist ""))
  3076. (vmarkers (mapconcat
  3077. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3078. org-emphasis-alist "")))
  3079. ;; make sure special characters appear at the right position in the class
  3080. (if (string-match "\\^" markers)
  3081. (setq markers (concat (replace-match "" t t markers) "^")))
  3082. (if (string-match "-" markers)
  3083. (setq markers (concat (replace-match "" t t markers) "-")))
  3084. (if (string-match "\\^" vmarkers)
  3085. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3086. (if (string-match "-" vmarkers)
  3087. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3088. (if (> nl 0)
  3089. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3090. (int-to-string nl) "\\}")))
  3091. ;; Make the regexp
  3092. (setq org-emph-re
  3093. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3094. "\\("
  3095. "\\([" markers "]\\)"
  3096. "\\("
  3097. "[^" border "]\\|"
  3098. "[^" border (if (and nil stacked) markers) "]"
  3099. body1
  3100. "[^" border (if (and nil stacked) markers) "]"
  3101. "\\)"
  3102. "\\3\\)"
  3103. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3104. (setq org-verbatim-re
  3105. (concat "\\([" pre "]\\|^\\)"
  3106. "\\("
  3107. "\\([" vmarkers "]\\)"
  3108. "\\("
  3109. "[^" border "]\\|"
  3110. "[^" border "]"
  3111. body1
  3112. "[^" border "]"
  3113. "\\)"
  3114. "\\3\\)"
  3115. "\\([" post "]\\|$\\)")))))
  3116. (defcustom org-emphasis-regexp-components
  3117. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3118. "Components used to build the regular expression for emphasis.
  3119. This is a list with 6 entries. Terminology: In an emphasis string
  3120. like \" *strong word* \", we call the initial space PREMATCH, the final
  3121. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3122. and \"trong wor\" is the body. The different components in this variable
  3123. specify what is allowed/forbidden in each part:
  3124. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3125. post Chars allowed as postmatch. End of line will be allowed too.
  3126. border The chars *forbidden* as border characters.
  3127. body-regexp A regexp like \".\" to match a body character. Don't use
  3128. non-shy groups here, and don't allow newline here.
  3129. newline The maximum number of newlines allowed in an emphasis exp.
  3130. Use customize to modify this, or restart Emacs after changing it."
  3131. :group 'org-font-lock
  3132. :set 'org-set-emph-re
  3133. :type '(list
  3134. (sexp :tag "Allowed chars in pre ")
  3135. (sexp :tag "Allowed chars in post ")
  3136. (sexp :tag "Forbidden chars in border ")
  3137. (sexp :tag "Regexp for body ")
  3138. (integer :tag "number of newlines allowed")
  3139. (option (boolean :tag "Stacking (DISABLED) "))))
  3140. (defcustom org-emphasis-alist
  3141. '(("*" bold "<b>" "</b>")
  3142. ("/" italic "<i>" "</i>")
  3143. ("_" underline "<u>" "</u>")
  3144. ("=" org-code "<code>" "</code>" verbatim)
  3145. ("~" org-verbatim "" "" verbatim)
  3146. ("+" (:strike-through t) "<del>" "</del>")
  3147. )
  3148. "Special syntax for emphasized text.
  3149. Text starting and ending with a special character will be emphasized, for
  3150. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3151. characters, the face to be used by font-lock for highlighting in Org-mode
  3152. Emacs buffers, and the HTML tags to be used for this.
  3153. Use customize to modify this, or restart Emacs after changing it."
  3154. :group 'org-font-lock
  3155. :set 'org-set-emph-re
  3156. :type '(repeat
  3157. (list
  3158. (string :tag "Marker character")
  3159. (choice
  3160. (face :tag "Font-lock-face")
  3161. (plist :tag "Face property list"))
  3162. (string :tag "HTML start tag")
  3163. (string :tag "HTML end tag")
  3164. (option (const verbatim)))))
  3165. ;;; The faces
  3166. (defgroup org-faces nil
  3167. "Faces in Org-mode."
  3168. :tag "Org Faces"
  3169. :group 'org-font-lock)
  3170. (defun org-compatible-face (inherits specs)
  3171. "Make a compatible face specification.
  3172. If INHERITS is an existing face and if the Emacs version supports it,
  3173. just inherit the face. If not, use SPECS to define the face.
  3174. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3175. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3176. to the top of the list. The `min-colors' attribute will be removed from
  3177. any other entries, and any resulting duplicates will be removed entirely."
  3178. (cond
  3179. ((and inherits (facep inherits)
  3180. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3181. ;; In Emacs 23, we use inheritance where possible.
  3182. ;; We only do this in Emacs 23, because only there the outline
  3183. ;; faces have been changed to the original org-mode-level-faces.
  3184. (list (list t :inherit inherits)))
  3185. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3186. ;; These do not understand the `min-colors' attribute.
  3187. (let (r e a)
  3188. (while (setq e (pop specs))
  3189. (cond
  3190. ((memq (car e) '(t default)) (push e r))
  3191. ((setq a (member '(min-colors 8) (car e)))
  3192. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3193. (cdr e)))))
  3194. ((setq a (assq 'min-colors (car e)))
  3195. (setq e (cons (delq a (car e)) (cdr e)))
  3196. (or (assoc (car e) r) (push e r)))
  3197. (t (or (assoc (car e) r) (push e r)))))
  3198. (nreverse r)))
  3199. (t specs)))
  3200. (put 'org-compatible-face 'lisp-indent-function 1)
  3201. (defface org-hide
  3202. '((((background light)) (:foreground "white"))
  3203. (((background dark)) (:foreground "black")))
  3204. "Face used to hide leading stars in headlines.
  3205. The forground color of this face should be equal to the background
  3206. color of the frame."
  3207. :group 'org-faces)
  3208. (defface org-level-1 ;; font-lock-function-name-face
  3209. (org-compatible-face 'outline-1
  3210. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3211. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3212. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3213. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3214. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3215. (t (:bold t))))
  3216. "Face used for level 1 headlines."
  3217. :group 'org-faces)
  3218. (defface org-level-2 ;; font-lock-variable-name-face
  3219. (org-compatible-face 'outline-2
  3220. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3221. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3222. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3223. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3224. (t (:bold t))))
  3225. "Face used for level 2 headlines."
  3226. :group 'org-faces)
  3227. (defface org-level-3 ;; font-lock-keyword-face
  3228. (org-compatible-face 'outline-3
  3229. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3230. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3231. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3232. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3233. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3234. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3235. (t (:bold t))))
  3236. "Face used for level 3 headlines."
  3237. :group 'org-faces)
  3238. (defface org-level-4 ;; font-lock-comment-face
  3239. (org-compatible-face 'outline-4
  3240. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3241. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3242. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3243. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3244. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3245. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3246. (t (:bold t))))
  3247. "Face used for level 4 headlines."
  3248. :group 'org-faces)
  3249. (defface org-level-5 ;; font-lock-type-face
  3250. (org-compatible-face 'outline-5
  3251. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3252. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3253. (((class color) (min-colors 8)) (:foreground "green"))))
  3254. "Face used for level 5 headlines."
  3255. :group 'org-faces)
  3256. (defface org-level-6 ;; font-lock-constant-face
  3257. (org-compatible-face 'outline-6
  3258. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3259. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3260. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3261. "Face used for level 6 headlines."
  3262. :group 'org-faces)
  3263. (defface org-level-7 ;; font-lock-builtin-face
  3264. (org-compatible-face 'outline-7
  3265. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3266. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3267. (((class color) (min-colors 8)) (:foreground "blue"))))
  3268. "Face used for level 7 headlines."
  3269. :group 'org-faces)
  3270. (defface org-level-8 ;; font-lock-string-face
  3271. (org-compatible-face 'outline-8
  3272. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3273. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3274. (((class color) (min-colors 8)) (:foreground "green"))))
  3275. "Face used for level 8 headlines."
  3276. :group 'org-faces)
  3277. (defface org-special-keyword ;; font-lock-string-face
  3278. (org-compatible-face nil
  3279. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3280. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3281. (t (:italic t))))
  3282. "Face used for special keywords."
  3283. :group 'org-faces)
  3284. (defface org-drawer ;; font-lock-function-name-face
  3285. (org-compatible-face nil
  3286. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3287. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3288. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3289. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3290. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3291. (t (:bold t))))
  3292. "Face used for drawers."
  3293. :group 'org-faces)
  3294. (defface org-property-value nil
  3295. "Face used for the value of a property."
  3296. :group 'org-faces)
  3297. (defface org-column
  3298. (org-compatible-face nil
  3299. '((((class color) (min-colors 16) (background light))
  3300. (:background "grey90"))
  3301. (((class color) (min-colors 16) (background dark))
  3302. (:background "grey30"))
  3303. (((class color) (min-colors 8))
  3304. (:background "cyan" :foreground "black"))
  3305. (t (:inverse-video t))))
  3306. "Face for column display of entry properties."
  3307. :group 'org-faces)
  3308. (when (fboundp 'set-face-attribute)
  3309. ;; Make sure that a fixed-width face is used when we have a column table.
  3310. (set-face-attribute 'org-column nil
  3311. :height (face-attribute 'default :height)
  3312. :family (face-attribute 'default :family)))
  3313. (defface org-warning
  3314. (org-compatible-face 'font-lock-warning-face
  3315. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3316. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3317. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3318. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3319. (t (:bold t))))
  3320. "Face for deadlines and TODO keywords."
  3321. :group 'org-faces)
  3322. (defface org-archived ; similar to shadow
  3323. (org-compatible-face 'shadow
  3324. '((((class color grayscale) (min-colors 88) (background light))
  3325. (:foreground "grey50"))
  3326. (((class color grayscale) (min-colors 88) (background dark))
  3327. (:foreground "grey70"))
  3328. (((class color) (min-colors 8) (background light))
  3329. (:foreground "green"))
  3330. (((class color) (min-colors 8) (background dark))
  3331. (:foreground "yellow"))))
  3332. "Face for headline with the ARCHIVE tag."
  3333. :group 'org-faces)
  3334. (defface org-link
  3335. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3336. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3337. (t (:underline t)))
  3338. "Face for links."
  3339. :group 'org-faces)
  3340. (defface org-ellipsis
  3341. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3342. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3343. (t (:strike-through t)))
  3344. "Face for the ellipsis in folded text."
  3345. :group 'org-faces)
  3346. (defface org-target
  3347. '((((class color) (background light)) (:underline t))
  3348. (((class color) (background dark)) (:underline t))
  3349. (t (:underline t)))
  3350. "Face for links."
  3351. :group 'org-faces)
  3352. (defface org-date
  3353. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3354. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3355. (t (:underline t)))
  3356. "Face for links."
  3357. :group 'org-faces)
  3358. (defface org-sexp-date
  3359. '((((class color) (background light)) (:foreground "Purple"))
  3360. (((class color) (background dark)) (:foreground "Cyan"))
  3361. (t (:underline t)))
  3362. "Face for links."
  3363. :group 'org-faces)
  3364. (defface org-tag
  3365. '((t (:bold t)))
  3366. "Face for tags."
  3367. :group 'org-faces)
  3368. (defface org-todo ; font-lock-warning-face
  3369. (org-compatible-face nil
  3370. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3371. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3372. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3373. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3374. (t (:inverse-video t :bold t))))
  3375. "Face for TODO keywords."
  3376. :group 'org-faces)
  3377. (defface org-done ;; font-lock-type-face
  3378. (org-compatible-face nil
  3379. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3380. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3381. (((class color) (min-colors 8)) (:foreground "green"))
  3382. (t (:bold t))))
  3383. "Face used for todo keywords that indicate DONE items."
  3384. :group 'org-faces)
  3385. (defface org-headline-done ;; font-lock-string-face
  3386. (org-compatible-face nil
  3387. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3388. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3389. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3390. "Face used to indicate that a headline is DONE.
  3391. This face is only used if `org-fontify-done-headline' is set. If applies
  3392. to the part of the headline after the DONE keyword."
  3393. :group 'org-faces)
  3394. (defcustom org-todo-keyword-faces nil
  3395. "Faces for specific TODO keywords.
  3396. This is a list of cons cells, with TODO keywords in the car
  3397. and faces in the cdr. The face can be a symbol, or a property
  3398. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3399. :group 'org-faces
  3400. :group 'org-todo
  3401. :type '(repeat
  3402. (cons
  3403. (string :tag "keyword")
  3404. (sexp :tag "face"))))
  3405. (defface org-table ;; font-lock-function-name-face
  3406. (org-compatible-face nil
  3407. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3408. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3409. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3410. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3411. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3412. (((class color) (min-colors 8) (background dark)))))
  3413. "Face used for tables."
  3414. :group 'org-faces)
  3415. (defface org-formula
  3416. (org-compatible-face nil
  3417. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3418. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3419. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3420. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3421. (t (:bold t :italic t))))
  3422. "Face for formulas."
  3423. :group 'org-faces)
  3424. (defface org-code
  3425. (org-compatible-face nil
  3426. '((((class color grayscale) (min-colors 88) (background light))
  3427. (:foreground "grey50"))
  3428. (((class color grayscale) (min-colors 88) (background dark))
  3429. (:foreground "grey70"))
  3430. (((class color) (min-colors 8) (background light))
  3431. (:foreground "green"))
  3432. (((class color) (min-colors 8) (background dark))
  3433. (:foreground "yellow"))))
  3434. "Face for fixed-with text like code snippets."
  3435. :group 'org-faces
  3436. :version "22.1")
  3437. (defface org-verbatim
  3438. (org-compatible-face nil
  3439. '((((class color grayscale) (min-colors 88) (background light))
  3440. (:foreground "grey50" :underline t))
  3441. (((class color grayscale) (min-colors 88) (background dark))
  3442. (:foreground "grey70" :underline t))
  3443. (((class color) (min-colors 8) (background light))
  3444. (:foreground "green" :underline t))
  3445. (((class color) (min-colors 8) (background dark))
  3446. (:foreground "yellow" :underline t))))
  3447. "Face for fixed-with text like code snippets."
  3448. :group 'org-faces
  3449. :version "22.1")
  3450. (defface org-agenda-structure ;; font-lock-function-name-face
  3451. (org-compatible-face nil
  3452. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3453. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3454. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3455. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3456. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3457. (t (:bold t))))
  3458. "Face used in agenda for captions and dates."
  3459. :group 'org-faces)
  3460. (defface org-scheduled-today
  3461. (org-compatible-face nil
  3462. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3463. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3464. (((class color) (min-colors 8)) (:foreground "green"))
  3465. (t (:bold t :italic t))))
  3466. "Face for items scheduled for a certain day."
  3467. :group 'org-faces)
  3468. (defface org-scheduled-previously
  3469. (org-compatible-face nil
  3470. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3471. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3472. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3473. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3474. (t (:bold t))))
  3475. "Face for items scheduled previously, and not yet done."
  3476. :group 'org-faces)
  3477. (defface org-upcoming-deadline
  3478. (org-compatible-face nil
  3479. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3480. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3481. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3482. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3483. (t (:bold t))))
  3484. "Face for items scheduled previously, and not yet done."
  3485. :group 'org-faces)
  3486. (defcustom org-agenda-deadline-faces
  3487. '((1.0 . org-warning)
  3488. (0.5 . org-upcoming-deadline)
  3489. (0.0 . default))
  3490. "Faces for showing deadlines in the agenda.
  3491. This is a list of cons cells. The cdr of each cess is a face to be used,
  3492. and it can also just be a like like '(:foreground \"yellow\").
  3493. Each car is a fraction of the head-warning time that must have passed for
  3494. this the face in the cdr to be used for display. The numbers must be
  3495. given in descending order. The head-warning time is normally taken
  3496. from `org-deadline-warning-days', but can also be specified in the deadline
  3497. timestamp itself, like this:
  3498. DEADLINE: <2007-08-13 Mon -8d>
  3499. You may use d for days, w for weeks, m for months and y for years. Months
  3500. and years will only be treated in an approximate fashion (30.4 days for a
  3501. month and 365.24 days for a year)."
  3502. :group 'org-faces
  3503. :group 'org-agenda-daily/weekly
  3504. :type '(repeat
  3505. (cons
  3506. (number :tag "Fraction of head-warning time passed")
  3507. (sexp :tag "Face"))))
  3508. ;; FIXME: this is not a good face yet.
  3509. (defface org-agenda-restriction-lock
  3510. (org-compatible-face nil
  3511. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3512. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3513. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3514. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3515. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3516. (t (:inverse-video t))))
  3517. "Face for showing the agenda restriction lock."
  3518. :group 'org-faces)
  3519. (defface org-time-grid ;; font-lock-variable-name-face
  3520. (org-compatible-face nil
  3521. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3522. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3523. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3524. "Face used for time grids."
  3525. :group 'org-faces)
  3526. (defconst org-level-faces
  3527. '(org-level-1 org-level-2 org-level-3 org-level-4
  3528. org-level-5 org-level-6 org-level-7 org-level-8
  3529. ))
  3530. (defcustom org-n-level-faces (length org-level-faces)
  3531. "The number different faces to be used for headlines.
  3532. Org-mode defines 8 different headline faces, so this can be at most 8.
  3533. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3534. :type 'number
  3535. :group 'org-faces)
  3536. ;;; Functions and variables from ther packages
  3537. ;; Declared here to avoid compiler warnings
  3538. (unless (fboundp 'declare-function) (defmacro declare-function (&rest args)))
  3539. ;; XEmacs only
  3540. (defvar outline-mode-menu-heading)
  3541. (defvar outline-mode-menu-show)
  3542. (defvar outline-mode-menu-hide)
  3543. (defvar zmacs-regions) ; XEmacs regions
  3544. ;; Emacs only
  3545. (defvar mark-active)
  3546. ;; Various packages
  3547. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3548. (declare-function add-to-diary-list "diary-lib"
  3549. (date string specifier &optional marker globcolor literal))
  3550. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3551. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3552. (declare-function Info-goto-node "info" (nodename &optional fork))
  3553. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3554. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3555. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3556. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3557. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3558. (declare-function bbdb-record-name "ext:bbdb" (record))
  3559. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3560. (declare-function bibtex-generate-autokey "bibtex" ())
  3561. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3562. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3563. (defvar calc-embedded-close-formula)
  3564. (defvar calc-embedded-open-formula)
  3565. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3566. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3567. (declare-function calendar-check-holidays "holidays" (date))
  3568. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3569. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3570. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3571. (declare-function calendar-forward-day "cal-move" (arg))
  3572. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3573. (declare-function calendar-goto-date "cal-move" (date))
  3574. (declare-function calendar-goto-today "cal-move" ())
  3575. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3576. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3577. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3578. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3579. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3580. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3581. (defvar calendar-mode-map)
  3582. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3583. (declare-function cdlatex-tab "ext:cdlatex" ())
  3584. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3585. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3586. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3587. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3588. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3589. (defvar font-lock-unfontify-region-function)
  3590. (declare-function gnus-article-show-summary "gnus-art" ())
  3591. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3592. (defvar gnus-other-frame-object)
  3593. (defvar gnus-group-name)
  3594. (defvar gnus-article-current)
  3595. (defvar Info-current-file)
  3596. (defvar Info-current-node)
  3597. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3598. (declare-function mh-find-path "mh-utils" ())
  3599. (declare-function mh-get-header-field "mh-utils" (field))
  3600. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3601. (declare-function mh-header-display "mh-show" ())
  3602. (declare-function mh-index-previous-folder "mh-search" ())
  3603. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3604. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3605. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3606. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3607. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3608. (declare-function mh-show-header-display "mh-show" t t)
  3609. (declare-function mh-show-msg "mh-show" (msg))
  3610. (declare-function mh-show-show "mh-show" t t)
  3611. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3612. (defvar mh-progs)
  3613. (defvar mh-current-folder)
  3614. (defvar mh-show-folder-buffer)
  3615. (defvar mh-index-folder)
  3616. (defvar mh-searcher)
  3617. (declare-function org-export-latex-cleaned-string "org-export-latex" (&optional commentsp))
  3618. (declare-function parse-time-string "parse-time" (string))
  3619. (declare-function remember "remember" (&optional initial))
  3620. (declare-function remember-buffer-desc "remember" ())
  3621. (defvar remember-save-after-remembering)
  3622. (defvar remember-data-file)
  3623. (defvar remember-register)
  3624. (defvar remember-buffer)
  3625. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3626. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3627. (declare-function rmail-what-message "rmail" ())
  3628. (defvar texmathp-why)
  3629. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3630. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3631. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3632. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3633. (declare-function vm-isearch-update "ext:vm-search" ())
  3634. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3635. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3636. (declare-function vm-su-subject "ext:vm-summary" (m))
  3637. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3638. (defvar vm-message-pointer)
  3639. (defvar vm-folder-directory)
  3640. (defvar w3m-current-url)
  3641. (defvar w3m-current-title)
  3642. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3643. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3644. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3645. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3646. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3647. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3648. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3649. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3650. (defvar wl-summary-buffer-elmo-folder)
  3651. (defvar wl-summary-buffer-folder-name)
  3652. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3653. (defvar org-latex-regexps)
  3654. (defvar constants-unit-system)
  3655. ;;; Variables for pre-computed regular expressions, all buffer local
  3656. (defvar org-drawer-regexp nil
  3657. "Matches first line of a hidden block.")
  3658. (make-variable-buffer-local 'org-drawer-regexp)
  3659. (defvar org-todo-regexp nil
  3660. "Matches any of the TODO state keywords.")
  3661. (make-variable-buffer-local 'org-todo-regexp)
  3662. (defvar org-not-done-regexp nil
  3663. "Matches any of the TODO state keywords except the last one.")
  3664. (make-variable-buffer-local 'org-not-done-regexp)
  3665. (defvar org-todo-line-regexp nil
  3666. "Matches a headline and puts TODO state into group 2 if present.")
  3667. (make-variable-buffer-local 'org-todo-line-regexp)
  3668. (defvar org-complex-heading-regexp nil
  3669. "Matches a headline and puts everything into groups:
  3670. group 1: the stars
  3671. group 2: The todo keyword, maybe
  3672. group 3: Priority cookie
  3673. group 4: True headline
  3674. group 5: Tags")
  3675. (make-variable-buffer-local 'org-complex-heading-regexp)
  3676. (defvar org-todo-line-tags-regexp nil
  3677. "Matches a headline and puts TODO state into group 2 if present.
  3678. Also put tags into group 4 if tags are present.")
  3679. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3680. (defvar org-nl-done-regexp nil
  3681. "Matches newline followed by a headline with the DONE keyword.")
  3682. (make-variable-buffer-local 'org-nl-done-regexp)
  3683. (defvar org-looking-at-done-regexp nil
  3684. "Matches the DONE keyword a point.")
  3685. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3686. (defvar org-ds-keyword-length 12
  3687. "Maximum length of the Deadline and SCHEDULED keywords.")
  3688. (make-variable-buffer-local 'org-ds-keyword-length)
  3689. (defvar org-deadline-regexp nil
  3690. "Matches the DEADLINE keyword.")
  3691. (make-variable-buffer-local 'org-deadline-regexp)
  3692. (defvar org-deadline-time-regexp nil
  3693. "Matches the DEADLINE keyword together with a time stamp.")
  3694. (make-variable-buffer-local 'org-deadline-time-regexp)
  3695. (defvar org-deadline-line-regexp nil
  3696. "Matches the DEADLINE keyword and the rest of the line.")
  3697. (make-variable-buffer-local 'org-deadline-line-regexp)
  3698. (defvar org-scheduled-regexp nil
  3699. "Matches the SCHEDULED keyword.")
  3700. (make-variable-buffer-local 'org-scheduled-regexp)
  3701. (defvar org-scheduled-time-regexp nil
  3702. "Matches the SCHEDULED keyword together with a time stamp.")
  3703. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3704. (defvar org-closed-time-regexp nil
  3705. "Matches the CLOSED keyword together with a time stamp.")
  3706. (make-variable-buffer-local 'org-closed-time-regexp)
  3707. (defvar org-keyword-time-regexp nil
  3708. "Matches any of the 4 keywords, together with the time stamp.")
  3709. (make-variable-buffer-local 'org-keyword-time-regexp)
  3710. (defvar org-keyword-time-not-clock-regexp nil
  3711. "Matches any of the 3 keywords, together with the time stamp.")
  3712. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3713. (defvar org-maybe-keyword-time-regexp nil
  3714. "Matches a timestamp, possibly preceeded by a keyword.")
  3715. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3716. (defvar org-planning-or-clock-line-re nil
  3717. "Matches a line with planning or clock info.")
  3718. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3719. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3720. rear-nonsticky t mouse-map t fontified t)
  3721. "Properties to remove when a string without properties is wanted.")
  3722. (defsubst org-match-string-no-properties (num &optional string)
  3723. (if (featurep 'xemacs)
  3724. (let ((s (match-string num string)))
  3725. (remove-text-properties 0 (length s) org-rm-props s)
  3726. s)
  3727. (match-string-no-properties num string)))
  3728. (defsubst org-no-properties (s)
  3729. (if (fboundp 'set-text-properties)
  3730. (set-text-properties 0 (length s) nil s)
  3731. (remove-text-properties 0 (length s) org-rm-props s))
  3732. s)
  3733. (defsubst org-get-alist-option (option key)
  3734. (cond ((eq key t) t)
  3735. ((eq option t) t)
  3736. ((assoc key option) (cdr (assoc key option)))
  3737. (t (cdr (assq 'default option)))))
  3738. (defsubst org-inhibit-invisibility ()
  3739. "Modified `buffer-invisibility-spec' for Emacs 21.
  3740. Some ops with invisible text do not work correctly on Emacs 21. For these
  3741. we turn off invisibility temporarily. Use this in a `let' form."
  3742. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3743. (defsubst org-set-local (var value)
  3744. "Make VAR local in current buffer and set it to VALUE."
  3745. (set (make-variable-buffer-local var) value))
  3746. (defsubst org-mode-p ()
  3747. "Check if the current buffer is in Org-mode."
  3748. (eq major-mode 'org-mode))
  3749. (defsubst org-last (list)
  3750. "Return the last element of LIST."
  3751. (car (last list)))
  3752. (defun org-let (list &rest body)
  3753. (eval (cons 'let (cons list body))))
  3754. (put 'org-let 'lisp-indent-function 1)
  3755. (defun org-let2 (list1 list2 &rest body)
  3756. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3757. (put 'org-let2 'lisp-indent-function 2)
  3758. (defconst org-startup-options
  3759. '(("fold" org-startup-folded t)
  3760. ("overview" org-startup-folded t)
  3761. ("nofold" org-startup-folded nil)
  3762. ("showall" org-startup-folded nil)
  3763. ("content" org-startup-folded content)
  3764. ("hidestars" org-hide-leading-stars t)
  3765. ("showstars" org-hide-leading-stars nil)
  3766. ("odd" org-odd-levels-only t)
  3767. ("oddeven" org-odd-levels-only nil)
  3768. ("align" org-startup-align-all-tables t)
  3769. ("noalign" org-startup-align-all-tables nil)
  3770. ("customtime" org-display-custom-times t)
  3771. ("logging" org-log-done t)
  3772. ("logdone" org-log-done t)
  3773. ("nologging" org-log-done nil)
  3774. ("lognotedone" org-log-done done push)
  3775. ("lognotestate" org-log-done state push)
  3776. ("lognoteclock-out" org-log-done clock-out push)
  3777. ("logrepeat" org-log-repeat t)
  3778. ("nologrepeat" org-log-repeat nil)
  3779. ("constcgs" constants-unit-system cgs)
  3780. ("constSI" constants-unit-system SI))
  3781. "Variable associated with STARTUP options for org-mode.
  3782. Each element is a list of three items: The startup options as written
  3783. in the #+STARTUP line, the corresponding variable, and the value to
  3784. set this variable to if the option is found. An optional forth element PUSH
  3785. means to push this value onto the list in the variable.")
  3786. (defun org-set-regexps-and-options ()
  3787. "Precompute regular expressions for current buffer."
  3788. (when (org-mode-p)
  3789. (org-set-local 'org-todo-kwd-alist nil)
  3790. (org-set-local 'org-todo-key-alist nil)
  3791. (org-set-local 'org-todo-key-trigger nil)
  3792. (org-set-local 'org-todo-keywords-1 nil)
  3793. (org-set-local 'org-done-keywords nil)
  3794. (org-set-local 'org-todo-heads nil)
  3795. (org-set-local 'org-todo-sets nil)
  3796. (org-set-local 'org-todo-log-states nil)
  3797. (let ((re (org-make-options-regexp
  3798. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3799. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3800. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3801. (splitre "[ \t]+")
  3802. kwds kws0 kwsa key value cat arch tags const links hw dws
  3803. tail sep kws1 prio props drawers
  3804. ex log)
  3805. (save-excursion
  3806. (save-restriction
  3807. (widen)
  3808. (goto-char (point-min))
  3809. (while (re-search-forward re nil t)
  3810. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3811. (cond
  3812. ((equal key "CATEGORY")
  3813. (if (string-match "[ \t]+$" value)
  3814. (setq value (replace-match "" t t value)))
  3815. (setq cat value))
  3816. ((member key '("SEQ_TODO" "TODO"))
  3817. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3818. ((equal key "TYP_TODO")
  3819. (push (cons 'type (org-split-string value splitre)) kwds))
  3820. ((equal key "TAGS")
  3821. (setq tags (append tags (org-split-string value splitre))))
  3822. ((equal key "COLUMNS")
  3823. (org-set-local 'org-columns-default-format value))
  3824. ((equal key "LINK")
  3825. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3826. (push (cons (match-string 1 value)
  3827. (org-trim (match-string 2 value)))
  3828. links)))
  3829. ((equal key "PRIORITIES")
  3830. (setq prio (org-split-string value " +")))
  3831. ((equal key "PROPERTY")
  3832. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3833. (push (cons (match-string 1 value) (match-string 2 value))
  3834. props)))
  3835. ((equal key "DRAWERS")
  3836. (setq drawers (org-split-string value splitre)))
  3837. ((equal key "CONSTANTS")
  3838. (setq const (append const (org-split-string value splitre))))
  3839. ((equal key "STARTUP")
  3840. (let ((opts (org-split-string value splitre))
  3841. l var val)
  3842. (while (setq l (pop opts))
  3843. (when (setq l (assoc l org-startup-options))
  3844. (setq var (nth 1 l) val (nth 2 l))
  3845. (if (not (nth 3 l))
  3846. (set (make-local-variable var) val)
  3847. (if (not (listp (symbol-value var)))
  3848. (set (make-local-variable var) nil))
  3849. (set (make-local-variable var) (symbol-value var))
  3850. (add-to-list var val))))))
  3851. ((equal key "ARCHIVE")
  3852. (string-match " *$" value)
  3853. (setq arch (replace-match "" t t value))
  3854. (remove-text-properties 0 (length arch)
  3855. '(face t fontified t) arch)))
  3856. )))
  3857. (when cat
  3858. (org-set-local 'org-category (intern cat))
  3859. (push (cons "CATEGORY" cat) props))
  3860. (when prio
  3861. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3862. (setq prio (mapcar 'string-to-char prio))
  3863. (org-set-local 'org-highest-priority (nth 0 prio))
  3864. (org-set-local 'org-lowest-priority (nth 1 prio))
  3865. (org-set-local 'org-default-priority (nth 2 prio)))
  3866. (and props (org-set-local 'org-local-properties (nreverse props)))
  3867. (and drawers (org-set-local 'org-drawers drawers))
  3868. (and arch (org-set-local 'org-archive-location arch))
  3869. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3870. ;; Process the TODO keywords
  3871. (unless kwds
  3872. ;; Use the global values as if they had been given locally.
  3873. (setq kwds (default-value 'org-todo-keywords))
  3874. (if (stringp (car kwds))
  3875. (setq kwds (list (cons org-todo-interpretation
  3876. (default-value 'org-todo-keywords)))))
  3877. (setq kwds (reverse kwds)))
  3878. (setq kwds (nreverse kwds))
  3879. (let (inter kws kw)
  3880. (while (setq kws (pop kwds))
  3881. (setq inter (pop kws) sep (member "|" kws)
  3882. kws0 (delete "|" (copy-sequence kws))
  3883. kwsa nil
  3884. kws1 (mapcar
  3885. (lambda (x)
  3886. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3887. (progn
  3888. (setq kw (match-string 1 x)
  3889. ex (and (match-end 2) (match-string 2 x))
  3890. log (and ex (string-match "@" ex))
  3891. key (and ex (substring ex 0 1)))
  3892. (if (equal key "@") (setq key nil))
  3893. (push (cons kw (and key (string-to-char key))) kwsa)
  3894. (and log (push kw org-todo-log-states))
  3895. kw)
  3896. (error "Invalid TODO keyword %s" x)))
  3897. kws0)
  3898. kwsa (if kwsa (append '((:startgroup))
  3899. (nreverse kwsa)
  3900. '((:endgroup))))
  3901. hw (car kws1)
  3902. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3903. tail (list inter hw (car dws) (org-last dws)))
  3904. (add-to-list 'org-todo-heads hw 'append)
  3905. (push kws1 org-todo-sets)
  3906. (setq org-done-keywords (append org-done-keywords dws nil))
  3907. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3908. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3909. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3910. (setq org-todo-sets (nreverse org-todo-sets)
  3911. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3912. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3913. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3914. ;; Process the constants
  3915. (when const
  3916. (let (e cst)
  3917. (while (setq e (pop const))
  3918. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3919. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3920. (setq org-table-formula-constants-local cst)))
  3921. ;; Process the tags.
  3922. (when tags
  3923. (let (e tgs)
  3924. (while (setq e (pop tags))
  3925. (cond
  3926. ((equal e "{") (push '(:startgroup) tgs))
  3927. ((equal e "}") (push '(:endgroup) tgs))
  3928. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3929. (push (cons (match-string 1 e)
  3930. (string-to-char (match-string 2 e)))
  3931. tgs))
  3932. (t (push (list e) tgs))))
  3933. (org-set-local 'org-tag-alist nil)
  3934. (while (setq e (pop tgs))
  3935. (or (and (stringp (car e))
  3936. (assoc (car e) org-tag-alist))
  3937. (push e org-tag-alist))))))
  3938. ;; Compute the regular expressions and other local variables
  3939. (if (not org-done-keywords)
  3940. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3941. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3942. (length org-scheduled-string)))
  3943. org-drawer-regexp
  3944. (concat "^[ \t]*:\\("
  3945. (mapconcat 'regexp-quote org-drawers "\\|")
  3946. "\\):[ \t]*$")
  3947. org-not-done-keywords
  3948. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3949. org-todo-regexp
  3950. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3951. "\\|") "\\)\\>")
  3952. org-not-done-regexp
  3953. (concat "\\<\\("
  3954. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3955. "\\)\\>")
  3956. org-todo-line-regexp
  3957. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3958. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3959. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3960. org-complex-heading-regexp
  3961. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  3962. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3963. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3964. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3965. org-nl-done-regexp
  3966. (concat "\n\\*+[ \t]+"
  3967. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3968. "\\)" "\\>")
  3969. org-todo-line-tags-regexp
  3970. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3971. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3972. (org-re
  3973. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3974. org-looking-at-done-regexp
  3975. (concat "^" "\\(?:"
  3976. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3977. "\\>")
  3978. org-deadline-regexp (concat "\\<" org-deadline-string)
  3979. org-deadline-time-regexp
  3980. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3981. org-deadline-line-regexp
  3982. (concat "\\<\\(" org-deadline-string "\\).*")
  3983. org-scheduled-regexp
  3984. (concat "\\<" org-scheduled-string)
  3985. org-scheduled-time-regexp
  3986. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3987. org-closed-time-regexp
  3988. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3989. org-keyword-time-regexp
  3990. (concat "\\<\\(" org-scheduled-string
  3991. "\\|" org-deadline-string
  3992. "\\|" org-closed-string
  3993. "\\|" org-clock-string "\\)"
  3994. " *[[<]\\([^]>]+\\)[]>]")
  3995. org-keyword-time-not-clock-regexp
  3996. (concat "\\<\\(" org-scheduled-string
  3997. "\\|" org-deadline-string
  3998. "\\|" org-closed-string
  3999. "\\)"
  4000. " *[[<]\\([^]>]+\\)[]>]")
  4001. org-maybe-keyword-time-regexp
  4002. (concat "\\(\\<\\(" org-scheduled-string
  4003. "\\|" org-deadline-string
  4004. "\\|" org-closed-string
  4005. "\\|" org-clock-string "\\)\\)?"
  4006. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4007. org-planning-or-clock-line-re
  4008. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4009. "\\|" org-deadline-string
  4010. "\\|" org-closed-string "\\|" org-clock-string
  4011. "\\)\\>\\)")
  4012. )
  4013. (org-compute-latex-and-specials-regexp)
  4014. (org-set-font-lock-defaults)))
  4015. (defun org-remove-keyword-keys (list)
  4016. (mapcar (lambda (x)
  4017. (if (string-match "(..?)$" x)
  4018. (substring x 0 (match-beginning 0))
  4019. x))
  4020. list))
  4021. ;; FIXME: this could be done much better, using second characters etc.
  4022. (defun org-assign-fast-keys (alist)
  4023. "Assign fast keys to a keyword-key alist.
  4024. Respect keys that are already there."
  4025. (let (new e k c c1 c2 (char ?a))
  4026. (while (setq e (pop alist))
  4027. (cond
  4028. ((equal e '(:startgroup)) (push e new))
  4029. ((equal e '(:endgroup)) (push e new))
  4030. (t
  4031. (setq k (car e) c2 nil)
  4032. (if (cdr e)
  4033. (setq c (cdr e))
  4034. ;; automatically assign a character.
  4035. (setq c1 (string-to-char
  4036. (downcase (substring
  4037. k (if (= (string-to-char k) ?@) 1 0)))))
  4038. (if (or (rassoc c1 new) (rassoc c1 alist))
  4039. (while (or (rassoc char new) (rassoc char alist))
  4040. (setq char (1+ char)))
  4041. (setq c2 c1))
  4042. (setq c (or c2 char)))
  4043. (push (cons k c) new))))
  4044. (nreverse new)))
  4045. ;;; Some variables ujsed in various places
  4046. (defvar org-window-configuration nil
  4047. "Used in various places to store a window configuration.")
  4048. (defvar org-finish-function nil
  4049. "Function to be called when `C-c C-c' is used.
  4050. This is for getting out of special buffers like remember.")
  4051. ;; FIXME: Occasionally check by commenting these, to make sure
  4052. ;; no other functions uses these, forgetting to let-bind them.
  4053. (defvar entry)
  4054. (defvar state)
  4055. (defvar last-state)
  4056. (defvar date)
  4057. (defvar description)
  4058. ;; Defined somewhere in this file, but used before definition.
  4059. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4060. (defvar org-agenda-buffer-name)
  4061. (defvar org-agenda-undo-list)
  4062. (defvar org-agenda-pending-undo-list)
  4063. (defvar org-agenda-overriding-header)
  4064. (defvar orgtbl-mode)
  4065. (defvar org-html-entities)
  4066. (defvar org-struct-menu)
  4067. (defvar org-org-menu)
  4068. (defvar org-tbl-menu)
  4069. (defvar org-agenda-keymap)
  4070. ;;;; Emacs/XEmacs compatibility
  4071. ;; Overlay compatibility functions
  4072. (defun org-make-overlay (beg end &optional buffer)
  4073. (if (featurep 'xemacs)
  4074. (make-extent beg end buffer)
  4075. (make-overlay beg end buffer)))
  4076. (defun org-delete-overlay (ovl)
  4077. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4078. (defun org-detach-overlay (ovl)
  4079. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4080. (defun org-move-overlay (ovl beg end &optional buffer)
  4081. (if (featurep 'xemacs)
  4082. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4083. (move-overlay ovl beg end buffer)))
  4084. (defun org-overlay-put (ovl prop value)
  4085. (if (featurep 'xemacs)
  4086. (set-extent-property ovl prop value)
  4087. (overlay-put ovl prop value)))
  4088. (defun org-overlay-display (ovl text &optional face evap)
  4089. "Make overlay OVL display TEXT with face FACE."
  4090. (if (featurep 'xemacs)
  4091. (let ((gl (make-glyph text)))
  4092. (and face (set-glyph-face gl face))
  4093. (set-extent-property ovl 'invisible t)
  4094. (set-extent-property ovl 'end-glyph gl))
  4095. (overlay-put ovl 'display text)
  4096. (if face (overlay-put ovl 'face face))
  4097. (if evap (overlay-put ovl 'evaporate t))))
  4098. (defun org-overlay-before-string (ovl text &optional face evap)
  4099. "Make overlay OVL display TEXT with face FACE."
  4100. (if (featurep 'xemacs)
  4101. (let ((gl (make-glyph text)))
  4102. (and face (set-glyph-face gl face))
  4103. (set-extent-property ovl 'begin-glyph gl))
  4104. (if face (org-add-props text nil 'face face))
  4105. (overlay-put ovl 'before-string text)
  4106. (if evap (overlay-put ovl 'evaporate t))))
  4107. (defun org-overlay-get (ovl prop)
  4108. (if (featurep 'xemacs)
  4109. (extent-property ovl prop)
  4110. (overlay-get ovl prop)))
  4111. (defun org-overlays-at (pos)
  4112. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4113. (defun org-overlays-in (&optional start end)
  4114. (if (featurep 'xemacs)
  4115. (extent-list nil start end)
  4116. (overlays-in start end)))
  4117. (defun org-overlay-start (o)
  4118. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4119. (defun org-overlay-end (o)
  4120. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4121. (defun org-find-overlays (prop &optional pos delete)
  4122. "Find all overlays specifying PROP at POS or point.
  4123. If DELETE is non-nil, delete all those overlays."
  4124. (let ((overlays (org-overlays-at (or pos (point))))
  4125. ov found)
  4126. (while (setq ov (pop overlays))
  4127. (if (org-overlay-get ov prop)
  4128. (if delete (org-delete-overlay ov) (push ov found))))
  4129. found))
  4130. ;; Region compatibility
  4131. (defun org-add-hook (hook function &optional append local)
  4132. "Add-hook, compatible with both Emacsen."
  4133. (if (and local (featurep 'xemacs))
  4134. (add-local-hook hook function append)
  4135. (add-hook hook function append local)))
  4136. (defvar org-ignore-region nil
  4137. "To temporarily disable the active region.")
  4138. (defun org-region-active-p ()
  4139. "Is `transient-mark-mode' on and the region active?
  4140. Works on both Emacs and XEmacs."
  4141. (if org-ignore-region
  4142. nil
  4143. (if (featurep 'xemacs)
  4144. (and zmacs-regions (region-active-p))
  4145. (and transient-mark-mode mark-active))))
  4146. ;; Invisibility compatibility
  4147. (defun org-add-to-invisibility-spec (arg)
  4148. "Add elements to `buffer-invisibility-spec'.
  4149. See documentation for `buffer-invisibility-spec' for the kind of elements
  4150. that can be added."
  4151. (cond
  4152. ((fboundp 'add-to-invisibility-spec)
  4153. (add-to-invisibility-spec arg))
  4154. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4155. (setq buffer-invisibility-spec (list arg)))
  4156. (t
  4157. (setq buffer-invisibility-spec
  4158. (cons arg buffer-invisibility-spec)))))
  4159. (defun org-remove-from-invisibility-spec (arg)
  4160. "Remove elements from `buffer-invisibility-spec'."
  4161. (if (fboundp 'remove-from-invisibility-spec)
  4162. (remove-from-invisibility-spec arg)
  4163. (if (consp buffer-invisibility-spec)
  4164. (setq buffer-invisibility-spec
  4165. (delete arg buffer-invisibility-spec)))))
  4166. (defun org-in-invisibility-spec-p (arg)
  4167. "Is ARG a member of `buffer-invisibility-spec'?"
  4168. (if (consp buffer-invisibility-spec)
  4169. (member arg buffer-invisibility-spec)
  4170. nil))
  4171. ;;;; Define the Org-mode
  4172. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4173. (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."))
  4174. ;; We use a before-change function to check if a table might need
  4175. ;; an update.
  4176. (defvar org-table-may-need-update t
  4177. "Indicates that a table might need an update.
  4178. This variable is set by `org-before-change-function'.
  4179. `org-table-align' sets it back to nil.")
  4180. (defvar org-mode-map)
  4181. (defvar org-mode-hook nil)
  4182. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4183. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4184. (defvar org-table-buffer-is-an nil)
  4185. (defconst org-outline-regexp "\\*+ ")
  4186. ;;;###autoload
  4187. (define-derived-mode org-mode outline-mode "Org"
  4188. "Outline-based notes management and organizer, alias
  4189. \"Carsten's outline-mode for keeping track of everything.\"
  4190. Org-mode develops organizational tasks around a NOTES file which
  4191. contains information about projects as plain text. Org-mode is
  4192. implemented on top of outline-mode, which is ideal to keep the content
  4193. of large files well structured. It supports ToDo items, deadlines and
  4194. time stamps, which magically appear in the diary listing of the Emacs
  4195. calendar. Tables are easily created with a built-in table editor.
  4196. Plain text URL-like links connect to websites, emails (VM), Usenet
  4197. messages (Gnus), BBDB entries, and any files related to the project.
  4198. For printing and sharing of notes, an Org-mode file (or a part of it)
  4199. can be exported as a structured ASCII or HTML file.
  4200. The following commands are available:
  4201. \\{org-mode-map}"
  4202. ;; Get rid of Outline menus, they are not needed
  4203. ;; Need to do this here because define-derived-mode sets up
  4204. ;; the keymap so late. Still, it is a waste to call this each time
  4205. ;; we switch another buffer into org-mode.
  4206. (if (featurep 'xemacs)
  4207. (when (boundp 'outline-mode-menu-heading)
  4208. ;; Assume this is Greg's port, it used easymenu
  4209. (easy-menu-remove outline-mode-menu-heading)
  4210. (easy-menu-remove outline-mode-menu-show)
  4211. (easy-menu-remove outline-mode-menu-hide))
  4212. (define-key org-mode-map [menu-bar headings] 'undefined)
  4213. (define-key org-mode-map [menu-bar hide] 'undefined)
  4214. (define-key org-mode-map [menu-bar show] 'undefined))
  4215. (easy-menu-add org-org-menu)
  4216. (easy-menu-add org-tbl-menu)
  4217. (org-install-agenda-files-menu)
  4218. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4219. (org-add-to-invisibility-spec '(org-cwidth))
  4220. (when (featurep 'xemacs)
  4221. (org-set-local 'line-move-ignore-invisible t))
  4222. (org-set-local 'outline-regexp org-outline-regexp)
  4223. (org-set-local 'outline-level 'org-outline-level)
  4224. (when (and org-ellipsis
  4225. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4226. (fboundp 'make-glyph-code))
  4227. (unless org-display-table
  4228. (setq org-display-table (make-display-table)))
  4229. (set-display-table-slot
  4230. org-display-table 4
  4231. (vconcat (mapcar
  4232. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4233. org-ellipsis)))
  4234. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4235. (setq buffer-display-table org-display-table))
  4236. (org-set-regexps-and-options)
  4237. ;; Calc embedded
  4238. (org-set-local 'calc-embedded-open-mode "# ")
  4239. (modify-syntax-entry ?# "<")
  4240. (modify-syntax-entry ?@ "w")
  4241. (if org-startup-truncated (setq truncate-lines t))
  4242. (org-set-local 'font-lock-unfontify-region-function
  4243. 'org-unfontify-region)
  4244. ;; Activate before-change-function
  4245. (org-set-local 'org-table-may-need-update t)
  4246. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4247. 'local)
  4248. ;; Check for running clock before killing a buffer
  4249. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4250. ;; Paragraphs and auto-filling
  4251. (org-set-autofill-regexps)
  4252. (setq indent-line-function 'org-indent-line-function)
  4253. (org-update-radio-target-regexp)
  4254. ;; Comment characters
  4255. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4256. (org-set-local 'comment-padding " ")
  4257. ;; Imenu
  4258. (org-set-local 'imenu-create-index-function
  4259. 'org-imenu-get-tree)
  4260. ;; Make isearch reveal context
  4261. (if (or (featurep 'xemacs)
  4262. (not (boundp 'outline-isearch-open-invisible-function)))
  4263. ;; Emacs 21 and XEmacs make use of the hook
  4264. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4265. ;; Emacs 22 deals with this through a special variable
  4266. (org-set-local 'outline-isearch-open-invisible-function
  4267. (lambda (&rest ignore) (org-show-context 'isearch))))
  4268. ;; If empty file that did not turn on org-mode automatically, make it to.
  4269. (if (and org-insert-mode-line-in-empty-file
  4270. (interactive-p)
  4271. (= (point-min) (point-max)))
  4272. (insert "# -*- mode: org -*-\n\n"))
  4273. (unless org-inhibit-startup
  4274. (when org-startup-align-all-tables
  4275. (let ((bmp (buffer-modified-p)))
  4276. (org-table-map-tables 'org-table-align)
  4277. (set-buffer-modified-p bmp)))
  4278. (org-cycle-hide-drawers 'all)
  4279. (cond
  4280. ((eq org-startup-folded t)
  4281. (org-cycle '(4)))
  4282. ((eq org-startup-folded 'content)
  4283. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4284. (org-cycle '(4)) (org-cycle '(4)))))))
  4285. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4286. (defsubst org-call-with-arg (command arg)
  4287. "Call COMMAND interactively, but pretend prefix are was ARG."
  4288. (let ((current-prefix-arg arg)) (call-interactively command)))
  4289. (defsubst org-current-line (&optional pos)
  4290. (save-excursion
  4291. (and pos (goto-char pos))
  4292. ;; works also in narrowed buffer, because we start at 1, not point-min
  4293. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4294. (defun org-current-time ()
  4295. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4296. (if (> org-time-stamp-rounding-minutes 0)
  4297. (let ((r org-time-stamp-rounding-minutes)
  4298. (time (decode-time)))
  4299. (apply 'encode-time
  4300. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4301. (nthcdr 2 time))))
  4302. (current-time)))
  4303. (defun org-add-props (string plist &rest props)
  4304. "Add text properties to entire string, from beginning to end.
  4305. PLIST may be a list of properties, PROPS are individual properties and values
  4306. that will be added to PLIST. Returns the string that was modified."
  4307. (add-text-properties
  4308. 0 (length string) (if props (append plist props) plist) string)
  4309. string)
  4310. (put 'org-add-props 'lisp-indent-function 2)
  4311. ;;;; Font-Lock stuff, including the activators
  4312. (defvar org-mouse-map (make-sparse-keymap))
  4313. (org-defkey org-mouse-map
  4314. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4315. (org-defkey org-mouse-map
  4316. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4317. (when org-mouse-1-follows-link
  4318. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4319. (when org-tab-follows-link
  4320. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4321. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4322. (when org-return-follows-link
  4323. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4324. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4325. (require 'font-lock)
  4326. (defconst org-non-link-chars "]\t\n\r<>")
  4327. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4328. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  4329. (defvar org-link-re-with-space nil
  4330. "Matches a link with spaces, optional angular brackets around it.")
  4331. (defvar org-link-re-with-space2 nil
  4332. "Matches a link with spaces, optional angular brackets around it.")
  4333. (defvar org-angle-link-re nil
  4334. "Matches link with angular brackets, spaces are allowed.")
  4335. (defvar org-plain-link-re nil
  4336. "Matches plain link, without spaces.")
  4337. (defvar org-bracket-link-regexp nil
  4338. "Matches a link in double brackets.")
  4339. (defvar org-bracket-link-analytic-regexp nil
  4340. "Regular expression used to analyze links.
  4341. Here is what the match groups contain after a match:
  4342. 1: http:
  4343. 2: http
  4344. 3: path
  4345. 4: [desc]
  4346. 5: desc")
  4347. (defvar org-any-link-re nil
  4348. "Regular expression matching any link.")
  4349. (defun org-make-link-regexps ()
  4350. "Update the link regular expressions.
  4351. This should be called after the variable `org-link-types' has changed."
  4352. (setq org-link-re-with-space
  4353. (concat
  4354. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4355. "\\([^" org-non-link-chars " ]"
  4356. "[^" org-non-link-chars "]*"
  4357. "[^" org-non-link-chars " ]\\)>?")
  4358. org-link-re-with-space2
  4359. (concat
  4360. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4361. "\\([^" org-non-link-chars " ]"
  4362. "[^]\t\n\r]*"
  4363. "[^" org-non-link-chars " ]\\)>?")
  4364. org-angle-link-re
  4365. (concat
  4366. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4367. "\\([^" org-non-link-chars " ]"
  4368. "[^" org-non-link-chars "]*"
  4369. "\\)>")
  4370. org-plain-link-re
  4371. (concat
  4372. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4373. "\\([^]\t\n\r<>,;() ]+\\)")
  4374. org-bracket-link-regexp
  4375. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4376. org-bracket-link-analytic-regexp
  4377. (concat
  4378. "\\[\\["
  4379. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4380. "\\([^]]+\\)"
  4381. "\\]"
  4382. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4383. "\\]")
  4384. org-any-link-re
  4385. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4386. org-angle-link-re "\\)\\|\\("
  4387. org-plain-link-re "\\)")))
  4388. (org-make-link-regexps)
  4389. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4390. "Regular expression for fast time stamp matching.")
  4391. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4392. "Regular expression for fast time stamp matching.")
  4393. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4394. "Regular expression matching time strings for analysis.
  4395. This one does not require the space after the date.")
  4396. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4397. "Regular expression matching time strings for analysis.")
  4398. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4399. "Regular expression matching time stamps, with groups.")
  4400. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4401. "Regular expression matching time stamps (also [..]), with groups.")
  4402. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4403. "Regular expression matching a time stamp range.")
  4404. (defconst org-tr-regexp-both
  4405. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4406. "Regular expression matching a time stamp range.")
  4407. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4408. org-ts-regexp "\\)?")
  4409. "Regular expression matching a time stamp or time stamp range.")
  4410. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4411. org-ts-regexp-both "\\)?")
  4412. "Regular expression matching a time stamp or time stamp range.
  4413. The time stamps may be either active or inactive.")
  4414. (defvar org-emph-face nil)
  4415. (defun org-do-emphasis-faces (limit)
  4416. "Run through the buffer and add overlays to links."
  4417. (let (rtn)
  4418. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4419. (if (not (= (char-after (match-beginning 3))
  4420. (char-after (match-beginning 4))))
  4421. (progn
  4422. (setq rtn t)
  4423. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4424. 'face
  4425. (nth 1 (assoc (match-string 3)
  4426. org-emphasis-alist)))
  4427. (add-text-properties (match-beginning 2) (match-end 2)
  4428. '(font-lock-multiline t))
  4429. (when org-hide-emphasis-markers
  4430. (add-text-properties (match-end 4) (match-beginning 5)
  4431. '(invisible org-link))
  4432. (add-text-properties (match-beginning 3) (match-end 3)
  4433. '(invisible org-link)))))
  4434. (backward-char 1))
  4435. rtn))
  4436. (defun org-emphasize (&optional char)
  4437. "Insert or change an emphasis, i.e. a font like bold or italic.
  4438. If there is an active region, change that region to a new emphasis.
  4439. If there is no region, just insert the marker characters and position
  4440. the cursor between them.
  4441. CHAR should be either the marker character, or the first character of the
  4442. HTML tag associated with that emphasis. If CHAR is a space, the means
  4443. to remove the emphasis of the selected region.
  4444. If char is not given (for example in an interactive call) it
  4445. will be prompted for."
  4446. (interactive)
  4447. (let ((eal org-emphasis-alist) e det
  4448. (erc org-emphasis-regexp-components)
  4449. (prompt "")
  4450. (string "") beg end move tag c s)
  4451. (if (org-region-active-p)
  4452. (setq beg (region-beginning) end (region-end)
  4453. string (buffer-substring beg end))
  4454. (setq move t))
  4455. (while (setq e (pop eal))
  4456. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4457. c (aref tag 0))
  4458. (push (cons c (string-to-char (car e))) det)
  4459. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4460. (substring tag 1)))))
  4461. (unless char
  4462. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4463. (setq char (read-char-exclusive)))
  4464. (setq char (or (cdr (assoc char det)) char))
  4465. (if (equal char ?\ )
  4466. (setq s "" move nil)
  4467. (unless (assoc (char-to-string char) org-emphasis-alist)
  4468. (error "No such emphasis marker: \"%c\"" char))
  4469. (setq s (char-to-string char)))
  4470. (while (and (> (length string) 1)
  4471. (equal (substring string 0 1) (substring string -1))
  4472. (assoc (substring string 0 1) org-emphasis-alist))
  4473. (setq string (substring string 1 -1)))
  4474. (setq string (concat s string s))
  4475. (if beg (delete-region beg end))
  4476. (unless (or (bolp)
  4477. (string-match (concat "[" (nth 0 erc) "\n]")
  4478. (char-to-string (char-before (point)))))
  4479. (insert " "))
  4480. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4481. (char-to-string (char-after (point))))
  4482. (insert " ") (backward-char 1))
  4483. (insert string)
  4484. (and move (backward-char 1))))
  4485. (defconst org-nonsticky-props
  4486. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4487. (defun org-activate-plain-links (limit)
  4488. "Run through the buffer and add overlays to links."
  4489. (catch 'exit
  4490. (let (f)
  4491. (while (re-search-forward org-plain-link-re limit t)
  4492. (setq f (get-text-property (match-beginning 0) 'face))
  4493. (if (or (eq f 'org-tag)
  4494. (and (listp f) (memq 'org-tag f)))
  4495. nil
  4496. (add-text-properties (match-beginning 0) (match-end 0)
  4497. (list 'mouse-face 'highlight
  4498. 'rear-nonsticky org-nonsticky-props
  4499. 'keymap org-mouse-map
  4500. ))
  4501. (throw 'exit t))))))
  4502. (defun org-activate-code (limit)
  4503. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4504. (unless (get-text-property (match-beginning 1) 'face)
  4505. (remove-text-properties (match-beginning 0) (match-end 0)
  4506. '(display t invisible t intangible t))
  4507. t)))
  4508. (defun org-activate-angle-links (limit)
  4509. "Run through the buffer and add overlays to links."
  4510. (if (re-search-forward org-angle-link-re limit t)
  4511. (progn
  4512. (add-text-properties (match-beginning 0) (match-end 0)
  4513. (list 'mouse-face 'highlight
  4514. 'rear-nonsticky org-nonsticky-props
  4515. 'keymap org-mouse-map
  4516. ))
  4517. t)))
  4518. (defmacro org-maybe-intangible (props)
  4519. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4520. In emacs 21, invisible text is not avoided by the command loop, so the
  4521. intangible property is needed to make sure point skips this text.
  4522. In Emacs 22, this is not necessary. The intangible text property has
  4523. led to problems with flyspell. These problems are fixed in flyspell.el,
  4524. but we still avoid setting the property in Emacs 22 and later.
  4525. We use a macro so that the test can happen at compilation time."
  4526. (if (< emacs-major-version 22)
  4527. `(append '(intangible t) ,props)
  4528. props))
  4529. (defun org-activate-bracket-links (limit)
  4530. "Run through the buffer and add overlays to bracketed links."
  4531. (if (re-search-forward org-bracket-link-regexp limit t)
  4532. (let* ((help (concat "LINK: "
  4533. (org-match-string-no-properties 1)))
  4534. ;; FIXME: above we should remove the escapes.
  4535. ;; but that requires another match, protecting match data,
  4536. ;; a lot of overhead for font-lock.
  4537. (ip (org-maybe-intangible
  4538. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4539. 'keymap org-mouse-map 'mouse-face 'highlight
  4540. 'font-lock-multiline t 'help-echo help)))
  4541. (vp (list 'rear-nonsticky org-nonsticky-props
  4542. 'keymap org-mouse-map 'mouse-face 'highlight
  4543. ' font-lock-multiline t 'help-echo help)))
  4544. ;; We need to remove the invisible property here. Table narrowing
  4545. ;; may have made some of this invisible.
  4546. (remove-text-properties (match-beginning 0) (match-end 0)
  4547. '(invisible nil))
  4548. (if (match-end 3)
  4549. (progn
  4550. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4551. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4552. (add-text-properties (match-end 3) (match-end 0) ip))
  4553. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4554. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4555. (add-text-properties (match-end 1) (match-end 0) ip))
  4556. t)))
  4557. (defun org-activate-dates (limit)
  4558. "Run through the buffer and add overlays to dates."
  4559. (if (re-search-forward org-tsr-regexp-both limit t)
  4560. (progn
  4561. (add-text-properties (match-beginning 0) (match-end 0)
  4562. (list 'mouse-face 'highlight
  4563. 'rear-nonsticky org-nonsticky-props
  4564. 'keymap org-mouse-map))
  4565. (when org-display-custom-times
  4566. (if (match-end 3)
  4567. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4568. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4569. t)))
  4570. (defvar org-target-link-regexp nil
  4571. "Regular expression matching radio targets in plain text.")
  4572. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4573. "Regular expression matching a link target.")
  4574. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4575. "Regular expression matching a radio target.")
  4576. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4577. "Regular expression matching any target.")
  4578. (defun org-activate-target-links (limit)
  4579. "Run through the buffer and add overlays to target matches."
  4580. (when org-target-link-regexp
  4581. (let ((case-fold-search t))
  4582. (if (re-search-forward org-target-link-regexp limit t)
  4583. (progn
  4584. (add-text-properties (match-beginning 0) (match-end 0)
  4585. (list 'mouse-face 'highlight
  4586. 'rear-nonsticky org-nonsticky-props
  4587. 'keymap org-mouse-map
  4588. 'help-echo "Radio target link"
  4589. 'org-linked-text t))
  4590. t)))))
  4591. (defun org-update-radio-target-regexp ()
  4592. "Find all radio targets in this file and update the regular expression."
  4593. (interactive)
  4594. (when (memq 'radio org-activate-links)
  4595. (setq org-target-link-regexp
  4596. (org-make-target-link-regexp (org-all-targets 'radio)))
  4597. (org-restart-font-lock)))
  4598. (defun org-hide-wide-columns (limit)
  4599. (let (s e)
  4600. (setq s (text-property-any (point) (or limit (point-max))
  4601. 'org-cwidth t))
  4602. (when s
  4603. (setq e (next-single-property-change s 'org-cwidth))
  4604. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4605. (goto-char e)
  4606. t)))
  4607. (defvar org-latex-and-specials-regexp nil
  4608. "Regular expression for highlighting export special stuff.")
  4609. (defvar org-match-substring-regexp)
  4610. (defvar org-match-substring-with-braces-regexp)
  4611. (defvar org-export-html-special-string-regexps)
  4612. (defun org-compute-latex-and-specials-regexp ()
  4613. "Compute regular expression for stuff treated specially by exporters."
  4614. (if (not org-highlight-latex-fragments-and-specials)
  4615. (org-set-local 'org-latex-and-specials-regexp nil)
  4616. (let*
  4617. ((matchers (plist-get org-format-latex-options :matchers))
  4618. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4619. org-latex-regexps)))
  4620. (options (org-combine-plists (org-default-export-plist)
  4621. (org-infile-export-plist)))
  4622. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4623. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4624. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4625. (org-export-html-expand (plist-get options :expand-quoted-html))
  4626. (org-export-with-special-strings (plist-get options :special-strings))
  4627. (re-sub
  4628. (cond
  4629. ((equal org-export-with-sub-superscripts '{})
  4630. (list org-match-substring-with-braces-regexp))
  4631. (org-export-with-sub-superscripts
  4632. (list org-match-substring-regexp))
  4633. (t nil)))
  4634. (re-latex
  4635. (if org-export-with-LaTeX-fragments
  4636. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4637. (re-macros
  4638. (if org-export-with-TeX-macros
  4639. (list (concat "\\\\"
  4640. (regexp-opt
  4641. (append (mapcar 'car org-html-entities)
  4642. (if (boundp 'org-latex-entities)
  4643. org-latex-entities nil))
  4644. 'words))) ; FIXME
  4645. ))
  4646. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4647. (re-special (if org-export-with-special-strings
  4648. (mapcar (lambda (x) (car x))
  4649. org-export-html-special-string-regexps)))
  4650. (re-rest
  4651. (delq nil
  4652. (list
  4653. (if org-export-html-expand "@<[^>\n]+>")
  4654. ))))
  4655. (org-set-local
  4656. 'org-latex-and-specials-regexp
  4657. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4658. re-rest) "\\|")))))
  4659. (defface org-latex-and-export-specials
  4660. (let ((font (cond ((assq :inherit custom-face-attributes)
  4661. '(:inherit underline))
  4662. (t '(:underline t)))))
  4663. `((((class grayscale) (background light))
  4664. (:foreground "DimGray" ,@font))
  4665. (((class grayscale) (background dark))
  4666. (:foreground "LightGray" ,@font))
  4667. (((class color) (background light))
  4668. (:foreground "SaddleBrown"))
  4669. (((class color) (background dark))
  4670. (:foreground "burlywood"))
  4671. (t (,@font))))
  4672. "Face used to highlight math latex and other special exporter stuff."
  4673. :group 'org-faces)
  4674. (defun org-do-latex-and-special-faces (limit)
  4675. "Run through the buffer and add overlays to links."
  4676. (when org-latex-and-specials-regexp
  4677. (let (rtn d)
  4678. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4679. limit t))
  4680. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4681. 'face))
  4682. '(org-code org-verbatim underline)))
  4683. (progn
  4684. (setq rtn t
  4685. d (cond ((member (char-after (1+ (match-beginning 0)))
  4686. '(?_ ?^)) 1)
  4687. (t 0)))
  4688. (font-lock-prepend-text-property
  4689. (+ d (match-beginning 0)) (match-end 0)
  4690. 'face 'org-latex-and-export-specials)
  4691. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4692. '(font-lock-multiline t)))))
  4693. rtn)))
  4694. (defun org-restart-font-lock ()
  4695. "Restart font-lock-mode, to force refontification."
  4696. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4697. (font-lock-mode -1)
  4698. (font-lock-mode 1)))
  4699. (defun org-all-targets (&optional radio)
  4700. "Return a list of all targets in this file.
  4701. With optional argument RADIO, only find radio targets."
  4702. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4703. rtn)
  4704. (save-excursion
  4705. (goto-char (point-min))
  4706. (while (re-search-forward re nil t)
  4707. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4708. rtn)))
  4709. (defun org-make-target-link-regexp (targets)
  4710. "Make regular expression matching all strings in TARGETS.
  4711. The regular expression finds the targets also if there is a line break
  4712. between words."
  4713. (and targets
  4714. (concat
  4715. "\\<\\("
  4716. (mapconcat
  4717. (lambda (x)
  4718. (while (string-match " +" x)
  4719. (setq x (replace-match "\\s-+" t t x)))
  4720. x)
  4721. targets
  4722. "\\|")
  4723. "\\)\\>")))
  4724. (defun org-activate-tags (limit)
  4725. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4726. (progn
  4727. (add-text-properties (match-beginning 1) (match-end 1)
  4728. (list 'mouse-face 'highlight
  4729. 'rear-nonsticky org-nonsticky-props
  4730. 'keymap org-mouse-map))
  4731. t)))
  4732. (defun org-outline-level ()
  4733. (save-excursion
  4734. (looking-at outline-regexp)
  4735. (if (match-beginning 1)
  4736. (+ (org-get-string-indentation (match-string 1)) 1000)
  4737. (1- (- (match-end 0) (match-beginning 0))))))
  4738. (defvar org-font-lock-keywords nil)
  4739. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4740. "Regular expression matching a property line.")
  4741. (defun org-set-font-lock-defaults ()
  4742. (let* ((em org-fontify-emphasized-text)
  4743. (lk org-activate-links)
  4744. (org-font-lock-extra-keywords
  4745. (list
  4746. ;; Headlines
  4747. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4748. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4749. ;; Table lines
  4750. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4751. (1 'org-table t))
  4752. ;; Table internals
  4753. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4754. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4755. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4756. ;; Drawers
  4757. (list org-drawer-regexp '(0 'org-special-keyword t))
  4758. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4759. ;; Properties
  4760. (list org-property-re
  4761. '(1 'org-special-keyword t)
  4762. '(3 'org-property-value t))
  4763. (if org-format-transports-properties-p
  4764. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4765. ;; Links
  4766. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4767. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4768. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4769. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4770. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4771. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4772. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4773. '(org-hide-wide-columns (0 nil append))
  4774. ;; TODO lines
  4775. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4776. '(1 (org-get-todo-face 1) t))
  4777. ;; DONE
  4778. (if org-fontify-done-headline
  4779. (list (concat "^[*]+ +\\<\\("
  4780. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4781. "\\)\\(.*\\)")
  4782. '(2 'org-headline-done t))
  4783. nil)
  4784. ;; Priorities
  4785. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4786. ;; Special keywords
  4787. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4788. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4789. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4790. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4791. ;; Emphasis
  4792. (if em
  4793. (if (featurep 'xemacs)
  4794. '(org-do-emphasis-faces (0 nil append))
  4795. '(org-do-emphasis-faces)))
  4796. ;; Checkboxes
  4797. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4798. 2 'bold prepend)
  4799. (if org-provide-checkbox-statistics
  4800. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4801. (0 (org-get-checkbox-statistics-face) t)))
  4802. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4803. '(1 'org-archived prepend))
  4804. ;; Specials
  4805. '(org-do-latex-and-special-faces)
  4806. ;; Code
  4807. '(org-activate-code (1 'org-code t))
  4808. ;; COMMENT
  4809. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4810. "\\|" org-quote-string "\\)\\>")
  4811. '(1 'org-special-keyword t))
  4812. '("^#.*" (0 'font-lock-comment-face t))
  4813. )))
  4814. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4815. ;; Now set the full font-lock-keywords
  4816. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4817. (org-set-local 'font-lock-defaults
  4818. '(org-font-lock-keywords t nil nil backward-paragraph))
  4819. (kill-local-variable 'font-lock-keywords) nil))
  4820. (defvar org-m nil)
  4821. (defvar org-l nil)
  4822. (defvar org-f nil)
  4823. (defun org-get-level-face (n)
  4824. "Get the right face for match N in font-lock matching of healdines."
  4825. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4826. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4827. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4828. (cond
  4829. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4830. ((eq n 2) org-f)
  4831. (t (if org-level-color-stars-only nil org-f))))
  4832. (defun org-get-todo-face (kwd)
  4833. "Get the right face for a TODO keyword KWD.
  4834. If KWD is a number, get the corresponding match group."
  4835. (if (numberp kwd) (setq kwd (match-string kwd)))
  4836. (or (cdr (assoc kwd org-todo-keyword-faces))
  4837. (and (member kwd org-done-keywords) 'org-done)
  4838. 'org-todo))
  4839. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4840. "Remove fontification and activation overlays from links."
  4841. (font-lock-default-unfontify-region beg end)
  4842. (let* ((buffer-undo-list t)
  4843. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4844. (inhibit-modification-hooks t)
  4845. deactivate-mark buffer-file-name buffer-file-truename)
  4846. (remove-text-properties beg end
  4847. '(mouse-face t keymap t org-linked-text t
  4848. invisible t intangible t))))
  4849. ;;;; Visibility cycling, including org-goto and indirect buffer
  4850. ;;; Cycling
  4851. (defvar org-cycle-global-status nil)
  4852. (make-variable-buffer-local 'org-cycle-global-status)
  4853. (defvar org-cycle-subtree-status nil)
  4854. (make-variable-buffer-local 'org-cycle-subtree-status)
  4855. ;;;###autoload
  4856. (defun org-cycle (&optional arg)
  4857. "Visibility cycling for Org-mode.
  4858. - When this function is called with a prefix argument, rotate the entire
  4859. buffer through 3 states (global cycling)
  4860. 1. OVERVIEW: Show only top-level headlines.
  4861. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4862. 3. SHOW ALL: Show everything.
  4863. - When point is at the beginning of a headline, rotate the subtree started
  4864. by this line through 3 different states (local cycling)
  4865. 1. FOLDED: Only the main headline is shown.
  4866. 2. CHILDREN: The main headline and the direct children are shown.
  4867. From this state, you can move to one of the children
  4868. and zoom in further.
  4869. 3. SUBTREE: Show the entire subtree, including body text.
  4870. - When there is a numeric prefix, go up to a heading with level ARG, do
  4871. a `show-subtree' and return to the previous cursor position. If ARG
  4872. is negative, go up that many levels.
  4873. - When point is not at the beginning of a headline, execute
  4874. `indent-relative', like TAB normally does. See the option
  4875. `org-cycle-emulate-tab' for details.
  4876. - Special case: if point is at the beginning of the buffer and there is
  4877. no headline in line 1, this function will act as if called with prefix arg.
  4878. But only if also the variable `org-cycle-global-at-bob' is t."
  4879. (interactive "P")
  4880. (let* ((outline-regexp
  4881. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4882. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4883. outline-regexp))
  4884. (bob-special (and org-cycle-global-at-bob (bobp)
  4885. (not (looking-at outline-regexp))))
  4886. (org-cycle-hook
  4887. (if bob-special
  4888. (delq 'org-optimize-window-after-visibility-change
  4889. (copy-sequence org-cycle-hook))
  4890. org-cycle-hook))
  4891. (pos (point)))
  4892. (if (or bob-special (equal arg '(4)))
  4893. ;; special case: use global cycling
  4894. (setq arg t))
  4895. (cond
  4896. ((org-at-table-p 'any)
  4897. ;; Enter the table or move to the next field in the table
  4898. (or (org-table-recognize-table.el)
  4899. (progn
  4900. (if arg (org-table-edit-field t)
  4901. (org-table-justify-field-maybe)
  4902. (call-interactively 'org-table-next-field)))))
  4903. ((eq arg t) ;; Global cycling
  4904. (cond
  4905. ((and (eq last-command this-command)
  4906. (eq org-cycle-global-status 'overview))
  4907. ;; We just created the overview - now do table of contents
  4908. ;; This can be slow in very large buffers, so indicate action
  4909. (message "CONTENTS...")
  4910. (org-content)
  4911. (message "CONTENTS...done")
  4912. (setq org-cycle-global-status 'contents)
  4913. (run-hook-with-args 'org-cycle-hook 'contents))
  4914. ((and (eq last-command this-command)
  4915. (eq org-cycle-global-status 'contents))
  4916. ;; We just showed the table of contents - now show everything
  4917. (show-all)
  4918. (message "SHOW ALL")
  4919. (setq org-cycle-global-status 'all)
  4920. (run-hook-with-args 'org-cycle-hook 'all))
  4921. (t
  4922. ;; Default action: go to overview
  4923. (org-overview)
  4924. (message "OVERVIEW")
  4925. (setq org-cycle-global-status 'overview)
  4926. (run-hook-with-args 'org-cycle-hook 'overview))))
  4927. ((and org-drawers org-drawer-regexp
  4928. (save-excursion
  4929. (beginning-of-line 1)
  4930. (looking-at org-drawer-regexp)))
  4931. ;; Toggle block visibility
  4932. (org-flag-drawer
  4933. (not (get-char-property (match-end 0) 'invisible))))
  4934. ((integerp arg)
  4935. ;; Show-subtree, ARG levels up from here.
  4936. (save-excursion
  4937. (org-back-to-heading)
  4938. (outline-up-heading (if (< arg 0) (- arg)
  4939. (- (funcall outline-level) arg)))
  4940. (org-show-subtree)))
  4941. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4942. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4943. ;; At a heading: rotate between three different views
  4944. (org-back-to-heading)
  4945. (let ((goal-column 0) eoh eol eos)
  4946. ;; First, some boundaries
  4947. (save-excursion
  4948. (org-back-to-heading)
  4949. (save-excursion
  4950. (beginning-of-line 2)
  4951. (while (and (not (eobp)) ;; this is like `next-line'
  4952. (get-char-property (1- (point)) 'invisible))
  4953. (beginning-of-line 2)) (setq eol (point)))
  4954. (outline-end-of-heading) (setq eoh (point))
  4955. (org-end-of-subtree t)
  4956. (unless (eobp)
  4957. (skip-chars-forward " \t\n")
  4958. (beginning-of-line 1) ; in case this is an item
  4959. )
  4960. (setq eos (1- (point))))
  4961. ;; Find out what to do next and set `this-command'
  4962. (cond
  4963. ((= eos eoh)
  4964. ;; Nothing is hidden behind this heading
  4965. (message "EMPTY ENTRY")
  4966. (setq org-cycle-subtree-status nil)
  4967. (save-excursion
  4968. (goto-char eos)
  4969. (outline-next-heading)
  4970. (if (org-invisible-p) (org-flag-heading nil))))
  4971. ((or (>= eol eos)
  4972. (not (string-match "\\S-" (buffer-substring eol eos))))
  4973. ;; Entire subtree is hidden in one line: open it
  4974. (org-show-entry)
  4975. (show-children)
  4976. (message "CHILDREN")
  4977. (save-excursion
  4978. (goto-char eos)
  4979. (outline-next-heading)
  4980. (if (org-invisible-p) (org-flag-heading nil)))
  4981. (setq org-cycle-subtree-status 'children)
  4982. (run-hook-with-args 'org-cycle-hook 'children))
  4983. ((and (eq last-command this-command)
  4984. (eq org-cycle-subtree-status 'children))
  4985. ;; We just showed the children, now show everything.
  4986. (org-show-subtree)
  4987. (message "SUBTREE")
  4988. (setq org-cycle-subtree-status 'subtree)
  4989. (run-hook-with-args 'org-cycle-hook 'subtree))
  4990. (t
  4991. ;; Default action: hide the subtree.
  4992. (hide-subtree)
  4993. (message "FOLDED")
  4994. (setq org-cycle-subtree-status 'folded)
  4995. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4996. ;; TAB emulation
  4997. (buffer-read-only (org-back-to-heading))
  4998. ((org-try-cdlatex-tab))
  4999. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5000. (or (not (bolp))
  5001. (not (looking-at outline-regexp))))
  5002. (call-interactively (global-key-binding "\t")))
  5003. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5004. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5005. (or (and (eq org-cycle-emulate-tab 'white)
  5006. (= (match-end 0) (point-at-eol)))
  5007. (and (eq org-cycle-emulate-tab 'whitestart)
  5008. (>= (match-end 0) pos))))
  5009. t
  5010. (eq org-cycle-emulate-tab t))
  5011. ; (if (and (looking-at "[ \n\r\t]")
  5012. ; (string-match "^[ \t]*$" (buffer-substring
  5013. ; (point-at-bol) (point))))
  5014. ; (progn
  5015. ; (beginning-of-line 1)
  5016. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5017. (call-interactively (global-key-binding "\t")))
  5018. (t (save-excursion
  5019. (org-back-to-heading)
  5020. (org-cycle))))))
  5021. ;;;###autoload
  5022. (defun org-global-cycle (&optional arg)
  5023. "Cycle the global visibility. For details see `org-cycle'."
  5024. (interactive "P")
  5025. (let ((org-cycle-include-plain-lists
  5026. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5027. (if (integerp arg)
  5028. (progn
  5029. (show-all)
  5030. (hide-sublevels arg)
  5031. (setq org-cycle-global-status 'contents))
  5032. (org-cycle '(4)))))
  5033. (defun org-overview ()
  5034. "Switch to overview mode, shoing only top-level headlines.
  5035. Really, this shows all headlines with level equal or greater than the level
  5036. of the first headline in the buffer. This is important, because if the
  5037. first headline is not level one, then (hide-sublevels 1) gives confusing
  5038. results."
  5039. (interactive)
  5040. (let ((level (save-excursion
  5041. (goto-char (point-min))
  5042. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5043. (progn
  5044. (goto-char (match-beginning 0))
  5045. (funcall outline-level))))))
  5046. (and level (hide-sublevels level))))
  5047. (defun org-content (&optional arg)
  5048. "Show all headlines in the buffer, like a table of contents.
  5049. With numerical argument N, show content up to level N."
  5050. (interactive "P")
  5051. (save-excursion
  5052. ;; Visit all headings and show their offspring
  5053. (and (integerp arg) (org-overview))
  5054. (goto-char (point-max))
  5055. (catch 'exit
  5056. (while (and (progn (condition-case nil
  5057. (outline-previous-visible-heading 1)
  5058. (error (goto-char (point-min))))
  5059. t)
  5060. (looking-at outline-regexp))
  5061. (if (integerp arg)
  5062. (show-children (1- arg))
  5063. (show-branches))
  5064. (if (bobp) (throw 'exit nil))))))
  5065. (defun org-optimize-window-after-visibility-change (state)
  5066. "Adjust the window after a change in outline visibility.
  5067. This function is the default value of the hook `org-cycle-hook'."
  5068. (when (get-buffer-window (current-buffer))
  5069. (cond
  5070. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5071. ; ((eq state 'overview) (org-beginning-of-line))
  5072. ((eq state 'content) nil)
  5073. ((eq state 'all) nil)
  5074. ((eq state 'folded) nil)
  5075. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5076. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5077. (defun org-cycle-show-empty-lines (state)
  5078. "Show empty lines above all visible headlines.
  5079. The region to be covered depends on STATE when called through
  5080. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5081. entire buffer covered. Note that an empty line is only shown if there
  5082. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5083. (when (> org-cycle-separator-lines 0)
  5084. (save-excursion
  5085. (let* ((n org-cycle-separator-lines)
  5086. (re (cond
  5087. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5088. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5089. (t (let ((ns (number-to-string (- n 2))))
  5090. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5091. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5092. beg end)
  5093. (cond
  5094. ((memq state '(overview contents t))
  5095. (setq beg (point-min) end (point-max)))
  5096. ((memq state '(children folded))
  5097. (setq beg (point) end (progn (org-end-of-subtree t t)
  5098. (beginning-of-line 2)
  5099. (point)))))
  5100. (when beg
  5101. (goto-char beg)
  5102. (while (re-search-forward re end t)
  5103. (if (not (get-char-property (match-end 1) 'invisible))
  5104. (outline-flag-region
  5105. (match-beginning 1) (match-end 1) nil)))))))
  5106. ;; Never hide empty lines at the end of the file.
  5107. (save-excursion
  5108. (goto-char (point-max))
  5109. (outline-previous-heading)
  5110. (outline-end-of-heading)
  5111. (if (and (looking-at "[ \t\n]+")
  5112. (= (match-end 0) (point-max)))
  5113. (outline-flag-region (point) (match-end 0) nil))))
  5114. (defun org-subtree-end-visible-p ()
  5115. "Is the end of the current subtree visible?"
  5116. (pos-visible-in-window-p
  5117. (save-excursion (org-end-of-subtree t) (point))))
  5118. (defun org-first-headline-recenter (&optional N)
  5119. "Move cursor to the first headline and recenter the headline.
  5120. Optional argument N means, put the headline into the Nth line of the window."
  5121. (goto-char (point-min))
  5122. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5123. (beginning-of-line)
  5124. (recenter (prefix-numeric-value N))))
  5125. ;;; Org-goto
  5126. (defvar org-goto-window-configuration nil)
  5127. (defvar org-goto-marker nil)
  5128. (defvar org-goto-map
  5129. (let ((map (make-sparse-keymap)))
  5130. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5131. (while (setq cmd (pop cmds))
  5132. (substitute-key-definition cmd cmd map global-map)))
  5133. (suppress-keymap map)
  5134. (org-defkey map "\C-m" 'org-goto-ret)
  5135. (org-defkey map [(left)] 'org-goto-left)
  5136. (org-defkey map [(right)] 'org-goto-right)
  5137. (org-defkey map [(?q)] 'org-goto-quit)
  5138. (org-defkey map [(control ?g)] 'org-goto-quit)
  5139. (org-defkey map "\C-i" 'org-cycle)
  5140. (org-defkey map [(tab)] 'org-cycle)
  5141. (org-defkey map [(down)] 'outline-next-visible-heading)
  5142. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5143. (org-defkey map "n" 'outline-next-visible-heading)
  5144. (org-defkey map "p" 'outline-previous-visible-heading)
  5145. (org-defkey map "f" 'outline-forward-same-level)
  5146. (org-defkey map "b" 'outline-backward-same-level)
  5147. (org-defkey map "u" 'outline-up-heading)
  5148. (org-defkey map "/" 'org-occur)
  5149. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5150. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5151. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5152. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5153. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5154. map))
  5155. (defconst org-goto-help
  5156. "Browse copy of buffer to find location or copy text.
  5157. RET=jump to location [Q]uit and return to previous location
  5158. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur"
  5159. )
  5160. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5161. (defun org-goto ()
  5162. "Look up a different location in the current file, keeping current visibility.
  5163. When you want look-up or go to a different location in a document, the
  5164. fastest way is often to fold the entire buffer and then dive into the tree.
  5165. This method has the disadvantage, that the previous location will be folded,
  5166. which may not be what you want.
  5167. This command works around this by showing a copy of the current buffer
  5168. in an indirect buffer, in overview mode. You can dive into the tree in
  5169. that copy, use org-occur and incremental search to find a location.
  5170. When pressing RET or `Q', the command returns to the original buffer in
  5171. which the visibility is still unchanged. After RET is will also jump to
  5172. the location selected in the indirect buffer and expose the
  5173. the headline hierarchy above."
  5174. (interactive)
  5175. (let* ((org-goto-start-pos (point))
  5176. (selected-point
  5177. (car (org-get-location (current-buffer) org-goto-help))))
  5178. (if selected-point
  5179. (progn
  5180. (org-mark-ring-push org-goto-start-pos)
  5181. (goto-char selected-point)
  5182. (if (or (org-invisible-p) (org-invisible-p2))
  5183. (org-show-context 'org-goto)))
  5184. (message "Quit"))))
  5185. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5186. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5187. (defun org-get-location (buf help)
  5188. "Let the user select a location in the Org-mode buffer BUF.
  5189. This function uses a recursive edit. It returns the selected position
  5190. or nil."
  5191. (let (org-goto-selected-point org-goto-exit-command)
  5192. (save-excursion
  5193. (save-window-excursion
  5194. (delete-other-windows)
  5195. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5196. (switch-to-buffer
  5197. (condition-case nil
  5198. (make-indirect-buffer (current-buffer) "*org-goto*")
  5199. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5200. (with-output-to-temp-buffer "*Help*"
  5201. (princ help))
  5202. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5203. (setq buffer-read-only nil)
  5204. (let ((org-startup-truncated t)
  5205. (org-startup-folded nil)
  5206. (org-startup-align-all-tables nil))
  5207. (org-mode)
  5208. (org-overview))
  5209. (setq buffer-read-only t)
  5210. (if (and (boundp 'org-goto-start-pos)
  5211. (integer-or-marker-p org-goto-start-pos))
  5212. (let ((org-show-hierarchy-above t)
  5213. (org-show-siblings t)
  5214. (org-show-following-heading t))
  5215. (goto-char org-goto-start-pos)
  5216. (and (org-invisible-p) (org-show-context)))
  5217. (goto-char (point-min)))
  5218. (org-beginning-of-line)
  5219. (message "Select location and press RET")
  5220. ;; now we make sure that during selection, ony very few keys work
  5221. ;; and that it is impossible to switch to another window.
  5222. ; (let ((gm (current-global-map))
  5223. ; (overriding-local-map org-goto-map))
  5224. ; (unwind-protect
  5225. ; (progn
  5226. ; (use-global-map org-goto-map)
  5227. ; (recursive-edit))
  5228. ; (use-global-map gm)))
  5229. (use-local-map org-goto-map)
  5230. (recursive-edit)
  5231. ))
  5232. (kill-buffer "*org-goto*")
  5233. (cons org-goto-selected-point org-goto-exit-command)))
  5234. (defun org-goto-ret (&optional arg)
  5235. "Finish `org-goto' by going to the new location."
  5236. (interactive "P")
  5237. (setq org-goto-selected-point (point)
  5238. org-goto-exit-command 'return)
  5239. (throw 'exit nil))
  5240. (defun org-goto-left ()
  5241. "Finish `org-goto' by going to the new location."
  5242. (interactive)
  5243. (if (org-on-heading-p)
  5244. (progn
  5245. (beginning-of-line 1)
  5246. (setq org-goto-selected-point (point)
  5247. org-goto-exit-command 'left)
  5248. (throw 'exit nil))
  5249. (error "Not on a heading")))
  5250. (defun org-goto-right ()
  5251. "Finish `org-goto' by going to the new location."
  5252. (interactive)
  5253. (if (org-on-heading-p)
  5254. (progn
  5255. (setq org-goto-selected-point (point)
  5256. org-goto-exit-command 'right)
  5257. (throw 'exit nil))
  5258. (error "Not on a heading")))
  5259. (defun org-goto-quit ()
  5260. "Finish `org-goto' without cursor motion."
  5261. (interactive)
  5262. (setq org-goto-selected-point nil)
  5263. (setq org-goto-exit-command 'quit)
  5264. (throw 'exit nil))
  5265. ;;; Indirect buffer display of subtrees
  5266. (defvar org-indirect-dedicated-frame nil
  5267. "This is the frame being used for indirect tree display.")
  5268. (defvar org-last-indirect-buffer nil)
  5269. (defun org-tree-to-indirect-buffer (&optional arg)
  5270. "Create indirect buffer and narrow it to current subtree.
  5271. With numerical prefix ARG, go up to this level and then take that tree.
  5272. If ARG is negative, go up that many levels.
  5273. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5274. indirect buffer previously made with this command, to avoid proliferation of
  5275. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5276. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5277. is kept so that you can work with several indirect buffers at the same time.
  5278. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5279. requests that a new frame be made for the new buffer, so that the dedicated
  5280. frame is not changed."
  5281. (interactive "P")
  5282. (let ((cbuf (current-buffer))
  5283. (cwin (selected-window))
  5284. (pos (point))
  5285. beg end level heading ibuf)
  5286. (save-excursion
  5287. (org-back-to-heading t)
  5288. (when (numberp arg)
  5289. (setq level (org-outline-level))
  5290. (if (< arg 0) (setq arg (+ level arg)))
  5291. (while (> (setq level (org-outline-level)) arg)
  5292. (outline-up-heading 1 t)))
  5293. (setq beg (point)
  5294. heading (org-get-heading))
  5295. (org-end-of-subtree t) (setq end (point)))
  5296. (if (and (buffer-live-p org-last-indirect-buffer)
  5297. (not (eq org-indirect-buffer-display 'new-frame))
  5298. (not arg))
  5299. (kill-buffer org-last-indirect-buffer))
  5300. (setq ibuf (org-get-indirect-buffer cbuf)
  5301. org-last-indirect-buffer ibuf)
  5302. (cond
  5303. ((or (eq org-indirect-buffer-display 'new-frame)
  5304. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5305. (select-frame (make-frame))
  5306. (delete-other-windows)
  5307. (switch-to-buffer ibuf)
  5308. (org-set-frame-title heading))
  5309. ((eq org-indirect-buffer-display 'dedicated-frame)
  5310. (raise-frame
  5311. (select-frame (or (and org-indirect-dedicated-frame
  5312. (frame-live-p org-indirect-dedicated-frame)
  5313. org-indirect-dedicated-frame)
  5314. (setq org-indirect-dedicated-frame (make-frame)))))
  5315. (delete-other-windows)
  5316. (switch-to-buffer ibuf)
  5317. (org-set-frame-title (concat "Indirect: " heading)))
  5318. ((eq org-indirect-buffer-display 'current-window)
  5319. (switch-to-buffer ibuf))
  5320. ((eq org-indirect-buffer-display 'other-window)
  5321. (pop-to-buffer ibuf))
  5322. (t (error "Invalid value.")))
  5323. (if (featurep 'xemacs)
  5324. (save-excursion (org-mode) (turn-on-font-lock)))
  5325. (narrow-to-region beg end)
  5326. (show-all)
  5327. (goto-char pos)
  5328. (and (window-live-p cwin) (select-window cwin))))
  5329. (defun org-get-indirect-buffer (&optional buffer)
  5330. (setq buffer (or buffer (current-buffer)))
  5331. (let ((n 1) (base (buffer-name buffer)) bname)
  5332. (while (buffer-live-p
  5333. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5334. (setq n (1+ n)))
  5335. (condition-case nil
  5336. (make-indirect-buffer buffer bname 'clone)
  5337. (error (make-indirect-buffer buffer bname)))))
  5338. (defun org-set-frame-title (title)
  5339. "Set the title of the current frame to the string TITLE."
  5340. ;; FIXME: how to name a single frame in XEmacs???
  5341. (unless (featurep 'xemacs)
  5342. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5343. ;;;; Structure editing
  5344. ;;; Inserting headlines
  5345. (defun org-insert-heading (&optional force-heading)
  5346. "Insert a new heading or item with same depth at point.
  5347. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5348. If point is at the beginning of a headline, insert a sibling before the
  5349. current headline. If point is in the middle of a headline, split the headline
  5350. at that position and make the rest of the headline part of the sibling below
  5351. the current headline."
  5352. (interactive "P")
  5353. (if (= (buffer-size) 0)
  5354. (insert "\n* ")
  5355. (when (or force-heading (not (org-insert-item)))
  5356. (let* ((head (save-excursion
  5357. (condition-case nil
  5358. (progn
  5359. (org-back-to-heading)
  5360. (match-string 0))
  5361. (error "*"))))
  5362. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5363. pos)
  5364. (cond
  5365. ((and (org-on-heading-p) (bolp)
  5366. (or (bobp)
  5367. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5368. (open-line (if blank 2 1)))
  5369. ((and (bolp)
  5370. (or (bobp)
  5371. (save-excursion
  5372. (backward-char 1) (not (org-invisible-p)))))
  5373. nil)
  5374. (t (newline (if blank 2 1))))
  5375. (insert head) (just-one-space)
  5376. (setq pos (point))
  5377. (end-of-line 1)
  5378. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5379. (run-hooks 'org-insert-heading-hook)))))
  5380. (defun org-insert-heading-after-current ()
  5381. "Insert a new heading with same level as current, after current subtree."
  5382. (interactive)
  5383. (org-back-to-heading)
  5384. (org-insert-heading)
  5385. (org-move-subtree-down)
  5386. (end-of-line 1))
  5387. (defun org-insert-todo-heading (arg)
  5388. "Insert a new heading with the same level and TODO state as current heading.
  5389. If the heading has no TODO state, or if the state is DONE, use the first
  5390. state (TODO by default). Also with prefix arg, force first state."
  5391. (interactive "P")
  5392. (when (not (org-insert-item 'checkbox))
  5393. (org-insert-heading)
  5394. (save-excursion
  5395. (org-back-to-heading)
  5396. (outline-previous-heading)
  5397. (looking-at org-todo-line-regexp))
  5398. (if (or arg
  5399. (not (match-beginning 2))
  5400. (member (match-string 2) org-done-keywords))
  5401. (insert (car org-todo-keywords-1) " ")
  5402. (insert (match-string 2) " "))))
  5403. (defun org-insert-subheading (arg)
  5404. "Insert a new subheading and demote it.
  5405. Works for outline headings and for plain lists alike."
  5406. (interactive "P")
  5407. (org-insert-heading arg)
  5408. (cond
  5409. ((org-on-heading-p) (org-do-demote))
  5410. ((org-at-item-p) (org-indent-item 1))))
  5411. (defun org-insert-todo-subheading (arg)
  5412. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5413. Works for outline headings and for plain lists alike."
  5414. (interactive "P")
  5415. (org-insert-todo-heading arg)
  5416. (cond
  5417. ((org-on-heading-p) (org-do-demote))
  5418. ((org-at-item-p) (org-indent-item 1))))
  5419. ;;; Promotion and Demotion
  5420. (defun org-promote-subtree ()
  5421. "Promote the entire subtree.
  5422. See also `org-promote'."
  5423. (interactive)
  5424. (save-excursion
  5425. (org-map-tree 'org-promote))
  5426. (org-fix-position-after-promote))
  5427. (defun org-demote-subtree ()
  5428. "Demote the entire subtree. See `org-demote'.
  5429. See also `org-promote'."
  5430. (interactive)
  5431. (save-excursion
  5432. (org-map-tree 'org-demote))
  5433. (org-fix-position-after-promote))
  5434. (defun org-do-promote ()
  5435. "Promote the current heading higher up the tree.
  5436. If the region is active in `transient-mark-mode', promote all headings
  5437. in the region."
  5438. (interactive)
  5439. (save-excursion
  5440. (if (org-region-active-p)
  5441. (org-map-region 'org-promote (region-beginning) (region-end))
  5442. (org-promote)))
  5443. (org-fix-position-after-promote))
  5444. (defun org-do-demote ()
  5445. "Demote the current heading lower down the tree.
  5446. If the region is active in `transient-mark-mode', demote all headings
  5447. in the region."
  5448. (interactive)
  5449. (save-excursion
  5450. (if (org-region-active-p)
  5451. (org-map-region 'org-demote (region-beginning) (region-end))
  5452. (org-demote)))
  5453. (org-fix-position-after-promote))
  5454. (defun org-fix-position-after-promote ()
  5455. "Make sure that after pro/demotion cursor position is right."
  5456. (let ((pos (point)))
  5457. (when (save-excursion
  5458. (beginning-of-line 1)
  5459. (looking-at org-todo-line-regexp)
  5460. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5461. (cond ((eobp) (insert " "))
  5462. ((eolp) (insert " "))
  5463. ((equal (char-after) ?\ ) (forward-char 1))))))
  5464. (defun org-reduced-level (l)
  5465. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5466. (defun org-get-legal-level (level &optional change)
  5467. "Rectify a level change under the influence of `org-odd-levels-only'
  5468. LEVEL is a current level, CHANGE is by how much the level should be
  5469. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5470. even level numbers will become the next higher odd number."
  5471. (if org-odd-levels-only
  5472. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5473. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5474. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5475. (max 1 (+ level change))))
  5476. (defun org-promote ()
  5477. "Promote the current heading higher up the tree.
  5478. If the region is active in `transient-mark-mode', promote all headings
  5479. in the region."
  5480. (org-back-to-heading t)
  5481. (let* ((level (save-match-data (funcall outline-level)))
  5482. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5483. (diff (abs (- level (length up-head) -1))))
  5484. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5485. (replace-match up-head nil t)
  5486. ;; Fixup tag positioning
  5487. (and org-auto-align-tags (org-set-tags nil t))
  5488. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5489. (defun org-demote ()
  5490. "Demote the current heading lower down the tree.
  5491. If the region is active in `transient-mark-mode', demote all headings
  5492. in the region."
  5493. (org-back-to-heading t)
  5494. (let* ((level (save-match-data (funcall outline-level)))
  5495. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5496. (diff (abs (- level (length down-head) -1))))
  5497. (replace-match down-head nil t)
  5498. ;; Fixup tag positioning
  5499. (and org-auto-align-tags (org-set-tags nil t))
  5500. (if org-adapt-indentation (org-fixup-indentation diff))))
  5501. (defun org-map-tree (fun)
  5502. "Call FUN for every heading underneath the current one."
  5503. (org-back-to-heading)
  5504. (let ((level (funcall outline-level)))
  5505. (save-excursion
  5506. (funcall fun)
  5507. (while (and (progn
  5508. (outline-next-heading)
  5509. (> (funcall outline-level) level))
  5510. (not (eobp)))
  5511. (funcall fun)))))
  5512. (defun org-map-region (fun beg end)
  5513. "Call FUN for every heading between BEG and END."
  5514. (let ((org-ignore-region t))
  5515. (save-excursion
  5516. (setq end (copy-marker end))
  5517. (goto-char beg)
  5518. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5519. (< (point) end))
  5520. (funcall fun))
  5521. (while (and (progn
  5522. (outline-next-heading)
  5523. (< (point) end))
  5524. (not (eobp)))
  5525. (funcall fun)))))
  5526. (defun org-fixup-indentation (diff)
  5527. "Change the indentation in the current entry by DIFF
  5528. However, if any line in the current entry has no indentation, or if it
  5529. would end up with no indentation after the change, nothing at all is done."
  5530. (save-excursion
  5531. (let ((end (save-excursion (outline-next-heading)
  5532. (point-marker)))
  5533. (prohibit (if (> diff 0)
  5534. "^\\S-"
  5535. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5536. col)
  5537. (unless (save-excursion (end-of-line 1)
  5538. (re-search-forward prohibit end t))
  5539. (while (and (< (point) end)
  5540. (re-search-forward "^[ \t]+" end t))
  5541. (goto-char (match-end 0))
  5542. (setq col (current-column))
  5543. (if (< diff 0) (replace-match ""))
  5544. (indent-to (+ diff col))))
  5545. (move-marker end nil))))
  5546. (defun org-convert-to-odd-levels ()
  5547. "Convert an org-mode file with all levels allowed to one with odd levels.
  5548. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5549. level 5 etc."
  5550. (interactive)
  5551. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5552. (let ((org-odd-levels-only nil) n)
  5553. (save-excursion
  5554. (goto-char (point-min))
  5555. (while (re-search-forward "^\\*\\*+ " nil t)
  5556. (setq n (- (length (match-string 0)) 2))
  5557. (while (>= (setq n (1- n)) 0)
  5558. (org-demote))
  5559. (end-of-line 1))))))
  5560. (defun org-convert-to-oddeven-levels ()
  5561. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5562. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5563. section with an even level, conversion would destroy the structure of the file. An error
  5564. is signaled in this case."
  5565. (interactive)
  5566. (goto-char (point-min))
  5567. ;; First check if there are no even levels
  5568. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5569. (org-show-context t)
  5570. (error "Not all levels are odd in this file. Conversion not possible."))
  5571. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5572. (let ((org-odd-levels-only nil) n)
  5573. (save-excursion
  5574. (goto-char (point-min))
  5575. (while (re-search-forward "^\\*\\*+ " nil t)
  5576. (setq n (/ (1- (length (match-string 0))) 2))
  5577. (while (>= (setq n (1- n)) 0)
  5578. (org-promote))
  5579. (end-of-line 1))))))
  5580. (defun org-tr-level (n)
  5581. "Make N odd if required."
  5582. (if org-odd-levels-only (1+ (/ n 2)) n))
  5583. ;;; Vertical tree motion, cutting and pasting of subtrees
  5584. (defun org-move-subtree-up (&optional arg)
  5585. "Move the current subtree up past ARG headlines of the same level."
  5586. (interactive "p")
  5587. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5588. (defun org-move-subtree-down (&optional arg)
  5589. "Move the current subtree down past ARG headlines of the same level."
  5590. (interactive "p")
  5591. (setq arg (prefix-numeric-value arg))
  5592. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5593. 'outline-get-last-sibling))
  5594. (ins-point (make-marker))
  5595. (cnt (abs arg))
  5596. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5597. ;; Select the tree
  5598. (org-back-to-heading)
  5599. (setq beg0 (point))
  5600. (save-excursion
  5601. (setq ne-beg (org-back-over-empty-lines))
  5602. (setq beg (point)))
  5603. (save-match-data
  5604. (save-excursion (outline-end-of-heading)
  5605. (setq folded (org-invisible-p)))
  5606. (outline-end-of-subtree))
  5607. (outline-next-heading)
  5608. (setq ne-end (org-back-over-empty-lines))
  5609. (setq end (point))
  5610. (goto-char beg0)
  5611. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5612. ;; include less whitespace
  5613. (save-excursion
  5614. (goto-char beg)
  5615. (forward-line (- ne-beg ne-end))
  5616. (setq beg (point))))
  5617. ;; Find insertion point, with error handling
  5618. (while (> cnt 0)
  5619. (or (and (funcall movfunc) (looking-at outline-regexp))
  5620. (progn (goto-char beg0)
  5621. (error "Cannot move past superior level or buffer limit")))
  5622. (setq cnt (1- cnt)))
  5623. (if (> arg 0)
  5624. ;; Moving forward - still need to move over subtree
  5625. (progn (outline-end-of-subtree)
  5626. (org-back-over-empty-lines)
  5627. (or (bolp) (newline))))
  5628. (setq ne-ins (org-back-over-empty-lines))
  5629. (move-marker ins-point (point))
  5630. (setq txt (buffer-substring beg end))
  5631. (delete-region beg end)
  5632. (insert txt)
  5633. (or (bolp) (insert "\n"))
  5634. (setq ins-end (point))
  5635. (goto-char ins-point)
  5636. (org-skip-whitespace)
  5637. (when (and (< arg 0)
  5638. (org-first-sibling-p)
  5639. (> ne-ins ne-beg))
  5640. ;; Move whitespace back to beginning
  5641. (save-excursion
  5642. (goto-char ins-end)
  5643. (let ((kill-whole-line t))
  5644. (kill-line (- ne-ins ne-beg)) (point)))
  5645. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5646. (if folded (hide-subtree))
  5647. (move-marker ins-point nil)))
  5648. (defvar org-subtree-clip ""
  5649. "Clipboard for cut and paste of subtrees.
  5650. This is actually only a copy of the kill, because we use the normal kill
  5651. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5652. (defvar org-subtree-clip-folded nil
  5653. "Was the last copied subtree folded?
  5654. This is used to fold the tree back after pasting.")
  5655. (defun org-cut-subtree (&optional n)
  5656. "Cut the current subtree into the clipboard.
  5657. With prefix arg N, cut this many sequential subtrees.
  5658. This is a short-hand for marking the subtree and then cutting it."
  5659. (interactive "p")
  5660. (org-copy-subtree n 'cut))
  5661. (defun org-copy-subtree (&optional n cut)
  5662. "Cut the current subtree into the clipboard.
  5663. With prefix arg N, cut this many sequential subtrees.
  5664. This is a short-hand for marking the subtree and then copying it.
  5665. If CUT is non-nil, actually cut the subtree."
  5666. (interactive "p")
  5667. (let (beg end folded)
  5668. (if (interactive-p)
  5669. (org-back-to-heading nil) ; take what looks like a subtree
  5670. (org-back-to-heading t)) ; take what is really there
  5671. (org-back-over-empty-lines)
  5672. (setq beg (point))
  5673. (skip-chars-forward " \t\r\n")
  5674. (save-match-data
  5675. (save-excursion (outline-end-of-heading)
  5676. (setq folded (org-invisible-p)))
  5677. (condition-case nil
  5678. (outline-forward-same-level (1- n))
  5679. (error nil))
  5680. (org-end-of-subtree t t))
  5681. (org-back-over-empty-lines)
  5682. (setq end (point))
  5683. (goto-char beg)
  5684. (when (> end beg)
  5685. (setq org-subtree-clip-folded folded)
  5686. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5687. (setq org-subtree-clip (current-kill 0))
  5688. (message "%s: Subtree(s) with %d characters"
  5689. (if cut "Cut" "Copied")
  5690. (length org-subtree-clip)))))
  5691. (defun org-paste-subtree (&optional level tree)
  5692. "Paste the clipboard as a subtree, with modification of headline level.
  5693. The entire subtree is promoted or demoted in order to match a new headline
  5694. level. By default, the new level is derived from the visible headings
  5695. before and after the insertion point, and taken to be the inferior headline
  5696. level of the two. So if the previous visible heading is level 3 and the
  5697. next is level 4 (or vice versa), level 4 will be used for insertion.
  5698. This makes sure that the subtree remains an independent subtree and does
  5699. not swallow low level entries.
  5700. You can also force a different level, either by using a numeric prefix
  5701. argument, or by inserting the heading marker by hand. For example, if the
  5702. cursor is after \"*****\", then the tree will be shifted to level 5.
  5703. If you want to insert the tree as is, just use \\[yank].
  5704. If optional TREE is given, use this text instead of the kill ring."
  5705. (interactive "P")
  5706. (unless (org-kill-is-subtree-p tree)
  5707. (error "%s"
  5708. (substitute-command-keys
  5709. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5710. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5711. (^re (concat "^\\(" outline-regexp "\\)"))
  5712. (re (concat "\\(" outline-regexp "\\)"))
  5713. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5714. (old-level (if (string-match ^re txt)
  5715. (- (match-end 0) (match-beginning 0) 1)
  5716. -1))
  5717. (force-level (cond (level (prefix-numeric-value level))
  5718. ((string-match
  5719. ^re_ (buffer-substring (point-at-bol) (point)))
  5720. (- (match-end 1) (match-beginning 1)))
  5721. (t nil)))
  5722. (previous-level (save-excursion
  5723. (condition-case nil
  5724. (progn
  5725. (outline-previous-visible-heading 1)
  5726. (if (looking-at re)
  5727. (- (match-end 0) (match-beginning 0) 1)
  5728. 1))
  5729. (error 1))))
  5730. (next-level (save-excursion
  5731. (condition-case nil
  5732. (progn
  5733. (or (looking-at outline-regexp)
  5734. (outline-next-visible-heading 1))
  5735. (if (looking-at re)
  5736. (- (match-end 0) (match-beginning 0) 1)
  5737. 1))
  5738. (error 1))))
  5739. (new-level (or force-level (max previous-level next-level)))
  5740. (shift (if (or (= old-level -1)
  5741. (= new-level -1)
  5742. (= old-level new-level))
  5743. 0
  5744. (- new-level old-level)))
  5745. (delta (if (> shift 0) -1 1))
  5746. (func (if (> shift 0) 'org-demote 'org-promote))
  5747. (org-odd-levels-only nil)
  5748. beg end)
  5749. ;; Remove the forced level indicator
  5750. (if force-level
  5751. (delete-region (point-at-bol) (point)))
  5752. ;; Paste
  5753. (beginning-of-line 1)
  5754. (setq beg (point))
  5755. (insert txt)
  5756. (unless (string-match "\n\\'" txt) (insert "\n"))
  5757. (setq end (point))
  5758. (goto-char beg)
  5759. (skip-chars-forward " \t\n\r")
  5760. (setq beg (point))
  5761. ;; Shift if necessary
  5762. (unless (= shift 0)
  5763. (save-restriction
  5764. (narrow-to-region beg end)
  5765. (while (not (= shift 0))
  5766. (org-map-region func (point-min) (point-max))
  5767. (setq shift (+ delta shift)))
  5768. (goto-char (point-min))))
  5769. (when (interactive-p)
  5770. (message "Clipboard pasted as level %d subtree" new-level))
  5771. (if (and kill-ring
  5772. (eq org-subtree-clip (current-kill 0))
  5773. org-subtree-clip-folded)
  5774. ;; The tree was folded before it was killed/copied
  5775. (hide-subtree))))
  5776. (defun org-kill-is-subtree-p (&optional txt)
  5777. "Check if the current kill is an outline subtree, or a set of trees.
  5778. Returns nil if kill does not start with a headline, or if the first
  5779. headline level is not the largest headline level in the tree.
  5780. So this will actually accept several entries of equal levels as well,
  5781. which is OK for `org-paste-subtree'.
  5782. If optional TXT is given, check this string instead of the current kill."
  5783. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5784. (start-level (and kill
  5785. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5786. org-outline-regexp "\\)")
  5787. kill)
  5788. (- (match-end 2) (match-beginning 2) 1)))
  5789. (re (concat "^" org-outline-regexp))
  5790. (start (1+ (match-beginning 2))))
  5791. (if (not start-level)
  5792. (progn
  5793. nil) ;; does not even start with a heading
  5794. (catch 'exit
  5795. (while (setq start (string-match re kill (1+ start)))
  5796. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5797. (throw 'exit nil)))
  5798. t))))
  5799. (defun org-narrow-to-subtree ()
  5800. "Narrow buffer to the current subtree."
  5801. (interactive)
  5802. (save-excursion
  5803. (narrow-to-region
  5804. (progn (org-back-to-heading) (point))
  5805. (progn (org-end-of-subtree t t) (point)))))
  5806. ;;; Outline Sorting
  5807. (defun org-sort (with-case)
  5808. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5809. Optional argument WITH-CASE means sort case-sensitively."
  5810. (interactive "P")
  5811. (if (org-at-table-p)
  5812. (org-call-with-arg 'org-table-sort-lines with-case)
  5813. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5814. (defvar org-priority-regexp) ; defined later in the file
  5815. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5816. "Sort entries on a certain level of an outline tree.
  5817. If there is an active region, the entries in the region are sorted.
  5818. Else, if the cursor is before the first entry, sort the top-level items.
  5819. Else, the children of the entry at point are sorted.
  5820. Sorting can be alphabetically, numerically, and by date/time as given by
  5821. the first time stamp in the entry. The command prompts for the sorting
  5822. type unless it has been given to the function through the SORTING-TYPE
  5823. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5824. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5825. called with point at the beginning of the record. It must return either
  5826. a string or a number that should serve as the sorting key for that record.
  5827. Comparing entries ignores case by default. However, with an optional argument
  5828. WITH-CASE, the sorting considers case as well."
  5829. (interactive "P")
  5830. (let ((case-func (if with-case 'identity 'downcase))
  5831. start beg end stars re re2
  5832. txt what tmp plain-list-p)
  5833. ;; Find beginning and end of region to sort
  5834. (cond
  5835. ((org-region-active-p)
  5836. ;; we will sort the region
  5837. (setq end (region-end)
  5838. what "region")
  5839. (goto-char (region-beginning))
  5840. (if (not (org-on-heading-p)) (outline-next-heading))
  5841. (setq start (point)))
  5842. ((org-at-item-p)
  5843. ;; we will sort this plain list
  5844. (org-beginning-of-item-list) (setq start (point))
  5845. (org-end-of-item-list) (setq end (point))
  5846. (goto-char start)
  5847. (setq plain-list-p t
  5848. what "plain list"))
  5849. ((or (org-on-heading-p)
  5850. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5851. ;; we will sort the children of the current headline
  5852. (org-back-to-heading)
  5853. (setq start (point) end (org-end-of-subtree) what "children")
  5854. (goto-char start)
  5855. (show-subtree)
  5856. (outline-next-heading))
  5857. (t
  5858. ;; we will sort the top-level entries in this file
  5859. (goto-char (point-min))
  5860. (or (org-on-heading-p) (outline-next-heading))
  5861. (setq start (point) end (point-max) what "top-level")
  5862. (goto-char start)
  5863. (show-all)))
  5864. (setq beg (point))
  5865. (if (>= beg end) (error "Nothing to sort"))
  5866. (unless plain-list-p
  5867. (looking-at "\\(\\*+\\)")
  5868. (setq stars (match-string 1)
  5869. re (concat "^" (regexp-quote stars) " +")
  5870. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5871. txt (buffer-substring beg end))
  5872. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5873. (if (and (not (equal stars "*")) (string-match re2 txt))
  5874. (error "Region to sort contains a level above the first entry")))
  5875. (unless sorting-type
  5876. (message
  5877. (if plain-list-p
  5878. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5879. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  5880. what)
  5881. (setq sorting-type (read-char-exclusive))
  5882. (and (= (downcase sorting-type) ?f)
  5883. (setq getkey-func
  5884. (completing-read "Sort using function: "
  5885. obarray 'fboundp t nil nil))
  5886. (setq getkey-func (intern getkey-func)))
  5887. (and (= (downcase sorting-type) ?r)
  5888. (setq property
  5889. (completing-read "Property: "
  5890. (mapcar 'list (org-buffer-property-keys t))
  5891. nil t))))
  5892. (message "Sorting entries...")
  5893. (save-restriction
  5894. (narrow-to-region start end)
  5895. (let ((dcst (downcase sorting-type))
  5896. (now (current-time)))
  5897. (sort-subr
  5898. (/= dcst sorting-type)
  5899. ;; This function moves to the beginning character of the "record" to
  5900. ;; be sorted.
  5901. (if plain-list-p
  5902. (lambda nil
  5903. (if (org-at-item-p) t (goto-char (point-max))))
  5904. (lambda nil
  5905. (if (re-search-forward re nil t)
  5906. (goto-char (match-beginning 0))
  5907. (goto-char (point-max)))))
  5908. ;; This function moves to the last character of the "record" being
  5909. ;; sorted.
  5910. (if plain-list-p
  5911. 'org-end-of-item
  5912. (lambda nil
  5913. (save-match-data
  5914. (condition-case nil
  5915. (outline-forward-same-level 1)
  5916. (error
  5917. (goto-char (point-max)))))))
  5918. ;; This function returns the value that gets sorted against.
  5919. (if plain-list-p
  5920. (lambda nil
  5921. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5922. (cond
  5923. ((= dcst ?n)
  5924. (string-to-number (buffer-substring (match-end 0)
  5925. (point-at-eol))))
  5926. ((= dcst ?a)
  5927. (buffer-substring (match-end 0) (point-at-eol)))
  5928. ((= dcst ?t)
  5929. (if (re-search-forward org-ts-regexp
  5930. (point-at-eol) t)
  5931. (org-time-string-to-time (match-string 0))
  5932. now))
  5933. ((= dcst ?f)
  5934. (if getkey-func
  5935. (progn
  5936. (setq tmp (funcall getkey-func))
  5937. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5938. tmp)
  5939. (error "Invalid key function `%s'" getkey-func)))
  5940. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5941. (lambda nil
  5942. (cond
  5943. ((= dcst ?n)
  5944. (if (looking-at outline-regexp)
  5945. (string-to-number (buffer-substring (match-end 0)
  5946. (point-at-eol)))
  5947. nil))
  5948. ((= dcst ?a)
  5949. (funcall case-func (buffer-substring (point-at-bol)
  5950. (point-at-eol))))
  5951. ((= dcst ?t)
  5952. (if (re-search-forward org-ts-regexp
  5953. (save-excursion
  5954. (forward-line 2)
  5955. (point)) t)
  5956. (org-time-string-to-time (match-string 0))
  5957. now))
  5958. ((= dcst ?p)
  5959. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5960. (string-to-char (match-string 2))
  5961. org-default-priority))
  5962. ((= dcst ?r)
  5963. (or (org-entry-get nil property) ""))
  5964. ((= dcst ?f)
  5965. (if getkey-func
  5966. (progn
  5967. (setq tmp (funcall getkey-func))
  5968. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5969. tmp)
  5970. (error "Invalid key function `%s'" getkey-func)))
  5971. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5972. nil
  5973. (cond
  5974. ((= dcst ?a) 'string<)
  5975. ((= dcst ?t) 'time-less-p)
  5976. (t nil)))))
  5977. (message "Sorting entries...done")))
  5978. (defun org-do-sort (table what &optional with-case sorting-type)
  5979. "Sort TABLE of WHAT according to SORTING-TYPE.
  5980. The user will be prompted for the SORTING-TYPE if the call to this
  5981. function does not specify it. WHAT is only for the prompt, to indicate
  5982. what is being sorted. The sorting key will be extracted from
  5983. the car of the elements of the table.
  5984. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5985. (unless sorting-type
  5986. (message
  5987. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5988. what)
  5989. (setq sorting-type (read-char-exclusive)))
  5990. (let ((dcst (downcase sorting-type))
  5991. extractfun comparefun)
  5992. ;; Define the appropriate functions
  5993. (cond
  5994. ((= dcst ?n)
  5995. (setq extractfun 'string-to-number
  5996. comparefun (if (= dcst sorting-type) '< '>)))
  5997. ((= dcst ?a)
  5998. (setq extractfun (if with-case 'identity 'downcase)
  5999. comparefun (if (= dcst sorting-type)
  6000. 'string<
  6001. (lambda (a b) (and (not (string< a b))
  6002. (not (string= a b)))))))
  6003. ((= dcst ?t)
  6004. (setq extractfun
  6005. (lambda (x)
  6006. (if (string-match org-ts-regexp x)
  6007. (time-to-seconds
  6008. (org-time-string-to-time (match-string 0 x)))
  6009. 0))
  6010. comparefun (if (= dcst sorting-type) '< '>)))
  6011. (t (error "Invalid sorting type `%c'" sorting-type)))
  6012. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6013. table)
  6014. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6015. ;;;; Plain list items, including checkboxes
  6016. ;;; Plain list items
  6017. (defun org-at-item-p ()
  6018. "Is point in a line starting a hand-formatted item?"
  6019. (let ((llt org-plain-list-ordered-item-terminator))
  6020. (save-excursion
  6021. (goto-char (point-at-bol))
  6022. (looking-at
  6023. (cond
  6024. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6025. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6026. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6027. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6028. (defun org-in-item-p ()
  6029. "It the cursor inside a plain list item.
  6030. Does not have to be the first line."
  6031. (save-excursion
  6032. (condition-case nil
  6033. (progn
  6034. (org-beginning-of-item)
  6035. (org-at-item-p)
  6036. t)
  6037. (error nil))))
  6038. (defun org-insert-item (&optional checkbox)
  6039. "Insert a new item at the current level.
  6040. Return t when things worked, nil when we are not in an item."
  6041. (when (save-excursion
  6042. (condition-case nil
  6043. (progn
  6044. (org-beginning-of-item)
  6045. (org-at-item-p)
  6046. (if (org-invisible-p) (error "Invisible item"))
  6047. t)
  6048. (error nil)))
  6049. (let* ((bul (match-string 0))
  6050. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6051. (match-end 0)))
  6052. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6053. pos)
  6054. (cond
  6055. ((and (org-at-item-p) (<= (point) eow))
  6056. ;; before the bullet
  6057. (beginning-of-line 1)
  6058. (open-line (if blank 2 1)))
  6059. ((<= (point) eow)
  6060. (beginning-of-line 1))
  6061. (t (newline (if blank 2 1))))
  6062. (insert bul (if checkbox "[ ]" ""))
  6063. (just-one-space)
  6064. (setq pos (point))
  6065. (end-of-line 1)
  6066. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6067. (org-maybe-renumber-ordered-list)
  6068. (and checkbox (org-update-checkbox-count-maybe))
  6069. t))
  6070. ;;; Checkboxes
  6071. (defun org-at-item-checkbox-p ()
  6072. "Is point at a line starting a plain-list item with a checklet?"
  6073. (and (org-at-item-p)
  6074. (save-excursion
  6075. (goto-char (match-end 0))
  6076. (skip-chars-forward " \t")
  6077. (looking-at "\\[[- X]\\]"))))
  6078. (defun org-toggle-checkbox (&optional arg)
  6079. "Toggle the checkbox in the current line."
  6080. (interactive "P")
  6081. (catch 'exit
  6082. (let (beg end status (firstnew 'unknown))
  6083. (cond
  6084. ((org-region-active-p)
  6085. (setq beg (region-beginning) end (region-end)))
  6086. ((org-on-heading-p)
  6087. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6088. ((org-at-item-checkbox-p)
  6089. (let ((pos (point)))
  6090. (replace-match
  6091. (cond (arg "[-]")
  6092. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6093. (t "[ ]"))
  6094. t t)
  6095. (goto-char pos))
  6096. (throw 'exit t))
  6097. (t (error "Not at a checkbox or heading, and no active region")))
  6098. (save-excursion
  6099. (goto-char beg)
  6100. (while (< (point) end)
  6101. (when (org-at-item-checkbox-p)
  6102. (setq status (equal (match-string 0) "[X]"))
  6103. (when (eq firstnew 'unknown)
  6104. (setq firstnew (not status)))
  6105. (replace-match
  6106. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6107. (beginning-of-line 2)))))
  6108. (org-update-checkbox-count-maybe))
  6109. (defun org-update-checkbox-count-maybe ()
  6110. "Update checkbox statistics unless turned off by user."
  6111. (when org-provide-checkbox-statistics
  6112. (org-update-checkbox-count)))
  6113. (defun org-update-checkbox-count (&optional all)
  6114. "Update the checkbox statistics in the current section.
  6115. This will find all statistic cookies like [57%] and [6/12] and update them
  6116. with the current numbers. With optional prefix argument ALL, do this for
  6117. the whole buffer."
  6118. (interactive "P")
  6119. (save-excursion
  6120. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6121. (beg (condition-case nil
  6122. (progn (outline-back-to-heading) (point))
  6123. (error (point-min))))
  6124. (end (move-marker (make-marker)
  6125. (progn (outline-next-heading) (point))))
  6126. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  6127. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6128. b1 e1 f1 c-on c-off lim (cstat 0))
  6129. (when all
  6130. (goto-char (point-min))
  6131. (outline-next-heading)
  6132. (setq beg (point) end (point-max)))
  6133. (goto-char beg)
  6134. (while (re-search-forward re end t)
  6135. (setq cstat (1+ cstat)
  6136. b1 (match-beginning 0)
  6137. e1 (match-end 0)
  6138. f1 (match-beginning 1)
  6139. lim (cond
  6140. ((org-on-heading-p) (outline-next-heading) (point))
  6141. ((org-at-item-p) (org-end-of-item) (point))
  6142. (t nil))
  6143. c-on 0 c-off 0)
  6144. (goto-char e1)
  6145. (when lim
  6146. (while (re-search-forward re-box lim t)
  6147. (if (member (match-string 2) '("[ ]" "[-]"))
  6148. (setq c-off (1+ c-off))
  6149. (setq c-on (1+ c-on))))
  6150. ; (delete-region b1 e1)
  6151. (goto-char b1)
  6152. (insert (if f1
  6153. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6154. (format "[%d/%d]" c-on (+ c-on c-off))))
  6155. (and (looking-at "\\[.*?\\]")
  6156. (replace-match ""))))
  6157. (when (interactive-p)
  6158. (message "Checkbox satistics updated %s (%d places)"
  6159. (if all "in entire file" "in current outline entry") cstat)))))
  6160. (defun org-get-checkbox-statistics-face ()
  6161. "Select the face for checkbox statistics.
  6162. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6163. it will be `org-todo'."
  6164. (if (match-end 1)
  6165. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6166. (if (and (> (match-end 2) (match-beginning 2))
  6167. (equal (match-string 2) (match-string 3)))
  6168. 'org-done
  6169. 'org-todo)))
  6170. (defun org-get-indentation (&optional line)
  6171. "Get the indentation of the current line, interpreting tabs.
  6172. When LINE is given, assume it represents a line and compute its indentation."
  6173. (if line
  6174. (if (string-match "^ *" (org-remove-tabs line))
  6175. (match-end 0))
  6176. (save-excursion
  6177. (beginning-of-line 1)
  6178. (skip-chars-forward " \t")
  6179. (current-column))))
  6180. (defun org-remove-tabs (s &optional width)
  6181. "Replace tabulators in S with spaces.
  6182. Assumes that s is a single line, starting in column 0."
  6183. (setq width (or width tab-width))
  6184. (while (string-match "\t" s)
  6185. (setq s (replace-match
  6186. (make-string
  6187. (- (* width (/ (+ (match-beginning 0) width) width))
  6188. (match-beginning 0)) ?\ )
  6189. t t s)))
  6190. s)
  6191. (defun org-fix-indentation (line ind)
  6192. "Fix indentation in LINE.
  6193. IND is a cons cell with target and minimum indentation.
  6194. If the current indenation in LINE is smaller than the minimum,
  6195. leave it alone. If it is larger than ind, set it to the target."
  6196. (let* ((l (org-remove-tabs line))
  6197. (i (org-get-indentation l))
  6198. (i1 (car ind)) (i2 (cdr ind)))
  6199. (if (>= i i2) (setq l (substring line i2)))
  6200. (if (> i1 0)
  6201. (concat (make-string i1 ?\ ) l)
  6202. l)))
  6203. (defcustom org-empty-line-terminates-plain-lists nil
  6204. "Non-nil means, an empty line ends all plain list levels.
  6205. When nil, empty lines are part of the preceeding item."
  6206. :group 'org-plain-lists
  6207. :type 'boolean)
  6208. (defun org-beginning-of-item ()
  6209. "Go to the beginning of the current hand-formatted item.
  6210. If the cursor is not in an item, throw an error."
  6211. (interactive)
  6212. (let ((pos (point))
  6213. (limit (save-excursion
  6214. (condition-case nil
  6215. (progn
  6216. (org-back-to-heading)
  6217. (beginning-of-line 2) (point))
  6218. (error (point-min)))))
  6219. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6220. ind ind1)
  6221. (if (org-at-item-p)
  6222. (beginning-of-line 1)
  6223. (beginning-of-line 1)
  6224. (skip-chars-forward " \t")
  6225. (setq ind (current-column))
  6226. (if (catch 'exit
  6227. (while t
  6228. (beginning-of-line 0)
  6229. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6230. (if (looking-at "[ \t]*$")
  6231. (setq ind1 ind-empty)
  6232. (skip-chars-forward " \t")
  6233. (setq ind1 (current-column)))
  6234. (if (< ind1 ind)
  6235. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6236. nil
  6237. (goto-char pos)
  6238. (error "Not in an item")))))
  6239. (defun org-end-of-item ()
  6240. "Go to the end of the current hand-formatted item.
  6241. If the cursor is not in an item, throw an error."
  6242. (interactive)
  6243. (let* ((pos (point))
  6244. ind1
  6245. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6246. (limit (save-excursion (outline-next-heading) (point)))
  6247. (ind (save-excursion
  6248. (org-beginning-of-item)
  6249. (skip-chars-forward " \t")
  6250. (current-column)))
  6251. (end (catch 'exit
  6252. (while t
  6253. (beginning-of-line 2)
  6254. (if (eobp) (throw 'exit (point)))
  6255. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6256. (if (looking-at "[ \t]*$")
  6257. (setq ind1 ind-empty)
  6258. (skip-chars-forward " \t")
  6259. (setq ind1 (current-column)))
  6260. (if (<= ind1 ind)
  6261. (throw 'exit (point-at-bol)))))))
  6262. (if end
  6263. (goto-char end)
  6264. (goto-char pos)
  6265. (error "Not in an item"))))
  6266. (defun org-next-item ()
  6267. "Move to the beginning of the next item in the current plain list.
  6268. Error if not at a plain list, or if this is the last item in the list."
  6269. (interactive)
  6270. (let (ind ind1 (pos (point)))
  6271. (org-beginning-of-item)
  6272. (setq ind (org-get-indentation))
  6273. (org-end-of-item)
  6274. (setq ind1 (org-get-indentation))
  6275. (unless (and (org-at-item-p) (= ind ind1))
  6276. (goto-char pos)
  6277. (error "On last item"))))
  6278. (defun org-previous-item ()
  6279. "Move to the beginning of the previous item in the current plain list.
  6280. Error if not at a plain list, or if this is the first item in the list."
  6281. (interactive)
  6282. (let (beg ind ind1 (pos (point)))
  6283. (org-beginning-of-item)
  6284. (setq beg (point))
  6285. (setq ind (org-get-indentation))
  6286. (goto-char beg)
  6287. (catch 'exit
  6288. (while t
  6289. (beginning-of-line 0)
  6290. (if (looking-at "[ \t]*$")
  6291. nil
  6292. (if (<= (setq ind1 (org-get-indentation)) ind)
  6293. (throw 'exit t)))))
  6294. (condition-case nil
  6295. (if (or (not (org-at-item-p))
  6296. (< ind1 (1- ind)))
  6297. (error "")
  6298. (org-beginning-of-item))
  6299. (error (goto-char pos)
  6300. (error "On first item")))))
  6301. (defun org-move-item-down ()
  6302. "Move the plain list item at point down, i.e. swap with following item.
  6303. Subitems (items with larger indentation) are considered part of the item,
  6304. so this really moves item trees."
  6305. (interactive)
  6306. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6307. (org-beginning-of-item)
  6308. (setq beg0 (point))
  6309. (save-excursion
  6310. (setq ne-beg (org-back-over-empty-lines))
  6311. (setq beg (point)))
  6312. (goto-char beg0)
  6313. (setq ind (org-get-indentation))
  6314. (org-end-of-item)
  6315. (setq end0 (point))
  6316. (setq ind1 (org-get-indentation))
  6317. (setq ne-end (org-back-over-empty-lines))
  6318. (setq end (point))
  6319. (goto-char beg0)
  6320. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6321. ;; include less whitespace
  6322. (save-excursion
  6323. (goto-char beg)
  6324. (forward-line (- ne-beg ne-end))
  6325. (setq beg (point))))
  6326. (goto-char end0)
  6327. (if (and (org-at-item-p) (= ind ind1))
  6328. (progn
  6329. (org-end-of-item)
  6330. (org-back-over-empty-lines)
  6331. (setq txt (buffer-substring beg end))
  6332. (save-excursion
  6333. (delete-region beg end))
  6334. (setq pos (point))
  6335. (insert txt)
  6336. (goto-char pos) (org-skip-whitespace)
  6337. (org-maybe-renumber-ordered-list))
  6338. (goto-char pos)
  6339. (error "Cannot move this item further down"))))
  6340. (defun org-move-item-up (arg)
  6341. "Move the plain list item at point up, i.e. swap with previous item.
  6342. Subitems (items with larger indentation) are considered part of the item,
  6343. so this really moves item trees."
  6344. (interactive "p")
  6345. (let (beg beg0 end end0 ind ind1 (pos (point)) txt
  6346. ne-beg ne-end ne-ins ins-end)
  6347. (org-beginning-of-item)
  6348. (setq beg0 (point))
  6349. (setq ind (org-get-indentation))
  6350. (save-excursion
  6351. (setq ne-beg (org-back-over-empty-lines))
  6352. (setq beg (point)))
  6353. (goto-char beg0)
  6354. (org-end-of-item)
  6355. (setq ne-end (org-back-over-empty-lines))
  6356. (setq end (point))
  6357. (goto-char beg0)
  6358. (catch 'exit
  6359. (while t
  6360. (beginning-of-line 0)
  6361. (if (looking-at "[ \t]*$")
  6362. (if org-empty-line-terminates-plain-lists
  6363. (progn
  6364. (goto-char pos)
  6365. (error "Cannot move this item further up"))
  6366. nil)
  6367. (if (<= (setq ind1 (org-get-indentation)) ind)
  6368. (throw 'exit t)))))
  6369. (condition-case nil
  6370. (org-beginning-of-item)
  6371. (error (goto-char beg)
  6372. (error "Cannot move this item further up")))
  6373. (setq ind1 (org-get-indentation))
  6374. (if (and (org-at-item-p) (= ind ind1))
  6375. (progn
  6376. (setq ne-ins (org-back-over-empty-lines))
  6377. (setq txt (buffer-substring beg end))
  6378. (save-excursion
  6379. (delete-region beg end))
  6380. (setq pos (point))
  6381. (insert txt)
  6382. (setq ins-end (point))
  6383. (goto-char pos) (org-skip-whitespace)
  6384. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6385. ;; Move whitespace back to beginning
  6386. (save-excursion
  6387. (goto-char ins-end)
  6388. (let ((kill-whole-line t))
  6389. (kill-line (- ne-ins ne-beg)) (point)))
  6390. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6391. (org-maybe-renumber-ordered-list))
  6392. (goto-char pos)
  6393. (error "Cannot move this item further up"))))
  6394. (defun org-maybe-renumber-ordered-list ()
  6395. "Renumber the ordered list at point if setup allows it.
  6396. This tests the user option `org-auto-renumber-ordered-lists' before
  6397. doing the renumbering."
  6398. (interactive)
  6399. (when (and org-auto-renumber-ordered-lists
  6400. (org-at-item-p))
  6401. (if (match-beginning 3)
  6402. (org-renumber-ordered-list 1)
  6403. (org-fix-bullet-type))))
  6404. (defun org-maybe-renumber-ordered-list-safe ()
  6405. (condition-case nil
  6406. (save-excursion
  6407. (org-maybe-renumber-ordered-list))
  6408. (error nil)))
  6409. (defun org-cycle-list-bullet (&optional which)
  6410. "Cycle through the different itemize/enumerate bullets.
  6411. This cycle the entire list level through the sequence:
  6412. `-' -> `+' -> `*' -> `1.' -> `1)'
  6413. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6414. 0 meand `-', 1 means `+' etc."
  6415. (interactive "P")
  6416. (org-preserve-lc
  6417. (org-beginning-of-item-list)
  6418. (org-at-item-p)
  6419. (beginning-of-line 1)
  6420. (let ((current (match-string 0))
  6421. (prevp (eq which 'previous))
  6422. new)
  6423. (setq new (cond
  6424. ((and (numberp which)
  6425. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6426. ((string-match "-" current) (if prevp "1)" "+"))
  6427. ((string-match "\\+" current)
  6428. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6429. ((string-match "\\*" current) (if prevp "+" "1."))
  6430. ((string-match "\\." current) (if prevp "*" "1)"))
  6431. ((string-match ")" current) (if prevp "1." "-"))
  6432. (t (error "This should not happen"))))
  6433. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6434. (org-fix-bullet-type)
  6435. (org-maybe-renumber-ordered-list))))
  6436. (defun org-get-string-indentation (s)
  6437. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6438. (let ((n -1) (i 0) (w tab-width) c)
  6439. (catch 'exit
  6440. (while (< (setq n (1+ n)) (length s))
  6441. (setq c (aref s n))
  6442. (cond ((= c ?\ ) (setq i (1+ i)))
  6443. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6444. (t (throw 'exit t)))))
  6445. i))
  6446. (defun org-renumber-ordered-list (arg)
  6447. "Renumber an ordered plain list.
  6448. Cursor needs to be in the first line of an item, the line that starts
  6449. with something like \"1.\" or \"2)\"."
  6450. (interactive "p")
  6451. (unless (and (org-at-item-p)
  6452. (match-beginning 3))
  6453. (error "This is not an ordered list"))
  6454. (let ((line (org-current-line))
  6455. (col (current-column))
  6456. (ind (org-get-string-indentation
  6457. (buffer-substring (point-at-bol) (match-beginning 3))))
  6458. ;; (term (substring (match-string 3) -1))
  6459. ind1 (n (1- arg))
  6460. fmt)
  6461. ;; find where this list begins
  6462. (org-beginning-of-item-list)
  6463. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6464. (setq fmt (concat "%d" (match-string 1)))
  6465. (beginning-of-line 0)
  6466. ;; walk forward and replace these numbers
  6467. (catch 'exit
  6468. (while t
  6469. (catch 'next
  6470. (beginning-of-line 2)
  6471. (if (eobp) (throw 'exit nil))
  6472. (if (looking-at "[ \t]*$") (throw 'next nil))
  6473. (skip-chars-forward " \t") (setq ind1 (current-column))
  6474. (if (> ind1 ind) (throw 'next t))
  6475. (if (< ind1 ind) (throw 'exit t))
  6476. (if (not (org-at-item-p)) (throw 'exit nil))
  6477. (delete-region (match-beginning 2) (match-end 2))
  6478. (goto-char (match-beginning 2))
  6479. (insert (format fmt (setq n (1+ n)))))))
  6480. (goto-line line)
  6481. (move-to-column col)))
  6482. (defun org-fix-bullet-type ()
  6483. "Make sure all items in this list have the same bullet as the firsst item."
  6484. (interactive)
  6485. (unless (org-at-item-p) (error "This is not a list"))
  6486. (let ((line (org-current-line))
  6487. (col (current-column))
  6488. (ind (current-indentation))
  6489. ind1 bullet)
  6490. ;; find where this list begins
  6491. (org-beginning-of-item-list)
  6492. (beginning-of-line 1)
  6493. ;; find out what the bullet type is
  6494. (looking-at "[ \t]*\\(\\S-+\\)")
  6495. (setq bullet (match-string 1))
  6496. ;; walk forward and replace these numbers
  6497. (beginning-of-line 0)
  6498. (catch 'exit
  6499. (while t
  6500. (catch 'next
  6501. (beginning-of-line 2)
  6502. (if (eobp) (throw 'exit nil))
  6503. (if (looking-at "[ \t]*$") (throw 'next nil))
  6504. (skip-chars-forward " \t") (setq ind1 (current-column))
  6505. (if (> ind1 ind) (throw 'next t))
  6506. (if (< ind1 ind) (throw 'exit t))
  6507. (if (not (org-at-item-p)) (throw 'exit nil))
  6508. (skip-chars-forward " \t")
  6509. (looking-at "\\S-+")
  6510. (replace-match bullet))))
  6511. (goto-line line)
  6512. (move-to-column col)
  6513. (if (string-match "[0-9]" bullet)
  6514. (org-renumber-ordered-list 1))))
  6515. (defun org-beginning-of-item-list ()
  6516. "Go to the beginning of the current item list.
  6517. I.e. to the first item in this list."
  6518. (interactive)
  6519. (org-beginning-of-item)
  6520. (let ((pos (point-at-bol))
  6521. (ind (org-get-indentation))
  6522. ind1)
  6523. ;; find where this list begins
  6524. (catch 'exit
  6525. (while t
  6526. (catch 'next
  6527. (beginning-of-line 0)
  6528. (if (looking-at "[ \t]*$")
  6529. (throw (if (bobp) 'exit 'next) t))
  6530. (skip-chars-forward " \t") (setq ind1 (current-column))
  6531. (if (or (< ind1 ind)
  6532. (and (= ind1 ind)
  6533. (not (org-at-item-p)))
  6534. (bobp))
  6535. (throw 'exit t)
  6536. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6537. (goto-char pos)))
  6538. (defun org-end-of-item-list ()
  6539. "Go to the end of the current item list.
  6540. I.e. to the text after the last item."
  6541. (interactive)
  6542. (org-beginning-of-item)
  6543. (let ((pos (point-at-bol))
  6544. (ind (org-get-indentation))
  6545. ind1)
  6546. ;; find where this list begins
  6547. (catch 'exit
  6548. (while t
  6549. (catch 'next
  6550. (beginning-of-line 2)
  6551. (if (looking-at "[ \t]*$")
  6552. (throw (if (eobp) 'exit 'next) t))
  6553. (skip-chars-forward " \t") (setq ind1 (current-column))
  6554. (if (or (< ind1 ind)
  6555. (and (= ind1 ind)
  6556. (not (org-at-item-p)))
  6557. (eobp))
  6558. (progn
  6559. (setq pos (point-at-bol))
  6560. (throw 'exit t))))))
  6561. (goto-char pos)))
  6562. (defvar org-last-indent-begin-marker (make-marker))
  6563. (defvar org-last-indent-end-marker (make-marker))
  6564. (defun org-outdent-item (arg)
  6565. "Outdent a local list item."
  6566. (interactive "p")
  6567. (org-indent-item (- arg)))
  6568. (defun org-indent-item (arg)
  6569. "Indent a local list item."
  6570. (interactive "p")
  6571. (unless (org-at-item-p)
  6572. (error "Not on an item"))
  6573. (save-excursion
  6574. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6575. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6576. (setq beg org-last-indent-begin-marker
  6577. end org-last-indent-end-marker)
  6578. (org-beginning-of-item)
  6579. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6580. (org-end-of-item)
  6581. (setq end (move-marker org-last-indent-end-marker (point))))
  6582. (goto-char beg)
  6583. (setq tmp (org-item-indent-positions)
  6584. ind (car tmp)
  6585. ind-down (nth 2 tmp)
  6586. ind-up (nth 1 tmp)
  6587. delta (if (> arg 0)
  6588. (if ind-down (- ind-down ind) 2)
  6589. (if ind-up (- ind-up ind) -2)))
  6590. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6591. (while (< (point) end)
  6592. (beginning-of-line 1)
  6593. (skip-chars-forward " \t") (setq ind1 (current-column))
  6594. (delete-region (point-at-bol) (point))
  6595. (or (eolp) (indent-to-column (+ ind1 delta)))
  6596. (beginning-of-line 2))))
  6597. (org-fix-bullet-type)
  6598. (org-maybe-renumber-ordered-list-safe)
  6599. (save-excursion
  6600. (beginning-of-line 0)
  6601. (condition-case nil (org-beginning-of-item) (error nil))
  6602. (org-maybe-renumber-ordered-list-safe)))
  6603. (defun org-item-indent-positions ()
  6604. "Return indentation for plain list items.
  6605. This returns a list with three values: The current indentation, the
  6606. parent indentation and the indentation a child should habe.
  6607. Assumes cursor in item line."
  6608. (let* ((bolpos (point-at-bol))
  6609. (ind (org-get-indentation))
  6610. ind-down ind-up pos)
  6611. (save-excursion
  6612. (org-beginning-of-item-list)
  6613. (skip-chars-backward "\n\r \t")
  6614. (when (org-in-item-p)
  6615. (org-beginning-of-item)
  6616. (setq ind-up (org-get-indentation))))
  6617. (setq pos (point))
  6618. (save-excursion
  6619. (cond
  6620. ((and (condition-case nil (progn (org-previous-item) t)
  6621. (error nil))
  6622. (or (forward-char 1) t)
  6623. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6624. (setq ind-down (org-get-indentation)))
  6625. ((and (goto-char pos)
  6626. (org-at-item-p))
  6627. (goto-char (match-end 0))
  6628. (skip-chars-forward " \t")
  6629. (setq ind-down (current-column)))))
  6630. (list ind ind-up ind-down)))
  6631. ;;; The orgstruct minor mode
  6632. ;; Define a minor mode which can be used in other modes in order to
  6633. ;; integrate the org-mode structure editing commands.
  6634. ;; This is really a hack, because the org-mode structure commands use
  6635. ;; keys which normally belong to the major mode. Here is how it
  6636. ;; works: The minor mode defines all the keys necessary to operate the
  6637. ;; structure commands, but wraps the commands into a function which
  6638. ;; tests if the cursor is currently at a headline or a plain list
  6639. ;; item. If that is the case, the structure command is used,
  6640. ;; temporarily setting many Org-mode variables like regular
  6641. ;; expressions for filling etc. However, when any of those keys is
  6642. ;; used at a different location, function uses `key-binding' to look
  6643. ;; up if the key has an associated command in another currently active
  6644. ;; keymap (minor modes, major mode, global), and executes that
  6645. ;; command. There might be problems if any of the keys is otherwise
  6646. ;; used as a prefix key.
  6647. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6648. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6649. ;; addresses this by checking explicitly for both bindings.
  6650. (defvar orgstruct-mode-map (make-sparse-keymap)
  6651. "Keymap for the minor `orgstruct-mode'.")
  6652. (defvar org-local-vars nil
  6653. "List of local variables, for use by `orgstruct-mode'")
  6654. ;;;###autoload
  6655. (define-minor-mode orgstruct-mode
  6656. "Toggle the minor more `orgstruct-mode'.
  6657. This mode is for using Org-mode structure commands in other modes.
  6658. The following key behave as if Org-mode was active, if the cursor
  6659. is on a headline, or on a plain list item (both in the definition
  6660. of Org-mode).
  6661. M-up Move entry/item up
  6662. M-down Move entry/item down
  6663. M-left Promote
  6664. M-right Demote
  6665. M-S-up Move entry/item up
  6666. M-S-down Move entry/item down
  6667. M-S-left Promote subtree
  6668. M-S-right Demote subtree
  6669. M-q Fill paragraph and items like in Org-mode
  6670. C-c ^ Sort entries
  6671. C-c - Cycle list bullet
  6672. TAB Cycle item visibility
  6673. M-RET Insert new heading/item
  6674. S-M-RET Insert new TODO heading / Chekbox item
  6675. C-c C-c Set tags / toggle checkbox"
  6676. nil " OrgStruct" nil
  6677. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6678. ;;;###autoload
  6679. (defun turn-on-orgstruct ()
  6680. "Unconditionally turn on `orgstruct-mode'."
  6681. (orgstruct-mode 1))
  6682. ;;;###autoload
  6683. (defun turn-on-orgstruct++ ()
  6684. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6685. In addition to setting orgstruct-mode, this also exports all indentation and
  6686. autofilling variables from org-mode into the buffer. Note that turning
  6687. off orgstruct-mode will *not* remove these additonal settings."
  6688. (orgstruct-mode 1)
  6689. (let (var val)
  6690. (mapc
  6691. (lambda (x)
  6692. (when (string-match
  6693. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6694. (symbol-name (car x)))
  6695. (setq var (car x) val (nth 1 x))
  6696. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6697. org-local-vars)))
  6698. (defun orgstruct-error ()
  6699. "Error when there is no default binding for a structure key."
  6700. (interactive)
  6701. (error "This key is has no function outside structure elements"))
  6702. (defun orgstruct-setup ()
  6703. "Setup orgstruct keymaps."
  6704. (let ((nfunc 0)
  6705. (bindings
  6706. (list
  6707. '([(meta up)] org-metaup)
  6708. '([(meta down)] org-metadown)
  6709. '([(meta left)] org-metaleft)
  6710. '([(meta right)] org-metaright)
  6711. '([(meta shift up)] org-shiftmetaup)
  6712. '([(meta shift down)] org-shiftmetadown)
  6713. '([(meta shift left)] org-shiftmetaleft)
  6714. '([(meta shift right)] org-shiftmetaright)
  6715. '([(shift up)] org-shiftup)
  6716. '([(shift down)] org-shiftdown)
  6717. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6718. '("\M-q" fill-paragraph)
  6719. '("\C-c^" org-sort)
  6720. '("\C-c-" org-cycle-list-bullet)))
  6721. elt key fun cmd)
  6722. (while (setq elt (pop bindings))
  6723. (setq nfunc (1+ nfunc))
  6724. (setq key (org-key (car elt))
  6725. fun (nth 1 elt)
  6726. cmd (orgstruct-make-binding fun nfunc key))
  6727. (org-defkey orgstruct-mode-map key cmd))
  6728. ;; Special treatment needed for TAB and RET
  6729. (org-defkey orgstruct-mode-map [(tab)]
  6730. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6731. (org-defkey orgstruct-mode-map "\C-i"
  6732. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6733. (org-defkey orgstruct-mode-map "\M-\C-m"
  6734. (orgstruct-make-binding 'org-insert-heading 105
  6735. "\M-\C-m" [(meta return)]))
  6736. (org-defkey orgstruct-mode-map [(meta return)]
  6737. (orgstruct-make-binding 'org-insert-heading 106
  6738. [(meta return)] "\M-\C-m"))
  6739. (org-defkey orgstruct-mode-map [(shift meta return)]
  6740. (orgstruct-make-binding 'org-insert-todo-heading 107
  6741. [(meta return)] "\M-\C-m"))
  6742. (unless org-local-vars
  6743. (setq org-local-vars (org-get-local-variables)))
  6744. t))
  6745. (defun orgstruct-make-binding (fun n &rest keys)
  6746. "Create a function for binding in the structure minor mode.
  6747. FUN is the command to call inside a table. N is used to create a unique
  6748. command name. KEYS are keys that should be checked in for a command
  6749. to execute outside of tables."
  6750. (eval
  6751. (list 'defun
  6752. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6753. '(arg)
  6754. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6755. "Outside of structure, run the binding of `"
  6756. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6757. "'.")
  6758. '(interactive "p")
  6759. (list 'if
  6760. '(org-context-p 'headline 'item)
  6761. (list 'org-run-like-in-org-mode (list 'quote fun))
  6762. (list 'let '(orgstruct-mode)
  6763. (list 'call-interactively
  6764. (append '(or)
  6765. (mapcar (lambda (k)
  6766. (list 'key-binding k))
  6767. keys)
  6768. '('orgstruct-error))))))))
  6769. (defun org-context-p (&rest contexts)
  6770. "Check if local context is and of CONTEXTS.
  6771. Possible values in the list of contexts are `table', `headline', and `item'."
  6772. (let ((pos (point)))
  6773. (goto-char (point-at-bol))
  6774. (prog1 (or (and (memq 'table contexts)
  6775. (looking-at "[ \t]*|"))
  6776. (and (memq 'headline contexts)
  6777. (looking-at "\\*+"))
  6778. (and (memq 'item contexts)
  6779. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6780. (goto-char pos))))
  6781. (defun org-get-local-variables ()
  6782. "Return a list of all local variables in an org-mode buffer."
  6783. (let (varlist)
  6784. (with-current-buffer (get-buffer-create "*Org tmp*")
  6785. (erase-buffer)
  6786. (org-mode)
  6787. (setq varlist (buffer-local-variables)))
  6788. (kill-buffer "*Org tmp*")
  6789. (delq nil
  6790. (mapcar
  6791. (lambda (x)
  6792. (setq x
  6793. (if (symbolp x)
  6794. (list x)
  6795. (list (car x) (list 'quote (cdr x)))))
  6796. (if (string-match
  6797. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6798. (symbol-name (car x)))
  6799. x nil))
  6800. varlist))))
  6801. ;;;###autoload
  6802. (defun org-run-like-in-org-mode (cmd)
  6803. (unless org-local-vars
  6804. (setq org-local-vars (org-get-local-variables)))
  6805. (eval (list 'let org-local-vars
  6806. (list 'call-interactively (list 'quote cmd)))))
  6807. ;;;; Archiving
  6808. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6809. (defun org-archive-subtree (&optional find-done)
  6810. "Move the current subtree to the archive.
  6811. The archive can be a certain top-level heading in the current file, or in
  6812. a different file. The tree will be moved to that location, the subtree
  6813. heading be marked DONE, and the current time will be added.
  6814. When called with prefix argument FIND-DONE, find whole trees without any
  6815. open TODO items and archive them (after getting confirmation from the user).
  6816. If the cursor is not at a headline when this comand is called, try all level
  6817. 1 trees. If the cursor is on a headline, only try the direct children of
  6818. this heading."
  6819. (interactive "P")
  6820. (if find-done
  6821. (org-archive-all-done)
  6822. ;; Save all relevant TODO keyword-relatex variables
  6823. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6824. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6825. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6826. (tr-org-done-keywords org-done-keywords)
  6827. (tr-org-todo-regexp org-todo-regexp)
  6828. (tr-org-todo-line-regexp org-todo-line-regexp)
  6829. (tr-org-odd-levels-only org-odd-levels-only)
  6830. (this-buffer (current-buffer))
  6831. (org-archive-location org-archive-location)
  6832. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6833. ;; start of variables that will be used for saving context
  6834. ;; The compiler complains about them - keep them anyway!
  6835. (file (abbreviate-file-name (buffer-file-name)))
  6836. (time (format-time-string
  6837. (substring (cdr org-time-stamp-formats) 1 -1)
  6838. (current-time)))
  6839. afile heading buffer level newfile-p
  6840. category todo priority
  6841. ;; start of variables that will be used for savind context
  6842. ltags itags prop)
  6843. ;; Try to find a local archive location
  6844. (save-excursion
  6845. (save-restriction
  6846. (widen)
  6847. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6848. (if (and prop (string-match "\\S-" prop))
  6849. (setq org-archive-location prop)
  6850. (if (or (re-search-backward re nil t)
  6851. (re-search-forward re nil t))
  6852. (setq org-archive-location (match-string 1))))))
  6853. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6854. (progn
  6855. (setq afile (format (match-string 1 org-archive-location)
  6856. (file-name-nondirectory buffer-file-name))
  6857. heading (match-string 2 org-archive-location)))
  6858. (error "Invalid `org-archive-location'"))
  6859. (if (> (length afile) 0)
  6860. (setq newfile-p (not (file-exists-p afile))
  6861. buffer (find-file-noselect afile))
  6862. (setq buffer (current-buffer)))
  6863. (unless buffer
  6864. (error "Cannot access file \"%s\"" afile))
  6865. (if (and (> (length heading) 0)
  6866. (string-match "^\\*+" heading))
  6867. (setq level (match-end 0))
  6868. (setq heading nil level 0))
  6869. (save-excursion
  6870. (org-back-to-heading t)
  6871. ;; Get context information that will be lost by moving the tree
  6872. (org-refresh-category-properties)
  6873. (setq category (org-get-category)
  6874. todo (and (looking-at org-todo-line-regexp)
  6875. (match-string 2))
  6876. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  6877. ltags (org-get-tags)
  6878. itags (org-delete-all ltags (org-get-tags-at)))
  6879. (setq ltags (mapconcat 'identity ltags " ")
  6880. itags (mapconcat 'identity itags " "))
  6881. ;; We first only copy, in case something goes wrong
  6882. ;; we need to protect this-command, to avoid kill-region sets it,
  6883. ;; which would lead to duplication of subtrees
  6884. (let (this-command) (org-copy-subtree))
  6885. (set-buffer buffer)
  6886. ;; Enforce org-mode for the archive buffer
  6887. (if (not (org-mode-p))
  6888. ;; Force the mode for future visits.
  6889. (let ((org-insert-mode-line-in-empty-file t)
  6890. (org-inhibit-startup t))
  6891. (call-interactively 'org-mode)))
  6892. (when newfile-p
  6893. (goto-char (point-max))
  6894. (insert (format "\nArchived entries from file %s\n\n"
  6895. (buffer-file-name this-buffer))))
  6896. ;; Force the TODO keywords of the original buffer
  6897. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  6898. (org-todo-keywords-1 tr-org-todo-keywords-1)
  6899. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  6900. (org-done-keywords tr-org-done-keywords)
  6901. (org-todo-regexp tr-org-todo-regexp)
  6902. (org-todo-line-regexp tr-org-todo-line-regexp)
  6903. (org-odd-levels-only
  6904. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  6905. org-odd-levels-only
  6906. tr-org-odd-levels-only)))
  6907. (goto-char (point-min))
  6908. (if heading
  6909. (progn
  6910. (if (re-search-forward
  6911. (concat "^" (regexp-quote heading)
  6912. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  6913. nil t)
  6914. (goto-char (match-end 0))
  6915. ;; Heading not found, just insert it at the end
  6916. (goto-char (point-max))
  6917. (or (bolp) (insert "\n"))
  6918. (insert "\n" heading "\n")
  6919. (end-of-line 0))
  6920. ;; Make the subtree visible
  6921. (show-subtree)
  6922. (org-end-of-subtree t)
  6923. (skip-chars-backward " \t\r\n")
  6924. (and (looking-at "[ \t\r\n]*")
  6925. (replace-match "\n\n")))
  6926. ;; No specific heading, just go to end of file.
  6927. (goto-char (point-max)) (insert "\n"))
  6928. ;; Paste
  6929. (org-paste-subtree (org-get-legal-level level 1))
  6930. ;; Mark the entry as done
  6931. (when (and org-archive-mark-done
  6932. (looking-at org-todo-line-regexp)
  6933. (or (not (match-end 2))
  6934. (not (member (match-string 2) org-done-keywords))))
  6935. (let (org-log-done)
  6936. (org-todo
  6937. (car (or (member org-archive-mark-done org-done-keywords)
  6938. org-done-keywords)))))
  6939. ;; Add the context info
  6940. (when org-archive-save-context-info
  6941. (let ((l org-archive-save-context-info) e n v)
  6942. (while (setq e (pop l))
  6943. (when (and (setq v (symbol-value e))
  6944. (stringp v) (string-match "\\S-" v))
  6945. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  6946. (org-entry-put (point) n v)))))
  6947. ;; Save the buffer, if it is not the same buffer.
  6948. (if (not (eq this-buffer buffer)) (save-buffer))))
  6949. ;; Here we are back in the original buffer. Everything seems to have
  6950. ;; worked. So now cut the tree and finish up.
  6951. (let (this-command) (org-cut-subtree))
  6952. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  6953. (message "Subtree archived %s"
  6954. (if (eq this-buffer buffer)
  6955. (concat "under heading: " heading)
  6956. (concat "in file: " (abbreviate-file-name afile)))))))
  6957. (defun org-refresh-category-properties ()
  6958. "Refresh category text properties in teh buffer."
  6959. (let ((def-cat (cond
  6960. ((null org-category)
  6961. (if buffer-file-name
  6962. (file-name-sans-extension
  6963. (file-name-nondirectory buffer-file-name))
  6964. "???"))
  6965. ((symbolp org-category) (symbol-name org-category))
  6966. (t org-category)))
  6967. beg end cat pos optionp)
  6968. (org-unmodified
  6969. (save-excursion
  6970. (save-restriction
  6971. (widen)
  6972. (goto-char (point-min))
  6973. (put-text-property (point) (point-max) 'org-category def-cat)
  6974. (while (re-search-forward
  6975. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6976. (setq pos (match-end 0)
  6977. optionp (equal (char-after (match-beginning 0)) ?#)
  6978. cat (org-trim (match-string 2)))
  6979. (if optionp
  6980. (setq beg (point-at-bol) end (point-max))
  6981. (org-back-to-heading t)
  6982. (setq beg (point) end (org-end-of-subtree t t)))
  6983. (put-text-property beg end 'org-category cat)
  6984. (goto-char pos)))))))
  6985. (defun org-archive-all-done (&optional tag)
  6986. "Archive sublevels of the current tree without open TODO items.
  6987. If the cursor is not on a headline, try all level 1 trees. If
  6988. it is on a headline, try all direct children.
  6989. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  6990. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  6991. (rea (concat ".*:" org-archive-tag ":"))
  6992. (begm (make-marker))
  6993. (endm (make-marker))
  6994. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  6995. "Move subtree to archive (no open TODO items)? "))
  6996. beg end (cntarch 0))
  6997. (if (org-on-heading-p)
  6998. (progn
  6999. (setq re1 (concat "^" (regexp-quote
  7000. (make-string
  7001. (1+ (- (match-end 0) (match-beginning 0)))
  7002. ?*))
  7003. " "))
  7004. (move-marker begm (point))
  7005. (move-marker endm (org-end-of-subtree t)))
  7006. (setq re1 "^* ")
  7007. (move-marker begm (point-min))
  7008. (move-marker endm (point-max)))
  7009. (save-excursion
  7010. (goto-char begm)
  7011. (while (re-search-forward re1 endm t)
  7012. (setq beg (match-beginning 0)
  7013. end (save-excursion (org-end-of-subtree t) (point)))
  7014. (goto-char beg)
  7015. (if (re-search-forward re end t)
  7016. (goto-char end)
  7017. (goto-char beg)
  7018. (if (and (or (not tag) (not (looking-at rea)))
  7019. (y-or-n-p question))
  7020. (progn
  7021. (if tag
  7022. (org-toggle-tag org-archive-tag 'on)
  7023. (org-archive-subtree))
  7024. (setq cntarch (1+ cntarch)))
  7025. (goto-char end)))))
  7026. (message "%d trees archived" cntarch)))
  7027. (defun org-cycle-hide-drawers (state)
  7028. "Re-hide all drawers after a visibility state change."
  7029. (when (and (org-mode-p)
  7030. (not (memq state '(overview folded))))
  7031. (save-excursion
  7032. (let* ((globalp (memq state '(contents all)))
  7033. (beg (if globalp (point-min) (point)))
  7034. (end (if globalp (point-max) (org-end-of-subtree t))))
  7035. (goto-char beg)
  7036. (while (re-search-forward org-drawer-regexp end t)
  7037. (org-flag-drawer t))))))
  7038. (defun org-flag-drawer (flag)
  7039. (save-excursion
  7040. (beginning-of-line 1)
  7041. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7042. (let ((b (match-end 0)))
  7043. (if (re-search-forward
  7044. "^[ \t]*:END:"
  7045. (save-excursion (outline-next-heading) (point)) t)
  7046. (outline-flag-region b (point-at-eol) flag)
  7047. (error ":END: line missing"))))))
  7048. (defun org-cycle-hide-archived-subtrees (state)
  7049. "Re-hide all archived subtrees after a visibility state change."
  7050. (when (and (not org-cycle-open-archived-trees)
  7051. (not (memq state '(overview folded))))
  7052. (save-excursion
  7053. (let* ((globalp (memq state '(contents all)))
  7054. (beg (if globalp (point-min) (point)))
  7055. (end (if globalp (point-max) (org-end-of-subtree t))))
  7056. (org-hide-archived-subtrees beg end)
  7057. (goto-char beg)
  7058. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7059. (message "%s" (substitute-command-keys
  7060. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7061. (defun org-force-cycle-archived ()
  7062. "Cycle subtree even if it is archived."
  7063. (interactive)
  7064. (setq this-command 'org-cycle)
  7065. (let ((org-cycle-open-archived-trees t))
  7066. (call-interactively 'org-cycle)))
  7067. (defun org-hide-archived-subtrees (beg end)
  7068. "Re-hide all archived subtrees after a visibility state change."
  7069. (save-excursion
  7070. (let* ((re (concat ":" org-archive-tag ":")))
  7071. (goto-char beg)
  7072. (while (re-search-forward re end t)
  7073. (and (org-on-heading-p) (hide-subtree))
  7074. (org-end-of-subtree t)))))
  7075. (defun org-toggle-tag (tag &optional onoff)
  7076. "Toggle the tag TAG for the current line.
  7077. If ONOFF is `on' or `off', don't toggle but set to this state."
  7078. (unless (org-on-heading-p t) (error "Not on headling"))
  7079. (let (res current)
  7080. (save-excursion
  7081. (beginning-of-line)
  7082. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7083. (point-at-eol) t)
  7084. (progn
  7085. (setq current (match-string 1))
  7086. (replace-match ""))
  7087. (setq current ""))
  7088. (setq current (nreverse (org-split-string current ":")))
  7089. (cond
  7090. ((eq onoff 'on)
  7091. (setq res t)
  7092. (or (member tag current) (push tag current)))
  7093. ((eq onoff 'off)
  7094. (or (not (member tag current)) (setq current (delete tag current))))
  7095. (t (if (member tag current)
  7096. (setq current (delete tag current))
  7097. (setq res t)
  7098. (push tag current))))
  7099. (end-of-line 1)
  7100. (if current
  7101. (progn
  7102. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7103. (org-set-tags nil t))
  7104. (delete-horizontal-space))
  7105. (run-hooks 'org-after-tags-change-hook))
  7106. res))
  7107. (defun org-toggle-archive-tag (&optional arg)
  7108. "Toggle the archive tag for the current headline.
  7109. With prefix ARG, check all children of current headline and offer tagging
  7110. the children that do not contain any open TODO items."
  7111. (interactive "P")
  7112. (if arg
  7113. (org-archive-all-done 'tag)
  7114. (let (set)
  7115. (save-excursion
  7116. (org-back-to-heading t)
  7117. (setq set (org-toggle-tag org-archive-tag))
  7118. (when set (hide-subtree)))
  7119. (and set (beginning-of-line 1))
  7120. (message "Subtree %s" (if set "archived" "unarchived")))))
  7121. ;;;; Tables
  7122. ;;; The table editor
  7123. ;; Watch out: Here we are talking about two different kind of tables.
  7124. ;; Most of the code is for the tables created with the Org-mode table editor.
  7125. ;; Sometimes, we talk about tables created and edited with the table.el
  7126. ;; Emacs package. We call the former org-type tables, and the latter
  7127. ;; table.el-type tables.
  7128. (defun org-before-change-function (beg end)
  7129. "Every change indicates that a table might need an update."
  7130. (setq org-table-may-need-update t))
  7131. (defconst org-table-line-regexp "^[ \t]*|"
  7132. "Detects an org-type table line.")
  7133. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7134. "Detects an org-type table line.")
  7135. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7136. "Detects a table line marked for automatic recalculation.")
  7137. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7138. "Detects a table line marked for automatic recalculation.")
  7139. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7140. "Detects a table line marked for automatic recalculation.")
  7141. (defconst org-table-hline-regexp "^[ \t]*|-"
  7142. "Detects an org-type table hline.")
  7143. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7144. "Detects a table-type table hline.")
  7145. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7146. "Detects an org-type or table-type table.")
  7147. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7148. "Searching from within a table (any type) this finds the first line
  7149. outside the table.")
  7150. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7151. "Searching from within a table (any type) this finds the first line
  7152. outside the table.")
  7153. (defvar org-table-last-highlighted-reference nil)
  7154. (defvar org-table-formula-history nil)
  7155. (defvar org-table-column-names nil
  7156. "Alist with column names, derived from the `!' line.")
  7157. (defvar org-table-column-name-regexp nil
  7158. "Regular expression matching the current column names.")
  7159. (defvar org-table-local-parameters nil
  7160. "Alist with parameter names, derived from the `$' line.")
  7161. (defvar org-table-named-field-locations nil
  7162. "Alist with locations of named fields.")
  7163. (defvar org-table-current-line-types nil
  7164. "Table row types, non-nil only for the duration of a comand.")
  7165. (defvar org-table-current-begin-line nil
  7166. "Table begin line, non-nil only for the duration of a comand.")
  7167. (defvar org-table-current-begin-pos nil
  7168. "Table begin position, non-nil only for the duration of a comand.")
  7169. (defvar org-table-dlines nil
  7170. "Vector of data line line numbers in the current table.")
  7171. (defvar org-table-hlines nil
  7172. "Vector of hline line numbers in the current table.")
  7173. (defconst org-table-range-regexp
  7174. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7175. ;; 1 2 3 4 5
  7176. "Regular expression for matching ranges in formulas.")
  7177. (defconst org-table-range-regexp2
  7178. (concat
  7179. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7180. "\\.\\."
  7181. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7182. "Match a range for reference display.")
  7183. (defconst org-table-translate-regexp
  7184. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7185. "Match a reference that needs translation, for reference display.")
  7186. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7187. (defun org-table-create-with-table.el ()
  7188. "Use the table.el package to insert a new table.
  7189. If there is already a table at point, convert between Org-mode tables
  7190. and table.el tables."
  7191. (interactive)
  7192. (require 'table)
  7193. (cond
  7194. ((org-at-table.el-p)
  7195. (if (y-or-n-p "Convert table to Org-mode table? ")
  7196. (org-table-convert)))
  7197. ((org-at-table-p)
  7198. (if (y-or-n-p "Convert table to table.el table? ")
  7199. (org-table-convert)))
  7200. (t (call-interactively 'table-insert))))
  7201. (defun org-table-create-or-convert-from-region (arg)
  7202. "Convert region to table, or create an empty table.
  7203. If there is an active region, convert it to a table, using the function
  7204. `org-table-convert-region'. See the documentation of that function
  7205. to learn how the prefix argument is interpreted to determine the field
  7206. separator.
  7207. If there is no such region, create an empty table with `org-table-create'."
  7208. (interactive "P")
  7209. (if (org-region-active-p)
  7210. (org-table-convert-region (region-beginning) (region-end) arg)
  7211. (org-table-create arg)))
  7212. (defun org-table-create (&optional size)
  7213. "Query for a size and insert a table skeleton.
  7214. SIZE is a string Columns x Rows like for example \"3x2\"."
  7215. (interactive "P")
  7216. (unless size
  7217. (setq size (read-string
  7218. (concat "Table size Columns x Rows [e.g. "
  7219. org-table-default-size "]: ")
  7220. "" nil org-table-default-size)))
  7221. (let* ((pos (point))
  7222. (indent (make-string (current-column) ?\ ))
  7223. (split (org-split-string size " *x *"))
  7224. (rows (string-to-number (nth 1 split)))
  7225. (columns (string-to-number (car split)))
  7226. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7227. "\n")))
  7228. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7229. (point-at-bol) (point)))
  7230. (beginning-of-line 1)
  7231. (newline))
  7232. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7233. (dotimes (i rows) (insert line))
  7234. (goto-char pos)
  7235. (if (> rows 1)
  7236. ;; Insert a hline after the first row.
  7237. (progn
  7238. (end-of-line 1)
  7239. (insert "\n|-")
  7240. (goto-char pos)))
  7241. (org-table-align)))
  7242. (defun org-table-convert-region (beg0 end0 &optional separator)
  7243. "Convert region to a table.
  7244. The region goes from BEG0 to END0, but these borders will be moved
  7245. slightly, to make sure a beginning of line in the first line is included.
  7246. SEPARATOR specifies the field separator in the lines. It can have the
  7247. following values:
  7248. '(4) Use the comma as a field separator
  7249. '(16) Use a TAB as field separator
  7250. integer When a number, use that many spaces as field separator
  7251. nil When nil, the command tries to be smart and figure out the
  7252. separator in the following way:
  7253. - when each line contains a TAB, assume TAB-separated material
  7254. - when each line contains a comme, assume CSV material
  7255. - else, assume one or more SPACE charcters as separator."
  7256. (interactive "rP")
  7257. (let* ((beg (min beg0 end0))
  7258. (end (max beg0 end0))
  7259. re)
  7260. (goto-char beg)
  7261. (beginning-of-line 1)
  7262. (setq beg (move-marker (make-marker) (point)))
  7263. (goto-char end)
  7264. (if (bolp) (backward-char 1) (end-of-line 1))
  7265. (setq end (move-marker (make-marker) (point)))
  7266. ;; Get the right field separator
  7267. (unless separator
  7268. (goto-char beg)
  7269. (setq separator
  7270. (cond
  7271. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7272. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7273. (t 1))))
  7274. (setq re (cond
  7275. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7276. ((equal separator '(16)) "^\\|\t")
  7277. ((integerp separator)
  7278. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7279. (t (error "This should not happen"))))
  7280. (goto-char beg)
  7281. (while (re-search-forward re end t)
  7282. (replace-match "| " t t))
  7283. (goto-char beg)
  7284. (insert " ")
  7285. (org-table-align)))
  7286. (defun org-table-import (file arg)
  7287. "Import FILE as a table.
  7288. The file is assumed to be tab-separated. Such files can be produced by most
  7289. spreadsheet and database applications. If no tabs (at least one per line)
  7290. are found, lines will be split on whitespace into fields."
  7291. (interactive "f\nP")
  7292. (or (bolp) (newline))
  7293. (let ((beg (point))
  7294. (pm (point-max)))
  7295. (insert-file-contents file)
  7296. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7297. (defun org-table-export ()
  7298. "Export table as a tab-separated file.
  7299. Such a file can be imported into a spreadsheet program like Excel."
  7300. (interactive)
  7301. (let* ((beg (org-table-begin))
  7302. (end (org-table-end))
  7303. (table (buffer-substring beg end))
  7304. (file (read-file-name "Export table to: "))
  7305. buf)
  7306. (unless (or (not (file-exists-p file))
  7307. (y-or-n-p (format "Overwrite file %s? " file)))
  7308. (error "Abort"))
  7309. (with-current-buffer (find-file-noselect file)
  7310. (setq buf (current-buffer))
  7311. (erase-buffer)
  7312. (fundamental-mode)
  7313. (insert table)
  7314. (goto-char (point-min))
  7315. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7316. (replace-match "" t t)
  7317. (end-of-line 1))
  7318. (goto-char (point-min))
  7319. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7320. (replace-match "" t t)
  7321. (goto-char (min (1+ (point)) (point-max))))
  7322. (goto-char (point-min))
  7323. (while (re-search-forward "^-[-+]*$" nil t)
  7324. (replace-match "")
  7325. (if (looking-at "\n")
  7326. (delete-char 1)))
  7327. (goto-char (point-min))
  7328. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7329. (replace-match "\t" t t))
  7330. (save-buffer))
  7331. (kill-buffer buf)))
  7332. (defvar org-table-aligned-begin-marker (make-marker)
  7333. "Marker at the beginning of the table last aligned.
  7334. Used to check if cursor still is in that table, to minimize realignment.")
  7335. (defvar org-table-aligned-end-marker (make-marker)
  7336. "Marker at the end of the table last aligned.
  7337. Used to check if cursor still is in that table, to minimize realignment.")
  7338. (defvar org-table-last-alignment nil
  7339. "List of flags for flushright alignment, from the last re-alignment.
  7340. This is being used to correctly align a single field after TAB or RET.")
  7341. (defvar org-table-last-column-widths nil
  7342. "List of max width of fields in each column.
  7343. This is being used to correctly align a single field after TAB or RET.")
  7344. (defvar org-table-overlay-coordinates nil
  7345. "Overlay coordinates after each align of a table.")
  7346. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7347. (defvar org-last-recalc-line nil)
  7348. (defconst org-narrow-column-arrow "=>"
  7349. "Used as display property in narrowed table columns.")
  7350. (defun org-table-align ()
  7351. "Align the table at point by aligning all vertical bars."
  7352. (interactive)
  7353. (let* (
  7354. ;; Limits of table
  7355. (beg (org-table-begin))
  7356. (end (org-table-end))
  7357. ;; Current cursor position
  7358. (linepos (org-current-line))
  7359. (colpos (org-table-current-column))
  7360. (winstart (window-start))
  7361. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7362. lines (new "") lengths l typenums ty fields maxfields i
  7363. column
  7364. (indent "") cnt frac
  7365. rfmt hfmt
  7366. (spaces '(1 . 1))
  7367. (sp1 (car spaces))
  7368. (sp2 (cdr spaces))
  7369. (rfmt1 (concat
  7370. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7371. (hfmt1 (concat
  7372. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7373. emptystrings links dates emph narrow fmax f1 len c e)
  7374. (untabify beg end)
  7375. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7376. ;; Check if we have links or dates
  7377. (goto-char beg)
  7378. (setq links (re-search-forward org-bracket-link-regexp end t))
  7379. (goto-char beg)
  7380. (setq emph (and org-hide-emphasis-markers
  7381. (re-search-forward org-emph-re end t)))
  7382. (goto-char beg)
  7383. (setq dates (and org-display-custom-times
  7384. (re-search-forward org-ts-regexp-both end t)))
  7385. ;; Make sure the link properties are right
  7386. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7387. ;; Make sure the date properties are right
  7388. (when dates (goto-char beg) (while (org-activate-dates end)))
  7389. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7390. ;; Check if we are narrowing any columns
  7391. (goto-char beg)
  7392. (setq narrow (and org-format-transports-properties-p
  7393. (re-search-forward "<[0-9]+>" end t)))
  7394. ;; Get the rows
  7395. (setq lines (org-split-string
  7396. (buffer-substring beg end) "\n"))
  7397. ;; Store the indentation of the first line
  7398. (if (string-match "^ *" (car lines))
  7399. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7400. ;; Mark the hlines by setting the corresponding element to nil
  7401. ;; At the same time, we remove trailing space.
  7402. (setq lines (mapcar (lambda (l)
  7403. (if (string-match "^ *|-" l)
  7404. nil
  7405. (if (string-match "[ \t]+$" l)
  7406. (substring l 0 (match-beginning 0))
  7407. l)))
  7408. lines))
  7409. ;; Get the data fields by splitting the lines.
  7410. (setq fields (mapcar
  7411. (lambda (l)
  7412. (org-split-string l " *| *"))
  7413. (delq nil (copy-sequence lines))))
  7414. ;; How many fields in the longest line?
  7415. (condition-case nil
  7416. (setq maxfields (apply 'max (mapcar 'length fields)))
  7417. (error
  7418. (kill-region beg end)
  7419. (org-table-create org-table-default-size)
  7420. (error "Empty table - created default table")))
  7421. ;; A list of empty strings to fill any short rows on output
  7422. (setq emptystrings (make-list maxfields ""))
  7423. ;; Check for special formatting.
  7424. (setq i -1)
  7425. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7426. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7427. ;; Check if there is an explicit width specified
  7428. (when narrow
  7429. (setq c column fmax nil)
  7430. (while c
  7431. (setq e (pop c))
  7432. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7433. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7434. ;; Find fields that are wider than fmax, and shorten them
  7435. (when fmax
  7436. (loop for xx in column do
  7437. (when (and (stringp xx)
  7438. (> (org-string-width xx) fmax))
  7439. (org-add-props xx nil
  7440. 'help-echo
  7441. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7442. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7443. (unless (> f1 1)
  7444. (error "Cannot narrow field starting with wide link \"%s\""
  7445. (match-string 0 xx)))
  7446. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7447. (add-text-properties (- f1 2) f1
  7448. (list 'display org-narrow-column-arrow)
  7449. xx)))))
  7450. ;; Get the maximum width for each column
  7451. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7452. ;; Get the fraction of numbers, to decide about alignment of the column
  7453. (setq cnt 0 frac 0.0)
  7454. (loop for x in column do
  7455. (if (equal x "")
  7456. nil
  7457. (setq frac ( / (+ (* frac cnt)
  7458. (if (string-match org-table-number-regexp x) 1 0))
  7459. (setq cnt (1+ cnt))))))
  7460. (push (>= frac org-table-number-fraction) typenums))
  7461. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7462. ;; Store the alignment of this table, for later editing of single fields
  7463. (setq org-table-last-alignment typenums
  7464. org-table-last-column-widths lengths)
  7465. ;; With invisible characters, `format' does not get the field width right
  7466. ;; So we need to make these fields wide by hand.
  7467. (when (or links emph)
  7468. (loop for i from 0 upto (1- maxfields) do
  7469. (setq len (nth i lengths))
  7470. (loop for j from 0 upto (1- (length fields)) do
  7471. (setq c (nthcdr i (car (nthcdr j fields))))
  7472. (if (and (stringp (car c))
  7473. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7474. ; (string-match org-bracket-link-regexp (car c))
  7475. (< (org-string-width (car c)) len))
  7476. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7477. ;; Compute the formats needed for output of the table
  7478. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7479. (while (setq l (pop lengths))
  7480. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7481. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7482. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7483. (setq rfmt (concat rfmt "\n")
  7484. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7485. (setq new (mapconcat
  7486. (lambda (l)
  7487. (if l (apply 'format rfmt
  7488. (append (pop fields) emptystrings))
  7489. hfmt))
  7490. lines ""))
  7491. ;; Replace the old one
  7492. (delete-region beg end)
  7493. (move-marker end nil)
  7494. (move-marker org-table-aligned-begin-marker (point))
  7495. (insert new)
  7496. (move-marker org-table-aligned-end-marker (point))
  7497. (when (and orgtbl-mode (not (org-mode-p)))
  7498. (goto-char org-table-aligned-begin-marker)
  7499. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7500. ;; Try to move to the old location
  7501. (goto-line winstartline)
  7502. (setq winstart (point-at-bol))
  7503. (goto-line linepos)
  7504. (set-window-start (selected-window) winstart 'noforce)
  7505. (org-table-goto-column colpos)
  7506. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7507. (setq org-table-may-need-update nil)
  7508. ))
  7509. (defun org-string-width (s)
  7510. "Compute width of string, ignoring invisible characters.
  7511. This ignores character with invisibility property `org-link', and also
  7512. characters with property `org-cwidth', because these will become invisible
  7513. upon the next fontification round."
  7514. (let (b l)
  7515. (when (or (eq t buffer-invisibility-spec)
  7516. (assq 'org-link buffer-invisibility-spec))
  7517. (while (setq b (text-property-any 0 (length s)
  7518. 'invisible 'org-link s))
  7519. (setq s (concat (substring s 0 b)
  7520. (substring s (or (next-single-property-change
  7521. b 'invisible s) (length s)))))))
  7522. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7523. (setq s (concat (substring s 0 b)
  7524. (substring s (or (next-single-property-change
  7525. b 'org-cwidth s) (length s))))))
  7526. (setq l (string-width s) b -1)
  7527. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7528. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7529. l))
  7530. (defun org-table-begin (&optional table-type)
  7531. "Find the beginning of the table and return its position.
  7532. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7533. (save-excursion
  7534. (if (not (re-search-backward
  7535. (if table-type org-table-any-border-regexp
  7536. org-table-border-regexp)
  7537. nil t))
  7538. (progn (goto-char (point-min)) (point))
  7539. (goto-char (match-beginning 0))
  7540. (beginning-of-line 2)
  7541. (point))))
  7542. (defun org-table-end (&optional table-type)
  7543. "Find the end of the table and return its position.
  7544. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7545. (save-excursion
  7546. (if (not (re-search-forward
  7547. (if table-type org-table-any-border-regexp
  7548. org-table-border-regexp)
  7549. nil t))
  7550. (goto-char (point-max))
  7551. (goto-char (match-beginning 0)))
  7552. (point-marker)))
  7553. (defun org-table-justify-field-maybe (&optional new)
  7554. "Justify the current field, text to left, number to right.
  7555. Optional argument NEW may specify text to replace the current field content."
  7556. (cond
  7557. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7558. ((org-at-table-hline-p))
  7559. ((and (not new)
  7560. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7561. (current-buffer)))
  7562. (< (point) org-table-aligned-begin-marker)
  7563. (>= (point) org-table-aligned-end-marker)))
  7564. ;; This is not the same table, force a full re-align
  7565. (setq org-table-may-need-update t))
  7566. (t ;; realign the current field, based on previous full realign
  7567. (let* ((pos (point)) s
  7568. (col (org-table-current-column))
  7569. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7570. l f n o e)
  7571. (when (> col 0)
  7572. (skip-chars-backward "^|\n")
  7573. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7574. (progn
  7575. (setq s (match-string 1)
  7576. o (match-string 0)
  7577. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7578. e (not (= (match-beginning 2) (match-end 2))))
  7579. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7580. l (if e "|" (setq org-table-may-need-update t) ""))
  7581. n (format f s))
  7582. (if new
  7583. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7584. (setq n (format f new))
  7585. (setq n (concat new "|") org-table-may-need-update t)))
  7586. (or (equal n o)
  7587. (let (org-table-may-need-update)
  7588. (replace-match n t t))))
  7589. (setq org-table-may-need-update t))
  7590. (goto-char pos))))))
  7591. (defun org-table-next-field ()
  7592. "Go to the next field in the current table, creating new lines as needed.
  7593. Before doing so, re-align the table if necessary."
  7594. (interactive)
  7595. (org-table-maybe-eval-formula)
  7596. (org-table-maybe-recalculate-line)
  7597. (if (and org-table-automatic-realign
  7598. org-table-may-need-update)
  7599. (org-table-align))
  7600. (let ((end (org-table-end)))
  7601. (if (org-at-table-hline-p)
  7602. (end-of-line 1))
  7603. (condition-case nil
  7604. (progn
  7605. (re-search-forward "|" end)
  7606. (if (looking-at "[ \t]*$")
  7607. (re-search-forward "|" end))
  7608. (if (and (looking-at "-")
  7609. org-table-tab-jumps-over-hlines
  7610. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7611. (goto-char (match-beginning 1)))
  7612. (if (looking-at "-")
  7613. (progn
  7614. (beginning-of-line 0)
  7615. (org-table-insert-row 'below))
  7616. (if (looking-at " ") (forward-char 1))))
  7617. (error
  7618. (org-table-insert-row 'below)))))
  7619. (defun org-table-previous-field ()
  7620. "Go to the previous field in the table.
  7621. Before doing so, re-align the table if necessary."
  7622. (interactive)
  7623. (org-table-justify-field-maybe)
  7624. (org-table-maybe-recalculate-line)
  7625. (if (and org-table-automatic-realign
  7626. org-table-may-need-update)
  7627. (org-table-align))
  7628. (if (org-at-table-hline-p)
  7629. (end-of-line 1))
  7630. (re-search-backward "|" (org-table-begin))
  7631. (re-search-backward "|" (org-table-begin))
  7632. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7633. (re-search-backward "|" (org-table-begin)))
  7634. (if (looking-at "| ?")
  7635. (goto-char (match-end 0))))
  7636. (defun org-table-next-row ()
  7637. "Go to the next row (same column) in the current table.
  7638. Before doing so, re-align the table if necessary."
  7639. (interactive)
  7640. (org-table-maybe-eval-formula)
  7641. (org-table-maybe-recalculate-line)
  7642. (if (or (looking-at "[ \t]*$")
  7643. (save-excursion (skip-chars-backward " \t") (bolp)))
  7644. (newline)
  7645. (if (and org-table-automatic-realign
  7646. org-table-may-need-update)
  7647. (org-table-align))
  7648. (let ((col (org-table-current-column)))
  7649. (beginning-of-line 2)
  7650. (if (or (not (org-at-table-p))
  7651. (org-at-table-hline-p))
  7652. (progn
  7653. (beginning-of-line 0)
  7654. (org-table-insert-row 'below)))
  7655. (org-table-goto-column col)
  7656. (skip-chars-backward "^|\n\r")
  7657. (if (looking-at " ") (forward-char 1)))))
  7658. (defun org-table-copy-down (n)
  7659. "Copy a field down in the current column.
  7660. If the field at the cursor is empty, copy into it the content of the nearest
  7661. non-empty field above. With argument N, use the Nth non-empty field.
  7662. If the current field is not empty, it is copied down to the next row, and
  7663. the cursor is moved with it. Therefore, repeating this command causes the
  7664. column to be filled row-by-row.
  7665. If the variable `org-table-copy-increment' is non-nil and the field is an
  7666. integer or a timestamp, it will be incremented while copying. In the case of
  7667. a timestamp, if the cursor is on the year, change the year. If it is on the
  7668. month or the day, change that. Point will stay on the current date field
  7669. in order to easily repeat the interval."
  7670. (interactive "p")
  7671. (let* ((colpos (org-table-current-column))
  7672. (col (current-column))
  7673. (field (org-table-get-field))
  7674. (non-empty (string-match "[^ \t]" field))
  7675. (beg (org-table-begin))
  7676. txt)
  7677. (org-table-check-inside-data-field)
  7678. (if non-empty
  7679. (progn
  7680. (setq txt (org-trim field))
  7681. (org-table-next-row)
  7682. (org-table-blank-field))
  7683. (save-excursion
  7684. (setq txt
  7685. (catch 'exit
  7686. (while (progn (beginning-of-line 1)
  7687. (re-search-backward org-table-dataline-regexp
  7688. beg t))
  7689. (org-table-goto-column colpos t)
  7690. (if (and (looking-at
  7691. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7692. (= (setq n (1- n)) 0))
  7693. (throw 'exit (match-string 1))))))))
  7694. (if txt
  7695. (progn
  7696. (if (and org-table-copy-increment
  7697. (string-match "^[0-9]+$" txt))
  7698. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7699. (insert txt)
  7700. (move-to-column col)
  7701. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7702. (org-timestamp-up 1)
  7703. (org-table-maybe-recalculate-line))
  7704. (org-table-align)
  7705. (move-to-column col))
  7706. (error "No non-empty field found"))))
  7707. (defun org-table-check-inside-data-field ()
  7708. "Is point inside a table data field?
  7709. I.e. not on a hline or before the first or after the last column?
  7710. This actually throws an error, so it aborts the current command."
  7711. (if (or (not (org-at-table-p))
  7712. (= (org-table-current-column) 0)
  7713. (org-at-table-hline-p)
  7714. (looking-at "[ \t]*$"))
  7715. (error "Not in table data field")))
  7716. (defvar org-table-clip nil
  7717. "Clipboard for table regions.")
  7718. (defun org-table-blank-field ()
  7719. "Blank the current table field or active region."
  7720. (interactive)
  7721. (org-table-check-inside-data-field)
  7722. (if (and (interactive-p) (org-region-active-p))
  7723. (let (org-table-clip)
  7724. (org-table-cut-region (region-beginning) (region-end)))
  7725. (skip-chars-backward "^|")
  7726. (backward-char 1)
  7727. (if (looking-at "|[^|\n]+")
  7728. (let* ((pos (match-beginning 0))
  7729. (match (match-string 0))
  7730. (len (org-string-width match)))
  7731. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7732. (goto-char (+ 2 pos))
  7733. (substring match 1)))))
  7734. (defun org-table-get-field (&optional n replace)
  7735. "Return the value of the field in column N of current row.
  7736. N defaults to current field.
  7737. If REPLACE is a string, replace field with this value. The return value
  7738. is always the old value."
  7739. (and n (org-table-goto-column n))
  7740. (skip-chars-backward "^|\n")
  7741. (backward-char 1)
  7742. (if (looking-at "|[^|\r\n]*")
  7743. (let* ((pos (match-beginning 0))
  7744. (val (buffer-substring (1+ pos) (match-end 0))))
  7745. (if replace
  7746. (replace-match (concat "|" replace) t t))
  7747. (goto-char (min (point-at-eol) (+ 2 pos)))
  7748. val)
  7749. (forward-char 1) ""))
  7750. (defun org-table-field-info (arg)
  7751. "Show info about the current field, and highlight any reference at point."
  7752. (interactive "P")
  7753. (org-table-get-specials)
  7754. (save-excursion
  7755. (let* ((pos (point))
  7756. (col (org-table-current-column))
  7757. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7758. (name (car (rassoc (list (org-current-line) col)
  7759. org-table-named-field-locations)))
  7760. (eql (org-table-get-stored-formulas))
  7761. (dline (org-table-current-dline))
  7762. (ref (format "@%d$%d" dline col))
  7763. (ref1 (org-table-convert-refs-to-an ref))
  7764. (fequation (or (assoc name eql) (assoc ref eql)))
  7765. (cequation (assoc (int-to-string col) eql))
  7766. (eqn (or fequation cequation)))
  7767. (goto-char pos)
  7768. (condition-case nil
  7769. (org-table-show-reference 'local)
  7770. (error nil))
  7771. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7772. dline col
  7773. (if cname (concat " or $" cname) "")
  7774. dline col ref1
  7775. (if name (concat " or $" name) "")
  7776. ;; FIXME: formula info not correct if special table line
  7777. (if eqn
  7778. (concat ", formula: "
  7779. (org-table-formula-to-user
  7780. (concat
  7781. (if (string-match "^[$@]"(car eqn)) "" "$")
  7782. (car eqn) "=" (cdr eqn))))
  7783. "")))))
  7784. (defun org-table-current-column ()
  7785. "Find out which column we are in.
  7786. When called interactively, column is also displayed in echo area."
  7787. (interactive)
  7788. (if (interactive-p) (org-table-check-inside-data-field))
  7789. (save-excursion
  7790. (let ((cnt 0) (pos (point)))
  7791. (beginning-of-line 1)
  7792. (while (search-forward "|" pos t)
  7793. (setq cnt (1+ cnt)))
  7794. (if (interactive-p) (message "This is table column %d" cnt))
  7795. cnt)))
  7796. (defun org-table-current-dline ()
  7797. "Find out what table data line we are in.
  7798. Only datalins count for this."
  7799. (interactive)
  7800. (if (interactive-p) (org-table-check-inside-data-field))
  7801. (save-excursion
  7802. (let ((cnt 0) (pos (point)))
  7803. (goto-char (org-table-begin))
  7804. (while (<= (point) pos)
  7805. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7806. (beginning-of-line 2))
  7807. (if (interactive-p) (message "This is table line %d" cnt))
  7808. cnt)))
  7809. (defun org-table-goto-column (n &optional on-delim force)
  7810. "Move the cursor to the Nth column in the current table line.
  7811. With optional argument ON-DELIM, stop with point before the left delimiter
  7812. of the field.
  7813. If there are less than N fields, just go to after the last delimiter.
  7814. However, when FORCE is non-nil, create new columns if necessary."
  7815. (interactive "p")
  7816. (let ((pos (point-at-eol)))
  7817. (beginning-of-line 1)
  7818. (when (> n 0)
  7819. (while (and (> (setq n (1- n)) -1)
  7820. (or (search-forward "|" pos t)
  7821. (and force
  7822. (progn (end-of-line 1)
  7823. (skip-chars-backward "^|")
  7824. (insert " | "))))))
  7825. ; (backward-char 2) t)))))
  7826. (when (and force (not (looking-at ".*|")))
  7827. (save-excursion (end-of-line 1) (insert " | ")))
  7828. (if on-delim
  7829. (backward-char 1)
  7830. (if (looking-at " ") (forward-char 1))))))
  7831. (defun org-at-table-p (&optional table-type)
  7832. "Return t if the cursor is inside an org-type table.
  7833. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7834. (if org-enable-table-editor
  7835. (save-excursion
  7836. (beginning-of-line 1)
  7837. (looking-at (if table-type org-table-any-line-regexp
  7838. org-table-line-regexp)))
  7839. nil))
  7840. (defun org-at-table.el-p ()
  7841. "Return t if and only if we are at a table.el table."
  7842. (and (org-at-table-p 'any)
  7843. (save-excursion
  7844. (goto-char (org-table-begin 'any))
  7845. (looking-at org-table1-hline-regexp))))
  7846. (defun org-table-recognize-table.el ()
  7847. "If there is a table.el table nearby, recognize it and move into it."
  7848. (if org-table-tab-recognizes-table.el
  7849. (if (org-at-table.el-p)
  7850. (progn
  7851. (beginning-of-line 1)
  7852. (if (looking-at org-table-dataline-regexp)
  7853. nil
  7854. (if (looking-at org-table1-hline-regexp)
  7855. (progn
  7856. (beginning-of-line 2)
  7857. (if (looking-at org-table-any-border-regexp)
  7858. (beginning-of-line -1)))))
  7859. (if (re-search-forward "|" (org-table-end t) t)
  7860. (progn
  7861. (require 'table)
  7862. (if (table--at-cell-p (point))
  7863. t
  7864. (message "recognizing table.el table...")
  7865. (table-recognize-table)
  7866. (message "recognizing table.el table...done")))
  7867. (error "This should not happen..."))
  7868. t)
  7869. nil)
  7870. nil))
  7871. (defun org-at-table-hline-p ()
  7872. "Return t if the cursor is inside a hline in a table."
  7873. (if org-enable-table-editor
  7874. (save-excursion
  7875. (beginning-of-line 1)
  7876. (looking-at org-table-hline-regexp))
  7877. nil))
  7878. (defun org-table-insert-column ()
  7879. "Insert a new column into the table."
  7880. (interactive)
  7881. (if (not (org-at-table-p))
  7882. (error "Not at a table"))
  7883. (org-table-find-dataline)
  7884. (let* ((col (max 1 (org-table-current-column)))
  7885. (beg (org-table-begin))
  7886. (end (org-table-end))
  7887. ;; Current cursor position
  7888. (linepos (org-current-line))
  7889. (colpos col))
  7890. (goto-char beg)
  7891. (while (< (point) end)
  7892. (if (org-at-table-hline-p)
  7893. nil
  7894. (org-table-goto-column col t)
  7895. (insert "| "))
  7896. (beginning-of-line 2))
  7897. (move-marker end nil)
  7898. (goto-line linepos)
  7899. (org-table-goto-column colpos)
  7900. (org-table-align)
  7901. (org-table-fix-formulas "$" nil (1- col) 1)))
  7902. (defun org-table-find-dataline ()
  7903. "Find a dataline in the current table, which is needed for column commands."
  7904. (if (and (org-at-table-p)
  7905. (not (org-at-table-hline-p)))
  7906. t
  7907. (let ((col (current-column))
  7908. (end (org-table-end)))
  7909. (move-to-column col)
  7910. (while (and (< (point) end)
  7911. (or (not (= (current-column) col))
  7912. (org-at-table-hline-p)))
  7913. (beginning-of-line 2)
  7914. (move-to-column col))
  7915. (if (and (org-at-table-p)
  7916. (not (org-at-table-hline-p)))
  7917. t
  7918. (error
  7919. "Please position cursor in a data line for column operations")))))
  7920. (defun org-table-delete-column ()
  7921. "Delete a column from the table."
  7922. (interactive)
  7923. (if (not (org-at-table-p))
  7924. (error "Not at a table"))
  7925. (org-table-find-dataline)
  7926. (org-table-check-inside-data-field)
  7927. (let* ((col (org-table-current-column))
  7928. (beg (org-table-begin))
  7929. (end (org-table-end))
  7930. ;; Current cursor position
  7931. (linepos (org-current-line))
  7932. (colpos col))
  7933. (goto-char beg)
  7934. (while (< (point) end)
  7935. (if (org-at-table-hline-p)
  7936. nil
  7937. (org-table-goto-column col t)
  7938. (and (looking-at "|[^|\n]+|")
  7939. (replace-match "|")))
  7940. (beginning-of-line 2))
  7941. (move-marker end nil)
  7942. (goto-line linepos)
  7943. (org-table-goto-column colpos)
  7944. (org-table-align)
  7945. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  7946. col -1 col)))
  7947. (defun org-table-move-column-right ()
  7948. "Move column to the right."
  7949. (interactive)
  7950. (org-table-move-column nil))
  7951. (defun org-table-move-column-left ()
  7952. "Move column to the left."
  7953. (interactive)
  7954. (org-table-move-column 'left))
  7955. (defun org-table-move-column (&optional left)
  7956. "Move the current column to the right. With arg LEFT, move to the left."
  7957. (interactive "P")
  7958. (if (not (org-at-table-p))
  7959. (error "Not at a table"))
  7960. (org-table-find-dataline)
  7961. (org-table-check-inside-data-field)
  7962. (let* ((col (org-table-current-column))
  7963. (col1 (if left (1- col) col))
  7964. (beg (org-table-begin))
  7965. (end (org-table-end))
  7966. ;; Current cursor position
  7967. (linepos (org-current-line))
  7968. (colpos (if left (1- col) (1+ col))))
  7969. (if (and left (= col 1))
  7970. (error "Cannot move column further left"))
  7971. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  7972. (error "Cannot move column further right"))
  7973. (goto-char beg)
  7974. (while (< (point) end)
  7975. (if (org-at-table-hline-p)
  7976. nil
  7977. (org-table-goto-column col1 t)
  7978. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  7979. (replace-match "|\\2|\\1|")))
  7980. (beginning-of-line 2))
  7981. (move-marker end nil)
  7982. (goto-line linepos)
  7983. (org-table-goto-column colpos)
  7984. (org-table-align)
  7985. (org-table-fix-formulas
  7986. "$" (list (cons (number-to-string col) (number-to-string colpos))
  7987. (cons (number-to-string colpos) (number-to-string col))))))
  7988. (defun org-table-move-row-down ()
  7989. "Move table row down."
  7990. (interactive)
  7991. (org-table-move-row nil))
  7992. (defun org-table-move-row-up ()
  7993. "Move table row up."
  7994. (interactive)
  7995. (org-table-move-row 'up))
  7996. (defun org-table-move-row (&optional up)
  7997. "Move the current table line down. With arg UP, move it up."
  7998. (interactive "P")
  7999. (let* ((col (current-column))
  8000. (pos (point))
  8001. (hline1p (save-excursion (beginning-of-line 1)
  8002. (looking-at org-table-hline-regexp)))
  8003. (dline1 (org-table-current-dline))
  8004. (dline2 (+ dline1 (if up -1 1)))
  8005. (tonew (if up 0 2))
  8006. txt hline2p)
  8007. (beginning-of-line tonew)
  8008. (unless (org-at-table-p)
  8009. (goto-char pos)
  8010. (error "Cannot move row further"))
  8011. (setq hline2p (looking-at org-table-hline-regexp))
  8012. (goto-char pos)
  8013. (beginning-of-line 1)
  8014. (setq pos (point))
  8015. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8016. (delete-region (point) (1+ (point-at-eol)))
  8017. (beginning-of-line tonew)
  8018. (insert txt)
  8019. (beginning-of-line 0)
  8020. (move-to-column col)
  8021. (unless (or hline1p hline2p)
  8022. (org-table-fix-formulas
  8023. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8024. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8025. (defun org-table-insert-row (&optional arg)
  8026. "Insert a new row above the current line into the table.
  8027. With prefix ARG, insert below the current line."
  8028. (interactive "P")
  8029. (if (not (org-at-table-p))
  8030. (error "Not at a table"))
  8031. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8032. (new (org-table-clean-line line)))
  8033. ;; Fix the first field if necessary
  8034. (if (string-match "^[ \t]*| *[#$] *|" line)
  8035. (setq new (replace-match (match-string 0 line) t t new)))
  8036. (beginning-of-line (if arg 2 1))
  8037. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8038. (beginning-of-line 0)
  8039. (re-search-forward "| ?" (point-at-eol) t)
  8040. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8041. (org-table-align))
  8042. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8043. (defun org-table-insert-hline (&optional above)
  8044. "Insert a horizontal-line below the current line into the table.
  8045. With prefix ABOVE, insert above the current line."
  8046. (interactive "P")
  8047. (if (not (org-at-table-p))
  8048. (error "Not at a table"))
  8049. (let ((line (org-table-clean-line
  8050. (buffer-substring (point-at-bol) (point-at-eol))))
  8051. (col (current-column)))
  8052. (while (string-match "|\\( +\\)|" line)
  8053. (setq line (replace-match
  8054. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8055. ?-) "|") t t line)))
  8056. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8057. (beginning-of-line (if above 1 2))
  8058. (insert line "\n")
  8059. (beginning-of-line (if above 1 -1))
  8060. (move-to-column col)
  8061. (and org-table-overlay-coordinates (org-table-align))))
  8062. (defun org-table-hline-and-move (&optional same-column)
  8063. "Insert a hline and move to the row below that line."
  8064. (interactive "P")
  8065. (let ((col (org-table-current-column)))
  8066. (org-table-maybe-eval-formula)
  8067. (org-table-maybe-recalculate-line)
  8068. (org-table-insert-hline)
  8069. (end-of-line 2)
  8070. (if (looking-at "\n[ \t]*|-")
  8071. (progn (insert "\n|") (org-table-align))
  8072. (org-table-next-field))
  8073. (if same-column (org-table-goto-column col))))
  8074. (defun org-table-clean-line (s)
  8075. "Convert a table line S into a string with only \"|\" and space.
  8076. In particular, this does handle wide and invisible characters."
  8077. (if (string-match "^[ \t]*|-" s)
  8078. ;; It's a hline, just map the characters
  8079. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8080. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8081. (setq s (replace-match
  8082. (concat "|" (make-string (org-string-width (match-string 1 s))
  8083. ?\ ) "|")
  8084. t t s)))
  8085. s))
  8086. (defun org-table-kill-row ()
  8087. "Delete the current row or horizontal line from the table."
  8088. (interactive)
  8089. (if (not (org-at-table-p))
  8090. (error "Not at a table"))
  8091. (let ((col (current-column))
  8092. (dline (org-table-current-dline)))
  8093. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8094. (if (not (org-at-table-p)) (beginning-of-line 0))
  8095. (move-to-column col)
  8096. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8097. dline -1 dline)))
  8098. (defun org-table-sort-lines (with-case &optional sorting-type)
  8099. "Sort table lines according to the column at point.
  8100. The position of point indicates the column to be used for
  8101. sorting, and the range of lines is the range between the nearest
  8102. horizontal separator lines, or the entire table of no such lines
  8103. exist. If point is before the first column, you will be prompted
  8104. for the sorting column. If there is an active region, the mark
  8105. specifies the first line and the sorting column, while point
  8106. should be in the last line to be included into the sorting.
  8107. The command then prompts for the sorting type which can be
  8108. alphabetically, numerically, or by time (as given in a time stamp
  8109. in the field). Sorting in reverse order is also possible.
  8110. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8111. If SORTING-TYPE is specified when this function is called from a Lisp
  8112. program, no prompting will take place. SORTING-TYPE must be a character,
  8113. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8114. should be done in reverse order."
  8115. (interactive "P")
  8116. (let* ((thisline (org-current-line))
  8117. (thiscol (org-table-current-column))
  8118. beg end bcol ecol tend tbeg column lns pos)
  8119. (when (equal thiscol 0)
  8120. (if (interactive-p)
  8121. (setq thiscol
  8122. (string-to-number
  8123. (read-string "Use column N for sorting: ")))
  8124. (setq thiscol 1))
  8125. (org-table-goto-column thiscol))
  8126. (org-table-check-inside-data-field)
  8127. (if (org-region-active-p)
  8128. (progn
  8129. (setq beg (region-beginning) end (region-end))
  8130. (goto-char beg)
  8131. (setq column (org-table-current-column)
  8132. beg (point-at-bol))
  8133. (goto-char end)
  8134. (setq end (point-at-bol 2)))
  8135. (setq column (org-table-current-column)
  8136. pos (point)
  8137. tbeg (org-table-begin)
  8138. tend (org-table-end))
  8139. (if (re-search-backward org-table-hline-regexp tbeg t)
  8140. (setq beg (point-at-bol 2))
  8141. (goto-char tbeg)
  8142. (setq beg (point-at-bol 1)))
  8143. (goto-char pos)
  8144. (if (re-search-forward org-table-hline-regexp tend t)
  8145. (setq end (point-at-bol 1))
  8146. (goto-char tend)
  8147. (setq end (point-at-bol))))
  8148. (setq beg (move-marker (make-marker) beg)
  8149. end (move-marker (make-marker) end))
  8150. (untabify beg end)
  8151. (goto-char beg)
  8152. (org-table-goto-column column)
  8153. (skip-chars-backward "^|")
  8154. (setq bcol (current-column))
  8155. (org-table-goto-column (1+ column))
  8156. (skip-chars-backward "^|")
  8157. (setq ecol (1- (current-column)))
  8158. (org-table-goto-column column)
  8159. (setq lns (mapcar (lambda(x) (cons (org-sort-remove-invisible
  8160. (org-trim (substring x bcol ecol))) x))
  8161. (org-split-string (buffer-substring beg end) "\n")))
  8162. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8163. (delete-region beg end)
  8164. (move-marker beg nil)
  8165. (move-marker end nil)
  8166. (insert (mapconcat 'cdr lns "\n") "\n")
  8167. (goto-line thisline)
  8168. (org-table-goto-column thiscol)
  8169. (message "%d lines sorted, based on column %d" (length lns) column)))
  8170. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8171. (defun org-sort-remove-invisible (s)
  8172. (remove-text-properties 0 (length s) org-rm-props s)
  8173. (if (string-match org-bracket-link-regexp s)
  8174. (setq s (replace-match (if (match-end 2) (match-string 3 s)
  8175. (match-string 1 s)))))
  8176. s)
  8177. (defun org-table-cut-region (beg end)
  8178. "Copy region in table to the clipboard and blank all relevant fields."
  8179. (interactive "r")
  8180. (org-table-copy-region beg end 'cut))
  8181. (defun org-table-copy-region (beg end &optional cut)
  8182. "Copy rectangular region in table to clipboard.
  8183. A special clipboard is used which can only be accessed
  8184. with `org-table-paste-rectangle'."
  8185. (interactive "rP")
  8186. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8187. region cols
  8188. (rpl (if cut " " nil)))
  8189. (goto-char beg)
  8190. (org-table-check-inside-data-field)
  8191. (setq l01 (org-current-line)
  8192. c01 (org-table-current-column))
  8193. (goto-char end)
  8194. (org-table-check-inside-data-field)
  8195. (setq l02 (org-current-line)
  8196. c02 (org-table-current-column))
  8197. (setq l1 (min l01 l02) l2 (max l01 l02)
  8198. c1 (min c01 c02) c2 (max c01 c02))
  8199. (catch 'exit
  8200. (while t
  8201. (catch 'nextline
  8202. (if (> l1 l2) (throw 'exit t))
  8203. (goto-line l1)
  8204. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8205. (setq cols nil ic1 c1 ic2 c2)
  8206. (while (< ic1 (1+ ic2))
  8207. (push (org-table-get-field ic1 rpl) cols)
  8208. (setq ic1 (1+ ic1)))
  8209. (push (nreverse cols) region)
  8210. (setq l1 (1+ l1)))))
  8211. (setq org-table-clip (nreverse region))
  8212. (if cut (org-table-align))
  8213. org-table-clip))
  8214. (defun org-table-paste-rectangle ()
  8215. "Paste a rectangular region into a table.
  8216. The upper right corner ends up in the current field. All involved fields
  8217. will be overwritten. If the rectangle does not fit into the present table,
  8218. the table is enlarged as needed. The process ignores horizontal separator
  8219. lines."
  8220. (interactive)
  8221. (unless (and org-table-clip (listp org-table-clip))
  8222. (error "First cut/copy a region to paste!"))
  8223. (org-table-check-inside-data-field)
  8224. (let* ((clip org-table-clip)
  8225. (line (org-current-line))
  8226. (col (org-table-current-column))
  8227. (org-enable-table-editor t)
  8228. (org-table-automatic-realign nil)
  8229. c cols field)
  8230. (while (setq cols (pop clip))
  8231. (while (org-at-table-hline-p) (beginning-of-line 2))
  8232. (if (not (org-at-table-p))
  8233. (progn (end-of-line 0) (org-table-next-field)))
  8234. (setq c col)
  8235. (while (setq field (pop cols))
  8236. (org-table-goto-column c nil 'force)
  8237. (org-table-get-field nil field)
  8238. (setq c (1+ c)))
  8239. (beginning-of-line 2))
  8240. (goto-line line)
  8241. (org-table-goto-column col)
  8242. (org-table-align)))
  8243. (defun org-table-convert ()
  8244. "Convert from `org-mode' table to table.el and back.
  8245. Obviously, this only works within limits. When an Org-mode table is
  8246. converted to table.el, all horizontal separator lines get lost, because
  8247. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8248. A table.el table can be converted to an Org-mode table only if it does not
  8249. do row or column spanning. Multiline cells will become multiple cells.
  8250. Beware, Org-mode does not test if the table can be successfully converted - it
  8251. blindly applies a recipe that works for simple tables."
  8252. (interactive)
  8253. (require 'table)
  8254. (if (org-at-table.el-p)
  8255. ;; convert to Org-mode table
  8256. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8257. (end (move-marker (make-marker) (org-table-end t))))
  8258. (table-unrecognize-region beg end)
  8259. (goto-char beg)
  8260. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8261. (replace-match ""))
  8262. (goto-char beg))
  8263. (if (org-at-table-p)
  8264. ;; convert to table.el table
  8265. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8266. (end (move-marker (make-marker) (org-table-end))))
  8267. ;; first, get rid of all horizontal lines
  8268. (goto-char beg)
  8269. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8270. (replace-match ""))
  8271. ;; insert a hline before first
  8272. (goto-char beg)
  8273. (org-table-insert-hline 'above)
  8274. (beginning-of-line -1)
  8275. ;; insert a hline after each line
  8276. (while (progn (beginning-of-line 3) (< (point) end))
  8277. (org-table-insert-hline))
  8278. (goto-char beg)
  8279. (setq end (move-marker end (org-table-end)))
  8280. ;; replace "+" at beginning and ending of hlines
  8281. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8282. (replace-match "\\1+-"))
  8283. (goto-char beg)
  8284. (while (re-search-forward "-|[ \t]*$" end t)
  8285. (replace-match "-+"))
  8286. (goto-char beg)))))
  8287. (defun org-table-wrap-region (arg)
  8288. "Wrap several fields in a column like a paragraph.
  8289. This is useful if you'd like to spread the contents of a field over several
  8290. lines, in order to keep the table compact.
  8291. If there is an active region, and both point and mark are in the same column,
  8292. the text in the column is wrapped to minimum width for the given number of
  8293. lines. Generally, this makes the table more compact. A prefix ARG may be
  8294. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8295. formats the selected text to two lines. If the region was longer than two
  8296. lines, the remaining lines remain empty. A negative prefix argument reduces
  8297. the current number of lines by that amount. The wrapped text is pasted back
  8298. into the table. If you formatted it to more lines than it was before, fields
  8299. further down in the table get overwritten - so you might need to make space in
  8300. the table first.
  8301. If there is no region, the current field is split at the cursor position and
  8302. the text fragment to the right of the cursor is prepended to the field one
  8303. line down.
  8304. If there is no region, but you specify a prefix ARG, the current field gets
  8305. blank, and the content is appended to the field above."
  8306. (interactive "P")
  8307. (org-table-check-inside-data-field)
  8308. (if (org-region-active-p)
  8309. ;; There is a region: fill as a paragraph
  8310. (let* ((beg (region-beginning))
  8311. (cline (save-excursion (goto-char beg) (org-current-line)))
  8312. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8313. nlines)
  8314. (org-table-cut-region (region-beginning) (region-end))
  8315. (if (> (length (car org-table-clip)) 1)
  8316. (error "Region must be limited to single column"))
  8317. (setq nlines (if arg
  8318. (if (< arg 1)
  8319. (+ (length org-table-clip) arg)
  8320. arg)
  8321. (length org-table-clip)))
  8322. (setq org-table-clip
  8323. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8324. nil nlines)))
  8325. (goto-line cline)
  8326. (org-table-goto-column ccol)
  8327. (org-table-paste-rectangle))
  8328. ;; No region, split the current field at point
  8329. (if arg
  8330. ;; combine with field above
  8331. (let ((s (org-table-blank-field))
  8332. (col (org-table-current-column)))
  8333. (beginning-of-line 0)
  8334. (while (org-at-table-hline-p) (beginning-of-line 0))
  8335. (org-table-goto-column col)
  8336. (skip-chars-forward "^|")
  8337. (skip-chars-backward " ")
  8338. (insert " " (org-trim s))
  8339. (org-table-align))
  8340. ;; split field
  8341. (when (looking-at "\\([^|]+\\)+|")
  8342. (let ((s (match-string 1)))
  8343. (replace-match " |")
  8344. (goto-char (match-beginning 0))
  8345. (org-table-next-row)
  8346. (insert (org-trim s) " ")
  8347. (org-table-align))))))
  8348. (defvar org-field-marker nil)
  8349. (defun org-table-edit-field (arg)
  8350. "Edit table field in a different window.
  8351. This is mainly useful for fields that contain hidden parts.
  8352. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8353. it can be edited in place."
  8354. (interactive "P")
  8355. (if arg
  8356. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8357. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8358. (remove-text-properties b e '(org-cwidth t invisible t
  8359. display t intangible t))
  8360. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8361. (font-lock-fontify-block)))
  8362. (let ((pos (move-marker (make-marker) (point)))
  8363. (field (org-table-get-field))
  8364. (cw (current-window-configuration))
  8365. p)
  8366. (org-switch-to-buffer-other-window "*Org tmp*")
  8367. (erase-buffer)
  8368. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8369. (let ((org-inhibit-startup t)) (org-mode))
  8370. (goto-char (setq p (point-max)))
  8371. (insert (org-trim field))
  8372. (remove-text-properties p (point-max)
  8373. '(invisible t org-cwidth t display t
  8374. intangible t))
  8375. (goto-char p)
  8376. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8377. (org-set-local 'org-window-configuration cw)
  8378. (org-set-local 'org-field-marker pos)
  8379. (message "Edit and finish with C-c C-c"))))
  8380. (defun org-table-finish-edit-field ()
  8381. "Finish editing a table data field.
  8382. Remove all newline characters, insert the result into the table, realign
  8383. the table and kill the editing buffer."
  8384. (let ((pos org-field-marker)
  8385. (cw org-window-configuration)
  8386. (cb (current-buffer))
  8387. text)
  8388. (goto-char (point-min))
  8389. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8390. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8391. (replace-match " "))
  8392. (setq text (org-trim (buffer-string)))
  8393. (set-window-configuration cw)
  8394. (kill-buffer cb)
  8395. (select-window (get-buffer-window (marker-buffer pos)))
  8396. (goto-char pos)
  8397. (move-marker pos nil)
  8398. (org-table-check-inside-data-field)
  8399. (org-table-get-field nil text)
  8400. (org-table-align)
  8401. (message "New field value inserted")))
  8402. (defun org-trim (s)
  8403. "Remove whitespace at beginning and end of string."
  8404. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8405. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8406. s)
  8407. (defun org-wrap (string &optional width lines)
  8408. "Wrap string to either a number of lines, or a width in characters.
  8409. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8410. that costs. If there is a word longer than WIDTH, the text is actually
  8411. wrapped to the length of that word.
  8412. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8413. many lines, whatever width that takes.
  8414. The return value is a list of lines, without newlines at the end."
  8415. (let* ((words (org-split-string string "[ \t\n]+"))
  8416. (maxword (apply 'max (mapcar 'org-string-width words)))
  8417. w ll)
  8418. (cond (width
  8419. (org-do-wrap words (max maxword width)))
  8420. (lines
  8421. (setq w maxword)
  8422. (setq ll (org-do-wrap words maxword))
  8423. (if (<= (length ll) lines)
  8424. ll
  8425. (setq ll words)
  8426. (while (> (length ll) lines)
  8427. (setq w (1+ w))
  8428. (setq ll (org-do-wrap words w)))
  8429. ll))
  8430. (t (error "Cannot wrap this")))))
  8431. (defun org-do-wrap (words width)
  8432. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8433. (let (lines line)
  8434. (while words
  8435. (setq line (pop words))
  8436. (while (and words (< (+ (length line) (length (car words))) width))
  8437. (setq line (concat line " " (pop words))))
  8438. (setq lines (push line lines)))
  8439. (nreverse lines)))
  8440. (defun org-split-string (string &optional separators)
  8441. "Splits STRING into substrings at SEPARATORS.
  8442. No empty strings are returned if there are matches at the beginning
  8443. and end of string."
  8444. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8445. (start 0)
  8446. notfirst
  8447. (list nil))
  8448. (while (and (string-match rexp string
  8449. (if (and notfirst
  8450. (= start (match-beginning 0))
  8451. (< start (length string)))
  8452. (1+ start) start))
  8453. (< (match-beginning 0) (length string)))
  8454. (setq notfirst t)
  8455. (or (eq (match-beginning 0) 0)
  8456. (and (eq (match-beginning 0) (match-end 0))
  8457. (eq (match-beginning 0) start))
  8458. (setq list
  8459. (cons (substring string start (match-beginning 0))
  8460. list)))
  8461. (setq start (match-end 0)))
  8462. (or (eq start (length string))
  8463. (setq list
  8464. (cons (substring string start)
  8465. list)))
  8466. (nreverse list)))
  8467. (defun org-table-map-tables (function)
  8468. "Apply FUNCTION to the start of all tables in the buffer."
  8469. (save-excursion
  8470. (save-restriction
  8471. (widen)
  8472. (goto-char (point-min))
  8473. (while (re-search-forward org-table-any-line-regexp nil t)
  8474. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8475. (beginning-of-line 1)
  8476. (if (looking-at org-table-line-regexp)
  8477. (save-excursion (funcall function)))
  8478. (re-search-forward org-table-any-border-regexp nil 1))))
  8479. (message "Mapping tables: done"))
  8480. (defvar org-timecnt) ; dynamically scoped parameter
  8481. (defun org-table-sum (&optional beg end nlast)
  8482. "Sum numbers in region of current table column.
  8483. The result will be displayed in the echo area, and will be available
  8484. as kill to be inserted with \\[yank].
  8485. If there is an active region, it is interpreted as a rectangle and all
  8486. numbers in that rectangle will be summed. If there is no active
  8487. region and point is located in a table column, sum all numbers in that
  8488. column.
  8489. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8490. numbers are assumed to be times as well (in decimal hours) and the
  8491. numbers are added as such.
  8492. If NLAST is a number, only the NLAST fields will actually be summed."
  8493. (interactive)
  8494. (save-excursion
  8495. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8496. (cond
  8497. ((and beg end)) ; beg and end given explicitly
  8498. ((org-region-active-p)
  8499. (setq beg (region-beginning) end (region-end)))
  8500. (t
  8501. (setq col (org-table-current-column))
  8502. (goto-char (org-table-begin))
  8503. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8504. (error "No table data"))
  8505. (org-table-goto-column col)
  8506. (setq beg (point))
  8507. (goto-char (org-table-end))
  8508. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8509. (error "No table data"))
  8510. (org-table-goto-column col)
  8511. (setq end (point))))
  8512. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8513. (items1 (cond ((not nlast) items)
  8514. ((>= nlast (length items)) items)
  8515. (t (setq items (reverse items))
  8516. (setcdr (nthcdr (1- nlast) items) nil)
  8517. (nreverse items))))
  8518. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8519. items1)))
  8520. (res (apply '+ numbers))
  8521. (sres (if (= org-timecnt 0)
  8522. (format "%g" res)
  8523. (setq diff (* 3600 res)
  8524. h (floor (/ diff 3600)) diff (mod diff 3600)
  8525. m (floor (/ diff 60)) diff (mod diff 60)
  8526. s diff)
  8527. (format "%d:%02d:%02d" h m s))))
  8528. (kill-new sres)
  8529. (if (interactive-p)
  8530. (message "%s"
  8531. (substitute-command-keys
  8532. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8533. (length numbers) sres))))
  8534. sres))))
  8535. (defun org-table-get-number-for-summing (s)
  8536. (let (n)
  8537. (if (string-match "^ *|? *" s)
  8538. (setq s (replace-match "" nil nil s)))
  8539. (if (string-match " *|? *$" s)
  8540. (setq s (replace-match "" nil nil s)))
  8541. (setq n (string-to-number s))
  8542. (cond
  8543. ((and (string-match "0" s)
  8544. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8545. ((string-match "\\`[ \t]+\\'" s) nil)
  8546. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8547. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8548. (m (string-to-number (or (match-string 2 s) "0")))
  8549. (s (string-to-number (or (match-string 4 s) "0"))))
  8550. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8551. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8552. ((equal n 0) nil)
  8553. (t n))))
  8554. (defun org-table-current-field-formula (&optional key noerror)
  8555. "Return the formula active for the current field.
  8556. Assumes that specials are in place.
  8557. If KEY is given, return the key to this formula.
  8558. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8559. (let* ((name (car (rassoc (list (org-current-line)
  8560. (org-table-current-column))
  8561. org-table-named-field-locations)))
  8562. (col (org-table-current-column))
  8563. (scol (int-to-string col))
  8564. (ref (format "@%d$%d" (org-table-current-dline) col))
  8565. (stored-list (org-table-get-stored-formulas noerror))
  8566. (ass (or (assoc name stored-list)
  8567. (assoc ref stored-list)
  8568. (assoc scol stored-list))))
  8569. (if key
  8570. (car ass)
  8571. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8572. (cdr ass))))))
  8573. (defun org-table-get-formula (&optional equation named)
  8574. "Read a formula from the minibuffer, offer stored formula as default.
  8575. When NAMED is non-nil, look for a named equation."
  8576. (let* ((stored-list (org-table-get-stored-formulas))
  8577. (name (car (rassoc (list (org-current-line)
  8578. (org-table-current-column))
  8579. org-table-named-field-locations)))
  8580. (ref (format "@%d$%d" (org-table-current-dline)
  8581. (org-table-current-column)))
  8582. (refass (assoc ref stored-list))
  8583. (scol (if named
  8584. (if name name ref)
  8585. (int-to-string (org-table-current-column))))
  8586. (dummy (and (or name refass) (not named)
  8587. (not (y-or-n-p "Replace field formula with column formula? " ))
  8588. (error "Abort")))
  8589. (name (or name ref))
  8590. (org-table-may-need-update nil)
  8591. (stored (cdr (assoc scol stored-list)))
  8592. (eq (cond
  8593. ((and stored equation (string-match "^ *=? *$" equation))
  8594. stored)
  8595. ((stringp equation)
  8596. equation)
  8597. (t (org-table-formula-from-user
  8598. (read-string
  8599. (org-table-formula-to-user
  8600. (format "%s formula %s%s="
  8601. (if named "Field" "Column")
  8602. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8603. scol))
  8604. (if stored (org-table-formula-to-user stored) "")
  8605. 'org-table-formula-history
  8606. )))))
  8607. mustsave)
  8608. (when (not (string-match "\\S-" eq))
  8609. ;; remove formula
  8610. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8611. (org-table-store-formulas stored-list)
  8612. (error "Formula removed"))
  8613. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8614. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8615. (if (and name (not named))
  8616. ;; We set the column equation, delete the named one.
  8617. (setq stored-list (delq (assoc name stored-list) stored-list)
  8618. mustsave t))
  8619. (if stored
  8620. (setcdr (assoc scol stored-list) eq)
  8621. (setq stored-list (cons (cons scol eq) stored-list)))
  8622. (if (or mustsave (not (equal stored eq)))
  8623. (org-table-store-formulas stored-list))
  8624. eq))
  8625. (defun org-table-store-formulas (alist)
  8626. "Store the list of formulas below the current table."
  8627. (setq alist (sort alist 'org-table-formula-less-p))
  8628. (save-excursion
  8629. (goto-char (org-table-end))
  8630. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8631. (progn
  8632. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8633. (goto-char (match-beginning 2))
  8634. (delete-region (match-beginning 2) (match-end 0)))
  8635. (insert "#+TBLFM:"))
  8636. (insert " "
  8637. (mapconcat (lambda (x)
  8638. (concat
  8639. (if (equal (string-to-char (car x)) ?@) "" "$")
  8640. (car x) "=" (cdr x)))
  8641. alist "::")
  8642. "\n")))
  8643. (defsubst org-table-formula-make-cmp-string (a)
  8644. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8645. (concat
  8646. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8647. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8648. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8649. (defun org-table-formula-less-p (a b)
  8650. "Compare two formulas for sorting."
  8651. (let ((as (org-table-formula-make-cmp-string (car a)))
  8652. (bs (org-table-formula-make-cmp-string (car b))))
  8653. (and as bs (string< as bs))))
  8654. (defun org-table-get-stored-formulas (&optional noerror)
  8655. "Return an alist with the stored formulas directly after current table."
  8656. (interactive)
  8657. (let (scol eq eq-alist strings string seen)
  8658. (save-excursion
  8659. (goto-char (org-table-end))
  8660. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8661. (setq strings (org-split-string (match-string 2) " *:: *"))
  8662. (while (setq string (pop strings))
  8663. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8664. (setq scol (if (match-end 2)
  8665. (match-string 2 string)
  8666. (match-string 1 string))
  8667. eq (match-string 3 string)
  8668. eq-alist (cons (cons scol eq) eq-alist))
  8669. (if (member scol seen)
  8670. (if noerror
  8671. (progn
  8672. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8673. (ding)
  8674. (sit-for 2))
  8675. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8676. (push scol seen))))))
  8677. (nreverse eq-alist)))
  8678. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8679. "Modify the equations after the table structure has been edited.
  8680. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8681. For all numbers larger than LIMIT, shift them by DELTA."
  8682. (save-excursion
  8683. (goto-char (org-table-end))
  8684. (when (looking-at "#\\+TBLFM:")
  8685. (let ((re (concat key "\\([0-9]+\\)"))
  8686. (re2
  8687. (when remove
  8688. (if (equal key "$")
  8689. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8690. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8691. s n a)
  8692. (when remove
  8693. (while (re-search-forward re2 (point-at-eol) t)
  8694. (replace-match "")))
  8695. (while (re-search-forward re (point-at-eol) t)
  8696. (setq s (match-string 1) n (string-to-number s))
  8697. (cond
  8698. ((setq a (assoc s replace))
  8699. (replace-match (concat key (cdr a)) t t))
  8700. ((and limit (> n limit))
  8701. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8702. (defun org-table-get-specials ()
  8703. "Get the column names and local parameters for this table."
  8704. (save-excursion
  8705. (let ((beg (org-table-begin)) (end (org-table-end))
  8706. names name fields fields1 field cnt
  8707. c v l line col types dlines hlines)
  8708. (setq org-table-column-names nil
  8709. org-table-local-parameters nil
  8710. org-table-named-field-locations nil
  8711. org-table-current-begin-line nil
  8712. org-table-current-begin-pos nil
  8713. org-table-current-line-types nil)
  8714. (goto-char beg)
  8715. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8716. (setq names (org-split-string (match-string 1) " *| *")
  8717. cnt 1)
  8718. (while (setq name (pop names))
  8719. (setq cnt (1+ cnt))
  8720. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8721. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8722. (setq org-table-column-names (nreverse org-table-column-names))
  8723. (setq org-table-column-name-regexp
  8724. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8725. (goto-char beg)
  8726. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8727. (setq fields (org-split-string (match-string 1) " *| *"))
  8728. (while (setq field (pop fields))
  8729. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8730. (push (cons (match-string 1 field) (match-string 2 field))
  8731. org-table-local-parameters))))
  8732. (goto-char beg)
  8733. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8734. (setq c (match-string 1)
  8735. fields (org-split-string (match-string 2) " *| *"))
  8736. (save-excursion
  8737. (beginning-of-line (if (equal c "_") 2 0))
  8738. (setq line (org-current-line) col 1)
  8739. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8740. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8741. (while (and fields1 (setq field (pop fields)))
  8742. (setq v (pop fields1) col (1+ col))
  8743. (when (and (stringp field) (stringp v)
  8744. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8745. (push (cons field v) org-table-local-parameters)
  8746. (push (list field line col) org-table-named-field-locations))))
  8747. ;; Analyse the line types
  8748. (goto-char beg)
  8749. (setq org-table-current-begin-line (org-current-line)
  8750. org-table-current-begin-pos (point)
  8751. l org-table-current-begin-line)
  8752. (while (looking-at "[ \t]*|\\(-\\)?")
  8753. (push (if (match-end 1) 'hline 'dline) types)
  8754. (if (match-end 1) (push l hlines) (push l dlines))
  8755. (beginning-of-line 2)
  8756. (setq l (1+ l)))
  8757. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8758. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8759. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8760. (defun org-table-maybe-eval-formula ()
  8761. "Check if the current field starts with \"=\" or \":=\".
  8762. If yes, store the formula and apply it."
  8763. ;; We already know we are in a table. Get field will only return a formula
  8764. ;; when appropriate. It might return a separator line, but no problem.
  8765. (when org-table-formula-evaluate-inline
  8766. (let* ((field (org-trim (or (org-table-get-field) "")))
  8767. named eq)
  8768. (when (string-match "^:?=\\(.*\\)" field)
  8769. (setq named (equal (string-to-char field) ?:)
  8770. eq (match-string 1 field))
  8771. (if (or (fboundp 'calc-eval)
  8772. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8773. (org-table-eval-formula (if named '(4) nil)
  8774. (org-table-formula-from-user eq))
  8775. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8776. (defvar org-recalc-commands nil
  8777. "List of commands triggering the recalculation of a line.
  8778. Will be filled automatically during use.")
  8779. (defvar org-recalc-marks
  8780. '((" " . "Unmarked: no special line, no automatic recalculation")
  8781. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8782. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8783. ("!" . "Column name definition line. Reference in formula as $name.")
  8784. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8785. ("_" . "Names for values in row below this one.")
  8786. ("^" . "Names for values in row above this one.")))
  8787. (defun org-table-rotate-recalc-marks (&optional newchar)
  8788. "Rotate the recalculation mark in the first column.
  8789. If in any row, the first field is not consistent with a mark,
  8790. insert a new column for the markers.
  8791. When there is an active region, change all the lines in the region,
  8792. after prompting for the marking character.
  8793. After each change, a message will be displayed indicating the meaning
  8794. of the new mark."
  8795. (interactive)
  8796. (unless (org-at-table-p) (error "Not at a table"))
  8797. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8798. (beg (org-table-begin))
  8799. (end (org-table-end))
  8800. (l (org-current-line))
  8801. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8802. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8803. (have-col
  8804. (save-excursion
  8805. (goto-char beg)
  8806. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8807. (col (org-table-current-column))
  8808. (forcenew (car (assoc newchar org-recalc-marks)))
  8809. epos new)
  8810. (when l1
  8811. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8812. (setq newchar (char-to-string (read-char-exclusive))
  8813. forcenew (car (assoc newchar org-recalc-marks))))
  8814. (if (and newchar (not forcenew))
  8815. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8816. newchar))
  8817. (if l1 (goto-line l1))
  8818. (save-excursion
  8819. (beginning-of-line 1)
  8820. (unless (looking-at org-table-dataline-regexp)
  8821. (error "Not at a table data line")))
  8822. (unless have-col
  8823. (org-table-goto-column 1)
  8824. (org-table-insert-column)
  8825. (org-table-goto-column (1+ col)))
  8826. (setq epos (point-at-eol))
  8827. (save-excursion
  8828. (beginning-of-line 1)
  8829. (org-table-get-field
  8830. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8831. (concat " "
  8832. (setq new (or forcenew
  8833. (cadr (member (match-string 1) marks))))
  8834. " ")
  8835. " # ")))
  8836. (if (and l1 l2)
  8837. (progn
  8838. (goto-line l1)
  8839. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8840. (and (looking-at org-table-dataline-regexp)
  8841. (org-table-get-field 1 (concat " " new " "))))
  8842. (goto-line l1)))
  8843. (if (not (= epos (point-at-eol))) (org-table-align))
  8844. (goto-line l)
  8845. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  8846. (defun org-table-maybe-recalculate-line ()
  8847. "Recompute the current line if marked for it, and if we haven't just done it."
  8848. (interactive)
  8849. (and org-table-allow-automatic-line-recalculation
  8850. (not (and (memq last-command org-recalc-commands)
  8851. (equal org-last-recalc-line (org-current-line))))
  8852. (save-excursion (beginning-of-line 1)
  8853. (looking-at org-table-auto-recalculate-regexp))
  8854. (org-table-recalculate) t))
  8855. (defvar org-table-formula-debug nil
  8856. "Non-nil means, debug table formulas.
  8857. When nil, simply write \"#ERROR\" in corrupted fields.")
  8858. (make-variable-buffer-local 'org-table-formula-debug)
  8859. (defvar modes)
  8860. (defsubst org-set-calc-mode (var &optional value)
  8861. (if (stringp var)
  8862. (setq var (assoc var '(("D" calc-angle-mode deg)
  8863. ("R" calc-angle-mode rad)
  8864. ("F" calc-prefer-frac t)
  8865. ("S" calc-symbolic-mode t)))
  8866. value (nth 2 var) var (nth 1 var)))
  8867. (if (memq var modes)
  8868. (setcar (cdr (memq var modes)) value)
  8869. (cons var (cons value modes)))
  8870. modes)
  8871. (defun org-table-eval-formula (&optional arg equation
  8872. suppress-align suppress-const
  8873. suppress-store suppress-analysis)
  8874. "Replace the table field value at the cursor by the result of a calculation.
  8875. This function makes use of Dave Gillespie's Calc package, in my view the
  8876. most exciting program ever written for GNU Emacs. So you need to have Calc
  8877. installed in order to use this function.
  8878. In a table, this command replaces the value in the current field with the
  8879. result of a formula. It also installs the formula as the \"current\" column
  8880. formula, by storing it in a special line below the table. When called
  8881. with a `C-u' prefix, the current field must ba a named field, and the
  8882. formula is installed as valid in only this specific field.
  8883. When called with two `C-u' prefixes, insert the active equation
  8884. for the field back into the current field, so that it can be
  8885. edited there. This is useful in order to use \\[org-table-show-reference]
  8886. to check the referenced fields.
  8887. When called, the command first prompts for a formula, which is read in
  8888. the minibuffer. Previously entered formulas are available through the
  8889. history list, and the last used formula is offered as a default.
  8890. These stored formulas are adapted correctly when moving, inserting, or
  8891. deleting columns with the corresponding commands.
  8892. The formula can be any algebraic expression understood by the Calc package.
  8893. For details, see the Org-mode manual.
  8894. This function can also be called from Lisp programs and offers
  8895. additional arguments: EQUATION can be the formula to apply. If this
  8896. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  8897. used to speed-up recursive calls by by-passing unnecessary aligns.
  8898. SUPPRESS-CONST suppresses the interpretation of constants in the
  8899. formula, assuming that this has been done already outside the function.
  8900. SUPPRESS-STORE means the formula should not be stored, either because
  8901. it is already stored, or because it is a modified equation that should
  8902. not overwrite the stored one."
  8903. (interactive "P")
  8904. (org-table-check-inside-data-field)
  8905. (or suppress-analysis (org-table-get-specials))
  8906. (if (equal arg '(16))
  8907. (let ((eq (org-table-current-field-formula)))
  8908. (or eq (error "No equation active for current field"))
  8909. (org-table-get-field nil eq)
  8910. (org-table-align)
  8911. (setq org-table-may-need-update t))
  8912. (let* (fields
  8913. (ndown (if (integerp arg) arg 1))
  8914. (org-table-automatic-realign nil)
  8915. (case-fold-search nil)
  8916. (down (> ndown 1))
  8917. (formula (if (and equation suppress-store)
  8918. equation
  8919. (org-table-get-formula equation (equal arg '(4)))))
  8920. (n0 (org-table-current-column))
  8921. (modes (copy-sequence org-calc-default-modes))
  8922. (numbers nil) ; was a variable, now fixed default
  8923. (keep-empty nil)
  8924. n form form0 bw fmt x ev orig c lispp literal)
  8925. ;; Parse the format string. Since we have a lot of modes, this is
  8926. ;; a lot of work. However, I think calc still uses most of the time.
  8927. (if (string-match ";" formula)
  8928. (let ((tmp (org-split-string formula ";")))
  8929. (setq formula (car tmp)
  8930. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  8931. (nth 1 tmp)))
  8932. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  8933. (setq c (string-to-char (match-string 1 fmt))
  8934. n (string-to-number (match-string 2 fmt)))
  8935. (if (= c ?p)
  8936. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  8937. (setq modes (org-set-calc-mode
  8938. 'calc-float-format
  8939. (list (cdr (assoc c '((?n . float) (?f . fix)
  8940. (?s . sci) (?e . eng))))
  8941. n))))
  8942. (setq fmt (replace-match "" t t fmt)))
  8943. (if (string-match "[NT]" fmt)
  8944. (setq numbers (equal (match-string 0 fmt) "N")
  8945. fmt (replace-match "" t t fmt)))
  8946. (if (string-match "L" fmt)
  8947. (setq literal t
  8948. fmt (replace-match "" t t fmt)))
  8949. (if (string-match "E" fmt)
  8950. (setq keep-empty t
  8951. fmt (replace-match "" t t fmt)))
  8952. (while (string-match "[DRFS]" fmt)
  8953. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  8954. (setq fmt (replace-match "" t t fmt)))
  8955. (unless (string-match "\\S-" fmt)
  8956. (setq fmt nil))))
  8957. (if (and (not suppress-const) org-table-formula-use-constants)
  8958. (setq formula (org-table-formula-substitute-names formula)))
  8959. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  8960. (while (> ndown 0)
  8961. (setq fields (org-split-string
  8962. (org-no-properties
  8963. (buffer-substring (point-at-bol) (point-at-eol)))
  8964. " *| *"))
  8965. (if (eq numbers t)
  8966. (setq fields (mapcar
  8967. (lambda (x) (number-to-string (string-to-number x)))
  8968. fields)))
  8969. (setq ndown (1- ndown))
  8970. (setq form (copy-sequence formula)
  8971. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  8972. (if (and lispp literal) (setq lispp 'literal))
  8973. ;; Check for old vertical references
  8974. (setq form (org-rewrite-old-row-references form))
  8975. ;; Insert complex ranges
  8976. (while (string-match org-table-range-regexp form)
  8977. (setq form
  8978. (replace-match
  8979. (save-match-data
  8980. (org-table-make-reference
  8981. (org-table-get-range (match-string 0 form) nil n0)
  8982. keep-empty numbers lispp))
  8983. t t form)))
  8984. ;; Insert simple ranges
  8985. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  8986. (setq form
  8987. (replace-match
  8988. (save-match-data
  8989. (org-table-make-reference
  8990. (org-sublist
  8991. fields (string-to-number (match-string 1 form))
  8992. (string-to-number (match-string 2 form)))
  8993. keep-empty numbers lispp))
  8994. t t form)))
  8995. (setq form0 form)
  8996. ;; Insert the references to fields in same row
  8997. (while (string-match "\\$\\([0-9]+\\)" form)
  8998. (setq n (string-to-number (match-string 1 form))
  8999. x (nth (1- (if (= n 0) n0 n)) fields))
  9000. (unless x (error "Invalid field specifier \"%s\""
  9001. (match-string 0 form)))
  9002. (setq form (replace-match
  9003. (save-match-data
  9004. (org-table-make-reference x nil numbers lispp))
  9005. t t form)))
  9006. (if lispp
  9007. (setq ev (condition-case nil
  9008. (eval (eval (read form)))
  9009. (error "#ERROR"))
  9010. ev (if (numberp ev) (number-to-string ev) ev))
  9011. (or (fboundp 'calc-eval)
  9012. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9013. (setq ev (calc-eval (cons form modes)
  9014. (if numbers 'num))))
  9015. (when org-table-formula-debug
  9016. (with-output-to-temp-buffer "*Substitution History*"
  9017. (princ (format "Substitution history of formula
  9018. Orig: %s
  9019. $xyz-> %s
  9020. @r$c-> %s
  9021. $1-> %s\n" orig formula form0 form))
  9022. (if (listp ev)
  9023. (princ (format " %s^\nError: %s"
  9024. (make-string (car ev) ?\-) (nth 1 ev)))
  9025. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9026. ev (or fmt "NONE")
  9027. (if fmt (format fmt (string-to-number ev)) ev)))))
  9028. (setq bw (get-buffer-window "*Substitution History*"))
  9029. (shrink-window-if-larger-than-buffer bw)
  9030. (unless (and (interactive-p) (not ndown))
  9031. (unless (let (inhibit-redisplay)
  9032. (y-or-n-p "Debugging Formula. Continue to next? "))
  9033. (org-table-align)
  9034. (error "Abort"))
  9035. (delete-window bw)
  9036. (message "")))
  9037. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9038. (org-table-justify-field-maybe
  9039. (if fmt (format fmt (string-to-number ev)) ev))
  9040. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9041. (call-interactively 'org-return)
  9042. (setq ndown 0)))
  9043. (and down (org-table-maybe-recalculate-line))
  9044. (or suppress-align (and org-table-may-need-update
  9045. (org-table-align))))))
  9046. (defun org-table-put-field-property (prop value)
  9047. (save-excursion
  9048. (put-text-property (progn (skip-chars-backward "^|") (point))
  9049. (progn (skip-chars-forward "^|") (point))
  9050. prop value)))
  9051. (defun org-table-get-range (desc &optional tbeg col highlight)
  9052. "Get a calc vector from a column, accorting to descriptor DESC.
  9053. Optional arguments TBEG and COL can give the beginning of the table and
  9054. the current column, to avoid unnecessary parsing.
  9055. HIGHLIGHT means, just highlight the range."
  9056. (if (not (equal (string-to-char desc) ?@))
  9057. (setq desc (concat "@" desc)))
  9058. (save-excursion
  9059. (or tbeg (setq tbeg (org-table-begin)))
  9060. (or col (setq col (org-table-current-column)))
  9061. (let ((thisline (org-current-line))
  9062. beg end c1 c2 r1 r2 rangep tmp)
  9063. (unless (string-match org-table-range-regexp desc)
  9064. (error "Invalid table range specifier `%s'" desc))
  9065. (setq rangep (match-end 3)
  9066. r1 (and (match-end 1) (match-string 1 desc))
  9067. r2 (and (match-end 4) (match-string 4 desc))
  9068. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9069. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9070. (and c1 (setq c1 (+ (string-to-number c1)
  9071. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9072. (and c2 (setq c2 (+ (string-to-number c2)
  9073. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9074. (if (equal r1 "") (setq r1 nil))
  9075. (if (equal r2 "") (setq r2 nil))
  9076. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9077. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9078. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9079. (if (not r1) (setq r1 thisline))
  9080. (if (not r2) (setq r2 thisline))
  9081. (if (not c1) (setq c1 col))
  9082. (if (not c2) (setq c2 col))
  9083. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9084. ;; just one field
  9085. (progn
  9086. (goto-line r1)
  9087. (while (not (looking-at org-table-dataline-regexp))
  9088. (beginning-of-line 2))
  9089. (prog1 (org-trim (org-table-get-field c1))
  9090. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9091. ;; A range, return a vector
  9092. ;; First sort the numbers to get a regular ractangle
  9093. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9094. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9095. (goto-line r1)
  9096. (while (not (looking-at org-table-dataline-regexp))
  9097. (beginning-of-line 2))
  9098. (org-table-goto-column c1)
  9099. (setq beg (point))
  9100. (goto-line r2)
  9101. (while (not (looking-at org-table-dataline-regexp))
  9102. (beginning-of-line 0))
  9103. (org-table-goto-column c2)
  9104. (setq end (point))
  9105. (if highlight
  9106. (org-table-highlight-rectangle
  9107. beg (progn (skip-chars-forward "^|\n") (point))))
  9108. ;; return string representation of calc vector
  9109. (mapcar 'org-trim
  9110. (apply 'append (org-table-copy-region beg end)))))))
  9111. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9112. "Analyze descriptor DESC and retrieve the corresponding line number.
  9113. The cursor is currently in line CLINE, the table begins in line BLINE,
  9114. and TABLE is a vector with line types."
  9115. (if (string-match "^[0-9]+$" desc)
  9116. (aref org-table-dlines (string-to-number desc))
  9117. (setq cline (or cline (org-current-line))
  9118. bline (or bline org-table-current-begin-line)
  9119. table (or table org-table-current-line-types))
  9120. (if (or
  9121. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9122. ;; 1 2 3 4 5 6
  9123. (and (not (match-end 3)) (not (match-end 6)))
  9124. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9125. (error "invalid row descriptor `%s'" desc))
  9126. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9127. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9128. (odir (and (match-end 5) (match-string 5 desc)))
  9129. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9130. (i (- cline bline))
  9131. (rel (and (match-end 6)
  9132. (or (and (match-end 1) (not (match-end 3)))
  9133. (match-end 5)))))
  9134. (if (and hn (not hdir))
  9135. (progn
  9136. (setq i 0 hdir "+")
  9137. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9138. (if (and (not hn) on (not odir))
  9139. (error "should never happen");;(aref org-table-dlines on)
  9140. (if (and hn (> hn 0))
  9141. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9142. (if on
  9143. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9144. (+ bline i)))))
  9145. (defun org-find-row-type (table i type backwards relative n)
  9146. (let ((l (length table)))
  9147. (while (> n 0)
  9148. (while (and (setq i (+ i (if backwards -1 1)))
  9149. (>= i 0) (< i l)
  9150. (not (eq (aref table i) type))
  9151. (if (and relative (eq (aref table i) 'hline))
  9152. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9153. t)))
  9154. (setq n (1- n)))
  9155. (if (or (< i 0) (>= i l))
  9156. (error "Row descriptior leads outside table")
  9157. i)))
  9158. (defun org-rewrite-old-row-references (s)
  9159. (if (string-match "&[-+0-9I]" s)
  9160. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9161. s))
  9162. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9163. "Convert list ELEMENTS to something appropriate to insert into formula.
  9164. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9165. NUMBERS indicates that everything should be converted to numbers.
  9166. LISPP means to return something appropriate for a Lisp list."
  9167. (if (stringp elements) ; just a single val
  9168. (if lispp
  9169. (if (eq lispp 'literal)
  9170. elements
  9171. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9172. (if (equal elements "") (setq elements "0"))
  9173. (if numbers (number-to-string (string-to-number elements)) elements))
  9174. (unless keep-empty
  9175. (setq elements
  9176. (delq nil
  9177. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9178. elements))))
  9179. (setq elements (or elements '("0")))
  9180. (if lispp
  9181. (mapconcat
  9182. (lambda (x)
  9183. (if (eq lispp 'literal)
  9184. x
  9185. (prin1-to-string (if numbers (string-to-number x) x))))
  9186. elements " ")
  9187. (concat "[" (mapconcat
  9188. (lambda (x)
  9189. (if numbers (number-to-string (string-to-number x)) x))
  9190. elements
  9191. ",") "]"))))
  9192. (defun org-table-recalculate (&optional all noalign)
  9193. "Recalculate the current table line by applying all stored formulas.
  9194. With prefix arg ALL, do this for all lines in the table."
  9195. (interactive "P")
  9196. (or (memq this-command org-recalc-commands)
  9197. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9198. (unless (org-at-table-p) (error "Not at a table"))
  9199. (if (equal all '(16))
  9200. (org-table-iterate)
  9201. (org-table-get-specials)
  9202. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9203. (lambda (a b) (string< (car a) (car b)))))
  9204. (inhibit-redisplay (not debug-on-error))
  9205. (line-re org-table-dataline-regexp)
  9206. (thisline (org-current-line))
  9207. (thiscol (org-table-current-column))
  9208. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9209. ;; Insert constants in all formulas
  9210. (setq eqlist
  9211. (mapcar (lambda (x)
  9212. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9213. x)
  9214. eqlist))
  9215. ;; Split the equation list
  9216. (while (setq eq (pop eqlist))
  9217. (if (<= (string-to-char (car eq)) ?9)
  9218. (push eq eqlnum)
  9219. (push eq eqlname)))
  9220. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9221. (if all
  9222. (progn
  9223. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9224. (goto-char (setq beg (org-table-begin)))
  9225. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9226. ;; This is a table with marked lines, compute selected lines
  9227. (setq line-re org-table-recalculate-regexp)
  9228. ;; Move forward to the first non-header line
  9229. (if (and (re-search-forward org-table-dataline-regexp end t)
  9230. (re-search-forward org-table-hline-regexp end t)
  9231. (re-search-forward org-table-dataline-regexp end t))
  9232. (setq beg (match-beginning 0))
  9233. nil))) ;; just leave beg where it is
  9234. (setq beg (point-at-bol)
  9235. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9236. (goto-char beg)
  9237. (and all (message "Re-applying formulas to full table..."))
  9238. ;; First find the named fields, and mark them untouchanble
  9239. (remove-text-properties beg end '(org-untouchable t))
  9240. (while (setq eq (pop eqlname))
  9241. (setq name (car eq)
  9242. a (assoc name org-table-named-field-locations))
  9243. (and (not a)
  9244. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9245. (setq a (list name
  9246. (aref org-table-dlines
  9247. (string-to-number (match-string 1 name)))
  9248. (string-to-number (match-string 2 name)))))
  9249. (when (and a (or all (equal (nth 1 a) thisline)))
  9250. (message "Re-applying formula to field: %s" name)
  9251. (goto-line (nth 1 a))
  9252. (org-table-goto-column (nth 2 a))
  9253. (push (append a (list (cdr eq))) eqlname1)
  9254. (org-table-put-field-property :org-untouchable t)))
  9255. ;; Now evauluate the column formulas, but skip fields covered by
  9256. ;; field formulas
  9257. (goto-char beg)
  9258. (while (re-search-forward line-re end t)
  9259. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9260. ;; Unprotected line, recalculate
  9261. (and all (message "Re-applying formulas to full table...(line %d)"
  9262. (setq cnt (1+ cnt))))
  9263. (setq org-last-recalc-line (org-current-line))
  9264. (setq eql eqlnum)
  9265. (while (setq entry (pop eql))
  9266. (goto-line org-last-recalc-line)
  9267. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9268. (unless (get-text-property (point) :org-untouchable)
  9269. (org-table-eval-formula nil (cdr entry)
  9270. 'noalign 'nocst 'nostore 'noanalysis)))))
  9271. ;; Now evaluate the field formulas
  9272. (while (setq eq (pop eqlname1))
  9273. (message "Re-applying formula to field: %s" (car eq))
  9274. (goto-line (nth 1 eq))
  9275. (org-table-goto-column (nth 2 eq))
  9276. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9277. 'nostore 'noanalysis))
  9278. (goto-line thisline)
  9279. (org-table-goto-column thiscol)
  9280. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9281. (or noalign (and org-table-may-need-update (org-table-align))
  9282. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9283. ;; back to initial position
  9284. (message "Re-applying formulas...done")
  9285. (goto-line thisline)
  9286. (org-table-goto-column thiscol)
  9287. (or noalign (and org-table-may-need-update (org-table-align))
  9288. (and all (message "Re-applying formulas...done"))))))
  9289. (defun org-table-iterate (&optional arg)
  9290. "Recalculate the table until it does not change anymore."
  9291. (interactive "P")
  9292. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9293. (i 0)
  9294. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9295. thistbl)
  9296. (catch 'exit
  9297. (while (< i imax)
  9298. (setq i (1+ i))
  9299. (org-table-recalculate 'all)
  9300. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9301. (if (not (string= lasttbl thistbl))
  9302. (setq lasttbl thistbl)
  9303. (if (> i 1)
  9304. (message "Convergence after %d iterations" i)
  9305. (message "Table was already stable"))
  9306. (throw 'exit t)))
  9307. (error "No convergence after %d iterations" i))))
  9308. (defun org-table-formula-substitute-names (f)
  9309. "Replace $const with values in string F."
  9310. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9311. ;; First, check for column names
  9312. (while (setq start (string-match org-table-column-name-regexp f start))
  9313. (setq start (1+ start))
  9314. (setq a (assoc (match-string 1 f) org-table-column-names))
  9315. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9316. ;; Parameters and constants
  9317. (setq start 0)
  9318. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9319. (setq start (1+ start))
  9320. (if (setq a (save-match-data
  9321. (org-table-get-constant (match-string 1 f))))
  9322. (setq f (replace-match
  9323. (concat (if pp "(") a (if pp ")")) t t f))))
  9324. (if org-table-formula-debug
  9325. (put-text-property 0 (length f) :orig-formula f1 f))
  9326. f))
  9327. (defun org-table-get-constant (const)
  9328. "Find the value for a parameter or constant in a formula.
  9329. Parameters get priority."
  9330. (or (cdr (assoc const org-table-local-parameters))
  9331. (cdr (assoc const org-table-formula-constants-local))
  9332. (cdr (assoc const org-table-formula-constants))
  9333. (and (fboundp 'constants-get) (constants-get const))
  9334. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9335. (org-entry-get nil (substring const 5) 'inherit))
  9336. "#UNDEFINED_NAME"))
  9337. (defvar org-table-fedit-map
  9338. (let ((map (make-sparse-keymap)))
  9339. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9340. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9341. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9342. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9343. (org-defkey map "\C-c?" 'org-table-show-reference)
  9344. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9345. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9346. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9347. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9348. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9349. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9350. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9351. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9352. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9353. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9354. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9355. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9356. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9357. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9358. map))
  9359. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9360. '("Edit-Formulas"
  9361. ["Finish and Install" org-table-fedit-finish t]
  9362. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9363. ["Abort" org-table-fedit-abort t]
  9364. "--"
  9365. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9366. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9367. "--"
  9368. "Shift Reference at Point"
  9369. ["Up" org-table-fedit-ref-up t]
  9370. ["Down" org-table-fedit-ref-down t]
  9371. ["Left" org-table-fedit-ref-left t]
  9372. ["Right" org-table-fedit-ref-right t]
  9373. "-"
  9374. "Change Test Row for Column Formulas"
  9375. ["Up" org-table-fedit-line-up t]
  9376. ["Down" org-table-fedit-line-down t]
  9377. "--"
  9378. ["Scroll Table Window" org-table-fedit-scroll t]
  9379. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9380. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9381. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9382. org-table-overlay-coordinates)]
  9383. "--"
  9384. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9385. :style toggle :selected org-table-buffer-is-an]))
  9386. (defvar org-pos)
  9387. (defun org-table-edit-formulas ()
  9388. "Edit the formulas of the current table in a separate buffer."
  9389. (interactive)
  9390. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9391. (beginning-of-line 0))
  9392. (unless (org-at-table-p) (error "Not at a table"))
  9393. (org-table-get-specials)
  9394. (let ((key (org-table-current-field-formula 'key 'noerror))
  9395. (eql (sort (org-table-get-stored-formulas 'noerror)
  9396. 'org-table-formula-less-p))
  9397. (pos (move-marker (make-marker) (point)))
  9398. (startline 1)
  9399. (wc (current-window-configuration))
  9400. (titles '((column . "# Column Formulas\n")
  9401. (field . "# Field Formulas\n")
  9402. (named . "# Named Field Formulas\n")))
  9403. entry s type title)
  9404. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9405. (erase-buffer)
  9406. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9407. (let ((font-lock-global-modes '(not fundamental-mode)))
  9408. (fundamental-mode))
  9409. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9410. (org-set-local 'org-pos pos)
  9411. (org-set-local 'org-window-configuration wc)
  9412. (use-local-map org-table-fedit-map)
  9413. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9414. (easy-menu-add org-table-fedit-menu)
  9415. (setq startline (org-current-line))
  9416. (while (setq entry (pop eql))
  9417. (setq type (cond
  9418. ((equal (string-to-char (car entry)) ?@) 'field)
  9419. ((string-match "^[0-9]" (car entry)) 'column)
  9420. (t 'named)))
  9421. (when (setq title (assq type titles))
  9422. (or (bobp) (insert "\n"))
  9423. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9424. (setq titles (delq title titles)))
  9425. (if (equal key (car entry)) (setq startline (org-current-line)))
  9426. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9427. (car entry) " = " (cdr entry) "\n"))
  9428. (remove-text-properties 0 (length s) '(face nil) s)
  9429. (insert s))
  9430. (if (eq org-table-use-standard-references t)
  9431. (org-table-fedit-toggle-ref-type))
  9432. (goto-line startline)
  9433. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9434. (defun org-table-fedit-post-command ()
  9435. (when (not (memq this-command '(lisp-complete-symbol)))
  9436. (let ((win (selected-window)))
  9437. (save-excursion
  9438. (condition-case nil
  9439. (org-table-show-reference)
  9440. (error nil))
  9441. (select-window win)))))
  9442. (defun org-table-formula-to-user (s)
  9443. "Convert a formula from internal to user representation."
  9444. (if (eq org-table-use-standard-references t)
  9445. (org-table-convert-refs-to-an s)
  9446. s))
  9447. (defun org-table-formula-from-user (s)
  9448. "Convert a formula from user to internal representation."
  9449. (if org-table-use-standard-references
  9450. (org-table-convert-refs-to-rc s)
  9451. s))
  9452. (defun org-table-convert-refs-to-rc (s)
  9453. "Convert spreadsheet references from AB7 to @7$28.
  9454. Works for single references, but also for entire formulas and even the
  9455. full TBLFM line."
  9456. (let ((start 0))
  9457. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9458. (cond
  9459. ((match-end 3)
  9460. ;; format match, just advance
  9461. (setq start (match-end 0)))
  9462. ((and (> (match-beginning 0) 0)
  9463. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9464. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9465. ;; 3.e5 or something like this.
  9466. (setq start (match-end 0)))
  9467. (t
  9468. (setq start (match-beginning 0)
  9469. s (replace-match
  9470. (if (equal (match-string 2 s) "&")
  9471. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9472. (format "@%d$%d"
  9473. (string-to-number (match-string 2 s))
  9474. (org-letters-to-number (match-string 1 s))))
  9475. t t s)))))
  9476. s))
  9477. (defun org-table-convert-refs-to-an (s)
  9478. "Convert spreadsheet references from to @7$28 to AB7.
  9479. Works for single references, but also for entire formulas and even the
  9480. full TBLFM line."
  9481. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9482. (setq s (replace-match
  9483. (format "%s%d"
  9484. (org-number-to-letters
  9485. (string-to-number (match-string 2 s)))
  9486. (string-to-number (match-string 1 s)))
  9487. t t s)))
  9488. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9489. (setq s (replace-match (concat "\\1"
  9490. (org-number-to-letters
  9491. (string-to-number (match-string 2 s))) "&")
  9492. t nil s)))
  9493. s)
  9494. (defun org-letters-to-number (s)
  9495. "Convert a base 26 number represented by letters into an integer.
  9496. For example: AB -> 28."
  9497. (let ((n 0))
  9498. (setq s (upcase s))
  9499. (while (> (length s) 0)
  9500. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9501. s (substring s 1)))
  9502. n))
  9503. (defun org-number-to-letters (n)
  9504. "Convert an integer into a base 26 number represented by letters.
  9505. For example: 28 -> AB."
  9506. (let ((s ""))
  9507. (while (> n 0)
  9508. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9509. n (/ (1- n) 26)))
  9510. s))
  9511. (defun org-table-fedit-convert-buffer (function)
  9512. "Convert all references in this buffer, using FUNTION."
  9513. (let ((line (org-current-line)))
  9514. (goto-char (point-min))
  9515. (while (not (eobp))
  9516. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9517. (delete-region (point) (point-at-eol))
  9518. (or (eobp) (forward-char 1)))
  9519. (goto-line line)))
  9520. (defun org-table-fedit-toggle-ref-type ()
  9521. "Convert all references in the buffer from B3 to @3$2 and back."
  9522. (interactive)
  9523. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9524. (org-table-fedit-convert-buffer
  9525. (if org-table-buffer-is-an
  9526. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9527. (message "Reference type switched to %s"
  9528. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9529. (defun org-table-fedit-ref-up ()
  9530. "Shift the reference at point one row/hline up."
  9531. (interactive)
  9532. (org-table-fedit-shift-reference 'up))
  9533. (defun org-table-fedit-ref-down ()
  9534. "Shift the reference at point one row/hline down."
  9535. (interactive)
  9536. (org-table-fedit-shift-reference 'down))
  9537. (defun org-table-fedit-ref-left ()
  9538. "Shift the reference at point one field to the left."
  9539. (interactive)
  9540. (org-table-fedit-shift-reference 'left))
  9541. (defun org-table-fedit-ref-right ()
  9542. "Shift the reference at point one field to the right."
  9543. (interactive)
  9544. (org-table-fedit-shift-reference 'right))
  9545. (defun org-table-fedit-shift-reference (dir)
  9546. (cond
  9547. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9548. (if (memq dir '(left right))
  9549. (org-rematch-and-replace 1 (eq dir 'left))
  9550. (error "Cannot shift reference in this direction")))
  9551. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9552. ;; A B3-like reference
  9553. (if (memq dir '(up down))
  9554. (org-rematch-and-replace 2 (eq dir 'up))
  9555. (org-rematch-and-replace 1 (eq dir 'left))))
  9556. ((org-at-regexp-p
  9557. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9558. ;; An internal reference
  9559. (if (memq dir '(up down))
  9560. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9561. (org-rematch-and-replace 5 (eq dir 'left))))))
  9562. (defun org-rematch-and-replace (n &optional decr hline)
  9563. "Re-match the group N, and replace it with the shifted refrence."
  9564. (or (match-end n) (error "Cannot shift reference in this direction"))
  9565. (goto-char (match-beginning n))
  9566. (and (looking-at (regexp-quote (match-string n)))
  9567. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9568. t t)))
  9569. (defun org-shift-refpart (ref &optional decr hline)
  9570. "Shift a refrence part REF.
  9571. If DECR is set, decrease the references row/column, else increase.
  9572. If HLINE is set, this may be a hline reference, it certainly is not
  9573. a translation reference."
  9574. (save-match-data
  9575. (let* ((sign (string-match "^[-+]" ref)) n)
  9576. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9577. (cond
  9578. ((and hline (string-match "^I+" ref))
  9579. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9580. (setq n (+ n (if decr -1 1)))
  9581. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9582. (if sign
  9583. (setq sign (if (< n 0) "-" "+") n (abs n))
  9584. (setq n (max 1 n)))
  9585. (concat sign (make-string n ?I)))
  9586. ((string-match "^[0-9]+" ref)
  9587. (setq n (string-to-number (concat sign ref)))
  9588. (setq n (+ n (if decr -1 1)))
  9589. (if sign
  9590. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9591. (number-to-string (max 1 n))))
  9592. ((string-match "^[a-zA-Z]+" ref)
  9593. (org-number-to-letters
  9594. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9595. (t (error "Cannot shift reference"))))))
  9596. (defun org-table-fedit-toggle-coordinates ()
  9597. "Toggle the display of coordinates in the refrenced table."
  9598. (interactive)
  9599. (let ((pos (marker-position org-pos)))
  9600. (with-current-buffer (marker-buffer org-pos)
  9601. (save-excursion
  9602. (goto-char pos)
  9603. (org-table-toggle-coordinate-overlays)))))
  9604. (defun org-table-fedit-finish (&optional arg)
  9605. "Parse the buffer for formula definitions and install them.
  9606. With prefix ARG, apply the new formulas to the table."
  9607. (interactive "P")
  9608. (org-table-remove-rectangle-highlight)
  9609. (if org-table-use-standard-references
  9610. (progn
  9611. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9612. (setq org-table-buffer-is-an nil)))
  9613. (let ((pos org-pos) eql var form)
  9614. (goto-char (point-min))
  9615. (while (re-search-forward
  9616. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9617. nil t)
  9618. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9619. form (match-string 3))
  9620. (setq form (org-trim form))
  9621. (when (not (equal form ""))
  9622. (while (string-match "[ \t]*\n[ \t]*" form)
  9623. (setq form (replace-match " " t t form)))
  9624. (when (assoc var eql)
  9625. (error "Double formulas for %s" var))
  9626. (push (cons var form) eql)))
  9627. (setq org-pos nil)
  9628. (set-window-configuration org-window-configuration)
  9629. (select-window (get-buffer-window (marker-buffer pos)))
  9630. (goto-char pos)
  9631. (unless (org-at-table-p)
  9632. (error "Lost table position - cannot install formulae"))
  9633. (org-table-store-formulas eql)
  9634. (move-marker pos nil)
  9635. (kill-buffer "*Edit Formulas*")
  9636. (if arg
  9637. (org-table-recalculate 'all)
  9638. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9639. (defun org-table-fedit-abort ()
  9640. "Abort editing formulas, without installing the changes."
  9641. (interactive)
  9642. (org-table-remove-rectangle-highlight)
  9643. (let ((pos org-pos))
  9644. (set-window-configuration org-window-configuration)
  9645. (select-window (get-buffer-window (marker-buffer pos)))
  9646. (goto-char pos)
  9647. (move-marker pos nil)
  9648. (message "Formula editing aborted without installing changes")))
  9649. (defun org-table-fedit-lisp-indent ()
  9650. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9651. (interactive)
  9652. (let ((pos (point)) beg end ind)
  9653. (beginning-of-line 1)
  9654. (cond
  9655. ((looking-at "[ \t]")
  9656. (goto-char pos)
  9657. (call-interactively 'lisp-indent-line))
  9658. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9659. ((not (fboundp 'pp-buffer))
  9660. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9661. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9662. (goto-char (- (match-end 0) 2))
  9663. (setq beg (point))
  9664. (setq ind (make-string (current-column) ?\ ))
  9665. (condition-case nil (forward-sexp 1)
  9666. (error
  9667. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9668. (setq end (point))
  9669. (save-restriction
  9670. (narrow-to-region beg end)
  9671. (if (eq last-command this-command)
  9672. (progn
  9673. (goto-char (point-min))
  9674. (setq this-command nil)
  9675. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9676. (replace-match " ")))
  9677. (pp-buffer)
  9678. (untabify (point-min) (point-max))
  9679. (goto-char (1+ (point-min)))
  9680. (while (re-search-forward "^." nil t)
  9681. (beginning-of-line 1)
  9682. (insert ind))
  9683. (goto-char (point-max))
  9684. (backward-delete-char 1)))
  9685. (goto-char beg))
  9686. (t nil))))
  9687. (defvar org-show-positions nil)
  9688. (defun org-table-show-reference (&optional local)
  9689. "Show the location/value of the $ expression at point."
  9690. (interactive)
  9691. (org-table-remove-rectangle-highlight)
  9692. (catch 'exit
  9693. (let ((pos (if local (point) org-pos))
  9694. (face2 'highlight)
  9695. (org-inhibit-highlight-removal t)
  9696. (win (selected-window))
  9697. (org-show-positions nil)
  9698. var name e what match dest)
  9699. (if local (org-table-get-specials))
  9700. (setq what (cond
  9701. ((or (org-at-regexp-p org-table-range-regexp2)
  9702. (org-at-regexp-p org-table-translate-regexp)
  9703. (org-at-regexp-p org-table-range-regexp))
  9704. (setq match
  9705. (save-match-data
  9706. (org-table-convert-refs-to-rc (match-string 0))))
  9707. 'range)
  9708. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9709. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9710. ((not local) nil)
  9711. (t (error "No reference at point")))
  9712. match (and what (or match (match-string 0))))
  9713. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9714. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9715. 'secondary-selection))
  9716. (org-add-hook 'before-change-functions
  9717. 'org-table-remove-rectangle-highlight)
  9718. (if (eq what 'name) (setq var (substring match 1)))
  9719. (when (eq what 'range)
  9720. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9721. (setq match (org-table-formula-substitute-names match)))
  9722. (unless local
  9723. (save-excursion
  9724. (end-of-line 1)
  9725. (re-search-backward "^\\S-" nil t)
  9726. (beginning-of-line 1)
  9727. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9728. (setq dest
  9729. (save-match-data
  9730. (org-table-convert-refs-to-rc (match-string 1))))
  9731. (org-table-add-rectangle-overlay
  9732. (match-beginning 1) (match-end 1) face2))))
  9733. (if (and (markerp pos) (marker-buffer pos))
  9734. (if (get-buffer-window (marker-buffer pos))
  9735. (select-window (get-buffer-window (marker-buffer pos)))
  9736. (org-switch-to-buffer-other-window (get-buffer-window
  9737. (marker-buffer pos)))))
  9738. (goto-char pos)
  9739. (org-table-force-dataline)
  9740. (when dest
  9741. (setq name (substring dest 1))
  9742. (cond
  9743. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9744. (setq e (assoc name org-table-named-field-locations))
  9745. (goto-line (nth 1 e))
  9746. (org-table-goto-column (nth 2 e)))
  9747. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9748. (let ((l (string-to-number (match-string 1 dest)))
  9749. (c (string-to-number (match-string 2 dest))))
  9750. (goto-line (aref org-table-dlines l))
  9751. (org-table-goto-column c)))
  9752. (t (org-table-goto-column (string-to-number name))))
  9753. (move-marker pos (point))
  9754. (org-table-highlight-rectangle nil nil face2))
  9755. (cond
  9756. ((equal dest match))
  9757. ((not match))
  9758. ((eq what 'range)
  9759. (condition-case nil
  9760. (save-excursion
  9761. (org-table-get-range match nil nil 'highlight))
  9762. (error nil)))
  9763. ((setq e (assoc var org-table-named-field-locations))
  9764. (goto-line (nth 1 e))
  9765. (org-table-goto-column (nth 2 e))
  9766. (org-table-highlight-rectangle (point) (point))
  9767. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9768. ((setq e (assoc var org-table-column-names))
  9769. (org-table-goto-column (string-to-number (cdr e)))
  9770. (org-table-highlight-rectangle (point) (point))
  9771. (goto-char (org-table-begin))
  9772. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9773. (org-table-end) t)
  9774. (progn
  9775. (goto-char (match-beginning 1))
  9776. (org-table-highlight-rectangle)
  9777. (message "Named column (column %s)" (cdr e)))
  9778. (error "Column name not found")))
  9779. ((eq what 'column)
  9780. ;; column number
  9781. (org-table-goto-column (string-to-number (substring match 1)))
  9782. (org-table-highlight-rectangle (point) (point))
  9783. (message "Column %s" (substring match 1)))
  9784. ((setq e (assoc var org-table-local-parameters))
  9785. (goto-char (org-table-begin))
  9786. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9787. (progn
  9788. (goto-char (match-beginning 1))
  9789. (org-table-highlight-rectangle)
  9790. (message "Local parameter."))
  9791. (error "Parameter not found")))
  9792. (t
  9793. (cond
  9794. ((not var) (error "No reference at point"))
  9795. ((setq e (assoc var org-table-formula-constants-local))
  9796. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9797. var (cdr e)))
  9798. ((setq e (assoc var org-table-formula-constants))
  9799. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9800. var (cdr e)))
  9801. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9802. (message "Constant: $%s=%s, from `constants.el'%s."
  9803. var e (format " (%s units)" constants-unit-system)))
  9804. (t (error "Undefined name $%s" var)))))
  9805. (goto-char pos)
  9806. (when (and org-show-positions
  9807. (not (memq this-command '(org-table-fedit-scroll
  9808. org-table-fedit-scroll-down))))
  9809. (push pos org-show-positions)
  9810. (push org-table-current-begin-pos org-show-positions)
  9811. (let ((min (apply 'min org-show-positions))
  9812. (max (apply 'max org-show-positions)))
  9813. (goto-char min) (recenter 0)
  9814. (goto-char max)
  9815. (or (pos-visible-in-window-p max) (recenter -1))))
  9816. (select-window win))))
  9817. (defun org-table-force-dataline ()
  9818. "Make sure the cursor is in a dataline in a table."
  9819. (unless (save-excursion
  9820. (beginning-of-line 1)
  9821. (looking-at org-table-dataline-regexp))
  9822. (let* ((re org-table-dataline-regexp)
  9823. (p1 (save-excursion (re-search-forward re nil 'move)))
  9824. (p2 (save-excursion (re-search-backward re nil 'move))))
  9825. (cond ((and p1 p2)
  9826. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9827. p1 p2)))
  9828. ((or p1 p2) (goto-char (or p1 p2)))
  9829. (t (error "No table dataline around here"))))))
  9830. (defun org-table-fedit-line-up ()
  9831. "Move cursor one line up in the window showing the table."
  9832. (interactive)
  9833. (org-table-fedit-move 'previous-line))
  9834. (defun org-table-fedit-line-down ()
  9835. "Move cursor one line down in the window showing the table."
  9836. (interactive)
  9837. (org-table-fedit-move 'next-line))
  9838. (defun org-table-fedit-move (command)
  9839. "Move the cursor in the window shoinw the table.
  9840. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9841. (let ((org-table-allow-automatic-line-recalculation nil)
  9842. (pos org-pos) (win (selected-window)) p)
  9843. (select-window (get-buffer-window (marker-buffer org-pos)))
  9844. (setq p (point))
  9845. (call-interactively command)
  9846. (while (and (org-at-table-p)
  9847. (org-at-table-hline-p))
  9848. (call-interactively command))
  9849. (or (org-at-table-p) (goto-char p))
  9850. (move-marker pos (point))
  9851. (select-window win)))
  9852. (defun org-table-fedit-scroll (N)
  9853. (interactive "p")
  9854. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9855. (scroll-other-window N)))
  9856. (defun org-table-fedit-scroll-down (N)
  9857. (interactive "p")
  9858. (org-table-fedit-scroll (- N)))
  9859. (defvar org-table-rectangle-overlays nil)
  9860. (defun org-table-add-rectangle-overlay (beg end &optional face)
  9861. "Add a new overlay."
  9862. (let ((ov (org-make-overlay beg end)))
  9863. (org-overlay-put ov 'face (or face 'secondary-selection))
  9864. (push ov org-table-rectangle-overlays)))
  9865. (defun org-table-highlight-rectangle (&optional beg end face)
  9866. "Highlight rectangular region in a table."
  9867. (setq beg (or beg (point)) end (or end (point)))
  9868. (let ((b (min beg end))
  9869. (e (max beg end))
  9870. l1 c1 l2 c2 tmp)
  9871. (and (boundp 'org-show-positions)
  9872. (setq org-show-positions (cons b (cons e org-show-positions))))
  9873. (goto-char (min beg end))
  9874. (setq l1 (org-current-line)
  9875. c1 (org-table-current-column))
  9876. (goto-char (max beg end))
  9877. (setq l2 (org-current-line)
  9878. c2 (org-table-current-column))
  9879. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  9880. (goto-line l1)
  9881. (beginning-of-line 1)
  9882. (loop for line from l1 to l2 do
  9883. (when (looking-at org-table-dataline-regexp)
  9884. (org-table-goto-column c1)
  9885. (skip-chars-backward "^|\n") (setq beg (point))
  9886. (org-table-goto-column c2)
  9887. (skip-chars-forward "^|\n") (setq end (point))
  9888. (org-table-add-rectangle-overlay beg end face))
  9889. (beginning-of-line 2))
  9890. (goto-char b))
  9891. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  9892. (defun org-table-remove-rectangle-highlight (&rest ignore)
  9893. "Remove the rectangle overlays."
  9894. (unless org-inhibit-highlight-removal
  9895. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  9896. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  9897. (setq org-table-rectangle-overlays nil)))
  9898. (defvar org-table-coordinate-overlays nil
  9899. "Collects the cooordinate grid overlays, so that they can be removed.")
  9900. (make-variable-buffer-local 'org-table-coordinate-overlays)
  9901. (defun org-table-overlay-coordinates ()
  9902. "Add overlays to the table at point, to show row/column coordinates."
  9903. (interactive)
  9904. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9905. (setq org-table-coordinate-overlays nil)
  9906. (save-excursion
  9907. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  9908. (goto-char (org-table-begin))
  9909. (while (org-at-table-p)
  9910. (setq eol (point-at-eol))
  9911. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  9912. (push ov org-table-coordinate-overlays)
  9913. (setq hline (looking-at org-table-hline-regexp))
  9914. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  9915. (format "%4d" (setq id (1+ id)))))
  9916. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  9917. (when hline
  9918. (setq ic 0)
  9919. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  9920. (setq beg (1+ (match-beginning 0))
  9921. ic (1+ ic)
  9922. s1 (concat "$" (int-to-string ic))
  9923. s2 (org-number-to-letters ic)
  9924. str (if (eq org-table-use-standard-references t) s2 s1))
  9925. (setq ov (org-make-overlay beg (+ beg (length str))))
  9926. (push ov org-table-coordinate-overlays)
  9927. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  9928. (beginning-of-line 2)))))
  9929. (defun org-table-toggle-coordinate-overlays ()
  9930. "Toggle the display of Row/Column numbers in tables."
  9931. (interactive)
  9932. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  9933. (message "Row/Column number display turned %s"
  9934. (if org-table-overlay-coordinates "on" "off"))
  9935. (if (and (org-at-table-p) org-table-overlay-coordinates)
  9936. (org-table-align))
  9937. (unless org-table-overlay-coordinates
  9938. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9939. (setq org-table-coordinate-overlays nil)))
  9940. (defun org-table-toggle-formula-debugger ()
  9941. "Toggle the formula debugger in tables."
  9942. (interactive)
  9943. (setq org-table-formula-debug (not org-table-formula-debug))
  9944. (message "Formula debugging has been turned %s"
  9945. (if org-table-formula-debug "on" "off")))
  9946. ;;; The orgtbl minor mode
  9947. ;; Define a minor mode which can be used in other modes in order to
  9948. ;; integrate the org-mode table editor.
  9949. ;; This is really a hack, because the org-mode table editor uses several
  9950. ;; keys which normally belong to the major mode, for example the TAB and
  9951. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9952. ;; necessary to operate the table editor, but wraps the commands into a
  9953. ;; function which tests if the cursor is currently inside a table. If that
  9954. ;; is the case, the table editor command is executed. However, when any of
  9955. ;; those keys is used outside a table, the function uses `key-binding' to
  9956. ;; look up if the key has an associated command in another currently active
  9957. ;; keymap (minor modes, major mode, global), and executes that command.
  9958. ;; There might be problems if any of the keys used by the table editor is
  9959. ;; otherwise used as a prefix key.
  9960. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9961. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9962. ;; addresses this by checking explicitly for both bindings.
  9963. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9964. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9965. ;; Some modes bind other commands to simple characters, for example
  9966. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9967. ;; active, this binding is ignored inside tables and replaced with a
  9968. ;; modified self-insert.
  9969. (defvar orgtbl-mode nil
  9970. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9971. table editor in arbitrary modes.")
  9972. (make-variable-buffer-local 'orgtbl-mode)
  9973. (defvar orgtbl-mode-map (make-keymap)
  9974. "Keymap for `orgtbl-mode'.")
  9975. ;;;###autoload
  9976. (defun turn-on-orgtbl ()
  9977. "Unconditionally turn on `orgtbl-mode'."
  9978. (orgtbl-mode 1))
  9979. (defvar org-old-auto-fill-inhibit-regexp nil
  9980. "Local variable used by `orgtbl-mode'")
  9981. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  9982. "Matches a line belonging to an orgtbl.")
  9983. (defconst orgtbl-extra-font-lock-keywords
  9984. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  9985. 0 (quote 'org-table) 'prepend))
  9986. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  9987. ;;;###autoload
  9988. (defun orgtbl-mode (&optional arg)
  9989. "The `org-mode' table editor as a minor mode for use in other modes."
  9990. (interactive)
  9991. (if (org-mode-p)
  9992. ;; Exit without error, in case some hook functions calls this
  9993. ;; by accident in org-mode.
  9994. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9995. (setq orgtbl-mode
  9996. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9997. (if orgtbl-mode
  9998. (progn
  9999. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10000. ;; Make sure we are first in minor-mode-map-alist
  10001. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10002. (and c (setq minor-mode-map-alist
  10003. (cons c (delq c minor-mode-map-alist)))))
  10004. (org-set-local (quote org-table-may-need-update) t)
  10005. (org-add-hook 'before-change-functions 'org-before-change-function
  10006. nil 'local)
  10007. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10008. auto-fill-inhibit-regexp)
  10009. (org-set-local 'auto-fill-inhibit-regexp
  10010. (if auto-fill-inhibit-regexp
  10011. (concat orgtbl-line-start-regexp "\\|"
  10012. auto-fill-inhibit-regexp)
  10013. orgtbl-line-start-regexp))
  10014. (org-add-to-invisibility-spec '(org-cwidth))
  10015. (when (fboundp 'font-lock-add-keywords)
  10016. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10017. (org-restart-font-lock))
  10018. (easy-menu-add orgtbl-mode-menu)
  10019. (run-hooks 'orgtbl-mode-hook))
  10020. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10021. (org-cleanup-narrow-column-properties)
  10022. (org-remove-from-invisibility-spec '(org-cwidth))
  10023. (remove-hook 'before-change-functions 'org-before-change-function t)
  10024. (when (fboundp 'font-lock-remove-keywords)
  10025. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10026. (org-restart-font-lock))
  10027. (easy-menu-remove orgtbl-mode-menu)
  10028. (force-mode-line-update 'all))))
  10029. (defun org-cleanup-narrow-column-properties ()
  10030. "Remove all properties related to narrow-column invisibility."
  10031. (let ((s 1))
  10032. (while (setq s (text-property-any s (point-max)
  10033. 'display org-narrow-column-arrow))
  10034. (remove-text-properties s (1+ s) '(display t)))
  10035. (setq s 1)
  10036. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10037. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10038. (setq s 1)
  10039. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10040. (remove-text-properties s (1+ s) '(invisible t)))))
  10041. ;; Install it as a minor mode.
  10042. (put 'orgtbl-mode :included t)
  10043. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10044. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10045. (defun orgtbl-make-binding (fun n &rest keys)
  10046. "Create a function for binding in the table minor mode.
  10047. FUN is the command to call inside a table. N is used to create a unique
  10048. command name. KEYS are keys that should be checked in for a command
  10049. to execute outside of tables."
  10050. (eval
  10051. (list 'defun
  10052. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10053. '(arg)
  10054. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10055. "Outside of tables, run the binding of `"
  10056. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10057. "'.")
  10058. '(interactive "p")
  10059. (list 'if
  10060. '(org-at-table-p)
  10061. (list 'call-interactively (list 'quote fun))
  10062. (list 'let '(orgtbl-mode)
  10063. (list 'call-interactively
  10064. (append '(or)
  10065. (mapcar (lambda (k)
  10066. (list 'key-binding k))
  10067. keys)
  10068. '('orgtbl-error))))))))
  10069. (defun orgtbl-error ()
  10070. "Error when there is no default binding for a table key."
  10071. (interactive)
  10072. (error "This key is has no function outside tables"))
  10073. (defun orgtbl-setup ()
  10074. "Setup orgtbl keymaps."
  10075. (let ((nfunc 0)
  10076. (bindings
  10077. (list
  10078. '([(meta shift left)] org-table-delete-column)
  10079. '([(meta left)] org-table-move-column-left)
  10080. '([(meta right)] org-table-move-column-right)
  10081. '([(meta shift right)] org-table-insert-column)
  10082. '([(meta shift up)] org-table-kill-row)
  10083. '([(meta shift down)] org-table-insert-row)
  10084. '([(meta up)] org-table-move-row-up)
  10085. '([(meta down)] org-table-move-row-down)
  10086. '("\C-c\C-w" org-table-cut-region)
  10087. '("\C-c\M-w" org-table-copy-region)
  10088. '("\C-c\C-y" org-table-paste-rectangle)
  10089. '("\C-c-" org-table-insert-hline)
  10090. '("\C-c}" org-table-toggle-coordinate-overlays)
  10091. '("\C-c{" org-table-toggle-formula-debugger)
  10092. '("\C-m" org-table-next-row)
  10093. '([(shift return)] org-table-copy-down)
  10094. '("\C-c\C-q" org-table-wrap-region)
  10095. '("\C-c?" org-table-field-info)
  10096. '("\C-c " org-table-blank-field)
  10097. '("\C-c+" org-table-sum)
  10098. '("\C-c=" org-table-eval-formula)
  10099. '("\C-c'" org-table-edit-formulas)
  10100. '("\C-c`" org-table-edit-field)
  10101. '("\C-c*" org-table-recalculate)
  10102. '("\C-c|" org-table-create-or-convert-from-region)
  10103. '("\C-c^" org-table-sort-lines)
  10104. '([(control ?#)] org-table-rotate-recalc-marks)))
  10105. elt key fun cmd)
  10106. (while (setq elt (pop bindings))
  10107. (setq nfunc (1+ nfunc))
  10108. (setq key (org-key (car elt))
  10109. fun (nth 1 elt)
  10110. cmd (orgtbl-make-binding fun nfunc key))
  10111. (org-defkey orgtbl-mode-map key cmd))
  10112. ;; Special treatment needed for TAB and RET
  10113. (org-defkey orgtbl-mode-map [(return)]
  10114. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10115. (org-defkey orgtbl-mode-map "\C-m"
  10116. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10117. (org-defkey orgtbl-mode-map [(tab)]
  10118. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10119. (org-defkey orgtbl-mode-map "\C-i"
  10120. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10121. (org-defkey orgtbl-mode-map [(shift tab)]
  10122. (orgtbl-make-binding 'org-table-previous-field 104
  10123. [(shift tab)] [(tab)] "\C-i"))
  10124. (org-defkey orgtbl-mode-map "\M-\C-m"
  10125. (orgtbl-make-binding 'org-table-wrap-region 105
  10126. "\M-\C-m" [(meta return)]))
  10127. (org-defkey orgtbl-mode-map [(meta return)]
  10128. (orgtbl-make-binding 'org-table-wrap-region 106
  10129. [(meta return)] "\M-\C-m"))
  10130. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10131. (when orgtbl-optimized
  10132. ;; If the user wants maximum table support, we need to hijack
  10133. ;; some standard editing functions
  10134. (org-remap orgtbl-mode-map
  10135. 'self-insert-command 'orgtbl-self-insert-command
  10136. 'delete-char 'org-delete-char
  10137. 'delete-backward-char 'org-delete-backward-char)
  10138. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10139. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10140. '("OrgTbl"
  10141. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10142. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10143. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10144. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10145. "--"
  10146. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10147. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10148. ["Copy Field from Above"
  10149. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10150. "--"
  10151. ("Column"
  10152. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10153. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10154. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10155. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10156. ("Row"
  10157. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10158. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10159. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10160. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10161. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10162. "--"
  10163. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10164. ("Rectangle"
  10165. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10166. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10167. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10168. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10169. "--"
  10170. ("Radio tables"
  10171. ["Insert table template" orgtbl-insert-radio-table
  10172. (assq major-mode orgtbl-radio-table-templates)]
  10173. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10174. "--"
  10175. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10176. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10177. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10178. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10179. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10180. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10181. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10182. ["Sum Column/Rectangle" org-table-sum
  10183. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10184. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10185. ["Debug Formulas"
  10186. org-table-toggle-formula-debugger :active (org-at-table-p)
  10187. :keys "C-c {"
  10188. :style toggle :selected org-table-formula-debug]
  10189. ["Show Col/Row Numbers"
  10190. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10191. :keys "C-c }"
  10192. :style toggle :selected org-table-overlay-coordinates]
  10193. ))
  10194. t))
  10195. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10196. "If the cursor is inside a table, realign the table.
  10197. It it is a table to be sent away to a receiver, do it.
  10198. With prefix arg, also recompute table."
  10199. (interactive "P")
  10200. (let ((pos (point)) action)
  10201. (save-excursion
  10202. (beginning-of-line 1)
  10203. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10204. ((looking-at "[ \t]*|") pos)
  10205. ((looking-at "#\\+TBLFM:") 'recalc))))
  10206. (cond
  10207. ((integerp action)
  10208. (goto-char action)
  10209. (org-table-maybe-eval-formula)
  10210. (if arg
  10211. (call-interactively 'org-table-recalculate)
  10212. (org-table-maybe-recalculate-line))
  10213. (call-interactively 'org-table-align)
  10214. (orgtbl-send-table 'maybe))
  10215. ((eq action 'recalc)
  10216. (save-excursion
  10217. (beginning-of-line 1)
  10218. (skip-chars-backward " \r\n\t")
  10219. (if (org-at-table-p)
  10220. (org-call-with-arg 'org-table-recalculate t))))
  10221. (t (let (orgtbl-mode)
  10222. (call-interactively (key-binding "\C-c\C-c")))))))
  10223. (defun orgtbl-tab (arg)
  10224. "Justification and field motion for `orgtbl-mode'."
  10225. (interactive "P")
  10226. (if arg (org-table-edit-field t)
  10227. (org-table-justify-field-maybe)
  10228. (org-table-next-field)))
  10229. (defun orgtbl-ret ()
  10230. "Justification and field motion for `orgtbl-mode'."
  10231. (interactive)
  10232. (org-table-justify-field-maybe)
  10233. (org-table-next-row))
  10234. (defun orgtbl-self-insert-command (N)
  10235. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10236. If the cursor is in a table looking at whitespace, the whitespace is
  10237. overwritten, and the table is not marked as requiring realignment."
  10238. (interactive "p")
  10239. (if (and (org-at-table-p)
  10240. (or
  10241. (and org-table-auto-blank-field
  10242. (member last-command
  10243. '(orgtbl-hijacker-command-100
  10244. orgtbl-hijacker-command-101
  10245. orgtbl-hijacker-command-102
  10246. orgtbl-hijacker-command-103
  10247. orgtbl-hijacker-command-104
  10248. orgtbl-hijacker-command-105))
  10249. (org-table-blank-field))
  10250. t)
  10251. (eq N 1)
  10252. (looking-at "[^|\n]* +|"))
  10253. (let (org-table-may-need-update)
  10254. (goto-char (1- (match-end 0)))
  10255. (delete-backward-char 1)
  10256. (goto-char (match-beginning 0))
  10257. (self-insert-command N))
  10258. (setq org-table-may-need-update t)
  10259. (let (orgtbl-mode)
  10260. (call-interactively (key-binding (vector last-input-event))))))
  10261. (defun org-force-self-insert (N)
  10262. "Needed to enforce self-insert under remapping."
  10263. (interactive "p")
  10264. (self-insert-command N))
  10265. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10266. "Regula expression matching exponentials as produced by calc.")
  10267. (defvar org-table-clean-did-remove-column nil)
  10268. (defun orgtbl-export (table target)
  10269. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10270. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10271. org-table-last-alignment org-table-last-column-widths
  10272. maxcol column)
  10273. (if (not (fboundp func))
  10274. (error "Cannot export orgtbl table to %s" target))
  10275. (setq lines (org-table-clean-before-export lines))
  10276. (setq table
  10277. (mapcar
  10278. (lambda (x)
  10279. (if (string-match org-table-hline-regexp x)
  10280. 'hline
  10281. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10282. lines))
  10283. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10284. table)))
  10285. (loop for i from (1- maxcol) downto 0 do
  10286. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10287. (setq column (delq nil column))
  10288. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10289. (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))
  10290. (funcall func table nil)))
  10291. (defun orgtbl-send-table (&optional maybe)
  10292. "Send a tranformed version of this table to the receiver position.
  10293. With argument MAYBE, fail quietly if no transformation is defined for
  10294. this table."
  10295. (interactive)
  10296. (catch 'exit
  10297. (unless (org-at-table-p) (error "Not at a table"))
  10298. ;; when non-interactive, we assume align has just happened.
  10299. (when (interactive-p) (org-table-align))
  10300. (save-excursion
  10301. (goto-char (org-table-begin))
  10302. (beginning-of-line 0)
  10303. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10304. (if maybe
  10305. (throw 'exit nil)
  10306. (error "Don't know how to transform this table."))))
  10307. (let* ((name (match-string 1))
  10308. beg
  10309. (transform (intern (match-string 2)))
  10310. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10311. (skip (plist-get params :skip))
  10312. (skipcols (plist-get params :skipcols))
  10313. (txt (buffer-substring-no-properties
  10314. (org-table-begin) (org-table-end)))
  10315. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10316. (lines (org-table-clean-before-export lines))
  10317. (i0 (if org-table-clean-did-remove-column 2 1))
  10318. (table (mapcar
  10319. (lambda (x)
  10320. (if (string-match org-table-hline-regexp x)
  10321. 'hline
  10322. (org-remove-by-index
  10323. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10324. skipcols i0)))
  10325. lines))
  10326. (fun (if (= i0 2) 'cdr 'identity))
  10327. (org-table-last-alignment
  10328. (org-remove-by-index (funcall fun org-table-last-alignment)
  10329. skipcols i0))
  10330. (org-table-last-column-widths
  10331. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10332. skipcols i0)))
  10333. (unless (fboundp transform)
  10334. (error "No such transformation function %s" transform))
  10335. (setq txt (funcall transform table params))
  10336. ;; Find the insertion place
  10337. (save-excursion
  10338. (goto-char (point-min))
  10339. (unless (re-search-forward
  10340. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10341. (error "Don't know where to insert translated table"))
  10342. (goto-char (match-beginning 0))
  10343. (beginning-of-line 2)
  10344. (setq beg (point))
  10345. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10346. (error "Cannot find end of insertion region"))
  10347. (beginning-of-line 1)
  10348. (delete-region beg (point))
  10349. (goto-char beg)
  10350. (insert txt "\n"))
  10351. (message "Table converted and installed at receiver location"))))
  10352. (defun org-remove-by-index (list indices &optional i0)
  10353. "Remove the elements in LIST with indices in INDICES.
  10354. First element has index 0, or I0 if given."
  10355. (if (not indices)
  10356. list
  10357. (if (integerp indices) (setq indices (list indices)))
  10358. (setq i0 (1- (or i0 0)))
  10359. (delq :rm (mapcar (lambda (x)
  10360. (setq i0 (1+ i0))
  10361. (if (memq i0 indices) :rm x))
  10362. list))))
  10363. (defun orgtbl-toggle-comment ()
  10364. "Comment or uncomment the orgtbl at point."
  10365. (interactive)
  10366. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10367. (re2 (concat "^" orgtbl-line-start-regexp))
  10368. (commented (save-excursion (beginning-of-line 1)
  10369. (cond ((looking-at re1) t)
  10370. ((looking-at re2) nil)
  10371. (t (error "Not at an org table")))))
  10372. (re (if commented re1 re2))
  10373. beg end)
  10374. (save-excursion
  10375. (beginning-of-line 1)
  10376. (while (looking-at re) (beginning-of-line 0))
  10377. (beginning-of-line 2)
  10378. (setq beg (point))
  10379. (while (looking-at re) (beginning-of-line 2))
  10380. (setq end (point)))
  10381. (comment-region beg end (if commented '(4) nil))))
  10382. (defun orgtbl-insert-radio-table ()
  10383. "Insert a radio table template appropriate for this major mode."
  10384. (interactive)
  10385. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10386. (txt (nth 1 e))
  10387. name pos)
  10388. (unless e (error "No radio table setup defined for %s" major-mode))
  10389. (setq name (read-string "Table name: "))
  10390. (while (string-match "%n" txt)
  10391. (setq txt (replace-match name t t txt)))
  10392. (or (bolp) (insert "\n"))
  10393. (setq pos (point))
  10394. (insert txt)
  10395. (goto-char pos)))
  10396. (defun org-get-param (params header i sym &optional hsym)
  10397. "Get parameter value for symbol SYM.
  10398. If this is a header line, actually get the value for the symbol with an
  10399. additional \"h\" inserted after the colon.
  10400. If the value is a protperty list, get the element for the current column.
  10401. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10402. (let ((val (plist-get params sym)))
  10403. (and hsym header (setq val (or (plist-get params hsym) val)))
  10404. (if (consp val) (plist-get val i) val)))
  10405. (defun orgtbl-to-generic (table params)
  10406. "Convert the orgtbl-mode TABLE to some other format.
  10407. This generic routine can be used for many standard cases.
  10408. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10409. separator line, or a list of fields for that line.
  10410. PARAMS is a property list of parameters that can influence the conversion.
  10411. For the generic converter, some parameters are obligatory: You need to
  10412. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10413. :splice, you must have :tstart and :tend.
  10414. Valid parameters are
  10415. :tstart String to start the table. Ignored when :splice is t.
  10416. :tend String to end the table. Ignored when :splice is t.
  10417. :splice When set to t, return only table body lines, don't wrap
  10418. them into :tstart and :tend. Default is nil.
  10419. :hline String to be inserted on horizontal separation lines.
  10420. May be nil to ignore hlines.
  10421. :lstart String to start a new table line.
  10422. :lend String to end a table line
  10423. :sep Separator between two fields
  10424. :lfmt Format for entire line, with enough %s to capture all fields.
  10425. If this is present, :lstart, :lend, and :sep are ignored.
  10426. :fmt A format to be used to wrap the field, should contain
  10427. %s for the original field value. For example, to wrap
  10428. everything in dollars, you could use :fmt \"$%s$\".
  10429. This may also be a property list with column numbers and
  10430. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10431. :hlstart :hlend :hlsep :hlfmt :hfmt
  10432. Same as above, specific for the header lines in the table.
  10433. All lines before the first hline are treated as header.
  10434. If any of these is not present, the data line value is used.
  10435. :efmt Use this format to print numbers with exponentials.
  10436. The format should have %s twice for inserting mantissa
  10437. and exponent, for example \"%s\\\\times10^{%s}\". This
  10438. may also be a property list with column numbers and
  10439. formats. :fmt will still be applied after :efmt.
  10440. In addition to this, the parameters :skip and :skipcols are always handled
  10441. directly by `orgtbl-send-table'. See manual."
  10442. (interactive)
  10443. (let* ((p params)
  10444. (splicep (plist-get p :splice))
  10445. (hline (plist-get p :hline))
  10446. rtn line i fm efm lfmt h)
  10447. ;; Do we have a header?
  10448. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10449. (setq h t))
  10450. ;; Put header
  10451. (unless splicep
  10452. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10453. ;; Now loop over all lines
  10454. (while (setq line (pop table))
  10455. (if (eq line 'hline)
  10456. ;; A horizontal separator line
  10457. (progn (if hline (push hline rtn))
  10458. (setq h nil)) ; no longer in header
  10459. ;; A normal line. Convert the fields, push line onto the result list
  10460. (setq i 0)
  10461. (setq line
  10462. (mapcar
  10463. (lambda (f)
  10464. (setq i (1+ i)
  10465. fm (org-get-param p h i :fmt :hfmt)
  10466. efm (org-get-param p h i :efmt))
  10467. (if (and efm (string-match orgtbl-exp-regexp f))
  10468. (setq f (format
  10469. efm (match-string 1 f) (match-string 2 f))))
  10470. (if fm (setq f (format fm f)))
  10471. f)
  10472. line))
  10473. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10474. (push (apply 'format lfmt line) rtn)
  10475. (push (concat
  10476. (org-get-param p h i :lstart :hlstart)
  10477. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10478. (org-get-param p h i :lend :hlend))
  10479. rtn))))
  10480. (unless splicep
  10481. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10482. (mapconcat 'identity (nreverse rtn) "\n")))
  10483. (defun orgtbl-to-latex (table params)
  10484. "Convert the orgtbl-mode TABLE to LaTeX.
  10485. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10486. separator line, or a list of fields for that line.
  10487. PARAMS is a property list of parameters that can influence the conversion.
  10488. Supports all parameters from `orgtbl-to-generic'. Most important for
  10489. LaTeX are:
  10490. :splice When set to t, return only table body lines, don't wrap
  10491. them into a tabular environment. Default is nil.
  10492. :fmt A format to be used to wrap the field, should contain %s for the
  10493. original field value. For example, to wrap everything in dollars,
  10494. use :fmt \"$%s$\". This may also be a property list with column
  10495. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10496. :efmt Format for transforming numbers with exponentials. The format
  10497. should have %s twice for inserting mantissa and exponent, for
  10498. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10499. This may also be a property list with column numbers and formats.
  10500. The general parameters :skip and :skipcols have already been applied when
  10501. this function is called."
  10502. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10503. org-table-last-alignment ""))
  10504. (params2
  10505. (list
  10506. :tstart (concat "\\begin{tabular}{" alignment "}")
  10507. :tend "\\end{tabular}"
  10508. :lstart "" :lend " \\\\" :sep " & "
  10509. :efmt "%s\\,(%s)" :hline "\\hline")))
  10510. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10511. (defun orgtbl-to-html (table params)
  10512. "Convert the orgtbl-mode TABLE to LaTeX.
  10513. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10514. separator line, or a list of fields for that line.
  10515. PARAMS is a property list of parameters that can influence the conversion.
  10516. Currently this function recognizes the following parameters:
  10517. :splice When set to t, return only table body lines, don't wrap
  10518. them into a <table> environment. Default is nil.
  10519. The general parameters :skip and :skipcols have already been applied when
  10520. this function is called. The function does *not* use `orgtbl-to-generic',
  10521. so you cannot specify parameters for it."
  10522. (let* ((splicep (plist-get params :splice))
  10523. html)
  10524. ;; Just call the formatter we already have
  10525. ;; We need to make text lines for it, so put the fields back together.
  10526. (setq html (org-format-org-table-html
  10527. (mapcar
  10528. (lambda (x)
  10529. (if (eq x 'hline)
  10530. "|----+----|"
  10531. (concat "| " (mapconcat 'identity x " | ") " |")))
  10532. table)
  10533. splicep))
  10534. (if (string-match "\n+\\'" html)
  10535. (setq html (replace-match "" t t html)))
  10536. html))
  10537. (defun orgtbl-to-texinfo (table params)
  10538. "Convert the orgtbl-mode TABLE to TeXInfo.
  10539. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10540. separator line, or a list of fields for that line.
  10541. PARAMS is a property list of parameters that can influence the conversion.
  10542. Supports all parameters from `orgtbl-to-generic'. Most important for
  10543. TeXInfo are:
  10544. :splice nil/t When set to t, return only table body lines, don't wrap
  10545. them into a multitable environment. Default is nil.
  10546. :fmt fmt A format to be used to wrap the field, should contain
  10547. %s for the original field value. For example, to wrap
  10548. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10549. This may also be a property list with column numbers and
  10550. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10551. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  10552. are computed automatically from the width of the columns
  10553. under org-mode.
  10554. The general parameters :skip and :skipcols have already been applied when
  10555. this function is called."
  10556. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10557. (colfrac (or (plist-get params :cf)
  10558. (mapconcat
  10559. (lambda (x) (format "%.3f" (/ (float x) total)))
  10560. org-table-last-column-widths " ")))
  10561. (params2
  10562. (list
  10563. :tstart (concat "@multitable @columnfractions " colfrac)
  10564. :tend "@end multitable"
  10565. :lstart "@item " :lend "" :sep " @tab "
  10566. :hlstart "@headitem ")))
  10567. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10568. ;;;; Link Stuff
  10569. ;;; Link abbreviations
  10570. (defun org-link-expand-abbrev (link)
  10571. "Apply replacements as defined in `org-link-abbrev-alist."
  10572. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10573. (let* ((key (match-string 1 link))
  10574. (as (or (assoc key org-link-abbrev-alist-local)
  10575. (assoc key org-link-abbrev-alist)))
  10576. (tag (and (match-end 2) (match-string 3 link)))
  10577. rpl)
  10578. (if (not as)
  10579. link
  10580. (setq rpl (cdr as))
  10581. (cond
  10582. ((symbolp rpl) (funcall rpl tag))
  10583. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10584. (t (concat rpl tag)))))
  10585. link))
  10586. ;;; Storing and inserting links
  10587. (defvar org-insert-link-history nil
  10588. "Minibuffer history for links inserted with `org-insert-link'.")
  10589. (defvar org-stored-links nil
  10590. "Contains the links stored with `org-store-link'.")
  10591. (defvar org-store-link-plist nil
  10592. "Plist with info about the most recently link created with `org-store-link'.")
  10593. (defvar org-link-protocols nil
  10594. "Link protocols added to Org-mode using `org-add-link-type'.")
  10595. (defvar org-store-link-functions nil
  10596. "List of functions that are called to create and store a link.
  10597. Each function will be called in turn until one returns a non-nil
  10598. value. Each function should check if it is responsible for creating
  10599. this link (for example by looking at the major mode).
  10600. If not, it must exit and return nil.
  10601. If yes, it should return a non-nil value after a calling
  10602. `org-store-link-props' with a list of properties and values.
  10603. Special properties are:
  10604. :type The link prefix. like \"http\". This must be given.
  10605. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10606. This is obligatory as well.
  10607. :description Optional default description for the second pair
  10608. of brackets in an Org-mode link. The user can still change
  10609. this when inserting this link into an Org-mode buffer.
  10610. In addition to these, any additional properties can be specified
  10611. and then used in remember templates.")
  10612. (defun org-add-link-type (type &optional follow publish)
  10613. "Add TYPE to the list of `org-link-types'.
  10614. Re-compute all regular expressions depending on `org-link-types'
  10615. FOLLOW and PUBLISH are two functions. Both take the link path as
  10616. an argument.
  10617. FOLLOW should do whatever is necessary to follow the link, for example
  10618. to find a file or display a mail message.
  10619. PUBLISH takes the path and retuns the string that should be used when
  10620. this document is published. FIMXE: This is actually not yet implemented."
  10621. (add-to-list 'org-link-types type t)
  10622. (org-make-link-regexps)
  10623. (add-to-list 'org-link-protocols
  10624. (list type follow publish)))
  10625. (defun org-add-agenda-custom-command (entry)
  10626. "Replace or add a command in `org-agenda-custom-commands'.
  10627. This is mostly for hacking and trying a new command - once the command
  10628. works you probably want to add it to `org-agenda-custom-commands' for good."
  10629. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10630. (if ass
  10631. (setcdr ass (cdr entry))
  10632. (push entry org-agenda-custom-commands))))
  10633. ;;;###autoload
  10634. (defun org-store-link (arg)
  10635. "\\<org-mode-map>Store an org-link to the current location.
  10636. This link can later be inserted into an org-buffer with
  10637. \\[org-insert-link].
  10638. For some link types, a prefix arg is interpreted:
  10639. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10640. For file links, arg negates `org-context-in-file-links'."
  10641. (interactive "P")
  10642. (setq org-store-link-plist nil) ; reset
  10643. (let (link cpltxt desc description search txt)
  10644. (cond
  10645. ((run-hook-with-args-until-success 'org-store-link-functions)
  10646. (setq link (plist-get org-store-link-plist :link)
  10647. desc (or (plist-get org-store-link-plist :description) link)))
  10648. ((eq major-mode 'bbdb-mode)
  10649. (let ((name (bbdb-record-name (bbdb-current-record)))
  10650. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10651. (setq cpltxt (concat "bbdb:" (or name company))
  10652. link (org-make-link cpltxt))
  10653. (org-store-link-props :type "bbdb" :name name :company company)))
  10654. ((eq major-mode 'Info-mode)
  10655. (setq link (org-make-link "info:"
  10656. (file-name-nondirectory Info-current-file)
  10657. ":" Info-current-node))
  10658. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10659. ":" Info-current-node))
  10660. (org-store-link-props :type "info" :file Info-current-file
  10661. :node Info-current-node))
  10662. ((eq major-mode 'calendar-mode)
  10663. (let ((cd (calendar-cursor-to-date)))
  10664. (setq link
  10665. (format-time-string
  10666. (car org-time-stamp-formats)
  10667. (apply 'encode-time
  10668. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10669. nil nil nil))))
  10670. (org-store-link-props :type "calendar" :date cd)))
  10671. ((or (eq major-mode 'vm-summary-mode)
  10672. (eq major-mode 'vm-presentation-mode))
  10673. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10674. (vm-follow-summary-cursor)
  10675. (save-excursion
  10676. (vm-select-folder-buffer)
  10677. (let* ((message (car vm-message-pointer))
  10678. (folder buffer-file-name)
  10679. (subject (vm-su-subject message))
  10680. (to (vm-get-header-contents message "To"))
  10681. (from (vm-get-header-contents message "From"))
  10682. (message-id (vm-su-message-id message)))
  10683. (org-store-link-props :type "vm" :from from :to to :subject subject
  10684. :message-id message-id)
  10685. (setq message-id (org-remove-angle-brackets message-id))
  10686. (setq folder (abbreviate-file-name folder))
  10687. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10688. folder)
  10689. (setq folder (replace-match "" t t folder)))
  10690. (setq cpltxt (org-email-link-description))
  10691. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10692. ((eq major-mode 'wl-summary-mode)
  10693. (let* ((msgnum (wl-summary-message-number))
  10694. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10695. msgnum 'message-id))
  10696. (wl-message-entity
  10697. (if (fboundp 'elmo-message-entity)
  10698. (elmo-message-entity
  10699. wl-summary-buffer-elmo-folder msgnum)
  10700. (elmo-msgdb-overview-get-entity
  10701. msgnum (wl-summary-buffer-msgdb))))
  10702. (from (wl-summary-line-from))
  10703. (to (elmo-message-entity-field wl-message-entity 'to))
  10704. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10705. (wl-summary-line-subject))))
  10706. (org-store-link-props :type "wl" :from from :to to
  10707. :subject subject :message-id message-id)
  10708. (setq message-id (org-remove-angle-brackets message-id))
  10709. (setq cpltxt (org-email-link-description))
  10710. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10711. "#" message-id))))
  10712. ((or (equal major-mode 'mh-folder-mode)
  10713. (equal major-mode 'mh-show-mode))
  10714. (let ((from (org-mhe-get-header "From:"))
  10715. (to (org-mhe-get-header "To:"))
  10716. (message-id (org-mhe-get-header "Message-Id:"))
  10717. (subject (org-mhe-get-header "Subject:")))
  10718. (org-store-link-props :type "mh" :from from :to to
  10719. :subject subject :message-id message-id)
  10720. (setq cpltxt (org-email-link-description))
  10721. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10722. (org-remove-angle-brackets message-id)))))
  10723. ((eq major-mode 'rmail-mode)
  10724. (save-excursion
  10725. (save-restriction
  10726. (rmail-narrow-to-non-pruned-header)
  10727. (let ((folder buffer-file-name)
  10728. (message-id (mail-fetch-field "message-id"))
  10729. (from (mail-fetch-field "from"))
  10730. (to (mail-fetch-field "to"))
  10731. (subject (mail-fetch-field "subject")))
  10732. (org-store-link-props
  10733. :type "rmail" :from from :to to
  10734. :subject subject :message-id message-id)
  10735. (setq message-id (org-remove-angle-brackets message-id))
  10736. (setq cpltxt (org-email-link-description))
  10737. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10738. ((eq major-mode 'gnus-group-mode)
  10739. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10740. (gnus-group-group-name)) ; version
  10741. ((fboundp 'gnus-group-name)
  10742. (gnus-group-name))
  10743. (t "???"))))
  10744. (unless group (error "Not on a group"))
  10745. (org-store-link-props :type "gnus" :group group)
  10746. (setq cpltxt (concat
  10747. (if (org-xor arg org-usenet-links-prefer-google)
  10748. "http://groups.google.com/groups?group="
  10749. "gnus:")
  10750. group)
  10751. link (org-make-link cpltxt))))
  10752. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10753. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10754. (let* ((group gnus-newsgroup-name)
  10755. (article (gnus-summary-article-number))
  10756. (header (gnus-summary-article-header article))
  10757. (from (mail-header-from header))
  10758. (message-id (mail-header-id header))
  10759. (date (mail-header-date header))
  10760. (subject (gnus-summary-subject-string)))
  10761. (org-store-link-props :type "gnus" :from from :subject subject
  10762. :message-id message-id :group group)
  10763. (setq cpltxt (org-email-link-description))
  10764. (if (org-xor arg org-usenet-links-prefer-google)
  10765. (setq link
  10766. (concat
  10767. cpltxt "\n "
  10768. (format "http://groups.google.com/groups?as_umsgid=%s"
  10769. (org-fixup-message-id-for-http message-id))))
  10770. (setq link (org-make-link "gnus:" group
  10771. "#" (number-to-string article))))))
  10772. ((eq major-mode 'w3-mode)
  10773. (setq cpltxt (url-view-url t)
  10774. link (org-make-link cpltxt))
  10775. (org-store-link-props :type "w3" :url (url-view-url t)))
  10776. ((eq major-mode 'w3m-mode)
  10777. (setq cpltxt (or w3m-current-title w3m-current-url)
  10778. link (org-make-link w3m-current-url))
  10779. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10780. ((setq search (run-hook-with-args-until-success
  10781. 'org-create-file-search-functions))
  10782. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10783. "::" search))
  10784. (setq cpltxt (or description link)))
  10785. ((eq major-mode 'image-mode)
  10786. (setq cpltxt (concat "file:"
  10787. (abbreviate-file-name buffer-file-name))
  10788. link (org-make-link cpltxt))
  10789. (org-store-link-props :type "image" :file buffer-file-name))
  10790. ((eq major-mode 'dired-mode)
  10791. ;; link to the file in the current line
  10792. (setq cpltxt (concat "file:"
  10793. (abbreviate-file-name
  10794. (expand-file-name
  10795. (dired-get-filename nil t))))
  10796. link (org-make-link cpltxt)))
  10797. ((and buffer-file-name (org-mode-p))
  10798. ;; Just link to current headline
  10799. (setq cpltxt (concat "file:"
  10800. (abbreviate-file-name buffer-file-name)))
  10801. ;; Add a context search string
  10802. (when (org-xor org-context-in-file-links arg)
  10803. ;; Check if we are on a target
  10804. (if (org-in-regexp "<<\\(.*?\\)>>")
  10805. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10806. (setq txt (cond
  10807. ((org-on-heading-p) nil)
  10808. ((org-region-active-p)
  10809. (buffer-substring (region-beginning) (region-end)))
  10810. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10811. (when (or (null txt) (string-match "\\S-" txt))
  10812. (setq cpltxt
  10813. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10814. desc "NONE"))))
  10815. (if (string-match "::\\'" cpltxt)
  10816. (setq cpltxt (substring cpltxt 0 -2)))
  10817. (setq link (org-make-link cpltxt)))
  10818. ((buffer-file-name (buffer-base-buffer))
  10819. ;; Just link to this file here.
  10820. (setq cpltxt (concat "file:"
  10821. (abbreviate-file-name
  10822. (buffer-file-name (buffer-base-buffer)))))
  10823. ;; Add a context string
  10824. (when (org-xor org-context-in-file-links arg)
  10825. (setq txt (if (org-region-active-p)
  10826. (buffer-substring (region-beginning) (region-end))
  10827. (buffer-substring (point-at-bol) (point-at-eol))))
  10828. ;; Only use search option if there is some text.
  10829. (when (string-match "\\S-" txt)
  10830. (setq cpltxt
  10831. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10832. desc "NONE")))
  10833. (setq link (org-make-link cpltxt)))
  10834. ((interactive-p)
  10835. (error "Cannot link to a buffer which is not visiting a file"))
  10836. (t (setq link nil)))
  10837. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10838. (setq link (or link cpltxt)
  10839. desc (or desc cpltxt))
  10840. (if (equal desc "NONE") (setq desc nil))
  10841. (if (and (interactive-p) link)
  10842. (progn
  10843. (setq org-stored-links
  10844. (cons (list link desc) org-stored-links))
  10845. (message "Stored: %s" (or desc link)))
  10846. (and link (org-make-link-string link desc)))))
  10847. (defun org-store-link-props (&rest plist)
  10848. "Store link properties, extract names and addresses."
  10849. (let (x adr)
  10850. (when (setq x (plist-get plist :from))
  10851. (setq adr (mail-extract-address-components x))
  10852. (plist-put plist :fromname (car adr))
  10853. (plist-put plist :fromaddress (nth 1 adr)))
  10854. (when (setq x (plist-get plist :to))
  10855. (setq adr (mail-extract-address-components x))
  10856. (plist-put plist :toname (car adr))
  10857. (plist-put plist :toaddress (nth 1 adr))))
  10858. (let ((from (plist-get plist :from))
  10859. (to (plist-get plist :to)))
  10860. (when (and from to org-from-is-user-regexp)
  10861. (plist-put plist :fromto
  10862. (if (string-match org-from-is-user-regexp from)
  10863. (concat "to %t")
  10864. (concat "from %f")))))
  10865. (setq org-store-link-plist plist))
  10866. (defun org-email-link-description (&optional fmt)
  10867. "Return the description part of an email link.
  10868. This takes information from `org-store-link-plist' and formats it
  10869. according to FMT (default from `org-email-link-description-format')."
  10870. (setq fmt (or fmt org-email-link-description-format))
  10871. (let* ((p org-store-link-plist)
  10872. (to (plist-get p :toaddress))
  10873. (from (plist-get p :fromaddress))
  10874. (table
  10875. (list
  10876. (cons "%c" (plist-get p :fromto))
  10877. (cons "%F" (plist-get p :from))
  10878. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  10879. (cons "%T" (plist-get p :to))
  10880. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  10881. (cons "%s" (plist-get p :subject))
  10882. (cons "%m" (plist-get p :message-id)))))
  10883. (when (string-match "%c" fmt)
  10884. ;; Check if the user wrote this message
  10885. (if (and org-from-is-user-regexp from to
  10886. (save-match-data (string-match org-from-is-user-regexp from)))
  10887. (setq fmt (replace-match "to %t" t t fmt))
  10888. (setq fmt (replace-match "from %f" t t fmt))))
  10889. (org-replace-escapes fmt table)))
  10890. (defun org-make-org-heading-search-string (&optional string heading)
  10891. "Make search string for STRING or current headline."
  10892. (interactive)
  10893. (let ((s (or string (org-get-heading))))
  10894. (unless (and string (not heading))
  10895. ;; We are using a headline, clean up garbage in there.
  10896. (if (string-match org-todo-regexp s)
  10897. (setq s (replace-match "" t t s)))
  10898. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  10899. (setq s (replace-match "" t t s)))
  10900. (setq s (org-trim s))
  10901. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10902. org-comment-string "\\)") s)
  10903. (setq s (replace-match "" t t s)))
  10904. (while (string-match org-ts-regexp s)
  10905. (setq s (replace-match "" t t s))))
  10906. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10907. (setq s (replace-match " " t t s)))
  10908. (or string (setq s (concat "*" s))) ; Add * for headlines
  10909. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10910. (defun org-make-link (&rest strings)
  10911. "Concatenate STRINGS."
  10912. (apply 'concat strings))
  10913. (defun org-make-link-string (link &optional description)
  10914. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10915. (unless (string-match "\\S-" link)
  10916. (error "Empty link"))
  10917. (when (stringp description)
  10918. ;; Remove brackets from the description, they are fatal.
  10919. (while (string-match "\\[\\|\\]" description)
  10920. (setq description (replace-match "" t t description))))
  10921. (when (equal (org-link-escape link) description)
  10922. ;; No description needed, it is identical
  10923. (setq description nil))
  10924. (when (and (not description)
  10925. (not (equal link (org-link-escape link))))
  10926. (setq description link))
  10927. (concat "[[" (org-link-escape link) "]"
  10928. (if description (concat "[" description "]") "")
  10929. "]"))
  10930. (defconst org-link-escape-chars
  10931. '((?\ . "%20")
  10932. (?\[ . "%5B")
  10933. (?\] . "%5d")
  10934. (?\340 . "%E0") ; `a
  10935. (?\342 . "%E2") ; ^a
  10936. (?\347 . "%E7") ; ,c
  10937. (?\350 . "%E8") ; `e
  10938. (?\351 . "%E9") ; 'e
  10939. (?\352 . "%EA") ; ^e
  10940. (?\356 . "%EE") ; ^i
  10941. (?\364 . "%F4") ; ^o
  10942. (?\371 . "%F9") ; `u
  10943. (?\373 . "%FB") ; ^u
  10944. (?\; . "%3B")
  10945. (?? . "%3F")
  10946. (?= . "%3D")
  10947. (?+ . "%2B")
  10948. )
  10949. "Association list of escapes for some characters problematic in links.
  10950. This is the list that is used for internal purposes.")
  10951. (defconst org-link-escape-chars-browser
  10952. '((?\ . "%20")) ; 32 for the SPC char
  10953. "Association list of escapes for some characters problematic in links.
  10954. This is the list that is used before handing over to the browser.")
  10955. (defun org-link-escape (text &optional table)
  10956. "Escape charaters in TEXT that are problematic for links."
  10957. (setq table (or table org-link-escape-chars))
  10958. (when text
  10959. (let ((re (mapconcat (lambda (x) (regexp-quote
  10960. (char-to-string (car x))))
  10961. table "\\|")))
  10962. (while (string-match re text)
  10963. (setq text
  10964. (replace-match
  10965. (cdr (assoc (string-to-char (match-string 0 text))
  10966. table))
  10967. t t text)))
  10968. text)))
  10969. (defun org-link-unescape (text &optional table)
  10970. "Reverse the action of `org-link-escape'."
  10971. (setq table (or table org-link-escape-chars))
  10972. (when text
  10973. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  10974. table "\\|")))
  10975. (while (string-match re text)
  10976. (setq text
  10977. (replace-match
  10978. (char-to-string (car (rassoc (match-string 0 text) table)))
  10979. t t text)))
  10980. text)))
  10981. (defun org-xor (a b)
  10982. "Exclusive or."
  10983. (if a (not b) b))
  10984. (defun org-get-header (header)
  10985. "Find a header field in the current buffer."
  10986. (save-excursion
  10987. (goto-char (point-min))
  10988. (let ((case-fold-search t) s)
  10989. (cond
  10990. ((eq header 'from)
  10991. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  10992. (setq s (match-string 1)))
  10993. (while (string-match "\"" s)
  10994. (setq s (replace-match "" t t s)))
  10995. (if (string-match "[<(].*" s)
  10996. (setq s (replace-match "" t t s))))
  10997. ((eq header 'message-id)
  10998. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  10999. (setq s (match-string 1))))
  11000. ((eq header 'subject)
  11001. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11002. (setq s (match-string 1)))))
  11003. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11004. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11005. s)))
  11006. (defun org-fixup-message-id-for-http (s)
  11007. "Replace special characters in a message id, so it can be used in an http query."
  11008. (while (string-match "<" s)
  11009. (setq s (replace-match "%3C" t t s)))
  11010. (while (string-match ">" s)
  11011. (setq s (replace-match "%3E" t t s)))
  11012. (while (string-match "@" s)
  11013. (setq s (replace-match "%40" t t s)))
  11014. s)
  11015. ;;;###autoload
  11016. (defun org-insert-link-global ()
  11017. "Insert a link like Org-mode does.
  11018. This command can be called in any mode to insert a link in Org-mode syntax."
  11019. (interactive)
  11020. (org-run-like-in-org-mode 'org-insert-link))
  11021. (defun org-insert-link (&optional complete-file)
  11022. "Insert a link. At the prompt, enter the link.
  11023. Completion can be used to select a link previously stored with
  11024. `org-store-link'. When the empty string is entered (i.e. if you just
  11025. press RET at the prompt), the link defaults to the most recently
  11026. stored link. As SPC triggers completion in the minibuffer, you need to
  11027. use M-SPC or C-q SPC to force the insertion of a space character.
  11028. You will also be prompted for a description, and if one is given, it will
  11029. be displayed in the buffer instead of the link.
  11030. If there is already a link at point, this command will allow you to edit link
  11031. and description parts.
  11032. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11033. selected using completion. The path to the file will be relative to
  11034. the current directory if the file is in the current directory or a
  11035. subdirectory. Otherwise, the link will be the absolute path as
  11036. completed in the minibuffer (i.e. normally ~/path/to/file).
  11037. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11038. is in the current directory or below.
  11039. With three \\[universal-argument] prefixes, negate the meaning of
  11040. `org-keep-stored-link-after-insertion'."
  11041. (interactive "P")
  11042. (let* ((wcf (current-window-configuration))
  11043. (region (if (org-region-active-p)
  11044. (buffer-substring (region-beginning) (region-end))))
  11045. (remove (and region (list (region-beginning) (region-end))))
  11046. (desc region)
  11047. tmphist ; byte-compile incorrectly complains about this
  11048. link entry file)
  11049. (cond
  11050. ((org-in-regexp org-bracket-link-regexp 1)
  11051. ;; We do have a link at point, and we are going to edit it.
  11052. (setq remove (list (match-beginning 0) (match-end 0)))
  11053. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11054. (setq link (read-string "Link: "
  11055. (org-link-unescape
  11056. (org-match-string-no-properties 1)))))
  11057. ((or (org-in-regexp org-angle-link-re)
  11058. (org-in-regexp org-plain-link-re))
  11059. ;; Convert to bracket link
  11060. (setq remove (list (match-beginning 0) (match-end 0))
  11061. link (read-string "Link: "
  11062. (org-remove-angle-brackets (match-string 0)))))
  11063. ((equal complete-file '(4))
  11064. ;; Completing read for file names.
  11065. (setq file (read-file-name "File: "))
  11066. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11067. (pwd1 (file-name-as-directory (abbreviate-file-name
  11068. (expand-file-name ".")))))
  11069. (cond
  11070. ((equal complete-file '(16))
  11071. (setq link (org-make-link
  11072. "file:"
  11073. (abbreviate-file-name (expand-file-name file)))))
  11074. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11075. (setq link (org-make-link "file:" (match-string 1 file))))
  11076. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11077. (expand-file-name file))
  11078. (setq link (org-make-link
  11079. "file:" (match-string 1 (expand-file-name file)))))
  11080. (t (setq link (org-make-link "file:" file))))))
  11081. (t
  11082. ;; Read link, with completion for stored links.
  11083. (with-output-to-temp-buffer "*Org Links*"
  11084. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11085. (when org-stored-links
  11086. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11087. (princ (mapconcat
  11088. (lambda (x)
  11089. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11090. (reverse org-stored-links) "\n"))))
  11091. (let ((cw (selected-window)))
  11092. (select-window (get-buffer-window "*Org Links*"))
  11093. (shrink-window-if-larger-than-buffer)
  11094. (setq truncate-lines t)
  11095. (select-window cw))
  11096. ;; Fake a link history, containing the stored links.
  11097. (setq tmphist (append (mapcar 'car org-stored-links)
  11098. org-insert-link-history))
  11099. (unwind-protect
  11100. (setq link (org-completing-read
  11101. "Link: "
  11102. (append
  11103. (mapcar (lambda (x) (list (concat (car x) ":")))
  11104. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11105. (mapcar (lambda (x) (list (concat x ":")))
  11106. org-link-types))
  11107. nil nil nil
  11108. 'tmphist
  11109. (or (car (car org-stored-links)))))
  11110. (set-window-configuration wcf)
  11111. (kill-buffer "*Org Links*"))
  11112. (setq entry (assoc link org-stored-links))
  11113. (or entry (push link org-insert-link-history))
  11114. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11115. (not org-keep-stored-link-after-insertion))
  11116. (setq org-stored-links (delq (assoc link org-stored-links)
  11117. org-stored-links)))
  11118. (setq desc (or desc (nth 1 entry)))))
  11119. (if (string-match org-plain-link-re link)
  11120. ;; URL-like link, normalize the use of angular brackets.
  11121. (setq link (org-make-link (org-remove-angle-brackets link))))
  11122. ;; Check if we are linking to the current file with a search option
  11123. ;; If yes, simplify the link by using only the search option.
  11124. (when (and buffer-file-name
  11125. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11126. (let* ((path (match-string 1 link))
  11127. (case-fold-search nil)
  11128. (search (match-string 2 link)))
  11129. (save-match-data
  11130. (if (equal (file-truename buffer-file-name) (file-truename path))
  11131. ;; We are linking to this same file, with a search option
  11132. (setq link search)))))
  11133. ;; Check if we can/should use a relative path. If yes, simplify the link
  11134. (when (string-match "\\<file:\\(.*\\)" link)
  11135. (let* ((path (match-string 1 link))
  11136. (origpath path)
  11137. (desc-is-link (equal link desc))
  11138. (case-fold-search nil))
  11139. (cond
  11140. ((eq org-link-file-path-type 'absolute)
  11141. (setq path (abbreviate-file-name (expand-file-name path))))
  11142. ((eq org-link-file-path-type 'noabbrev)
  11143. (setq path (expand-file-name path)))
  11144. ((eq org-link-file-path-type 'relative)
  11145. (setq path (file-relative-name path)))
  11146. (t
  11147. (save-match-data
  11148. (if (string-match (concat "^" (regexp-quote
  11149. (file-name-as-directory
  11150. (expand-file-name "."))))
  11151. (expand-file-name path))
  11152. ;; We are linking a file with relative path name.
  11153. (setq path (substring (expand-file-name path)
  11154. (match-end 0)))))))
  11155. (setq link (concat "file:" path))
  11156. (if (equal desc origpath)
  11157. (setq desc path))))
  11158. (setq desc (read-string "Description: " desc))
  11159. (unless (string-match "\\S-" desc) (setq desc nil))
  11160. (if remove (apply 'delete-region remove))
  11161. (insert (org-make-link-string link desc))))
  11162. (defun org-completing-read (&rest args)
  11163. (let ((minibuffer-local-completion-map
  11164. (copy-keymap minibuffer-local-completion-map)))
  11165. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11166. (apply 'completing-read args)))
  11167. ;;; Opening/following a link
  11168. (defvar org-link-search-failed nil)
  11169. (defun org-next-link ()
  11170. "Move forward to the next link.
  11171. If the link is in hidden text, expose it."
  11172. (interactive)
  11173. (when (and org-link-search-failed (eq this-command last-command))
  11174. (goto-char (point-min))
  11175. (message "Link search wrapped back to beginning of buffer"))
  11176. (setq org-link-search-failed nil)
  11177. (let* ((pos (point))
  11178. (ct (org-context))
  11179. (a (assoc :link ct)))
  11180. (if a (goto-char (nth 2 a)))
  11181. (if (re-search-forward org-any-link-re nil t)
  11182. (progn
  11183. (goto-char (match-beginning 0))
  11184. (if (org-invisible-p) (org-show-context)))
  11185. (goto-char pos)
  11186. (setq org-link-search-failed t)
  11187. (error "No further link found"))))
  11188. (defun org-previous-link ()
  11189. "Move backward to the previous link.
  11190. If the link is in hidden text, expose it."
  11191. (interactive)
  11192. (when (and org-link-search-failed (eq this-command last-command))
  11193. (goto-char (point-max))
  11194. (message "Link search wrapped back to end of buffer"))
  11195. (setq org-link-search-failed nil)
  11196. (let* ((pos (point))
  11197. (ct (org-context))
  11198. (a (assoc :link ct)))
  11199. (if a (goto-char (nth 1 a)))
  11200. (if (re-search-backward org-any-link-re nil t)
  11201. (progn
  11202. (goto-char (match-beginning 0))
  11203. (if (org-invisible-p) (org-show-context)))
  11204. (goto-char pos)
  11205. (setq org-link-search-failed t)
  11206. (error "No further link found"))))
  11207. (defun org-find-file-at-mouse (ev)
  11208. "Open file link or URL at mouse."
  11209. (interactive "e")
  11210. (mouse-set-point ev)
  11211. (org-open-at-point 'in-emacs))
  11212. (defun org-open-at-mouse (ev)
  11213. "Open file link or URL at mouse."
  11214. (interactive "e")
  11215. (mouse-set-point ev)
  11216. (org-open-at-point))
  11217. (defvar org-window-config-before-follow-link nil
  11218. "The window configuration before following a link.
  11219. This is saved in case the need arises to restore it.")
  11220. (defvar org-open-link-marker (make-marker)
  11221. "Marker pointing to the location where `org-open-at-point; was called.")
  11222. ;;;###autoload
  11223. (defun org-open-at-point-global ()
  11224. "Follow a link like Org-mode does.
  11225. This command can be called in any mode to follow a link that has
  11226. Org-mode syntax."
  11227. (interactive)
  11228. (org-run-like-in-org-mode 'org-open-at-point))
  11229. (defun org-open-at-point (&optional in-emacs)
  11230. "Open link at or after point.
  11231. If there is no link at point, this function will search forward up to
  11232. the end of the current subtree.
  11233. Normally, files will be opened by an appropriate application. If the
  11234. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11235. (interactive "P")
  11236. (catch 'abort
  11237. (move-marker org-open-link-marker (point))
  11238. (setq org-window-config-before-follow-link (current-window-configuration))
  11239. (org-remove-occur-highlights nil nil t)
  11240. (if (org-at-timestamp-p t)
  11241. (org-follow-timestamp-link)
  11242. (let (type path link line search (pos (point)))
  11243. (catch 'match
  11244. (save-excursion
  11245. (skip-chars-forward "^]\n\r")
  11246. (when (org-in-regexp org-bracket-link-regexp)
  11247. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11248. (while (string-match " *\n *" link)
  11249. (setq link (replace-match " " t t link)))
  11250. (setq link (org-link-expand-abbrev link))
  11251. (if (string-match org-link-re-with-space2 link)
  11252. (setq type (match-string 1 link) path (match-string 2 link))
  11253. (setq type "thisfile" path link))
  11254. (throw 'match t)))
  11255. (when (get-text-property (point) 'org-linked-text)
  11256. (setq type "thisfile"
  11257. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11258. (1+ (point)) (point))
  11259. path (buffer-substring
  11260. (previous-single-property-change pos 'org-linked-text)
  11261. (next-single-property-change pos 'org-linked-text)))
  11262. (throw 'match t))
  11263. (save-excursion
  11264. (when (or (org-in-regexp org-angle-link-re)
  11265. (org-in-regexp org-plain-link-re))
  11266. (setq type (match-string 1) path (match-string 2))
  11267. (throw 'match t)))
  11268. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11269. (setq type "tree-match"
  11270. path (match-string 1))
  11271. (throw 'match t))
  11272. (save-excursion
  11273. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11274. (setq type "tags"
  11275. path (match-string 1))
  11276. (while (string-match ":" path)
  11277. (setq path (replace-match "+" t t path)))
  11278. (throw 'match t))))
  11279. (unless path
  11280. (error "No link found"))
  11281. ;; Remove any trailing spaces in path
  11282. (if (string-match " +\\'" path)
  11283. (setq path (replace-match "" t t path)))
  11284. (cond
  11285. ((assoc type org-link-protocols)
  11286. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11287. ((equal type "mailto")
  11288. (let ((cmd (car org-link-mailto-program))
  11289. (args (cdr org-link-mailto-program)) args1
  11290. (address path) (subject "") a)
  11291. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11292. (setq address (match-string 1 path)
  11293. subject (org-link-escape (match-string 2 path))))
  11294. (while args
  11295. (cond
  11296. ((not (stringp (car args))) (push (pop args) args1))
  11297. (t (setq a (pop args))
  11298. (if (string-match "%a" a)
  11299. (setq a (replace-match address t t a)))
  11300. (if (string-match "%s" a)
  11301. (setq a (replace-match subject t t a)))
  11302. (push a args1))))
  11303. (apply cmd (nreverse args1))))
  11304. ((member type '("http" "https" "ftp" "news"))
  11305. (browse-url (concat type ":" (org-link-escape
  11306. path org-link-escape-chars-browser))))
  11307. ((string= type "tags")
  11308. (org-tags-view in-emacs path))
  11309. ((string= type "thisfile")
  11310. (if in-emacs
  11311. (switch-to-buffer-other-window
  11312. (org-get-buffer-for-internal-link (current-buffer)))
  11313. (org-mark-ring-push))
  11314. (let ((cmd `(org-link-search
  11315. ,path
  11316. ,(cond ((equal in-emacs '(4)) 'occur)
  11317. ((equal in-emacs '(16)) 'org-occur)
  11318. (t nil))
  11319. ,pos)))
  11320. (condition-case nil (eval cmd)
  11321. (error (progn (widen) (eval cmd))))))
  11322. ((string= type "tree-match")
  11323. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11324. ((string= type "file")
  11325. (if (string-match "::\\([0-9]+\\)\\'" path)
  11326. (setq line (string-to-number (match-string 1 path))
  11327. path (substring path 0 (match-beginning 0)))
  11328. (if (string-match "::\\(.+\\)\\'" path)
  11329. (setq search (match-string 1 path)
  11330. path (substring path 0 (match-beginning 0)))))
  11331. (if (string-match "[*?{]" (file-name-nondirectory path))
  11332. (dired path)
  11333. (org-open-file path in-emacs line search)))
  11334. ((string= type "news")
  11335. (org-follow-gnus-link path))
  11336. ((string= type "bbdb")
  11337. (org-follow-bbdb-link path))
  11338. ((string= type "info")
  11339. (org-follow-info-link path))
  11340. ((string= type "gnus")
  11341. (let (group article)
  11342. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11343. (error "Error in Gnus link"))
  11344. (setq group (match-string 1 path)
  11345. article (match-string 3 path))
  11346. (org-follow-gnus-link group article)))
  11347. ((string= type "vm")
  11348. (let (folder article)
  11349. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11350. (error "Error in VM link"))
  11351. (setq folder (match-string 1 path)
  11352. article (match-string 3 path))
  11353. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11354. (org-follow-vm-link folder article in-emacs)))
  11355. ((string= type "wl")
  11356. (let (folder article)
  11357. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11358. (error "Error in Wanderlust link"))
  11359. (setq folder (match-string 1 path)
  11360. article (match-string 3 path))
  11361. (org-follow-wl-link folder article)))
  11362. ((string= type "mhe")
  11363. (let (folder article)
  11364. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11365. (error "Error in MHE link"))
  11366. (setq folder (match-string 1 path)
  11367. article (match-string 3 path))
  11368. (org-follow-mhe-link folder article)))
  11369. ((string= type "rmail")
  11370. (let (folder article)
  11371. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11372. (error "Error in RMAIL link"))
  11373. (setq folder (match-string 1 path)
  11374. article (match-string 3 path))
  11375. (org-follow-rmail-link folder article)))
  11376. ((string= type "shell")
  11377. (let ((cmd path))
  11378. (if (or (not org-confirm-shell-link-function)
  11379. (funcall org-confirm-shell-link-function
  11380. (format "Execute \"%s\" in shell? "
  11381. (org-add-props cmd nil
  11382. 'face 'org-warning))))
  11383. (progn
  11384. (message "Executing %s" cmd)
  11385. (shell-command cmd))
  11386. (error "Abort"))))
  11387. ((string= type "elisp")
  11388. (let ((cmd path))
  11389. (if (or (not org-confirm-elisp-link-function)
  11390. (funcall org-confirm-elisp-link-function
  11391. (format "Execute \"%s\" as elisp? "
  11392. (org-add-props cmd nil
  11393. 'face 'org-warning))))
  11394. (message "%s => %s" cmd (eval (read cmd)))
  11395. (error "Abort"))))
  11396. (t
  11397. (browse-url-at-point)))))
  11398. (move-marker org-open-link-marker nil)))
  11399. ;;; File search
  11400. (defvar org-create-file-search-functions nil
  11401. "List of functions to construct the right search string for a file link.
  11402. These functions are called in turn with point at the location to
  11403. which the link should point.
  11404. A function in the hook should first test if it would like to
  11405. handle this file type, for example by checking the major-mode or
  11406. the file extension. If it decides not to handle this file, it
  11407. should just return nil to give other functions a chance. If it
  11408. does handle the file, it must return the search string to be used
  11409. when following the link. The search string will be part of the
  11410. file link, given after a double colon, and `org-open-at-point'
  11411. will automatically search for it. If special measures must be
  11412. taken to make the search successful, another function should be
  11413. added to the companion hook `org-execute-file-search-functions',
  11414. which see.
  11415. A function in this hook may also use `setq' to set the variable
  11416. `description' to provide a suggestion for the descriptive text to
  11417. be used for this link when it gets inserted into an Org-mode
  11418. buffer with \\[org-insert-link].")
  11419. (defvar org-execute-file-search-functions nil
  11420. "List of functions to execute a file search triggered by a link.
  11421. Functions added to this hook must accept a single argument, the
  11422. search string that was part of the file link, the part after the
  11423. double colon. The function must first check if it would like to
  11424. handle this search, for example by checking the major-mode or the
  11425. file extension. If it decides not to handle this search, it
  11426. should just return nil to give other functions a chance. If it
  11427. does handle the search, it must return a non-nil value to keep
  11428. other functions from trying.
  11429. Each function can access the current prefix argument through the
  11430. variable `current-prefix-argument'. Note that a single prefix is
  11431. used to force opening a link in Emacs, so it may be good to only
  11432. use a numeric or double prefix to guide the search function.
  11433. In case this is needed, a function in this hook can also restore
  11434. the window configuration before `org-open-at-point' was called using:
  11435. (set-window-configuration org-window-config-before-follow-link)")
  11436. (defun org-link-search (s &optional type avoid-pos)
  11437. "Search for a link search option.
  11438. If S is surrounded by forward slashes, it is interpreted as a
  11439. regular expression. In org-mode files, this will create an `org-occur'
  11440. sparse tree. In ordinary files, `occur' will be used to list matches.
  11441. If the current buffer is in `dired-mode', grep will be used to search
  11442. in all files. If AVOID-POS is given, ignore matches near that position."
  11443. (let ((case-fold-search t)
  11444. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11445. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11446. (append '(("") (" ") ("\t") ("\n"))
  11447. org-emphasis-alist)
  11448. "\\|") "\\)"))
  11449. (pos (point))
  11450. (pre "") (post "")
  11451. words re0 re1 re2 re3 re4 re5 re2a reall)
  11452. (cond
  11453. ;; First check if there are any special
  11454. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11455. ;; Now try the builtin stuff
  11456. ((save-excursion
  11457. (goto-char (point-min))
  11458. (and
  11459. (re-search-forward
  11460. (concat "<<" (regexp-quote s0) ">>") nil t)
  11461. (setq pos (match-beginning 0))))
  11462. ;; There is an exact target for this
  11463. (goto-char pos))
  11464. ((string-match "^/\\(.*\\)/$" s)
  11465. ;; A regular expression
  11466. (cond
  11467. ((org-mode-p)
  11468. (org-occur (match-string 1 s)))
  11469. ;;((eq major-mode 'dired-mode)
  11470. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11471. (t (org-do-occur (match-string 1 s)))))
  11472. (t
  11473. ;; A normal search strings
  11474. (when (equal (string-to-char s) ?*)
  11475. ;; Anchor on headlines, post may include tags.
  11476. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11477. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11478. s (substring s 1)))
  11479. (remove-text-properties
  11480. 0 (length s)
  11481. '(face nil mouse-face nil keymap nil fontified nil) s)
  11482. ;; Make a series of regular expressions to find a match
  11483. (setq words (org-split-string s "[ \n\r\t]+")
  11484. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11485. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11486. "\\)" markers)
  11487. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11488. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11489. re1 (concat pre re2 post)
  11490. re3 (concat pre re4 post)
  11491. re5 (concat pre ".*" re4)
  11492. re2 (concat pre re2)
  11493. re2a (concat pre re2a)
  11494. re4 (concat pre re4)
  11495. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11496. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11497. re5 "\\)"
  11498. ))
  11499. (cond
  11500. ((eq type 'org-occur) (org-occur reall))
  11501. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11502. (t (goto-char (point-min))
  11503. (if (or (org-search-not-self 1 re0 nil t)
  11504. (org-search-not-self 1 re1 nil t)
  11505. (org-search-not-self 1 re2 nil t)
  11506. (org-search-not-self 1 re2a nil t)
  11507. (org-search-not-self 1 re3 nil t)
  11508. (org-search-not-self 1 re4 nil t)
  11509. (org-search-not-self 1 re5 nil t)
  11510. )
  11511. (goto-char (match-beginning 1))
  11512. (goto-char pos)
  11513. (error "No match")))))
  11514. (t
  11515. ;; Normal string-search
  11516. (goto-char (point-min))
  11517. (if (search-forward s nil t)
  11518. (goto-char (match-beginning 0))
  11519. (error "No match"))))
  11520. (and (org-mode-p) (org-show-context 'link-search))))
  11521. (defun org-search-not-self (group &rest args)
  11522. "Execute `re-search-forward', but only accept matches that do not
  11523. enclose the position of `org-open-link-marker'."
  11524. (let ((m org-open-link-marker))
  11525. (catch 'exit
  11526. (while (apply 're-search-forward args)
  11527. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11528. (goto-char (match-end group))
  11529. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11530. (> (match-beginning 0) (marker-position m))
  11531. (< (match-end 0) (marker-position m)))
  11532. (save-match-data
  11533. (or (not (org-in-regexp
  11534. org-bracket-link-analytic-regexp 1))
  11535. (not (match-end 4)) ; no description
  11536. (and (<= (match-beginning 4) (point))
  11537. (>= (match-end 4) (point))))))
  11538. (throw 'exit (point))))))))
  11539. (defun org-get-buffer-for-internal-link (buffer)
  11540. "Return a buffer to be used for displaying the link target of internal links."
  11541. (cond
  11542. ((not org-display-internal-link-with-indirect-buffer)
  11543. buffer)
  11544. ((string-match "(Clone)$" (buffer-name buffer))
  11545. (message "Buffer is already a clone, not making another one")
  11546. ;; we also do not modify visibility in this case
  11547. buffer)
  11548. (t ; make a new indirect buffer for displaying the link
  11549. (let* ((bn (buffer-name buffer))
  11550. (ibn (concat bn "(Clone)"))
  11551. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11552. (with-current-buffer ib (org-overview))
  11553. ib))))
  11554. (defun org-do-occur (regexp &optional cleanup)
  11555. "Call the Emacs command `occur'.
  11556. If CLEANUP is non-nil, remove the printout of the regular expression
  11557. in the *Occur* buffer. This is useful if the regex is long and not useful
  11558. to read."
  11559. (occur regexp)
  11560. (when cleanup
  11561. (let ((cwin (selected-window)) win beg end)
  11562. (when (setq win (get-buffer-window "*Occur*"))
  11563. (select-window win))
  11564. (goto-char (point-min))
  11565. (when (re-search-forward "match[a-z]+" nil t)
  11566. (setq beg (match-end 0))
  11567. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11568. (setq end (1- (match-beginning 0)))))
  11569. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11570. (goto-char (point-min))
  11571. (select-window cwin))))
  11572. ;;; The mark ring for links jumps
  11573. (defvar org-mark-ring nil
  11574. "Mark ring for positions before jumps in Org-mode.")
  11575. (defvar org-mark-ring-last-goto nil
  11576. "Last position in the mark ring used to go back.")
  11577. ;; Fill and close the ring
  11578. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11579. (loop for i from 1 to org-mark-ring-length do
  11580. (push (make-marker) org-mark-ring))
  11581. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11582. org-mark-ring)
  11583. (defun org-mark-ring-push (&optional pos buffer)
  11584. "Put the current position or POS into the mark ring and rotate it."
  11585. (interactive)
  11586. (setq pos (or pos (point)))
  11587. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11588. (move-marker (car org-mark-ring)
  11589. (or pos (point))
  11590. (or buffer (current-buffer)))
  11591. (message "%s"
  11592. (substitute-command-keys
  11593. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11594. (defun org-mark-ring-goto (&optional n)
  11595. "Jump to the previous position in the mark ring.
  11596. With prefix arg N, jump back that many stored positions. When
  11597. called several times in succession, walk through the entire ring.
  11598. Org-mode commands jumping to a different position in the current file,
  11599. or to another Org-mode file, automatically push the old position
  11600. onto the ring."
  11601. (interactive "p")
  11602. (let (p m)
  11603. (if (eq last-command this-command)
  11604. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11605. (setq p org-mark-ring))
  11606. (setq org-mark-ring-last-goto p)
  11607. (setq m (car p))
  11608. (switch-to-buffer (marker-buffer m))
  11609. (goto-char m)
  11610. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11611. (defun org-remove-angle-brackets (s)
  11612. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11613. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11614. s)
  11615. (defun org-add-angle-brackets (s)
  11616. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11617. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11618. s)
  11619. ;;; Following specific links
  11620. (defun org-follow-timestamp-link ()
  11621. (cond
  11622. ((org-at-date-range-p t)
  11623. (let ((org-agenda-start-on-weekday)
  11624. (t1 (match-string 1))
  11625. (t2 (match-string 2)))
  11626. (setq t1 (time-to-days (org-time-string-to-time t1))
  11627. t2 (time-to-days (org-time-string-to-time t2)))
  11628. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11629. ((org-at-timestamp-p t)
  11630. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11631. (substring (match-string 1) 0 10)))
  11632. 1))
  11633. (t (error "This should not happen"))))
  11634. (defun org-follow-bbdb-link (name)
  11635. "Follow a BBDB link to NAME."
  11636. (require 'bbdb)
  11637. (let ((inhibit-redisplay (not debug-on-error))
  11638. (bbdb-electric-p nil))
  11639. (catch 'exit
  11640. ;; Exact match on name
  11641. (bbdb-name (concat "\\`" name "\\'") nil)
  11642. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11643. ;; Exact match on name
  11644. (bbdb-company (concat "\\`" name "\\'") nil)
  11645. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11646. ;; Partial match on name
  11647. (bbdb-name name nil)
  11648. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11649. ;; Partial match on company
  11650. (bbdb-company name nil)
  11651. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11652. ;; General match including network address and notes
  11653. (bbdb name nil)
  11654. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11655. (delete-window (get-buffer-window "*BBDB*"))
  11656. (error "No matching BBDB record")))))
  11657. (defun org-follow-info-link (name)
  11658. "Follow an info file & node link to NAME."
  11659. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11660. (string-match "\\(.*\\)" name))
  11661. (progn
  11662. (require 'info)
  11663. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11664. (Info-find-node (match-string 1 name) (match-string 2 name))
  11665. (Info-find-node (match-string 1 name) "Top")))
  11666. (message "Could not open: %s" name)))
  11667. (defun org-follow-gnus-link (&optional group article)
  11668. "Follow a Gnus link to GROUP and ARTICLE."
  11669. (require 'gnus)
  11670. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11671. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11672. (cond ((and group article)
  11673. (gnus-group-read-group 1 nil group)
  11674. (gnus-summary-goto-article (string-to-number article) nil t))
  11675. (group (gnus-group-jump-to-group group))))
  11676. (defun org-follow-vm-link (&optional folder article readonly)
  11677. "Follow a VM link to FOLDER and ARTICLE."
  11678. (require 'vm)
  11679. (setq article (org-add-angle-brackets article))
  11680. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11681. ;; ange-ftp or efs or tramp access
  11682. (let ((user (or (match-string 1 folder) (user-login-name)))
  11683. (host (match-string 2 folder))
  11684. (file (match-string 3 folder)))
  11685. (cond
  11686. ((featurep 'tramp)
  11687. ;; use tramp to access the file
  11688. (if (featurep 'xemacs)
  11689. (setq folder (format "[%s@%s]%s" user host file))
  11690. (setq folder (format "/%s@%s:%s" user host file))))
  11691. (t
  11692. ;; use ange-ftp or efs
  11693. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11694. (setq folder (format "/%s@%s:%s" user host file))))))
  11695. (when folder
  11696. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11697. (sit-for 0.1)
  11698. (when article
  11699. (vm-select-folder-buffer)
  11700. (widen)
  11701. (let ((case-fold-search t))
  11702. (goto-char (point-min))
  11703. (if (not (re-search-forward
  11704. (concat "^" "message-id: *" (regexp-quote article))))
  11705. (error "Could not find the specified message in this folder"))
  11706. (vm-isearch-update)
  11707. (vm-isearch-narrow)
  11708. (vm-beginning-of-message)
  11709. (vm-summarize)))))
  11710. (defun org-follow-wl-link (folder article)
  11711. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11712. (if (and (string= folder "%")
  11713. article
  11714. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11715. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11716. ;; Thus, we recompose folder and article ids.
  11717. (setq folder (format "%s#%s" folder (match-string 1 article))
  11718. article (match-string 3 article)))
  11719. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11720. (error "No such folder: %s" folder))
  11721. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11722. (and article
  11723. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11724. (wl-summary-redisplay)))
  11725. (defun org-follow-rmail-link (folder article)
  11726. "Follow an RMAIL link to FOLDER and ARTICLE."
  11727. (setq article (org-add-angle-brackets article))
  11728. (let (message-number)
  11729. (save-excursion
  11730. (save-window-excursion
  11731. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11732. (setq message-number
  11733. (save-restriction
  11734. (widen)
  11735. (goto-char (point-max))
  11736. (if (re-search-backward
  11737. (concat "^Message-ID:\\s-+" (regexp-quote
  11738. (or article "")))
  11739. nil t)
  11740. (rmail-what-message))))))
  11741. (if message-number
  11742. (progn
  11743. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11744. (rmail-show-message message-number)
  11745. message-number)
  11746. (error "Message not found"))))
  11747. ;;; mh-e integration based on planner-mode
  11748. (defun org-mhe-get-message-real-folder ()
  11749. "Return the name of the current message real folder, so if you use
  11750. sequences, it will now work."
  11751. (save-excursion
  11752. (let* ((folder
  11753. (if (equal major-mode 'mh-folder-mode)
  11754. mh-current-folder
  11755. ;; Refer to the show buffer
  11756. mh-show-folder-buffer))
  11757. (end-index
  11758. (if (boundp 'mh-index-folder)
  11759. (min (length mh-index-folder) (length folder))))
  11760. )
  11761. ;; a simple test on mh-index-data does not work, because
  11762. ;; mh-index-data is always nil in a show buffer.
  11763. (if (and (boundp 'mh-index-folder)
  11764. (string= mh-index-folder (substring folder 0 end-index)))
  11765. (if (equal major-mode 'mh-show-mode)
  11766. (save-window-excursion
  11767. (let (pop-up-frames)
  11768. (when (buffer-live-p (get-buffer folder))
  11769. (progn
  11770. (pop-to-buffer folder)
  11771. (org-mhe-get-message-folder-from-index)
  11772. )
  11773. )))
  11774. (org-mhe-get-message-folder-from-index)
  11775. )
  11776. folder
  11777. )
  11778. )))
  11779. (defun org-mhe-get-message-folder-from-index ()
  11780. "Returns the name of the message folder in a index folder buffer."
  11781. (save-excursion
  11782. (mh-index-previous-folder)
  11783. (re-search-forward "^\\(+.*\\)$" nil t)
  11784. (message "%s" (match-string 1))))
  11785. (defun org-mhe-get-message-folder ()
  11786. "Return the name of the current message folder. Be careful if you
  11787. use sequences."
  11788. (save-excursion
  11789. (if (equal major-mode 'mh-folder-mode)
  11790. mh-current-folder
  11791. ;; Refer to the show buffer
  11792. mh-show-folder-buffer)))
  11793. (defun org-mhe-get-message-num ()
  11794. "Return the number of the current message. Be careful if you
  11795. use sequences."
  11796. (save-excursion
  11797. (if (equal major-mode 'mh-folder-mode)
  11798. (mh-get-msg-num nil)
  11799. ;; Refer to the show buffer
  11800. (mh-show-buffer-message-number))))
  11801. (defun org-mhe-get-header (header)
  11802. "Return a header of the message in folder mode. This will create a
  11803. show buffer for the corresponding message. If you have a more clever
  11804. idea..."
  11805. (let* ((folder (org-mhe-get-message-folder))
  11806. (num (org-mhe-get-message-num))
  11807. (buffer (get-buffer-create (concat "show-" folder)))
  11808. (header-field))
  11809. (with-current-buffer buffer
  11810. (mh-display-msg num folder)
  11811. (if (equal major-mode 'mh-folder-mode)
  11812. (mh-header-display)
  11813. (mh-show-header-display))
  11814. (set-buffer buffer)
  11815. (setq header-field (mh-get-header-field header))
  11816. (if (equal major-mode 'mh-folder-mode)
  11817. (mh-show)
  11818. (mh-show-show))
  11819. header-field)))
  11820. (defun org-follow-mhe-link (folder article)
  11821. "Follow an MHE link to FOLDER and ARTICLE.
  11822. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11823. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11824. ARTICLE is searched in all folders. Indexed searches (swish++,
  11825. namazu, and others supported by MH-E) will always search in all
  11826. folders."
  11827. (require 'mh-e)
  11828. (require 'mh-search)
  11829. (require 'mh-utils)
  11830. (mh-find-path)
  11831. (if (not article)
  11832. (mh-visit-folder (mh-normalize-folder-name folder))
  11833. (setq article (org-add-angle-brackets article))
  11834. (mh-search-choose)
  11835. (if (equal mh-searcher 'pick)
  11836. (progn
  11837. (mh-search folder (list "--message-id" article))
  11838. (when (and org-mhe-search-all-folders
  11839. (not (org-mhe-get-message-real-folder)))
  11840. (kill-this-buffer)
  11841. (mh-search "+" (list "--message-id" article))))
  11842. (mh-search "+" article))
  11843. (if (org-mhe-get-message-real-folder)
  11844. (mh-show-msg 1)
  11845. (kill-this-buffer)
  11846. (error "Message not found"))))
  11847. ;;; BibTeX links
  11848. ;; Use the custom search meachnism to construct and use search strings for
  11849. ;; file links to BibTeX database entries.
  11850. (defun org-create-file-search-in-bibtex ()
  11851. "Create the search string and description for a BibTeX database entry."
  11852. (when (eq major-mode 'bibtex-mode)
  11853. ;; yes, we want to construct this search string.
  11854. ;; Make a good description for this entry, using names, year and the title
  11855. ;; Put it into the `description' variable which is dynamically scoped.
  11856. (let ((bibtex-autokey-names 1)
  11857. (bibtex-autokey-names-stretch 1)
  11858. (bibtex-autokey-name-case-convert-function 'identity)
  11859. (bibtex-autokey-name-separator " & ")
  11860. (bibtex-autokey-additional-names " et al.")
  11861. (bibtex-autokey-year-length 4)
  11862. (bibtex-autokey-name-year-separator " ")
  11863. (bibtex-autokey-titlewords 3)
  11864. (bibtex-autokey-titleword-separator " ")
  11865. (bibtex-autokey-titleword-case-convert-function 'identity)
  11866. (bibtex-autokey-titleword-length 'infty)
  11867. (bibtex-autokey-year-title-separator ": "))
  11868. (setq description (bibtex-generate-autokey)))
  11869. ;; Now parse the entry, get the key and return it.
  11870. (save-excursion
  11871. (bibtex-beginning-of-entry)
  11872. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11873. (defun org-execute-file-search-in-bibtex (s)
  11874. "Find the link search string S as a key for a database entry."
  11875. (when (eq major-mode 'bibtex-mode)
  11876. ;; Yes, we want to do the search in this file.
  11877. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11878. (goto-char (point-min))
  11879. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11880. (regexp-quote s) "[ \t\n]*,") nil t)
  11881. (goto-char (match-beginning 0)))
  11882. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11883. ;; Use double prefix to indicate that any web link should be browsed
  11884. (let ((b (current-buffer)) (p (point)))
  11885. ;; Restore the window configuration because we just use the web link
  11886. (set-window-configuration org-window-config-before-follow-link)
  11887. (save-excursion (set-buffer b) (goto-char p)
  11888. (bibtex-url)))
  11889. (recenter 0)) ; Move entry start to beginning of window
  11890. ;; return t to indicate that the search is done.
  11891. t))
  11892. ;; Finally add the functions to the right hooks.
  11893. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11894. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11895. ;; end of Bibtex link setup
  11896. ;;; Following file links
  11897. (defun org-open-file (path &optional in-emacs line search)
  11898. "Open the file at PATH.
  11899. First, this expands any special file name abbreviations. Then the
  11900. configuration variable `org-file-apps' is checked if it contains an
  11901. entry for this file type, and if yes, the corresponding command is launched.
  11902. If no application is found, Emacs simply visits the file.
  11903. With optional argument IN-EMACS, Emacs will visit the file.
  11904. Optional LINE specifies a line to go to, optional SEARCH a string to
  11905. search for. If LINE or SEARCH is given, the file will always be
  11906. opened in Emacs.
  11907. If the file does not exist, an error is thrown."
  11908. (setq in-emacs (or in-emacs line search))
  11909. (let* ((file (if (equal path "")
  11910. buffer-file-name
  11911. (substitute-in-file-name (expand-file-name path))))
  11912. (apps (append org-file-apps (org-default-apps)))
  11913. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  11914. (dirp (if remp nil (file-directory-p file)))
  11915. (dfile (downcase file))
  11916. (old-buffer (current-buffer))
  11917. (old-pos (point))
  11918. (old-mode major-mode)
  11919. ext cmd)
  11920. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  11921. (setq ext (match-string 1 dfile))
  11922. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  11923. (setq ext (match-string 1 dfile))))
  11924. (if in-emacs
  11925. (setq cmd 'emacs)
  11926. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  11927. (and dirp (cdr (assoc 'directory apps)))
  11928. (cdr (assoc ext apps))
  11929. (cdr (assoc t apps)))))
  11930. (when (eq cmd 'mailcap)
  11931. (require 'mailcap)
  11932. (mailcap-parse-mailcaps)
  11933. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  11934. (command (mailcap-mime-info mime-type)))
  11935. (if (stringp command)
  11936. (setq cmd command)
  11937. (setq cmd 'emacs))))
  11938. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  11939. (not (file-exists-p file))
  11940. (not org-open-non-existing-files))
  11941. (error "No such file: %s" file))
  11942. (cond
  11943. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  11944. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  11945. (if (string-match "['\"]%s['\"]" cmd)
  11946. (setq cmd (replace-match "%s" t t cmd)))
  11947. (setq cmd (format cmd (shell-quote-argument file)))
  11948. (save-window-excursion
  11949. (start-process-shell-command cmd nil cmd)))
  11950. ((or (stringp cmd)
  11951. (eq cmd 'emacs))
  11952. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  11953. (widen)
  11954. (if line (goto-line line)
  11955. (if search (org-link-search search))))
  11956. ((consp cmd)
  11957. (eval cmd))
  11958. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  11959. (and (org-mode-p) (eq old-mode 'org-mode)
  11960. (or (not (equal old-buffer (current-buffer)))
  11961. (not (equal old-pos (point))))
  11962. (org-mark-ring-push old-pos old-buffer))))
  11963. (defun org-default-apps ()
  11964. "Return the default applications for this operating system."
  11965. (cond
  11966. ((eq system-type 'darwin)
  11967. org-file-apps-defaults-macosx)
  11968. ((eq system-type 'windows-nt)
  11969. org-file-apps-defaults-windowsnt)
  11970. (t org-file-apps-defaults-gnu)))
  11971. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  11972. (defun org-file-remote-p (file)
  11973. "Test whether FILE specifies a location on a remote system.
  11974. Return non-nil if the location is indeed remote.
  11975. For example, the filename \"/user@host:/foo\" specifies a location
  11976. on the system \"/user@host:\"."
  11977. (cond ((fboundp 'file-remote-p)
  11978. (file-remote-p file))
  11979. ((fboundp 'tramp-handle-file-remote-p)
  11980. (tramp-handle-file-remote-p file))
  11981. ((and (boundp 'ange-ftp-name-format)
  11982. (string-match (car ange-ftp-name-format) file))
  11983. t)
  11984. (t nil)))
  11985. ;;;; Hooks for remember.el, and refiling
  11986. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  11987. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  11988. ;;;###autoload
  11989. (defun org-remember-insinuate ()
  11990. "Setup remember.el for use wiht Org-mode."
  11991. (require 'remember)
  11992. (setq remember-annotation-functions '(org-remember-annotation))
  11993. (setq remember-handler-functions '(org-remember-handler))
  11994. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  11995. ;;;###autoload
  11996. (defun org-remember-annotation ()
  11997. "Return a link to the current location as an annotation for remember.el.
  11998. If you are using Org-mode files as target for data storage with
  11999. remember.el, then the annotations should include a link compatible with the
  12000. conventions in Org-mode. This function returns such a link."
  12001. (org-store-link nil))
  12002. (defconst org-remember-help
  12003. "Select a destination location for the note.
  12004. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12005. RET on headline -> Store as sublevel entry to current headline
  12006. RET at beg-of-buf -> Append to file as level 2 headline
  12007. <left>/<right> -> before/after current headline, same headings level")
  12008. (defvar org-remember-previous-location nil)
  12009. (defvar org-force-remember-template-char) ;; dynamically scoped
  12010. (defun org-select-remember-template (&optional use-char)
  12011. (when org-remember-templates
  12012. (let* ((templates (mapcar (lambda (x)
  12013. (if (stringp (car x))
  12014. (append (list (nth 1 x) (car x)) (cddr x))
  12015. (append (list (car x) "") (cdr x))))
  12016. org-remember-templates))
  12017. (char (or use-char
  12018. (cond
  12019. ((= (length templates) 1)
  12020. (caar templates))
  12021. ((and (boundp 'org-force-remember-template-char)
  12022. org-force-remember-template-char)
  12023. (if (stringp org-force-remember-template-char)
  12024. (string-to-char org-force-remember-template-char)
  12025. org-force-remember-template-char))
  12026. (t
  12027. (message "Select template: %s"
  12028. (mapconcat
  12029. (lambda (x)
  12030. (cond
  12031. ((not (string-match "\\S-" (nth 1 x)))
  12032. (format "[%c]" (car x)))
  12033. ((equal (downcase (car x))
  12034. (downcase (aref (nth 1 x) 0)))
  12035. (format "[%c]%s" (car x)
  12036. (substring (nth 1 x) 1)))
  12037. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12038. templates " "))
  12039. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12040. (when (equal char0 ?\C-g)
  12041. (jump-to-register remember-register)
  12042. (kill-buffer remember-buffer))
  12043. char0))))))
  12044. (cddr (assoc char templates)))))
  12045. (defvar x-last-selected-text)
  12046. (defvar x-last-selected-text-primary)
  12047. ;;;###autoload
  12048. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12049. "Initialize *remember* buffer with template, invoke `org-mode'.
  12050. This function should be placed into `remember-mode-hook' and in fact requires
  12051. to be run from that hook to function properly."
  12052. (unless (fboundp 'remember-finalize)
  12053. (defalias 'remember-finalize 'remember-buffer))
  12054. (if org-remember-templates
  12055. (let* ((entry (org-select-remember-template use-char))
  12056. (tpl (car entry))
  12057. (plist-p (if org-store-link-plist t nil))
  12058. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12059. (string-match "\\S-" (nth 1 entry)))
  12060. (nth 1 entry)
  12061. org-default-notes-file))
  12062. (headline (nth 2 entry))
  12063. (v-c (or (and (eq window-system 'x)
  12064. (fboundp 'x-cut-buffer-or-selection-value)
  12065. (x-cut-buffer-or-selection-value))
  12066. (org-bound-and-true-p x-last-selected-text)
  12067. (org-bound-and-true-p x-last-selected-text-primary)
  12068. (and (> (length kill-ring) 0) (current-kill 0))))
  12069. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12070. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12071. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12072. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12073. ;; `initial' and `annotation' are bound in `remember'
  12074. (v-i (if (boundp 'initial) initial))
  12075. (v-a (if (and (boundp 'annotation) annotation)
  12076. (if (equal annotation "[[]]") "" annotation)
  12077. ""))
  12078. (v-A (if (and v-a
  12079. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12080. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12081. v-a))
  12082. (v-n user-full-name)
  12083. (org-startup-folded nil)
  12084. org-time-was-given org-end-time-was-given x
  12085. prompt completions char time pos default histvar)
  12086. (setq org-store-link-plist
  12087. (append (list :annotation v-a :initial v-i)
  12088. org-store-link-plist))
  12089. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12090. (erase-buffer)
  12091. (insert (substitute-command-keys
  12092. (format
  12093. "## Filing location: Select interactively, default, or last used:
  12094. ## %s to select file and header location interactively.
  12095. ## %s \"%s\" -> \"* %s\"
  12096. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12097. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12098. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12099. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12100. (abbreviate-file-name (or file org-default-notes-file))
  12101. (or headline "")
  12102. (or (car org-remember-previous-location) "???")
  12103. (or (cdr org-remember-previous-location) "???"))))
  12104. (insert tpl) (goto-char (point-min))
  12105. ;; Simple %-escapes
  12106. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12107. (when (and initial (equal (match-string 0) "%i"))
  12108. (save-match-data
  12109. (let* ((lead (buffer-substring
  12110. (point-at-bol) (match-beginning 0))))
  12111. (setq v-i (mapconcat 'identity
  12112. (org-split-string initial "\n")
  12113. (concat "\n" lead))))))
  12114. (replace-match
  12115. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12116. t t))
  12117. ;; %[] Insert contents of a file.
  12118. (goto-char (point-min))
  12119. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12120. (let ((start (match-beginning 0))
  12121. (end (match-end 0))
  12122. (filename (expand-file-name (match-string 1))))
  12123. (goto-char start)
  12124. (delete-region start end)
  12125. (condition-case error
  12126. (insert-file-contents filename)
  12127. (error (insert (format "%%![Couldn't insert %s: %s]"
  12128. filename error))))))
  12129. ;; %() embedded elisp
  12130. (goto-char (point-min))
  12131. (while (re-search-forward "%\\((.+)\\)" nil t)
  12132. (goto-char (match-beginning 0))
  12133. (let ((template-start (point)))
  12134. (forward-char 1)
  12135. (let ((result
  12136. (condition-case error
  12137. (eval (read (current-buffer)))
  12138. (error (format "%%![Error: %s]" error)))))
  12139. (delete-region template-start (point))
  12140. (insert result))))
  12141. ;; From the property list
  12142. (when plist-p
  12143. (goto-char (point-min))
  12144. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12145. (and (setq x (or (plist-get org-store-link-plist
  12146. (intern (match-string 1))) ""))
  12147. (replace-match x t t))))
  12148. ;; Turn on org-mode in the remember buffer, set local variables
  12149. (org-mode)
  12150. (org-set-local 'org-finish-function 'remember-finalize)
  12151. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12152. (org-set-local 'org-default-notes-file file))
  12153. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12154. (org-set-local 'org-remember-default-headline headline))
  12155. ;; Interactive template entries
  12156. (goto-char (point-min))
  12157. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  12158. (setq char (if (match-end 3) (match-string 3))
  12159. prompt (if (match-end 2) (match-string 2)))
  12160. (goto-char (match-beginning 0))
  12161. (replace-match "")
  12162. (setq completions nil default nil)
  12163. (when prompt
  12164. (setq completions (org-split-string prompt "|")
  12165. prompt (pop completions)
  12166. default (car completions)
  12167. histvar (intern (concat
  12168. "org-remember-template-prompt-history::"
  12169. (or prompt "")))
  12170. completions (mapcar 'list completions)))
  12171. (cond
  12172. ((member char '("G" "g"))
  12173. (let* ((org-last-tags-completion-table
  12174. (org-global-tags-completion-table
  12175. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12176. (org-add-colon-after-tag-completion t)
  12177. (ins (completing-read
  12178. (if prompt (concat prompt ": ") "Tags: ")
  12179. 'org-tags-completion-function nil nil nil
  12180. 'org-tags-history)))
  12181. (setq ins (mapconcat 'identity
  12182. (org-split-string ins (org-re "[^[:alnum:]]+"))
  12183. ":"))
  12184. (when (string-match "\\S-" ins)
  12185. (or (equal (char-before) ?:) (insert ":"))
  12186. (insert ins)
  12187. (or (equal (char-after) ?:) (insert ":")))))
  12188. (char
  12189. (setq org-time-was-given (equal (upcase char) char))
  12190. (setq time (org-read-date (equal (upcase char) "U") t nil
  12191. prompt))
  12192. (org-insert-time-stamp time org-time-was-given
  12193. (member char '("u" "U"))
  12194. nil nil (list org-end-time-was-given)))
  12195. (t
  12196. (insert (completing-read
  12197. (concat (if prompt prompt "Enter string")
  12198. (if default (concat " [" default "]"))
  12199. ": ")
  12200. completions nil nil nil histvar default)))))
  12201. (goto-char (point-min))
  12202. (if (re-search-forward "%\\?" nil t)
  12203. (replace-match "")
  12204. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12205. (org-mode)
  12206. (org-set-local 'org-finish-function 'remember-finalize))
  12207. (when (save-excursion
  12208. (goto-char (point-min))
  12209. (re-search-forward "%!" nil t))
  12210. (replace-match "")
  12211. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12212. (defun org-remember-finish-immediately ()
  12213. "File remember note immediately.
  12214. This should be run in `post-command-hook' and will remove itself
  12215. from that hook."
  12216. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12217. (when org-finish-function
  12218. (funcall org-finish-function)))
  12219. ;;;###autoload
  12220. (defun org-remember (&optional goto org-force-remember-template-char)
  12221. "Call `remember'. If this is already a remember buffer, re-apply template.
  12222. If there is an active region, make sure remember uses it as initial content
  12223. of the remember buffer.
  12224. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12225. anything, just go to the file/headline where the selected templated usually
  12226. stores its notes. With a double prefix arg `C-u C-u', got to the last
  12227. note stored by remember.
  12228. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12229. associated with a template in `org-remember-tempates'."
  12230. (interactive "P")
  12231. (cond
  12232. ((equal goto '(4)) (org-go-to-remember-target))
  12233. ((equal goto '(16)) (org-remember-goto-last-stored))
  12234. (t
  12235. (if (eq org-finish-function 'remember-buffer)
  12236. (progn
  12237. (when (< (length org-remember-templates) 2)
  12238. (error "No other template available"))
  12239. (erase-buffer)
  12240. (let ((annotation (plist-get org-store-link-plist :annotation))
  12241. (initial (plist-get org-store-link-plist :initial)))
  12242. (org-remember-apply-template))
  12243. (message "Press C-c C-c to remember data"))
  12244. (if (org-region-active-p)
  12245. (remember (buffer-substring (point) (mark)))
  12246. (call-interactively 'remember))))))
  12247. (defun org-remember-goto-last-stored ()
  12248. "Go to the location where the last remember note was stored."
  12249. (interactive)
  12250. (bookmark-jump "org-remember-last-stored")
  12251. (message "This is the last note stored by remember"))
  12252. (defun org-go-to-remember-target (&optional template-key)
  12253. "Go to the target location of a remember template.
  12254. The user is queried for the template."
  12255. (interactive)
  12256. (let* ((entry (org-select-remember-template template-key))
  12257. (file (nth 1 entry))
  12258. (heading (nth 2 entry))
  12259. visiting)
  12260. (unless (and file (stringp file) (string-match "\\S-" file))
  12261. (setq file org-default-notes-file))
  12262. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12263. (setq heading org-remember-default-headline))
  12264. (setq visiting (org-find-base-buffer-visiting file))
  12265. (if (not visiting) (find-file-noselect file))
  12266. (switch-to-buffer (or visiting (get-file-buffer file)))
  12267. (widen)
  12268. (goto-char (point-min))
  12269. (if (re-search-forward
  12270. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12271. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12272. nil t)
  12273. (goto-char (match-beginning 0))
  12274. (error "Target headline not found: %s" heading))))
  12275. (defvar org-note-abort nil) ; dynamically scoped
  12276. ;;;###autoload
  12277. (defun org-remember-handler ()
  12278. "Store stuff from remember.el into an org file.
  12279. First prompts for an org file. If the user just presses return, the value
  12280. of `org-default-notes-file' is used.
  12281. Then the command offers the headings tree of the selected file in order to
  12282. file the text at a specific location.
  12283. You can either immediately press RET to get the note appended to the
  12284. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12285. find a better place. Then press RET or <left> or <right> in insert the note.
  12286. Key Cursor position Note gets inserted
  12287. -----------------------------------------------------------------------------
  12288. RET buffer-start as level 1 heading at end of file
  12289. RET on headline as sublevel of the heading at cursor
  12290. RET no heading at cursor position, level taken from context.
  12291. Or use prefix arg to specify level manually.
  12292. <left> on headline as same level, before current heading
  12293. <right> on headline as same level, after current heading
  12294. So the fastest way to store the note is to press RET RET to append it to
  12295. the default file. This way your current train of thought is not
  12296. interrupted, in accordance with the principles of remember.el.
  12297. You can also get the fast execution without prompting by using
  12298. C-u C-c C-c to exit the remember buffer. See also the variable
  12299. `org-remember-store-without-prompt'.
  12300. Before being stored away, the function ensures that the text has a
  12301. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12302. is constructed from the current date and some additional data.
  12303. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12304. also indented so that it starts in the same column as the headline
  12305. \(i.e. after the stars).
  12306. See also the variable `org-reverse-note-order'."
  12307. (goto-char (point-min))
  12308. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12309. (replace-match ""))
  12310. (goto-char (point-max))
  12311. (catch 'quit
  12312. (if org-note-abort (throw 'quit nil))
  12313. (let* ((txt (buffer-substring (point-min) (point-max)))
  12314. (fastp (org-xor (equal current-prefix-arg '(4))
  12315. org-remember-store-without-prompt))
  12316. (file (if fastp org-default-notes-file (org-get-org-file)))
  12317. (heading org-remember-default-headline)
  12318. (visiting (org-find-base-buffer-visiting file))
  12319. (org-startup-folded nil)
  12320. (org-startup-align-all-tables nil)
  12321. (org-goto-start-pos 1)
  12322. spos exitcmd level indent reversed)
  12323. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12324. (setq file (car org-remember-previous-location)
  12325. heading (cdr org-remember-previous-location)))
  12326. (setq current-prefix-arg nil)
  12327. ;; Modify text so that it becomes a nice subtree which can be inserted
  12328. ;; into an org tree.
  12329. (let* ((lines (split-string txt "\n"))
  12330. first)
  12331. (setq first (car lines) lines (cdr lines))
  12332. (if (string-match "^\\*+ " first)
  12333. ;; Is already a headline
  12334. (setq indent nil)
  12335. ;; We need to add a headline: Use time and first buffer line
  12336. (setq lines (cons first lines)
  12337. first (concat "* " (current-time-string)
  12338. " (" (remember-buffer-desc) ")")
  12339. indent " "))
  12340. (if (and org-adapt-indentation indent)
  12341. (setq lines (mapcar
  12342. (lambda (x)
  12343. (if (string-match "\\S-" x)
  12344. (concat indent x) x))
  12345. lines)))
  12346. (setq txt (concat first "\n"
  12347. (mapconcat 'identity lines "\n"))))
  12348. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12349. (setq txt (replace-match "\n\n" t t txt))
  12350. (if (string-match "[ \t\n]*\\'" txt)
  12351. (setq txt (replace-match "\n" t t txt))))
  12352. ;; Find the file
  12353. (if (not visiting) (find-file-noselect file))
  12354. (with-current-buffer (or visiting (get-file-buffer file))
  12355. (unless (org-mode-p)
  12356. (error "Target files for remember notes must be in Org-mode"))
  12357. (save-excursion
  12358. (save-restriction
  12359. (widen)
  12360. (and (goto-char (point-min))
  12361. (not (re-search-forward "^\\* " nil t))
  12362. (insert "\n* " (or heading "Notes") "\n"))
  12363. (setq reversed (org-notes-order-reversed-p))
  12364. ;; Find the default location
  12365. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12366. (goto-char (point-min))
  12367. (if (re-search-forward
  12368. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12369. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12370. nil t)
  12371. (setq org-goto-start-pos (match-beginning 0))
  12372. (when fastp
  12373. (goto-char (point-max))
  12374. (unless (bolp) (newline))
  12375. (insert "* " heading "\n")
  12376. (setq org-goto-start-pos (point-at-bol 0)))))
  12377. ;; Ask the User for a location
  12378. (if fastp
  12379. (setq spos org-goto-start-pos
  12380. exitcmd 'return)
  12381. (setq spos (org-get-location (current-buffer) org-remember-help)
  12382. exitcmd (cdr spos)
  12383. spos (car spos)))
  12384. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12385. ; not handle this note
  12386. (goto-char spos)
  12387. (cond ((org-on-heading-p t)
  12388. (org-back-to-heading t)
  12389. (setq level (funcall outline-level))
  12390. (cond
  12391. ((eq exitcmd 'return)
  12392. ;; sublevel of current
  12393. (setq org-remember-previous-location
  12394. (cons (abbreviate-file-name file)
  12395. (org-get-heading 'notags)))
  12396. (if reversed
  12397. (outline-next-heading)
  12398. (org-end-of-subtree)
  12399. (if (not (bolp))
  12400. (if (looking-at "[ \t]*\n")
  12401. (beginning-of-line 2)
  12402. (end-of-line 1)
  12403. (insert "\n"))))
  12404. (bookmark-set "org-remember-last-stored")
  12405. (org-paste-subtree (org-get-legal-level level 1) txt))
  12406. ((eq exitcmd 'left)
  12407. ;; before current
  12408. (bookmark-set "org-remember-last-stored")
  12409. (org-paste-subtree level txt))
  12410. ((eq exitcmd 'right)
  12411. ;; after current
  12412. (org-end-of-subtree t)
  12413. (bookmark-set "org-remember-last-stored")
  12414. (org-paste-subtree level txt))
  12415. (t (error "This should not happen"))))
  12416. ((and (bobp) (not reversed))
  12417. ;; Put it at the end, one level below level 1
  12418. (save-restriction
  12419. (widen)
  12420. (goto-char (point-max))
  12421. (if (not (bolp)) (newline))
  12422. (bookmark-set "org-remember-last-stored")
  12423. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12424. ((and (bobp) reversed)
  12425. ;; Put it at the start, as level 1
  12426. (save-restriction
  12427. (widen)
  12428. (goto-char (point-min))
  12429. (re-search-forward "^\\*+ " nil t)
  12430. (beginning-of-line 1)
  12431. (bookmark-set "org-remember-last-stored")
  12432. (org-paste-subtree 1 txt)))
  12433. (t
  12434. ;; Put it right there, with automatic level determined by
  12435. ;; org-paste-subtree or from prefix arg
  12436. (bookmark-set "org-remember-last-stored")
  12437. (org-paste-subtree
  12438. (if (numberp current-prefix-arg) current-prefix-arg)
  12439. txt)))
  12440. (when remember-save-after-remembering
  12441. (save-buffer)
  12442. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12443. t) ;; return t to indicate that we took care of this note.
  12444. (defun org-get-org-file ()
  12445. "Read a filename, with default directory `org-directory'."
  12446. (let ((default (or org-default-notes-file remember-data-file)))
  12447. (read-file-name (format "File name [%s]: " default)
  12448. (file-name-as-directory org-directory)
  12449. default)))
  12450. (defun org-notes-order-reversed-p ()
  12451. "Check if the current file should receive notes in reversed order."
  12452. (cond
  12453. ((not org-reverse-note-order) nil)
  12454. ((eq t org-reverse-note-order) t)
  12455. ((not (listp org-reverse-note-order)) nil)
  12456. (t (catch 'exit
  12457. (let ((all org-reverse-note-order)
  12458. entry)
  12459. (while (setq entry (pop all))
  12460. (if (string-match (car entry) buffer-file-name)
  12461. (throw 'exit (cdr entry))))
  12462. nil)))))
  12463. ;;; Refiling
  12464. (defvar org-refile-target-table nil
  12465. "The list of refile targets, created by `org-refile'.")
  12466. (defvar org-agenda-new-buffers nil
  12467. "Buffers created to visit agenda files.")
  12468. (defun org-get-refile-targets ()
  12469. "Produce a table with refile targets."
  12470. (let ((entries org-refile-targets)
  12471. org-agenda-new-files targets txt re files f desc descre)
  12472. (while (setq entry (pop entries))
  12473. (setq files (car entry) desc (cdr entry))
  12474. (cond
  12475. ((null files) (setq files (list (current-buffer))))
  12476. ((eq files 'org-agenda-files)
  12477. (setq files (org-agenda-files 'unrestricted)))
  12478. ((and (symbolp files) (fboundp files))
  12479. (setq files (funcall files)))
  12480. ((and (symbolp files) (boundp files))
  12481. (setq files (symbol-value files))))
  12482. (if (stringp files) (setq files (list files)))
  12483. (cond
  12484. ((eq (car desc) :tag)
  12485. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12486. ((eq (car desc) :todo)
  12487. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12488. ((eq (car desc) :regexp)
  12489. (setq descre (cdr desc)))
  12490. ((eq (car desc) :level)
  12491. (setq descre (concat "^\\*\\{" (number-to-string
  12492. (if org-odd-levels-only
  12493. (1- (* 2 (cdr desc)))
  12494. (cdr desc)))
  12495. "\\}[ \t]")))
  12496. ((eq (car desc) :maxlevel)
  12497. (setq descre (concat "^\\*\\{1," (number-to-string
  12498. (if org-odd-levels-only
  12499. (1- (* 2 (cdr desc)))
  12500. (cdr desc)))
  12501. "\\}[ \t]")))
  12502. (t (error "Bad refiling target description %s" desc)))
  12503. (while (setq f (pop files))
  12504. (save-excursion
  12505. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12506. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12507. (save-excursion
  12508. (save-restriction
  12509. (widen)
  12510. (goto-char (point-min))
  12511. (while (re-search-forward descre nil t)
  12512. (goto-char (point-at-bol))
  12513. (when (looking-at org-complex-heading-regexp)
  12514. (setq txt (match-string 4)
  12515. re (concat "^" (regexp-quote
  12516. (buffer-substring (match-beginning 1)
  12517. (match-end 4)))))
  12518. (if (match-end 5) (setq re (concat re "[ \t]+"
  12519. (regexp-quote
  12520. (match-string 5)))))
  12521. (setq re (concat re "[ \t]*$"))
  12522. (when org-refile-use-outline-path
  12523. (setq txt (mapconcat 'identity
  12524. (append (org-get-outline-path)
  12525. (list txt))
  12526. "/")))
  12527. (push (list txt f re (point)) targets))
  12528. (goto-char (point-at-eol))))))))
  12529. (org-release-buffers org-agenda-new-buffers)
  12530. (nreverse targets)))
  12531. (defun org-get-outline-path ()
  12532. (let (rtn)
  12533. (save-excursion
  12534. (while (org-up-heading-safe)
  12535. (when (looking-at org-complex-heading-regexp)
  12536. (push (org-match-string-no-properties 4) rtn)))
  12537. rtn)))
  12538. (defun org-refile (&optional reversed-or-update)
  12539. "Move the entry at point to another heading.
  12540. The list of target headings is compiled using the information in
  12541. `org-refile-targets', which see. This list is created upon first use, and
  12542. you can update it by calling this command with a double prefix (`C-u C-u').
  12543. At the target location, the entry is filed as a subitem of the target heading.
  12544. Depending on `org-reverse-note-order', the new subitem will either be the
  12545. first of the last subitem. A single C-u prefix will toggle the value of this
  12546. variable for the duration of the command."
  12547. (interactive "P")
  12548. (if (equal reversed-or-update '(16))
  12549. (progn
  12550. (setq org-refile-target-table (org-get-refile-targets))
  12551. (message "Refile targets updated (%d targets)"
  12552. (length org-refile-target-table)))
  12553. (when (or (not org-refile-target-table)
  12554. (and (= (length org-refile-targets) 1)
  12555. (not (caar org-refile-targets))))
  12556. (setq org-refile-target-table (org-get-refile-targets)))
  12557. (unless org-refile-target-table
  12558. (error "No refile targets"))
  12559. (let* ((cbuf (current-buffer))
  12560. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12561. (fname (and filename (file-truename filename)))
  12562. (tbl (mapcar
  12563. (lambda (x)
  12564. (if (not (equal fname (file-truename (nth 1 x))))
  12565. (cons (concat (car x) " (" (file-name-nondirectory
  12566. (nth 1 x)) ")")
  12567. (cdr x))
  12568. x))
  12569. org-refile-target-table))
  12570. (completion-ignore-case t)
  12571. pos it nbuf file re level reversed)
  12572. (when (setq it (completing-read "Refile to: " tbl
  12573. nil t nil 'org-refile-history))
  12574. (setq it (assoc it tbl)
  12575. file (nth 1 it)
  12576. re (nth 2 it))
  12577. (org-copy-special)
  12578. (save-excursion
  12579. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12580. (find-file-noselect file))))
  12581. (setq reversed (org-notes-order-reversed-p))
  12582. (if (equal reversed-or-update '(16)) (setq reversed (not reversed)))
  12583. (save-excursion
  12584. (save-restriction
  12585. (widen)
  12586. (goto-char (point-min))
  12587. (unless (re-search-forward re nil t)
  12588. (error "Cannot find target location - try again with `C-u' prefix."))
  12589. (goto-char (match-beginning 0))
  12590. (looking-at outline-regexp)
  12591. (setq level (org-get-legal-level (funcall outline-level) 1))
  12592. (goto-char (or (save-excursion
  12593. (if reversed
  12594. (outline-next-heading)
  12595. (outline-get-next-sibling)))
  12596. (point-max)))
  12597. (org-paste-subtree level))))
  12598. (org-cut-special)
  12599. (message "Entry refiled to \"%s\"" (car it))))))
  12600. ;;;; Dynamic blocks
  12601. (defun org-find-dblock (name)
  12602. "Find the first dynamic block with name NAME in the buffer.
  12603. If not found, stay at current position and return nil."
  12604. (let (pos)
  12605. (save-excursion
  12606. (goto-char (point-min))
  12607. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12608. nil t)
  12609. (match-beginning 0))))
  12610. (if pos (goto-char pos))
  12611. pos))
  12612. (defconst org-dblock-start-re
  12613. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12614. "Matches the startline of a dynamic block, with parameters.")
  12615. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12616. "Matches the end of a dyhamic block.")
  12617. (defun org-create-dblock (plist)
  12618. "Create a dynamic block section, with parameters taken from PLIST.
  12619. PLIST must containe a :name entry which is used as name of the block."
  12620. (unless (bolp) (newline))
  12621. (let ((name (plist-get plist :name)))
  12622. (insert "#+BEGIN: " name)
  12623. (while plist
  12624. (if (eq (car plist) :name)
  12625. (setq plist (cddr plist))
  12626. (insert " " (prin1-to-string (pop plist)))))
  12627. (insert "\n\n#+END:\n")
  12628. (beginning-of-line -2)))
  12629. (defun org-prepare-dblock ()
  12630. "Prepare dynamic block for refresh.
  12631. This empties the block, puts the cursor at the insert position and returns
  12632. the property list including an extra property :name with the block name."
  12633. (unless (looking-at org-dblock-start-re)
  12634. (error "Not at a dynamic block"))
  12635. (let* ((begdel (1+ (match-end 0)))
  12636. (name (org-no-properties (match-string 1)))
  12637. (params (append (list :name name)
  12638. (read (concat "(" (match-string 3) ")")))))
  12639. (unless (re-search-forward org-dblock-end-re nil t)
  12640. (error "Dynamic block not terminated"))
  12641. (delete-region begdel (match-beginning 0))
  12642. (goto-char begdel)
  12643. (open-line 1)
  12644. params))
  12645. (defun org-map-dblocks (&optional command)
  12646. "Apply COMMAND to all dynamic blocks in the current buffer.
  12647. If COMMAND is not given, use `org-update-dblock'."
  12648. (let ((cmd (or command 'org-update-dblock))
  12649. pos)
  12650. (save-excursion
  12651. (goto-char (point-min))
  12652. (while (re-search-forward org-dblock-start-re nil t)
  12653. (goto-char (setq pos (match-beginning 0)))
  12654. (condition-case nil
  12655. (funcall cmd)
  12656. (error (message "Error during update of dynamic block")))
  12657. (goto-char pos)
  12658. (unless (re-search-forward org-dblock-end-re nil t)
  12659. (error "Dynamic block not terminated"))))))
  12660. (defun org-dblock-update (&optional arg)
  12661. "User command for updating dynamic blocks.
  12662. Update the dynamic block at point. With prefix ARG, update all dynamic
  12663. blocks in the buffer."
  12664. (interactive "P")
  12665. (if arg
  12666. (org-update-all-dblocks)
  12667. (or (looking-at org-dblock-start-re)
  12668. (org-beginning-of-dblock))
  12669. (org-update-dblock)))
  12670. (defun org-update-dblock ()
  12671. "Update the dynamic block at point
  12672. This means to empty the block, parse for parameters and then call
  12673. the correct writing function."
  12674. (save-window-excursion
  12675. (let* ((pos (point))
  12676. (line (org-current-line))
  12677. (params (org-prepare-dblock))
  12678. (name (plist-get params :name))
  12679. (cmd (intern (concat "org-dblock-write:" name))))
  12680. (message "Updating dynamic block `%s' at line %d..." name line)
  12681. (funcall cmd params)
  12682. (message "Updating dynamic block `%s' at line %d...done" name line)
  12683. (goto-char pos))))
  12684. (defun org-beginning-of-dblock ()
  12685. "Find the beginning of the dynamic block at point.
  12686. Error if there is no scuh block at point."
  12687. (let ((pos (point))
  12688. beg)
  12689. (end-of-line 1)
  12690. (if (and (re-search-backward org-dblock-start-re nil t)
  12691. (setq beg (match-beginning 0))
  12692. (re-search-forward org-dblock-end-re nil t)
  12693. (> (match-end 0) pos))
  12694. (goto-char beg)
  12695. (goto-char pos)
  12696. (error "Not in a dynamic block"))))
  12697. (defun org-update-all-dblocks ()
  12698. "Update all dynamic blocks in the buffer.
  12699. This function can be used in a hook."
  12700. (when (org-mode-p)
  12701. (org-map-dblocks 'org-update-dblock)))
  12702. ;;;; Completion
  12703. (defconst org-additional-option-like-keywords
  12704. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12705. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12706. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12707. (defun org-complete (&optional arg)
  12708. "Perform completion on word at point.
  12709. At the beginning of a headline, this completes TODO keywords as given in
  12710. `org-todo-keywords'.
  12711. If the current word is preceded by a backslash, completes the TeX symbols
  12712. that are supported for HTML support.
  12713. If the current word is preceded by \"#+\", completes special words for
  12714. setting file options.
  12715. In the line after \"#+STARTUP:, complete valid keywords.\"
  12716. At all other locations, this simply calls the value of
  12717. `org-completion-fallback-command'."
  12718. (interactive "P")
  12719. (org-without-partial-completion
  12720. (catch 'exit
  12721. (let* ((end (point))
  12722. (beg1 (save-excursion
  12723. (skip-chars-backward (org-re "[:alnum:]_@"))
  12724. (point)))
  12725. (beg (save-excursion
  12726. (skip-chars-backward "a-zA-Z0-9_:$")
  12727. (point)))
  12728. (confirm (lambda (x) (stringp (car x))))
  12729. (searchhead (equal (char-before beg) ?*))
  12730. (tag (and (equal (char-before beg1) ?:)
  12731. (equal (char-after (point-at-bol)) ?*)))
  12732. (prop (and (equal (char-before beg1) ?:)
  12733. (not (equal (char-after (point-at-bol)) ?*))))
  12734. (texp (equal (char-before beg) ?\\))
  12735. (link (equal (char-before beg) ?\[))
  12736. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12737. beg)
  12738. "#+"))
  12739. (startup (string-match "^#\\+STARTUP:.*"
  12740. (buffer-substring (point-at-bol) (point))))
  12741. (completion-ignore-case opt)
  12742. (type nil)
  12743. (tbl nil)
  12744. (table (cond
  12745. (opt
  12746. (setq type :opt)
  12747. (append
  12748. (mapcar
  12749. (lambda (x)
  12750. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12751. (cons (match-string 2 x) (match-string 1 x)))
  12752. (org-split-string (org-get-current-options) "\n"))
  12753. (mapcar 'list org-additional-option-like-keywords)))
  12754. (startup
  12755. (setq type :startup)
  12756. org-startup-options)
  12757. (link (append org-link-abbrev-alist-local
  12758. org-link-abbrev-alist))
  12759. (texp
  12760. (setq type :tex)
  12761. org-html-entities)
  12762. ((string-match "\\`\\*+[ \t]+\\'"
  12763. (buffer-substring (point-at-bol) beg))
  12764. (setq type :todo)
  12765. (mapcar 'list org-todo-keywords-1))
  12766. (searchhead
  12767. (setq type :searchhead)
  12768. (save-excursion
  12769. (goto-char (point-min))
  12770. (while (re-search-forward org-todo-line-regexp nil t)
  12771. (push (list
  12772. (org-make-org-heading-search-string
  12773. (match-string 3) t))
  12774. tbl)))
  12775. tbl)
  12776. (tag (setq type :tag beg beg1)
  12777. (or org-tag-alist (org-get-buffer-tags)))
  12778. (prop (setq type :prop beg beg1)
  12779. (mapcar 'list (org-buffer-property-keys)))
  12780. (t (progn
  12781. (call-interactively org-completion-fallback-command)
  12782. (throw 'exit nil)))))
  12783. (pattern (buffer-substring-no-properties beg end))
  12784. (completion (try-completion pattern table confirm)))
  12785. (cond ((eq completion t)
  12786. (if (not (assoc (upcase pattern) table))
  12787. (message "Already complete")
  12788. (if (equal type :opt)
  12789. (insert (substring (cdr (assoc (upcase pattern) table))
  12790. (length pattern)))
  12791. (if (memq type '(:tag :prop)) (insert ":")))))
  12792. ((null completion)
  12793. (message "Can't find completion for \"%s\"" pattern)
  12794. (ding))
  12795. ((not (string= pattern completion))
  12796. (delete-region beg end)
  12797. (if (string-match " +$" completion)
  12798. (setq completion (replace-match "" t t completion)))
  12799. (insert completion)
  12800. (if (get-buffer-window "*Completions*")
  12801. (delete-window (get-buffer-window "*Completions*")))
  12802. (if (assoc completion table)
  12803. (if (eq type :todo) (insert " ")
  12804. (if (memq type '(:tag :prop)) (insert ":"))))
  12805. (if (and (equal type :opt) (assoc completion table))
  12806. (message "%s" (substitute-command-keys
  12807. "Press \\[org-complete] again to insert example settings"))))
  12808. (t
  12809. (message "Making completion list...")
  12810. (let ((list (sort (all-completions pattern table confirm)
  12811. 'string<)))
  12812. (with-output-to-temp-buffer "*Completions*"
  12813. (condition-case nil
  12814. ;; Protection needed for XEmacs and emacs 21
  12815. (display-completion-list list pattern)
  12816. (error (display-completion-list list)))))
  12817. (message "Making completion list...%s" "done")))))))
  12818. ;;;; TODO, DEADLINE, Comments
  12819. (defun org-toggle-comment ()
  12820. "Change the COMMENT state of an entry."
  12821. (interactive)
  12822. (save-excursion
  12823. (org-back-to-heading)
  12824. (let (case-fold-search)
  12825. (if (looking-at (concat outline-regexp
  12826. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  12827. (replace-match "" t t nil 1)
  12828. (if (looking-at outline-regexp)
  12829. (progn
  12830. (goto-char (match-end 0))
  12831. (insert org-comment-string " ")))))))
  12832. (defvar org-last-todo-state-is-todo nil
  12833. "This is non-nil when the last TODO state change led to a TODO state.
  12834. If the last change removed the TODO tag or switched to DONE, then
  12835. this is nil.")
  12836. (defvar org-setting-tags nil) ; dynamically skiped
  12837. ;; FIXME: better place
  12838. (defun org-property-or-variable-value (var &optional inherit)
  12839. "Check if there is a property fixing the value of VAR.
  12840. If yes, return this value. If not, return the current value of the variable."
  12841. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12842. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12843. (read prop)
  12844. (symbol-value var))))
  12845. (defun org-parse-local-options (string var)
  12846. "Parse STRING for startup setting relevant for variable VAR."
  12847. (let ((rtn (symbol-value var))
  12848. e opts)
  12849. (save-match-data
  12850. (if (or (not string) (not (string-match "\\S-" string)))
  12851. rtn
  12852. (setq opts (delq nil (mapcar (lambda (x)
  12853. (setq e (assoc x org-startup-options))
  12854. (if (eq (nth 1 e) var) e nil))
  12855. (org-split-string string "[ \t]+"))))
  12856. (if (not opts)
  12857. rtn
  12858. (setq rtn nil)
  12859. (while (setq e (pop opts))
  12860. (if (not (nth 3 e))
  12861. (setq rtn (nth 2 e))
  12862. (if (not (listp rtn)) (setq rtn nil))
  12863. (push (nth 2 e) rtn)))
  12864. rtn)))))
  12865. (defvar org-blocker-hook nil
  12866. "Hook for functions that are allowed to block a state change.
  12867. Each function gets as its single argument a property list, see
  12868. `org-trigger-hook' for more information about this list.
  12869. If any of the functions in this hook returns nil, the state change
  12870. is blocked.")
  12871. (defvar org-trigger-hook nil
  12872. "Hook for functions that are triggered by a state change.
  12873. Each function gets as its single argument a property list with at least
  12874. the following elements:
  12875. (:type type-of-change :position pos-at-entry-start
  12876. :from old-state :to new-state)
  12877. Depending on the type, more properties may be present.
  12878. This mechanism is currently implemented for:
  12879. TODO state changes
  12880. ------------------
  12881. :type todo-state-change
  12882. :from previous state (keyword as a string), or nil
  12883. :to new state (keyword as a string), or nil")
  12884. (defun org-todo (&optional arg)
  12885. "Change the TODO state of an item.
  12886. The state of an item is given by a keyword at the start of the heading,
  12887. like
  12888. *** TODO Write paper
  12889. *** DONE Call mom
  12890. The different keywords are specified in the variable `org-todo-keywords'.
  12891. By default the available states are \"TODO\" and \"DONE\".
  12892. So for this example: when the item starts with TODO, it is changed to DONE.
  12893. When it starts with DONE, the DONE is removed. And when neither TODO nor
  12894. DONE are present, add TODO at the beginning of the heading.
  12895. With C-u prefix arg, use completion to determine the new state.
  12896. With numeric prefix arg, switch to that state.
  12897. For calling through lisp, arg is also interpreted in the following way:
  12898. 'none -> empty state
  12899. \"\"(empty string) -> switch to empty state
  12900. 'done -> switch to DONE
  12901. 'nextset -> switch to the next set of keywords
  12902. 'previousset -> switch to the previous set of keywords
  12903. \"WAITING\" -> switch to the specified keyword, but only if it
  12904. really is a member of `org-todo-keywords'."
  12905. (interactive "P")
  12906. (save-excursion
  12907. (catch 'exit
  12908. (org-back-to-heading)
  12909. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  12910. (or (looking-at (concat " +" org-todo-regexp " *"))
  12911. (looking-at " *"))
  12912. (let* ((match-data (match-data))
  12913. (startpos (point-at-bol))
  12914. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  12915. (org-log-done (org-parse-local-options logging 'org-log-done))
  12916. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  12917. (this (match-string 1))
  12918. (hl-pos (match-beginning 0))
  12919. (head (org-get-todo-sequence-head this))
  12920. (ass (assoc head org-todo-kwd-alist))
  12921. (interpret (nth 1 ass))
  12922. (done-word (nth 3 ass))
  12923. (final-done-word (nth 4 ass))
  12924. (last-state (or this ""))
  12925. (completion-ignore-case t)
  12926. (member (member this org-todo-keywords-1))
  12927. (tail (cdr member))
  12928. (state (cond
  12929. ((and org-todo-key-trigger
  12930. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  12931. (and (not arg) org-use-fast-todo-selection
  12932. (not (eq org-use-fast-todo-selection 'prefix)))))
  12933. ;; Use fast selection
  12934. (org-fast-todo-selection))
  12935. ((and (equal arg '(4))
  12936. (or (not org-use-fast-todo-selection)
  12937. (not org-todo-key-trigger)))
  12938. ;; Read a state with completion
  12939. (completing-read "State: " (mapcar (lambda(x) (list x))
  12940. org-todo-keywords-1)
  12941. nil t))
  12942. ((eq arg 'right)
  12943. (if this
  12944. (if tail (car tail) nil)
  12945. (car org-todo-keywords-1)))
  12946. ((eq arg 'left)
  12947. (if (equal member org-todo-keywords-1)
  12948. nil
  12949. (if this
  12950. (nth (- (length org-todo-keywords-1) (length tail) 2)
  12951. org-todo-keywords-1)
  12952. (org-last org-todo-keywords-1))))
  12953. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  12954. (setq arg nil))) ; hack to fall back to cycling
  12955. (arg
  12956. ;; user or caller requests a specific state
  12957. (cond
  12958. ((equal arg "") nil)
  12959. ((eq arg 'none) nil)
  12960. ((eq arg 'done) (or done-word (car org-done-keywords)))
  12961. ((eq arg 'nextset)
  12962. (or (car (cdr (member head org-todo-heads)))
  12963. (car org-todo-heads)))
  12964. ((eq arg 'previousset)
  12965. (let ((org-todo-heads (reverse org-todo-heads)))
  12966. (or (car (cdr (member head org-todo-heads)))
  12967. (car org-todo-heads))))
  12968. ((car (member arg org-todo-keywords-1)))
  12969. ((nth (1- (prefix-numeric-value arg))
  12970. org-todo-keywords-1))))
  12971. ((null member) (or head (car org-todo-keywords-1)))
  12972. ((equal this final-done-word) nil) ;; -> make empty
  12973. ((null tail) nil) ;; -> first entry
  12974. ((eq interpret 'sequence)
  12975. (car tail))
  12976. ((memq interpret '(type priority))
  12977. (if (eq this-command last-command)
  12978. (car tail)
  12979. (if (> (length tail) 0)
  12980. (or done-word (car org-done-keywords))
  12981. nil)))
  12982. (t nil)))
  12983. (next (if state (concat " " state " ") " "))
  12984. (change-plist (list :type 'todo-state-change :from this :to state
  12985. :position startpos))
  12986. dostates)
  12987. (when org-blocker-hook
  12988. (unless (save-excursion
  12989. (save-match-data
  12990. (run-hook-with-args-until-failure
  12991. 'org-blocker-hook change-plist)))
  12992. (if (interactive-p)
  12993. (error "TODO state change from %s to %s blocked" this state)
  12994. ;; fail silently
  12995. (message "TODO state change from %s to %s blocked" this state)
  12996. (throw 'exit nil))))
  12997. (store-match-data match-data)
  12998. (replace-match next t t)
  12999. (unless (pos-visible-in-window-p hl-pos)
  13000. (message "TODO state changed to %s" (org-trim next)))
  13001. (unless head
  13002. (setq head (org-get-todo-sequence-head state)
  13003. ass (assoc head org-todo-kwd-alist)
  13004. interpret (nth 1 ass)
  13005. done-word (nth 3 ass)
  13006. final-done-word (nth 4 ass)))
  13007. (when (memq arg '(nextset previousset))
  13008. (message "Keyword-Set %d/%d: %s"
  13009. (- (length org-todo-sets) -1
  13010. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13011. (length org-todo-sets)
  13012. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13013. (setq org-last-todo-state-is-todo
  13014. (not (member state org-done-keywords)))
  13015. (when (and org-log-done (not (memq arg '(nextset previousset))))
  13016. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  13017. (or (not org-todo-log-states)
  13018. (member state org-todo-log-states))))
  13019. (cond
  13020. ((and state (member state org-not-done-keywords)
  13021. (not (member this org-not-done-keywords)))
  13022. ;; This is now a todo state and was not one before
  13023. ;; Remove any CLOSED timestamp, and possibly log the state change
  13024. (org-add-planning-info nil nil 'closed)
  13025. (and dostates (org-add-log-maybe 'state state 'findpos)))
  13026. ((and state dostates)
  13027. ;; This is a non-nil state, and we need to log it
  13028. (org-add-log-maybe 'state state 'findpos))
  13029. ((and (member state org-done-keywords)
  13030. (not (member this org-done-keywords)))
  13031. ;; It is now done, and it was not done before
  13032. (org-add-planning-info 'closed (org-current-time))
  13033. (org-add-log-maybe 'done state 'findpos))))
  13034. ;; Fixup tag positioning
  13035. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13036. (run-hooks 'org-after-todo-state-change-hook)
  13037. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  13038. (if (and arg (not (member state org-done-keywords)))
  13039. (setq head (org-get-todo-sequence-head state)))
  13040. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13041. ;; Fixup cursor location if close to the keyword
  13042. (if (and (outline-on-heading-p)
  13043. (not (bolp))
  13044. (save-excursion (beginning-of-line 1)
  13045. (looking-at org-todo-line-regexp))
  13046. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13047. (progn
  13048. (goto-char (or (match-end 2) (match-end 1)))
  13049. (just-one-space)))
  13050. (when org-trigger-hook
  13051. (save-excursion
  13052. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13053. (defun org-get-todo-sequence-head (kwd)
  13054. "Return the head of the TODO sequence to which KWD belongs.
  13055. If KWD is not set, check if there is a text property remembering the
  13056. right sequence."
  13057. (let (p)
  13058. (cond
  13059. ((not kwd)
  13060. (or (get-text-property (point-at-bol) 'org-todo-head)
  13061. (progn
  13062. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13063. nil (point-at-eol)))
  13064. (get-text-property p 'org-todo-head))))
  13065. ((not (member kwd org-todo-keywords-1))
  13066. (car org-todo-keywords-1))
  13067. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13068. (defun org-fast-todo-selection ()
  13069. "Fast TODO keyword selection with single keys.
  13070. Returns the new TODO keyword, or nil if no state change should occur."
  13071. (let* ((fulltable org-todo-key-alist)
  13072. (done-keywords org-done-keywords) ;; needed for the faces.
  13073. (maxlen (apply 'max (mapcar
  13074. (lambda (x)
  13075. (if (stringp (car x)) (string-width (car x)) 0))
  13076. fulltable)))
  13077. (expert nil)
  13078. (fwidth (+ maxlen 3 1 3))
  13079. (ncol (/ (- (window-width) 4) fwidth))
  13080. tg cnt e c tbl
  13081. groups ingroup)
  13082. (save-window-excursion
  13083. (if expert
  13084. (set-buffer (get-buffer-create " *Org todo*"))
  13085. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13086. (erase-buffer)
  13087. (org-set-local 'org-done-keywords done-keywords)
  13088. (setq tbl fulltable cnt 0)
  13089. (while (setq e (pop tbl))
  13090. (cond
  13091. ((equal e '(:startgroup))
  13092. (push '() groups) (setq ingroup t)
  13093. (when (not (= cnt 0))
  13094. (setq cnt 0)
  13095. (insert "\n"))
  13096. (insert "{ "))
  13097. ((equal e '(:endgroup))
  13098. (setq ingroup nil cnt 0)
  13099. (insert "}\n"))
  13100. (t
  13101. (setq tg (car e) c (cdr e))
  13102. (if ingroup (push tg (car groups)))
  13103. (setq tg (org-add-props tg nil 'face
  13104. (org-get-todo-face tg)))
  13105. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13106. (insert "[" c "] " tg (make-string
  13107. (- fwidth 4 (length tg)) ?\ ))
  13108. (when (= (setq cnt (1+ cnt)) ncol)
  13109. (insert "\n")
  13110. (if ingroup (insert " "))
  13111. (setq cnt 0)))))
  13112. (insert "\n")
  13113. (goto-char (point-min))
  13114. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13115. (fit-window-to-buffer))
  13116. (message "[a-z..]:Set [SPC]:clear")
  13117. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13118. (cond
  13119. ((or (= c ?\C-g)
  13120. (and (= c ?q) (not (rassoc c fulltable))))
  13121. (setq quit-flag t))
  13122. ((= c ?\ ) nil)
  13123. ((setq e (rassoc c fulltable) tg (car e))
  13124. tg)
  13125. (t (setq quit-flag t))))))
  13126. (defun org-get-repeat ()
  13127. "Check if tere is a deadline/schedule with repeater in this entry."
  13128. (save-match-data
  13129. (save-excursion
  13130. (org-back-to-heading t)
  13131. (if (re-search-forward
  13132. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13133. (match-string 1)))))
  13134. (defvar org-last-changed-timestamp)
  13135. (defvar org-log-post-message)
  13136. (defun org-auto-repeat-maybe ()
  13137. "Check if the current headline contains a repeated deadline/schedule.
  13138. If yes, set TODO state back to what it was and change the base date
  13139. of repeating deadline/scheduled time stamps to new date.
  13140. This function should be run in the `org-after-todo-state-change-hook'."
  13141. ;; last-state is dynamically scoped into this function
  13142. (let* ((repeat (org-get-repeat))
  13143. (aa (assoc last-state org-todo-kwd-alist))
  13144. (interpret (nth 1 aa))
  13145. (head (nth 2 aa))
  13146. (done-word (nth 3 aa))
  13147. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13148. (msg "Entry repeats: ")
  13149. (org-log-done)
  13150. re type n what ts)
  13151. (when repeat
  13152. (org-todo (if (eq interpret 'type) last-state head))
  13153. (when (and org-log-repeat
  13154. (not (memq 'org-add-log-note
  13155. (default-value 'post-command-hook))))
  13156. ;; Make sure a note is taken
  13157. (let ((org-log-done '(done)))
  13158. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  13159. 'findpos)))
  13160. (org-back-to-heading t)
  13161. (org-add-planning-info nil nil 'closed)
  13162. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13163. org-deadline-time-regexp "\\)"))
  13164. (while (re-search-forward
  13165. re (save-excursion (outline-next-heading) (point)) t)
  13166. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  13167. ts (match-string (if (match-end 2) 2 4)))
  13168. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13169. (setq n (string-to-number (match-string 1 ts))
  13170. what (match-string 2 ts))
  13171. (if (equal what "w") (setq n (* n 7) what "d"))
  13172. (org-timestamp-change n (cdr (assoc what whata))))
  13173. (setq msg (concat msg type org-last-changed-timestamp " ")))
  13174. (setq org-log-post-message msg)
  13175. (message "%s" msg))))
  13176. (defun org-show-todo-tree (arg)
  13177. "Make a compact tree which shows all headlines marked with TODO.
  13178. The tree will show the lines where the regexp matches, and all higher
  13179. headlines above the match.
  13180. With \\[universal-argument] prefix, also show the DONE entries.
  13181. With a numeric prefix N, construct a sparse tree for the Nth element
  13182. of `org-todo-keywords-1'."
  13183. (interactive "P")
  13184. (let ((case-fold-search nil)
  13185. (kwd-re
  13186. (cond ((null arg) org-not-done-regexp)
  13187. ((equal arg '(4))
  13188. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13189. (mapcar 'list org-todo-keywords-1))))
  13190. (concat "\\("
  13191. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13192. "\\)\\>")))
  13193. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13194. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13195. org-todo-keywords-1)))
  13196. (t (error "Invalid prefix argument: %s" arg)))))
  13197. (message "%d TODO entries found"
  13198. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13199. (defun org-deadline (&optional remove)
  13200. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13201. With argument REMOVE, remove any deadline from the item."
  13202. (interactive "P")
  13203. (if remove
  13204. (progn
  13205. (org-add-planning-info nil nil 'deadline)
  13206. (message "Item no longer has a deadline."))
  13207. (org-add-planning-info 'deadline nil 'closed)))
  13208. (defun org-schedule (&optional remove)
  13209. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13210. With argument REMOVE, remove any scheduling date from the item."
  13211. (interactive "P")
  13212. (if remove
  13213. (progn
  13214. (org-add-planning-info nil nil 'scheduled)
  13215. (message "Item is no longer scheduled."))
  13216. (org-add-planning-info 'scheduled nil 'closed)))
  13217. (defun org-add-planning-info (what &optional time &rest remove)
  13218. "Insert new timestamp with keyword in the line directly after the headline.
  13219. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13220. If non is given, the user is prompted for a date.
  13221. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13222. be removed."
  13223. (interactive)
  13224. (let (org-time-was-given org-end-time-was-given)
  13225. (when what (setq time (or time (org-read-date nil 'to-time))))
  13226. (when (and org-insert-labeled-timestamps-at-point
  13227. (member what '(scheduled deadline)))
  13228. (insert
  13229. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13230. (org-insert-time-stamp time org-time-was-given
  13231. nil nil nil (list org-end-time-was-given))
  13232. (setq what nil))
  13233. (save-excursion
  13234. (save-restriction
  13235. (let (col list elt ts buffer-invisibility-spec)
  13236. (org-back-to-heading t)
  13237. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13238. (goto-char (match-end 1))
  13239. (setq col (current-column))
  13240. (goto-char (match-end 0))
  13241. (if (eobp) (insert "\n") (forward-char 1))
  13242. (if (and (not (looking-at outline-regexp))
  13243. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13244. "[^\r\n]*"))
  13245. (not (equal (match-string 1) org-clock-string)))
  13246. (narrow-to-region (match-beginning 0) (match-end 0))
  13247. (insert-before-markers "\n")
  13248. (backward-char 1)
  13249. (narrow-to-region (point) (point))
  13250. (indent-to-column col))
  13251. ;; Check if we have to remove something.
  13252. (setq list (cons what remove))
  13253. (while list
  13254. (setq elt (pop list))
  13255. (goto-char (point-min))
  13256. (when (or (and (eq elt 'scheduled)
  13257. (re-search-forward org-scheduled-time-regexp nil t))
  13258. (and (eq elt 'deadline)
  13259. (re-search-forward org-deadline-time-regexp nil t))
  13260. (and (eq elt 'closed)
  13261. (re-search-forward org-closed-time-regexp nil t)))
  13262. (replace-match "")
  13263. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13264. (if (looking-at " +") (replace-match ""))))
  13265. (goto-char (point-max))
  13266. (when what
  13267. (insert
  13268. (if (not (equal (char-before) ?\ )) " " "")
  13269. (cond ((eq what 'scheduled) org-scheduled-string)
  13270. ((eq what 'deadline) org-deadline-string)
  13271. ((eq what 'closed) org-closed-string))
  13272. " ")
  13273. (setq ts (org-insert-time-stamp
  13274. time
  13275. (or org-time-was-given
  13276. (and (eq what 'closed) org-log-done-with-time))
  13277. (eq what 'closed)
  13278. nil nil (list org-end-time-was-given)))
  13279. (end-of-line 1))
  13280. (goto-char (point-min))
  13281. (widen)
  13282. (if (looking-at "[ \t]+\r?\n")
  13283. (replace-match ""))
  13284. ts)))))
  13285. (defvar org-log-note-marker (make-marker))
  13286. (defvar org-log-note-purpose nil)
  13287. (defvar org-log-note-state nil)
  13288. (defvar org-log-note-window-configuration nil)
  13289. (defvar org-log-note-return-to (make-marker))
  13290. (defvar org-log-post-message nil
  13291. "Message to be displayed after a log note has been stored.
  13292. The auto-repeater uses this.")
  13293. (defun org-add-log-maybe (&optional purpose state findpos)
  13294. "Set up the post command hook to take a note."
  13295. (save-excursion
  13296. (when (and (listp org-log-done)
  13297. (memq purpose org-log-done))
  13298. (when findpos
  13299. (org-back-to-heading t)
  13300. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13301. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13302. "[^\r\n]*\\)?"))
  13303. (goto-char (match-end 0))
  13304. (unless org-log-states-order-reversed
  13305. (and (= (char-after) ?\n) (forward-char 1))
  13306. (org-skip-over-state-notes)
  13307. (skip-chars-backward " \t\n\r")))
  13308. (move-marker org-log-note-marker (point))
  13309. (setq org-log-note-purpose purpose)
  13310. (setq org-log-note-state state)
  13311. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  13312. (defun org-skip-over-state-notes ()
  13313. "Skip past the list of State notes in an entry."
  13314. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13315. (while (looking-at "[ \t]*- State")
  13316. (condition-case nil
  13317. (org-next-item)
  13318. (error (org-end-of-item)))))
  13319. (defun org-add-log-note (&optional purpose)
  13320. "Pop up a window for taking a note, and add this note later at point."
  13321. (remove-hook 'post-command-hook 'org-add-log-note)
  13322. (setq org-log-note-window-configuration (current-window-configuration))
  13323. (delete-other-windows)
  13324. (move-marker org-log-note-return-to (point))
  13325. (switch-to-buffer (marker-buffer org-log-note-marker))
  13326. (goto-char org-log-note-marker)
  13327. (org-switch-to-buffer-other-window "*Org Note*")
  13328. (erase-buffer)
  13329. (let ((org-inhibit-startup t)) (org-mode))
  13330. (insert (format "# Insert note for %s.
  13331. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13332. (cond
  13333. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13334. ((eq org-log-note-purpose 'done) "closed todo item")
  13335. ((eq org-log-note-purpose 'state)
  13336. (format "state change to \"%s\"" org-log-note-state))
  13337. (t (error "This should not happen")))))
  13338. (org-set-local 'org-finish-function 'org-store-log-note))
  13339. (defun org-store-log-note ()
  13340. "Finish taking a log note, and insert it to where it belongs."
  13341. (let ((txt (buffer-string))
  13342. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13343. lines ind)
  13344. (kill-buffer (current-buffer))
  13345. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13346. (setq txt (replace-match "" t t txt)))
  13347. (if (string-match "\\s-+\\'" txt)
  13348. (setq txt (replace-match "" t t txt)))
  13349. (setq lines (org-split-string txt "\n"))
  13350. (when (and note (string-match "\\S-" note))
  13351. (setq note
  13352. (org-replace-escapes
  13353. note
  13354. (list (cons "%u" (user-login-name))
  13355. (cons "%U" user-full-name)
  13356. (cons "%t" (format-time-string
  13357. (org-time-stamp-format 'long 'inactive)
  13358. (current-time)))
  13359. (cons "%s" (if org-log-note-state
  13360. (concat "\"" org-log-note-state "\"")
  13361. "")))))
  13362. (if lines (setq note (concat note " \\\\")))
  13363. (push note lines))
  13364. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13365. (when lines
  13366. (save-excursion
  13367. (set-buffer (marker-buffer org-log-note-marker))
  13368. (save-excursion
  13369. (goto-char org-log-note-marker)
  13370. (move-marker org-log-note-marker nil)
  13371. (end-of-line 1)
  13372. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13373. (indent-relative nil)
  13374. (insert "- " (pop lines))
  13375. (org-indent-line-function)
  13376. (beginning-of-line 1)
  13377. (looking-at "[ \t]*")
  13378. (setq ind (concat (match-string 0) " "))
  13379. (end-of-line 1)
  13380. (while lines (insert "\n" ind (pop lines)))))))
  13381. (set-window-configuration org-log-note-window-configuration)
  13382. (with-current-buffer (marker-buffer org-log-note-return-to)
  13383. (goto-char org-log-note-return-to))
  13384. (move-marker org-log-note-return-to nil)
  13385. (and org-log-post-message (message "%s" org-log-post-message)))
  13386. ;; FIXME: what else would be useful?
  13387. ;; - priority
  13388. ;; - date
  13389. (defun org-sparse-tree (&optional arg)
  13390. "Create a sparse tree, prompt for the details.
  13391. This command can create sparse trees. You first need to select the type
  13392. of match used to create the tree:
  13393. t Show entries with a specific TODO keyword.
  13394. T Show entries selected by a tags match.
  13395. p Enter a property name and its value (both with completion on existing
  13396. names/values) and show entries with that property.
  13397. r Show entries matching a regular expression
  13398. d Show deadlines due within `org-deadline-warning-days'."
  13399. (interactive "P")
  13400. (let (ans kwd value)
  13401. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13402. (setq ans (read-char-exclusive))
  13403. (cond
  13404. ((equal ans ?d)
  13405. (call-interactively 'org-check-deadlines))
  13406. ((equal ans ?b)
  13407. (call-interactively 'org-check-before-date))
  13408. ((equal ans ?t)
  13409. (org-show-todo-tree '(4)))
  13410. ((equal ans ?T)
  13411. (call-interactively 'org-tags-sparse-tree))
  13412. ((member ans '(?p ?P))
  13413. (setq kwd (completing-read "Property: "
  13414. (mapcar 'list (org-buffer-property-keys))))
  13415. (setq value (completing-read "Value: "
  13416. (mapcar 'list (org-property-values kwd))))
  13417. (unless (string-match "\\`{.*}\\'" value)
  13418. (setq value (concat "\"" value "\"")))
  13419. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13420. ((member ans '(?r ?R ?/))
  13421. (call-interactively 'org-occur))
  13422. (t (error "No such sparse tree command \"%c\"" ans)))))
  13423. (defvar org-occur-highlights nil)
  13424. (make-variable-buffer-local 'org-occur-highlights)
  13425. (defun org-occur (regexp &optional keep-previous callback)
  13426. "Make a compact tree which shows all matches of REGEXP.
  13427. The tree will show the lines where the regexp matches, and all higher
  13428. headlines above the match. It will also show the heading after the match,
  13429. to make sure editing the matching entry is easy.
  13430. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13431. call to `org-occur' will be kept, to allow stacking of calls to this
  13432. command.
  13433. If CALLBACK is non-nil, it is a function which is called to confirm
  13434. that the match should indeed be shown."
  13435. (interactive "sRegexp: \nP")
  13436. (or keep-previous (org-remove-occur-highlights nil nil t))
  13437. (let ((cnt 0))
  13438. (save-excursion
  13439. (goto-char (point-min))
  13440. (if (or (not keep-previous) ; do not want to keep
  13441. (not org-occur-highlights)) ; no previous matches
  13442. ;; hide everything
  13443. (org-overview))
  13444. (while (re-search-forward regexp nil t)
  13445. (when (or (not callback)
  13446. (save-match-data (funcall callback)))
  13447. (setq cnt (1+ cnt))
  13448. (when org-highlight-sparse-tree-matches
  13449. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13450. (org-show-context 'occur-tree))))
  13451. (when org-remove-highlights-with-change
  13452. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13453. nil 'local))
  13454. (unless org-sparse-tree-open-archived-trees
  13455. (org-hide-archived-subtrees (point-min) (point-max)))
  13456. (run-hooks 'org-occur-hook)
  13457. (if (interactive-p)
  13458. (message "%d match(es) for regexp %s" cnt regexp))
  13459. cnt))
  13460. (defun org-show-context (&optional key)
  13461. "Make sure point and context and visible.
  13462. How much context is shown depends upon the variables
  13463. `org-show-hierarchy-above', `org-show-following-heading'. and
  13464. `org-show-siblings'."
  13465. (let ((heading-p (org-on-heading-p t))
  13466. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13467. (following-p (org-get-alist-option org-show-following-heading key))
  13468. (entry-p (org-get-alist-option org-show-entry-below key))
  13469. (siblings-p (org-get-alist-option org-show-siblings key)))
  13470. (catch 'exit
  13471. ;; Show heading or entry text
  13472. (if (and heading-p (not entry-p))
  13473. (org-flag-heading nil) ; only show the heading
  13474. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13475. (org-show-hidden-entry))) ; show entire entry
  13476. (when following-p
  13477. ;; Show next sibling, or heading below text
  13478. (save-excursion
  13479. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13480. (org-flag-heading nil))))
  13481. (when siblings-p (org-show-siblings))
  13482. (when hierarchy-p
  13483. ;; show all higher headings, possibly with siblings
  13484. (save-excursion
  13485. (while (and (condition-case nil
  13486. (progn (org-up-heading-all 1) t)
  13487. (error nil))
  13488. (not (bobp)))
  13489. (org-flag-heading nil)
  13490. (when siblings-p (org-show-siblings))))))))
  13491. (defun org-reveal (&optional siblings)
  13492. "Show current entry, hierarchy above it, and the following headline.
  13493. This can be used to show a consistent set of context around locations
  13494. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13495. not t for the search context.
  13496. With optional argument SIBLINGS, on each level of the hierarchy all
  13497. siblings are shown. This repairs the tree structure to what it would
  13498. look like when opened with hierarchical calls to `org-cycle'."
  13499. (interactive "P")
  13500. (let ((org-show-hierarchy-above t)
  13501. (org-show-following-heading t)
  13502. (org-show-siblings (if siblings t org-show-siblings)))
  13503. (org-show-context nil)))
  13504. (defun org-highlight-new-match (beg end)
  13505. "Highlight from BEG to END and mark the highlight is an occur headline."
  13506. (let ((ov (org-make-overlay beg end)))
  13507. (org-overlay-put ov 'face 'secondary-selection)
  13508. (push ov org-occur-highlights)))
  13509. (defun org-remove-occur-highlights (&optional beg end noremove)
  13510. "Remove the occur highlights from the buffer.
  13511. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13512. from the `before-change-functions' in the current buffer."
  13513. (interactive)
  13514. (unless org-inhibit-highlight-removal
  13515. (mapc 'org-delete-overlay org-occur-highlights)
  13516. (setq org-occur-highlights nil)
  13517. (unless noremove
  13518. (remove-hook 'before-change-functions
  13519. 'org-remove-occur-highlights 'local))))
  13520. ;;;; Priorities
  13521. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13522. "Regular expression matching the priority indicator.")
  13523. (defvar org-remove-priority-next-time nil)
  13524. (defun org-priority-up ()
  13525. "Increase the priority of the current item."
  13526. (interactive)
  13527. (org-priority 'up))
  13528. (defun org-priority-down ()
  13529. "Decrease the priority of the current item."
  13530. (interactive)
  13531. (org-priority 'down))
  13532. (defun org-priority (&optional action)
  13533. "Change the priority of an item by ARG.
  13534. ACTION can be `set', `up', `down', or a character."
  13535. (interactive)
  13536. (setq action (or action 'set))
  13537. (let (current new news have remove)
  13538. (save-excursion
  13539. (org-back-to-heading)
  13540. (if (looking-at org-priority-regexp)
  13541. (setq current (string-to-char (match-string 2))
  13542. have t)
  13543. (setq current org-default-priority))
  13544. (cond
  13545. ((or (eq action 'set) (integerp action))
  13546. (if (integerp action)
  13547. (setq new action)
  13548. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13549. (setq new (read-char-exclusive)))
  13550. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13551. (= (upcase org-lowest-priority) org-lowest-priority))
  13552. (setq new (upcase new)))
  13553. (cond ((equal new ?\ ) (setq remove t))
  13554. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13555. (error "Priority must be between `%c' and `%c'"
  13556. org-highest-priority org-lowest-priority))))
  13557. ((eq action 'up)
  13558. (if (and (not have) (eq last-command this-command))
  13559. (setq new org-lowest-priority)
  13560. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13561. org-default-priority (1- current)))))
  13562. ((eq action 'down)
  13563. (if (and (not have) (eq last-command this-command))
  13564. (setq new org-highest-priority)
  13565. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13566. org-default-priority (1+ current)))))
  13567. (t (error "Invalid action")))
  13568. (if (or (< (upcase new) org-highest-priority)
  13569. (> (upcase new) org-lowest-priority))
  13570. (setq remove t))
  13571. (setq news (format "%c" new))
  13572. (if have
  13573. (if remove
  13574. (replace-match "" t t nil 1)
  13575. (replace-match news t t nil 2))
  13576. (if remove
  13577. (error "No priority cookie found in line")
  13578. (looking-at org-todo-line-regexp)
  13579. (if (match-end 2)
  13580. (progn
  13581. (goto-char (match-end 2))
  13582. (insert " [#" news "]"))
  13583. (goto-char (match-beginning 3))
  13584. (insert "[#" news "] ")))))
  13585. (org-preserve-lc (org-set-tags nil 'align))
  13586. (if remove
  13587. (message "Priority removed")
  13588. (message "Priority of current item set to %s" news))))
  13589. (defun org-get-priority (s)
  13590. "Find priority cookie and return priority."
  13591. (save-match-data
  13592. (if (not (string-match org-priority-regexp s))
  13593. (* 1000 (- org-lowest-priority org-default-priority))
  13594. (* 1000 (- org-lowest-priority
  13595. (string-to-char (match-string 2 s)))))))
  13596. ;;;; Tags
  13597. (defun org-scan-tags (action matcher &optional todo-only)
  13598. "Scan headline tags with inheritance and produce output ACTION.
  13599. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13600. evaluated, testing if a given set of tags qualifies a headline for
  13601. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13602. are included in the output."
  13603. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13604. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13605. (org-re
  13606. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13607. (props (list 'face nil
  13608. 'done-face 'org-done
  13609. 'undone-face nil
  13610. 'mouse-face 'highlight
  13611. 'org-not-done-regexp org-not-done-regexp
  13612. 'org-todo-regexp org-todo-regexp
  13613. 'keymap org-agenda-keymap
  13614. 'help-echo
  13615. (format "mouse-2 or RET jump to org file %s"
  13616. (abbreviate-file-name
  13617. (or (buffer-file-name (buffer-base-buffer))
  13618. (buffer-name (buffer-base-buffer)))))))
  13619. (case-fold-search nil)
  13620. lspos
  13621. tags tags-list tags-alist (llast 0) rtn level category i txt
  13622. todo marker entry priority)
  13623. (save-excursion
  13624. (goto-char (point-min))
  13625. (when (eq action 'sparse-tree)
  13626. (org-overview)
  13627. (org-remove-occur-highlights))
  13628. (while (re-search-forward re nil t)
  13629. (catch :skip
  13630. (setq todo (if (match-end 1) (match-string 2))
  13631. tags (if (match-end 4) (match-string 4)))
  13632. (goto-char (setq lspos (1+ (match-beginning 0))))
  13633. (setq level (org-reduced-level (funcall outline-level))
  13634. category (org-get-category))
  13635. (setq i llast llast level)
  13636. ;; remove tag lists from same and sublevels
  13637. (while (>= i level)
  13638. (when (setq entry (assoc i tags-alist))
  13639. (setq tags-alist (delete entry tags-alist)))
  13640. (setq i (1- i)))
  13641. ;; add the nex tags
  13642. (when tags
  13643. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13644. tags-alist
  13645. (cons (cons level tags) tags-alist)))
  13646. ;; compile tags for current headline
  13647. (setq tags-list
  13648. (if org-use-tag-inheritance
  13649. (apply 'append (mapcar 'cdr tags-alist))
  13650. tags))
  13651. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13652. (eval matcher)
  13653. (or (not org-agenda-skip-archived-trees)
  13654. (not (member org-archive-tag tags-list))))
  13655. (and (eq action 'agenda) (org-agenda-skip))
  13656. ;; list this headline
  13657. (if (eq action 'sparse-tree)
  13658. (progn
  13659. (and org-highlight-sparse-tree-matches
  13660. (org-get-heading) (match-end 0)
  13661. (org-highlight-new-match
  13662. (match-beginning 0) (match-beginning 1)))
  13663. (org-show-context 'tags-tree))
  13664. (setq txt (org-format-agenda-item
  13665. ""
  13666. (concat
  13667. (if org-tags-match-list-sublevels
  13668. (make-string (1- level) ?.) "")
  13669. (org-get-heading))
  13670. category tags-list)
  13671. priority (org-get-priority txt))
  13672. (goto-char lspos)
  13673. (setq marker (org-agenda-new-marker))
  13674. (org-add-props txt props
  13675. 'org-marker marker 'org-hd-marker marker 'org-category category
  13676. 'priority priority 'type "tagsmatch")
  13677. (push txt rtn))
  13678. ;; if we are to skip sublevels, jump to end of subtree
  13679. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13680. (when (and (eq action 'sparse-tree)
  13681. (not org-sparse-tree-open-archived-trees))
  13682. (org-hide-archived-subtrees (point-min) (point-max)))
  13683. (nreverse rtn)))
  13684. (defvar todo-only) ;; dynamically scoped
  13685. (defun org-tags-sparse-tree (&optional todo-only match)
  13686. "Create a sparse tree according to tags string MATCH.
  13687. MATCH can contain positive and negative selection of tags, like
  13688. \"+WORK+URGENT-WITHBOSS\".
  13689. If optional argument TODO_ONLY is non-nil, only select lines that are
  13690. also TODO lines."
  13691. (interactive "P")
  13692. (org-prepare-agenda-buffers (list (current-buffer)))
  13693. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13694. (defvar org-cached-props nil)
  13695. (defun org-cached-entry-get (pom property)
  13696. (if (or (eq t org-use-property-inheritance)
  13697. (member property org-use-property-inheritance))
  13698. ;; Caching is not possible, check it directly
  13699. (org-entry-get pom property 'inherit)
  13700. ;; Get all properties, so that we can do complicated checks easily
  13701. (cdr (assoc property (or org-cached-props
  13702. (setq org-cached-props
  13703. (org-entry-properties pom)))))))
  13704. (defun org-global-tags-completion-table (&optional files)
  13705. "Return the list of all tags in all agenda buffer/files."
  13706. (save-excursion
  13707. (org-uniquify
  13708. (apply 'append
  13709. (mapcar
  13710. (lambda (file)
  13711. (set-buffer (find-file-noselect file))
  13712. (org-get-buffer-tags))
  13713. (if (and files (car files))
  13714. files
  13715. (org-agenda-files)))))))
  13716. (defun org-make-tags-matcher (match)
  13717. "Create the TAGS//TODO matcher form for the selection string MATCH."
  13718. ;; todo-only is scoped dynamically into this function, and the function
  13719. ;; may change it it the matcher asksk for it.
  13720. (unless match
  13721. ;; Get a new match request, with completion
  13722. (let ((org-last-tags-completion-table
  13723. (org-global-tags-completion-table)))
  13724. (setq match (completing-read
  13725. "Match: " 'org-tags-completion-function nil nil nil
  13726. 'org-tags-history))))
  13727. ;; Parse the string and create a lisp form
  13728. (let ((match0 match)
  13729. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  13730. minus tag mm
  13731. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13732. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  13733. (if (string-match "/+" match)
  13734. ;; match contains also a todo-matching request
  13735. (progn
  13736. (setq tagsmatch (substring match 0 (match-beginning 0))
  13737. todomatch (substring match (match-end 0)))
  13738. (if (string-match "^!" todomatch)
  13739. (setq todo-only t todomatch (substring todomatch 1)))
  13740. (if (string-match "^\\s-*$" todomatch)
  13741. (setq todomatch nil)))
  13742. ;; only matching tags
  13743. (setq tagsmatch match todomatch nil))
  13744. ;; Make the tags matcher
  13745. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13746. (setq tagsmatcher t)
  13747. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13748. (while (setq term (pop orterms))
  13749. (while (and (equal (substring term -1) "\\") orterms)
  13750. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13751. (while (string-match re term)
  13752. (setq minus (and (match-end 1)
  13753. (equal (match-string 1 term) "-"))
  13754. tag (match-string 2 term)
  13755. re-p (equal (string-to-char tag) ?{)
  13756. level-p (match-end 3)
  13757. prop-p (match-end 4)
  13758. mm (cond
  13759. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13760. (level-p `(= level ,(string-to-number
  13761. (match-string 3 term))))
  13762. (prop-p
  13763. (setq pn (match-string 4 term)
  13764. pv (match-string 5 term)
  13765. cat-p (equal pn "CATEGORY")
  13766. re-p (equal (string-to-char pv) ?{)
  13767. pv (substring pv 1 -1))
  13768. (if (equal pn "CATEGORY")
  13769. (setq gv '(get-text-property (point) 'org-category))
  13770. (setq gv `(org-cached-entry-get nil ,pn)))
  13771. (if re-p
  13772. `(string-match ,pv (or ,gv ""))
  13773. `(equal ,pv ,gv)))
  13774. (t `(member ,(downcase tag) tags-list)))
  13775. mm (if minus (list 'not mm) mm)
  13776. term (substring term (match-end 0)))
  13777. (push mm tagsmatcher))
  13778. (push (if (> (length tagsmatcher) 1)
  13779. (cons 'and tagsmatcher)
  13780. (car tagsmatcher))
  13781. orlist)
  13782. (setq tagsmatcher nil))
  13783. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13784. (setq tagsmatcher
  13785. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13786. ;; Make the todo matcher
  13787. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13788. (setq todomatcher t)
  13789. (setq orterms (org-split-string todomatch "|") orlist nil)
  13790. (while (setq term (pop orterms))
  13791. (while (string-match re term)
  13792. (setq minus (and (match-end 1)
  13793. (equal (match-string 1 term) "-"))
  13794. kwd (match-string 2 term)
  13795. re-p (equal (string-to-char kwd) ?{)
  13796. term (substring term (match-end 0))
  13797. mm (if re-p
  13798. `(string-match ,(substring kwd 1 -1) todo)
  13799. (list 'equal 'todo kwd))
  13800. mm (if minus (list 'not mm) mm))
  13801. (push mm todomatcher))
  13802. (push (if (> (length todomatcher) 1)
  13803. (cons 'and todomatcher)
  13804. (car todomatcher))
  13805. orlist)
  13806. (setq todomatcher nil))
  13807. (setq todomatcher (if (> (length orlist) 1)
  13808. (cons 'or orlist) (car orlist))))
  13809. ;; Return the string and lisp forms of the matcher
  13810. (setq matcher (if todomatcher
  13811. (list 'and tagsmatcher todomatcher)
  13812. tagsmatcher))
  13813. (cons match0 matcher)))
  13814. (defun org-match-any-p (re list)
  13815. "Does re match any element of list?"
  13816. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13817. (delq nil list))
  13818. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  13819. (defvar org-tags-overlay (org-make-overlay 1 1))
  13820. (org-detach-overlay org-tags-overlay)
  13821. (defun org-align-tags-here (to-col)
  13822. ;; Assumes that this is a headline
  13823. (let ((pos (point)) (col (current-column)) tags)
  13824. (beginning-of-line 1)
  13825. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13826. (< pos (match-beginning 2)))
  13827. (progn
  13828. (setq tags (match-string 2))
  13829. (goto-char (match-beginning 1))
  13830. (insert " ")
  13831. (delete-region (point) (1+ (match-end 0)))
  13832. (backward-char 1)
  13833. (move-to-column
  13834. (max (1+ (current-column))
  13835. (1+ col)
  13836. (if (> to-col 0)
  13837. to-col
  13838. (- (abs to-col) (length tags))))
  13839. t)
  13840. (insert tags)
  13841. (move-to-column (min (current-column) col) t))
  13842. (goto-char pos))))
  13843. (defun org-set-tags (&optional arg just-align)
  13844. "Set the tags for the current headline.
  13845. With prefix ARG, realign all tags in headings in the current buffer."
  13846. (interactive "P")
  13847. (let* ((re (concat "^" outline-regexp))
  13848. (current (org-get-tags-string))
  13849. (col (current-column))
  13850. (org-setting-tags t)
  13851. table current-tags inherited-tags ; computed below when needed
  13852. tags p0 c0 c1 rpl)
  13853. (if arg
  13854. (save-excursion
  13855. (goto-char (point-min))
  13856. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13857. (while (re-search-forward re nil t)
  13858. (org-set-tags nil t)
  13859. (end-of-line 1)))
  13860. (message "All tags realigned to column %d" org-tags-column))
  13861. (if just-align
  13862. (setq tags current)
  13863. ;; Get a new set of tags from the user
  13864. (save-excursion
  13865. (setq table (or org-tag-alist (org-get-buffer-tags))
  13866. org-last-tags-completion-table table
  13867. current-tags (org-split-string current ":")
  13868. inherited-tags (nreverse
  13869. (nthcdr (length current-tags)
  13870. (nreverse (org-get-tags-at))))
  13871. tags
  13872. (if (or (eq t org-use-fast-tag-selection)
  13873. (and org-use-fast-tag-selection
  13874. (delq nil (mapcar 'cdr table))))
  13875. (org-fast-tag-selection
  13876. current-tags inherited-tags table
  13877. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  13878. (let ((org-add-colon-after-tag-completion t))
  13879. (org-trim
  13880. (org-without-partial-completion
  13881. (completing-read "Tags: " 'org-tags-completion-function
  13882. nil nil current 'org-tags-history)))))))
  13883. (while (string-match "[-+&]+" tags)
  13884. ;; No boolean logic, just a list
  13885. (setq tags (replace-match ":" t t tags))))
  13886. (if (string-match "\\`[\t ]*\\'" tags)
  13887. (setq tags "")
  13888. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13889. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13890. ;; Insert new tags at the correct column
  13891. (beginning-of-line 1)
  13892. (cond
  13893. ((and (equal current "") (equal tags "")))
  13894. ((re-search-forward
  13895. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13896. (point-at-eol) t)
  13897. (if (equal tags "")
  13898. (setq rpl "")
  13899. (goto-char (match-beginning 0))
  13900. (setq c0 (current-column) p0 (point)
  13901. c1 (max (1+ c0) (if (> org-tags-column 0)
  13902. org-tags-column
  13903. (- (- org-tags-column) (length tags))))
  13904. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13905. (replace-match rpl t t)
  13906. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  13907. tags)
  13908. (t (error "Tags alignment failed")))
  13909. (move-to-column col)
  13910. (unless just-align
  13911. (run-hooks 'org-after-tags-change-hook)))))
  13912. (defun org-change-tag-in-region (beg end tag off)
  13913. "Add or remove TAG for each entry in the region.
  13914. This works in the agenda, and also in an org-mode buffer."
  13915. (interactive
  13916. (list (region-beginning) (region-end)
  13917. (let ((org-last-tags-completion-table
  13918. (if (org-mode-p)
  13919. (org-get-buffer-tags)
  13920. (org-global-tags-completion-table))))
  13921. (completing-read
  13922. "Tag: " 'org-tags-completion-function nil nil nil
  13923. 'org-tags-history))
  13924. (progn
  13925. (message "[s]et or [r]emove? ")
  13926. (equal (read-char-exclusive) ?r))))
  13927. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13928. (let ((agendap (equal major-mode 'org-agenda-mode))
  13929. l1 l2 m buf pos newhead (cnt 0))
  13930. (goto-char end)
  13931. (setq l2 (1- (org-current-line)))
  13932. (goto-char beg)
  13933. (setq l1 (org-current-line))
  13934. (loop for l from l1 to l2 do
  13935. (goto-line l)
  13936. (setq m (get-text-property (point) 'org-hd-marker))
  13937. (when (or (and (org-mode-p) (org-on-heading-p))
  13938. (and agendap m))
  13939. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13940. pos (if agendap m (point)))
  13941. (with-current-buffer buf
  13942. (save-excursion
  13943. (save-restriction
  13944. (goto-char pos)
  13945. (setq cnt (1+ cnt))
  13946. (org-toggle-tag tag (if off 'off 'on))
  13947. (setq newhead (org-get-heading)))))
  13948. (and agendap (org-agenda-change-all-lines newhead m))))
  13949. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13950. (defun org-tags-completion-function (string predicate &optional flag)
  13951. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13952. (confirm (lambda (x) (stringp (car x)))))
  13953. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  13954. (setq s1 (match-string 1 string)
  13955. s2 (match-string 2 string))
  13956. (setq s1 "" s2 string))
  13957. (cond
  13958. ((eq flag nil)
  13959. ;; try completion
  13960. (setq rtn (try-completion s2 ctable confirm))
  13961. (if (stringp rtn)
  13962. (setq rtn
  13963. (concat s1 s2 (substring rtn (length s2))
  13964. (if (and org-add-colon-after-tag-completion
  13965. (assoc rtn ctable))
  13966. ":" ""))))
  13967. rtn)
  13968. ((eq flag t)
  13969. ;; all-completions
  13970. (all-completions s2 ctable confirm)
  13971. )
  13972. ((eq flag 'lambda)
  13973. ;; exact match?
  13974. (assoc s2 ctable)))
  13975. ))
  13976. (defun org-fast-tag-insert (kwd tags face &optional end)
  13977. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  13978. (insert (format "%-12s" (concat kwd ":"))
  13979. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13980. (or end "")))
  13981. (defun org-fast-tag-show-exit (flag)
  13982. (save-excursion
  13983. (goto-line 3)
  13984. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13985. (replace-match ""))
  13986. (when flag
  13987. (end-of-line 1)
  13988. (move-to-column (- (window-width) 19) t)
  13989. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13990. (defun org-set-current-tags-overlay (current prefix)
  13991. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13992. (if (featurep 'xemacs)
  13993. (org-overlay-display org-tags-overlay (concat prefix s)
  13994. 'secondary-selection)
  13995. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13996. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13997. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13998. "Fast tag selection with single keys.
  13999. CURRENT is the current list of tags in the headline, INHERITED is the
  14000. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14001. possibly with grouping information. TODO-TABLE is a similar table with
  14002. TODO keywords, should these have keys assigned to them.
  14003. If the keys are nil, a-z are automatically assigned.
  14004. Returns the new tags string, or nil to not change the current settings."
  14005. (let* ((fulltable (append table todo-table))
  14006. (maxlen (apply 'max (mapcar
  14007. (lambda (x)
  14008. (if (stringp (car x)) (string-width (car x)) 0))
  14009. fulltable)))
  14010. (buf (current-buffer))
  14011. (expert (eq org-fast-tag-selection-single-key 'expert))
  14012. (buffer-tags nil)
  14013. (fwidth (+ maxlen 3 1 3))
  14014. (ncol (/ (- (window-width) 4) fwidth))
  14015. (i-face 'org-done)
  14016. (c-face 'org-todo)
  14017. tg cnt e c char c1 c2 ntable tbl rtn
  14018. ov-start ov-end ov-prefix
  14019. (exit-after-next org-fast-tag-selection-single-key)
  14020. (done-keywords org-done-keywords)
  14021. groups ingroup)
  14022. (save-excursion
  14023. (beginning-of-line 1)
  14024. (if (looking-at
  14025. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14026. (setq ov-start (match-beginning 1)
  14027. ov-end (match-end 1)
  14028. ov-prefix "")
  14029. (setq ov-start (1- (point-at-eol))
  14030. ov-end (1+ ov-start))
  14031. (skip-chars-forward "^\n\r")
  14032. (setq ov-prefix
  14033. (concat
  14034. (buffer-substring (1- (point)) (point))
  14035. (if (> (current-column) org-tags-column)
  14036. " "
  14037. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14038. (org-move-overlay org-tags-overlay ov-start ov-end)
  14039. (save-window-excursion
  14040. (if expert
  14041. (set-buffer (get-buffer-create " *Org tags*"))
  14042. (delete-other-windows)
  14043. (split-window-vertically)
  14044. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14045. (erase-buffer)
  14046. (org-set-local 'org-done-keywords done-keywords)
  14047. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14048. (org-fast-tag-insert "Current" current c-face "\n\n")
  14049. (org-fast-tag-show-exit exit-after-next)
  14050. (org-set-current-tags-overlay current ov-prefix)
  14051. (setq tbl fulltable char ?a cnt 0)
  14052. (while (setq e (pop tbl))
  14053. (cond
  14054. ((equal e '(:startgroup))
  14055. (push '() groups) (setq ingroup t)
  14056. (when (not (= cnt 0))
  14057. (setq cnt 0)
  14058. (insert "\n"))
  14059. (insert "{ "))
  14060. ((equal e '(:endgroup))
  14061. (setq ingroup nil cnt 0)
  14062. (insert "}\n"))
  14063. (t
  14064. (setq tg (car e) c2 nil)
  14065. (if (cdr e)
  14066. (setq c (cdr e))
  14067. ;; automatically assign a character.
  14068. (setq c1 (string-to-char
  14069. (downcase (substring
  14070. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14071. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14072. (while (or (rassoc char ntable) (rassoc char table))
  14073. (setq char (1+ char)))
  14074. (setq c2 c1))
  14075. (setq c (or c2 char)))
  14076. (if ingroup (push tg (car groups)))
  14077. (setq tg (org-add-props tg nil 'face
  14078. (cond
  14079. ((not (assoc tg table))
  14080. (org-get-todo-face tg))
  14081. ((member tg current) c-face)
  14082. ((member tg inherited) i-face)
  14083. (t nil))))
  14084. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14085. (insert "[" c "] " tg (make-string
  14086. (- fwidth 4 (length tg)) ?\ ))
  14087. (push (cons tg c) ntable)
  14088. (when (= (setq cnt (1+ cnt)) ncol)
  14089. (insert "\n")
  14090. (if ingroup (insert " "))
  14091. (setq cnt 0)))))
  14092. (setq ntable (nreverse ntable))
  14093. (insert "\n")
  14094. (goto-char (point-min))
  14095. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14096. (fit-window-to-buffer))
  14097. (setq rtn
  14098. (catch 'exit
  14099. (while t
  14100. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14101. (if groups " [!] no groups" " [!]groups")
  14102. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14103. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14104. (cond
  14105. ((= c ?\r) (throw 'exit t))
  14106. ((= c ?!)
  14107. (setq groups (not groups))
  14108. (goto-char (point-min))
  14109. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14110. ((= c ?\C-c)
  14111. (if (not expert)
  14112. (org-fast-tag-show-exit
  14113. (setq exit-after-next (not exit-after-next)))
  14114. (setq expert nil)
  14115. (delete-other-windows)
  14116. (split-window-vertically)
  14117. (org-switch-to-buffer-other-window " *Org tags*")
  14118. (and (fboundp 'fit-window-to-buffer)
  14119. (fit-window-to-buffer))))
  14120. ((or (= c ?\C-g)
  14121. (and (= c ?q) (not (rassoc c ntable))))
  14122. (org-detach-overlay org-tags-overlay)
  14123. (setq quit-flag t))
  14124. ((= c ?\ )
  14125. (setq current nil)
  14126. (if exit-after-next (setq exit-after-next 'now)))
  14127. ((= c ?\t)
  14128. (condition-case nil
  14129. (setq tg (completing-read
  14130. "Tag: "
  14131. (or buffer-tags
  14132. (with-current-buffer buf
  14133. (org-get-buffer-tags)))))
  14134. (quit (setq tg "")))
  14135. (when (string-match "\\S-" tg)
  14136. (add-to-list 'buffer-tags (list tg))
  14137. (if (member tg current)
  14138. (setq current (delete tg current))
  14139. (push tg current)))
  14140. (if exit-after-next (setq exit-after-next 'now)))
  14141. ((setq e (rassoc c todo-table) tg (car e))
  14142. (with-current-buffer buf
  14143. (save-excursion (org-todo tg)))
  14144. (if exit-after-next (setq exit-after-next 'now)))
  14145. ((setq e (rassoc c ntable) tg (car e))
  14146. (if (member tg current)
  14147. (setq current (delete tg current))
  14148. (loop for g in groups do
  14149. (if (member tg g)
  14150. (mapc (lambda (x)
  14151. (setq current (delete x current)))
  14152. g)))
  14153. (push tg current))
  14154. (if exit-after-next (setq exit-after-next 'now))))
  14155. ;; Create a sorted list
  14156. (setq current
  14157. (sort current
  14158. (lambda (a b)
  14159. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14160. (if (eq exit-after-next 'now) (throw 'exit t))
  14161. (goto-char (point-min))
  14162. (beginning-of-line 2)
  14163. (delete-region (point) (point-at-eol))
  14164. (org-fast-tag-insert "Current" current c-face)
  14165. (org-set-current-tags-overlay current ov-prefix)
  14166. (while (re-search-forward
  14167. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14168. (setq tg (match-string 1))
  14169. (add-text-properties
  14170. (match-beginning 1) (match-end 1)
  14171. (list 'face
  14172. (cond
  14173. ((member tg current) c-face)
  14174. ((member tg inherited) i-face)
  14175. (t (get-text-property (match-beginning 1) 'face))))))
  14176. (goto-char (point-min)))))
  14177. (org-detach-overlay org-tags-overlay)
  14178. (if rtn
  14179. (mapconcat 'identity current ":")
  14180. nil))))
  14181. (defun org-get-tags-string ()
  14182. "Get the TAGS string in the current headline."
  14183. (unless (org-on-heading-p t)
  14184. (error "Not on a heading"))
  14185. (save-excursion
  14186. (beginning-of-line 1)
  14187. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14188. (org-match-string-no-properties 1)
  14189. "")))
  14190. (defun org-get-tags ()
  14191. "Get the list of tags specified in the current headline."
  14192. (org-split-string (org-get-tags-string) ":"))
  14193. (defun org-get-buffer-tags ()
  14194. "Get a table of all tags used in the buffer, for completion."
  14195. (let (tags)
  14196. (save-excursion
  14197. (goto-char (point-min))
  14198. (while (re-search-forward
  14199. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14200. (when (equal (char-after (point-at-bol 0)) ?*)
  14201. (mapc (lambda (x) (add-to-list 'tags x))
  14202. (org-split-string (org-match-string-no-properties 1) ":")))))
  14203. (mapcar 'list tags)))
  14204. ;;;; Properties
  14205. ;;; Setting and retrieving properties
  14206. (defconst org-special-properties
  14207. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14208. "TIMESTAMP" "TIMESTAMP_IA")
  14209. "The special properties valid in Org-mode.
  14210. These are properties that are not defined in the property drawer,
  14211. but in some other way.")
  14212. (defconst org-default-properties
  14213. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14214. "LOCATION" "LOGGING" "COLUMNS")
  14215. "Some properties that are used by Org-mode for various purposes.
  14216. Being in this list makes sure that they are offered for completion.")
  14217. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14218. "Regular expression matching the first line of a property drawer.")
  14219. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14220. "Regular expression matching the first line of a property drawer.")
  14221. (defun org-property-action ()
  14222. "Do an action on properties."
  14223. (interactive)
  14224. (let (c)
  14225. (org-at-property-p)
  14226. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14227. (setq c (read-char-exclusive))
  14228. (cond
  14229. ((equal c ?s)
  14230. (call-interactively 'org-set-property))
  14231. ((equal c ?d)
  14232. (call-interactively 'org-delete-property))
  14233. ((equal c ?D)
  14234. (call-interactively 'org-delete-property-globally))
  14235. ((equal c ?c)
  14236. (call-interactively 'org-compute-property-at-point))
  14237. (t (error "No such property action %c" c)))))
  14238. (defun org-at-property-p ()
  14239. "Is the cursor in a property line?"
  14240. ;; FIXME: Does not check if we are actually in the drawer.
  14241. ;; FIXME: also returns true on any drawers.....
  14242. ;; This is used by C-c C-c for property action.
  14243. (save-excursion
  14244. (beginning-of-line 1)
  14245. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14246. (defmacro org-with-point-at (pom &rest body)
  14247. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14248. (declare (indent 1) (debug t))
  14249. `(save-excursion
  14250. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14251. (save-excursion
  14252. (goto-char (or pom (point)))
  14253. ,@body)))
  14254. (defun org-get-property-block (&optional beg end force)
  14255. "Return the (beg . end) range of the body of the property drawer.
  14256. BEG and END can be beginning and end of subtree, if not given
  14257. they will be found.
  14258. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14259. (catch 'exit
  14260. (save-excursion
  14261. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14262. (end (or end (progn (outline-next-heading) (point)))))
  14263. (goto-char beg)
  14264. (if (re-search-forward org-property-start-re end t)
  14265. (setq beg (1+ (match-end 0)))
  14266. (if force
  14267. (save-excursion
  14268. (org-insert-property-drawer)
  14269. (setq end (progn (outline-next-heading) (point))))
  14270. (throw 'exit nil))
  14271. (goto-char beg)
  14272. (if (re-search-forward org-property-start-re end t)
  14273. (setq beg (1+ (match-end 0)))))
  14274. (if (re-search-forward org-property-end-re end t)
  14275. (setq end (match-beginning 0))
  14276. (or force (throw 'exit nil))
  14277. (goto-char beg)
  14278. (setq end beg)
  14279. (org-indent-line-function)
  14280. (insert ":END:\n"))
  14281. (cons beg end)))))
  14282. (defun org-entry-properties (&optional pom which)
  14283. "Get all properties of the entry at point-or-marker POM.
  14284. This includes the TODO keyword, the tags, time strings for deadline,
  14285. scheduled, and clocking, and any additional properties defined in the
  14286. entry. The return value is an alist, keys may occur multiple times
  14287. if the property key was used several times.
  14288. POM may also be nil, in which case the current entry is used.
  14289. If WHICH is nil or `all', get all properties. If WHICH is
  14290. `special' or `standard', only get that subclass."
  14291. (setq which (or which 'all))
  14292. (org-with-point-at pom
  14293. (let ((clockstr (substring org-clock-string 0 -1))
  14294. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14295. beg end range props sum-props key value string)
  14296. (save-excursion
  14297. (when (condition-case nil (org-back-to-heading t) (error nil))
  14298. (setq beg (point))
  14299. (setq sum-props (get-text-property (point) 'org-summaries))
  14300. (outline-next-heading)
  14301. (setq end (point))
  14302. (when (memq which '(all special))
  14303. ;; Get the special properties, like TODO and tags
  14304. (goto-char beg)
  14305. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14306. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14307. (when (looking-at org-priority-regexp)
  14308. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14309. (when (and (setq value (org-get-tags-string))
  14310. (string-match "\\S-" value))
  14311. (push (cons "TAGS" value) props))
  14312. (when (setq value (org-get-tags-at))
  14313. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14314. props))
  14315. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14316. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14317. string (if (equal key clockstr)
  14318. (org-no-properties
  14319. (org-trim
  14320. (buffer-substring
  14321. (match-beginning 3) (goto-char (point-at-eol)))))
  14322. (substring (org-match-string-no-properties 3) 1 -1)))
  14323. (unless key
  14324. (if (= (char-after (match-beginning 3)) ?\[)
  14325. (setq key "TIMESTAMP_IA")
  14326. (setq key "TIMESTAMP")))
  14327. (when (or (equal key clockstr) (not (assoc key props)))
  14328. (push (cons key string) props)))
  14329. )
  14330. (when (memq which '(all standard))
  14331. ;; Get the standard properties, like :PORP: ...
  14332. (setq range (org-get-property-block beg end))
  14333. (when range
  14334. (goto-char (car range))
  14335. (while (re-search-forward
  14336. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14337. (cdr range) t)
  14338. (setq key (org-match-string-no-properties 1)
  14339. value (org-trim (or (org-match-string-no-properties 2) "")))
  14340. (unless (member key excluded)
  14341. (push (cons key (or value "")) props)))))
  14342. (append sum-props (nreverse props)))))))
  14343. (defun org-entry-get (pom property &optional inherit)
  14344. "Get value of PROPERTY for entry at point-or-marker POM.
  14345. If INHERIT is non-nil and the entry does not have the property,
  14346. then also check higher levels of the hierarchy.
  14347. If the property is present but empty, the return value is the empty string.
  14348. If the property is not present at all, nil is returned."
  14349. (org-with-point-at pom
  14350. (if inherit
  14351. (org-entry-get-with-inheritance property)
  14352. (if (member property org-special-properties)
  14353. ;; We need a special property. Use brute force, get all properties.
  14354. (cdr (assoc property (org-entry-properties nil 'special)))
  14355. (let ((range (org-get-property-block)))
  14356. (if (and range
  14357. (goto-char (car range))
  14358. (re-search-forward
  14359. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14360. (cdr range) t))
  14361. ;; Found the property, return it.
  14362. (if (match-end 1)
  14363. (org-match-string-no-properties 1)
  14364. "")))))))
  14365. (defun org-entry-delete (pom property)
  14366. "Delete the property PROPERTY from entry at point-or-marker POM."
  14367. (org-with-point-at pom
  14368. (if (member property org-special-properties)
  14369. nil ; cannot delete these properties.
  14370. (let ((range (org-get-property-block)))
  14371. (if (and range
  14372. (goto-char (car range))
  14373. (re-search-forward
  14374. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14375. (cdr range) t))
  14376. (progn
  14377. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14378. t)
  14379. nil)))))
  14380. ;; Multi-values properties are properties that contain multiple values
  14381. ;; These values are assumed to be single words, separated by whitespace.
  14382. (defun org-entry-add-to-multivalued-property (pom property value)
  14383. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14384. (let* ((old (org-entry-get pom property))
  14385. (values (and old (org-split-string old "[ \t]"))))
  14386. (unless (member value values)
  14387. (setq values (cons value values))
  14388. (org-entry-put pom property
  14389. (mapconcat 'identity values " ")))))
  14390. (defun org-entry-remove-from-multivalued-property (pom property value)
  14391. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14392. (let* ((old (org-entry-get pom property))
  14393. (values (and old (org-split-string old "[ \t]"))))
  14394. (when (member value values)
  14395. (setq values (delete value values))
  14396. (org-entry-put pom property
  14397. (mapconcat 'identity values " ")))))
  14398. (defun org-entry-member-in-multivalued-property (pom property value)
  14399. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14400. (let* ((old (org-entry-get pom property))
  14401. (values (and old (org-split-string old "[ \t]"))))
  14402. (member value values)))
  14403. (defvar org-entry-property-inherited-from (make-marker))
  14404. (defun org-entry-get-with-inheritance (property)
  14405. "Get entry property, and search higher levels if not present."
  14406. (let (tmp)
  14407. (save-excursion
  14408. (save-restriction
  14409. (widen)
  14410. (catch 'ex
  14411. (while t
  14412. (when (setq tmp (org-entry-get nil property))
  14413. (org-back-to-heading t)
  14414. (move-marker org-entry-property-inherited-from (point))
  14415. (throw 'ex tmp))
  14416. (or (org-up-heading-safe) (throw 'ex nil)))))
  14417. (or tmp (cdr (assoc property org-local-properties))
  14418. (cdr (assoc property org-global-properties))))))
  14419. (defun org-entry-put (pom property value)
  14420. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14421. (org-with-point-at pom
  14422. (org-back-to-heading t)
  14423. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14424. range)
  14425. (cond
  14426. ((equal property "TODO")
  14427. (when (and (stringp value) (string-match "\\S-" value)
  14428. (not (member value org-todo-keywords-1)))
  14429. (error "\"%s\" is not a valid TODO state" value))
  14430. (if (or (not value)
  14431. (not (string-match "\\S-" value)))
  14432. (setq value 'none))
  14433. (org-todo value)
  14434. (org-set-tags nil 'align))
  14435. ((equal property "PRIORITY")
  14436. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14437. (string-to-char value) ?\ ))
  14438. (org-set-tags nil 'align))
  14439. ((equal property "SCHEDULED")
  14440. (if (re-search-forward org-scheduled-time-regexp end t)
  14441. (cond
  14442. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14443. ((eq value 'later) (org-timestamp-change 1 'day))
  14444. (t (call-interactively 'org-schedule)))
  14445. (call-interactively 'org-schedule)))
  14446. ((equal property "DEADLINE")
  14447. (if (re-search-forward org-deadline-time-regexp end t)
  14448. (cond
  14449. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14450. ((eq value 'later) (org-timestamp-change 1 'day))
  14451. (t (call-interactively 'org-deadline)))
  14452. (call-interactively 'org-deadline)))
  14453. ((member property org-special-properties)
  14454. (error "The %s property can not yet be set with `org-entry-put'"
  14455. property))
  14456. (t ; a non-special property
  14457. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14458. (setq range (org-get-property-block beg end 'force))
  14459. (goto-char (car range))
  14460. (if (re-search-forward
  14461. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14462. (progn
  14463. (delete-region (match-beginning 1) (match-end 1))
  14464. (goto-char (match-beginning 1)))
  14465. (goto-char (cdr range))
  14466. (insert "\n")
  14467. (backward-char 1)
  14468. (org-indent-line-function)
  14469. (insert ":" property ":"))
  14470. (and value (insert " " value))
  14471. (org-indent-line-function)))))))
  14472. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  14473. "Get all property keys in the current buffer.
  14474. With INCLUDE-SPECIALS, also list the special properties that relect things
  14475. like tags and TODO state.
  14476. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14477. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  14478. (let (rtn range)
  14479. (save-excursion
  14480. (save-restriction
  14481. (widen)
  14482. (goto-char (point-min))
  14483. (while (re-search-forward org-property-start-re nil t)
  14484. (setq range (org-get-property-block))
  14485. (goto-char (car range))
  14486. (while (re-search-forward
  14487. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  14488. (cdr range) t)
  14489. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14490. (outline-next-heading))))
  14491. (when include-specials
  14492. (setq rtn (append org-special-properties rtn)))
  14493. (when include-defaults
  14494. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14495. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14496. (defun org-property-values (key)
  14497. "Return a list of all values of property KEY."
  14498. (save-excursion
  14499. (save-restriction
  14500. (widen)
  14501. (goto-char (point-min))
  14502. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14503. values)
  14504. (while (re-search-forward re nil t)
  14505. (add-to-list 'values (org-trim (match-string 1))))
  14506. (delete "" values)))))
  14507. (defun org-insert-property-drawer ()
  14508. "Insert a property drawer into the current entry."
  14509. (interactive)
  14510. (org-back-to-heading t)
  14511. (looking-at outline-regexp)
  14512. (let ((indent (- (match-end 0)(match-beginning 0)))
  14513. (beg (point))
  14514. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14515. end hiddenp)
  14516. (outline-next-heading)
  14517. (setq end (point))
  14518. (goto-char beg)
  14519. (while (re-search-forward re end t))
  14520. (setq hiddenp (org-invisible-p))
  14521. (end-of-line 1)
  14522. (and (equal (char-after) ?\n) (forward-char 1))
  14523. (org-skip-over-state-notes)
  14524. (skip-chars-backward " \t\n\r")
  14525. (if (eq (char-before) ?*) (forward-char 1))
  14526. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14527. (beginning-of-line 0)
  14528. (indent-to-column indent)
  14529. (beginning-of-line 2)
  14530. (indent-to-column indent)
  14531. (beginning-of-line 0)
  14532. (if hiddenp
  14533. (save-excursion
  14534. (org-back-to-heading t)
  14535. (hide-entry))
  14536. (org-flag-drawer t))))
  14537. (defun org-set-property (property value)
  14538. "In the current entry, set PROPERTY to VALUE.
  14539. When called interactively, this will prompt for a property name, offering
  14540. completion on existing and default properties. And then it will prompt
  14541. for a value, offering competion either on allowed values (via an inherited
  14542. xxx_ALL property) or on existing values in other instances of this property
  14543. in the current file."
  14544. (interactive
  14545. (let* ((prop (completing-read
  14546. "Property: " (mapcar 'list (org-buffer-property-keys nil t))))
  14547. (cur (org-entry-get nil prop))
  14548. (allowed (org-property-get-allowed-values nil prop 'table))
  14549. (existing (mapcar 'list (org-property-values prop)))
  14550. (val (if allowed
  14551. (completing-read "Value: " allowed nil 'req-match)
  14552. (completing-read
  14553. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14554. (concat "[" cur "]") "")
  14555. ": ")
  14556. existing nil nil "" nil cur))))
  14557. (list prop (if (equal val "") cur val))))
  14558. (unless (equal (org-entry-get nil property) value)
  14559. (org-entry-put nil property value)))
  14560. (defun org-delete-property (property)
  14561. "In the current entry, delete PROPERTY."
  14562. (interactive
  14563. (let* ((prop (completing-read
  14564. "Property: " (org-entry-properties nil 'standard))))
  14565. (list prop)))
  14566. (message "Property %s %s" property
  14567. (if (org-entry-delete nil property)
  14568. "deleted"
  14569. "was not present in the entry")))
  14570. (defun org-delete-property-globally (property)
  14571. "Remove PROPERTY globally, from all entries."
  14572. (interactive
  14573. (let* ((prop (completing-read
  14574. "Globally remove property: "
  14575. (mapcar 'list (org-buffer-property-keys)))))
  14576. (list prop)))
  14577. (save-excursion
  14578. (save-restriction
  14579. (widen)
  14580. (goto-char (point-min))
  14581. (let ((cnt 0))
  14582. (while (re-search-forward
  14583. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14584. nil t)
  14585. (setq cnt (1+ cnt))
  14586. (replace-match ""))
  14587. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14588. (defvar org-columns-current-fmt-compiled) ; defined below
  14589. (defun org-compute-property-at-point ()
  14590. "Compute the property at point.
  14591. This looks for an enclosing column format, extracts the operator and
  14592. then applies it to the proerty in the column format's scope."
  14593. (interactive)
  14594. (unless (org-at-property-p)
  14595. (error "Not at a property"))
  14596. (let ((prop (org-match-string-no-properties 2)))
  14597. (org-columns-get-format-and-top-level)
  14598. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14599. (error "No operator defined for property %s" prop))
  14600. (org-columns-compute prop)))
  14601. (defun org-property-get-allowed-values (pom property &optional table)
  14602. "Get allowed values for the property PROPERTY.
  14603. When TABLE is non-nil, return an alist that can directly be used for
  14604. completion."
  14605. (let (vals)
  14606. (cond
  14607. ((equal property "TODO")
  14608. (setq vals (org-with-point-at pom
  14609. (append org-todo-keywords-1 '("")))))
  14610. ((equal property "PRIORITY")
  14611. (let ((n org-lowest-priority))
  14612. (while (>= n org-highest-priority)
  14613. (push (char-to-string n) vals)
  14614. (setq n (1- n)))))
  14615. ((member property org-special-properties))
  14616. (t
  14617. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14618. (when (and vals (string-match "\\S-" vals))
  14619. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14620. (setq vals (mapcar (lambda (x)
  14621. (cond ((stringp x) x)
  14622. ((numberp x) (number-to-string x))
  14623. ((symbolp x) (symbol-name x))
  14624. (t "???")))
  14625. vals)))))
  14626. (if table (mapcar 'list vals) vals)))
  14627. (defun org-property-previous-allowed-value (&optional previous)
  14628. "Switch to the next allowed value for this property."
  14629. (interactive)
  14630. (org-property-next-allowed-value t))
  14631. (defun org-property-next-allowed-value (&optional previous)
  14632. "Switch to the next allowed value for this property."
  14633. (interactive)
  14634. (unless (org-at-property-p)
  14635. (error "Not at a property"))
  14636. (let* ((key (match-string 2))
  14637. (value (match-string 3))
  14638. (allowed (or (org-property-get-allowed-values (point) key)
  14639. (and (member value '("[ ]" "[-]" "[X]"))
  14640. '("[ ]" "[X]"))))
  14641. nval)
  14642. (unless allowed
  14643. (error "Allowed values for this property have not been defined"))
  14644. (if previous (setq allowed (reverse allowed)))
  14645. (if (member value allowed)
  14646. (setq nval (car (cdr (member value allowed)))))
  14647. (setq nval (or nval (car allowed)))
  14648. (if (equal nval value)
  14649. (error "Only one allowed value for this property"))
  14650. (org-at-property-p)
  14651. (replace-match (concat " :" key ": " nval) t t)
  14652. (org-indent-line-function)
  14653. (beginning-of-line 1)
  14654. (skip-chars-forward " \t")))
  14655. (defun org-find-entry-with-id (ident)
  14656. "Locate the entry that contains the ID property with exact value IDENT.
  14657. IDENT can be a string, a symbol or a number, this function will search for
  14658. the string representation of it.
  14659. Return the position where this entry starts, or nil if there is no such entry."
  14660. (let ((id (cond
  14661. ((stringp ident) ident)
  14662. ((symbol-name ident) (symbol-name ident))
  14663. ((numberp ident) (number-to-string ident))
  14664. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14665. (case-fold-search nil))
  14666. (save-excursion
  14667. (save-restriction
  14668. (goto-char (point-min))
  14669. (when (re-search-forward
  14670. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14671. nil t)
  14672. (org-back-to-heading)
  14673. (point))))))
  14674. ;;; Column View
  14675. (defvar org-columns-overlays nil
  14676. "Holds the list of current column overlays.")
  14677. (defvar org-columns-current-fmt nil
  14678. "Local variable, holds the currently active column format.")
  14679. (defvar org-columns-current-fmt-compiled nil
  14680. "Local variable, holds the currently active column format.
  14681. This is the compiled version of the format.")
  14682. (defvar org-columns-current-widths nil
  14683. "Loval variable, holds the currently widths of fields.")
  14684. (defvar org-columns-current-maxwidths nil
  14685. "Loval variable, holds the currently active maximum column widths.")
  14686. (defvar org-columns-begin-marker (make-marker)
  14687. "Points to the position where last a column creation command was called.")
  14688. (defvar org-columns-top-level-marker (make-marker)
  14689. "Points to the position where current columns region starts.")
  14690. (defvar org-columns-map (make-sparse-keymap)
  14691. "The keymap valid in column display.")
  14692. (defun org-columns-content ()
  14693. "Switch to contents view while in columns view."
  14694. (interactive)
  14695. (org-overview)
  14696. (org-content))
  14697. (org-defkey org-columns-map "c" 'org-columns-content)
  14698. (org-defkey org-columns-map "o" 'org-overview)
  14699. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  14700. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  14701. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  14702. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  14703. (org-defkey org-columns-map "v" 'org-columns-show-value)
  14704. (org-defkey org-columns-map "q" 'org-columns-quit)
  14705. (org-defkey org-columns-map "r" 'org-columns-redo)
  14706. (org-defkey org-columns-map [left] 'backward-char)
  14707. (org-defkey org-columns-map "\M-b" 'backward-char)
  14708. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  14709. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  14710. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  14711. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  14712. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  14713. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  14714. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  14715. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  14716. (org-defkey org-columns-map "<" 'org-columns-narrow)
  14717. (org-defkey org-columns-map ">" 'org-columns-widen)
  14718. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  14719. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  14720. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  14721. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  14722. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  14723. '("Column"
  14724. ["Edit property" org-columns-edit-value t]
  14725. ["Next allowed value" org-columns-next-allowed-value t]
  14726. ["Previous allowed value" org-columns-previous-allowed-value t]
  14727. ["Show full value" org-columns-show-value t]
  14728. ["Edit allowed values" org-columns-edit-allowed t]
  14729. "--"
  14730. ["Edit column attributes" org-columns-edit-attributes t]
  14731. ["Increase column width" org-columns-widen t]
  14732. ["Decrease column width" org-columns-narrow t]
  14733. "--"
  14734. ["Move column right" org-columns-move-right t]
  14735. ["Move column left" org-columns-move-left t]
  14736. ["Add column" org-columns-new t]
  14737. ["Delete column" org-columns-delete t]
  14738. "--"
  14739. ["CONTENTS" org-columns-content t]
  14740. ["OVERVIEW" org-overview t]
  14741. ["Refresh columns display" org-columns-redo t]
  14742. "--"
  14743. ["Open link" org-columns-open-link t]
  14744. "--"
  14745. ["Quit" org-columns-quit t]))
  14746. (defun org-columns-new-overlay (beg end &optional string face)
  14747. "Create a new column overlay and add it to the list."
  14748. (let ((ov (org-make-overlay beg end)))
  14749. (org-overlay-put ov 'face (or face 'secondary-selection))
  14750. (org-overlay-display ov string face)
  14751. (push ov org-columns-overlays)
  14752. ov))
  14753. (defun org-columns-display-here (&optional props)
  14754. "Overlay the current line with column display."
  14755. (interactive)
  14756. (let* ((fmt org-columns-current-fmt-compiled)
  14757. (beg (point-at-bol))
  14758. (level-face (save-excursion
  14759. (beginning-of-line 1)
  14760. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  14761. (org-get-level-face 2))))
  14762. (color (list :foreground
  14763. (face-attribute (or level-face 'default) :foreground)))
  14764. props pom property ass width f string ov column val modval)
  14765. ;; Check if the entry is in another buffer.
  14766. (unless props
  14767. (if (eq major-mode 'org-agenda-mode)
  14768. (setq pom (or (get-text-property (point) 'org-hd-marker)
  14769. (get-text-property (point) 'org-marker))
  14770. props (if pom (org-entry-properties pom) nil))
  14771. (setq props (org-entry-properties nil))))
  14772. ;; Walk the format
  14773. (while (setq column (pop fmt))
  14774. (setq property (car column)
  14775. ass (if (equal property "ITEM")
  14776. (cons "ITEM"
  14777. (save-match-data
  14778. (org-no-properties
  14779. (org-remove-tabs
  14780. (buffer-substring-no-properties
  14781. (point-at-bol) (point-at-eol))))))
  14782. (assoc property props))
  14783. width (or (cdr (assoc property org-columns-current-maxwidths))
  14784. (nth 2 column)
  14785. (length property))
  14786. f (format "%%-%d.%ds | " width width)
  14787. val (or (cdr ass) "")
  14788. modval (if (equal property "ITEM")
  14789. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  14790. string (format f (or modval val)))
  14791. ;; Create the overlay
  14792. (org-unmodified
  14793. (setq ov (org-columns-new-overlay
  14794. beg (setq beg (1+ beg)) string
  14795. (list color 'org-column)))
  14796. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  14797. (org-overlay-put ov 'keymap org-columns-map)
  14798. (org-overlay-put ov 'org-columns-key property)
  14799. (org-overlay-put ov 'org-columns-value (cdr ass))
  14800. (org-overlay-put ov 'org-columns-value-modified modval)
  14801. (org-overlay-put ov 'org-columns-pom pom)
  14802. (org-overlay-put ov 'org-columns-format f))
  14803. (if (or (not (char-after beg))
  14804. (equal (char-after beg) ?\n))
  14805. (let ((inhibit-read-only t))
  14806. (save-excursion
  14807. (goto-char beg)
  14808. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  14809. ;; Make the rest of the line disappear.
  14810. (org-unmodified
  14811. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  14812. (org-overlay-put ov 'invisible t)
  14813. (org-overlay-put ov 'keymap org-columns-map)
  14814. (org-overlay-put ov 'intangible t)
  14815. (push ov org-columns-overlays)
  14816. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  14817. (org-overlay-put ov 'keymap org-columns-map)
  14818. (push ov org-columns-overlays)
  14819. (let ((inhibit-read-only t))
  14820. (put-text-property (max (point-min) (1- (point-at-bol)))
  14821. (min (point-max) (1+ (point-at-eol)))
  14822. 'read-only "Type `e' to edit property")))))
  14823. (defvar org-previous-header-line-format nil
  14824. "The header line format before column view was turned on.")
  14825. (defvar org-columns-inhibit-recalculation nil
  14826. "Inhibit recomputing of columns on column view startup.")
  14827. (defvar header-line-format)
  14828. (defun org-columns-display-here-title ()
  14829. "Overlay the newline before the current line with the table title."
  14830. (interactive)
  14831. (let ((fmt org-columns-current-fmt-compiled)
  14832. string (title "")
  14833. property width f column str widths)
  14834. (while (setq column (pop fmt))
  14835. (setq property (car column)
  14836. str (or (nth 1 column) property)
  14837. width (or (cdr (assoc property org-columns-current-maxwidths))
  14838. (nth 2 column)
  14839. (length str))
  14840. widths (push width widths)
  14841. f (format "%%-%d.%ds | " width width)
  14842. string (format f str)
  14843. title (concat title string)))
  14844. (setq title (concat
  14845. (org-add-props " " nil 'display '(space :align-to 0))
  14846. (org-add-props title nil 'face '(:weight bold :underline t))))
  14847. (org-set-local 'org-previous-header-line-format header-line-format)
  14848. (org-set-local 'org-columns-current-widths (nreverse widths))
  14849. (setq header-line-format title)))
  14850. (defun org-columns-remove-overlays ()
  14851. "Remove all currently active column overlays."
  14852. (interactive)
  14853. (when (marker-buffer org-columns-begin-marker)
  14854. (with-current-buffer (marker-buffer org-columns-begin-marker)
  14855. (when (local-variable-p 'org-previous-header-line-format)
  14856. (setq header-line-format org-previous-header-line-format)
  14857. (kill-local-variable 'org-previous-header-line-format))
  14858. (move-marker org-columns-begin-marker nil)
  14859. (move-marker org-columns-top-level-marker nil)
  14860. (org-unmodified
  14861. (mapc 'org-delete-overlay org-columns-overlays)
  14862. (setq org-columns-overlays nil)
  14863. (let ((inhibit-read-only t))
  14864. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  14865. (defun org-columns-cleanup-item (item fmt)
  14866. "Remove from ITEM what is a column in the format FMT."
  14867. (if (not org-complex-heading-regexp)
  14868. item
  14869. (when (string-match org-complex-heading-regexp item)
  14870. (concat
  14871. (org-add-props (concat (match-string 1 item) " ") nil
  14872. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  14873. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  14874. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  14875. " " (match-string 4 item)
  14876. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  14877. (defun org-columns-show-value ()
  14878. "Show the full value of the property."
  14879. (interactive)
  14880. (let ((value (get-char-property (point) 'org-columns-value)))
  14881. (message "Value is: %s" (or value ""))))
  14882. (defun org-columns-quit ()
  14883. "Remove the column overlays and in this way exit column editing."
  14884. (interactive)
  14885. (org-unmodified
  14886. (org-columns-remove-overlays)
  14887. (let ((inhibit-read-only t))
  14888. (remove-text-properties (point-min) (point-max) '(read-only t))))
  14889. (when (eq major-mode 'org-agenda-mode)
  14890. (message
  14891. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  14892. (defun org-columns-check-computed ()
  14893. "Check if this column value is computed.
  14894. If yes, throw an error indicating that changing it does not make sense."
  14895. (let ((val (get-char-property (point) 'org-columns-value)))
  14896. (when (and (stringp val)
  14897. (get-char-property 0 'org-computed val))
  14898. (error "This value is computed from the entry's children"))))
  14899. (defun org-columns-todo (&optional arg)
  14900. "Change the TODO state during column view."
  14901. (interactive "P")
  14902. (org-columns-edit-value "TODO"))
  14903. (defun org-columns-set-tags-or-toggle (&optional arg)
  14904. "Toggle checkbox at point, or set tags for current headline."
  14905. (interactive "P")
  14906. (if (string-match "\\`\\[[ xX-]\\]\\'"
  14907. (get-char-property (point) 'org-columns-value))
  14908. (org-columns-next-allowed-value)
  14909. (org-columns-edit-value "TAGS")))
  14910. (defun org-columns-edit-value (&optional key)
  14911. "Edit the value of the property at point in column view.
  14912. Where possible, use the standard interface for changing this line."
  14913. (interactive)
  14914. (org-columns-check-computed)
  14915. (let* ((external-key key)
  14916. (col (current-column))
  14917. (key (or key (get-char-property (point) 'org-columns-key)))
  14918. (value (get-char-property (point) 'org-columns-value))
  14919. (bol (point-at-bol)) (eol (point-at-eol))
  14920. (pom (or (get-text-property bol 'org-hd-marker)
  14921. (point))) ; keep despite of compiler waring
  14922. (line-overlays
  14923. (delq nil (mapcar (lambda (x)
  14924. (and (eq (overlay-buffer x) (current-buffer))
  14925. (>= (overlay-start x) bol)
  14926. (<= (overlay-start x) eol)
  14927. x))
  14928. org-columns-overlays)))
  14929. nval eval allowed)
  14930. (cond
  14931. ((equal key "ITEM")
  14932. (setq eval '(org-with-point-at pom
  14933. (org-edit-headline))))
  14934. ((equal key "TODO")
  14935. (setq eval '(org-with-point-at pom
  14936. (let ((current-prefix-arg
  14937. (if external-key current-prefix-arg '(4))))
  14938. (call-interactively 'org-todo)))))
  14939. ((equal key "PRIORITY")
  14940. (setq eval '(org-with-point-at pom
  14941. (call-interactively 'org-priority))))
  14942. ((equal key "TAGS")
  14943. (setq eval '(org-with-point-at pom
  14944. (let ((org-fast-tag-selection-single-key
  14945. (if (eq org-fast-tag-selection-single-key 'expert)
  14946. t org-fast-tag-selection-single-key)))
  14947. (call-interactively 'org-set-tags)))))
  14948. ((equal key "DEADLINE")
  14949. (setq eval '(org-with-point-at pom
  14950. (call-interactively 'org-deadline))))
  14951. ((equal key "SCHEDULED")
  14952. (setq eval '(org-with-point-at pom
  14953. (call-interactively 'org-schedule))))
  14954. (t
  14955. (setq allowed (org-property-get-allowed-values pom key 'table))
  14956. (if allowed
  14957. (setq nval (completing-read "Value: " allowed nil t))
  14958. (setq nval (read-string "Edit: " value)))
  14959. (setq nval (org-trim nval))
  14960. (when (not (equal nval value))
  14961. (setq eval '(org-entry-put pom key nval)))))
  14962. (when eval
  14963. (let ((inhibit-read-only t))
  14964. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  14965. (unwind-protect
  14966. (progn
  14967. (setq org-columns-overlays
  14968. (org-delete-all line-overlays org-columns-overlays))
  14969. (mapc 'org-delete-overlay line-overlays)
  14970. (org-columns-eval eval))
  14971. (org-columns-display-here))))
  14972. (move-to-column col)
  14973. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  14974. (org-columns-update key))))
  14975. (defun org-edit-headline () ; FIXME: this is not columns specific
  14976. "Edit the current headline, the part without TODO keyword, TAGS."
  14977. (org-back-to-heading)
  14978. (when (looking-at org-todo-line-regexp)
  14979. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  14980. (txt (match-string 3))
  14981. (post "")
  14982. txt2)
  14983. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  14984. (setq post (match-string 0 txt)
  14985. txt (substring txt 0 (match-beginning 0))))
  14986. (setq txt2 (read-string "Edit: " txt))
  14987. (when (not (equal txt txt2))
  14988. (beginning-of-line 1)
  14989. (insert pre txt2 post)
  14990. (delete-region (point) (point-at-eol))
  14991. (org-set-tags nil t)))))
  14992. (defun org-columns-edit-allowed ()
  14993. "Edit the list of allowed values for the current property."
  14994. (interactive)
  14995. (let* ((key (get-char-property (point) 'org-columns-key))
  14996. (key1 (concat key "_ALL"))
  14997. (allowed (org-entry-get (point) key1 t))
  14998. nval)
  14999. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15000. (setq nval (read-string "Allowed: " allowed))
  15001. (org-entry-put
  15002. (cond ((marker-position org-entry-property-inherited-from)
  15003. org-entry-property-inherited-from)
  15004. ((marker-position org-columns-top-level-marker)
  15005. org-columns-top-level-marker))
  15006. key1 nval)))
  15007. (defmacro org-no-warnings (&rest body)
  15008. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15009. (defun org-columns-eval (form)
  15010. (let (hidep)
  15011. (save-excursion
  15012. (beginning-of-line 1)
  15013. ;; `next-line' is needed here, because it skips invisible line.
  15014. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15015. (setq hidep (org-on-heading-p 1)))
  15016. (eval form)
  15017. (and hidep (hide-entry))))
  15018. (defun org-columns-previous-allowed-value ()
  15019. "Switch to the previous allowed value for this column."
  15020. (interactive)
  15021. (org-columns-next-allowed-value t))
  15022. (defun org-columns-next-allowed-value (&optional previous)
  15023. "Switch to the next allowed value for this column."
  15024. (interactive)
  15025. (org-columns-check-computed)
  15026. (let* ((col (current-column))
  15027. (key (get-char-property (point) 'org-columns-key))
  15028. (value (get-char-property (point) 'org-columns-value))
  15029. (bol (point-at-bol)) (eol (point-at-eol))
  15030. (pom (or (get-text-property bol 'org-hd-marker)
  15031. (point))) ; keep despite of compiler waring
  15032. (line-overlays
  15033. (delq nil (mapcar (lambda (x)
  15034. (and (eq (overlay-buffer x) (current-buffer))
  15035. (>= (overlay-start x) bol)
  15036. (<= (overlay-start x) eol)
  15037. x))
  15038. org-columns-overlays)))
  15039. (allowed (or (org-property-get-allowed-values pom key)
  15040. (and (equal
  15041. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15042. 'checkbox) '("[ ]" "[X]"))))
  15043. nval)
  15044. (when (equal key "ITEM")
  15045. (error "Cannot edit item headline from here"))
  15046. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15047. (error "Allowed values for this property have not been defined"))
  15048. (if (member key '("SCHEDULED" "DEADLINE"))
  15049. (setq nval (if previous 'earlier 'later))
  15050. (if previous (setq allowed (reverse allowed)))
  15051. (if (member value allowed)
  15052. (setq nval (car (cdr (member value allowed)))))
  15053. (setq nval (or nval (car allowed)))
  15054. (if (equal nval value)
  15055. (error "Only one allowed value for this property")))
  15056. (let ((inhibit-read-only t))
  15057. (remove-text-properties (1- bol) eol '(read-only t))
  15058. (unwind-protect
  15059. (progn
  15060. (setq org-columns-overlays
  15061. (org-delete-all line-overlays org-columns-overlays))
  15062. (mapc 'org-delete-overlay line-overlays)
  15063. (org-columns-eval '(org-entry-put pom key nval)))
  15064. (org-columns-display-here)))
  15065. (move-to-column col)
  15066. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  15067. (org-columns-update key))))
  15068. (defun org-verify-version (task)
  15069. (cond
  15070. ((eq task 'columns)
  15071. (if (or (featurep 'xemacs)
  15072. (< emacs-major-version 22))
  15073. (error "Emacs 22 is required for the columns feature")))))
  15074. (defun org-columns-open-link (&optional arg)
  15075. (interactive "P")
  15076. (let ((key (get-char-property (point) 'org-columns-key))
  15077. (value (get-char-property (point) 'org-columns-value)))
  15078. (org-open-link-from-string arg)))
  15079. (defun org-open-link-from-string (s &optional arg)
  15080. "Open a link in the string S, as if it was in Org-mode."
  15081. (interactive)
  15082. (with-temp-buffer
  15083. (let ((org-inhibit-startup t))
  15084. (org-mode)
  15085. (insert s)
  15086. (goto-char (point-min))
  15087. (org-open-at-point arg))))
  15088. (defun org-columns-get-format-and-top-level ()
  15089. (let (fmt)
  15090. (when (condition-case nil (org-back-to-heading) (error nil))
  15091. (move-marker org-entry-property-inherited-from nil)
  15092. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15093. (setq fmt (or fmt org-columns-default-format))
  15094. (org-set-local 'org-columns-current-fmt fmt)
  15095. (org-columns-compile-format fmt)
  15096. (if (marker-position org-entry-property-inherited-from)
  15097. (move-marker org-columns-top-level-marker
  15098. org-entry-property-inherited-from)
  15099. (move-marker org-columns-top-level-marker (point)))
  15100. fmt))
  15101. (defun org-columns ()
  15102. "Turn on column view on an org-mode file."
  15103. (interactive)
  15104. (org-verify-version 'columns)
  15105. (org-columns-remove-overlays)
  15106. (move-marker org-columns-begin-marker (point))
  15107. (let (beg end fmt cache maxwidths)
  15108. (setq fmt (org-columns-get-format-and-top-level))
  15109. (save-excursion
  15110. (goto-char org-columns-top-level-marker)
  15111. (setq beg (point))
  15112. (unless org-columns-inhibit-recalculation
  15113. (org-columns-compute-all))
  15114. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15115. (point-max)))
  15116. (goto-char beg)
  15117. ;; Get and cache the properties
  15118. (while (re-search-forward (concat "^" outline-regexp) end t)
  15119. (push (cons (org-current-line) (org-entry-properties)) cache))
  15120. (when cache
  15121. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15122. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15123. (org-columns-display-here-title)
  15124. (mapc (lambda (x)
  15125. (goto-line (car x))
  15126. (org-columns-display-here (cdr x)))
  15127. cache)))))
  15128. (defun org-columns-new (&optional prop title width op fmt)
  15129. "Insert a new column, to the leeft o the current column."
  15130. (interactive)
  15131. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15132. cell)
  15133. (setq prop (completing-read
  15134. "Property: " (mapcar 'list (org-buffer-property-keys t))
  15135. nil nil prop))
  15136. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15137. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15138. (if (string-match "\\S-" width)
  15139. (setq width (string-to-number width))
  15140. (setq width nil))
  15141. (setq fmt (completing-read "Summary [none]: "
  15142. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox"))
  15143. nil t))
  15144. (if (string-match "\\S-" fmt)
  15145. (setq fmt (intern fmt))
  15146. (setq fmt nil))
  15147. (if (eq fmt 'none) (setq fmt nil))
  15148. (if editp
  15149. (progn
  15150. (setcar editp prop)
  15151. (setcdr editp (list title width nil fmt)))
  15152. (setq cell (nthcdr (1- (current-column))
  15153. org-columns-current-fmt-compiled))
  15154. (setcdr cell (cons (list prop title width nil fmt)
  15155. (cdr cell))))
  15156. (org-columns-store-format)
  15157. (org-columns-redo)))
  15158. (defun org-columns-delete ()
  15159. "Delete the column at point from columns view."
  15160. (interactive)
  15161. (let* ((n (current-column))
  15162. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15163. (when (y-or-n-p
  15164. (format "Are you sure you want to remove column \"%s\"? " title))
  15165. (setq org-columns-current-fmt-compiled
  15166. (delq (nth n org-columns-current-fmt-compiled)
  15167. org-columns-current-fmt-compiled))
  15168. (org-columns-store-format)
  15169. (org-columns-redo)
  15170. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15171. (backward-char 1)))))
  15172. (defun org-columns-edit-attributes ()
  15173. "Edit the attributes of the current column."
  15174. (interactive)
  15175. (let* ((n (current-column))
  15176. (info (nth n org-columns-current-fmt-compiled)))
  15177. (apply 'org-columns-new info)))
  15178. (defun org-columns-widen (arg)
  15179. "Make the column wider by ARG characters."
  15180. (interactive "p")
  15181. (let* ((n (current-column))
  15182. (entry (nth n org-columns-current-fmt-compiled))
  15183. (width (or (nth 2 entry)
  15184. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15185. (setq width (max 1 (+ width arg)))
  15186. (setcar (nthcdr 2 entry) width)
  15187. (org-columns-store-format)
  15188. (org-columns-redo)))
  15189. (defun org-columns-narrow (arg)
  15190. "Make the column nrrower by ARG characters."
  15191. (interactive "p")
  15192. (org-columns-widen (- arg)))
  15193. (defun org-columns-move-right ()
  15194. "Swap this column with the one to the right."
  15195. (interactive)
  15196. (let* ((n (current-column))
  15197. (cell (nthcdr n org-columns-current-fmt-compiled))
  15198. e)
  15199. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15200. (error "Cannot shift this column further to the right"))
  15201. (setq e (car cell))
  15202. (setcar cell (car (cdr cell)))
  15203. (setcdr cell (cons e (cdr (cdr cell))))
  15204. (org-columns-store-format)
  15205. (org-columns-redo)
  15206. (forward-char 1)))
  15207. (defun org-columns-move-left ()
  15208. "Swap this column with the one to the left."
  15209. (interactive)
  15210. (let* ((n (current-column)))
  15211. (when (= n 0)
  15212. (error "Cannot shift this column further to the left"))
  15213. (backward-char 1)
  15214. (org-columns-move-right)
  15215. (backward-char 1)))
  15216. (defun org-columns-store-format ()
  15217. "Store the text version of the current columns format in appropriate place.
  15218. This is either in the COLUMNS property of the node starting the current column
  15219. display, or in the #+COLUMNS line of the current buffer."
  15220. (let (fmt (cnt 0))
  15221. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15222. (org-set-local 'org-columns-current-fmt fmt)
  15223. (if (marker-position org-columns-top-level-marker)
  15224. (save-excursion
  15225. (goto-char org-columns-top-level-marker)
  15226. (if (and (org-at-heading-p)
  15227. (org-entry-get nil "COLUMNS"))
  15228. (org-entry-put nil "COLUMNS" fmt)
  15229. (goto-char (point-min))
  15230. ;; Overwrite all #+COLUMNS lines....
  15231. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15232. (setq cnt (1+ cnt))
  15233. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15234. (unless (> cnt 0)
  15235. (goto-char (point-min))
  15236. (or (org-on-heading-p t) (outline-next-heading))
  15237. (let ((inhibit-read-only t))
  15238. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15239. (org-set-local 'org-columns-default-format fmt))))))
  15240. (defvar org-overriding-columns-format nil
  15241. "When set, overrides any other definition.")
  15242. (defvar org-agenda-view-columns-initially nil
  15243. "When set, switch to columns view immediately after creating the agenda.")
  15244. (defun org-agenda-columns ()
  15245. "Turn on column view in the agenda."
  15246. (interactive)
  15247. (org-verify-version 'columns)
  15248. (org-columns-remove-overlays)
  15249. (move-marker org-columns-begin-marker (point))
  15250. (let (fmt cache maxwidths m)
  15251. (cond
  15252. ((and (local-variable-p 'org-overriding-columns-format)
  15253. org-overriding-columns-format)
  15254. (setq fmt org-overriding-columns-format))
  15255. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15256. (setq fmt (org-entry-get m "COLUMNS" t)))
  15257. ((and (boundp 'org-columns-current-fmt)
  15258. (local-variable-p 'org-columns-current-fmt)
  15259. org-columns-current-fmt)
  15260. (setq fmt org-columns-current-fmt))
  15261. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15262. (setq m (get-text-property m 'org-hd-marker))
  15263. (setq fmt (org-entry-get m "COLUMNS" t))))
  15264. (setq fmt (or fmt org-columns-default-format))
  15265. (org-set-local 'org-columns-current-fmt fmt)
  15266. (org-columns-compile-format fmt)
  15267. (save-excursion
  15268. ;; Get and cache the properties
  15269. (goto-char (point-min))
  15270. (while (not (eobp))
  15271. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15272. (get-text-property (point) 'org-marker)))
  15273. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15274. (beginning-of-line 2))
  15275. (when cache
  15276. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15277. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15278. (org-columns-display-here-title)
  15279. (mapc (lambda (x)
  15280. (goto-line (car x))
  15281. (org-columns-display-here (cdr x)))
  15282. cache)))))
  15283. (defun org-columns-get-autowidth-alist (s cache)
  15284. "Derive the maximum column widths from the format and the cache."
  15285. (let ((start 0) rtn)
  15286. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  15287. (push (cons (match-string 1 s) 1) rtn)
  15288. (setq start (match-end 0)))
  15289. (mapc (lambda (x)
  15290. (setcdr x (apply 'max
  15291. (mapcar
  15292. (lambda (y)
  15293. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15294. cache))))
  15295. rtn)
  15296. rtn))
  15297. (defun org-columns-compute-all ()
  15298. "Compute all columns that have operators defined."
  15299. (org-unmodified
  15300. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15301. (let ((columns org-columns-current-fmt-compiled) col)
  15302. (while (setq col (pop columns))
  15303. (when (nth 3 col)
  15304. (save-excursion
  15305. (org-columns-compute (car col)))))))
  15306. (defun org-columns-update (property)
  15307. "Recompute PROPERTY, and update the columns display for it."
  15308. (org-columns-compute property)
  15309. (let (fmt val pos)
  15310. (save-excursion
  15311. (mapc (lambda (ov)
  15312. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15313. (setq pos (org-overlay-start ov))
  15314. (goto-char pos)
  15315. (when (setq val (cdr (assoc property
  15316. (get-text-property
  15317. (point-at-bol) 'org-summaries))))
  15318. (setq fmt (org-overlay-get ov 'org-columns-format))
  15319. (org-overlay-put ov 'org-columns-value val)
  15320. (org-overlay-put ov 'display (format fmt val)))))
  15321. org-columns-overlays))))
  15322. (defun org-columns-compute (property)
  15323. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15324. (interactive)
  15325. (let* ((re (concat "^" outline-regexp))
  15326. (lmax 30) ; Does anyone use deeper levels???
  15327. (lsum (make-vector lmax 0))
  15328. (lflag (make-vector lmax nil))
  15329. (level 0)
  15330. (ass (assoc property org-columns-current-fmt-compiled))
  15331. (format (nth 4 ass))
  15332. (printf (nth 5 ass))
  15333. (beg org-columns-top-level-marker)
  15334. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15335. (save-excursion
  15336. ;; Find the region to compute
  15337. (goto-char beg)
  15338. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15339. (goto-char end)
  15340. ;; Walk the tree from the back and do the computations
  15341. (while (re-search-backward re beg t)
  15342. (setq sumpos (match-beginning 0)
  15343. last-level level
  15344. level (org-outline-level)
  15345. val (org-entry-get nil property)
  15346. valflag (and val (string-match "\\S-" val)))
  15347. (cond
  15348. ((< level last-level)
  15349. ;; put the sum of lower levels here as a property
  15350. (setq sum (aref lsum last-level) ; current sum
  15351. flag (aref lflag last-level) ; any valid entries from children?
  15352. str (org-column-number-to-string sum format printf)
  15353. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15354. useval (if flag str1 (if valflag val ""))
  15355. sum-alist (get-text-property sumpos 'org-summaries))
  15356. (if (assoc property sum-alist)
  15357. (setcdr (assoc property sum-alist) useval)
  15358. (push (cons property useval) sum-alist)
  15359. (org-unmodified
  15360. (add-text-properties sumpos (1+ sumpos)
  15361. (list 'org-summaries sum-alist))))
  15362. (when val
  15363. (org-entry-put nil property (if flag str val)))
  15364. ;; add current to current level accumulator
  15365. (when (or flag valflag)
  15366. (aset lsum level (+ (aref lsum level)
  15367. (if flag sum (org-column-string-to-number
  15368. (if flag str val) format))))
  15369. (aset lflag level t))
  15370. ;; clear accumulators for deeper levels
  15371. (loop for l from (1+ level) to (1- lmax) do
  15372. (aset lsum l 0)
  15373. (aset lflag l nil)))
  15374. ((>= level last-level)
  15375. ;; add what we have here to the accumulator for this level
  15376. (aset lsum level (+ (aref lsum level)
  15377. (org-column-string-to-number (or val "0") format)))
  15378. (and valflag (aset lflag level t)))
  15379. (t (error "This should not happen")))))))
  15380. (defun org-columns-redo ()
  15381. "Construct the column display again."
  15382. (interactive)
  15383. (message "Recomputing columns...")
  15384. (save-excursion
  15385. (if (marker-position org-columns-begin-marker)
  15386. (goto-char org-columns-begin-marker))
  15387. (org-columns-remove-overlays)
  15388. (if (org-mode-p)
  15389. (call-interactively 'org-columns)
  15390. (call-interactively 'org-agenda-columns)))
  15391. (message "Recomputing columns...done"))
  15392. (defun org-columns-not-in-agenda ()
  15393. (if (eq major-mode 'org-agenda-mode)
  15394. (error "This command is only allowed in Org-mode buffers")))
  15395. (defun org-string-to-number (s)
  15396. "Convert string to number, and interpret hh:mm:ss."
  15397. (if (not (string-match ":" s))
  15398. (string-to-number s)
  15399. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15400. (while l
  15401. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15402. sum)))
  15403. (defun org-column-number-to-string (n fmt printf)
  15404. "Convert a computed column number to a string value, according to FMT."
  15405. (cond
  15406. ((eq fmt 'add_times)
  15407. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15408. (format "%d:%02d" h m)))
  15409. ((eq fmt 'checkbox)
  15410. (cond ((= n (floor n)) "[X]")
  15411. ((> n 1.) "[-]")
  15412. (t "[ ]")))
  15413. (printf (format printf n))
  15414. ((eq fmt 'currency)
  15415. (format "%.2f" n))
  15416. (t (number-to-string n))))
  15417. (defun org-column-string-to-number (s fmt)
  15418. "Convert a column value to a number that can be used for column computing."
  15419. (cond
  15420. ((string-match ":" s)
  15421. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15422. (while l
  15423. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15424. sum))
  15425. ((eq fmt 'checkbox)
  15426. (if (equal s "[X]") 1. 0.000001))
  15427. (t (string-to-number s))))
  15428. (defun org-columns-uncompile-format (cfmt)
  15429. "Turn the compiled columns format back into a string representation."
  15430. (let ((rtn "") e s prop title op width fmt printf)
  15431. (while (setq e (pop cfmt))
  15432. (setq prop (car e)
  15433. title (nth 1 e)
  15434. width (nth 2 e)
  15435. op (nth 3 e)
  15436. fmt (nth 4 e)
  15437. printf (nth 5 e))
  15438. (cond
  15439. ((eq fmt 'add_times) (setq op ":"))
  15440. ((eq fmt 'checkbox) (setq op "X"))
  15441. ((eq fmt 'add_numbers) (setq op "+"))
  15442. ((eq fmt 'currency) (setq op "$")))
  15443. (if (and op printf) (setq op (concat op ";" printf)))
  15444. (if (equal title prop) (setq title nil))
  15445. (setq s (concat "%" (if width (number-to-string width))
  15446. prop
  15447. (if title (concat "(" title ")"))
  15448. (if op (concat "{" op "}"))))
  15449. (setq rtn (concat rtn " " s)))
  15450. (org-trim rtn)))
  15451. (defun org-columns-compile-format (fmt)
  15452. "Turn a column format string into an alist of specifications.
  15453. The alist has one entry for each column in the format. The elements of
  15454. that list are:
  15455. property the property
  15456. title the title field for the columns
  15457. width the column width in characters, can be nil for automatic
  15458. operator the operator if any
  15459. format the output format for computed results, derived from operator
  15460. printf a printf format for computed values"
  15461. (let ((start 0) width prop title op f printf)
  15462. (setq org-columns-current-fmt-compiled nil)
  15463. (while (string-match
  15464. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15465. fmt start)
  15466. (setq start (match-end 0)
  15467. width (match-string 1 fmt)
  15468. prop (match-string 2 fmt)
  15469. title (or (match-string 3 fmt) prop)
  15470. op (match-string 4 fmt)
  15471. f nil
  15472. printf nil)
  15473. (if width (setq width (string-to-number width)))
  15474. (when (and op (string-match ";" op))
  15475. (setq printf (substring op (match-end 0))
  15476. op (substring op 0 (match-beginning 0))))
  15477. (cond
  15478. ((equal op "+") (setq f 'add_numbers))
  15479. ((equal op "$") (setq f 'currency))
  15480. ((equal op ":") (setq f 'add_times))
  15481. ((equal op "X") (setq f 'checkbox)))
  15482. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15483. (setq org-columns-current-fmt-compiled
  15484. (nreverse org-columns-current-fmt-compiled))))
  15485. ;;; Dynamic block for Column view
  15486. (defun org-columns-capture-view ()
  15487. "Get the column view of the current buffer and return it as a list.
  15488. The list will contains the title row and all other rows. Each row is
  15489. a list of fields."
  15490. (save-excursion
  15491. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15492. (n (length title)) row tbl)
  15493. (goto-char (point-min))
  15494. (while (re-search-forward "^\\*+ " nil t)
  15495. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15496. (setq row nil)
  15497. (loop for i from 0 to (1- n) do
  15498. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15499. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15500. "")
  15501. row))
  15502. (setq row (nreverse row))
  15503. (push row tbl)))
  15504. (append (list title 'hline) (nreverse tbl)))))
  15505. (defun org-dblock-write:columnview (params)
  15506. "Write the column view table.
  15507. PARAMS is a property list of parameters:
  15508. :width enforce same column widths with <N> specifiers.
  15509. :id the :ID: property of the entry where the columns view
  15510. should be built, as a string. When `local', call locally.
  15511. When `global' call column view with the cursor at the beginning
  15512. of the buffer (usually this means that the whole buffer switches
  15513. to column view).
  15514. :hlines When t, insert a hline before each item. When a number, insert
  15515. a hline before each level <= that number.
  15516. :vlines When t, make each column a colgroup to enforce vertical lines."
  15517. (let ((pos (move-marker (make-marker) (point)))
  15518. (hlines (plist-get params :hlines))
  15519. (vlines (plist-get params :vlines))
  15520. tbl id idpos nfields tmp)
  15521. (save-excursion
  15522. (save-restriction
  15523. (when (setq id (plist-get params :id))
  15524. (cond ((not id) nil)
  15525. ((eq id 'global) (goto-char (point-min)))
  15526. ((eq id 'local) nil)
  15527. ((setq idpos (org-find-entry-with-id id))
  15528. (goto-char idpos))
  15529. (t (error "Cannot find entry with :ID: %s" id))))
  15530. (org-columns)
  15531. (setq tbl (org-columns-capture-view))
  15532. (setq nfields (length (car tbl)))
  15533. (org-columns-quit)))
  15534. (goto-char pos)
  15535. (move-marker pos nil)
  15536. (when tbl
  15537. (when (plist-get params :hlines)
  15538. (setq tmp nil)
  15539. (while tbl
  15540. (if (eq (car tbl) 'hline)
  15541. (push (pop tbl) tmp)
  15542. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15543. (if (and (not (eq (car tmp) 'hline))
  15544. (or (eq hlines t)
  15545. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15546. (push 'hline tmp)))
  15547. (push (pop tbl) tmp)))
  15548. (setq tbl (nreverse tmp)))
  15549. (when vlines
  15550. (setq tbl (mapcar (lambda (x)
  15551. (if (eq 'hline x) x (cons "" x)))
  15552. tbl))
  15553. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15554. (setq pos (point))
  15555. (insert (org-listtable-to-string tbl))
  15556. (when (plist-get params :width)
  15557. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15558. org-columns-current-widths "|")))
  15559. (goto-char pos)
  15560. (org-table-align))))
  15561. (defun org-listtable-to-string (tbl)
  15562. "Convert a listtable TBL to a string that contains the Org-mode table.
  15563. The table still need to be alligned. The resulting string has no leading
  15564. and tailing newline characters."
  15565. (mapconcat
  15566. (lambda (x)
  15567. (cond
  15568. ((listp x)
  15569. (concat "|" (mapconcat 'identity x "|") "|"))
  15570. ((eq x 'hline) "|-|")
  15571. (t (error "Garbage in listtable: %s" x))))
  15572. tbl "\n"))
  15573. (defun org-insert-columns-dblock ()
  15574. "Create a dynamic block capturing a column view table."
  15575. (interactive)
  15576. (let ((defaults '(:name "columnview" :hlines 1))
  15577. (id (completing-read
  15578. "Capture columns (local, global, entry with :ID: property) [local]: "
  15579. (append '(("global") ("local"))
  15580. (mapcar 'list (org-property-values "ID"))))))
  15581. (if (equal id "") (setq id 'local))
  15582. (if (equal id "global") (setq id 'global))
  15583. (setq defaults (append defaults (list :id id)))
  15584. (org-create-dblock defaults)
  15585. (org-update-dblock)))
  15586. ;;;; Timestamps
  15587. (defvar org-last-changed-timestamp nil)
  15588. (defvar org-time-was-given) ; dynamically scoped parameter
  15589. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15590. (defvar org-ts-what) ; dynamically scoped parameter
  15591. (defun org-time-stamp (arg)
  15592. "Prompt for a date/time and insert a time stamp.
  15593. If the user specifies a time like HH:MM, or if this command is called
  15594. with a prefix argument, the time stamp will contain date and time.
  15595. Otherwise, only the date will be included. All parts of a date not
  15596. specified by the user will be filled in from the current date/time.
  15597. So if you press just return without typing anything, the time stamp
  15598. will represent the current date/time. If there is already a timestamp
  15599. at the cursor, it will be modified."
  15600. (interactive "P")
  15601. (let* ((ts nil)
  15602. (default-time
  15603. ;; Default time is either today, or, when entering a range,
  15604. ;; the range start.
  15605. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15606. (save-excursion
  15607. (re-search-backward
  15608. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15609. (- (point) 20) t)))
  15610. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15611. (current-time)))
  15612. (default-input (and ts (org-get-compact-tod ts)))
  15613. org-time-was-given org-end-time-was-given time)
  15614. (cond
  15615. ((and (org-at-timestamp-p)
  15616. (eq last-command 'org-time-stamp)
  15617. (eq this-command 'org-time-stamp))
  15618. (insert "--")
  15619. (setq time (let ((this-command this-command))
  15620. (org-read-date arg 'totime nil nil default-time default-input)))
  15621. (org-insert-time-stamp time (or org-time-was-given arg)))
  15622. ((org-at-timestamp-p)
  15623. (setq time (let ((this-command this-command))
  15624. (org-read-date arg 'totime nil nil default-time default-input)))
  15625. (when (org-at-timestamp-p) ; just to get the match data
  15626. (replace-match "")
  15627. (setq org-last-changed-timestamp
  15628. (org-insert-time-stamp
  15629. time (or org-time-was-given arg)
  15630. nil nil nil (list org-end-time-was-given))))
  15631. (message "Timestamp updated"))
  15632. (t
  15633. (setq time (let ((this-command this-command))
  15634. (org-read-date arg 'totime nil nil default-time default-input)))
  15635. (org-insert-time-stamp time (or org-time-was-given arg)
  15636. nil nil nil (list org-end-time-was-given))))))
  15637. ;; FIXME: can we use this for something else????
  15638. ;; like computing time differences?????
  15639. (defun org-get-compact-tod (s)
  15640. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15641. (let* ((t1 (match-string 1 s))
  15642. (h1 (string-to-number (match-string 2 s)))
  15643. (m1 (string-to-number (match-string 3 s)))
  15644. (t2 (and (match-end 4) (match-string 5 s)))
  15645. (h2 (and t2 (string-to-number (match-string 6 s))))
  15646. (m2 (and t2 (string-to-number (match-string 7 s))))
  15647. dh dm)
  15648. (if (not t2)
  15649. t1
  15650. (setq dh (- h2 h1) dm (- m2 m1))
  15651. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15652. (concat t1 "+" (number-to-string dh)
  15653. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  15654. (defun org-time-stamp-inactive (&optional arg)
  15655. "Insert an inactive time stamp.
  15656. An inactive time stamp is enclosed in square brackets instead of angle
  15657. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15658. does not link to the calendar and cannot be changed with the S-cursor keys.
  15659. So these are more for recording a certain time/date."
  15660. (interactive "P")
  15661. (let (org-time-was-given org-end-time-was-given time)
  15662. (setq time (org-read-date arg 'totime))
  15663. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  15664. nil nil (list org-end-time-was-given))))
  15665. (defvar org-date-ovl (org-make-overlay 1 1))
  15666. (org-overlay-put org-date-ovl 'face 'org-warning)
  15667. (org-detach-overlay org-date-ovl)
  15668. (defvar org-ans1) ; dynamically scoped parameter
  15669. (defvar org-ans2) ; dynamically scoped parameter
  15670. (defvar org-plain-time-of-day-regexp) ; defined below
  15671. (defvar org-read-date-overlay nil)
  15672. (defvar org-dcst nil) ; dynamically scoped
  15673. (defun org-read-date (&optional with-time to-time from-string prompt
  15674. default-time default-input)
  15675. "Read a date, possibly a time, and make things smooth for the user.
  15676. The prompt will suggest to enter an ISO date, but you can also enter anything
  15677. which will at least partially be understood by `parse-time-string'.
  15678. Unrecognized parts of the date will default to the current day, month, year,
  15679. hour and minute. If this command is called to replace a timestamp at point,
  15680. of to enter the second timestamp of a range, the default time is taken from the
  15681. existing stamp. For example,
  15682. 3-2-5 --> 2003-02-05
  15683. feb 15 --> currentyear-02-15
  15684. sep 12 9 --> 2009-09-12
  15685. 12:45 --> today 12:45
  15686. 22 sept 0:34 --> currentyear-09-22 0:34
  15687. 12 --> currentyear-currentmonth-12
  15688. Fri --> nearest Friday (today or later)
  15689. etc.
  15690. Furthermore you can specify a relative date by giving, as the *first* thing
  15691. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  15692. change in days weeks, months, years.
  15693. With a single plus or minus, the date is relative to today. With a double
  15694. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15695. +4d --> four days from today
  15696. +4 --> same as above
  15697. +2w --> two weeks from today
  15698. ++5 --> five days from default date
  15699. The function understands only English month and weekday abbreviations,
  15700. but this can be configured with the variables `parse-time-months' and
  15701. `parse-time-weekdays'.
  15702. While prompting, a calendar is popped up - you can also select the
  15703. date with the mouse (button 1). The calendar shows a period of three
  15704. months. To scroll it to other months, use the keys `>' and `<'.
  15705. If you don't like the calendar, turn it off with
  15706. \(setq org-read-date-popup-calendar nil)
  15707. With optional argument TO-TIME, the date will immediately be converted
  15708. to an internal time.
  15709. With an optional argument WITH-TIME, the prompt will suggest to also
  15710. insert a time. Note that when WITH-TIME is not set, you can still
  15711. enter a time, and this function will inform the calling routine about
  15712. this change. The calling routine may then choose to change the format
  15713. used to insert the time stamp into the buffer to include the time.
  15714. With optional argument FROM-STRING, read from this string instead from
  15715. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15716. the time/date that is used for everything that is not specified by the
  15717. user."
  15718. (require 'parse-time)
  15719. (let* ((org-time-stamp-rounding-minutes
  15720. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  15721. (org-dcst org-display-custom-times)
  15722. (ct (org-current-time))
  15723. (def (or default-time ct))
  15724. (defdecode (decode-time def))
  15725. (dummy (progn
  15726. (when (< (nth 2 defdecode) org-extend-today-until)
  15727. (setcar (nthcdr 2 defdecode) -1)
  15728. (setcar (nthcdr 1 defdecode) 59)
  15729. (setq def (apply 'encode-time defdecode)
  15730. defdecode (decode-time def)))))
  15731. (calendar-move-hook nil)
  15732. (view-diary-entries-initially nil)
  15733. (view-calendar-holidays-initially nil)
  15734. (timestr (format-time-string
  15735. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  15736. (prompt (concat (if prompt (concat prompt " ") "")
  15737. (format "Date+time [%s]: " timestr)))
  15738. ans (org-ans0 "") org-ans1 org-ans2 final)
  15739. (cond
  15740. (from-string (setq ans from-string))
  15741. (org-read-date-popup-calendar
  15742. (save-excursion
  15743. (save-window-excursion
  15744. (calendar)
  15745. (calendar-forward-day (- (time-to-days def)
  15746. (calendar-absolute-from-gregorian
  15747. (calendar-current-date))))
  15748. (org-eval-in-calendar nil t)
  15749. (let* ((old-map (current-local-map))
  15750. (map (copy-keymap calendar-mode-map))
  15751. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  15752. (org-defkey map (kbd "RET") 'org-calendar-select)
  15753. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  15754. 'org-calendar-select-mouse)
  15755. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  15756. 'org-calendar-select-mouse)
  15757. (org-defkey minibuffer-local-map [(meta shift left)]
  15758. (lambda () (interactive)
  15759. (org-eval-in-calendar '(calendar-backward-month 1))))
  15760. (org-defkey minibuffer-local-map [(meta shift right)]
  15761. (lambda () (interactive)
  15762. (org-eval-in-calendar '(calendar-forward-month 1))))
  15763. (org-defkey minibuffer-local-map [(meta shift up)]
  15764. (lambda () (interactive)
  15765. (org-eval-in-calendar '(calendar-backward-year 1))))
  15766. (org-defkey minibuffer-local-map [(meta shift down)]
  15767. (lambda () (interactive)
  15768. (org-eval-in-calendar '(calendar-forward-year 1))))
  15769. (org-defkey minibuffer-local-map [(shift up)]
  15770. (lambda () (interactive)
  15771. (org-eval-in-calendar '(calendar-backward-week 1))))
  15772. (org-defkey minibuffer-local-map [(shift down)]
  15773. (lambda () (interactive)
  15774. (org-eval-in-calendar '(calendar-forward-week 1))))
  15775. (org-defkey minibuffer-local-map [(shift left)]
  15776. (lambda () (interactive)
  15777. (org-eval-in-calendar '(calendar-backward-day 1))))
  15778. (org-defkey minibuffer-local-map [(shift right)]
  15779. (lambda () (interactive)
  15780. (org-eval-in-calendar '(calendar-forward-day 1))))
  15781. (org-defkey minibuffer-local-map ">"
  15782. (lambda () (interactive)
  15783. (org-eval-in-calendar '(scroll-calendar-left 1))))
  15784. (org-defkey minibuffer-local-map "<"
  15785. (lambda () (interactive)
  15786. (org-eval-in-calendar '(scroll-calendar-right 1))))
  15787. (unwind-protect
  15788. (progn
  15789. (use-local-map map)
  15790. (add-hook 'post-command-hook 'org-read-date-display)
  15791. (setq org-ans0 (read-string prompt default-input nil nil))
  15792. ;; org-ans0: from prompt
  15793. ;; org-ans1: from mouse click
  15794. ;; org-ans2: from calendar motion
  15795. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15796. (remove-hook 'post-command-hook 'org-read-date-display)
  15797. (use-local-map old-map)
  15798. (when org-read-date-overlay
  15799. (org-delete-overlay org-read-date-overlay)
  15800. (setq org-read-date-overlay nil)))))))
  15801. (t ; Naked prompt only
  15802. (unwind-protect
  15803. (setq ans (read-string prompt default-input nil timestr))
  15804. (when org-read-date-overlay
  15805. (org-delete-overlay org-read-date-overlay)
  15806. (setq org-read-date-overlay nil)))))
  15807. (setq final (org-read-date-analyze ans def defdecode))
  15808. (if to-time
  15809. (apply 'encode-time final)
  15810. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15811. (format "%04d-%02d-%02d %02d:%02d"
  15812. (nth 5 final) (nth 4 final) (nth 3 final)
  15813. (nth 2 final) (nth 1 final))
  15814. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15815. (defvar def)
  15816. (defvar defdecode)
  15817. (defvar with-time)
  15818. (defun org-read-date-display ()
  15819. "Display the currrent date prompt interpretation in the minibuffer."
  15820. (when org-read-date-display-live
  15821. (when org-read-date-overlay
  15822. (org-delete-overlay org-read-date-overlay))
  15823. (let ((p (point)))
  15824. (end-of-line 1)
  15825. (while (not (equal (buffer-substring
  15826. (max (point-min) (- (point) 4)) (point))
  15827. " "))
  15828. (insert " "))
  15829. (goto-char p))
  15830. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15831. " " (or org-ans1 org-ans2)))
  15832. (org-end-time-was-given nil)
  15833. (f (org-read-date-analyze ans def defdecode))
  15834. (fmts (if org-dcst
  15835. org-time-stamp-custom-formats
  15836. org-time-stamp-formats))
  15837. (fmt (if (or with-time
  15838. (and (boundp 'org-time-was-given) org-time-was-given))
  15839. (cdr fmts)
  15840. (car fmts)))
  15841. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  15842. (when (and org-end-time-was-given
  15843. (string-match org-plain-time-of-day-regexp txt))
  15844. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15845. org-end-time-was-given
  15846. (substring txt (match-end 0)))))
  15847. (setq org-read-date-overlay
  15848. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15849. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  15850. (defun org-read-date-analyze (ans def defdecode)
  15851. "Analyze the combined answer of the date prompt."
  15852. ;; FIXME: cleanup and comment
  15853. (let (delta deltan deltaw deltadef year month day
  15854. hour minute second wday pm h2 m2 tl wday1)
  15855. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  15856. (setq ans (replace-match "" t t ans)
  15857. deltan (car delta)
  15858. deltaw (nth 1 delta)
  15859. deltadef (nth 2 delta)))
  15860. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  15861. (when (string-match
  15862. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15863. (setq year (if (match-end 2)
  15864. (string-to-number (match-string 2 ans))
  15865. (string-to-number (format-time-string "%Y")))
  15866. month (string-to-number (match-string 3 ans))
  15867. day (string-to-number (match-string 4 ans)))
  15868. (if (< year 100) (setq year (+ 2000 year)))
  15869. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15870. t nil ans)))
  15871. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15872. ;; If there is a time with am/pm, and *no* time without it, we convert
  15873. ;; so that matching will be successful.
  15874. (loop for i from 1 to 2 do ; twice, for end time as well
  15875. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15876. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15877. (setq hour (string-to-number (match-string 1 ans))
  15878. minute (if (match-end 3)
  15879. (string-to-number (match-string 3 ans))
  15880. 0)
  15881. pm (equal ?p
  15882. (string-to-char (downcase (match-string 4 ans)))))
  15883. (if (and (= hour 12) (not pm))
  15884. (setq hour 0)
  15885. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15886. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15887. t t ans))))
  15888. ;; Check if a time range is given as a duration
  15889. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15890. (setq hour (string-to-number (match-string 1 ans))
  15891. h2 (+ hour (string-to-number (match-string 3 ans)))
  15892. minute (string-to-number (match-string 2 ans))
  15893. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  15894. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15895. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  15896. ;; Check if there is a time range
  15897. (when (boundp 'org-end-time-was-given)
  15898. (setq org-time-was-given nil)
  15899. (when (and (string-match org-plain-time-of-day-regexp ans)
  15900. (match-end 8))
  15901. (setq org-end-time-was-given (match-string 8 ans))
  15902. (setq ans (concat (substring ans 0 (match-beginning 7))
  15903. (substring ans (match-end 7))))))
  15904. (setq tl (parse-time-string ans)
  15905. day (or (nth 3 tl) (nth 3 defdecode))
  15906. month (or (nth 4 tl)
  15907. (if (and org-read-date-prefer-future
  15908. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  15909. (1+ (nth 4 defdecode))
  15910. (nth 4 defdecode)))
  15911. year (or (nth 5 tl)
  15912. (if (and org-read-date-prefer-future
  15913. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  15914. (1+ (nth 5 defdecode))
  15915. (nth 5 defdecode)))
  15916. hour (or (nth 2 tl) (nth 2 defdecode))
  15917. minute (or (nth 1 tl) (nth 1 defdecode))
  15918. second (or (nth 0 tl) 0)
  15919. wday (nth 6 tl))
  15920. (when deltan
  15921. (unless deltadef
  15922. (let ((now (decode-time (current-time))))
  15923. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15924. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15925. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15926. ((equal deltaw "m") (setq month (+ month deltan)))
  15927. ((equal deltaw "y") (setq year (+ year deltan)))))
  15928. (when (and wday (not (nth 3 tl)))
  15929. ;; Weekday was given, but no day, so pick that day in the week
  15930. ;; on or after the derived date.
  15931. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15932. (unless (equal wday wday1)
  15933. (setq day (+ day (% (- wday wday1 -7) 7)))))
  15934. (if (and (boundp 'org-time-was-given)
  15935. (nth 2 tl))
  15936. (setq org-time-was-given t))
  15937. (if (< year 100) (setq year (+ 2000 year)))
  15938. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  15939. (list second minute hour day month year)))
  15940. (defvar parse-time-weekdays)
  15941. (defun org-read-date-get-relative (s today default)
  15942. "Check string S for special relative date string.
  15943. TODAY and DEFAULT are internal times, for today and for a default.
  15944. Return shift list (N what def-flag)
  15945. WHAT is \"d\", \"w\", \"m\", or \"y\" for day. week, month, year.
  15946. N is the number if WHATs to shift
  15947. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15948. the DEFAULT date rather than TODAY."
  15949. (when (string-match
  15950. (concat
  15951. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  15952. "\\([0-9]+\\)?"
  15953. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15954. "\\([ \t]\\|$\\)") s)
  15955. (let* ((dir (if (match-end 1)
  15956. (string-to-char (substring (match-string 1 s) -1))
  15957. ?+))
  15958. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15959. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15960. (what (if (match-end 3) (match-string 3 s) "d"))
  15961. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15962. (date (if rel default today))
  15963. (wday (nth 6 (decode-time date)))
  15964. delta)
  15965. (if wday1
  15966. (progn
  15967. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15968. (if (= dir ?-) (setq delta (- delta 7)))
  15969. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15970. (list delta "d" rel))
  15971. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15972. (defun org-eval-in-calendar (form &optional keepdate)
  15973. "Eval FORM in the calendar window and return to current window.
  15974. Also, store the cursor date in variable org-ans2."
  15975. (let ((sw (selected-window)))
  15976. (select-window (get-buffer-window "*Calendar*"))
  15977. (eval form)
  15978. (when (and (not keepdate) (calendar-cursor-to-date))
  15979. (let* ((date (calendar-cursor-to-date))
  15980. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15981. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15982. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15983. (select-window sw)))
  15984. ; ;; Update the prompt to show new default date
  15985. ; (save-excursion
  15986. ; (goto-char (point-min))
  15987. ; (when (and org-ans2
  15988. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  15989. ; (get-text-property (match-end 0) 'field))
  15990. ; (let ((inhibit-read-only t))
  15991. ; (replace-match (concat "[" org-ans2 "]") t t)
  15992. ; (add-text-properties (point-min) (1+ (match-end 0))
  15993. ; (text-properties-at (1+ (point-min)))))))))
  15994. (defun org-calendar-select ()
  15995. "Return to `org-read-date' with the date currently selected.
  15996. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15997. (interactive)
  15998. (when (calendar-cursor-to-date)
  15999. (let* ((date (calendar-cursor-to-date))
  16000. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16001. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16002. (if (active-minibuffer-window) (exit-minibuffer))))
  16003. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16004. "Insert a date stamp for the date given by the internal TIME.
  16005. WITH-HM means, use the stamp format that includes the time of the day.
  16006. INACTIVE means use square brackets instead of angular ones, so that the
  16007. stamp will not contribute to the agenda.
  16008. PRE and POST are optional strings to be inserted before and after the
  16009. stamp.
  16010. The command returns the inserted time stamp."
  16011. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16012. stamp)
  16013. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16014. (insert-before-markers (or pre ""))
  16015. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16016. (when (listp extra)
  16017. (setq extra (car extra))
  16018. (if (and (stringp extra)
  16019. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16020. (setq extra (format "-%02d:%02d"
  16021. (string-to-number (match-string 1 extra))
  16022. (string-to-number (match-string 2 extra))))
  16023. (setq extra nil)))
  16024. (when extra
  16025. (backward-char 1)
  16026. (insert-before-markers extra)
  16027. (forward-char 1))
  16028. (insert-before-markers (or post ""))
  16029. stamp))
  16030. (defun org-toggle-time-stamp-overlays ()
  16031. "Toggle the use of custom time stamp formats."
  16032. (interactive)
  16033. (setq org-display-custom-times (not org-display-custom-times))
  16034. (unless org-display-custom-times
  16035. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16036. (while (setq p (next-single-property-change p 'display))
  16037. (if (and (get-text-property p 'display)
  16038. (eq (get-text-property p 'face) 'org-date))
  16039. (remove-text-properties
  16040. p (setq p (next-single-property-change p 'display))
  16041. '(display t))))
  16042. (set-buffer-modified-p bmp)))
  16043. (if (featurep 'xemacs)
  16044. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16045. (org-restart-font-lock)
  16046. (setq org-table-may-need-update t)
  16047. (if org-display-custom-times
  16048. (message "Time stamps are overlayed with custom format")
  16049. (message "Time stamp overlays removed")))
  16050. (defun org-display-custom-time (beg end)
  16051. "Overlay modified time stamp format over timestamp between BED and END."
  16052. (let* ((ts (buffer-substring beg end))
  16053. t1 w1 with-hm tf time str w2 (off 0))
  16054. (save-match-data
  16055. (setq t1 (org-parse-time-string ts t))
  16056. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16057. (setq off (- (match-end 0) (match-beginning 0)))))
  16058. (setq end (- end off))
  16059. (setq w1 (- end beg)
  16060. with-hm (and (nth 1 t1) (nth 2 t1))
  16061. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16062. time (org-fix-decoded-time t1)
  16063. str (org-add-props
  16064. (format-time-string
  16065. (substring tf 1 -1) (apply 'encode-time time))
  16066. nil 'mouse-face 'highlight)
  16067. w2 (length str))
  16068. (if (not (= w2 w1))
  16069. (add-text-properties (1+ beg) (+ 2 beg)
  16070. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16071. (if (featurep 'xemacs)
  16072. (progn
  16073. (put-text-property beg end 'invisible t)
  16074. (put-text-property beg end 'end-glyph (make-glyph str)))
  16075. (put-text-property beg end 'display str))))
  16076. (defun org-translate-time (string)
  16077. "Translate all timestamps in STRING to custom format.
  16078. But do this only if the variable `org-display-custom-times' is set."
  16079. (when org-display-custom-times
  16080. (save-match-data
  16081. (let* ((start 0)
  16082. (re org-ts-regexp-both)
  16083. t1 with-hm inactive tf time str beg end)
  16084. (while (setq start (string-match re string start))
  16085. (setq beg (match-beginning 0)
  16086. end (match-end 0)
  16087. t1 (save-match-data
  16088. (org-parse-time-string (substring string beg end) t))
  16089. with-hm (and (nth 1 t1) (nth 2 t1))
  16090. inactive (equal (substring string beg (1+ beg)) "[")
  16091. tf (funcall (if with-hm 'cdr 'car)
  16092. org-time-stamp-custom-formats)
  16093. time (org-fix-decoded-time t1)
  16094. str (format-time-string
  16095. (concat
  16096. (if inactive "[" "<") (substring tf 1 -1)
  16097. (if inactive "]" ">"))
  16098. (apply 'encode-time time))
  16099. string (replace-match str t t string)
  16100. start (+ start (length str)))))))
  16101. string)
  16102. (defun org-fix-decoded-time (time)
  16103. "Set 0 instead of nil for the first 6 elements of time.
  16104. Don't touch the rest."
  16105. (let ((n 0))
  16106. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16107. (defun org-days-to-time (timestamp-string)
  16108. "Difference between TIMESTAMP-STRING and now in days."
  16109. (- (time-to-days (org-time-string-to-time timestamp-string))
  16110. (time-to-days (current-time))))
  16111. (defun org-deadline-close (timestamp-string &optional ndays)
  16112. "Is the time in TIMESTAMP-STRING close to the current date?"
  16113. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16114. (and (< (org-days-to-time timestamp-string) ndays)
  16115. (not (org-entry-is-done-p))))
  16116. (defun org-get-wdays (ts)
  16117. "Get the deadline lead time appropriate for timestring TS."
  16118. (cond
  16119. ((<= org-deadline-warning-days 0)
  16120. ;; 0 or negative, enforce this value no matter what
  16121. (- org-deadline-warning-days))
  16122. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16123. ;; lead time is specified.
  16124. (floor (* (string-to-number (match-string 1 ts))
  16125. (cdr (assoc (match-string 2 ts)
  16126. '(("d" . 1) ("w" . 7)
  16127. ("m" . 30.4) ("y" . 365.25)))))))
  16128. ;; go for the default.
  16129. (t org-deadline-warning-days)))
  16130. (defun org-calendar-select-mouse (ev)
  16131. "Return to `org-read-date' with the date currently selected.
  16132. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16133. (interactive "e")
  16134. (mouse-set-point ev)
  16135. (when (calendar-cursor-to-date)
  16136. (let* ((date (calendar-cursor-to-date))
  16137. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16138. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16139. (if (active-minibuffer-window) (exit-minibuffer))))
  16140. (defun org-check-deadlines (ndays)
  16141. "Check if there are any deadlines due or past due.
  16142. A deadline is considered due if it happens within `org-deadline-warning-days'
  16143. days from today's date. If the deadline appears in an entry marked DONE,
  16144. it is not shown. The prefix arg NDAYS can be used to test that many
  16145. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16146. (interactive "P")
  16147. (let* ((org-warn-days
  16148. (cond
  16149. ((equal ndays '(4)) 100000)
  16150. (ndays (prefix-numeric-value ndays))
  16151. (t (abs org-deadline-warning-days))))
  16152. (case-fold-search nil)
  16153. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16154. (callback
  16155. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16156. (message "%d deadlines past-due or due within %d days"
  16157. (org-occur regexp nil callback)
  16158. org-warn-days)))
  16159. (defun org-check-before-date (date)
  16160. "Check if there are deadlines or scheduled entries before DATE."
  16161. (interactive (list (org-read-date)))
  16162. (let ((case-fold-search nil)
  16163. (regexp (concat "\\<\\(" org-deadline-string
  16164. "\\|" org-scheduled-string
  16165. "\\) *<\\([^>]+\\)>"))
  16166. (callback
  16167. (lambda () (time-less-p
  16168. (org-time-string-to-time (match-string 2))
  16169. (org-time-string-to-time date)))))
  16170. (message "%d entries before %s"
  16171. (org-occur regexp nil callback) date)))
  16172. (defun org-evaluate-time-range (&optional to-buffer)
  16173. "Evaluate a time range by computing the difference between start and end.
  16174. Normally the result is just printed in the echo area, but with prefix arg
  16175. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16176. If the time range is actually in a table, the result is inserted into the
  16177. next column.
  16178. For time difference computation, a year is assumed to be exactly 365
  16179. days in order to avoid rounding problems."
  16180. (interactive "P")
  16181. (or
  16182. (org-clock-update-time-maybe)
  16183. (save-excursion
  16184. (unless (org-at-date-range-p t)
  16185. (goto-char (point-at-bol))
  16186. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16187. (if (not (org-at-date-range-p t))
  16188. (error "Not at a time-stamp range, and none found in current line")))
  16189. (let* ((ts1 (match-string 1))
  16190. (ts2 (match-string 2))
  16191. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16192. (match-end (match-end 0))
  16193. (time1 (org-time-string-to-time ts1))
  16194. (time2 (org-time-string-to-time ts2))
  16195. (t1 (time-to-seconds time1))
  16196. (t2 (time-to-seconds time2))
  16197. (diff (abs (- t2 t1)))
  16198. (negative (< (- t2 t1) 0))
  16199. ;; (ys (floor (* 365 24 60 60)))
  16200. (ds (* 24 60 60))
  16201. (hs (* 60 60))
  16202. (fy "%dy %dd %02d:%02d")
  16203. (fy1 "%dy %dd")
  16204. (fd "%dd %02d:%02d")
  16205. (fd1 "%dd")
  16206. (fh "%02d:%02d")
  16207. y d h m align)
  16208. (if havetime
  16209. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16210. y 0
  16211. d (floor (/ diff ds)) diff (mod diff ds)
  16212. h (floor (/ diff hs)) diff (mod diff hs)
  16213. m (floor (/ diff 60)))
  16214. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16215. y 0
  16216. d (floor (+ (/ diff ds) 0.5))
  16217. h 0 m 0))
  16218. (if (not to-buffer)
  16219. (message "%s" (org-make-tdiff-string y d h m))
  16220. (if (org-at-table-p)
  16221. (progn
  16222. (goto-char match-end)
  16223. (setq align t)
  16224. (and (looking-at " *|") (goto-char (match-end 0))))
  16225. (goto-char match-end))
  16226. (if (looking-at
  16227. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16228. (replace-match ""))
  16229. (if negative (insert " -"))
  16230. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16231. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16232. (insert " " (format fh h m))))
  16233. (if align (org-table-align))
  16234. (message "Time difference inserted")))))
  16235. (defun org-make-tdiff-string (y d h m)
  16236. (let ((fmt "")
  16237. (l nil))
  16238. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16239. l (push y l)))
  16240. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16241. l (push d l)))
  16242. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16243. l (push h l)))
  16244. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16245. l (push m l)))
  16246. (apply 'format fmt (nreverse l))))
  16247. (defun org-time-string-to-time (s)
  16248. (apply 'encode-time (org-parse-time-string s)))
  16249. (defun org-time-string-to-absolute (s &optional daynr)
  16250. "Convert a time stamp to an absolute day number.
  16251. If there is a specifyer for a cyclic time stamp, get the closest date to
  16252. DAYNR."
  16253. (cond
  16254. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16255. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16256. daynr
  16257. (+ daynr 1000)))
  16258. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16259. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16260. (time-to-days (current-time))) (match-string 0 s)))
  16261. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16262. (defun org-time-from-absolute (d)
  16263. "Return the time corresponding to date D.
  16264. D may be an absolute day number, or a calendar-type list (month day year)."
  16265. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16266. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16267. (defun org-calendar-holiday ()
  16268. "List of holidays, for Diary display in Org-mode."
  16269. (require 'holidays)
  16270. (let ((hl (funcall
  16271. (if (fboundp 'calendar-check-holidays)
  16272. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16273. (if hl (mapconcat 'identity hl "; "))))
  16274. (defun org-diary-sexp-entry (sexp entry date)
  16275. "Process a SEXP diary ENTRY for DATE."
  16276. (require 'diary-lib)
  16277. (let ((result (if calendar-debug-sexp
  16278. (let ((stack-trace-on-error t))
  16279. (eval (car (read-from-string sexp))))
  16280. (condition-case nil
  16281. (eval (car (read-from-string sexp)))
  16282. (error
  16283. (beep)
  16284. (message "Bad sexp at line %d in %s: %s"
  16285. (org-current-line)
  16286. (buffer-file-name) sexp)
  16287. (sleep-for 2))))))
  16288. (cond ((stringp result) result)
  16289. ((and (consp result)
  16290. (stringp (cdr result))) (cdr result))
  16291. (result entry)
  16292. (t nil))))
  16293. (defun org-diary-to-ical-string (frombuf)
  16294. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16295. This uses the icalendar.el library."
  16296. (let* ((tmpdir (if (featurep 'xemacs)
  16297. (temp-directory)
  16298. temporary-file-directory))
  16299. (tmpfile (make-temp-name
  16300. (expand-file-name "orgics" tmpdir)))
  16301. buf rtn b e)
  16302. (save-excursion
  16303. (set-buffer frombuf)
  16304. (icalendar-export-region (point-min) (point-max) tmpfile)
  16305. (setq buf (find-buffer-visiting tmpfile))
  16306. (set-buffer buf)
  16307. (goto-char (point-min))
  16308. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16309. (setq b (match-beginning 0)))
  16310. (goto-char (point-max))
  16311. (if (re-search-backward "^END:VEVENT" nil t)
  16312. (setq e (match-end 0)))
  16313. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16314. (kill-buffer buf)
  16315. (kill-buffer frombuf)
  16316. (delete-file tmpfile)
  16317. rtn))
  16318. (defun org-closest-date (start current change)
  16319. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  16320. ;; Make the proper lists from the dates
  16321. (catch 'exit
  16322. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16323. dn dw sday cday n1 n2
  16324. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16325. (setq start (org-date-to-gregorian start)
  16326. current (org-date-to-gregorian
  16327. (if org-agenda-repeating-timestamp-show-all
  16328. current
  16329. (time-to-days (current-time))))
  16330. sday (calendar-absolute-from-gregorian start)
  16331. cday (calendar-absolute-from-gregorian current))
  16332. (if (<= cday sday) (throw 'exit sday))
  16333. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16334. (setq dn (string-to-number (match-string 1 change))
  16335. dw (cdr (assoc (match-string 2 change) a1)))
  16336. (error "Invalid change specifyer: %s" change))
  16337. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16338. (cond
  16339. ((eq dw 'day)
  16340. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16341. n2 (+ n1 dn)))
  16342. ((eq dw 'year)
  16343. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16344. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16345. (setq date1 (list m d y1)
  16346. n1 (calendar-absolute-from-gregorian date1)
  16347. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16348. n2 (calendar-absolute-from-gregorian date2)))
  16349. ((eq dw 'month)
  16350. ;; approx number of month between the tow dates
  16351. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16352. ;; How often does dn fit in there?
  16353. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16354. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16355. m (+ m nm)
  16356. ny (floor (/ m 12))
  16357. y (+ y ny)
  16358. m (- m (* ny 12)))
  16359. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16360. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16361. (setq m2 (+ m dn) y2 y)
  16362. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16363. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16364. (while (< n2 cday)
  16365. (setq n1 n2 m m2 y y2)
  16366. (setq m2 (+ m dn) y2 y)
  16367. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16368. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16369. (if org-agenda-repeating-timestamp-show-all
  16370. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  16371. (if (= cday n1) n1 n2)))))
  16372. (defun org-date-to-gregorian (date)
  16373. "Turn any specification of DATE into a gregorian date for the calendar."
  16374. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16375. ((and (listp date) (= (length date) 3)) date)
  16376. ((stringp date)
  16377. (setq date (org-parse-time-string date))
  16378. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16379. ((listp date)
  16380. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16381. (defun org-parse-time-string (s &optional nodefault)
  16382. "Parse the standard Org-mode time string.
  16383. This should be a lot faster than the normal `parse-time-string'.
  16384. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16385. hour and minute fields will be nil if not given."
  16386. (if (string-match org-ts-regexp0 s)
  16387. (list 0
  16388. (if (or (match-beginning 8) (not nodefault))
  16389. (string-to-number (or (match-string 8 s) "0")))
  16390. (if (or (match-beginning 7) (not nodefault))
  16391. (string-to-number (or (match-string 7 s) "0")))
  16392. (string-to-number (match-string 4 s))
  16393. (string-to-number (match-string 3 s))
  16394. (string-to-number (match-string 2 s))
  16395. nil nil nil)
  16396. (make-list 9 0)))
  16397. (defun org-timestamp-up (&optional arg)
  16398. "Increase the date item at the cursor by one.
  16399. If the cursor is on the year, change the year. If it is on the month or
  16400. the day, change that.
  16401. With prefix ARG, change by that many units."
  16402. (interactive "p")
  16403. (org-timestamp-change (prefix-numeric-value arg)))
  16404. (defun org-timestamp-down (&optional arg)
  16405. "Decrease the date item at the cursor by one.
  16406. If the cursor is on the year, change the year. If it is on the month or
  16407. the day, change that.
  16408. With prefix ARG, change by that many units."
  16409. (interactive "p")
  16410. (org-timestamp-change (- (prefix-numeric-value arg))))
  16411. (defun org-timestamp-up-day (&optional arg)
  16412. "Increase the date in the time stamp by one day.
  16413. With prefix ARG, change that many days."
  16414. (interactive "p")
  16415. (if (and (not (org-at-timestamp-p t))
  16416. (org-on-heading-p))
  16417. (org-todo 'up)
  16418. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16419. (defun org-timestamp-down-day (&optional arg)
  16420. "Decrease the date in the time stamp by one day.
  16421. With prefix ARG, change that many days."
  16422. (interactive "p")
  16423. (if (and (not (org-at-timestamp-p t))
  16424. (org-on-heading-p))
  16425. (org-todo 'down)
  16426. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16427. (defsubst org-pos-in-match-range (pos n)
  16428. (and (match-beginning n)
  16429. (<= (match-beginning n) pos)
  16430. (>= (match-end n) pos)))
  16431. (defun org-at-timestamp-p (&optional inactive-ok)
  16432. "Determine if the cursor is in or at a timestamp."
  16433. (interactive)
  16434. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16435. (pos (point))
  16436. (ans (or (looking-at tsr)
  16437. (save-excursion
  16438. (skip-chars-backward "^[<\n\r\t")
  16439. (if (> (point) (point-min)) (backward-char 1))
  16440. (and (looking-at tsr)
  16441. (> (- (match-end 0) pos) -1))))))
  16442. (and ans
  16443. (boundp 'org-ts-what)
  16444. (setq org-ts-what
  16445. (cond
  16446. ((= pos (match-beginning 0)) 'bracket)
  16447. ((= pos (1- (match-end 0))) 'bracket)
  16448. ((org-pos-in-match-range pos 2) 'year)
  16449. ((org-pos-in-match-range pos 3) 'month)
  16450. ((org-pos-in-match-range pos 7) 'hour)
  16451. ((org-pos-in-match-range pos 8) 'minute)
  16452. ((or (org-pos-in-match-range pos 4)
  16453. (org-pos-in-match-range pos 5)) 'day)
  16454. ((and (> pos (or (match-end 8) (match-end 5)))
  16455. (< pos (match-end 0)))
  16456. (- pos (or (match-end 8) (match-end 5))))
  16457. (t 'day))))
  16458. ans))
  16459. (defun org-toggle-timestamp-type ()
  16460. ""
  16461. (interactive)
  16462. (when (org-at-timestamp-p t)
  16463. (save-excursion
  16464. (goto-char (match-beginning 0))
  16465. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16466. (goto-char (1- (match-end 0)))
  16467. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16468. (message "Timestamp is now %sactive"
  16469. (if (equal (char-before) ?>) "in" ""))))
  16470. (defun org-timestamp-change (n &optional what)
  16471. "Change the date in the time stamp at point.
  16472. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16473. `year', `minute', `second'. If WHAT is not given, the cursor position
  16474. in the timestamp determines what will be changed."
  16475. (let ((pos (point))
  16476. with-hm inactive
  16477. org-ts-what
  16478. extra
  16479. ts time time0)
  16480. (if (not (org-at-timestamp-p t))
  16481. (error "Not at a timestamp"))
  16482. (if (and (not what) (eq org-ts-what 'bracket))
  16483. (org-toggle-timestamp-type)
  16484. (if (and (not what) (not (eq org-ts-what 'day))
  16485. org-display-custom-times
  16486. (get-text-property (point) 'display)
  16487. (not (get-text-property (1- (point)) 'display)))
  16488. (setq org-ts-what 'day))
  16489. (setq org-ts-what (or what org-ts-what)
  16490. inactive (= (char-after (match-beginning 0)) ?\[)
  16491. ts (match-string 0))
  16492. (replace-match "")
  16493. (if (string-match
  16494. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16495. ts)
  16496. (setq extra (match-string 1 ts)))
  16497. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16498. (setq with-hm t))
  16499. (setq time0 (org-parse-time-string ts))
  16500. (setq time
  16501. (encode-time (or (car time0) 0)
  16502. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16503. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16504. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16505. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16506. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16507. (nthcdr 6 time0)))
  16508. (when (integerp org-ts-what)
  16509. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16510. (if (eq what 'calendar)
  16511. (let ((cal-date (org-get-date-from-calendar)))
  16512. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16513. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16514. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16515. (setcar time0 (or (car time0) 0))
  16516. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16517. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16518. (setq time (apply 'encode-time time0))))
  16519. (setq org-last-changed-timestamp
  16520. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16521. (org-clock-update-time-maybe)
  16522. (goto-char pos)
  16523. ;; Try to recenter the calendar window, if any
  16524. (if (and org-calendar-follow-timestamp-change
  16525. (get-buffer-window "*Calendar*" t)
  16526. (memq org-ts-what '(day month year)))
  16527. (org-recenter-calendar (time-to-days time))))))
  16528. ;; FIXME: does not yet work for lead times
  16529. (defun org-modify-ts-extra (s pos n)
  16530. "Change the different parts of the lead-time and repeat fields in timestamp."
  16531. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16532. ng h m new)
  16533. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16534. (cond
  16535. ((or (org-pos-in-match-range pos 2)
  16536. (org-pos-in-match-range pos 3))
  16537. (setq m (string-to-number (match-string 3 s))
  16538. h (string-to-number (match-string 2 s)))
  16539. (if (org-pos-in-match-range pos 2)
  16540. (setq h (+ h n))
  16541. (setq m (+ m n)))
  16542. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16543. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16544. (setq h (min 24 (max 0 h)))
  16545. (setq ng 1 new (format "-%02d:%02d" h m)))
  16546. ((org-pos-in-match-range pos 6)
  16547. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16548. ((org-pos-in-match-range pos 5)
  16549. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16550. (when ng
  16551. (setq s (concat
  16552. (substring s 0 (match-beginning ng))
  16553. new
  16554. (substring s (match-end ng))))))
  16555. s))
  16556. (defun org-recenter-calendar (date)
  16557. "If the calendar is visible, recenter it to DATE."
  16558. (let* ((win (selected-window))
  16559. (cwin (get-buffer-window "*Calendar*" t))
  16560. (calendar-move-hook nil))
  16561. (when cwin
  16562. (select-window cwin)
  16563. (calendar-goto-date (if (listp date) date
  16564. (calendar-gregorian-from-absolute date)))
  16565. (select-window win))))
  16566. (defun org-goto-calendar (&optional arg)
  16567. "Go to the Emacs calendar at the current date.
  16568. If there is a time stamp in the current line, go to that date.
  16569. A prefix ARG can be used to force the current date."
  16570. (interactive "P")
  16571. (let ((tsr org-ts-regexp) diff
  16572. (calendar-move-hook nil)
  16573. (view-calendar-holidays-initially nil)
  16574. (view-diary-entries-initially nil))
  16575. (if (or (org-at-timestamp-p)
  16576. (save-excursion
  16577. (beginning-of-line 1)
  16578. (looking-at (concat ".*" tsr))))
  16579. (let ((d1 (time-to-days (current-time)))
  16580. (d2 (time-to-days
  16581. (org-time-string-to-time (match-string 1)))))
  16582. (setq diff (- d2 d1))))
  16583. (calendar)
  16584. (calendar-goto-today)
  16585. (if (and diff (not arg)) (calendar-forward-day diff))))
  16586. (defun org-get-date-from-calendar ()
  16587. "Return a list (month day year) of date at point in calendar."
  16588. (with-current-buffer "*Calendar*"
  16589. (save-match-data
  16590. (calendar-cursor-to-date))))
  16591. (defun org-date-from-calendar ()
  16592. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16593. If there is already a time stamp at the cursor position, update it."
  16594. (interactive)
  16595. (if (org-at-timestamp-p t)
  16596. (org-timestamp-change 0 'calendar)
  16597. (let ((cal-date (org-get-date-from-calendar)))
  16598. (org-insert-time-stamp
  16599. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16600. ;; Make appt aware of appointments from the agenda
  16601. ;;;###autoload
  16602. (defun org-agenda-to-appt (&optional filter)
  16603. "Activate appointments found in `org-agenda-files'.
  16604. When prefixed, prompt for a regular expression and use it as a
  16605. filter: only add entries if they match this regular expression.
  16606. FILTER can be a string. In this case, use this string as a
  16607. regular expression to filter results.
  16608. FILTER can also be an alist, with the car of each cell being
  16609. either 'headline or 'category. For example:
  16610. '((headline \"IMPORTANT\")
  16611. (category \"Work\"))
  16612. will only add headlines containing IMPORTANT or headlines
  16613. belonging to the category \"Work\"."
  16614. (interactive "P")
  16615. (require 'calendar)
  16616. (if (equal filter '(4))
  16617. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16618. (let* ((cnt 0) ; count added events
  16619. (org-agenda-new-buffers nil)
  16620. (today (org-date-to-gregorian
  16621. (time-to-days (current-time))))
  16622. (files (org-agenda-files)) entries file)
  16623. ;; Get all entries which may contain an appt
  16624. (while (setq file (pop files))
  16625. (setq entries
  16626. (append entries
  16627. (org-agenda-get-day-entries
  16628. file today
  16629. :timestamp :scheduled :deadline))))
  16630. (setq entries (delq nil entries))
  16631. ;; Map thru entries and find if they pass thru the filter
  16632. (mapc
  16633. (lambda(x)
  16634. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  16635. (cat (get-text-property 1 'org-category x))
  16636. (tod (get-text-property 1 'time-of-day x))
  16637. (ok (or (null filter)
  16638. (and (stringp filter) (string-match filter evt))
  16639. (and (listp filter)
  16640. (or (string-match
  16641. (cadr (assoc 'category filter)) cat)
  16642. (string-match
  16643. (cadr (assoc 'headline filter)) evt))))))
  16644. ;; FIXME: Shall we remove text-properties for the appt text?
  16645. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  16646. (when (and ok tod)
  16647. (setq tod (number-to-string tod)
  16648. tod (when (string-match
  16649. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  16650. (concat (match-string 1 tod) ":"
  16651. (match-string 2 tod))))
  16652. (appt-add tod evt)
  16653. (setq cnt (1+ cnt))))) entries)
  16654. (org-release-buffers org-agenda-new-buffers)
  16655. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  16656. ;;; The clock for measuring work time.
  16657. (defvar org-mode-line-string "")
  16658. (put 'org-mode-line-string 'risky-local-variable t)
  16659. (defvar org-mode-line-timer nil)
  16660. (defvar org-clock-heading "")
  16661. (defvar org-clock-start-time "")
  16662. (defun org-update-mode-line ()
  16663. (let* ((delta (- (time-to-seconds (current-time))
  16664. (time-to-seconds org-clock-start-time)))
  16665. (h (floor delta 3600))
  16666. (m (floor (- delta (* 3600 h)) 60)))
  16667. (setq org-mode-line-string
  16668. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  16669. 'help-echo "Org-mode clock is running"))
  16670. (force-mode-line-update)))
  16671. (defvar org-clock-marker (make-marker)
  16672. "Marker recording the last clock-in.")
  16673. (defvar org-clock-mode-line-entry nil
  16674. "Information for the modeline about the running clock.")
  16675. (defun org-clock-in ()
  16676. "Start the clock on the current item.
  16677. If necessary, clock-out of the currently active clock."
  16678. (interactive)
  16679. (org-clock-out t)
  16680. (let (ts)
  16681. (save-excursion
  16682. (org-back-to-heading t)
  16683. (if (and org-clock-heading-function
  16684. (functionp org-clock-heading-function))
  16685. (setq org-clock-heading (funcall org-clock-heading-function))
  16686. (if (looking-at org-complex-heading-regexp)
  16687. (setq org-clock-heading (match-string 4))
  16688. (setq org-clock-heading "???")))
  16689. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  16690. (org-clock-find-position)
  16691. (insert "\n") (backward-char 1)
  16692. (indent-relative)
  16693. (insert org-clock-string " ")
  16694. (setq org-clock-start-time (current-time))
  16695. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16696. (move-marker org-clock-marker (point) (buffer-base-buffer))
  16697. (or global-mode-string (setq global-mode-string '("")))
  16698. (or (memq 'org-mode-line-string global-mode-string)
  16699. (setq global-mode-string
  16700. (append global-mode-string '(org-mode-line-string))))
  16701. (org-update-mode-line)
  16702. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  16703. (message "Clock started at %s" ts))))
  16704. (defun org-clock-find-position ()
  16705. "Find the location where the next clock line should be inserted."
  16706. (org-back-to-heading t)
  16707. (catch 'exit
  16708. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  16709. (re (concat "^[ \t]*" org-clock-string))
  16710. (cnt 0)
  16711. first last)
  16712. (goto-char beg)
  16713. (when (eobp) (newline) (setq end (max (point) end)))
  16714. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  16715. ;; we seem to have a CLOCK drawer, so go there.
  16716. (beginning-of-line 2)
  16717. (throw 'exit t))
  16718. ;; Lets count the CLOCK lines
  16719. (goto-char beg)
  16720. (while (re-search-forward re end t)
  16721. (setq first (or first (match-beginning 0))
  16722. last (match-beginning 0)
  16723. cnt (1+ cnt)))
  16724. (when (and (integerp org-clock-into-drawer)
  16725. (>= (1+ cnt) org-clock-into-drawer))
  16726. ;; Wrap current entries into a new drawer
  16727. (goto-char last)
  16728. (beginning-of-line 2)
  16729. (if (org-at-item-p) (org-end-of-item))
  16730. (insert ":END:\n")
  16731. (beginning-of-line 0)
  16732. (org-indent-line-function)
  16733. (goto-char first)
  16734. (insert ":CLOCK:\n")
  16735. (beginning-of-line 0)
  16736. (org-indent-line-function)
  16737. (org-flag-drawer t)
  16738. (beginning-of-line 2)
  16739. (throw 'exit nil))
  16740. (goto-char beg)
  16741. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16742. (not (equal (match-string 1) org-clock-string)))
  16743. ;; Planning info, skip to after it
  16744. (beginning-of-line 2)
  16745. (or (bolp) (newline)))
  16746. (when (eq t org-clock-into-drawer)
  16747. (insert ":CLOCK:\n:END:\n")
  16748. (beginning-of-line -1)
  16749. (org-indent-line-function)
  16750. (org-flag-drawer t)
  16751. (beginning-of-line 2)
  16752. (org-indent-line-function)))))
  16753. (defun org-clock-out (&optional fail-quietly)
  16754. "Stop the currently running clock.
  16755. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  16756. (interactive)
  16757. (catch 'exit
  16758. (if (not (marker-buffer org-clock-marker))
  16759. (if fail-quietly (throw 'exit t) (error "No active clock")))
  16760. (let (ts te s h m)
  16761. (save-excursion
  16762. (set-buffer (marker-buffer org-clock-marker))
  16763. (goto-char org-clock-marker)
  16764. (beginning-of-line 1)
  16765. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16766. (equal (match-string 1) org-clock-string))
  16767. (setq ts (match-string 2))
  16768. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  16769. (goto-char (match-end 0))
  16770. (delete-region (point) (point-at-eol))
  16771. (insert "--")
  16772. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16773. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  16774. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  16775. h (floor (/ s 3600))
  16776. s (- s (* 3600 h))
  16777. m (floor (/ s 60))
  16778. s (- s (* 60 s)))
  16779. (insert " => " (format "%2d:%02d" h m))
  16780. (move-marker org-clock-marker nil)
  16781. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  16782. (org-log-done (org-parse-local-options logging 'org-log-done))
  16783. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  16784. (org-add-log-maybe 'clock-out))
  16785. (when org-mode-line-timer
  16786. (cancel-timer org-mode-line-timer)
  16787. (setq org-mode-line-timer nil))
  16788. (setq global-mode-string
  16789. (delq 'org-mode-line-string global-mode-string))
  16790. (force-mode-line-update)
  16791. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  16792. (defun org-clock-cancel ()
  16793. "Cancel the running clock be removing the start timestamp."
  16794. (interactive)
  16795. (if (not (marker-buffer org-clock-marker))
  16796. (error "No active clock"))
  16797. (save-excursion
  16798. (set-buffer (marker-buffer org-clock-marker))
  16799. (goto-char org-clock-marker)
  16800. (delete-region (1- (point-at-bol)) (point-at-eol)))
  16801. (setq global-mode-string
  16802. (delq 'org-mode-line-string global-mode-string))
  16803. (force-mode-line-update)
  16804. (message "Clock canceled"))
  16805. (defun org-clock-goto (&optional delete-windows)
  16806. "Go to the currently clocked-in entry."
  16807. (interactive "P")
  16808. (if (not (marker-buffer org-clock-marker))
  16809. (error "No active clock"))
  16810. (switch-to-buffer-other-window
  16811. (marker-buffer org-clock-marker))
  16812. (if delete-windows (delete-other-windows))
  16813. (goto-char org-clock-marker)
  16814. (org-show-entry)
  16815. (org-back-to-heading)
  16816. (recenter))
  16817. (defvar org-clock-file-total-minutes nil
  16818. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  16819. (make-variable-buffer-local 'org-clock-file-total-minutes)
  16820. (defun org-clock-sum (&optional tstart tend)
  16821. "Sum the times for each subtree.
  16822. Puts the resulting times in minutes as a text property on each headline."
  16823. (interactive)
  16824. (let* ((bmp (buffer-modified-p))
  16825. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  16826. org-clock-string
  16827. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  16828. (lmax 30)
  16829. (ltimes (make-vector lmax 0))
  16830. (t1 0)
  16831. (level 0)
  16832. ts te dt
  16833. time)
  16834. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  16835. (save-excursion
  16836. (goto-char (point-max))
  16837. (while (re-search-backward re nil t)
  16838. (cond
  16839. ((match-end 2)
  16840. ;; Two time stamps
  16841. (setq ts (match-string 2)
  16842. te (match-string 3)
  16843. ts (time-to-seconds
  16844. (apply 'encode-time (org-parse-time-string ts)))
  16845. te (time-to-seconds
  16846. (apply 'encode-time (org-parse-time-string te)))
  16847. ts (if tstart (max ts tstart) ts)
  16848. te (if tend (min te tend) te)
  16849. dt (- te ts)
  16850. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  16851. ((match-end 4)
  16852. ;; A naket time
  16853. (setq t1 (+ t1 (string-to-number (match-string 5))
  16854. (* 60 (string-to-number (match-string 4))))))
  16855. (t ;; A headline
  16856. (setq level (- (match-end 1) (match-beginning 1)))
  16857. (when (or (> t1 0) (> (aref ltimes level) 0))
  16858. (loop for l from 0 to level do
  16859. (aset ltimes l (+ (aref ltimes l) t1)))
  16860. (setq t1 0 time (aref ltimes level))
  16861. (loop for l from level to (1- lmax) do
  16862. (aset ltimes l 0))
  16863. (goto-char (match-beginning 0))
  16864. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  16865. (setq org-clock-file-total-minutes (aref ltimes 0)))
  16866. (set-buffer-modified-p bmp)))
  16867. (defun org-clock-display (&optional total-only)
  16868. "Show subtree times in the entire buffer.
  16869. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  16870. in the echo area."
  16871. (interactive)
  16872. (org-remove-clock-overlays)
  16873. (let (time h m p)
  16874. (org-clock-sum)
  16875. (unless total-only
  16876. (save-excursion
  16877. (goto-char (point-min))
  16878. (while (or (and (equal (setq p (point)) (point-min))
  16879. (get-text-property p :org-clock-minutes))
  16880. (setq p (next-single-property-change
  16881. (point) :org-clock-minutes)))
  16882. (goto-char p)
  16883. (when (setq time (get-text-property p :org-clock-minutes))
  16884. (org-put-clock-overlay time (funcall outline-level))))
  16885. (setq h (/ org-clock-file-total-minutes 60)
  16886. m (- org-clock-file-total-minutes (* 60 h)))
  16887. ;; Arrange to remove the overlays upon next change.
  16888. (when org-remove-highlights-with-change
  16889. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  16890. nil 'local))))
  16891. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  16892. (defvar org-clock-overlays nil)
  16893. (make-variable-buffer-local 'org-clock-overlays)
  16894. (defun org-put-clock-overlay (time &optional level)
  16895. "Put an overlays on the current line, displaying TIME.
  16896. If LEVEL is given, prefix time with a corresponding number of stars.
  16897. This creates a new overlay and stores it in `org-clock-overlays', so that it
  16898. will be easy to remove."
  16899. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  16900. (l (if level (org-get-legal-level level 0) 0))
  16901. (off 0)
  16902. ov tx)
  16903. (move-to-column c)
  16904. (unless (eolp) (skip-chars-backward "^ \t"))
  16905. (skip-chars-backward " \t")
  16906. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  16907. tx (concat (buffer-substring (1- (point)) (point))
  16908. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  16909. (org-add-props (format "%s %2d:%02d%s"
  16910. (make-string l ?*) h m
  16911. (make-string (- 10 l) ?\ ))
  16912. '(face secondary-selection))
  16913. ""))
  16914. (if (not (featurep 'xemacs))
  16915. (org-overlay-put ov 'display tx)
  16916. (org-overlay-put ov 'invisible t)
  16917. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  16918. (push ov org-clock-overlays)))
  16919. (defun org-remove-clock-overlays (&optional beg end noremove)
  16920. "Remove the occur highlights from the buffer.
  16921. BEG and END are ignored. If NOREMOVE is nil, remove this function
  16922. from the `before-change-functions' in the current buffer."
  16923. (interactive)
  16924. (unless org-inhibit-highlight-removal
  16925. (mapc 'org-delete-overlay org-clock-overlays)
  16926. (setq org-clock-overlays nil)
  16927. (unless noremove
  16928. (remove-hook 'before-change-functions
  16929. 'org-remove-clock-overlays 'local))))
  16930. (defun org-clock-out-if-current ()
  16931. "Clock out if the current entry contains the running clock.
  16932. This is used to stop the clock after a TODO entry is marked DONE,
  16933. and is only done if the variable `org-clock-out-when-done' is not nil."
  16934. (when (and org-clock-out-when-done
  16935. (member state org-done-keywords)
  16936. (equal (marker-buffer org-clock-marker) (current-buffer))
  16937. (< (point) org-clock-marker)
  16938. (> (save-excursion (outline-next-heading) (point))
  16939. org-clock-marker))
  16940. ;; Clock out, but don't accept a logging message for this.
  16941. (let ((org-log-done (if (and (listp org-log-done)
  16942. (member 'clock-out org-log-done))
  16943. '(done)
  16944. org-log-done)))
  16945. (org-clock-out))))
  16946. (add-hook 'org-after-todo-state-change-hook
  16947. 'org-clock-out-if-current)
  16948. (defun org-check-running-clock ()
  16949. "Check if the current buffer contains the running clock.
  16950. If yes, offer to stop it and to save the buffer with the changes."
  16951. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  16952. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  16953. (buffer-name))))
  16954. (org-clock-out)
  16955. (when (y-or-n-p "Save changed buffer?")
  16956. (save-buffer))))
  16957. (defun org-clock-report (&optional arg)
  16958. "Create a table containing a report about clocked time.
  16959. If the cursor is inside an existing clocktable block, then the table
  16960. will be updated. If not, a new clocktable will be inserted.
  16961. When called with a prefix argument, move to the first clock table in the
  16962. buffer and update it."
  16963. (interactive "P")
  16964. (org-remove-clock-overlays)
  16965. (when arg (org-find-dblock "clocktable"))
  16966. (if (org-in-clocktable-p)
  16967. (goto-char (org-in-clocktable-p))
  16968. (org-create-dblock (list :name "clocktable"
  16969. :maxlevel 2 :scope 'file)))
  16970. (org-update-dblock))
  16971. (defun org-in-clocktable-p ()
  16972. "Check if the cursor is in a clocktable."
  16973. (let ((pos (point)) start)
  16974. (save-excursion
  16975. (end-of-line 1)
  16976. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  16977. (setq start (match-beginning 0))
  16978. (re-search-forward "^#\\+END:.*" nil t)
  16979. (>= (match-end 0) pos)
  16980. start))))
  16981. (defun org-clock-update-time-maybe ()
  16982. "If this is a CLOCK line, update it and return t.
  16983. Otherwise, return nil."
  16984. (interactive)
  16985. (save-excursion
  16986. (beginning-of-line 1)
  16987. (skip-chars-forward " \t")
  16988. (when (looking-at org-clock-string)
  16989. (let ((re (concat "[ \t]*" org-clock-string
  16990. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  16991. "\\([ \t]*=>.*\\)?"))
  16992. ts te h m s)
  16993. (if (not (looking-at re))
  16994. nil
  16995. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  16996. (end-of-line 1)
  16997. (setq ts (match-string 1)
  16998. te (match-string 2))
  16999. (setq s (- (time-to-seconds
  17000. (apply 'encode-time (org-parse-time-string te)))
  17001. (time-to-seconds
  17002. (apply 'encode-time (org-parse-time-string ts))))
  17003. h (floor (/ s 3600))
  17004. s (- s (* 3600 h))
  17005. m (floor (/ s 60))
  17006. s (- s (* 60 s)))
  17007. (insert " => " (format "%2d:%02d" h m))
  17008. t)))))
  17009. (defun org-clock-special-range (key &optional time as-strings)
  17010. "Return two times bordering a special time range.
  17011. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17012. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17013. A week starts Monday 0:00 and ends Sunday 24:00.
  17014. The range is determined relative to TIME. TIME defaults to the current time.
  17015. The return value is a cons cell with two internal times like the ones
  17016. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17017. the returned times will be formatted strings."
  17018. (let* ((tm (decode-time (or time (current-time))))
  17019. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17020. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17021. (dow (nth 6 tm))
  17022. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17023. (cond
  17024. ((eq key 'today)
  17025. (setq h 0 m 0 h1 24 m1 0))
  17026. ((eq key 'yesterday)
  17027. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17028. ((eq key 'thisweek)
  17029. (setq diff (if (= dow 0) 6 (1- dow))
  17030. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17031. ((eq key 'lastweek)
  17032. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17033. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17034. ((eq key 'thismonth)
  17035. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17036. ((eq key 'lastmonth)
  17037. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17038. ((eq key 'thisyear)
  17039. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17040. ((eq key 'lastyear)
  17041. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17042. (t (error "No such time block %s" key)))
  17043. (setq ts (encode-time s m h d month y)
  17044. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17045. (or d1 d) (or month1 month) (or y1 y)))
  17046. (setq fm (cdr org-time-stamp-formats))
  17047. (if as-strings
  17048. (cons (format-time-string fm ts) (format-time-string fm te))
  17049. (cons ts te))))
  17050. (defun org-dblock-write:clocktable (params)
  17051. "Write the standard clocktable."
  17052. (let ((hlchars '((1 . "*") (2 . "/")))
  17053. (emph nil)
  17054. (ins (make-marker))
  17055. (total-time nil)
  17056. ipos time h m p level hlc hdl maxlevel
  17057. ts te cc block beg end pos scope tbl tostring multifile)
  17058. (setq scope (plist-get params :scope)
  17059. tostring (plist-get params :tostring)
  17060. multifile (plist-get params :multifile)
  17061. maxlevel (or (plist-get params :maxlevel) 3)
  17062. emph (plist-get params :emphasize)
  17063. ts (plist-get params :tstart)
  17064. te (plist-get params :tend)
  17065. block (plist-get params :block))
  17066. (when block
  17067. (setq cc (org-clock-special-range block nil t)
  17068. ts (car cc) te (cdr cc)))
  17069. (if ts (setq ts (time-to-seconds
  17070. (apply 'encode-time (org-parse-time-string ts)))))
  17071. (if te (setq te (time-to-seconds
  17072. (apply 'encode-time (org-parse-time-string te)))))
  17073. (move-marker ins (point))
  17074. (setq ipos (point))
  17075. ;; Get the right scope
  17076. (setq pos (point))
  17077. (save-restriction
  17078. (cond
  17079. ((not scope))
  17080. ((eq scope 'file) (widen))
  17081. ((eq scope 'subtree) (org-narrow-to-subtree))
  17082. ((eq scope 'tree)
  17083. (while (org-up-heading-safe))
  17084. (org-narrow-to-subtree))
  17085. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17086. (symbol-name scope)))
  17087. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17088. (catch 'exit
  17089. (while (org-up-heading-safe)
  17090. (looking-at outline-regexp)
  17091. (if (<= (org-reduced-level (funcall outline-level)) level)
  17092. (throw 'exit nil))))
  17093. (org-narrow-to-subtree))
  17094. ((or (listp scope) (eq scope 'agenda))
  17095. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17096. (scope 'agenda)
  17097. (p1 (copy-sequence params))
  17098. file)
  17099. (plist-put p1 :tostring t)
  17100. (plist-put p1 :multifile t)
  17101. (plist-put p1 :scope 'file)
  17102. (org-prepare-agenda-buffers files)
  17103. (while (setq file (pop files))
  17104. (with-current-buffer (find-buffer-visiting file)
  17105. (push (org-clocktable-add-file
  17106. file (org-dblock-write:clocktable p1)) tbl)
  17107. (setq total-time (+ (or total-time 0)
  17108. org-clock-file-total-minutes)))))))
  17109. (goto-char pos)
  17110. (unless (eq scope 'agenda)
  17111. (org-clock-sum ts te)
  17112. (goto-char (point-min))
  17113. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17114. (goto-char p)
  17115. (when (setq time (get-text-property p :org-clock-minutes))
  17116. (save-excursion
  17117. (beginning-of-line 1)
  17118. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17119. (setq level (org-reduced-level
  17120. (- (match-end 1) (match-beginning 1))))
  17121. (<= level maxlevel))
  17122. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17123. hdl (match-string 2)
  17124. h (/ time 60)
  17125. m (- time (* 60 h)))
  17126. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17127. (push (concat
  17128. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17129. (make-string (1- level) ?|)
  17130. hlc (format "%d:%02d" h m) hlc
  17131. " |") tbl))))))
  17132. (setq tbl (nreverse tbl))
  17133. (if tostring
  17134. (if tbl (mapconcat 'identity tbl "\n") nil)
  17135. (goto-char ins)
  17136. (insert-before-markers
  17137. "Clock summary at ["
  17138. (substring
  17139. (format-time-string (cdr org-time-stamp-formats))
  17140. 1 -1)
  17141. "]."
  17142. (if block
  17143. (format " Considered range is /%s/." block)
  17144. "")
  17145. "\n\n"
  17146. (if (eq scope 'agenda) "|File" "")
  17147. "|L|Headline|Time|\n")
  17148. (setq total-time (or total-time org-clock-file-total-minutes)
  17149. h (/ total-time 60)
  17150. m (- total-time (* 60 h)))
  17151. (insert-before-markers
  17152. "|-\n|"
  17153. (if (eq scope 'agenda) "|" "")
  17154. "|"
  17155. "*Total time*| "
  17156. (format "*%d:%02d*" h m)
  17157. "|\n|-\n")
  17158. (setq tbl (delq nil tbl))
  17159. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17160. (equal (substring (car tbl) 0 2) "|-"))
  17161. (pop tbl))
  17162. (insert-before-markers (mapconcat
  17163. 'identity (delq nil tbl)
  17164. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17165. (backward-delete-char 1)
  17166. (goto-char ipos)
  17167. (skip-chars-forward "^|")
  17168. (org-table-align)))))
  17169. (defun org-clocktable-add-file (file table)
  17170. (if table
  17171. (let ((lines (org-split-string table "\n"))
  17172. (ff (file-name-nondirectory file)))
  17173. (mapconcat 'identity
  17174. (mapcar (lambda (x)
  17175. (if (string-match org-table-dataline-regexp x)
  17176. (concat "|" ff x)
  17177. x))
  17178. lines)
  17179. "\n"))))
  17180. ;; FIXME: I don't think anybody uses this, ask David
  17181. (defun org-collect-clock-time-entries ()
  17182. "Return an internal list with clocking information.
  17183. This list has one entry for each CLOCK interval.
  17184. FIXME: describe the elements."
  17185. (interactive)
  17186. (let ((re (concat "^[ \t]*" org-clock-string
  17187. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17188. rtn beg end next cont level title total closedp leafp
  17189. clockpos titlepos h m donep)
  17190. (save-excursion
  17191. (org-clock-sum)
  17192. (goto-char (point-min))
  17193. (while (re-search-forward re nil t)
  17194. (setq clockpos (match-beginning 0)
  17195. beg (match-string 1) end (match-string 2)
  17196. cont (match-end 0))
  17197. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17198. end (apply 'encode-time (org-parse-time-string end)))
  17199. (org-back-to-heading t)
  17200. (setq donep (org-entry-is-done-p))
  17201. (setq titlepos (point)
  17202. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17203. h (/ total 60) m (- total (* 60 h))
  17204. total (cons h m))
  17205. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17206. (setq level (- (match-end 1) (match-beginning 1))
  17207. title (org-match-string-no-properties 2))
  17208. (save-excursion (outline-next-heading) (setq next (point)))
  17209. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17210. (goto-char next)
  17211. (setq leafp (and (looking-at "^\\*+ ")
  17212. (<= (- (match-end 0) (point)) level)))
  17213. (push (list beg end clockpos closedp donep
  17214. total title titlepos level leafp)
  17215. rtn)
  17216. (goto-char cont)))
  17217. (nreverse rtn)))
  17218. ;;;; Agenda, and Diary Integration
  17219. ;;; Define the Org-agenda-mode
  17220. (defvar org-agenda-mode-map (make-sparse-keymap)
  17221. "Keymap for `org-agenda-mode'.")
  17222. (defvar org-agenda-menu) ; defined later in this file.
  17223. (defvar org-agenda-follow-mode nil)
  17224. (defvar org-agenda-show-log nil)
  17225. (defvar org-agenda-redo-command nil)
  17226. (defvar org-agenda-mode-hook nil)
  17227. (defvar org-agenda-type nil)
  17228. (defvar org-agenda-force-single-file nil)
  17229. (defun org-agenda-mode ()
  17230. "Mode for time-sorted view on action items in Org-mode files.
  17231. The following commands are available:
  17232. \\{org-agenda-mode-map}"
  17233. (interactive)
  17234. (kill-all-local-variables)
  17235. (setq org-agenda-undo-list nil
  17236. org-agenda-pending-undo-list nil)
  17237. (setq major-mode 'org-agenda-mode)
  17238. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17239. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17240. (setq mode-name "Org-Agenda")
  17241. (use-local-map org-agenda-mode-map)
  17242. (easy-menu-add org-agenda-menu)
  17243. (if org-startup-truncated (setq truncate-lines t))
  17244. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17245. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17246. ;; Make sure properties are removed when copying text
  17247. (when (boundp 'buffer-substring-filters)
  17248. (org-set-local 'buffer-substring-filters
  17249. (cons (lambda (x)
  17250. (set-text-properties 0 (length x) nil x) x)
  17251. buffer-substring-filters)))
  17252. (unless org-agenda-keep-modes
  17253. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17254. org-agenda-show-log nil))
  17255. (easy-menu-change
  17256. '("Agenda") "Agenda Files"
  17257. (append
  17258. (list
  17259. (vector
  17260. (if (get 'org-agenda-files 'org-restrict)
  17261. "Restricted to single file"
  17262. "Edit File List")
  17263. '(org-edit-agenda-file-list)
  17264. (not (get 'org-agenda-files 'org-restrict)))
  17265. "--")
  17266. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17267. (org-agenda-set-mode-name)
  17268. (apply
  17269. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17270. (list 'org-agenda-mode-hook)))
  17271. (substitute-key-definition 'undo 'org-agenda-undo
  17272. org-agenda-mode-map global-map)
  17273. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17274. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17275. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17276. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17277. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17278. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17279. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17280. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17281. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17282. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17283. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17284. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17285. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17286. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17287. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17288. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17289. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17290. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17291. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17292. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17293. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17294. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17295. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17296. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17297. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17298. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17299. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17300. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17301. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17302. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17303. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17304. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17305. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17306. (while l (org-defkey org-agenda-mode-map
  17307. (int-to-string (pop l)) 'digit-argument)))
  17308. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17309. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17310. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17311. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17312. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17313. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17314. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17315. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17316. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17317. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17318. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17319. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17320. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17321. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17322. (org-defkey org-agenda-mode-map "n" 'next-line)
  17323. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17324. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17325. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17326. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17327. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17328. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17329. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17330. (eval-after-load "calendar"
  17331. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17332. 'org-calendar-goto-agenda))
  17333. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17334. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17335. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17336. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17337. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17338. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17339. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17340. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17341. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17342. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17343. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17344. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17345. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17346. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17347. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17348. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17349. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17350. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17351. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17352. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17353. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17354. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17355. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17356. "Local keymap for agenda entries from Org-mode.")
  17357. (org-defkey org-agenda-keymap
  17358. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17359. (org-defkey org-agenda-keymap
  17360. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17361. (when org-agenda-mouse-1-follows-link
  17362. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17363. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17364. '("Agenda"
  17365. ("Agenda Files")
  17366. "--"
  17367. ["Show" org-agenda-show t]
  17368. ["Go To (other window)" org-agenda-goto t]
  17369. ["Go To (this window)" org-agenda-switch-to t]
  17370. ["Follow Mode" org-agenda-follow-mode
  17371. :style toggle :selected org-agenda-follow-mode :active t]
  17372. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17373. "--"
  17374. ["Cycle TODO" org-agenda-todo t]
  17375. ["Archive subtree" org-agenda-archive t]
  17376. ["Delete subtree" org-agenda-kill t]
  17377. "--"
  17378. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17379. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17380. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17381. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17382. "--"
  17383. ("Tags and Properties"
  17384. ["Show all Tags" org-agenda-show-tags t]
  17385. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17386. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17387. "--"
  17388. ["Column View" org-columns t])
  17389. ("Date/Schedule"
  17390. ["Schedule" org-agenda-schedule t]
  17391. ["Set Deadline" org-agenda-deadline t]
  17392. "--"
  17393. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17394. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17395. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17396. ("Clock"
  17397. ["Clock in" org-agenda-clock-in t]
  17398. ["Clock out" org-agenda-clock-out t]
  17399. ["Clock cancel" org-agenda-clock-cancel t]
  17400. ["Goto running clock" org-clock-goto t])
  17401. ("Priority"
  17402. ["Set Priority" org-agenda-priority t]
  17403. ["Increase Priority" org-agenda-priority-up t]
  17404. ["Decrease Priority" org-agenda-priority-down t]
  17405. ["Show Priority" org-agenda-show-priority t])
  17406. ("Calendar/Diary"
  17407. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17408. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17409. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17410. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17411. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17412. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17413. "--"
  17414. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17415. "--"
  17416. ("View"
  17417. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17418. :style radio :selected (equal org-agenda-ndays 1)]
  17419. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17420. :style radio :selected (equal org-agenda-ndays 7)]
  17421. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17422. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17423. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17424. :style radio :selected (member org-agenda-ndays '(365 366))]
  17425. "--"
  17426. ["Show Logbook entries" org-agenda-log-mode
  17427. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17428. ["Include Diary" org-agenda-toggle-diary
  17429. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17430. ["Use Time Grid" org-agenda-toggle-time-grid
  17431. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17432. ["Write view to file" org-write-agenda t]
  17433. ["Rebuild buffer" org-agenda-redo t]
  17434. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17435. "--"
  17436. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17437. "--"
  17438. ["Quit" org-agenda-quit t]
  17439. ["Exit and Release Buffers" org-agenda-exit t]
  17440. ))
  17441. ;;; Agenda undo
  17442. (defvar org-agenda-allow-remote-undo t
  17443. "Non-nil means, allow remote undo from the agenda buffer.")
  17444. (defvar org-agenda-undo-list nil
  17445. "List of undoable operations in the agenda since last refresh.")
  17446. (defvar org-agenda-undo-has-started-in nil
  17447. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17448. (defvar org-agenda-pending-undo-list nil
  17449. "In a series of undo commands, this is the list of remaning undo items.")
  17450. (defmacro org-if-unprotected (&rest body)
  17451. "Execute BODY if there is no `org-protected' text property at point."
  17452. (declare (debug t))
  17453. `(unless (get-text-property (point) 'org-protected)
  17454. ,@body))
  17455. (defmacro org-with-remote-undo (_buffer &rest _body)
  17456. "Execute BODY while recording undo information in two buffers."
  17457. (declare (indent 1) (debug t))
  17458. `(let ((_cline (org-current-line))
  17459. (_cmd this-command)
  17460. (_buf1 (current-buffer))
  17461. (_buf2 ,_buffer)
  17462. (_undo1 buffer-undo-list)
  17463. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17464. _c1 _c2)
  17465. ,@_body
  17466. (when org-agenda-allow-remote-undo
  17467. (setq _c1 (org-verify-change-for-undo
  17468. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17469. _c2 (org-verify-change-for-undo
  17470. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17471. (when (or _c1 _c2)
  17472. ;; make sure there are undo boundaries
  17473. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17474. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17475. ;; remember which buffer to undo
  17476. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17477. org-agenda-undo-list)))))
  17478. (defun org-agenda-undo ()
  17479. "Undo a remote editing step in the agenda.
  17480. This undoes changes both in the agenda buffer and in the remote buffer
  17481. that have been changed along."
  17482. (interactive)
  17483. (or org-agenda-allow-remote-undo
  17484. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17485. (if (not (eq this-command last-command))
  17486. (setq org-agenda-undo-has-started-in nil
  17487. org-agenda-pending-undo-list org-agenda-undo-list))
  17488. (if (not org-agenda-pending-undo-list)
  17489. (error "No further undo information"))
  17490. (let* ((entry (pop org-agenda-pending-undo-list))
  17491. buf line cmd rembuf)
  17492. (setq cmd (pop entry) line (pop entry))
  17493. (setq rembuf (nth 2 entry))
  17494. (org-with-remote-undo rembuf
  17495. (while (bufferp (setq buf (pop entry)))
  17496. (if (pop entry)
  17497. (with-current-buffer buf
  17498. (let ((last-undo-buffer buf)
  17499. (inhibit-read-only t))
  17500. (unless (memq buf org-agenda-undo-has-started-in)
  17501. (push buf org-agenda-undo-has-started-in)
  17502. (make-local-variable 'pending-undo-list)
  17503. (undo-start))
  17504. (while (and pending-undo-list
  17505. (listp pending-undo-list)
  17506. (not (car pending-undo-list)))
  17507. (pop pending-undo-list))
  17508. (undo-more 1))))))
  17509. (goto-line line)
  17510. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17511. (defun org-verify-change-for-undo (l1 l2)
  17512. "Verify that a real change occurred between the undo lists L1 and L2."
  17513. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17514. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17515. (not (eq l1 l2)))
  17516. ;;; Agenda dispatch
  17517. (defvar org-agenda-restrict nil)
  17518. (defvar org-agenda-restrict-begin (make-marker))
  17519. (defvar org-agenda-restrict-end (make-marker))
  17520. (defvar org-agenda-last-dispatch-buffer nil)
  17521. (defvar org-agenda-overriding-restriction nil)
  17522. ;;;###autoload
  17523. (defun org-agenda (arg &optional keys restriction)
  17524. "Dispatch agenda commands to collect entries to the agenda buffer.
  17525. Prompts for a command to execute. Any prefix arg will be passed
  17526. on to the selected command. The default selections are:
  17527. a Call `org-agenda-list' to display the agenda for current day or week.
  17528. t Call `org-todo-list' to display the global todo list.
  17529. T Call `org-todo-list' to display the global todo list, select only
  17530. entries with a specific TODO keyword (the user gets a prompt).
  17531. m Call `org-tags-view' to display headlines with tags matching
  17532. a condition (the user is prompted for the condition).
  17533. M Like `m', but select only TODO entries, no ordinary headlines.
  17534. L Create a timeline for the current buffer.
  17535. e Export views to associated files.
  17536. More commands can be added by configuring the variable
  17537. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17538. searches can be pre-defined in this way.
  17539. If the current buffer is in Org-mode and visiting a file, you can also
  17540. first press `<' once to indicate that the agenda should be temporarily
  17541. \(until the next use of \\[org-agenda]) restricted to the current file.
  17542. Pressing `<' twice means to restrict to the current subtree or region
  17543. \(if active)."
  17544. (interactive "P")
  17545. (catch 'exit
  17546. (let* ((prefix-descriptions nil)
  17547. (org-agenda-custom-commands
  17548. ;; normalize different versions
  17549. (delq nil
  17550. (mapcar
  17551. (lambda (x)
  17552. (cond ((stringp (cdr x))
  17553. (push x prefix-descriptions)
  17554. nil)
  17555. ((stringp (nth 1 x)) x)
  17556. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17557. (t (cons (car x) (cons "" (cdr x))))))
  17558. org-agenda-custom-commands)))
  17559. (buf (current-buffer))
  17560. (bfn (buffer-file-name (buffer-base-buffer)))
  17561. entry key type match lprops ans)
  17562. ;; Turn off restriction unless there is an overriding one
  17563. (unless org-agenda-overriding-restriction
  17564. (put 'org-agenda-files 'org-restrict nil)
  17565. (setq org-agenda-restrict nil)
  17566. (move-marker org-agenda-restrict-begin nil)
  17567. (move-marker org-agenda-restrict-end nil))
  17568. ;; Delete old local properties
  17569. (put 'org-agenda-redo-command 'org-lprops nil)
  17570. ;; Remember where this call originated
  17571. (setq org-agenda-last-dispatch-buffer (current-buffer))
  17572. (unless keys
  17573. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  17574. keys (car ans)
  17575. restriction (cdr ans)))
  17576. ;; Estabish the restriction, if any
  17577. (when (and (not org-agenda-overriding-restriction) restriction)
  17578. (put 'org-agenda-files 'org-restrict (list bfn))
  17579. (cond
  17580. ((eq restriction 'region)
  17581. (setq org-agenda-restrict t)
  17582. (move-marker org-agenda-restrict-begin (region-beginning))
  17583. (move-marker org-agenda-restrict-end (region-end)))
  17584. ((eq restriction 'subtree)
  17585. (save-excursion
  17586. (setq org-agenda-restrict t)
  17587. (org-back-to-heading t)
  17588. (move-marker org-agenda-restrict-begin (point))
  17589. (move-marker org-agenda-restrict-end
  17590. (progn (org-end-of-subtree t)))))))
  17591. (require 'calendar) ; FIXME: can we avoid this for some commands?
  17592. ;; For example the todo list should not need it (but does...)
  17593. (cond
  17594. ((setq entry (assoc keys org-agenda-custom-commands))
  17595. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  17596. (progn
  17597. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  17598. (put 'org-agenda-redo-command 'org-lprops lprops)
  17599. (cond
  17600. ((eq type 'agenda)
  17601. (org-let lprops '(org-agenda-list current-prefix-arg)))
  17602. ((eq type 'alltodo)
  17603. (org-let lprops '(org-todo-list current-prefix-arg)))
  17604. ((eq type 'stuck)
  17605. (org-let lprops '(org-agenda-list-stuck-projects
  17606. current-prefix-arg)))
  17607. ((eq type 'tags)
  17608. (org-let lprops '(org-tags-view current-prefix-arg match)))
  17609. ((eq type 'tags-todo)
  17610. (org-let lprops '(org-tags-view '(4) match)))
  17611. ((eq type 'todo)
  17612. (org-let lprops '(org-todo-list match)))
  17613. ((eq type 'tags-tree)
  17614. (org-check-for-org-mode)
  17615. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  17616. ((eq type 'todo-tree)
  17617. (org-check-for-org-mode)
  17618. (org-let lprops
  17619. '(org-occur (concat "^" outline-regexp "[ \t]*"
  17620. (regexp-quote match) "\\>"))))
  17621. ((eq type 'occur-tree)
  17622. (org-check-for-org-mode)
  17623. (org-let lprops '(org-occur match)))
  17624. ((functionp type)
  17625. (org-let lprops '(funcall type match)))
  17626. ((fboundp type)
  17627. (org-let lprops '(funcall type match)))
  17628. (t (error "Invalid custom agenda command type %s" type))))
  17629. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  17630. ((equal keys "C") (customize-variable 'org-agenda-custom-commands))
  17631. ((equal keys "a") (call-interactively 'org-agenda-list))
  17632. ((equal keys "t") (call-interactively 'org-todo-list))
  17633. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  17634. ((equal keys "m") (call-interactively 'org-tags-view))
  17635. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  17636. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  17637. ((equal keys "L")
  17638. (unless (org-mode-p)
  17639. (error "This is not an Org-mode file"))
  17640. (unless restriction
  17641. (put 'org-agenda-files 'org-restrict (list bfn))
  17642. (org-call-with-arg 'org-timeline arg)))
  17643. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  17644. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  17645. ((equal keys "!") (customize-variable 'org-stuck-projects))
  17646. (t (error "Invalid agenda key"))))))
  17647. (defun org-agenda-normalize-custom-commands (cmds)
  17648. (delq nil
  17649. (mapcar
  17650. (lambda (x)
  17651. (cond ((stringp (cdr x)) nil)
  17652. ((stringp (nth 1 x)) x)
  17653. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17654. (t (cons (car x) (cons "" (cdr x))))))
  17655. cmds)))
  17656. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  17657. "The user interface for selecting an agenda command."
  17658. (catch 'exit
  17659. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  17660. (restrict-ok (and bfn (org-mode-p)))
  17661. (region-p (org-region-active-p))
  17662. (custom org-agenda-custom-commands)
  17663. (selstring "")
  17664. restriction second-time
  17665. c entry key type match prefixes rmheader header-end custom1 desc)
  17666. (save-window-excursion
  17667. (delete-other-windows)
  17668. (org-switch-to-buffer-other-window " *Agenda Commands*")
  17669. (erase-buffer)
  17670. (insert (eval-when-compile
  17671. (let ((header
  17672. "
  17673. Press key for an agenda command: < Buffer,subtree/region restriction
  17674. -------------------------------- > Remove restriction
  17675. a Agenda for current week or day e Export agenda views
  17676. t List of all TODO entries T Entries with special TODO kwd
  17677. m Match a TAGS query M Like m, but only TODO entries
  17678. L Timeline for current buffer # List stuck projects (!=configure)
  17679. / Multi-occur C Configure custom agenda commands
  17680. ")
  17681. (start 0))
  17682. (while (string-match
  17683. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  17684. header start)
  17685. (setq start (match-end 0))
  17686. (add-text-properties (match-beginning 2) (match-end 2)
  17687. '(face bold) header))
  17688. header)))
  17689. (setq header-end (move-marker (make-marker) (point)))
  17690. (while t
  17691. (setq custom1 custom)
  17692. (when (eq rmheader t)
  17693. (goto-line 1)
  17694. (re-search-forward ":" nil t)
  17695. (delete-region (match-end 0) (point-at-eol))
  17696. (forward-char 1)
  17697. (looking-at "-+")
  17698. (delete-region (match-end 0) (point-at-eol))
  17699. (move-marker header-end (match-end 0)))
  17700. (goto-char header-end)
  17701. (delete-region (point) (point-max))
  17702. (while (setq entry (pop custom1))
  17703. (setq key (car entry) desc (nth 1 entry)
  17704. type (nth 2 entry) match (nth 3 entry))
  17705. (if (> (length key) 1)
  17706. (add-to-list 'prefixes (string-to-char key))
  17707. (insert
  17708. (format
  17709. "\n%-4s%-14s: %s"
  17710. (org-add-props (copy-sequence key)
  17711. '(face bold))
  17712. (cond
  17713. ((string-match "\\S-" desc) desc)
  17714. ((eq type 'agenda) "Agenda for current week or day")
  17715. ((eq type 'alltodo) "List of all TODO entries")
  17716. ((eq type 'stuck) "List of stuck projects")
  17717. ((eq type 'todo) "TODO keyword")
  17718. ((eq type 'tags) "Tags query")
  17719. ((eq type 'tags-todo) "Tags (TODO)")
  17720. ((eq type 'tags-tree) "Tags tree")
  17721. ((eq type 'todo-tree) "TODO kwd tree")
  17722. ((eq type 'occur-tree) "Occur tree")
  17723. ((functionp type) (if (symbolp type)
  17724. (symbol-name type)
  17725. "Lambda expression"))
  17726. (t "???"))
  17727. (cond
  17728. ((stringp match)
  17729. (org-add-props match nil 'face 'org-warning))
  17730. (match
  17731. (format "set of %d commands" (length match)))
  17732. (t ""))))))
  17733. (when prefixes
  17734. (mapc (lambda (x)
  17735. (insert
  17736. (format "\n%s %s"
  17737. (org-add-props (char-to-string x)
  17738. nil 'face 'bold)
  17739. (or (cdr (assoc (concat selstring (char-to-string x))
  17740. prefix-descriptions))
  17741. "Prefix key"))))
  17742. prefixes))
  17743. (goto-char (point-min))
  17744. (when (fboundp 'fit-window-to-buffer)
  17745. (if second-time
  17746. (if (not (pos-visible-in-window-p (point-max)))
  17747. (fit-window-to-buffer))
  17748. (setq second-time t)
  17749. (fit-window-to-buffer)))
  17750. (message "Press key for agenda command%s:"
  17751. (if (or restrict-ok org-agenda-overriding-restriction)
  17752. (if org-agenda-overriding-restriction
  17753. " (restriction lock active)"
  17754. (if restriction
  17755. (format " (restricted to %s)" restriction)
  17756. " (unrestricted)"))
  17757. ""))
  17758. (setq c (read-char-exclusive))
  17759. (message "")
  17760. (cond
  17761. ((assoc (char-to-string c) custom)
  17762. (setq selstring (concat selstring (char-to-string c)))
  17763. (throw 'exit (cons selstring restriction)))
  17764. ((memq c prefixes)
  17765. (setq selstring (concat selstring (char-to-string c))
  17766. prefixes nil
  17767. rmheader (or rmheader t)
  17768. custom (delq nil (mapcar
  17769. (lambda (x)
  17770. (if (or (= (length (car x)) 1)
  17771. (/= (string-to-char (car x)) c))
  17772. nil
  17773. (cons (substring (car x) 1) (cdr x))))
  17774. custom))))
  17775. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  17776. (message "Restriction is only possible in Org-mode buffers")
  17777. (ding) (sit-for 1))
  17778. ((eq c ?1)
  17779. (org-agenda-remove-restriction-lock 'noupdate)
  17780. (setq restriction 'buffer))
  17781. ((eq c ?0)
  17782. (org-agenda-remove-restriction-lock 'noupdate)
  17783. (setq restriction (if region-p 'region 'subtree)))
  17784. ((eq c ?<)
  17785. (org-agenda-remove-restriction-lock 'noupdate)
  17786. (setq restriction
  17787. (cond
  17788. ((eq restriction 'buffer)
  17789. (if region-p 'region 'subtree))
  17790. ((memq restriction '(subtree region))
  17791. nil)
  17792. (t 'buffer))))
  17793. ((eq c ?>)
  17794. (org-agenda-remove-restriction-lock 'noupdate)
  17795. (setq restriction nil))
  17796. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?/)))
  17797. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  17798. ((equal c ?q) (error "Abort"))
  17799. (t (error "Invalid key %c" c))))))))
  17800. (defun org-run-agenda-series (name series)
  17801. (org-prepare-agenda name)
  17802. (let* ((org-agenda-multi t)
  17803. (redo (list 'org-run-agenda-series name (list 'quote series)))
  17804. (cmds (car series))
  17805. (gprops (nth 1 series))
  17806. match ;; The byte compiler incorrectly complains about this. Keep it!
  17807. cmd type lprops)
  17808. (while (setq cmd (pop cmds))
  17809. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  17810. (cond
  17811. ((eq type 'agenda)
  17812. (org-let2 gprops lprops
  17813. '(call-interactively 'org-agenda-list)))
  17814. ((eq type 'alltodo)
  17815. (org-let2 gprops lprops
  17816. '(call-interactively 'org-todo-list)))
  17817. ((eq type 'stuck)
  17818. (org-let2 gprops lprops
  17819. '(call-interactively 'org-agenda-list-stuck-projects)))
  17820. ((eq type 'tags)
  17821. (org-let2 gprops lprops
  17822. '(org-tags-view current-prefix-arg match)))
  17823. ((eq type 'tags-todo)
  17824. (org-let2 gprops lprops
  17825. '(org-tags-view '(4) match)))
  17826. ((eq type 'todo)
  17827. (org-let2 gprops lprops
  17828. '(org-todo-list match)))
  17829. ((fboundp type)
  17830. (org-let2 gprops lprops
  17831. '(funcall type match)))
  17832. (t (error "Invalid type in command series"))))
  17833. (widen)
  17834. (setq org-agenda-redo-command redo)
  17835. (goto-char (point-min)))
  17836. (org-finalize-agenda))
  17837. ;;;###autoload
  17838. (defmacro org-batch-agenda (cmd-key &rest parameters)
  17839. "Run an agenda command in batch mode and send the result to STDOUT.
  17840. If CMD-KEY is a string of length 1, it is used as a key in
  17841. `org-agenda-custom-commands' and triggers this command. If it is a
  17842. longer string is is used as a tags/todo match string.
  17843. Paramters are alternating variable names and values that will be bound
  17844. before running the agenda command."
  17845. (let (pars)
  17846. (while parameters
  17847. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17848. (if (> (length cmd-key) 2)
  17849. (eval (list 'let (nreverse pars)
  17850. (list 'org-tags-view nil cmd-key)))
  17851. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17852. (set-buffer org-agenda-buffer-name)
  17853. (princ (org-encode-for-stdout (buffer-string)))))
  17854. (defun org-encode-for-stdout (string)
  17855. (if (fboundp 'encode-coding-string)
  17856. (encode-coding-string string buffer-file-coding-system)
  17857. string))
  17858. (defvar org-agenda-info nil)
  17859. ;;;###autoload
  17860. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  17861. "Run an agenda command in batch mode and send the result to STDOUT.
  17862. If CMD-KEY is a string of length 1, it is used as a key in
  17863. `org-agenda-custom-commands' and triggers this command. If it is a
  17864. longer string is is used as a tags/todo match string.
  17865. Paramters are alternating variable names and values that will be bound
  17866. before running the agenda command.
  17867. The output gives a line for each selected agenda item. Each
  17868. item is a list of comma-separated values, like this:
  17869. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  17870. category The category of the item
  17871. head The headline, without TODO kwd, TAGS and PRIORITY
  17872. type The type of the agenda entry, can be
  17873. todo selected in TODO match
  17874. tagsmatch selected in tags match
  17875. diary imported from diary
  17876. deadline a deadline on given date
  17877. scheduled scheduled on given date
  17878. timestamp entry has timestamp on given date
  17879. closed entry was closed on given date
  17880. upcoming-deadline warning about deadline
  17881. past-scheduled forwarded scheduled item
  17882. block entry has date block including g. date
  17883. todo The todo keyword, if any
  17884. tags All tags including inherited ones, separated by colons
  17885. date The relevant date, like 2007-2-14
  17886. time The time, like 15:00-16:50
  17887. extra Sting with extra planning info
  17888. priority-l The priority letter if any was given
  17889. priority-n The computed numerical priority
  17890. agenda-day The day in the agenda where this is listed"
  17891. (let (pars)
  17892. (while parameters
  17893. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17894. (push (list 'org-agenda-remove-tags t) pars)
  17895. (if (> (length cmd-key) 2)
  17896. (eval (list 'let (nreverse pars)
  17897. (list 'org-tags-view nil cmd-key)))
  17898. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17899. (set-buffer org-agenda-buffer-name)
  17900. (let* ((lines (org-split-string (buffer-string) "\n"))
  17901. line)
  17902. (while (setq line (pop lines))
  17903. (catch 'next
  17904. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  17905. (setq org-agenda-info
  17906. (org-fix-agenda-info (text-properties-at 0 line)))
  17907. (princ
  17908. (org-encode-for-stdout
  17909. (mapconcat 'org-agenda-export-csv-mapper
  17910. '(org-category txt type todo tags date time-of-day extra
  17911. priority-letter priority agenda-day)
  17912. ",")))
  17913. (princ "\n"))))))
  17914. (defun org-fix-agenda-info (props)
  17915. "Make sure all properties on an agenda item have a canonical form,
  17916. so the the export commands caneasily use it."
  17917. (let (tmp re)
  17918. (when (setq tmp (plist-get props 'tags))
  17919. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  17920. (when (setq tmp (plist-get props 'date))
  17921. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  17922. (let ((calendar-date-display-form '(year "-" month "-" day)))
  17923. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  17924. (setq tmp (calendar-date-string tmp)))
  17925. (setq props (plist-put props 'date tmp)))
  17926. (when (setq tmp (plist-get props 'day))
  17927. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  17928. (let ((calendar-date-display-form '(year "-" month "-" day)))
  17929. (setq tmp (calendar-date-string tmp)))
  17930. (setq props (plist-put props 'day tmp))
  17931. (setq props (plist-put props 'agenda-day tmp)))
  17932. (when (setq tmp (plist-get props 'txt))
  17933. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  17934. (plist-put props 'priority-letter (match-string 1 tmp))
  17935. (setq tmp (replace-match "" t t tmp)))
  17936. (when (and (setq re (plist-get props 'org-todo-regexp))
  17937. (setq re (concat "\\`\\.*" re " ?"))
  17938. (string-match re tmp))
  17939. (plist-put props 'todo (match-string 1 tmp))
  17940. (setq tmp (replace-match "" t t tmp)))
  17941. (plist-put props 'txt tmp)))
  17942. props)
  17943. (defun org-agenda-export-csv-mapper (prop)
  17944. (let ((res (plist-get org-agenda-info prop)))
  17945. (setq res
  17946. (cond
  17947. ((not res) "")
  17948. ((stringp res) res)
  17949. (t (prin1-to-string res))))
  17950. (while (string-match "," res)
  17951. (setq res (replace-match ";" t t res)))
  17952. (org-trim res)))
  17953. ;;;###autoload
  17954. (defun org-store-agenda-views (&rest parameters)
  17955. (interactive)
  17956. (eval (list 'org-batch-store-agenda-views)))
  17957. ;; FIXME, why is this a macro?????
  17958. ;;;###autoload
  17959. (defmacro org-batch-store-agenda-views (&rest parameters)
  17960. "Run all custom agenda commands that have a file argument."
  17961. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  17962. (pop-up-frames nil)
  17963. (dir default-directory)
  17964. pars cmd thiscmdkey files opts)
  17965. (while parameters
  17966. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17967. (setq pars (reverse pars))
  17968. (save-window-excursion
  17969. (while cmds
  17970. (setq cmd (pop cmds)
  17971. thiscmdkey (car cmd)
  17972. opts (nth 4 cmd)
  17973. files (nth 5 cmd))
  17974. (if (stringp files) (setq files (list files)))
  17975. (when files
  17976. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  17977. (list 'org-agenda nil thiscmdkey)))
  17978. (set-buffer org-agenda-buffer-name)
  17979. (while files
  17980. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  17981. (list 'org-write-agenda
  17982. (expand-file-name (pop files) dir) t))))
  17983. (and (get-buffer org-agenda-buffer-name)
  17984. (kill-buffer org-agenda-buffer-name)))))))
  17985. (defun org-write-agenda (file &optional nosettings)
  17986. "Write the current buffer (an agenda view) as a file.
  17987. Depending on the extension of the file name, plain text (.txt),
  17988. HTML (.html or .htm) or Postscript (.ps) is produced.
  17989. If NOSETTINGS is given, do not scope the settings of
  17990. `org-agenda-exporter-settings' into the export commands. This is used when
  17991. the settings have already been scoped and we do not wish to overrule other,
  17992. higher priority settings."
  17993. (interactive "FWrite agenda to file: ")
  17994. (if (not (file-writable-p file))
  17995. (error "Cannot write agenda to file %s" file))
  17996. (cond
  17997. ((string-match "\\.html?\\'" file) (require 'htmlize))
  17998. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  17999. (org-let (if nosettings nil org-agenda-exporter-settings)
  18000. '(save-excursion
  18001. (save-window-excursion
  18002. (cond
  18003. ((string-match "\\.html?\\'" file)
  18004. (set-buffer (htmlize-buffer (current-buffer)))
  18005. (when (and org-agenda-export-html-style
  18006. (string-match "<style>" org-agenda-export-html-style))
  18007. ;; replace <style> section with org-agenda-export-html-style
  18008. (goto-char (point-min))
  18009. (kill-region (- (search-forward "<style") 6)
  18010. (search-forward "</style>"))
  18011. (insert org-agenda-export-html-style))
  18012. (write-file file)
  18013. (kill-buffer (current-buffer))
  18014. (message "HTML written to %s" file))
  18015. ((string-match "\\.ps\\'" file)
  18016. (ps-print-buffer-with-faces file)
  18017. (message "Postscript written to %s" file))
  18018. (t
  18019. (let ((bs (buffer-string)))
  18020. (find-file file)
  18021. (insert bs)
  18022. (save-buffer 0)
  18023. (kill-buffer (current-buffer))
  18024. (message "Plain text written to %s" file))))))
  18025. (set-buffer org-agenda-buffer-name)))
  18026. (defmacro org-no-read-only (&rest body)
  18027. "Inhibit read-only for BODY."
  18028. `(let ((inhibit-read-only t)) ,@body))
  18029. (defun org-check-for-org-mode ()
  18030. "Make sure current buffer is in org-mode. Error if not."
  18031. (or (org-mode-p)
  18032. (error "Cannot execute org-mode agenda command on buffer in %s."
  18033. major-mode)))
  18034. (defun org-fit-agenda-window ()
  18035. "Fit the window to the buffer size."
  18036. (and (memq org-agenda-window-setup '(reorganize-frame))
  18037. (fboundp 'fit-window-to-buffer)
  18038. (fit-window-to-buffer
  18039. nil
  18040. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18041. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18042. ;;; Agenda file list
  18043. (defun org-agenda-files (&optional unrestricted)
  18044. "Get the list of agenda files.
  18045. Optional UNRESTRICTED means return the full list even if a restriction
  18046. is currently in place."
  18047. (let ((files
  18048. (cond
  18049. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18050. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18051. ((listp org-agenda-files) org-agenda-files)
  18052. (t (error "Invalid value of `org-agenda-files'")))))
  18053. (setq files (apply 'append
  18054. (mapcar (lambda (f)
  18055. (if (file-directory-p f)
  18056. (directory-files f t "\\.org\\'")
  18057. (list f)))
  18058. files)))
  18059. (if org-agenda-skip-unavailable-files
  18060. (delq nil
  18061. (mapcar (function
  18062. (lambda (file)
  18063. (and (file-readable-p file) file)))
  18064. files))
  18065. files))) ; `org-check-agenda-file' will remove them from the list
  18066. (defun org-edit-agenda-file-list ()
  18067. "Edit the list of agenda files.
  18068. Depending on setup, this either uses customize to edit the variable
  18069. `org-agenda-files', or it visits the file that is holding the list. In the
  18070. latter case, the buffer is set up in a way that saving it automatically kills
  18071. the buffer and restores the previous window configuration."
  18072. (interactive)
  18073. (if (stringp org-agenda-files)
  18074. (let ((cw (current-window-configuration)))
  18075. (find-file org-agenda-files)
  18076. (org-set-local 'org-window-configuration cw)
  18077. (org-add-hook 'after-save-hook
  18078. (lambda ()
  18079. (set-window-configuration
  18080. (prog1 org-window-configuration
  18081. (kill-buffer (current-buffer))))
  18082. (org-install-agenda-files-menu)
  18083. (message "New agenda file list installed"))
  18084. nil 'local)
  18085. (message "%s" (substitute-command-keys
  18086. "Edit list and finish with \\[save-buffer]")))
  18087. (customize-variable 'org-agenda-files)))
  18088. (defun org-store-new-agenda-file-list (list)
  18089. "Set new value for the agenda file list and save it correcly."
  18090. (if (stringp org-agenda-files)
  18091. (let ((f org-agenda-files) b)
  18092. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18093. (with-temp-file f
  18094. (insert (mapconcat 'identity list "\n") "\n")))
  18095. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18096. (setq org-agenda-files list)
  18097. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18098. (defun org-read-agenda-file-list ()
  18099. "Read the list of agenda files from a file."
  18100. (when (stringp org-agenda-files)
  18101. (with-temp-buffer
  18102. (insert-file-contents org-agenda-files)
  18103. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18104. ;;;###autoload
  18105. (defun org-cycle-agenda-files ()
  18106. "Cycle through the files in `org-agenda-files'.
  18107. If the current buffer visits an agenda file, find the next one in the list.
  18108. If the current buffer does not, find the first agenda file."
  18109. (interactive)
  18110. (let* ((fs (org-agenda-files t))
  18111. (files (append fs (list (car fs))))
  18112. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18113. file)
  18114. (unless files (error "No agenda files"))
  18115. (catch 'exit
  18116. (while (setq file (pop files))
  18117. (if (equal (file-truename file) tcf)
  18118. (when (car files)
  18119. (find-file (car files))
  18120. (throw 'exit t))))
  18121. (find-file (car fs)))
  18122. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18123. (defun org-agenda-file-to-front (&optional to-end)
  18124. "Move/add the current file to the top of the agenda file list.
  18125. If the file is not present in the list, it is added to the front. If it is
  18126. present, it is moved there. With optional argument TO-END, add/move to the
  18127. end of the list."
  18128. (interactive "P")
  18129. (let ((org-agenda-skip-unavailable-files nil)
  18130. (file-alist (mapcar (lambda (x)
  18131. (cons (file-truename x) x))
  18132. (org-agenda-files t)))
  18133. (ctf (file-truename buffer-file-name))
  18134. x had)
  18135. (setq x (assoc ctf file-alist) had x)
  18136. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18137. (if to-end
  18138. (setq file-alist (append (delq x file-alist) (list x)))
  18139. (setq file-alist (cons x (delq x file-alist))))
  18140. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18141. (org-install-agenda-files-menu)
  18142. (message "File %s to %s of agenda file list"
  18143. (if had "moved" "added") (if to-end "end" "front"))))
  18144. (defun org-remove-file (&optional file)
  18145. "Remove current file from the list of files in variable `org-agenda-files'.
  18146. These are the files which are being checked for agenda entries.
  18147. Optional argument FILE means, use this file instead of the current."
  18148. (interactive)
  18149. (let* ((org-agenda-skip-unavailable-files nil)
  18150. (file (or file buffer-file-name))
  18151. (true-file (file-truename file))
  18152. (afile (abbreviate-file-name file))
  18153. (files (delq nil (mapcar
  18154. (lambda (x)
  18155. (if (equal true-file
  18156. (file-truename x))
  18157. nil x))
  18158. (org-agenda-files t)))))
  18159. (if (not (= (length files) (length (org-agenda-files t))))
  18160. (progn
  18161. (org-store-new-agenda-file-list files)
  18162. (org-install-agenda-files-menu)
  18163. (message "Removed file: %s" afile))
  18164. (message "File was not in list: %s" afile))))
  18165. (defun org-file-menu-entry (file)
  18166. (vector file (list 'find-file file) t))
  18167. (defun org-check-agenda-file (file)
  18168. "Make sure FILE exists. If not, ask user what to do."
  18169. (when (not (file-exists-p file))
  18170. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18171. (abbreviate-file-name file))
  18172. (let ((r (downcase (read-char-exclusive))))
  18173. (cond
  18174. ((equal r ?r)
  18175. (org-remove-file file)
  18176. (throw 'nextfile t))
  18177. (t (error "Abort"))))))
  18178. ;;; Agenda prepare and finalize
  18179. (defvar org-agenda-multi nil) ; dynammically scoped
  18180. (defvar org-agenda-buffer-name "*Org Agenda*")
  18181. (defvar org-pre-agenda-window-conf nil)
  18182. (defvar org-agenda-name nil)
  18183. (defun org-prepare-agenda (&optional name)
  18184. (setq org-todo-keywords-for-agenda nil)
  18185. (setq org-done-keywords-for-agenda nil)
  18186. (if org-agenda-multi
  18187. (progn
  18188. (setq buffer-read-only nil)
  18189. (goto-char (point-max))
  18190. (unless (or (bobp) org-agenda-compact-blocks)
  18191. (insert "\n" (make-string (window-width) ?=) "\n"))
  18192. (narrow-to-region (point) (point-max)))
  18193. (org-agenda-maybe-reset-markers 'force)
  18194. (org-prepare-agenda-buffers (org-agenda-files))
  18195. (setq org-todo-keywords-for-agenda
  18196. (org-uniquify org-todo-keywords-for-agenda))
  18197. (setq org-done-keywords-for-agenda
  18198. (org-uniquify org-done-keywords-for-agenda))
  18199. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18200. (awin (get-buffer-window abuf)))
  18201. (cond
  18202. ((equal (current-buffer) abuf) nil)
  18203. (awin (select-window awin))
  18204. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18205. ((equal org-agenda-window-setup 'current-window)
  18206. (switch-to-buffer abuf))
  18207. ((equal org-agenda-window-setup 'other-window)
  18208. (org-switch-to-buffer-other-window abuf))
  18209. ((equal org-agenda-window-setup 'other-frame)
  18210. (switch-to-buffer-other-frame abuf))
  18211. ((equal org-agenda-window-setup 'reorganize-frame)
  18212. (delete-other-windows)
  18213. (org-switch-to-buffer-other-window abuf))))
  18214. (setq buffer-read-only nil)
  18215. (erase-buffer)
  18216. (org-agenda-mode)
  18217. (and name (not org-agenda-name)
  18218. (org-set-local 'org-agenda-name name)))
  18219. (setq buffer-read-only nil))
  18220. (defun org-finalize-agenda ()
  18221. "Finishing touch for the agenda buffer, called just before displaying it."
  18222. (unless org-agenda-multi
  18223. (save-excursion
  18224. (let ((inhibit-read-only t))
  18225. (goto-char (point-min))
  18226. (while (org-activate-bracket-links (point-max))
  18227. (add-text-properties (match-beginning 0) (match-end 0)
  18228. '(face org-link)))
  18229. (org-agenda-align-tags)
  18230. (unless org-agenda-with-colors
  18231. (remove-text-properties (point-min) (point-max) '(face nil))))
  18232. (if (and (boundp 'org-overriding-columns-format)
  18233. org-overriding-columns-format)
  18234. (org-set-local 'org-overriding-columns-format
  18235. org-overriding-columns-format))
  18236. (if (and (boundp 'org-agenda-view-columns-initially)
  18237. org-agenda-view-columns-initially)
  18238. (org-agenda-columns))
  18239. (when org-agenda-fontify-priorities
  18240. (org-fontify-priorities))
  18241. (run-hooks 'org-finalize-agenda-hook))))
  18242. (defun org-fontify-priorities ()
  18243. "Make highest priority lines bold, and lowest italic."
  18244. (interactive)
  18245. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18246. (org-delete-overlay o)))
  18247. (org-overlays-in (point-min) (point-max)))
  18248. (save-excursion
  18249. (let ((inhibit-read-only t)
  18250. b e p ov h l)
  18251. (goto-char (point-min))
  18252. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18253. (setq h (or (get-char-property (point) 'org-highest-priority)
  18254. org-highest-priority)
  18255. l (or (get-char-property (point) 'org-lowest-priority)
  18256. org-lowest-priority)
  18257. p (string-to-char (match-string 1))
  18258. b (match-beginning 0) e (point-at-eol)
  18259. ov (org-make-overlay b e))
  18260. (org-overlay-put
  18261. ov 'face
  18262. (cond ((listp org-agenda-fontify-priorities)
  18263. (cdr (assoc p org-agenda-fontify-priorities)))
  18264. ((equal p l) 'italic)
  18265. ((equal p h) 'bold)))
  18266. (org-overlay-put ov 'org-type 'org-priority)))))
  18267. (defun org-prepare-agenda-buffers (files)
  18268. "Create buffers for all agenda files, protect archived trees and comments."
  18269. (interactive)
  18270. (let ((pa '(:org-archived t))
  18271. (pc '(:org-comment t))
  18272. (pall '(:org-archived t :org-comment t))
  18273. (inhibit-read-only t)
  18274. (rea (concat ":" org-archive-tag ":"))
  18275. bmp file re)
  18276. (save-excursion
  18277. (save-restriction
  18278. (while (setq file (pop files))
  18279. (if (bufferp file)
  18280. (set-buffer file)
  18281. (org-check-agenda-file file)
  18282. (set-buffer (org-get-agenda-file-buffer file)))
  18283. (widen)
  18284. (setq bmp (buffer-modified-p))
  18285. (org-refresh-category-properties)
  18286. (setq org-todo-keywords-for-agenda
  18287. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18288. (setq org-done-keywords-for-agenda
  18289. (append org-done-keywords-for-agenda org-done-keywords))
  18290. (save-excursion
  18291. (remove-text-properties (point-min) (point-max) pall)
  18292. (when org-agenda-skip-archived-trees
  18293. (goto-char (point-min))
  18294. (while (re-search-forward rea nil t)
  18295. (if (org-on-heading-p t)
  18296. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18297. (goto-char (point-min))
  18298. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18299. (while (re-search-forward re nil t)
  18300. (add-text-properties
  18301. (match-beginning 0) (org-end-of-subtree t) pc)))
  18302. (set-buffer-modified-p bmp))))))
  18303. (defvar org-agenda-skip-function nil
  18304. "Function to be called at each match during agenda construction.
  18305. If this function returns nil, the current match should not be skipped.
  18306. Otherwise, the function must return a position from where the search
  18307. should be continued.
  18308. This may also be a Lisp form, it will be evaluated.
  18309. Never set this variable using `setq' or so, because then it will apply
  18310. to all future agenda commands. Instead, bind it with `let' to scope
  18311. it dynamically into the agenda-constructing command. A good way to set
  18312. it is through options in org-agenda-custom-commands.")
  18313. (defun org-agenda-skip ()
  18314. "Throw to `:skip' in places that should be skipped.
  18315. Also moves point to the end of the skipped region, so that search can
  18316. continue from there."
  18317. (let ((p (point-at-bol)) to fp)
  18318. (and org-agenda-skip-archived-trees
  18319. (get-text-property p :org-archived)
  18320. (org-end-of-subtree t)
  18321. (throw :skip t))
  18322. (and (get-text-property p :org-comment)
  18323. (org-end-of-subtree t)
  18324. (throw :skip t))
  18325. (if (equal (char-after p) ?#) (throw :skip t))
  18326. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18327. (consp org-agenda-skip-function))
  18328. (setq to (save-excursion
  18329. (save-match-data
  18330. (if fp
  18331. (funcall org-agenda-skip-function)
  18332. (eval org-agenda-skip-function))))))
  18333. (goto-char to)
  18334. (throw :skip t))))
  18335. (defvar org-agenda-markers nil
  18336. "List of all currently active markers created by `org-agenda'.")
  18337. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18338. "Creation time of the last agenda marker.")
  18339. (defun org-agenda-new-marker (&optional pos)
  18340. "Return a new agenda marker.
  18341. Org-mode keeps a list of these markers and resets them when they are
  18342. no longer in use."
  18343. (let ((m (copy-marker (or pos (point)))))
  18344. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18345. (push m org-agenda-markers)
  18346. m))
  18347. (defun org-agenda-maybe-reset-markers (&optional force)
  18348. "Reset markers created by `org-agenda'. But only if they are old enough."
  18349. (if (or (and force (not org-agenda-multi))
  18350. (> (- (time-to-seconds (current-time))
  18351. org-agenda-last-marker-time)
  18352. 5))
  18353. (while org-agenda-markers
  18354. (move-marker (pop org-agenda-markers) nil))))
  18355. (defun org-get-agenda-file-buffer (file)
  18356. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18357. it to the list of buffers which might be released later."
  18358. (let ((buf (org-find-base-buffer-visiting file)))
  18359. (if buf
  18360. buf ; just return it
  18361. ;; Make a new buffer and remember it
  18362. (setq buf (find-file-noselect file))
  18363. (if buf (push buf org-agenda-new-buffers))
  18364. buf)))
  18365. (defun org-release-buffers (blist)
  18366. "Release all buffers in list, asking the user for confirmation when needed.
  18367. When a buffer is unmodified, it is just killed. When modified, it is saved
  18368. \(if the user agrees) and then killed."
  18369. (let (buf file)
  18370. (while (setq buf (pop blist))
  18371. (setq file (buffer-file-name buf))
  18372. (when (and (buffer-modified-p buf)
  18373. file
  18374. (y-or-n-p (format "Save file %s? " file)))
  18375. (with-current-buffer buf (save-buffer)))
  18376. (kill-buffer buf))))
  18377. (defun org-get-category (&optional pos)
  18378. "Get the category applying to position POS."
  18379. (get-text-property (or pos (point)) 'org-category))
  18380. ;;; Agenda timeline
  18381. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18382. (defun org-timeline (&optional include-all)
  18383. "Show a time-sorted view of the entries in the current org file.
  18384. Only entries with a time stamp of today or later will be listed. With
  18385. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18386. under the current date.
  18387. If the buffer contains an active region, only check the region for
  18388. dates."
  18389. (interactive "P")
  18390. (require 'calendar)
  18391. (org-compile-prefix-format 'timeline)
  18392. (org-set-sorting-strategy 'timeline)
  18393. (let* ((dopast t)
  18394. (dotodo include-all)
  18395. (doclosed org-agenda-show-log)
  18396. (entry buffer-file-name)
  18397. (date (calendar-current-date))
  18398. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18399. (end (if (org-region-active-p) (region-end) (point-max)))
  18400. (day-numbers (org-get-all-dates beg end 'no-ranges
  18401. t doclosed ; always include today
  18402. org-timeline-show-empty-dates))
  18403. (org-deadline-warning-days 0)
  18404. (org-agenda-only-exact-dates t)
  18405. (today (time-to-days (current-time)))
  18406. (past t)
  18407. args
  18408. s e rtn d emptyp)
  18409. (setq org-agenda-redo-command
  18410. (list 'progn
  18411. (list 'org-switch-to-buffer-other-window (current-buffer))
  18412. (list 'org-timeline (list 'quote include-all))))
  18413. (if (not dopast)
  18414. ;; Remove past dates from the list of dates.
  18415. (setq day-numbers (delq nil (mapcar (lambda(x)
  18416. (if (>= x today) x nil))
  18417. day-numbers))))
  18418. (org-prepare-agenda (concat "Timeline "
  18419. (file-name-nondirectory buffer-file-name)))
  18420. (if doclosed (push :closed args))
  18421. (push :timestamp args)
  18422. (push :deadline args)
  18423. (push :scheduled args)
  18424. (push :sexp args)
  18425. (if dotodo (push :todo args))
  18426. (while (setq d (pop day-numbers))
  18427. (if (and (listp d) (eq (car d) :omitted))
  18428. (progn
  18429. (setq s (point))
  18430. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18431. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18432. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18433. (if (and (>= d today)
  18434. dopast
  18435. past)
  18436. (progn
  18437. (setq past nil)
  18438. (insert (make-string 79 ?-) "\n")))
  18439. (setq date (calendar-gregorian-from-absolute d))
  18440. (setq s (point))
  18441. (setq rtn (and (not emptyp)
  18442. (apply 'org-agenda-get-day-entries entry
  18443. date args)))
  18444. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18445. (progn
  18446. (insert
  18447. (if (stringp org-agenda-format-date)
  18448. (format-time-string org-agenda-format-date
  18449. (org-time-from-absolute date))
  18450. (funcall org-agenda-format-date date))
  18451. "\n")
  18452. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18453. (put-text-property s (1- (point)) 'org-date-line t)
  18454. (if (equal d today)
  18455. (put-text-property s (1- (point)) 'org-today t))
  18456. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18457. (put-text-property s (1- (point)) 'day d)))))
  18458. (goto-char (point-min))
  18459. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18460. (point-min)))
  18461. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18462. (org-finalize-agenda)
  18463. (setq buffer-read-only t)))
  18464. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  18465. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18466. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18467. not every single day in the range. If FORCE-TODAY is non-nil, make
  18468. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18469. inactive time stamps (those in square brackets) are included.
  18470. When EMPTY is non-nil, also include days without any entries."
  18471. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  18472. dates dates1 date day day1 day2 ts1 ts2)
  18473. (if force-today
  18474. (setq dates (list (time-to-days (current-time)))))
  18475. (save-excursion
  18476. (goto-char beg)
  18477. (while (re-search-forward re end t)
  18478. (setq day (time-to-days (org-time-string-to-time
  18479. (substring (match-string 1) 0 10))))
  18480. (or (memq day dates) (push day dates)))
  18481. (unless no-ranges
  18482. (goto-char beg)
  18483. (while (re-search-forward org-tr-regexp end t)
  18484. (setq ts1 (substring (match-string 1) 0 10)
  18485. ts2 (substring (match-string 2) 0 10)
  18486. day1 (time-to-days (org-time-string-to-time ts1))
  18487. day2 (time-to-days (org-time-string-to-time ts2)))
  18488. (while (< (setq day1 (1+ day1)) day2)
  18489. (or (memq day1 dates) (push day1 dates)))))
  18490. (setq dates (sort dates '<))
  18491. (when empty
  18492. (while (setq day (pop dates))
  18493. (setq day2 (car dates))
  18494. (push day dates1)
  18495. (when (and day2 empty)
  18496. (if (or (eq empty t)
  18497. (and (numberp empty) (<= (- day2 day) empty)))
  18498. (while (< (setq day (1+ day)) day2)
  18499. (push (list day) dates1))
  18500. (push (cons :omitted (- day2 day)) dates1))))
  18501. (setq dates (nreverse dates1)))
  18502. dates)))
  18503. ;;; Agenda Daily/Weekly
  18504. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18505. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18506. (defvar org-agenda-last-arguments nil
  18507. "The arguments of the previous call to org-agenda")
  18508. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18509. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18510. (defvar org-include-all-loc nil) ; local variable
  18511. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18512. ;;;###autoload
  18513. (defun org-agenda-list (&optional include-all start-day ndays)
  18514. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18515. The view will be for the current day or week, but from the overview buffer
  18516. you will be able to go to other days/weeks.
  18517. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18518. all unfinished TODO items will also be shown, before the agenda.
  18519. This feature is considered obsolete, please use the TODO list or a block
  18520. agenda instead.
  18521. With a numeric prefix argument in an interactive call, the agenda will
  18522. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18523. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18524. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18525. given in `org-agenda-start-on-weekday'."
  18526. (interactive "P")
  18527. (if (and (integerp include-all) (> include-all 0))
  18528. (setq ndays include-all include-all nil))
  18529. (setq ndays (or ndays org-agenda-ndays)
  18530. start-day (or start-day org-agenda-start-day))
  18531. (if org-agenda-overriding-arguments
  18532. (setq include-all (car org-agenda-overriding-arguments)
  18533. start-day (nth 1 org-agenda-overriding-arguments)
  18534. ndays (nth 2 org-agenda-overriding-arguments)))
  18535. (if (stringp start-day)
  18536. ;; Convert to an absolute day number
  18537. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18538. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18539. (org-compile-prefix-format 'agenda)
  18540. (org-set-sorting-strategy 'agenda)
  18541. (require 'calendar)
  18542. (let* ((org-agenda-start-on-weekday
  18543. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18544. org-agenda-start-on-weekday nil))
  18545. (thefiles (org-agenda-files))
  18546. (files thefiles)
  18547. (today (time-to-days
  18548. (time-subtract (current-time)
  18549. (list 0 (* 3600 org-extend-today-until) 0))))
  18550. (sd (or start-day today))
  18551. (start (if (or (null org-agenda-start-on-weekday)
  18552. (< org-agenda-ndays 7))
  18553. sd
  18554. (let* ((nt (calendar-day-of-week
  18555. (calendar-gregorian-from-absolute sd)))
  18556. (n1 org-agenda-start-on-weekday)
  18557. (d (- nt n1)))
  18558. (- sd (+ (if (< d 0) 7 0) d)))))
  18559. (day-numbers (list start))
  18560. (day-cnt 0)
  18561. (inhibit-redisplay (not debug-on-error))
  18562. s e rtn rtnall file date d start-pos end-pos todayp nd)
  18563. (setq org-agenda-redo-command
  18564. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  18565. ;; Make the list of days
  18566. (setq ndays (or ndays org-agenda-ndays)
  18567. nd ndays)
  18568. (while (> ndays 1)
  18569. (push (1+ (car day-numbers)) day-numbers)
  18570. (setq ndays (1- ndays)))
  18571. (setq day-numbers (nreverse day-numbers))
  18572. (org-prepare-agenda "Day/Week")
  18573. (org-set-local 'org-starting-day (car day-numbers))
  18574. (org-set-local 'org-include-all-loc include-all)
  18575. (org-set-local 'org-agenda-span
  18576. (org-agenda-ndays-to-span nd))
  18577. (when (and (or include-all org-agenda-include-all-todo)
  18578. (member today day-numbers))
  18579. (setq files thefiles
  18580. rtnall nil)
  18581. (while (setq file (pop files))
  18582. (catch 'nextfile
  18583. (org-check-agenda-file file)
  18584. (setq date (calendar-gregorian-from-absolute today)
  18585. rtn (org-agenda-get-day-entries
  18586. file date :todo))
  18587. (setq rtnall (append rtnall rtn))))
  18588. (when rtnall
  18589. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  18590. (add-text-properties (point-min) (1- (point))
  18591. (list 'face 'org-agenda-structure))
  18592. (insert (org-finalize-agenda-entries rtnall) "\n")))
  18593. (unless org-agenda-compact-blocks
  18594. (setq s (point))
  18595. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  18596. "-agenda:\n")
  18597. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  18598. 'org-date-line t)))
  18599. (while (setq d (pop day-numbers))
  18600. (setq date (calendar-gregorian-from-absolute d)
  18601. s (point))
  18602. (if (or (setq todayp (= d today))
  18603. (and (not start-pos) (= d sd)))
  18604. (setq start-pos (point))
  18605. (if (and start-pos (not end-pos))
  18606. (setq end-pos (point))))
  18607. (setq files thefiles
  18608. rtnall nil)
  18609. (while (setq file (pop files))
  18610. (catch 'nextfile
  18611. (org-check-agenda-file file)
  18612. (if org-agenda-show-log
  18613. (setq rtn (org-agenda-get-day-entries
  18614. file date
  18615. :deadline :scheduled :timestamp :sexp :closed))
  18616. (setq rtn (org-agenda-get-day-entries
  18617. file date
  18618. :deadline :scheduled :sexp :timestamp)))
  18619. (setq rtnall (append rtnall rtn))))
  18620. (if org-agenda-include-diary
  18621. (progn
  18622. (require 'diary-lib)
  18623. (setq rtn (org-get-entries-from-diary date))
  18624. (setq rtnall (append rtnall rtn))))
  18625. (if (or rtnall org-agenda-show-all-dates)
  18626. (progn
  18627. (setq day-cnt (1+ day-cnt))
  18628. (insert
  18629. (if (stringp org-agenda-format-date)
  18630. (format-time-string org-agenda-format-date
  18631. (org-time-from-absolute date))
  18632. (funcall org-agenda-format-date date))
  18633. "\n")
  18634. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18635. (put-text-property s (1- (point)) 'org-date-line t)
  18636. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  18637. (if todayp (put-text-property s (1- (point)) 'org-today t))
  18638. (if rtnall (insert
  18639. (org-finalize-agenda-entries
  18640. (org-agenda-add-time-grid-maybe
  18641. rtnall nd todayp))
  18642. "\n"))
  18643. (put-text-property s (1- (point)) 'day d)
  18644. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  18645. (goto-char (point-min))
  18646. (org-fit-agenda-window)
  18647. (unless (and (pos-visible-in-window-p (point-min))
  18648. (pos-visible-in-window-p (point-max)))
  18649. (goto-char (1- (point-max)))
  18650. (recenter -1)
  18651. (if (not (pos-visible-in-window-p (or start-pos 1)))
  18652. (progn
  18653. (goto-char (or start-pos 1))
  18654. (recenter 1))))
  18655. (goto-char (or start-pos 1))
  18656. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  18657. (org-finalize-agenda)
  18658. (setq buffer-read-only t)
  18659. (message "")))
  18660. (defun org-agenda-ndays-to-span (n)
  18661. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  18662. ;;; Agenda TODO list
  18663. (defvar org-select-this-todo-keyword nil)
  18664. (defvar org-last-arg nil)
  18665. ;;;###autoload
  18666. (defun org-todo-list (arg)
  18667. "Show all TODO entries from all agenda file in a single list.
  18668. The prefix arg can be used to select a specific TODO keyword and limit
  18669. the list to these. When using \\[universal-argument], you will be prompted
  18670. for a keyword. A numeric prefix directly selects the Nth keyword in
  18671. `org-todo-keywords-1'."
  18672. (interactive "P")
  18673. (require 'calendar)
  18674. (org-compile-prefix-format 'todo)
  18675. (org-set-sorting-strategy 'todo)
  18676. (org-prepare-agenda "TODO")
  18677. (let* ((today (time-to-days (current-time)))
  18678. (date (calendar-gregorian-from-absolute today))
  18679. (kwds org-todo-keywords-for-agenda)
  18680. (completion-ignore-case t)
  18681. (org-select-this-todo-keyword
  18682. (if (stringp arg) arg
  18683. (and arg (integerp arg) (> arg 0)
  18684. (nth (1- arg) kwds))))
  18685. rtn rtnall files file pos)
  18686. (when (equal arg '(4))
  18687. (setq org-select-this-todo-keyword
  18688. (completing-read "Keyword (or KWD1|K2D2|...): "
  18689. (mapcar 'list kwds) nil nil)))
  18690. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  18691. (org-set-local 'org-last-arg arg)
  18692. (setq org-agenda-redo-command
  18693. '(org-todo-list (or current-prefix-arg org-last-arg)))
  18694. (setq files (org-agenda-files)
  18695. rtnall nil)
  18696. (while (setq file (pop files))
  18697. (catch 'nextfile
  18698. (org-check-agenda-file file)
  18699. (setq rtn (org-agenda-get-day-entries file date :todo))
  18700. (setq rtnall (append rtnall rtn))))
  18701. (if org-agenda-overriding-header
  18702. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18703. nil 'face 'org-agenda-structure) "\n")
  18704. (insert "Global list of TODO items of type: ")
  18705. (add-text-properties (point-min) (1- (point))
  18706. (list 'face 'org-agenda-structure))
  18707. (setq pos (point))
  18708. (insert (or org-select-this-todo-keyword "ALL") "\n")
  18709. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  18710. (setq pos (point))
  18711. (unless org-agenda-multi
  18712. (insert "Available with `N r': (0)ALL")
  18713. (let ((n 0) s)
  18714. (mapc (lambda (x)
  18715. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  18716. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  18717. (insert "\n "))
  18718. (insert " " s))
  18719. kwds))
  18720. (insert "\n"))
  18721. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  18722. (when rtnall
  18723. (insert (org-finalize-agenda-entries rtnall) "\n"))
  18724. (goto-char (point-min))
  18725. (org-fit-agenda-window)
  18726. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  18727. (org-finalize-agenda)
  18728. (setq buffer-read-only t)))
  18729. ;;; Agenda tags match
  18730. ;;;###autoload
  18731. (defun org-tags-view (&optional todo-only match)
  18732. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  18733. The prefix arg TODO-ONLY limits the search to TODO entries."
  18734. (interactive "P")
  18735. (org-compile-prefix-format 'tags)
  18736. (org-set-sorting-strategy 'tags)
  18737. (let* ((org-tags-match-list-sublevels
  18738. (if todo-only t org-tags-match-list-sublevels))
  18739. (completion-ignore-case t)
  18740. rtn rtnall files file pos matcher
  18741. buffer)
  18742. (setq matcher (org-make-tags-matcher match)
  18743. match (car matcher) matcher (cdr matcher))
  18744. (org-prepare-agenda (concat "TAGS " match))
  18745. (setq org-agenda-redo-command
  18746. (list 'org-tags-view (list 'quote todo-only)
  18747. (list 'if 'current-prefix-arg nil match)))
  18748. (setq files (org-agenda-files)
  18749. rtnall nil)
  18750. (while (setq file (pop files))
  18751. (catch 'nextfile
  18752. (org-check-agenda-file file)
  18753. (setq buffer (if (file-exists-p file)
  18754. (org-get-agenda-file-buffer file)
  18755. (error "No such file %s" file)))
  18756. (if (not buffer)
  18757. ;; If file does not exist, merror message to agenda
  18758. (setq rtn (list
  18759. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  18760. rtnall (append rtnall rtn))
  18761. (with-current-buffer buffer
  18762. (unless (org-mode-p)
  18763. (error "Agenda file %s is not in `org-mode'" file))
  18764. (save-excursion
  18765. (save-restriction
  18766. (if org-agenda-restrict
  18767. (narrow-to-region org-agenda-restrict-begin
  18768. org-agenda-restrict-end)
  18769. (widen))
  18770. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  18771. (setq rtnall (append rtnall rtn))))))))
  18772. (if org-agenda-overriding-header
  18773. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18774. nil 'face 'org-agenda-structure) "\n")
  18775. (insert "Headlines with TAGS match: ")
  18776. (add-text-properties (point-min) (1- (point))
  18777. (list 'face 'org-agenda-structure))
  18778. (setq pos (point))
  18779. (insert match "\n")
  18780. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  18781. (setq pos (point))
  18782. (unless org-agenda-multi
  18783. (insert "Press `C-u r' to search again with new search string\n"))
  18784. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  18785. (when rtnall
  18786. (insert (org-finalize-agenda-entries rtnall) "\n"))
  18787. (goto-char (point-min))
  18788. (org-fit-agenda-window)
  18789. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  18790. (org-finalize-agenda)
  18791. (setq buffer-read-only t)))
  18792. ;;; Agenda Finding stuck projects
  18793. (defvar org-agenda-skip-regexp nil
  18794. "Regular expression used in skipping subtrees for the agenda.
  18795. This is basically a temporary global variable that can be set and then
  18796. used by user-defined selections using `org-agenda-skip-function'.")
  18797. (defvar org-agenda-overriding-header nil
  18798. "When this is set during todo and tags searches, will replace header.")
  18799. (defun org-agenda-skip-subtree-when-regexp-matches ()
  18800. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  18801. If yes, it returns the end position of this tree, causing agenda commands
  18802. to skip this subtree. This is a function that can be put into
  18803. `org-agenda-skip-function' for the duration of a command."
  18804. (let ((end (save-excursion (org-end-of-subtree t)))
  18805. skip)
  18806. (save-excursion
  18807. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  18808. (and skip end)))
  18809. (defun org-agenda-skip-entry-if (&rest conditions)
  18810. "Skip entry if any of CONDITIONS is true.
  18811. See `org-agenda-skip-if for details."
  18812. (org-agenda-skip-if nil conditions))
  18813. (defun org-agenda-skip-subtree-if (&rest conditions)
  18814. "Skip entry if any of CONDITIONS is true.
  18815. See `org-agenda-skip-if for details."
  18816. (org-agenda-skip-if t conditions))
  18817. (defun org-agenda-skip-if (subtree conditions)
  18818. "Checks current entity for CONDITIONS.
  18819. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  18820. the entry, i.e. the text before the next heading is checked.
  18821. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  18822. from different tests. Valid conditions are:
  18823. scheduled Check if there is a scheduled cookie
  18824. notscheduled Check if there is no scheduled cookie
  18825. deadline Check if there is a deadline
  18826. notdeadline Check if there is no deadline
  18827. regexp Check if regexp matches
  18828. notregexp Check if regexp does not match.
  18829. The regexp is taken from the conditions list, it must com right after the
  18830. `regexp' of `notregexp' element.
  18831. If any of these conditions is met, this function returns the end point of
  18832. the entity, causing the search to continue from there. This is a function
  18833. that can be put into `org-agenda-skip-function' for the duration of a command."
  18834. (let (beg end m)
  18835. (org-back-to-heading t)
  18836. (setq beg (point)
  18837. end (if subtree
  18838. (progn (org-end-of-subtree t) (point))
  18839. (progn (outline-next-heading) (1- (point)))))
  18840. (goto-char beg)
  18841. (and
  18842. (or
  18843. (and (memq 'scheduled conditions)
  18844. (re-search-forward org-scheduled-time-regexp end t))
  18845. (and (memq 'notscheduled conditions)
  18846. (not (re-search-forward org-scheduled-time-regexp end t)))
  18847. (and (memq 'deadline conditions)
  18848. (re-search-forward org-deadline-time-regexp end t))
  18849. (and (memq 'notdeadline conditions)
  18850. (not (re-search-forward org-deadline-time-regexp end t)))
  18851. (and (setq m (memq 'regexp conditions))
  18852. (stringp (nth 1 m))
  18853. (re-search-forward (nth 1 m) end t))
  18854. (and (setq m (memq 'notregexp conditions))
  18855. (stringp (nth 1 m))
  18856. (not (re-search-forward (nth 1 m) end t))))
  18857. end)))
  18858. ;;;###autoload
  18859. (defun org-agenda-list-stuck-projects (&rest ignore)
  18860. "Create agenda view for projects that are stuck.
  18861. Stuck projects are project that have no next actions. For the definitions
  18862. of what a project is and how to check if it stuck, customize the variable
  18863. `org-stuck-projects'.
  18864. MATCH is being ignored."
  18865. (interactive)
  18866. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  18867. ;; FIXME: we could have used org-agenda-skip-if here.
  18868. (org-agenda-overriding-header "List of stuck projects: ")
  18869. (matcher (nth 0 org-stuck-projects))
  18870. (todo (nth 1 org-stuck-projects))
  18871. (todo-wds (if (member "*" todo)
  18872. (progn
  18873. (org-prepare-agenda-buffers (org-agenda-files))
  18874. (org-delete-all
  18875. org-done-keywords-for-agenda
  18876. (copy-sequence org-todo-keywords-for-agenda)))
  18877. todo))
  18878. (todo-re (concat "^\\*+[ \t]+\\("
  18879. (mapconcat 'identity todo-wds "\\|")
  18880. "\\)\\>"))
  18881. (tags (nth 2 org-stuck-projects))
  18882. (tags-re (if (member "*" tags)
  18883. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  18884. (concat "^\\*+ .*:\\("
  18885. (mapconcat 'identity tags "\\|")
  18886. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  18887. (gen-re (nth 3 org-stuck-projects))
  18888. (re-list
  18889. (delq nil
  18890. (list
  18891. (if todo todo-re)
  18892. (if tags tags-re)
  18893. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  18894. gen-re)))))
  18895. (setq org-agenda-skip-regexp
  18896. (if re-list
  18897. (mapconcat 'identity re-list "\\|")
  18898. (error "No information how to identify unstuck projects")))
  18899. (org-tags-view nil matcher)
  18900. (with-current-buffer org-agenda-buffer-name
  18901. (setq org-agenda-redo-command
  18902. '(org-agenda-list-stuck-projects
  18903. (or current-prefix-arg org-last-arg))))))
  18904. ;;; Diary integration
  18905. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  18906. (defun org-get-entries-from-diary (date)
  18907. "Get the (Emacs Calendar) diary entries for DATE."
  18908. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  18909. (diary-display-hook '(fancy-diary-display))
  18910. (pop-up-frames nil)
  18911. (list-diary-entries-hook
  18912. (cons 'org-diary-default-entry list-diary-entries-hook))
  18913. (diary-file-name-prefix-function nil) ; turn this feature off
  18914. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  18915. entries
  18916. (org-disable-agenda-to-diary t))
  18917. (save-excursion
  18918. (save-window-excursion
  18919. (funcall (if (fboundp 'diary-list-entries)
  18920. 'diary-list-entries 'list-diary-entries)
  18921. date 1)))
  18922. (if (not (get-buffer fancy-diary-buffer))
  18923. (setq entries nil)
  18924. (with-current-buffer fancy-diary-buffer
  18925. (setq buffer-read-only nil)
  18926. (if (zerop (buffer-size))
  18927. ;; No entries
  18928. (setq entries nil)
  18929. ;; Omit the date and other unnecessary stuff
  18930. (org-agenda-cleanup-fancy-diary)
  18931. ;; Add prefix to each line and extend the text properties
  18932. (if (zerop (buffer-size))
  18933. (setq entries nil)
  18934. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  18935. (set-buffer-modified-p nil)
  18936. (kill-buffer fancy-diary-buffer)))
  18937. (when entries
  18938. (setq entries (org-split-string entries "\n"))
  18939. (setq entries
  18940. (mapcar
  18941. (lambda (x)
  18942. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  18943. ;; Extend the text properties to the beginning of the line
  18944. (org-add-props x (text-properties-at (1- (length x)) x)
  18945. 'type "diary" 'date date))
  18946. entries)))))
  18947. (defun org-agenda-cleanup-fancy-diary ()
  18948. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  18949. This gets rid of the date, the underline under the date, and
  18950. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  18951. date. It also removes lines that contain only whitespace."
  18952. (goto-char (point-min))
  18953. (if (looking-at ".*?:[ \t]*")
  18954. (progn
  18955. (replace-match "")
  18956. (re-search-forward "\n=+$" nil t)
  18957. (replace-match "")
  18958. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  18959. (re-search-forward "\n=+$" nil t)
  18960. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  18961. (goto-char (point-min))
  18962. (while (re-search-forward "^ +\n" nil t)
  18963. (replace-match ""))
  18964. (goto-char (point-min))
  18965. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  18966. (replace-match "")))
  18967. ;; Make sure entries from the diary have the right text properties.
  18968. (eval-after-load "diary-lib"
  18969. '(if (boundp 'diary-modify-entry-list-string-function)
  18970. ;; We can rely on the hook, nothing to do
  18971. nil
  18972. ;; Hook not avaiable, must use advice to make this work
  18973. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  18974. "Make the position visible."
  18975. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  18976. (stringp string)
  18977. buffer-file-name)
  18978. (setq string (org-modify-diary-entry-string string))))))
  18979. (defun org-modify-diary-entry-string (string)
  18980. "Add text properties to string, allowing org-mode to act on it."
  18981. (org-add-props string nil
  18982. 'mouse-face 'highlight
  18983. 'keymap org-agenda-keymap
  18984. 'help-echo (if buffer-file-name
  18985. (format "mouse-2 or RET jump to diary file %s"
  18986. (abbreviate-file-name buffer-file-name))
  18987. "")
  18988. 'org-agenda-diary-link t
  18989. 'org-marker (org-agenda-new-marker (point-at-bol))))
  18990. (defun org-diary-default-entry ()
  18991. "Add a dummy entry to the diary.
  18992. Needed to avoid empty dates which mess up holiday display."
  18993. ;; Catch the error if dealing with the new add-to-diary-alist
  18994. (when org-disable-agenda-to-diary
  18995. (condition-case nil
  18996. (add-to-diary-list original-date "Org-mode dummy" "")
  18997. (error
  18998. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  18999. ;;;###autoload
  19000. (defun org-diary (&rest args)
  19001. "Return diary information from org-files.
  19002. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19003. It accesses org files and extracts information from those files to be
  19004. listed in the diary. The function accepts arguments specifying what
  19005. items should be listed. The following arguments are allowed:
  19006. :timestamp List the headlines of items containing a date stamp or
  19007. date range matching the selected date. Deadlines will
  19008. also be listed, on the expiration day.
  19009. :sexp List entries resulting from diary-like sexps.
  19010. :deadline List any deadlines past due, or due within
  19011. `org-deadline-warning-days'. The listing occurs only
  19012. in the diary for *today*, not at any other date. If
  19013. an entry is marked DONE, it is no longer listed.
  19014. :scheduled List all items which are scheduled for the given date.
  19015. The diary for *today* also contains items which were
  19016. scheduled earlier and are not yet marked DONE.
  19017. :todo List all TODO items from the org-file. This may be a
  19018. long list - so this is not turned on by default.
  19019. Like deadlines, these entries only show up in the
  19020. diary for *today*, not at any other date.
  19021. The call in the diary file should look like this:
  19022. &%%(org-diary) ~/path/to/some/orgfile.org
  19023. Use a separate line for each org file to check. Or, if you omit the file name,
  19024. all files listed in `org-agenda-files' will be checked automatically:
  19025. &%%(org-diary)
  19026. If you don't give any arguments (as in the example above), the default
  19027. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19028. So the example above may also be written as
  19029. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19030. The function expects the lisp variables `entry' and `date' to be provided
  19031. by the caller, because this is how the calendar works. Don't use this
  19032. function from a program - use `org-agenda-get-day-entries' instead."
  19033. (org-agenda-maybe-reset-markers)
  19034. (org-compile-prefix-format 'agenda)
  19035. (org-set-sorting-strategy 'agenda)
  19036. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19037. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19038. (list entry)
  19039. (org-agenda-files t)))
  19040. file rtn results)
  19041. (org-prepare-agenda-buffers files)
  19042. ;; If this is called during org-agenda, don't return any entries to
  19043. ;; the calendar. Org Agenda will list these entries itself.
  19044. (if org-disable-agenda-to-diary (setq files nil))
  19045. (while (setq file (pop files))
  19046. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19047. (setq results (append results rtn)))
  19048. (if results
  19049. (concat (org-finalize-agenda-entries results) "\n"))))
  19050. ;;; Agenda entry finders
  19051. (defun org-agenda-get-day-entries (file date &rest args)
  19052. "Does the work for `org-diary' and `org-agenda'.
  19053. FILE is the path to a file to be checked for entries. DATE is date like
  19054. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19055. which kind of entries should be extracted. For details about these, see
  19056. the documentation of `org-diary'."
  19057. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19058. (let* ((org-startup-folded nil)
  19059. (org-startup-align-all-tables nil)
  19060. (buffer (if (file-exists-p file)
  19061. (org-get-agenda-file-buffer file)
  19062. (error "No such file %s" file)))
  19063. arg results rtn)
  19064. (if (not buffer)
  19065. ;; If file does not exist, make sure an error message ends up in diary
  19066. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19067. (with-current-buffer buffer
  19068. (unless (org-mode-p)
  19069. (error "Agenda file %s is not in `org-mode'" file))
  19070. (let ((case-fold-search nil))
  19071. (save-excursion
  19072. (save-restriction
  19073. (if org-agenda-restrict
  19074. (narrow-to-region org-agenda-restrict-begin
  19075. org-agenda-restrict-end)
  19076. (widen))
  19077. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19078. (while (setq arg (pop args))
  19079. (cond
  19080. ((and (eq arg :todo)
  19081. (equal date (calendar-current-date)))
  19082. (setq rtn (org-agenda-get-todos))
  19083. (setq results (append results rtn)))
  19084. ((eq arg :timestamp)
  19085. (setq rtn (org-agenda-get-blocks))
  19086. (setq results (append results rtn))
  19087. (setq rtn (org-agenda-get-timestamps))
  19088. (setq results (append results rtn)))
  19089. ((eq arg :sexp)
  19090. (setq rtn (org-agenda-get-sexps))
  19091. (setq results (append results rtn)))
  19092. ((eq arg :scheduled)
  19093. (setq rtn (org-agenda-get-scheduled))
  19094. (setq results (append results rtn)))
  19095. ((eq arg :closed)
  19096. (setq rtn (org-agenda-get-closed))
  19097. (setq results (append results rtn)))
  19098. ((eq arg :deadline)
  19099. (setq rtn (org-agenda-get-deadlines))
  19100. (setq results (append results rtn))))))))
  19101. results))))
  19102. (defun org-entry-is-todo-p ()
  19103. (member (org-get-todo-state) org-not-done-keywords))
  19104. (defun org-entry-is-done-p ()
  19105. (member (org-get-todo-state) org-done-keywords))
  19106. (defun org-get-todo-state ()
  19107. (save-excursion
  19108. (org-back-to-heading t)
  19109. (and (looking-at org-todo-line-regexp)
  19110. (match-end 2)
  19111. (match-string 2))))
  19112. (defun org-at-date-range-p (&optional inactive-ok)
  19113. "Is the cursor inside a date range?"
  19114. (interactive)
  19115. (save-excursion
  19116. (catch 'exit
  19117. (let ((pos (point)))
  19118. (skip-chars-backward "^[<\r\n")
  19119. (skip-chars-backward "<[")
  19120. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19121. (>= (match-end 0) pos)
  19122. (throw 'exit t))
  19123. (skip-chars-backward "^<[\r\n")
  19124. (skip-chars-backward "<[")
  19125. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19126. (>= (match-end 0) pos)
  19127. (throw 'exit t)))
  19128. nil)))
  19129. (defun org-agenda-get-todos ()
  19130. "Return the TODO information for agenda display."
  19131. (let* ((props (list 'face nil
  19132. 'done-face 'org-done
  19133. 'org-not-done-regexp org-not-done-regexp
  19134. 'org-todo-regexp org-todo-regexp
  19135. 'mouse-face 'highlight
  19136. 'keymap org-agenda-keymap
  19137. 'help-echo
  19138. (format "mouse-2 or RET jump to org file %s"
  19139. (abbreviate-file-name buffer-file-name))))
  19140. ;; FIXME: get rid of the \n at some point but watch out
  19141. (regexp (concat "^\\*+[ \t]+\\("
  19142. (if org-select-this-todo-keyword
  19143. (if (equal org-select-this-todo-keyword "*")
  19144. org-todo-regexp
  19145. (concat "\\<\\("
  19146. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19147. "\\)\\>"))
  19148. org-not-done-regexp)
  19149. "[^\n\r]*\\)"))
  19150. marker priority category tags
  19151. ee txt beg end)
  19152. (goto-char (point-min))
  19153. (while (re-search-forward regexp nil t)
  19154. (catch :skip
  19155. (save-match-data
  19156. (beginning-of-line)
  19157. (setq beg (point) end (progn (outline-next-heading) (point)))
  19158. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19159. (re-search-forward org-ts-regexp end t))
  19160. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19161. (re-search-forward org-scheduled-time-regexp end t))
  19162. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19163. (re-search-forward org-deadline-time-regexp end t)
  19164. (org-deadline-close (match-string 1))))
  19165. (goto-char (1+ beg))
  19166. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19167. (throw :skip nil)))
  19168. (goto-char beg)
  19169. (org-agenda-skip)
  19170. (goto-char (match-beginning 1))
  19171. (setq marker (org-agenda-new-marker (match-beginning 0))
  19172. category (org-get-category)
  19173. tags (org-get-tags-at (point))
  19174. txt (org-format-agenda-item "" (match-string 1) category tags)
  19175. priority (1+ (org-get-priority txt)))
  19176. (org-add-props txt props
  19177. 'org-marker marker 'org-hd-marker marker
  19178. 'priority priority 'org-category category
  19179. 'type "todo")
  19180. (push txt ee)
  19181. (if org-agenda-todo-list-sublevels
  19182. (goto-char (match-end 1))
  19183. (org-end-of-subtree 'invisible))))
  19184. (nreverse ee)))
  19185. (defconst org-agenda-no-heading-message
  19186. "No heading for this item in buffer or region.")
  19187. (defun org-agenda-get-timestamps ()
  19188. "Return the date stamp information for agenda display."
  19189. (let* ((props (list 'face nil
  19190. 'org-not-done-regexp org-not-done-regexp
  19191. 'org-todo-regexp org-todo-regexp
  19192. 'mouse-face 'highlight
  19193. 'keymap org-agenda-keymap
  19194. 'help-echo
  19195. (format "mouse-2 or RET jump to org file %s"
  19196. (abbreviate-file-name buffer-file-name))))
  19197. (d1 (calendar-absolute-from-gregorian date))
  19198. (remove-re
  19199. (concat
  19200. (regexp-quote
  19201. (format-time-string
  19202. "<%Y-%m-%d"
  19203. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19204. ".*?>"))
  19205. (regexp
  19206. (concat
  19207. (regexp-quote
  19208. (substring
  19209. (format-time-string
  19210. (car org-time-stamp-formats)
  19211. (apply 'encode-time ; DATE bound by calendar
  19212. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19213. 0 11))
  19214. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19215. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19216. marker hdmarker deadlinep scheduledp donep tmp priority category
  19217. ee txt timestr tags b0 b3 e3 head)
  19218. (goto-char (point-min))
  19219. (while (re-search-forward regexp nil t)
  19220. (setq b0 (match-beginning 0)
  19221. b3 (match-beginning 3) e3 (match-end 3))
  19222. (catch :skip
  19223. (and (org-at-date-range-p) (throw :skip nil))
  19224. (org-agenda-skip)
  19225. (if (and (match-end 1)
  19226. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19227. (throw :skip nil))
  19228. (if (and e3
  19229. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19230. (throw :skip nil))
  19231. (setq marker (org-agenda-new-marker b0)
  19232. category (org-get-category b0)
  19233. tmp (buffer-substring (max (point-min)
  19234. (- b0 org-ds-keyword-length))
  19235. b0)
  19236. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19237. deadlinep (string-match org-deadline-regexp tmp)
  19238. scheduledp (string-match org-scheduled-regexp tmp)
  19239. donep (org-entry-is-done-p))
  19240. (if (or scheduledp deadlinep) (throw :skip t))
  19241. (if (string-match ">" timestr)
  19242. ;; substring should only run to end of time stamp
  19243. (setq timestr (substring timestr 0 (match-end 0))))
  19244. (save-excursion
  19245. (if (re-search-backward "^\\*+ " nil t)
  19246. (progn
  19247. (goto-char (match-beginning 0))
  19248. (setq hdmarker (org-agenda-new-marker)
  19249. tags (org-get-tags-at))
  19250. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19251. (setq head (match-string 1))
  19252. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19253. (setq txt (org-format-agenda-item
  19254. nil head category tags timestr nil
  19255. remove-re)))
  19256. (setq txt org-agenda-no-heading-message))
  19257. (setq priority (org-get-priority txt))
  19258. (org-add-props txt props
  19259. 'org-marker marker 'org-hd-marker hdmarker)
  19260. (org-add-props txt nil 'priority priority
  19261. 'org-category category 'date date
  19262. 'type "timestamp")
  19263. (push txt ee))
  19264. (outline-next-heading)))
  19265. (nreverse ee)))
  19266. (defun org-agenda-get-sexps ()
  19267. "Return the sexp information for agenda display."
  19268. (require 'diary-lib)
  19269. (let* ((props (list 'face nil
  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. (regexp "^&?%%(")
  19276. marker category ee txt tags entry result beg b sexp sexp-entry)
  19277. (goto-char (point-min))
  19278. (while (re-search-forward regexp nil t)
  19279. (catch :skip
  19280. (org-agenda-skip)
  19281. (setq beg (match-beginning 0))
  19282. (goto-char (1- (match-end 0)))
  19283. (setq b (point))
  19284. (forward-sexp 1)
  19285. (setq sexp (buffer-substring b (point)))
  19286. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19287. (org-trim (match-string 1))
  19288. ""))
  19289. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19290. (when result
  19291. (setq marker (org-agenda-new-marker beg)
  19292. category (org-get-category beg))
  19293. (if (string-match "\\S-" result)
  19294. (setq txt result)
  19295. (setq txt "SEXP entry returned empty string"))
  19296. (setq txt (org-format-agenda-item
  19297. "" txt category tags 'time))
  19298. (org-add-props txt props 'org-marker marker)
  19299. (org-add-props txt nil
  19300. 'org-category category 'date date
  19301. 'type "sexp")
  19302. (push txt ee))))
  19303. (nreverse ee)))
  19304. (defun org-agenda-get-closed ()
  19305. "Return the logged TODO entries for agenda display."
  19306. (let* ((props (list 'mouse-face 'highlight
  19307. 'org-not-done-regexp org-not-done-regexp
  19308. 'org-todo-regexp org-todo-regexp
  19309. 'keymap org-agenda-keymap
  19310. 'help-echo
  19311. (format "mouse-2 or RET jump to org file %s"
  19312. (abbreviate-file-name buffer-file-name))))
  19313. (regexp (concat
  19314. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19315. (regexp-quote
  19316. (substring
  19317. (format-time-string
  19318. (car org-time-stamp-formats)
  19319. (apply 'encode-time ; DATE bound by calendar
  19320. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19321. 1 11))))
  19322. marker hdmarker priority category tags closedp
  19323. ee txt timestr)
  19324. (goto-char (point-min))
  19325. (while (re-search-forward regexp nil t)
  19326. (catch :skip
  19327. (org-agenda-skip)
  19328. (setq marker (org-agenda-new-marker (match-beginning 0))
  19329. closedp (equal (match-string 1) org-closed-string)
  19330. category (org-get-category (match-beginning 0))
  19331. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19332. ;; donep (org-entry-is-done-p)
  19333. )
  19334. (if (string-match "\\]" timestr)
  19335. ;; substring should only run to end of time stamp
  19336. (setq timestr (substring timestr 0 (match-end 0))))
  19337. (save-excursion
  19338. (if (re-search-backward "^\\*+ " nil t)
  19339. (progn
  19340. (goto-char (match-beginning 0))
  19341. (setq hdmarker (org-agenda-new-marker)
  19342. tags (org-get-tags-at))
  19343. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19344. (setq txt (org-format-agenda-item
  19345. (if closedp "Closed: " "Clocked: ")
  19346. (match-string 1) category tags timestr)))
  19347. (setq txt org-agenda-no-heading-message))
  19348. (setq priority 100000)
  19349. (org-add-props txt props
  19350. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19351. 'priority priority 'org-category category
  19352. 'type "closed" 'date date
  19353. 'undone-face 'org-warning 'done-face 'org-done)
  19354. (push txt ee))
  19355. (outline-next-heading)))
  19356. (nreverse ee)))
  19357. (defun org-agenda-get-deadlines ()
  19358. "Return the deadline information for agenda display."
  19359. (let* ((props (list 'mouse-face 'highlight
  19360. 'org-not-done-regexp org-not-done-regexp
  19361. 'org-todo-regexp org-todo-regexp
  19362. 'keymap org-agenda-keymap
  19363. 'help-echo
  19364. (format "mouse-2 or RET jump to org file %s"
  19365. (abbreviate-file-name buffer-file-name))))
  19366. (regexp org-deadline-time-regexp)
  19367. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19368. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19369. d2 diff dfrac wdays pos pos1 category tags
  19370. ee txt head face s upcomingp donep timestr)
  19371. (goto-char (point-min))
  19372. (while (re-search-forward regexp nil t)
  19373. (catch :skip
  19374. (org-agenda-skip)
  19375. (setq s (match-string 1)
  19376. pos (1- (match-beginning 1))
  19377. d2 (org-time-string-to-absolute (match-string 1) d1)
  19378. diff (- d2 d1)
  19379. wdays (org-get-wdays s)
  19380. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  19381. upcomingp (and todayp (> diff 0)))
  19382. ;; When to show a deadline in the calendar:
  19383. ;; If the expiration is within wdays warning time.
  19384. ;; Past-due deadlines are only shown on the current date
  19385. (if (or (and (<= diff wdays)
  19386. (and todayp (not org-agenda-only-exact-dates)))
  19387. (= diff 0))
  19388. (save-excursion
  19389. (setq category (org-get-category))
  19390. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19391. (progn
  19392. (goto-char (match-end 0))
  19393. (setq pos1 (match-beginning 0))
  19394. (setq tags (org-get-tags-at pos1))
  19395. (setq head (buffer-substring-no-properties
  19396. (point)
  19397. (progn (skip-chars-forward "^\r\n")
  19398. (point))))
  19399. (setq donep (string-match org-looking-at-done-regexp head))
  19400. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19401. (setq timestr
  19402. (concat (substring s (match-beginning 1)) " "))
  19403. (setq timestr 'time))
  19404. (if (and donep
  19405. (or org-agenda-skip-deadline-if-done
  19406. (not (= diff 0))))
  19407. (setq txt nil)
  19408. (setq txt (org-format-agenda-item
  19409. (if (= diff 0)
  19410. (car org-agenda-deadline-leaders)
  19411. (format (nth 1 org-agenda-deadline-leaders)
  19412. diff))
  19413. head category tags timestr))))
  19414. (setq txt org-agenda-no-heading-message))
  19415. (when txt
  19416. (setq face (org-agenda-deadline-face dfrac))
  19417. (org-add-props txt props
  19418. 'org-marker (org-agenda-new-marker pos)
  19419. 'org-hd-marker (org-agenda-new-marker pos1)
  19420. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  19421. (org-get-priority txt))
  19422. 'org-category category
  19423. 'type (if upcomingp "upcoming-deadline" "deadline")
  19424. 'date (if upcomingp date d2)
  19425. 'face (if donep 'org-done face)
  19426. 'undone-face face 'done-face 'org-done)
  19427. (push txt ee))))))
  19428. (nreverse ee)))
  19429. (defun org-agenda-deadline-face (fraction)
  19430. "Return the face to displaying a deadline item.
  19431. FRACTION is what fraction of the head-warning time has passed."
  19432. (let ((faces org-agenda-deadline-faces) f)
  19433. (catch 'exit
  19434. (while (setq f (pop faces))
  19435. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19436. (defun org-agenda-get-scheduled ()
  19437. "Return the scheduled information for agenda display."
  19438. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19439. 'org-todo-regexp org-todo-regexp
  19440. 'done-face 'org-done
  19441. 'mouse-face 'highlight
  19442. 'keymap org-agenda-keymap
  19443. 'help-echo
  19444. (format "mouse-2 or RET jump to org file %s"
  19445. (abbreviate-file-name buffer-file-name))))
  19446. (regexp org-scheduled-time-regexp)
  19447. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19448. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19449. d2 diff pos pos1 category tags
  19450. ee txt head pastschedp donep face timestr s)
  19451. (goto-char (point-min))
  19452. (while (re-search-forward regexp nil t)
  19453. (catch :skip
  19454. (org-agenda-skip)
  19455. (setq s (match-string 1)
  19456. pos (1- (match-beginning 1))
  19457. d2 (org-time-string-to-absolute (match-string 1) d1)
  19458. diff (- d2 d1))
  19459. (setq pastschedp (and todayp (< diff 0)))
  19460. ;; When to show a scheduled item in the calendar:
  19461. ;; If it is on or past the date.
  19462. (if (or (and (< diff 0)
  19463. (and todayp (not org-agenda-only-exact-dates)))
  19464. (= diff 0))
  19465. (save-excursion
  19466. (setq category (org-get-category))
  19467. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19468. (progn
  19469. (goto-char (match-end 0))
  19470. (setq pos1 (match-beginning 0))
  19471. (setq tags (org-get-tags-at))
  19472. (setq head (buffer-substring-no-properties
  19473. (point)
  19474. (progn (skip-chars-forward "^\r\n") (point))))
  19475. (setq donep (string-match org-looking-at-done-regexp head))
  19476. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19477. (setq timestr
  19478. (concat (substring s (match-beginning 1)) " "))
  19479. (setq timestr 'time))
  19480. (if (and donep
  19481. (or org-agenda-skip-scheduled-if-done
  19482. (not (= diff 0))))
  19483. (setq txt nil)
  19484. (setq txt (org-format-agenda-item
  19485. (if (= diff 0)
  19486. (car org-agenda-scheduled-leaders)
  19487. (format (nth 1 org-agenda-scheduled-leaders)
  19488. (- 1 diff)))
  19489. head category tags timestr))))
  19490. (setq txt org-agenda-no-heading-message))
  19491. (when txt
  19492. (setq face (if pastschedp
  19493. 'org-scheduled-previously
  19494. 'org-scheduled-today))
  19495. (org-add-props txt props
  19496. 'undone-face face
  19497. 'face (if donep 'org-done face)
  19498. 'org-marker (org-agenda-new-marker pos)
  19499. 'org-hd-marker (org-agenda-new-marker pos1)
  19500. 'type (if pastschedp "past-scheduled" "scheduled")
  19501. 'date (if pastschedp d2 date)
  19502. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19503. 'org-category category)
  19504. (push txt ee))))))
  19505. (nreverse ee)))
  19506. (defun org-agenda-get-blocks ()
  19507. "Return the date-range information for agenda display."
  19508. (let* ((props (list 'face nil
  19509. 'org-not-done-regexp org-not-done-regexp
  19510. 'org-todo-regexp org-todo-regexp
  19511. 'mouse-face 'highlight
  19512. 'keymap org-agenda-keymap
  19513. 'help-echo
  19514. (format "mouse-2 or RET jump to org file %s"
  19515. (abbreviate-file-name buffer-file-name))))
  19516. (regexp org-tr-regexp)
  19517. (d0 (calendar-absolute-from-gregorian date))
  19518. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19519. donep head)
  19520. (goto-char (point-min))
  19521. (while (re-search-forward regexp nil t)
  19522. (catch :skip
  19523. (org-agenda-skip)
  19524. (setq pos (point))
  19525. (setq timestr (match-string 0)
  19526. s1 (match-string 1)
  19527. s2 (match-string 2)
  19528. d1 (time-to-days (org-time-string-to-time s1))
  19529. d2 (time-to-days (org-time-string-to-time s2)))
  19530. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19531. ;; Only allow days between the limits, because the normal
  19532. ;; date stamps will catch the limits.
  19533. (save-excursion
  19534. (setq marker (org-agenda-new-marker (point)))
  19535. (setq category (org-get-category))
  19536. (if (re-search-backward "^\\*+ " nil t)
  19537. (progn
  19538. (goto-char (match-beginning 0))
  19539. (setq hdmarker (org-agenda-new-marker (point)))
  19540. (setq tags (org-get-tags-at))
  19541. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19542. (setq head (match-string 1))
  19543. (and org-agenda-skip-timestamp-if-done
  19544. (org-entry-is-done-p)
  19545. (throw :skip t))
  19546. (setq txt (org-format-agenda-item
  19547. (format (if (= d1 d2) "" "(%d/%d): ")
  19548. (1+ (- d0 d1)) (1+ (- d2 d1)))
  19549. head category tags
  19550. (if (= d0 d1) timestr))))
  19551. (setq txt org-agenda-no-heading-message))
  19552. (org-add-props txt props
  19553. 'org-marker marker 'org-hd-marker hdmarker
  19554. 'type "block" 'date date
  19555. 'priority (org-get-priority txt) 'org-category category)
  19556. (push txt ee)))
  19557. (goto-char pos)))
  19558. ;; Sort the entries by expiration date.
  19559. (nreverse ee)))
  19560. ;;; Agenda presentation and sorting
  19561. (defconst org-plain-time-of-day-regexp
  19562. (concat
  19563. "\\(\\<[012]?[0-9]"
  19564. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19565. "\\(--?"
  19566. "\\(\\<[012]?[0-9]"
  19567. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19568. "\\)?")
  19569. "Regular expression to match a plain time or time range.
  19570. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19571. groups carry important information:
  19572. 0 the full match
  19573. 1 the first time, range or not
  19574. 8 the second time, if it is a range.")
  19575. (defconst org-plain-time-extension-regexp
  19576. (concat
  19577. "\\(\\<[012]?[0-9]"
  19578. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19579. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  19580. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  19581. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19582. groups carry important information:
  19583. 0 the full match
  19584. 7 hours of duration
  19585. 9 minutes of duration")
  19586. (defconst org-stamp-time-of-day-regexp
  19587. (concat
  19588. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  19589. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  19590. "\\(--?"
  19591. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  19592. "Regular expression to match a timestamp time or time range.
  19593. After a match, the following groups carry important information:
  19594. 0 the full match
  19595. 1 date plus weekday, for backreferencing to make sure both times on same day
  19596. 2 the first time, range or not
  19597. 4 the second time, if it is a range.")
  19598. (defvar org-prefix-has-time nil
  19599. "A flag, set by `org-compile-prefix-format'.
  19600. The flag is set if the currently compiled format contains a `%t'.")
  19601. (defvar org-prefix-has-tag nil
  19602. "A flag, set by `org-compile-prefix-format'.
  19603. The flag is set if the currently compiled format contains a `%T'.")
  19604. (defun org-format-agenda-item (extra txt &optional category tags dotime
  19605. noprefix remove-re)
  19606. "Format TXT to be inserted into the agenda buffer.
  19607. In particular, it adds the prefix and corresponding text properties. EXTRA
  19608. must be a string and replaces the `%s' specifier in the prefix format.
  19609. CATEGORY (string, symbol or nil) may be used to overrule the default
  19610. category taken from local variable or file name. It will replace the `%c'
  19611. specifier in the format. DOTIME, when non-nil, indicates that a
  19612. time-of-day should be extracted from TXT for sorting of this entry, and for
  19613. the `%t' specifier in the format. When DOTIME is a string, this string is
  19614. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  19615. only the correctly processes TXT should be returned - this is used by
  19616. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  19617. Any match of REMOVE-RE will be removed from TXT."
  19618. (save-match-data
  19619. ;; Diary entries sometimes have extra whitespace at the beginning
  19620. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  19621. (let* ((category (or category
  19622. org-category
  19623. (if buffer-file-name
  19624. (file-name-sans-extension
  19625. (file-name-nondirectory buffer-file-name))
  19626. "")))
  19627. (tag (if tags (nth (1- (length tags)) tags) ""))
  19628. time ; time and tag are needed for the eval of the prefix format
  19629. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  19630. (time-of-day (and dotime (org-get-time-of-day ts)))
  19631. stamp plain s0 s1 s2 rtn srp)
  19632. (when (and dotime time-of-day org-prefix-has-time)
  19633. ;; Extract starting and ending time and move them to prefix
  19634. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  19635. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  19636. (setq s0 (match-string 0 ts)
  19637. srp (and stamp (match-end 3))
  19638. s1 (match-string (if plain 1 2) ts)
  19639. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  19640. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  19641. ;; them, we might want to remove them there to avoid duplication.
  19642. ;; The user can turn this off with a variable.
  19643. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  19644. (string-match (concat (regexp-quote s0) " *") txt)
  19645. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  19646. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  19647. (= (match-beginning 0) 0)
  19648. t))
  19649. (setq txt (replace-match "" nil nil txt))))
  19650. ;; Normalize the time(s) to 24 hour
  19651. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  19652. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  19653. (when (and s1 (not s2) org-agenda-default-appointment-duration
  19654. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  19655. (let ((m (+ (string-to-number (match-string 2 s1))
  19656. (* 60 (string-to-number (match-string 1 s1)))
  19657. org-agenda-default-appointment-duration))
  19658. h)
  19659. (setq h (/ m 60) m (- m (* h 60)))
  19660. (setq s2 (format "%02d:%02d" h m))))
  19661. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  19662. txt)
  19663. ;; Tags are in the string
  19664. (if (or (eq org-agenda-remove-tags t)
  19665. (and org-agenda-remove-tags
  19666. org-prefix-has-tag))
  19667. (setq txt (replace-match "" t t txt))
  19668. (setq txt (replace-match
  19669. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  19670. (match-string 2 txt))
  19671. t t txt))))
  19672. (when remove-re
  19673. (while (string-match remove-re txt)
  19674. (setq txt (replace-match "" t t txt))))
  19675. ;; Create the final string
  19676. (if noprefix
  19677. (setq rtn txt)
  19678. ;; Prepare the variables needed in the eval of the compiled format
  19679. (setq time (cond (s2 (concat s1 "-" s2))
  19680. (s1 (concat s1 "......"))
  19681. (t ""))
  19682. extra (or extra "")
  19683. category (if (symbolp category) (symbol-name category) category))
  19684. ;; Evaluate the compiled format
  19685. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  19686. ;; And finally add the text properties
  19687. (org-add-props rtn nil
  19688. 'org-category (downcase category) 'tags tags
  19689. 'org-highest-priority org-highest-priority
  19690. 'org-lowest-priority org-lowest-priority
  19691. 'prefix-length (- (length rtn) (length txt))
  19692. 'time-of-day time-of-day
  19693. 'txt txt
  19694. 'time time
  19695. 'extra extra
  19696. 'dotime dotime))))
  19697. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  19698. (defvar org-agenda-sorting-strategy-selected nil)
  19699. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  19700. (catch 'exit
  19701. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  19702. ((and todayp (member 'today (car org-agenda-time-grid))))
  19703. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  19704. ((member 'weekly (car org-agenda-time-grid)))
  19705. (t (throw 'exit list)))
  19706. (let* ((have (delq nil (mapcar
  19707. (lambda (x) (get-text-property 1 'time-of-day x))
  19708. list)))
  19709. (string (nth 1 org-agenda-time-grid))
  19710. (gridtimes (nth 2 org-agenda-time-grid))
  19711. (req (car org-agenda-time-grid))
  19712. (remove (member 'remove-match req))
  19713. new time)
  19714. (if (and (member 'require-timed req) (not have))
  19715. ;; don't show empty grid
  19716. (throw 'exit list))
  19717. (while (setq time (pop gridtimes))
  19718. (unless (and remove (member time have))
  19719. (setq time (int-to-string time))
  19720. (push (org-format-agenda-item
  19721. nil string "" nil
  19722. (concat (substring time 0 -2) ":" (substring time -2)))
  19723. new)
  19724. (put-text-property
  19725. 1 (length (car new)) 'face 'org-time-grid (car new))))
  19726. (if (member 'time-up org-agenda-sorting-strategy-selected)
  19727. (append new list)
  19728. (append list new)))))
  19729. (defun org-compile-prefix-format (key)
  19730. "Compile the prefix format into a Lisp form that can be evaluated.
  19731. The resulting form is returned and stored in the variable
  19732. `org-prefix-format-compiled'."
  19733. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  19734. (let ((s (cond
  19735. ((stringp org-agenda-prefix-format)
  19736. org-agenda-prefix-format)
  19737. ((assq key org-agenda-prefix-format)
  19738. (cdr (assq key org-agenda-prefix-format)))
  19739. (t " %-12:c%?-12t% s")))
  19740. (start 0)
  19741. varform vars var e c f opt)
  19742. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  19743. s start)
  19744. (setq var (cdr (assoc (match-string 4 s)
  19745. '(("c" . category) ("t" . time) ("s" . extra)
  19746. ("T" . tag))))
  19747. c (or (match-string 3 s) "")
  19748. opt (match-beginning 1)
  19749. start (1+ (match-beginning 0)))
  19750. (if (equal var 'time) (setq org-prefix-has-time t))
  19751. (if (equal var 'tag) (setq org-prefix-has-tag t))
  19752. (setq f (concat "%" (match-string 2 s) "s"))
  19753. (if opt
  19754. (setq varform
  19755. `(if (equal "" ,var)
  19756. ""
  19757. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  19758. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  19759. (setq s (replace-match "%s" t nil s))
  19760. (push varform vars))
  19761. (setq vars (nreverse vars))
  19762. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  19763. (defun org-set-sorting-strategy (key)
  19764. (if (symbolp (car org-agenda-sorting-strategy))
  19765. ;; the old format
  19766. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  19767. (setq org-agenda-sorting-strategy-selected
  19768. (or (cdr (assq key org-agenda-sorting-strategy))
  19769. (cdr (assq 'agenda org-agenda-sorting-strategy))
  19770. '(time-up category-keep priority-down)))))
  19771. (defun org-get-time-of-day (s &optional string mod24)
  19772. "Check string S for a time of day.
  19773. If found, return it as a military time number between 0 and 2400.
  19774. If not found, return nil.
  19775. The optional STRING argument forces conversion into a 5 character wide string
  19776. HH:MM."
  19777. (save-match-data
  19778. (when
  19779. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  19780. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  19781. (let* ((h (string-to-number (match-string 1 s)))
  19782. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  19783. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  19784. (am-p (equal ampm "am"))
  19785. (h1 (cond ((not ampm) h)
  19786. ((= h 12) (if am-p 0 12))
  19787. (t (+ h (if am-p 0 12)))))
  19788. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  19789. (mod h1 24) h1))
  19790. (t0 (+ (* 100 h2) m))
  19791. (t1 (concat (if (>= h1 24) "+" " ")
  19792. (if (< t0 100) "0" "")
  19793. (if (< t0 10) "0" "")
  19794. (int-to-string t0))))
  19795. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  19796. (defun org-finalize-agenda-entries (list &optional nosort)
  19797. "Sort and concatenate the agenda items."
  19798. (setq list (mapcar 'org-agenda-highlight-todo list))
  19799. (if nosort
  19800. list
  19801. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  19802. (defun org-agenda-highlight-todo (x)
  19803. (let (re pl)
  19804. (if (eq x 'line)
  19805. (save-excursion
  19806. (beginning-of-line 1)
  19807. (setq re (get-text-property (point) 'org-todo-regexp))
  19808. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  19809. (and (looking-at (concat "[ \t]*\\.*" re))
  19810. (add-text-properties (match-beginning 0) (match-end 0)
  19811. (list 'face (org-get-todo-face 0)))))
  19812. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  19813. pl (get-text-property 0 'prefix-length x))
  19814. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  19815. (add-text-properties
  19816. (or (match-end 1) (match-end 0)) (match-end 0)
  19817. (list 'face (org-get-todo-face (match-string 2 x)))
  19818. x))
  19819. x)))
  19820. (defsubst org-cmp-priority (a b)
  19821. "Compare the priorities of string A and B."
  19822. (let ((pa (or (get-text-property 1 'priority a) 0))
  19823. (pb (or (get-text-property 1 'priority b) 0)))
  19824. (cond ((> pa pb) +1)
  19825. ((< pa pb) -1)
  19826. (t nil))))
  19827. (defsubst org-cmp-category (a b)
  19828. "Compare the string values of categories of strings A and B."
  19829. (let ((ca (or (get-text-property 1 'org-category a) ""))
  19830. (cb (or (get-text-property 1 'org-category b) "")))
  19831. (cond ((string-lessp ca cb) -1)
  19832. ((string-lessp cb ca) +1)
  19833. (t nil))))
  19834. (defsubst org-cmp-tag (a b)
  19835. "Compare the string values of categories of strings A and B."
  19836. (let ((ta (car (last (get-text-property 1 'tags a))))
  19837. (tb (car (last (get-text-property 1 'tags b)))))
  19838. (cond ((not ta) +1)
  19839. ((not tb) -1)
  19840. ((string-lessp ta tb) -1)
  19841. ((string-lessp tb ta) +1)
  19842. (t nil))))
  19843. (defsubst org-cmp-time (a b)
  19844. "Compare the time-of-day values of strings A and B."
  19845. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  19846. (ta (or (get-text-property 1 'time-of-day a) def))
  19847. (tb (or (get-text-property 1 'time-of-day b) def)))
  19848. (cond ((< ta tb) -1)
  19849. ((< tb ta) +1)
  19850. (t nil))))
  19851. (defun org-entries-lessp (a b)
  19852. "Predicate for sorting agenda entries."
  19853. ;; The following variables will be used when the form is evaluated.
  19854. ;; So even though the compiler complains, keep them.
  19855. (let* ((time-up (org-cmp-time a b))
  19856. (time-down (if time-up (- time-up) nil))
  19857. (priority-up (org-cmp-priority a b))
  19858. (priority-down (if priority-up (- priority-up) nil))
  19859. (category-up (org-cmp-category a b))
  19860. (category-down (if category-up (- category-up) nil))
  19861. (category-keep (if category-up +1 nil))
  19862. (tag-up (org-cmp-tag a b))
  19863. (tag-down (if tag-up (- tag-up) nil)))
  19864. (cdr (assoc
  19865. (eval (cons 'or org-agenda-sorting-strategy-selected))
  19866. '((-1 . t) (1 . nil) (nil . nil))))))
  19867. ;;; Agenda restriction lock
  19868. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  19869. "Overlay to mark the headline to which arenda commands are restricted.")
  19870. (org-overlay-put org-agenda-restriction-lock-overlay
  19871. 'face 'org-agenda-restriction-lock)
  19872. (org-overlay-put org-agenda-restriction-lock-overlay
  19873. 'help-echo "Agendas are currently limited to this subtree.")
  19874. (org-detach-overlay org-agenda-restriction-lock-overlay)
  19875. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  19876. "Overlay marking the agenda restriction line in speedbar.")
  19877. (org-overlay-put org-speedbar-restriction-lock-overlay
  19878. 'face 'org-agenda-restriction-lock)
  19879. (org-overlay-put org-speedbar-restriction-lock-overlay
  19880. 'help-echo "Agendas are currently limited to this item.")
  19881. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19882. (defun org-agenda-set-restriction-lock (&optional type)
  19883. "Set restriction lock for agenda, to current subtree or file.
  19884. Restriction will be the file if TYPE is `file', or if type is the
  19885. universal prefix '(4), or if the cursor is before the first headline
  19886. in the file. Otherwise, restriction will be to the current subtree."
  19887. (interactive "P")
  19888. (and (equal type '(4)) (setq type 'file))
  19889. (setq type (cond
  19890. (type type)
  19891. ((org-at-heading-p) 'subtree)
  19892. ((condition-case nil (org-back-to-heading t) (error nil))
  19893. 'subtree)
  19894. (t 'file)))
  19895. (if (eq type 'subtree)
  19896. (progn
  19897. (setq org-agenda-restrict t)
  19898. (setq org-agenda-overriding-restriction 'subtree)
  19899. (put 'org-agenda-files 'org-restrict
  19900. (list (buffer-file-name (buffer-base-buffer))))
  19901. (org-back-to-heading t)
  19902. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  19903. (move-marker org-agenda-restrict-begin (point))
  19904. (move-marker org-agenda-restrict-end
  19905. (save-excursion (org-end-of-subtree t)))
  19906. (message "Locking agenda restriction to subtree"))
  19907. (put 'org-agenda-files 'org-restrict
  19908. (list (buffer-file-name (buffer-base-buffer))))
  19909. (setq org-agenda-restrict nil)
  19910. (setq org-agenda-overriding-restriction 'file)
  19911. (move-marker org-agenda-restrict-begin nil)
  19912. (move-marker org-agenda-restrict-end nil)
  19913. (message "Locking agenda restriction to file"))
  19914. (setq current-prefix-arg nil)
  19915. (org-agenda-maybe-redo))
  19916. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  19917. "Remove the agenda restriction lock."
  19918. (interactive "P")
  19919. (org-detach-overlay org-agenda-restriction-lock-overlay)
  19920. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19921. (setq org-agenda-overriding-restriction nil)
  19922. (setq org-agenda-restrict nil)
  19923. (put 'org-agenda-files 'org-restrict nil)
  19924. (move-marker org-agenda-restrict-begin nil)
  19925. (move-marker org-agenda-restrict-end nil)
  19926. (setq current-prefix-arg nil)
  19927. (message "Agenda restriction lock removed")
  19928. (or noupdate (org-agenda-maybe-redo)))
  19929. (defun org-agenda-maybe-redo ()
  19930. "If there is any window showing the agenda view, update it."
  19931. (let ((w (get-buffer-window org-agenda-buffer-name t))
  19932. (w0 (selected-window)))
  19933. (when w
  19934. (select-window w)
  19935. (org-agenda-redo)
  19936. (select-window w0)
  19937. (if org-agenda-overriding-restriction
  19938. (message "Agenda view shifted to new %s restriction"
  19939. org-agenda-overriding-restriction)
  19940. (message "Agenda restriction lock removed")))))
  19941. ;;; Agenda commands
  19942. (defun org-agenda-check-type (error &rest types)
  19943. "Check if agenda buffer is of allowed type.
  19944. If ERROR is non-nil, throw an error, otherwise just return nil."
  19945. (if (memq org-agenda-type types)
  19946. t
  19947. (if error
  19948. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  19949. nil)))
  19950. (defun org-agenda-quit ()
  19951. "Exit agenda by removing the window or the buffer."
  19952. (interactive)
  19953. (let ((buf (current-buffer)))
  19954. (if (not (one-window-p)) (delete-window))
  19955. (kill-buffer buf)
  19956. (org-agenda-maybe-reset-markers 'force)
  19957. (org-columns-remove-overlays))
  19958. ;; Maybe restore the pre-agenda window configuration.
  19959. (and org-agenda-restore-windows-after-quit
  19960. (not (eq org-agenda-window-setup 'other-frame))
  19961. org-pre-agenda-window-conf
  19962. (set-window-configuration org-pre-agenda-window-conf)))
  19963. (defun org-agenda-exit ()
  19964. "Exit agenda by removing the window or the buffer.
  19965. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  19966. Org-mode buffers visited directly by the user will not be touched."
  19967. (interactive)
  19968. (org-release-buffers org-agenda-new-buffers)
  19969. (setq org-agenda-new-buffers nil)
  19970. (org-agenda-quit))
  19971. (defun org-agenda-execute (arg)
  19972. "Execute another agenda command, keeping same window.\\<global-map>
  19973. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  19974. (interactive "P")
  19975. (let ((org-agenda-window-setup 'current-window))
  19976. (org-agenda arg)))
  19977. (defun org-save-all-org-buffers ()
  19978. "Save all Org-mode buffers without user confirmation."
  19979. (interactive)
  19980. (message "Saving all Org-mode buffers...")
  19981. (save-some-buffers t 'org-mode-p)
  19982. (message "Saving all Org-mode buffers... done"))
  19983. (defun org-agenda-redo ()
  19984. "Rebuild Agenda.
  19985. When this is the global TODO list, a prefix argument will be interpreted."
  19986. (interactive)
  19987. (let* ((org-agenda-keep-modes t)
  19988. (line (org-current-line))
  19989. (window-line (- line (org-current-line (window-start))))
  19990. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  19991. (message "Rebuilding agenda buffer...")
  19992. (org-let lprops '(eval org-agenda-redo-command))
  19993. (setq org-agenda-undo-list nil
  19994. org-agenda-pending-undo-list nil)
  19995. (message "Rebuilding agenda buffer...done")
  19996. (goto-line line)
  19997. (recenter window-line)))
  19998. (defun org-agenda-goto-date (date)
  19999. "Jump to DATE in agenda."
  20000. (interactive (list (org-read-date)))
  20001. (org-agenda-list nil date))
  20002. (defun org-agenda-goto-today ()
  20003. "Go to today."
  20004. (interactive)
  20005. (org-agenda-check-type t 'timeline 'agenda)
  20006. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20007. (cond
  20008. (tdpos (goto-char tdpos))
  20009. ((eq org-agenda-type 'agenda)
  20010. (let* ((sd (time-to-days
  20011. (time-subtract (current-time)
  20012. (list 0 (* 3600 org-extend-today-until) 0))))
  20013. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20014. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20015. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20016. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20017. (org-agenda-redo)
  20018. (org-agenda-find-same-or-today-or-agenda)))
  20019. (t (error "Cannot find today")))))
  20020. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20021. (goto-char
  20022. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20023. (text-property-any (point-min) (point-max) 'org-today t)
  20024. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20025. (point-min))))
  20026. (defun org-agenda-later (arg)
  20027. "Go forward in time by thee current span.
  20028. With prefix ARG, go forward that many times the current span."
  20029. (interactive "p")
  20030. (org-agenda-check-type t 'agenda)
  20031. (let* ((span org-agenda-span)
  20032. (sd org-starting-day)
  20033. (greg (calendar-gregorian-from-absolute sd))
  20034. (cnt (get-text-property (point) 'org-day-cnt))
  20035. greg2 nd)
  20036. (cond
  20037. ((eq span 'day)
  20038. (setq sd (+ arg sd) nd 1))
  20039. ((eq span 'week)
  20040. (setq sd (+ (* 7 arg) sd) nd 7))
  20041. ((eq span 'month)
  20042. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20043. sd (calendar-absolute-from-gregorian greg2))
  20044. (setcar greg2 (1+ (car greg2)))
  20045. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20046. ((eq span 'year)
  20047. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20048. sd (calendar-absolute-from-gregorian greg2))
  20049. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20050. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20051. (let ((org-agenda-overriding-arguments
  20052. (list (car org-agenda-last-arguments) sd nd t)))
  20053. (org-agenda-redo)
  20054. (org-agenda-find-same-or-today-or-agenda cnt))))
  20055. (defun org-agenda-earlier (arg)
  20056. "Go backward in time by the current span.
  20057. With prefix ARG, go backward that many times the current span."
  20058. (interactive "p")
  20059. (org-agenda-later (- arg)))
  20060. (defun org-agenda-day-view ()
  20061. "Switch to daily view for agenda."
  20062. (interactive)
  20063. (setq org-agenda-ndays 1)
  20064. (org-agenda-change-time-span 'day))
  20065. (defun org-agenda-week-view ()
  20066. "Switch to daily view for agenda."
  20067. (interactive)
  20068. (setq org-agenda-ndays 7)
  20069. (org-agenda-change-time-span 'week))
  20070. (defun org-agenda-month-view ()
  20071. "Switch to daily view for agenda."
  20072. (interactive)
  20073. (org-agenda-change-time-span 'month))
  20074. (defun org-agenda-year-view ()
  20075. "Switch to daily view for agenda."
  20076. (interactive)
  20077. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20078. (org-agenda-change-time-span 'year)
  20079. (error "Abort")))
  20080. (defun org-agenda-change-time-span (span)
  20081. "Change the agenda view to SPAN.
  20082. SPAN may be `day', `week', `month', `year'."
  20083. (org-agenda-check-type t 'agenda)
  20084. (if (equal org-agenda-span span)
  20085. (error "Viewing span is already \"%s\"" span))
  20086. (let* ((sd (or (get-text-property (point) 'day)
  20087. org-starting-day))
  20088. (computed (org-agenda-compute-time-span sd span))
  20089. (org-agenda-overriding-arguments
  20090. (list (car org-agenda-last-arguments)
  20091. (car computed) (cdr computed) t)))
  20092. (org-agenda-redo)
  20093. (org-agenda-find-same-or-today-or-agenda))
  20094. (org-agenda-set-mode-name)
  20095. (message "Switched to %s view" span))
  20096. (defun org-agenda-compute-time-span (sd span)
  20097. "Compute starting date and number of days for agenda.
  20098. SPAN may be `day', `week', `month', `year'. The return value
  20099. is a cons cell with the starting date and the number of days,
  20100. so that the date SD will be in that range."
  20101. (let* ((greg (calendar-gregorian-from-absolute sd))
  20102. nd)
  20103. (cond
  20104. ((eq span 'day)
  20105. (setq nd 1))
  20106. ((eq span 'week)
  20107. (let* ((nt (calendar-day-of-week
  20108. (calendar-gregorian-from-absolute sd)))
  20109. (d (if org-agenda-start-on-weekday
  20110. (- nt org-agenda-start-on-weekday)
  20111. 0)))
  20112. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20113. (setq nd 7)))
  20114. ((eq span 'month)
  20115. (setq sd (calendar-absolute-from-gregorian
  20116. (list (car greg) 1 (nth 2 greg)))
  20117. nd (- (calendar-absolute-from-gregorian
  20118. (list (1+ (car greg)) 1 (nth 2 greg)))
  20119. sd)))
  20120. ((eq span 'year)
  20121. (setq sd (calendar-absolute-from-gregorian
  20122. (list 1 1 (nth 2 greg)))
  20123. nd (- (calendar-absolute-from-gregorian
  20124. (list 1 1 (1+ (nth 2 greg))))
  20125. sd))))
  20126. (cons sd nd)))
  20127. ;; FIXME: does not work if user makes date format that starts with a blank
  20128. (defun org-agenda-next-date-line (&optional arg)
  20129. "Jump to the next line indicating a date in agenda buffer."
  20130. (interactive "p")
  20131. (org-agenda-check-type t 'agenda 'timeline)
  20132. (beginning-of-line 1)
  20133. (if (looking-at "^\\S-") (forward-char 1))
  20134. (if (not (re-search-forward "^\\S-" nil t arg))
  20135. (progn
  20136. (backward-char 1)
  20137. (error "No next date after this line in this buffer")))
  20138. (goto-char (match-beginning 0)))
  20139. (defun org-agenda-previous-date-line (&optional arg)
  20140. "Jump to the previous line indicating a date in agenda buffer."
  20141. (interactive "p")
  20142. (org-agenda-check-type t 'agenda 'timeline)
  20143. (beginning-of-line 1)
  20144. (if (not (re-search-backward "^\\S-" nil t arg))
  20145. (error "No previous date before this line in this buffer")))
  20146. ;; Initialize the highlight
  20147. (defvar org-hl (org-make-overlay 1 1))
  20148. (org-overlay-put org-hl 'face 'highlight)
  20149. (defun org-highlight (begin end &optional buffer)
  20150. "Highlight a region with overlay."
  20151. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20152. org-hl begin end (or buffer (current-buffer))))
  20153. (defun org-unhighlight ()
  20154. "Detach overlay INDEX."
  20155. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20156. ;; FIXME this is currently not used.
  20157. (defun org-highlight-until-next-command (beg end &optional buffer)
  20158. (org-highlight beg end buffer)
  20159. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20160. (defun org-unhighlight-once ()
  20161. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20162. (org-unhighlight))
  20163. (defun org-agenda-follow-mode ()
  20164. "Toggle follow mode in an agenda buffer."
  20165. (interactive)
  20166. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20167. (org-agenda-set-mode-name)
  20168. (message "Follow mode is %s"
  20169. (if org-agenda-follow-mode "on" "off")))
  20170. (defun org-agenda-log-mode ()
  20171. "Toggle log mode in an agenda buffer."
  20172. (interactive)
  20173. (org-agenda-check-type t 'agenda 'timeline)
  20174. (setq org-agenda-show-log (not org-agenda-show-log))
  20175. (org-agenda-set-mode-name)
  20176. (org-agenda-redo)
  20177. (message "Log mode is %s"
  20178. (if org-agenda-show-log "on" "off")))
  20179. (defun org-agenda-toggle-diary ()
  20180. "Toggle diary inclusion in an agenda buffer."
  20181. (interactive)
  20182. (org-agenda-check-type t 'agenda)
  20183. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20184. (org-agenda-redo)
  20185. (org-agenda-set-mode-name)
  20186. (message "Diary inclusion turned %s"
  20187. (if org-agenda-include-diary "on" "off")))
  20188. (defun org-agenda-toggle-time-grid ()
  20189. "Toggle time grid in an agenda buffer."
  20190. (interactive)
  20191. (org-agenda-check-type t 'agenda)
  20192. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20193. (org-agenda-redo)
  20194. (org-agenda-set-mode-name)
  20195. (message "Time-grid turned %s"
  20196. (if org-agenda-use-time-grid "on" "off")))
  20197. (defun org-agenda-set-mode-name ()
  20198. "Set the mode name to indicate all the small mode settings."
  20199. (setq mode-name
  20200. (concat "Org-Agenda"
  20201. (if (equal org-agenda-ndays 1) " Day" "")
  20202. (if (equal org-agenda-ndays 7) " Week" "")
  20203. (if org-agenda-follow-mode " Follow" "")
  20204. (if org-agenda-include-diary " Diary" "")
  20205. (if org-agenda-use-time-grid " Grid" "")
  20206. (if org-agenda-show-log " Log" "")))
  20207. (force-mode-line-update))
  20208. (defun org-agenda-post-command-hook ()
  20209. (and (eolp) (not (bolp)) (backward-char 1))
  20210. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20211. (if (and org-agenda-follow-mode
  20212. (get-text-property (point) 'org-marker))
  20213. (org-agenda-show)))
  20214. (defun org-agenda-show-priority ()
  20215. "Show the priority of the current item.
  20216. This priority is composed of the main priority given with the [#A] cookies,
  20217. and by additional input from the age of a schedules or deadline entry."
  20218. (interactive)
  20219. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20220. (message "Priority is %d" (if pri pri -1000))))
  20221. (defun org-agenda-show-tags ()
  20222. "Show the tags applicable to the current item."
  20223. (interactive)
  20224. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20225. (if tags
  20226. (message "Tags are :%s:"
  20227. (org-no-properties (mapconcat 'identity tags ":")))
  20228. (message "No tags associated with this line"))))
  20229. (defun org-agenda-goto (&optional highlight)
  20230. "Go to the Org-mode file which contains the item at point."
  20231. (interactive)
  20232. (let* ((marker (or (get-text-property (point) 'org-marker)
  20233. (org-agenda-error)))
  20234. (buffer (marker-buffer marker))
  20235. (pos (marker-position marker)))
  20236. (switch-to-buffer-other-window buffer)
  20237. (widen)
  20238. (goto-char pos)
  20239. (when (org-mode-p)
  20240. (org-show-context 'agenda)
  20241. (save-excursion
  20242. (and (outline-next-heading)
  20243. (org-flag-heading nil)))) ; show the next heading
  20244. (run-hooks 'org-agenda-after-show-hook)
  20245. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20246. (defvar org-agenda-after-show-hook nil
  20247. "Normal hook run after an item has been shown from the agenda.
  20248. Point is in the buffer where the item originated.")
  20249. (defun org-agenda-kill ()
  20250. "Kill the entry or subtree belonging to the current agenda entry."
  20251. (interactive)
  20252. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20253. (let* ((marker (or (get-text-property (point) 'org-marker)
  20254. (org-agenda-error)))
  20255. (buffer (marker-buffer marker))
  20256. (pos (marker-position marker))
  20257. (type (get-text-property (point) 'type))
  20258. dbeg dend (n 0) conf)
  20259. (org-with-remote-undo buffer
  20260. (with-current-buffer buffer
  20261. (save-excursion
  20262. (goto-char pos)
  20263. (if (and (org-mode-p) (not (member type '("sexp"))))
  20264. (setq dbeg (progn (org-back-to-heading t) (point))
  20265. dend (org-end-of-subtree t t))
  20266. (setq dbeg (point-at-bol)
  20267. dend (min (point-max) (1+ (point-at-eol)))))
  20268. (goto-char dbeg)
  20269. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20270. (setq conf (or (eq t org-agenda-confirm-kill)
  20271. (and (numberp org-agenda-confirm-kill)
  20272. (> n org-agenda-confirm-kill))))
  20273. (and conf
  20274. (not (y-or-n-p
  20275. (format "Delete entry with %d lines in buffer \"%s\"? "
  20276. n (buffer-name buffer))))
  20277. (error "Abort"))
  20278. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20279. (with-current-buffer buffer (delete-region dbeg dend))
  20280. (message "Agenda item and source killed"))))
  20281. (defun org-agenda-archive ()
  20282. "Kill the entry or subtree belonging to the current agenda entry."
  20283. (interactive)
  20284. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20285. (let* ((marker (or (get-text-property (point) 'org-marker)
  20286. (org-agenda-error)))
  20287. (buffer (marker-buffer marker))
  20288. (pos (marker-position marker)))
  20289. (org-with-remote-undo buffer
  20290. (with-current-buffer buffer
  20291. (if (org-mode-p)
  20292. (save-excursion
  20293. (goto-char pos)
  20294. (org-remove-subtree-entries-from-agenda)
  20295. (org-back-to-heading t)
  20296. (org-archive-subtree))
  20297. (error "Archiving works only in Org-mode files"))))))
  20298. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20299. "Remove all lines in the agenda that correspond to a given subtree.
  20300. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20301. If this information is not given, the function uses the tree at point."
  20302. (let ((buf (or buf (current-buffer))) m p)
  20303. (save-excursion
  20304. (unless (and beg end)
  20305. (org-back-to-heading t)
  20306. (setq beg (point))
  20307. (org-end-of-subtree t)
  20308. (setq end (point)))
  20309. (set-buffer (get-buffer org-agenda-buffer-name))
  20310. (save-excursion
  20311. (goto-char (point-max))
  20312. (beginning-of-line 1)
  20313. (while (not (bobp))
  20314. (when (and (setq m (get-text-property (point) 'org-marker))
  20315. (equal buf (marker-buffer m))
  20316. (setq p (marker-position m))
  20317. (>= p beg)
  20318. (<= p end))
  20319. (let ((inhibit-read-only t))
  20320. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20321. (beginning-of-line 0))))))
  20322. (defun org-agenda-open-link ()
  20323. "Follow the link in the current line, if any."
  20324. (interactive)
  20325. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20326. (save-excursion
  20327. (save-restriction
  20328. (narrow-to-region (point-at-bol) (point-at-eol))
  20329. (org-open-at-point))))
  20330. (defun org-agenda-copy-local-variable (var)
  20331. "Get a variable from a referenced buffer and install it here."
  20332. (let ((m (get-text-property (point) 'org-marker)))
  20333. (when (and m (buffer-live-p (marker-buffer m)))
  20334. (org-set-local var (with-current-buffer (marker-buffer m)
  20335. (symbol-value var))))))
  20336. (defun org-agenda-switch-to (&optional delete-other-windows)
  20337. "Go to the Org-mode file which contains the item at point."
  20338. (interactive)
  20339. (let* ((marker (or (get-text-property (point) 'org-marker)
  20340. (org-agenda-error)))
  20341. (buffer (marker-buffer marker))
  20342. (pos (marker-position marker)))
  20343. (switch-to-buffer buffer)
  20344. (and delete-other-windows (delete-other-windows))
  20345. (widen)
  20346. (goto-char pos)
  20347. (when (org-mode-p)
  20348. (org-show-context 'agenda)
  20349. (save-excursion
  20350. (and (outline-next-heading)
  20351. (org-flag-heading nil)))))) ; show the next heading
  20352. (defun org-agenda-goto-mouse (ev)
  20353. "Go to the Org-mode file which contains the item at the mouse click."
  20354. (interactive "e")
  20355. (mouse-set-point ev)
  20356. (org-agenda-goto))
  20357. (defun org-agenda-show ()
  20358. "Display the Org-mode file which contains the item at point."
  20359. (interactive)
  20360. (let ((win (selected-window)))
  20361. (org-agenda-goto t)
  20362. (select-window win)))
  20363. (defun org-agenda-recenter (arg)
  20364. "Display the Org-mode file which contains the item at point and recenter."
  20365. (interactive "P")
  20366. (let ((win (selected-window)))
  20367. (org-agenda-goto t)
  20368. (recenter arg)
  20369. (select-window win)))
  20370. (defun org-agenda-show-mouse (ev)
  20371. "Display the Org-mode file which contains the item at the mouse click."
  20372. (interactive "e")
  20373. (mouse-set-point ev)
  20374. (org-agenda-show))
  20375. (defun org-agenda-check-no-diary ()
  20376. "Check if the entry is a diary link and abort if yes."
  20377. (if (get-text-property (point) 'org-agenda-diary-link)
  20378. (org-agenda-error)))
  20379. (defun org-agenda-error ()
  20380. (error "Command not allowed in this line"))
  20381. (defun org-agenda-tree-to-indirect-buffer ()
  20382. "Show the subtree corresponding to the current entry in an indirect buffer.
  20383. This calls the command `org-tree-to-indirect-buffer' from the original
  20384. Org-mode buffer.
  20385. With numerical prefix arg ARG, go up to this level and then take that tree.
  20386. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20387. dedicated frame)."
  20388. (interactive)
  20389. (org-agenda-check-no-diary)
  20390. (let* ((marker (or (get-text-property (point) 'org-marker)
  20391. (org-agenda-error)))
  20392. (buffer (marker-buffer marker))
  20393. (pos (marker-position marker)))
  20394. (with-current-buffer buffer
  20395. (save-excursion
  20396. (goto-char pos)
  20397. (call-interactively 'org-tree-to-indirect-buffer)))))
  20398. (defvar org-last-heading-marker (make-marker)
  20399. "Marker pointing to the headline that last changed its TODO state
  20400. by a remote command from the agenda.")
  20401. (defun org-agenda-todo-nextset ()
  20402. "Switch TODO entry to next sequence."
  20403. (interactive)
  20404. (org-agenda-todo 'nextset))
  20405. (defun org-agenda-todo-previousset ()
  20406. "Switch TODO entry to previous sequence."
  20407. (interactive)
  20408. (org-agenda-todo 'previousset))
  20409. (defun org-agenda-todo (&optional arg)
  20410. "Cycle TODO state of line at point, also in Org-mode file.
  20411. This changes the line at point, all other lines in the agenda referring to
  20412. the same tree node, and the headline of the tree node in the Org-mode file."
  20413. (interactive "P")
  20414. (org-agenda-check-no-diary)
  20415. (let* ((col (current-column))
  20416. (marker (or (get-text-property (point) 'org-marker)
  20417. (org-agenda-error)))
  20418. (buffer (marker-buffer marker))
  20419. (pos (marker-position marker))
  20420. (hdmarker (get-text-property (point) 'org-hd-marker))
  20421. (inhibit-read-only t)
  20422. newhead)
  20423. (org-with-remote-undo buffer
  20424. (with-current-buffer buffer
  20425. (widen)
  20426. (goto-char pos)
  20427. (org-show-context 'agenda)
  20428. (save-excursion
  20429. (and (outline-next-heading)
  20430. (org-flag-heading nil))) ; show the next heading
  20431. (org-todo arg)
  20432. (and (bolp) (forward-char 1))
  20433. (setq newhead (org-get-heading))
  20434. (save-excursion
  20435. (org-back-to-heading)
  20436. (move-marker org-last-heading-marker (point))))
  20437. (beginning-of-line 1)
  20438. (save-excursion
  20439. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20440. (move-to-column col))))
  20441. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20442. "Change all lines in the agenda buffer which match HDMARKER.
  20443. The new content of the line will be NEWHEAD (as modified by
  20444. `org-format-agenda-item'). HDMARKER is checked with
  20445. `equal' against all `org-hd-marker' text properties in the file.
  20446. If FIXFACE is non-nil, the face of each item is modified acording to
  20447. the new TODO state."
  20448. (let* ((inhibit-read-only t)
  20449. props m pl undone-face done-face finish new dotime cat tags)
  20450. (save-excursion
  20451. (goto-char (point-max))
  20452. (beginning-of-line 1)
  20453. (while (not finish)
  20454. (setq finish (bobp))
  20455. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20456. (equal m hdmarker))
  20457. (setq props (text-properties-at (point))
  20458. dotime (get-text-property (point) 'dotime)
  20459. cat (get-text-property (point) 'org-category)
  20460. tags (get-text-property (point) 'tags)
  20461. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20462. pl (get-text-property (point) 'prefix-length)
  20463. undone-face (get-text-property (point) 'undone-face)
  20464. done-face (get-text-property (point) 'done-face))
  20465. (move-to-column pl)
  20466. (cond
  20467. ((equal new "")
  20468. (beginning-of-line 1)
  20469. (and (looking-at ".*\n?") (replace-match "")))
  20470. ((looking-at ".*")
  20471. (replace-match new t t)
  20472. (beginning-of-line 1)
  20473. (add-text-properties (point-at-bol) (point-at-eol) props)
  20474. (when fixface
  20475. (add-text-properties
  20476. (point-at-bol) (point-at-eol)
  20477. (list 'face
  20478. (if org-last-todo-state-is-todo
  20479. undone-face done-face))))
  20480. (org-agenda-highlight-todo 'line)
  20481. (beginning-of-line 1))
  20482. (t (error "Line update did not work"))))
  20483. (beginning-of-line 0)))
  20484. (org-finalize-agenda)))
  20485. (defun org-agenda-align-tags (&optional line)
  20486. "Align all tags in agenda items to `org-agenda-tags-column'."
  20487. (let ((inhibit-read-only t) l c)
  20488. (save-excursion
  20489. (goto-char (if line (point-at-bol) (point-min)))
  20490. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20491. (if line (point-at-eol) nil) t)
  20492. (add-text-properties
  20493. (match-beginning 2) (match-end 2)
  20494. (list 'face (list 'org-tag (get-text-property
  20495. (match-beginning 2) 'face))))
  20496. (setq l (- (match-end 2) (match-beginning 2))
  20497. c (if (< org-agenda-tags-column 0)
  20498. (- (abs org-agenda-tags-column) l)
  20499. org-agenda-tags-column))
  20500. (delete-region (match-beginning 1) (match-end 1))
  20501. (goto-char (match-beginning 1))
  20502. (insert (org-add-props
  20503. (make-string (max 1 (- c (current-column))) ?\ )
  20504. (text-properties-at (point))))))))
  20505. (defun org-agenda-priority-up ()
  20506. "Increase the priority of line at point, also in Org-mode file."
  20507. (interactive)
  20508. (org-agenda-priority 'up))
  20509. (defun org-agenda-priority-down ()
  20510. "Decrease the priority of line at point, also in Org-mode file."
  20511. (interactive)
  20512. (org-agenda-priority 'down))
  20513. (defun org-agenda-priority (&optional force-direction)
  20514. "Set the priority of line at point, also in Org-mode file.
  20515. This changes the line at point, all other lines in the agenda referring to
  20516. the same tree node, and the headline of the tree node in the Org-mode file."
  20517. (interactive)
  20518. (org-agenda-check-no-diary)
  20519. (let* ((marker (or (get-text-property (point) 'org-marker)
  20520. (org-agenda-error)))
  20521. (hdmarker (get-text-property (point) 'org-hd-marker))
  20522. (buffer (marker-buffer hdmarker))
  20523. (pos (marker-position hdmarker))
  20524. (inhibit-read-only t)
  20525. newhead)
  20526. (org-with-remote-undo buffer
  20527. (with-current-buffer buffer
  20528. (widen)
  20529. (goto-char pos)
  20530. (org-show-context 'agenda)
  20531. (save-excursion
  20532. (and (outline-next-heading)
  20533. (org-flag-heading nil))) ; show the next heading
  20534. (funcall 'org-priority force-direction)
  20535. (end-of-line 1)
  20536. (setq newhead (org-get-heading)))
  20537. (org-agenda-change-all-lines newhead hdmarker)
  20538. (beginning-of-line 1))))
  20539. (defun org-get-tags-at (&optional pos)
  20540. "Get a list of all headline tags applicable at POS.
  20541. POS defaults to point. If tags are inherited, the list contains
  20542. the targets in the same sequence as the headlines appear, i.e.
  20543. the tags of the current headline come last."
  20544. (interactive)
  20545. (let (tags lastpos)
  20546. (save-excursion
  20547. (save-restriction
  20548. (widen)
  20549. (goto-char (or pos (point)))
  20550. (save-match-data
  20551. (org-back-to-heading t)
  20552. (condition-case nil
  20553. (while (not (equal lastpos (point)))
  20554. (setq lastpos (point))
  20555. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  20556. (setq tags (append (org-split-string
  20557. (org-match-string-no-properties 1) ":")
  20558. tags)))
  20559. (or org-use-tag-inheritance (error ""))
  20560. (org-up-heading-all 1))
  20561. (error nil))))
  20562. tags)))
  20563. ;; FIXME: should fix the tags property of the agenda line.
  20564. (defun org-agenda-set-tags ()
  20565. "Set tags for the current headline."
  20566. (interactive)
  20567. (org-agenda-check-no-diary)
  20568. (if (and (org-region-active-p) (interactive-p))
  20569. (call-interactively 'org-change-tag-in-region)
  20570. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20571. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20572. (org-agenda-error)))
  20573. (buffer (marker-buffer hdmarker))
  20574. (pos (marker-position hdmarker))
  20575. (inhibit-read-only t)
  20576. newhead)
  20577. (org-with-remote-undo buffer
  20578. (with-current-buffer buffer
  20579. (widen)
  20580. (goto-char pos)
  20581. (save-excursion
  20582. (org-show-context 'agenda))
  20583. (save-excursion
  20584. (and (outline-next-heading)
  20585. (org-flag-heading nil))) ; show the next heading
  20586. (goto-char pos)
  20587. (call-interactively 'org-set-tags)
  20588. (end-of-line 1)
  20589. (setq newhead (org-get-heading)))
  20590. (org-agenda-change-all-lines newhead hdmarker)
  20591. (beginning-of-line 1)))))
  20592. (defun org-agenda-toggle-archive-tag ()
  20593. "Toggle the archive tag for the current entry."
  20594. (interactive)
  20595. (org-agenda-check-no-diary)
  20596. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20597. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20598. (org-agenda-error)))
  20599. (buffer (marker-buffer hdmarker))
  20600. (pos (marker-position hdmarker))
  20601. (inhibit-read-only t)
  20602. newhead)
  20603. (org-with-remote-undo buffer
  20604. (with-current-buffer buffer
  20605. (widen)
  20606. (goto-char pos)
  20607. (org-show-context 'agenda)
  20608. (save-excursion
  20609. (and (outline-next-heading)
  20610. (org-flag-heading nil))) ; show the next heading
  20611. (call-interactively 'org-toggle-archive-tag)
  20612. (end-of-line 1)
  20613. (setq newhead (org-get-heading)))
  20614. (org-agenda-change-all-lines newhead hdmarker)
  20615. (beginning-of-line 1))))
  20616. (defun org-agenda-date-later (arg &optional what)
  20617. "Change the date of this item to one day later."
  20618. (interactive "p")
  20619. (org-agenda-check-type t 'agenda 'timeline)
  20620. (org-agenda-check-no-diary)
  20621. (let* ((marker (or (get-text-property (point) 'org-marker)
  20622. (org-agenda-error)))
  20623. (buffer (marker-buffer marker))
  20624. (pos (marker-position marker)))
  20625. (org-with-remote-undo buffer
  20626. (with-current-buffer buffer
  20627. (widen)
  20628. (goto-char pos)
  20629. (if (not (org-at-timestamp-p))
  20630. (error "Cannot find time stamp"))
  20631. (org-timestamp-change arg (or what 'day)))
  20632. (org-agenda-show-new-time marker org-last-changed-timestamp))
  20633. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  20634. (defun org-agenda-date-earlier (arg &optional what)
  20635. "Change the date of this item to one day earlier."
  20636. (interactive "p")
  20637. (org-agenda-date-later (- arg) what))
  20638. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  20639. "Show new date stamp via text properties."
  20640. ;; We use text properties to make this undoable
  20641. (let ((inhibit-read-only t))
  20642. (setq stamp (concat " " prefix " => " stamp))
  20643. (save-excursion
  20644. (goto-char (point-max))
  20645. (while (not (bobp))
  20646. (when (equal marker (get-text-property (point) 'org-marker))
  20647. (move-to-column (- (window-width) (length stamp)) t)
  20648. (if (featurep 'xemacs)
  20649. ;; Use `duplicable' property to trigger undo recording
  20650. (let ((ex (make-extent nil nil))
  20651. (gl (make-glyph stamp)))
  20652. (set-glyph-face gl 'secondary-selection)
  20653. (set-extent-properties
  20654. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  20655. (insert-extent ex (1- (point)) (point-at-eol)))
  20656. (add-text-properties
  20657. (1- (point)) (point-at-eol)
  20658. (list 'display (org-add-props stamp nil
  20659. 'face 'secondary-selection))))
  20660. (beginning-of-line 1))
  20661. (beginning-of-line 0)))))
  20662. (defun org-agenda-date-prompt (arg)
  20663. "Change the date of this item. Date is prompted for, with default today.
  20664. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  20665. be used to request time specification in the time stamp."
  20666. (interactive "P")
  20667. (org-agenda-check-type t 'agenda 'timeline)
  20668. (org-agenda-check-no-diary)
  20669. (let* ((marker (or (get-text-property (point) 'org-marker)
  20670. (org-agenda-error)))
  20671. (buffer (marker-buffer marker))
  20672. (pos (marker-position marker)))
  20673. (org-with-remote-undo buffer
  20674. (with-current-buffer buffer
  20675. (widen)
  20676. (goto-char pos)
  20677. (if (not (org-at-timestamp-p))
  20678. (error "Cannot find time stamp"))
  20679. (org-time-stamp arg)
  20680. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  20681. (defun org-agenda-schedule (arg)
  20682. "Schedule the item at point."
  20683. (interactive "P")
  20684. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20685. (org-agenda-check-no-diary)
  20686. (let* ((marker (or (get-text-property (point) 'org-marker)
  20687. (org-agenda-error)))
  20688. (buffer (marker-buffer marker))
  20689. (pos (marker-position marker))
  20690. (org-insert-labeled-timestamps-at-point nil)
  20691. ts)
  20692. (org-with-remote-undo buffer
  20693. (with-current-buffer buffer
  20694. (widen)
  20695. (goto-char pos)
  20696. (setq ts (org-schedule arg)))
  20697. (org-agenda-show-new-time marker ts "S"))
  20698. (message "Item scheduled for %s" ts)))
  20699. (defun org-agenda-deadline (arg)
  20700. "Schedule the item at point."
  20701. (interactive "P")
  20702. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20703. (org-agenda-check-no-diary)
  20704. (let* ((marker (or (get-text-property (point) 'org-marker)
  20705. (org-agenda-error)))
  20706. (buffer (marker-buffer marker))
  20707. (pos (marker-position marker))
  20708. (org-insert-labeled-timestamps-at-point nil)
  20709. ts)
  20710. (org-with-remote-undo buffer
  20711. (with-current-buffer buffer
  20712. (widen)
  20713. (goto-char pos)
  20714. (setq ts (org-deadline arg)))
  20715. (org-agenda-show-new-time marker ts "S"))
  20716. (message "Deadline for this item set to %s" ts)))
  20717. (defun org-get-heading (&optional no-tags)
  20718. "Return the heading of the current entry, without the stars."
  20719. (save-excursion
  20720. (org-back-to-heading t)
  20721. (if (looking-at
  20722. (if no-tags
  20723. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  20724. "\\*+[ \t]+\\([^\r\n]*\\)"))
  20725. (match-string 1) "")))
  20726. (defun org-agenda-clock-in (&optional arg)
  20727. "Start the clock on the currently selected item."
  20728. (interactive "P")
  20729. (org-agenda-check-no-diary)
  20730. (let* ((marker (or (get-text-property (point) 'org-marker)
  20731. (org-agenda-error)))
  20732. (pos (marker-position marker)))
  20733. (org-with-remote-undo (marker-buffer marker)
  20734. (with-current-buffer (marker-buffer marker)
  20735. (widen)
  20736. (goto-char pos)
  20737. (org-clock-in)))))
  20738. (defun org-agenda-clock-out (&optional arg)
  20739. "Stop the currently running clock."
  20740. (interactive "P")
  20741. (unless (marker-buffer org-clock-marker)
  20742. (error "No running clock"))
  20743. (org-with-remote-undo (marker-buffer org-clock-marker)
  20744. (org-clock-out)))
  20745. (defun org-agenda-clock-cancel (&optional arg)
  20746. "Cancel the currently running clock."
  20747. (interactive "P")
  20748. (unless (marker-buffer org-clock-marker)
  20749. (error "No running clock"))
  20750. (org-with-remote-undo (marker-buffer org-clock-marker)
  20751. (org-clock-cancel)))
  20752. (defun org-agenda-diary-entry ()
  20753. "Make a diary entry, like the `i' command from the calendar.
  20754. All the standard commands work: block, weekly etc."
  20755. (interactive)
  20756. (org-agenda-check-type t 'agenda 'timeline)
  20757. (require 'diary-lib)
  20758. (let* ((char (progn
  20759. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  20760. (read-char-exclusive)))
  20761. (cmd (cdr (assoc char
  20762. '((?d . insert-diary-entry)
  20763. (?w . insert-weekly-diary-entry)
  20764. (?m . insert-monthly-diary-entry)
  20765. (?y . insert-yearly-diary-entry)
  20766. (?a . insert-anniversary-diary-entry)
  20767. (?b . insert-block-diary-entry)
  20768. (?c . insert-cyclic-diary-entry)))))
  20769. (oldf (symbol-function 'calendar-cursor-to-date))
  20770. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  20771. (point (point))
  20772. (mark (or (mark t) (point))))
  20773. (unless cmd
  20774. (error "No command associated with <%c>" char))
  20775. (unless (and (get-text-property point 'day)
  20776. (or (not (equal ?b char))
  20777. (get-text-property mark 'day)))
  20778. (error "Don't know which date to use for diary entry"))
  20779. ;; We implement this by hacking the `calendar-cursor-to-date' function
  20780. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  20781. (let ((calendar-mark-ring
  20782. (list (calendar-gregorian-from-absolute
  20783. (or (get-text-property mark 'day)
  20784. (get-text-property point 'day))))))
  20785. (unwind-protect
  20786. (progn
  20787. (fset 'calendar-cursor-to-date
  20788. (lambda (&optional error)
  20789. (calendar-gregorian-from-absolute
  20790. (get-text-property point 'day))))
  20791. (call-interactively cmd))
  20792. (fset 'calendar-cursor-to-date oldf)))))
  20793. (defun org-agenda-execute-calendar-command (cmd)
  20794. "Execute a calendar command from the agenda, with the date associated to
  20795. the cursor position."
  20796. (org-agenda-check-type t 'agenda 'timeline)
  20797. (require 'diary-lib)
  20798. (unless (get-text-property (point) 'day)
  20799. (error "Don't know which date to use for calendar command"))
  20800. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  20801. (point (point))
  20802. (date (calendar-gregorian-from-absolute
  20803. (get-text-property point 'day)))
  20804. ;; the following 3 vars are needed in the calendar
  20805. (displayed-day (extract-calendar-day date))
  20806. (displayed-month (extract-calendar-month date))
  20807. (displayed-year (extract-calendar-year date)))
  20808. (unwind-protect
  20809. (progn
  20810. (fset 'calendar-cursor-to-date
  20811. (lambda (&optional error)
  20812. (calendar-gregorian-from-absolute
  20813. (get-text-property point 'day))))
  20814. (call-interactively cmd))
  20815. (fset 'calendar-cursor-to-date oldf))))
  20816. (defun org-agenda-phases-of-moon ()
  20817. "Display the phases of the moon for the 3 months around the cursor date."
  20818. (interactive)
  20819. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  20820. (defun org-agenda-holidays ()
  20821. "Display the holidays for the 3 months around the cursor date."
  20822. (interactive)
  20823. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  20824. (defun org-agenda-sunrise-sunset (arg)
  20825. "Display sunrise and sunset for the cursor date.
  20826. Latitude and longitude can be specified with the variables
  20827. `calendar-latitude' and `calendar-longitude'. When called with prefix
  20828. argument, latitude and longitude will be prompted for."
  20829. (interactive "P")
  20830. (let ((calendar-longitude (if arg nil calendar-longitude))
  20831. (calendar-latitude (if arg nil calendar-latitude))
  20832. (calendar-location-name
  20833. (if arg "the given coordinates" calendar-location-name)))
  20834. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  20835. (defun org-agenda-goto-calendar ()
  20836. "Open the Emacs calendar with the date at the cursor."
  20837. (interactive)
  20838. (org-agenda-check-type t 'agenda 'timeline)
  20839. (let* ((day (or (get-text-property (point) 'day)
  20840. (error "Don't know which date to open in calendar")))
  20841. (date (calendar-gregorian-from-absolute day))
  20842. (calendar-move-hook nil)
  20843. (view-calendar-holidays-initially nil)
  20844. (view-diary-entries-initially nil))
  20845. (calendar)
  20846. (calendar-goto-date date)))
  20847. (defun org-calendar-goto-agenda ()
  20848. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  20849. This is a command that has to be installed in `calendar-mode-map'."
  20850. (interactive)
  20851. (org-agenda-list nil (calendar-absolute-from-gregorian
  20852. (calendar-cursor-to-date))
  20853. nil))
  20854. (defun org-agenda-convert-date ()
  20855. (interactive)
  20856. (org-agenda-check-type t 'agenda 'timeline)
  20857. (let ((day (get-text-property (point) 'day))
  20858. date s)
  20859. (unless day
  20860. (error "Don't know which date to convert"))
  20861. (setq date (calendar-gregorian-from-absolute day))
  20862. (setq s (concat
  20863. "Gregorian: " (calendar-date-string date) "\n"
  20864. "ISO: " (calendar-iso-date-string date) "\n"
  20865. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  20866. "Julian: " (calendar-julian-date-string date) "\n"
  20867. "Astron. JD: " (calendar-astro-date-string date)
  20868. " (Julian date number at noon UTC)\n"
  20869. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  20870. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  20871. "French: " (calendar-french-date-string date) "\n"
  20872. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  20873. "Mayan: " (calendar-mayan-date-string date) "\n"
  20874. "Coptic: " (calendar-coptic-date-string date) "\n"
  20875. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  20876. "Persian: " (calendar-persian-date-string date) "\n"
  20877. "Chinese: " (calendar-chinese-date-string date) "\n"))
  20878. (with-output-to-temp-buffer "*Dates*"
  20879. (princ s))
  20880. (if (fboundp 'fit-window-to-buffer)
  20881. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  20882. ;;;; Embedded LaTeX
  20883. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  20884. "Keymap for the minor `org-cdlatex-mode'.")
  20885. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  20886. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  20887. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  20888. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  20889. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  20890. (defvar org-cdlatex-texmathp-advice-is-done nil
  20891. "Flag remembering if we have applied the advice to texmathp already.")
  20892. (define-minor-mode org-cdlatex-mode
  20893. "Toggle the minor `org-cdlatex-mode'.
  20894. This mode supports entering LaTeX environment and math in LaTeX fragments
  20895. in Org-mode.
  20896. \\{org-cdlatex-mode-map}"
  20897. nil " OCDL" nil
  20898. (when org-cdlatex-mode (require 'cdlatex))
  20899. (unless org-cdlatex-texmathp-advice-is-done
  20900. (setq org-cdlatex-texmathp-advice-is-done t)
  20901. (defadvice texmathp (around org-math-always-on activate)
  20902. "Always return t in org-mode buffers.
  20903. This is because we want to insert math symbols without dollars even outside
  20904. the LaTeX math segments. If Orgmode thinks that point is actually inside
  20905. en embedded LaTeX fragement, let texmathp do its job.
  20906. \\[org-cdlatex-mode-map]"
  20907. (interactive)
  20908. (let (p)
  20909. (cond
  20910. ((not (org-mode-p)) ad-do-it)
  20911. ((eq this-command 'cdlatex-math-symbol)
  20912. (setq ad-return-value t
  20913. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  20914. (t
  20915. (let ((p (org-inside-LaTeX-fragment-p)))
  20916. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  20917. (setq ad-return-value t
  20918. texmathp-why '("Org-mode embedded math" . 0))
  20919. (if p ad-do-it)))))))))
  20920. (defun turn-on-org-cdlatex ()
  20921. "Unconditionally turn on `org-cdlatex-mode'."
  20922. (org-cdlatex-mode 1))
  20923. (defun org-inside-LaTeX-fragment-p ()
  20924. "Test if point is inside a LaTeX fragment.
  20925. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  20926. sequence appearing also before point.
  20927. Even though the matchers for math are configurable, this function assumes
  20928. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  20929. delimiters are skipped when they have been removed by customization.
  20930. The return value is nil, or a cons cell with the delimiter and
  20931. and the position of this delimiter.
  20932. This function does a reasonably good job, but can locally be fooled by
  20933. for example currency specifications. For example it will assume being in
  20934. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  20935. fragments that are properly closed, but during editing, we have to live
  20936. with the uncertainty caused by missing closing delimiters. This function
  20937. looks only before point, not after."
  20938. (catch 'exit
  20939. (let ((pos (point))
  20940. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  20941. (lim (progn
  20942. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  20943. (point)))
  20944. dd-on str (start 0) m re)
  20945. (goto-char pos)
  20946. (when dodollar
  20947. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  20948. re (nth 1 (assoc "$" org-latex-regexps)))
  20949. (while (string-match re str start)
  20950. (cond
  20951. ((= (match-end 0) (length str))
  20952. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  20953. ((= (match-end 0) (- (length str) 5))
  20954. (throw 'exit nil))
  20955. (t (setq start (match-end 0))))))
  20956. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  20957. (goto-char pos)
  20958. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  20959. (and (match-beginning 2) (throw 'exit nil))
  20960. ;; count $$
  20961. (while (re-search-backward "\\$\\$" lim t)
  20962. (setq dd-on (not dd-on)))
  20963. (goto-char pos)
  20964. (if dd-on (cons "$$" m))))))
  20965. (defun org-try-cdlatex-tab ()
  20966. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  20967. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  20968. - inside a LaTeX fragment, or
  20969. - after the first word in a line, where an abbreviation expansion could
  20970. insert a LaTeX environment."
  20971. (when org-cdlatex-mode
  20972. (cond
  20973. ((save-excursion
  20974. (skip-chars-backward "a-zA-Z0-9*")
  20975. (skip-chars-backward " \t")
  20976. (bolp))
  20977. (cdlatex-tab) t)
  20978. ((org-inside-LaTeX-fragment-p)
  20979. (cdlatex-tab) t)
  20980. (t nil))))
  20981. (defun org-cdlatex-underscore-caret (&optional arg)
  20982. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  20983. Revert to the normal definition outside of these fragments."
  20984. (interactive "P")
  20985. (if (org-inside-LaTeX-fragment-p)
  20986. (call-interactively 'cdlatex-sub-superscript)
  20987. (let (org-cdlatex-mode)
  20988. (call-interactively (key-binding (vector last-input-event))))))
  20989. (defun org-cdlatex-math-modify (&optional arg)
  20990. "Execute `cdlatex-math-modify' in LaTeX fragments.
  20991. Revert to the normal definition outside of these fragments."
  20992. (interactive "P")
  20993. (if (org-inside-LaTeX-fragment-p)
  20994. (call-interactively 'cdlatex-math-modify)
  20995. (let (org-cdlatex-mode)
  20996. (call-interactively (key-binding (vector last-input-event))))))
  20997. (defvar org-latex-fragment-image-overlays nil
  20998. "List of overlays carrying the images of latex fragments.")
  20999. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21000. (defun org-remove-latex-fragment-image-overlays ()
  21001. "Remove all overlays with LaTeX fragment images in current buffer."
  21002. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21003. (setq org-latex-fragment-image-overlays nil))
  21004. (defun org-preview-latex-fragment (&optional subtree)
  21005. "Preview the LaTeX fragment at point, or all locally or globally.
  21006. If the cursor is in a LaTeX fragment, create the image and overlay
  21007. it over the source code. If there is no fragment at point, display
  21008. all fragments in the current text, from one headline to the next. With
  21009. prefix SUBTREE, display all fragments in the current subtree. With a
  21010. double prefix `C-u C-u', or when the cursor is before the first headline,
  21011. display all fragments in the buffer.
  21012. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21013. (interactive "P")
  21014. (org-remove-latex-fragment-image-overlays)
  21015. (save-excursion
  21016. (save-restriction
  21017. (let (beg end at msg)
  21018. (cond
  21019. ((or (equal subtree '(16))
  21020. (not (save-excursion
  21021. (re-search-backward (concat "^" outline-regexp) nil t))))
  21022. (setq beg (point-min) end (point-max)
  21023. msg "Creating images for buffer...%s"))
  21024. ((equal subtree '(4))
  21025. (org-back-to-heading)
  21026. (setq beg (point) end (org-end-of-subtree t)
  21027. msg "Creating images for subtree...%s"))
  21028. (t
  21029. (if (setq at (org-inside-LaTeX-fragment-p))
  21030. (goto-char (max (point-min) (- (cdr at) 2)))
  21031. (org-back-to-heading))
  21032. (setq beg (point) end (progn (outline-next-heading) (point))
  21033. msg (if at "Creating image...%s"
  21034. "Creating images for entry...%s"))))
  21035. (message msg "")
  21036. (narrow-to-region beg end)
  21037. (goto-char beg)
  21038. (org-format-latex
  21039. (concat "ltxpng/" (file-name-sans-extension
  21040. (file-name-nondirectory
  21041. buffer-file-name)))
  21042. default-directory 'overlays msg at 'forbuffer)
  21043. (message msg "done. Use `C-c C-c' to remove images.")))))
  21044. (defvar org-latex-regexps
  21045. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21046. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21047. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21048. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21049. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21050. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21051. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21052. "Regular expressions for matching embedded LaTeX.")
  21053. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21054. "Replace LaTeX fragments with links to an image, and produce images."
  21055. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21056. (let* ((prefixnodir (file-name-nondirectory prefix))
  21057. (absprefix (expand-file-name prefix dir))
  21058. (todir (file-name-directory absprefix))
  21059. (opt org-format-latex-options)
  21060. (matchers (plist-get opt :matchers))
  21061. (re-list org-latex-regexps)
  21062. (cnt 0) txt link beg end re e checkdir
  21063. m n block linkfile movefile ov)
  21064. ;; Check if there are old images files with this prefix, and remove them
  21065. (when (file-directory-p todir)
  21066. (mapc 'delete-file
  21067. (directory-files
  21068. todir 'full
  21069. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21070. ;; Check the different regular expressions
  21071. (while (setq e (pop re-list))
  21072. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21073. block (if (nth 3 e) "\n\n" ""))
  21074. (when (member m matchers)
  21075. (goto-char (point-min))
  21076. (while (re-search-forward re nil t)
  21077. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21078. (setq txt (match-string n)
  21079. beg (match-beginning n) end (match-end n)
  21080. cnt (1+ cnt)
  21081. linkfile (format "%s_%04d.png" prefix cnt)
  21082. movefile (format "%s_%04d.png" absprefix cnt)
  21083. link (concat block "[[file:" linkfile "]]" block))
  21084. (if msg (message msg cnt))
  21085. (goto-char beg)
  21086. (unless checkdir ; make sure the directory exists
  21087. (setq checkdir t)
  21088. (or (file-directory-p todir) (make-directory todir)))
  21089. (org-create-formula-image
  21090. txt movefile opt forbuffer)
  21091. (if overlays
  21092. (progn
  21093. (setq ov (org-make-overlay beg end))
  21094. (if (featurep 'xemacs)
  21095. (progn
  21096. (org-overlay-put ov 'invisible t)
  21097. (org-overlay-put
  21098. ov 'end-glyph
  21099. (make-glyph (vector 'png :file movefile))))
  21100. (org-overlay-put
  21101. ov 'display
  21102. (list 'image :type 'png :file movefile :ascent 'center)))
  21103. (push ov org-latex-fragment-image-overlays)
  21104. (goto-char end))
  21105. (delete-region beg end)
  21106. (insert link))))))))
  21107. ;; This function borrows from Ganesh Swami's latex2png.el
  21108. (defun org-create-formula-image (string tofile options buffer)
  21109. (let* ((tmpdir (if (featurep 'xemacs)
  21110. (temp-directory)
  21111. temporary-file-directory))
  21112. (texfilebase (make-temp-name
  21113. (expand-file-name "orgtex" tmpdir)))
  21114. (texfile (concat texfilebase ".tex"))
  21115. (dvifile (concat texfilebase ".dvi"))
  21116. (pngfile (concat texfilebase ".png"))
  21117. (fnh (face-attribute 'default :height nil))
  21118. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21119. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21120. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21121. "Black"))
  21122. (bg (or (plist-get options (if buffer :background :html-background))
  21123. "Transparent")))
  21124. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21125. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21126. (with-temp-file texfile
  21127. (insert org-format-latex-header
  21128. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21129. (let ((dir default-directory))
  21130. (condition-case nil
  21131. (progn
  21132. (cd tmpdir)
  21133. (call-process "latex" nil nil nil texfile))
  21134. (error nil))
  21135. (cd dir))
  21136. (if (not (file-exists-p dvifile))
  21137. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21138. (call-process "dvipng" nil nil nil
  21139. "-E" "-fg" fg "-bg" bg
  21140. "-D" dpi
  21141. ;;"-x" scale "-y" scale
  21142. "-T" "tight"
  21143. "-o" pngfile
  21144. dvifile)
  21145. (if (not (file-exists-p pngfile))
  21146. (progn (message "Failed to create png file from %s" texfile) nil)
  21147. ;; Use the requested file name and clean up
  21148. (copy-file pngfile tofile 'replace)
  21149. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21150. (delete-file (concat texfilebase e)))
  21151. pngfile))))
  21152. (defun org-dvipng-color (attr)
  21153. "Return an rgb color specification for dvipng."
  21154. (apply 'format "rgb %s %s %s"
  21155. (mapcar 'org-normalize-color
  21156. (color-values (face-attribute 'default attr nil)))))
  21157. (defun org-normalize-color (value)
  21158. "Return string to be used as color value for an RGB component."
  21159. (format "%g" (/ value 65535.0)))
  21160. ;;;; Exporting
  21161. ;;; Variables, constants, and parameter plists
  21162. (defconst org-level-max 20)
  21163. (defvar org-export-html-preamble nil
  21164. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21165. (defvar org-export-html-postamble nil
  21166. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21167. (defvar org-export-html-auto-preamble t
  21168. "Should default preamble be inserted? Set by publishing functions.")
  21169. (defvar org-export-html-auto-postamble t
  21170. "Should default postamble be inserted? Set by publishing functions.")
  21171. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21172. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21173. (defconst org-export-plist-vars
  21174. '((:language . org-export-default-language)
  21175. (:customtime . org-display-custom-times)
  21176. (:headline-levels . org-export-headline-levels)
  21177. (:section-numbers . org-export-with-section-numbers)
  21178. (:table-of-contents . org-export-with-toc)
  21179. (:preserve-breaks . org-export-preserve-breaks)
  21180. (:archived-trees . org-export-with-archived-trees)
  21181. (:emphasize . org-export-with-emphasize)
  21182. (:sub-superscript . org-export-with-sub-superscripts)
  21183. (:special-strings . org-export-with-special-strings)
  21184. (:footnotes . org-export-with-footnotes)
  21185. (:drawers . org-export-with-drawers)
  21186. (:tags . org-export-with-tags)
  21187. (:TeX-macros . org-export-with-TeX-macros)
  21188. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21189. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21190. (:fixed-width . org-export-with-fixed-width)
  21191. (:timestamps . org-export-with-timestamps)
  21192. (:author-info . org-export-author-info)
  21193. (:time-stamp-file . org-export-time-stamp-file)
  21194. (:tables . org-export-with-tables)
  21195. (:table-auto-headline . org-export-highlight-first-table-line)
  21196. (:style . org-export-html-style)
  21197. (:agenda-style . org-agenda-export-html-style)
  21198. (:convert-org-links . org-export-html-link-org-files-as-html)
  21199. (:inline-images . org-export-html-inline-images)
  21200. (:html-extension . org-export-html-extension)
  21201. (:html-table-tag . org-export-html-table-tag)
  21202. (:expand-quoted-html . org-export-html-expand)
  21203. (:timestamp . org-export-html-with-timestamp)
  21204. (:publishing-directory . org-export-publishing-directory)
  21205. (:preamble . org-export-html-preamble)
  21206. (:postamble . org-export-html-postamble)
  21207. (:auto-preamble . org-export-html-auto-preamble)
  21208. (:auto-postamble . org-export-html-auto-postamble)
  21209. (:author . user-full-name)
  21210. (:email . user-mail-address)))
  21211. (defun org-default-export-plist ()
  21212. "Return the property list with default settings for the export variables."
  21213. (let ((l org-export-plist-vars) rtn e)
  21214. (while (setq e (pop l))
  21215. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21216. rtn))
  21217. (defun org-infile-export-plist ()
  21218. "Return the property list with file-local settings for export."
  21219. (save-excursion
  21220. (goto-char 0)
  21221. (let ((re (org-make-options-regexp
  21222. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21223. p key val text options)
  21224. (while (re-search-forward re nil t)
  21225. (setq key (org-match-string-no-properties 1)
  21226. val (org-match-string-no-properties 2))
  21227. (cond
  21228. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21229. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21230. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21231. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21232. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21233. ((string-equal key "TEXT")
  21234. (setq text (if text (concat text "\n" val) val)))
  21235. ((string-equal key "OPTIONS") (setq options val))))
  21236. (setq p (plist-put p :text text))
  21237. (when options
  21238. (let ((op '(("H" . :headline-levels)
  21239. ("num" . :section-numbers)
  21240. ("toc" . :table-of-contents)
  21241. ("\\n" . :preserve-breaks)
  21242. ("@" . :expand-quoted-html)
  21243. (":" . :fixed-width)
  21244. ("|" . :tables)
  21245. ("^" . :sub-superscript)
  21246. ("-" . :special-strings)
  21247. ("f" . :footnotes)
  21248. ("d" . :drawers)
  21249. ("tags" . :tags)
  21250. ("*" . :emphasize)
  21251. ("TeX" . :TeX-macros)
  21252. ("LaTeX" . :LaTeX-fragments)
  21253. ("skip" . :skip-before-1st-heading)
  21254. ("author" . :author-info)
  21255. ("timestamp" . :time-stamp-file)))
  21256. o)
  21257. (while (setq o (pop op))
  21258. (if (string-match (concat (regexp-quote (car o))
  21259. ":\\([^ \t\n\r;,.]*\\)")
  21260. options)
  21261. (setq p (plist-put p (cdr o)
  21262. (car (read-from-string
  21263. (match-string 1 options)))))))))
  21264. p)))
  21265. (defun org-export-directory (type plist)
  21266. (let* ((val (plist-get plist :publishing-directory))
  21267. (dir (if (listp val)
  21268. (or (cdr (assoc type val)) ".")
  21269. val)))
  21270. dir))
  21271. (defun org-skip-comments (lines)
  21272. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21273. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21274. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21275. (case-fold-search nil)
  21276. rtn line level)
  21277. (while (setq line (pop lines))
  21278. (cond
  21279. ((and (string-match re1 line)
  21280. (setq level (- (match-end 1) (match-beginning 1))))
  21281. ;; Beginning of a COMMENT subtree. Skip it.
  21282. (while (and (setq line (pop lines))
  21283. (or (not (string-match re2 line))
  21284. (> (- (match-end 1) (match-beginning 1)) level))))
  21285. (setq lines (cons line lines)))
  21286. ((string-match "^#" line)
  21287. ;; an ordinary comment line
  21288. )
  21289. ((and org-export-table-remove-special-lines
  21290. (string-match "^[ \t]*|" line)
  21291. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21292. (and (string-match "| *<[0-9]+> *|" line)
  21293. (not (string-match "| *[^ <|]" line)))))
  21294. ;; a special table line that should be removed
  21295. )
  21296. (t (setq rtn (cons line rtn)))))
  21297. (nreverse rtn)))
  21298. (defun org-export (&optional arg)
  21299. (interactive)
  21300. (let ((help "[t] insert the export option template
  21301. \[v] limit export to visible part of outline tree
  21302. \[a] export as ASCII
  21303. \[h] export as HTML
  21304. \[H] export as HTML to temporary buffer
  21305. \[R] export region as HTML
  21306. \[b] export as HTML and browse immediately
  21307. \[x] export as XOXO
  21308. \[l] export as LaTeX
  21309. \[L] export as LaTeX to temporary buffer
  21310. \[i] export current file as iCalendar file
  21311. \[I] export all agenda files as iCalendar files
  21312. \[c] export agenda files into combined iCalendar file
  21313. \[F] publish current file
  21314. \[P] publish current project
  21315. \[X] publish... (project will be prompted for)
  21316. \[A] publish all projects")
  21317. (cmds
  21318. '((?t . org-insert-export-options-template)
  21319. (?v . org-export-visible)
  21320. (?a . org-export-as-ascii)
  21321. (?h . org-export-as-html)
  21322. (?b . org-export-as-html-and-open)
  21323. (?H . org-export-as-html-to-buffer)
  21324. (?R . org-export-region-as-html)
  21325. (?x . org-export-as-xoxo)
  21326. (?l . org-export-as-latex)
  21327. (?L . org-export-as-latex-to-buffer)
  21328. (?i . org-export-icalendar-this-file)
  21329. (?I . org-export-icalendar-all-agenda-files)
  21330. (?c . org-export-icalendar-combine-agenda-files)
  21331. (?F . org-publish-current-file)
  21332. (?P . org-publish-current-project)
  21333. (?X . org-publish)
  21334. (?A . org-publish-all)))
  21335. r1 r2 ass)
  21336. (save-window-excursion
  21337. (delete-other-windows)
  21338. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21339. (princ help))
  21340. (message "Select command: ")
  21341. (setq r1 (read-char-exclusive)))
  21342. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21343. (if (setq ass (assq r2 cmds))
  21344. (call-interactively (cdr ass))
  21345. (error "No command associated with key %c" r1))))
  21346. (defconst org-html-entities
  21347. '(("nbsp")
  21348. ("iexcl")
  21349. ("cent")
  21350. ("pound")
  21351. ("curren")
  21352. ("yen")
  21353. ("brvbar")
  21354. ("vert" . "&#124;")
  21355. ("sect")
  21356. ("uml")
  21357. ("copy")
  21358. ("ordf")
  21359. ("laquo")
  21360. ("not")
  21361. ("shy")
  21362. ("reg")
  21363. ("macr")
  21364. ("deg")
  21365. ("plusmn")
  21366. ("sup2")
  21367. ("sup3")
  21368. ("acute")
  21369. ("micro")
  21370. ("para")
  21371. ("middot")
  21372. ("odot"."o")
  21373. ("star"."*")
  21374. ("cedil")
  21375. ("sup1")
  21376. ("ordm")
  21377. ("raquo")
  21378. ("frac14")
  21379. ("frac12")
  21380. ("frac34")
  21381. ("iquest")
  21382. ("Agrave")
  21383. ("Aacute")
  21384. ("Acirc")
  21385. ("Atilde")
  21386. ("Auml")
  21387. ("Aring") ("AA"."&Aring;")
  21388. ("AElig")
  21389. ("Ccedil")
  21390. ("Egrave")
  21391. ("Eacute")
  21392. ("Ecirc")
  21393. ("Euml")
  21394. ("Igrave")
  21395. ("Iacute")
  21396. ("Icirc")
  21397. ("Iuml")
  21398. ("ETH")
  21399. ("Ntilde")
  21400. ("Ograve")
  21401. ("Oacute")
  21402. ("Ocirc")
  21403. ("Otilde")
  21404. ("Ouml")
  21405. ("times")
  21406. ("Oslash")
  21407. ("Ugrave")
  21408. ("Uacute")
  21409. ("Ucirc")
  21410. ("Uuml")
  21411. ("Yacute")
  21412. ("THORN")
  21413. ("szlig")
  21414. ("agrave")
  21415. ("aacute")
  21416. ("acirc")
  21417. ("atilde")
  21418. ("auml")
  21419. ("aring")
  21420. ("aelig")
  21421. ("ccedil")
  21422. ("egrave")
  21423. ("eacute")
  21424. ("ecirc")
  21425. ("euml")
  21426. ("igrave")
  21427. ("iacute")
  21428. ("icirc")
  21429. ("iuml")
  21430. ("eth")
  21431. ("ntilde")
  21432. ("ograve")
  21433. ("oacute")
  21434. ("ocirc")
  21435. ("otilde")
  21436. ("ouml")
  21437. ("divide")
  21438. ("oslash")
  21439. ("ugrave")
  21440. ("uacute")
  21441. ("ucirc")
  21442. ("uuml")
  21443. ("yacute")
  21444. ("thorn")
  21445. ("yuml")
  21446. ("fnof")
  21447. ("Alpha")
  21448. ("Beta")
  21449. ("Gamma")
  21450. ("Delta")
  21451. ("Epsilon")
  21452. ("Zeta")
  21453. ("Eta")
  21454. ("Theta")
  21455. ("Iota")
  21456. ("Kappa")
  21457. ("Lambda")
  21458. ("Mu")
  21459. ("Nu")
  21460. ("Xi")
  21461. ("Omicron")
  21462. ("Pi")
  21463. ("Rho")
  21464. ("Sigma")
  21465. ("Tau")
  21466. ("Upsilon")
  21467. ("Phi")
  21468. ("Chi")
  21469. ("Psi")
  21470. ("Omega")
  21471. ("alpha")
  21472. ("beta")
  21473. ("gamma")
  21474. ("delta")
  21475. ("epsilon")
  21476. ("varepsilon"."&epsilon;")
  21477. ("zeta")
  21478. ("eta")
  21479. ("theta")
  21480. ("iota")
  21481. ("kappa")
  21482. ("lambda")
  21483. ("mu")
  21484. ("nu")
  21485. ("xi")
  21486. ("omicron")
  21487. ("pi")
  21488. ("rho")
  21489. ("sigmaf") ("varsigma"."&sigmaf;")
  21490. ("sigma")
  21491. ("tau")
  21492. ("upsilon")
  21493. ("phi")
  21494. ("chi")
  21495. ("psi")
  21496. ("omega")
  21497. ("thetasym") ("vartheta"."&thetasym;")
  21498. ("upsih")
  21499. ("piv")
  21500. ("bull") ("bullet"."&bull;")
  21501. ("hellip") ("dots"."&hellip;")
  21502. ("prime")
  21503. ("Prime")
  21504. ("oline")
  21505. ("frasl")
  21506. ("weierp")
  21507. ("image")
  21508. ("real")
  21509. ("trade")
  21510. ("alefsym")
  21511. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21512. ("uarr") ("uparrow"."&uarr;")
  21513. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21514. ("darr")("downarrow"."&darr;")
  21515. ("harr") ("leftrightarrow"."&harr;")
  21516. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21517. ("lArr") ("Leftarrow"."&lArr;")
  21518. ("uArr") ("Uparrow"."&uArr;")
  21519. ("rArr") ("Rightarrow"."&rArr;")
  21520. ("dArr") ("Downarrow"."&dArr;")
  21521. ("hArr") ("Leftrightarrow"."&hArr;")
  21522. ("forall")
  21523. ("part") ("partial"."&part;")
  21524. ("exist") ("exists"."&exist;")
  21525. ("empty") ("emptyset"."&empty;")
  21526. ("nabla")
  21527. ("isin") ("in"."&isin;")
  21528. ("notin")
  21529. ("ni")
  21530. ("prod")
  21531. ("sum")
  21532. ("minus")
  21533. ("lowast") ("ast"."&lowast;")
  21534. ("radic")
  21535. ("prop") ("proptp"."&prop;")
  21536. ("infin") ("infty"."&infin;")
  21537. ("ang") ("angle"."&ang;")
  21538. ("and") ("wedge"."&and;")
  21539. ("or") ("vee"."&or;")
  21540. ("cap")
  21541. ("cup")
  21542. ("int")
  21543. ("there4")
  21544. ("sim")
  21545. ("cong") ("simeq"."&cong;")
  21546. ("asymp")("approx"."&asymp;")
  21547. ("ne") ("neq"."&ne;")
  21548. ("equiv")
  21549. ("le")
  21550. ("ge")
  21551. ("sub") ("subset"."&sub;")
  21552. ("sup") ("supset"."&sup;")
  21553. ("nsub")
  21554. ("sube")
  21555. ("supe")
  21556. ("oplus")
  21557. ("otimes")
  21558. ("perp")
  21559. ("sdot") ("cdot"."&sdot;")
  21560. ("lceil")
  21561. ("rceil")
  21562. ("lfloor")
  21563. ("rfloor")
  21564. ("lang")
  21565. ("rang")
  21566. ("loz") ("Diamond"."&loz;")
  21567. ("spades") ("spadesuit"."&spades;")
  21568. ("clubs") ("clubsuit"."&clubs;")
  21569. ("hearts") ("diamondsuit"."&hearts;")
  21570. ("diams") ("diamondsuit"."&diams;")
  21571. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  21572. ("quot")
  21573. ("amp")
  21574. ("lt")
  21575. ("gt")
  21576. ("OElig")
  21577. ("oelig")
  21578. ("Scaron")
  21579. ("scaron")
  21580. ("Yuml")
  21581. ("circ")
  21582. ("tilde")
  21583. ("ensp")
  21584. ("emsp")
  21585. ("thinsp")
  21586. ("zwnj")
  21587. ("zwj")
  21588. ("lrm")
  21589. ("rlm")
  21590. ("ndash")
  21591. ("mdash")
  21592. ("lsquo")
  21593. ("rsquo")
  21594. ("sbquo")
  21595. ("ldquo")
  21596. ("rdquo")
  21597. ("bdquo")
  21598. ("dagger")
  21599. ("Dagger")
  21600. ("permil")
  21601. ("lsaquo")
  21602. ("rsaquo")
  21603. ("euro")
  21604. ("arccos"."arccos")
  21605. ("arcsin"."arcsin")
  21606. ("arctan"."arctan")
  21607. ("arg"."arg")
  21608. ("cos"."cos")
  21609. ("cosh"."cosh")
  21610. ("cot"."cot")
  21611. ("coth"."coth")
  21612. ("csc"."csc")
  21613. ("deg"."deg")
  21614. ("det"."det")
  21615. ("dim"."dim")
  21616. ("exp"."exp")
  21617. ("gcd"."gcd")
  21618. ("hom"."hom")
  21619. ("inf"."inf")
  21620. ("ker"."ker")
  21621. ("lg"."lg")
  21622. ("lim"."lim")
  21623. ("liminf"."liminf")
  21624. ("limsup"."limsup")
  21625. ("ln"."ln")
  21626. ("log"."log")
  21627. ("max"."max")
  21628. ("min"."min")
  21629. ("Pr"."Pr")
  21630. ("sec"."sec")
  21631. ("sin"."sin")
  21632. ("sinh"."sinh")
  21633. ("sup"."sup")
  21634. ("tan"."tan")
  21635. ("tanh"."tanh")
  21636. )
  21637. "Entities for TeX->HTML translation.
  21638. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  21639. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  21640. In that case, \"\\ent\" will be translated to \"&other;\".
  21641. The list contains HTML entities for Latin-1, Greek and other symbols.
  21642. It is supplemented by a number of commonly used TeX macros with appropriate
  21643. translations. There is currently no way for users to extend this.")
  21644. ;;; General functions for all backends
  21645. (defun org-cleaned-string-for-export (string &rest parameters)
  21646. "Cleanup a buffer STRING so that links can be created safely."
  21647. (interactive)
  21648. (let* ((re-radio (and org-target-link-regexp
  21649. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  21650. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  21651. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  21652. (re-archive (concat ":" org-archive-tag ":"))
  21653. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  21654. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  21655. (htmlp (plist-get parameters :for-html))
  21656. (asciip (plist-get parameters :for-ascii))
  21657. (latexp (plist-get parameters :for-LaTeX))
  21658. (commentsp (plist-get parameters :comments))
  21659. (archived-trees (plist-get parameters :archived-trees))
  21660. (inhibit-read-only t)
  21661. (drawers org-drawers)
  21662. (exp-drawers (plist-get parameters :drawers))
  21663. (outline-regexp "\\*+ ")
  21664. a b xx
  21665. rtn p)
  21666. (with-current-buffer (get-buffer-create " org-mode-tmp")
  21667. (erase-buffer)
  21668. (insert string)
  21669. ;; Remove license-to-kill stuff
  21670. (while (setq p (text-property-any (point-min) (point-max)
  21671. :org-license-to-kill t))
  21672. (delete-region p (next-single-property-change p :org-license-to-kill)))
  21673. (let ((org-inhibit-startup t)) (org-mode))
  21674. (untabify (point-min) (point-max))
  21675. ;; Get the correct stuff before the first headline
  21676. (when (plist-get parameters :skip-before-1st-heading)
  21677. (goto-char (point-min))
  21678. (when (re-search-forward "^\\*+[ \t]" nil t)
  21679. (delete-region (point-min) (match-beginning 0))
  21680. (goto-char (point-min))
  21681. (insert "\n")))
  21682. (when (plist-get parameters :add-text)
  21683. (goto-char (point-min))
  21684. (insert (plist-get parameters :add-text) "\n"))
  21685. ;; Get rid of archived trees
  21686. (when (not (eq archived-trees t))
  21687. (goto-char (point-min))
  21688. (while (re-search-forward re-archive nil t)
  21689. (if (not (org-on-heading-p t))
  21690. (org-end-of-subtree t)
  21691. (beginning-of-line 1)
  21692. (setq a (if archived-trees
  21693. (1+ (point-at-eol)) (point))
  21694. b (org-end-of-subtree t))
  21695. (if (> b a) (delete-region a b)))))
  21696. ;; Get rid of drawers
  21697. (unless (eq t exp-drawers)
  21698. (goto-char (point-min))
  21699. (let ((re (concat "^[ \t]*:\\("
  21700. (mapconcat
  21701. 'identity
  21702. (org-delete-all exp-drawers
  21703. (copy-sequence drawers))
  21704. "\\|")
  21705. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  21706. (while (re-search-forward re nil t)
  21707. (replace-match ""))))
  21708. ;; Find targets in comments and move them out of comments,
  21709. ;; but mark them as targets that should be invisible
  21710. (goto-char (point-min))
  21711. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  21712. (replace-match "\\1(INVISIBLE)"))
  21713. ;; Protect backend specific stuff, throw away the others.
  21714. (let ((formatters
  21715. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  21716. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  21717. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  21718. fmt)
  21719. (goto-char (point-min))
  21720. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  21721. (goto-char (match-end 0))
  21722. (while (not (looking-at "#\\+END_EXAMPLE"))
  21723. (insert ": ")
  21724. (beginning-of-line 2)))
  21725. (goto-char (point-min))
  21726. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  21727. (add-text-properties (match-beginning 0) (match-end 0)
  21728. '(org-protected t)))
  21729. (while formatters
  21730. (setq fmt (pop formatters))
  21731. (when (car fmt)
  21732. (goto-char (point-min))
  21733. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  21734. ":[ \t]*\\(.*\\)") nil t)
  21735. (replace-match "\\1" t)
  21736. (add-text-properties
  21737. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  21738. '(org-protected t))))
  21739. (goto-char (point-min))
  21740. (while (re-search-forward
  21741. (concat "^#\\+"
  21742. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  21743. (cadddr fmt) "\\>.*\n?") nil t)
  21744. (if (car fmt)
  21745. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  21746. '(org-protected t))
  21747. (delete-region (match-beginning 0) (match-end 0))))))
  21748. ;; Protect quoted subtrees
  21749. (goto-char (point-min))
  21750. (while (re-search-forward re-quote nil t)
  21751. (goto-char (match-beginning 0))
  21752. (end-of-line 1)
  21753. (add-text-properties (point) (org-end-of-subtree t)
  21754. '(org-protected t)))
  21755. ;; Protect verbatim elements
  21756. (goto-char (point-min))
  21757. (while (re-search-forward org-verbatim-re nil t)
  21758. (add-text-properties (match-beginning 4) (match-end 4)
  21759. '(org-protected t))
  21760. (goto-char (1+ (match-end 4))))
  21761. ;; Remove subtrees that are commented
  21762. (goto-char (point-min))
  21763. (while (re-search-forward re-commented nil t)
  21764. (goto-char (match-beginning 0))
  21765. (delete-region (point) (org-end-of-subtree t)))
  21766. ;; Remove special table lines
  21767. (when org-export-table-remove-special-lines
  21768. (goto-char (point-min))
  21769. (while (re-search-forward "^[ \t]*|" nil t)
  21770. (beginning-of-line 1)
  21771. (if (or (looking-at "[ \t]*| *[!_^] *|")
  21772. (and (looking-at ".*?| *<[0-9]+> *|")
  21773. (not (looking-at ".*?| *[^ <|]"))))
  21774. (delete-region (max (point-min) (1- (point-at-bol)))
  21775. (point-at-eol))
  21776. (end-of-line 1))))
  21777. ;; Specific LaTeX stuff
  21778. (when latexp
  21779. (require 'org-export-latex nil)
  21780. (org-export-latex-cleaned-string))
  21781. (when asciip
  21782. (org-export-ascii-clean-string))
  21783. ;; Specific HTML stuff
  21784. (when htmlp
  21785. ;; Convert LaTeX fragments to images
  21786. (when (plist-get parameters :LaTeX-fragments)
  21787. (org-format-latex
  21788. (concat "ltxpng/" (file-name-sans-extension
  21789. (file-name-nondirectory
  21790. org-current-export-file)))
  21791. org-current-export-dir nil "Creating LaTeX image %s"))
  21792. (message "Exporting..."))
  21793. ;; Remove or replace comments
  21794. (goto-char (point-min))
  21795. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  21796. (if commentsp
  21797. (progn (add-text-properties
  21798. (match-beginning 0) (match-end 0) '(org-protected t))
  21799. (replace-match (format commentsp (match-string 1)) t t))
  21800. (replace-match "")))
  21801. ;; Find matches for radio targets and turn them into internal links
  21802. (goto-char (point-min))
  21803. (when re-radio
  21804. (while (re-search-forward re-radio nil t)
  21805. (org-if-unprotected
  21806. (replace-match "\\1[[\\2]]"))))
  21807. ;; Find all links that contain a newline and put them into a single line
  21808. (goto-char (point-min))
  21809. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  21810. (org-if-unprotected
  21811. (replace-match "\\1 \\3")
  21812. (goto-char (match-beginning 0))))
  21813. ;; Normalize links: Convert angle and plain links into bracket links
  21814. ;; Expand link abbreviations
  21815. (goto-char (point-min))
  21816. (while (re-search-forward re-plain-link nil t)
  21817. (goto-char (1- (match-end 0)))
  21818. (org-if-unprotected
  21819. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  21820. ":" (match-string 3) "]]")))
  21821. ;; added 'org-link face to links
  21822. (put-text-property 0 (length s) 'face 'org-link s)
  21823. (replace-match s t t))))
  21824. (goto-char (point-min))
  21825. (while (re-search-forward re-angle-link nil t)
  21826. (goto-char (1- (match-end 0)))
  21827. (org-if-unprotected
  21828. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  21829. ":" (match-string 3) "]]")))
  21830. (put-text-property 0 (length s) 'face 'org-link s)
  21831. (replace-match s t t))))
  21832. (goto-char (point-min))
  21833. (while (re-search-forward org-bracket-link-regexp nil t)
  21834. (org-if-unprotected
  21835. (let* ((s (concat "[[" (setq xx (save-match-data
  21836. (org-link-expand-abbrev (match-string 1))))
  21837. "]"
  21838. (if (match-end 3)
  21839. (match-string 2)
  21840. (concat "[" xx "]"))
  21841. "]")))
  21842. (put-text-property 0 (length s) 'face 'org-link s)
  21843. (replace-match s t t))))
  21844. ;; Find multiline emphasis and put them into single line
  21845. (when (plist-get parameters :emph-multiline)
  21846. (goto-char (point-min))
  21847. (while (re-search-forward org-emph-re nil t)
  21848. (if (not (= (char-after (match-beginning 3))
  21849. (char-after (match-beginning 4))))
  21850. (org-if-unprotected
  21851. (subst-char-in-region (match-beginning 0) (match-end 0)
  21852. ?\n ?\ t)
  21853. (goto-char (1- (match-end 0))))
  21854. (goto-char (1+ (match-beginning 0))))))
  21855. (setq rtn (buffer-string)))
  21856. (kill-buffer " org-mode-tmp")
  21857. rtn))
  21858. (defun org-export-grab-title-from-buffer ()
  21859. "Get a title for the current document, from looking at the buffer."
  21860. (let ((inhibit-read-only t))
  21861. (save-excursion
  21862. (goto-char (point-min))
  21863. (let ((end (save-excursion (outline-next-heading) (point))))
  21864. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  21865. ;; Mark the line so that it will not be exported as normal text.
  21866. (org-unmodified
  21867. (add-text-properties (match-beginning 0) (match-end 0)
  21868. (list :org-license-to-kill t)))
  21869. ;; Return the title string
  21870. (org-trim (match-string 0)))))))
  21871. (defun org-export-get-title-from-subtree ()
  21872. "Return subtree title and exclude it from export."
  21873. (let (title (m (mark)))
  21874. (save-excursion
  21875. (goto-char (region-beginning))
  21876. (when (and (org-at-heading-p)
  21877. (>= (org-end-of-subtree t t) (region-end)))
  21878. ;; This is a subtree, we take the title from the first heading
  21879. (goto-char (region-beginning))
  21880. (looking-at org-todo-line-regexp)
  21881. (setq title (match-string 3))
  21882. (org-unmodified
  21883. (add-text-properties (point) (1+ (point-at-eol))
  21884. (list :org-license-to-kill t)))))
  21885. title))
  21886. (defun org-solidify-link-text (s &optional alist)
  21887. "Take link text and make a safe target out of it."
  21888. (save-match-data
  21889. (let* ((rtn
  21890. (mapconcat
  21891. 'identity
  21892. (org-split-string s "[ \t\r\n]+") "--"))
  21893. (a (assoc rtn alist)))
  21894. (or (cdr a) rtn))))
  21895. (defun org-get-min-level (lines)
  21896. "Get the minimum level in LINES."
  21897. (let ((re "^\\(\\*+\\) ") l min)
  21898. (catch 'exit
  21899. (while (setq l (pop lines))
  21900. (if (string-match re l)
  21901. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  21902. 1)))
  21903. ;; Variable holding the vector with section numbers
  21904. (defvar org-section-numbers (make-vector org-level-max 0))
  21905. (defun org-init-section-numbers ()
  21906. "Initialize the vector for the section numbers."
  21907. (let* ((level -1)
  21908. (numbers (nreverse (org-split-string "" "\\.")))
  21909. (depth (1- (length org-section-numbers)))
  21910. (i depth) number-string)
  21911. (while (>= i 0)
  21912. (if (> i level)
  21913. (aset org-section-numbers i 0)
  21914. (setq number-string (or (car numbers) "0"))
  21915. (if (string-match "\\`[A-Z]\\'" number-string)
  21916. (aset org-section-numbers i
  21917. (- (string-to-char number-string) ?A -1))
  21918. (aset org-section-numbers i (string-to-number number-string)))
  21919. (pop numbers))
  21920. (setq i (1- i)))))
  21921. (defun org-section-number (&optional level)
  21922. "Return a string with the current section number.
  21923. When LEVEL is non-nil, increase section numbers on that level."
  21924. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  21925. (when level
  21926. (when (> level -1)
  21927. (aset org-section-numbers
  21928. level (1+ (aref org-section-numbers level))))
  21929. (setq idx (1+ level))
  21930. (while (<= idx depth)
  21931. (if (not (= idx 1))
  21932. (aset org-section-numbers idx 0))
  21933. (setq idx (1+ idx))))
  21934. (setq idx 0)
  21935. (while (<= idx depth)
  21936. (setq n (aref org-section-numbers idx))
  21937. (setq string (concat string (if (not (string= string "")) "." "")
  21938. (int-to-string n)))
  21939. (setq idx (1+ idx)))
  21940. (save-match-data
  21941. (if (string-match "\\`\\([@0]\\.\\)+" string)
  21942. (setq string (replace-match "" t nil string)))
  21943. (if (string-match "\\(\\.0\\)+\\'" string)
  21944. (setq string (replace-match "" t nil string))))
  21945. string))
  21946. ;;; ASCII export
  21947. (defvar org-last-level nil) ; dynamically scoped variable
  21948. (defvar org-min-level nil) ; dynamically scoped variable
  21949. (defvar org-levels-open nil) ; dynamically scoped parameter
  21950. (defvar org-ascii-current-indentation nil) ; For communication
  21951. (defun org-export-as-ascii (arg)
  21952. "Export the outline as a pretty ASCII file.
  21953. If there is an active region, export only the region.
  21954. The prefix ARG specifies how many levels of the outline should become
  21955. underlined headlines. The default is 3."
  21956. (interactive "P")
  21957. (setq-default org-todo-line-regexp org-todo-line-regexp)
  21958. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  21959. (org-infile-export-plist)))
  21960. (region-p (org-region-active-p))
  21961. (subtree-p
  21962. (when region-p
  21963. (save-excursion
  21964. (goto-char (region-beginning))
  21965. (and (org-at-heading-p)
  21966. (>= (org-end-of-subtree t t) (region-end))))))
  21967. (custom-times org-display-custom-times)
  21968. (org-ascii-current-indentation '(0 . 0))
  21969. (level 0) line txt
  21970. (umax nil)
  21971. (umax-toc nil)
  21972. (case-fold-search nil)
  21973. (filename (concat (file-name-as-directory
  21974. (org-export-directory :ascii opt-plist))
  21975. (file-name-sans-extension
  21976. (or (and subtree-p
  21977. (org-entry-get (region-beginning)
  21978. "EXPORT_FILE_NAME" t))
  21979. (file-name-nondirectory buffer-file-name)))
  21980. ".txt"))
  21981. (filename (if (equal (file-truename filename)
  21982. (file-truename buffer-file-name))
  21983. (concat filename ".txt")
  21984. filename))
  21985. (buffer (find-file-noselect filename))
  21986. (org-levels-open (make-vector org-level-max nil))
  21987. (odd org-odd-levels-only)
  21988. (date (plist-get opt-plist :date))
  21989. (author (plist-get opt-plist :author))
  21990. (title (or (and subtree-p (org-export-get-title-from-subtree))
  21991. (plist-get opt-plist :title)
  21992. (and (not
  21993. (plist-get opt-plist :skip-before-1st-heading))
  21994. (org-export-grab-title-from-buffer))
  21995. (file-name-sans-extension
  21996. (file-name-nondirectory buffer-file-name))))
  21997. (email (plist-get opt-plist :email))
  21998. (language (plist-get opt-plist :language))
  21999. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22000. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22001. (todo nil)
  22002. (lang-words nil)
  22003. (region
  22004. (buffer-substring
  22005. (if (org-region-active-p) (region-beginning) (point-min))
  22006. (if (org-region-active-p) (region-end) (point-max))))
  22007. (lines (org-split-string
  22008. (org-cleaned-string-for-export
  22009. region
  22010. :for-ascii t
  22011. :skip-before-1st-heading
  22012. (plist-get opt-plist :skip-before-1st-heading)
  22013. :drawers (plist-get opt-plist :drawers)
  22014. :verbatim-multiline t
  22015. :archived-trees
  22016. (plist-get opt-plist :archived-trees)
  22017. :add-text (plist-get opt-plist :text))
  22018. "\n"))
  22019. thetoc have-headings first-heading-pos
  22020. table-open table-buffer)
  22021. (let ((inhibit-read-only t))
  22022. (org-unmodified
  22023. (remove-text-properties (point-min) (point-max)
  22024. '(:org-license-to-kill t))))
  22025. (setq org-min-level (org-get-min-level lines))
  22026. (setq org-last-level org-min-level)
  22027. (org-init-section-numbers)
  22028. (find-file-noselect filename)
  22029. (setq lang-words (or (assoc language org-export-language-setup)
  22030. (assoc "en" org-export-language-setup)))
  22031. (switch-to-buffer-other-window buffer)
  22032. (erase-buffer)
  22033. (fundamental-mode)
  22034. ;; create local variables for all options, to make sure all called
  22035. ;; functions get the correct information
  22036. (mapc (lambda (x)
  22037. (set (make-local-variable (cdr x))
  22038. (plist-get opt-plist (car x))))
  22039. org-export-plist-vars)
  22040. (org-set-local 'org-odd-levels-only odd)
  22041. (setq umax (if arg (prefix-numeric-value arg)
  22042. org-export-headline-levels))
  22043. (setq umax-toc (if (integerp org-export-with-toc)
  22044. (min org-export-with-toc umax)
  22045. umax))
  22046. ;; File header
  22047. (if title (org-insert-centered title ?=))
  22048. (insert "\n")
  22049. (if (and (or author email)
  22050. org-export-author-info)
  22051. (insert (concat (nth 1 lang-words) ": " (or author "")
  22052. (if email (concat " <" email ">") "")
  22053. "\n")))
  22054. (cond
  22055. ((and date (string-match "%" date))
  22056. (setq date (format-time-string date (current-time))))
  22057. (date)
  22058. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22059. (if (and date org-export-time-stamp-file)
  22060. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22061. (insert "\n\n")
  22062. (if org-export-with-toc
  22063. (progn
  22064. (push (concat (nth 3 lang-words) "\n") thetoc)
  22065. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22066. (mapc '(lambda (line)
  22067. (if (string-match org-todo-line-regexp
  22068. line)
  22069. ;; This is a headline
  22070. (progn
  22071. (setq have-headings t)
  22072. (setq level (- (match-end 1) (match-beginning 1))
  22073. level (org-tr-level level)
  22074. txt (match-string 3 line)
  22075. todo
  22076. (or (and org-export-mark-todo-in-toc
  22077. (match-beginning 2)
  22078. (not (member (match-string 2 line)
  22079. org-done-keywords)))
  22080. ; TODO, not DONE
  22081. (and org-export-mark-todo-in-toc
  22082. (= level umax-toc)
  22083. (org-search-todo-below
  22084. line lines level))))
  22085. (setq txt (org-html-expand-for-ascii txt))
  22086. (while (string-match org-bracket-link-regexp txt)
  22087. (setq txt
  22088. (replace-match
  22089. (match-string (if (match-end 2) 3 1) txt)
  22090. t t txt)))
  22091. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22092. (string-match
  22093. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22094. txt))
  22095. (setq txt (replace-match "" t t txt)))
  22096. (if (string-match quote-re0 txt)
  22097. (setq txt (replace-match "" t t txt)))
  22098. (if org-export-with-section-numbers
  22099. (setq txt (concat (org-section-number level)
  22100. " " txt)))
  22101. (if (<= level umax-toc)
  22102. (progn
  22103. (push
  22104. (concat
  22105. (make-string
  22106. (* (max 0 (- level org-min-level)) 4) ?\ )
  22107. (format (if todo "%s (*)\n" "%s\n") txt))
  22108. thetoc)
  22109. (setq org-last-level level))
  22110. ))))
  22111. lines)
  22112. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22113. (org-init-section-numbers)
  22114. (while (setq line (pop lines))
  22115. ;; Remove the quoted HTML tags.
  22116. (setq line (org-html-expand-for-ascii line))
  22117. ;; Remove targets
  22118. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22119. (setq line (replace-match "" t t line)))
  22120. ;; Replace internal links
  22121. (while (string-match org-bracket-link-regexp line)
  22122. (setq line (replace-match
  22123. (if (match-end 3) "[\\3]" "[\\1]")
  22124. t nil line)))
  22125. (when custom-times
  22126. (setq line (org-translate-time line)))
  22127. (cond
  22128. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22129. ;; a Headline
  22130. (setq first-heading-pos (or first-heading-pos (point)))
  22131. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22132. txt (match-string 2 line))
  22133. (org-ascii-level-start level txt umax lines))
  22134. ((and org-export-with-tables
  22135. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22136. (if (not table-open)
  22137. ;; New table starts
  22138. (setq table-open t table-buffer nil))
  22139. ;; Accumulate lines
  22140. (setq table-buffer (cons line table-buffer))
  22141. (when (or (not lines)
  22142. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22143. (car lines))))
  22144. (setq table-open nil
  22145. table-buffer (nreverse table-buffer))
  22146. (insert (mapconcat
  22147. (lambda (x)
  22148. (org-fix-indentation x org-ascii-current-indentation))
  22149. (org-format-table-ascii table-buffer)
  22150. "\n") "\n")))
  22151. (t
  22152. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22153. (if (and org-export-with-fixed-width
  22154. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22155. (setq line (replace-match "\\1" nil nil line)))
  22156. (insert line "\n"))))
  22157. (normal-mode)
  22158. ;; insert the table of contents
  22159. (when thetoc
  22160. (goto-char (point-min))
  22161. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22162. (progn
  22163. (goto-char (match-beginning 0))
  22164. (replace-match ""))
  22165. (goto-char first-heading-pos))
  22166. (mapc 'insert thetoc)
  22167. (or (looking-at "[ \t]*\n[ \t]*\n")
  22168. (insert "\n\n")))
  22169. ;; Convert whitespace place holders
  22170. (goto-char (point-min))
  22171. (let (beg end)
  22172. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22173. (setq end (next-single-property-change beg 'org-whitespace))
  22174. (goto-char beg)
  22175. (delete-region beg end)
  22176. (insert (make-string (- end beg) ?\ ))))
  22177. (save-buffer)
  22178. ;; remove display and invisible chars
  22179. (let (beg end)
  22180. (goto-char (point-min))
  22181. (while (setq beg (next-single-property-change (point) 'display))
  22182. (setq end (next-single-property-change beg 'display))
  22183. (delete-region beg end)
  22184. (goto-char beg)
  22185. (insert "=>"))
  22186. (goto-char (point-min))
  22187. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22188. (setq end (next-single-property-change beg 'org-cwidth))
  22189. (delete-region beg end)
  22190. (goto-char beg)))
  22191. (goto-char (point-min))))
  22192. (defun org-export-ascii-clean-string ()
  22193. "Do extra work for ASCII export"
  22194. (goto-char (point-min))
  22195. (while (re-search-forward org-verbatim-re nil t)
  22196. (goto-char (match-end 2))
  22197. (backward-delete-char 1) (insert "'")
  22198. (goto-char (match-beginning 2))
  22199. (delete-char 1) (insert "`")
  22200. (goto-char (match-end 2))))
  22201. (defun org-search-todo-below (line lines level)
  22202. "Search the subtree below LINE for any TODO entries."
  22203. (let ((rest (cdr (memq line lines)))
  22204. (re org-todo-line-regexp)
  22205. line lv todo)
  22206. (catch 'exit
  22207. (while (setq line (pop rest))
  22208. (if (string-match re line)
  22209. (progn
  22210. (setq lv (- (match-end 1) (match-beginning 1))
  22211. todo (and (match-beginning 2)
  22212. (not (member (match-string 2 line)
  22213. org-done-keywords))))
  22214. ; TODO, not DONE
  22215. (if (<= lv level) (throw 'exit nil))
  22216. (if todo (throw 'exit t))))))))
  22217. (defun org-html-expand-for-ascii (line)
  22218. "Handle quoted HTML for ASCII export."
  22219. (if org-export-html-expand
  22220. (while (string-match "@<[^<>\n]*>" line)
  22221. ;; We just remove the tags for now.
  22222. (setq line (replace-match "" nil nil line))))
  22223. line)
  22224. (defun org-insert-centered (s &optional underline)
  22225. "Insert the string S centered and underline it with character UNDERLINE."
  22226. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22227. (insert (make-string ind ?\ ) s "\n")
  22228. (if underline
  22229. (insert (make-string ind ?\ )
  22230. (make-string (string-width s) underline)
  22231. "\n"))))
  22232. (defun org-ascii-level-start (level title umax &optional lines)
  22233. "Insert a new level in ASCII export."
  22234. (let (char (n (- level umax 1)) (ind 0))
  22235. (if (> level umax)
  22236. (progn
  22237. (insert (make-string (* 2 n) ?\ )
  22238. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22239. org-export-ascii-bullets))
  22240. " " title "\n")
  22241. ;; find the indentation of the next non-empty line
  22242. (catch 'stop
  22243. (while lines
  22244. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22245. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22246. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22247. (pop lines)))
  22248. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22249. (if (or (not (equal (char-before) ?\n))
  22250. (not (equal (char-before (1- (point))) ?\n)))
  22251. (insert "\n"))
  22252. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22253. (unless org-export-with-tags
  22254. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22255. (setq title (replace-match "" t t title))))
  22256. (if org-export-with-section-numbers
  22257. (setq title (concat (org-section-number level) " " title)))
  22258. (insert title "\n" (make-string (string-width title) char) "\n")
  22259. (setq org-ascii-current-indentation '(0 . 0)))))
  22260. (defun org-export-visible (type arg)
  22261. "Create a copy of the visible part of the current buffer, and export it.
  22262. The copy is created in a temporary buffer and removed after use.
  22263. TYPE is the final key (as a string) that also select the export command in
  22264. the `C-c C-e' export dispatcher.
  22265. As a special case, if the you type SPC at the prompt, the temporary
  22266. org-mode file will not be removed but presented to you so that you can
  22267. continue to use it. The prefix arg ARG is passed through to the exporting
  22268. command."
  22269. (interactive
  22270. (list (progn
  22271. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22272. (read-char-exclusive))
  22273. current-prefix-arg))
  22274. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22275. (error "Invalid export key"))
  22276. (let* ((binding (cdr (assoc type
  22277. '((?a . org-export-as-ascii)
  22278. (?\C-a . org-export-as-ascii)
  22279. (?b . org-export-as-html-and-open)
  22280. (?\C-b . org-export-as-html-and-open)
  22281. (?h . org-export-as-html)
  22282. (?H . org-export-as-html-to-buffer)
  22283. (?R . org-export-region-as-html)
  22284. (?x . org-export-as-xoxo)))))
  22285. (keepp (equal type ?\ ))
  22286. (file buffer-file-name)
  22287. (buffer (get-buffer-create "*Org Export Visible*"))
  22288. s e)
  22289. ;; Need to hack the drawers here.
  22290. (save-excursion
  22291. (goto-char (point-min))
  22292. (while (re-search-forward org-drawer-regexp nil t)
  22293. (goto-char (match-beginning 1))
  22294. (or (org-invisible-p) (org-flag-drawer nil))))
  22295. (with-current-buffer buffer (erase-buffer))
  22296. (save-excursion
  22297. (setq s (goto-char (point-min)))
  22298. (while (not (= (point) (point-max)))
  22299. (goto-char (org-find-invisible))
  22300. (append-to-buffer buffer s (point))
  22301. (setq s (goto-char (org-find-visible))))
  22302. (org-cycle-hide-drawers 'all)
  22303. (goto-char (point-min))
  22304. (unless keepp
  22305. ;; Copy all comment lines to the end, to make sure #+ settings are
  22306. ;; still available for the second export step. Kind of a hack, but
  22307. ;; does do the trick.
  22308. (if (looking-at "#[^\r\n]*")
  22309. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22310. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22311. (append-to-buffer buffer (1+ (match-beginning 0))
  22312. (min (point-max) (1+ (match-end 0))))))
  22313. (set-buffer buffer)
  22314. (let ((buffer-file-name file)
  22315. (org-inhibit-startup t))
  22316. (org-mode)
  22317. (show-all)
  22318. (unless keepp (funcall binding arg))))
  22319. (if (not keepp)
  22320. (kill-buffer buffer)
  22321. (switch-to-buffer-other-window buffer)
  22322. (goto-char (point-min)))))
  22323. (defun org-find-visible ()
  22324. (let ((s (point)))
  22325. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22326. (get-char-property s 'invisible)))
  22327. s))
  22328. (defun org-find-invisible ()
  22329. (let ((s (point)))
  22330. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22331. (not (get-char-property s 'invisible))))
  22332. s))
  22333. ;;; HTML export
  22334. (defun org-get-current-options ()
  22335. "Return a string with current options as keyword options.
  22336. Does include HTML export options as well as TODO and CATEGORY stuff."
  22337. (format
  22338. "#+TITLE: %s
  22339. #+AUTHOR: %s
  22340. #+EMAIL: %s
  22341. #+LANGUAGE: %s
  22342. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22343. #+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
  22344. #+CATEGORY: %s
  22345. #+SEQ_TODO: %s
  22346. #+TYP_TODO: %s
  22347. #+PRIORITIES: %c %c %c
  22348. #+DRAWERS: %s
  22349. #+STARTUP: %s %s %s %s %s
  22350. #+TAGS: %s
  22351. #+ARCHIVE: %s
  22352. #+LINK: %s
  22353. "
  22354. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22355. org-export-headline-levels
  22356. org-export-with-section-numbers
  22357. org-export-with-toc
  22358. org-export-preserve-breaks
  22359. org-export-html-expand
  22360. org-export-with-fixed-width
  22361. org-export-with-tables
  22362. org-export-with-sub-superscripts
  22363. org-export-with-special-strings
  22364. org-export-with-footnotes
  22365. org-export-with-emphasize
  22366. org-export-with-TeX-macros
  22367. org-export-with-LaTeX-fragments
  22368. org-export-skip-text-before-1st-heading
  22369. org-export-with-drawers
  22370. org-export-with-tags
  22371. (file-name-nondirectory buffer-file-name)
  22372. "TODO FEEDBACK VERIFY DONE"
  22373. "Me Jason Marie DONE"
  22374. org-highest-priority org-lowest-priority org-default-priority
  22375. (mapconcat 'identity org-drawers " ")
  22376. (cdr (assoc org-startup-folded
  22377. '((nil . "showall") (t . "overview") (content . "content"))))
  22378. (if org-odd-levels-only "odd" "oddeven")
  22379. (if org-hide-leading-stars "hidestars" "showstars")
  22380. (if org-startup-align-all-tables "align" "noalign")
  22381. (cond ((eq t org-log-done) "logdone")
  22382. ((not org-log-done) "nologging")
  22383. ((listp org-log-done)
  22384. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  22385. org-log-done " ")))
  22386. (or (mapconcat (lambda (x)
  22387. (cond
  22388. ((equal '(:startgroup) x) "{")
  22389. ((equal '(:endgroup) x) "}")
  22390. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22391. (t (car x))))
  22392. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22393. org-archive-location
  22394. "org file:~/org/%s.org"
  22395. ))
  22396. (defun org-insert-export-options-template ()
  22397. "Insert into the buffer a template with information for exporting."
  22398. (interactive)
  22399. (if (not (bolp)) (newline))
  22400. (let ((s (org-get-current-options)))
  22401. (and (string-match "#\\+CATEGORY" s)
  22402. (setq s (substring s 0 (match-beginning 0))))
  22403. (insert s)))
  22404. (defun org-toggle-fixed-width-section (arg)
  22405. "Toggle the fixed-width export.
  22406. If there is no active region, the QUOTE keyword at the current headline is
  22407. inserted or removed. When present, it causes the text between this headline
  22408. and the next to be exported as fixed-width text, and unmodified.
  22409. If there is an active region, this command adds or removes a colon as the
  22410. first character of this line. If the first character of a line is a colon,
  22411. this line is also exported in fixed-width font."
  22412. (interactive "P")
  22413. (let* ((cc 0)
  22414. (regionp (org-region-active-p))
  22415. (beg (if regionp (region-beginning) (point)))
  22416. (end (if regionp (region-end)))
  22417. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22418. (case-fold-search nil)
  22419. (re "[ \t]*\\(:\\)")
  22420. off)
  22421. (if regionp
  22422. (save-excursion
  22423. (goto-char beg)
  22424. (setq cc (current-column))
  22425. (beginning-of-line 1)
  22426. (setq off (looking-at re))
  22427. (while (> nlines 0)
  22428. (setq nlines (1- nlines))
  22429. (beginning-of-line 1)
  22430. (cond
  22431. (arg
  22432. (move-to-column cc t)
  22433. (insert ":\n")
  22434. (forward-line -1))
  22435. ((and off (looking-at re))
  22436. (replace-match "" t t nil 1))
  22437. ((not off) (move-to-column cc t) (insert ":")))
  22438. (forward-line 1)))
  22439. (save-excursion
  22440. (org-back-to-heading)
  22441. (if (looking-at (concat outline-regexp
  22442. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22443. (replace-match "" t t nil 1)
  22444. (if (looking-at outline-regexp)
  22445. (progn
  22446. (goto-char (match-end 0))
  22447. (insert org-quote-string " "))))))))
  22448. (defun org-export-as-html-and-open (arg)
  22449. "Export the outline as HTML and immediately open it with a browser.
  22450. If there is an active region, export only the region.
  22451. The prefix ARG specifies how many levels of the outline should become
  22452. headlines. The default is 3. Lower levels will become bulleted lists."
  22453. (interactive "P")
  22454. (org-export-as-html arg 'hidden)
  22455. (org-open-file buffer-file-name))
  22456. (defun org-export-as-html-batch ()
  22457. "Call `org-export-as-html', may be used in batch processing as
  22458. emacs --batch
  22459. --load=$HOME/lib/emacs/org.el
  22460. --eval \"(setq org-export-headline-levels 2)\"
  22461. --visit=MyFile --funcall org-export-as-html-batch"
  22462. (org-export-as-html org-export-headline-levels 'hidden))
  22463. (defun org-export-as-html-to-buffer (arg)
  22464. "Call `org-exort-as-html` with output to a temporary buffer.
  22465. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22466. (interactive "P")
  22467. (org-export-as-html arg nil nil "*Org HTML Export*")
  22468. (switch-to-buffer-other-window "*Org HTML Export*"))
  22469. (defun org-replace-region-by-html (beg end)
  22470. "Assume the current region has org-mode syntax, and convert it to HTML.
  22471. This can be used in any buffer. For example, you could write an
  22472. itemized list in org-mode syntax in an HTML buffer and then use this
  22473. command to convert it."
  22474. (interactive "r")
  22475. (let (reg html buf pop-up-frames)
  22476. (save-window-excursion
  22477. (if (org-mode-p)
  22478. (setq html (org-export-region-as-html
  22479. beg end t 'string))
  22480. (setq reg (buffer-substring beg end)
  22481. buf (get-buffer-create "*Org tmp*"))
  22482. (with-current-buffer buf
  22483. (erase-buffer)
  22484. (insert reg)
  22485. (org-mode)
  22486. (setq html (org-export-region-as-html
  22487. (point-min) (point-max) t 'string)))
  22488. (kill-buffer buf)))
  22489. (delete-region beg end)
  22490. (insert html)))
  22491. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22492. "Convert region from BEG to END in org-mode buffer to HTML.
  22493. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22494. contents, and only produce the region of converted text, useful for
  22495. cut-and-paste operations.
  22496. If BUFFER is a buffer or a string, use/create that buffer as a target
  22497. of the converted HTML. If BUFFER is the symbol `string', return the
  22498. produced HTML as a string and leave not buffer behind. For example,
  22499. a Lisp program could call this function in the following way:
  22500. (setq html (org-export-region-as-html beg end t 'string))
  22501. When called interactively, the output buffer is selected, and shown
  22502. in a window. A non-interactive call will only retunr the buffer."
  22503. (interactive "r\nP")
  22504. (when (interactive-p)
  22505. (setq buffer "*Org HTML Export*"))
  22506. (let ((transient-mark-mode t) (zmacs-regions t)
  22507. rtn)
  22508. (goto-char end)
  22509. (set-mark (point)) ;; to activate the region
  22510. (goto-char beg)
  22511. (setq rtn (org-export-as-html
  22512. nil nil nil
  22513. buffer body-only))
  22514. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22515. (if (and (interactive-p) (bufferp rtn))
  22516. (switch-to-buffer-other-window rtn)
  22517. rtn)))
  22518. (defvar html-table-tag nil) ; dynamically scoped into this.
  22519. (defun org-export-as-html (arg &optional hidden ext-plist
  22520. to-buffer body-only)
  22521. "Export the outline as a pretty HTML file.
  22522. If there is an active region, export only the region. The prefix
  22523. ARG specifies how many levels of the outline should become
  22524. headlines. The default is 3. Lower levels will become bulleted
  22525. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22526. EXT-PLIST is a property list with external parameters overriding
  22527. org-mode's default settings, but still inferior to file-local
  22528. settings. When TO-BUFFER is non-nil, create a buffer with that
  22529. name and export to that buffer. If TO-BUFFER is the symbol `string',
  22530. don't leave any buffer behind but just return the resulting HTML as
  22531. a string. When BODY-ONLY is set, don't produce the file header and footer,
  22532. simply return the content of <body>...</body>, without even
  22533. the body tags themselves."
  22534. (interactive "P")
  22535. ;; Make sure we have a file name when we need it.
  22536. (when (and (not (or to-buffer body-only))
  22537. (not buffer-file-name))
  22538. (if (buffer-base-buffer)
  22539. (org-set-local 'buffer-file-name
  22540. (with-current-buffer (buffer-base-buffer)
  22541. buffer-file-name))
  22542. (error "Need a file name to be able to export.")))
  22543. (message "Exporting...")
  22544. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22545. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  22546. (setq-default org-done-keywords org-done-keywords)
  22547. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  22548. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22549. ext-plist
  22550. (org-infile-export-plist)))
  22551. (style (plist-get opt-plist :style))
  22552. (link-validate (plist-get opt-plist :link-validation-function))
  22553. valid thetoc have-headings first-heading-pos
  22554. (odd org-odd-levels-only)
  22555. (region-p (org-region-active-p))
  22556. (subtree-p
  22557. (when region-p
  22558. (save-excursion
  22559. (goto-char (region-beginning))
  22560. (and (org-at-heading-p)
  22561. (>= (org-end-of-subtree t t) (region-end))))))
  22562. ;; The following two are dynamically scoped into other
  22563. ;; routines below.
  22564. (org-current-export-dir (org-export-directory :html opt-plist))
  22565. (org-current-export-file buffer-file-name)
  22566. (level 0) (line "") (origline "") txt todo
  22567. (umax nil)
  22568. (umax-toc nil)
  22569. (filename (if to-buffer nil
  22570. (concat (file-name-as-directory
  22571. (org-export-directory :html opt-plist))
  22572. (file-name-sans-extension
  22573. (or (and subtree-p
  22574. (org-entry-get (region-beginning)
  22575. "EXPORT_FILE_NAME" t))
  22576. (file-name-nondirectory buffer-file-name)))
  22577. "." org-export-html-extension)))
  22578. (current-dir (if buffer-file-name
  22579. (file-name-directory buffer-file-name)
  22580. default-directory))
  22581. (buffer (if to-buffer
  22582. (cond
  22583. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  22584. (t (get-buffer-create to-buffer)))
  22585. (find-file-noselect filename)))
  22586. (org-levels-open (make-vector org-level-max nil))
  22587. (date (plist-get opt-plist :date))
  22588. (author (plist-get opt-plist :author))
  22589. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22590. (plist-get opt-plist :title)
  22591. (and (not
  22592. (plist-get opt-plist :skip-before-1st-heading))
  22593. (org-export-grab-title-from-buffer))
  22594. (and buffer-file-name
  22595. (file-name-sans-extension
  22596. (file-name-nondirectory buffer-file-name)))
  22597. "UNTITLED"))
  22598. (html-table-tag (plist-get opt-plist :html-table-tag))
  22599. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22600. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  22601. (inquote nil)
  22602. (infixed nil)
  22603. (in-local-list nil)
  22604. (local-list-num nil)
  22605. (local-list-indent nil)
  22606. (llt org-plain-list-ordered-item-terminator)
  22607. (email (plist-get opt-plist :email))
  22608. (language (plist-get opt-plist :language))
  22609. (lang-words nil)
  22610. (target-alist nil) tg
  22611. (head-count 0) cnt
  22612. (start 0)
  22613. (coding-system (and (boundp 'buffer-file-coding-system)
  22614. buffer-file-coding-system))
  22615. (coding-system-for-write (or org-export-html-coding-system
  22616. coding-system))
  22617. (save-buffer-coding-system (or org-export-html-coding-system
  22618. coding-system))
  22619. (charset (and coding-system-for-write
  22620. (fboundp 'coding-system-get)
  22621. (coding-system-get coding-system-for-write
  22622. 'mime-charset)))
  22623. (region
  22624. (buffer-substring
  22625. (if region-p (region-beginning) (point-min))
  22626. (if region-p (region-end) (point-max))))
  22627. (lines
  22628. (org-split-string
  22629. (org-cleaned-string-for-export
  22630. region
  22631. :emph-multiline t
  22632. :for-html t
  22633. :skip-before-1st-heading
  22634. (plist-get opt-plist :skip-before-1st-heading)
  22635. :drawers (plist-get opt-plist :drawers)
  22636. :archived-trees
  22637. (plist-get opt-plist :archived-trees)
  22638. :add-text
  22639. (plist-get opt-plist :text)
  22640. :LaTeX-fragments
  22641. (plist-get opt-plist :LaTeX-fragments))
  22642. "[\r\n]"))
  22643. table-open type
  22644. table-buffer table-orig-buffer
  22645. ind start-is-num starter didclose
  22646. rpl path desc descp desc1 desc2 link
  22647. )
  22648. (let ((inhibit-read-only t))
  22649. (org-unmodified
  22650. (remove-text-properties (point-min) (point-max)
  22651. '(:org-license-to-kill t))))
  22652. (message "Exporting...")
  22653. (setq org-min-level (org-get-min-level lines))
  22654. (setq org-last-level org-min-level)
  22655. (org-init-section-numbers)
  22656. (cond
  22657. ((and date (string-match "%" date))
  22658. (setq date (format-time-string date (current-time))))
  22659. (date)
  22660. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22661. ;; Get the language-dependent settings
  22662. (setq lang-words (or (assoc language org-export-language-setup)
  22663. (assoc "en" org-export-language-setup)))
  22664. ;; Switch to the output buffer
  22665. (set-buffer buffer)
  22666. (erase-buffer)
  22667. (fundamental-mode)
  22668. (and (fboundp 'set-buffer-file-coding-system)
  22669. (set-buffer-file-coding-system coding-system-for-write))
  22670. (let ((case-fold-search nil)
  22671. (org-odd-levels-only odd))
  22672. ;; create local variables for all options, to make sure all called
  22673. ;; functions get the correct information
  22674. (mapc (lambda (x)
  22675. (set (make-local-variable (cdr x))
  22676. (plist-get opt-plist (car x))))
  22677. org-export-plist-vars)
  22678. (setq umax (if arg (prefix-numeric-value arg)
  22679. org-export-headline-levels))
  22680. (setq umax-toc (if (integerp org-export-with-toc)
  22681. (min org-export-with-toc umax)
  22682. umax))
  22683. (unless body-only
  22684. ;; File header
  22685. (insert (format
  22686. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  22687. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  22688. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  22689. lang=\"%s\" xml:lang=\"%s\">
  22690. <head>
  22691. <title>%s</title>
  22692. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  22693. <meta name=\"generator\" content=\"Org-mode\"/>
  22694. <meta name=\"generated\" content=\"%s\"/>
  22695. <meta name=\"author\" content=\"%s\"/>
  22696. %s
  22697. </head><body>
  22698. "
  22699. language language (org-html-expand title)
  22700. (or charset "iso-8859-1") date author style))
  22701. (insert (or (plist-get opt-plist :preamble) ""))
  22702. (when (plist-get opt-plist :auto-preamble)
  22703. (if title (insert (format org-export-html-title-format
  22704. (org-html-expand title))))))
  22705. (if (and org-export-with-toc (not body-only))
  22706. (progn
  22707. (push (format "<h%d>%s</h%d>\n"
  22708. org-export-html-toplevel-hlevel
  22709. (nth 3 lang-words)
  22710. org-export-html-toplevel-hlevel)
  22711. thetoc)
  22712. (push "<ul>\n<li>" thetoc)
  22713. (setq lines
  22714. (mapcar '(lambda (line)
  22715. (if (string-match org-todo-line-regexp line)
  22716. ;; This is a headline
  22717. (progn
  22718. (setq have-headings t)
  22719. (setq level (- (match-end 1) (match-beginning 1))
  22720. level (org-tr-level level)
  22721. txt (save-match-data
  22722. (org-html-expand
  22723. (org-export-cleanup-toc-line
  22724. (match-string 3 line))))
  22725. todo
  22726. (or (and org-export-mark-todo-in-toc
  22727. (match-beginning 2)
  22728. (not (member (match-string 2 line)
  22729. org-done-keywords)))
  22730. ; TODO, not DONE
  22731. (and org-export-mark-todo-in-toc
  22732. (= level umax-toc)
  22733. (org-search-todo-below
  22734. line lines level))))
  22735. (if (string-match
  22736. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  22737. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  22738. (if (string-match quote-re0 txt)
  22739. (setq txt (replace-match "" t t txt)))
  22740. (if org-export-with-section-numbers
  22741. (setq txt (concat (org-section-number level)
  22742. " " txt)))
  22743. (if (<= level (max umax umax-toc))
  22744. (setq head-count (+ head-count 1)))
  22745. (if (<= level umax-toc)
  22746. (progn
  22747. (if (> level org-last-level)
  22748. (progn
  22749. (setq cnt (- level org-last-level))
  22750. (while (>= (setq cnt (1- cnt)) 0)
  22751. (push "\n<ul>\n<li>" thetoc))
  22752. (push "\n" thetoc)))
  22753. (if (< level org-last-level)
  22754. (progn
  22755. (setq cnt (- org-last-level level))
  22756. (while (>= (setq cnt (1- cnt)) 0)
  22757. (push "</li>\n</ul>" thetoc))
  22758. (push "\n" thetoc)))
  22759. ;; Check for targets
  22760. (while (string-match org-target-regexp line)
  22761. (setq tg (match-string 1 line)
  22762. line (replace-match
  22763. (concat "@<span class=\"target\">" tg "@</span> ")
  22764. t t line))
  22765. (push (cons (org-solidify-link-text tg)
  22766. (format "sec-%d" head-count))
  22767. target-alist))
  22768. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  22769. (setq txt (replace-match "" t t txt)))
  22770. (push
  22771. (format
  22772. (if todo
  22773. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  22774. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  22775. head-count txt) thetoc)
  22776. (setq org-last-level level))
  22777. )))
  22778. line)
  22779. lines))
  22780. (while (> org-last-level (1- org-min-level))
  22781. (setq org-last-level (1- org-last-level))
  22782. (push "</li>\n</ul>\n" thetoc))
  22783. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22784. (setq head-count 0)
  22785. (org-init-section-numbers)
  22786. (while (setq line (pop lines) origline line)
  22787. (catch 'nextline
  22788. ;; end of quote section?
  22789. (when (and inquote (string-match "^\\*+ " line))
  22790. (insert "</pre>\n")
  22791. (setq inquote nil))
  22792. ;; inside a quote section?
  22793. (when inquote
  22794. (insert (org-html-protect line) "\n")
  22795. (throw 'nextline nil))
  22796. ;; verbatim lines
  22797. (when (and org-export-with-fixed-width
  22798. (string-match "^[ \t]*:\\(.*\\)" line))
  22799. (when (not infixed)
  22800. (setq infixed t)
  22801. (insert "<pre>\n"))
  22802. (insert (org-html-protect (match-string 1 line)) "\n")
  22803. (when (and lines
  22804. (not (string-match "^[ \t]*\\(:.*\\)"
  22805. (car lines))))
  22806. (setq infixed nil)
  22807. (insert "</pre>\n"))
  22808. (throw 'nextline nil))
  22809. ;; Protected HTML
  22810. (when (get-text-property 0 'org-protected line)
  22811. (let (par)
  22812. (when (re-search-backward
  22813. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  22814. (setq par (match-string 1))
  22815. (replace-match "\\2\n"))
  22816. (insert line "\n")
  22817. (while (and lines
  22818. (or (= (length (car lines)) 0)
  22819. (get-text-property 0 'org-protected (car lines))))
  22820. (insert (pop lines) "\n"))
  22821. (and par (insert "<p>\n")))
  22822. (throw 'nextline nil))
  22823. ;; Horizontal line
  22824. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  22825. (insert "\n<hr/>\n")
  22826. (throw 'nextline nil))
  22827. ;; make targets to anchors
  22828. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  22829. (cond
  22830. ((match-end 2)
  22831. (setq line (replace-match
  22832. (concat "@<a name=\""
  22833. (org-solidify-link-text (match-string 1 line))
  22834. "\">\\nbsp@</a>")
  22835. t t line)))
  22836. ((and org-export-with-toc (equal (string-to-char line) ?*))
  22837. (setq line (replace-match
  22838. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  22839. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  22840. t t line)))
  22841. (t
  22842. (setq line (replace-match
  22843. (concat "@<a name=\""
  22844. (org-solidify-link-text (match-string 1 line))
  22845. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  22846. t t line)))))
  22847. (setq line (org-html-handle-time-stamps line))
  22848. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  22849. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  22850. ;; Also handle sub_superscripts and checkboxes
  22851. (or (string-match org-table-hline-regexp line)
  22852. (setq line (org-html-expand line)))
  22853. ;; Format the links
  22854. (setq start 0)
  22855. (while (string-match org-bracket-link-analytic-regexp line start)
  22856. (setq start (match-beginning 0))
  22857. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  22858. (setq path (match-string 3 line))
  22859. (setq desc1 (if (match-end 5) (match-string 5 line))
  22860. desc2 (if (match-end 2) (concat type ":" path) path)
  22861. descp (and desc1 (not (equal desc1 desc2)))
  22862. desc (or desc1 desc2))
  22863. ;; Make an image out of the description if that is so wanted
  22864. (when (and descp (org-file-image-p desc))
  22865. (save-match-data
  22866. (if (string-match "^file:" desc)
  22867. (setq desc (substring desc (match-end 0)))))
  22868. (setq desc (concat "<img src=\"" desc "\"/>")))
  22869. ;; FIXME: do we need to unescape here somewhere?
  22870. (cond
  22871. ((equal type "internal")
  22872. (setq rpl
  22873. (concat
  22874. "<a href=\"#"
  22875. (org-solidify-link-text
  22876. (save-match-data (org-link-unescape path)) target-alist)
  22877. "\">" desc "</a>")))
  22878. ((member type '("http" "https"))
  22879. ;; standard URL, just check if we need to inline an image
  22880. (if (and (or (eq t org-export-html-inline-images)
  22881. (and org-export-html-inline-images (not descp)))
  22882. (org-file-image-p path))
  22883. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  22884. (setq link (concat type ":" path))
  22885. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  22886. ((member type '("ftp" "mailto" "news"))
  22887. ;; standard URL
  22888. (setq link (concat type ":" path))
  22889. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  22890. ((string= type "file")
  22891. ;; FILE link
  22892. (let* ((filename path)
  22893. (abs-p (file-name-absolute-p filename))
  22894. thefile file-is-image-p search)
  22895. (save-match-data
  22896. (if (string-match "::\\(.*\\)" filename)
  22897. (setq search (match-string 1 filename)
  22898. filename (replace-match "" t nil filename)))
  22899. (setq valid
  22900. (if (functionp link-validate)
  22901. (funcall link-validate filename current-dir)
  22902. t))
  22903. (setq file-is-image-p (org-file-image-p filename))
  22904. (setq thefile (if abs-p (expand-file-name filename) filename))
  22905. (when (and org-export-html-link-org-files-as-html
  22906. (string-match "\\.org$" thefile))
  22907. (setq thefile (concat (substring thefile 0
  22908. (match-beginning 0))
  22909. "." org-export-html-extension))
  22910. (if (and search
  22911. ;; make sure this is can be used as target search
  22912. (not (string-match "^[0-9]*$" search))
  22913. (not (string-match "^\\*" search))
  22914. (not (string-match "^/.*/$" search)))
  22915. (setq thefile (concat thefile "#"
  22916. (org-solidify-link-text
  22917. (org-link-unescape search)))))
  22918. (when (string-match "^file:" desc)
  22919. (setq desc (replace-match "" t t desc))
  22920. (if (string-match "\\.org$" desc)
  22921. (setq desc (replace-match "" t t desc))))))
  22922. (setq rpl (if (and file-is-image-p
  22923. (or (eq t org-export-html-inline-images)
  22924. (and org-export-html-inline-images
  22925. (not descp))))
  22926. (concat "<img src=\"" thefile "\"/>")
  22927. (concat "<a href=\"" thefile "\">" desc "</a>")))
  22928. (if (not valid) (setq rpl desc))))
  22929. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  22930. (setq rpl (concat "<i>&lt;" type ":"
  22931. (save-match-data (org-link-unescape path))
  22932. "&gt;</i>"))))
  22933. (setq line (replace-match rpl t t line)
  22934. start (+ start (length rpl))))
  22935. ;; TODO items
  22936. (if (and (string-match org-todo-line-regexp line)
  22937. (match-beginning 2))
  22938. (setq line
  22939. (concat (substring line 0 (match-beginning 2))
  22940. "<span class=\""
  22941. (if (member (match-string 2 line)
  22942. org-done-keywords)
  22943. "done" "todo")
  22944. "\">" (match-string 2 line)
  22945. "</span>" (substring line (match-end 2)))))
  22946. ;; Does this contain a reference to a footnote?
  22947. (when org-export-with-footnotes
  22948. (setq start 0)
  22949. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  22950. (if (get-text-property (match-beginning 2) 'org-protected line)
  22951. (setq start (match-end 2))
  22952. (let ((n (match-string 2 line)))
  22953. (setq line
  22954. (replace-match
  22955. (format
  22956. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  22957. (match-string 1 line) n n n)
  22958. t t line))))))
  22959. (cond
  22960. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22961. ;; This is a headline
  22962. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22963. txt (match-string 2 line))
  22964. (if (string-match quote-re0 txt)
  22965. (setq txt (replace-match "" t t txt)))
  22966. (if (<= level (max umax umax-toc))
  22967. (setq head-count (+ head-count 1)))
  22968. (when in-local-list
  22969. ;; Close any local lists before inserting a new header line
  22970. (while local-list-num
  22971. (org-close-li)
  22972. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  22973. (pop local-list-num))
  22974. (setq local-list-indent nil
  22975. in-local-list nil))
  22976. (setq first-heading-pos (or first-heading-pos (point)))
  22977. (org-html-level-start level txt umax
  22978. (and org-export-with-toc (<= level umax))
  22979. head-count)
  22980. ;; QUOTES
  22981. (when (string-match quote-re line)
  22982. (insert "<pre>")
  22983. (setq inquote t)))
  22984. ((and org-export-with-tables
  22985. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22986. (if (not table-open)
  22987. ;; New table starts
  22988. (setq table-open t table-buffer nil table-orig-buffer nil))
  22989. ;; Accumulate lines
  22990. (setq table-buffer (cons line table-buffer)
  22991. table-orig-buffer (cons origline table-orig-buffer))
  22992. (when (or (not lines)
  22993. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22994. (car lines))))
  22995. (setq table-open nil
  22996. table-buffer (nreverse table-buffer)
  22997. table-orig-buffer (nreverse table-orig-buffer))
  22998. (org-close-par-maybe)
  22999. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23000. (t
  23001. ;; Normal lines
  23002. (when (string-match
  23003. (cond
  23004. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23005. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23006. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23007. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23008. line)
  23009. (setq ind (org-get-string-indentation line)
  23010. start-is-num (match-beginning 4)
  23011. starter (if (match-beginning 2)
  23012. (substring (match-string 2 line) 0 -1))
  23013. line (substring line (match-beginning 5)))
  23014. (unless (string-match "[^ \t]" line)
  23015. ;; empty line. Pretend indentation is large.
  23016. (setq ind (if org-empty-line-terminates-plain-lists
  23017. 0
  23018. (1+ (or (car local-list-indent) 1)))))
  23019. (setq didclose nil)
  23020. (while (and in-local-list
  23021. (or (and (= ind (car local-list-indent))
  23022. (not starter))
  23023. (< ind (car local-list-indent))))
  23024. (setq didclose t)
  23025. (org-close-li)
  23026. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23027. (pop local-list-num) (pop local-list-indent)
  23028. (setq in-local-list local-list-indent))
  23029. (cond
  23030. ((and starter
  23031. (or (not in-local-list)
  23032. (> ind (car local-list-indent))))
  23033. ;; Start new (level of) list
  23034. (org-close-par-maybe)
  23035. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23036. (push start-is-num local-list-num)
  23037. (push ind local-list-indent)
  23038. (setq in-local-list t))
  23039. (starter
  23040. ;; continue current list
  23041. (org-close-li)
  23042. (insert "<li>\n"))
  23043. (didclose
  23044. ;; we did close a list, normal text follows: need <p>
  23045. (org-open-par)))
  23046. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23047. (setq line
  23048. (replace-match
  23049. (if (equal (match-string 1 line) "X")
  23050. "<b>[X]</b>"
  23051. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23052. t t line))))
  23053. ;; Empty lines start a new paragraph. If hand-formatted lists
  23054. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23055. ;; also start a new paragraph.
  23056. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23057. ;; Is this the start of a footnote?
  23058. (when org-export-with-footnotes
  23059. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23060. (org-close-par-maybe)
  23061. (let ((n (match-string 1 line)))
  23062. (setq line (replace-match
  23063. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23064. ;; Check if the line break needs to be conserved
  23065. (cond
  23066. ((string-match "\\\\\\\\[ \t]*$" line)
  23067. (setq line (replace-match "<br/>" t t line)))
  23068. (org-export-preserve-breaks
  23069. (setq line (concat line "<br/>"))))
  23070. (insert line "\n")))))
  23071. ;; Properly close all local lists and other lists
  23072. (when inquote (insert "</pre>\n"))
  23073. (when in-local-list
  23074. ;; Close any local lists before inserting a new header line
  23075. (while local-list-num
  23076. (org-close-li)
  23077. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23078. (pop local-list-num))
  23079. (setq local-list-indent nil
  23080. in-local-list nil))
  23081. (org-html-level-start 1 nil umax
  23082. (and org-export-with-toc (<= level umax))
  23083. head-count)
  23084. (unless body-only
  23085. (when (plist-get opt-plist :auto-postamble)
  23086. (insert "<div id=\"postamble\">")
  23087. (when (and org-export-author-info author)
  23088. (insert "<p class=\"author\"> "
  23089. (nth 1 lang-words) ": " author "\n")
  23090. (when email
  23091. (if (listp (split-string email ",+ *"))
  23092. (mapc (lambda(e)
  23093. (insert "<a href=\"mailto:" e "\">&lt;"
  23094. e "&gt;</a>\n"))
  23095. (split-string email ",+ *"))
  23096. (insert "<a href=\"mailto:" email "\">&lt;"
  23097. email "&gt;</a>\n")))
  23098. (insert "</p>\n"))
  23099. (when (and date org-export-time-stamp-file)
  23100. (insert "<p class=\"date\"> "
  23101. (nth 2 lang-words) ": "
  23102. date "</p>\n"))
  23103. (insert "</div>"))
  23104. (if org-export-html-with-timestamp
  23105. (insert org-export-html-html-helper-timestamp))
  23106. (insert (or (plist-get opt-plist :postamble) ""))
  23107. (insert "</body>\n</html>\n"))
  23108. (normal-mode)
  23109. (if (eq major-mode default-major-mode) (html-mode))
  23110. ;; insert the table of contents
  23111. (goto-char (point-min))
  23112. (when thetoc
  23113. (if (or (re-search-forward
  23114. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23115. (re-search-forward
  23116. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23117. (progn
  23118. (goto-char (match-beginning 0))
  23119. (replace-match ""))
  23120. (goto-char first-heading-pos)
  23121. (when (looking-at "\\s-*</p>")
  23122. (goto-char (match-end 0))
  23123. (insert "\n")))
  23124. (insert "<div id=\"table-of-contents\">\n")
  23125. (mapc 'insert thetoc)
  23126. (insert "</div>\n"))
  23127. ;; remove empty paragraphs and lists
  23128. (goto-char (point-min))
  23129. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23130. (replace-match ""))
  23131. (goto-char (point-min))
  23132. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23133. (replace-match ""))
  23134. ;; Convert whitespace place holders
  23135. (goto-char (point-min))
  23136. (let (beg end n)
  23137. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23138. (setq n (get-text-property beg 'org-whitespace)
  23139. end (next-single-property-change beg 'org-whitespace))
  23140. (goto-char beg)
  23141. (delete-region beg end)
  23142. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23143. (make-string n ?x)))))
  23144. (or to-buffer (save-buffer))
  23145. (goto-char (point-min))
  23146. (message "Exporting... done")
  23147. (if (eq to-buffer 'string)
  23148. (prog1 (buffer-substring (point-min) (point-max))
  23149. (kill-buffer (current-buffer)))
  23150. (current-buffer)))))
  23151. (defvar org-table-colgroup-info nil)
  23152. (defun org-format-table-ascii (lines)
  23153. "Format a table for ascii export."
  23154. (if (stringp lines)
  23155. (setq lines (org-split-string lines "\n")))
  23156. (if (not (string-match "^[ \t]*|" (car lines)))
  23157. ;; Table made by table.el - test for spanning
  23158. lines
  23159. ;; A normal org table
  23160. ;; Get rid of hlines at beginning and end
  23161. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23162. (setq lines (nreverse lines))
  23163. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23164. (setq lines (nreverse lines))
  23165. (when org-export-table-remove-special-lines
  23166. ;; Check if the table has a marking column. If yes remove the
  23167. ;; column and the special lines
  23168. (setq lines (org-table-clean-before-export lines)))
  23169. ;; Get rid of the vertical lines except for grouping
  23170. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23171. rtn line vl1 start)
  23172. (while (setq line (pop lines))
  23173. (if (string-match org-table-hline-regexp line)
  23174. (and (string-match "|\\(.*\\)|" line)
  23175. (setq line (replace-match " \\1" t nil line)))
  23176. (setq start 0 vl1 vl)
  23177. (while (string-match "|" line start)
  23178. (setq start (match-end 0))
  23179. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23180. (push line rtn))
  23181. (nreverse rtn))))
  23182. (defun org-colgroup-info-to-vline-list (info)
  23183. (let (vl new last)
  23184. (while info
  23185. (setq last new new (pop info))
  23186. (if (or (memq last '(:end :startend))
  23187. (memq new '(:start :startend)))
  23188. (push t vl)
  23189. (push nil vl)))
  23190. (setq vl (nreverse vl))
  23191. (and vl (setcar vl nil))
  23192. vl))
  23193. (defun org-format-table-html (lines olines)
  23194. "Find out which HTML converter to use and return the HTML code."
  23195. (if (stringp lines)
  23196. (setq lines (org-split-string lines "\n")))
  23197. (if (string-match "^[ \t]*|" (car lines))
  23198. ;; A normal org table
  23199. (org-format-org-table-html lines)
  23200. ;; Table made by table.el - test for spanning
  23201. (let* ((hlines (delq nil (mapcar
  23202. (lambda (x)
  23203. (if (string-match "^[ \t]*\\+-" x) x
  23204. nil))
  23205. lines)))
  23206. (first (car hlines))
  23207. (ll (and (string-match "\\S-+" first)
  23208. (match-string 0 first)))
  23209. (re (concat "^[ \t]*" (regexp-quote ll)))
  23210. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23211. hlines))))
  23212. (if (and (not spanning)
  23213. (not org-export-prefer-native-exporter-for-tables))
  23214. ;; We can use my own converter with HTML conversions
  23215. (org-format-table-table-html lines)
  23216. ;; Need to use the code generator in table.el, with the original text.
  23217. (org-format-table-table-html-using-table-generate-source olines)))))
  23218. (defun org-format-org-table-html (lines &optional splice)
  23219. "Format a table into HTML."
  23220. ;; Get rid of hlines at beginning and end
  23221. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23222. (setq lines (nreverse lines))
  23223. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23224. (setq lines (nreverse lines))
  23225. (when org-export-table-remove-special-lines
  23226. ;; Check if the table has a marking column. If yes remove the
  23227. ;; column and the special lines
  23228. (setq lines (org-table-clean-before-export lines)))
  23229. (let ((head (and org-export-highlight-first-table-line
  23230. (delq nil (mapcar
  23231. (lambda (x) (string-match "^[ \t]*|-" x))
  23232. (cdr lines)))))
  23233. (nlines 0) fnum i
  23234. tbopen line fields html gr colgropen)
  23235. (if splice (setq head nil))
  23236. (unless splice (push (if head "<thead>" "<tbody>") html))
  23237. (setq tbopen t)
  23238. (while (setq line (pop lines))
  23239. (catch 'next-line
  23240. (if (string-match "^[ \t]*|-" line)
  23241. (progn
  23242. (unless splice
  23243. (push (if head "</thead>" "</tbody>") html)
  23244. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23245. (setq head nil) ;; head ends here, first time around
  23246. ;; ignore this line
  23247. (throw 'next-line t)))
  23248. ;; Break the line into fields
  23249. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23250. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23251. (setq nlines (1+ nlines) i -1)
  23252. (push (concat "<tr>"
  23253. (mapconcat
  23254. (lambda (x)
  23255. (setq i (1+ i))
  23256. (if (and (< i nlines)
  23257. (string-match org-table-number-regexp x))
  23258. (incf (aref fnum i)))
  23259. (if head
  23260. (concat (car org-export-table-header-tags) x
  23261. (cdr org-export-table-header-tags))
  23262. (concat (car org-export-table-data-tags) x
  23263. (cdr org-export-table-data-tags))))
  23264. fields "")
  23265. "</tr>")
  23266. html)))
  23267. (unless splice (if tbopen (push "</tbody>" html)))
  23268. (unless splice (push "</table>\n" html))
  23269. (setq html (nreverse html))
  23270. (unless splice
  23271. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23272. (push (mapconcat
  23273. (lambda (x)
  23274. (setq gr (pop org-table-colgroup-info))
  23275. (format "%s<col align=\"%s\"></col>%s"
  23276. (if (memq gr '(:start :startend))
  23277. (prog1
  23278. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23279. (setq colgropen t))
  23280. "")
  23281. (if (> (/ (float x) nlines) org-table-number-fraction)
  23282. "right" "left")
  23283. (if (memq gr '(:end :startend))
  23284. (progn (setq colgropen nil) "</colgroup>")
  23285. "")))
  23286. fnum "")
  23287. html)
  23288. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23289. (push html-table-tag html))
  23290. (concat (mapconcat 'identity html "\n") "\n")))
  23291. (defun org-table-clean-before-export (lines)
  23292. "Check if the table has a marking column.
  23293. If yes remove the column and the special lines."
  23294. (setq org-table-colgroup-info nil)
  23295. (if (memq nil
  23296. (mapcar
  23297. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23298. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23299. lines))
  23300. (progn
  23301. (setq org-table-clean-did-remove-column nil)
  23302. (delq nil
  23303. (mapcar
  23304. (lambda (x)
  23305. (cond
  23306. ((string-match "^[ \t]*| */ *|" x)
  23307. (setq org-table-colgroup-info
  23308. (mapcar (lambda (x)
  23309. (cond ((member x '("<" "&lt;")) :start)
  23310. ((member x '(">" "&gt;")) :end)
  23311. ((member x '("<>" "&lt;&gt;")) :startend)
  23312. (t nil)))
  23313. (org-split-string x "[ \t]*|[ \t]*")))
  23314. nil)
  23315. (t x)))
  23316. lines)))
  23317. (setq org-table-clean-did-remove-column t)
  23318. (delq nil
  23319. (mapcar
  23320. (lambda (x)
  23321. (cond
  23322. ((string-match "^[ \t]*| */ *|" x)
  23323. (setq org-table-colgroup-info
  23324. (mapcar (lambda (x)
  23325. (cond ((member x '("<" "&lt;")) :start)
  23326. ((member x '(">" "&gt;")) :end)
  23327. ((member x '("<>" "&lt;&gt;")) :startend)
  23328. (t nil)))
  23329. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23330. nil)
  23331. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23332. nil) ; ignore this line
  23333. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23334. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23335. ;; remove the first column
  23336. (replace-match "\\1|" t nil x))))
  23337. lines))))
  23338. (defun org-format-table-table-html (lines)
  23339. "Format a table generated by table.el into HTML.
  23340. This conversion does *not* use `table-generate-source' from table.el.
  23341. This has the advantage that Org-mode's HTML conversions can be used.
  23342. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23343. (let (line field-buffer
  23344. (head org-export-highlight-first-table-line)
  23345. fields html empty)
  23346. (setq html (concat html-table-tag "\n"))
  23347. (while (setq line (pop lines))
  23348. (setq empty "&nbsp;")
  23349. (catch 'next-line
  23350. (if (string-match "^[ \t]*\\+-" line)
  23351. (progn
  23352. (if field-buffer
  23353. (progn
  23354. (setq
  23355. html
  23356. (concat
  23357. html
  23358. "<tr>"
  23359. (mapconcat
  23360. (lambda (x)
  23361. (if (equal x "") (setq x empty))
  23362. (if head
  23363. (concat (car org-export-table-header-tags) x
  23364. (cdr org-export-table-header-tags))
  23365. (concat (car org-export-table-data-tags) x
  23366. (cdr org-export-table-data-tags))))
  23367. field-buffer "\n")
  23368. "</tr>\n"))
  23369. (setq head nil)
  23370. (setq field-buffer nil)))
  23371. ;; Ignore this line
  23372. (throw 'next-line t)))
  23373. ;; Break the line into fields and store the fields
  23374. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23375. (if field-buffer
  23376. (setq field-buffer (mapcar
  23377. (lambda (x)
  23378. (concat x "<br/>" (pop fields)))
  23379. field-buffer))
  23380. (setq field-buffer fields))))
  23381. (setq html (concat html "</table>\n"))
  23382. html))
  23383. (defun org-format-table-table-html-using-table-generate-source (lines)
  23384. "Format a table into html, using `table-generate-source' from table.el.
  23385. This has the advantage that cell- or row-spanning is allowed.
  23386. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23387. (require 'table)
  23388. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23389. (erase-buffer)
  23390. (insert (mapconcat 'identity lines "\n"))
  23391. (goto-char (point-min))
  23392. (if (not (re-search-forward "|[^+]" nil t))
  23393. (error "Error processing table"))
  23394. (table-recognize-table)
  23395. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23396. (table-generate-source 'html " org-tmp2 ")
  23397. (set-buffer " org-tmp2 ")
  23398. (buffer-substring (point-min) (point-max))))
  23399. (defun org-html-handle-time-stamps (s)
  23400. "Format time stamps in string S, or remove them."
  23401. (catch 'exit
  23402. (let (r b)
  23403. (while (string-match org-maybe-keyword-time-regexp s)
  23404. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23405. ;; never export CLOCK
  23406. (throw 'exit ""))
  23407. (or b (setq b (substring s 0 (match-beginning 0))))
  23408. (if (not org-export-with-timestamps)
  23409. (setq r (concat r (substring s 0 (match-beginning 0)))
  23410. s (substring s (match-end 0)))
  23411. (setq r (concat
  23412. r (substring s 0 (match-beginning 0))
  23413. (if (match-end 1)
  23414. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23415. (match-string 1 s)))
  23416. (format " @<span class=\"timestamp\">%s@</span>"
  23417. (substring
  23418. (org-translate-time (match-string 3 s)) 1 -1)))
  23419. s (substring s (match-end 0)))))
  23420. ;; Line break if line started and ended with time stamp stuff
  23421. (if (not r)
  23422. s
  23423. (setq r (concat r s))
  23424. (unless (string-match "\\S-" (concat b s))
  23425. (setq r (concat r "@<br/>")))
  23426. r))))
  23427. (defun org-html-protect (s)
  23428. ;; convert & to &amp;, < to &lt; and > to &gt;
  23429. (let ((start 0))
  23430. (while (string-match "&" s start)
  23431. (setq s (replace-match "&amp;" t t s)
  23432. start (1+ (match-beginning 0))))
  23433. (while (string-match "<" s)
  23434. (setq s (replace-match "&lt;" t t s)))
  23435. (while (string-match ">" s)
  23436. (setq s (replace-match "&gt;" t t s))))
  23437. s)
  23438. (defun org-export-cleanup-toc-line (s)
  23439. "Remove tags and time staps from lines going into the toc."
  23440. (when (memq org-export-with-tags '(not-in-toc nil))
  23441. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23442. (setq s (replace-match "" t t s))))
  23443. (when org-export-remove-timestamps-from-toc
  23444. (while (string-match org-maybe-keyword-time-regexp s)
  23445. (setq s (replace-match "" t t s))))
  23446. (while (string-match org-bracket-link-regexp s)
  23447. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23448. t t s)))
  23449. s)
  23450. (defun org-html-expand (string)
  23451. "Prepare STRING for HTML export. Applies all active conversions.
  23452. If there are links in the string, don't modify these."
  23453. (let* ((re (concat org-bracket-link-regexp "\\|"
  23454. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23455. m s l res)
  23456. (while (setq m (string-match re string))
  23457. (setq s (substring string 0 m)
  23458. l (match-string 0 string)
  23459. string (substring string (match-end 0)))
  23460. (push (org-html-do-expand s) res)
  23461. (push l res))
  23462. (push (org-html-do-expand string) res)
  23463. (apply 'concat (nreverse res))))
  23464. (defun org-html-do-expand (s)
  23465. "Apply all active conversions to translate special ASCII to HTML."
  23466. (setq s (org-html-protect s))
  23467. (if org-export-html-expand
  23468. (let ((start 0))
  23469. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23470. (setq s (replace-match "<\\1>" t nil s)))))
  23471. (if org-export-with-emphasize
  23472. (setq s (org-export-html-convert-emphasize s)))
  23473. (if org-export-with-special-strings
  23474. (setq s (org-export-html-convert-special-strings s)))
  23475. (if org-export-with-sub-superscripts
  23476. (setq s (org-export-html-convert-sub-super s)))
  23477. (if org-export-with-TeX-macros
  23478. (let ((start 0) wd ass)
  23479. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23480. (if (get-text-property (match-beginning 0) 'org-protected s)
  23481. (setq start (match-end 0))
  23482. (setq wd (match-string 1 s))
  23483. (if (setq ass (assoc wd org-html-entities))
  23484. (setq s (replace-match (or (cdr ass)
  23485. (concat "&" (car ass) ";"))
  23486. t t s))
  23487. (setq start (+ start (length wd))))))))
  23488. s)
  23489. (defun org-create-multibrace-regexp (left right n)
  23490. "Create a regular expression which will match a balanced sexp.
  23491. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23492. as single character strings.
  23493. The regexp returned will match the entire expression including the
  23494. delimiters. It will also define a single group which contains the
  23495. match except for the outermost delimiters. The maximum depth of
  23496. stacked delimiters is N. Escaping delimiters is not possible."
  23497. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23498. (or "\\|")
  23499. (re nothing)
  23500. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23501. (while (> n 1)
  23502. (setq n (1- n)
  23503. re (concat re or next)
  23504. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23505. (concat left "\\(" re "\\)" right)))
  23506. (defvar org-match-substring-regexp
  23507. (concat
  23508. "\\([^\\]\\)\\([_^]\\)\\("
  23509. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23510. "\\|"
  23511. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23512. "\\|"
  23513. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23514. "The regular expression matching a sub- or superscript.")
  23515. (defvar org-match-substring-with-braces-regexp
  23516. (concat
  23517. "\\([^\\]\\)\\([_^]\\)\\("
  23518. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23519. "\\)")
  23520. "The regular expression matching a sub- or superscript, forcing braces.")
  23521. (defconst org-export-html-special-string-regexps
  23522. '(("\\\\-" . "&shy;")
  23523. ("---\\([^-]\\)" . "&mdash;\\1")
  23524. ("--\\([^-]\\)" . "&ndash;\\1")
  23525. ("\\.\\.\\." . "&hellip;"))
  23526. "Regular expressions for special string conversion.")
  23527. (defun org-export-html-convert-special-strings (string)
  23528. "Convert special characters in STRING to HTML."
  23529. (let ((all org-export-html-special-string-regexps)
  23530. e a re rpl start)
  23531. (while (setq a (pop all))
  23532. (setq re (car a) rpl (cdr a) start 0)
  23533. (while (string-match re string start)
  23534. (if (get-text-property (match-beginning 0) 'org-protected string)
  23535. (setq start (match-end 0))
  23536. (setq string (replace-match rpl t nil string)))))
  23537. string))
  23538. (defun org-export-html-convert-sub-super (string)
  23539. "Convert sub- and superscripts in STRING to HTML."
  23540. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  23541. (while (string-match org-match-substring-regexp string s)
  23542. (cond
  23543. ((and requireb (match-end 8)) (setq s (match-end 2)))
  23544. ((get-text-property (match-beginning 2) 'org-protected string)
  23545. (setq s (match-end 2)))
  23546. (t
  23547. (setq s (match-end 1)
  23548. key (if (string= (match-string 2 string) "_") "sub" "sup")
  23549. c (or (match-string 8 string)
  23550. (match-string 6 string)
  23551. (match-string 5 string))
  23552. string (replace-match
  23553. (concat (match-string 1 string)
  23554. "<" key ">" c "</" key ">")
  23555. t t string)))))
  23556. (while (string-match "\\\\\\([_^]\\)" string)
  23557. (setq string (replace-match (match-string 1 string) t t string)))
  23558. string))
  23559. (defun org-export-html-convert-emphasize (string)
  23560. "Apply emphasis."
  23561. (let ((s 0) rpl)
  23562. (while (string-match org-emph-re string s)
  23563. (if (not (equal
  23564. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  23565. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  23566. (setq s (match-beginning 0)
  23567. rpl
  23568. (concat
  23569. (match-string 1 string)
  23570. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  23571. (match-string 4 string)
  23572. (nth 3 (assoc (match-string 3 string)
  23573. org-emphasis-alist))
  23574. (match-string 5 string))
  23575. string (replace-match rpl t t string)
  23576. s (+ s (- (length rpl) 2)))
  23577. (setq s (1+ s))))
  23578. string))
  23579. (defvar org-par-open nil)
  23580. (defun org-open-par ()
  23581. "Insert <p>, but first close previous paragraph if any."
  23582. (org-close-par-maybe)
  23583. (insert "\n<p>")
  23584. (setq org-par-open t))
  23585. (defun org-close-par-maybe ()
  23586. "Close paragraph if there is one open."
  23587. (when org-par-open
  23588. (insert "</p>")
  23589. (setq org-par-open nil)))
  23590. (defun org-close-li ()
  23591. "Close <li> if necessary."
  23592. (org-close-par-maybe)
  23593. (insert "</li>\n"))
  23594. (defvar body-only) ; dynamically scoped into this.
  23595. (defun org-html-level-start (level title umax with-toc head-count)
  23596. "Insert a new level in HTML export.
  23597. When TITLE is nil, just close all open levels."
  23598. (org-close-par-maybe)
  23599. (let ((l org-level-max))
  23600. (while (>= l level)
  23601. (if (aref org-levels-open (1- l))
  23602. (progn
  23603. (org-html-level-close l umax)
  23604. (aset org-levels-open (1- l) nil)))
  23605. (setq l (1- l)))
  23606. (when title
  23607. ;; If title is nil, this means this function is called to close
  23608. ;; all levels, so the rest is done only if title is given
  23609. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23610. (setq title (replace-match
  23611. (if org-export-with-tags
  23612. (save-match-data
  23613. (concat
  23614. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  23615. (mapconcat 'identity (org-split-string
  23616. (match-string 1 title) ":")
  23617. "&nbsp;")
  23618. "</span>"))
  23619. "")
  23620. t t title)))
  23621. (if (> level umax)
  23622. (progn
  23623. (if (aref org-levels-open (1- level))
  23624. (progn
  23625. (org-close-li)
  23626. (insert "<li>" title "<br/>\n"))
  23627. (aset org-levels-open (1- level) t)
  23628. (org-close-par-maybe)
  23629. (insert "<ul>\n<li>" title "<br/>\n")))
  23630. (aset org-levels-open (1- level) t)
  23631. (if (and org-export-with-section-numbers (not body-only))
  23632. (setq title (concat (org-section-number level) " " title)))
  23633. (setq level (+ level org-export-html-toplevel-hlevel -1))
  23634. (if with-toc
  23635. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  23636. level level head-count title level))
  23637. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  23638. (org-open-par)))))
  23639. (defun org-html-level-close (level max-outline-level)
  23640. "Terminate one level in HTML export."
  23641. (if (<= level max-outline-level)
  23642. (insert "</div>\n")
  23643. (org-close-li)
  23644. (insert "</ul>\n")))
  23645. ;;; iCalendar export
  23646. ;;;###autoload
  23647. (defun org-export-icalendar-this-file ()
  23648. "Export current file as an iCalendar file.
  23649. The iCalendar file will be located in the same directory as the Org-mode
  23650. file, but with extension `.ics'."
  23651. (interactive)
  23652. (org-export-icalendar nil buffer-file-name))
  23653. ;;;###autoload
  23654. (defun org-export-icalendar-all-agenda-files ()
  23655. "Export all files in `org-agenda-files' to iCalendar .ics files.
  23656. Each iCalendar file will be located in the same directory as the Org-mode
  23657. file, but with extension `.ics'."
  23658. (interactive)
  23659. (apply 'org-export-icalendar nil (org-agenda-files t)))
  23660. ;;;###autoload
  23661. (defun org-export-icalendar-combine-agenda-files ()
  23662. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  23663. The file is stored under the name `org-combined-agenda-icalendar-file'."
  23664. (interactive)
  23665. (apply 'org-export-icalendar t (org-agenda-files t)))
  23666. (defun org-export-icalendar (combine &rest files)
  23667. "Create iCalendar files for all elements of FILES.
  23668. If COMBINE is non-nil, combine all calendar entries into a single large
  23669. file and store it under the name `org-combined-agenda-icalendar-file'."
  23670. (save-excursion
  23671. (org-prepare-agenda-buffers files)
  23672. (let* ((dir (org-export-directory
  23673. :ical (list :publishing-directory
  23674. org-export-publishing-directory)))
  23675. file ical-file ical-buffer category started org-agenda-new-buffers)
  23676. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  23677. (when combine
  23678. (setq ical-file
  23679. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  23680. org-combined-agenda-icalendar-file
  23681. (expand-file-name org-combined-agenda-icalendar-file dir))
  23682. ical-buffer (org-get-agenda-file-buffer ical-file))
  23683. (set-buffer ical-buffer) (erase-buffer))
  23684. (while (setq file (pop files))
  23685. (catch 'nextfile
  23686. (org-check-agenda-file file)
  23687. (set-buffer (org-get-agenda-file-buffer file))
  23688. (unless combine
  23689. (setq ical-file (concat (file-name-as-directory dir)
  23690. (file-name-sans-extension
  23691. (file-name-nondirectory buffer-file-name))
  23692. ".ics"))
  23693. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  23694. (with-current-buffer ical-buffer (erase-buffer)))
  23695. (setq category (or org-category
  23696. (file-name-sans-extension
  23697. (file-name-nondirectory buffer-file-name))))
  23698. (if (symbolp category) (setq category (symbol-name category)))
  23699. (let ((standard-output ical-buffer))
  23700. (if combine
  23701. (and (not started) (setq started t)
  23702. (org-start-icalendar-file org-icalendar-combined-name))
  23703. (org-start-icalendar-file category))
  23704. (org-print-icalendar-entries combine)
  23705. (when (or (and combine (not files)) (not combine))
  23706. (org-finish-icalendar-file)
  23707. (set-buffer ical-buffer)
  23708. (save-buffer)
  23709. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  23710. (org-release-buffers org-agenda-new-buffers))))
  23711. (defvar org-after-save-iCalendar-file-hook nil
  23712. "Hook run after an iCalendar file has been saved.
  23713. The iCalendar buffer is still current when this hook is run.
  23714. A good way to use this is to tell a desktop calenndar application to re-read
  23715. the iCalendar file.")
  23716. (defun org-print-icalendar-entries (&optional combine)
  23717. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  23718. When COMBINE is non nil, add the category to each line."
  23719. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  23720. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  23721. (dts (org-ical-ts-to-string
  23722. (format-time-string (cdr org-time-stamp-formats) (current-time))
  23723. "DTSTART"))
  23724. hd ts ts2 state status (inc t) pos b sexp rrule
  23725. scheduledp deadlinep tmp pri category entry location summary desc
  23726. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  23727. (org-refresh-category-properties)
  23728. (save-excursion
  23729. (goto-char (point-min))
  23730. (while (re-search-forward re1 nil t)
  23731. (catch :skip
  23732. (org-agenda-skip)
  23733. (setq pos (match-beginning 0)
  23734. ts (match-string 0)
  23735. inc t
  23736. hd (org-get-heading)
  23737. summary (org-icalendar-cleanup-string
  23738. (org-entry-get nil "SUMMARY"))
  23739. desc (org-icalendar-cleanup-string
  23740. (or (org-entry-get nil "DESCRIPTION")
  23741. (and org-icalendar-include-body (org-get-entry)))
  23742. t org-icalendar-include-body)
  23743. location (org-icalendar-cleanup-string
  23744. (org-entry-get nil "LOCATION"))
  23745. category (org-get-category))
  23746. (if (looking-at re2)
  23747. (progn
  23748. (goto-char (match-end 0))
  23749. (setq ts2 (match-string 1) inc nil))
  23750. (setq tmp (buffer-substring (max (point-min)
  23751. (- pos org-ds-keyword-length))
  23752. pos)
  23753. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  23754. (progn
  23755. (setq inc nil)
  23756. (replace-match "\\1" t nil ts))
  23757. ts)
  23758. deadlinep (string-match org-deadline-regexp tmp)
  23759. scheduledp (string-match org-scheduled-regexp tmp)
  23760. ;; donep (org-entry-is-done-p)
  23761. ))
  23762. (if (or (string-match org-tr-regexp hd)
  23763. (string-match org-ts-regexp hd))
  23764. (setq hd (replace-match "" t t hd)))
  23765. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  23766. (setq rrule
  23767. (concat "\nRRULE:FREQ="
  23768. (cdr (assoc
  23769. (match-string 2 ts)
  23770. '(("d" . "DAILY")("w" . "WEEKLY")
  23771. ("m" . "MONTHLY")("y" . "YEARLY"))))
  23772. ";INTERVAL=" (match-string 1 ts)))
  23773. (setq rrule ""))
  23774. (setq summary (or summary hd))
  23775. (if (string-match org-bracket-link-regexp summary)
  23776. (setq summary
  23777. (replace-match (if (match-end 3)
  23778. (match-string 3 summary)
  23779. (match-string 1 summary))
  23780. t t summary)))
  23781. (if deadlinep (setq summary (concat "DL: " summary)))
  23782. (if scheduledp (setq summary (concat "S: " summary)))
  23783. (if (string-match "\\`<%%" ts)
  23784. (with-current-buffer sexp-buffer
  23785. (insert (substring ts 1 -1) " " summary "\n"))
  23786. (princ (format "BEGIN:VEVENT
  23787. %s
  23788. %s%s
  23789. SUMMARY:%s%s%s
  23790. CATEGORIES:%s
  23791. END:VEVENT\n"
  23792. (org-ical-ts-to-string ts "DTSTART")
  23793. (org-ical-ts-to-string ts2 "DTEND" inc)
  23794. rrule summary
  23795. (if (and desc (string-match "\\S-" desc))
  23796. (concat "\nDESCRIPTION: " desc) "")
  23797. (if (and location (string-match "\\S-" location))
  23798. (concat "\nLOCATION: " location) "")
  23799. category)))))
  23800. (when (and org-icalendar-include-sexps
  23801. (condition-case nil (require 'icalendar) (error nil))
  23802. (fboundp 'icalendar-export-region))
  23803. ;; Get all the literal sexps
  23804. (goto-char (point-min))
  23805. (while (re-search-forward "^&?%%(" nil t)
  23806. (catch :skip
  23807. (org-agenda-skip)
  23808. (setq b (match-beginning 0))
  23809. (goto-char (1- (match-end 0)))
  23810. (forward-sexp 1)
  23811. (end-of-line 1)
  23812. (setq sexp (buffer-substring b (point)))
  23813. (with-current-buffer sexp-buffer
  23814. (insert sexp "\n"))
  23815. (princ (org-diary-to-ical-string sexp-buffer)))))
  23816. (when org-icalendar-include-todo
  23817. (goto-char (point-min))
  23818. (while (re-search-forward org-todo-line-regexp nil t)
  23819. (catch :skip
  23820. (org-agenda-skip)
  23821. (setq state (match-string 2))
  23822. (setq status (if (member state org-done-keywords)
  23823. "COMPLETED" "NEEDS-ACTION"))
  23824. (when (and state
  23825. (or (not (member state org-done-keywords))
  23826. (eq org-icalendar-include-todo 'all))
  23827. (not (member org-archive-tag (org-get-tags-at)))
  23828. )
  23829. (setq hd (match-string 3)
  23830. summary (org-icalendar-cleanup-string
  23831. (org-entry-get nil "SUMMARY"))
  23832. desc (org-icalendar-cleanup-string
  23833. (or (org-entry-get nil "DESCRIPTION")
  23834. (and org-icalendar-include-body (org-get-entry)))
  23835. t org-icalendar-include-body)
  23836. location (org-icalendar-cleanup-string
  23837. (org-entry-get nil "LOCATION")))
  23838. (if (string-match org-bracket-link-regexp hd)
  23839. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  23840. (match-string 1 hd))
  23841. t t hd)))
  23842. (if (string-match org-priority-regexp hd)
  23843. (setq pri (string-to-char (match-string 2 hd))
  23844. hd (concat (substring hd 0 (match-beginning 1))
  23845. (substring hd (match-end 1))))
  23846. (setq pri org-default-priority))
  23847. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  23848. (- org-lowest-priority org-highest-priority))))))
  23849. (princ (format "BEGIN:VTODO
  23850. %s
  23851. SUMMARY:%s%s%s
  23852. CATEGORIES:%s
  23853. SEQUENCE:1
  23854. PRIORITY:%d
  23855. STATUS:%s
  23856. END:VTODO\n"
  23857. dts
  23858. (or summary hd)
  23859. (if (and location (string-match "\\S-" location))
  23860. (concat "\nLOCATION: " location) "")
  23861. (if (and desc (string-match "\\S-" desc))
  23862. (concat "\nDESCRIPTION: " desc) "")
  23863. category pri status)))))))))
  23864. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  23865. "Take out stuff and quote what needs to be quoted.
  23866. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  23867. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  23868. characters."
  23869. (if (not s)
  23870. nil
  23871. (when is-body
  23872. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  23873. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  23874. (while (string-match re s) (setq s (replace-match "" t t s)))
  23875. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  23876. (let ((start 0))
  23877. (while (string-match "\\([,;\\]\\)" s start)
  23878. (setq start (+ (match-beginning 0) 2)
  23879. s (replace-match "\\\\\\1" nil nil s))))
  23880. (when is-body
  23881. (while (string-match "[ \t]*\n[ \t]*" s)
  23882. (setq s (replace-match "\\n" t t s))))
  23883. (setq s (org-trim s))
  23884. (if is-body
  23885. (if maxlength
  23886. (if (and (numberp maxlength)
  23887. (> (length s) maxlength))
  23888. (setq s (substring s 0 maxlength)))))
  23889. s))
  23890. (defun org-get-entry ()
  23891. "Clean-up description string."
  23892. (save-excursion
  23893. (org-back-to-heading t)
  23894. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  23895. (defun org-start-icalendar-file (name)
  23896. "Start an iCalendar file by inserting the header."
  23897. (let ((user user-full-name)
  23898. (name (or name "unknown"))
  23899. (timezone (cadr (current-time-zone))))
  23900. (princ
  23901. (format "BEGIN:VCALENDAR
  23902. VERSION:2.0
  23903. X-WR-CALNAME:%s
  23904. PRODID:-//%s//Emacs with Org-mode//EN
  23905. X-WR-TIMEZONE:%s
  23906. CALSCALE:GREGORIAN\n" name user timezone))))
  23907. (defun org-finish-icalendar-file ()
  23908. "Finish an iCalendar file by inserting the END statement."
  23909. (princ "END:VCALENDAR\n"))
  23910. (defun org-ical-ts-to-string (s keyword &optional inc)
  23911. "Take a time string S and convert it to iCalendar format.
  23912. KEYWORD is added in front, to make a complete line like DTSTART....
  23913. When INC is non-nil, increase the hour by two (if time string contains
  23914. a time), or the day by one (if it does not contain a time)."
  23915. (let ((t1 (org-parse-time-string s 'nodefault))
  23916. t2 fmt have-time time)
  23917. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  23918. (setq t2 t1 have-time t)
  23919. (setq t2 (org-parse-time-string s)))
  23920. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  23921. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  23922. (when inc
  23923. (if have-time
  23924. (if org-agenda-default-appointment-duration
  23925. (setq mi (+ org-agenda-default-appointment-duration mi))
  23926. (setq h (+ 2 h)))
  23927. (setq d (1+ d))))
  23928. (setq time (encode-time s mi h d m y)))
  23929. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  23930. (concat keyword (format-time-string fmt time))))
  23931. ;;; XOXO export
  23932. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  23933. (with-current-buffer buffer
  23934. (apply 'insert output)))
  23935. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  23936. (defun org-export-as-xoxo (&optional buffer)
  23937. "Export the org buffer as XOXO.
  23938. The XOXO buffer is named *xoxo-<source buffer name>*"
  23939. (interactive (list (current-buffer)))
  23940. ;; A quickie abstraction
  23941. ;; Output everything as XOXO
  23942. (with-current-buffer (get-buffer buffer)
  23943. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  23944. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23945. (org-infile-export-plist)))
  23946. (filename (concat (file-name-as-directory
  23947. (org-export-directory :xoxo opt-plist))
  23948. (file-name-sans-extension
  23949. (file-name-nondirectory buffer-file-name))
  23950. ".html"))
  23951. (out (find-file-noselect filename))
  23952. (last-level 1)
  23953. (hanging-li nil))
  23954. ;; Check the output buffer is empty.
  23955. (with-current-buffer out (erase-buffer))
  23956. ;; Kick off the output
  23957. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  23958. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  23959. (let* ((hd (match-string-no-properties 1))
  23960. (level (length hd))
  23961. (text (concat
  23962. (match-string-no-properties 2)
  23963. (save-excursion
  23964. (goto-char (match-end 0))
  23965. (let ((str ""))
  23966. (catch 'loop
  23967. (while 't
  23968. (forward-line)
  23969. (if (looking-at "^[ \t]\\(.*\\)")
  23970. (setq str (concat str (match-string-no-properties 1)))
  23971. (throw 'loop str)))))))))
  23972. ;; Handle level rendering
  23973. (cond
  23974. ((> level last-level)
  23975. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  23976. ((< level last-level)
  23977. (dotimes (- (- last-level level) 1)
  23978. (if hanging-li
  23979. (org-export-as-xoxo-insert-into out "</li>\n"))
  23980. (org-export-as-xoxo-insert-into out "</ol>\n"))
  23981. (when hanging-li
  23982. (org-export-as-xoxo-insert-into out "</li>\n")
  23983. (setq hanging-li nil)))
  23984. ((equal level last-level)
  23985. (if hanging-li
  23986. (org-export-as-xoxo-insert-into out "</li>\n")))
  23987. )
  23988. (setq last-level level)
  23989. ;; And output the new li
  23990. (setq hanging-li 't)
  23991. (if (equal ?+ (elt text 0))
  23992. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  23993. (org-export-as-xoxo-insert-into out "<li>" text))))
  23994. ;; Finally finish off the ol
  23995. (dotimes (- last-level 1)
  23996. (if hanging-li
  23997. (org-export-as-xoxo-insert-into out "</li>\n"))
  23998. (org-export-as-xoxo-insert-into out "</ol>\n"))
  23999. ;; Finish the buffer off and clean it up.
  24000. (switch-to-buffer-other-window out)
  24001. (indent-region (point-min) (point-max) nil)
  24002. (save-buffer)
  24003. (goto-char (point-min))
  24004. )))
  24005. ;;;; Key bindings
  24006. ;; Make `C-c C-x' a prefix key
  24007. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24008. ;; TAB key with modifiers
  24009. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24010. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24011. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24012. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24013. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24014. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24015. ;; The following line is necessary under Suse GNU/Linux
  24016. (unless (featurep 'xemacs)
  24017. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24018. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24019. (define-key org-mode-map [backtab] 'org-shifttab)
  24020. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24021. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24022. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24023. ;; Cursor keys with modifiers
  24024. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24025. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24026. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24027. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24028. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24029. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24030. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24031. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24032. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24033. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24034. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24035. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24036. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24037. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24038. ;;; Extra keys for tty access.
  24039. ;; We only set them when really needed because otherwise the
  24040. ;; menus don't show the simple keys
  24041. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24042. (not window-system))
  24043. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24044. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24045. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24046. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24047. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24048. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24049. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24050. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24051. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24052. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24053. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24054. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24055. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24056. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24057. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24058. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24059. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24060. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24061. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24062. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24063. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24064. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24065. ;; All the other keys
  24066. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24067. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24068. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24069. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24070. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24071. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24072. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24073. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24074. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24075. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24076. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24077. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24078. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24079. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24080. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24081. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24082. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24083. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24084. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24085. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24086. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24087. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24088. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24089. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24090. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24091. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24092. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24093. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24094. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24095. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24096. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24097. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24098. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24099. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24100. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24101. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24102. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24103. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24104. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24105. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24106. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24107. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24108. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24109. (org-defkey org-mode-map "\C-m" 'org-return)
  24110. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24111. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24112. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24113. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24114. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24115. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24116. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24117. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24118. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24119. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24120. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24121. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24122. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24123. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24124. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24125. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24126. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24127. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24128. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24129. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24130. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24131. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24132. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24133. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24134. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24135. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24136. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24137. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24138. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24139. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24140. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24141. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24142. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24143. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24144. (when (featurep 'xemacs)
  24145. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24146. (defsubst org-table-p () (org-at-table-p))
  24147. (defun org-self-insert-command (N)
  24148. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24149. If the cursor is in a table looking at whitespace, the whitespace is
  24150. overwritten, and the table is not marked as requiring realignment."
  24151. (interactive "p")
  24152. (if (and (org-table-p)
  24153. (progn
  24154. ;; check if we blank the field, and if that triggers align
  24155. (and org-table-auto-blank-field
  24156. (member last-command
  24157. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24158. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24159. ;; got extra space, this field does not determine column width
  24160. (let (org-table-may-need-update) (org-table-blank-field))
  24161. ;; no extra space, this field may determine column width
  24162. (org-table-blank-field)))
  24163. t)
  24164. (eq N 1)
  24165. (looking-at "[^|\n]* |"))
  24166. (let (org-table-may-need-update)
  24167. (goto-char (1- (match-end 0)))
  24168. (delete-backward-char 1)
  24169. (goto-char (match-beginning 0))
  24170. (self-insert-command N))
  24171. (setq org-table-may-need-update t)
  24172. (self-insert-command N)
  24173. (org-fix-tags-on-the-fly)))
  24174. (defun org-fix-tags-on-the-fly ()
  24175. (when (and (equal (char-after (point-at-bol)) ?*)
  24176. (org-on-heading-p))
  24177. (org-align-tags-here org-tags-column)))
  24178. (defun org-delete-backward-char (N)
  24179. "Like `delete-backward-char', insert whitespace at field end in tables.
  24180. When deleting backwards, in tables this function will insert whitespace in
  24181. front of the next \"|\" separator, to keep the table aligned. The table will
  24182. still be marked for re-alignment if the field did fill the entire column,
  24183. because, in this case the deletion might narrow the column."
  24184. (interactive "p")
  24185. (if (and (org-table-p)
  24186. (eq N 1)
  24187. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24188. (looking-at ".*?|"))
  24189. (let ((pos (point))
  24190. (noalign (looking-at "[^|\n\r]* |"))
  24191. (c org-table-may-need-update))
  24192. (backward-delete-char N)
  24193. (skip-chars-forward "^|")
  24194. (insert " ")
  24195. (goto-char (1- pos))
  24196. ;; noalign: if there were two spaces at the end, this field
  24197. ;; does not determine the width of the column.
  24198. (if noalign (setq org-table-may-need-update c)))
  24199. (backward-delete-char N)
  24200. (org-fix-tags-on-the-fly)))
  24201. (defun org-delete-char (N)
  24202. "Like `delete-char', but insert whitespace at field end in tables.
  24203. When deleting characters, in tables this function will insert whitespace in
  24204. front of the next \"|\" separator, to keep the table aligned. The table will
  24205. still be marked for re-alignment if the field did fill the entire column,
  24206. because, in this case the deletion might narrow the column."
  24207. (interactive "p")
  24208. (if (and (org-table-p)
  24209. (not (bolp))
  24210. (not (= (char-after) ?|))
  24211. (eq N 1))
  24212. (if (looking-at ".*?|")
  24213. (let ((pos (point))
  24214. (noalign (looking-at "[^|\n\r]* |"))
  24215. (c org-table-may-need-update))
  24216. (replace-match (concat
  24217. (substring (match-string 0) 1 -1)
  24218. " |"))
  24219. (goto-char pos)
  24220. ;; noalign: if there were two spaces at the end, this field
  24221. ;; does not determine the width of the column.
  24222. (if noalign (setq org-table-may-need-update c)))
  24223. (delete-char N))
  24224. (delete-char N)
  24225. (org-fix-tags-on-the-fly)))
  24226. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24227. (put 'org-self-insert-command 'delete-selection t)
  24228. (put 'orgtbl-self-insert-command 'delete-selection t)
  24229. (put 'org-delete-char 'delete-selection 'supersede)
  24230. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24231. ;; Make `flyspell-mode' delay after some commands
  24232. (put 'org-self-insert-command 'flyspell-delayed t)
  24233. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24234. (put 'org-delete-char 'flyspell-delayed t)
  24235. (put 'org-delete-backward-char 'flyspell-delayed t)
  24236. ;; Make pabbrev-mode expand after org-mode commands
  24237. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24238. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24239. ;; How to do this: Measure non-white length of current string
  24240. ;; If equal to column width, we should realign.
  24241. (defun org-remap (map &rest commands)
  24242. "In MAP, remap the functions given in COMMANDS.
  24243. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24244. (let (new old)
  24245. (while commands
  24246. (setq old (pop commands) new (pop commands))
  24247. (if (fboundp 'command-remapping)
  24248. (org-defkey map (vector 'remap old) new)
  24249. (substitute-key-definition old new map global-map)))))
  24250. (when (eq org-enable-table-editor 'optimized)
  24251. ;; If the user wants maximum table support, we need to hijack
  24252. ;; some standard editing functions
  24253. (org-remap org-mode-map
  24254. 'self-insert-command 'org-self-insert-command
  24255. 'delete-char 'org-delete-char
  24256. 'delete-backward-char 'org-delete-backward-char)
  24257. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24258. (defun org-shiftcursor-error ()
  24259. "Throw an error because Shift-Cursor command was applied in wrong context."
  24260. (error "This command is active in special context like tables, headlines or timestamps"))
  24261. (defun org-shifttab (&optional arg)
  24262. "Global visibility cycling or move to previous table field.
  24263. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24264. on context.
  24265. See the individual commands for more information."
  24266. (interactive "P")
  24267. (cond
  24268. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24269. (arg (message "Content view to level: ")
  24270. (org-content (prefix-numeric-value arg))
  24271. (setq org-cycle-global-status 'overview))
  24272. (t (call-interactively 'org-global-cycle))))
  24273. (defun org-shiftmetaleft ()
  24274. "Promote subtree or delete table column.
  24275. Calls `org-promote-subtree', `org-outdent-item',
  24276. or `org-table-delete-column', depending on context.
  24277. See the individual commands for more information."
  24278. (interactive)
  24279. (cond
  24280. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24281. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24282. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24283. (t (org-shiftcursor-error))))
  24284. (defun org-shiftmetaright ()
  24285. "Demote subtree or insert table column.
  24286. Calls `org-demote-subtree', `org-indent-item',
  24287. or `org-table-insert-column', depending on context.
  24288. See the individual commands for more information."
  24289. (interactive)
  24290. (cond
  24291. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24292. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24293. ((org-at-item-p) (call-interactively 'org-indent-item))
  24294. (t (org-shiftcursor-error))))
  24295. (defun org-shiftmetaup (&optional arg)
  24296. "Move subtree up or kill table row.
  24297. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24298. `org-move-item-up' depending on context. See the individual commands
  24299. for more information."
  24300. (interactive "P")
  24301. (cond
  24302. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24303. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24304. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24305. (t (org-shiftcursor-error))))
  24306. (defun org-shiftmetadown (&optional arg)
  24307. "Move subtree down or insert table row.
  24308. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24309. `org-move-item-down', depending on context. See the individual
  24310. commands for more information."
  24311. (interactive "P")
  24312. (cond
  24313. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24314. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24315. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24316. (t (org-shiftcursor-error))))
  24317. (defun org-metaleft (&optional arg)
  24318. "Promote heading or move table column to left.
  24319. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24320. With no specific context, calls the Emacs default `backward-word'.
  24321. See the individual commands for more information."
  24322. (interactive "P")
  24323. (cond
  24324. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24325. ((or (org-on-heading-p) (org-region-active-p))
  24326. (call-interactively 'org-do-promote))
  24327. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24328. (t (call-interactively 'backward-word))))
  24329. (defun org-metaright (&optional arg)
  24330. "Demote subtree or move table column to right.
  24331. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24332. With no specific context, calls the Emacs default `forward-word'.
  24333. See the individual commands for more information."
  24334. (interactive "P")
  24335. (cond
  24336. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24337. ((or (org-on-heading-p) (org-region-active-p))
  24338. (call-interactively 'org-do-demote))
  24339. ((org-at-item-p) (call-interactively 'org-indent-item))
  24340. (t (call-interactively 'forward-word))))
  24341. (defun org-metaup (&optional arg)
  24342. "Move subtree up or move table row up.
  24343. Calls `org-move-subtree-up' or `org-table-move-row' or
  24344. `org-move-item-up', depending on context. See the individual commands
  24345. for more information."
  24346. (interactive "P")
  24347. (cond
  24348. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24349. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24350. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24351. (t (transpose-lines 1) (beginning-of-line -1))))
  24352. (defun org-metadown (&optional arg)
  24353. "Move subtree down or move table row down.
  24354. Calls `org-move-subtree-down' or `org-table-move-row' or
  24355. `org-move-item-down', depending on context. See the individual
  24356. commands for more information."
  24357. (interactive "P")
  24358. (cond
  24359. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24360. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24361. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24362. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24363. (defun org-shiftup (&optional arg)
  24364. "Increase item in timestamp or increase priority of current headline.
  24365. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24366. depending on context. See the individual commands for more information."
  24367. (interactive "P")
  24368. (cond
  24369. ((org-at-timestamp-p t)
  24370. (call-interactively (if org-edit-timestamp-down-means-later
  24371. 'org-timestamp-down 'org-timestamp-up)))
  24372. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24373. ((org-at-item-p) (call-interactively 'org-previous-item))
  24374. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24375. (defun org-shiftdown (&optional arg)
  24376. "Decrease item in timestamp or decrease priority of current headline.
  24377. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24378. depending on context. See the individual commands for more information."
  24379. (interactive "P")
  24380. (cond
  24381. ((org-at-timestamp-p t)
  24382. (call-interactively (if org-edit-timestamp-down-means-later
  24383. 'org-timestamp-up 'org-timestamp-down)))
  24384. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24385. (t (call-interactively 'org-next-item))))
  24386. (defun org-shiftright ()
  24387. "Next TODO keyword or timestamp one day later, depending on context."
  24388. (interactive)
  24389. (cond
  24390. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24391. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24392. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24393. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24394. (t (org-shiftcursor-error))))
  24395. (defun org-shiftleft ()
  24396. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24397. (interactive)
  24398. (cond
  24399. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24400. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24401. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24402. ((org-at-property-p)
  24403. (call-interactively 'org-property-previous-allowed-value))
  24404. (t (org-shiftcursor-error))))
  24405. (defun org-shiftcontrolright ()
  24406. "Switch to next TODO set."
  24407. (interactive)
  24408. (cond
  24409. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24410. (t (org-shiftcursor-error))))
  24411. (defun org-shiftcontrolleft ()
  24412. "Switch to previous TODO set."
  24413. (interactive)
  24414. (cond
  24415. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24416. (t (org-shiftcursor-error))))
  24417. (defun org-ctrl-c-ret ()
  24418. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24419. (interactive)
  24420. (cond
  24421. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24422. (t (call-interactively 'org-insert-heading))))
  24423. (defun org-copy-special ()
  24424. "Copy region in table or copy current subtree.
  24425. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24426. See the individual commands for more information."
  24427. (interactive)
  24428. (call-interactively
  24429. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24430. (defun org-cut-special ()
  24431. "Cut region in table or cut current subtree.
  24432. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24433. See the individual commands for more information."
  24434. (interactive)
  24435. (call-interactively
  24436. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24437. (defun org-paste-special (arg)
  24438. "Paste rectangular region into table, or past subtree relative to level.
  24439. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24440. See the individual commands for more information."
  24441. (interactive "P")
  24442. (if (org-at-table-p)
  24443. (org-table-paste-rectangle)
  24444. (org-paste-subtree arg)))
  24445. (defun org-ctrl-c-ctrl-c (&optional arg)
  24446. "Set tags in headline, or update according to changed information at point.
  24447. This command does many different things, depending on context:
  24448. - If the cursor is in a headline, prompt for tags and insert them
  24449. into the current line, aligned to `org-tags-column'. When called
  24450. with prefix arg, realign all tags in the current buffer.
  24451. - If the cursor is in one of the special #+KEYWORD lines, this
  24452. triggers scanning the buffer for these lines and updating the
  24453. information.
  24454. - If the cursor is inside a table, realign the table. This command
  24455. works even if the automatic table editor has been turned off.
  24456. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24457. the entire table.
  24458. - If the cursor is a the beginning of a dynamic block, update it.
  24459. - If the cursor is inside a table created by the table.el package,
  24460. activate that table.
  24461. - If the current buffer is a remember buffer, close note and file it.
  24462. with a prefix argument, file it without further interaction to the default
  24463. location.
  24464. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24465. links in this buffer.
  24466. - If the cursor is on a numbered item in a plain list, renumber the
  24467. ordered list.
  24468. - If the cursor is on a checkbox, toggle it."
  24469. (interactive "P")
  24470. (let ((org-enable-table-editor t))
  24471. (cond
  24472. ((or org-clock-overlays
  24473. org-occur-highlights
  24474. org-latex-fragment-image-overlays)
  24475. (org-remove-clock-overlays)
  24476. (org-remove-occur-highlights)
  24477. (org-remove-latex-fragment-image-overlays)
  24478. (message "Temporary highlights/overlays removed from current buffer"))
  24479. ((and (local-variable-p 'org-finish-function (current-buffer))
  24480. (fboundp org-finish-function))
  24481. (funcall org-finish-function))
  24482. ((org-at-property-p)
  24483. (call-interactively 'org-property-action))
  24484. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24485. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24486. ((org-at-table.el-p)
  24487. (require 'table)
  24488. (beginning-of-line 1)
  24489. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24490. (call-interactively 'table-recognize-table))
  24491. ((org-at-table-p)
  24492. (org-table-maybe-eval-formula)
  24493. (if arg
  24494. (call-interactively 'org-table-recalculate)
  24495. (org-table-maybe-recalculate-line))
  24496. (call-interactively 'org-table-align))
  24497. ((org-at-item-checkbox-p)
  24498. (call-interactively 'org-toggle-checkbox))
  24499. ((org-at-item-p)
  24500. (call-interactively 'org-maybe-renumber-ordered-list))
  24501. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24502. ;; Dynamic block
  24503. (beginning-of-line 1)
  24504. (org-update-dblock))
  24505. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24506. (cond
  24507. ((equal (match-string 1) "TBLFM")
  24508. ;; Recalculate the table before this line
  24509. (save-excursion
  24510. (beginning-of-line 1)
  24511. (skip-chars-backward " \r\n\t")
  24512. (if (org-at-table-p)
  24513. (org-call-with-arg 'org-table-recalculate t))))
  24514. (t
  24515. (call-interactively 'org-mode-restart))))
  24516. (t (error "C-c C-c can do nothing useful at this location.")))))
  24517. (defun org-mode-restart ()
  24518. "Restart Org-mode, to scan again for special lines.
  24519. Also updates the keyword regular expressions."
  24520. (interactive)
  24521. (let ((org-inhibit-startup t)) (org-mode))
  24522. (message "Org-mode restarted to refresh keyword and special line setup"))
  24523. (defun org-kill-note-or-show-branches ()
  24524. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  24525. (interactive)
  24526. (if (not org-finish-function)
  24527. (call-interactively 'show-branches)
  24528. (let ((org-note-abort t))
  24529. (funcall org-finish-function))))
  24530. (defun org-return (&optional indent)
  24531. "Goto next table row or insert a newline.
  24532. Calls `org-table-next-row' or `newline', depending on context.
  24533. See the individual commands for more information."
  24534. (interactive)
  24535. (cond
  24536. ((bobp) (if indent (newline-and-indent) (newline)))
  24537. ((org-at-table-p)
  24538. (org-table-justify-field-maybe)
  24539. (call-interactively 'org-table-next-row))
  24540. (t (if indent (newline-and-indent) (newline)))))
  24541. (defun org-return-indent ()
  24542. (interactive)
  24543. "Goto next table row or insert a newline and indent.
  24544. Calls `org-table-next-row' or `newline-and-indent', depending on
  24545. context. See the individual commands for more information."
  24546. (org-return t))
  24547. (defun org-ctrl-c-minus ()
  24548. "Insert separator line in table or modify bullet type in list.
  24549. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  24550. depending on context."
  24551. (interactive)
  24552. (cond
  24553. ((org-at-table-p)
  24554. (call-interactively 'org-table-insert-hline))
  24555. ((org-on-heading-p)
  24556. ;; Convert to item
  24557. (save-excursion
  24558. (beginning-of-line 1)
  24559. (if (looking-at "\\*+ ")
  24560. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  24561. ((org-in-item-p)
  24562. (call-interactively 'org-cycle-list-bullet))
  24563. (t (error "`C-c -' does have no function here."))))
  24564. (defun org-meta-return (&optional arg)
  24565. "Insert a new heading or wrap a region in a table.
  24566. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  24567. See the individual commands for more information."
  24568. (interactive "P")
  24569. (cond
  24570. ((org-at-table-p)
  24571. (call-interactively 'org-table-wrap-region))
  24572. (t (call-interactively 'org-insert-heading))))
  24573. ;;; Menu entries
  24574. ;; Define the Org-mode menus
  24575. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  24576. '("Tbl"
  24577. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  24578. ["Next Field" org-cycle (org-at-table-p)]
  24579. ["Previous Field" org-shifttab (org-at-table-p)]
  24580. ["Next Row" org-return (org-at-table-p)]
  24581. "--"
  24582. ["Blank Field" org-table-blank-field (org-at-table-p)]
  24583. ["Edit Field" org-table-edit-field (org-at-table-p)]
  24584. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  24585. "--"
  24586. ("Column"
  24587. ["Move Column Left" org-metaleft (org-at-table-p)]
  24588. ["Move Column Right" org-metaright (org-at-table-p)]
  24589. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  24590. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  24591. ("Row"
  24592. ["Move Row Up" org-metaup (org-at-table-p)]
  24593. ["Move Row Down" org-metadown (org-at-table-p)]
  24594. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  24595. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  24596. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  24597. "--"
  24598. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  24599. ("Rectangle"
  24600. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  24601. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  24602. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  24603. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  24604. "--"
  24605. ("Calculate"
  24606. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  24607. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  24608. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  24609. "--"
  24610. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  24611. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  24612. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  24613. "--"
  24614. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  24615. "--"
  24616. ["Sum Column/Rectangle" org-table-sum
  24617. (or (org-at-table-p) (org-region-active-p))]
  24618. ["Which Column?" org-table-current-column (org-at-table-p)])
  24619. ["Debug Formulas"
  24620. org-table-toggle-formula-debugger
  24621. :style toggle :selected org-table-formula-debug]
  24622. ["Show Col/Row Numbers"
  24623. org-table-toggle-coordinate-overlays
  24624. :style toggle :selected org-table-overlay-coordinates]
  24625. "--"
  24626. ["Create" org-table-create (and (not (org-at-table-p))
  24627. org-enable-table-editor)]
  24628. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  24629. ["Import from File" org-table-import (not (org-at-table-p))]
  24630. ["Export to File" org-table-export (org-at-table-p)]
  24631. "--"
  24632. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  24633. (easy-menu-define org-org-menu org-mode-map "Org menu"
  24634. '("Org"
  24635. ("Show/Hide"
  24636. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  24637. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  24638. ["Sparse Tree" org-occur t]
  24639. ["Reveal Context" org-reveal t]
  24640. ["Show All" show-all t]
  24641. "--"
  24642. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  24643. "--"
  24644. ["New Heading" org-insert-heading t]
  24645. ("Navigate Headings"
  24646. ["Up" outline-up-heading t]
  24647. ["Next" outline-next-visible-heading t]
  24648. ["Previous" outline-previous-visible-heading t]
  24649. ["Next Same Level" outline-forward-same-level t]
  24650. ["Previous Same Level" outline-backward-same-level t]
  24651. "--"
  24652. ["Jump" org-goto t])
  24653. ("Edit Structure"
  24654. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  24655. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  24656. "--"
  24657. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  24658. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  24659. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  24660. "--"
  24661. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  24662. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  24663. ["Demote Heading" org-metaright (not (org-at-table-p))]
  24664. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  24665. "--"
  24666. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  24667. "--"
  24668. ["Convert to odd levels" org-convert-to-odd-levels t]
  24669. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  24670. ("Editing"
  24671. ["Emphasis..." org-emphasize t])
  24672. ("Archive"
  24673. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  24674. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  24675. ; :active t :keys "C-u C-c C-x C-a"]
  24676. ["Sparse trees open ARCHIVE trees"
  24677. (setq org-sparse-tree-open-archived-trees
  24678. (not org-sparse-tree-open-archived-trees))
  24679. :style toggle :selected org-sparse-tree-open-archived-trees]
  24680. ["Cycling opens ARCHIVE trees"
  24681. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  24682. :style toggle :selected org-cycle-open-archived-trees]
  24683. ["Agenda includes ARCHIVE trees"
  24684. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  24685. :style toggle :selected (not org-agenda-skip-archived-trees)]
  24686. "--"
  24687. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  24688. ; ["Check and Move Children" (org-archive-subtree '(4))
  24689. ; :active t :keys "C-u C-c C-x C-s"]
  24690. )
  24691. "--"
  24692. ("TODO Lists"
  24693. ["TODO/DONE/-" org-todo t]
  24694. ("Select keyword"
  24695. ["Next keyword" org-shiftright (org-on-heading-p)]
  24696. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  24697. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  24698. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  24699. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  24700. ["Show TODO Tree" org-show-todo-tree t]
  24701. ["Global TODO list" org-todo-list t]
  24702. "--"
  24703. ["Set Priority" org-priority t]
  24704. ["Priority Up" org-shiftup t]
  24705. ["Priority Down" org-shiftdown t])
  24706. ("TAGS and Properties"
  24707. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  24708. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  24709. "--"
  24710. ["Set property" 'org-set-property t]
  24711. ["Column view of properties" org-columns t]
  24712. ["Insert Column View DBlock" org-insert-columns-dblock t])
  24713. ("Dates and Scheduling"
  24714. ["Timestamp" org-time-stamp t]
  24715. ["Timestamp (inactive)" org-time-stamp-inactive t]
  24716. ("Change Date"
  24717. ["1 Day Later" org-shiftright t]
  24718. ["1 Day Earlier" org-shiftleft t]
  24719. ["1 ... Later" org-shiftup t]
  24720. ["1 ... Earlier" org-shiftdown t])
  24721. ["Compute Time Range" org-evaluate-time-range t]
  24722. ["Schedule Item" org-schedule t]
  24723. ["Deadline" org-deadline t]
  24724. "--"
  24725. ["Custom time format" org-toggle-time-stamp-overlays
  24726. :style radio :selected org-display-custom-times]
  24727. "--"
  24728. ["Goto Calendar" org-goto-calendar t]
  24729. ["Date from Calendar" org-date-from-calendar t])
  24730. ("Logging work"
  24731. ["Clock in" org-clock-in t]
  24732. ["Clock out" org-clock-out t]
  24733. ["Clock cancel" org-clock-cancel t]
  24734. ["Goto running clock" org-clock-goto t]
  24735. ["Display times" org-clock-display t]
  24736. ["Create clock table" org-clock-report t]
  24737. "--"
  24738. ["Record DONE time"
  24739. (progn (setq org-log-done (not org-log-done))
  24740. (message "Switching to %s will %s record a timestamp"
  24741. (car org-done-keywords)
  24742. (if org-log-done "automatically" "not")))
  24743. :style toggle :selected org-log-done])
  24744. "--"
  24745. ["Agenda Command..." org-agenda t]
  24746. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  24747. ("File List for Agenda")
  24748. ("Special views current file"
  24749. ["TODO Tree" org-show-todo-tree t]
  24750. ["Check Deadlines" org-check-deadlines t]
  24751. ["Timeline" org-timeline t]
  24752. ["Tags Tree" org-tags-sparse-tree t])
  24753. "--"
  24754. ("Hyperlinks"
  24755. ["Store Link (Global)" org-store-link t]
  24756. ["Insert Link" org-insert-link t]
  24757. ["Follow Link" org-open-at-point t]
  24758. "--"
  24759. ["Next link" org-next-link t]
  24760. ["Previous link" org-previous-link t]
  24761. "--"
  24762. ["Descriptive Links"
  24763. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  24764. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  24765. ["Literal Links"
  24766. (progn
  24767. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  24768. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  24769. "--"
  24770. ["Export/Publish..." org-export t]
  24771. ("LaTeX"
  24772. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  24773. :selected org-cdlatex-mode]
  24774. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  24775. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  24776. ["Modify math symbol" org-cdlatex-math-modify
  24777. (org-inside-LaTeX-fragment-p)]
  24778. ["Export LaTeX fragments as images"
  24779. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  24780. :style toggle :selected org-export-with-LaTeX-fragments])
  24781. "--"
  24782. ("Documentation"
  24783. ["Show Version" org-version t]
  24784. ["Info Documentation" org-info t])
  24785. ("Customize"
  24786. ["Browse Org Group" org-customize t]
  24787. "--"
  24788. ["Expand This Menu" org-create-customize-menu
  24789. (fboundp 'customize-menu-create)])
  24790. "--"
  24791. ["Refresh setup" org-mode-restart t]
  24792. ))
  24793. (defun org-info (&optional node)
  24794. "Read documentation for Org-mode in the info system.
  24795. With optional NODE, go directly to that node."
  24796. (interactive)
  24797. (require 'info)
  24798. (Info-goto-node (format "(org)%s" (or node ""))))
  24799. (defun org-install-agenda-files-menu ()
  24800. (let ((bl (buffer-list)))
  24801. (save-excursion
  24802. (while bl
  24803. (set-buffer (pop bl))
  24804. (if (org-mode-p) (setq bl nil)))
  24805. (when (org-mode-p)
  24806. (easy-menu-change
  24807. '("Org") "File List for Agenda"
  24808. (append
  24809. (list
  24810. ["Edit File List" (org-edit-agenda-file-list) t]
  24811. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  24812. ["Remove Current File from List" org-remove-file t]
  24813. ["Cycle through agenda files" org-cycle-agenda-files t]
  24814. ["Occur in all agenda files" org-occur-in-agenda-files t]
  24815. "--")
  24816. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  24817. ;;;; Documentation
  24818. (defun org-customize ()
  24819. "Call the customize function with org as argument."
  24820. (interactive)
  24821. (customize-browse 'org))
  24822. (defun org-create-customize-menu ()
  24823. "Create a full customization menu for Org-mode, insert it into the menu."
  24824. (interactive)
  24825. (if (fboundp 'customize-menu-create)
  24826. (progn
  24827. (easy-menu-change
  24828. '("Org") "Customize"
  24829. `(["Browse Org group" org-customize t]
  24830. "--"
  24831. ,(customize-menu-create 'org)
  24832. ["Set" Custom-set t]
  24833. ["Save" Custom-save t]
  24834. ["Reset to Current" Custom-reset-current t]
  24835. ["Reset to Saved" Custom-reset-saved t]
  24836. ["Reset to Standard Settings" Custom-reset-standard t]))
  24837. (message "\"Org\"-menu now contains full customization menu"))
  24838. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  24839. ;;;; Miscellaneous stuff
  24840. ;;; Generally useful functions
  24841. (defun org-context ()
  24842. "Return a list of contexts of the current cursor position.
  24843. If several contexts apply, all are returned.
  24844. Each context entry is a list with a symbol naming the context, and
  24845. two positions indicating start and end of the context. Possible
  24846. contexts are:
  24847. :headline anywhere in a headline
  24848. :headline-stars on the leading stars in a headline
  24849. :todo-keyword on a TODO keyword (including DONE) in a headline
  24850. :tags on the TAGS in a headline
  24851. :priority on the priority cookie in a headline
  24852. :item on the first line of a plain list item
  24853. :item-bullet on the bullet/number of a plain list item
  24854. :checkbox on the checkbox in a plain list item
  24855. :table in an org-mode table
  24856. :table-special on a special filed in a table
  24857. :table-table in a table.el table
  24858. :link on a hyperlink
  24859. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  24860. :target on a <<target>>
  24861. :radio-target on a <<<radio-target>>>
  24862. :latex-fragment on a LaTeX fragment
  24863. :latex-preview on a LaTeX fragment with overlayed preview image
  24864. This function expects the position to be visible because it uses font-lock
  24865. faces as a help to recognize the following contexts: :table-special, :link,
  24866. and :keyword."
  24867. (let* ((f (get-text-property (point) 'face))
  24868. (faces (if (listp f) f (list f)))
  24869. (p (point)) clist o)
  24870. ;; First the large context
  24871. (cond
  24872. ((org-on-heading-p t)
  24873. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  24874. (when (progn
  24875. (beginning-of-line 1)
  24876. (looking-at org-todo-line-tags-regexp))
  24877. (push (org-point-in-group p 1 :headline-stars) clist)
  24878. (push (org-point-in-group p 2 :todo-keyword) clist)
  24879. (push (org-point-in-group p 4 :tags) clist))
  24880. (goto-char p)
  24881. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  24882. (if (looking-at "\\[#[A-Z0-9]\\]")
  24883. (push (org-point-in-group p 0 :priority) clist)))
  24884. ((org-at-item-p)
  24885. (push (org-point-in-group p 2 :item-bullet) clist)
  24886. (push (list :item (point-at-bol)
  24887. (save-excursion (org-end-of-item) (point)))
  24888. clist)
  24889. (and (org-at-item-checkbox-p)
  24890. (push (org-point-in-group p 0 :checkbox) clist)))
  24891. ((org-at-table-p)
  24892. (push (list :table (org-table-begin) (org-table-end)) clist)
  24893. (if (memq 'org-formula faces)
  24894. (push (list :table-special
  24895. (previous-single-property-change p 'face)
  24896. (next-single-property-change p 'face)) clist)))
  24897. ((org-at-table-p 'any)
  24898. (push (list :table-table) clist)))
  24899. (goto-char p)
  24900. ;; Now the small context
  24901. (cond
  24902. ((org-at-timestamp-p)
  24903. (push (org-point-in-group p 0 :timestamp) clist))
  24904. ((memq 'org-link faces)
  24905. (push (list :link
  24906. (previous-single-property-change p 'face)
  24907. (next-single-property-change p 'face)) clist))
  24908. ((memq 'org-special-keyword faces)
  24909. (push (list :keyword
  24910. (previous-single-property-change p 'face)
  24911. (next-single-property-change p 'face)) clist))
  24912. ((org-on-target-p)
  24913. (push (org-point-in-group p 0 :target) clist)
  24914. (goto-char (1- (match-beginning 0)))
  24915. (if (looking-at org-radio-target-regexp)
  24916. (push (org-point-in-group p 0 :radio-target) clist))
  24917. (goto-char p))
  24918. ((setq o (car (delq nil
  24919. (mapcar
  24920. (lambda (x)
  24921. (if (memq x org-latex-fragment-image-overlays) x))
  24922. (org-overlays-at (point))))))
  24923. (push (list :latex-fragment
  24924. (org-overlay-start o) (org-overlay-end o)) clist)
  24925. (push (list :latex-preview
  24926. (org-overlay-start o) (org-overlay-end o)) clist))
  24927. ((org-inside-LaTeX-fragment-p)
  24928. ;; FIXME: positions wrong.
  24929. (push (list :latex-fragment (point) (point)) clist)))
  24930. (setq clist (nreverse (delq nil clist)))
  24931. clist))
  24932. ;; FIXME: Compare with at-regexp-p Do we need both?
  24933. (defun org-in-regexp (re &optional nlines visually)
  24934. "Check if point is inside a match of regexp.
  24935. Normally only the current line is checked, but you can include NLINES extra
  24936. lines both before and after point into the search.
  24937. If VISUALLY is set, require that the cursor is not after the match but
  24938. really on, so that the block visually is on the match."
  24939. (catch 'exit
  24940. (let ((pos (point))
  24941. (eol (point-at-eol (+ 1 (or nlines 0))))
  24942. (inc (if visually 1 0)))
  24943. (save-excursion
  24944. (beginning-of-line (- 1 (or nlines 0)))
  24945. (while (re-search-forward re eol t)
  24946. (if (and (<= (match-beginning 0) pos)
  24947. (>= (+ inc (match-end 0)) pos))
  24948. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  24949. (defun org-at-regexp-p (regexp)
  24950. "Is point inside a match of REGEXP in the current line?"
  24951. (catch 'exit
  24952. (save-excursion
  24953. (let ((pos (point)) (end (point-at-eol)))
  24954. (beginning-of-line 1)
  24955. (while (re-search-forward regexp end t)
  24956. (if (and (<= (match-beginning 0) pos)
  24957. (>= (match-end 0) pos))
  24958. (throw 'exit t)))
  24959. nil))))
  24960. (defun org-occur-in-agenda-files (regexp &optional nlines)
  24961. "Call `multi-occur' with buffers for all agenda files."
  24962. (interactive "sOrg-files matching: \np")
  24963. (let* ((files (org-agenda-files))
  24964. (tnames (mapcar 'file-truename files))
  24965. (extra org-agenda-multi-occur-extra-files)
  24966. f)
  24967. (while (setq f (pop extra))
  24968. (unless (member (file-truename f) tnames)
  24969. (add-to-list 'files f 'append)
  24970. (add-to-list 'tnames (file-truename f) 'append)))
  24971. (multi-occur
  24972. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  24973. regexp)))
  24974. (defun org-uniquify (list)
  24975. "Remove duplicate elements from LIST."
  24976. (let (res)
  24977. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  24978. res))
  24979. (defun org-delete-all (elts list)
  24980. "Remove all elements in ELTS from LIST."
  24981. (while elts
  24982. (setq list (delete (pop elts) list)))
  24983. list)
  24984. (defun org-back-over-empty-lines ()
  24985. "Move backwards over witespace, to the beginning of the first empty line.
  24986. Returns the number o empty lines passed."
  24987. (let ((pos (point)))
  24988. (skip-chars-backward " \t\n\r")
  24989. (beginning-of-line 2)
  24990. (count-lines (point) pos)))
  24991. (defun org-skip-whitespace ()
  24992. (skip-chars-forward " \t\n\r"))
  24993. (defun org-point-in-group (point group &optional context)
  24994. "Check if POINT is in match-group GROUP.
  24995. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  24996. match. If the match group does ot exist or point is not inside it,
  24997. return nil."
  24998. (and (match-beginning group)
  24999. (>= point (match-beginning group))
  25000. (<= point (match-end group))
  25001. (if context
  25002. (list context (match-beginning group) (match-end group))
  25003. t)))
  25004. (defun org-switch-to-buffer-other-window (&rest args)
  25005. "Switch to buffer in a second window on the current frame.
  25006. In particular, do not allow pop-up frames."
  25007. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25008. special-display-function)
  25009. (apply 'switch-to-buffer-other-window args)))
  25010. (defun org-combine-plists (&rest plists)
  25011. "Create a single property list from all plists in PLISTS.
  25012. The process starts by copying the first list, and then setting properties
  25013. from the other lists. Settings in the last list are the most significant
  25014. ones and overrule settings in the other lists."
  25015. (let ((rtn (copy-sequence (pop plists)))
  25016. p v ls)
  25017. (while plists
  25018. (setq ls (pop plists))
  25019. (while ls
  25020. (setq p (pop ls) v (pop ls))
  25021. (setq rtn (plist-put rtn p v))))
  25022. rtn))
  25023. (defun org-move-line-down (arg)
  25024. "Move the current line down. With prefix argument, move it past ARG lines."
  25025. (interactive "p")
  25026. (let ((col (current-column))
  25027. beg end pos)
  25028. (beginning-of-line 1) (setq beg (point))
  25029. (beginning-of-line 2) (setq end (point))
  25030. (beginning-of-line (+ 1 arg))
  25031. (setq pos (move-marker (make-marker) (point)))
  25032. (insert (delete-and-extract-region beg end))
  25033. (goto-char pos)
  25034. (move-to-column col)))
  25035. (defun org-move-line-up (arg)
  25036. "Move the current line up. With prefix argument, move it past ARG lines."
  25037. (interactive "p")
  25038. (let ((col (current-column))
  25039. beg end pos)
  25040. (beginning-of-line 1) (setq beg (point))
  25041. (beginning-of-line 2) (setq end (point))
  25042. (beginning-of-line (- arg))
  25043. (setq pos (move-marker (make-marker) (point)))
  25044. (insert (delete-and-extract-region beg end))
  25045. (goto-char pos)
  25046. (move-to-column col)))
  25047. (defun org-replace-escapes (string table)
  25048. "Replace %-escapes in STRING with values in TABLE.
  25049. TABLE is an association list with keys like \"%a\" and string values.
  25050. The sequences in STRING may contain normal field width and padding information,
  25051. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25052. so values can contain further %-escapes if they are define later in TABLE."
  25053. (let ((case-fold-search nil)
  25054. e re rpl)
  25055. (while (setq e (pop table))
  25056. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25057. (while (string-match re string)
  25058. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25059. (cdr e)))
  25060. (setq string (replace-match rpl t t string))))
  25061. string))
  25062. (defun org-sublist (list start end)
  25063. "Return a section of LIST, from START to END.
  25064. Counting starts at 1."
  25065. (let (rtn (c start))
  25066. (setq list (nthcdr (1- start) list))
  25067. (while (and list (<= c end))
  25068. (push (pop list) rtn)
  25069. (setq c (1+ c)))
  25070. (nreverse rtn)))
  25071. (defun org-find-base-buffer-visiting (file)
  25072. "Like `find-buffer-visiting' but alway return the base buffer and
  25073. not an indirect buffer"
  25074. (let ((buf (find-buffer-visiting file)))
  25075. (if buf
  25076. (or (buffer-base-buffer buf) buf)
  25077. nil)))
  25078. (defun org-image-file-name-regexp ()
  25079. "Return regexp matching the file names of images."
  25080. (if (fboundp 'image-file-name-regexp)
  25081. (image-file-name-regexp)
  25082. (let ((image-file-name-extensions
  25083. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25084. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25085. (concat "\\."
  25086. (regexp-opt (nconc (mapcar 'upcase
  25087. image-file-name-extensions)
  25088. image-file-name-extensions)
  25089. t)
  25090. "\\'"))))
  25091. (defun org-file-image-p (file)
  25092. "Return non-nil if FILE is an image."
  25093. (save-match-data
  25094. (string-match (org-image-file-name-regexp) file)))
  25095. ;;; Paragraph filling stuff.
  25096. ;; We want this to be just right, so use the full arsenal.
  25097. (defun org-indent-line-function ()
  25098. "Indent line like previous, but further if previous was headline or item."
  25099. (interactive)
  25100. (let* ((pos (point))
  25101. (itemp (org-at-item-p))
  25102. column bpos bcol tpos tcol bullet btype bullet-type)
  25103. ;; Find the previous relevant line
  25104. (beginning-of-line 1)
  25105. (cond
  25106. ((looking-at "#") (setq column 0))
  25107. ((looking-at "\\*+ ") (setq column 0))
  25108. (t
  25109. (beginning-of-line 0)
  25110. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25111. (beginning-of-line 0))
  25112. (cond
  25113. ((looking-at "\\*+[ \t]+")
  25114. (goto-char (match-end 0))
  25115. (setq column (current-column)))
  25116. ((org-in-item-p)
  25117. (org-beginning-of-item)
  25118. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25119. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25120. (setq bpos (match-beginning 1) tpos (match-end 0)
  25121. bcol (progn (goto-char bpos) (current-column))
  25122. tcol (progn (goto-char tpos) (current-column))
  25123. bullet (match-string 1)
  25124. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25125. (if (not itemp)
  25126. (setq column tcol)
  25127. (goto-char pos)
  25128. (beginning-of-line 1)
  25129. (if (looking-at "\\S-")
  25130. (progn
  25131. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25132. (setq bullet (match-string 1)
  25133. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25134. (setq column (if (equal btype bullet-type) bcol tcol)))
  25135. (setq column (org-get-indentation)))))
  25136. (t (setq column (org-get-indentation))))))
  25137. (goto-char pos)
  25138. (if (<= (current-column) (current-indentation))
  25139. (indent-line-to column)
  25140. (save-excursion (indent-line-to column)))
  25141. (setq column (current-column))
  25142. (beginning-of-line 1)
  25143. (if (looking-at
  25144. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25145. (replace-match (concat "\\1" (format org-property-format
  25146. (match-string 2) (match-string 3)))
  25147. t nil))
  25148. (move-to-column column)))
  25149. (defun org-set-autofill-regexps ()
  25150. (interactive)
  25151. ;; In the paragraph separator we include headlines, because filling
  25152. ;; text in a line directly attached to a headline would otherwise
  25153. ;; fill the headline as well.
  25154. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25155. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25156. ;; The paragraph starter includes hand-formatted lists.
  25157. (org-set-local 'paragraph-start
  25158. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25159. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25160. ;; But only if the user has not turned off tables or fixed-width regions
  25161. (org-set-local
  25162. 'auto-fill-inhibit-regexp
  25163. (concat "\\*+ \\|#\\+"
  25164. "\\|[ \t]*" org-keyword-time-regexp
  25165. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25166. (concat
  25167. "\\|[ \t]*["
  25168. (if org-enable-table-editor "|" "")
  25169. (if org-enable-fixed-width-editor ":" "")
  25170. "]"))))
  25171. ;; We use our own fill-paragraph function, to make sure that tables
  25172. ;; and fixed-width regions are not wrapped. That function will pass
  25173. ;; through to `fill-paragraph' when appropriate.
  25174. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25175. ; Adaptive filling: To get full control, first make sure that
  25176. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25177. (org-set-local 'adaptive-fill-regexp "\000")
  25178. (org-set-local 'adaptive-fill-function
  25179. 'org-adaptive-fill-function))
  25180. (defun org-fill-paragraph (&optional justify)
  25181. "Re-align a table, pass through to fill-paragraph if no table."
  25182. (let ((table-p (org-at-table-p))
  25183. (table.el-p (org-at-table.el-p)))
  25184. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25185. (save-excursion (goto-char (point-at-bol))
  25186. (looking-at outline-regexp)))
  25187. t) ; skip headlines
  25188. (table.el-p t) ; skip table.el tables
  25189. (table-p (org-table-align) t) ; align org-mode tables
  25190. (t nil)))) ; call paragraph-fill
  25191. ;; For reference, this is the default value of adaptive-fill-regexp
  25192. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25193. (defun org-adaptive-fill-function ()
  25194. "Return a fill prefix for org-mode files.
  25195. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25196. work correctly."
  25197. (cond ((looking-at "#[ \t]+")
  25198. (match-string 0))
  25199. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25200. (save-excursion
  25201. (goto-char (match-end 0))
  25202. (make-string (current-column) ?\ )))
  25203. (t nil)))
  25204. ;;;; Functions extending outline functionality
  25205. (defun org-beginning-of-line (&optional arg)
  25206. "Go to the beginning of the current line. If that is invisible, continue
  25207. to a visible line beginning. This makes the function of C-a more intuitive.
  25208. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25209. first attempt, and only move to after the tags when the cursor is already
  25210. beyond the end of the headline."
  25211. (interactive "P")
  25212. (let ((pos (point)))
  25213. (beginning-of-line 1)
  25214. (if (bobp)
  25215. nil
  25216. (backward-char 1)
  25217. (if (org-invisible-p)
  25218. (while (and (not (bobp)) (org-invisible-p))
  25219. (backward-char 1)
  25220. (beginning-of-line 1))
  25221. (forward-char 1)))
  25222. (when org-special-ctrl-a/e
  25223. (cond
  25224. ((and (looking-at org-todo-line-regexp)
  25225. (= (char-after (match-end 1)) ?\ ))
  25226. (goto-char
  25227. (if (eq org-special-ctrl-a/e t)
  25228. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25229. ((= pos (point)) (match-beginning 3))
  25230. (t (point)))
  25231. (cond ((> pos (point)) (point))
  25232. ((not (eq last-command this-command)) (point))
  25233. (t (match-beginning 3))))))
  25234. ((org-at-item-p)
  25235. (goto-char
  25236. (if (eq org-special-ctrl-a/e t)
  25237. (cond ((> pos (match-end 4)) (match-end 4))
  25238. ((= pos (point)) (match-end 4))
  25239. (t (point)))
  25240. (cond ((> pos (point)) (point))
  25241. ((not (eq last-command this-command)) (point))
  25242. (t (match-end 4))))))))))
  25243. (defun org-end-of-line (&optional arg)
  25244. "Go to the end of the line.
  25245. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25246. first attempt, and only move to after the tags when the cursor is already
  25247. beyond the end of the headline."
  25248. (interactive "P")
  25249. (if (or (not org-special-ctrl-a/e)
  25250. (not (org-on-heading-p)))
  25251. (end-of-line arg)
  25252. (let ((pos (point)))
  25253. (beginning-of-line 1)
  25254. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25255. (if (eq org-special-ctrl-a/e t)
  25256. (if (or (< pos (match-beginning 1))
  25257. (= pos (match-end 0)))
  25258. (goto-char (match-beginning 1))
  25259. (goto-char (match-end 0)))
  25260. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25261. (goto-char (match-end 0))
  25262. (goto-char (match-beginning 1))))
  25263. (end-of-line arg)))))
  25264. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25265. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25266. (defun org-invisible-p ()
  25267. "Check if point is at a character currently not visible."
  25268. ;; Early versions of noutline don't have `outline-invisible-p'.
  25269. (if (fboundp 'outline-invisible-p)
  25270. (outline-invisible-p)
  25271. (get-char-property (point) 'invisible)))
  25272. (defun org-invisible-p2 ()
  25273. "Check if point is at a character currently not visible."
  25274. (save-excursion
  25275. (if (and (eolp) (not (bobp))) (backward-char 1))
  25276. ;; Early versions of noutline don't have `outline-invisible-p'.
  25277. (if (fboundp 'outline-invisible-p)
  25278. (outline-invisible-p)
  25279. (get-char-property (point) 'invisible))))
  25280. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25281. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25282. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25283. (defun org-at-heading-or-item-p ()
  25284. (or (org-on-heading-p) (org-at-item-p)))
  25285. (defun org-on-target-p ()
  25286. (or (org-in-regexp org-radio-target-regexp)
  25287. (org-in-regexp org-target-regexp)))
  25288. (defun org-up-heading-all (arg)
  25289. "Move to the heading line of which the present line is a subheading.
  25290. This function considers both visible and invisible heading lines.
  25291. With argument, move up ARG levels."
  25292. (if (fboundp 'outline-up-heading-all)
  25293. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25294. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25295. (defun org-up-heading-safe ()
  25296. "Move to the heading line of which the present line is a subheading.
  25297. This version will not throw an error. It will return the level of the
  25298. headline found, or nil if no higher level is found."
  25299. (let ((pos (point)) start-level level
  25300. (re (concat "^" outline-regexp)))
  25301. (catch 'exit
  25302. (outline-back-to-heading t)
  25303. (setq start-level (funcall outline-level))
  25304. (if (equal start-level 1) (throw 'exit nil))
  25305. (while (re-search-backward re nil t)
  25306. (setq level (funcall outline-level))
  25307. (if (< level start-level) (throw 'exit level)))
  25308. nil)))
  25309. (defun org-first-sibling-p ()
  25310. "Is this heading the first child of its parents?"
  25311. (interactive)
  25312. (let ((re (concat "^" outline-regexp))
  25313. level l)
  25314. (unless (org-at-heading-p t)
  25315. (error "Not at a heading"))
  25316. (setq level (funcall outline-level))
  25317. (save-excursion
  25318. (if (not (re-search-backward re nil t))
  25319. t
  25320. (setq l (funcall outline-level))
  25321. (< l level)))))
  25322. (defun org-goto-sibling (&optional previous)
  25323. "Goto the next sibling, even if it is invisible.
  25324. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25325. when a sibling was found. When none is found, return nil and don't
  25326. move point."
  25327. (let ((fun (if previous 're-search-backward 're-search-forward))
  25328. (pos (point))
  25329. (re (concat "^" outline-regexp))
  25330. level l)
  25331. (when (condition-case nil (org-back-to-heading t) (error nil))
  25332. (setq level (funcall outline-level))
  25333. (catch 'exit
  25334. (or previous (forward-char 1))
  25335. (while (funcall fun re nil t)
  25336. (setq l (funcall outline-level))
  25337. (when (< l level) (goto-char pos) (throw 'exit nil))
  25338. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25339. (goto-char pos)
  25340. nil))))
  25341. (defun org-show-siblings ()
  25342. "Show all siblings of the current headline."
  25343. (save-excursion
  25344. (while (org-goto-sibling) (org-flag-heading nil)))
  25345. (save-excursion
  25346. (while (org-goto-sibling 'previous)
  25347. (org-flag-heading nil))))
  25348. (defun org-show-hidden-entry ()
  25349. "Show an entry where even the heading is hidden."
  25350. (save-excursion
  25351. (org-show-entry)))
  25352. (defun org-flag-heading (flag &optional entry)
  25353. "Flag the current heading. FLAG non-nil means make invisible.
  25354. When ENTRY is non-nil, show the entire entry."
  25355. (save-excursion
  25356. (org-back-to-heading t)
  25357. ;; Check if we should show the entire entry
  25358. (if entry
  25359. (progn
  25360. (org-show-entry)
  25361. (save-excursion
  25362. (and (outline-next-heading)
  25363. (org-flag-heading nil))))
  25364. (outline-flag-region (max (point-min) (1- (point)))
  25365. (save-excursion (outline-end-of-heading) (point))
  25366. flag))))
  25367. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25368. ;; This is an exact copy of the original function, but it uses
  25369. ;; `org-back-to-heading', to make it work also in invisible
  25370. ;; trees. And is uses an invisible-OK argument.
  25371. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25372. (org-back-to-heading invisible-OK)
  25373. (let ((first t)
  25374. (level (funcall outline-level)))
  25375. (while (and (not (eobp))
  25376. (or first (> (funcall outline-level) level)))
  25377. (setq first nil)
  25378. (outline-next-heading))
  25379. (unless to-heading
  25380. (if (memq (preceding-char) '(?\n ?\^M))
  25381. (progn
  25382. ;; Go to end of line before heading
  25383. (forward-char -1)
  25384. (if (memq (preceding-char) '(?\n ?\^M))
  25385. ;; leave blank line before heading
  25386. (forward-char -1))))))
  25387. (point))
  25388. (defun org-show-subtree ()
  25389. "Show everything after this heading at deeper levels."
  25390. (outline-flag-region
  25391. (point)
  25392. (save-excursion
  25393. (outline-end-of-subtree) (outline-next-heading) (point))
  25394. nil))
  25395. (defun org-show-entry ()
  25396. "Show the body directly following this heading.
  25397. Show the heading too, if it is currently invisible."
  25398. (interactive)
  25399. (save-excursion
  25400. (condition-case nil
  25401. (progn
  25402. (org-back-to-heading t)
  25403. (outline-flag-region
  25404. (max (point-min) (1- (point)))
  25405. (save-excursion
  25406. (re-search-forward
  25407. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25408. (or (match-beginning 1) (point-max)))
  25409. nil))
  25410. (error nil))))
  25411. (defun org-make-options-regexp (kwds)
  25412. "Make a regular expression for keyword lines."
  25413. (concat
  25414. "^"
  25415. "#?[ \t]*\\+\\("
  25416. (mapconcat 'regexp-quote kwds "\\|")
  25417. "\\):[ \t]*"
  25418. "\\(.+\\)"))
  25419. ;; Make isearch reveal the necessary context
  25420. (defun org-isearch-end ()
  25421. "Reveal context after isearch exits."
  25422. (when isearch-success ; only if search was successful
  25423. (if (featurep 'xemacs)
  25424. ;; Under XEmacs, the hook is run in the correct place,
  25425. ;; we directly show the context.
  25426. (org-show-context 'isearch)
  25427. ;; In Emacs the hook runs *before* restoring the overlays.
  25428. ;; So we have to use a one-time post-command-hook to do this.
  25429. ;; (Emacs 22 has a special variable, see function `org-mode')
  25430. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25431. isearch-mode-end-hook-quit)
  25432. ;; Only when the isearch was not quitted.
  25433. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25434. 'append 'local)))))
  25435. (defun org-isearch-post-command ()
  25436. "Remove self from hook, and show context."
  25437. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25438. (org-show-context 'isearch))
  25439. ;;;; Integration with and fixes for other packages
  25440. ;;; Imenu support
  25441. (defvar org-imenu-markers nil
  25442. "All markers currently used by Imenu.")
  25443. (make-variable-buffer-local 'org-imenu-markers)
  25444. (defun org-imenu-new-marker (&optional pos)
  25445. "Return a new marker for use by Imenu, and remember the marker."
  25446. (let ((m (make-marker)))
  25447. (move-marker m (or pos (point)))
  25448. (push m org-imenu-markers)
  25449. m))
  25450. (defun org-imenu-get-tree ()
  25451. "Produce the index for Imenu."
  25452. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25453. (setq org-imenu-markers nil)
  25454. (let* ((n org-imenu-depth)
  25455. (re (concat "^" outline-regexp))
  25456. (subs (make-vector (1+ n) nil))
  25457. (last-level 0)
  25458. m tree level head)
  25459. (save-excursion
  25460. (save-restriction
  25461. (widen)
  25462. (goto-char (point-max))
  25463. (while (re-search-backward re nil t)
  25464. (setq level (org-reduced-level (funcall outline-level)))
  25465. (when (<= level n)
  25466. (looking-at org-complex-heading-regexp)
  25467. (setq head (org-match-string-no-properties 4)
  25468. m (org-imenu-new-marker))
  25469. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25470. (if (>= level last-level)
  25471. (push (cons head m) (aref subs level))
  25472. (push (cons head (aref subs (1+ level))) (aref subs level))
  25473. (loop for i from (1+ level) to n do (aset subs i nil)))
  25474. (setq last-level level)))))
  25475. (aref subs 1)))
  25476. (eval-after-load "imenu"
  25477. '(progn
  25478. (add-hook 'imenu-after-jump-hook
  25479. (lambda () (org-show-context 'org-goto)))))
  25480. ;; Speedbar support
  25481. (defun org-speedbar-set-agenda-restriction ()
  25482. "Restrict future agenda commands to the location at point in speedbar.
  25483. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  25484. (interactive)
  25485. (let (p m tp np dir txt w)
  25486. (cond
  25487. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25488. 'org-imenu t))
  25489. (setq m (get-text-property p 'org-imenu-marker))
  25490. (save-excursion
  25491. (save-restriction
  25492. (set-buffer (marker-buffer m))
  25493. (goto-char m)
  25494. (org-agenda-set-restriction-lock 'subtree))))
  25495. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25496. 'speedbar-function 'speedbar-find-file))
  25497. (setq tp (previous-single-property-change
  25498. (1+ p) 'speedbar-function)
  25499. np (next-single-property-change
  25500. tp 'speedbar-function)
  25501. dir (speedbar-line-directory)
  25502. txt (buffer-substring-no-properties (or tp (point-min))
  25503. (or np (point-max))))
  25504. (save-excursion
  25505. (save-restriction
  25506. (set-buffer (find-file-noselect
  25507. (let ((default-directory dir))
  25508. (expand-file-name txt))))
  25509. (unless (org-mode-p)
  25510. (error "Cannot restrict to non-Org-mode file"))
  25511. (org-agenda-set-restriction-lock 'file))))
  25512. (t (error "Don't know how to restrict Org-mode's agenda")))
  25513. (org-move-overlay org-speedbar-restriction-lock-overlay
  25514. (point-at-bol) (point-at-eol))
  25515. (setq current-prefix-arg nil)
  25516. (org-agenda-maybe-redo)))
  25517. (eval-after-load "speedbar"
  25518. '(progn
  25519. (speedbar-add-supported-extension ".org")
  25520. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  25521. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  25522. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  25523. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  25524. (add-hook 'speedbar-visiting-tag-hook
  25525. (lambda () (org-show-context 'org-goto)))))
  25526. ;;; Fixes and Hacks
  25527. ;; Make flyspell not check words in links, to not mess up our keymap
  25528. (defun org-mode-flyspell-verify ()
  25529. "Don't let flyspell put overlays at active buttons."
  25530. (not (get-text-property (point) 'keymap)))
  25531. ;; Make `bookmark-jump' show the jump location if it was hidden.
  25532. (eval-after-load "bookmark"
  25533. '(if (boundp 'bookmark-after-jump-hook)
  25534. ;; We can use the hook
  25535. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  25536. ;; Hook not available, use advice
  25537. (defadvice bookmark-jump (after org-make-visible activate)
  25538. "Make the position visible."
  25539. (org-bookmark-jump-unhide))))
  25540. (defun org-bookmark-jump-unhide ()
  25541. "Unhide the current position, to show the bookmark location."
  25542. (and (org-mode-p)
  25543. (or (org-invisible-p)
  25544. (save-excursion (goto-char (max (point-min) (1- (point))))
  25545. (org-invisible-p)))
  25546. (org-show-context 'bookmark-jump)))
  25547. ;; Fix a bug in htmlize where there are text properties (face nil)
  25548. (eval-after-load "htmlize"
  25549. '(progn
  25550. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  25551. "Make sure there are no nil faces"
  25552. (setq ad-return-value (delq nil ad-return-value)))))
  25553. ;; Make session.el ignore our circular variable
  25554. (eval-after-load "session"
  25555. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  25556. ;;;; Experimental code
  25557. (defun org-closed-in-range ()
  25558. "Sparse tree of items closed in a certain time range.
  25559. Still experimental, may disappear in the future."
  25560. (interactive)
  25561. ;; Get the time interval from the user.
  25562. (let* ((time1 (time-to-seconds
  25563. (org-read-date nil 'to-time nil "Starting date: ")))
  25564. (time2 (time-to-seconds
  25565. (org-read-date nil 'to-time nil "End date:")))
  25566. ;; callback function
  25567. (callback (lambda ()
  25568. (let ((time
  25569. (time-to-seconds
  25570. (apply 'encode-time
  25571. (org-parse-time-string
  25572. (match-string 1))))))
  25573. ;; check if time in interval
  25574. (and (>= time time1) (<= time time2))))))
  25575. ;; make tree, check each match with the callback
  25576. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  25577. (defun org-first-list-item-p ()
  25578. "Is this heading the item in a plain list?"
  25579. (unless (org-at-item-p)
  25580. (error "Not at a plain list item"))
  25581. (org-beginning-of-item)
  25582. (= (point) (save-excursion (org-beginning-of-item-list))))
  25583. ;;;; Finish up
  25584. (provide 'org)
  25585. (run-hooks 'org-load-hook)
  25586. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  25587. ;;; org.el ends here