org.el 1000 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229
  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.14-alpha7
  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. (require 'easymenu)
  77. ;;;; Customization variables
  78. ;;; Version
  79. (defconst org-version "5.14-alpha7"
  80. "The version number of the file org.el.")
  81. (defun org-version ()
  82. (interactive)
  83. (message "Org-mode version %s" org-version))
  84. ;;; Compatibility constants
  85. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  86. (defconst org-format-transports-properties-p
  87. (let ((x "a"))
  88. (add-text-properties 0 1 '(test t) x)
  89. (get-text-property 0 'test (format "%s" x)))
  90. "Does format transport text properties?")
  91. (defmacro org-unmodified (&rest body)
  92. "Execute body without changing buffer-modified-p."
  93. `(set-buffer-modified-p
  94. (prog1 (buffer-modified-p) ,@body)))
  95. (defmacro org-re (s)
  96. "Replace posix classes in regular expression."
  97. (if (featurep 'xemacs)
  98. (let ((ss s))
  99. (save-match-data
  100. (while (string-match "\\[:alnum:\\]" ss)
  101. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  102. (while (string-match "\\[:alpha:\\]" ss)
  103. (setq ss (replace-match "a-zA-Z" t t ss)))
  104. ss))
  105. s))
  106. (defmacro org-preserve-lc (&rest body)
  107. `(let ((_line (org-current-line))
  108. (_col (current-column)))
  109. (unwind-protect
  110. (progn ,@body)
  111. (goto-line _line)
  112. (move-to-column _col))))
  113. (defmacro org-without-partial-completion (&rest body)
  114. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  115. partial-completion-mode)))
  116. (unwind-protect
  117. (progn
  118. (if pc-mode (partial-completion-mode -1))
  119. ,@body)
  120. (if pc-mode (partial-completion-mode 1)))))
  121. ;;; The custom variables
  122. (defgroup org nil
  123. "Outline-based notes management and organizer."
  124. :tag "Org"
  125. :group 'outlines
  126. :group 'hypermedia
  127. :group 'calendar)
  128. ;; FIXME: Needs a separate group...
  129. (defcustom org-completion-fallback-command 'hippie-expand
  130. "The expansion command called by \\[org-complete] in normal context.
  131. Normal means, no org-mode-specific context."
  132. :group 'org
  133. :type 'function)
  134. (defgroup org-startup nil
  135. "Options concerning startup of Org-mode."
  136. :tag "Org Startup"
  137. :group 'org)
  138. (defcustom org-startup-folded t
  139. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  140. This can also be configured on a per-file basis by adding one of
  141. the following lines anywhere in the buffer:
  142. #+STARTUP: fold
  143. #+STARTUP: nofold
  144. #+STARTUP: content"
  145. :group 'org-startup
  146. :type '(choice
  147. (const :tag "nofold: show all" nil)
  148. (const :tag "fold: overview" t)
  149. (const :tag "content: all headlines" content)))
  150. (defcustom org-startup-truncated t
  151. "Non-nil means, entering Org-mode will set `truncate-lines'.
  152. This is useful since some lines containing links can be very long and
  153. uninteresting. Also tables look terrible when wrapped."
  154. :group 'org-startup
  155. :type 'boolean)
  156. (defcustom org-startup-align-all-tables nil
  157. "Non-nil means, align all tables when visiting a file.
  158. This is useful when the column width in tables is forced with <N> cookies
  159. in table fields. Such tables will look correct only after the first re-align.
  160. This can also be configured on a per-file basis by adding one of
  161. the following lines anywhere in the buffer:
  162. #+STARTUP: align
  163. #+STARTUP: noalign"
  164. :group 'org-startup
  165. :type 'boolean)
  166. (defcustom org-insert-mode-line-in-empty-file nil
  167. "Non-nil means insert the first line setting Org-mode in empty files.
  168. When the function `org-mode' is called interactively in an empty file, this
  169. normally means that the file name does not automatically trigger Org-mode.
  170. To ensure that the file will always be in Org-mode in the future, a
  171. line enforcing Org-mode will be inserted into the buffer, if this option
  172. has been set."
  173. :group 'org-startup
  174. :type 'boolean)
  175. (defcustom org-replace-disputed-keys nil
  176. "Non-nil means use alternative key bindings for some keys.
  177. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  178. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  179. If you want to use Org-mode together with one of these other modes,
  180. or more generally if you would like to move some Org-mode commands to
  181. other keys, set this variable and configure the keys with the variable
  182. `org-disputed-keys'.
  183. This option is only relevant at load-time of Org-mode, and must be set
  184. *before* org.el is loaded. Changing it requires a restart of Emacs to
  185. become effective."
  186. :group 'org-startup
  187. :type 'boolean)
  188. (if (fboundp 'defvaralias)
  189. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  190. (defcustom org-disputed-keys
  191. '(([(shift up)] . [(meta p)])
  192. ([(shift down)] . [(meta n)])
  193. ([(shift left)] . [(meta -)])
  194. ([(shift right)] . [(meta +)])
  195. ([(control shift right)] . [(meta shift +)])
  196. ([(control shift left)] . [(meta shift -)]))
  197. "Keys for which Org-mode and other modes compete.
  198. This is an alist, cars are the default keys, second element specifies
  199. the alternative to use when `org-replace-disputed-keys' is t.
  200. Keys can be specified in any syntax supported by `define-key'.
  201. The value of this option takes effect only at Org-mode's startup,
  202. therefore you'll have to restart Emacs to apply it after changing."
  203. :group 'org-startup
  204. :type 'alist)
  205. (defun org-key (key)
  206. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  207. Or return the original if not disputed."
  208. (if org-replace-disputed-keys
  209. (let* ((nkey (key-description key))
  210. (x (org-find-if (lambda (x)
  211. (equal (key-description (car x)) nkey))
  212. org-disputed-keys)))
  213. (if x (cdr x) key))
  214. key))
  215. (defun org-find-if (predicate seq)
  216. (catch 'exit
  217. (while seq
  218. (if (funcall predicate (car seq))
  219. (throw 'exit (car seq))
  220. (pop seq)))))
  221. (defun org-defkey (keymap key def)
  222. "Define a key, possibly translated, as returned by `org-key'."
  223. (define-key keymap (org-key key) def))
  224. (defcustom org-ellipsis nil
  225. "The ellipsis to use in the Org-mode outline.
  226. When nil, just use the standard three dots. When a string, use that instead,
  227. When a face, use the standart 3 dots, but with the specified face.
  228. The change affects only Org-mode (which will then use its own display table).
  229. Changing this requires executing `M-x org-mode' in a buffer to become
  230. effective."
  231. :group 'org-startup
  232. :type '(choice (const :tag "Default" nil)
  233. (face :tag "Face" :value org-warning)
  234. (string :tag "String" :value "...#")))
  235. (defvar org-display-table nil
  236. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  237. (defgroup org-keywords nil
  238. "Keywords in Org-mode."
  239. :tag "Org Keywords"
  240. :group 'org)
  241. (defcustom org-deadline-string "DEADLINE:"
  242. "String to mark deadline entries.
  243. A deadline is this string, followed by a time stamp. Should be a word,
  244. terminated by a colon. You can insert a schedule keyword and
  245. a timestamp with \\[org-deadline].
  246. Changes become only effective after restarting Emacs."
  247. :group 'org-keywords
  248. :type 'string)
  249. (defcustom org-scheduled-string "SCHEDULED:"
  250. "String to mark scheduled TODO entries.
  251. A schedule is this string, followed by a time stamp. Should be a word,
  252. terminated by a colon. You can insert a schedule keyword and
  253. a timestamp with \\[org-schedule].
  254. Changes become only effective after restarting Emacs."
  255. :group 'org-keywords
  256. :type 'string)
  257. (defcustom org-closed-string "CLOSED:"
  258. "String used as the prefix for timestamps logging closing a TODO entry."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-clock-string "CLOCK:"
  262. "String used as prefix for timestamps clocking work hours on an item."
  263. :group 'org-keywords
  264. :type 'string)
  265. (defcustom org-comment-string "COMMENT"
  266. "Entries starting with this keyword will never be exported.
  267. An entry can be toggled between COMMENT and normal with
  268. \\[org-toggle-comment].
  269. Changes become only effective after restarting Emacs."
  270. :group 'org-keywords
  271. :type 'string)
  272. (defcustom org-quote-string "QUOTE"
  273. "Entries starting with this keyword will be exported in fixed-width font.
  274. Quoting applies only to the text in the entry following the headline, and does
  275. not extend beyond the next headline, even if that is lower level.
  276. An entry can be toggled between QUOTE and normal with
  277. \\[org-toggle-fixed-width-section]."
  278. :group 'org-keywords
  279. :type 'string)
  280. (defconst org-repeat-re
  281. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  282. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  283. "Regular expression for specifying repeated events.
  284. After a match, group 1 contains the repeat expression.")
  285. (defgroup org-structure nil
  286. "Options concerning the general structure of Org-mode files."
  287. :tag "Org Structure"
  288. :group 'org)
  289. (defgroup org-reveal-location nil
  290. "Options about how to make context of a location visible."
  291. :tag "Org Reveal Location"
  292. :group 'org-structure)
  293. (defcustom org-show-hierarchy-above '((default . t))
  294. "Non-nil means, show full hierarchy when revealing a location.
  295. Org-mode often shows locations in an org-mode file which might have
  296. been invisible before. When this is set, the hierarchy of headings
  297. above the exposed location is shown.
  298. Turning this off for example for sparse trees makes them very compact.
  299. Instead of t, this can also be an alist specifying this option for different
  300. contexts. Valid contexts are
  301. agenda when exposing an entry from the agenda
  302. org-goto when using the command `org-goto' on key C-c C-j
  303. occur-tree when using the command `org-occur' on key C-c /
  304. tags-tree when constructing a sparse tree based on tags matches
  305. link-search when exposing search matches associated with a link
  306. mark-goto when exposing the jump goal of a mark
  307. bookmark-jump when exposing a bookmark location
  308. isearch when exiting from an incremental search
  309. default default for all contexts not set explicitly"
  310. :group 'org-reveal-location
  311. :type '(choice
  312. (const :tag "Always" t)
  313. (const :tag "Never" nil)
  314. (repeat :greedy t :tag "Individual contexts"
  315. (cons
  316. (choice :tag "Context"
  317. (const agenda)
  318. (const org-goto)
  319. (const occur-tree)
  320. (const tags-tree)
  321. (const link-search)
  322. (const mark-goto)
  323. (const bookmark-jump)
  324. (const isearch)
  325. (const default))
  326. (boolean)))))
  327. (defcustom org-show-following-heading '((default . nil))
  328. "Non-nil means, show following heading when revealing a location.
  329. Org-mode often shows locations in an org-mode file which might have
  330. been invisible before. When this is set, the heading following the
  331. match is shown.
  332. Turning this off for example for sparse trees makes them very compact,
  333. but makes it harder to edit the location of the match. In such a case,
  334. use the command \\[org-reveal] to show more context.
  335. Instead of t, this can also be an alist specifying this option for different
  336. contexts. See `org-show-hierarchy-above' for valid contexts."
  337. :group 'org-reveal-location
  338. :type '(choice
  339. (const :tag "Always" t)
  340. (const :tag "Never" nil)
  341. (repeat :greedy t :tag "Individual contexts"
  342. (cons
  343. (choice :tag "Context"
  344. (const agenda)
  345. (const org-goto)
  346. (const occur-tree)
  347. (const tags-tree)
  348. (const link-search)
  349. (const mark-goto)
  350. (const bookmark-jump)
  351. (const isearch)
  352. (const default))
  353. (boolean)))))
  354. (defcustom org-show-siblings '((default . nil) (isearch t))
  355. "Non-nil means, show all sibling heading when revealing a location.
  356. Org-mode often shows locations in an org-mode file which might have
  357. been invisible before. When this is set, the sibling of the current entry
  358. heading are all made visible. If `org-show-hierarchy-above' is t,
  359. the same happens on each level of the hierarchy above the current entry.
  360. By default this is on for the isearch context, off for all other contexts.
  361. Turning this off for example for sparse trees makes them very compact,
  362. but makes it harder to edit the location of the match. In such a case,
  363. use the command \\[org-reveal] to show more context.
  364. Instead of t, this can also be an alist specifying this option for different
  365. contexts. See `org-show-hierarchy-above' for valid contexts."
  366. :group 'org-reveal-location
  367. :type '(choice
  368. (const :tag "Always" t)
  369. (const :tag "Never" nil)
  370. (repeat :greedy t :tag "Individual contexts"
  371. (cons
  372. (choice :tag "Context"
  373. (const agenda)
  374. (const org-goto)
  375. (const occur-tree)
  376. (const tags-tree)
  377. (const link-search)
  378. (const mark-goto)
  379. (const bookmark-jump)
  380. (const isearch)
  381. (const default))
  382. (boolean)))))
  383. (defcustom org-show-entry-below '((default . nil))
  384. "Non-nil means, show the entry below a headline when revealing a location.
  385. Org-mode often shows locations in an org-mode file which might have
  386. been invisible before. When this is set, the text below the headline that is
  387. exposed is also shown.
  388. By default this is off for all contexts.
  389. Instead of t, this can also be an alist specifying this option for different
  390. contexts. See `org-show-hierarchy-above' for valid contexts."
  391. :group 'org-reveal-location
  392. :type '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean)))))
  408. (defgroup org-cycle nil
  409. "Options concerning visibility cycling in Org-mode."
  410. :tag "Org Cycle"
  411. :group 'org-structure)
  412. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  413. "Names of drawers. Drawers are not opened by cycling on the headline above.
  414. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  415. this:
  416. :DRAWERNAME:
  417. .....
  418. :END:
  419. The drawer \"PROPERTIES\" is special for capturing properties through
  420. the property API.
  421. Drawers can be defined on the per-file basis with a line like:
  422. #+DRAWERS: HIDDEN STATE PROPERTIES"
  423. :group 'org-structure
  424. :type '(repeat (string :tag "Drawer Name")))
  425. (defcustom org-cycle-global-at-bob nil
  426. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  427. This makes it possible to do global cycling without having to use S-TAB or
  428. C-u TAB. For this special case to work, the first line of the buffer
  429. must not be a headline - it may be empty ot some other text. When used in
  430. this way, `org-cycle-hook' is disables temporarily, to make sure the
  431. cursor stays at the beginning of the buffer.
  432. When this option is nil, don't do anything special at the beginning
  433. of the buffer."
  434. :group 'org-cycle
  435. :type 'boolean)
  436. (defcustom org-cycle-emulate-tab t
  437. "Where should `org-cycle' emulate TAB.
  438. nil Never
  439. white Only in completely white lines
  440. whitestart Only at the beginning of lines, before the first non-white char.
  441. t Everywhere except in headlines
  442. exc-hl-bol Everywhere except at the start of a headline
  443. If TAB is used in a place where it does not emulate TAB, the current subtree
  444. visibility is cycled."
  445. :group 'org-cycle
  446. :type '(choice (const :tag "Never" nil)
  447. (const :tag "Only in completely white lines" white)
  448. (const :tag "Before first char in a line" whitestart)
  449. (const :tag "Everywhere except in headlines" t)
  450. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  451. ))
  452. (defcustom org-cycle-separator-lines 2
  453. "Number of empty lines needed to keep an empty line between collapsed trees.
  454. If you leave an empty line between the end of a subtree and the following
  455. headline, this empty line is hidden when the subtree is folded.
  456. Org-mode will leave (exactly) one empty line visible if the number of
  457. empty lines is equal or larger to the number given in this variable.
  458. So the default 2 means, at least 2 empty lines after the end of a subtree
  459. are needed to produce free space between a collapsed subtree and the
  460. following headline.
  461. Special case: when 0, never leave empty lines in collapsed view."
  462. :group 'org-cycle
  463. :type 'integer)
  464. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  465. org-cycle-hide-drawers
  466. org-cycle-show-empty-lines
  467. org-optimize-window-after-visibility-change)
  468. "Hook that is run after `org-cycle' has changed the buffer visibility.
  469. The function(s) in this hook must accept a single argument which indicates
  470. the new state that was set by the most recent `org-cycle' command. The
  471. argument is a symbol. After a global state change, it can have the values
  472. `overview', `content', or `all'. After a local state change, it can have
  473. the values `folded', `children', or `subtree'."
  474. :group 'org-cycle
  475. :type 'hook)
  476. (defgroup org-edit-structure nil
  477. "Options concerning structure editing in Org-mode."
  478. :tag "Org Edit Structure"
  479. :group 'org-structure)
  480. (defcustom org-special-ctrl-a/e nil
  481. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  482. When t, `C-a' will bring back the cursor to the beginning of the
  483. headline text, i.e. after the stars and after a possible TODO keyword.
  484. In an item, this will be the position after the bullet.
  485. When the cursor is already at that position, another `C-a' will bring
  486. it to the beginning of the line.
  487. `C-e' will jump to the end of the headline, ignoring the presence of tags
  488. in the headline. A second `C-e' will then jump to the true end of the
  489. line, after any tags.
  490. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  491. and only a directly following, identical keypress will bring the cursor
  492. to the special positions."
  493. :group 'org-edit-structure
  494. :type '(choice
  495. (const :tag "off" nil)
  496. (const :tag "after bullet first" t)
  497. (const :tag "border first" reversed)))
  498. (if (fboundp 'defvaralias)
  499. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  500. (defcustom org-odd-levels-only nil
  501. "Non-nil means, skip even levels and only use odd levels for the outline.
  502. This has the effect that two stars are being added/taken away in
  503. promotion/demotion commands. It also influences how levels are
  504. handled by the exporters.
  505. Changing it requires restart of `font-lock-mode' to become effective
  506. for fontification also in regions already fontified.
  507. You may also set this on a per-file basis by adding one of the following
  508. lines to the buffer:
  509. #+STARTUP: odd
  510. #+STARTUP: oddeven"
  511. :group 'org-edit-structure
  512. :group 'org-font-lock
  513. :type 'boolean)
  514. (defcustom org-adapt-indentation t
  515. "Non-nil means, adapt indentation when promoting and demoting.
  516. When this is set and the *entire* text in an entry is indented, the
  517. indentation is increased by one space in a demotion command, and
  518. decreased by one in a promotion command. If any line in the entry
  519. body starts at column 0, indentation is not changed at all."
  520. :group 'org-edit-structure
  521. :type 'boolean)
  522. (defcustom org-blank-before-new-entry '((heading . nil)
  523. (plain-list-item . nil))
  524. "Should `org-insert-heading' leave a blank line before new heading/item?
  525. The value is an alist, with `heading' and `plain-list-item' as car,
  526. and a boolean flag as cdr."
  527. :group 'org-edit-structure
  528. :type '(list
  529. (cons (const heading) (boolean))
  530. (cons (const plain-list-item) (boolean))))
  531. (defcustom org-insert-heading-hook nil
  532. "Hook being run after inserting a new heading."
  533. :group 'org-edit-structure
  534. :type 'hook)
  535. (defcustom org-enable-fixed-width-editor t
  536. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  537. This currently only means, they are never auto-wrapped.
  538. When nil, such lines will be treated like ordinary lines.
  539. See also the QUOTE keyword."
  540. :group 'org-edit-structure
  541. :type 'boolean)
  542. (defgroup org-sparse-trees nil
  543. "Options concerning sparse trees in Org-mode."
  544. :tag "Org Sparse Trees"
  545. :group 'org-structure)
  546. (defcustom org-highlight-sparse-tree-matches t
  547. "Non-nil means, highlight all matches that define a sparse tree.
  548. The highlights will automatically disappear the next time the buffer is
  549. changed by an edit command."
  550. :group 'org-sparse-trees
  551. :type 'boolean)
  552. (defcustom org-remove-highlights-with-change t
  553. "Non-nil means, any change to the buffer will remove temporary highlights.
  554. Such highlights are created by `org-occur' and `org-clock-display'.
  555. When nil, `C-c C-c needs to be used to get rid of the highlights.
  556. The highlights created by `org-preview-latex-fragment' always need
  557. `C-c C-c' to be removed."
  558. :group 'org-sparse-trees
  559. :group 'org-time
  560. :type 'boolean)
  561. (defcustom org-occur-hook '(org-first-headline-recenter)
  562. "Hook that is run after `org-occur' has constructed a sparse tree.
  563. This can be used to recenter the window to show as much of the structure
  564. as possible."
  565. :group 'org-sparse-trees
  566. :type 'hook)
  567. (defgroup org-plain-lists nil
  568. "Options concerning plain lists in Org-mode."
  569. :tag "Org Plain lists"
  570. :group 'org-structure)
  571. (defcustom org-cycle-include-plain-lists nil
  572. "Non-nil means, include plain lists into visibility cycling.
  573. This means that during cycling, plain list items will *temporarily* be
  574. interpreted as outline headlines with a level given by 1000+i where i is the
  575. indentation of the bullet. In all other operations, plain list items are
  576. not seen as headlines. For example, you cannot assign a TODO keyword to
  577. such an item."
  578. :group 'org-plain-lists
  579. :type 'boolean)
  580. (defcustom org-plain-list-ordered-item-terminator t
  581. "The character that makes a line with leading number an ordered list item.
  582. Valid values are ?. and ?\). To get both terminators, use t. While
  583. ?. may look nicer, it creates the danger that a line with leading
  584. number may be incorrectly interpreted as an item. ?\) therefore is
  585. the safe choice."
  586. :group 'org-plain-lists
  587. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  588. (const :tag "paren like in \"2)\"" ?\))
  589. (const :tab "both" t)))
  590. (defcustom org-auto-renumber-ordered-lists t
  591. "Non-nil means, automatically renumber ordered plain lists.
  592. Renumbering happens when the sequence have been changed with
  593. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  594. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  595. :group 'org-plain-lists
  596. :type 'boolean)
  597. (defcustom org-provide-checkbox-statistics t
  598. "Non-nil means, update checkbox statistics after insert and toggle.
  599. When this is set, checkbox statistics is updated each time you either insert
  600. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  601. with \\[org-ctrl-c-ctrl-c\\]."
  602. :group 'org-plain-lists
  603. :type 'boolean)
  604. (defgroup org-archive nil
  605. "Options concerning archiving in Org-mode."
  606. :tag "Org Archive"
  607. :group 'org-structure)
  608. (defcustom org-archive-tag "ARCHIVE"
  609. "The tag that marks a subtree as archived.
  610. An archived subtree does not open during visibility cycling, and does
  611. not contribute to the agenda listings.
  612. After changing this, font-lock must be restarted in the relevant buffers to
  613. get the proper fontification."
  614. :group 'org-archive
  615. :group 'org-keywords
  616. :type 'string)
  617. (defcustom org-agenda-skip-archived-trees t
  618. "Non-nil means, the agenda will skip any items located in archived trees.
  619. An archived tree is a tree marked with the tag ARCHIVE."
  620. :group 'org-archive
  621. :group 'org-agenda-skip
  622. :type 'boolean)
  623. (defcustom org-cycle-open-archived-trees nil
  624. "Non-nil means, `org-cycle' will open archived trees.
  625. An archived tree is a tree marked with the tag ARCHIVE.
  626. When nil, archived trees will stay folded. You can still open them with
  627. normal outline commands like `show-all', but not with the cycling commands."
  628. :group 'org-archive
  629. :group 'org-cycle
  630. :type 'boolean)
  631. (defcustom org-sparse-tree-open-archived-trees nil
  632. "Non-nil means sparse tree construction shows matches in archived trees.
  633. When nil, matches in these trees are highlighted, but the trees are kept in
  634. collapsed state."
  635. :group 'org-archive
  636. :group 'org-sparse-trees
  637. :type 'boolean)
  638. (defcustom org-archive-location "%s_archive::"
  639. "The location where subtrees should be archived.
  640. This string consists of two parts, separated by a double-colon.
  641. The first part is a file name - when omitted, archiving happens in the same
  642. file. %s will be replaced by the current file name (without directory part).
  643. Archiving to a different file is useful to keep archived entries from
  644. contributing to the Org-mode Agenda.
  645. The part after the double colon is a headline. The archived entries will be
  646. filed under that headline. When omitted, the subtrees are simply filed away
  647. at the end of the file, as top-level entries.
  648. Here are a few examples:
  649. \"%s_archive::\"
  650. If the current file is Projects.org, archive in file
  651. Projects.org_archive, as top-level trees. This is the default.
  652. \"::* Archived Tasks\"
  653. Archive in the current file, under the top-level headline
  654. \"* Archived Tasks\".
  655. \"~/org/archive.org::\"
  656. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  657. \"basement::** Finished Tasks\"
  658. Archive in file ./basement (relative path), as level 3 trees
  659. below the level 2 heading \"** Finished Tasks\".
  660. You may set this option on a per-file basis by adding to the buffer a
  661. line like
  662. #+ARCHIVE: basement::** Finished Tasks"
  663. :group 'org-archive
  664. :type 'string)
  665. (defcustom org-archive-mark-done t
  666. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  667. This can be a string to set the keyword to use. When t, Org-mode will
  668. use the first keyword in its list that means done."
  669. :group 'org-archive
  670. :type '(choice
  671. (const :tag "No" nil)
  672. (const :tag "Yes" t)
  673. (string :tag "Use this keyword")))
  674. (defcustom org-archive-stamp-time t
  675. "Non-nil means, add a time stamp to entries moved to an archive file.
  676. This variable is obsolete and has no effect anymore, instead add ot remove
  677. `time' from the variablle `org-archive-save-context-info'."
  678. :group 'org-archive
  679. :type 'boolean)
  680. (defcustom org-archive-save-context-info '(time file category todo itags)
  681. "Parts of context info that should be stored as properties when archiving.
  682. When a subtree is moved to an archive file, it looses information given by
  683. context, like inherited tags, the category, and possibly also the TODO
  684. state (depending on the variable `org-archive-mark-done').
  685. This variable can be a list of any of the following symbols:
  686. time The time of archiving.
  687. file The file where the entry originates.
  688. itags The local tags, in the headline of the subtree.
  689. ltags The tags the subtree inherits from further up the hierarchy.
  690. todo The pre-archive TODO state.
  691. category The category, taken from file name or #+CATEGORY lines.
  692. For each symbol present in the list, a property will be created in
  693. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  694. information."
  695. :group 'org-archive
  696. :type '(set :greedy t
  697. (const :tag "Time" time)
  698. (const :tag "File" file)
  699. (const :tag "Category" category)
  700. (const :tag "TODO state" todo)
  701. (const :tag "TODO state" priority)
  702. (const :tag "Inherited tags" itags)
  703. (const :tag "Local tags" ltags)))
  704. (defgroup org-table nil
  705. "Options concerning tables in Org-mode."
  706. :tag "Org Table"
  707. :group 'org)
  708. (defcustom org-enable-table-editor 'optimized
  709. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  710. When nil, such lines will be treated like ordinary lines.
  711. When equal to the symbol `optimized', the table editor will be optimized to
  712. do the following:
  713. - Automatic overwrite mode in front of whitespace in table fields.
  714. This makes the structure of the table stay in tact as long as the edited
  715. field does not exceed the column width.
  716. - Minimize the number of realigns. Normally, the table is aligned each time
  717. TAB or RET are pressed to move to another field. With optimization this
  718. happens only if changes to a field might have changed the column width.
  719. Optimization requires replacing the functions `self-insert-command',
  720. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  721. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  722. very good at guessing when a re-align will be necessary, but you can always
  723. force one with \\[org-ctrl-c-ctrl-c].
  724. If you would like to use the optimized version in Org-mode, but the
  725. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  726. This variable can be used to turn on and off the table editor during a session,
  727. but in order to toggle optimization, a restart is required.
  728. See also the variable `org-table-auto-blank-field'."
  729. :group 'org-table
  730. :type '(choice
  731. (const :tag "off" nil)
  732. (const :tag "on" t)
  733. (const :tag "on, optimized" optimized)))
  734. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  735. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  736. In the optimized version, the table editor takes over all simple keys that
  737. normally just insert a character. In tables, the characters are inserted
  738. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  739. for empty fields). Outside tables, the correct binding of the keys is
  740. restored.
  741. The default for this option is t if the optimized version is also used in
  742. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  743. this variable requires a restart of Emacs to become effective."
  744. :group 'org-table
  745. :type 'boolean)
  746. (defcustom orgtbl-radio-table-templates
  747. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  748. % END RECEIVE ORGTBL %n
  749. \\begin{comment}
  750. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  751. | | |
  752. \\end{comment}\n")
  753. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  754. @c END RECEIVE ORGTBL %n
  755. @ignore
  756. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  757. | | |
  758. @end ignore\n")
  759. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  760. <!-- END RECEIVE ORGTBL %n -->
  761. <!--
  762. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  763. | | |
  764. -->\n"))
  765. "Templates for radio tables in different major modes.
  766. All occurrences of %n in a template will be replaced with the name of the
  767. table, obtained by prompting the user."
  768. :group 'org-table
  769. :type '(repeat
  770. (list (symbol :tag "Major mode")
  771. (string :tag "Format"))))
  772. (defgroup org-table-settings nil
  773. "Settings for tables in Org-mode."
  774. :tag "Org Table Settings"
  775. :group 'org-table)
  776. (defcustom org-table-default-size "5x2"
  777. "The default size for newly created tables, Columns x Rows."
  778. :group 'org-table-settings
  779. :type 'string)
  780. (defcustom org-table-number-regexp
  781. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  782. "Regular expression for recognizing numbers in table columns.
  783. If a table column contains mostly numbers, it will be aligned to the
  784. right. If not, it will be aligned to the left.
  785. The default value of this option is a regular expression which allows
  786. anything which looks remotely like a number as used in scientific
  787. context. For example, all of the following will be considered a
  788. number:
  789. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  790. Other options offered by the customize interface are more restrictive."
  791. :group 'org-table-settings
  792. :type '(choice
  793. (const :tag "Positive Integers"
  794. "^[0-9]+$")
  795. (const :tag "Integers"
  796. "^[-+]?[0-9]+$")
  797. (const :tag "Floating Point Numbers"
  798. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  799. (const :tag "Floating Point Number or Integer"
  800. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  801. (const :tag "Exponential, Floating point, Integer"
  802. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  803. (const :tag "Very General Number-Like, including hex"
  804. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  805. (string :tag "Regexp:")))
  806. (defcustom org-table-number-fraction 0.5
  807. "Fraction of numbers in a column required to make the column align right.
  808. In a column all non-white fields are considered. If at least this
  809. fraction of fields is matched by `org-table-number-fraction',
  810. alignment to the right border applies."
  811. :group 'org-table-settings
  812. :type 'number)
  813. (defgroup org-table-editing nil
  814. "Behavior of tables during editing in Org-mode."
  815. :tag "Org Table Editing"
  816. :group 'org-table)
  817. (defcustom org-table-automatic-realign t
  818. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  819. When nil, aligning is only done with \\[org-table-align], or after column
  820. removal/insertion."
  821. :group 'org-table-editing
  822. :type 'boolean)
  823. (defcustom org-table-auto-blank-field t
  824. "Non-nil means, automatically blank table field when starting to type into it.
  825. This only happens when typing immediately after a field motion
  826. command (TAB, S-TAB or RET).
  827. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  828. :group 'org-table-editing
  829. :type 'boolean)
  830. (defcustom org-table-tab-jumps-over-hlines t
  831. "Non-nil means, tab in the last column of a table with jump over a hline.
  832. If a horizontal separator line is following the current line,
  833. `org-table-next-field' can either create a new row before that line, or jump
  834. over the line. When this option is nil, a new line will be created before
  835. this line."
  836. :group 'org-table-editing
  837. :type 'boolean)
  838. (defcustom org-table-tab-recognizes-table.el t
  839. "Non-nil means, TAB will automatically notice a table.el table.
  840. When it sees such a table, it moves point into it and - if necessary -
  841. calls `table-recognize-table'."
  842. :group 'org-table-editing
  843. :type 'boolean)
  844. (defgroup org-table-calculation nil
  845. "Options concerning tables in Org-mode."
  846. :tag "Org Table Calculation"
  847. :group 'org-table)
  848. (defcustom org-table-use-standard-references t
  849. "Should org-mode work with table refrences like B3 instead of @3$2?
  850. Possible values are:
  851. nil never use them
  852. from accept as input, do not present for editing
  853. t: accept as input and present for editing"
  854. :group 'org-table-calculation
  855. :type '(choice
  856. (const :tag "Never, don't even check unser input for them" nil)
  857. (const :tag "Always, both as user input, and when editing" t)
  858. (const :tag "Convert user input, don't offer during editing" 'from)))
  859. (defcustom org-table-copy-increment t
  860. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  861. :group 'org-table-calculation
  862. :type 'boolean)
  863. (defcustom org-calc-default-modes
  864. '(calc-internal-prec 12
  865. calc-float-format (float 5)
  866. calc-angle-mode deg
  867. calc-prefer-frac nil
  868. calc-symbolic-mode nil
  869. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  870. calc-display-working-message t
  871. )
  872. "List with Calc mode settings for use in calc-eval for table formulas.
  873. The list must contain alternating symbols (Calc modes variables and values).
  874. Don't remove any of the default settings, just change the values. Org-mode
  875. relies on the variables to be present in the list."
  876. :group 'org-table-calculation
  877. :type 'plist)
  878. (defcustom org-table-formula-evaluate-inline t
  879. "Non-nil means, TAB and RET evaluate a formula in current table field.
  880. If the current field starts with an equal sign, it is assumed to be a formula
  881. which should be evaluated as described in the manual and in the documentation
  882. string of the command `org-table-eval-formula'. This feature requires the
  883. Emacs calc package.
  884. When this variable is nil, formula calculation is only available through
  885. the command \\[org-table-eval-formula]."
  886. :group 'org-table-calculation
  887. :type 'boolean)
  888. (defcustom org-table-formula-use-constants t
  889. "Non-nil means, interpret constants in formulas in tables.
  890. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  891. by the value given in `org-table-formula-constants', or by a value obtained
  892. from the `constants.el' package."
  893. :group 'org-table-calculation
  894. :type 'boolean)
  895. (defcustom org-table-formula-constants nil
  896. "Alist with constant names and values, for use in table formulas.
  897. The car of each element is a name of a constant, without the `$' before it.
  898. The cdr is the value as a string. For example, if you'd like to use the
  899. speed of light in a formula, you would configure
  900. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  901. and then use it in an equation like `$1*$c'.
  902. Constants can also be defined on a per-file basis using a line like
  903. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  904. :group 'org-table-calculation
  905. :type '(repeat
  906. (cons (string :tag "name")
  907. (string :tag "value"))))
  908. (defvar org-table-formula-constants-local nil
  909. "Local version of `org-table-formula-constants'.")
  910. (make-variable-buffer-local 'org-table-formula-constants-local)
  911. (defcustom org-table-allow-automatic-line-recalculation t
  912. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  913. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  914. :group 'org-table-calculation
  915. :type 'boolean)
  916. (defgroup org-link nil
  917. "Options concerning links in Org-mode."
  918. :tag "Org Link"
  919. :group 'org)
  920. (defvar org-link-abbrev-alist-local nil
  921. "Buffer-local version of `org-link-abbrev-alist', which see.
  922. The value of this is taken from the #+LINK lines.")
  923. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  924. (defcustom org-link-abbrev-alist nil
  925. "Alist of link abbreviations.
  926. The car of each element is a string, to be replaced at the start of a link.
  927. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  928. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  929. [[linkkey:tag][description]]
  930. If REPLACE is a string, the tag will simply be appended to create the link.
  931. If the string contains \"%s\", the tag will be inserted there. REPLACE may
  932. also be a function that will be called with the tag as the only argument to
  933. create the link. See the manual for examples."
  934. :group 'org-link
  935. :type 'alist)
  936. (defcustom org-descriptive-links t
  937. "Non-nil means, hide link part and only show description of bracket links.
  938. Bracket links are like [[link][descritpion]]. This variable sets the initial
  939. state in new org-mode buffers. The setting can then be toggled on a
  940. per-buffer basis from the Org->Hyperlinks menu."
  941. :group 'org-link
  942. :type 'boolean)
  943. (defcustom org-link-file-path-type 'adaptive
  944. "How the path name in file links should be stored.
  945. Valid values are:
  946. relative relative to the current directory, i.e. the directory of the file
  947. into which the link is being inserted.
  948. absolute absolute path, if possible with ~ for home directory.
  949. noabbrev absolute path, no abbreviation of home directory.
  950. adaptive Use relative path for files in the current directory and sub-
  951. directories of it. For other files, use an absolute path."
  952. :group 'org-link
  953. :type '(choice
  954. (const relative)
  955. (const absolute)
  956. (const noabbrev)
  957. (const adaptive)))
  958. (defcustom org-activate-links '(bracket angle plain radio tag date)
  959. "Types of links that should be activated in Org-mode files.
  960. This is a list of symbols, each leading to the activation of a certain link
  961. type. In principle, it does not hurt to turn on most link types - there may
  962. be a small gain when turning off unused link types. The types are:
  963. bracket The recommended [[link][description]] or [[link]] links with hiding.
  964. angular Links in angular brackes that may contain whitespace like
  965. <bbdb:Carsten Dominik>.
  966. plain Plain links in normal text, no whitespace, like http://google.com.
  967. radio Text that is matched by a radio target, see manual for details.
  968. tag Tag settings in a headline (link to tag search).
  969. date Time stamps (link to calendar).
  970. Changing this variable requires a restart of Emacs to become effective."
  971. :group 'org-link
  972. :type '(set (const :tag "Double bracket links (new style)" bracket)
  973. (const :tag "Angular bracket links (old style)" angular)
  974. (const :tag "plain text links" plain)
  975. (const :tag "Radio target matches" radio)
  976. (const :tag "Tags" tag)
  977. (const :tag "Tags" target)
  978. (const :tag "Timestamps" date)))
  979. (defgroup org-link-store nil
  980. "Options concerning storing links in Org-mode"
  981. :tag "Org Store Link"
  982. :group 'org-link)
  983. (defcustom org-email-link-description-format "Email %c: %.30s"
  984. "Format of the description part of a link to an email or usenet message.
  985. The following %-excapes will be replaced by corresponding information:
  986. %F full \"From\" field
  987. %f name, taken from \"From\" field, address if no name
  988. %T full \"To\" field
  989. %t first name in \"To\" field, address if no name
  990. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  991. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  992. %s subject
  993. %m message-id.
  994. You may use normal field width specification between the % and the letter.
  995. This is for example useful to limit the length of the subject.
  996. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  997. :group 'org-link-store
  998. :type 'string)
  999. (defcustom org-from-is-user-regexp
  1000. (let (r1 r2)
  1001. (when (and user-mail-address (not (string= user-mail-address "")))
  1002. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1003. (when (and user-full-name (not (string= user-full-name "")))
  1004. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1005. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1006. "Regexp mached against the \"From:\" header of an email or usenet message.
  1007. It should match if the message is from the user him/herself."
  1008. :group 'org-link-store
  1009. :type 'regexp)
  1010. (defcustom org-context-in-file-links t
  1011. "Non-nil means, file links from `org-store-link' contain context.
  1012. A search string will be added to the file name with :: as separator and
  1013. used to find the context when the link is activated by the command
  1014. `org-open-at-point'.
  1015. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1016. negates this setting for the duration of the command."
  1017. :group 'org-link-store
  1018. :type 'boolean)
  1019. (defcustom org-keep-stored-link-after-insertion nil
  1020. "Non-nil means, keep link in list for entire session.
  1021. The command `org-store-link' adds a link pointing to the current
  1022. location to an internal list. These links accumulate during a session.
  1023. The command `org-insert-link' can be used to insert links into any
  1024. Org-mode file (offering completion for all stored links). When this
  1025. option is nil, every link which has been inserted once using \\[org-insert-link]
  1026. will be removed from the list, to make completing the unused links
  1027. more efficient."
  1028. :group 'org-link-store
  1029. :type 'boolean)
  1030. (defcustom org-usenet-links-prefer-google nil
  1031. "Non-nil means, `org-store-link' will create web links to Google groups.
  1032. When nil, Gnus will be used for such links.
  1033. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1034. negates this setting for the duration of the command."
  1035. :group 'org-link-store
  1036. :type 'boolean)
  1037. (defgroup org-link-follow nil
  1038. "Options concerning following links in Org-mode"
  1039. :tag "Org Follow Link"
  1040. :group 'org-link)
  1041. (defcustom org-tab-follows-link nil
  1042. "Non-nil means, on links TAB will follow the link.
  1043. Needs to be set before org.el is loaded."
  1044. :group 'org-link-follow
  1045. :type 'boolean)
  1046. (defcustom org-return-follows-link nil
  1047. "Non-nil means, on links RET will follow the link.
  1048. Needs to be set before org.el is loaded."
  1049. :group 'org-link-follow
  1050. :type 'boolean)
  1051. (defcustom org-mouse-1-follows-link t
  1052. "Non-nil means, mouse-1 on a link will follow the link.
  1053. A longer mouse click will still set point. Does not wortk on XEmacs.
  1054. Needs to be set before org.el is loaded."
  1055. :group 'org-link-follow
  1056. :type 'boolean)
  1057. (defcustom org-mark-ring-length 4
  1058. "Number of different positions to be recorded in the ring
  1059. Changing this requires a restart of Emacs to work correctly."
  1060. :group 'org-link-follow
  1061. :type 'interger)
  1062. (defcustom org-link-frame-setup
  1063. '((vm . vm-visit-folder-other-frame)
  1064. (gnus . gnus-other-frame)
  1065. (file . find-file-other-window))
  1066. "Setup the frame configuration for following links.
  1067. When following a link with Emacs, it may often be useful to display
  1068. this link in another window or frame. This variable can be used to
  1069. set this up for the different types of links.
  1070. For VM, use any of
  1071. `vm-visit-folder'
  1072. `vm-visit-folder-other-frame'
  1073. For Gnus, use any of
  1074. `gnus'
  1075. `gnus-other-frame'
  1076. For FILE, use any of
  1077. `find-file'
  1078. `find-file-other-window'
  1079. `find-file-other-frame'
  1080. For the calendar, use the variable `calendar-setup'.
  1081. For BBDB, it is currently only possible to display the matches in
  1082. another window."
  1083. :group 'org-link-follow
  1084. :type '(list
  1085. (cons (const vm)
  1086. (choice
  1087. (const vm-visit-folder)
  1088. (const vm-visit-folder-other-window)
  1089. (const vm-visit-folder-other-frame)))
  1090. (cons (const gnus)
  1091. (choice
  1092. (const gnus)
  1093. (const gnus-other-frame)))
  1094. (cons (const file)
  1095. (choice
  1096. (const find-file)
  1097. (const find-file-other-window)
  1098. (const find-file-other-frame)))))
  1099. (defcustom org-display-internal-link-with-indirect-buffer nil
  1100. "Non-nil means, use indirect buffer to display infile links.
  1101. Activating internal links (from one location in a file to another location
  1102. in the same file) normally just jumps to the location. When the link is
  1103. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1104. another window. When this option is set, the other window actually displays
  1105. an indirect buffer clone of the current buffer, to avoid any visibility
  1106. changes to the current buffer."
  1107. :group 'org-link-follow
  1108. :type 'boolean)
  1109. (defcustom org-open-non-existing-files nil
  1110. "Non-nil means, `org-open-file' will open non-existing files.
  1111. When nil, an error will be generated."
  1112. :group 'org-link-follow
  1113. :type 'boolean)
  1114. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1115. "Function and arguments to call for following mailto links.
  1116. This is a list with the first element being a lisp function, and the
  1117. remaining elements being arguments to the function. In string arguments,
  1118. %a will be replaced by the address, and %s will be replaced by the subject
  1119. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1120. :group 'org-link-follow
  1121. :type '(choice
  1122. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1123. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1124. (const :tag "message-mail" (message-mail "%a" "%s"))
  1125. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1126. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1127. "Non-nil means, ask for confirmation before executing shell links.
  1128. Shell links can be dangerous: just think about a link
  1129. [[shell:rm -rf ~/*][Google Search]]
  1130. This link would show up in your Org-mode document as \"Google Search\",
  1131. but really it would remove your entire home directory.
  1132. Therefore we advise against setting this variable to nil.
  1133. Just change it to `y-or-n-p' of you want to confirm with a
  1134. single keystroke rather than having to type \"yes\"."
  1135. :group 'org-link-follow
  1136. :type '(choice
  1137. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1138. (const :tag "with y-or-n (faster)" y-or-n-p)
  1139. (const :tag "no confirmation (dangerous)" nil)))
  1140. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1141. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1142. Elisp links can be dangerous: just think about a link
  1143. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1144. This link would show up in your Org-mode document as \"Google Search\",
  1145. but really it would remove your entire home directory.
  1146. Therefore we advise against setting this variable to nil.
  1147. Just change it to `y-or-n-p' of you want to confirm with a
  1148. single keystroke rather than having to type \"yes\"."
  1149. :group 'org-link-follow
  1150. :type '(choice
  1151. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1152. (const :tag "with y-or-n (faster)" y-or-n-p)
  1153. (const :tag "no confirmation (dangerous)" nil)))
  1154. (defconst org-file-apps-defaults-gnu
  1155. '((remote . emacs)
  1156. (t . mailcap))
  1157. "Default file applications on a UNIX or GNU/Linux system.
  1158. See `org-file-apps'.")
  1159. (defconst org-file-apps-defaults-macosx
  1160. '((remote . emacs)
  1161. (t . "open %s")
  1162. ("ps" . "gv %s")
  1163. ("ps.gz" . "gv %s")
  1164. ("eps" . "gv %s")
  1165. ("eps.gz" . "gv %s")
  1166. ("dvi" . "xdvi %s")
  1167. ("fig" . "xfig %s"))
  1168. "Default file applications on a MacOS X system.
  1169. The system \"open\" is known as a default, but we use X11 applications
  1170. for some files for which the OS does not have a good default.
  1171. See `org-file-apps'.")
  1172. (defconst org-file-apps-defaults-windowsnt
  1173. (list
  1174. '(remote . emacs)
  1175. (cons t
  1176. (list (if (featurep 'xemacs)
  1177. 'mswindows-shell-execute
  1178. 'w32-shell-execute)
  1179. "open" 'file)))
  1180. "Default file applications on a Windows NT system.
  1181. The system \"open\" is used for most files.
  1182. See `org-file-apps'.")
  1183. (defcustom org-file-apps
  1184. '(
  1185. ("txt" . emacs)
  1186. ("tex" . emacs)
  1187. ("ltx" . emacs)
  1188. ("org" . emacs)
  1189. ("el" . emacs)
  1190. ("bib" . emacs)
  1191. )
  1192. "External applications for opening `file:path' items in a document.
  1193. Org-mode uses system defaults for different file types, but
  1194. you can use this variable to set the application for a given file
  1195. extension. The entries in this list are cons cells where the car identifies
  1196. files and the cdr the corresponding command. Possible values for the
  1197. file identifier are
  1198. \"ext\" A string identifying an extension
  1199. `directory' Matches a directory
  1200. `remote' Matches a remote file, accessible through tramp or efs.
  1201. Remote files most likely should be visited through Emacs
  1202. because external applications cannot handle such paths.
  1203. t Default for all remaining files
  1204. Possible values for the command are:
  1205. `emacs' The file will be visited by the current Emacs process.
  1206. `default' Use the default application for this file type.
  1207. string A command to be executed by a shell; %s will be replaced
  1208. by the path to the file.
  1209. sexp A Lisp form which will be evaluated. The file path will
  1210. be available in the Lisp variable `file'.
  1211. For more examples, see the system specific constants
  1212. `org-file-apps-defaults-macosx'
  1213. `org-file-apps-defaults-windowsnt'
  1214. `org-file-apps-defaults-gnu'."
  1215. :group 'org-link-follow
  1216. :type '(repeat
  1217. (cons (choice :value ""
  1218. (string :tag "Extension")
  1219. (const :tag "Default for unrecognized files" t)
  1220. (const :tag "Remote file" remote)
  1221. (const :tag "Links to a directory" directory))
  1222. (choice :value ""
  1223. (const :tag "Visit with Emacs" emacs)
  1224. (const :tag "Use system default" default)
  1225. (string :tag "Command")
  1226. (sexp :tag "Lisp form")))))
  1227. (defcustom org-mhe-search-all-folders nil
  1228. "Non-nil means, that the search for the mh-message will be extended to
  1229. all folders if the message cannot be found in the folder given in the link.
  1230. Searching all folders is very efficient with one of the search engines
  1231. supported by MH-E, but will be slow with pick."
  1232. :group 'org-link-follow
  1233. :type 'boolean)
  1234. (defgroup org-remember nil
  1235. "Options concerning interaction with remember.el."
  1236. :tag "Org Remember"
  1237. :group 'org)
  1238. (defcustom org-directory "~/org"
  1239. "Directory with org files.
  1240. This directory will be used as default to prompt for org files.
  1241. Used by the hooks for remember.el."
  1242. :group 'org-remember
  1243. :type 'directory)
  1244. (defcustom org-default-notes-file "~/.notes"
  1245. "Default target for storing notes.
  1246. Used by the hooks for remember.el. This can be a string, or nil to mean
  1247. the value of `remember-data-file'.
  1248. You can set this on a per-template basis with the variable
  1249. `org-remember-templates'."
  1250. :group 'org-remember
  1251. :type '(choice
  1252. (const :tag "Default from remember-data-file" nil)
  1253. file))
  1254. (defcustom org-remember-store-without-prompt t
  1255. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1256. In this case, you need `C-u C-c C-c' to get the prompts for
  1257. note file and headline.
  1258. When this variable is nil, `C-c C-c' give you the prompts, and
  1259. `C-u C-c C-c' trigger the fasttrack."
  1260. :group 'org-remember
  1261. :type 'boolean)
  1262. (defcustom org-remember-default-headline ""
  1263. "The headline that should be the default location in the notes file.
  1264. When filing remember notes, the cursor will start at that position.
  1265. You can set this on a per-template basis with the variable
  1266. `org-remember-templates'."
  1267. :group 'org-remember
  1268. :type 'string)
  1269. (defcustom org-remember-templates nil
  1270. "Templates for the creation of remember buffers.
  1271. When nil, just let remember make the buffer.
  1272. When not nil, this is a list of 5-element lists. In each entry, the first
  1273. element is a the name of the template, It should be a single short word.
  1274. The second element is a character, a unique key to select this template.
  1275. The third element is the template. The forth element is optional and can
  1276. specify a destination file for remember items created with this template.
  1277. The default file is given by `org-default-notes-file'. An optional fifth
  1278. element can specify the headline in that file that should be offered
  1279. first when the user is asked to file the entry. The default headline is
  1280. given in the variable `org-remember-default-headline'.
  1281. The template specifies the structure of the remember buffer. It should have
  1282. a first line starting with a star, to act as the org-mode headline.
  1283. Furthermore, the following %-escapes will be replaced with content:
  1284. %^{prompt} prompt the user for a string and replace this sequence with it.
  1285. %t time stamp, date only
  1286. %T time stamp with date and time
  1287. %u, %U like the above, but inactive time stamps
  1288. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1289. You may define a prompt like %^{Please specify birthday}t
  1290. %n user name (taken from `user-full-name')
  1291. %a annotation, normally the link created with org-store-link
  1292. %i initial content, the region when remember is called with C-u.
  1293. If %i is indented, the entire inserted text will be indented
  1294. as well.
  1295. %? After completing the template, position cursor here.
  1296. Apart from these general escapes, you can access information specific to the
  1297. link type that is created. For example, calling `remember' in emails or gnus
  1298. will record the author and the subject of the message, which you can access
  1299. with %:author and %:subject, respectively. Here is a complete list of what
  1300. is recorded for each link type.
  1301. Link type | Available information
  1302. -------------------+------------------------------------------------------
  1303. bbdb | %:type %:name %:company
  1304. vm, wl, mh, rmail | %:type %:subject %:message-id
  1305. | %:from %:fromname %:fromaddress
  1306. | %:to %:toname %:toaddress
  1307. | %:fromto (either \"to NAME\" or \"from NAME\")
  1308. gnus | %:group, for messages also all email fields
  1309. w3, w3m | %:type %:url
  1310. info | %:type %:file %:node
  1311. calendar | %:type %:date"
  1312. :group 'org-remember
  1313. :get (lambda (var) ; Make sure all entries have 5 elements
  1314. (mapcar (lambda (x)
  1315. (if (not (stringp (car x))) (setq x (cons "" x)))
  1316. (cond ((= (length x) 4) (append x '("")))
  1317. ((= (length x) 3) (append x '("" "")))
  1318. (t x)))
  1319. (default-value var)))
  1320. :type '(repeat
  1321. :tag "enabled"
  1322. (list :value ("" ?a "\n" nil nil)
  1323. (string :tag "Name")
  1324. (character :tag "Selection Key")
  1325. (string :tag "Template")
  1326. (choice
  1327. (file :tag "Destination file")
  1328. (const :tag "Prompt for file" nil))
  1329. (choice
  1330. (string :tag "Destination headline")
  1331. (const :tag "Selection interface for heading")))))
  1332. (defcustom org-reverse-note-order nil
  1333. "Non-nil means, store new notes at the beginning of a file or entry.
  1334. When nil, new notes will be filed to the end of a file or entry.
  1335. This can also be a list with cons cells of regular expressions that
  1336. are matched against file names, and values."
  1337. :group 'org-remember
  1338. :type '(choice
  1339. (const :tag "Reverse always" t)
  1340. (const :tag "Reverse never" nil)
  1341. (repeat :tag "By file name regexp"
  1342. (cons regexp boolean))))
  1343. (defgroup org-todo nil
  1344. "Options concerning TODO items in Org-mode."
  1345. :tag "Org TODO"
  1346. :group 'org)
  1347. (defgroup org-progress nil
  1348. "Options concerning Progress logging in Org-mode."
  1349. :tag "Org Progress"
  1350. :group 'org-time)
  1351. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1352. "List of TODO entry keyword sequences and their interpretation.
  1353. \\<org-mode-map>This is a list of sequences.
  1354. Each sequence starts with a symbol, either `sequence' or `type',
  1355. indicating if the keywords should be interpreted as a sequence of
  1356. action steps, or as different types of TODO items. The first
  1357. keywords are states requiring action - these states will select a headline
  1358. for inclusion into the global TODO list Org-mode produces. If one of
  1359. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1360. signify that no further action is necessary. If \"|\" is not found,
  1361. the last keyword is treated as the only DONE state of the sequence.
  1362. The command \\[org-todo] cycles an entry through these states, and one
  1363. additional state where no keyword is present. For details about this
  1364. cycling, see the manual.
  1365. TODO keywords and interpretation can also be set on a per-file basis with
  1366. the special #+SEQ_TODO and #+TYP_TODO lines.
  1367. For backward compatibility, this variable may also be just a list
  1368. of keywords - in this case the interptetation (sequence or type) will be
  1369. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1370. :group 'org-todo
  1371. :group 'org-keywords
  1372. :type '(choice
  1373. (repeat :tag "Old syntax, just keywords"
  1374. (string :tag "Keyword"))
  1375. (repeat :tag "New syntax"
  1376. (cons
  1377. (choice
  1378. :tag "Interpretation"
  1379. (const :tag "Sequence (cycling hits every state)" sequence)
  1380. (const :tag "Type (cycling directly to DONE)" type))
  1381. (repeat
  1382. (string :tag "Keyword"))))))
  1383. (defvar org-todo-keywords-1 nil)
  1384. (make-variable-buffer-local 'org-todo-keywords-1)
  1385. (defvar org-todo-keywords-for-agenda nil)
  1386. (defvar org-done-keywords-for-agenda nil)
  1387. (defvar org-not-done-keywords nil)
  1388. (make-variable-buffer-local 'org-not-done-keywords)
  1389. (defvar org-done-keywords nil)
  1390. (make-variable-buffer-local 'org-done-keywords)
  1391. (defvar org-todo-heads nil)
  1392. (make-variable-buffer-local 'org-todo-heads)
  1393. (defvar org-todo-sets nil)
  1394. (make-variable-buffer-local 'org-todo-sets)
  1395. (defvar org-todo-log-states nil)
  1396. (make-variable-buffer-local 'org-todo-log-states)
  1397. (defvar org-todo-kwd-alist nil)
  1398. (make-variable-buffer-local 'org-todo-kwd-alist)
  1399. (defvar org-todo-key-alist nil)
  1400. (make-variable-buffer-local 'org-todo-key-alist)
  1401. (defvar org-todo-key-trigger nil)
  1402. (make-variable-buffer-local 'org-todo-key-trigger)
  1403. (defcustom org-todo-interpretation 'sequence
  1404. "Controls how TODO keywords are interpreted.
  1405. This variable is in principle obsolete and is only used for
  1406. backward compatibility, if the interpretation of todo keywords is
  1407. not given already in `org-todo-keywords'. See that variable for
  1408. more information."
  1409. :group 'org-todo
  1410. :group 'org-keywords
  1411. :type '(choice (const sequence)
  1412. (const type)))
  1413. (defcustom org-use-fast-todo-selection 'prefix
  1414. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1415. This variable describes if and under what circumstances the cycling
  1416. mechanism for TODO keywords will be replaced by a single-key, direct
  1417. selection scheme.
  1418. When nil, fast selection is never used.
  1419. When the symbol `prefix', it will be used when `org-todo' is called with
  1420. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1421. in an agenda buffer.
  1422. When t, fast selection is used by default. In this case, the prefix
  1423. argument forces cycling instead.
  1424. In all cases, the special interface is only used if access keys have actually
  1425. been assigned by the user, i.e. if keywords in the configuration are followed
  1426. by a letter in parenthesis, like TODO(t)."
  1427. :group 'org-todo
  1428. :type '(choice
  1429. (const :tag "Never" nil)
  1430. (const :tag "By default" t)
  1431. (const :tag "Only with C-u C-c C-t" prefix)))
  1432. (defcustom org-after-todo-state-change-hook nil
  1433. "Hook which is run after the state of a TODO item was changed.
  1434. The new state (a string with a TODO keyword, or nil) is available in the
  1435. Lisp variable `state'."
  1436. :group 'org-todo
  1437. :type 'hook)
  1438. (defcustom org-log-done nil
  1439. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1440. When the state of an entry is changed from nothing or a DONE state to
  1441. a not-done TODO state, remove a previous closing date.
  1442. This can also be a list of symbols indicating under which conditions
  1443. the time stamp recording the action should be annotated with a short note.
  1444. Valid members of this list are
  1445. done Offer to record a note when marking entries done
  1446. state Offer to record a note whenever changing the TODO state
  1447. of an item. This is only relevant if TODO keywords are
  1448. interpreted as sequence, see variable `org-todo-interpretation'.
  1449. When `state' is set, this includes tracking `done'.
  1450. clock-out Offer to record a note when clocking out of an item.
  1451. A separate window will then pop up and allow you to type a note.
  1452. After finishing with C-c C-c, the note will be added directly after the
  1453. timestamp, as a plain list item. See also the variable
  1454. `org-log-note-headings'.
  1455. Logging can also be configured on a per-file basis by adding one of
  1456. the following lines anywhere in the buffer:
  1457. #+STARTUP: logdone
  1458. #+STARTUP: nologging
  1459. #+STARTUP: lognotedone
  1460. #+STARTUP: lognotestate
  1461. #+STARTUP: lognoteclock-out
  1462. You can have local logging settings for a subtree by setting the LOGGING
  1463. property to one or more of these keywords."
  1464. :group 'org-todo
  1465. :group 'org-progress
  1466. :type '(choice
  1467. (const :tag "off" nil)
  1468. (const :tag "on" t)
  1469. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1470. (const :tag "when item is marked DONE" done)
  1471. (const :tag "when TODO state changes" state)
  1472. (const :tag "when clocking out" clock-out))))
  1473. (defcustom org-log-done-with-time t
  1474. "Non-nil means, the CLOSED time stamp will contain date and time.
  1475. When nil, only the date will be recorded."
  1476. :group 'org-progress
  1477. :type 'boolean)
  1478. (defcustom org-log-note-headings
  1479. '((done . "CLOSING NOTE %t")
  1480. (state . "State %-12s %t")
  1481. (clock-out . ""))
  1482. "Headings for notes added when clocking out or closing TODO items.
  1483. The value is an alist, with the car being a symbol indicating the note
  1484. context, and the cdr is the heading to be used. The heading may also be the
  1485. empty string.
  1486. %t in the heading will be replaced by a time stamp.
  1487. %s will be replaced by the new TODO state, in double quotes.
  1488. %u will be replaced by the user name.
  1489. %U will be replaced by the full user name."
  1490. :group 'org-todo
  1491. :group 'org-progress
  1492. :type '(list :greedy t
  1493. (cons (const :tag "Heading when closing an item" done) string)
  1494. (cons (const :tag
  1495. "Heading when changing todo state (todo sequence only)"
  1496. state) string)
  1497. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1498. (defcustom org-log-states-order-reversed t
  1499. "Non-nil means, the latest state change note will be directly after heading.
  1500. When nil, the notes will be orderer according to time."
  1501. :group 'org-todo
  1502. :group 'org-progress
  1503. :type 'boolean)
  1504. (defcustom org-log-repeat t
  1505. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1506. When nil, no note will be taken.
  1507. This option can also be set with on a per-file-basis with
  1508. #+STARTUP: logrepeat
  1509. #+STARTUP: nologrepeat
  1510. You can have local logging settings for a subtree by setting the LOGGING
  1511. property to one or more of these keywords."
  1512. :group 'org-todo
  1513. :group 'org-progress
  1514. :type 'boolean)
  1515. (defcustom org-clock-into-drawer 2
  1516. "Should clocking info be wrapped into a drawer?
  1517. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1518. If necessary, the drawer will be created.
  1519. When nil, the drawer will not be created, but used when present.
  1520. When an integer and the number of clocking entries in an item
  1521. reaches or exceeds this number, a drawer will be created."
  1522. :group 'org-todo
  1523. :group 'org-progress
  1524. :type '(choice
  1525. (const :tag "Always" t)
  1526. (const :tag "Only when drawer exists" nil)
  1527. (integer :tag "When at least N clock entries")))
  1528. (defcustom org-clock-out-when-done t
  1529. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1530. Nil means, clock will keep running until stopped explicitly with
  1531. `C-c C-x C-o', or until the clock is started in a different item."
  1532. :group 'org-progress
  1533. :type 'boolean)
  1534. (defgroup org-priorities nil
  1535. "Priorities in Org-mode."
  1536. :tag "Org Priorities"
  1537. :group 'org-todo)
  1538. (defcustom org-highest-priority ?A
  1539. "The highest priority of TODO items. A character like ?A, ?B etc.
  1540. Must have a smaller ASCII number than `org-lowest-priority'."
  1541. :group 'org-priorities
  1542. :type 'character)
  1543. (defcustom org-lowest-priority ?C
  1544. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1545. Must have a larger ASCII number than `org-highest-priority'."
  1546. :group 'org-priorities
  1547. :type 'character)
  1548. (defcustom org-default-priority ?B
  1549. "The default priority of TODO items.
  1550. This is the priority an item get if no explicit priority is given."
  1551. :group 'org-priorities
  1552. :type 'character)
  1553. (defcustom org-priority-start-cycle-with-default t
  1554. "Non-nil means, start with default priority when starting to cycle.
  1555. When this is nil, the first step in the cycle will be (depending on the
  1556. command used) one higher or lower that the default priority."
  1557. :group 'org-priorities
  1558. :type 'boolean)
  1559. (defgroup org-time nil
  1560. "Options concerning time stamps and deadlines in Org-mode."
  1561. :tag "Org Time"
  1562. :group 'org)
  1563. (defcustom org-insert-labeled-timestamps-at-point nil
  1564. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1565. When nil, these labeled time stamps are forces into the second line of an
  1566. entry, just after the headline. When scheduling from the global TODO list,
  1567. the time stamp will always be forced into the second line."
  1568. :group 'org-time
  1569. :type 'boolean)
  1570. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1571. "Formats for `format-time-string' which are used for time stamps.
  1572. It is not recommended to change this constant.")
  1573. (defcustom org-time-stamp-rounding-minutes 0
  1574. "Number of minutes to round time stamps to upon insertion.
  1575. When zero, insert the time unmodified. Useful rounding numbers
  1576. should be factors of 60, so for example 5, 10, 15.
  1577. When this is not zero, you can still force an exact time-stamp by using
  1578. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1579. :group 'org-time
  1580. :type 'integer)
  1581. (defcustom org-display-custom-times nil
  1582. "Non-nil means, overlay custom formats over all time stamps.
  1583. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1584. To turn this on on a per-file basis, insert anywhere in the file:
  1585. #+STARTUP: customtime"
  1586. :group 'org-time
  1587. :set 'set-default
  1588. :type 'sexp)
  1589. (make-variable-buffer-local 'org-display-custom-times)
  1590. (defcustom org-time-stamp-custom-formats
  1591. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1592. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1593. These are overlayed over the default ISO format if the variable
  1594. `org-display-custom-times' is set. Time like %H:%M should be at the
  1595. end of the second format."
  1596. :group 'org-time
  1597. :type 'sexp)
  1598. (defun org-time-stamp-format (&optional long inactive)
  1599. "Get the right format for a time string."
  1600. (let ((f (if long (cdr org-time-stamp-formats)
  1601. (car org-time-stamp-formats))))
  1602. (if inactive
  1603. (concat "[" (substring f 1 -1) "]")
  1604. f)))
  1605. (defcustom org-read-date-prefer-future t
  1606. "Non-nil means, assume future for incomplete date input from user.
  1607. This affects the following situations:
  1608. 1. The user gives a day, but no month.
  1609. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1610. read this as the third of *next* month. However, if you enter \"17\",
  1611. it will be considered as *this* month.
  1612. 2. The user gives a month but not a year.
  1613. For example, if it is april and you enter \"feb 2\", this will be read
  1614. as feb 2, *next* year. \"May 5\", however, will be this year.
  1615. When this option is nil, the current month and year will always be used
  1616. as defaults."
  1617. :group 'org-time
  1618. :type 'boolean)
  1619. (defcustom org-read-date-display-live t
  1620. "Non-nil means, display current interpretation of date prompt live.
  1621. This display will be in an overlay, in the minibuffer."
  1622. :group 'org-time
  1623. :type 'boolean)
  1624. (defcustom org-read-date-popup-calendar t
  1625. "Non-nil means, pop up a calendar when prompting for a date.
  1626. In the calendar, the date can be selected with mouse-1. However, the
  1627. minibuffer will also be active, and you can simply enter the date as well.
  1628. When nil, only the minibuffer will be available."
  1629. :group 'org-time
  1630. :type 'boolean)
  1631. (if (fboundp 'defvaralias)
  1632. (defvaralias 'org-popup-calendar-for-date-prompt
  1633. 'org-read-date-popup-calendar))
  1634. (defcustom org-extend-today-until 0
  1635. "The hour when your day really ends.
  1636. This has influence for the following applications:
  1637. - When switching the agenda to \"today\". It it is still earlier than
  1638. the time given here, the day recognized as TODAY is actually yesterday.
  1639. - When a date is read from the user and it is still before the time given
  1640. here, the current date and time will be assumed to be yesterday, 23:59.
  1641. FIXME:
  1642. IMPORTANT: This is still a very experimental feature, it may disappear
  1643. again or it may be extended to mean more things."
  1644. :group 'org-time
  1645. :type 'number)
  1646. (defcustom org-edit-timestamp-down-means-later nil
  1647. "Non-nil means, S-down will increase the time in a time stamp.
  1648. When nil, S-up will increase."
  1649. :group 'org-time
  1650. :type 'boolean)
  1651. (defcustom org-calendar-follow-timestamp-change t
  1652. "Non-nil means, make the calendar window follow timestamp changes.
  1653. When a timestamp is modified and the calendar window is visible, it will be
  1654. moved to the new date."
  1655. :group 'org-time
  1656. :type 'boolean)
  1657. (defgroup org-tags nil
  1658. "Options concerning tags in Org-mode."
  1659. :tag "Org Tags"
  1660. :group 'org)
  1661. (defcustom org-tag-alist nil
  1662. "List of tags allowed in Org-mode files.
  1663. When this list is nil, Org-mode will base TAG input on what is already in the
  1664. buffer.
  1665. The value of this variable is an alist, the car of each entry must be a
  1666. keyword as a string, the cdr may be a character that is used to select
  1667. that tag through the fast-tag-selection interface.
  1668. See the manual for details."
  1669. :group 'org-tags
  1670. :type '(repeat
  1671. (choice
  1672. (cons (string :tag "Tag name")
  1673. (character :tag "Access char"))
  1674. (const :tag "Start radio group" (:startgroup))
  1675. (const :tag "End radio group" (:endgroup)))))
  1676. (defcustom org-use-fast-tag-selection 'auto
  1677. "Non-nil means, use fast tag selection scheme.
  1678. This is a special interface to select and deselect tags with single keys.
  1679. When nil, fast selection is never used.
  1680. When the symbol `auto', fast selection is used if and only if selection
  1681. characters for tags have been configured, either through the variable
  1682. `org-tag-alist' or through a #+TAGS line in the buffer.
  1683. When t, fast selection is always used and selection keys are assigned
  1684. automatically if necessary."
  1685. :group 'org-tags
  1686. :type '(choice
  1687. (const :tag "Always" t)
  1688. (const :tag "Never" nil)
  1689. (const :tag "When selection characters are configured" 'auto)))
  1690. (defcustom org-fast-tag-selection-single-key nil
  1691. "Non-nil means, fast tag selection exits after first change.
  1692. When nil, you have to press RET to exit it.
  1693. During fast tag selection, you can toggle this flag with `C-c'.
  1694. This variable can also have the value `expert'. In this case, the window
  1695. displaying the tags menu is not even shown, until you press C-c again."
  1696. :group 'org-tags
  1697. :type '(choice
  1698. (const :tag "No" nil)
  1699. (const :tag "Yes" t)
  1700. (const :tag "Expert" expert)))
  1701. (defvar org-fast-tag-selection-include-todo nil
  1702. "Non-nil means, fast tags selection interface will also offer TODO states.
  1703. This is an undocumented feature, you should not rely on it.")
  1704. (defcustom org-tags-column -80
  1705. "The column to which tags should be indented in a headline.
  1706. If this number is positive, it specifies the column. If it is negative,
  1707. it means that the tags should be flushright to that column. For example,
  1708. -80 works well for a normal 80 character screen."
  1709. :group 'org-tags
  1710. :type 'integer)
  1711. (defcustom org-auto-align-tags t
  1712. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1713. These operations change the length of a headline and therefore shift
  1714. the tags around. With this options turned on, after each such operation
  1715. the tags are again aligned to `org-tags-column'."
  1716. :group 'org-tags
  1717. :type 'boolean)
  1718. (defcustom org-use-tag-inheritance t
  1719. "Non-nil means, tags in levels apply also for sublevels.
  1720. When nil, only the tags directly given in a specific line apply there.
  1721. If you turn off this option, you very likely want to turn on the
  1722. companion option `org-tags-match-list-sublevels'."
  1723. :group 'org-tags
  1724. :type 'boolean)
  1725. (defcustom org-tags-match-list-sublevels nil
  1726. "Non-nil means list also sublevels of headlines matching tag search.
  1727. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1728. the sublevels of a headline matching a tag search often also match
  1729. the same search. Listing all of them can create very long lists.
  1730. Setting this variable to nil causes subtrees of a match to be skipped.
  1731. This option is off by default, because inheritance in on. If you turn
  1732. inheritance off, you very likely want to turn this option on.
  1733. As a special case, if the tag search is restricted to TODO items, the
  1734. value of this variable is ignored and sublevels are always checked, to
  1735. make sure all corresponding TODO items find their way into the list."
  1736. :group 'org-tags
  1737. :type 'boolean)
  1738. (defvar org-tags-history nil
  1739. "History of minibuffer reads for tags.")
  1740. (defvar org-last-tags-completion-table nil
  1741. "The last used completion table for tags.")
  1742. (defvar org-after-tags-change-hook nil
  1743. "Hook that is run after the tags in a line have changed.")
  1744. (defgroup org-properties nil
  1745. "Options concerning properties in Org-mode."
  1746. :tag "Org Properties"
  1747. :group 'org)
  1748. (defcustom org-property-format "%-10s %s"
  1749. "How property key/value pairs should be formatted by `indent-line'.
  1750. When `indent-line' hits a property definition, it will format the line
  1751. according to this format, mainly to make sure that the values are
  1752. lined-up with respect to each other."
  1753. :group 'org-properties
  1754. :type 'string)
  1755. (defcustom org-use-property-inheritance nil
  1756. "Non-nil means, properties apply also for sublevels.
  1757. This setting is only relevant during property searches, not when querying
  1758. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1759. you need to call `org-entry-get' with the inheritance flag.
  1760. Turning this on can cause significant overhead when doing a search, so
  1761. this is turned off by default.
  1762. When nil, only the properties directly given in the current entry count.
  1763. The value may also be a list of properties that shouldhave inheritance.
  1764. However, note that some special properties use inheritance under special
  1765. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1766. and the properties ending in \"_ALL\" when they are used as descriptor
  1767. for valid values of a property."
  1768. :group 'org-properties
  1769. :type '(choice
  1770. (const :tag "Not" nil)
  1771. (const :tag "Always" nil)
  1772. (repeat :tag "Specific properties")))
  1773. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1774. "The default column format, if no other format has been defined.
  1775. This variable can be set on the per-file basis by inserting a line
  1776. #+COLUMNS: %25ITEM ....."
  1777. :group 'org-properties
  1778. :type 'string)
  1779. (defcustom org-global-properties nil
  1780. "List of property/value pairs that can be inherited by any entry.
  1781. You can set buffer-local values for this by adding lines like
  1782. #+PROPERTY: NAME VALUE"
  1783. :group 'org-properties
  1784. :type '(repeat
  1785. (cons (string :tag "Property")
  1786. (string :tag "Value"))))
  1787. (defvar org-local-properties nil
  1788. "List of property/value pairs that can be inherited by any entry.
  1789. Valid for the current buffer.
  1790. This variable is populated from #+PROPERTY lines.")
  1791. (defgroup org-agenda nil
  1792. "Options concerning agenda views in Org-mode."
  1793. :tag "Org Agenda"
  1794. :group 'org)
  1795. (defvar org-category nil
  1796. "Variable used by org files to set a category for agenda display.
  1797. Such files should use a file variable to set it, for example
  1798. # -*- mode: org; org-category: \"ELisp\"
  1799. or contain a special line
  1800. #+CATEGORY: ELisp
  1801. If the file does not specify a category, then file's base name
  1802. is used instead.")
  1803. (make-variable-buffer-local 'org-category)
  1804. (defcustom org-agenda-files nil
  1805. "The files to be used for agenda display.
  1806. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1807. \\[org-remove-file]. You can also use customize to edit the list.
  1808. If an entry is a directory, all files in that directory that are matched by
  1809. `org-agenda-file-regexp' will be part of the file list.
  1810. If the value of the variable is not a list but a single file name, then
  1811. the list of agenda files is actually stored and maintained in that file, one
  1812. agenda file per line."
  1813. :group 'org-agenda
  1814. :type '(choice
  1815. (repeat :tag "List of files and directories" file)
  1816. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1817. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1818. "Regular expression to match files for `org-agenda-files'.
  1819. If any element in the list in that variable contains a directory instead
  1820. of a normal file, all files in that directory that are matched by this
  1821. regular expression will be included."
  1822. :group 'org-agenda
  1823. :type 'regexp)
  1824. (defcustom org-agenda-skip-unavailable-files nil
  1825. "t means to just skip non-reachable files in `org-agenda-files'.
  1826. Nil means to remove them, after a query, from the list."
  1827. :group 'org-agenda
  1828. :type 'boolean)
  1829. (defcustom org-agenda-multi-occur-extra-files nil
  1830. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1831. The files in `org-agenda-files' are always searched."
  1832. :group 'org-agenda
  1833. :type '(repeat file))
  1834. (defcustom org-agenda-confirm-kill 1
  1835. "When set, remote killing from the agenda buffer needs confirmation.
  1836. When t, a confirmation is always needed. When a number N, confirmation is
  1837. only needed when the text to be killed contains more than N non-white lines."
  1838. :group 'org-agenda
  1839. :type '(choice
  1840. (const :tag "Never" nil)
  1841. (const :tag "Always" t)
  1842. (number :tag "When more than N lines")))
  1843. (defcustom org-calendar-to-agenda-key [?c]
  1844. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1845. The command `org-calendar-goto-agenda' will be bound to this key. The
  1846. default is the character `c' because then `c' can be used to switch back and
  1847. forth between agenda and calendar."
  1848. :group 'org-agenda
  1849. :type 'sexp)
  1850. (defcustom org-agenda-compact-blocks nil
  1851. "Non-nil means, make the block agenda more compact.
  1852. This is done by leaving out unnecessary lines."
  1853. :group 'org-agenda
  1854. :type nil)
  1855. (defgroup org-agenda-export nil
  1856. "Options concerning exporting agenda views in Org-mode."
  1857. :tag "Org Agenda Export"
  1858. :group 'org-agenda)
  1859. (defcustom org-agenda-with-colors t
  1860. "Non-nil means, use colors in agenda views."
  1861. :group 'org-agenda-export
  1862. :type 'boolean)
  1863. (defcustom org-agenda-exporter-settings nil
  1864. "Alist of variable/value pairs that should be active during agenda export.
  1865. This is a good place to set uptions for ps-print and for htmlize."
  1866. :group 'org-agenda-export
  1867. :type '(repeat
  1868. (list
  1869. (variable)
  1870. (sexp :tag "Value"))))
  1871. (defcustom org-agenda-export-html-style ""
  1872. "The style specification for exported HTML Agenda files.
  1873. If this variable contains a string, it will replace the default <style>
  1874. section as produced by `htmlize'.
  1875. Since there are different ways of setting style information, this variable
  1876. needs to contain the full HTML structure to provide a style, including the
  1877. surrounding HTML tags. The style specifications should include definitions
  1878. the fonts used by the agenda, here is an example:
  1879. <style type=\"text/css\">
  1880. p { font-weight: normal; color: gray; }
  1881. .org-agenda-structure {
  1882. font-size: 110%;
  1883. color: #003399;
  1884. font-weight: 600;
  1885. }
  1886. .org-todo {
  1887. color: #cc6666;Week-agenda:
  1888. font-weight: bold;
  1889. }
  1890. .org-done {
  1891. color: #339933;
  1892. }
  1893. .title { text-align: center; }
  1894. .todo, .deadline { color: red; }
  1895. .done { color: green; }
  1896. </style>
  1897. or, if you want to keep the style in a file,
  1898. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1899. As the value of this option simply gets inserted into the HTML <head> header,
  1900. you can \"misuse\" it to also add other text to the header. However,
  1901. <style>...</style> is required, if not present the variable will be ignored."
  1902. :group 'org-agenda-export
  1903. :group 'org-export-html
  1904. :type 'string)
  1905. (defgroup org-agenda-custom-commands nil
  1906. "Options concerning agenda views in Org-mode."
  1907. :tag "Org Agenda Custom Commands"
  1908. :group 'org-agenda)
  1909. (defcustom org-agenda-custom-commands nil
  1910. "Custom commands for the agenda.
  1911. These commands will be offered on the splash screen displayed by the
  1912. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1913. (key desc type match options files)
  1914. key The key (one or more characters as a string) to be associated
  1915. with the command.
  1916. desc A description of the commend, when omitted or nil, a default
  1917. description is built using MATCH.
  1918. type The command type, any of the following symbols:
  1919. todo Entries with a specific TODO keyword, in all agenda files.
  1920. tags Tags match in all agenda files.
  1921. tags-todo Tags match in all agenda files, TODO entries only.
  1922. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1923. tags-tree Sparse tree with all tags matches in *current* file.
  1924. occur-tree Occur sparse tree for *current* file.
  1925. ... A user-defined function.
  1926. match What to search for:
  1927. - a single keyword for TODO keyword searches
  1928. - a tags match expression for tags searches
  1929. - a regular expression for occur searches
  1930. options A list of option settings, similar to that in a let form, so like
  1931. this: ((opt1 val1) (opt2 val2) ...)
  1932. files A list of files file to write the produced agenda buffer to
  1933. with the command `org-store-agenda-views'.
  1934. If a file name ends in \".html\", an HTML version of the buffer
  1935. is written out. If it ends in \".ps\", a postscript version is
  1936. produced. Otherwide, only the plain text is written to the file.
  1937. You can also define a set of commands, to create a composite agenda buffer.
  1938. In this case, an entry looks like this:
  1939. (key desc (cmd1 cmd2 ...) general-options file)
  1940. where
  1941. desc A description string to be displayed in the dispatcher menu.
  1942. cmd An agenda command, similar to the above. However, tree commands
  1943. are no allowed, but instead you can get agenda and global todo list.
  1944. So valid commands for a set are:
  1945. (agenda)
  1946. (alltodo)
  1947. (stuck)
  1948. (todo \"match\" options files)
  1949. (tags \"match\" options files)
  1950. (tags-todo \"match\" options files)
  1951. Each command can carry a list of options, and another set of options can be
  1952. given for the whole set of commands. Individual command options take
  1953. precedence over the general options.
  1954. When using several characters as key to a command, the first characters
  1955. are prefix commands. For the dispatcher to display useful information, you
  1956. should provide a description for the prefix, like
  1957. (setq org-agenda-custom-commands
  1958. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  1959. (\"hl\" tags \"+HOME+Lisa\")
  1960. (\"hp\" tags \"+HOME+Peter\")
  1961. (\"hk\" tags \"+HOME+Kim\")))"
  1962. :group 'org-agenda-custom-commands
  1963. :type '(repeat
  1964. (choice :value ("a" "" tags "" nil)
  1965. (list :tag "Single command"
  1966. (string :tag "Access Key(s) ")
  1967. (option (string :tag "Description"))
  1968. (choice
  1969. (const :tag "Agenda" agenda)
  1970. (const :tag "TODO list" alltodo)
  1971. (const :tag "Stuck projects" stuck)
  1972. (const :tag "Tags search (all agenda files)" tags)
  1973. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1974. (const :tag "TODO keyword search (all agenda files)" todo)
  1975. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1976. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1977. (const :tag "Occur tree (current buffer)" occur-tree)
  1978. (sexp :tag "Other, user-defined function"))
  1979. (string :tag "Match")
  1980. (repeat :tag "Local options"
  1981. (list (variable :tag "Option") (sexp :tag "Value")))
  1982. (option (repeat :tag "Export" (file :tag "Export to"))))
  1983. (list :tag "Command series, all agenda files"
  1984. (string :tag "Access Key(s)")
  1985. (string :tag "Description ")
  1986. (repeat
  1987. (choice
  1988. (const :tag "Agenda" (agenda))
  1989. (const :tag "TODO list" (alltodo))
  1990. (const :tag "Stuck projects" (stuck))
  1991. (list :tag "Tags search"
  1992. (const :format "" tags)
  1993. (string :tag "Match")
  1994. (repeat :tag "Local options"
  1995. (list (variable :tag "Option")
  1996. (sexp :tag "Value"))))
  1997. (list :tag "Tags search, TODO entries only"
  1998. (const :format "" tags-todo)
  1999. (string :tag "Match")
  2000. (repeat :tag "Local options"
  2001. (list (variable :tag "Option")
  2002. (sexp :tag "Value"))))
  2003. (list :tag "TODO keyword search"
  2004. (const :format "" todo)
  2005. (string :tag "Match")
  2006. (repeat :tag "Local options"
  2007. (list (variable :tag "Option")
  2008. (sexp :tag "Value"))))
  2009. (list :tag "Other, user-defined function"
  2010. (symbol :tag "function")
  2011. (string :tag "Match")
  2012. (repeat :tag "Local options"
  2013. (list (variable :tag "Option")
  2014. (sexp :tag "Value"))))))
  2015. (repeat :tag "General options"
  2016. (list (variable :tag "Option")
  2017. (sexp :tag "Value")))
  2018. (option (repeat :tag "Export" (file :tag "Export to"))))
  2019. (cons :tag "Prefix key documentation"
  2020. (string :tag "Access Key(s)")
  2021. (string :tag "Description ")))))
  2022. (defcustom org-stuck-projects
  2023. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2024. "How to identify stuck projects.
  2025. This is a list of four items:
  2026. 1. A tags/todo matcher string that is used to identify a project.
  2027. The entire tree below a headline matched by this is considered one project.
  2028. 2. A list of TODO keywords identifying non-stuck projects.
  2029. If the project subtree contains any headline with one of these todo
  2030. keywords, the project is considered to be not stuck. If you specify
  2031. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2032. 3. A list of tags identifying non-stuck projects.
  2033. If the project subtree contains any headline with one of these tags,
  2034. the project is considered to be not stuck. If you specify \"*\" as
  2035. a tag, any tag will mark the project unstuck.
  2036. 4. An arbitrary regular expression matching non-stuck projects.
  2037. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2038. or `C-c a #' to produce the list."
  2039. :group 'org-agenda-custom-commands
  2040. :type '(list
  2041. (string :tag "Tags/TODO match to identify a project")
  2042. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2043. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2044. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2045. (defgroup org-agenda-skip nil
  2046. "Options concerning skipping parts of agenda files."
  2047. :tag "Org Agenda Skip"
  2048. :group 'org-agenda)
  2049. (defcustom org-agenda-todo-list-sublevels t
  2050. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2051. When nil, the sublevels of a TODO entry are not checked, resulting in
  2052. potentially much shorter TODO lists."
  2053. :group 'org-agenda-skip
  2054. :group 'org-todo
  2055. :type 'boolean)
  2056. (defcustom org-agenda-todo-ignore-with-date nil
  2057. "Non-nil means, don't show entries with a date in the global todo list.
  2058. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2059. but don't want them to show up in the TODO list.
  2060. When this is set, it also covers deadlines and scheduled items, the settings
  2061. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2062. will be ignored."
  2063. :group 'org-agenda-skip
  2064. :group 'org-todo
  2065. :type 'boolean)
  2066. (defcustom org-agenda-todo-ignore-scheduled nil
  2067. "Non-nil means, don't show scheduled entries in the global todo list.
  2068. The idea behind this is that by scheduling it, you have already taken care
  2069. of this item.
  2070. See also `org-agenda-todo-ignore-with-date'."
  2071. :group 'org-agenda-skip
  2072. :group 'org-todo
  2073. :type 'boolean)
  2074. (defcustom org-agenda-todo-ignore-deadlines nil
  2075. "Non-nil means, don't show near deadline entries in the global todo list.
  2076. Near means closer than `org-deadline-warning-days' days.
  2077. The idea behind this is that such items will appear in the agenda anyway.
  2078. See also `org-agenda-todo-ignore-with-date'."
  2079. :group 'org-agenda-skip
  2080. :group 'org-todo
  2081. :type 'boolean)
  2082. (defcustom org-agenda-skip-scheduled-if-done nil
  2083. "Non-nil means don't show scheduled items in agenda when they are done.
  2084. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2085. it applies only to the actual date of the scheduling. Warnings about
  2086. an item with a past scheduling dates are always turned off when the item
  2087. is DONE."
  2088. :group 'org-agenda-skip
  2089. :type 'boolean)
  2090. (defcustom org-agenda-skip-deadline-if-done nil
  2091. "Non-nil means don't show deadines when the corresponding item is done.
  2092. When nil, the deadline is still shown and should give you a happy feeling.
  2093. This is relevant for the daily/weekly agenda. And it applied only to the
  2094. actualy date of the deadline. Warnings about approching and past-due
  2095. deadlines are always turned off when the item is DONE."
  2096. :group 'org-agenda-skip
  2097. :type 'boolean)
  2098. (defcustom org-agenda-skip-timestamp-if-done nil
  2099. "Non-nil means don't don't select item by timestamp or -range if it is DONE."
  2100. :group 'org-agenda-skip
  2101. :type 'boolean)
  2102. (defcustom org-timeline-show-empty-dates 3
  2103. "Non-nil means, `org-timeline' also shows dates without an entry.
  2104. When nil, only the days which actually have entries are shown.
  2105. When t, all days between the first and the last date are shown.
  2106. When an integer, show also empty dates, but if there is a gap of more than
  2107. N days, just insert a special line indicating the size of the gap."
  2108. :group 'org-agenda-skip
  2109. :type '(choice
  2110. (const :tag "None" nil)
  2111. (const :tag "All" t)
  2112. (number :tag "at most")))
  2113. (defgroup org-agenda-startup nil
  2114. "Options concerning initial settings in the Agenda in Org Mode."
  2115. :tag "Org Agenda Startup"
  2116. :group 'org-agenda)
  2117. (defcustom org-finalize-agenda-hook nil
  2118. "Hook run just before displaying an agenda buffer."
  2119. :group 'org-agenda-startup
  2120. :type 'hook)
  2121. (defcustom org-agenda-mouse-1-follows-link nil
  2122. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2123. A longer mouse click will still set point. Does not wortk on XEmacs.
  2124. Needs to be set before org.el is loaded."
  2125. :group 'org-agenda-startup
  2126. :type 'boolean)
  2127. (defcustom org-agenda-start-with-follow-mode nil
  2128. "The initial value of follow-mode in a newly created agenda window."
  2129. :group 'org-agenda-startup
  2130. :type 'boolean)
  2131. (defgroup org-agenda-windows nil
  2132. "Options concerning the windows used by the Agenda in Org Mode."
  2133. :tag "Org Agenda Windows"
  2134. :group 'org-agenda)
  2135. (defcustom org-agenda-window-setup 'reorganize-frame
  2136. "How the agenda buffer should be displayed.
  2137. Possible values for this option are:
  2138. current-window Show agenda in the current window, keeping all other windows.
  2139. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2140. other-window Use `switch-to-buffer-other-window' to display agenda.
  2141. reorganize-frame Show only two windows on the current frame, the current
  2142. window and the agenda.
  2143. See also the variable `org-agenda-restore-windows-after-quit'."
  2144. :group 'org-agenda-windows
  2145. :type '(choice
  2146. (const current-window)
  2147. (const other-frame)
  2148. (const other-window)
  2149. (const reorganize-frame)))
  2150. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2151. "The min and max height of the agenda window as a fraction of frame height.
  2152. The value of the variable is a cons cell with two numbers between 0 and 1.
  2153. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2154. :group 'org-agenda-windows
  2155. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2156. (defcustom org-agenda-restore-windows-after-quit nil
  2157. "Non-nil means, restore window configuration open exiting agenda.
  2158. Before the window configuration is changed for displaying the agenda,
  2159. the current status is recorded. When the agenda is exited with
  2160. `q' or `x' and this option is set, the old state is restored. If
  2161. `org-agenda-window-setup' is `other-frame', the value of this
  2162. option will be ignored.."
  2163. :group 'org-agenda-windows
  2164. :type 'boolean)
  2165. (defcustom org-indirect-buffer-display 'other-window
  2166. "How should indirect tree buffers be displayed?
  2167. This applies to indirect buffers created with the commands
  2168. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2169. Valid values are:
  2170. current-window Display in the current window
  2171. other-window Just display in another window.
  2172. dedicated-frame Create one new frame, and re-use it each time.
  2173. new-frame Make a new frame each time. Note that in this case
  2174. previously-made indirect buffers are kept, and you need to
  2175. kill these buffers yourself."
  2176. :group 'org-structure
  2177. :group 'org-agenda-windows
  2178. :type '(choice
  2179. (const :tag "In current window" current-window)
  2180. (const :tag "In current frame, other window" other-window)
  2181. (const :tag "Each time a new frame" new-frame)
  2182. (const :tag "One dedicated frame" dedicated-frame)))
  2183. (defgroup org-agenda-daily/weekly nil
  2184. "Options concerning the daily/weekly agenda."
  2185. :tag "Org Agenda Daily/Weekly"
  2186. :group 'org-agenda)
  2187. (defcustom org-agenda-ndays 7
  2188. "Number of days to include in overview display.
  2189. Should be 1 or 7."
  2190. :group 'org-agenda-daily/weekly
  2191. :type 'number)
  2192. (defcustom org-agenda-start-on-weekday 1
  2193. "Non-nil means, start the overview always on the specified weekday.
  2194. 0 denotes Sunday, 1 denotes Monday etc.
  2195. When nil, always start on the current day."
  2196. :group 'org-agenda-daily/weekly
  2197. :type '(choice (const :tag "Today" nil)
  2198. (number :tag "Weekday No.")))
  2199. (defcustom org-agenda-show-all-dates t
  2200. "Non-nil means, `org-agenda' shows every day in the selected range.
  2201. When nil, only the days which actually have entries are shown."
  2202. :group 'org-agenda-daily/weekly
  2203. :type 'boolean)
  2204. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2205. "Format string for displaying dates in the agenda.
  2206. Used by the daily/weekly agenda and by the timeline. This should be
  2207. a format string understood by `format-time-string', or a function returning
  2208. the formatted date as a string. The function must take a single argument,
  2209. a calendar-style date list like (month day year)."
  2210. :group 'org-agenda-daily/weekly
  2211. :type '(choice
  2212. (string :tag "Format string")
  2213. (function :tag "Function")))
  2214. (defun org-agenda-format-date-aligned (date)
  2215. "Format a date string for display in the daily/weekly agenda, or timeline.
  2216. This function makes sure that dates are aligned for easy reading."
  2217. (format "%-9s %2d %s %4d"
  2218. (calendar-day-name date)
  2219. (extract-calendar-day date)
  2220. (calendar-month-name (extract-calendar-month date))
  2221. (extract-calendar-year date)))
  2222. (defcustom org-agenda-include-diary nil
  2223. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2224. :group 'org-agenda-daily/weekly
  2225. :type 'boolean)
  2226. (defcustom org-agenda-include-all-todo nil
  2227. "Set means weekly/daily agenda will always contain all TODO entries.
  2228. The TODO entries will be listed at the top of the agenda, before
  2229. the entries for specific days."
  2230. :group 'org-agenda-daily/weekly
  2231. :type 'boolean)
  2232. (defcustom org-agenda-repeating-timestamp-show-all t
  2233. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2234. When nil, only one occurence is shown, either today or the
  2235. nearest into the future."
  2236. :group 'org-agenda-daily/weekly
  2237. :type 'boolean)
  2238. (defcustom org-deadline-warning-days 14
  2239. "No. of days before expiration during which a deadline becomes active.
  2240. This variable governs the display in sparse trees and in the agenda.
  2241. When negative, it means use this number (the absolute value of it)
  2242. even if a deadline has a different individual lead time specified."
  2243. :group 'org-time
  2244. :group 'org-agenda-daily/weekly
  2245. :type 'number)
  2246. (defcustom org-scheduled-past-days 10000
  2247. "No. of days to continue listing scheduled items that are not marked DONE.
  2248. When an item is scheduled on a date, it shows up in the agenda on this
  2249. day and will be listed until it is marked done for the number of days
  2250. given here."
  2251. :group 'org-agenda-daily/weekly
  2252. :type 'number)
  2253. (defgroup org-agenda-time-grid nil
  2254. "Options concerning the time grid in the Org-mode Agenda."
  2255. :tag "Org Agenda Time Grid"
  2256. :group 'org-agenda)
  2257. (defcustom org-agenda-use-time-grid t
  2258. "Non-nil means, show a time grid in the agenda schedule.
  2259. A time grid is a set of lines for specific times (like every two hours between
  2260. 8:00 and 20:00). The items scheduled for a day at specific times are
  2261. sorted in between these lines.
  2262. For details about when the grid will be shown, and what it will look like, see
  2263. the variable `org-agenda-time-grid'."
  2264. :group 'org-agenda-time-grid
  2265. :type 'boolean)
  2266. (defcustom org-agenda-time-grid
  2267. '((daily today require-timed)
  2268. "----------------"
  2269. (800 1000 1200 1400 1600 1800 2000))
  2270. "The settings for time grid for agenda display.
  2271. This is a list of three items. The first item is again a list. It contains
  2272. symbols specifying conditions when the grid should be displayed:
  2273. daily if the agenda shows a single day
  2274. weekly if the agenda shows an entire week
  2275. today show grid on current date, independent of daily/weekly display
  2276. require-timed show grid only if at least one item has a time specification
  2277. The second item is a string which will be places behing the grid time.
  2278. The third item is a list of integers, indicating the times that should have
  2279. a grid line."
  2280. :group 'org-agenda-time-grid
  2281. :type
  2282. '(list
  2283. (set :greedy t :tag "Grid Display Options"
  2284. (const :tag "Show grid in single day agenda display" daily)
  2285. (const :tag "Show grid in weekly agenda display" weekly)
  2286. (const :tag "Always show grid for today" today)
  2287. (const :tag "Show grid only if any timed entries are present"
  2288. require-timed)
  2289. (const :tag "Skip grid times already present in an entry"
  2290. remove-match))
  2291. (string :tag "Grid String")
  2292. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2293. (defgroup org-agenda-sorting nil
  2294. "Options concerning sorting in the Org-mode Agenda."
  2295. :tag "Org Agenda Sorting"
  2296. :group 'org-agenda)
  2297. (let ((sorting-choice
  2298. '(choice
  2299. (const time-up) (const time-down)
  2300. (const category-keep) (const category-up) (const category-down)
  2301. (const tag-down) (const tag-up)
  2302. (const priority-up) (const priority-down))))
  2303. (defcustom org-agenda-sorting-strategy
  2304. '((agenda time-up category-keep priority-down)
  2305. (todo category-keep priority-down)
  2306. (tags category-keep priority-down))
  2307. "Sorting structure for the agenda items of a single day.
  2308. This is a list of symbols which will be used in sequence to determine
  2309. if an entry should be listed before another entry. The following
  2310. symbols are recognized:
  2311. time-up Put entries with time-of-day indications first, early first
  2312. time-down Put entries with time-of-day indications first, late first
  2313. category-keep Keep the default order of categories, corresponding to the
  2314. sequence in `org-agenda-files'.
  2315. category-up Sort alphabetically by category, A-Z.
  2316. category-down Sort alphabetically by category, Z-A.
  2317. tag-up Sort alphabetically by last tag, A-Z.
  2318. tag-down Sort alphabetically by last tag, Z-A.
  2319. priority-up Sort numerically by priority, high priority last.
  2320. priority-down Sort numerically by priority, high priority first.
  2321. The different possibilities will be tried in sequence, and testing stops
  2322. if one comparison returns a \"not-equal\". For example, the default
  2323. '(time-up category-keep priority-down)
  2324. means: Pull out all entries having a specified time of day and sort them,
  2325. in order to make a time schedule for the current day the first thing in the
  2326. agenda listing for the day. Of the entries without a time indication, keep
  2327. the grouped in categories, don't sort the categories, but keep them in
  2328. the sequence given in `org-agenda-files'. Within each category sort by
  2329. priority.
  2330. Leaving out `category-keep' would mean that items will be sorted across
  2331. categories by priority.
  2332. Instead of a single list, this can also be a set of list for specific
  2333. contents, with a context symbol in the car of the list, any of
  2334. `agenda', `todo', `tags' for the corresponding agenda views."
  2335. :group 'org-agenda-sorting
  2336. :type `(choice
  2337. (repeat :tag "General" ,sorting-choice)
  2338. (list :tag "Individually"
  2339. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2340. (repeat ,sorting-choice))
  2341. (cons (const :tag "Strategy for TODO lists" todo)
  2342. (repeat ,sorting-choice))
  2343. (cons (const :tag "Strategy for Tags matches" tags)
  2344. (repeat ,sorting-choice))))))
  2345. (defcustom org-sort-agenda-notime-is-late t
  2346. "Non-nil means, items without time are considered late.
  2347. This is only relevant for sorting. When t, items which have no explicit
  2348. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2349. do have a time. When nil, the default time is before 0:00. You can use this
  2350. option to decide if the schedule for today should come before or after timeless
  2351. agenda entries."
  2352. :group 'org-agenda-sorting
  2353. :type 'boolean)
  2354. (defgroup org-agenda-line-format nil
  2355. "Options concerning the entry prefix in the Org-mode agenda display."
  2356. :tag "Org Agenda Line Format"
  2357. :group 'org-agenda)
  2358. (defcustom org-agenda-prefix-format
  2359. '((agenda . " %-12:c%?-12t% s")
  2360. (timeline . " % s")
  2361. (todo . " %-12:c")
  2362. (tags . " %-12:c"))
  2363. "Format specifications for the prefix of items in the agenda views.
  2364. An alist with four entries, for the different agenda types. The keys to the
  2365. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2366. are format strings.
  2367. This format works similar to a printf format, with the following meaning:
  2368. %c the category of the item, \"Diary\" for entries from the diary, or
  2369. as given by the CATEGORY keyword or derived from the file name.
  2370. %T the *last* tag of the item. Last because inherited tags come
  2371. first in the list.
  2372. %t the time-of-day specification if one applies to the entry, in the
  2373. format HH:MM
  2374. %s Scheduling/Deadline information, a short string
  2375. All specifiers work basically like the standard `%s' of printf, but may
  2376. contain two additional characters: A question mark just after the `%' and
  2377. a whitespace/punctuation character just before the final letter.
  2378. If the first character after `%' is a question mark, the entire field
  2379. will only be included if the corresponding value applies to the
  2380. current entry. This is useful for fields which should have fixed
  2381. width when present, but zero width when absent. For example,
  2382. \"%?-12t\" will result in a 12 character time field if a time of the
  2383. day is specified, but will completely disappear in entries which do
  2384. not contain a time.
  2385. If there is punctuation or whitespace character just before the final
  2386. format letter, this character will be appended to the field value if
  2387. the value is not empty. For example, the format \"%-12:c\" leads to
  2388. \"Diary: \" if the category is \"Diary\". If the category were be
  2389. empty, no additional colon would be interted.
  2390. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2391. - Indent the line with two space characters
  2392. - Give the category in a 12 chars wide field, padded with whitespace on
  2393. the right (because of `-'). Append a colon if there is a category
  2394. (because of `:').
  2395. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2396. time, don't put in an empty field, just skip it (because of '?').
  2397. - Finally, put the scheduling information and append a whitespace.
  2398. As another example, if you don't want the time-of-day of entries in
  2399. the prefix, you could use:
  2400. (setq org-agenda-prefix-format \" %-11:c% s\")
  2401. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2402. `org-agenda-remove-tags'."
  2403. :type '(choice
  2404. (string :tag "General format")
  2405. (list :greedy t :tag "View dependent"
  2406. (cons (const agenda) (string :tag "Format"))
  2407. (cons (const timeline) (string :tag "Format"))
  2408. (cons (const todo) (string :tag "Format"))
  2409. (cons (const tags) (string :tag "Format"))))
  2410. :group 'org-agenda-line-format)
  2411. (defvar org-prefix-format-compiled nil
  2412. "The compiled version of the most recently used prefix format.
  2413. See the variable `org-agenda-prefix-format'.")
  2414. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2415. "Text preceeding scheduled items in the agenda view.
  2416. THis is a list with two strings. The first applies when the item is
  2417. scheduled on the current day. The second applies when it has been scheduled
  2418. previously, it may contain a %d to capture how many days ago the item was
  2419. scheduled."
  2420. :group 'org-agenda-line-format
  2421. :type '(list
  2422. (string :tag "Scheduled today ")
  2423. (string :tag "Scheduled previously")))
  2424. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2425. "Text preceeding deadline items in the agenda view.
  2426. This is a list with two strings. The first applies when the item has its
  2427. deadline on the current day. The second applies when it is in the past or
  2428. in the future, it may contain %d to capture how many days away the deadline
  2429. is (was)."
  2430. :group 'org-agenda-line-format
  2431. :type '(list
  2432. (string :tag "Deadline today ")
  2433. (string :tag "Deadline relative")))
  2434. (defcustom org-agenda-remove-times-when-in-prefix t
  2435. "Non-nil means, remove duplicate time specifications in agenda items.
  2436. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2437. time-of-day specification in a headline or diary entry is extracted and
  2438. placed into the prefix. If this option is non-nil, the original specification
  2439. \(a timestamp or -range, or just a plain time(range) specification like
  2440. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2441. cluttered.
  2442. The option can be t or nil. It may also be the symbol `beg', indicating
  2443. that the time should only be removed what it is located at the beginning of
  2444. the headline/diary entry."
  2445. :group 'org-agenda-line-format
  2446. :type '(choice
  2447. (const :tag "Always" t)
  2448. (const :tag "Never" nil)
  2449. (const :tag "When at beginning of entry" beg)))
  2450. (defcustom org-agenda-default-appointment-duration nil
  2451. "Default duration for appointments that only have a starting time.
  2452. When nil, no duration is specified in such cases.
  2453. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2454. :group 'org-agenda-line-format
  2455. :type '(choice
  2456. (integer :tag "Minutes")
  2457. (const :tag "No default duration")))
  2458. (defcustom org-agenda-remove-tags nil
  2459. "Non-nil means, remove the tags from the headline copy in the agenda.
  2460. When this is the symbol `prefix', only remove tags when
  2461. `org-agenda-prefix-format' contains a `%T' specifier."
  2462. :group 'org-agenda-line-format
  2463. :type '(choice
  2464. (const :tag "Always" t)
  2465. (const :tag "Never" nil)
  2466. (const :tag "When prefix format contains %T" prefix)))
  2467. (if (fboundp 'defvaralias)
  2468. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2469. 'org-agenda-remove-tags))
  2470. (defcustom org-agenda-tags-column -80
  2471. "Shift tags in agenda items to this column.
  2472. If this number is positive, it specifies the column. If it is negative,
  2473. it means that the tags should be flushright to that column. For example,
  2474. -80 works well for a normal 80 character screen."
  2475. :group 'org-agenda-line-format
  2476. :type 'integer)
  2477. (if (fboundp 'defvaralias)
  2478. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2479. (defcustom org-agenda-fontify-priorities t
  2480. "Non-nil means, highlight low and high priorities in agenda.
  2481. When t, the highest priority entries are bold, lowest priority italic.
  2482. This may also be an association list of priority faces. The face may be
  2483. a names face, or a list like `(:background \"Red\")'."
  2484. :group 'org-agenda-line-format
  2485. :type '(choice
  2486. (const :tag "Never" nil)
  2487. (const :tag "Defaults" t)
  2488. (repeat :tag "Specify"
  2489. (list (character :tag "Priority" :value ?A)
  2490. (sexp :tag "face")))))
  2491. (defgroup org-latex nil
  2492. "Options for embedding LaTeX code into Org-mode"
  2493. :tag "Org LaTeX"
  2494. :group 'org)
  2495. (defcustom org-format-latex-options
  2496. '(:foreground default :background default :scale 1.0
  2497. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2498. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2499. "Options for creating images from LaTeX fragments.
  2500. This is a property list with the following properties:
  2501. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2502. `default' means use the forground of the default face.
  2503. :background the background color, or \"Transparent\".
  2504. `default' means use the background of the default face.
  2505. :scale a scaling factor for the size of the images
  2506. :html-foreground, :html-background, :html-scale
  2507. The same numbers for HTML export.
  2508. :matchers a list indicating which matchers should be used to
  2509. find LaTeX fragments. Valid members of this list are:
  2510. \"begin\" find environments
  2511. \"$\" find math expressions surrounded by $...$
  2512. \"$$\" find math expressions surrounded by $$....$$
  2513. \"\\(\" find math expressions surrounded by \\(...\\)
  2514. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2515. :group 'org-latex
  2516. :type 'plist)
  2517. (defcustom org-format-latex-header "\\documentclass{article}
  2518. \\usepackage{fullpage} % do not remove
  2519. \\usepackage{amssymb}
  2520. \\usepackage[usenames]{color}
  2521. \\usepackage{amsmath}
  2522. \\usepackage{latexsym}
  2523. \\usepackage[mathscr]{eucal}
  2524. \\pagestyle{empty} % do not remove"
  2525. "The document header used for processing LaTeX fragments."
  2526. :group 'org-latex
  2527. :type 'string)
  2528. (defgroup org-export nil
  2529. "Options for exporting org-listings."
  2530. :tag "Org Export"
  2531. :group 'org)
  2532. (defgroup org-export-general nil
  2533. "General options for exporting Org-mode files."
  2534. :tag "Org Export General"
  2535. :group 'org-export)
  2536. ;; FIXME
  2537. (defvar org-export-publishing-directory nil)
  2538. (defcustom org-export-with-special-strings t
  2539. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2540. When this option is turned on, these strings will be exported as:
  2541. Org HTML LaTeX
  2542. -----+----------+--------
  2543. \\- &shy; \\-
  2544. -- &ndash; --
  2545. --- &mdash; ---
  2546. ... &hellip; \ldots
  2547. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2548. :group 'org-export-translation
  2549. :type 'boolean)
  2550. (defcustom org-export-language-setup
  2551. '(("en" "Author" "Date" "Table of Contents")
  2552. ("cs" "Autor" "Datum" "Obsah")
  2553. ("da" "Ophavsmand" "Dato" "Indhold")
  2554. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2555. ("es" "Autor" "Fecha" "\xcdndice")
  2556. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2557. ("it" "Autore" "Data" "Indice")
  2558. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2559. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2560. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2561. "Terms used in export text, translated to different languages.
  2562. Use the variable `org-export-default-language' to set the language,
  2563. or use the +OPTION lines for a per-file setting."
  2564. :group 'org-export-general
  2565. :type '(repeat
  2566. (list
  2567. (string :tag "HTML language tag")
  2568. (string :tag "Author")
  2569. (string :tag "Date")
  2570. (string :tag "Table of Contents"))))
  2571. (defcustom org-export-default-language "en"
  2572. "The default language of HTML export, as a string.
  2573. This should have an association in `org-export-language-setup'."
  2574. :group 'org-export-general
  2575. :type 'string)
  2576. (defcustom org-export-skip-text-before-1st-heading t
  2577. "Non-nil means, skip all text before the first headline when exporting.
  2578. When nil, that text is exported as well."
  2579. :group 'org-export-general
  2580. :type 'boolean)
  2581. (defcustom org-export-headline-levels 3
  2582. "The last level which is still exported as a headline.
  2583. Inferior levels will produce itemize lists when exported.
  2584. Note that a numeric prefix argument to an exporter function overrides
  2585. this setting.
  2586. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2587. :group 'org-export-general
  2588. :type 'number)
  2589. (defcustom org-export-with-section-numbers t
  2590. "Non-nil means, add section numbers to headlines when exporting.
  2591. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2592. :group 'org-export-general
  2593. :type 'boolean)
  2594. (defcustom org-export-with-toc t
  2595. "Non-nil means, create a table of contents in exported files.
  2596. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2597. When an integer, include levels up to N in the toc, this may then be
  2598. different from `org-export-headline-levels', but it will not be allowed
  2599. to be larger than the number of headline levels.
  2600. When nil, no table of contents is made.
  2601. Headlines which contain any TODO items will be marked with \"(*)\" in
  2602. ASCII export, and with red color in HTML output, if the option
  2603. `org-export-mark-todo-in-toc' is set.
  2604. In HTML output, the TOC will be clickable.
  2605. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2606. or \"toc:3\"."
  2607. :group 'org-export-general
  2608. :type '(choice
  2609. (const :tag "No Table of Contents" nil)
  2610. (const :tag "Full Table of Contents" t)
  2611. (integer :tag "TOC to level")))
  2612. (defcustom org-export-mark-todo-in-toc nil
  2613. "Non-nil means, mark TOC lines that contain any open TODO items."
  2614. :group 'org-export-general
  2615. :type 'boolean)
  2616. (defcustom org-export-preserve-breaks nil
  2617. "Non-nil means, preserve all line breaks when exporting.
  2618. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2619. export, line breaks will always be preserved, regardless of this variable.
  2620. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2621. :group 'org-export-general
  2622. :type 'boolean)
  2623. (defcustom org-export-with-archived-trees 'headline
  2624. "Whether subtrees with the ARCHIVE tag should be exported.
  2625. This can have three different values
  2626. nil Do not export, pretend this tree is not present
  2627. t Do export the entire tree
  2628. headline Only export the headline, but skip the tree below it."
  2629. :group 'org-export-general
  2630. :group 'org-archive
  2631. :type '(choice
  2632. (const :tag "not at all" nil)
  2633. (const :tag "headline only" 'headline)
  2634. (const :tag "entirely" t)))
  2635. (defcustom org-export-author-info t
  2636. "Non-nil means, insert author name and email into the exported file.
  2637. This option can also be set with the +OPTIONS line,
  2638. e.g. \"author-info:nil\"."
  2639. :group 'org-export-general
  2640. :type 'boolean)
  2641. (defcustom org-export-time-stamp-file t
  2642. "Non-nil means, insert a time stamp into the exported file.
  2643. The time stamp shows when the file was created.
  2644. This option can also be set with the +OPTIONS line,
  2645. e.g. \"timestamp:nil\"."
  2646. :group 'org-export-general
  2647. :type 'boolean)
  2648. (defcustom org-export-with-timestamps t
  2649. "If nil, do not export time stamps and associated keywords."
  2650. :group 'org-export-general
  2651. :type 'boolean)
  2652. (defcustom org-export-remove-timestamps-from-toc t
  2653. "If nil, remove timestamps from the table of contents entries."
  2654. :group 'org-export-general
  2655. :type 'boolean)
  2656. (defcustom org-export-with-tags 'not-in-toc
  2657. "If nil, do not export tags, just remove them from headlines.
  2658. If this is the symbol `not-in-toc', tags will be removed from table of
  2659. contents entries, but still be shown in the headlines of the document.
  2660. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2661. :group 'org-export-general
  2662. :type '(choice
  2663. (const :tag "Off" nil)
  2664. (const :tag "Not in TOC" not-in-toc)
  2665. (const :tag "On" t)))
  2666. (defcustom org-export-with-drawers nil
  2667. "Non-nil means, export with drawers like the property drawer.
  2668. When t, all drawers are exported. This may also be a list of
  2669. drawer names to export."
  2670. :group 'org-export-general
  2671. :type '(choice
  2672. (const :tag "All drawers" t)
  2673. (const :tag "None" nil)
  2674. (repeat :tag "Selected drawers"
  2675. (string :tag "Drawer name"))))
  2676. (defgroup org-export-translation nil
  2677. "Options for translating special ascii sequences for the export backends."
  2678. :tag "Org Export Translation"
  2679. :group 'org-export)
  2680. (defcustom org-export-with-emphasize t
  2681. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2682. If the export target supports emphasizing text, the word will be
  2683. typeset in bold, italic, or underlined, respectively. Works only for
  2684. single words, but you can say: I *really* *mean* *this*.
  2685. Not all export backends support this.
  2686. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2687. :group 'org-export-translation
  2688. :type 'boolean)
  2689. (defcustom org-export-with-footnotes t
  2690. "If nil, export [1] as a footnote marker.
  2691. Lines starting with [1] will be formatted as footnotes.
  2692. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2693. :group 'org-export-translation
  2694. :type 'boolean)
  2695. (defcustom org-export-with-sub-superscripts t
  2696. "Non-nil means, interpret \"_\" and \"^\" for export.
  2697. When this option is turned on, you can use TeX-like syntax for sub- and
  2698. superscripts. Several characters after \"_\" or \"^\" will be
  2699. considered as a single item - so grouping with {} is normally not
  2700. needed. For example, the following things will be parsed as single
  2701. sub- or superscripts.
  2702. 10^24 or 10^tau several digits will be considered 1 item.
  2703. 10^-12 or 10^-tau a leading sign with digits or a word
  2704. x^2-y^3 will be read as x^2 - y^3, because items are
  2705. terminated by almost any nonword/nondigit char.
  2706. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2707. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2708. sub/superscript. If you set this variable to the symbol `{}',
  2709. the braces are *required* in order to trigger interpretations as
  2710. sub/superscript. This can be helpful in documents that need \"_\"
  2711. frequently in plain text.
  2712. Not all export backends support this, but HTML does.
  2713. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2714. :group 'org-export-translation
  2715. :type '(choice
  2716. (const :tag "Always interpret" t)
  2717. (const :tag "Only with braces" {})
  2718. (const :tag "Never interpret" nil)))
  2719. (defcustom org-export-with-special-strings t
  2720. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2721. When this option is turned on, these strings will be exported as:
  2722. \\- : &shy;
  2723. -- : &ndash;
  2724. --- : &mdash;
  2725. Not all export backends support this, but HTML does.
  2726. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2727. :group 'org-export-translation
  2728. :type 'boolean)
  2729. (defcustom org-export-with-TeX-macros t
  2730. "Non-nil means, interpret simple TeX-like macros when exporting.
  2731. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2732. No only real TeX macros will work here, but the standard HTML entities
  2733. for math can be used as macro names as well. For a list of supported
  2734. names in HTML export, see the constant `org-html-entities'.
  2735. Not all export backends support this.
  2736. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2737. :group 'org-export-translation
  2738. :group 'org-export-latex
  2739. :type 'boolean)
  2740. (defcustom org-export-with-LaTeX-fragments nil
  2741. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2742. When set, the exporter will find LaTeX environments if the \\begin line is
  2743. the first non-white thing on a line. It will also find the math delimiters
  2744. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2745. display math.
  2746. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2747. :group 'org-export-translation
  2748. :group 'org-export-latex
  2749. :type 'boolean)
  2750. (defcustom org-export-with-fixed-width t
  2751. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2752. This can be used to have pre-formatted text, fragments of code etc. For
  2753. example:
  2754. : ;; Some Lisp examples
  2755. : (while (defc cnt)
  2756. : (ding))
  2757. will be looking just like this in also HTML. See also the QUOTE keyword.
  2758. Not all export backends support this.
  2759. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2760. :group 'org-export-translation
  2761. :type 'boolean)
  2762. (defcustom org-match-sexp-depth 3
  2763. "Number of stacked braces for sub/superscript matching.
  2764. This has to be set before loading org.el to be effective."
  2765. :group 'org-export-translation
  2766. :type 'integer)
  2767. (defgroup org-export-tables nil
  2768. "Options for exporting tables in Org-mode."
  2769. :tag "Org Export Tables"
  2770. :group 'org-export)
  2771. (defcustom org-export-with-tables t
  2772. "If non-nil, lines starting with \"|\" define a table.
  2773. For example:
  2774. | Name | Address | Birthday |
  2775. |-------------+----------+-----------|
  2776. | Arthur Dent | England | 29.2.2100 |
  2777. Not all export backends support this.
  2778. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2779. :group 'org-export-tables
  2780. :type 'boolean)
  2781. (defcustom org-export-highlight-first-table-line t
  2782. "Non-nil means, highlight the first table line.
  2783. In HTML export, this means use <th> instead of <td>.
  2784. In tables created with table.el, this applies to the first table line.
  2785. In Org-mode tables, all lines before the first horizontal separator
  2786. line will be formatted with <th> tags."
  2787. :group 'org-export-tables
  2788. :type 'boolean)
  2789. (defcustom org-export-table-remove-special-lines t
  2790. "Remove special lines and marking characters in calculating tables.
  2791. This removes the special marking character column from tables that are set
  2792. up for spreadsheet calculations. It also removes the entire lines
  2793. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2794. the values of constants may be useful to have."
  2795. :group 'org-export-tables
  2796. :type 'boolean)
  2797. (defcustom org-export-prefer-native-exporter-for-tables nil
  2798. "Non-nil means, always export tables created with table.el natively.
  2799. Natively means, use the HTML code generator in table.el.
  2800. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2801. the table does not use row- or column-spanning). This has the
  2802. advantage, that the automatic HTML conversions for math symbols and
  2803. sub/superscripts can be applied. Org-mode's HTML generator is also
  2804. much faster."
  2805. :group 'org-export-tables
  2806. :type 'boolean)
  2807. (defgroup org-export-ascii nil
  2808. "Options specific for ASCII export of Org-mode files."
  2809. :tag "Org Export ASCII"
  2810. :group 'org-export)
  2811. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2812. "Characters for underlining headings in ASCII export.
  2813. In the given sequence, these characters will be used for level 1, 2, ..."
  2814. :group 'org-export-ascii
  2815. :type '(repeat character))
  2816. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2817. "Bullet characters for headlines converted to lists in ASCII export.
  2818. The first character is is used for the first lest level generated in this
  2819. way, and so on. If there are more levels than characters given here,
  2820. the list will be repeated.
  2821. Note that plain lists will keep the same bullets as the have in the
  2822. Org-mode file."
  2823. :group 'org-export-ascii
  2824. :type '(repeat character))
  2825. (defgroup org-export-xml nil
  2826. "Options specific for XML export of Org-mode files."
  2827. :tag "Org Export XML"
  2828. :group 'org-export)
  2829. (defgroup org-export-html nil
  2830. "Options specific for HTML export of Org-mode files."
  2831. :tag "Org Export HTML"
  2832. :group 'org-export)
  2833. (defcustom org-export-html-coding-system nil
  2834. ""
  2835. :group 'org-export-html
  2836. :type 'coding-system)
  2837. (defcustom org-export-html-extension "html"
  2838. "The extension for exported HTML files."
  2839. :group 'org-export-html
  2840. :type 'string)
  2841. (defcustom org-export-html-style
  2842. "<style type=\"text/css\">
  2843. html {
  2844. font-family: Times, serif;
  2845. font-size: 12pt;
  2846. }
  2847. .title { text-align: center; }
  2848. .todo { color: red; }
  2849. .done { color: green; }
  2850. .timestamp { color: grey }
  2851. .timestamp-kwd { color: CadetBlue }
  2852. .tag { background-color:lightblue; font-weight:normal }
  2853. .target { background-color: lavender; }
  2854. pre {
  2855. border: 1pt solid #AEBDCC;
  2856. background-color: #F3F5F7;
  2857. padding: 5pt;
  2858. font-family: courier, monospace;
  2859. }
  2860. table { border-collapse: collapse; }
  2861. td, th {
  2862. vertical-align: top;
  2863. <!--border: 1pt solid #ADB9CC;-->
  2864. }
  2865. </style>"
  2866. "The default style specification for exported HTML files.
  2867. Since there are different ways of setting style information, this variable
  2868. needs to contain the full HTML structure to provide a style, including the
  2869. surrounding HTML tags. The style specifications should include definitions
  2870. for new classes todo, done, title, and deadline. For example, legal values
  2871. would be:
  2872. <style type=\"text/css\">
  2873. p { font-weight: normal; color: gray; }
  2874. h1 { color: black; }
  2875. .title { text-align: center; }
  2876. .todo, .deadline { color: red; }
  2877. .done { color: green; }
  2878. </style>
  2879. or, if you want to keep the style in a file,
  2880. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2881. As the value of this option simply gets inserted into the HTML <head> header,
  2882. you can \"misuse\" it to add arbitrary text to the header."
  2883. :group 'org-export-html
  2884. :type 'string)
  2885. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2886. "Format for typesetting the document title in HTML export."
  2887. :group 'org-export-html
  2888. :type 'string)
  2889. (defcustom org-export-html-toplevel-hlevel 2
  2890. "The <H> level for level 1 headings in HTML export."
  2891. :group 'org-export-html
  2892. :type 'string)
  2893. (defcustom org-export-html-link-org-files-as-html t
  2894. "Non-nil means, make file links to `file.org' point to `file.html'.
  2895. When org-mode is exporting an org-mode file to HTML, links to
  2896. non-html files are directly put into a href tag in HTML.
  2897. However, links to other Org-mode files (recognized by the
  2898. extension `.org.) should become links to the corresponding html
  2899. file, assuming that the linked org-mode file will also be
  2900. converted to HTML.
  2901. When nil, the links still point to the plain `.org' file."
  2902. :group 'org-export-html
  2903. :type 'boolean)
  2904. (defcustom org-export-html-inline-images 'maybe
  2905. "Non-nil means, inline images into exported HTML pages.
  2906. This is done using an <img> tag. When nil, an anchor with href is used to
  2907. link to the image. If this option is `maybe', then images in links with
  2908. an empty description will be inlined, while images with a description will
  2909. be linked only."
  2910. :group 'org-export-html
  2911. :type '(choice (const :tag "Never" nil)
  2912. (const :tag "Always" t)
  2913. (const :tag "When there is no description" maybe)))
  2914. ;; FIXME: rename
  2915. (defcustom org-export-html-expand t
  2916. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2917. When nil, these tags will be exported as plain text and therefore
  2918. not be interpreted by a browser.
  2919. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2920. :group 'org-export-html
  2921. :type 'boolean)
  2922. (defcustom org-export-html-table-tag
  2923. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2924. "The HTML tag that is used to start a table.
  2925. This must be a <table> tag, but you may change the options like
  2926. borders and spacing."
  2927. :group 'org-export-html
  2928. :type 'string)
  2929. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2930. "The opening tag for table header fields.
  2931. This is customizable so that alignment options can be specified."
  2932. :group 'org-export-tables
  2933. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2934. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2935. "The opening tag for table data fields.
  2936. This is customizable so that alignment options can be specified."
  2937. :group 'org-export-tables
  2938. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2939. (defcustom org-export-html-with-timestamp nil
  2940. "If non-nil, write `org-export-html-html-helper-timestamp'
  2941. into the exported HTML text. Otherwise, the buffer will just be saved
  2942. to a file."
  2943. :group 'org-export-html
  2944. :type 'boolean)
  2945. (defcustom org-export-html-html-helper-timestamp
  2946. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2947. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2948. :group 'org-export-html
  2949. :type 'string)
  2950. (defgroup org-export-icalendar nil
  2951. "Options specific for iCalendar export of Org-mode files."
  2952. :tag "Org Export iCalendar"
  2953. :group 'org-export)
  2954. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2955. "The file name for the iCalendar file covering all agenda files.
  2956. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2957. The file name should be absolute, the file will be overwritten without warning."
  2958. :group 'org-export-icalendar
  2959. :type 'file)
  2960. (defcustom org-icalendar-include-todo nil
  2961. "Non-nil means, export to iCalendar files should also cover TODO items."
  2962. :group 'org-export-icalendar
  2963. :type '(choice
  2964. (const :tag "None" nil)
  2965. (const :tag "Unfinished" t)
  2966. (const :tag "All" all)))
  2967. (defcustom org-icalendar-include-sexps t
  2968. "Non-nil means, export to iCalendar files should also cover sexp entries.
  2969. These are entries like in the diary, but directly in an Org-mode file."
  2970. :group 'org-export-icalendar
  2971. :type 'boolean)
  2972. (defcustom org-icalendar-include-body 100
  2973. "Amount of text below headline to be included in iCalendar export.
  2974. This is a number of characters that should maximally be included.
  2975. Properties, scheduling and clocking lines will always be removed.
  2976. The text will be inserted into the DESCRIPTION field."
  2977. :group 'org-export-icalendar
  2978. :type '(choice
  2979. (const :tag "Nothing" nil)
  2980. (const :tag "Everything" t)
  2981. (integer :tag "Max characters")))
  2982. (defcustom org-icalendar-combined-name "OrgMode"
  2983. "Calendar name for the combined iCalendar representing all agenda files."
  2984. :group 'org-export-icalendar
  2985. :type 'string)
  2986. (defgroup org-font-lock nil
  2987. "Font-lock settings for highlighting in Org-mode."
  2988. :tag "Org Font Lock"
  2989. :group 'org)
  2990. (defcustom org-level-color-stars-only nil
  2991. "Non-nil means fontify only the stars in each headline.
  2992. When nil, the entire headline is fontified.
  2993. Changing it requires restart of `font-lock-mode' to become effective
  2994. also in regions already fontified."
  2995. :group 'org-font-lock
  2996. :type 'boolean)
  2997. (defcustom org-hide-leading-stars nil
  2998. "Non-nil means, hide the first N-1 stars in a headline.
  2999. This works by using the face `org-hide' for these stars. This
  3000. face is white for a light background, and black for a dark
  3001. background. You may have to customize the face `org-hide' to
  3002. make this work.
  3003. Changing it requires restart of `font-lock-mode' to become effective
  3004. also in regions already fontified.
  3005. You may also set this on a per-file basis by adding one of the following
  3006. lines to the buffer:
  3007. #+STARTUP: hidestars
  3008. #+STARTUP: showstars"
  3009. :group 'org-font-lock
  3010. :type 'boolean)
  3011. (defcustom org-fontify-done-headline nil
  3012. "Non-nil means, change the face of a headline if it is marked DONE.
  3013. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3014. When this is non-nil, the headline after the keyword is set to the
  3015. `org-headline-done' as an additional indication."
  3016. :group 'org-font-lock
  3017. :type 'boolean)
  3018. (defcustom org-fontify-emphasized-text t
  3019. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3020. Changing this variable requires a restart of Emacs to take effect."
  3021. :group 'org-font-lock
  3022. :type 'boolean)
  3023. (defcustom org-hide-emphasis-markers nil
  3024. "Non-nil measn font-lock should hide the emphasis marker characters."
  3025. :group 'org-font-lock
  3026. :type 'boolean)
  3027. (defvar org-emph-re nil
  3028. "Regular expression for matching emphasis.")
  3029. (defvar org-verbatim-re nil
  3030. "Regular expression for matching verbatim text.")
  3031. (defvar org-emphasis-regexp-components) ; defined just below
  3032. (defvar org-emphasis-alist) ; defined just below
  3033. (defun org-set-emph-re (var val)
  3034. "Set variable and compute the emphasis regular expression."
  3035. (set var val)
  3036. (when (and (boundp 'org-emphasis-alist)
  3037. (boundp 'org-emphasis-regexp-components)
  3038. org-emphasis-alist org-emphasis-regexp-components)
  3039. (let* ((e org-emphasis-regexp-components)
  3040. (pre (car e))
  3041. (post (nth 1 e))
  3042. (border (nth 2 e))
  3043. (body (nth 3 e))
  3044. (nl (nth 4 e))
  3045. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3046. (body1 (concat body "*?"))
  3047. (markers (mapconcat 'car org-emphasis-alist ""))
  3048. (vmarkers (mapconcat
  3049. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3050. org-emphasis-alist "")))
  3051. ;; make sure special characters appear at the right position in the class
  3052. (if (string-match "\\^" markers)
  3053. (setq markers (concat (replace-match "" t t markers) "^")))
  3054. (if (string-match "-" markers)
  3055. (setq markers (concat (replace-match "" t t markers) "-")))
  3056. (if (string-match "\\^" vmarkers)
  3057. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3058. (if (string-match "-" vmarkers)
  3059. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3060. (if (> nl 0)
  3061. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3062. (int-to-string nl) "\\}")))
  3063. ;; Make the regexp
  3064. (setq org-emph-re
  3065. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3066. "\\("
  3067. "\\([" markers "]\\)"
  3068. "\\("
  3069. "[^" border "]\\|"
  3070. "[^" border (if (and nil stacked) markers) "]"
  3071. body1
  3072. "[^" border (if (and nil stacked) markers) "]"
  3073. "\\)"
  3074. "\\3\\)"
  3075. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3076. (setq org-verbatim-re
  3077. (concat "\\([" pre "]\\|^\\)"
  3078. "\\("
  3079. "\\([" vmarkers "]\\)"
  3080. "\\("
  3081. "[^" border "]\\|"
  3082. "[^" border "]"
  3083. body1
  3084. "[^" border "]"
  3085. "\\)"
  3086. "\\3\\)"
  3087. "\\([" post "]\\|$\\)")))))
  3088. (defcustom org-emphasis-regexp-components
  3089. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3090. "Components used to build the regular expression for emphasis.
  3091. This is a list with 6 entries. Terminology: In an emphasis string
  3092. like \" *strong word* \", we call the initial space PREMATCH, the final
  3093. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3094. and \"trong wor\" is the body. The different components in this variable
  3095. specify what is allowed/forbidden in each part:
  3096. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3097. post Chars allowed as postmatch. End of line will be allowed too.
  3098. border The chars *forbidden* as border characters.
  3099. body-regexp A regexp like \".\" to match a body character. Don't use
  3100. non-shy groups here, and don't allow newline here.
  3101. newline The maximum number of newlines allowed in an emphasis exp.
  3102. Use customize to modify this, or restart Emacs after changing it."
  3103. :group 'org-font-lock
  3104. :set 'org-set-emph-re
  3105. :type '(list
  3106. (sexp :tag "Allowed chars in pre ")
  3107. (sexp :tag "Allowed chars in post ")
  3108. (sexp :tag "Forbidden chars in border ")
  3109. (sexp :tag "Regexp for body ")
  3110. (integer :tag "number of newlines allowed")
  3111. (option (boolean :tag "Stacking (DISABLED) "))))
  3112. (defcustom org-emphasis-alist
  3113. '(("*" bold "<b>" "</b>")
  3114. ("/" italic "<i>" "</i>")
  3115. ("_" underline "<u>" "</u>")
  3116. ("=" org-code "<code>" "</code>" verbatim)
  3117. ("~" org-verbatim "" "" verbatim)
  3118. ("+" (:strike-through t) "<del>" "</del>")
  3119. )
  3120. "Special syntax for emphasized text.
  3121. Text starting and ending with a special character will be emphasized, for
  3122. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3123. characters, the face to be used by font-lock for highlighting in Org-mode
  3124. Emacs buffers, and the HTML tags to be used for this.
  3125. Use customize to modify this, or restart Emacs after changing it."
  3126. :group 'org-font-lock
  3127. :set 'org-set-emph-re
  3128. :type '(repeat
  3129. (list
  3130. (string :tag "Marker character")
  3131. (choice
  3132. (face :tag "Font-lock-face")
  3133. (plist :tag "Face property list"))
  3134. (string :tag "HTML start tag")
  3135. (string :tag "HTML end tag")
  3136. (option (const verbatim)))))
  3137. ;;; The faces
  3138. (defgroup org-faces nil
  3139. "Faces in Org-mode."
  3140. :tag "Org Faces"
  3141. :group 'org-font-lock)
  3142. (defun org-compatible-face (inherits specs)
  3143. "Make a compatible face specification.
  3144. If INHERITS is an existing face and if the Emacs version supports it,
  3145. just inherit the face. If not, use SPECS to define the face.
  3146. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3147. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3148. to the top of the list. The `min-colors' attribute will be removed from
  3149. any other entries, and any resulting duplicates will be removed entirely."
  3150. (cond
  3151. ((and inherits (facep inherits)
  3152. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3153. ;; In Emacs 23, we use inheritance where possible.
  3154. ;; We only do this in Emacs 23, because only there the outline
  3155. ;; faces have been changed to the original org-mode-level-faces.
  3156. (list (list t :inherit inherits)))
  3157. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3158. ;; These do not understand the `min-colors' attribute.
  3159. (let (r e a)
  3160. (while (setq e (pop specs))
  3161. (cond
  3162. ((memq (car e) '(t default)) (push e r))
  3163. ((setq a (member '(min-colors 8) (car e)))
  3164. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3165. (cdr e)))))
  3166. ((setq a (assq 'min-colors (car e)))
  3167. (setq e (cons (delq a (car e)) (cdr e)))
  3168. (or (assoc (car e) r) (push e r)))
  3169. (t (or (assoc (car e) r) (push e r)))))
  3170. (nreverse r)))
  3171. (t specs)))
  3172. (defface org-hide
  3173. '((((background light)) (:foreground "white"))
  3174. (((background dark)) (:foreground "black")))
  3175. "Face used to hide leading stars in headlines.
  3176. The forground color of this face should be equal to the background
  3177. color of the frame."
  3178. :group 'org-faces)
  3179. (defface org-level-1 ;; font-lock-function-name-face
  3180. (org-compatible-face
  3181. 'outline-1
  3182. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3183. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3184. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3185. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3186. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3187. (t (:bold t))))
  3188. "Face used for level 1 headlines."
  3189. :group 'org-faces)
  3190. (defface org-level-2 ;; font-lock-variable-name-face
  3191. (org-compatible-face
  3192. 'outline-2
  3193. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3194. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3195. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3196. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3197. (t (:bold t))))
  3198. "Face used for level 2 headlines."
  3199. :group 'org-faces)
  3200. (defface org-level-3 ;; font-lock-keyword-face
  3201. (org-compatible-face
  3202. 'outline-3
  3203. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3204. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3205. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3206. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3207. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3208. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3209. (t (:bold t))))
  3210. "Face used for level 3 headlines."
  3211. :group 'org-faces)
  3212. (defface org-level-4 ;; font-lock-comment-face
  3213. (org-compatible-face
  3214. 'outline-4
  3215. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3216. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3217. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3218. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3219. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3220. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3221. (t (:bold t))))
  3222. "Face used for level 4 headlines."
  3223. :group 'org-faces)
  3224. (defface org-level-5 ;; font-lock-type-face
  3225. (org-compatible-face
  3226. 'outline-5
  3227. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3228. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3229. (((class color) (min-colors 8)) (:foreground "green"))))
  3230. "Face used for level 5 headlines."
  3231. :group 'org-faces)
  3232. (defface org-level-6 ;; font-lock-constant-face
  3233. (org-compatible-face
  3234. 'outline-6
  3235. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3236. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3237. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3238. "Face used for level 6 headlines."
  3239. :group 'org-faces)
  3240. (defface org-level-7 ;; font-lock-builtin-face
  3241. (org-compatible-face
  3242. 'outline-7
  3243. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3244. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3245. (((class color) (min-colors 8)) (:foreground "blue"))))
  3246. "Face used for level 7 headlines."
  3247. :group 'org-faces)
  3248. (defface org-level-8 ;; font-lock-string-face
  3249. (org-compatible-face
  3250. 'outline-8
  3251. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3252. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3253. (((class color) (min-colors 8)) (:foreground "green"))))
  3254. "Face used for level 8 headlines."
  3255. :group 'org-faces)
  3256. (defface org-special-keyword ;; font-lock-string-face
  3257. (org-compatible-face
  3258. nil
  3259. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3260. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3261. (t (:italic t))))
  3262. "Face used for special keywords."
  3263. :group 'org-faces)
  3264. (defface org-drawer ;; font-lock-function-name-face
  3265. (org-compatible-face
  3266. nil
  3267. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3268. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3269. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3270. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3271. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3272. (t (:bold t))))
  3273. "Face used for drawers."
  3274. :group 'org-faces)
  3275. (defface org-property-value nil
  3276. "Face used for the value of a property."
  3277. :group 'org-faces)
  3278. (defface org-column
  3279. (org-compatible-face
  3280. nil
  3281. '((((class color) (min-colors 16) (background light))
  3282. (:background "grey90"))
  3283. (((class color) (min-colors 16) (background dark))
  3284. (:background "grey30"))
  3285. (((class color) (min-colors 8))
  3286. (:background "cyan" :foreground "black"))
  3287. (t (:inverse-video t))))
  3288. "Face for column display of entry properties."
  3289. :group 'org-faces)
  3290. (when (fboundp 'set-face-attribute)
  3291. ;; Make sure that a fixed-width face is used when we have a column table.
  3292. (set-face-attribute 'org-column nil
  3293. :height (face-attribute 'default :height)
  3294. :family (face-attribute 'default :family)))
  3295. (defface org-warning
  3296. (org-compatible-face
  3297. 'font-lock-warning-face
  3298. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3299. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3300. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3301. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3302. (t (:bold t))))
  3303. "Face for deadlines and TODO keywords."
  3304. :group 'org-faces)
  3305. (defface org-archived ; similar to shadow
  3306. (org-compatible-face
  3307. 'shadow
  3308. '((((class color grayscale) (min-colors 88) (background light))
  3309. (:foreground "grey50"))
  3310. (((class color grayscale) (min-colors 88) (background dark))
  3311. (:foreground "grey70"))
  3312. (((class color) (min-colors 8) (background light))
  3313. (:foreground "green"))
  3314. (((class color) (min-colors 8) (background dark))
  3315. (:foreground "yellow"))))
  3316. "Face for headline with the ARCHIVE tag."
  3317. :group 'org-faces)
  3318. (defface org-link
  3319. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3320. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3321. (t (:underline t)))
  3322. "Face for links."
  3323. :group 'org-faces)
  3324. (defface org-ellipsis
  3325. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3326. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3327. (t (:strike-through t)))
  3328. "Face for the ellipsis in folded text."
  3329. :group 'org-faces)
  3330. (defface org-target
  3331. '((((class color) (background light)) (:underline t))
  3332. (((class color) (background dark)) (:underline t))
  3333. (t (:underline t)))
  3334. "Face for links."
  3335. :group 'org-faces)
  3336. (defface org-date
  3337. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3338. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3339. (t (:underline t)))
  3340. "Face for links."
  3341. :group 'org-faces)
  3342. (defface org-sexp-date
  3343. '((((class color) (background light)) (:foreground "Purple"))
  3344. (((class color) (background dark)) (:foreground "Cyan"))
  3345. (t (:underline t)))
  3346. "Face for links."
  3347. :group 'org-faces)
  3348. (defface org-tag
  3349. '((t (:bold t)))
  3350. "Face for tags."
  3351. :group 'org-faces)
  3352. (defface org-todo ; font-lock-warning-face
  3353. (org-compatible-face
  3354. nil
  3355. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3356. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3357. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3358. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3359. (t (:inverse-video t :bold t))))
  3360. "Face for TODO keywords."
  3361. :group 'org-faces)
  3362. (defface org-done ;; font-lock-type-face
  3363. (org-compatible-face
  3364. nil
  3365. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3366. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3367. (((class color) (min-colors 8)) (:foreground "green"))
  3368. (t (:bold t))))
  3369. "Face used for todo keywords that indicate DONE items."
  3370. :group 'org-faces)
  3371. (defface org-headline-done ;; font-lock-string-face
  3372. (org-compatible-face
  3373. nil
  3374. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3375. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3376. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3377. "Face used to indicate that a headline is DONE.
  3378. This face is only used if `org-fontify-done-headline' is set. If applies
  3379. to the part of the headline after the DONE keyword."
  3380. :group 'org-faces)
  3381. (defcustom org-todo-keyword-faces nil
  3382. "Faces for specific TODO keywords.
  3383. This is a list of cons cells, with TODO keywords in the car
  3384. and faces in the cdr. The face can be a symbol, or a property
  3385. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3386. :group 'org-faces
  3387. :group 'org-todo
  3388. :type '(repeat
  3389. (cons
  3390. (string :tag "keyword")
  3391. (sexp :tag "face"))))
  3392. (defface org-table ;; font-lock-function-name-face
  3393. (org-compatible-face
  3394. nil
  3395. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3396. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3397. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3398. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3399. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3400. (((class color) (min-colors 8) (background dark)))))
  3401. "Face used for tables."
  3402. :group 'org-faces)
  3403. (defface org-formula
  3404. (org-compatible-face
  3405. nil
  3406. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3407. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3408. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3409. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3410. (t (:bold t :italic t))))
  3411. "Face for formulas."
  3412. :group 'org-faces)
  3413. (defface org-code
  3414. (org-compatible-face
  3415. nil
  3416. '((((class color grayscale) (min-colors 88) (background light))
  3417. (:foreground "grey50"))
  3418. (((class color grayscale) (min-colors 88) (background dark))
  3419. (:foreground "grey70"))
  3420. (((class color) (min-colors 8) (background light))
  3421. (:foreground "green"))
  3422. (((class color) (min-colors 8) (background dark))
  3423. (:foreground "yellow"))))
  3424. "Face for fixed-with text like code snippets."
  3425. :group 'org-faces
  3426. :version "22.1")
  3427. (defface org-verbatim
  3428. (org-compatible-face
  3429. nil
  3430. '((((class color grayscale) (min-colors 88) (background light))
  3431. (:foreground "grey50" :underline t))
  3432. (((class color grayscale) (min-colors 88) (background dark))
  3433. (:foreground "grey70" :underline t))
  3434. (((class color) (min-colors 8) (background light))
  3435. (:foreground "green" :underline t))
  3436. (((class color) (min-colors 8) (background dark))
  3437. (:foreground "yellow" :underline t))))
  3438. "Face for fixed-with text like code snippets."
  3439. :group 'org-faces
  3440. :version "22.1")
  3441. (defface org-agenda-structure ;; font-lock-function-name-face
  3442. (org-compatible-face
  3443. nil
  3444. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3445. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3446. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3447. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3448. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3449. (t (:bold t))))
  3450. "Face used in agenda for captions and dates."
  3451. :group 'org-faces)
  3452. (defface org-scheduled-today
  3453. (org-compatible-face
  3454. nil
  3455. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3456. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3457. (((class color) (min-colors 8)) (:foreground "green"))
  3458. (t (:bold t :italic t))))
  3459. "Face for items scheduled for a certain day."
  3460. :group 'org-faces)
  3461. (defface org-scheduled-previously
  3462. (org-compatible-face
  3463. nil
  3464. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3465. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3466. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3467. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3468. (t (:bold t))))
  3469. "Face for items scheduled previously, and not yet done."
  3470. :group 'org-faces)
  3471. (defface org-upcoming-deadline
  3472. (org-compatible-face
  3473. nil
  3474. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3475. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3476. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3477. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3478. (t (:bold t))))
  3479. "Face for items scheduled previously, and not yet done."
  3480. :group 'org-faces)
  3481. (defcustom org-agenda-deadline-faces
  3482. '((1.0 . org-warning)
  3483. (0.5 . org-upcoming-deadline)
  3484. (0.0 . default))
  3485. "Faces for showing deadlines in the agenda.
  3486. This is a list of cons cells. The cdr of each cess is a face to be used,
  3487. and it can also just be a like like '(:foreground \"yellow\").
  3488. Each car is a fraction of the head-warning time that must have passed for
  3489. this the face in the cdr to be used for display. The numbers must be
  3490. given in descending order. The head-warning time is normally taken
  3491. from `org-deadline-warning-days', but can also be specified in the deadline
  3492. timestamp itself, like this:
  3493. DEADLINE: <2007-08-13 Mon -8d>
  3494. You may use d for days, w for weeks, m for months and y for years. Months
  3495. and years will only be treated in an approximate fashion (30.4 days for a
  3496. month and 365.24 days for a year)."
  3497. :group 'org-faces
  3498. :group 'org-agenda-daily/weekly
  3499. :type '(repeat
  3500. (cons
  3501. (number :tag "Fraction of head-warning time passed")
  3502. (sexp :tag "Face"))))
  3503. (defface org-time-grid ;; font-lock-variable-name-face
  3504. (org-compatible-face
  3505. nil
  3506. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3507. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3508. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3509. "Face used for time grids."
  3510. :group 'org-faces)
  3511. (defconst org-level-faces
  3512. '(org-level-1 org-level-2 org-level-3 org-level-4
  3513. org-level-5 org-level-6 org-level-7 org-level-8
  3514. ))
  3515. (defcustom org-n-level-faces (length org-level-faces)
  3516. "The number different faces to be used for headlines.
  3517. Org-mode defines 8 different headline faces, so this can be at most 8.
  3518. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3519. :type 'number
  3520. :group 'org-faces)
  3521. ;;; Variables for pre-computed regular expressions, all buffer local
  3522. (defvar org-drawer-regexp nil
  3523. "Matches first line of a hidden block.")
  3524. (make-variable-buffer-local 'org-drawer-regexp)
  3525. (defvar org-todo-regexp nil
  3526. "Matches any of the TODO state keywords.")
  3527. (make-variable-buffer-local 'org-todo-regexp)
  3528. (defvar org-not-done-regexp nil
  3529. "Matches any of the TODO state keywords except the last one.")
  3530. (make-variable-buffer-local 'org-not-done-regexp)
  3531. (defvar org-todo-line-regexp nil
  3532. "Matches a headline and puts TODO state into group 2 if present.")
  3533. (make-variable-buffer-local 'org-todo-line-regexp)
  3534. (defvar org-complex-heading-regexp nil
  3535. "Matches a headline and puts everything into groups:
  3536. group 1: the stars
  3537. group 2: The todo keyword, maybe
  3538. group 3: Priority cookie
  3539. group 4: True headline
  3540. group 5: Tags")
  3541. (make-variable-buffer-local 'org-complex-heading-regexp)
  3542. (defvar org-todo-line-tags-regexp nil
  3543. "Matches a headline and puts TODO state into group 2 if present.
  3544. Also put tags into group 4 if tags are present.")
  3545. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3546. (defvar org-nl-done-regexp nil
  3547. "Matches newline followed by a headline with the DONE keyword.")
  3548. (make-variable-buffer-local 'org-nl-done-regexp)
  3549. (defvar org-looking-at-done-regexp nil
  3550. "Matches the DONE keyword a point.")
  3551. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3552. (defvar org-ds-keyword-length 12
  3553. "Maximum length of the Deadline and SCHEDULED keywords.")
  3554. (make-variable-buffer-local 'org-ds-keyword-length)
  3555. (defvar org-deadline-regexp nil
  3556. "Matches the DEADLINE keyword.")
  3557. (make-variable-buffer-local 'org-deadline-regexp)
  3558. (defvar org-deadline-time-regexp nil
  3559. "Matches the DEADLINE keyword together with a time stamp.")
  3560. (make-variable-buffer-local 'org-deadline-time-regexp)
  3561. (defvar org-deadline-line-regexp nil
  3562. "Matches the DEADLINE keyword and the rest of the line.")
  3563. (make-variable-buffer-local 'org-deadline-line-regexp)
  3564. (defvar org-scheduled-regexp nil
  3565. "Matches the SCHEDULED keyword.")
  3566. (make-variable-buffer-local 'org-scheduled-regexp)
  3567. (defvar org-scheduled-time-regexp nil
  3568. "Matches the SCHEDULED keyword together with a time stamp.")
  3569. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3570. (defvar org-closed-time-regexp nil
  3571. "Matches the CLOSED keyword together with a time stamp.")
  3572. (make-variable-buffer-local 'org-closed-time-regexp)
  3573. (defvar org-keyword-time-regexp nil
  3574. "Matches any of the 4 keywords, together with the time stamp.")
  3575. (make-variable-buffer-local 'org-keyword-time-regexp)
  3576. (defvar org-keyword-time-not-clock-regexp nil
  3577. "Matches any of the 3 keywords, together with the time stamp.")
  3578. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3579. (defvar org-maybe-keyword-time-regexp nil
  3580. "Matches a timestamp, possibly preceeded by a keyword.")
  3581. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3582. (defvar org-planning-or-clock-line-re nil
  3583. "Matches a line with planning or clock info.")
  3584. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3585. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3586. rear-nonsticky t mouse-map t fontified t)
  3587. "Properties to remove when a string without properties is wanted.")
  3588. (defsubst org-match-string-no-properties (num &optional string)
  3589. (if (featurep 'xemacs)
  3590. (let ((s (match-string num string)))
  3591. (remove-text-properties 0 (length s) org-rm-props s)
  3592. s)
  3593. (match-string-no-properties num string)))
  3594. (defsubst org-no-properties (s)
  3595. (if (fboundp 'set-text-properties)
  3596. (set-text-properties 0 (length s) nil s)
  3597. (remove-text-properties 0 (length s) org-rm-props s))
  3598. s)
  3599. (defsubst org-get-alist-option (option key)
  3600. (cond ((eq key t) t)
  3601. ((eq option t) t)
  3602. ((assoc key option) (cdr (assoc key option)))
  3603. (t (cdr (assq 'default option)))))
  3604. (defsubst org-inhibit-invisibility ()
  3605. "Modified `buffer-invisibility-spec' for Emacs 21.
  3606. Some ops with invisible text do not work correctly on Emacs 21. For these
  3607. we turn off invisibility temporarily. Use this in a `let' form."
  3608. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3609. (defsubst org-set-local (var value)
  3610. "Make VAR local in current buffer and set it to VALUE."
  3611. (set (make-variable-buffer-local var) value))
  3612. (defsubst org-mode-p ()
  3613. "Check if the current buffer is in Org-mode."
  3614. (eq major-mode 'org-mode))
  3615. (defsubst org-last (list)
  3616. "Return the last element of LIST."
  3617. (car (last list)))
  3618. (defun org-let (list &rest body)
  3619. (eval (cons 'let (cons list body))))
  3620. (put 'org-let 'lisp-indent-function 1)
  3621. (defun org-let2 (list1 list2 &rest body)
  3622. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3623. (put 'org-let2 'lisp-indent-function 2)
  3624. (defconst org-startup-options
  3625. '(("fold" org-startup-folded t)
  3626. ("overview" org-startup-folded t)
  3627. ("nofold" org-startup-folded nil)
  3628. ("showall" org-startup-folded nil)
  3629. ("content" org-startup-folded content)
  3630. ("hidestars" org-hide-leading-stars t)
  3631. ("showstars" org-hide-leading-stars nil)
  3632. ("odd" org-odd-levels-only t)
  3633. ("oddeven" org-odd-levels-only nil)
  3634. ("align" org-startup-align-all-tables t)
  3635. ("noalign" org-startup-align-all-tables nil)
  3636. ("customtime" org-display-custom-times t)
  3637. ("logging" org-log-done t)
  3638. ("logdone" org-log-done t)
  3639. ("nologging" org-log-done nil)
  3640. ("lognotedone" org-log-done done push)
  3641. ("lognotestate" org-log-done state push)
  3642. ("lognoteclock-out" org-log-done clock-out push)
  3643. ("logrepeat" org-log-repeat t)
  3644. ("nologrepeat" org-log-repeat nil)
  3645. ("constcgs" constants-unit-system cgs)
  3646. ("constSI" constants-unit-system SI))
  3647. "Variable associated with STARTUP options for org-mode.
  3648. Each element is a list of three items: The startup options as written
  3649. in the #+STARTUP line, the corresponding variable, and the value to
  3650. set this variable to if the option is found. An optional forth element PUSH
  3651. means to push this value onto the list in the variable.")
  3652. (defun org-set-regexps-and-options ()
  3653. "Precompute regular expressions for current buffer."
  3654. (when (org-mode-p)
  3655. (org-set-local 'org-todo-kwd-alist nil)
  3656. (org-set-local 'org-todo-key-alist nil)
  3657. (org-set-local 'org-todo-key-trigger nil)
  3658. (org-set-local 'org-todo-keywords-1 nil)
  3659. (org-set-local 'org-done-keywords nil)
  3660. (org-set-local 'org-todo-heads nil)
  3661. (org-set-local 'org-todo-sets nil)
  3662. (org-set-local 'org-todo-log-states nil)
  3663. (let ((re (org-make-options-regexp
  3664. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3665. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3666. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3667. (splitre "[ \t]+")
  3668. kwds kws0 kwsa key value cat arch tags const links hw dws
  3669. tail sep kws1 prio props drawers
  3670. ex log)
  3671. (save-excursion
  3672. (save-restriction
  3673. (widen)
  3674. (goto-char (point-min))
  3675. (while (re-search-forward re nil t)
  3676. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3677. (cond
  3678. ((equal key "CATEGORY")
  3679. (if (string-match "[ \t]+$" value)
  3680. (setq value (replace-match "" t t value)))
  3681. (setq cat value))
  3682. ((member key '("SEQ_TODO" "TODO"))
  3683. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3684. ((equal key "TYP_TODO")
  3685. (push (cons 'type (org-split-string value splitre)) kwds))
  3686. ((equal key "TAGS")
  3687. (setq tags (append tags (org-split-string value splitre))))
  3688. ((equal key "COLUMNS")
  3689. (org-set-local 'org-columns-default-format value))
  3690. ((equal key "LINK")
  3691. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3692. (push (cons (match-string 1 value)
  3693. (org-trim (match-string 2 value)))
  3694. links)))
  3695. ((equal key "PRIORITIES")
  3696. (setq prio (org-split-string value " +")))
  3697. ((equal key "PROPERTY")
  3698. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3699. (push (cons (match-string 1 value) (match-string 2 value))
  3700. props)))
  3701. ((equal key "DRAWERS")
  3702. (setq drawers (org-split-string value splitre)))
  3703. ((equal key "CONSTANTS")
  3704. (setq const (append const (org-split-string value splitre))))
  3705. ((equal key "STARTUP")
  3706. (let ((opts (org-split-string value splitre))
  3707. l var val)
  3708. (while (setq l (pop opts))
  3709. (when (setq l (assoc l org-startup-options))
  3710. (setq var (nth 1 l) val (nth 2 l))
  3711. (if (not (nth 3 l))
  3712. (set (make-local-variable var) val)
  3713. (if (not (listp (symbol-value var)))
  3714. (set (make-local-variable var) nil))
  3715. (set (make-local-variable var) (symbol-value var))
  3716. (add-to-list var val))))))
  3717. ((equal key "ARCHIVE")
  3718. (string-match " *$" value)
  3719. (setq arch (replace-match "" t t value))
  3720. (remove-text-properties 0 (length arch)
  3721. '(face t fontified t) arch)))
  3722. )))
  3723. (when cat
  3724. (org-set-local 'org-category (intern cat))
  3725. (push (cons "CATEGORY" cat) props))
  3726. (when prio
  3727. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3728. (setq prio (mapcar 'string-to-char prio))
  3729. (org-set-local 'org-highest-priority (nth 0 prio))
  3730. (org-set-local 'org-lowest-priority (nth 1 prio))
  3731. (org-set-local 'org-default-priority (nth 2 prio)))
  3732. (and props (org-set-local 'org-local-properties (nreverse props)))
  3733. (and drawers (org-set-local 'org-drawers drawers))
  3734. (and arch (org-set-local 'org-archive-location arch))
  3735. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3736. ;; Process the TODO keywords
  3737. (unless kwds
  3738. ;; Use the global values as if they had been given locally.
  3739. (setq kwds (default-value 'org-todo-keywords))
  3740. (if (stringp (car kwds))
  3741. (setq kwds (list (cons org-todo-interpretation
  3742. (default-value 'org-todo-keywords)))))
  3743. (setq kwds (reverse kwds)))
  3744. (setq kwds (nreverse kwds))
  3745. (let (inter kws kw)
  3746. (while (setq kws (pop kwds))
  3747. (setq inter (pop kws) sep (member "|" kws)
  3748. kws0 (delete "|" (copy-sequence kws))
  3749. kwsa nil
  3750. kws1 (mapcar
  3751. (lambda (x)
  3752. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3753. (progn
  3754. (setq kw (match-string 1 x)
  3755. ex (and (match-end 2) (match-string 2 x))
  3756. log (and ex (string-match "@" ex))
  3757. key (and ex (substring ex 0 1)))
  3758. (if (equal key "@") (setq key nil))
  3759. (push (cons kw (and key (string-to-char key))) kwsa)
  3760. (and log (push kw org-todo-log-states))
  3761. kw)
  3762. (error "Invalid TODO keyword %s" x)))
  3763. kws0)
  3764. kwsa (if kwsa (append '((:startgroup))
  3765. (nreverse kwsa)
  3766. '((:endgroup))))
  3767. hw (car kws1)
  3768. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3769. tail (list inter hw (car dws) (org-last dws)))
  3770. (add-to-list 'org-todo-heads hw 'append)
  3771. ;????????????????? (setq org-todo-heads (append org-todo-heads (list hw)))
  3772. (add-to-list 'org-todo-heads hw 'append)
  3773. (push kws1 org-todo-sets)
  3774. (setq org-done-keywords (append org-done-keywords dws nil))
  3775. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3776. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3777. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3778. (setq org-todo-sets (nreverse org-todo-sets)
  3779. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3780. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3781. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3782. ;; Process the constants
  3783. (when const
  3784. (let (e cst)
  3785. (while (setq e (pop const))
  3786. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3787. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3788. (setq org-table-formula-constants-local cst)))
  3789. ;; Process the tags.
  3790. (when tags
  3791. (let (e tgs)
  3792. (while (setq e (pop tags))
  3793. (cond
  3794. ((equal e "{") (push '(:startgroup) tgs))
  3795. ((equal e "}") (push '(:endgroup) tgs))
  3796. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3797. (push (cons (match-string 1 e)
  3798. (string-to-char (match-string 2 e)))
  3799. tgs))
  3800. (t (push (list e) tgs))))
  3801. (org-set-local 'org-tag-alist nil)
  3802. (while (setq e (pop tgs))
  3803. (or (and (stringp (car e))
  3804. (assoc (car e) org-tag-alist))
  3805. (push e org-tag-alist))))))
  3806. ;; Compute the regular expressions and other local variables
  3807. (if (not org-done-keywords)
  3808. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3809. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3810. (length org-scheduled-string)))
  3811. org-drawer-regexp
  3812. (concat "^[ \t]*:\\("
  3813. (mapconcat 'regexp-quote org-drawers "\\|")
  3814. "\\):[ \t]*$")
  3815. org-not-done-keywords
  3816. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3817. org-todo-regexp
  3818. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3819. "\\|") "\\)\\>")
  3820. org-not-done-regexp
  3821. (concat "\\<\\("
  3822. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3823. "\\)\\>")
  3824. org-todo-line-regexp
  3825. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3826. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3827. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3828. org-complex-heading-regexp
  3829. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  3830. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3831. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3832. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3833. org-nl-done-regexp
  3834. (concat "\n\\*+[ \t]+"
  3835. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3836. "\\)" "\\>")
  3837. org-todo-line-tags-regexp
  3838. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3839. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3840. (org-re
  3841. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3842. org-looking-at-done-regexp
  3843. (concat "^" "\\(?:"
  3844. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3845. "\\>")
  3846. org-deadline-regexp (concat "\\<" org-deadline-string)
  3847. org-deadline-time-regexp
  3848. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3849. org-deadline-line-regexp
  3850. (concat "\\<\\(" org-deadline-string "\\).*")
  3851. org-scheduled-regexp
  3852. (concat "\\<" org-scheduled-string)
  3853. org-scheduled-time-regexp
  3854. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3855. org-closed-time-regexp
  3856. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3857. org-keyword-time-regexp
  3858. (concat "\\<\\(" org-scheduled-string
  3859. "\\|" org-deadline-string
  3860. "\\|" org-closed-string
  3861. "\\|" org-clock-string "\\)"
  3862. " *[[<]\\([^]>]+\\)[]>]")
  3863. org-keyword-time-not-clock-regexp
  3864. (concat "\\<\\(" org-scheduled-string
  3865. "\\|" org-deadline-string
  3866. "\\|" org-closed-string
  3867. "\\)"
  3868. " *[[<]\\([^]>]+\\)[]>]")
  3869. org-maybe-keyword-time-regexp
  3870. (concat "\\(\\<\\(" org-scheduled-string
  3871. "\\|" org-deadline-string
  3872. "\\|" org-closed-string
  3873. "\\|" org-clock-string "\\)\\)?"
  3874. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3875. org-planning-or-clock-line-re
  3876. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3877. "\\|" org-deadline-string
  3878. "\\|" org-closed-string "\\|" org-clock-string
  3879. "\\)\\>\\)")
  3880. )
  3881. (org-compute-latex-and-specials-regexp)
  3882. (org-set-font-lock-defaults)))
  3883. (defun org-remove-keyword-keys (list)
  3884. (mapcar (lambda (x)
  3885. (if (string-match "(..?)$" x)
  3886. (substring x 0 (match-beginning 0))
  3887. x))
  3888. list))
  3889. ;;; Some variables ujsed in various places
  3890. (defvar org-window-configuration nil
  3891. "Used in various places to store a window configuration.")
  3892. (defvar org-finish-function nil
  3893. "Function to be called when `C-c C-c' is used.
  3894. This is for getting out of special buffers like remember.")
  3895. ;;; Foreign variables, to inform the compiler
  3896. ;; XEmacs only
  3897. (defvar outline-mode-menu-heading)
  3898. (defvar outline-mode-menu-show)
  3899. (defvar outline-mode-menu-hide)
  3900. (defvar zmacs-regions) ; XEmacs regions
  3901. ;; Emacs only
  3902. (defvar mark-active)
  3903. ;; Packages that org-mode interacts with
  3904. (defvar calc-embedded-close-formula)
  3905. (defvar calc-embedded-open-formula)
  3906. (defvar font-lock-unfontify-region-function)
  3907. (defvar org-goto-start-pos)
  3908. (defvar vm-message-pointer)
  3909. (defvar vm-folder-directory)
  3910. (defvar wl-summary-buffer-elmo-folder)
  3911. (defvar wl-summary-buffer-folder-name)
  3912. (defvar gnus-other-frame-object)
  3913. (defvar gnus-group-name)
  3914. (defvar gnus-article-current)
  3915. (defvar w3m-current-url)
  3916. (defvar w3m-current-title)
  3917. (defvar mh-progs)
  3918. (defvar mh-current-folder)
  3919. (defvar mh-show-folder-buffer)
  3920. (defvar mh-index-folder)
  3921. (defvar mh-searcher)
  3922. (defvar calendar-mode-map)
  3923. (defvar Info-current-file)
  3924. (defvar Info-current-node)
  3925. (defvar texmathp-why)
  3926. (defvar remember-save-after-remembering)
  3927. (defvar remember-data-file)
  3928. (defvar remember-register)
  3929. (defvar remember-buffer)
  3930. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  3931. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  3932. (defvar org-latex-regexps)
  3933. (defvar constants-unit-system)
  3934. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3935. ;; FIXME: Occasionally check by commenting these, to make sure
  3936. ;; no other functions uses these, forgetting to let-bind them.
  3937. (defvar entry)
  3938. (defvar state)
  3939. (defvar last-state)
  3940. (defvar date)
  3941. (defvar description)
  3942. ;; Defined somewhere in this file, but used before definition.
  3943. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  3944. (defvar org-agenda-buffer-name)
  3945. (defvar org-agenda-undo-list)
  3946. (defvar org-agenda-pending-undo-list)
  3947. (defvar org-agenda-overriding-header)
  3948. (defvar orgtbl-mode)
  3949. (defvar org-html-entities)
  3950. (defvar org-struct-menu)
  3951. (defvar org-org-menu)
  3952. (defvar org-tbl-menu)
  3953. (defvar org-agenda-keymap)
  3954. ;;;; Emacs/XEmacs compatibility
  3955. ;; Overlay compatibility functions
  3956. (defun org-make-overlay (beg end &optional buffer)
  3957. (if (featurep 'xemacs)
  3958. (make-extent beg end buffer)
  3959. (make-overlay beg end buffer)))
  3960. (defun org-delete-overlay (ovl)
  3961. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3962. (defun org-detach-overlay (ovl)
  3963. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3964. (defun org-move-overlay (ovl beg end &optional buffer)
  3965. (if (featurep 'xemacs)
  3966. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  3967. (move-overlay ovl beg end buffer)))
  3968. (defun org-overlay-put (ovl prop value)
  3969. (if (featurep 'xemacs)
  3970. (set-extent-property ovl prop value)
  3971. (overlay-put ovl prop value)))
  3972. (defun org-overlay-display (ovl text &optional face evap)
  3973. "Make overlay OVL display TEXT with face FACE."
  3974. (if (featurep 'xemacs)
  3975. (let ((gl (make-glyph text)))
  3976. (and face (set-glyph-face gl face))
  3977. (set-extent-property ovl 'invisible t)
  3978. (set-extent-property ovl 'end-glyph gl))
  3979. (overlay-put ovl 'display text)
  3980. (if face (overlay-put ovl 'face face))
  3981. (if evap (overlay-put ovl 'evaporate t))))
  3982. (defun org-overlay-before-string (ovl text &optional face evap)
  3983. "Make overlay OVL display TEXT with face FACE."
  3984. (if (featurep 'xemacs)
  3985. (let ((gl (make-glyph text)))
  3986. (and face (set-glyph-face gl face))
  3987. (set-extent-property ovl 'begin-glyph gl))
  3988. (if face (org-add-props text nil 'face face))
  3989. (overlay-put ovl 'before-string text)
  3990. (if evap (overlay-put ovl 'evaporate t))))
  3991. (defun org-overlay-get (ovl prop)
  3992. (if (featurep 'xemacs)
  3993. (extent-property ovl prop)
  3994. (overlay-get ovl prop)))
  3995. (defun org-overlays-at (pos)
  3996. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  3997. (defun org-overlays-in (&optional start end)
  3998. (if (featurep 'xemacs)
  3999. (extent-list nil start end)
  4000. (overlays-in start end)))
  4001. (defun org-overlay-start (o)
  4002. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4003. (defun org-overlay-end (o)
  4004. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4005. (defun org-find-overlays (prop &optional pos delete)
  4006. "Find all overlays specifying PROP at POS or point.
  4007. If DELETE is non-nil, delete all those overlays."
  4008. (let ((overlays (org-overlays-at (or pos (point))))
  4009. ov found)
  4010. (while (setq ov (pop overlays))
  4011. (if (org-overlay-get ov prop)
  4012. (if delete (org-delete-overlay ov) (push ov found))))
  4013. found))
  4014. ;; Region compatibility
  4015. (defun org-add-hook (hook function &optional append local)
  4016. "Add-hook, compatible with both Emacsen."
  4017. (if (and local (featurep 'xemacs))
  4018. (add-local-hook hook function append)
  4019. (add-hook hook function append local)))
  4020. (defvar org-ignore-region nil
  4021. "To temporarily disable the active region.")
  4022. (defun org-region-active-p ()
  4023. "Is `transient-mark-mode' on and the region active?
  4024. Works on both Emacs and XEmacs."
  4025. (if org-ignore-region
  4026. nil
  4027. (if (featurep 'xemacs)
  4028. (and zmacs-regions (region-active-p))
  4029. (and transient-mark-mode mark-active))))
  4030. ;; Invisibility compatibility
  4031. (defun org-add-to-invisibility-spec (arg)
  4032. "Add elements to `buffer-invisibility-spec'.
  4033. See documentation for `buffer-invisibility-spec' for the kind of elements
  4034. that can be added."
  4035. (cond
  4036. ((fboundp 'add-to-invisibility-spec)
  4037. (add-to-invisibility-spec arg))
  4038. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4039. (setq buffer-invisibility-spec (list arg)))
  4040. (t
  4041. (setq buffer-invisibility-spec
  4042. (cons arg buffer-invisibility-spec)))))
  4043. (defun org-remove-from-invisibility-spec (arg)
  4044. "Remove elements from `buffer-invisibility-spec'."
  4045. (if (fboundp 'remove-from-invisibility-spec)
  4046. (remove-from-invisibility-spec arg)
  4047. (if (consp buffer-invisibility-spec)
  4048. (setq buffer-invisibility-spec
  4049. (delete arg buffer-invisibility-spec)))))
  4050. (defun org-in-invisibility-spec-p (arg)
  4051. "Is ARG a member of `buffer-invisibility-spec'?"
  4052. (if (consp buffer-invisibility-spec)
  4053. (member arg buffer-invisibility-spec)
  4054. nil))
  4055. ;;;; Define the Org-mode
  4056. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4057. (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."))
  4058. ;; We use a before-change function to check if a table might need
  4059. ;; an update.
  4060. (defvar org-table-may-need-update t
  4061. "Indicates that a table might need an update.
  4062. This variable is set by `org-before-change-function'.
  4063. `org-table-align' sets it back to nil.")
  4064. (defvar org-mode-map)
  4065. (defvar org-mode-hook nil)
  4066. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4067. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4068. (defvar org-table-buffer-is-an nil)
  4069. (defconst org-outline-regexp "\\*+ ")
  4070. ;;;###autoload
  4071. (define-derived-mode org-mode outline-mode "Org"
  4072. "Outline-based notes management and organizer, alias
  4073. \"Carsten's outline-mode for keeping track of everything.\"
  4074. Org-mode develops organizational tasks around a NOTES file which
  4075. contains information about projects as plain text. Org-mode is
  4076. implemented on top of outline-mode, which is ideal to keep the content
  4077. of large files well structured. It supports ToDo items, deadlines and
  4078. time stamps, which magically appear in the diary listing of the Emacs
  4079. calendar. Tables are easily created with a built-in table editor.
  4080. Plain text URL-like links connect to websites, emails (VM), Usenet
  4081. messages (Gnus), BBDB entries, and any files related to the project.
  4082. For printing and sharing of notes, an Org-mode file (or a part of it)
  4083. can be exported as a structured ASCII or HTML file.
  4084. The following commands are available:
  4085. \\{org-mode-map}"
  4086. ;; Get rid of Outline menus, they are not needed
  4087. ;; Need to do this here because define-derived-mode sets up
  4088. ;; the keymap so late. Still, it is a waste to call this each time
  4089. ;; we switch another buffer into org-mode.
  4090. (if (featurep 'xemacs)
  4091. (when (boundp 'outline-mode-menu-heading)
  4092. ;; Assume this is Greg's port, it used easymenu
  4093. (easy-menu-remove outline-mode-menu-heading)
  4094. (easy-menu-remove outline-mode-menu-show)
  4095. (easy-menu-remove outline-mode-menu-hide))
  4096. (define-key org-mode-map [menu-bar headings] 'undefined)
  4097. (define-key org-mode-map [menu-bar hide] 'undefined)
  4098. (define-key org-mode-map [menu-bar show] 'undefined))
  4099. (easy-menu-add org-org-menu)
  4100. (easy-menu-add org-tbl-menu)
  4101. (org-install-agenda-files-menu)
  4102. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4103. (org-add-to-invisibility-spec '(org-cwidth))
  4104. (when (featurep 'xemacs)
  4105. (org-set-local 'line-move-ignore-invisible t))
  4106. (org-set-local 'outline-regexp org-outline-regexp)
  4107. (org-set-local 'outline-level 'org-outline-level)
  4108. (when (and org-ellipsis
  4109. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4110. (fboundp 'make-glyph-code))
  4111. (unless org-display-table
  4112. (setq org-display-table (make-display-table)))
  4113. (set-display-table-slot
  4114. org-display-table 4
  4115. (vconcat (mapcar
  4116. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4117. org-ellipsis)))
  4118. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4119. (setq buffer-display-table org-display-table))
  4120. (org-set-regexps-and-options)
  4121. ;; Calc embedded
  4122. (org-set-local 'calc-embedded-open-mode "# ")
  4123. (modify-syntax-entry ?# "<")
  4124. (modify-syntax-entry ?@ "w")
  4125. (if org-startup-truncated (setq truncate-lines t))
  4126. (org-set-local 'font-lock-unfontify-region-function
  4127. 'org-unfontify-region)
  4128. ;; Activate before-change-function
  4129. (org-set-local 'org-table-may-need-update t)
  4130. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4131. 'local)
  4132. ;; Check for running clock before killing a buffer
  4133. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4134. ;; Paragraphs and auto-filling
  4135. (org-set-autofill-regexps)
  4136. (setq indent-line-function 'org-indent-line-function)
  4137. (org-update-radio-target-regexp)
  4138. ;; Comment characters
  4139. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4140. (org-set-local 'comment-padding " ")
  4141. ;; Make isearch reveal context
  4142. (if (or (featurep 'xemacs)
  4143. (not (boundp 'outline-isearch-open-invisible-function)))
  4144. ;; Emacs 21 and XEmacs make use of the hook
  4145. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4146. ;; Emacs 22 deals with this through a special variable
  4147. (org-set-local 'outline-isearch-open-invisible-function
  4148. (lambda (&rest ignore) (org-show-context 'isearch))))
  4149. ;; If empty file that did not turn on org-mode automatically, make it to.
  4150. (if (and org-insert-mode-line-in-empty-file
  4151. (interactive-p)
  4152. (= (point-min) (point-max)))
  4153. (insert "# -*- mode: org -*-\n\n"))
  4154. (unless org-inhibit-startup
  4155. (when org-startup-align-all-tables
  4156. (let ((bmp (buffer-modified-p)))
  4157. (org-table-map-tables 'org-table-align)
  4158. (set-buffer-modified-p bmp)))
  4159. (org-cycle-hide-drawers 'all)
  4160. (cond
  4161. ((eq org-startup-folded t)
  4162. (org-cycle '(4)))
  4163. ((eq org-startup-folded 'content)
  4164. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4165. (org-cycle '(4)) (org-cycle '(4)))))))
  4166. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4167. (defsubst org-call-with-arg (command arg)
  4168. "Call COMMAND interactively, but pretend prefix are was ARG."
  4169. (let ((current-prefix-arg arg)) (call-interactively command)))
  4170. (defsubst org-current-line (&optional pos)
  4171. (save-excursion
  4172. (and pos (goto-char pos))
  4173. ;; works also in narrowed buffer, because we start at 1, not point-min
  4174. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4175. (defun org-current-time ()
  4176. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4177. (if (> org-time-stamp-rounding-minutes 0)
  4178. (let ((r org-time-stamp-rounding-minutes)
  4179. (time (decode-time)))
  4180. (apply 'encode-time
  4181. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4182. (nthcdr 2 time))))
  4183. (current-time)))
  4184. (defun org-add-props (string plist &rest props)
  4185. "Add text properties to entire string, from beginning to end.
  4186. PLIST may be a list of properties, PROPS are individual properties and values
  4187. that will be added to PLIST. Returns the string that was modified."
  4188. (add-text-properties
  4189. 0 (length string) (if props (append plist props) plist) string)
  4190. string)
  4191. (put 'org-add-props 'lisp-indent-function 2)
  4192. ;;;; Font-Lock stuff, including the activators
  4193. (defvar org-mouse-map (make-sparse-keymap))
  4194. (org-defkey org-mouse-map
  4195. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4196. (org-defkey org-mouse-map
  4197. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4198. (when org-mouse-1-follows-link
  4199. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4200. (when org-tab-follows-link
  4201. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4202. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4203. (when org-return-follows-link
  4204. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4205. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4206. (require 'font-lock)
  4207. (defconst org-non-link-chars "]\t\n\r<>")
  4208. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4209. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  4210. (defvar org-link-re-with-space nil
  4211. "Matches a link with spaces, optional angular brackets around it.")
  4212. (defvar org-link-re-with-space2 nil
  4213. "Matches a link with spaces, optional angular brackets around it.")
  4214. (defvar org-angle-link-re nil
  4215. "Matches link with angular brackets, spaces are allowed.")
  4216. (defvar org-plain-link-re nil
  4217. "Matches plain link, without spaces.")
  4218. (defvar org-bracket-link-regexp nil
  4219. "Matches a link in double brackets.")
  4220. (defvar org-bracket-link-analytic-regexp nil
  4221. "Regular expression used to analyze links.
  4222. Here is what the match groups contain after a match:
  4223. 1: http:
  4224. 2: http
  4225. 3: path
  4226. 4: [desc]
  4227. 5: desc")
  4228. (defvar org-any-link-re nil
  4229. "Regular expression matching any link.")
  4230. (defun org-make-link-regexps ()
  4231. "Update the link regular expressions.
  4232. This should be called after the variable `org-link-types' has changed."
  4233. (setq org-link-re-with-space
  4234. (concat
  4235. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4236. "\\([^" org-non-link-chars " ]"
  4237. "[^" org-non-link-chars "]*"
  4238. "[^" org-non-link-chars " ]\\)>?")
  4239. org-link-re-with-space2
  4240. (concat
  4241. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4242. "\\([^" org-non-link-chars " ]"
  4243. "[^]\t\n\r]*"
  4244. "[^" org-non-link-chars " ]\\)>?")
  4245. org-angle-link-re
  4246. (concat
  4247. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4248. "\\([^" org-non-link-chars " ]"
  4249. "[^" org-non-link-chars "]*"
  4250. "\\)>")
  4251. org-plain-link-re
  4252. (concat
  4253. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4254. "\\([^]\t\n\r<>,;() ]+\\)")
  4255. org-bracket-link-regexp
  4256. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4257. org-bracket-link-analytic-regexp
  4258. (concat
  4259. "\\[\\["
  4260. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4261. "\\([^]]+\\)"
  4262. "\\]"
  4263. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4264. "\\]")
  4265. org-any-link-re
  4266. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4267. org-angle-link-re "\\)\\|\\("
  4268. org-plain-link-re "\\)")))
  4269. (org-make-link-regexps)
  4270. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4271. "Regular expression for fast time stamp matching.")
  4272. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4273. "Regular expression for fast time stamp matching.")
  4274. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4275. "Regular expression matching time strings for analysis.
  4276. This one does not require the space after the date.")
  4277. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4278. "Regular expression matching time strings for analysis.")
  4279. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4280. "Regular expression matching time stamps, with groups.")
  4281. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4282. "Regular expression matching time stamps (also [..]), with groups.")
  4283. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4284. "Regular expression matching a time stamp range.")
  4285. (defconst org-tr-regexp-both
  4286. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4287. "Regular expression matching a time stamp range.")
  4288. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4289. org-ts-regexp "\\)?")
  4290. "Regular expression matching a time stamp or time stamp range.")
  4291. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4292. org-ts-regexp-both "\\)?")
  4293. "Regular expression matching a time stamp or time stamp range.
  4294. The time stamps may be either active or inactive.")
  4295. (defvar org-emph-face nil)
  4296. (defun org-do-emphasis-faces (limit)
  4297. "Run through the buffer and add overlays to links."
  4298. (let (rtn)
  4299. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4300. (if (not (= (char-after (match-beginning 3))
  4301. (char-after (match-beginning 4))))
  4302. (progn
  4303. (setq rtn t)
  4304. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4305. 'face
  4306. (nth 1 (assoc (match-string 3)
  4307. org-emphasis-alist)))
  4308. (add-text-properties (match-beginning 2) (match-end 2)
  4309. '(font-lock-multiline t))
  4310. (when org-hide-emphasis-markers
  4311. (add-text-properties (match-end 4) (match-beginning 5)
  4312. '(invisible org-link))
  4313. (add-text-properties (match-beginning 3) (match-end 3)
  4314. '(invisible org-link)))
  4315. (backward-char 1))))
  4316. rtn))
  4317. (defun org-emphasize (&optional char)
  4318. "Insert or change an emphasis, i.e. a font like bold or italic.
  4319. If there is an active region, change that region to a new emphasis.
  4320. If there is no region, just insert the marker characters and position
  4321. the cursor between them.
  4322. CHAR should be either the marker character, or the first character of the
  4323. HTML tag associated with that emphasis. If CHAR is a space, the means
  4324. to remove the emphasis of the selected region.
  4325. If char is not given (for example in an interactive call) it
  4326. will be prompted for."
  4327. (interactive)
  4328. (let ((eal org-emphasis-alist) e det
  4329. (erc org-emphasis-regexp-components)
  4330. (prompt "")
  4331. (string "") beg end move tag c s)
  4332. (if (org-region-active-p)
  4333. (setq beg (region-beginning) end (region-end)
  4334. string (buffer-substring beg end))
  4335. (setq move t))
  4336. (while (setq e (pop eal))
  4337. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4338. c (aref tag 0))
  4339. (push (cons c (string-to-char (car e))) det)
  4340. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4341. (substring tag 1)))))
  4342. (unless char
  4343. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4344. (setq char (read-char-exclusive)))
  4345. (setq char (or (cdr (assoc char det)) char))
  4346. (if (equal char ?\ )
  4347. (setq s "" move nil)
  4348. (unless (assoc (char-to-string char) org-emphasis-alist)
  4349. (error "No such emphasis marker: \"%c\"" char))
  4350. (setq s (char-to-string char)))
  4351. (while (and (> (length string) 1)
  4352. (equal (substring string 0 1) (substring string -1))
  4353. (assoc (substring string 0 1) org-emphasis-alist))
  4354. (setq string (substring string 1 -1)))
  4355. (setq string (concat s string s))
  4356. (if beg (delete-region beg end))
  4357. (unless (or (bolp)
  4358. (string-match (concat "[" (nth 0 erc) "\n]")
  4359. (char-to-string (char-before (point)))))
  4360. (insert " "))
  4361. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4362. (char-to-string (char-after (point))))
  4363. (insert " ") (backward-char 1))
  4364. (insert string)
  4365. (and move (backward-char 1))))
  4366. (defconst org-nonsticky-props
  4367. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4368. (defun org-activate-plain-links (limit)
  4369. "Run through the buffer and add overlays to links."
  4370. (catch 'exit
  4371. (let (f)
  4372. (while (re-search-forward org-plain-link-re limit t)
  4373. (setq f (get-text-property (match-beginning 0) 'face))
  4374. (if (or (eq f 'org-tag)
  4375. (and (listp f) (memq 'org-tag f)))
  4376. nil
  4377. (add-text-properties (match-beginning 0) (match-end 0)
  4378. (list 'mouse-face 'highlight
  4379. 'rear-nonsticky org-nonsticky-props
  4380. 'keymap org-mouse-map
  4381. ))
  4382. (throw 'exit t))))))
  4383. (defun org-activate-code (limit)
  4384. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4385. (unless (get-text-property (match-beginning 1) 'face)
  4386. (remove-text-properties (match-beginning 0) (match-end 0)
  4387. '(display t invisible t intangible t))
  4388. t)))
  4389. (defun org-activate-angle-links (limit)
  4390. "Run through the buffer and add overlays to links."
  4391. (if (re-search-forward org-angle-link-re limit t)
  4392. (progn
  4393. (add-text-properties (match-beginning 0) (match-end 0)
  4394. (list 'mouse-face 'highlight
  4395. 'rear-nonsticky org-nonsticky-props
  4396. 'keymap org-mouse-map
  4397. ))
  4398. t)))
  4399. (defmacro org-maybe-intangible (props)
  4400. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4401. In emacs 21, invisible text is not avoided by the command loop, so the
  4402. intangible property is needed to make sure point skips this text.
  4403. In Emacs 22, this is not necessary. The intangible text property has
  4404. led to problems with flyspell. These problems are fixed in flyspell.el,
  4405. but we still avoid setting the property in Emacs 22 and later.
  4406. We use a macro so that the test can happen at compilation time."
  4407. (if (< emacs-major-version 22)
  4408. `(append '(intangible t) ,props)
  4409. props))
  4410. (defun org-activate-bracket-links (limit)
  4411. "Run through the buffer and add overlays to bracketed links."
  4412. (if (re-search-forward org-bracket-link-regexp limit t)
  4413. (let* ((help (concat "LINK: "
  4414. (org-match-string-no-properties 1)))
  4415. ;; FIXME: above we should remove the escapes.
  4416. ;; but that requires another match, protecting match data,
  4417. ;; a lot of overhead for font-lock.
  4418. (ip (org-maybe-intangible
  4419. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4420. 'keymap org-mouse-map 'mouse-face 'highlight
  4421. 'font-lock-multiline t 'help-echo help)))
  4422. (vp (list 'rear-nonsticky org-nonsticky-props
  4423. 'keymap org-mouse-map 'mouse-face 'highlight
  4424. ' font-lock-multiline t 'help-echo help)))
  4425. ;; We need to remove the invisible property here. Table narrowing
  4426. ;; may have made some of this invisible.
  4427. (remove-text-properties (match-beginning 0) (match-end 0)
  4428. '(invisible nil))
  4429. (if (match-end 3)
  4430. (progn
  4431. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4432. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4433. (add-text-properties (match-end 3) (match-end 0) ip))
  4434. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4435. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4436. (add-text-properties (match-end 1) (match-end 0) ip))
  4437. t)))
  4438. (defun org-activate-dates (limit)
  4439. "Run through the buffer and add overlays to dates."
  4440. (if (re-search-forward org-tsr-regexp-both limit t)
  4441. (progn
  4442. (add-text-properties (match-beginning 0) (match-end 0)
  4443. (list 'mouse-face 'highlight
  4444. 'rear-nonsticky org-nonsticky-props
  4445. 'keymap org-mouse-map))
  4446. (when org-display-custom-times
  4447. (if (match-end 3)
  4448. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4449. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4450. t)))
  4451. (defvar org-target-link-regexp nil
  4452. "Regular expression matching radio targets in plain text.")
  4453. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4454. "Regular expression matching a link target.")
  4455. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4456. "Regular expression matching a radio target.")
  4457. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4458. "Regular expression matching any target.")
  4459. (defun org-activate-target-links (limit)
  4460. "Run through the buffer and add overlays to target matches."
  4461. (when org-target-link-regexp
  4462. (let ((case-fold-search t))
  4463. (if (re-search-forward org-target-link-regexp limit t)
  4464. (progn
  4465. (add-text-properties (match-beginning 0) (match-end 0)
  4466. (list 'mouse-face 'highlight
  4467. 'rear-nonsticky org-nonsticky-props
  4468. 'keymap org-mouse-map
  4469. 'help-echo "Radio target link"
  4470. 'org-linked-text t))
  4471. t)))))
  4472. (defun org-update-radio-target-regexp ()
  4473. "Find all radio targets in this file and update the regular expression."
  4474. (interactive)
  4475. (when (memq 'radio org-activate-links)
  4476. (setq org-target-link-regexp
  4477. (org-make-target-link-regexp (org-all-targets 'radio)))
  4478. (org-restart-font-lock)))
  4479. (defun org-hide-wide-columns (limit)
  4480. (let (s e)
  4481. (setq s (text-property-any (point) (or limit (point-max))
  4482. 'org-cwidth t))
  4483. (when s
  4484. (setq e (next-single-property-change s 'org-cwidth))
  4485. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4486. (goto-char e)
  4487. t)))
  4488. (defun org-restart-font-lock ()
  4489. "Restart font-lock-mode, to force refontification."
  4490. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4491. (font-lock-mode -1)
  4492. (font-lock-mode 1)))
  4493. (defun org-all-targets (&optional radio)
  4494. "Return a list of all targets in this file.
  4495. With optional argument RADIO, only find radio targets."
  4496. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4497. rtn)
  4498. (save-excursion
  4499. (goto-char (point-min))
  4500. (while (re-search-forward re nil t)
  4501. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4502. rtn)))
  4503. (defun org-make-target-link-regexp (targets)
  4504. "Make regular expression matching all strings in TARGETS.
  4505. The regular expression finds the targets also if there is a line break
  4506. between words."
  4507. (and targets
  4508. (concat
  4509. "\\<\\("
  4510. (mapconcat
  4511. (lambda (x)
  4512. (while (string-match " +" x)
  4513. (setq x (replace-match "\\s-+" t t x)))
  4514. x)
  4515. targets
  4516. "\\|")
  4517. "\\)\\>")))
  4518. (defun org-activate-tags (limit)
  4519. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4520. (progn
  4521. (add-text-properties (match-beginning 1) (match-end 1)
  4522. (list 'mouse-face 'highlight
  4523. 'rear-nonsticky org-nonsticky-props
  4524. 'keymap org-mouse-map))
  4525. t)))
  4526. (defun org-outline-level ()
  4527. (save-excursion
  4528. (looking-at outline-regexp)
  4529. (if (match-beginning 1)
  4530. (+ (org-get-string-indentation (match-string 1)) 1000)
  4531. (1- (- (match-end 0) (match-beginning 0))))))
  4532. (defvar org-font-lock-keywords nil)
  4533. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4534. "Regular expression matching a property line.")
  4535. (defun org-set-font-lock-defaults ()
  4536. (let* ((em org-fontify-emphasized-text)
  4537. (lk org-activate-links)
  4538. (org-font-lock-extra-keywords
  4539. (list
  4540. ;; Headlines
  4541. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4542. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4543. ;; Table lines
  4544. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4545. (1 'org-table t))
  4546. ;; Table internals
  4547. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4548. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4549. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4550. ;; Drawers
  4551. (list org-drawer-regexp '(0 'org-special-keyword t))
  4552. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4553. ;; Properties
  4554. (list org-property-re
  4555. '(1 'org-special-keyword t)
  4556. '(3 'org-property-value t))
  4557. (if org-format-transports-properties-p
  4558. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4559. ;; Links
  4560. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4561. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4562. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4563. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4564. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4565. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4566. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4567. '(org-hide-wide-columns (0 nil append))
  4568. ;; TODO lines
  4569. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4570. '(1 (org-get-todo-face 1) t))
  4571. ;; DONE
  4572. (if org-fontify-done-headline
  4573. (list (concat "^[*]+ +\\<\\("
  4574. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4575. "\\)\\(.*\\)")
  4576. '(2 'org-headline-done t))
  4577. nil)
  4578. ;; Priorities
  4579. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4580. ;; Special keywords
  4581. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4582. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4583. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4584. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4585. ;; Emphasis
  4586. (if em
  4587. (if (featurep 'xemacs)
  4588. '(org-do-emphasis-faces (0 nil append))
  4589. '(org-do-emphasis-faces)))
  4590. ;; Checkboxes
  4591. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4592. 2 'bold prepend)
  4593. (if org-provide-checkbox-statistics
  4594. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4595. (0 (org-get-checkbox-statistics-face) t)))
  4596. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4597. '(1 'org-archived prepend))
  4598. ;; Specials
  4599. '(org-do-latex-and-special-faces)
  4600. ;; Code
  4601. '(org-activate-code (1 'org-code t))
  4602. ;; COMMENT
  4603. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4604. "\\|" org-quote-string "\\)\\>")
  4605. '(1 'org-special-keyword t))
  4606. '("^#.*" (0 'font-lock-comment-face t))
  4607. )))
  4608. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4609. ;; Now set the full font-lock-keywords
  4610. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4611. (org-set-local 'font-lock-defaults
  4612. '(org-font-lock-keywords t nil nil backward-paragraph))
  4613. (kill-local-variable 'font-lock-keywords) nil))
  4614. (defvar org-m nil)
  4615. (defvar org-l nil)
  4616. (defvar org-f nil)
  4617. (defun org-get-level-face (n)
  4618. "Get the right face for match N in font-lock matching of healdines."
  4619. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4620. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4621. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4622. (cond
  4623. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4624. ((eq n 2) org-f)
  4625. (t (if org-level-color-stars-only nil org-f))))
  4626. (defun org-get-todo-face (kwd)
  4627. "Get the right face for a TODO keyword KWD.
  4628. If KWD is a number, get the corresponding match group."
  4629. (if (numberp kwd) (setq kwd (match-string kwd)))
  4630. (or (cdr (assoc kwd org-todo-keyword-faces))
  4631. (and (member kwd org-done-keywords) 'org-done)
  4632. 'org-todo))
  4633. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4634. "Remove fontification and activation overlays from links."
  4635. (font-lock-default-unfontify-region beg end)
  4636. (let* ((buffer-undo-list t)
  4637. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4638. (inhibit-modification-hooks t)
  4639. deactivate-mark buffer-file-name buffer-file-truename)
  4640. (remove-text-properties beg end
  4641. '(mouse-face t keymap t org-linked-text t
  4642. invisible t intangible t))))
  4643. ;;;; Visibility cycling, including org-goto and indirect buffer
  4644. ;;; Cycling
  4645. (defvar org-cycle-global-status nil)
  4646. (make-variable-buffer-local 'org-cycle-global-status)
  4647. (defvar org-cycle-subtree-status nil)
  4648. (make-variable-buffer-local 'org-cycle-subtree-status)
  4649. ;;;###autoload
  4650. (defun org-cycle (&optional arg)
  4651. "Visibility cycling for Org-mode.
  4652. - When this function is called with a prefix argument, rotate the entire
  4653. buffer through 3 states (global cycling)
  4654. 1. OVERVIEW: Show only top-level headlines.
  4655. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4656. 3. SHOW ALL: Show everything.
  4657. - When point is at the beginning of a headline, rotate the subtree started
  4658. by this line through 3 different states (local cycling)
  4659. 1. FOLDED: Only the main headline is shown.
  4660. 2. CHILDREN: The main headline and the direct children are shown.
  4661. From this state, you can move to one of the children
  4662. and zoom in further.
  4663. 3. SUBTREE: Show the entire subtree, including body text.
  4664. - When there is a numeric prefix, go up to a heading with level ARG, do
  4665. a `show-subtree' and return to the previous cursor position. If ARG
  4666. is negative, go up that many levels.
  4667. - When point is not at the beginning of a headline, execute
  4668. `indent-relative', like TAB normally does. See the option
  4669. `org-cycle-emulate-tab' for details.
  4670. - Special case: if point is at the beginning of the buffer and there is
  4671. no headline in line 1, this function will act as if called with prefix arg.
  4672. But only if also the variable `org-cycle-global-at-bob' is t."
  4673. (interactive "P")
  4674. (let* ((outline-regexp
  4675. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4676. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4677. outline-regexp))
  4678. (bob-special (and org-cycle-global-at-bob (bobp)
  4679. (not (looking-at outline-regexp))))
  4680. (org-cycle-hook
  4681. (if bob-special
  4682. (delq 'org-optimize-window-after-visibility-change
  4683. (copy-sequence org-cycle-hook))
  4684. org-cycle-hook))
  4685. (pos (point)))
  4686. (if (or bob-special (equal arg '(4)))
  4687. ;; special case: use global cycling
  4688. (setq arg t))
  4689. (cond
  4690. ((org-at-table-p 'any)
  4691. ;; Enter the table or move to the next field in the table
  4692. (or (org-table-recognize-table.el)
  4693. (progn
  4694. (if arg (org-table-edit-field t)
  4695. (org-table-justify-field-maybe)
  4696. (call-interactively 'org-table-next-field)))))
  4697. ((eq arg t) ;; Global cycling
  4698. (cond
  4699. ((and (eq last-command this-command)
  4700. (eq org-cycle-global-status 'overview))
  4701. ;; We just created the overview - now do table of contents
  4702. ;; This can be slow in very large buffers, so indicate action
  4703. (message "CONTENTS...")
  4704. (org-content)
  4705. (message "CONTENTS...done")
  4706. (setq org-cycle-global-status 'contents)
  4707. (run-hook-with-args 'org-cycle-hook 'contents))
  4708. ((and (eq last-command this-command)
  4709. (eq org-cycle-global-status 'contents))
  4710. ;; We just showed the table of contents - now show everything
  4711. (show-all)
  4712. (message "SHOW ALL")
  4713. (setq org-cycle-global-status 'all)
  4714. (run-hook-with-args 'org-cycle-hook 'all))
  4715. (t
  4716. ;; Default action: go to overview
  4717. (org-overview)
  4718. (message "OVERVIEW")
  4719. (setq org-cycle-global-status 'overview)
  4720. (run-hook-with-args 'org-cycle-hook 'overview))))
  4721. ((and org-drawers org-drawer-regexp
  4722. (save-excursion
  4723. (beginning-of-line 1)
  4724. (looking-at org-drawer-regexp)))
  4725. ;; Toggle block visibility
  4726. (org-flag-drawer
  4727. (not (get-char-property (match-end 0) 'invisible))))
  4728. ((integerp arg)
  4729. ;; Show-subtree, ARG levels up from here.
  4730. (save-excursion
  4731. (org-back-to-heading)
  4732. (outline-up-heading (if (< arg 0) (- arg)
  4733. (- (funcall outline-level) arg)))
  4734. (org-show-subtree)))
  4735. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4736. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4737. ;; At a heading: rotate between three different views
  4738. (org-back-to-heading)
  4739. (let ((goal-column 0) eoh eol eos)
  4740. ;; First, some boundaries
  4741. (save-excursion
  4742. (org-back-to-heading)
  4743. (save-excursion
  4744. (beginning-of-line 2)
  4745. (while (and (not (eobp)) ;; this is like `next-line'
  4746. (get-char-property (1- (point)) 'invisible))
  4747. (beginning-of-line 2)) (setq eol (point)))
  4748. (outline-end-of-heading) (setq eoh (point))
  4749. (org-end-of-subtree t)
  4750. (unless (eobp)
  4751. (skip-chars-forward " \t\n")
  4752. (beginning-of-line 1) ; in case this is an item
  4753. )
  4754. (setq eos (1- (point))))
  4755. ;; Find out what to do next and set `this-command'
  4756. (cond
  4757. ((= eos eoh)
  4758. ;; Nothing is hidden behind this heading
  4759. (message "EMPTY ENTRY")
  4760. (setq org-cycle-subtree-status nil)
  4761. (save-excursion
  4762. (goto-char eos)
  4763. (outline-next-heading)
  4764. (if (org-invisible-p) (org-flag-heading nil))))
  4765. ((or (>= eol eos)
  4766. (not (string-match "\\S-" (buffer-substring eol eos))))
  4767. ;; Entire subtree is hidden in one line: open it
  4768. (org-show-entry)
  4769. (show-children)
  4770. (message "CHILDREN")
  4771. (save-excursion
  4772. (goto-char eos)
  4773. (outline-next-heading)
  4774. (if (org-invisible-p) (org-flag-heading nil)))
  4775. (setq org-cycle-subtree-status 'children)
  4776. (run-hook-with-args 'org-cycle-hook 'children))
  4777. ((and (eq last-command this-command)
  4778. (eq org-cycle-subtree-status 'children))
  4779. ;; We just showed the children, now show everything.
  4780. (org-show-subtree)
  4781. (message "SUBTREE")
  4782. (setq org-cycle-subtree-status 'subtree)
  4783. (run-hook-with-args 'org-cycle-hook 'subtree))
  4784. (t
  4785. ;; Default action: hide the subtree.
  4786. (hide-subtree)
  4787. (message "FOLDED")
  4788. (setq org-cycle-subtree-status 'folded)
  4789. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4790. ;; TAB emulation
  4791. (buffer-read-only (org-back-to-heading))
  4792. ((org-try-cdlatex-tab))
  4793. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4794. (or (not (bolp))
  4795. (not (looking-at outline-regexp))))
  4796. (call-interactively (global-key-binding "\t")))
  4797. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4798. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4799. (or (and (eq org-cycle-emulate-tab 'white)
  4800. (= (match-end 0) (point-at-eol)))
  4801. (and (eq org-cycle-emulate-tab 'whitestart)
  4802. (>= (match-end 0) pos))))
  4803. t
  4804. (eq org-cycle-emulate-tab t))
  4805. ; (if (and (looking-at "[ \n\r\t]")
  4806. ; (string-match "^[ \t]*$" (buffer-substring
  4807. ; (point-at-bol) (point))))
  4808. ; (progn
  4809. ; (beginning-of-line 1)
  4810. ; (and (looking-at "[ \t]+") (replace-match ""))))
  4811. (call-interactively (global-key-binding "\t")))
  4812. (t (save-excursion
  4813. (org-back-to-heading)
  4814. (org-cycle))))))
  4815. ;;;###autoload
  4816. (defun org-global-cycle (&optional arg)
  4817. "Cycle the global visibility. For details see `org-cycle'."
  4818. (interactive "P")
  4819. (let ((org-cycle-include-plain-lists
  4820. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4821. (if (integerp arg)
  4822. (progn
  4823. (show-all)
  4824. (hide-sublevels arg)
  4825. (setq org-cycle-global-status 'contents))
  4826. (org-cycle '(4)))))
  4827. (defun org-overview ()
  4828. "Switch to overview mode, shoing only top-level headlines.
  4829. Really, this shows all headlines with level equal or greater than the level
  4830. of the first headline in the buffer. This is important, because if the
  4831. first headline is not level one, then (hide-sublevels 1) gives confusing
  4832. results."
  4833. (interactive)
  4834. (let ((level (save-excursion
  4835. (goto-char (point-min))
  4836. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4837. (progn
  4838. (goto-char (match-beginning 0))
  4839. (funcall outline-level))))))
  4840. (and level (hide-sublevels level))))
  4841. (defun org-content (&optional arg)
  4842. "Show all headlines in the buffer, like a table of contents.
  4843. With numerical argument N, show content up to level N."
  4844. (interactive "P")
  4845. (save-excursion
  4846. ;; Visit all headings and show their offspring
  4847. (and (integerp arg) (org-overview))
  4848. (goto-char (point-max))
  4849. (catch 'exit
  4850. (while (and (progn (condition-case nil
  4851. (outline-previous-visible-heading 1)
  4852. (error (goto-char (point-min))))
  4853. t)
  4854. (looking-at outline-regexp))
  4855. (if (integerp arg)
  4856. (show-children (1- arg))
  4857. (show-branches))
  4858. (if (bobp) (throw 'exit nil))))))
  4859. (defun org-optimize-window-after-visibility-change (state)
  4860. "Adjust the window after a change in outline visibility.
  4861. This function is the default value of the hook `org-cycle-hook'."
  4862. (when (get-buffer-window (current-buffer))
  4863. (cond
  4864. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4865. ; ((eq state 'overview) (org-beginning-of-line))
  4866. ((eq state 'content) nil)
  4867. ((eq state 'all) nil)
  4868. ((eq state 'folded) nil)
  4869. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4870. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4871. (defun org-cycle-show-empty-lines (state)
  4872. "Show empty lines above all visible headlines.
  4873. The region to be covered depends on STATE when called through
  4874. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4875. entire buffer covered. Note that an empty line is only shown if there
  4876. are at least `org-cycle-separator-lines' empty lines before the headeline."
  4877. (when (> org-cycle-separator-lines 0)
  4878. (save-excursion
  4879. (let* ((n org-cycle-separator-lines)
  4880. (re (cond
  4881. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4882. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4883. (t (let ((ns (number-to-string (- n 2))))
  4884. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4885. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4886. beg end)
  4887. (cond
  4888. ((memq state '(overview contents t))
  4889. (setq beg (point-min) end (point-max)))
  4890. ((memq state '(children folded))
  4891. (setq beg (point) end (progn (org-end-of-subtree t t)
  4892. (beginning-of-line 2)
  4893. (point)))))
  4894. (when beg
  4895. (goto-char beg)
  4896. (while (re-search-forward re end t)
  4897. (if (not (get-char-property (match-end 1) 'invisible))
  4898. (outline-flag-region
  4899. (match-beginning 1) (match-end 1) nil)))))))
  4900. ;; Never hide empty lines at the end of the file.
  4901. (save-excursion
  4902. (goto-char (point-max))
  4903. (outline-previous-heading)
  4904. (outline-end-of-heading)
  4905. (if (and (looking-at "[ \t\n]+")
  4906. (= (match-end 0) (point-max)))
  4907. (outline-flag-region (point) (match-end 0) nil))))
  4908. (defun org-subtree-end-visible-p ()
  4909. "Is the end of the current subtree visible?"
  4910. (pos-visible-in-window-p
  4911. (save-excursion (org-end-of-subtree t) (point))))
  4912. (defun org-first-headline-recenter (&optional N)
  4913. "Move cursor to the first headline and recenter the headline.
  4914. Optional argument N means, put the headline into the Nth line of the window."
  4915. (goto-char (point-min))
  4916. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4917. (beginning-of-line)
  4918. (recenter (prefix-numeric-value N))))
  4919. ;;; Org-goto
  4920. (defvar org-goto-window-configuration nil)
  4921. (defvar org-goto-marker nil)
  4922. (defvar org-goto-map
  4923. (let ((map (make-sparse-keymap)))
  4924. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4925. (while (setq cmd (pop cmds))
  4926. (substitute-key-definition cmd cmd map global-map)))
  4927. (suppress-keymap map)
  4928. (org-defkey map "\C-m" 'org-goto-ret)
  4929. (org-defkey map [(left)] 'org-goto-left)
  4930. (org-defkey map [(right)] 'org-goto-right)
  4931. (org-defkey map [(?q)] 'org-goto-quit)
  4932. (org-defkey map [(control ?g)] 'org-goto-quit)
  4933. (org-defkey map "\C-i" 'org-cycle)
  4934. (org-defkey map [(tab)] 'org-cycle)
  4935. (org-defkey map [(down)] 'outline-next-visible-heading)
  4936. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4937. (org-defkey map "n" 'outline-next-visible-heading)
  4938. (org-defkey map "p" 'outline-previous-visible-heading)
  4939. (org-defkey map "f" 'outline-forward-same-level)
  4940. (org-defkey map "b" 'outline-backward-same-level)
  4941. (org-defkey map "u" 'outline-up-heading)
  4942. (org-defkey map "/" 'org-occur)
  4943. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4944. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4945. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4946. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4947. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4948. map))
  4949. (defconst org-goto-help
  4950. "Browse copy of buffer to find location or copy text.
  4951. RET=jump to location [Q]uit and return to previous location
  4952. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur"
  4953. )
  4954. (defun org-goto ()
  4955. "Look up a different location in the current file, keeping current visibility.
  4956. When you want look-up or go to a different location in a document, the
  4957. fastest way is often to fold the entire buffer and then dive into the tree.
  4958. This method has the disadvantage, that the previous location will be folded,
  4959. which may not be what you want.
  4960. This command works around this by showing a copy of the current buffer
  4961. in an indirect buffer, in overview mode. You can dive into the tree in
  4962. that copy, use org-occur and incremental search to find a location.
  4963. When pressing RET or `Q', the command returns to the original buffer in
  4964. which the visibility is still unchanged. After RET is will also jump to
  4965. the location selected in the indirect buffer and expose the
  4966. the headline hierarchy above."
  4967. (interactive)
  4968. (let* ((org-goto-start-pos (point))
  4969. (selected-point
  4970. (car (org-get-location (current-buffer) org-goto-help))))
  4971. (if selected-point
  4972. (progn
  4973. (org-mark-ring-push org-goto-start-pos)
  4974. (goto-char selected-point)
  4975. (if (or (org-invisible-p) (org-invisible-p2))
  4976. (org-show-context 'org-goto)))
  4977. (message "Quit"))))
  4978. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4979. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4980. (defun org-get-location (buf help)
  4981. "Let the user select a location in the Org-mode buffer BUF.
  4982. This function uses a recursive edit. It returns the selected position
  4983. or nil."
  4984. (let (org-goto-selected-point org-goto-exit-command)
  4985. (save-excursion
  4986. (save-window-excursion
  4987. (delete-other-windows)
  4988. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4989. (switch-to-buffer
  4990. (condition-case nil
  4991. (make-indirect-buffer (current-buffer) "*org-goto*")
  4992. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4993. (with-output-to-temp-buffer "*Help*"
  4994. (princ help))
  4995. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  4996. (setq buffer-read-only nil)
  4997. (let ((org-startup-truncated t)
  4998. (org-startup-folded nil)
  4999. (org-startup-align-all-tables nil))
  5000. (org-mode)
  5001. (org-overview))
  5002. (setq buffer-read-only t)
  5003. (if (and (boundp 'org-goto-start-pos)
  5004. (integer-or-marker-p org-goto-start-pos))
  5005. (let ((org-show-hierarchy-above t)
  5006. (org-show-siblings t)
  5007. (org-show-following-heading t))
  5008. (goto-char org-goto-start-pos)
  5009. (and (org-invisible-p) (org-show-context)))
  5010. (goto-char (point-min)))
  5011. (org-beginning-of-line)
  5012. (message "Select location and press RET")
  5013. ;; now we make sure that during selection, ony very few keys work
  5014. ;; and that it is impossible to switch to another window.
  5015. ; (let ((gm (current-global-map))
  5016. ; (overriding-local-map org-goto-map))
  5017. ; (unwind-protect
  5018. ; (progn
  5019. ; (use-global-map org-goto-map)
  5020. ; (recursive-edit))
  5021. ; (use-global-map gm)))
  5022. (use-local-map org-goto-map)
  5023. (recursive-edit)
  5024. ))
  5025. (kill-buffer "*org-goto*")
  5026. (cons org-goto-selected-point org-goto-exit-command)))
  5027. (defun org-goto-ret (&optional arg)
  5028. "Finish `org-goto' by going to the new location."
  5029. (interactive "P")
  5030. (setq org-goto-selected-point (point)
  5031. org-goto-exit-command 'return)
  5032. (throw 'exit nil))
  5033. (defun org-goto-left ()
  5034. "Finish `org-goto' by going to the new location."
  5035. (interactive)
  5036. (if (org-on-heading-p)
  5037. (progn
  5038. (beginning-of-line 1)
  5039. (setq org-goto-selected-point (point)
  5040. org-goto-exit-command 'left)
  5041. (throw 'exit nil))
  5042. (error "Not on a heading")))
  5043. (defun org-goto-right ()
  5044. "Finish `org-goto' by going to the new location."
  5045. (interactive)
  5046. (if (org-on-heading-p)
  5047. (progn
  5048. (setq org-goto-selected-point (point)
  5049. org-goto-exit-command 'right)
  5050. (throw 'exit nil))
  5051. (error "Not on a heading")))
  5052. (defun org-goto-quit ()
  5053. "Finish `org-goto' without cursor motion."
  5054. (interactive)
  5055. (setq org-goto-selected-point nil)
  5056. (setq org-goto-exit-command 'quit)
  5057. (throw 'exit nil))
  5058. ;;; Indirect buffer display of subtrees
  5059. (defvar org-indirect-dedicated-frame nil
  5060. "This is the frame being used for indirect tree display.")
  5061. (defvar org-last-indirect-buffer nil)
  5062. (defun org-tree-to-indirect-buffer (&optional arg)
  5063. "Create indirect buffer and narrow it to current subtree.
  5064. With numerical prefix ARG, go up to this level and then take that tree.
  5065. If ARG is negative, go up that many levels.
  5066. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5067. indirect buffer previously made with this command, to avoid proliferation of
  5068. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5069. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5070. is kept so that you can work with several indirect buffers at the same time.
  5071. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5072. requests that a new frame be made for the new buffer, so that the dedicated
  5073. frame is not changed."
  5074. (interactive "P")
  5075. (let ((cbuf (current-buffer))
  5076. (cwin (selected-window))
  5077. (pos (point))
  5078. beg end level heading ibuf)
  5079. (save-excursion
  5080. (org-back-to-heading t)
  5081. (when (numberp arg)
  5082. (setq level (org-outline-level))
  5083. (if (< arg 0) (setq arg (+ level arg)))
  5084. (while (> (setq level (org-outline-level)) arg)
  5085. (outline-up-heading 1 t)))
  5086. (setq beg (point)
  5087. heading (org-get-heading))
  5088. (org-end-of-subtree t) (setq end (point)))
  5089. (if (and (buffer-live-p org-last-indirect-buffer)
  5090. (not (eq org-indirect-buffer-display 'new-frame))
  5091. (not arg))
  5092. (kill-buffer org-last-indirect-buffer))
  5093. (setq ibuf (org-get-indirect-buffer cbuf)
  5094. org-last-indirect-buffer ibuf)
  5095. (cond
  5096. ((or (eq org-indirect-buffer-display 'new-frame)
  5097. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5098. (select-frame (make-frame))
  5099. (delete-other-windows)
  5100. (switch-to-buffer ibuf)
  5101. (org-set-frame-title heading))
  5102. ((eq org-indirect-buffer-display 'dedicated-frame)
  5103. (raise-frame
  5104. (select-frame (or (and org-indirect-dedicated-frame
  5105. (frame-live-p org-indirect-dedicated-frame)
  5106. org-indirect-dedicated-frame)
  5107. (setq org-indirect-dedicated-frame (make-frame)))))
  5108. (delete-other-windows)
  5109. (switch-to-buffer ibuf)
  5110. (org-set-frame-title (concat "Indirect: " heading)))
  5111. ((eq org-indirect-buffer-display 'current-window)
  5112. (switch-to-buffer ibuf))
  5113. ((eq org-indirect-buffer-display 'other-window)
  5114. (pop-to-buffer ibuf))
  5115. (t (error "Invalid value.")))
  5116. (if (featurep 'xemacs)
  5117. (save-excursion (org-mode) (turn-on-font-lock)))
  5118. (narrow-to-region beg end)
  5119. (show-all)
  5120. (goto-char pos)
  5121. (and (window-live-p cwin) (select-window cwin))))
  5122. (defun org-get-indirect-buffer (&optional buffer)
  5123. (setq buffer (or buffer (current-buffer)))
  5124. (let ((n 1) (base (buffer-name buffer)) bname)
  5125. (while (buffer-live-p
  5126. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5127. (setq n (1+ n)))
  5128. (condition-case nil
  5129. (make-indirect-buffer buffer bname 'clone)
  5130. (error (make-indirect-buffer buffer bname)))))
  5131. (defun org-set-frame-title (title)
  5132. "Set the title of the current frame to the string TITLE."
  5133. ;; FIXME: how to name a single frame in XEmacs???
  5134. (unless (featurep 'xemacs)
  5135. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5136. ;;;; Structure editing
  5137. ;;; Inserting headlines
  5138. (defun org-insert-heading (&optional force-heading)
  5139. "Insert a new heading or item with same depth at point.
  5140. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5141. If point is at the beginning of a headline, insert a sibling before the
  5142. current headline. If point is in the middle of a headline, split the headline
  5143. at that position and make the rest of the headline part of the sibling below
  5144. the current headline."
  5145. (interactive "P")
  5146. (if (= (buffer-size) 0)
  5147. (insert "\n* ")
  5148. (when (or force-heading (not (org-insert-item)))
  5149. (let* ((head (save-excursion
  5150. (condition-case nil
  5151. (progn
  5152. (org-back-to-heading)
  5153. (match-string 0))
  5154. (error "*"))))
  5155. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5156. pos)
  5157. (cond
  5158. ((and (org-on-heading-p) (bolp)
  5159. (or (bobp)
  5160. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5161. (open-line (if blank 2 1)))
  5162. ((and (bolp)
  5163. (or (bobp)
  5164. (save-excursion
  5165. (backward-char 1) (not (org-invisible-p)))))
  5166. nil)
  5167. (t (newline (if blank 2 1))))
  5168. (insert head) (just-one-space)
  5169. (setq pos (point))
  5170. (end-of-line 1)
  5171. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5172. (run-hooks 'org-insert-heading-hook)))))
  5173. (defun org-insert-heading-after-current ()
  5174. "Insert a new heading with same level as current, after current subtree."
  5175. (interactive)
  5176. (org-back-to-heading)
  5177. (org-insert-heading)
  5178. (org-move-subtree-down)
  5179. (end-of-line 1))
  5180. (defun org-insert-todo-heading (arg)
  5181. "Insert a new heading with the same level and TODO state as current heading.
  5182. If the heading has no TODO state, or if the state is DONE, use the first
  5183. state (TODO by default). Also with prefix arg, force first state."
  5184. (interactive "P")
  5185. (when (not (org-insert-item 'checkbox))
  5186. (org-insert-heading)
  5187. (save-excursion
  5188. (org-back-to-heading)
  5189. (outline-previous-heading)
  5190. (looking-at org-todo-line-regexp))
  5191. (if (or arg
  5192. (not (match-beginning 2))
  5193. (member (match-string 2) org-done-keywords))
  5194. (insert (car org-todo-keywords-1) " ")
  5195. (insert (match-string 2) " "))))
  5196. (defun org-insert-subheading (arg)
  5197. "Insert a new subheading and demote it.
  5198. Works for outline headings and for plain lists alike."
  5199. (interactive "P")
  5200. (org-insert-heading arg)
  5201. (cond
  5202. ((org-on-heading-p) (org-do-demote))
  5203. ((org-at-item-p) (org-indent-item 1))))
  5204. (defun org-insert-todo-subheading (arg)
  5205. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5206. Works for outline headings and for plain lists alike."
  5207. (interactive "P")
  5208. (org-insert-todo-heading arg)
  5209. (cond
  5210. ((org-on-heading-p) (org-do-demote))
  5211. ((org-at-item-p) (org-indent-item 1))))
  5212. ;;; Promotion and Demotion
  5213. (defun org-promote-subtree ()
  5214. "Promote the entire subtree.
  5215. See also `org-promote'."
  5216. (interactive)
  5217. (save-excursion
  5218. (org-map-tree 'org-promote))
  5219. (org-fix-position-after-promote))
  5220. (defun org-demote-subtree ()
  5221. "Demote the entire subtree. See `org-demote'.
  5222. See also `org-promote'."
  5223. (interactive)
  5224. (save-excursion
  5225. (org-map-tree 'org-demote))
  5226. (org-fix-position-after-promote))
  5227. (defun org-do-promote ()
  5228. "Promote the current heading higher up the tree.
  5229. If the region is active in `transient-mark-mode', promote all headings
  5230. in the region."
  5231. (interactive)
  5232. (save-excursion
  5233. (if (org-region-active-p)
  5234. (org-map-region 'org-promote (region-beginning) (region-end))
  5235. (org-promote)))
  5236. (org-fix-position-after-promote))
  5237. (defun org-do-demote ()
  5238. "Demote the current heading lower down the tree.
  5239. If the region is active in `transient-mark-mode', demote all headings
  5240. in the region."
  5241. (interactive)
  5242. (save-excursion
  5243. (if (org-region-active-p)
  5244. (org-map-region 'org-demote (region-beginning) (region-end))
  5245. (org-demote)))
  5246. (org-fix-position-after-promote))
  5247. (defun org-fix-position-after-promote ()
  5248. "Make sure that after pro/demotion cursor position is right."
  5249. (let ((pos (point)))
  5250. (when (save-excursion
  5251. (beginning-of-line 1)
  5252. (looking-at org-todo-line-regexp)
  5253. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5254. (cond ((eobp) (insert " "))
  5255. ((eolp) (insert " "))
  5256. ((equal (char-after) ?\ ) (forward-char 1))))))
  5257. (defun org-reduced-level (l)
  5258. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5259. (defun org-get-legal-level (level &optional change)
  5260. "Rectify a level change under the influence of `org-odd-levels-only'
  5261. LEVEL is a current level, CHANGE is by how much the level should be
  5262. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5263. even level numbers will become the next higher odd number."
  5264. (if org-odd-levels-only
  5265. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5266. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5267. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5268. (max 1 (+ level change))))
  5269. (defun org-promote ()
  5270. "Promote the current heading higher up the tree.
  5271. If the region is active in `transient-mark-mode', promote all headings
  5272. in the region."
  5273. (org-back-to-heading t)
  5274. (let* ((level (save-match-data (funcall outline-level)))
  5275. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5276. (diff (abs (- level (length up-head) -1))))
  5277. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5278. (replace-match up-head nil t)
  5279. ;; Fixup tag positioning
  5280. (and org-auto-align-tags (org-set-tags nil t))
  5281. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5282. (defun org-demote ()
  5283. "Demote the current heading lower down the tree.
  5284. If the region is active in `transient-mark-mode', demote all headings
  5285. in the region."
  5286. (org-back-to-heading t)
  5287. (let* ((level (save-match-data (funcall outline-level)))
  5288. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5289. (diff (abs (- level (length down-head) -1))))
  5290. (replace-match down-head nil t)
  5291. ;; Fixup tag positioning
  5292. (and org-auto-align-tags (org-set-tags nil t))
  5293. (if org-adapt-indentation (org-fixup-indentation diff))))
  5294. (defun org-map-tree (fun)
  5295. "Call FUN for every heading underneath the current one."
  5296. (org-back-to-heading)
  5297. (let ((level (funcall outline-level)))
  5298. (save-excursion
  5299. (funcall fun)
  5300. (while (and (progn
  5301. (outline-next-heading)
  5302. (> (funcall outline-level) level))
  5303. (not (eobp)))
  5304. (funcall fun)))))
  5305. (defun org-map-region (fun beg end)
  5306. "Call FUN for every heading between BEG and END."
  5307. (let ((org-ignore-region t))
  5308. (save-excursion
  5309. (setq end (copy-marker end))
  5310. (goto-char beg)
  5311. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5312. (< (point) end))
  5313. (funcall fun))
  5314. (while (and (progn
  5315. (outline-next-heading)
  5316. (< (point) end))
  5317. (not (eobp)))
  5318. (funcall fun)))))
  5319. (defun org-fixup-indentation (diff)
  5320. "Change the indentation in the current entry by DIFF
  5321. However, if any line in the current entry has no indentation, or if it
  5322. would end up with no indentation after the change, nothing at all is done."
  5323. (save-excursion
  5324. (let ((end (save-excursion (outline-next-heading)
  5325. (point-marker)))
  5326. (prohibit (if (> diff 0)
  5327. "^\\S-"
  5328. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5329. col)
  5330. (unless (save-excursion (end-of-line 1)
  5331. (re-search-forward prohibit end t))
  5332. (while (and (< (point) end)
  5333. (re-search-forward "^[ \t]+" end t))
  5334. (goto-char (match-end 0))
  5335. (setq col (current-column))
  5336. (if (< diff 0) (replace-match ""))
  5337. (indent-to (+ diff col))))
  5338. (move-marker end nil))))
  5339. (defun org-convert-to-odd-levels ()
  5340. "Convert an org-mode file with all levels allowed to one with odd levels.
  5341. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5342. level 5 etc."
  5343. (interactive)
  5344. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5345. (let ((org-odd-levels-only nil) n)
  5346. (save-excursion
  5347. (goto-char (point-min))
  5348. (while (re-search-forward "^\\*\\*+ " nil t)
  5349. (setq n (- (length (match-string 0)) 2))
  5350. (while (>= (setq n (1- n)) 0)
  5351. (org-demote))
  5352. (end-of-line 1))))))
  5353. (defun org-convert-to-oddeven-levels ()
  5354. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5355. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5356. section with an even level, conversion would destroy the structure of the file. An error
  5357. is signaled in this case."
  5358. (interactive)
  5359. (goto-char (point-min))
  5360. ;; First check if there are no even levels
  5361. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5362. (org-show-context t)
  5363. (error "Not all levels are odd in this file. Conversion not possible."))
  5364. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5365. (let ((org-odd-levels-only nil) n)
  5366. (save-excursion
  5367. (goto-char (point-min))
  5368. (while (re-search-forward "^\\*\\*+ " nil t)
  5369. (setq n (/ (1- (length (match-string 0))) 2))
  5370. (while (>= (setq n (1- n)) 0)
  5371. (org-promote))
  5372. (end-of-line 1))))))
  5373. (defun org-tr-level (n)
  5374. "Make N odd if required."
  5375. (if org-odd-levels-only (1+ (/ n 2)) n))
  5376. ;;; Vertical tree motion, cutting and pasting of subtrees
  5377. (defun org-move-subtree-up (&optional arg)
  5378. "Move the current subtree up past ARG headlines of the same level."
  5379. (interactive "p")
  5380. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5381. (defun org-move-subtree-down (&optional arg)
  5382. "Move the current subtree down past ARG headlines of the same level."
  5383. (interactive "p")
  5384. (setq arg (prefix-numeric-value arg))
  5385. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5386. 'outline-get-last-sibling))
  5387. (ins-point (make-marker))
  5388. (cnt (abs arg))
  5389. beg end txt folded)
  5390. ;; Select the tree
  5391. (org-back-to-heading)
  5392. (setq beg (point))
  5393. (save-match-data
  5394. (save-excursion (outline-end-of-heading)
  5395. (setq folded (org-invisible-p)))
  5396. (outline-end-of-subtree))
  5397. (outline-next-heading)
  5398. (setq end (point))
  5399. ;; Find insertion point, with error handling
  5400. (goto-char beg)
  5401. (while (> cnt 0)
  5402. (or (and (funcall movfunc) (looking-at outline-regexp))
  5403. (progn (goto-char beg)
  5404. (error "Cannot move past superior level or buffer limit")))
  5405. (setq cnt (1- cnt)))
  5406. (if (> arg 0)
  5407. ;; Moving forward - still need to move over subtree
  5408. (progn (outline-end-of-subtree)
  5409. (outline-next-heading)
  5410. (if (not (or (looking-at (concat "^" outline-regexp))
  5411. (bolp)))
  5412. (newline))))
  5413. (move-marker ins-point (point))
  5414. (setq txt (buffer-substring beg end))
  5415. (delete-region beg end)
  5416. (insert txt)
  5417. (or (bolp) (insert "\n"))
  5418. (goto-char ins-point)
  5419. (if folded (hide-subtree))
  5420. (move-marker ins-point nil)))
  5421. (defvar org-subtree-clip ""
  5422. "Clipboard for cut and paste of subtrees.
  5423. This is actually only a copy of the kill, because we use the normal kill
  5424. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5425. (defvar org-subtree-clip-folded nil
  5426. "Was the last copied subtree folded?
  5427. This is used to fold the tree back after pasting.")
  5428. (defun org-cut-subtree (&optional n)
  5429. "Cut the current subtree into the clipboard.
  5430. With prefix arg N, cut this many sequential subtrees.
  5431. This is a short-hand for marking the subtree and then cutting it."
  5432. (interactive "p")
  5433. (org-copy-subtree n 'cut))
  5434. (defun org-copy-subtree (&optional n cut)
  5435. "Cut the current subtree into the clipboard.
  5436. With prefix arg N, cut this many sequential subtrees.
  5437. This is a short-hand for marking the subtree and then copying it.
  5438. If CUT is non-nil, actually cut the subtree."
  5439. (interactive "p")
  5440. (let (beg end folded)
  5441. (if (interactive-p)
  5442. (org-back-to-heading nil) ; take what looks like a subtree
  5443. (org-back-to-heading t)) ; take what is really there
  5444. (setq beg (point))
  5445. (save-match-data
  5446. (save-excursion (outline-end-of-heading)
  5447. (setq folded (org-invisible-p)))
  5448. (condition-case nil
  5449. (outline-forward-same-level (1- n))
  5450. (error nil))
  5451. (org-end-of-subtree t t))
  5452. (setq end (point))
  5453. (goto-char beg)
  5454. (when (> end beg)
  5455. (setq org-subtree-clip-folded folded)
  5456. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5457. (setq org-subtree-clip (current-kill 0))
  5458. (message "%s: Subtree(s) with %d characters"
  5459. (if cut "Cut" "Copied")
  5460. (length org-subtree-clip)))))
  5461. (defun org-paste-subtree (&optional level tree)
  5462. "Paste the clipboard as a subtree, with modification of headline level.
  5463. The entire subtree is promoted or demoted in order to match a new headline
  5464. level. By default, the new level is derived from the visible headings
  5465. before and after the insertion point, and taken to be the inferior headline
  5466. level of the two. So if the previous visible heading is level 3 and the
  5467. next is level 4 (or vice versa), level 4 will be used for insertion.
  5468. This makes sure that the subtree remains an independent subtree and does
  5469. not swallow low level entries.
  5470. You can also force a different level, either by using a numeric prefix
  5471. argument, or by inserting the heading marker by hand. For example, if the
  5472. cursor is after \"*****\", then the tree will be shifted to level 5.
  5473. If you want to insert the tree as is, just use \\[yank].
  5474. If optional TREE is given, use this text instead of the kill ring."
  5475. (interactive "P")
  5476. (unless (org-kill-is-subtree-p tree)
  5477. (error
  5478. (substitute-command-keys
  5479. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5480. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5481. (^re (concat "^\\(" outline-regexp "\\)"))
  5482. (re (concat "\\(" outline-regexp "\\)"))
  5483. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5484. (old-level (if (string-match ^re txt)
  5485. (- (match-end 0) (match-beginning 0) 1)
  5486. -1))
  5487. (force-level (cond (level (prefix-numeric-value level))
  5488. ((string-match
  5489. ^re_ (buffer-substring (point-at-bol) (point)))
  5490. (- (match-end 1) (match-beginning 1)))
  5491. (t nil)))
  5492. (previous-level (save-excursion
  5493. (condition-case nil
  5494. (progn
  5495. (outline-previous-visible-heading 1)
  5496. (if (looking-at re)
  5497. (- (match-end 0) (match-beginning 0) 1)
  5498. 1))
  5499. (error 1))))
  5500. (next-level (save-excursion
  5501. (condition-case nil
  5502. (progn
  5503. (or (looking-at outline-regexp)
  5504. (outline-next-visible-heading 1))
  5505. (if (looking-at re)
  5506. (- (match-end 0) (match-beginning 0) 1)
  5507. 1))
  5508. (error 1))))
  5509. (new-level (or force-level (max previous-level next-level)))
  5510. (shift (if (or (= old-level -1)
  5511. (= new-level -1)
  5512. (= old-level new-level))
  5513. 0
  5514. (- new-level old-level)))
  5515. (delta (if (> shift 0) -1 1))
  5516. (func (if (> shift 0) 'org-demote 'org-promote))
  5517. (org-odd-levels-only nil)
  5518. beg end)
  5519. ;; Remove the forced level indicator
  5520. (if force-level
  5521. (delete-region (point-at-bol) (point)))
  5522. ;; Paste
  5523. (beginning-of-line 1)
  5524. (setq beg (point))
  5525. (insert txt)
  5526. (unless (string-match "\n\\'" txt) (insert "\n"))
  5527. (setq end (point))
  5528. (goto-char beg)
  5529. ;; Shift if necessary
  5530. (unless (= shift 0)
  5531. (save-restriction
  5532. (narrow-to-region beg end)
  5533. (while (not (= shift 0))
  5534. (org-map-region func (point-min) (point-max))
  5535. (setq shift (+ delta shift)))
  5536. (goto-char (point-min))))
  5537. (when (interactive-p)
  5538. (message "Clipboard pasted as level %d subtree" new-level))
  5539. (if (and kill-ring
  5540. (eq org-subtree-clip (current-kill 0))
  5541. org-subtree-clip-folded)
  5542. ;; The tree was folded before it was killed/copied
  5543. (hide-subtree))))
  5544. (defun org-kill-is-subtree-p (&optional txt)
  5545. "Check if the current kill is an outline subtree, or a set of trees.
  5546. Returns nil if kill does not start with a headline, or if the first
  5547. headline level is not the largest headline level in the tree.
  5548. So this will actually accept several entries of equal levels as well,
  5549. which is OK for `org-paste-subtree'.
  5550. If optional TXT is given, check this string instead of the current kill."
  5551. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5552. (start-level (and kill
  5553. (string-match (concat "\\`" org-outline-regexp) kill)
  5554. (- (match-end 0) (match-beginning 0) 1)))
  5555. (re (concat "^" org-outline-regexp))
  5556. (start 1))
  5557. (if (not start-level)
  5558. (progn
  5559. nil) ;; does not even start with a heading
  5560. (catch 'exit
  5561. (while (setq start (string-match re kill (1+ start)))
  5562. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5563. (throw 'exit nil)))
  5564. t))))
  5565. (defun org-narrow-to-subtree ()
  5566. "Narrow buffer to the current subtree."
  5567. (interactive)
  5568. (save-excursion
  5569. (narrow-to-region
  5570. (progn (org-back-to-heading) (point))
  5571. (progn (org-end-of-subtree t t) (point)))))
  5572. ;;; Outline Sorting
  5573. (defun org-sort (with-case)
  5574. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5575. Optional argument WITH-CASE means sort case-sensitively."
  5576. (interactive "P")
  5577. (if (org-at-table-p)
  5578. (org-call-with-arg 'org-table-sort-lines with-case)
  5579. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5580. (defvar org-priority-regexp) ; defined later in the file
  5581. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5582. "Sort entries on a certain level of an outline tree.
  5583. If there is an active region, the entries in the region are sorted.
  5584. Else, if the cursor is before the first entry, sort the top-level items.
  5585. Else, the children of the entry at point are sorted.
  5586. Sorting can be alphabetically, numerically, and by date/time as given by
  5587. the first time stamp in the entry. The command prompts for the sorting
  5588. type unless it has been given to the function through the SORTING-TYPE
  5589. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5590. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5591. called with point at the beginning of the record. It must return either
  5592. a string or a number that should serve as the sorting key for that record.
  5593. Comparing entries ignores case by default. However, with an optional argument
  5594. WITH-CASE, the sorting considers case as well."
  5595. (interactive "P")
  5596. (let ((case-func (if with-case 'identity 'downcase))
  5597. start beg end stars re re2
  5598. txt what tmp plain-list-p)
  5599. ;; Find beginning and end of region to sort
  5600. (cond
  5601. ((org-region-active-p)
  5602. ;; we will sort the region
  5603. (setq end (region-end)
  5604. what "region")
  5605. (goto-char (region-beginning))
  5606. (if (not (org-on-heading-p)) (outline-next-heading))
  5607. (setq start (point)))
  5608. ((org-at-item-p)
  5609. ;; we will sort this plain list
  5610. (org-beginning-of-item-list) (setq start (point))
  5611. (org-end-of-item-list) (setq end (point))
  5612. (goto-char start)
  5613. (setq plain-list-p t
  5614. what "plain list"))
  5615. ((or (org-on-heading-p)
  5616. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5617. ;; we will sort the children of the current headline
  5618. (org-back-to-heading)
  5619. (setq start (point) end (org-end-of-subtree) what "children")
  5620. (goto-char start)
  5621. (show-subtree)
  5622. (outline-next-heading))
  5623. (t
  5624. ;; we will sort the top-level entries in this file
  5625. (goto-char (point-min))
  5626. (or (org-on-heading-p) (outline-next-heading))
  5627. (setq start (point) end (point-max) what "top-level")
  5628. (goto-char start)
  5629. (show-all)))
  5630. (setq beg (point))
  5631. (if (>= beg end) (error "Nothing to sort"))
  5632. (unless plain-list-p
  5633. (looking-at "\\(\\*+\\)")
  5634. (setq stars (match-string 1)
  5635. re (concat "^" (regexp-quote stars) " +")
  5636. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5637. txt (buffer-substring beg end))
  5638. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5639. (if (and (not (equal stars "*")) (string-match re2 txt))
  5640. (error "Region to sort contains a level above the first entry")))
  5641. (unless sorting-type
  5642. (message
  5643. (if plain-list-p
  5644. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5645. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  5646. what)
  5647. (setq sorting-type (read-char-exclusive))
  5648. (and (= (downcase sorting-type) ?f)
  5649. (setq getkey-func
  5650. (completing-read "Sort using function: "
  5651. obarray 'fboundp t nil nil))
  5652. (setq getkey-func (intern getkey-func)))
  5653. (and (= (downcase sorting-type) ?r)
  5654. (setq property
  5655. (completing-read "Property: "
  5656. (mapcar 'list (org-buffer-property-keys t))
  5657. nil t))))
  5658. (message "Sorting entries...")
  5659. (save-restriction
  5660. (narrow-to-region start end)
  5661. (let ((dcst (downcase sorting-type))
  5662. (now (current-time)))
  5663. (sort-subr
  5664. (/= dcst sorting-type)
  5665. ;; This function moves to the beginning character of the "record" to
  5666. ;; be sorted.
  5667. (if plain-list-p
  5668. (lambda nil
  5669. (if (org-at-item-p) t (goto-char (point-max))))
  5670. (lambda nil
  5671. (if (re-search-forward re nil t)
  5672. (goto-char (match-beginning 0))
  5673. (goto-char (point-max)))))
  5674. ;; This function moves to the last character of the "record" being
  5675. ;; sorted.
  5676. (if plain-list-p
  5677. 'org-end-of-item
  5678. (lambda nil
  5679. (save-match-data
  5680. (condition-case nil
  5681. (outline-forward-same-level 1)
  5682. (error
  5683. (goto-char (point-max)))))))
  5684. ;; This function returns the value that gets sorted against.
  5685. (if plain-list-p
  5686. (lambda nil
  5687. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5688. (cond
  5689. ((= dcst ?n)
  5690. (string-to-number (buffer-substring (match-end 0)
  5691. (point-at-eol))))
  5692. ((= dcst ?a)
  5693. (buffer-substring (match-end 0) (point-at-eol)))
  5694. ((= dcst ?t)
  5695. (if (re-search-forward org-ts-regexp
  5696. (point-at-eol) t)
  5697. (org-time-string-to-time (match-string 0))
  5698. now))
  5699. ((= dcst ?f)
  5700. (if getkey-func
  5701. (progn
  5702. (setq tmp (funcall getkey-func))
  5703. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5704. tmp)
  5705. (error "Invalid key function `%s'" getkey-func)))
  5706. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5707. (lambda nil
  5708. (cond
  5709. ((= dcst ?n)
  5710. (if (looking-at outline-regexp)
  5711. (string-to-number (buffer-substring (match-end 0)
  5712. (point-at-eol)))
  5713. nil))
  5714. ((= dcst ?a)
  5715. (funcall case-func (buffer-substring (point-at-bol)
  5716. (point-at-eol))))
  5717. ((= dcst ?t)
  5718. (if (re-search-forward org-ts-regexp
  5719. (save-excursion
  5720. (forward-line 2)
  5721. (point)) t)
  5722. (org-time-string-to-time (match-string 0))
  5723. now))
  5724. ((= dcst ?p)
  5725. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5726. (string-to-char (match-string 2))
  5727. org-default-priority))
  5728. ((= dcst ?r)
  5729. (or (org-entry-get nil property) ""))
  5730. ((= dcst ?f)
  5731. (if getkey-func
  5732. (progn
  5733. (setq tmp (funcall getkey-func))
  5734. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5735. tmp)
  5736. (error "Invalid key function `%s'" getkey-func)))
  5737. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5738. nil
  5739. (cond
  5740. ((= dcst ?a) 'string<)
  5741. ((= dcst ?t) 'time-less-p)
  5742. (t nil)))))
  5743. (message "Sorting entries...done")))
  5744. (defun org-do-sort (table what &optional with-case sorting-type)
  5745. "Sort TABLE of WHAT according to SORTING-TYPE.
  5746. The user will be prompted for the SORTING-TYPE if the call to this
  5747. function does not specify it. WHAT is only for the prompt, to indicate
  5748. what is being sorted. The sorting key will be extracted from
  5749. the car of the elements of the table.
  5750. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5751. (unless sorting-type
  5752. (message
  5753. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5754. what)
  5755. (setq sorting-type (read-char-exclusive)))
  5756. (let ((dcst (downcase sorting-type))
  5757. extractfun comparefun)
  5758. ;; Define the appropriate functions
  5759. (cond
  5760. ((= dcst ?n)
  5761. (setq extractfun 'string-to-number
  5762. comparefun (if (= dcst sorting-type) '< '>)))
  5763. ((= dcst ?a)
  5764. (setq extractfun (if with-case 'identity 'downcase)
  5765. comparefun (if (= dcst sorting-type)
  5766. 'string<
  5767. (lambda (a b) (and (not (string< a b))
  5768. (not (string= a b)))))))
  5769. ((= dcst ?t)
  5770. (setq extractfun
  5771. (lambda (x)
  5772. (if (string-match org-ts-regexp x)
  5773. (time-to-seconds
  5774. (org-time-string-to-time (match-string 0 x)))
  5775. 0))
  5776. comparefun (if (= dcst sorting-type) '< '>)))
  5777. (t (error "Invalid sorting type `%c'" sorting-type)))
  5778. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5779. table)
  5780. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5781. ;;;; Plain list items, including checkboxes
  5782. ;;; Plain list items
  5783. (defun org-at-item-p ()
  5784. "Is point in a line starting a hand-formatted item?"
  5785. (let ((llt org-plain-list-ordered-item-terminator))
  5786. (save-excursion
  5787. (goto-char (point-at-bol))
  5788. (looking-at
  5789. (cond
  5790. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5791. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5792. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5793. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5794. (defun org-in-item-p ()
  5795. "It the cursor inside a plain list item.
  5796. Does not have to be the first line."
  5797. (save-excursion
  5798. (condition-case nil
  5799. (progn
  5800. (org-beginning-of-item)
  5801. (org-at-item-p)
  5802. t)
  5803. (error nil))))
  5804. (defun org-insert-item (&optional checkbox)
  5805. "Insert a new item at the current level.
  5806. Return t when things worked, nil when we are not in an item."
  5807. (when (save-excursion
  5808. (condition-case nil
  5809. (progn
  5810. (org-beginning-of-item)
  5811. (org-at-item-p)
  5812. (if (org-invisible-p) (error "Invisible item"))
  5813. t)
  5814. (error nil)))
  5815. (let* ((bul (match-string 0))
  5816. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5817. (match-end 0)))
  5818. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5819. pos)
  5820. (cond
  5821. ((and (org-at-item-p) (<= (point) eow))
  5822. ;; before the bullet
  5823. (beginning-of-line 1)
  5824. (open-line (if blank 2 1)))
  5825. ((<= (point) eow)
  5826. (beginning-of-line 1))
  5827. (t (newline (if blank 2 1))))
  5828. (insert bul (if checkbox "[ ]" ""))
  5829. (just-one-space)
  5830. (setq pos (point))
  5831. (end-of-line 1)
  5832. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5833. (org-maybe-renumber-ordered-list)
  5834. (and checkbox (org-update-checkbox-count-maybe))
  5835. t))
  5836. ;;; Checkboxes
  5837. (defun org-at-item-checkbox-p ()
  5838. "Is point at a line starting a plain-list item with a checklet?"
  5839. (and (org-at-item-p)
  5840. (save-excursion
  5841. (goto-char (match-end 0))
  5842. (skip-chars-forward " \t")
  5843. (looking-at "\\[[- X]\\]"))))
  5844. (defun org-toggle-checkbox (&optional arg)
  5845. "Toggle the checkbox in the current line."
  5846. (interactive "P")
  5847. (catch 'exit
  5848. (let (beg end status (firstnew 'unknown))
  5849. (cond
  5850. ((org-region-active-p)
  5851. (setq beg (region-beginning) end (region-end)))
  5852. ((org-on-heading-p)
  5853. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5854. ((org-at-item-checkbox-p)
  5855. (let ((pos (point)))
  5856. (replace-match
  5857. (cond (arg "[-]")
  5858. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5859. (t "[ ]"))
  5860. t t)
  5861. (goto-char pos))
  5862. (throw 'exit t))
  5863. (t (error "Not at a checkbox or heading, and no active region")))
  5864. (save-excursion
  5865. (goto-char beg)
  5866. (while (< (point) end)
  5867. (when (org-at-item-checkbox-p)
  5868. (setq status (equal (match-string 0) "[X]"))
  5869. (when (eq firstnew 'unknown)
  5870. (setq firstnew (not status)))
  5871. (replace-match
  5872. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5873. (beginning-of-line 2)))))
  5874. (org-update-checkbox-count-maybe))
  5875. (defun org-update-checkbox-count-maybe ()
  5876. "Update checkbox statistics unless turned off by user."
  5877. (when org-provide-checkbox-statistics
  5878. (org-update-checkbox-count)))
  5879. (defun org-update-checkbox-count (&optional all)
  5880. "Update the checkbox statistics in the current section.
  5881. This will find all statistic cookies like [57%] and [6/12] and update them
  5882. with the current numbers. With optional prefix argument ALL, do this for
  5883. the whole buffer."
  5884. (interactive "P")
  5885. (save-excursion
  5886. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5887. (beg (condition-case nil
  5888. (progn (outline-back-to-heading) (point))
  5889. (error (point-min))))
  5890. (end (move-marker (make-marker)
  5891. (progn (outline-next-heading) (point))))
  5892. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  5893. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5894. b1 e1 f1 c-on c-off lim (cstat 0))
  5895. (when all
  5896. (goto-char (point-min))
  5897. (outline-next-heading)
  5898. (setq beg (point) end (point-max)))
  5899. (goto-char beg)
  5900. (while (re-search-forward re end t)
  5901. (setq cstat (1+ cstat)
  5902. b1 (match-beginning 0)
  5903. e1 (match-end 0)
  5904. f1 (match-beginning 1)
  5905. lim (cond
  5906. ((org-on-heading-p) (outline-next-heading) (point))
  5907. ((org-at-item-p) (org-end-of-item) (point))
  5908. (t nil))
  5909. c-on 0 c-off 0)
  5910. (goto-char e1)
  5911. (when lim
  5912. (while (re-search-forward re-box lim t)
  5913. (if (member (match-string 2) '("[ ]" "[-]"))
  5914. (setq c-off (1+ c-off))
  5915. (setq c-on (1+ c-on))))
  5916. ; (delete-region b1 e1)
  5917. (goto-char b1)
  5918. (insert (if f1
  5919. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5920. (format "[%d/%d]" c-on (+ c-on c-off))))
  5921. (and (looking-at "\\[.*?\\]")
  5922. (replace-match ""))))
  5923. (when (interactive-p)
  5924. (message "Checkbox satistics updated %s (%d places)"
  5925. (if all "in entire file" "in current outline entry") cstat)))))
  5926. (defun org-get-checkbox-statistics-face ()
  5927. "Select the face for checkbox statistics.
  5928. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5929. it will be `org-todo'."
  5930. (if (match-end 1)
  5931. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5932. (if (and (> (match-end 2) (match-beginning 2))
  5933. (equal (match-string 2) (match-string 3)))
  5934. 'org-done
  5935. 'org-todo)))
  5936. (defun org-get-indentation (&optional line)
  5937. "Get the indentation of the current line, interpreting tabs.
  5938. When LINE is given, assume it represents a line and compute its indentation."
  5939. (if line
  5940. (if (string-match "^ *" (org-remove-tabs line))
  5941. (match-end 0))
  5942. (save-excursion
  5943. (beginning-of-line 1)
  5944. (skip-chars-forward " \t")
  5945. (current-column))))
  5946. (defun org-remove-tabs (s &optional width)
  5947. "Replace tabulators in S with spaces.
  5948. Assumes that s is a single line, starting in column 0."
  5949. (setq width (or width tab-width))
  5950. (while (string-match "\t" s)
  5951. (setq s (replace-match
  5952. (make-string
  5953. (- (* width (/ (+ (match-beginning 0) width) width))
  5954. (match-beginning 0)) ?\ )
  5955. t t s)))
  5956. s)
  5957. (defun org-fix-indentation (line ind)
  5958. "Fix indentation in LINE.
  5959. IND is a cons cell with target and minimum indentation.
  5960. If the current indenation in LINE is smaller than the minimum,
  5961. leave it alone. If it is larger than ind, set it to the target."
  5962. (let* ((l (org-remove-tabs line))
  5963. (i (org-get-indentation l))
  5964. (i1 (car ind)) (i2 (cdr ind)))
  5965. (if (>= i i2) (setq l (substring line i2)))
  5966. (if (> i1 0)
  5967. (concat (make-string i1 ?\ ) l)
  5968. l)))
  5969. (defcustom org-empty-line-terminates-plain-lists nil
  5970. "Non-nil means, an empty line ends all plain list levels.
  5971. When nil, empty lines are part of the preceeding item."
  5972. :group 'org-plain-lists
  5973. :type 'boolean)
  5974. (defun org-beginning-of-item ()
  5975. "Go to the beginning of the current hand-formatted item.
  5976. If the cursor is not in an item, throw an error."
  5977. (interactive)
  5978. (let ((pos (point))
  5979. (limit (save-excursion
  5980. (condition-case nil
  5981. (progn
  5982. (org-back-to-heading)
  5983. (beginning-of-line 2) (point))
  5984. (error (point-min)))))
  5985. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5986. ind ind1)
  5987. (if (org-at-item-p)
  5988. (beginning-of-line 1)
  5989. (beginning-of-line 1)
  5990. (skip-chars-forward " \t")
  5991. (setq ind (current-column))
  5992. (if (catch 'exit
  5993. (while t
  5994. (beginning-of-line 0)
  5995. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5996. (if (looking-at "[ \t]*$")
  5997. (setq ind1 ind-empty)
  5998. (skip-chars-forward " \t")
  5999. (setq ind1 (current-column)))
  6000. (if (< ind1 ind)
  6001. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6002. nil
  6003. (goto-char pos)
  6004. (error "Not in an item")))))
  6005. (defun org-end-of-item ()
  6006. "Go to the end of the current hand-formatted item.
  6007. If the cursor is not in an item, throw an error."
  6008. (interactive)
  6009. (let* ((pos (point))
  6010. ind1
  6011. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6012. (limit (save-excursion (outline-next-heading) (point)))
  6013. (ind (save-excursion
  6014. (org-beginning-of-item)
  6015. (skip-chars-forward " \t")
  6016. (current-column)))
  6017. (end (catch 'exit
  6018. (while t
  6019. (beginning-of-line 2)
  6020. (if (eobp) (throw 'exit (point)))
  6021. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6022. (if (looking-at "[ \t]*$")
  6023. (setq ind1 ind-empty)
  6024. (skip-chars-forward " \t")
  6025. (setq ind1 (current-column)))
  6026. (if (<= ind1 ind)
  6027. (throw 'exit (point-at-bol)))))))
  6028. (if end
  6029. (goto-char end)
  6030. (goto-char pos)
  6031. (error "Not in an item"))))
  6032. (defun org-next-item ()
  6033. "Move to the beginning of the next item in the current plain list.
  6034. Error if not at a plain list, or if this is the last item in the list."
  6035. (interactive)
  6036. (let (ind ind1 (pos (point)))
  6037. (org-beginning-of-item)
  6038. (setq ind (org-get-indentation))
  6039. (org-end-of-item)
  6040. (setq ind1 (org-get-indentation))
  6041. (unless (and (org-at-item-p) (= ind ind1))
  6042. (goto-char pos)
  6043. (error "On last item"))))
  6044. (defun org-previous-item ()
  6045. "Move to the beginning of the previous item in the current plain list.
  6046. Error if not at a plain list, or if this is the first item in the list."
  6047. (interactive)
  6048. (let (beg ind ind1 (pos (point)))
  6049. (org-beginning-of-item)
  6050. (setq beg (point))
  6051. (setq ind (org-get-indentation))
  6052. (goto-char beg)
  6053. (catch 'exit
  6054. (while t
  6055. (beginning-of-line 0)
  6056. (if (looking-at "[ \t]*$")
  6057. nil
  6058. (if (<= (setq ind1 (org-get-indentation)) ind)
  6059. (throw 'exit t)))))
  6060. (condition-case nil
  6061. (if (or (not (org-at-item-p))
  6062. (< ind1 (1- ind)))
  6063. (error "")
  6064. (org-beginning-of-item))
  6065. (error (goto-char pos)
  6066. (error "On first item")))))
  6067. (defun org-move-item-down ()
  6068. "Move the plain list item at point down, i.e. swap with following item.
  6069. Subitems (items with larger indentation) are considered part of the item,
  6070. so this really moves item trees."
  6071. (interactive)
  6072. (let (beg end ind ind1 (pos (point)) txt)
  6073. (org-beginning-of-item)
  6074. (setq beg (point))
  6075. (setq ind (org-get-indentation))
  6076. (org-end-of-item)
  6077. (setq end (point))
  6078. (setq ind1 (org-get-indentation))
  6079. (if (and (org-at-item-p) (= ind ind1))
  6080. (progn
  6081. (org-end-of-item)
  6082. (setq txt (buffer-substring beg end))
  6083. (save-excursion
  6084. (delete-region beg end))
  6085. (setq pos (point))
  6086. (insert txt)
  6087. (goto-char pos)
  6088. (org-maybe-renumber-ordered-list))
  6089. (goto-char pos)
  6090. (error "Cannot move this item further down"))))
  6091. (defun org-move-item-up (arg)
  6092. "Move the plain list item at point up, i.e. swap with previous item.
  6093. Subitems (items with larger indentation) are considered part of the item,
  6094. so this really moves item trees."
  6095. (interactive "p")
  6096. (let (beg end ind ind1 (pos (point)) txt)
  6097. (org-beginning-of-item)
  6098. (setq beg (point))
  6099. (setq ind (org-get-indentation))
  6100. (org-end-of-item)
  6101. (setq end (point))
  6102. (goto-char beg)
  6103. (catch 'exit
  6104. (while t
  6105. (beginning-of-line 0)
  6106. (if (looking-at "[ \t]*$")
  6107. (if org-empty-line-terminates-plain-lists
  6108. (progn
  6109. (goto-char pos)
  6110. (error "Cannot move this item further up"))
  6111. nil)
  6112. (if (<= (setq ind1 (org-get-indentation)) ind)
  6113. (throw 'exit t)))))
  6114. (condition-case nil
  6115. (org-beginning-of-item)
  6116. (error (goto-char beg)
  6117. (error "Cannot move this item further up")))
  6118. (setq ind1 (org-get-indentation))
  6119. (if (and (org-at-item-p) (= ind ind1))
  6120. (progn
  6121. (setq txt (buffer-substring beg end))
  6122. (save-excursion
  6123. (delete-region beg end))
  6124. (setq pos (point))
  6125. (insert txt)
  6126. (goto-char pos)
  6127. (org-maybe-renumber-ordered-list))
  6128. (goto-char pos)
  6129. (error "Cannot move this item further up"))))
  6130. (defun org-maybe-renumber-ordered-list ()
  6131. "Renumber the ordered list at point if setup allows it.
  6132. This tests the user option `org-auto-renumber-ordered-lists' before
  6133. doing the renumbering."
  6134. (interactive)
  6135. (when (and org-auto-renumber-ordered-lists
  6136. (org-at-item-p))
  6137. (if (match-beginning 3)
  6138. (org-renumber-ordered-list 1)
  6139. (org-fix-bullet-type))))
  6140. (defun org-maybe-renumber-ordered-list-safe ()
  6141. (condition-case nil
  6142. (save-excursion
  6143. (org-maybe-renumber-ordered-list))
  6144. (error nil)))
  6145. (defun org-cycle-list-bullet (&optional which)
  6146. "Cycle through the different itemize/enumerate bullets.
  6147. This cycle the entire list level through the sequence:
  6148. `-' -> `+' -> `*' -> `1.' -> `1)'
  6149. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6150. 0 meand `-', 1 means `+' etc."
  6151. (interactive "P")
  6152. (org-preserve-lc
  6153. (org-beginning-of-item-list)
  6154. (org-at-item-p)
  6155. (beginning-of-line 1)
  6156. (let ((current (match-string 0))
  6157. (prevp (eq which 'previous))
  6158. new)
  6159. (setq new (cond
  6160. ((and (numberp which)
  6161. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6162. ((string-match "-" current) (if prevp "1)" "+"))
  6163. ((string-match "\\+" current)
  6164. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6165. ((string-match "\\*" current) (if prevp "+" "1."))
  6166. ((string-match "\\." current) (if prevp "*" "1)"))
  6167. ((string-match ")" current) (if prevp "1." "-"))
  6168. (t (error "This should not happen"))))
  6169. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6170. (org-fix-bullet-type)
  6171. (org-maybe-renumber-ordered-list))))
  6172. (defun org-get-string-indentation (s)
  6173. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6174. (let ((n -1) (i 0) (w tab-width) c)
  6175. (catch 'exit
  6176. (while (< (setq n (1+ n)) (length s))
  6177. (setq c (aref s n))
  6178. (cond ((= c ?\ ) (setq i (1+ i)))
  6179. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6180. (t (throw 'exit t)))))
  6181. i))
  6182. (defun org-renumber-ordered-list (arg)
  6183. "Renumber an ordered plain list.
  6184. Cursor needs to be in the first line of an item, the line that starts
  6185. with something like \"1.\" or \"2)\"."
  6186. (interactive "p")
  6187. (unless (and (org-at-item-p)
  6188. (match-beginning 3))
  6189. (error "This is not an ordered list"))
  6190. (let ((line (org-current-line))
  6191. (col (current-column))
  6192. (ind (org-get-string-indentation
  6193. (buffer-substring (point-at-bol) (match-beginning 3))))
  6194. ;; (term (substring (match-string 3) -1))
  6195. ind1 (n (1- arg))
  6196. fmt)
  6197. ;; find where this list begins
  6198. (org-beginning-of-item-list)
  6199. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6200. (setq fmt (concat "%d" (match-string 1)))
  6201. (beginning-of-line 0)
  6202. ;; walk forward and replace these numbers
  6203. (catch 'exit
  6204. (while t
  6205. (catch 'next
  6206. (beginning-of-line 2)
  6207. (if (eobp) (throw 'exit nil))
  6208. (if (looking-at "[ \t]*$") (throw 'next nil))
  6209. (skip-chars-forward " \t") (setq ind1 (current-column))
  6210. (if (> ind1 ind) (throw 'next t))
  6211. (if (< ind1 ind) (throw 'exit t))
  6212. (if (not (org-at-item-p)) (throw 'exit nil))
  6213. (delete-region (match-beginning 2) (match-end 2))
  6214. (goto-char (match-beginning 2))
  6215. (insert (format fmt (setq n (1+ n)))))))
  6216. (goto-line line)
  6217. (move-to-column col)))
  6218. (defun org-fix-bullet-type ()
  6219. "Make sure all items in this list have the same bullet as the firsst item."
  6220. (interactive)
  6221. (unless (org-at-item-p) (error "This is not a list"))
  6222. (let ((line (org-current-line))
  6223. (col (current-column))
  6224. (ind (current-indentation))
  6225. ind1 bullet)
  6226. ;; find where this list begins
  6227. (org-beginning-of-item-list)
  6228. (beginning-of-line 1)
  6229. ;; find out what the bullet type is
  6230. (looking-at "[ \t]*\\(\\S-+\\)")
  6231. (setq bullet (match-string 1))
  6232. ;; walk forward and replace these numbers
  6233. (beginning-of-line 0)
  6234. (catch 'exit
  6235. (while t
  6236. (catch 'next
  6237. (beginning-of-line 2)
  6238. (if (eobp) (throw 'exit nil))
  6239. (if (looking-at "[ \t]*$") (throw 'next nil))
  6240. (skip-chars-forward " \t") (setq ind1 (current-column))
  6241. (if (> ind1 ind) (throw 'next t))
  6242. (if (< ind1 ind) (throw 'exit t))
  6243. (if (not (org-at-item-p)) (throw 'exit nil))
  6244. (skip-chars-forward " \t")
  6245. (looking-at "\\S-+")
  6246. (replace-match bullet))))
  6247. (goto-line line)
  6248. (move-to-column col)
  6249. (if (string-match "[0-9]" bullet)
  6250. (org-renumber-ordered-list 1))))
  6251. (defun org-beginning-of-item-list ()
  6252. "Go to the beginning of the current item list.
  6253. I.e. to the first item in this list."
  6254. (interactive)
  6255. (org-beginning-of-item)
  6256. (let ((pos (point-at-bol))
  6257. (ind (org-get-indentation))
  6258. ind1)
  6259. ;; find where this list begins
  6260. (catch 'exit
  6261. (while t
  6262. (catch 'next
  6263. (beginning-of-line 0)
  6264. (if (looking-at "[ \t]*$")
  6265. (throw (if (bobp) 'exit 'next) t))
  6266. (skip-chars-forward " \t") (setq ind1 (current-column))
  6267. (if (or (< ind1 ind)
  6268. (and (= ind1 ind)
  6269. (not (org-at-item-p)))
  6270. (bobp))
  6271. (throw 'exit t)
  6272. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6273. (goto-char pos)))
  6274. (defun org-end-of-item-list ()
  6275. "Go to the end of the current item list.
  6276. I.e. to the text after the last item."
  6277. (interactive)
  6278. (org-beginning-of-item)
  6279. (let ((pos (point-at-bol))
  6280. (ind (org-get-indentation))
  6281. ind1)
  6282. ;; find where this list begins
  6283. (catch 'exit
  6284. (while t
  6285. (catch 'next
  6286. (beginning-of-line 2)
  6287. (if (looking-at "[ \t]*$")
  6288. (throw (if (eobp) 'exit 'next) t))
  6289. (skip-chars-forward " \t") (setq ind1 (current-column))
  6290. (if (or (< ind1 ind)
  6291. (and (= ind1 ind)
  6292. (not (org-at-item-p)))
  6293. (eobp))
  6294. (progn
  6295. (setq pos (point-at-bol))
  6296. (throw 'exit t))))))
  6297. (goto-char pos)))
  6298. (defvar org-last-indent-begin-marker (make-marker))
  6299. (defvar org-last-indent-end-marker (make-marker))
  6300. (defun org-outdent-item (arg)
  6301. "Outdent a local list item."
  6302. (interactive "p")
  6303. (org-indent-item (- arg)))
  6304. (defun org-indent-item (arg)
  6305. "Indent a local list item."
  6306. (interactive "p")
  6307. (unless (org-at-item-p)
  6308. (error "Not on an item"))
  6309. (save-excursion
  6310. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6311. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6312. (setq beg org-last-indent-begin-marker
  6313. end org-last-indent-end-marker)
  6314. (org-beginning-of-item)
  6315. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6316. (org-end-of-item)
  6317. (setq end (move-marker org-last-indent-end-marker (point))))
  6318. (goto-char beg)
  6319. (setq tmp (org-item-indent-positions)
  6320. ind (car tmp)
  6321. ind-down (nth 2 tmp)
  6322. ind-up (nth 1 tmp)
  6323. delta (if (> arg 0)
  6324. (if ind-down (- ind-down ind) 2)
  6325. (if ind-up (- ind-up ind) -2)))
  6326. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6327. (while (< (point) end)
  6328. (beginning-of-line 1)
  6329. (skip-chars-forward " \t") (setq ind1 (current-column))
  6330. (delete-region (point-at-bol) (point))
  6331. (or (eolp) (indent-to-column (+ ind1 delta)))
  6332. (beginning-of-line 2))))
  6333. (org-fix-bullet-type)
  6334. (org-maybe-renumber-ordered-list-safe)
  6335. (save-excursion
  6336. (beginning-of-line 0)
  6337. (condition-case nil (org-beginning-of-item) (error nil))
  6338. (org-maybe-renumber-ordered-list-safe)))
  6339. (defun org-item-indent-positions ()
  6340. "Return indentation for plain list items.
  6341. This returns a list with three values: The current indentation, the
  6342. parent indentation and the indentation a child should habe.
  6343. Assumes cursor in item line."
  6344. (let* ((bolpos (point-at-bol))
  6345. (ind (org-get-indentation))
  6346. ind-down ind-up pos)
  6347. (save-excursion
  6348. (org-beginning-of-item-list)
  6349. (skip-chars-backward "\n\r \t")
  6350. (when (org-in-item-p)
  6351. (org-beginning-of-item)
  6352. (setq ind-up (org-get-indentation))))
  6353. (setq pos (point))
  6354. (save-excursion
  6355. (cond
  6356. ((and (condition-case nil (progn (org-previous-item) t)
  6357. (error nil))
  6358. (or (forward-char 1) t)
  6359. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6360. (setq ind-down (org-get-indentation)))
  6361. ((and (goto-char pos)
  6362. (org-at-item-p))
  6363. (goto-char (match-end 0))
  6364. (skip-chars-forward " \t")
  6365. (setq ind-down (current-column)))))
  6366. (list ind ind-up ind-down)))
  6367. ;;; The orgstruct minor mode
  6368. ;; Define a minor mode which can be used in other modes in order to
  6369. ;; integrate the org-mode structure editing commands.
  6370. ;; This is really a hack, because the org-mode structure commands use
  6371. ;; keys which normally belong to the major mode. Here is how it
  6372. ;; works: The minor mode defines all the keys necessary to operate the
  6373. ;; structure commands, but wraps the commands into a function which
  6374. ;; tests if the cursor is currently at a headline or a plain list
  6375. ;; item. If that is the case, the structure command is used,
  6376. ;; temporarily setting many Org-mode variables like regular
  6377. ;; expressions for filling etc. However, when any of those keys is
  6378. ;; used at a different location, function uses `key-binding' to look
  6379. ;; up if the key has an associated command in another currently active
  6380. ;; keymap (minor modes, major mode, global), and executes that
  6381. ;; command. There might be problems if any of the keys is otherwise
  6382. ;; used as a prefix key.
  6383. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6384. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6385. ;; addresses this by checking explicitly for both bindings.
  6386. (defvar orgstruct-mode-map (make-sparse-keymap)
  6387. "Keymap for the minor `orgstruct-mode'.")
  6388. (defvar org-local-vars nil
  6389. "List of local variables, for use by `orgstruct-mode'")
  6390. ;;;###autoload
  6391. (define-minor-mode orgstruct-mode
  6392. "Toggle the minor more `orgstruct-mode'.
  6393. This mode is for using Org-mode structure commands in other modes.
  6394. The following key behave as if Org-mode was active, if the cursor
  6395. is on a headline, or on a plain list item (both in the definition
  6396. of Org-mode).
  6397. M-up Move entry/item up
  6398. M-down Move entry/item down
  6399. M-left Promote
  6400. M-right Demote
  6401. M-S-up Move entry/item up
  6402. M-S-down Move entry/item down
  6403. M-S-left Promote subtree
  6404. M-S-right Demote subtree
  6405. M-q Fill paragraph and items like in Org-mode
  6406. C-c ^ Sort entries
  6407. C-c - Cycle list bullet
  6408. TAB Cycle item visibility
  6409. M-RET Insert new heading/item
  6410. S-M-RET Insert new TODO heading / Chekbox item
  6411. C-c C-c Set tags / toggle checkbox"
  6412. nil " OrgStruct" nil
  6413. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6414. ;;;###autoload
  6415. (defun turn-on-orgstruct ()
  6416. "Unconditionally turn on `orgstruct-mode'."
  6417. (orgstruct-mode 1))
  6418. ;;;###autoload
  6419. (defun turn-on-orgstruct++ ()
  6420. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6421. In addition to setting orgstruct-mode, this also exports all indentation and
  6422. autofilling variables from org-mode into the buffer. Note that turning
  6423. off orgstruct-mode will *not* remove these additonal settings."
  6424. (orgstruct-mode 1)
  6425. (let (var val)
  6426. (mapc
  6427. (lambda (x)
  6428. (when (string-match
  6429. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6430. (symbol-name (car x)))
  6431. (setq var (car x) val (nth 1 x))
  6432. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6433. org-local-vars)))
  6434. (defun orgstruct-error ()
  6435. "Error when there is no default binding for a structure key."
  6436. (interactive)
  6437. (error "This key is has no function outside structure elements"))
  6438. (defun orgstruct-setup ()
  6439. "Setup orgstruct keymaps."
  6440. (let ((nfunc 0)
  6441. (bindings
  6442. (list
  6443. '([(meta up)] org-metaup)
  6444. '([(meta down)] org-metadown)
  6445. '([(meta left)] org-metaleft)
  6446. '([(meta right)] org-metaright)
  6447. '([(meta shift up)] org-shiftmetaup)
  6448. '([(meta shift down)] org-shiftmetadown)
  6449. '([(meta shift left)] org-shiftmetaleft)
  6450. '([(meta shift right)] org-shiftmetaright)
  6451. '([(shift up)] org-shiftup)
  6452. '([(shift down)] org-shiftdown)
  6453. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6454. '("\M-q" fill-paragraph)
  6455. '("\C-c^" org-sort)
  6456. '("\C-c-" org-cycle-list-bullet)))
  6457. elt key fun cmd)
  6458. (while (setq elt (pop bindings))
  6459. (setq nfunc (1+ nfunc))
  6460. (setq key (org-key (car elt))
  6461. fun (nth 1 elt)
  6462. cmd (orgstruct-make-binding fun nfunc key))
  6463. (org-defkey orgstruct-mode-map key cmd))
  6464. ;; Special treatment needed for TAB and RET
  6465. (org-defkey orgstruct-mode-map [(tab)]
  6466. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6467. (org-defkey orgstruct-mode-map "\C-i"
  6468. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6469. (org-defkey orgstruct-mode-map "\M-\C-m"
  6470. (orgstruct-make-binding 'org-insert-heading 105
  6471. "\M-\C-m" [(meta return)]))
  6472. (org-defkey orgstruct-mode-map [(meta return)]
  6473. (orgstruct-make-binding 'org-insert-heading 106
  6474. [(meta return)] "\M-\C-m"))
  6475. (org-defkey orgstruct-mode-map [(shift meta return)]
  6476. (orgstruct-make-binding 'org-insert-todo-heading 107
  6477. [(meta return)] "\M-\C-m"))
  6478. (unless org-local-vars
  6479. (setq org-local-vars (org-get-local-variables)))
  6480. t))
  6481. (defun orgstruct-make-binding (fun n &rest keys)
  6482. "Create a function for binding in the structure minor mode.
  6483. FUN is the command to call inside a table. N is used to create a unique
  6484. command name. KEYS are keys that should be checked in for a command
  6485. to execute outside of tables."
  6486. (eval
  6487. (list 'defun
  6488. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6489. '(arg)
  6490. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6491. "Outside of structure, run the binding of `"
  6492. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6493. "'.")
  6494. '(interactive "p")
  6495. (list 'if
  6496. '(org-context-p 'headline 'item)
  6497. (list 'org-run-like-in-org-mode (list 'quote fun))
  6498. (list 'let '(orgstruct-mode)
  6499. (list 'call-interactively
  6500. (append '(or)
  6501. (mapcar (lambda (k)
  6502. (list 'key-binding k))
  6503. keys)
  6504. '('orgstruct-error))))))))
  6505. (defun org-context-p (&rest contexts)
  6506. "Check if local context is and of CONTEXTS.
  6507. Possible values in the list of contexts are `table', `headline', and `item'."
  6508. (let ((pos (point)))
  6509. (goto-char (point-at-bol))
  6510. (prog1 (or (and (memq 'table contexts)
  6511. (looking-at "[ \t]*|"))
  6512. (and (memq 'headline contexts)
  6513. (looking-at "\\*+"))
  6514. (and (memq 'item contexts)
  6515. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6516. (goto-char pos))))
  6517. (defun org-get-local-variables ()
  6518. "Return a list of all local variables in an org-mode buffer."
  6519. (let (varlist)
  6520. (with-current-buffer (get-buffer-create "*Org tmp*")
  6521. (erase-buffer)
  6522. (org-mode)
  6523. (setq varlist (buffer-local-variables)))
  6524. (kill-buffer "*Org tmp*")
  6525. (delq nil
  6526. (mapcar
  6527. (lambda (x)
  6528. (setq x
  6529. (if (symbolp x)
  6530. (list x)
  6531. (list (car x) (list 'quote (cdr x)))))
  6532. (if (string-match
  6533. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6534. (symbol-name (car x)))
  6535. x nil))
  6536. varlist))))
  6537. ;;;###autoload
  6538. (defun org-run-like-in-org-mode (cmd)
  6539. (unless org-local-vars
  6540. (setq org-local-vars (org-get-local-variables)))
  6541. (eval (list 'let org-local-vars
  6542. (list 'call-interactively (list 'quote cmd)))))
  6543. ;;;; Archiving
  6544. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6545. (defun org-archive-subtree (&optional find-done)
  6546. "Move the current subtree to the archive.
  6547. The archive can be a certain top-level heading in the current file, or in
  6548. a different file. The tree will be moved to that location, the subtree
  6549. heading be marked DONE, and the current time will be added.
  6550. When called with prefix argument FIND-DONE, find whole trees without any
  6551. open TODO items and archive them (after getting confirmation from the user).
  6552. If the cursor is not at a headline when this comand is called, try all level
  6553. 1 trees. If the cursor is on a headline, only try the direct children of
  6554. this heading."
  6555. (interactive "P")
  6556. (if find-done
  6557. (org-archive-all-done)
  6558. ;; Save all relevant TODO keyword-relatex variables
  6559. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6560. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6561. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6562. (tr-org-done-keywords org-done-keywords)
  6563. (tr-org-todo-regexp org-todo-regexp)
  6564. (tr-org-todo-line-regexp org-todo-line-regexp)
  6565. (tr-org-odd-levels-only org-odd-levels-only)
  6566. (this-buffer (current-buffer))
  6567. (org-archive-location org-archive-location)
  6568. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6569. ;; start of variables that will be used for saving context
  6570. ;; The compiler complains about them - keep them anyway!
  6571. (file (abbreviate-file-name (buffer-file-name)))
  6572. (time (format-time-string
  6573. (substring (cdr org-time-stamp-formats) 1 -1)
  6574. (current-time)))
  6575. afile heading buffer level newfile-p
  6576. category todo priority
  6577. ;; start of variables that will be used for savind context
  6578. ltags itags prop)
  6579. ;; Try to find a local archive location
  6580. (save-excursion
  6581. (save-restriction
  6582. (widen)
  6583. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6584. (if (and prop (string-match "\\S-" prop))
  6585. (setq org-archive-location prop)
  6586. (if (or (re-search-backward re nil t)
  6587. (re-search-forward re nil t))
  6588. (setq org-archive-location (match-string 1))))))
  6589. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6590. (progn
  6591. (setq afile (format (match-string 1 org-archive-location)
  6592. (file-name-nondirectory buffer-file-name))
  6593. heading (match-string 2 org-archive-location)))
  6594. (error "Invalid `org-archive-location'"))
  6595. (if (> (length afile) 0)
  6596. (setq newfile-p (not (file-exists-p afile))
  6597. buffer (find-file-noselect afile))
  6598. (setq buffer (current-buffer)))
  6599. (unless buffer
  6600. (error "Cannot access file \"%s\"" afile))
  6601. (if (and (> (length heading) 0)
  6602. (string-match "^\\*+" heading))
  6603. (setq level (match-end 0))
  6604. (setq heading nil level 0))
  6605. (save-excursion
  6606. (org-back-to-heading t)
  6607. ;; Get context information that will be lost by moving the tree
  6608. (org-refresh-category-properties)
  6609. (setq category (org-get-category)
  6610. todo (and (looking-at org-todo-line-regexp)
  6611. (match-string 2))
  6612. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  6613. ltags (org-get-tags)
  6614. itags (org-delete-all ltags (org-get-tags-at)))
  6615. (setq ltags (mapconcat 'identity ltags " ")
  6616. itags (mapconcat 'identity itags " "))
  6617. ;; We first only copy, in case something goes wrong
  6618. ;; we need to protect this-command, to avoid kill-region sets it,
  6619. ;; which would lead to duplication of subtrees
  6620. (let (this-command) (org-copy-subtree))
  6621. (set-buffer buffer)
  6622. ;; Enforce org-mode for the archive buffer
  6623. (if (not (org-mode-p))
  6624. ;; Force the mode for future visits.
  6625. (let ((org-insert-mode-line-in-empty-file t)
  6626. (org-inhibit-startup t))
  6627. (call-interactively 'org-mode)))
  6628. (when newfile-p
  6629. (goto-char (point-max))
  6630. (insert (format "\nArchived entries from file %s\n\n"
  6631. (buffer-file-name this-buffer))))
  6632. ;; Force the TODO keywords of the original buffer
  6633. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  6634. (org-todo-keywords-1 tr-org-todo-keywords-1)
  6635. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  6636. (org-done-keywords tr-org-done-keywords)
  6637. (org-todo-regexp tr-org-todo-regexp)
  6638. (org-todo-line-regexp tr-org-todo-line-regexp)
  6639. (org-odd-levels-only
  6640. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  6641. org-odd-levels-only
  6642. tr-org-odd-levels-only)))
  6643. (goto-char (point-min))
  6644. (if heading
  6645. (progn
  6646. (if (re-search-forward
  6647. (concat "^" (regexp-quote heading)
  6648. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  6649. nil t)
  6650. (goto-char (match-end 0))
  6651. ;; Heading not found, just insert it at the end
  6652. (goto-char (point-max))
  6653. (or (bolp) (insert "\n"))
  6654. (insert "\n" heading "\n")
  6655. (end-of-line 0))
  6656. ;; Make the subtree visible
  6657. (show-subtree)
  6658. (org-end-of-subtree t)
  6659. (skip-chars-backward " \t\r\n")
  6660. (and (looking-at "[ \t\r\n]*")
  6661. (replace-match "\n\n")))
  6662. ;; No specific heading, just go to end of file.
  6663. (goto-char (point-max)) (insert "\n"))
  6664. ;; Paste
  6665. (org-paste-subtree (org-get-legal-level level 1))
  6666. ;; Mark the entry as done
  6667. (when (and org-archive-mark-done
  6668. (looking-at org-todo-line-regexp)
  6669. (or (not (match-end 2))
  6670. (not (member (match-string 2) org-done-keywords))))
  6671. (let (org-log-done)
  6672. (org-todo
  6673. (car (or (member org-archive-mark-done org-done-keywords)
  6674. org-done-keywords)))))
  6675. ;; Add the context info
  6676. (when org-archive-save-context-info
  6677. (let ((l org-archive-save-context-info) e n v)
  6678. (while (setq e (pop l))
  6679. (when (and (setq v (symbol-value e))
  6680. (stringp v) (string-match "\\S-" v))
  6681. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  6682. (org-entry-put (point) n v)))))
  6683. ;; Save the buffer, if it is not the same buffer.
  6684. (if (not (eq this-buffer buffer)) (save-buffer))))
  6685. ;; Here we are back in the original buffer. Everything seems to have
  6686. ;; worked. So now cut the tree and finish up.
  6687. (let (this-command) (org-cut-subtree))
  6688. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  6689. (message "Subtree archived %s"
  6690. (if (eq this-buffer buffer)
  6691. (concat "under heading: " heading)
  6692. (concat "in file: " (abbreviate-file-name afile)))))))
  6693. (defun org-refresh-category-properties ()
  6694. "Refresh category text properties in teh buffer."
  6695. (let ((def-cat (cond
  6696. ((null org-category)
  6697. (if buffer-file-name
  6698. (file-name-sans-extension
  6699. (file-name-nondirectory buffer-file-name))
  6700. "???"))
  6701. ((symbolp org-category) (symbol-name org-category))
  6702. (t org-category)))
  6703. beg end cat pos optionp)
  6704. (org-unmodified
  6705. (save-excursion
  6706. (save-restriction
  6707. (widen)
  6708. (goto-char (point-min))
  6709. (put-text-property (point) (point-max) 'org-category def-cat)
  6710. (while (re-search-forward
  6711. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6712. (setq pos (match-end 0)
  6713. optionp (equal (char-after (match-beginning 0)) ?#)
  6714. cat (org-trim (match-string 2)))
  6715. (if optionp
  6716. (setq beg (point-at-bol) end (point-max))
  6717. (org-back-to-heading t)
  6718. (setq beg (point) end (org-end-of-subtree t t)))
  6719. (put-text-property beg end 'org-category cat)
  6720. (goto-char pos)))))))
  6721. (defun org-archive-all-done (&optional tag)
  6722. "Archive sublevels of the current tree without open TODO items.
  6723. If the cursor is not on a headline, try all level 1 trees. If
  6724. it is on a headline, try all direct children.
  6725. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  6726. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  6727. (rea (concat ".*:" org-archive-tag ":"))
  6728. (begm (make-marker))
  6729. (endm (make-marker))
  6730. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  6731. "Move subtree to archive (no open TODO items)? "))
  6732. beg end (cntarch 0))
  6733. (if (org-on-heading-p)
  6734. (progn
  6735. (setq re1 (concat "^" (regexp-quote
  6736. (make-string
  6737. (1+ (- (match-end 0) (match-beginning 0)))
  6738. ?*))
  6739. " "))
  6740. (move-marker begm (point))
  6741. (move-marker endm (org-end-of-subtree t)))
  6742. (setq re1 "^* ")
  6743. (move-marker begm (point-min))
  6744. (move-marker endm (point-max)))
  6745. (save-excursion
  6746. (goto-char begm)
  6747. (while (re-search-forward re1 endm t)
  6748. (setq beg (match-beginning 0)
  6749. end (save-excursion (org-end-of-subtree t) (point)))
  6750. (goto-char beg)
  6751. (if (re-search-forward re end t)
  6752. (goto-char end)
  6753. (goto-char beg)
  6754. (if (and (or (not tag) (not (looking-at rea)))
  6755. (y-or-n-p question))
  6756. (progn
  6757. (if tag
  6758. (org-toggle-tag org-archive-tag 'on)
  6759. (org-archive-subtree))
  6760. (setq cntarch (1+ cntarch)))
  6761. (goto-char end)))))
  6762. (message "%d trees archived" cntarch)))
  6763. (defun org-cycle-hide-drawers (state)
  6764. "Re-hide all drawers after a visibility state change."
  6765. (when (and (org-mode-p)
  6766. (not (memq state '(overview folded))))
  6767. (save-excursion
  6768. (let* ((globalp (memq state '(contents all)))
  6769. (beg (if globalp (point-min) (point)))
  6770. (end (if globalp (point-max) (org-end-of-subtree t))))
  6771. (goto-char beg)
  6772. (while (re-search-forward org-drawer-regexp end t)
  6773. (org-flag-drawer t))))))
  6774. (defun org-flag-drawer (flag)
  6775. (save-excursion
  6776. (beginning-of-line 1)
  6777. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6778. (let ((b (match-end 0)))
  6779. (if (re-search-forward
  6780. "^[ \t]*:END:"
  6781. (save-excursion (outline-next-heading) (point)) t)
  6782. (outline-flag-region b (point-at-eol) flag)
  6783. (error ":END: line missing"))))))
  6784. (defun org-cycle-hide-archived-subtrees (state)
  6785. "Re-hide all archived subtrees after a visibility state change."
  6786. (when (and (not org-cycle-open-archived-trees)
  6787. (not (memq state '(overview folded))))
  6788. (save-excursion
  6789. (let* ((globalp (memq state '(contents all)))
  6790. (beg (if globalp (point-min) (point)))
  6791. (end (if globalp (point-max) (org-end-of-subtree t))))
  6792. (org-hide-archived-subtrees beg end)
  6793. (goto-char beg)
  6794. (if (looking-at (concat ".*:" org-archive-tag ":"))
  6795. (message (substitute-command-keys
  6796. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  6797. (defun org-force-cycle-archived ()
  6798. "Cycle subtree even if it is archived."
  6799. (interactive)
  6800. (setq this-command 'org-cycle)
  6801. (let ((org-cycle-open-archived-trees t))
  6802. (call-interactively 'org-cycle)))
  6803. (defun org-hide-archived-subtrees (beg end)
  6804. "Re-hide all archived subtrees after a visibility state change."
  6805. (save-excursion
  6806. (let* ((re (concat ":" org-archive-tag ":")))
  6807. (goto-char beg)
  6808. (while (re-search-forward re end t)
  6809. (and (org-on-heading-p) (hide-subtree))
  6810. (org-end-of-subtree t)))))
  6811. (defun org-toggle-tag (tag &optional onoff)
  6812. "Toggle the tag TAG for the current line.
  6813. If ONOFF is `on' or `off', don't toggle but set to this state."
  6814. (unless (org-on-heading-p t) (error "Not on headling"))
  6815. (let (res current)
  6816. (save-excursion
  6817. (beginning-of-line)
  6818. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  6819. (point-at-eol) t)
  6820. (progn
  6821. (setq current (match-string 1))
  6822. (replace-match ""))
  6823. (setq current ""))
  6824. (setq current (nreverse (org-split-string current ":")))
  6825. (cond
  6826. ((eq onoff 'on)
  6827. (setq res t)
  6828. (or (member tag current) (push tag current)))
  6829. ((eq onoff 'off)
  6830. (or (not (member tag current)) (setq current (delete tag current))))
  6831. (t (if (member tag current)
  6832. (setq current (delete tag current))
  6833. (setq res t)
  6834. (push tag current))))
  6835. (end-of-line 1)
  6836. (if current
  6837. (progn
  6838. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  6839. (org-set-tags nil t))
  6840. (delete-horizontal-space))
  6841. (run-hooks 'org-after-tags-change-hook))
  6842. res))
  6843. (defun org-toggle-archive-tag (&optional arg)
  6844. "Toggle the archive tag for the current headline.
  6845. With prefix ARG, check all children of current headline and offer tagging
  6846. the children that do not contain any open TODO items."
  6847. (interactive "P")
  6848. (if arg
  6849. (org-archive-all-done 'tag)
  6850. (let (set)
  6851. (save-excursion
  6852. (org-back-to-heading t)
  6853. (setq set (org-toggle-tag org-archive-tag))
  6854. (when set (hide-subtree)))
  6855. (and set (beginning-of-line 1))
  6856. (message "Subtree %s" (if set "archived" "unarchived")))))
  6857. ;;;; Tables
  6858. ;;; The table editor
  6859. ;; Watch out: Here we are talking about two different kind of tables.
  6860. ;; Most of the code is for the tables created with the Org-mode table editor.
  6861. ;; Sometimes, we talk about tables created and edited with the table.el
  6862. ;; Emacs package. We call the former org-type tables, and the latter
  6863. ;; table.el-type tables.
  6864. (defun org-before-change-function (beg end)
  6865. "Every change indicates that a table might need an update."
  6866. (setq org-table-may-need-update t))
  6867. (defconst org-table-line-regexp "^[ \t]*|"
  6868. "Detects an org-type table line.")
  6869. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  6870. "Detects an org-type table line.")
  6871. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  6872. "Detects a table line marked for automatic recalculation.")
  6873. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  6874. "Detects a table line marked for automatic recalculation.")
  6875. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  6876. "Detects a table line marked for automatic recalculation.")
  6877. (defconst org-table-hline-regexp "^[ \t]*|-"
  6878. "Detects an org-type table hline.")
  6879. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  6880. "Detects a table-type table hline.")
  6881. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  6882. "Detects an org-type or table-type table.")
  6883. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  6884. "Searching from within a table (any type) this finds the first line
  6885. outside the table.")
  6886. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  6887. "Searching from within a table (any type) this finds the first line
  6888. outside the table.")
  6889. (defvar org-table-last-highlighted-reference nil)
  6890. (defvar org-table-formula-history nil)
  6891. (defvar org-table-column-names nil
  6892. "Alist with column names, derived from the `!' line.")
  6893. (defvar org-table-column-name-regexp nil
  6894. "Regular expression matching the current column names.")
  6895. (defvar org-table-local-parameters nil
  6896. "Alist with parameter names, derived from the `$' line.")
  6897. (defvar org-table-named-field-locations nil
  6898. "Alist with locations of named fields.")
  6899. (defvar org-table-current-line-types nil
  6900. "Table row types, non-nil only for the duration of a comand.")
  6901. (defvar org-table-current-begin-line nil
  6902. "Table begin line, non-nil only for the duration of a comand.")
  6903. (defvar org-table-current-begin-pos nil
  6904. "Table begin position, non-nil only for the duration of a comand.")
  6905. (defvar org-table-dlines nil
  6906. "Vector of data line line numbers in the current table.")
  6907. (defvar org-table-hlines nil
  6908. "Vector of hline line numbers in the current table.")
  6909. (defconst org-table-range-regexp
  6910. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  6911. ;; 1 2 3 4 5
  6912. "Regular expression for matching ranges in formulas.")
  6913. (defconst org-table-range-regexp2
  6914. (concat
  6915. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  6916. "\\.\\."
  6917. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  6918. "Match a range for reference display.")
  6919. (defconst org-table-translate-regexp
  6920. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  6921. "Match a reference that needs translation, for reference display.")
  6922. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  6923. (defun org-table-create-with-table.el ()
  6924. "Use the table.el package to insert a new table.
  6925. If there is already a table at point, convert between Org-mode tables
  6926. and table.el tables."
  6927. (interactive)
  6928. (require 'table)
  6929. (cond
  6930. ((org-at-table.el-p)
  6931. (if (y-or-n-p "Convert table to Org-mode table? ")
  6932. (org-table-convert)))
  6933. ((org-at-table-p)
  6934. (if (y-or-n-p "Convert table to table.el table? ")
  6935. (org-table-convert)))
  6936. (t (call-interactively 'table-insert))))
  6937. (defun org-table-create-or-convert-from-region (arg)
  6938. "Convert region to table, or create an empty table.
  6939. If there is an active region, convert it to a table, using the function
  6940. `org-table-convert-region'. See the documentation of that function
  6941. to learn how the prefix argument is interpreted to determine the field
  6942. separator.
  6943. If there is no such region, create an empty table with `org-table-create'."
  6944. (interactive "P")
  6945. (if (org-region-active-p)
  6946. (org-table-convert-region (region-beginning) (region-end) arg)
  6947. (org-table-create arg)))
  6948. (defun org-table-create (&optional size)
  6949. "Query for a size and insert a table skeleton.
  6950. SIZE is a string Columns x Rows like for example \"3x2\"."
  6951. (interactive "P")
  6952. (unless size
  6953. (setq size (read-string
  6954. (concat "Table size Columns x Rows [e.g. "
  6955. org-table-default-size "]: ")
  6956. "" nil org-table-default-size)))
  6957. (let* ((pos (point))
  6958. (indent (make-string (current-column) ?\ ))
  6959. (split (org-split-string size " *x *"))
  6960. (rows (string-to-number (nth 1 split)))
  6961. (columns (string-to-number (car split)))
  6962. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  6963. "\n")))
  6964. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  6965. (point-at-bol) (point)))
  6966. (beginning-of-line 1)
  6967. (newline))
  6968. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  6969. (dotimes (i rows) (insert line))
  6970. (goto-char pos)
  6971. (if (> rows 1)
  6972. ;; Insert a hline after the first row.
  6973. (progn
  6974. (end-of-line 1)
  6975. (insert "\n|-")
  6976. (goto-char pos)))
  6977. (org-table-align)))
  6978. (defun org-table-convert-region (beg0 end0 &optional separator)
  6979. "Convert region to a table.
  6980. The region goes from BEG0 to END0, but these borders will be moved
  6981. slightly, to make sure a beginning of line in the first line is included.
  6982. SEPARATOR specifies the field separator in the lines. It can have the
  6983. following values:
  6984. '(4) Use the comma as a field separator
  6985. '(16) Use a TAB as field separator
  6986. integer When a number, use that many spaces as field separator
  6987. nil When nil, the command tries to be smart and figure out the
  6988. separator in the following way:
  6989. - when each line contains a TAB, assume TAB-separated material
  6990. - when each line contains a comme, assume CSV material
  6991. - else, assume one or more SPACE charcters as separator."
  6992. (interactive "rP")
  6993. (let* ((beg (min beg0 end0))
  6994. (end (max beg0 end0))
  6995. re)
  6996. (goto-char beg)
  6997. (beginning-of-line 1)
  6998. (setq beg (move-marker (make-marker) (point)))
  6999. (goto-char end)
  7000. (if (bolp) (backward-char 1) (end-of-line 1))
  7001. (setq end (move-marker (make-marker) (point)))
  7002. ;; Get the right field separator
  7003. (unless separator
  7004. (goto-char beg)
  7005. (setq separator
  7006. (cond
  7007. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7008. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7009. (t 1))))
  7010. (setq re (cond
  7011. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7012. ((equal separator '(16)) "^\\|\t")
  7013. ((integerp separator)
  7014. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7015. (t (error "This should not happen"))))
  7016. (goto-char beg)
  7017. (while (re-search-forward re end t)
  7018. (replace-match "| " t t))
  7019. (goto-char beg)
  7020. (insert " ")
  7021. (org-table-align)))
  7022. (defun org-table-import (file arg)
  7023. "Import FILE as a table.
  7024. The file is assumed to be tab-separated. Such files can be produced by most
  7025. spreadsheet and database applications. If no tabs (at least one per line)
  7026. are found, lines will be split on whitespace into fields."
  7027. (interactive "f\nP")
  7028. (or (bolp) (newline))
  7029. (let ((beg (point))
  7030. (pm (point-max)))
  7031. (insert-file-contents file)
  7032. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7033. (defun org-table-export ()
  7034. "Export table as a tab-separated file.
  7035. Such a file can be imported into a spreadsheet program like Excel."
  7036. (interactive)
  7037. (let* ((beg (org-table-begin))
  7038. (end (org-table-end))
  7039. (table (buffer-substring beg end))
  7040. (file (read-file-name "Export table to: "))
  7041. buf)
  7042. (unless (or (not (file-exists-p file))
  7043. (y-or-n-p (format "Overwrite file %s? " file)))
  7044. (error "Abort"))
  7045. (with-current-buffer (find-file-noselect file)
  7046. (setq buf (current-buffer))
  7047. (erase-buffer)
  7048. (fundamental-mode)
  7049. (insert table)
  7050. (goto-char (point-min))
  7051. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7052. (replace-match "" t t)
  7053. (end-of-line 1))
  7054. (goto-char (point-min))
  7055. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7056. (replace-match "" t t)
  7057. (goto-char (min (1+ (point)) (point-max))))
  7058. (goto-char (point-min))
  7059. (while (re-search-forward "^-[-+]*$" nil t)
  7060. (replace-match "")
  7061. (if (looking-at "\n")
  7062. (delete-char 1)))
  7063. (goto-char (point-min))
  7064. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7065. (replace-match "\t" t t))
  7066. (save-buffer))
  7067. (kill-buffer buf)))
  7068. (defvar org-table-aligned-begin-marker (make-marker)
  7069. "Marker at the beginning of the table last aligned.
  7070. Used to check if cursor still is in that table, to minimize realignment.")
  7071. (defvar org-table-aligned-end-marker (make-marker)
  7072. "Marker at the end of the table last aligned.
  7073. Used to check if cursor still is in that table, to minimize realignment.")
  7074. (defvar org-table-last-alignment nil
  7075. "List of flags for flushright alignment, from the last re-alignment.
  7076. This is being used to correctly align a single field after TAB or RET.")
  7077. (defvar org-table-last-column-widths nil
  7078. "List of max width of fields in each column.
  7079. This is being used to correctly align a single field after TAB or RET.")
  7080. (defvar org-table-overlay-coordinates nil
  7081. "Overlay coordinates after each align of a table.")
  7082. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7083. (defvar org-last-recalc-line nil)
  7084. (defconst org-narrow-column-arrow "=>"
  7085. "Used as display property in narrowed table columns.")
  7086. (defun org-table-align ()
  7087. "Align the table at point by aligning all vertical bars."
  7088. (interactive)
  7089. (let* (
  7090. ;; Limits of table
  7091. (beg (org-table-begin))
  7092. (end (org-table-end))
  7093. ;; Current cursor position
  7094. (linepos (org-current-line))
  7095. (colpos (org-table-current-column))
  7096. (winstart (window-start))
  7097. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7098. lines (new "") lengths l typenums ty fields maxfields i
  7099. column
  7100. (indent "") cnt frac
  7101. rfmt hfmt
  7102. (spaces '(1 . 1))
  7103. (sp1 (car spaces))
  7104. (sp2 (cdr spaces))
  7105. (rfmt1 (concat
  7106. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7107. (hfmt1 (concat
  7108. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7109. emptystrings links dates emph narrow fmax f1 len c e)
  7110. (untabify beg end)
  7111. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7112. ;; Check if we have links or dates
  7113. (goto-char beg)
  7114. (setq links (re-search-forward org-bracket-link-regexp end t))
  7115. (goto-char beg)
  7116. (setq emph (and org-hide-emphasis-markers
  7117. (re-search-forward org-emph-re end t)))
  7118. (goto-char beg)
  7119. (setq dates (and org-display-custom-times
  7120. (re-search-forward org-ts-regexp-both end t)))
  7121. ;; Make sure the link properties are right
  7122. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7123. ;; Make sure the date properties are right
  7124. (when dates (goto-char beg) (while (org-activate-dates end)))
  7125. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7126. ;; Check if we are narrowing any columns
  7127. (goto-char beg)
  7128. (setq narrow (and org-format-transports-properties-p
  7129. (re-search-forward "<[0-9]+>" end t)))
  7130. ;; Get the rows
  7131. (setq lines (org-split-string
  7132. (buffer-substring beg end) "\n"))
  7133. ;; Store the indentation of the first line
  7134. (if (string-match "^ *" (car lines))
  7135. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7136. ;; Mark the hlines by setting the corresponding element to nil
  7137. ;; At the same time, we remove trailing space.
  7138. (setq lines (mapcar (lambda (l)
  7139. (if (string-match "^ *|-" l)
  7140. nil
  7141. (if (string-match "[ \t]+$" l)
  7142. (substring l 0 (match-beginning 0))
  7143. l)))
  7144. lines))
  7145. ;; Get the data fields by splitting the lines.
  7146. (setq fields (mapcar
  7147. (lambda (l)
  7148. (org-split-string l " *| *"))
  7149. (delq nil (copy-sequence lines))))
  7150. ;; How many fields in the longest line?
  7151. (condition-case nil
  7152. (setq maxfields (apply 'max (mapcar 'length fields)))
  7153. (error
  7154. (kill-region beg end)
  7155. (org-table-create org-table-default-size)
  7156. (error "Empty table - created default table")))
  7157. ;; A list of empty strings to fill any short rows on output
  7158. (setq emptystrings (make-list maxfields ""))
  7159. ;; Check for special formatting.
  7160. (setq i -1)
  7161. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7162. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7163. ;; Check if there is an explicit width specified
  7164. (when narrow
  7165. (setq c column fmax nil)
  7166. (while c
  7167. (setq e (pop c))
  7168. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7169. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7170. ;; Find fields that are wider than fmax, and shorten them
  7171. (when fmax
  7172. (loop for xx in column do
  7173. (when (and (stringp xx)
  7174. (> (org-string-width xx) fmax))
  7175. (org-add-props xx nil
  7176. 'help-echo
  7177. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7178. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7179. (unless (> f1 1)
  7180. (error "Cannot narrow field starting with wide link \"%s\""
  7181. (match-string 0 xx)))
  7182. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7183. (add-text-properties (- f1 2) f1
  7184. (list 'display org-narrow-column-arrow)
  7185. xx)))))
  7186. ;; Get the maximum width for each column
  7187. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7188. ;; Get the fraction of numbers, to decide about alignment of the column
  7189. (setq cnt 0 frac 0.0)
  7190. (loop for x in column do
  7191. (if (equal x "")
  7192. nil
  7193. (setq frac ( / (+ (* frac cnt)
  7194. (if (string-match org-table-number-regexp x) 1 0))
  7195. (setq cnt (1+ cnt))))))
  7196. (push (>= frac org-table-number-fraction) typenums))
  7197. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7198. ;; Store the alignment of this table, for later editing of single fields
  7199. (setq org-table-last-alignment typenums
  7200. org-table-last-column-widths lengths)
  7201. ;; With invisible characters, `format' does not get the field width right
  7202. ;; So we need to make these fields wide by hand.
  7203. (when (or links emph)
  7204. (loop for i from 0 upto (1- maxfields) do
  7205. (setq len (nth i lengths))
  7206. (loop for j from 0 upto (1- (length fields)) do
  7207. (setq c (nthcdr i (car (nthcdr j fields))))
  7208. (if (and (stringp (car c))
  7209. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7210. ; (string-match org-bracket-link-regexp (car c))
  7211. (< (org-string-width (car c)) len))
  7212. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7213. ;; Compute the formats needed for output of the table
  7214. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7215. (while (setq l (pop lengths))
  7216. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7217. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7218. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7219. (setq rfmt (concat rfmt "\n")
  7220. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7221. (setq new (mapconcat
  7222. (lambda (l)
  7223. (if l (apply 'format rfmt
  7224. (append (pop fields) emptystrings))
  7225. hfmt))
  7226. lines ""))
  7227. ;; Replace the old one
  7228. (delete-region beg end)
  7229. (move-marker end nil)
  7230. (move-marker org-table-aligned-begin-marker (point))
  7231. (insert new)
  7232. (move-marker org-table-aligned-end-marker (point))
  7233. (when (and orgtbl-mode (not (org-mode-p)))
  7234. (goto-char org-table-aligned-begin-marker)
  7235. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7236. ;; Try to move to the old location
  7237. (goto-line winstartline)
  7238. (setq winstart (point-at-bol))
  7239. (goto-line linepos)
  7240. (set-window-start (selected-window) winstart 'noforce)
  7241. (org-table-goto-column colpos)
  7242. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7243. (setq org-table-may-need-update nil)
  7244. ))
  7245. (defun org-string-width (s)
  7246. "Compute width of string, ignoring invisible characters.
  7247. This ignores character with invisibility property `org-link', and also
  7248. characters with property `org-cwidth', because these will become invisible
  7249. upon the next fontification round."
  7250. (let (b l)
  7251. (when (or (eq t buffer-invisibility-spec)
  7252. (assq 'org-link buffer-invisibility-spec))
  7253. (while (setq b (text-property-any 0 (length s)
  7254. 'invisible 'org-link s))
  7255. (setq s (concat (substring s 0 b)
  7256. (substring s (or (next-single-property-change
  7257. b 'invisible s) (length s)))))))
  7258. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7259. (setq s (concat (substring s 0 b)
  7260. (substring s (or (next-single-property-change
  7261. b 'org-cwidth s) (length s))))))
  7262. (setq l (string-width s) b -1)
  7263. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7264. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7265. l))
  7266. (defun org-table-begin (&optional table-type)
  7267. "Find the beginning of the table and return its position.
  7268. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7269. (save-excursion
  7270. (if (not (re-search-backward
  7271. (if table-type org-table-any-border-regexp
  7272. org-table-border-regexp)
  7273. nil t))
  7274. (progn (goto-char (point-min)) (point))
  7275. (goto-char (match-beginning 0))
  7276. (beginning-of-line 2)
  7277. (point))))
  7278. (defun org-table-end (&optional table-type)
  7279. "Find the end of the table and return its position.
  7280. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7281. (save-excursion
  7282. (if (not (re-search-forward
  7283. (if table-type org-table-any-border-regexp
  7284. org-table-border-regexp)
  7285. nil t))
  7286. (goto-char (point-max))
  7287. (goto-char (match-beginning 0)))
  7288. (point-marker)))
  7289. (defun org-table-justify-field-maybe (&optional new)
  7290. "Justify the current field, text to left, number to right.
  7291. Optional argument NEW may specify text to replace the current field content."
  7292. (cond
  7293. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7294. ((org-at-table-hline-p))
  7295. ((and (not new)
  7296. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7297. (current-buffer)))
  7298. (< (point) org-table-aligned-begin-marker)
  7299. (>= (point) org-table-aligned-end-marker)))
  7300. ;; This is not the same table, force a full re-align
  7301. (setq org-table-may-need-update t))
  7302. (t ;; realign the current field, based on previous full realign
  7303. (let* ((pos (point)) s
  7304. (col (org-table-current-column))
  7305. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7306. l f n o e)
  7307. (when (> col 0)
  7308. (skip-chars-backward "^|\n")
  7309. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7310. (progn
  7311. (setq s (match-string 1)
  7312. o (match-string 0)
  7313. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7314. e (not (= (match-beginning 2) (match-end 2))))
  7315. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7316. l (if e "|" (setq org-table-may-need-update t) ""))
  7317. n (format f s))
  7318. (if new
  7319. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7320. (setq n (format f new))
  7321. (setq n (concat new "|") org-table-may-need-update t)))
  7322. (or (equal n o)
  7323. (let (org-table-may-need-update)
  7324. (replace-match n t t))))
  7325. (setq org-table-may-need-update t))
  7326. (goto-char pos))))))
  7327. (defun org-table-next-field ()
  7328. "Go to the next field in the current table, creating new lines as needed.
  7329. Before doing so, re-align the table if necessary."
  7330. (interactive)
  7331. (org-table-maybe-eval-formula)
  7332. (org-table-maybe-recalculate-line)
  7333. (if (and org-table-automatic-realign
  7334. org-table-may-need-update)
  7335. (org-table-align))
  7336. (let ((end (org-table-end)))
  7337. (if (org-at-table-hline-p)
  7338. (end-of-line 1))
  7339. (condition-case nil
  7340. (progn
  7341. (re-search-forward "|" end)
  7342. (if (looking-at "[ \t]*$")
  7343. (re-search-forward "|" end))
  7344. (if (and (looking-at "-")
  7345. org-table-tab-jumps-over-hlines
  7346. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7347. (goto-char (match-beginning 1)))
  7348. (if (looking-at "-")
  7349. (progn
  7350. (beginning-of-line 0)
  7351. (org-table-insert-row 'below))
  7352. (if (looking-at " ") (forward-char 1))))
  7353. (error
  7354. (org-table-insert-row 'below)))))
  7355. (defun org-table-previous-field ()
  7356. "Go to the previous field in the table.
  7357. Before doing so, re-align the table if necessary."
  7358. (interactive)
  7359. (org-table-justify-field-maybe)
  7360. (org-table-maybe-recalculate-line)
  7361. (if (and org-table-automatic-realign
  7362. org-table-may-need-update)
  7363. (org-table-align))
  7364. (if (org-at-table-hline-p)
  7365. (end-of-line 1))
  7366. (re-search-backward "|" (org-table-begin))
  7367. (re-search-backward "|" (org-table-begin))
  7368. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7369. (re-search-backward "|" (org-table-begin)))
  7370. (if (looking-at "| ?")
  7371. (goto-char (match-end 0))))
  7372. (defun org-table-next-row ()
  7373. "Go to the next row (same column) in the current table.
  7374. Before doing so, re-align the table if necessary."
  7375. (interactive)
  7376. (org-table-maybe-eval-formula)
  7377. (org-table-maybe-recalculate-line)
  7378. (if (or (looking-at "[ \t]*$")
  7379. (save-excursion (skip-chars-backward " \t") (bolp)))
  7380. (newline)
  7381. (if (and org-table-automatic-realign
  7382. org-table-may-need-update)
  7383. (org-table-align))
  7384. (let ((col (org-table-current-column)))
  7385. (beginning-of-line 2)
  7386. (if (or (not (org-at-table-p))
  7387. (org-at-table-hline-p))
  7388. (progn
  7389. (beginning-of-line 0)
  7390. (org-table-insert-row 'below)))
  7391. (org-table-goto-column col)
  7392. (skip-chars-backward "^|\n\r")
  7393. (if (looking-at " ") (forward-char 1)))))
  7394. (defun org-table-copy-down (n)
  7395. "Copy a field down in the current column.
  7396. If the field at the cursor is empty, copy into it the content of the nearest
  7397. non-empty field above. With argument N, use the Nth non-empty field.
  7398. If the current field is not empty, it is copied down to the next row, and
  7399. the cursor is moved with it. Therefore, repeating this command causes the
  7400. column to be filled row-by-row.
  7401. If the variable `org-table-copy-increment' is non-nil and the field is an
  7402. integer or a timestamp, it will be incremented while copying. In the case of
  7403. a timestamp, if the cursor is on the year, change the year. If it is on the
  7404. month or the day, change that. Point will stay on the current date field
  7405. in order to easily repeat the interval."
  7406. (interactive "p")
  7407. (let* ((colpos (org-table-current-column))
  7408. (col (current-column))
  7409. (field (org-table-get-field))
  7410. (non-empty (string-match "[^ \t]" field))
  7411. (beg (org-table-begin))
  7412. txt)
  7413. (org-table-check-inside-data-field)
  7414. (if non-empty
  7415. (progn
  7416. (setq txt (org-trim field))
  7417. (org-table-next-row)
  7418. (org-table-blank-field))
  7419. (save-excursion
  7420. (setq txt
  7421. (catch 'exit
  7422. (while (progn (beginning-of-line 1)
  7423. (re-search-backward org-table-dataline-regexp
  7424. beg t))
  7425. (org-table-goto-column colpos t)
  7426. (if (and (looking-at
  7427. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7428. (= (setq n (1- n)) 0))
  7429. (throw 'exit (match-string 1))))))))
  7430. (if txt
  7431. (progn
  7432. (if (and org-table-copy-increment
  7433. (string-match "^[0-9]+$" txt))
  7434. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7435. (insert txt)
  7436. (move-to-column col)
  7437. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7438. (org-timestamp-up 1)
  7439. (org-table-maybe-recalculate-line))
  7440. (org-table-align)
  7441. (move-to-column col))
  7442. (error "No non-empty field found"))))
  7443. (defun org-table-check-inside-data-field ()
  7444. "Is point inside a table data field?
  7445. I.e. not on a hline or before the first or after the last column?
  7446. This actually throws an error, so it aborts the current command."
  7447. (if (or (not (org-at-table-p))
  7448. (= (org-table-current-column) 0)
  7449. (org-at-table-hline-p)
  7450. (looking-at "[ \t]*$"))
  7451. (error "Not in table data field")))
  7452. (defvar org-table-clip nil
  7453. "Clipboard for table regions.")
  7454. (defun org-table-blank-field ()
  7455. "Blank the current table field or active region."
  7456. (interactive)
  7457. (org-table-check-inside-data-field)
  7458. (if (and (interactive-p) (org-region-active-p))
  7459. (let (org-table-clip)
  7460. (org-table-cut-region (region-beginning) (region-end)))
  7461. (skip-chars-backward "^|")
  7462. (backward-char 1)
  7463. (if (looking-at "|[^|\n]+")
  7464. (let* ((pos (match-beginning 0))
  7465. (match (match-string 0))
  7466. (len (org-string-width match)))
  7467. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7468. (goto-char (+ 2 pos))
  7469. (substring match 1)))))
  7470. (defun org-table-get-field (&optional n replace)
  7471. "Return the value of the field in column N of current row.
  7472. N defaults to current field.
  7473. If REPLACE is a string, replace field with this value. The return value
  7474. is always the old value."
  7475. (and n (org-table-goto-column n))
  7476. (skip-chars-backward "^|\n")
  7477. (backward-char 1)
  7478. (if (looking-at "|[^|\r\n]*")
  7479. (let* ((pos (match-beginning 0))
  7480. (val (buffer-substring (1+ pos) (match-end 0))))
  7481. (if replace
  7482. (replace-match (concat "|" replace) t t))
  7483. (goto-char (min (point-at-eol) (+ 2 pos)))
  7484. val)
  7485. (forward-char 1) ""))
  7486. (defun org-table-field-info (arg)
  7487. "Show info about the current field, and highlight any reference at point."
  7488. (interactive "P")
  7489. (org-table-get-specials)
  7490. (save-excursion
  7491. (let* ((pos (point))
  7492. (col (org-table-current-column))
  7493. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7494. (name (car (rassoc (list (org-current-line) col)
  7495. org-table-named-field-locations)))
  7496. (eql (org-table-get-stored-formulas))
  7497. (dline (org-table-current-dline))
  7498. (ref (format "@%d$%d" dline col))
  7499. (ref1 (org-table-convert-refs-to-an ref))
  7500. (fequation (or (assoc name eql) (assoc ref eql)))
  7501. (cequation (assoc (int-to-string col) eql))
  7502. (eqn (or fequation cequation)))
  7503. (goto-char pos)
  7504. (condition-case nil
  7505. (org-table-show-reference 'local)
  7506. (error nil))
  7507. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7508. dline col
  7509. (if cname (concat " or $" cname) "")
  7510. dline col ref1
  7511. (if name (concat " or $" name) "")
  7512. ;; FIXME: formula info not correct if special table line
  7513. (if eqn
  7514. (concat ", formula: "
  7515. (org-table-formula-to-user
  7516. (concat
  7517. (if (string-match "^[$@]"(car eqn)) "" "$")
  7518. (car eqn) "=" (cdr eqn))))
  7519. "")))))
  7520. (defun org-table-current-column ()
  7521. "Find out which column we are in.
  7522. When called interactively, column is also displayed in echo area."
  7523. (interactive)
  7524. (if (interactive-p) (org-table-check-inside-data-field))
  7525. (save-excursion
  7526. (let ((cnt 0) (pos (point)))
  7527. (beginning-of-line 1)
  7528. (while (search-forward "|" pos t)
  7529. (setq cnt (1+ cnt)))
  7530. (if (interactive-p) (message "This is table column %d" cnt))
  7531. cnt)))
  7532. (defun org-table-current-dline ()
  7533. "Find out what table data line we are in.
  7534. Only datalins count for this."
  7535. (interactive)
  7536. (if (interactive-p) (org-table-check-inside-data-field))
  7537. (save-excursion
  7538. (let ((cnt 0) (pos (point)))
  7539. (goto-char (org-table-begin))
  7540. (while (<= (point) pos)
  7541. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7542. (beginning-of-line 2))
  7543. (if (interactive-p) (message "This is table line %d" cnt))
  7544. cnt)))
  7545. (defun org-table-goto-column (n &optional on-delim force)
  7546. "Move the cursor to the Nth column in the current table line.
  7547. With optional argument ON-DELIM, stop with point before the left delimiter
  7548. of the field.
  7549. If there are less than N fields, just go to after the last delimiter.
  7550. However, when FORCE is non-nil, create new columns if necessary."
  7551. (interactive "p")
  7552. (let ((pos (point-at-eol)))
  7553. (beginning-of-line 1)
  7554. (when (> n 0)
  7555. (while (and (> (setq n (1- n)) -1)
  7556. (or (search-forward "|" pos t)
  7557. (and force
  7558. (progn (end-of-line 1)
  7559. (skip-chars-backward "^|")
  7560. (insert " | "))))))
  7561. ; (backward-char 2) t)))))
  7562. (when (and force (not (looking-at ".*|")))
  7563. (save-excursion (end-of-line 1) (insert " | ")))
  7564. (if on-delim
  7565. (backward-char 1)
  7566. (if (looking-at " ") (forward-char 1))))))
  7567. (defun org-at-table-p (&optional table-type)
  7568. "Return t if the cursor is inside an org-type table.
  7569. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7570. (if org-enable-table-editor
  7571. (save-excursion
  7572. (beginning-of-line 1)
  7573. (looking-at (if table-type org-table-any-line-regexp
  7574. org-table-line-regexp)))
  7575. nil))
  7576. (defun org-at-table.el-p ()
  7577. "Return t if and only if we are at a table.el table."
  7578. (and (org-at-table-p 'any)
  7579. (save-excursion
  7580. (goto-char (org-table-begin 'any))
  7581. (looking-at org-table1-hline-regexp))))
  7582. (defun org-table-recognize-table.el ()
  7583. "If there is a table.el table nearby, recognize it and move into it."
  7584. (if org-table-tab-recognizes-table.el
  7585. (if (org-at-table.el-p)
  7586. (progn
  7587. (beginning-of-line 1)
  7588. (if (looking-at org-table-dataline-regexp)
  7589. nil
  7590. (if (looking-at org-table1-hline-regexp)
  7591. (progn
  7592. (beginning-of-line 2)
  7593. (if (looking-at org-table-any-border-regexp)
  7594. (beginning-of-line -1)))))
  7595. (if (re-search-forward "|" (org-table-end t) t)
  7596. (progn
  7597. (require 'table)
  7598. (if (table--at-cell-p (point))
  7599. t
  7600. (message "recognizing table.el table...")
  7601. (table-recognize-table)
  7602. (message "recognizing table.el table...done")))
  7603. (error "This should not happen..."))
  7604. t)
  7605. nil)
  7606. nil))
  7607. (defun org-at-table-hline-p ()
  7608. "Return t if the cursor is inside a hline in a table."
  7609. (if org-enable-table-editor
  7610. (save-excursion
  7611. (beginning-of-line 1)
  7612. (looking-at org-table-hline-regexp))
  7613. nil))
  7614. (defun org-table-insert-column ()
  7615. "Insert a new column into the table."
  7616. (interactive)
  7617. (if (not (org-at-table-p))
  7618. (error "Not at a table"))
  7619. (org-table-find-dataline)
  7620. (let* ((col (max 1 (org-table-current-column)))
  7621. (beg (org-table-begin))
  7622. (end (org-table-end))
  7623. ;; Current cursor position
  7624. (linepos (org-current-line))
  7625. (colpos col))
  7626. (goto-char beg)
  7627. (while (< (point) end)
  7628. (if (org-at-table-hline-p)
  7629. nil
  7630. (org-table-goto-column col t)
  7631. (insert "| "))
  7632. (beginning-of-line 2))
  7633. (move-marker end nil)
  7634. (goto-line linepos)
  7635. (org-table-goto-column colpos)
  7636. (org-table-align)
  7637. (org-table-fix-formulas "$" nil (1- col) 1)))
  7638. (defun org-table-find-dataline ()
  7639. "Find a dataline in the current table, which is needed for column commands."
  7640. (if (and (org-at-table-p)
  7641. (not (org-at-table-hline-p)))
  7642. t
  7643. (let ((col (current-column))
  7644. (end (org-table-end)))
  7645. (move-to-column col)
  7646. (while (and (< (point) end)
  7647. (or (not (= (current-column) col))
  7648. (org-at-table-hline-p)))
  7649. (beginning-of-line 2)
  7650. (move-to-column col))
  7651. (if (and (org-at-table-p)
  7652. (not (org-at-table-hline-p)))
  7653. t
  7654. (error
  7655. "Please position cursor in a data line for column operations")))))
  7656. (defun org-table-delete-column ()
  7657. "Delete a column from the table."
  7658. (interactive)
  7659. (if (not (org-at-table-p))
  7660. (error "Not at a table"))
  7661. (org-table-find-dataline)
  7662. (org-table-check-inside-data-field)
  7663. (let* ((col (org-table-current-column))
  7664. (beg (org-table-begin))
  7665. (end (org-table-end))
  7666. ;; Current cursor position
  7667. (linepos (org-current-line))
  7668. (colpos col))
  7669. (goto-char beg)
  7670. (while (< (point) end)
  7671. (if (org-at-table-hline-p)
  7672. nil
  7673. (org-table-goto-column col t)
  7674. (and (looking-at "|[^|\n]+|")
  7675. (replace-match "|")))
  7676. (beginning-of-line 2))
  7677. (move-marker end nil)
  7678. (goto-line linepos)
  7679. (org-table-goto-column colpos)
  7680. (org-table-align)
  7681. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  7682. col -1 col)))
  7683. (defun org-table-move-column-right ()
  7684. "Move column to the right."
  7685. (interactive)
  7686. (org-table-move-column nil))
  7687. (defun org-table-move-column-left ()
  7688. "Move column to the left."
  7689. (interactive)
  7690. (org-table-move-column 'left))
  7691. (defun org-table-move-column (&optional left)
  7692. "Move the current column to the right. With arg LEFT, move to the left."
  7693. (interactive "P")
  7694. (if (not (org-at-table-p))
  7695. (error "Not at a table"))
  7696. (org-table-find-dataline)
  7697. (org-table-check-inside-data-field)
  7698. (let* ((col (org-table-current-column))
  7699. (col1 (if left (1- col) col))
  7700. (beg (org-table-begin))
  7701. (end (org-table-end))
  7702. ;; Current cursor position
  7703. (linepos (org-current-line))
  7704. (colpos (if left (1- col) (1+ col))))
  7705. (if (and left (= col 1))
  7706. (error "Cannot move column further left"))
  7707. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  7708. (error "Cannot move column further right"))
  7709. (goto-char beg)
  7710. (while (< (point) end)
  7711. (if (org-at-table-hline-p)
  7712. nil
  7713. (org-table-goto-column col1 t)
  7714. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  7715. (replace-match "|\\2|\\1|")))
  7716. (beginning-of-line 2))
  7717. (move-marker end nil)
  7718. (goto-line linepos)
  7719. (org-table-goto-column colpos)
  7720. (org-table-align)
  7721. (org-table-fix-formulas
  7722. "$" (list (cons (number-to-string col) (number-to-string colpos))
  7723. (cons (number-to-string colpos) (number-to-string col))))))
  7724. (defun org-table-move-row-down ()
  7725. "Move table row down."
  7726. (interactive)
  7727. (org-table-move-row nil))
  7728. (defun org-table-move-row-up ()
  7729. "Move table row up."
  7730. (interactive)
  7731. (org-table-move-row 'up))
  7732. (defun org-table-move-row (&optional up)
  7733. "Move the current table line down. With arg UP, move it up."
  7734. (interactive "P")
  7735. (let* ((col (current-column))
  7736. (pos (point))
  7737. (hline1p (save-excursion (beginning-of-line 1)
  7738. (looking-at org-table-hline-regexp)))
  7739. (dline1 (org-table-current-dline))
  7740. (dline2 (+ dline1 (if up -1 1)))
  7741. (tonew (if up 0 2))
  7742. txt hline2p)
  7743. (beginning-of-line tonew)
  7744. (unless (org-at-table-p)
  7745. (goto-char pos)
  7746. (error "Cannot move row further"))
  7747. (setq hline2p (looking-at org-table-hline-regexp))
  7748. (goto-char pos)
  7749. (beginning-of-line 1)
  7750. (setq pos (point))
  7751. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  7752. (delete-region (point) (1+ (point-at-eol)))
  7753. (beginning-of-line tonew)
  7754. (insert txt)
  7755. (beginning-of-line 0)
  7756. (move-to-column col)
  7757. (unless (or hline1p hline2p)
  7758. (org-table-fix-formulas
  7759. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  7760. (cons (number-to-string dline2) (number-to-string dline1)))))))
  7761. (defun org-table-insert-row (&optional arg)
  7762. "Insert a new row above the current line into the table.
  7763. With prefix ARG, insert below the current line."
  7764. (interactive "P")
  7765. (if (not (org-at-table-p))
  7766. (error "Not at a table"))
  7767. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  7768. (new (org-table-clean-line line)))
  7769. ;; Fix the first field if necessary
  7770. (if (string-match "^[ \t]*| *[#$] *|" line)
  7771. (setq new (replace-match (match-string 0 line) t t new)))
  7772. (beginning-of-line (if arg 2 1))
  7773. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  7774. (beginning-of-line 0)
  7775. (re-search-forward "| ?" (point-at-eol) t)
  7776. (and (or org-table-may-need-update org-table-overlay-coordinates)
  7777. (org-table-align))
  7778. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  7779. (defun org-table-insert-hline (&optional above)
  7780. "Insert a horizontal-line below the current line into the table.
  7781. With prefix ABOVE, insert above the current line."
  7782. (interactive "P")
  7783. (if (not (org-at-table-p))
  7784. (error "Not at a table"))
  7785. (let ((line (org-table-clean-line
  7786. (buffer-substring (point-at-bol) (point-at-eol))))
  7787. (col (current-column)))
  7788. (while (string-match "|\\( +\\)|" line)
  7789. (setq line (replace-match
  7790. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  7791. ?-) "|") t t line)))
  7792. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  7793. (beginning-of-line (if above 1 2))
  7794. (insert line "\n")
  7795. (beginning-of-line (if above 1 -1))
  7796. (move-to-column col)
  7797. (and org-table-overlay-coordinates (org-table-align))))
  7798. (defun org-table-hline-and-move (&optional same-column)
  7799. "Insert a hline and move to the row below that line."
  7800. (interactive "P")
  7801. (let ((col (org-table-current-column)))
  7802. (org-table-maybe-eval-formula)
  7803. (org-table-maybe-recalculate-line)
  7804. (org-table-insert-hline)
  7805. (end-of-line 2)
  7806. (if (looking-at "\n[ \t]*|-")
  7807. (progn (insert "\n|") (org-table-align))
  7808. (org-table-next-field))
  7809. (if same-column (org-table-goto-column col))))
  7810. (defun org-table-clean-line (s)
  7811. "Convert a table line S into a string with only \"|\" and space.
  7812. In particular, this does handle wide and invisible characters."
  7813. (if (string-match "^[ \t]*|-" s)
  7814. ;; It's a hline, just map the characters
  7815. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  7816. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  7817. (setq s (replace-match
  7818. (concat "|" (make-string (org-string-width (match-string 1 s))
  7819. ?\ ) "|")
  7820. t t s)))
  7821. s))
  7822. (defun org-table-kill-row ()
  7823. "Delete the current row or horizontal line from the table."
  7824. (interactive)
  7825. (if (not (org-at-table-p))
  7826. (error "Not at a table"))
  7827. (let ((col (current-column))
  7828. (dline (org-table-current-dline)))
  7829. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  7830. (if (not (org-at-table-p)) (beginning-of-line 0))
  7831. (move-to-column col)
  7832. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  7833. dline -1 dline)))
  7834. (defun org-table-sort-lines (with-case &optional sorting-type)
  7835. "Sort table lines according to the column at point.
  7836. The position of point indicates the column to be used for
  7837. sorting, and the range of lines is the range between the nearest
  7838. horizontal separator lines, or the entire table of no such lines
  7839. exist. If point is before the first column, you will be prompted
  7840. for the sorting column. If there is an active region, the mark
  7841. specifies the first line and the sorting column, while point
  7842. should be in the last line to be included into the sorting.
  7843. The command then prompts for the sorting type which can be
  7844. alphabetically, numerically, or by time (as given in a time stamp
  7845. in the field). Sorting in reverse order is also possible.
  7846. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  7847. If SORTING-TYPE is specified when this function is called from a Lisp
  7848. program, no prompting will take place. SORTING-TYPE must be a character,
  7849. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  7850. should be done in reverse order."
  7851. (interactive "P")
  7852. (let* ((thisline (org-current-line))
  7853. (thiscol (org-table-current-column))
  7854. beg end bcol ecol tend tbeg column lns pos)
  7855. (when (equal thiscol 0)
  7856. (if (interactive-p)
  7857. (setq thiscol
  7858. (string-to-number
  7859. (read-string "Use column N for sorting: ")))
  7860. (setq thiscol 1))
  7861. (org-table-goto-column thiscol))
  7862. (org-table-check-inside-data-field)
  7863. (if (org-region-active-p)
  7864. (progn
  7865. (setq beg (region-beginning) end (region-end))
  7866. (goto-char beg)
  7867. (setq column (org-table-current-column)
  7868. beg (point-at-bol))
  7869. (goto-char end)
  7870. (setq end (point-at-bol 2)))
  7871. (setq column (org-table-current-column)
  7872. pos (point)
  7873. tbeg (org-table-begin)
  7874. tend (org-table-end))
  7875. (if (re-search-backward org-table-hline-regexp tbeg t)
  7876. (setq beg (point-at-bol 2))
  7877. (goto-char tbeg)
  7878. (setq beg (point-at-bol 1)))
  7879. (goto-char pos)
  7880. (if (re-search-forward org-table-hline-regexp tend t)
  7881. (setq end (point-at-bol 1))
  7882. (goto-char tend)
  7883. (setq end (point-at-bol))))
  7884. (setq beg (move-marker (make-marker) beg)
  7885. end (move-marker (make-marker) end))
  7886. (untabify beg end)
  7887. (goto-char beg)
  7888. (org-table-goto-column column)
  7889. (skip-chars-backward "^|")
  7890. (setq bcol (current-column))
  7891. (org-table-goto-column (1+ column))
  7892. (skip-chars-backward "^|")
  7893. (setq ecol (1- (current-column)))
  7894. (org-table-goto-column column)
  7895. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  7896. (org-split-string (buffer-substring beg end) "\n")))
  7897. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  7898. (delete-region beg end)
  7899. (move-marker beg nil)
  7900. (move-marker end nil)
  7901. (insert (mapconcat 'cdr lns "\n") "\n")
  7902. (goto-line thisline)
  7903. (org-table-goto-column thiscol)
  7904. (message "%d lines sorted, based on column %d" (length lns) column)))
  7905. (defun org-table-cut-region (beg end)
  7906. "Copy region in table to the clipboard and blank all relevant fields."
  7907. (interactive "r")
  7908. (org-table-copy-region beg end 'cut))
  7909. (defun org-table-copy-region (beg end &optional cut)
  7910. "Copy rectangular region in table to clipboard.
  7911. A special clipboard is used which can only be accessed
  7912. with `org-table-paste-rectangle'."
  7913. (interactive "rP")
  7914. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  7915. region cols
  7916. (rpl (if cut " " nil)))
  7917. (goto-char beg)
  7918. (org-table-check-inside-data-field)
  7919. (setq l01 (org-current-line)
  7920. c01 (org-table-current-column))
  7921. (goto-char end)
  7922. (org-table-check-inside-data-field)
  7923. (setq l02 (org-current-line)
  7924. c02 (org-table-current-column))
  7925. (setq l1 (min l01 l02) l2 (max l01 l02)
  7926. c1 (min c01 c02) c2 (max c01 c02))
  7927. (catch 'exit
  7928. (while t
  7929. (catch 'nextline
  7930. (if (> l1 l2) (throw 'exit t))
  7931. (goto-line l1)
  7932. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  7933. (setq cols nil ic1 c1 ic2 c2)
  7934. (while (< ic1 (1+ ic2))
  7935. (push (org-table-get-field ic1 rpl) cols)
  7936. (setq ic1 (1+ ic1)))
  7937. (push (nreverse cols) region)
  7938. (setq l1 (1+ l1)))))
  7939. (setq org-table-clip (nreverse region))
  7940. (if cut (org-table-align))
  7941. org-table-clip))
  7942. (defun org-table-paste-rectangle ()
  7943. "Paste a rectangular region into a table.
  7944. The upper right corner ends up in the current field. All involved fields
  7945. will be overwritten. If the rectangle does not fit into the present table,
  7946. the table is enlarged as needed. The process ignores horizontal separator
  7947. lines."
  7948. (interactive)
  7949. (unless (and org-table-clip (listp org-table-clip))
  7950. (error "First cut/copy a region to paste!"))
  7951. (org-table-check-inside-data-field)
  7952. (let* ((clip org-table-clip)
  7953. (line (org-current-line))
  7954. (col (org-table-current-column))
  7955. (org-enable-table-editor t)
  7956. (org-table-automatic-realign nil)
  7957. c cols field)
  7958. (while (setq cols (pop clip))
  7959. (while (org-at-table-hline-p) (beginning-of-line 2))
  7960. (if (not (org-at-table-p))
  7961. (progn (end-of-line 0) (org-table-next-field)))
  7962. (setq c col)
  7963. (while (setq field (pop cols))
  7964. (org-table-goto-column c nil 'force)
  7965. (org-table-get-field nil field)
  7966. (setq c (1+ c)))
  7967. (beginning-of-line 2))
  7968. (goto-line line)
  7969. (org-table-goto-column col)
  7970. (org-table-align)))
  7971. (defun org-table-convert ()
  7972. "Convert from `org-mode' table to table.el and back.
  7973. Obviously, this only works within limits. When an Org-mode table is
  7974. converted to table.el, all horizontal separator lines get lost, because
  7975. table.el uses these as cell boundaries and has no notion of horizontal lines.
  7976. A table.el table can be converted to an Org-mode table only if it does not
  7977. do row or column spanning. Multiline cells will become multiple cells.
  7978. Beware, Org-mode does not test if the table can be successfully converted - it
  7979. blindly applies a recipe that works for simple tables."
  7980. (interactive)
  7981. (require 'table)
  7982. (if (org-at-table.el-p)
  7983. ;; convert to Org-mode table
  7984. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  7985. (end (move-marker (make-marker) (org-table-end t))))
  7986. (table-unrecognize-region beg end)
  7987. (goto-char beg)
  7988. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  7989. (replace-match ""))
  7990. (goto-char beg))
  7991. (if (org-at-table-p)
  7992. ;; convert to table.el table
  7993. (let ((beg (move-marker (make-marker) (org-table-begin)))
  7994. (end (move-marker (make-marker) (org-table-end))))
  7995. ;; first, get rid of all horizontal lines
  7996. (goto-char beg)
  7997. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  7998. (replace-match ""))
  7999. ;; insert a hline before first
  8000. (goto-char beg)
  8001. (org-table-insert-hline 'above)
  8002. (beginning-of-line -1)
  8003. ;; insert a hline after each line
  8004. (while (progn (beginning-of-line 3) (< (point) end))
  8005. (org-table-insert-hline))
  8006. (goto-char beg)
  8007. (setq end (move-marker end (org-table-end)))
  8008. ;; replace "+" at beginning and ending of hlines
  8009. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8010. (replace-match "\\1+-"))
  8011. (goto-char beg)
  8012. (while (re-search-forward "-|[ \t]*$" end t)
  8013. (replace-match "-+"))
  8014. (goto-char beg)))))
  8015. (defun org-table-wrap-region (arg)
  8016. "Wrap several fields in a column like a paragraph.
  8017. This is useful if you'd like to spread the contents of a field over several
  8018. lines, in order to keep the table compact.
  8019. If there is an active region, and both point and mark are in the same column,
  8020. the text in the column is wrapped to minimum width for the given number of
  8021. lines. Generally, this makes the table more compact. A prefix ARG may be
  8022. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8023. formats the selected text to two lines. If the region was longer than two
  8024. lines, the remaining lines remain empty. A negative prefix argument reduces
  8025. the current number of lines by that amount. The wrapped text is pasted back
  8026. into the table. If you formatted it to more lines than it was before, fields
  8027. further down in the table get overwritten - so you might need to make space in
  8028. the table first.
  8029. If there is no region, the current field is split at the cursor position and
  8030. the text fragment to the right of the cursor is prepended to the field one
  8031. line down.
  8032. If there is no region, but you specify a prefix ARG, the current field gets
  8033. blank, and the content is appended to the field above."
  8034. (interactive "P")
  8035. (org-table-check-inside-data-field)
  8036. (if (org-region-active-p)
  8037. ;; There is a region: fill as a paragraph
  8038. (let* ((beg (region-beginning))
  8039. (cline (save-excursion (goto-char beg) (org-current-line)))
  8040. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8041. nlines)
  8042. (org-table-cut-region (region-beginning) (region-end))
  8043. (if (> (length (car org-table-clip)) 1)
  8044. (error "Region must be limited to single column"))
  8045. (setq nlines (if arg
  8046. (if (< arg 1)
  8047. (+ (length org-table-clip) arg)
  8048. arg)
  8049. (length org-table-clip)))
  8050. (setq org-table-clip
  8051. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8052. nil nlines)))
  8053. (goto-line cline)
  8054. (org-table-goto-column ccol)
  8055. (org-table-paste-rectangle))
  8056. ;; No region, split the current field at point
  8057. (if arg
  8058. ;; combine with field above
  8059. (let ((s (org-table-blank-field))
  8060. (col (org-table-current-column)))
  8061. (beginning-of-line 0)
  8062. (while (org-at-table-hline-p) (beginning-of-line 0))
  8063. (org-table-goto-column col)
  8064. (skip-chars-forward "^|")
  8065. (skip-chars-backward " ")
  8066. (insert " " (org-trim s))
  8067. (org-table-align))
  8068. ;; split field
  8069. (when (looking-at "\\([^|]+\\)+|")
  8070. (let ((s (match-string 1)))
  8071. (replace-match " |")
  8072. (goto-char (match-beginning 0))
  8073. (org-table-next-row)
  8074. (insert (org-trim s) " ")
  8075. (org-table-align))))))
  8076. (defvar org-field-marker nil)
  8077. (defun org-table-edit-field (arg)
  8078. "Edit table field in a different window.
  8079. This is mainly useful for fields that contain hidden parts.
  8080. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8081. it can be edited in place."
  8082. (interactive "P")
  8083. (if arg
  8084. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8085. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8086. (remove-text-properties b e '(org-cwidth t invisible t
  8087. display t intangible t))
  8088. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8089. (font-lock-fontify-block)))
  8090. (let ((pos (move-marker (make-marker) (point)))
  8091. (field (org-table-get-field))
  8092. (cw (current-window-configuration))
  8093. p)
  8094. (org-switch-to-buffer-other-window "*Org tmp*")
  8095. (erase-buffer)
  8096. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8097. (let ((org-inhibit-startup t)) (org-mode))
  8098. (goto-char (setq p (point-max)))
  8099. (insert (org-trim field))
  8100. (remove-text-properties p (point-max)
  8101. '(invisible t org-cwidth t display t
  8102. intangible t))
  8103. (goto-char p)
  8104. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8105. (org-set-local 'org-window-configuration cw)
  8106. (org-set-local 'org-field-marker pos)
  8107. (message "Edit and finish with C-c C-c"))))
  8108. (defun org-table-finish-edit-field ()
  8109. "Finish editing a table data field.
  8110. Remove all newline characters, insert the result into the table, realign
  8111. the table and kill the editing buffer."
  8112. (let ((pos org-field-marker)
  8113. (cw org-window-configuration)
  8114. (cb (current-buffer))
  8115. text)
  8116. (goto-char (point-min))
  8117. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8118. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8119. (replace-match " "))
  8120. (setq text (org-trim (buffer-string)))
  8121. (set-window-configuration cw)
  8122. (kill-buffer cb)
  8123. (select-window (get-buffer-window (marker-buffer pos)))
  8124. (goto-char pos)
  8125. (move-marker pos nil)
  8126. (org-table-check-inside-data-field)
  8127. (org-table-get-field nil text)
  8128. (org-table-align)
  8129. (message "New field value inserted")))
  8130. (defun org-trim (s)
  8131. "Remove whitespace at beginning and end of string."
  8132. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8133. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8134. s)
  8135. (defun org-wrap (string &optional width lines)
  8136. "Wrap string to either a number of lines, or a width in characters.
  8137. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8138. that costs. If there is a word longer than WIDTH, the text is actually
  8139. wrapped to the length of that word.
  8140. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8141. many lines, whatever width that takes.
  8142. The return value is a list of lines, without newlines at the end."
  8143. (let* ((words (org-split-string string "[ \t\n]+"))
  8144. (maxword (apply 'max (mapcar 'org-string-width words)))
  8145. w ll)
  8146. (cond (width
  8147. (org-do-wrap words (max maxword width)))
  8148. (lines
  8149. (setq w maxword)
  8150. (setq ll (org-do-wrap words maxword))
  8151. (if (<= (length ll) lines)
  8152. ll
  8153. (setq ll words)
  8154. (while (> (length ll) lines)
  8155. (setq w (1+ w))
  8156. (setq ll (org-do-wrap words w)))
  8157. ll))
  8158. (t (error "Cannot wrap this")))))
  8159. (defun org-do-wrap (words width)
  8160. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8161. (let (lines line)
  8162. (while words
  8163. (setq line (pop words))
  8164. (while (and words (< (+ (length line) (length (car words))) width))
  8165. (setq line (concat line " " (pop words))))
  8166. (setq lines (push line lines)))
  8167. (nreverse lines)))
  8168. (defun org-split-string (string &optional separators)
  8169. "Splits STRING into substrings at SEPARATORS.
  8170. No empty strings are returned if there are matches at the beginning
  8171. and end of string."
  8172. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8173. (start 0)
  8174. notfirst
  8175. (list nil))
  8176. (while (and (string-match rexp string
  8177. (if (and notfirst
  8178. (= start (match-beginning 0))
  8179. (< start (length string)))
  8180. (1+ start) start))
  8181. (< (match-beginning 0) (length string)))
  8182. (setq notfirst t)
  8183. (or (eq (match-beginning 0) 0)
  8184. (and (eq (match-beginning 0) (match-end 0))
  8185. (eq (match-beginning 0) start))
  8186. (setq list
  8187. (cons (substring string start (match-beginning 0))
  8188. list)))
  8189. (setq start (match-end 0)))
  8190. (or (eq start (length string))
  8191. (setq list
  8192. (cons (substring string start)
  8193. list)))
  8194. (nreverse list)))
  8195. (defun org-table-map-tables (function)
  8196. "Apply FUNCTION to the start of all tables in the buffer."
  8197. (save-excursion
  8198. (save-restriction
  8199. (widen)
  8200. (goto-char (point-min))
  8201. (while (re-search-forward org-table-any-line-regexp nil t)
  8202. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8203. (beginning-of-line 1)
  8204. (if (looking-at org-table-line-regexp)
  8205. (save-excursion (funcall function)))
  8206. (re-search-forward org-table-any-border-regexp nil 1))))
  8207. (message "Mapping tables: done"))
  8208. (defvar org-timecnt) ; dynamically scoped parameter
  8209. (defun org-table-sum (&optional beg end nlast)
  8210. "Sum numbers in region of current table column.
  8211. The result will be displayed in the echo area, and will be available
  8212. as kill to be inserted with \\[yank].
  8213. If there is an active region, it is interpreted as a rectangle and all
  8214. numbers in that rectangle will be summed. If there is no active
  8215. region and point is located in a table column, sum all numbers in that
  8216. column.
  8217. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8218. numbers are assumed to be times as well (in decimal hours) and the
  8219. numbers are added as such.
  8220. If NLAST is a number, only the NLAST fields will actually be summed."
  8221. (interactive)
  8222. (save-excursion
  8223. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8224. (cond
  8225. ((and beg end)) ; beg and end given explicitly
  8226. ((org-region-active-p)
  8227. (setq beg (region-beginning) end (region-end)))
  8228. (t
  8229. (setq col (org-table-current-column))
  8230. (goto-char (org-table-begin))
  8231. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8232. (error "No table data"))
  8233. (org-table-goto-column col)
  8234. (setq beg (point))
  8235. (goto-char (org-table-end))
  8236. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8237. (error "No table data"))
  8238. (org-table-goto-column col)
  8239. (setq end (point))))
  8240. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8241. (items1 (cond ((not nlast) items)
  8242. ((>= nlast (length items)) items)
  8243. (t (setq items (reverse items))
  8244. (setcdr (nthcdr (1- nlast) items) nil)
  8245. (nreverse items))))
  8246. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8247. items1)))
  8248. (res (apply '+ numbers))
  8249. (sres (if (= org-timecnt 0)
  8250. (format "%g" res)
  8251. (setq diff (* 3600 res)
  8252. h (floor (/ diff 3600)) diff (mod diff 3600)
  8253. m (floor (/ diff 60)) diff (mod diff 60)
  8254. s diff)
  8255. (format "%d:%02d:%02d" h m s))))
  8256. (kill-new sres)
  8257. (if (interactive-p)
  8258. (message "%s"
  8259. (substitute-command-keys
  8260. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8261. (length numbers) sres))))
  8262. sres))))
  8263. (defun org-table-get-number-for-summing (s)
  8264. (let (n)
  8265. (if (string-match "^ *|? *" s)
  8266. (setq s (replace-match "" nil nil s)))
  8267. (if (string-match " *|? *$" s)
  8268. (setq s (replace-match "" nil nil s)))
  8269. (setq n (string-to-number s))
  8270. (cond
  8271. ((and (string-match "0" s)
  8272. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8273. ((string-match "\\`[ \t]+\\'" s) nil)
  8274. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8275. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8276. (m (string-to-number (or (match-string 2 s) "0")))
  8277. (s (string-to-number (or (match-string 4 s) "0"))))
  8278. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8279. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8280. ((equal n 0) nil)
  8281. (t n))))
  8282. (defun org-table-current-field-formula (&optional key noerror)
  8283. "Return the formula active for the current field.
  8284. Assumes that specials are in place.
  8285. If KEY is given, return the key to this formula.
  8286. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8287. (let* ((name (car (rassoc (list (org-current-line)
  8288. (org-table-current-column))
  8289. org-table-named-field-locations)))
  8290. (col (org-table-current-column))
  8291. (scol (int-to-string col))
  8292. (ref (format "@%d$%d" (org-table-current-dline) col))
  8293. (stored-list (org-table-get-stored-formulas noerror))
  8294. (ass (or (assoc name stored-list)
  8295. (assoc ref stored-list)
  8296. (assoc scol stored-list))))
  8297. (if key
  8298. (car ass)
  8299. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8300. (cdr ass))))))
  8301. (defun org-table-get-formula (&optional equation named)
  8302. "Read a formula from the minibuffer, offer stored formula as default.
  8303. When NAMED is non-nil, look for a named equation."
  8304. (let* ((stored-list (org-table-get-stored-formulas))
  8305. (name (car (rassoc (list (org-current-line)
  8306. (org-table-current-column))
  8307. org-table-named-field-locations)))
  8308. (ref (format "@%d$%d" (org-table-current-dline)
  8309. (org-table-current-column)))
  8310. (refass (assoc ref stored-list))
  8311. (scol (if named
  8312. (if name name ref)
  8313. (int-to-string (org-table-current-column))))
  8314. (dummy (and (or name refass) (not named)
  8315. (not (y-or-n-p "Replace field formula with column formula? " ))
  8316. (error "Abort")))
  8317. (name (or name ref))
  8318. (org-table-may-need-update nil)
  8319. (stored (cdr (assoc scol stored-list)))
  8320. (eq (cond
  8321. ((and stored equation (string-match "^ *=? *$" equation))
  8322. stored)
  8323. ((stringp equation)
  8324. equation)
  8325. (t (org-table-formula-from-user
  8326. (read-string
  8327. (org-table-formula-to-user
  8328. (format "%s formula %s%s="
  8329. (if named "Field" "Column")
  8330. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8331. scol))
  8332. (if stored (org-table-formula-to-user stored) "")
  8333. 'org-table-formula-history
  8334. )))))
  8335. mustsave)
  8336. (when (not (string-match "\\S-" eq))
  8337. ;; remove formula
  8338. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8339. (org-table-store-formulas stored-list)
  8340. (error "Formula removed"))
  8341. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8342. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8343. (if (and name (not named))
  8344. ;; We set the column equation, delete the named one.
  8345. (setq stored-list (delq (assoc name stored-list) stored-list)
  8346. mustsave t))
  8347. (if stored
  8348. (setcdr (assoc scol stored-list) eq)
  8349. (setq stored-list (cons (cons scol eq) stored-list)))
  8350. (if (or mustsave (not (equal stored eq)))
  8351. (org-table-store-formulas stored-list))
  8352. eq))
  8353. (defun org-table-store-formulas (alist)
  8354. "Store the list of formulas below the current table."
  8355. (setq alist (sort alist 'org-table-formula-less-p))
  8356. (save-excursion
  8357. (goto-char (org-table-end))
  8358. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8359. (progn
  8360. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8361. (goto-char (match-beginning 2))
  8362. (delete-region (match-beginning 2) (match-end 0)))
  8363. (insert "#+TBLFM:"))
  8364. (insert " "
  8365. (mapconcat (lambda (x)
  8366. (concat
  8367. (if (equal (string-to-char (car x)) ?@) "" "$")
  8368. (car x) "=" (cdr x)))
  8369. alist "::")
  8370. "\n")))
  8371. (defsubst org-table-formula-make-cmp-string (a)
  8372. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8373. (concat
  8374. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8375. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8376. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8377. (defun org-table-formula-less-p (a b)
  8378. "Compare two formulas for sorting."
  8379. (let ((as (org-table-formula-make-cmp-string (car a)))
  8380. (bs (org-table-formula-make-cmp-string (car b))))
  8381. (and as bs (string< as bs))))
  8382. (defun org-table-get-stored-formulas (&optional noerror)
  8383. "Return an alist with the stored formulas directly after current table."
  8384. (interactive)
  8385. (let (scol eq eq-alist strings string seen)
  8386. (save-excursion
  8387. (goto-char (org-table-end))
  8388. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8389. (setq strings (org-split-string (match-string 2) " *:: *"))
  8390. (while (setq string (pop strings))
  8391. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8392. (setq scol (if (match-end 2)
  8393. (match-string 2 string)
  8394. (match-string 1 string))
  8395. eq (match-string 3 string)
  8396. eq-alist (cons (cons scol eq) eq-alist))
  8397. (if (member scol seen)
  8398. (if noerror
  8399. (progn
  8400. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8401. (ding)
  8402. (sit-for 2))
  8403. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8404. (push scol seen))))))
  8405. (nreverse eq-alist)))
  8406. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8407. "Modify the equations after the table structure has been edited.
  8408. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8409. For all numbers larger than LIMIT, shift them by DELTA."
  8410. (save-excursion
  8411. (goto-char (org-table-end))
  8412. (when (looking-at "#\\+TBLFM:")
  8413. (let ((re (concat key "\\([0-9]+\\)"))
  8414. (re2
  8415. (when remove
  8416. (if (equal key "$")
  8417. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8418. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8419. s n a)
  8420. (when remove
  8421. (while (re-search-forward re2 (point-at-eol) t)
  8422. (replace-match "")))
  8423. (while (re-search-forward re (point-at-eol) t)
  8424. (setq s (match-string 1) n (string-to-number s))
  8425. (cond
  8426. ((setq a (assoc s replace))
  8427. (replace-match (concat key (cdr a)) t t))
  8428. ((and limit (> n limit))
  8429. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8430. (defun org-table-get-specials ()
  8431. "Get the column names and local parameters for this table."
  8432. (save-excursion
  8433. (let ((beg (org-table-begin)) (end (org-table-end))
  8434. names name fields fields1 field cnt
  8435. c v l line col types dlines hlines)
  8436. (setq org-table-column-names nil
  8437. org-table-local-parameters nil
  8438. org-table-named-field-locations nil
  8439. org-table-current-begin-line nil
  8440. org-table-current-begin-pos nil
  8441. org-table-current-line-types nil)
  8442. (goto-char beg)
  8443. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8444. (setq names (org-split-string (match-string 1) " *| *")
  8445. cnt 1)
  8446. (while (setq name (pop names))
  8447. (setq cnt (1+ cnt))
  8448. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8449. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8450. (setq org-table-column-names (nreverse org-table-column-names))
  8451. (setq org-table-column-name-regexp
  8452. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8453. (goto-char beg)
  8454. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8455. (setq fields (org-split-string (match-string 1) " *| *"))
  8456. (while (setq field (pop fields))
  8457. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8458. (push (cons (match-string 1 field) (match-string 2 field))
  8459. org-table-local-parameters))))
  8460. (goto-char beg)
  8461. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8462. (setq c (match-string 1)
  8463. fields (org-split-string (match-string 2) " *| *"))
  8464. (save-excursion
  8465. (beginning-of-line (if (equal c "_") 2 0))
  8466. (setq line (org-current-line) col 1)
  8467. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8468. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8469. (while (and fields1 (setq field (pop fields)))
  8470. (setq v (pop fields1) col (1+ col))
  8471. (when (and (stringp field) (stringp v)
  8472. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8473. (push (cons field v) org-table-local-parameters)
  8474. (push (list field line col) org-table-named-field-locations))))
  8475. ;; Analyse the line types
  8476. (goto-char beg)
  8477. (setq org-table-current-begin-line (org-current-line)
  8478. org-table-current-begin-pos (point)
  8479. l org-table-current-begin-line)
  8480. (while (looking-at "[ \t]*|\\(-\\)?")
  8481. (push (if (match-end 1) 'hline 'dline) types)
  8482. (if (match-end 1) (push l hlines) (push l dlines))
  8483. (beginning-of-line 2)
  8484. (setq l (1+ l)))
  8485. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8486. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8487. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8488. (defun org-table-maybe-eval-formula ()
  8489. "Check if the current field starts with \"=\" or \":=\".
  8490. If yes, store the formula and apply it."
  8491. ;; We already know we are in a table. Get field will only return a formula
  8492. ;; when appropriate. It might return a separator line, but no problem.
  8493. (when org-table-formula-evaluate-inline
  8494. (let* ((field (org-trim (or (org-table-get-field) "")))
  8495. named eq)
  8496. (when (string-match "^:?=\\(.*\\)" field)
  8497. (setq named (equal (string-to-char field) ?:)
  8498. eq (match-string 1 field))
  8499. (if (or (fboundp 'calc-eval)
  8500. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8501. (org-table-eval-formula (if named '(4) nil)
  8502. (org-table-formula-from-user eq))
  8503. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8504. (defvar org-recalc-commands nil
  8505. "List of commands triggering the recalculation of a line.
  8506. Will be filled automatically during use.")
  8507. (defvar org-recalc-marks
  8508. '((" " . "Unmarked: no special line, no automatic recalculation")
  8509. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8510. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8511. ("!" . "Column name definition line. Reference in formula as $name.")
  8512. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8513. ("_" . "Names for values in row below this one.")
  8514. ("^" . "Names for values in row above this one.")))
  8515. (defun org-table-rotate-recalc-marks (&optional newchar)
  8516. "Rotate the recalculation mark in the first column.
  8517. If in any row, the first field is not consistent with a mark,
  8518. insert a new column for the markers.
  8519. When there is an active region, change all the lines in the region,
  8520. after prompting for the marking character.
  8521. After each change, a message will be displayed indicating the meaning
  8522. of the new mark."
  8523. (interactive)
  8524. (unless (org-at-table-p) (error "Not at a table"))
  8525. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8526. (beg (org-table-begin))
  8527. (end (org-table-end))
  8528. (l (org-current-line))
  8529. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8530. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8531. (have-col
  8532. (save-excursion
  8533. (goto-char beg)
  8534. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8535. (col (org-table-current-column))
  8536. (forcenew (car (assoc newchar org-recalc-marks)))
  8537. epos new)
  8538. (when l1
  8539. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8540. (setq newchar (char-to-string (read-char-exclusive))
  8541. forcenew (car (assoc newchar org-recalc-marks))))
  8542. (if (and newchar (not forcenew))
  8543. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8544. newchar))
  8545. (if l1 (goto-line l1))
  8546. (save-excursion
  8547. (beginning-of-line 1)
  8548. (unless (looking-at org-table-dataline-regexp)
  8549. (error "Not at a table data line")))
  8550. (unless have-col
  8551. (org-table-goto-column 1)
  8552. (org-table-insert-column)
  8553. (org-table-goto-column (1+ col)))
  8554. (setq epos (point-at-eol))
  8555. (save-excursion
  8556. (beginning-of-line 1)
  8557. (org-table-get-field
  8558. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8559. (concat " "
  8560. (setq new (or forcenew
  8561. (cadr (member (match-string 1) marks))))
  8562. " ")
  8563. " # ")))
  8564. (if (and l1 l2)
  8565. (progn
  8566. (goto-line l1)
  8567. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8568. (and (looking-at org-table-dataline-regexp)
  8569. (org-table-get-field 1 (concat " " new " "))))
  8570. (goto-line l1)))
  8571. (if (not (= epos (point-at-eol))) (org-table-align))
  8572. (goto-line l)
  8573. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  8574. (defun org-table-maybe-recalculate-line ()
  8575. "Recompute the current line if marked for it, and if we haven't just done it."
  8576. (interactive)
  8577. (and org-table-allow-automatic-line-recalculation
  8578. (not (and (memq last-command org-recalc-commands)
  8579. (equal org-last-recalc-line (org-current-line))))
  8580. (save-excursion (beginning-of-line 1)
  8581. (looking-at org-table-auto-recalculate-regexp))
  8582. (org-table-recalculate) t))
  8583. (defvar org-table-formula-debug nil
  8584. "Non-nil means, debug table formulas.
  8585. When nil, simply write \"#ERROR\" in corrupted fields.")
  8586. (make-variable-buffer-local 'org-table-formula-debug)
  8587. (defvar modes)
  8588. (defsubst org-set-calc-mode (var &optional value)
  8589. (if (stringp var)
  8590. (setq var (assoc var '(("D" calc-angle-mode deg)
  8591. ("R" calc-angle-mode rad)
  8592. ("F" calc-prefer-frac t)
  8593. ("S" calc-symbolic-mode t)))
  8594. value (nth 2 var) var (nth 1 var)))
  8595. (if (memq var modes)
  8596. (setcar (cdr (memq var modes)) value)
  8597. (cons var (cons value modes)))
  8598. modes)
  8599. (defun org-table-eval-formula (&optional arg equation
  8600. suppress-align suppress-const
  8601. suppress-store suppress-analysis)
  8602. "Replace the table field value at the cursor by the result of a calculation.
  8603. This function makes use of Dave Gillespie's Calc package, in my view the
  8604. most exciting program ever written for GNU Emacs. So you need to have Calc
  8605. installed in order to use this function.
  8606. In a table, this command replaces the value in the current field with the
  8607. result of a formula. It also installs the formula as the \"current\" column
  8608. formula, by storing it in a special line below the table. When called
  8609. with a `C-u' prefix, the current field must ba a named field, and the
  8610. formula is installed as valid in only this specific field.
  8611. When called with two `C-u' prefixes, insert the active equation
  8612. for the field back into the current field, so that it can be
  8613. edited there. This is useful in order to use \\[org-table-show-reference]
  8614. to check the referenced fields.
  8615. When called, the command first prompts for a formula, which is read in
  8616. the minibuffer. Previously entered formulas are available through the
  8617. history list, and the last used formula is offered as a default.
  8618. These stored formulas are adapted correctly when moving, inserting, or
  8619. deleting columns with the corresponding commands.
  8620. The formula can be any algebraic expression understood by the Calc package.
  8621. For details, see the Org-mode manual.
  8622. This function can also be called from Lisp programs and offers
  8623. additional arguments: EQUATION can be the formula to apply. If this
  8624. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  8625. used to speed-up recursive calls by by-passing unnecessary aligns.
  8626. SUPPRESS-CONST suppresses the interpretation of constants in the
  8627. formula, assuming that this has been done already outside the function.
  8628. SUPPRESS-STORE means the formula should not be stored, either because
  8629. it is already stored, or because it is a modified equation that should
  8630. not overwrite the stored one."
  8631. (interactive "P")
  8632. (org-table-check-inside-data-field)
  8633. (or suppress-analysis (org-table-get-specials))
  8634. (if (equal arg '(16))
  8635. (let ((eq (org-table-current-field-formula)))
  8636. (or eq (error "No equation active for current field"))
  8637. (org-table-get-field nil eq)
  8638. (org-table-align)
  8639. (setq org-table-may-need-update t))
  8640. (let* (fields
  8641. (ndown (if (integerp arg) arg 1))
  8642. (org-table-automatic-realign nil)
  8643. (case-fold-search nil)
  8644. (down (> ndown 1))
  8645. (formula (if (and equation suppress-store)
  8646. equation
  8647. (org-table-get-formula equation (equal arg '(4)))))
  8648. (n0 (org-table-current-column))
  8649. (modes (copy-sequence org-calc-default-modes))
  8650. (numbers nil) ; was a variable, now fixed default
  8651. (keep-empty nil)
  8652. n form form0 bw fmt x ev orig c lispp literal)
  8653. ;; Parse the format string. Since we have a lot of modes, this is
  8654. ;; a lot of work. However, I think calc still uses most of the time.
  8655. (if (string-match ";" formula)
  8656. (let ((tmp (org-split-string formula ";")))
  8657. (setq formula (car tmp)
  8658. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  8659. (nth 1 tmp)))
  8660. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  8661. (setq c (string-to-char (match-string 1 fmt))
  8662. n (string-to-number (match-string 2 fmt)))
  8663. (if (= c ?p)
  8664. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  8665. (setq modes (org-set-calc-mode
  8666. 'calc-float-format
  8667. (list (cdr (assoc c '((?n . float) (?f . fix)
  8668. (?s . sci) (?e . eng))))
  8669. n))))
  8670. (setq fmt (replace-match "" t t fmt)))
  8671. (if (string-match "[NT]" fmt)
  8672. (setq numbers (equal (match-string 0 fmt) "N")
  8673. fmt (replace-match "" t t fmt)))
  8674. (if (string-match "L" fmt)
  8675. (setq literal t
  8676. fmt (replace-match "" t t fmt)))
  8677. (if (string-match "E" fmt)
  8678. (setq keep-empty t
  8679. fmt (replace-match "" t t fmt)))
  8680. (while (string-match "[DRFS]" fmt)
  8681. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  8682. (setq fmt (replace-match "" t t fmt)))
  8683. (unless (string-match "\\S-" fmt)
  8684. (setq fmt nil))))
  8685. (if (and (not suppress-const) org-table-formula-use-constants)
  8686. (setq formula (org-table-formula-substitute-names formula)))
  8687. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  8688. (while (> ndown 0)
  8689. (setq fields (org-split-string
  8690. (org-no-properties
  8691. (buffer-substring (point-at-bol) (point-at-eol)))
  8692. " *| *"))
  8693. (if (eq numbers t)
  8694. (setq fields (mapcar
  8695. (lambda (x) (number-to-string (string-to-number x)))
  8696. fields)))
  8697. (setq ndown (1- ndown))
  8698. (setq form (copy-sequence formula)
  8699. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  8700. (if (and lispp literal) (setq lispp 'literal))
  8701. ;; Check for old vertical references
  8702. (setq form (org-rewrite-old-row-references form))
  8703. ;; Insert complex ranges
  8704. (while (string-match org-table-range-regexp form)
  8705. (setq form
  8706. (replace-match
  8707. (save-match-data
  8708. (org-table-make-reference
  8709. (org-table-get-range (match-string 0 form) nil n0)
  8710. keep-empty numbers lispp))
  8711. t t form)))
  8712. ;; Insert simple ranges
  8713. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  8714. (setq form
  8715. (replace-match
  8716. (save-match-data
  8717. (org-table-make-reference
  8718. (org-sublist
  8719. fields (string-to-number (match-string 1 form))
  8720. (string-to-number (match-string 2 form)))
  8721. keep-empty numbers lispp))
  8722. t t form)))
  8723. (setq form0 form)
  8724. ;; Insert the references to fields in same row
  8725. (while (string-match "\\$\\([0-9]+\\)" form)
  8726. (setq n (string-to-number (match-string 1 form))
  8727. x (nth (1- (if (= n 0) n0 n)) fields))
  8728. (unless x (error "Invalid field specifier \"%s\""
  8729. (match-string 0 form)))
  8730. (setq form (replace-match
  8731. (save-match-data
  8732. (org-table-make-reference x nil numbers lispp))
  8733. t t form)))
  8734. (if lispp
  8735. (setq ev (condition-case nil
  8736. (eval (eval (read form)))
  8737. (error "#ERROR"))
  8738. ev (if (numberp ev) (number-to-string ev) ev))
  8739. (or (fboundp 'calc-eval)
  8740. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  8741. (setq ev (calc-eval (cons form modes)
  8742. (if numbers 'num))))
  8743. (when org-table-formula-debug
  8744. (with-output-to-temp-buffer "*Substitution History*"
  8745. (princ (format "Substitution history of formula
  8746. Orig: %s
  8747. $xyz-> %s
  8748. @r$c-> %s
  8749. $1-> %s\n" orig formula form0 form))
  8750. (if (listp ev)
  8751. (princ (format " %s^\nError: %s"
  8752. (make-string (car ev) ?\-) (nth 1 ev)))
  8753. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  8754. ev (or fmt "NONE")
  8755. (if fmt (format fmt (string-to-number ev)) ev)))))
  8756. (setq bw (get-buffer-window "*Substitution History*"))
  8757. (shrink-window-if-larger-than-buffer bw)
  8758. (unless (and (interactive-p) (not ndown))
  8759. (unless (let (inhibit-redisplay)
  8760. (y-or-n-p "Debugging Formula. Continue to next? "))
  8761. (org-table-align)
  8762. (error "Abort"))
  8763. (delete-window bw)
  8764. (message "")))
  8765. (if (listp ev) (setq fmt nil ev "#ERROR"))
  8766. (org-table-justify-field-maybe
  8767. (if fmt (format fmt (string-to-number ev)) ev))
  8768. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  8769. (call-interactively 'org-return)
  8770. (setq ndown 0)))
  8771. (and down (org-table-maybe-recalculate-line))
  8772. (or suppress-align (and org-table-may-need-update
  8773. (org-table-align))))))
  8774. (defun org-table-put-field-property (prop value)
  8775. (save-excursion
  8776. (put-text-property (progn (skip-chars-backward "^|") (point))
  8777. (progn (skip-chars-forward "^|") (point))
  8778. prop value)))
  8779. (defun org-table-get-range (desc &optional tbeg col highlight)
  8780. "Get a calc vector from a column, accorting to descriptor DESC.
  8781. Optional arguments TBEG and COL can give the beginning of the table and
  8782. the current column, to avoid unnecessary parsing.
  8783. HIGHLIGHT means, just highlight the range."
  8784. (if (not (equal (string-to-char desc) ?@))
  8785. (setq desc (concat "@" desc)))
  8786. (save-excursion
  8787. (or tbeg (setq tbeg (org-table-begin)))
  8788. (or col (setq col (org-table-current-column)))
  8789. (let ((thisline (org-current-line))
  8790. beg end c1 c2 r1 r2 rangep tmp)
  8791. (unless (string-match org-table-range-regexp desc)
  8792. (error "Invalid table range specifier `%s'" desc))
  8793. (setq rangep (match-end 3)
  8794. r1 (and (match-end 1) (match-string 1 desc))
  8795. r2 (and (match-end 4) (match-string 4 desc))
  8796. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  8797. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  8798. (and c1 (setq c1 (+ (string-to-number c1)
  8799. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  8800. (and c2 (setq c2 (+ (string-to-number c2)
  8801. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  8802. (if (equal r1 "") (setq r1 nil))
  8803. (if (equal r2 "") (setq r2 nil))
  8804. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  8805. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  8806. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  8807. (if (not r1) (setq r1 thisline))
  8808. (if (not r2) (setq r2 thisline))
  8809. (if (not c1) (setq c1 col))
  8810. (if (not c2) (setq c2 col))
  8811. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  8812. ;; just one field
  8813. (progn
  8814. (goto-line r1)
  8815. (while (not (looking-at org-table-dataline-regexp))
  8816. (beginning-of-line 2))
  8817. (prog1 (org-trim (org-table-get-field c1))
  8818. (if highlight (org-table-highlight-rectangle (point) (point)))))
  8819. ;; A range, return a vector
  8820. ;; First sort the numbers to get a regular ractangle
  8821. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  8822. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  8823. (goto-line r1)
  8824. (while (not (looking-at org-table-dataline-regexp))
  8825. (beginning-of-line 2))
  8826. (org-table-goto-column c1)
  8827. (setq beg (point))
  8828. (goto-line r2)
  8829. (while (not (looking-at org-table-dataline-regexp))
  8830. (beginning-of-line 0))
  8831. (org-table-goto-column c2)
  8832. (setq end (point))
  8833. (if highlight
  8834. (org-table-highlight-rectangle
  8835. beg (progn (skip-chars-forward "^|\n") (point))))
  8836. ;; return string representation of calc vector
  8837. (mapcar 'org-trim
  8838. (apply 'append (org-table-copy-region beg end)))))))
  8839. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  8840. "Analyze descriptor DESC and retrieve the corresponding line number.
  8841. The cursor is currently in line CLINE, the table begins in line BLINE,
  8842. and TABLE is a vector with line types."
  8843. (if (string-match "^[0-9]+$" desc)
  8844. (aref org-table-dlines (string-to-number desc))
  8845. (setq cline (or cline (org-current-line))
  8846. bline (or bline org-table-current-begin-line)
  8847. table (or table org-table-current-line-types))
  8848. (if (or
  8849. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  8850. ;; 1 2 3 4 5 6
  8851. (and (not (match-end 3)) (not (match-end 6)))
  8852. (and (match-end 3) (match-end 6) (not (match-end 5))))
  8853. (error "invalid row descriptor `%s'" desc))
  8854. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  8855. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  8856. (odir (and (match-end 5) (match-string 5 desc)))
  8857. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  8858. (i (- cline bline))
  8859. (rel (and (match-end 6)
  8860. (or (and (match-end 1) (not (match-end 3)))
  8861. (match-end 5)))))
  8862. (if (and hn (not hdir))
  8863. (progn
  8864. (setq i 0 hdir "+")
  8865. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  8866. (if (and (not hn) on (not odir))
  8867. (error "should never happen");;(aref org-table-dlines on)
  8868. (if (and hn (> hn 0))
  8869. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  8870. (if on
  8871. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  8872. (+ bline i)))))
  8873. (defun org-find-row-type (table i type backwards relative n)
  8874. (let ((l (length table)))
  8875. (while (> n 0)
  8876. (while (and (setq i (+ i (if backwards -1 1)))
  8877. (>= i 0) (< i l)
  8878. (not (eq (aref table i) type))
  8879. (if (and relative (eq (aref table i) 'hline))
  8880. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  8881. t)))
  8882. (setq n (1- n)))
  8883. (if (or (< i 0) (>= i l))
  8884. (error "Row descriptior leads outside table")
  8885. i)))
  8886. (defun org-rewrite-old-row-references (s)
  8887. (if (string-match "&[-+0-9I]" s)
  8888. (error "Formula contains old &row reference, please rewrite using @-syntax")
  8889. s))
  8890. (defun org-table-make-reference (elements keep-empty numbers lispp)
  8891. "Convert list ELEMENTS to something appropriate to insert into formula.
  8892. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  8893. NUMBERS indicates that everything should be converted to numbers.
  8894. LISPP means to return something appropriate for a Lisp list."
  8895. (if (stringp elements) ; just a single val
  8896. (if lispp
  8897. (if (eq lispp 'literal)
  8898. elements
  8899. (prin1-to-string (if numbers (string-to-number elements) elements)))
  8900. (if (equal elements "") (setq elements "0"))
  8901. (if numbers (number-to-string (string-to-number elements)) elements))
  8902. (unless keep-empty
  8903. (setq elements
  8904. (delq nil
  8905. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  8906. elements))))
  8907. (setq elements (or elements '("0")))
  8908. (if lispp
  8909. (mapconcat
  8910. (lambda (x)
  8911. (if (eq lispp 'literal)
  8912. x
  8913. (prin1-to-string (if numbers (string-to-number x) x))))
  8914. elements " ")
  8915. (concat "[" (mapconcat
  8916. (lambda (x)
  8917. (if numbers (number-to-string (string-to-number x)) x))
  8918. elements
  8919. ",") "]"))))
  8920. (defun org-table-recalculate (&optional all noalign)
  8921. "Recalculate the current table line by applying all stored formulas.
  8922. With prefix arg ALL, do this for all lines in the table."
  8923. (interactive "P")
  8924. (or (memq this-command org-recalc-commands)
  8925. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  8926. (unless (org-at-table-p) (error "Not at a table"))
  8927. (if (equal all '(16))
  8928. (org-table-iterate)
  8929. (org-table-get-specials)
  8930. (let* ((eqlist (sort (org-table-get-stored-formulas)
  8931. (lambda (a b) (string< (car a) (car b)))))
  8932. (inhibit-redisplay (not debug-on-error))
  8933. (line-re org-table-dataline-regexp)
  8934. (thisline (org-current-line))
  8935. (thiscol (org-table-current-column))
  8936. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  8937. ;; Insert constants in all formulas
  8938. (setq eqlist
  8939. (mapcar (lambda (x)
  8940. (setcdr x (org-table-formula-substitute-names (cdr x)))
  8941. x)
  8942. eqlist))
  8943. ;; Split the equation list
  8944. (while (setq eq (pop eqlist))
  8945. (if (<= (string-to-char (car eq)) ?9)
  8946. (push eq eqlnum)
  8947. (push eq eqlname)))
  8948. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  8949. (if all
  8950. (progn
  8951. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  8952. (goto-char (setq beg (org-table-begin)))
  8953. (if (re-search-forward org-table-calculate-mark-regexp end t)
  8954. ;; This is a table with marked lines, compute selected lines
  8955. (setq line-re org-table-recalculate-regexp)
  8956. ;; Move forward to the first non-header line
  8957. (if (and (re-search-forward org-table-dataline-regexp end t)
  8958. (re-search-forward org-table-hline-regexp end t)
  8959. (re-search-forward org-table-dataline-regexp end t))
  8960. (setq beg (match-beginning 0))
  8961. nil))) ;; just leave beg where it is
  8962. (setq beg (point-at-bol)
  8963. end (move-marker (make-marker) (1+ (point-at-eol)))))
  8964. (goto-char beg)
  8965. (and all (message "Re-applying formulas to full table..."))
  8966. ;; First find the named fields, and mark them untouchanble
  8967. (remove-text-properties beg end '(org-untouchable t))
  8968. (while (setq eq (pop eqlname))
  8969. (setq name (car eq)
  8970. a (assoc name org-table-named-field-locations))
  8971. (and (not a)
  8972. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  8973. (setq a (list name
  8974. (aref org-table-dlines
  8975. (string-to-number (match-string 1 name)))
  8976. (string-to-number (match-string 2 name)))))
  8977. (when (and a (or all (equal (nth 1 a) thisline)))
  8978. (message "Re-applying formula to field: %s" name)
  8979. (goto-line (nth 1 a))
  8980. (org-table-goto-column (nth 2 a))
  8981. (push (append a (list (cdr eq))) eqlname1)
  8982. (org-table-put-field-property :org-untouchable t)))
  8983. ;; Now evauluate the column formulas, but skip fields covered by
  8984. ;; field formulas
  8985. (goto-char beg)
  8986. (while (re-search-forward line-re end t)
  8987. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  8988. ;; Unprotected line, recalculate
  8989. (and all (message "Re-applying formulas to full table...(line %d)"
  8990. (setq cnt (1+ cnt))))
  8991. (setq org-last-recalc-line (org-current-line))
  8992. (setq eql eqlnum)
  8993. (while (setq entry (pop eql))
  8994. (goto-line org-last-recalc-line)
  8995. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  8996. (unless (get-text-property (point) :org-untouchable)
  8997. (org-table-eval-formula nil (cdr entry)
  8998. 'noalign 'nocst 'nostore 'noanalysis)))))
  8999. ;; Now evaluate the field formulas
  9000. (while (setq eq (pop eqlname1))
  9001. (message "Re-applying formula to field: %s" (car eq))
  9002. (goto-line (nth 1 eq))
  9003. (org-table-goto-column (nth 2 eq))
  9004. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9005. 'nostore 'noanalysis))
  9006. (goto-line thisline)
  9007. (org-table-goto-column thiscol)
  9008. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9009. (or noalign (and org-table-may-need-update (org-table-align))
  9010. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9011. ;; back to initial position
  9012. (message "Re-applying formulas...done")
  9013. (goto-line thisline)
  9014. (org-table-goto-column thiscol)
  9015. (or noalign (and org-table-may-need-update (org-table-align))
  9016. (and all (message "Re-applying formulas...done"))))))
  9017. (defun org-table-iterate (&optional arg)
  9018. "Recalculate the table until it does not change anymore."
  9019. (interactive "P")
  9020. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9021. (i 0)
  9022. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9023. thistbl)
  9024. (catch 'exit
  9025. (while (< i imax)
  9026. (setq i (1+ i))
  9027. (org-table-recalculate 'all)
  9028. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9029. (if (not (string= lasttbl thistbl))
  9030. (setq lasttbl thistbl)
  9031. (if (> i 1)
  9032. (message "Convergence after %d iterations" i)
  9033. (message "Table was already stable"))
  9034. (throw 'exit t)))
  9035. (error "No convergence after %d iterations" i))))
  9036. (defun org-table-formula-substitute-names (f)
  9037. "Replace $const with values in string F."
  9038. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9039. ;; First, check for column names
  9040. (while (setq start (string-match org-table-column-name-regexp f start))
  9041. (setq start (1+ start))
  9042. (setq a (assoc (match-string 1 f) org-table-column-names))
  9043. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9044. ;; Parameters and constants
  9045. (setq start 0)
  9046. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9047. (setq start (1+ start))
  9048. (if (setq a (save-match-data
  9049. (org-table-get-constant (match-string 1 f))))
  9050. (setq f (replace-match
  9051. (concat (if pp "(") a (if pp ")")) t t f))))
  9052. (if org-table-formula-debug
  9053. (put-text-property 0 (length f) :orig-formula f1 f))
  9054. f))
  9055. (defun org-table-get-constant (const)
  9056. "Find the value for a parameter or constant in a formula.
  9057. Parameters get priority."
  9058. (or (cdr (assoc const org-table-local-parameters))
  9059. (cdr (assoc const org-table-formula-constants-local))
  9060. (cdr (assoc const org-table-formula-constants))
  9061. (and (fboundp 'constants-get) (constants-get const))
  9062. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9063. (org-entry-get nil (substring const 5) 'inherit))
  9064. "#UNDEFINED_NAME"))
  9065. (defvar org-table-fedit-map
  9066. (let ((map (make-sparse-keymap)))
  9067. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9068. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9069. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9070. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9071. (org-defkey map "\C-c?" 'org-table-show-reference)
  9072. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9073. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9074. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9075. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9076. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9077. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9078. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9079. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9080. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9081. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9082. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9083. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9084. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9085. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9086. map))
  9087. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9088. '("Edit-Formulas"
  9089. ["Finish and Install" org-table-fedit-finish t]
  9090. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9091. ["Abort" org-table-fedit-abort t]
  9092. "--"
  9093. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9094. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9095. "--"
  9096. "Shift Reference at Point"
  9097. ["Up" org-table-fedit-ref-up t]
  9098. ["Down" org-table-fedit-ref-down t]
  9099. ["Left" org-table-fedit-ref-left t]
  9100. ["Right" org-table-fedit-ref-right t]
  9101. "-"
  9102. "Change Test Row for Column Formulas"
  9103. ["Up" org-table-fedit-line-up t]
  9104. ["Down" org-table-fedit-line-down t]
  9105. "--"
  9106. ["Scroll Table Window" org-table-fedit-scroll t]
  9107. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9108. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9109. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9110. org-table-overlay-coordinates)]
  9111. "--"
  9112. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9113. :style toggle :selected org-table-buffer-is-an]))
  9114. (defvar org-pos)
  9115. (defun org-table-edit-formulas ()
  9116. "Edit the formulas of the current table in a separate buffer."
  9117. (interactive)
  9118. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9119. (beginning-of-line 0))
  9120. (unless (org-at-table-p) (error "Not at a table"))
  9121. (org-table-get-specials)
  9122. (let ((key (org-table-current-field-formula 'key 'noerror))
  9123. (eql (sort (org-table-get-stored-formulas 'noerror)
  9124. 'org-table-formula-less-p))
  9125. (pos (move-marker (make-marker) (point)))
  9126. (startline 1)
  9127. (wc (current-window-configuration))
  9128. (titles '((column . "# Column Formulas\n")
  9129. (field . "# Field Formulas\n")
  9130. (named . "# Named Field Formulas\n")))
  9131. entry s type title)
  9132. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9133. (erase-buffer)
  9134. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9135. (let ((font-lock-global-modes '(not fundamental-mode)))
  9136. (fundamental-mode))
  9137. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9138. (org-set-local 'org-pos pos)
  9139. (org-set-local 'org-window-configuration wc)
  9140. (use-local-map org-table-fedit-map)
  9141. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9142. (easy-menu-add org-table-fedit-menu)
  9143. (setq startline (org-current-line))
  9144. (while (setq entry (pop eql))
  9145. (setq type (cond
  9146. ((equal (string-to-char (car entry)) ?@) 'field)
  9147. ((string-match "^[0-9]" (car entry)) 'column)
  9148. (t 'named)))
  9149. (when (setq title (assq type titles))
  9150. (or (bobp) (insert "\n"))
  9151. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9152. (setq titles (delq title titles)))
  9153. (if (equal key (car entry)) (setq startline (org-current-line)))
  9154. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9155. (car entry) " = " (cdr entry) "\n"))
  9156. (remove-text-properties 0 (length s) '(face nil) s)
  9157. (insert s))
  9158. (if (eq org-table-use-standard-references t)
  9159. (org-table-fedit-toggle-ref-type))
  9160. (goto-line startline)
  9161. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9162. (defun org-table-fedit-post-command ()
  9163. (when (not (memq this-command '(lisp-complete-symbol)))
  9164. (let ((win (selected-window)))
  9165. (save-excursion
  9166. (condition-case nil
  9167. (org-table-show-reference)
  9168. (error nil))
  9169. (select-window win)))))
  9170. (defun org-table-formula-to-user (s)
  9171. "Convert a formula from internal to user representation."
  9172. (if (eq org-table-use-standard-references t)
  9173. (org-table-convert-refs-to-an s)
  9174. s))
  9175. (defun org-table-formula-from-user (s)
  9176. "Convert a formula from user to internal representation."
  9177. (if org-table-use-standard-references
  9178. (org-table-convert-refs-to-rc s)
  9179. s))
  9180. (defun org-table-convert-refs-to-rc (s)
  9181. "Convert spreadsheet references from AB7 to @7$28.
  9182. Works for single references, but also for entire formulas and even the
  9183. full TBLFM line."
  9184. (let ((start 0))
  9185. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9186. (cond
  9187. ((match-end 3)
  9188. ;; format match, just advance
  9189. (setq start (match-end 0)))
  9190. ((and (> (match-beginning 0) 0)
  9191. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9192. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9193. ;; 3.e5 or something like this.
  9194. (setq start (match-end 0)))
  9195. (t
  9196. (setq start (match-beginning 0)
  9197. s (replace-match
  9198. (if (equal (match-string 2 s) "&")
  9199. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9200. (format "@%d$%d"
  9201. (string-to-number (match-string 2 s))
  9202. (org-letters-to-number (match-string 1 s))))
  9203. t t s)))))
  9204. s))
  9205. (defun org-table-convert-refs-to-an (s)
  9206. "Convert spreadsheet references from to @7$28 to AB7.
  9207. Works for single references, but also for entire formulas and even the
  9208. full TBLFM line."
  9209. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9210. (setq s (replace-match
  9211. (format "%s%d"
  9212. (org-number-to-letters
  9213. (string-to-number (match-string 2 s)))
  9214. (string-to-number (match-string 1 s)))
  9215. t t s)))
  9216. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9217. (setq s (replace-match (concat "\\1"
  9218. (org-number-to-letters
  9219. (string-to-number (match-string 2 s))) "&")
  9220. t nil s)))
  9221. s)
  9222. (defun org-letters-to-number (s)
  9223. "Convert a base 26 number represented by letters into an integer.
  9224. For example: AB -> 28."
  9225. (let ((n 0))
  9226. (setq s (upcase s))
  9227. (while (> (length s) 0)
  9228. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9229. s (substring s 1)))
  9230. n))
  9231. (defun org-number-to-letters (n)
  9232. "Convert an integer into a base 26 number represented by letters.
  9233. For example: 28 -> AB."
  9234. (let ((s ""))
  9235. (while (> n 0)
  9236. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9237. n (/ (1- n) 26)))
  9238. s))
  9239. (defun org-table-fedit-convert-buffer (function)
  9240. "Convert all references in this buffer, using FUNTION."
  9241. (let ((line (org-current-line)))
  9242. (goto-char (point-min))
  9243. (while (not (eobp))
  9244. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9245. (delete-region (point) (point-at-eol))
  9246. (or (eobp) (forward-char 1)))
  9247. (goto-line line)))
  9248. (defun org-table-fedit-toggle-ref-type ()
  9249. "Convert all references in the buffer from B3 to @3$2 and back."
  9250. (interactive)
  9251. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9252. (org-table-fedit-convert-buffer
  9253. (if org-table-buffer-is-an
  9254. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9255. (message "Reference type switched to %s"
  9256. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9257. (defun org-table-fedit-ref-up ()
  9258. "Shift the reference at point one row/hline up."
  9259. (interactive)
  9260. (org-table-fedit-shift-reference 'up))
  9261. (defun org-table-fedit-ref-down ()
  9262. "Shift the reference at point one row/hline down."
  9263. (interactive)
  9264. (org-table-fedit-shift-reference 'down))
  9265. (defun org-table-fedit-ref-left ()
  9266. "Shift the reference at point one field to the left."
  9267. (interactive)
  9268. (org-table-fedit-shift-reference 'left))
  9269. (defun org-table-fedit-ref-right ()
  9270. "Shift the reference at point one field to the right."
  9271. (interactive)
  9272. (org-table-fedit-shift-reference 'right))
  9273. (defun org-table-fedit-shift-reference (dir)
  9274. (cond
  9275. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9276. (if (memq dir '(left right))
  9277. (org-rematch-and-replace 1 (eq dir 'left))
  9278. (error "Cannot shift reference in this direction")))
  9279. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9280. ;; A B3-like reference
  9281. (if (memq dir '(up down))
  9282. (org-rematch-and-replace 2 (eq dir 'up))
  9283. (org-rematch-and-replace 1 (eq dir 'left))))
  9284. ((org-at-regexp-p
  9285. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9286. ;; An internal reference
  9287. (if (memq dir '(up down))
  9288. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9289. (org-rematch-and-replace 5 (eq dir 'left))))))
  9290. (defun org-rematch-and-replace (n &optional decr hline)
  9291. "Re-match the group N, and replace it with the shifted refrence."
  9292. (or (match-end n) (error "Cannot shift reference in this direction"))
  9293. (goto-char (match-beginning n))
  9294. (and (looking-at (regexp-quote (match-string n)))
  9295. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9296. t t)))
  9297. (defun org-shift-refpart (ref &optional decr hline)
  9298. "Shift a refrence part REF.
  9299. If DECR is set, decrease the references row/column, else increase.
  9300. If HLINE is set, this may be a hline reference, it certainly is not
  9301. a translation reference."
  9302. (save-match-data
  9303. (let* ((sign (string-match "^[-+]" ref)) n)
  9304. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9305. (cond
  9306. ((and hline (string-match "^I+" ref))
  9307. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9308. (setq n (+ n (if decr -1 1)))
  9309. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9310. (if sign
  9311. (setq sign (if (< n 0) "-" "+") n (abs n))
  9312. (setq n (max 1 n)))
  9313. (concat sign (make-string n ?I)))
  9314. ((string-match "^[0-9]+" ref)
  9315. (setq n (string-to-number (concat sign ref)))
  9316. (setq n (+ n (if decr -1 1)))
  9317. (if sign
  9318. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9319. (number-to-string (max 1 n))))
  9320. ((string-match "^[a-zA-Z]+" ref)
  9321. (org-number-to-letters
  9322. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9323. (t (error "Cannot shift reference"))))))
  9324. (defun org-table-fedit-toggle-coordinates ()
  9325. "Toggle the display of coordinates in the refrenced table."
  9326. (interactive)
  9327. (let ((pos (marker-position org-pos)))
  9328. (with-current-buffer (marker-buffer org-pos)
  9329. (save-excursion
  9330. (goto-char pos)
  9331. (org-table-toggle-coordinate-overlays)))))
  9332. (defun org-table-fedit-finish (&optional arg)
  9333. "Parse the buffer for formula definitions and install them.
  9334. With prefix ARG, apply the new formulas to the table."
  9335. (interactive "P")
  9336. (org-table-remove-rectangle-highlight)
  9337. (if org-table-use-standard-references
  9338. (progn
  9339. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9340. (setq org-table-buffer-is-an nil)))
  9341. (let ((pos org-pos) eql var form)
  9342. (goto-char (point-min))
  9343. (while (re-search-forward
  9344. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9345. nil t)
  9346. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9347. form (match-string 3))
  9348. (setq form (org-trim form))
  9349. (when (not (equal form ""))
  9350. (while (string-match "[ \t]*\n[ \t]*" form)
  9351. (setq form (replace-match " " t t form)))
  9352. (when (assoc var eql)
  9353. (error "Double formulas for %s" var))
  9354. (push (cons var form) eql)))
  9355. (setq org-pos nil)
  9356. (set-window-configuration org-window-configuration)
  9357. (select-window (get-buffer-window (marker-buffer pos)))
  9358. (goto-char pos)
  9359. (unless (org-at-table-p)
  9360. (error "Lost table position - cannot install formulae"))
  9361. (org-table-store-formulas eql)
  9362. (move-marker pos nil)
  9363. (kill-buffer "*Edit Formulas*")
  9364. (if arg
  9365. (org-table-recalculate 'all)
  9366. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9367. (defun org-table-fedit-abort ()
  9368. "Abort editing formulas, without installing the changes."
  9369. (interactive)
  9370. (org-table-remove-rectangle-highlight)
  9371. (let ((pos org-pos))
  9372. (set-window-configuration org-window-configuration)
  9373. (select-window (get-buffer-window (marker-buffer pos)))
  9374. (goto-char pos)
  9375. (move-marker pos nil)
  9376. (message "Formula editing aborted without installing changes")))
  9377. (defun org-table-fedit-lisp-indent ()
  9378. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9379. (interactive)
  9380. (let ((pos (point)) beg end ind)
  9381. (beginning-of-line 1)
  9382. (cond
  9383. ((looking-at "[ \t]")
  9384. (goto-char pos)
  9385. (call-interactively 'lisp-indent-line))
  9386. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9387. ((not (fboundp 'pp-buffer))
  9388. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9389. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9390. (goto-char (- (match-end 0) 2))
  9391. (setq beg (point))
  9392. (setq ind (make-string (current-column) ?\ ))
  9393. (condition-case nil (forward-sexp 1)
  9394. (error
  9395. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9396. (setq end (point))
  9397. (save-restriction
  9398. (narrow-to-region beg end)
  9399. (if (eq last-command this-command)
  9400. (progn
  9401. (goto-char (point-min))
  9402. (setq this-command nil)
  9403. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9404. (replace-match " ")))
  9405. (pp-buffer)
  9406. (untabify (point-min) (point-max))
  9407. (goto-char (1+ (point-min)))
  9408. (while (re-search-forward "^." nil t)
  9409. (beginning-of-line 1)
  9410. (insert ind))
  9411. (goto-char (point-max))
  9412. (backward-delete-char 1)))
  9413. (goto-char beg))
  9414. (t nil))))
  9415. (defvar org-show-positions nil)
  9416. (defun org-table-show-reference (&optional local)
  9417. "Show the location/value of the $ expression at point."
  9418. (interactive)
  9419. (org-table-remove-rectangle-highlight)
  9420. (catch 'exit
  9421. (let ((pos (if local (point) org-pos))
  9422. (face2 'highlight)
  9423. (org-inhibit-highlight-removal t)
  9424. (win (selected-window))
  9425. (org-show-positions nil)
  9426. var name e what match dest)
  9427. (if local (org-table-get-specials))
  9428. (setq what (cond
  9429. ((or (org-at-regexp-p org-table-range-regexp2)
  9430. (org-at-regexp-p org-table-translate-regexp)
  9431. (org-at-regexp-p org-table-range-regexp))
  9432. (setq match
  9433. (save-match-data
  9434. (org-table-convert-refs-to-rc (match-string 0))))
  9435. 'range)
  9436. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9437. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9438. ((not local) nil)
  9439. (t (error "No reference at point")))
  9440. match (and what (or match (match-string 0))))
  9441. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9442. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9443. 'secondary-selection))
  9444. (org-add-hook 'before-change-functions
  9445. 'org-table-remove-rectangle-highlight)
  9446. (if (eq what 'name) (setq var (substring match 1)))
  9447. (when (eq what 'range)
  9448. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9449. (setq match (org-table-formula-substitute-names match)))
  9450. (unless local
  9451. (save-excursion
  9452. (end-of-line 1)
  9453. (re-search-backward "^\\S-" nil t)
  9454. (beginning-of-line 1)
  9455. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9456. (setq dest
  9457. (save-match-data
  9458. (org-table-convert-refs-to-rc (match-string 1))))
  9459. (org-table-add-rectangle-overlay
  9460. (match-beginning 1) (match-end 1) face2))))
  9461. (if (and (markerp pos) (marker-buffer pos))
  9462. (if (get-buffer-window (marker-buffer pos))
  9463. (select-window (get-buffer-window (marker-buffer pos)))
  9464. (org-switch-to-buffer-other-window (get-buffer-window
  9465. (marker-buffer pos)))))
  9466. (goto-char pos)
  9467. (org-table-force-dataline)
  9468. (when dest
  9469. (setq name (substring dest 1))
  9470. (cond
  9471. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9472. (setq e (assoc name org-table-named-field-locations))
  9473. (goto-line (nth 1 e))
  9474. (org-table-goto-column (nth 2 e)))
  9475. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9476. (let ((l (string-to-number (match-string 1 dest)))
  9477. (c (string-to-number (match-string 2 dest))))
  9478. (goto-line (aref org-table-dlines l))
  9479. (org-table-goto-column c)))
  9480. (t (org-table-goto-column (string-to-number name))))
  9481. (move-marker pos (point))
  9482. (org-table-highlight-rectangle nil nil face2))
  9483. (cond
  9484. ((equal dest match))
  9485. ((not match))
  9486. ((eq what 'range)
  9487. (condition-case nil
  9488. (save-excursion
  9489. (org-table-get-range match nil nil 'highlight))
  9490. (error nil)))
  9491. ((setq e (assoc var org-table-named-field-locations))
  9492. (goto-line (nth 1 e))
  9493. (org-table-goto-column (nth 2 e))
  9494. (org-table-highlight-rectangle (point) (point))
  9495. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9496. ((setq e (assoc var org-table-column-names))
  9497. (org-table-goto-column (string-to-number (cdr e)))
  9498. (org-table-highlight-rectangle (point) (point))
  9499. (goto-char (org-table-begin))
  9500. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9501. (org-table-end) t)
  9502. (progn
  9503. (goto-char (match-beginning 1))
  9504. (org-table-highlight-rectangle)
  9505. (message "Named column (column %s)" (cdr e)))
  9506. (error "Column name not found")))
  9507. ((eq what 'column)
  9508. ;; column number
  9509. (org-table-goto-column (string-to-number (substring match 1)))
  9510. (org-table-highlight-rectangle (point) (point))
  9511. (message "Column %s" (substring match 1)))
  9512. ((setq e (assoc var org-table-local-parameters))
  9513. (goto-char (org-table-begin))
  9514. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9515. (progn
  9516. (goto-char (match-beginning 1))
  9517. (org-table-highlight-rectangle)
  9518. (message "Local parameter."))
  9519. (error "Parameter not found")))
  9520. (t
  9521. (cond
  9522. ((not var) (error "No reference at point"))
  9523. ((setq e (assoc var org-table-formula-constants-local))
  9524. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9525. var (cdr e)))
  9526. ((setq e (assoc var org-table-formula-constants))
  9527. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9528. var (cdr e)))
  9529. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9530. (message "Constant: $%s=%s, from `constants.el'%s."
  9531. var e (format " (%s units)" constants-unit-system)))
  9532. (t (error "Undefined name $%s" var)))))
  9533. (goto-char pos)
  9534. (when (and org-show-positions
  9535. (not (memq this-command '(org-table-fedit-scroll
  9536. org-table-fedit-scroll-down))))
  9537. (push pos org-show-positions)
  9538. (push org-table-current-begin-pos org-show-positions)
  9539. (let ((min (apply 'min org-show-positions))
  9540. (max (apply 'max org-show-positions)))
  9541. (goto-char min) (recenter 0)
  9542. (goto-char max)
  9543. (or (pos-visible-in-window-p max) (recenter -1))))
  9544. (select-window win))))
  9545. (defun org-table-force-dataline ()
  9546. "Make sure the cursor is in a dataline in a table."
  9547. (unless (save-excursion
  9548. (beginning-of-line 1)
  9549. (looking-at org-table-dataline-regexp))
  9550. (let* ((re org-table-dataline-regexp)
  9551. (p1 (save-excursion (re-search-forward re nil 'move)))
  9552. (p2 (save-excursion (re-search-backward re nil 'move))))
  9553. (cond ((and p1 p2)
  9554. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9555. p1 p2)))
  9556. ((or p1 p2) (goto-char (or p1 p2)))
  9557. (t (error "No table dataline around here"))))))
  9558. (defun org-table-fedit-line-up ()
  9559. "Move cursor one line up in the window showing the table."
  9560. (interactive)
  9561. (org-table-fedit-move 'previous-line))
  9562. (defun org-table-fedit-line-down ()
  9563. "Move cursor one line down in the window showing the table."
  9564. (interactive)
  9565. (org-table-fedit-move 'next-line))
  9566. (defun org-table-fedit-move (command)
  9567. "Move the cursor in the window shoinw the table.
  9568. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9569. (let ((org-table-allow-automatic-line-recalculation nil)
  9570. (pos org-pos) (win (selected-window)) p)
  9571. (select-window (get-buffer-window (marker-buffer org-pos)))
  9572. (setq p (point))
  9573. (call-interactively command)
  9574. (while (and (org-at-table-p)
  9575. (org-at-table-hline-p))
  9576. (call-interactively command))
  9577. (or (org-at-table-p) (goto-char p))
  9578. (move-marker pos (point))
  9579. (select-window win)))
  9580. (defun org-table-fedit-scroll (N)
  9581. (interactive "p")
  9582. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9583. (scroll-other-window N)))
  9584. (defun org-table-fedit-scroll-down (N)
  9585. (interactive "p")
  9586. (org-table-fedit-scroll (- N)))
  9587. (defvar org-table-rectangle-overlays nil)
  9588. (defun org-table-add-rectangle-overlay (beg end &optional face)
  9589. "Add a new overlay."
  9590. (let ((ov (org-make-overlay beg end)))
  9591. (org-overlay-put ov 'face (or face 'secondary-selection))
  9592. (push ov org-table-rectangle-overlays)))
  9593. (defun org-table-highlight-rectangle (&optional beg end face)
  9594. "Highlight rectangular region in a table."
  9595. (setq beg (or beg (point)) end (or end (point)))
  9596. (let ((b (min beg end))
  9597. (e (max beg end))
  9598. l1 c1 l2 c2 tmp)
  9599. (and (boundp 'org-show-positions)
  9600. (setq org-show-positions (cons b (cons e org-show-positions))))
  9601. (goto-char (min beg end))
  9602. (setq l1 (org-current-line)
  9603. c1 (org-table-current-column))
  9604. (goto-char (max beg end))
  9605. (setq l2 (org-current-line)
  9606. c2 (org-table-current-column))
  9607. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  9608. (goto-line l1)
  9609. (beginning-of-line 1)
  9610. (loop for line from l1 to l2 do
  9611. (when (looking-at org-table-dataline-regexp)
  9612. (org-table-goto-column c1)
  9613. (skip-chars-backward "^|\n") (setq beg (point))
  9614. (org-table-goto-column c2)
  9615. (skip-chars-forward "^|\n") (setq end (point))
  9616. (org-table-add-rectangle-overlay beg end face))
  9617. (beginning-of-line 2))
  9618. (goto-char b))
  9619. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  9620. (defun org-table-remove-rectangle-highlight (&rest ignore)
  9621. "Remove the rectangle overlays."
  9622. (unless org-inhibit-highlight-removal
  9623. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  9624. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  9625. (setq org-table-rectangle-overlays nil)))
  9626. (defvar org-table-coordinate-overlays nil
  9627. "Collects the cooordinate grid overlays, so that they can be removed.")
  9628. (make-variable-buffer-local 'org-table-coordinate-overlays)
  9629. (defun org-table-overlay-coordinates ()
  9630. "Add overlays to the table at point, to show row/column coordinates."
  9631. (interactive)
  9632. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9633. (setq org-table-coordinate-overlays nil)
  9634. (save-excursion
  9635. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  9636. (goto-char (org-table-begin))
  9637. (while (org-at-table-p)
  9638. (setq eol (point-at-eol))
  9639. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  9640. (push ov org-table-coordinate-overlays)
  9641. (setq hline (looking-at org-table-hline-regexp))
  9642. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  9643. (format "%4d" (setq id (1+ id)))))
  9644. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  9645. (when hline
  9646. (setq ic 0)
  9647. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  9648. (setq beg (1+ (match-beginning 0))
  9649. ic (1+ ic)
  9650. s1 (concat "$" (int-to-string ic))
  9651. s2 (org-number-to-letters ic)
  9652. str (if (eq org-table-use-standard-references t) s2 s1))
  9653. (setq ov (org-make-overlay beg (+ beg (length str))))
  9654. (push ov org-table-coordinate-overlays)
  9655. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  9656. (beginning-of-line 2)))))
  9657. (defun org-table-toggle-coordinate-overlays ()
  9658. "Toggle the display of Row/Column numbers in tables."
  9659. (interactive)
  9660. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  9661. (message "Row/Column number display turned %s"
  9662. (if org-table-overlay-coordinates "on" "off"))
  9663. (if (and (org-at-table-p) org-table-overlay-coordinates)
  9664. (org-table-align))
  9665. (unless org-table-overlay-coordinates
  9666. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9667. (setq org-table-coordinate-overlays nil)))
  9668. (defun org-table-toggle-formula-debugger ()
  9669. "Toggle the formula debugger in tables."
  9670. (interactive)
  9671. (setq org-table-formula-debug (not org-table-formula-debug))
  9672. (message "Formula debugging has been turned %s"
  9673. (if org-table-formula-debug "on" "off")))
  9674. ;;; The orgtbl minor mode
  9675. ;; Define a minor mode which can be used in other modes in order to
  9676. ;; integrate the org-mode table editor.
  9677. ;; This is really a hack, because the org-mode table editor uses several
  9678. ;; keys which normally belong to the major mode, for example the TAB and
  9679. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9680. ;; necessary to operate the table editor, but wraps the commands into a
  9681. ;; function which tests if the cursor is currently inside a table. If that
  9682. ;; is the case, the table editor command is executed. However, when any of
  9683. ;; those keys is used outside a table, the function uses `key-binding' to
  9684. ;; look up if the key has an associated command in another currently active
  9685. ;; keymap (minor modes, major mode, global), and executes that command.
  9686. ;; There might be problems if any of the keys used by the table editor is
  9687. ;; otherwise used as a prefix key.
  9688. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9689. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9690. ;; addresses this by checking explicitly for both bindings.
  9691. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9692. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9693. ;; Some modes bind other commands to simple characters, for example
  9694. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9695. ;; active, this binding is ignored inside tables and replaced with a
  9696. ;; modified self-insert.
  9697. (defvar orgtbl-mode nil
  9698. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9699. table editor in arbitrary modes.")
  9700. (make-variable-buffer-local 'orgtbl-mode)
  9701. (defvar orgtbl-mode-map (make-keymap)
  9702. "Keymap for `orgtbl-mode'.")
  9703. ;;;###autoload
  9704. (defun turn-on-orgtbl ()
  9705. "Unconditionally turn on `orgtbl-mode'."
  9706. (orgtbl-mode 1))
  9707. (defvar org-old-auto-fill-inhibit-regexp nil
  9708. "Local variable used by `orgtbl-mode'")
  9709. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  9710. "Matches a line belonging to an orgtbl.")
  9711. (defconst orgtbl-extra-font-lock-keywords
  9712. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  9713. 0 (quote 'org-table) 'prepend))
  9714. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  9715. ;;;###autoload
  9716. (defun orgtbl-mode (&optional arg)
  9717. "The `org-mode' table editor as a minor mode for use in other modes."
  9718. (interactive)
  9719. (if (org-mode-p)
  9720. ;; Exit without error, in case some hook functions calls this
  9721. ;; by accident in org-mode.
  9722. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9723. (setq orgtbl-mode
  9724. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9725. (if orgtbl-mode
  9726. (progn
  9727. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9728. ;; Make sure we are first in minor-mode-map-alist
  9729. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9730. (and c (setq minor-mode-map-alist
  9731. (cons c (delq c minor-mode-map-alist)))))
  9732. (org-set-local (quote org-table-may-need-update) t)
  9733. (org-add-hook 'before-change-functions 'org-before-change-function
  9734. nil 'local)
  9735. (org-set-local 'org-old-auto-fill-inhibit-regexp
  9736. auto-fill-inhibit-regexp)
  9737. (org-set-local 'auto-fill-inhibit-regexp
  9738. (if auto-fill-inhibit-regexp
  9739. (concat orgtbl-line-start-regexp "\\|"
  9740. auto-fill-inhibit-regexp)
  9741. orgtbl-line-start-regexp))
  9742. (org-add-to-invisibility-spec '(org-cwidth))
  9743. (when (fboundp 'font-lock-add-keywords)
  9744. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  9745. (org-restart-font-lock))
  9746. (easy-menu-add orgtbl-mode-menu)
  9747. (run-hooks 'orgtbl-mode-hook))
  9748. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9749. (org-cleanup-narrow-column-properties)
  9750. (org-remove-from-invisibility-spec '(org-cwidth))
  9751. (remove-hook 'before-change-functions 'org-before-change-function t)
  9752. (when (fboundp 'font-lock-remove-keywords)
  9753. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  9754. (org-restart-font-lock))
  9755. (easy-menu-remove orgtbl-mode-menu)
  9756. (force-mode-line-update 'all))))
  9757. (defun org-cleanup-narrow-column-properties ()
  9758. "Remove all properties related to narrow-column invisibility."
  9759. (let ((s 1))
  9760. (while (setq s (text-property-any s (point-max)
  9761. 'display org-narrow-column-arrow))
  9762. (remove-text-properties s (1+ s) '(display t)))
  9763. (setq s 1)
  9764. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  9765. (remove-text-properties s (1+ s) '(org-cwidth t)))
  9766. (setq s 1)
  9767. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  9768. (remove-text-properties s (1+ s) '(invisible t)))))
  9769. ;; Install it as a minor mode.
  9770. (put 'orgtbl-mode :included t)
  9771. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9772. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9773. (defun orgtbl-make-binding (fun n &rest keys)
  9774. "Create a function for binding in the table minor mode.
  9775. FUN is the command to call inside a table. N is used to create a unique
  9776. command name. KEYS are keys that should be checked in for a command
  9777. to execute outside of tables."
  9778. (eval
  9779. (list 'defun
  9780. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9781. '(arg)
  9782. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9783. "Outside of tables, run the binding of `"
  9784. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9785. "'.")
  9786. '(interactive "p")
  9787. (list 'if
  9788. '(org-at-table-p)
  9789. (list 'call-interactively (list 'quote fun))
  9790. (list 'let '(orgtbl-mode)
  9791. (list 'call-interactively
  9792. (append '(or)
  9793. (mapcar (lambda (k)
  9794. (list 'key-binding k))
  9795. keys)
  9796. '('orgtbl-error))))))))
  9797. (defun orgtbl-error ()
  9798. "Error when there is no default binding for a table key."
  9799. (interactive)
  9800. (error "This key is has no function outside tables"))
  9801. (defun orgtbl-setup ()
  9802. "Setup orgtbl keymaps."
  9803. (let ((nfunc 0)
  9804. (bindings
  9805. (list
  9806. '([(meta shift left)] org-table-delete-column)
  9807. '([(meta left)] org-table-move-column-left)
  9808. '([(meta right)] org-table-move-column-right)
  9809. '([(meta shift right)] org-table-insert-column)
  9810. '([(meta shift up)] org-table-kill-row)
  9811. '([(meta shift down)] org-table-insert-row)
  9812. '([(meta up)] org-table-move-row-up)
  9813. '([(meta down)] org-table-move-row-down)
  9814. '("\C-c\C-w" org-table-cut-region)
  9815. '("\C-c\M-w" org-table-copy-region)
  9816. '("\C-c\C-y" org-table-paste-rectangle)
  9817. '("\C-c-" org-table-insert-hline)
  9818. '("\C-c}" org-table-toggle-coordinate-overlays)
  9819. '("\C-c{" org-table-toggle-formula-debugger)
  9820. '("\C-m" org-table-next-row)
  9821. '([(shift return)] org-table-copy-down)
  9822. '("\C-c\C-q" org-table-wrap-region)
  9823. '("\C-c?" org-table-field-info)
  9824. '("\C-c " org-table-blank-field)
  9825. '("\C-c+" org-table-sum)
  9826. '("\C-c=" org-table-eval-formula)
  9827. '("\C-c'" org-table-edit-formulas)
  9828. '("\C-c`" org-table-edit-field)
  9829. '("\C-c*" org-table-recalculate)
  9830. '("\C-c|" org-table-create-or-convert-from-region)
  9831. '("\C-c^" org-table-sort-lines)
  9832. '([(control ?#)] org-table-rotate-recalc-marks)))
  9833. elt key fun cmd)
  9834. (while (setq elt (pop bindings))
  9835. (setq nfunc (1+ nfunc))
  9836. (setq key (org-key (car elt))
  9837. fun (nth 1 elt)
  9838. cmd (orgtbl-make-binding fun nfunc key))
  9839. (org-defkey orgtbl-mode-map key cmd))
  9840. ;; Special treatment needed for TAB and RET
  9841. (org-defkey orgtbl-mode-map [(return)]
  9842. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9843. (org-defkey orgtbl-mode-map "\C-m"
  9844. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9845. (org-defkey orgtbl-mode-map [(tab)]
  9846. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9847. (org-defkey orgtbl-mode-map "\C-i"
  9848. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  9849. (org-defkey orgtbl-mode-map [(shift tab)]
  9850. (orgtbl-make-binding 'org-table-previous-field 104
  9851. [(shift tab)] [(tab)] "\C-i"))
  9852. (org-defkey orgtbl-mode-map "\M-\C-m"
  9853. (orgtbl-make-binding 'org-table-wrap-region 105
  9854. "\M-\C-m" [(meta return)]))
  9855. (org-defkey orgtbl-mode-map [(meta return)]
  9856. (orgtbl-make-binding 'org-table-wrap-region 106
  9857. [(meta return)] "\M-\C-m"))
  9858. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  9859. (when orgtbl-optimized
  9860. ;; If the user wants maximum table support, we need to hijack
  9861. ;; some standard editing functions
  9862. (org-remap orgtbl-mode-map
  9863. 'self-insert-command 'orgtbl-self-insert-command
  9864. 'delete-char 'org-delete-char
  9865. 'delete-backward-char 'org-delete-backward-char)
  9866. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  9867. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9868. '("OrgTbl"
  9869. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9870. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9871. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9872. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9873. "--"
  9874. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9875. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9876. ["Copy Field from Above"
  9877. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9878. "--"
  9879. ("Column"
  9880. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9881. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9882. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9883. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  9884. ("Row"
  9885. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9886. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9887. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9888. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9889. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9890. "--"
  9891. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9892. ("Rectangle"
  9893. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9894. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9895. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9896. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9897. "--"
  9898. ("Radio tables"
  9899. ["Insert table template" orgtbl-insert-radio-table
  9900. (assq major-mode orgtbl-radio-table-templates)]
  9901. ["Comment/uncomment table" orgtbl-toggle-comment t])
  9902. "--"
  9903. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9904. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9905. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9906. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9907. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9908. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  9909. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9910. ["Sum Column/Rectangle" org-table-sum
  9911. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9912. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9913. ["Debug Formulas"
  9914. org-table-toggle-formula-debugger :active (org-at-table-p)
  9915. :keys "C-c {"
  9916. :style toggle :selected org-table-formula-debug]
  9917. ["Show Col/Row Numbers"
  9918. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  9919. :keys "C-c }"
  9920. :style toggle :selected org-table-overlay-coordinates]
  9921. ))
  9922. t))
  9923. (defun orgtbl-ctrl-c-ctrl-c (arg)
  9924. "If the cursor is inside a table, realign the table.
  9925. It it is a table to be sent away to a receiver, do it.
  9926. With prefix arg, also recompute table."
  9927. (interactive "P")
  9928. (let ((pos (point)) action)
  9929. (save-excursion
  9930. (beginning-of-line 1)
  9931. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  9932. ((looking-at "[ \t]*|") pos)
  9933. ((looking-at "#\\+TBLFM:") 'recalc))))
  9934. (cond
  9935. ((integerp action)
  9936. (goto-char action)
  9937. (org-table-maybe-eval-formula)
  9938. (if arg
  9939. (call-interactively 'org-table-recalculate)
  9940. (org-table-maybe-recalculate-line))
  9941. (call-interactively 'org-table-align)
  9942. (orgtbl-send-table 'maybe))
  9943. ((eq action 'recalc)
  9944. (save-excursion
  9945. (beginning-of-line 1)
  9946. (skip-chars-backward " \r\n\t")
  9947. (if (org-at-table-p)
  9948. (org-call-with-arg 'org-table-recalculate t))))
  9949. (t (let (orgtbl-mode)
  9950. (call-interactively (key-binding "\C-c\C-c")))))))
  9951. (defun orgtbl-tab (arg)
  9952. "Justification and field motion for `orgtbl-mode'."
  9953. (interactive "P")
  9954. (if arg (org-table-edit-field t)
  9955. (org-table-justify-field-maybe)
  9956. (org-table-next-field)))
  9957. (defun orgtbl-ret ()
  9958. "Justification and field motion for `orgtbl-mode'."
  9959. (interactive)
  9960. (org-table-justify-field-maybe)
  9961. (org-table-next-row))
  9962. (defun orgtbl-self-insert-command (N)
  9963. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9964. If the cursor is in a table looking at whitespace, the whitespace is
  9965. overwritten, and the table is not marked as requiring realignment."
  9966. (interactive "p")
  9967. (if (and (org-at-table-p)
  9968. (or
  9969. (and org-table-auto-blank-field
  9970. (member last-command
  9971. '(orgtbl-hijacker-command-100
  9972. orgtbl-hijacker-command-101
  9973. orgtbl-hijacker-command-102
  9974. orgtbl-hijacker-command-103
  9975. orgtbl-hijacker-command-104
  9976. orgtbl-hijacker-command-105))
  9977. (org-table-blank-field))
  9978. t)
  9979. (eq N 1)
  9980. (looking-at "[^|\n]* +|"))
  9981. (let (org-table-may-need-update)
  9982. (goto-char (1- (match-end 0)))
  9983. (delete-backward-char 1)
  9984. (goto-char (match-beginning 0))
  9985. (self-insert-command N))
  9986. (setq org-table-may-need-update t)
  9987. (let (orgtbl-mode)
  9988. (call-interactively (key-binding (vector last-input-event))))))
  9989. (defun org-force-self-insert (N)
  9990. "Needed to enforce self-insert under remapping."
  9991. (interactive "p")
  9992. (self-insert-command N))
  9993. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  9994. "Regula expression matching exponentials as produced by calc.")
  9995. (defvar org-table-clean-did-remove-column nil)
  9996. (defun orgtbl-export (table target)
  9997. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  9998. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  9999. org-table-last-alignment org-table-last-column-widths
  10000. maxcol column)
  10001. (if (not (fboundp func))
  10002. (error "Cannot export orgtbl table to %s" target))
  10003. (setq lines (org-table-clean-before-export lines))
  10004. (setq table
  10005. (mapcar
  10006. (lambda (x)
  10007. (if (string-match org-table-hline-regexp x)
  10008. 'hline
  10009. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10010. lines))
  10011. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10012. table)))
  10013. (loop for i from (1- maxcol) downto 0 do
  10014. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10015. (setq column (delq nil column))
  10016. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10017. (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))
  10018. (funcall func table nil)))
  10019. (defun orgtbl-send-table (&optional maybe)
  10020. "Send a tranformed version of this table to the receiver position.
  10021. With argument MAYBE, fail quietly if no transformation is defined for
  10022. this table."
  10023. (interactive)
  10024. (catch 'exit
  10025. (unless (org-at-table-p) (error "Not at a table"))
  10026. ;; when non-interactive, we assume align has just happened.
  10027. (when (interactive-p) (org-table-align))
  10028. (save-excursion
  10029. (goto-char (org-table-begin))
  10030. (beginning-of-line 0)
  10031. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10032. (if maybe
  10033. (throw 'exit nil)
  10034. (error "Don't know how to transform this table."))))
  10035. (let* ((name (match-string 1))
  10036. beg
  10037. (transform (intern (match-string 2)))
  10038. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10039. (skip (plist-get params :skip))
  10040. (skipcols (plist-get params :skipcols))
  10041. (txt (buffer-substring-no-properties
  10042. (org-table-begin) (org-table-end)))
  10043. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10044. (lines (org-table-clean-before-export lines))
  10045. (i0 (if org-table-clean-did-remove-column 2 1))
  10046. (table (mapcar
  10047. (lambda (x)
  10048. (if (string-match org-table-hline-regexp x)
  10049. 'hline
  10050. (org-remove-by-index
  10051. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10052. skipcols i0)))
  10053. lines))
  10054. (fun (if (= i0 2) 'cdr 'identity))
  10055. (org-table-last-alignment
  10056. (org-remove-by-index (funcall fun org-table-last-alignment)
  10057. skipcols i0))
  10058. (org-table-last-column-widths
  10059. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10060. skipcols i0)))
  10061. (unless (fboundp transform)
  10062. (error "No such transformation function %s" transform))
  10063. (setq txt (funcall transform table params))
  10064. ;; Find the insertion place
  10065. (save-excursion
  10066. (goto-char (point-min))
  10067. (unless (re-search-forward
  10068. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10069. (error "Don't know where to insert translated table"))
  10070. (goto-char (match-beginning 0))
  10071. (beginning-of-line 2)
  10072. (setq beg (point))
  10073. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10074. (error "Cannot find end of insertion region"))
  10075. (beginning-of-line 1)
  10076. (delete-region beg (point))
  10077. (goto-char beg)
  10078. (insert txt "\n"))
  10079. (message "Table converted and installed at receiver location"))))
  10080. (defun org-remove-by-index (list indices &optional i0)
  10081. "Remove the elements in LIST with indices in INDICES.
  10082. First element has index 0, or I0 if given."
  10083. (if (not indices)
  10084. list
  10085. (if (integerp indices) (setq indices (list indices)))
  10086. (setq i0 (1- (or i0 0)))
  10087. (delq :rm (mapcar (lambda (x)
  10088. (setq i0 (1+ i0))
  10089. (if (memq i0 indices) :rm x))
  10090. list))))
  10091. (defun orgtbl-toggle-comment ()
  10092. "Comment or uncomment the orgtbl at point."
  10093. (interactive)
  10094. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10095. (re2 (concat "^" orgtbl-line-start-regexp))
  10096. (commented (save-excursion (beginning-of-line 1)
  10097. (cond ((looking-at re1) t)
  10098. ((looking-at re2) nil)
  10099. (t (error "Not at an org table")))))
  10100. (re (if commented re1 re2))
  10101. beg end)
  10102. (save-excursion
  10103. (beginning-of-line 1)
  10104. (while (looking-at re) (beginning-of-line 0))
  10105. (beginning-of-line 2)
  10106. (setq beg (point))
  10107. (while (looking-at re) (beginning-of-line 2))
  10108. (setq end (point)))
  10109. (comment-region beg end (if commented '(4) nil))))
  10110. (defun orgtbl-insert-radio-table ()
  10111. "Insert a radio table template appropriate for this major mode."
  10112. (interactive)
  10113. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10114. (txt (nth 1 e))
  10115. name pos)
  10116. (unless e (error "No radio table setup defined for %s" major-mode))
  10117. (setq name (read-string "Table name: "))
  10118. (while (string-match "%n" txt)
  10119. (setq txt (replace-match name t t txt)))
  10120. (or (bolp) (insert "\n"))
  10121. (setq pos (point))
  10122. (insert txt)
  10123. (goto-char pos)))
  10124. (defun org-get-param (params header i sym &optional hsym)
  10125. "Get parameter value for symbol SYM.
  10126. If this is a header line, actually get the value for the symbol with an
  10127. additional \"h\" inserted after the colon.
  10128. If the value is a protperty list, get the element for the current column.
  10129. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10130. (let ((val (plist-get params sym)))
  10131. (and hsym header (setq val (or (plist-get params hsym) val)))
  10132. (if (consp val) (plist-get val i) val)))
  10133. (defun orgtbl-to-generic (table params)
  10134. "Convert the orgtbl-mode TABLE to some other format.
  10135. This generic routine can be used for many standard cases.
  10136. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10137. separator line, or a list of fields for that line.
  10138. PARAMS is a property list of parameters that can influence the conversion.
  10139. For the generic converter, some parameters are obligatory: You need to
  10140. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10141. :splice, you must have :tstart and :tend.
  10142. Valid parameters are
  10143. :tstart String to start the table. Ignored when :splice is t.
  10144. :tend String to end the table. Ignored when :splice is t.
  10145. :splice When set to t, return only table body lines, don't wrap
  10146. them into :tstart and :tend. Default is nil.
  10147. :hline String to be inserted on horizontal separation lines.
  10148. May be nil to ignore hlines.
  10149. :lstart String to start a new table line.
  10150. :lend String to end a table line
  10151. :sep Separator between two fields
  10152. :lfmt Format for entire line, with enough %s to capture all fields.
  10153. If this is present, :lstart, :lend, and :sep are ignored.
  10154. :fmt A format to be used to wrap the field, should contain
  10155. %s for the original field value. For example, to wrap
  10156. everything in dollars, you could use :fmt \"$%s$\".
  10157. This may also be a property list with column numbers and
  10158. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10159. :hlstart :hlend :hlsep :hlfmt :hfmt
  10160. Same as above, specific for the header lines in the table.
  10161. All lines before the first hline are treated as header.
  10162. If any of these is not present, the data line value is used.
  10163. :efmt Use this format to print numbers with exponentials.
  10164. The format should have %s twice for inserting mantissa
  10165. and exponent, for example \"%s\\\\times10^{%s}\". This
  10166. may also be a property list with column numbers and
  10167. formats. :fmt will still be applied after :efmt.
  10168. In addition to this, the parameters :skip and :skipcols are always handled
  10169. directly by `orgtbl-send-table'. See manual."
  10170. (interactive)
  10171. (let* ((p params)
  10172. (splicep (plist-get p :splice))
  10173. (hline (plist-get p :hline))
  10174. rtn line i fm efm lfmt h)
  10175. ;; Do we have a header?
  10176. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10177. (setq h t))
  10178. ;; Put header
  10179. (unless splicep
  10180. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10181. ;; Now loop over all lines
  10182. (while (setq line (pop table))
  10183. (if (eq line 'hline)
  10184. ;; A horizontal separator line
  10185. (progn (if hline (push hline rtn))
  10186. (setq h nil)) ; no longer in header
  10187. ;; A normal line. Convert the fields, push line onto the result list
  10188. (setq i 0)
  10189. (setq line
  10190. (mapcar
  10191. (lambda (f)
  10192. (setq i (1+ i)
  10193. fm (org-get-param p h i :fmt :hfmt)
  10194. efm (org-get-param p h i :efmt))
  10195. (if (and efm (string-match orgtbl-exp-regexp f))
  10196. (setq f (format
  10197. efm (match-string 1 f) (match-string 2 f))))
  10198. (if fm (setq f (format fm f)))
  10199. f)
  10200. line))
  10201. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10202. (push (apply 'format lfmt line) rtn)
  10203. (push (concat
  10204. (org-get-param p h i :lstart :hlstart)
  10205. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10206. (org-get-param p h i :lend :hlend))
  10207. rtn))))
  10208. (unless splicep
  10209. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10210. (mapconcat 'identity (nreverse rtn) "\n")))
  10211. (defun orgtbl-to-latex (table params)
  10212. "Convert the orgtbl-mode TABLE to LaTeX.
  10213. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10214. separator line, or a list of fields for that line.
  10215. PARAMS is a property list of parameters that can influence the conversion.
  10216. Supports all parameters from `orgtbl-to-generic'. Most important for
  10217. LaTeX are:
  10218. :splice When set to t, return only table body lines, don't wrap
  10219. them into a tabular environment. Default is nil.
  10220. :fmt A format to be used to wrap the field, should contain %s for the
  10221. original field value. For example, to wrap everything in dollars,
  10222. use :fmt \"$%s$\". This may also be a property list with column
  10223. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10224. :efmt Format for transforming numbers with exponentials. The format
  10225. should have %s twice for inserting mantissa and exponent, for
  10226. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10227. This may also be a property list with column numbers and formats.
  10228. The general parameters :skip and :skipcols have already been applied when
  10229. this function is called."
  10230. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10231. org-table-last-alignment ""))
  10232. (params2
  10233. (list
  10234. :tstart (concat "\\begin{tabular}{" alignment "}")
  10235. :tend "\\end{tabular}"
  10236. :lstart "" :lend " \\\\" :sep " & "
  10237. :efmt "%s\\,(%s)" :hline "\\hline")))
  10238. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10239. (defun orgtbl-to-html (table params)
  10240. "Convert the orgtbl-mode TABLE to LaTeX.
  10241. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10242. separator line, or a list of fields for that line.
  10243. PARAMS is a property list of parameters that can influence the conversion.
  10244. Currently this function recognizes the following parameters:
  10245. :splice When set to t, return only table body lines, don't wrap
  10246. them into a <table> environment. Default is nil.
  10247. The general parameters :skip and :skipcols have already been applied when
  10248. this function is called. The function does *not* use `orgtbl-to-generic',
  10249. so you cannot specify parameters for it."
  10250. (let* ((splicep (plist-get params :splice))
  10251. html)
  10252. ;; Just call the formatter we already have
  10253. ;; We need to make text lines for it, so put the fields back together.
  10254. (setq html (org-format-org-table-html
  10255. (mapcar
  10256. (lambda (x)
  10257. (if (eq x 'hline)
  10258. "|----+----|"
  10259. (concat "| " (mapconcat 'identity x " | ") " |")))
  10260. table)
  10261. splicep))
  10262. (if (string-match "\n+\\'" html)
  10263. (setq html (replace-match "" t t html)))
  10264. html))
  10265. (defun orgtbl-to-texinfo (table params)
  10266. "Convert the orgtbl-mode TABLE to TeXInfo.
  10267. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10268. separator line, or a list of fields for that line.
  10269. PARAMS is a property list of parameters that can influence the conversion.
  10270. Supports all parameters from `orgtbl-to-generic'. Most important for
  10271. TeXInfo are:
  10272. :splice nil/t When set to t, return only table body lines, don't wrap
  10273. them into a multitable environment. Default is nil.
  10274. :fmt fmt A format to be used to wrap the field, should contain
  10275. %s for the original field value. For example, to wrap
  10276. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10277. This may also be a property list with column numbers and
  10278. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10279. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  10280. are computed automatically from the width of the columns
  10281. under org-mode.
  10282. The general parameters :skip and :skipcols have already been applied when
  10283. this function is called."
  10284. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10285. (colfrac (or (plist-get params :cf)
  10286. (mapconcat
  10287. (lambda (x) (format "%.3f" (/ (float x) total)))
  10288. org-table-last-column-widths " ")))
  10289. (params2
  10290. (list
  10291. :tstart (concat "@multitable @columnfractions " colfrac)
  10292. :tend "@end multitable"
  10293. :lstart "@item " :lend "" :sep " @tab "
  10294. :hlstart "@headitem ")))
  10295. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10296. ;;;; Link Stuff
  10297. ;;; Link abbreviations
  10298. (defun org-link-expand-abbrev (link)
  10299. "Apply replacements as defined in `org-link-abbrev-alist."
  10300. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10301. (let* ((key (match-string 1 link))
  10302. (as (or (assoc key org-link-abbrev-alist-local)
  10303. (assoc key org-link-abbrev-alist)))
  10304. (tag (and (match-end 2) (match-string 3 link)))
  10305. rpl)
  10306. (if (not as)
  10307. link
  10308. (setq rpl (cdr as))
  10309. (cond
  10310. ((symbolp rpl) (funcall rpl tag))
  10311. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10312. (t (concat rpl tag)))))
  10313. link))
  10314. ;;; Storing and inserting links
  10315. (defvar org-insert-link-history nil
  10316. "Minibuffer history for links inserted with `org-insert-link'.")
  10317. (defvar org-stored-links nil
  10318. "Contains the links stored with `org-store-link'.")
  10319. (defvar org-store-link-plist nil
  10320. "Plist with info about the most recently link created with `org-store-link'.")
  10321. (defvar org-link-protocols nil
  10322. "Link protocols added to Org-mode using `org-add-link-type'.")
  10323. (defvar org-store-link-functions nil
  10324. "List of functions that are called to create and store a link.
  10325. Each function will be called in turn until one returns a non-nil
  10326. value. Each function should check if it is responsible for creating
  10327. this link (for example by looking at the major mode).
  10328. If not, it must exit and return nil.
  10329. If yes, it should return a non-nil value after a calling
  10330. `org-store-link-props' with a list of properties and values.
  10331. Special properties are:
  10332. :type The link prefix. like \"http\". This must be given.
  10333. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10334. This is obligatory as well.
  10335. :description Optional default description for the second pair
  10336. of brackets in an Org-mode link. The user can still change
  10337. this when inserting this link into an Org-mode buffer.
  10338. In addition to these, any additional properties can be specified
  10339. and then used in remember templates.")
  10340. (defun org-add-link-type (type &optional follow publish)
  10341. "Add TYPE to the list of `org-link-types'.
  10342. Re-compute all regular expressions depending on `org-link-types'
  10343. FOLLOW and PUBLISH are two functions. Both take the link path as
  10344. an argument.
  10345. FOLLOW should do whatever is necessary to follow the link, for example
  10346. to find a file or display a mail message.
  10347. PUBLISH takes the path and retuns the string that should be used when
  10348. this document is published."
  10349. (add-to-list 'org-link-types type t)
  10350. (org-make-link-regexps)
  10351. (add-to-list 'org-link-protocols
  10352. (list type follow publish)))
  10353. (defun org-add-agenda-custom-command (entry)
  10354. "Replace or add a command in `org-agenda-custom-commands'.
  10355. This is mostly for hacking and trying a new command - once the command
  10356. works you probably want to add it to `org-agenda-custom-commands' for good."
  10357. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10358. (if ass
  10359. (setcdr ass (cdr entry))
  10360. (push entry org-agenda-custom-commands))))
  10361. ;;;###autoload
  10362. (defun org-store-link (arg)
  10363. "\\<org-mode-map>Store an org-link to the current location.
  10364. This link can later be inserted into an org-buffer with
  10365. \\[org-insert-link].
  10366. For some link types, a prefix arg is interpreted:
  10367. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10368. For file links, arg negates `org-context-in-file-links'."
  10369. (interactive "P")
  10370. (setq org-store-link-plist nil) ; reset
  10371. (let (link cpltxt desc description search txt)
  10372. (cond
  10373. ((run-hook-with-args-until-success 'org-store-link-functions)
  10374. (setq link (plist-get org-store-link-plist :link)
  10375. desc (or (plist-get org-store-link-plist :description) link)))
  10376. ((eq major-mode 'bbdb-mode)
  10377. (let ((name (bbdb-record-name (bbdb-current-record)))
  10378. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10379. (setq cpltxt (concat "bbdb:" (or name company))
  10380. link (org-make-link cpltxt))
  10381. (org-store-link-props :type "bbdb" :name name :company company)))
  10382. ((eq major-mode 'Info-mode)
  10383. (setq link (org-make-link "info:"
  10384. (file-name-nondirectory Info-current-file)
  10385. ":" Info-current-node))
  10386. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10387. ":" Info-current-node))
  10388. (org-store-link-props :type "info" :file Info-current-file
  10389. :node Info-current-node))
  10390. ((eq major-mode 'calendar-mode)
  10391. (let ((cd (calendar-cursor-to-date)))
  10392. (setq link
  10393. (format-time-string
  10394. (car org-time-stamp-formats)
  10395. (apply 'encode-time
  10396. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10397. nil nil nil))))
  10398. (org-store-link-props :type "calendar" :date cd)))
  10399. ((or (eq major-mode 'vm-summary-mode)
  10400. (eq major-mode 'vm-presentation-mode))
  10401. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10402. (vm-follow-summary-cursor)
  10403. (save-excursion
  10404. (vm-select-folder-buffer)
  10405. (let* ((message (car vm-message-pointer))
  10406. (folder buffer-file-name)
  10407. (subject (vm-su-subject message))
  10408. (to (vm-get-header-contents message "To"))
  10409. (from (vm-get-header-contents message "From"))
  10410. (message-id (vm-su-message-id message)))
  10411. (org-store-link-props :type "vm" :from from :to to :subject subject
  10412. :message-id message-id)
  10413. (setq message-id (org-remove-angle-brackets message-id))
  10414. (setq folder (abbreviate-file-name folder))
  10415. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10416. folder)
  10417. (setq folder (replace-match "" t t folder)))
  10418. (setq cpltxt (org-email-link-description))
  10419. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10420. ((eq major-mode 'wl-summary-mode)
  10421. (let* ((msgnum (wl-summary-message-number))
  10422. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10423. msgnum 'message-id))
  10424. (wl-message-entity
  10425. (if (fboundp 'elmo-message-entity)
  10426. (elmo-message-entity
  10427. wl-summary-buffer-elmo-folder msgnum)
  10428. (elmo-msgdb-overview-get-entity
  10429. msgnum (wl-summary-buffer-msgdb))))
  10430. (from (wl-summary-line-from))
  10431. (to (elmo-message-entity-field wl-message-entity 'to))
  10432. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10433. (wl-summary-line-subject))))
  10434. (org-store-link-props :type "wl" :from from :to to
  10435. :subject subject :message-id message-id)
  10436. (setq message-id (org-remove-angle-brackets message-id))
  10437. (setq cpltxt (org-email-link-description))
  10438. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10439. "#" message-id))))
  10440. ((or (equal major-mode 'mh-folder-mode)
  10441. (equal major-mode 'mh-show-mode))
  10442. (let ((from (org-mhe-get-header "From:"))
  10443. (to (org-mhe-get-header "To:"))
  10444. (message-id (org-mhe-get-header "Message-Id:"))
  10445. (subject (org-mhe-get-header "Subject:")))
  10446. (org-store-link-props :type "mh" :from from :to to
  10447. :subject subject :message-id message-id)
  10448. (setq cpltxt (org-email-link-description))
  10449. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10450. (org-remove-angle-brackets message-id)))))
  10451. ((eq major-mode 'rmail-mode)
  10452. (save-excursion
  10453. (save-restriction
  10454. (rmail-narrow-to-non-pruned-header)
  10455. (let ((folder buffer-file-name)
  10456. (message-id (mail-fetch-field "message-id"))
  10457. (from (mail-fetch-field "from"))
  10458. (to (mail-fetch-field "to"))
  10459. (subject (mail-fetch-field "subject")))
  10460. (org-store-link-props
  10461. :type "rmail" :from from :to to
  10462. :subject subject :message-id message-id)
  10463. (setq message-id (org-remove-angle-brackets message-id))
  10464. (setq cpltxt (org-email-link-description))
  10465. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10466. ((eq major-mode 'gnus-group-mode)
  10467. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10468. (gnus-group-group-name)) ; version
  10469. ((fboundp 'gnus-group-name)
  10470. (gnus-group-name))
  10471. (t "???"))))
  10472. (unless group (error "Not on a group"))
  10473. (org-store-link-props :type "gnus" :group group)
  10474. (setq cpltxt (concat
  10475. (if (org-xor arg org-usenet-links-prefer-google)
  10476. "http://groups.google.com/groups?group="
  10477. "gnus:")
  10478. group)
  10479. link (org-make-link cpltxt))))
  10480. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10481. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10482. (let* ((group gnus-newsgroup-name)
  10483. (article (gnus-summary-article-number))
  10484. (header (gnus-summary-article-header article))
  10485. (from (mail-header-from header))
  10486. (message-id (mail-header-id header))
  10487. (date (mail-header-date header))
  10488. (subject (gnus-summary-subject-string)))
  10489. (org-store-link-props :type "gnus" :from from :subject subject
  10490. :message-id message-id :group group)
  10491. (setq cpltxt (org-email-link-description))
  10492. (if (org-xor arg org-usenet-links-prefer-google)
  10493. (setq link
  10494. (concat
  10495. cpltxt "\n "
  10496. (format "http://groups.google.com/groups?as_umsgid=%s"
  10497. (org-fixup-message-id-for-http message-id))))
  10498. (setq link (org-make-link "gnus:" group
  10499. "#" (number-to-string article))))))
  10500. ((eq major-mode 'w3-mode)
  10501. (setq cpltxt (url-view-url t)
  10502. link (org-make-link cpltxt))
  10503. (org-store-link-props :type "w3" :url (url-view-url t)))
  10504. ((eq major-mode 'w3m-mode)
  10505. (setq cpltxt (or w3m-current-title w3m-current-url)
  10506. link (org-make-link w3m-current-url))
  10507. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10508. ((setq search (run-hook-with-args-until-success
  10509. 'org-create-file-search-functions))
  10510. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10511. "::" search))
  10512. (setq cpltxt (or description link)))
  10513. ((eq major-mode 'image-mode)
  10514. (setq cpltxt (concat "file:"
  10515. (abbreviate-file-name buffer-file-name))
  10516. link (org-make-link cpltxt))
  10517. (org-store-link-props :type "image" :file buffer-file-name))
  10518. ((eq major-mode 'dired-mode)
  10519. ;; link to the file in the current line
  10520. (setq cpltxt (concat "file:"
  10521. (abbreviate-file-name
  10522. (expand-file-name
  10523. (dired-get-filename nil t))))
  10524. link (org-make-link cpltxt)))
  10525. ((and buffer-file-name (org-mode-p))
  10526. ;; Just link to current headline
  10527. (setq cpltxt (concat "file:"
  10528. (abbreviate-file-name buffer-file-name)))
  10529. ;; Add a context search string
  10530. (when (org-xor org-context-in-file-links arg)
  10531. ;; Check if we are on a target
  10532. (if (org-in-regexp "<<\\(.*?\\)>>")
  10533. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10534. (setq txt (cond
  10535. ((org-on-heading-p) nil)
  10536. ((org-region-active-p)
  10537. (buffer-substring (region-beginning) (region-end)))
  10538. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10539. (when (or (null txt) (string-match "\\S-" txt))
  10540. (setq cpltxt
  10541. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10542. desc "NONE"))))
  10543. (if (string-match "::\\'" cpltxt)
  10544. (setq cpltxt (substring cpltxt 0 -2)))
  10545. (setq link (org-make-link cpltxt)))
  10546. ((buffer-file-name (buffer-base-buffer))
  10547. ;; Just link to this file here.
  10548. (setq cpltxt (concat "file:"
  10549. (abbreviate-file-name
  10550. (buffer-file-name (buffer-base-buffer)))))
  10551. ;; Add a context string
  10552. (when (org-xor org-context-in-file-links arg)
  10553. (setq txt (if (org-region-active-p)
  10554. (buffer-substring (region-beginning) (region-end))
  10555. (buffer-substring (point-at-bol) (point-at-eol))))
  10556. ;; Only use search option if there is some text.
  10557. (when (string-match "\\S-" txt)
  10558. (setq cpltxt
  10559. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10560. desc "NONE")))
  10561. (setq link (org-make-link cpltxt)))
  10562. ((interactive-p)
  10563. (error "Cannot link to a buffer which is not visiting a file"))
  10564. (t (setq link nil)))
  10565. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10566. (setq link (or link cpltxt)
  10567. desc (or desc cpltxt))
  10568. (if (equal desc "NONE") (setq desc nil))
  10569. (if (and (interactive-p) link)
  10570. (progn
  10571. (setq org-stored-links
  10572. (cons (list link desc) org-stored-links))
  10573. (message "Stored: %s" (or desc link)))
  10574. (and link (org-make-link-string link desc)))))
  10575. (defun org-store-link-props (&rest plist)
  10576. "Store link properties, extract names and addresses."
  10577. (let (x adr)
  10578. (when (setq x (plist-get plist :from))
  10579. (setq adr (mail-extract-address-components x))
  10580. (plist-put plist :fromname (car adr))
  10581. (plist-put plist :fromaddress (nth 1 adr)))
  10582. (when (setq x (plist-get plist :to))
  10583. (setq adr (mail-extract-address-components x))
  10584. (plist-put plist :toname (car adr))
  10585. (plist-put plist :toaddress (nth 1 adr))))
  10586. (let ((from (plist-get plist :from))
  10587. (to (plist-get plist :to)))
  10588. (when (and from to org-from-is-user-regexp)
  10589. (plist-put plist :fromto
  10590. (if (string-match org-from-is-user-regexp from)
  10591. (concat "to %t")
  10592. (concat "from %f")))))
  10593. (setq org-store-link-plist plist))
  10594. (defun org-email-link-description (&optional fmt)
  10595. "Return the description part of an email link.
  10596. This takes information from `org-store-link-plist' and formats it
  10597. according to FMT (default from `org-email-link-description-format')."
  10598. (setq fmt (or fmt org-email-link-description-format))
  10599. (let* ((p org-store-link-plist)
  10600. (to (plist-get p :toaddress))
  10601. (from (plist-get p :fromaddress))
  10602. (table
  10603. (list
  10604. (cons "%c" (plist-get p :fromto))
  10605. (cons "%F" (plist-get p :from))
  10606. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  10607. (cons "%T" (plist-get p :to))
  10608. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  10609. (cons "%s" (plist-get p :subject))
  10610. (cons "%m" (plist-get p :message-id)))))
  10611. (when (string-match "%c" fmt)
  10612. ;; Check if the user wrote this message
  10613. (if (and org-from-is-user-regexp from to
  10614. (save-match-data (string-match org-from-is-user-regexp from)))
  10615. (setq fmt (replace-match "to %t" t t fmt))
  10616. (setq fmt (replace-match "from %f" t t fmt))))
  10617. (org-replace-escapes fmt table)))
  10618. (defun org-make-org-heading-search-string (&optional string heading)
  10619. "Make search string for STRING or current headline."
  10620. (interactive)
  10621. (let ((s (or string (org-get-heading))))
  10622. (unless (and string (not heading))
  10623. ;; We are using a headline, clean up garbage in there.
  10624. (if (string-match org-todo-regexp s)
  10625. (setq s (replace-match "" t t s)))
  10626. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  10627. (setq s (replace-match "" t t s)))
  10628. (setq s (org-trim s))
  10629. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10630. org-comment-string "\\)") s)
  10631. (setq s (replace-match "" t t s)))
  10632. (while (string-match org-ts-regexp s)
  10633. (setq s (replace-match "" t t s))))
  10634. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10635. (setq s (replace-match " " t t s)))
  10636. (or string (setq s (concat "*" s))) ; Add * for headlines
  10637. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10638. (defun org-make-link (&rest strings)
  10639. "Concatenate STRINGS."
  10640. (apply 'concat strings))
  10641. (defun org-make-link-string (link &optional description)
  10642. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10643. (unless (string-match "\\S-" link)
  10644. (error "Empty link"))
  10645. (when (stringp description)
  10646. ;; Remove brackets from the description, they are fatal.
  10647. (while (string-match "\\[\\|\\]" description)
  10648. (setq description (replace-match "" t t description))))
  10649. (when (equal (org-link-escape link) description)
  10650. ;; No description needed, it is identical
  10651. (setq description nil))
  10652. (when (and (not description)
  10653. (not (equal link (org-link-escape link))))
  10654. (setq description link))
  10655. (concat "[[" (org-link-escape link) "]"
  10656. (if description (concat "[" description "]") "")
  10657. "]"))
  10658. (defconst org-link-escape-chars
  10659. '((?\ . "%20")
  10660. (?\[ . "%5B")
  10661. (?\] . "%5d")
  10662. (?\340 . "%E0") ; `a
  10663. (?\342 . "%E2") ; ^a
  10664. (?\347 . "%E7") ; ,c
  10665. (?\350 . "%E8") ; `e
  10666. (?\351 . "%E9") ; 'e
  10667. (?\352 . "%EA") ; ^e
  10668. (?\356 . "%EE") ; ^i
  10669. (?\364 . "%F4") ; ^o
  10670. (?\371 . "%F9") ; `u
  10671. (?\373 . "%FB") ; ^u
  10672. (?\; . "%3B")
  10673. (?? . "%3F")
  10674. (?= . "%3D")
  10675. (?+ . "%2B")
  10676. )
  10677. "Association list of escapes for some characters problematic in links.
  10678. This is the list that is used for internal purposes.")
  10679. (defconst org-link-escape-chars-browser
  10680. '((?\ . "%20")) ; 32 for the SPC char
  10681. "Association list of escapes for some characters problematic in links.
  10682. This is the list that is used before handing over to the browser.")
  10683. (defun org-link-escape (text &optional table)
  10684. "Escape charaters in TEXT that are problematic for links."
  10685. (setq table (or table org-link-escape-chars))
  10686. (when text
  10687. (let ((re (mapconcat (lambda (x) (regexp-quote
  10688. (char-to-string (car x))))
  10689. table "\\|")))
  10690. (while (string-match re text)
  10691. (setq text
  10692. (replace-match
  10693. (cdr (assoc (string-to-char (match-string 0 text))
  10694. table))
  10695. t t text)))
  10696. text)))
  10697. (defun org-link-unescape (text &optional table)
  10698. "Reverse the action of `org-link-escape'."
  10699. (setq table (or table org-link-escape-chars))
  10700. (when text
  10701. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  10702. table "\\|")))
  10703. (while (string-match re text)
  10704. (setq text
  10705. (replace-match
  10706. (char-to-string (car (rassoc (match-string 0 text) table)))
  10707. t t text)))
  10708. text)))
  10709. (defun org-xor (a b)
  10710. "Exclusive or."
  10711. (if a (not b) b))
  10712. (defun org-get-header (header)
  10713. "Find a header field in the current buffer."
  10714. (save-excursion
  10715. (goto-char (point-min))
  10716. (let ((case-fold-search t) s)
  10717. (cond
  10718. ((eq header 'from)
  10719. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  10720. (setq s (match-string 1)))
  10721. (while (string-match "\"" s)
  10722. (setq s (replace-match "" t t s)))
  10723. (if (string-match "[<(].*" s)
  10724. (setq s (replace-match "" t t s))))
  10725. ((eq header 'message-id)
  10726. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  10727. (setq s (match-string 1))))
  10728. ((eq header 'subject)
  10729. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  10730. (setq s (match-string 1)))))
  10731. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  10732. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  10733. s)))
  10734. (defun org-fixup-message-id-for-http (s)
  10735. "Replace special characters in a message id, so it can be used in an http query."
  10736. (while (string-match "<" s)
  10737. (setq s (replace-match "%3C" t t s)))
  10738. (while (string-match ">" s)
  10739. (setq s (replace-match "%3E" t t s)))
  10740. (while (string-match "@" s)
  10741. (setq s (replace-match "%40" t t s)))
  10742. s)
  10743. ;;;###autoload
  10744. (defun org-insert-link-global ()
  10745. "Insert a link like Org-mode does.
  10746. This command can be called in any mode to insert a link in Org-mode syntax."
  10747. (interactive)
  10748. (org-run-like-in-org-mode 'org-insert-link))
  10749. (defun org-insert-link (&optional complete-file)
  10750. "Insert a link. At the prompt, enter the link.
  10751. Completion can be used to select a link previously stored with
  10752. `org-store-link'. When the empty string is entered (i.e. if you just
  10753. press RET at the prompt), the link defaults to the most recently
  10754. stored link. As SPC triggers completion in the minibuffer, you need to
  10755. use M-SPC or C-q SPC to force the insertion of a space character.
  10756. You will also be prompted for a description, and if one is given, it will
  10757. be displayed in the buffer instead of the link.
  10758. If there is already a link at point, this command will allow you to edit link
  10759. and description parts.
  10760. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  10761. selected using completion. The path to the file will be relative to
  10762. the current directory if the file is in the current directory or a
  10763. subdirectory. Otherwise, the link will be the absolute path as
  10764. completed in the minibuffer (i.e. normally ~/path/to/file).
  10765. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  10766. is in the current directory or below.
  10767. With three \\[universal-argument] prefixes, negate the meaning of
  10768. `org-keep-stored-link-after-insertion'."
  10769. (interactive "P")
  10770. (let* ((wcf (current-window-configuration))
  10771. (region (if (org-region-active-p)
  10772. (buffer-substring (region-beginning) (region-end))))
  10773. (remove (and region (list (region-beginning) (region-end))))
  10774. (desc region)
  10775. tmphist ; byte-compile incorrectly complains about this
  10776. link entry file)
  10777. (cond
  10778. ((org-in-regexp org-bracket-link-regexp 1)
  10779. ;; We do have a link at point, and we are going to edit it.
  10780. (setq remove (list (match-beginning 0) (match-end 0)))
  10781. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  10782. (setq link (read-string "Link: "
  10783. (org-link-unescape
  10784. (org-match-string-no-properties 1)))))
  10785. ((or (org-in-regexp org-angle-link-re)
  10786. (org-in-regexp org-plain-link-re))
  10787. ;; Convert to bracket link
  10788. (setq remove (list (match-beginning 0) (match-end 0))
  10789. link (read-string "Link: "
  10790. (org-remove-angle-brackets (match-string 0)))))
  10791. ((equal complete-file '(4))
  10792. ;; Completing read for file names.
  10793. (setq file (read-file-name "File: "))
  10794. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  10795. (pwd1 (file-name-as-directory (abbreviate-file-name
  10796. (expand-file-name ".")))))
  10797. (cond
  10798. ((equal complete-file '(16))
  10799. (setq link (org-make-link
  10800. "file:"
  10801. (abbreviate-file-name (expand-file-name file)))))
  10802. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  10803. (setq link (org-make-link "file:" (match-string 1 file))))
  10804. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  10805. (expand-file-name file))
  10806. (setq link (org-make-link
  10807. "file:" (match-string 1 (expand-file-name file)))))
  10808. (t (setq link (org-make-link "file:" file))))))
  10809. (t
  10810. ;; Read link, with completion for stored links.
  10811. (with-output-to-temp-buffer "*Org Links*"
  10812. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  10813. (when org-stored-links
  10814. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  10815. (princ (mapconcat
  10816. (lambda (x)
  10817. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  10818. (reverse org-stored-links) "\n"))))
  10819. (let ((cw (selected-window)))
  10820. (select-window (get-buffer-window "*Org Links*"))
  10821. (shrink-window-if-larger-than-buffer)
  10822. (setq truncate-lines t)
  10823. (select-window cw))
  10824. ;; Fake a link history, containing the stored links.
  10825. (setq tmphist (append (mapcar 'car org-stored-links)
  10826. org-insert-link-history))
  10827. (unwind-protect
  10828. (setq link (org-completing-read
  10829. "Link: "
  10830. (append
  10831. (mapcar (lambda (x) (list (concat (car x) ":")))
  10832. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  10833. (mapcar (lambda (x) (list (concat x ":")))
  10834. org-link-types))
  10835. nil nil nil
  10836. 'tmphist
  10837. (or (car (car org-stored-links)))))
  10838. (set-window-configuration wcf)
  10839. (kill-buffer "*Org Links*"))
  10840. (setq entry (assoc link org-stored-links))
  10841. (or entry (push link org-insert-link-history))
  10842. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  10843. (not org-keep-stored-link-after-insertion))
  10844. (setq org-stored-links (delq (assoc link org-stored-links)
  10845. org-stored-links)))
  10846. (setq desc (or desc (nth 1 entry)))))
  10847. (if (string-match org-plain-link-re link)
  10848. ;; URL-like link, normalize the use of angular brackets.
  10849. (setq link (org-make-link (org-remove-angle-brackets link))))
  10850. ;; Check if we are linking to the current file with a search option
  10851. ;; If yes, simplify the link by using only the search option.
  10852. (when (and buffer-file-name
  10853. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  10854. (let* ((path (match-string 1 link))
  10855. (case-fold-search nil)
  10856. (search (match-string 2 link)))
  10857. (save-match-data
  10858. (if (equal (file-truename buffer-file-name) (file-truename path))
  10859. ;; We are linking to this same file, with a search option
  10860. (setq link search)))))
  10861. ;; Check if we can/should use a relative path. If yes, simplify the link
  10862. (when (string-match "\\<file:\\(.*\\)" link)
  10863. (let* ((path (match-string 1 link))
  10864. (origpath path)
  10865. (desc-is-link (equal link desc))
  10866. (case-fold-search nil))
  10867. (cond
  10868. ((eq org-link-file-path-type 'absolute)
  10869. (setq path (abbreviate-file-name (expand-file-name path))))
  10870. ((eq org-link-file-path-type 'noabbrev)
  10871. (setq path (expand-file-name path)))
  10872. ((eq org-link-file-path-type 'relative)
  10873. (setq path (file-relative-name path)))
  10874. (t
  10875. (save-match-data
  10876. (if (string-match (concat "^" (regexp-quote
  10877. (file-name-as-directory
  10878. (expand-file-name "."))))
  10879. (expand-file-name path))
  10880. ;; We are linking a file with relative path name.
  10881. (setq path (substring (expand-file-name path)
  10882. (match-end 0)))))))
  10883. (setq link (concat "file:" path))
  10884. (if (equal desc origpath)
  10885. (setq desc path))))
  10886. (setq desc (read-string "Description: " desc))
  10887. (unless (string-match "\\S-" desc) (setq desc nil))
  10888. (if remove (apply 'delete-region remove))
  10889. (insert (org-make-link-string link desc))))
  10890. (defun org-completing-read (&rest args)
  10891. (let ((minibuffer-local-completion-map
  10892. (copy-keymap minibuffer-local-completion-map)))
  10893. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  10894. (apply 'completing-read args)))
  10895. ;;; Opening/following a link
  10896. (defvar org-link-search-failed nil)
  10897. (defun org-next-link ()
  10898. "Move forward to the next link.
  10899. If the link is in hidden text, expose it."
  10900. (interactive)
  10901. (when (and org-link-search-failed (eq this-command last-command))
  10902. (goto-char (point-min))
  10903. (message "Link search wrapped back to beginning of buffer"))
  10904. (setq org-link-search-failed nil)
  10905. (let* ((pos (point))
  10906. (ct (org-context))
  10907. (a (assoc :link ct)))
  10908. (if a (goto-char (nth 2 a)))
  10909. (if (re-search-forward org-any-link-re nil t)
  10910. (progn
  10911. (goto-char (match-beginning 0))
  10912. (if (org-invisible-p) (org-show-context)))
  10913. (goto-char pos)
  10914. (setq org-link-search-failed t)
  10915. (error "No further link found"))))
  10916. (defun org-previous-link ()
  10917. "Move backward to the previous link.
  10918. If the link is in hidden text, expose it."
  10919. (interactive)
  10920. (when (and org-link-search-failed (eq this-command last-command))
  10921. (goto-char (point-max))
  10922. (message "Link search wrapped back to end of buffer"))
  10923. (setq org-link-search-failed nil)
  10924. (let* ((pos (point))
  10925. (ct (org-context))
  10926. (a (assoc :link ct)))
  10927. (if a (goto-char (nth 1 a)))
  10928. (if (re-search-backward org-any-link-re nil t)
  10929. (progn
  10930. (goto-char (match-beginning 0))
  10931. (if (org-invisible-p) (org-show-context)))
  10932. (goto-char pos)
  10933. (setq org-link-search-failed t)
  10934. (error "No further link found"))))
  10935. (defun org-find-file-at-mouse (ev)
  10936. "Open file link or URL at mouse."
  10937. (interactive "e")
  10938. (mouse-set-point ev)
  10939. (org-open-at-point 'in-emacs))
  10940. (defun org-open-at-mouse (ev)
  10941. "Open file link or URL at mouse."
  10942. (interactive "e")
  10943. (mouse-set-point ev)
  10944. (org-open-at-point))
  10945. (defvar org-window-config-before-follow-link nil
  10946. "The window configuration before following a link.
  10947. This is saved in case the need arises to restore it.")
  10948. (defvar org-open-link-marker (make-marker)
  10949. "Marker pointing to the location where `org-open-at-point; was called.")
  10950. ;;;###autoload
  10951. (defun org-open-at-point-global ()
  10952. "Follow a link like Org-mode does.
  10953. This command can be called in any mode to follow a link that has
  10954. Org-mode syntax."
  10955. (interactive)
  10956. (org-run-like-in-org-mode 'org-open-at-point))
  10957. (defun org-open-at-point (&optional in-emacs)
  10958. "Open link at or after point.
  10959. If there is no link at point, this function will search forward up to
  10960. the end of the current subtree.
  10961. Normally, files will be opened by an appropriate application. If the
  10962. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  10963. (interactive "P")
  10964. (move-marker org-open-link-marker (point))
  10965. (setq org-window-config-before-follow-link (current-window-configuration))
  10966. (org-remove-occur-highlights nil nil t)
  10967. (if (org-at-timestamp-p t)
  10968. (org-follow-timestamp-link)
  10969. (let (type path link line search (pos (point)))
  10970. (catch 'match
  10971. (save-excursion
  10972. (skip-chars-forward "^]\n\r")
  10973. (when (org-in-regexp org-bracket-link-regexp)
  10974. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  10975. (while (string-match " *\n *" link)
  10976. (setq link (replace-match " " t t link)))
  10977. (setq link (org-link-expand-abbrev link))
  10978. (if (string-match org-link-re-with-space2 link)
  10979. (setq type (match-string 1 link) path (match-string 2 link))
  10980. (setq type "thisfile" path link))
  10981. (throw 'match t)))
  10982. (when (get-text-property (point) 'org-linked-text)
  10983. (setq type "thisfile"
  10984. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  10985. (1+ (point)) (point))
  10986. path (buffer-substring
  10987. (previous-single-property-change pos 'org-linked-text)
  10988. (next-single-property-change pos 'org-linked-text)))
  10989. (throw 'match t))
  10990. (save-excursion
  10991. (when (or (org-in-regexp org-angle-link-re)
  10992. (org-in-regexp org-plain-link-re))
  10993. (setq type (match-string 1) path (match-string 2))
  10994. (throw 'match t)))
  10995. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  10996. (setq type "tree-match"
  10997. path (match-string 1))
  10998. (throw 'match t))
  10999. (save-excursion
  11000. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11001. (setq type "tags"
  11002. path (match-string 1))
  11003. (while (string-match ":" path)
  11004. (setq path (replace-match "+" t t path)))
  11005. (throw 'match t))))
  11006. (unless path
  11007. (error "No link found"))
  11008. ;; Remove any trailing spaces in path
  11009. (if (string-match " +\\'" path)
  11010. (setq path (replace-match "" t t path)))
  11011. (cond
  11012. ((assoc type org-link-protocols)
  11013. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11014. ((equal type "mailto")
  11015. (let ((cmd (car org-link-mailto-program))
  11016. (args (cdr org-link-mailto-program)) args1
  11017. (address path) (subject "") a)
  11018. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11019. (setq address (match-string 1 path)
  11020. subject (org-link-escape (match-string 2 path))))
  11021. (while args
  11022. (cond
  11023. ((not (stringp (car args))) (push (pop args) args1))
  11024. (t (setq a (pop args))
  11025. (if (string-match "%a" a)
  11026. (setq a (replace-match address t t a)))
  11027. (if (string-match "%s" a)
  11028. (setq a (replace-match subject t t a)))
  11029. (push a args1))))
  11030. (apply cmd (nreverse args1))))
  11031. ((member type '("http" "https" "ftp" "news"))
  11032. (browse-url (concat type ":" (org-link-escape
  11033. path org-link-escape-chars-browser))))
  11034. ((string= type "tags")
  11035. (org-tags-view in-emacs path))
  11036. ((string= type "thisfile")
  11037. (if in-emacs
  11038. (switch-to-buffer-other-window
  11039. (org-get-buffer-for-internal-link (current-buffer)))
  11040. (org-mark-ring-push))
  11041. (let ((cmd `(org-link-search
  11042. ,path
  11043. ,(cond ((equal in-emacs '(4)) 'occur)
  11044. ((equal in-emacs '(16)) 'org-occur)
  11045. (t nil))
  11046. ,pos)))
  11047. (condition-case nil (eval cmd)
  11048. (error (progn (widen) (eval cmd))))))
  11049. ((string= type "tree-match")
  11050. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11051. ((string= type "file")
  11052. (if (string-match "::\\([0-9]+\\)\\'" path)
  11053. (setq line (string-to-number (match-string 1 path))
  11054. path (substring path 0 (match-beginning 0)))
  11055. (if (string-match "::\\(.+\\)\\'" path)
  11056. (setq search (match-string 1 path)
  11057. path (substring path 0 (match-beginning 0)))))
  11058. (if (string-match "[*?{]" (file-name-nondirectory path))
  11059. (dired path)
  11060. (org-open-file path in-emacs line search)))
  11061. ((string= type "news")
  11062. (org-follow-gnus-link path))
  11063. ((string= type "bbdb")
  11064. (org-follow-bbdb-link path))
  11065. ((string= type "info")
  11066. (org-follow-info-link path))
  11067. ((string= type "gnus")
  11068. (let (group article)
  11069. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11070. (error "Error in Gnus link"))
  11071. (setq group (match-string 1 path)
  11072. article (match-string 3 path))
  11073. (org-follow-gnus-link group article)))
  11074. ((string= type "vm")
  11075. (let (folder article)
  11076. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11077. (error "Error in VM link"))
  11078. (setq folder (match-string 1 path)
  11079. article (match-string 3 path))
  11080. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11081. (org-follow-vm-link folder article in-emacs)))
  11082. ((string= type "wl")
  11083. (let (folder article)
  11084. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11085. (error "Error in Wanderlust link"))
  11086. (setq folder (match-string 1 path)
  11087. article (match-string 3 path))
  11088. (org-follow-wl-link folder article)))
  11089. ((string= type "mhe")
  11090. (let (folder article)
  11091. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11092. (error "Error in MHE link"))
  11093. (setq folder (match-string 1 path)
  11094. article (match-string 3 path))
  11095. (org-follow-mhe-link folder article)))
  11096. ((string= type "rmail")
  11097. (let (folder article)
  11098. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11099. (error "Error in RMAIL link"))
  11100. (setq folder (match-string 1 path)
  11101. article (match-string 3 path))
  11102. (org-follow-rmail-link folder article)))
  11103. ((string= type "shell")
  11104. (let ((cmd path))
  11105. ;; The following is only for backward compatibility
  11106. (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
  11107. (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
  11108. (if (or (not org-confirm-shell-link-function)
  11109. (funcall org-confirm-shell-link-function
  11110. (format "Execute \"%s\" in shell? "
  11111. (org-add-props cmd nil
  11112. 'face 'org-warning))))
  11113. (progn
  11114. (message "Executing %s" cmd)
  11115. (shell-command cmd))
  11116. (error "Abort"))))
  11117. ((string= type "elisp")
  11118. (let ((cmd path))
  11119. (if (or (not org-confirm-elisp-link-function)
  11120. (funcall org-confirm-elisp-link-function
  11121. (format "Execute \"%s\" as elisp? "
  11122. (org-add-props cmd nil
  11123. 'face 'org-warning))))
  11124. (message "%s => %s" cmd (eval (read cmd)))
  11125. (error "Abort"))))
  11126. (t
  11127. (browse-url-at-point)))))
  11128. (move-marker org-open-link-marker nil))
  11129. ;;; File search
  11130. (defvar org-create-file-search-functions nil
  11131. "List of functions to construct the right search string for a file link.
  11132. These functions are called in turn with point at the location to
  11133. which the link should point.
  11134. A function in the hook should first test if it would like to
  11135. handle this file type, for example by checking the major-mode or
  11136. the file extension. If it decides not to handle this file, it
  11137. should just return nil to give other functions a chance. If it
  11138. does handle the file, it must return the search string to be used
  11139. when following the link. The search string will be part of the
  11140. file link, given after a double colon, and `org-open-at-point'
  11141. will automatically search for it. If special measures must be
  11142. taken to make the search successful, another function should be
  11143. added to the companion hook `org-execute-file-search-functions',
  11144. which see.
  11145. A function in this hook may also use `setq' to set the variable
  11146. `description' to provide a suggestion for the descriptive text to
  11147. be used for this link when it gets inserted into an Org-mode
  11148. buffer with \\[org-insert-link].")
  11149. (defvar org-execute-file-search-functions nil
  11150. "List of functions to execute a file search triggered by a link.
  11151. Functions added to this hook must accept a single argument, the
  11152. search string that was part of the file link, the part after the
  11153. double colon. The function must first check if it would like to
  11154. handle this search, for example by checking the major-mode or the
  11155. file extension. If it decides not to handle this search, it
  11156. should just return nil to give other functions a chance. If it
  11157. does handle the search, it must return a non-nil value to keep
  11158. other functions from trying.
  11159. Each function can access the current prefix argument through the
  11160. variable `current-prefix-argument'. Note that a single prefix is
  11161. used to force opening a link in Emacs, so it may be good to only
  11162. use a numeric or double prefix to guide the search function.
  11163. In case this is needed, a function in this hook can also restore
  11164. the window configuration before `org-open-at-point' was called using:
  11165. (set-window-configuration org-window-config-before-follow-link)")
  11166. (defun org-link-search (s &optional type avoid-pos)
  11167. "Search for a link search option.
  11168. If S is surrounded by forward slashes, it is interpreted as a
  11169. regular expression. In org-mode files, this will create an `org-occur'
  11170. sparse tree. In ordinary files, `occur' will be used to list matches.
  11171. If the current buffer is in `dired-mode', grep will be used to search
  11172. in all files. If AVOID-POS is given, ignore matches near that position."
  11173. (let ((case-fold-search t)
  11174. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11175. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11176. (append '(("") (" ") ("\t") ("\n"))
  11177. org-emphasis-alist)
  11178. "\\|") "\\)"))
  11179. (pos (point))
  11180. (pre "") (post "")
  11181. words re0 re1 re2 re3 re4 re5 re2a reall)
  11182. (cond
  11183. ;; First check if there are any special
  11184. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11185. ;; Now try the builtin stuff
  11186. ((save-excursion
  11187. (goto-char (point-min))
  11188. (and
  11189. (re-search-forward
  11190. (concat "<<" (regexp-quote s0) ">>") nil t)
  11191. (setq pos (match-beginning 0))))
  11192. ;; There is an exact target for this
  11193. (goto-char pos))
  11194. ((string-match "^/\\(.*\\)/$" s)
  11195. ;; A regular expression
  11196. (cond
  11197. ((org-mode-p)
  11198. (org-occur (match-string 1 s)))
  11199. ;;((eq major-mode 'dired-mode)
  11200. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11201. (t (org-do-occur (match-string 1 s)))))
  11202. (t
  11203. ;; A normal search strings
  11204. (when (equal (string-to-char s) ?*)
  11205. ;; Anchor on headlines, post may include tags.
  11206. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11207. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11208. s (substring s 1)))
  11209. (remove-text-properties
  11210. 0 (length s)
  11211. '(face nil mouse-face nil keymap nil fontified nil) s)
  11212. ;; Make a series of regular expressions to find a match
  11213. (setq words (org-split-string s "[ \n\r\t]+")
  11214. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11215. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11216. "\\)" markers)
  11217. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11218. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11219. re1 (concat pre re2 post)
  11220. re3 (concat pre re4 post)
  11221. re5 (concat pre ".*" re4)
  11222. re2 (concat pre re2)
  11223. re2a (concat pre re2a)
  11224. re4 (concat pre re4)
  11225. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11226. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11227. re5 "\\)"
  11228. ))
  11229. (cond
  11230. ((eq type 'org-occur) (org-occur reall))
  11231. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11232. (t (goto-char (point-min))
  11233. (if (or (org-search-not-self 1 re0 nil t)
  11234. (org-search-not-self 1 re1 nil t)
  11235. (org-search-not-self 1 re2 nil t)
  11236. (org-search-not-self 1 re2a nil t)
  11237. (org-search-not-self 1 re3 nil t)
  11238. (org-search-not-self 1 re4 nil t)
  11239. (org-search-not-self 1 re5 nil t)
  11240. )
  11241. (goto-char (match-beginning 1))
  11242. (goto-char pos)
  11243. (error "No match")))))
  11244. (t
  11245. ;; Normal string-search
  11246. (goto-char (point-min))
  11247. (if (search-forward s nil t)
  11248. (goto-char (match-beginning 0))
  11249. (error "No match"))))
  11250. (and (org-mode-p) (org-show-context 'link-search))))
  11251. (defun org-search-not-self (group &rest args)
  11252. "Execute `re-search-forward', but only accept matches that do not
  11253. enclose the position of `org-open-link-marker'."
  11254. (let ((m org-open-link-marker))
  11255. (catch 'exit
  11256. (while (apply 're-search-forward args)
  11257. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11258. (goto-char (match-end group))
  11259. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11260. (> (match-beginning 0) (marker-position m))
  11261. (< (match-end 0) (marker-position m)))
  11262. (save-match-data
  11263. (or (not (org-in-regexp
  11264. org-bracket-link-analytic-regexp 1))
  11265. (not (match-end 4)) ; no description
  11266. (and (<= (match-beginning 4) (point))
  11267. (>= (match-end 4) (point))))))
  11268. (throw 'exit (point))))))))
  11269. (defun org-get-buffer-for-internal-link (buffer)
  11270. "Return a buffer to be used for displaying the link target of internal links."
  11271. (cond
  11272. ((not org-display-internal-link-with-indirect-buffer)
  11273. buffer)
  11274. ((string-match "(Clone)$" (buffer-name buffer))
  11275. (message "Buffer is already a clone, not making another one")
  11276. ;; we also do not modify visibility in this case
  11277. buffer)
  11278. (t ; make a new indirect buffer for displaying the link
  11279. (let* ((bn (buffer-name buffer))
  11280. (ibn (concat bn "(Clone)"))
  11281. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11282. (with-current-buffer ib (org-overview))
  11283. ib))))
  11284. (defun org-do-occur (regexp &optional cleanup)
  11285. "Call the Emacs command `occur'.
  11286. If CLEANUP is non-nil, remove the printout of the regular expression
  11287. in the *Occur* buffer. This is useful if the regex is long and not useful
  11288. to read."
  11289. (occur regexp)
  11290. (when cleanup
  11291. (let ((cwin (selected-window)) win beg end)
  11292. (when (setq win (get-buffer-window "*Occur*"))
  11293. (select-window win))
  11294. (goto-char (point-min))
  11295. (when (re-search-forward "match[a-z]+" nil t)
  11296. (setq beg (match-end 0))
  11297. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11298. (setq end (1- (match-beginning 0)))))
  11299. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11300. (goto-char (point-min))
  11301. (select-window cwin))))
  11302. ;;; The mark ring for links jumps
  11303. (defvar org-mark-ring nil
  11304. "Mark ring for positions before jumps in Org-mode.")
  11305. (defvar org-mark-ring-last-goto nil
  11306. "Last position in the mark ring used to go back.")
  11307. ;; Fill and close the ring
  11308. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11309. (loop for i from 1 to org-mark-ring-length do
  11310. (push (make-marker) org-mark-ring))
  11311. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11312. org-mark-ring)
  11313. (defun org-mark-ring-push (&optional pos buffer)
  11314. "Put the current position or POS into the mark ring and rotate it."
  11315. (interactive)
  11316. (setq pos (or pos (point)))
  11317. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11318. (move-marker (car org-mark-ring)
  11319. (or pos (point))
  11320. (or buffer (current-buffer)))
  11321. (message
  11322. (substitute-command-keys
  11323. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11324. (defun org-mark-ring-goto (&optional n)
  11325. "Jump to the previous position in the mark ring.
  11326. With prefix arg N, jump back that many stored positions. When
  11327. called several times in succession, walk through the entire ring.
  11328. Org-mode commands jumping to a different position in the current file,
  11329. or to another Org-mode file, automatically push the old position
  11330. onto the ring."
  11331. (interactive "p")
  11332. (let (p m)
  11333. (if (eq last-command this-command)
  11334. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11335. (setq p org-mark-ring))
  11336. (setq org-mark-ring-last-goto p)
  11337. (setq m (car p))
  11338. (switch-to-buffer (marker-buffer m))
  11339. (goto-char m)
  11340. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11341. (defun org-remove-angle-brackets (s)
  11342. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11343. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11344. s)
  11345. (defun org-add-angle-brackets (s)
  11346. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11347. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11348. s)
  11349. ;;; Following specific links
  11350. (defun org-follow-timestamp-link ()
  11351. (cond
  11352. ((org-at-date-range-p t)
  11353. (let ((org-agenda-start-on-weekday)
  11354. (t1 (match-string 1))
  11355. (t2 (match-string 2)))
  11356. (setq t1 (time-to-days (org-time-string-to-time t1))
  11357. t2 (time-to-days (org-time-string-to-time t2)))
  11358. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11359. ((org-at-timestamp-p t)
  11360. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11361. (substring (match-string 1) 0 10)))
  11362. 1))
  11363. (t (error "This should not happen"))))
  11364. (defun org-follow-bbdb-link (name)
  11365. "Follow a BBDB link to NAME."
  11366. (require 'bbdb)
  11367. (let ((inhibit-redisplay (not debug-on-error))
  11368. (bbdb-electric-p nil))
  11369. (catch 'exit
  11370. ;; Exact match on name
  11371. (bbdb-name (concat "\\`" name "\\'") nil)
  11372. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11373. ;; Exact match on name
  11374. (bbdb-company (concat "\\`" name "\\'") nil)
  11375. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11376. ;; Partial match on name
  11377. (bbdb-name name nil)
  11378. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11379. ;; Partial match on company
  11380. (bbdb-company name nil)
  11381. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11382. ;; General match including network address and notes
  11383. (bbdb name nil)
  11384. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11385. (delete-window (get-buffer-window "*BBDB*"))
  11386. (error "No matching BBDB record")))))
  11387. (defun org-follow-info-link (name)
  11388. "Follow an info file & node link to NAME."
  11389. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11390. (string-match "\\(.*\\)" name))
  11391. (progn
  11392. (require 'info)
  11393. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11394. (Info-find-node (match-string 1 name) (match-string 2 name))
  11395. (Info-find-node (match-string 1 name) "Top")))
  11396. (message (concat "Could not open: " name))))
  11397. (defun org-follow-gnus-link (&optional group article)
  11398. "Follow a Gnus link to GROUP and ARTICLE."
  11399. (require 'gnus)
  11400. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11401. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11402. (cond ((and group article)
  11403. (gnus-group-read-group 1 nil group)
  11404. (gnus-summary-goto-article (string-to-number article) nil t))
  11405. (group (gnus-group-jump-to-group group))))
  11406. (defun org-follow-vm-link (&optional folder article readonly)
  11407. "Follow a VM link to FOLDER and ARTICLE."
  11408. (require 'vm)
  11409. (setq article (org-add-angle-brackets article))
  11410. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11411. ;; ange-ftp or efs or tramp access
  11412. (let ((user (or (match-string 1 folder) (user-login-name)))
  11413. (host (match-string 2 folder))
  11414. (file (match-string 3 folder)))
  11415. (cond
  11416. ((featurep 'tramp)
  11417. ;; use tramp to access the file
  11418. (if (featurep 'xemacs)
  11419. (setq folder (format "[%s@%s]%s" user host file))
  11420. (setq folder (format "/%s@%s:%s" user host file))))
  11421. (t
  11422. ;; use ange-ftp or efs
  11423. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11424. (setq folder (format "/%s@%s:%s" user host file))))))
  11425. (when folder
  11426. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11427. (sit-for 0.1)
  11428. (when article
  11429. (vm-select-folder-buffer)
  11430. (widen)
  11431. (let ((case-fold-search t))
  11432. (goto-char (point-min))
  11433. (if (not (re-search-forward
  11434. (concat "^" "message-id: *" (regexp-quote article))))
  11435. (error "Could not find the specified message in this folder"))
  11436. (vm-isearch-update)
  11437. (vm-isearch-narrow)
  11438. (vm-beginning-of-message)
  11439. (vm-summarize)))))
  11440. (defun org-follow-wl-link (folder article)
  11441. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11442. (if (and (string= folder "%")
  11443. article
  11444. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11445. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11446. ;; Thus, we recompose folder and article ids.
  11447. (setq folder (format "%s#%s" folder (match-string 1 article))
  11448. article (match-string 3 article)))
  11449. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11450. (error "No such folder: %s" folder))
  11451. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11452. (and article
  11453. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11454. (wl-summary-redisplay)))
  11455. (defun org-follow-rmail-link (folder article)
  11456. "Follow an RMAIL link to FOLDER and ARTICLE."
  11457. (setq article (org-add-angle-brackets article))
  11458. (let (message-number)
  11459. (save-excursion
  11460. (save-window-excursion
  11461. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11462. (setq message-number
  11463. (save-restriction
  11464. (widen)
  11465. (goto-char (point-max))
  11466. (if (re-search-backward
  11467. (concat "^Message-ID:\\s-+" (regexp-quote
  11468. (or article "")))
  11469. nil t)
  11470. (rmail-what-message))))))
  11471. (if message-number
  11472. (progn
  11473. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11474. (rmail-show-message message-number)
  11475. message-number)
  11476. (error "Message not found"))))
  11477. ;;; mh-e integration based on planner-mode
  11478. (defun org-mhe-get-message-real-folder ()
  11479. "Return the name of the current message real folder, so if you use
  11480. sequences, it will now work."
  11481. (save-excursion
  11482. (let* ((folder
  11483. (if (equal major-mode 'mh-folder-mode)
  11484. mh-current-folder
  11485. ;; Refer to the show buffer
  11486. mh-show-folder-buffer))
  11487. (end-index
  11488. (if (boundp 'mh-index-folder)
  11489. (min (length mh-index-folder) (length folder))))
  11490. )
  11491. ;; a simple test on mh-index-data does not work, because
  11492. ;; mh-index-data is always nil in a show buffer.
  11493. (if (and (boundp 'mh-index-folder)
  11494. (string= mh-index-folder (substring folder 0 end-index)))
  11495. (if (equal major-mode 'mh-show-mode)
  11496. (save-window-excursion
  11497. (let (pop-up-frames)
  11498. (when (buffer-live-p (get-buffer folder))
  11499. (progn
  11500. (pop-to-buffer folder)
  11501. (org-mhe-get-message-folder-from-index)
  11502. )
  11503. )))
  11504. (org-mhe-get-message-folder-from-index)
  11505. )
  11506. folder
  11507. )
  11508. )))
  11509. (defun org-mhe-get-message-folder-from-index ()
  11510. "Returns the name of the message folder in a index folder buffer."
  11511. (save-excursion
  11512. (mh-index-previous-folder)
  11513. (re-search-forward "^\\(+.*\\)$" nil t)
  11514. (message (match-string 1))))
  11515. (defun org-mhe-get-message-folder ()
  11516. "Return the name of the current message folder. Be careful if you
  11517. use sequences."
  11518. (save-excursion
  11519. (if (equal major-mode 'mh-folder-mode)
  11520. mh-current-folder
  11521. ;; Refer to the show buffer
  11522. mh-show-folder-buffer)))
  11523. (defun org-mhe-get-message-num ()
  11524. "Return the number of the current message. Be careful if you
  11525. use sequences."
  11526. (save-excursion
  11527. (if (equal major-mode 'mh-folder-mode)
  11528. (mh-get-msg-num nil)
  11529. ;; Refer to the show buffer
  11530. (mh-show-buffer-message-number))))
  11531. (defun org-mhe-get-header (header)
  11532. "Return a header of the message in folder mode. This will create a
  11533. show buffer for the corresponding message. If you have a more clever
  11534. idea..."
  11535. (let* ((folder (org-mhe-get-message-folder))
  11536. (num (org-mhe-get-message-num))
  11537. (buffer (get-buffer-create (concat "show-" folder)))
  11538. (header-field))
  11539. (with-current-buffer buffer
  11540. (mh-display-msg num folder)
  11541. (if (equal major-mode 'mh-folder-mode)
  11542. (mh-header-display)
  11543. (mh-show-header-display))
  11544. (set-buffer buffer)
  11545. (setq header-field (mh-get-header-field header))
  11546. (if (equal major-mode 'mh-folder-mode)
  11547. (mh-show)
  11548. (mh-show-show))
  11549. header-field)))
  11550. (defun org-follow-mhe-link (folder article)
  11551. "Follow an MHE link to FOLDER and ARTICLE.
  11552. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11553. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11554. ARTICLE is searched in all folders. Indexed searches (swish++,
  11555. namazu, and others supported by MH-E) will always search in all
  11556. folders."
  11557. (require 'mh-e)
  11558. (require 'mh-search)
  11559. (require 'mh-utils)
  11560. (mh-find-path)
  11561. (if (not article)
  11562. (mh-visit-folder (mh-normalize-folder-name folder))
  11563. (setq article (org-add-angle-brackets article))
  11564. (mh-search-choose)
  11565. (if (equal mh-searcher 'pick)
  11566. (progn
  11567. (mh-search folder (list "--message-id" article))
  11568. (when (and org-mhe-search-all-folders
  11569. (not (org-mhe-get-message-real-folder)))
  11570. (kill-this-buffer)
  11571. (mh-search "+" (list "--message-id" article))))
  11572. (mh-search "+" article))
  11573. (if (org-mhe-get-message-real-folder)
  11574. (mh-show-msg 1)
  11575. (kill-this-buffer)
  11576. (error "Message not found"))))
  11577. ;;; BibTeX links
  11578. ;; Use the custom search meachnism to construct and use search strings for
  11579. ;; file links to BibTeX database entries.
  11580. (defun org-create-file-search-in-bibtex ()
  11581. "Create the search string and description for a BibTeX database entry."
  11582. (when (eq major-mode 'bibtex-mode)
  11583. ;; yes, we want to construct this search string.
  11584. ;; Make a good description for this entry, using names, year and the title
  11585. ;; Put it into the `description' variable which is dynamically scoped.
  11586. (let ((bibtex-autokey-names 1)
  11587. (bibtex-autokey-names-stretch 1)
  11588. (bibtex-autokey-name-case-convert-function 'identity)
  11589. (bibtex-autokey-name-separator " & ")
  11590. (bibtex-autokey-additional-names " et al.")
  11591. (bibtex-autokey-year-length 4)
  11592. (bibtex-autokey-name-year-separator " ")
  11593. (bibtex-autokey-titlewords 3)
  11594. (bibtex-autokey-titleword-separator " ")
  11595. (bibtex-autokey-titleword-case-convert-function 'identity)
  11596. (bibtex-autokey-titleword-length 'infty)
  11597. (bibtex-autokey-year-title-separator ": "))
  11598. (setq description (bibtex-generate-autokey)))
  11599. ;; Now parse the entry, get the key and return it.
  11600. (save-excursion
  11601. (bibtex-beginning-of-entry)
  11602. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11603. (defun org-execute-file-search-in-bibtex (s)
  11604. "Find the link search string S as a key for a database entry."
  11605. (when (eq major-mode 'bibtex-mode)
  11606. ;; Yes, we want to do the search in this file.
  11607. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11608. (goto-char (point-min))
  11609. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11610. (regexp-quote s) "[ \t\n]*,") nil t)
  11611. (goto-char (match-beginning 0)))
  11612. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11613. ;; Use double prefix to indicate that any web link should be browsed
  11614. (let ((b (current-buffer)) (p (point)))
  11615. ;; Restore the window configuration because we just use the web link
  11616. (set-window-configuration org-window-config-before-follow-link)
  11617. (save-excursion (set-buffer b) (goto-char p)
  11618. (bibtex-url)))
  11619. (recenter 0)) ; Move entry start to beginning of window
  11620. ;; return t to indicate that the search is done.
  11621. t))
  11622. ;; Finally add the functions to the right hooks.
  11623. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11624. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11625. ;; end of Bibtex link setup
  11626. ;;; Following file links
  11627. (defun org-open-file (path &optional in-emacs line search)
  11628. "Open the file at PATH.
  11629. First, this expands any special file name abbreviations. Then the
  11630. configuration variable `org-file-apps' is checked if it contains an
  11631. entry for this file type, and if yes, the corresponding command is launched.
  11632. If no application is found, Emacs simply visits the file.
  11633. With optional argument IN-EMACS, Emacs will visit the file.
  11634. Optional LINE specifies a line to go to, optional SEARCH a string to
  11635. search for. If LINE or SEARCH is given, the file will always be
  11636. opened in Emacs.
  11637. If the file does not exist, an error is thrown."
  11638. (setq in-emacs (or in-emacs line search))
  11639. (let* ((file (if (equal path "")
  11640. buffer-file-name
  11641. (substitute-in-file-name (expand-file-name path))))
  11642. (apps (append org-file-apps (org-default-apps)))
  11643. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  11644. (dirp (if remp nil (file-directory-p file)))
  11645. (dfile (downcase file))
  11646. (old-buffer (current-buffer))
  11647. (old-pos (point))
  11648. (old-mode major-mode)
  11649. ext cmd)
  11650. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  11651. (setq ext (match-string 1 dfile))
  11652. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  11653. (setq ext (match-string 1 dfile))))
  11654. (if in-emacs
  11655. (setq cmd 'emacs)
  11656. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  11657. (and dirp (cdr (assoc 'directory apps)))
  11658. (cdr (assoc ext apps))
  11659. (cdr (assoc t apps)))))
  11660. (when (eq cmd 'mailcap)
  11661. (require 'mailcap)
  11662. (mailcap-parse-mailcaps)
  11663. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  11664. (command (mailcap-mime-info mime-type)))
  11665. (if (stringp command)
  11666. (setq cmd command)
  11667. (setq cmd 'emacs))))
  11668. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  11669. (not (file-exists-p file))
  11670. (not org-open-non-existing-files))
  11671. (error "No such file: %s" file))
  11672. (cond
  11673. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  11674. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  11675. (if (string-match "['\"]%s['\"]" cmd)
  11676. (setq cmd (replace-match "%s" t t cmd)))
  11677. (setq cmd (format cmd (shell-quote-argument file)))
  11678. (save-window-excursion
  11679. (start-process-shell-command cmd nil cmd)))
  11680. ((or (stringp cmd)
  11681. (eq cmd 'emacs))
  11682. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  11683. (widen)
  11684. (if line (goto-line line)
  11685. (if search (org-link-search search))))
  11686. ((consp cmd)
  11687. (eval cmd))
  11688. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  11689. (and (org-mode-p) (eq old-mode 'org-mode)
  11690. (or (not (equal old-buffer (current-buffer)))
  11691. (not (equal old-pos (point))))
  11692. (org-mark-ring-push old-pos old-buffer))))
  11693. (defun org-default-apps ()
  11694. "Return the default applications for this operating system."
  11695. (cond
  11696. ((eq system-type 'darwin)
  11697. org-file-apps-defaults-macosx)
  11698. ((eq system-type 'windows-nt)
  11699. org-file-apps-defaults-windowsnt)
  11700. (t org-file-apps-defaults-gnu)))
  11701. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  11702. (defun org-file-remote-p (file)
  11703. "Test whether FILE specifies a location on a remote system.
  11704. Return non-nil if the location is indeed remote.
  11705. For example, the filename \"/user@host:/foo\" specifies a location
  11706. on the system \"/user@host:\"."
  11707. (cond ((fboundp 'file-remote-p)
  11708. (file-remote-p file))
  11709. ((fboundp 'tramp-handle-file-remote-p)
  11710. (tramp-handle-file-remote-p file))
  11711. ((and (boundp 'ange-ftp-name-format)
  11712. (string-match (car ange-ftp-name-format) file))
  11713. t)
  11714. (t nil)))
  11715. ;;;; Hooks for remember.el
  11716. ;;;###autoload
  11717. (defun org-remember-insinuate ()
  11718. "Setup remember.el for use wiht Org-mode."
  11719. (require 'remember)
  11720. (setq remember-annotation-functions '(org-remember-annotation))
  11721. (setq remember-handler-functions '(org-remember-handler))
  11722. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  11723. ;;;###autoload
  11724. (defun org-remember-annotation ()
  11725. "Return a link to the current location as an annotation for remember.el.
  11726. If you are using Org-mode files as target for data storage with
  11727. remember.el, then the annotations should include a link compatible with the
  11728. conventions in Org-mode. This function returns such a link."
  11729. (org-store-link nil))
  11730. (defconst org-remember-help
  11731. "Select a destination location for the note.
  11732. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  11733. RET on headline -> Store as sublevel entry to current headline
  11734. RET at beg-of-buf -> Append to file as level 2 headline
  11735. <left>/<right> -> before/after current headline, same headings level")
  11736. (defvar org-remember-previous-location nil)
  11737. (defvar org-force-remember-template-char) ;; dynamically scoped
  11738. (defun org-select-remember-template (&optional use-char)
  11739. (when org-remember-templates
  11740. (let* ((templates (mapcar (lambda (x)
  11741. (if (stringp (car x))
  11742. (append (list (nth 1 x) (car x)) (cddr x))
  11743. (append (list (car x) "") (cdr x))))
  11744. org-remember-templates))
  11745. (char (or use-char
  11746. (cond
  11747. ((= (length templates) 1)
  11748. (caar templates))
  11749. ((and (boundp 'org-force-remember-template-char)
  11750. org-force-remember-template-char)
  11751. (if (stringp org-force-remember-template-char)
  11752. (string-to-char org-force-remember-template-char)
  11753. org-force-remember-template-char))
  11754. (t
  11755. (message "Select template: %s"
  11756. (mapconcat
  11757. (lambda (x)
  11758. (cond
  11759. ((not (string-match "\\S-" (nth 1 x)))
  11760. (format "[%c]" (car x)))
  11761. ((equal (downcase (car x))
  11762. (downcase (aref (nth 1 x) 0)))
  11763. (format "[%c]%s" (car x)
  11764. (substring (nth 1 x) 1)))
  11765. (t (format "[%c]%s" (car x) (nth 1 x)))))
  11766. templates " "))
  11767. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  11768. (when (equal char0 ?\C-g)
  11769. (jump-to-register remember-register)
  11770. (kill-buffer remember-buffer))
  11771. char0))))))
  11772. (cddr (assoc char templates)))))
  11773. ;;;###autoload
  11774. (defun org-remember-apply-template (&optional use-char skip-interactive)
  11775. "Initialize *remember* buffer with template, invoke `org-mode'.
  11776. This function should be placed into `remember-mode-hook' and in fact requires
  11777. to be run from that hook to function properly."
  11778. (unless (fboundp 'remember-finalize)
  11779. (defalias 'remember-finalize 'remember-buffer))
  11780. (if org-remember-templates
  11781. (let* ((entry (org-select-remember-template use-char))
  11782. (tpl (car entry))
  11783. (plist-p (if org-store-link-plist t nil))
  11784. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  11785. (string-match "\\S-" (nth 1 entry)))
  11786. (nth 1 entry)
  11787. org-default-notes-file))
  11788. (headline (nth 2 entry))
  11789. (v-c (current-kill 0)) ;; FIXME: protection needed?
  11790. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  11791. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  11792. (v-u (concat "[" (substring v-t 1 -1) "]"))
  11793. (v-U (concat "[" (substring v-T 1 -1) "]"))
  11794. ;; `initial' and `annotation' are bound in `remember'
  11795. (v-i (if (boundp 'initial) initial))
  11796. (v-a (if (and (boundp 'annotation) annotation)
  11797. (if (equal annotation "[[]]") "" annotation)
  11798. ""))
  11799. (v-A (if (and v-a
  11800. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  11801. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  11802. v-a))
  11803. (v-n user-full-name)
  11804. (org-startup-folded nil)
  11805. org-time-was-given org-end-time-was-given x prompt char time pos)
  11806. (setq org-store-link-plist
  11807. (append (list :annotation v-a :initial v-i)
  11808. org-store-link-plist))
  11809. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  11810. (erase-buffer)
  11811. (insert (substitute-command-keys
  11812. (format
  11813. "## Filing location: Select interactively, default, or last used:
  11814. ## %s to select file and header location interactively.
  11815. ## %s \"%s\" -> \"* %s\"
  11816. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  11817. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  11818. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  11819. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  11820. (abbreviate-file-name (or file org-default-notes-file))
  11821. (or headline "")
  11822. (or (car org-remember-previous-location) "???")
  11823. (or (cdr org-remember-previous-location) "???"))))
  11824. (insert tpl) (goto-char (point-min))
  11825. ;; Simple %-escapes
  11826. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  11827. (when (and initial (equal (match-string 0) "%i"))
  11828. (save-match-data
  11829. (let* ((lead (buffer-substring
  11830. (point-at-bol) (match-beginning 0))))
  11831. (setq v-i (mapconcat 'identity
  11832. (org-split-string initial "\n")
  11833. (concat "\n" lead))))))
  11834. (replace-match
  11835. (or (eval (intern (concat "v-" (match-string 1)))) "")
  11836. t t))
  11837. ;; %[] Insert contents of a file.
  11838. (goto-char (point-min))
  11839. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  11840. (let ((start (match-beginning 0))
  11841. (end (match-end 0))
  11842. (filename (expand-file-name (match-string 1))))
  11843. (goto-char start)
  11844. (delete-region start end)
  11845. (condition-case error
  11846. (insert-file-contents filename)
  11847. (error (insert (format "%%![Couldn't insert %s: %s]"
  11848. filename error))))))
  11849. ;; %() embedded elisp
  11850. (goto-char (point-min))
  11851. (while (re-search-forward "%\\((.+)\\)" nil t)
  11852. (goto-char (match-beginning 0))
  11853. (let ((template-start (point)))
  11854. (forward-char 1)
  11855. (let ((result
  11856. (condition-case error
  11857. (eval (read (current-buffer)))
  11858. (error (format "%%![Error: %s]" error)))))
  11859. (delete-region template-start (point))
  11860. (insert result))))
  11861. ;; From the property list
  11862. (when plist-p
  11863. (goto-char (point-min))
  11864. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  11865. (and (setq x (or (plist-get org-store-link-plist
  11866. (intern (match-string 1))) ""))
  11867. (replace-match x t t))))
  11868. ;; Turn on org-mode in the remember buffer, set local variables
  11869. (org-mode)
  11870. (org-set-local 'org-finish-function 'remember-finalize)
  11871. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  11872. (org-set-local 'org-default-notes-file file))
  11873. (if (and headline (stringp headline) (string-match "\\S-" headline))
  11874. (org-set-local 'org-remember-default-headline headline))
  11875. ;; Interactive template entries
  11876. (goto-char (point-min))
  11877. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  11878. (setq char (if (match-end 3) (match-string 3))
  11879. prompt (if (match-end 2) (match-string 2)))
  11880. (goto-char (match-beginning 0))
  11881. (replace-match "")
  11882. (cond
  11883. ((member char '("G" "g"))
  11884. (let* ((org-last-tags-completion-table
  11885. (org-global-tags-completion-table
  11886. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  11887. (org-add-colon-after-tag-completion t)
  11888. (ins (completing-read
  11889. (if prompt (concat prompt ": ") "Tags: ")
  11890. 'org-tags-completion-function nil nil nil
  11891. 'org-tags-history)))
  11892. (setq ins (mapconcat 'identity
  11893. (org-split-string ins (org-re "[^[:alnum:]]+"))
  11894. ":"))
  11895. (when (string-match "\\S-" ins)
  11896. (or (equal (char-before) ?:) (insert ":"))
  11897. (insert ins)
  11898. (or (equal (char-after) ?:) (insert ":")))))
  11899. (char
  11900. (setq org-time-was-given (equal (upcase char) char))
  11901. (setq time (org-read-date (equal (upcase char) "U") t nil
  11902. prompt))
  11903. (org-insert-time-stamp time org-time-was-given
  11904. (member char '("u" "U"))
  11905. nil nil (list org-end-time-was-given)))
  11906. (t
  11907. (insert (read-string
  11908. (if prompt (concat prompt ": ") "Enter string"))))))
  11909. (goto-char (point-min))
  11910. (if (re-search-forward "%\\?" nil t)
  11911. (replace-match "")
  11912. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  11913. (org-mode)
  11914. (org-set-local 'org-finish-function 'remember-finalize)))
  11915. ;;;###autoload
  11916. (defun org-remember (&optional goto org-force-remember-template-char)
  11917. "Call `remember'. If this is already a remember buffer, re-apply template.
  11918. If there is an active region, make sure remember uses it as initial content
  11919. of the remember buffer.
  11920. When called interactively with a `C-u' prefix argument GOTO, don't remember
  11921. anything, just go to the file/headline where the selected templated usually
  11922. stores its notes.
  11923. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  11924. associated with a template in `org-remember-tempates'."
  11925. (interactive "P")
  11926. (if (equal goto '(4))
  11927. (org-go-to-remember-target)
  11928. (if (eq org-finish-function 'remember-buffer)
  11929. (progn
  11930. (when (< (length org-remember-templates) 2)
  11931. (error "No other template available"))
  11932. (erase-buffer)
  11933. (let ((annotation (plist-get org-store-link-plist :annotation))
  11934. (initial (plist-get org-store-link-plist :initial)))
  11935. (org-remember-apply-template))
  11936. (message "Press C-c C-c to remember data"))
  11937. (if (org-region-active-p)
  11938. (remember (buffer-substring (point) (mark)))
  11939. (call-interactively 'remember)))))
  11940. (defun org-go-to-remember-target (&optional template-key)
  11941. "Go to the target location of a remember template.
  11942. The user is queried for the template."
  11943. (interactive)
  11944. (let* ((entry (org-select-remember-template template-key))
  11945. (file (nth 1 entry))
  11946. (heading (nth 2 entry))
  11947. visiting)
  11948. (unless (and file (stringp file) (string-match "\\S-" file))
  11949. (setq file org-default-notes-file))
  11950. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  11951. (setq heading org-remember-default-headline))
  11952. (setq visiting (org-find-base-buffer-visiting file))
  11953. (if (not visiting) (find-file-noselect file))
  11954. (switch-to-buffer (or visiting (get-file-buffer file)))
  11955. (widen)
  11956. (goto-char (point-min))
  11957. (if (re-search-forward
  11958. (concat "^\\*+[ \t]+" (regexp-quote heading)
  11959. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  11960. nil t)
  11961. (goto-char (match-beginning 0))
  11962. (error "Target headline not found: %s" heading))))
  11963. (defvar org-note-abort nil) ; dynamically scoped
  11964. ;;;###autoload
  11965. (defun org-remember-handler ()
  11966. "Store stuff from remember.el into an org file.
  11967. First prompts for an org file. If the user just presses return, the value
  11968. of `org-default-notes-file' is used.
  11969. Then the command offers the headings tree of the selected file in order to
  11970. file the text at a specific location.
  11971. You can either immediately press RET to get the note appended to the
  11972. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  11973. find a better place. Then press RET or <left> or <right> in insert the note.
  11974. Key Cursor position Note gets inserted
  11975. -----------------------------------------------------------------------------
  11976. RET buffer-start as level 1 heading at end of file
  11977. RET on headline as sublevel of the heading at cursor
  11978. RET no heading at cursor position, level taken from context.
  11979. Or use prefix arg to specify level manually.
  11980. <left> on headline as same level, before current heading
  11981. <right> on headline as same level, after current heading
  11982. So the fastest way to store the note is to press RET RET to append it to
  11983. the default file. This way your current train of thought is not
  11984. interrupted, in accordance with the principles of remember.el.
  11985. You can also get the fast execution without prompting by using
  11986. C-u C-c C-c to exit the remember buffer. See also the variable
  11987. `org-remember-store-without-prompt'.
  11988. Before being stored away, the function ensures that the text has a
  11989. headline, i.e. a first line that starts with a \"*\". If not, a headline
  11990. is constructed from the current date and some additional data.
  11991. If the variable `org-adapt-indentation' is non-nil, the entire text is
  11992. also indented so that it starts in the same column as the headline
  11993. \(i.e. after the stars).
  11994. See also the variable `org-reverse-note-order'."
  11995. (goto-char (point-min))
  11996. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  11997. (replace-match ""))
  11998. (goto-char (point-max))
  11999. (catch 'quit
  12000. (if org-note-abort (throw 'quit nil))
  12001. (let* ((txt (buffer-substring (point-min) (point-max)))
  12002. (fastp (org-xor (equal current-prefix-arg '(4))
  12003. org-remember-store-without-prompt))
  12004. (file (if fastp org-default-notes-file (org-get-org-file)))
  12005. (heading org-remember-default-headline)
  12006. (visiting (org-find-base-buffer-visiting file))
  12007. (org-startup-folded nil)
  12008. (org-startup-align-all-tables nil)
  12009. (org-goto-start-pos 1)
  12010. spos exitcmd level indent reversed)
  12011. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12012. (setq file (car org-remember-previous-location)
  12013. heading (cdr org-remember-previous-location)))
  12014. (setq current-prefix-arg nil)
  12015. ;; Modify text so that it becomes a nice subtree which can be inserted
  12016. ;; into an org tree.
  12017. (let* ((lines (split-string txt "\n"))
  12018. first)
  12019. (setq first (car lines) lines (cdr lines))
  12020. (if (string-match "^\\*+ " first)
  12021. ;; Is already a headline
  12022. (setq indent nil)
  12023. ;; We need to add a headline: Use time and first buffer line
  12024. (setq lines (cons first lines)
  12025. first (concat "* " (current-time-string)
  12026. " (" (remember-buffer-desc) ")")
  12027. indent " "))
  12028. (if (and org-adapt-indentation indent)
  12029. (setq lines (mapcar
  12030. (lambda (x)
  12031. (if (string-match "\\S-" x)
  12032. (concat indent x) x))
  12033. lines)))
  12034. (setq txt (concat first "\n"
  12035. (mapconcat 'identity lines "\n"))))
  12036. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12037. (setq txt (replace-match "\n\n" t t txt))
  12038. (if (string-match "[ \t\n]*\\'" txt)
  12039. (setq txt (replace-match "\n" t t txt))))
  12040. ;; Find the file
  12041. (if (not visiting) (find-file-noselect file))
  12042. (with-current-buffer (or visiting (get-file-buffer file))
  12043. (unless (org-mode-p)
  12044. (error "Target files for remember notes must be in Org-mode"))
  12045. (save-excursion
  12046. (save-restriction
  12047. (widen)
  12048. (and (goto-char (point-min))
  12049. (not (re-search-forward "^\\* " nil t))
  12050. (insert "\n* " (or heading "Notes") "\n"))
  12051. (setq reversed (org-notes-order-reversed-p))
  12052. ;; Find the default location
  12053. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12054. (goto-char (point-min))
  12055. (if (re-search-forward
  12056. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12057. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12058. nil t)
  12059. (setq org-goto-start-pos (match-beginning 0))
  12060. (when fastp
  12061. (goto-char (point-max))
  12062. (unless (bolp) (newline))
  12063. (insert "* " heading "\n")
  12064. (setq org-goto-start-pos (point-at-bol 0)))))
  12065. ;; Ask the User for a location
  12066. (if fastp
  12067. (setq spos org-goto-start-pos
  12068. exitcmd 'return)
  12069. (setq spos (org-get-location (current-buffer) org-remember-help)
  12070. exitcmd (cdr spos)
  12071. spos (car spos)))
  12072. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12073. ; not handle this note
  12074. (goto-char spos)
  12075. (cond ((org-on-heading-p t)
  12076. (org-back-to-heading t)
  12077. (setq level (funcall outline-level))
  12078. (cond
  12079. ((eq exitcmd 'return)
  12080. ;; sublevel of current
  12081. (setq org-remember-previous-location
  12082. (cons (abbreviate-file-name file)
  12083. (org-get-heading 'notags)))
  12084. (if reversed
  12085. (outline-next-heading)
  12086. (org-end-of-subtree)
  12087. (if (not (bolp))
  12088. (if (looking-at "[ \t]*\n")
  12089. (beginning-of-line 2)
  12090. (end-of-line 1)
  12091. (insert "\n"))))
  12092. (org-paste-subtree (org-get-legal-level level 1) txt))
  12093. ((eq exitcmd 'left)
  12094. ;; before current
  12095. (org-paste-subtree level txt))
  12096. ((eq exitcmd 'right)
  12097. ;; after current
  12098. (org-end-of-subtree t)
  12099. (org-paste-subtree level txt))
  12100. (t (error "This should not happen"))))
  12101. ((and (bobp) (not reversed))
  12102. ;; Put it at the end, one level below level 1
  12103. (save-restriction
  12104. (widen)
  12105. (goto-char (point-max))
  12106. (if (not (bolp)) (newline))
  12107. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12108. ((and (bobp) reversed)
  12109. ;; Put it at the start, as level 1
  12110. (save-restriction
  12111. (widen)
  12112. (goto-char (point-min))
  12113. (re-search-forward "^\\*+ " nil t)
  12114. (beginning-of-line 1)
  12115. (org-paste-subtree 1 txt)))
  12116. (t
  12117. ;; Put it right there, with automatic level determined by
  12118. ;; org-paste-subtree or from prefix arg
  12119. (org-paste-subtree
  12120. (if (numberp current-prefix-arg) current-prefix-arg)
  12121. txt)))
  12122. (when remember-save-after-remembering
  12123. (save-buffer)
  12124. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12125. t) ;; return t to indicate that we took care of this note.
  12126. (defun org-get-org-file ()
  12127. "Read a filename, with default directory `org-directory'."
  12128. (let ((default (or org-default-notes-file remember-data-file)))
  12129. (read-file-name (format "File name [%s]: " default)
  12130. (file-name-as-directory org-directory)
  12131. default)))
  12132. (defun org-notes-order-reversed-p ()
  12133. "Check if the current file should receive notes in reversed order."
  12134. (cond
  12135. ((not org-reverse-note-order) nil)
  12136. ((eq t org-reverse-note-order) t)
  12137. ((not (listp org-reverse-note-order)) nil)
  12138. (t (catch 'exit
  12139. (let ((all org-reverse-note-order)
  12140. entry)
  12141. (while (setq entry (pop all))
  12142. (if (string-match (car entry) buffer-file-name)
  12143. (throw 'exit (cdr entry))))
  12144. nil)))))
  12145. ;;;; Dynamic blocks
  12146. (defun org-find-dblock (name)
  12147. "Find the first dynamic block with name NAME in the buffer.
  12148. If not found, stay at current position and return nil."
  12149. (let (pos)
  12150. (save-excursion
  12151. (goto-char (point-min))
  12152. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12153. nil t)
  12154. (match-beginning 0))))
  12155. (if pos (goto-char pos))
  12156. pos))
  12157. (defconst org-dblock-start-re
  12158. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12159. "Matches the startline of a dynamic block, with parameters.")
  12160. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12161. "Matches the end of a dyhamic block.")
  12162. (defun org-create-dblock (plist)
  12163. "Create a dynamic block section, with parameters taken from PLIST.
  12164. PLIST must containe a :name entry which is used as name of the block."
  12165. (unless (bolp) (newline))
  12166. (let ((name (plist-get plist :name)))
  12167. (insert "#+BEGIN: " name)
  12168. (while plist
  12169. (if (eq (car plist) :name)
  12170. (setq plist (cddr plist))
  12171. (insert " " (prin1-to-string (pop plist)))))
  12172. (insert "\n\n#+END:\n")
  12173. (beginning-of-line -2)))
  12174. (defun org-prepare-dblock ()
  12175. "Prepare dynamic block for refresh.
  12176. This empties the block, puts the cursor at the insert position and returns
  12177. the property list including an extra property :name with the block name."
  12178. (unless (looking-at org-dblock-start-re)
  12179. (error "Not at a dynamic block"))
  12180. (let* ((begdel (1+ (match-end 0)))
  12181. (name (org-no-properties (match-string 1)))
  12182. (params (append (list :name name)
  12183. (read (concat "(" (match-string 3) ")")))))
  12184. (unless (re-search-forward org-dblock-end-re nil t)
  12185. (error "Dynamic block not terminated"))
  12186. (delete-region begdel (match-beginning 0))
  12187. (goto-char begdel)
  12188. (open-line 1)
  12189. params))
  12190. (defun org-map-dblocks (&optional command)
  12191. "Apply COMMAND to all dynamic blocks in the current buffer.
  12192. If COMMAND is not given, use `org-update-dblock'."
  12193. (let ((cmd (or command 'org-update-dblock))
  12194. pos)
  12195. (save-excursion
  12196. (goto-char (point-min))
  12197. (while (re-search-forward org-dblock-start-re nil t)
  12198. (goto-char (setq pos (match-beginning 0)))
  12199. (condition-case nil
  12200. (funcall cmd)
  12201. (error (message "Error during update of dynamic block")))
  12202. (goto-char pos)
  12203. (unless (re-search-forward org-dblock-end-re nil t)
  12204. (error "Dynamic block not terminated"))))))
  12205. (defun org-dblock-update (&optional arg)
  12206. "User command for updating dynamic blocks.
  12207. Update the dynamic block at point. With prefix ARG, update all dynamic
  12208. blocks in the buffer."
  12209. (interactive "P")
  12210. (if arg
  12211. (org-update-all-dblocks)
  12212. (or (looking-at org-dblock-start-re)
  12213. (org-beginning-of-dblock))
  12214. (org-update-dblock)))
  12215. (defun org-update-dblock ()
  12216. "Update the dynamic block at point
  12217. This means to empty the block, parse for parameters and then call
  12218. the correct writing function."
  12219. (save-window-excursion
  12220. (let* ((pos (point))
  12221. (line (org-current-line))
  12222. (params (org-prepare-dblock))
  12223. (name (plist-get params :name))
  12224. (cmd (intern (concat "org-dblock-write:" name))))
  12225. (message "Updating dynamic block `%s' at line %d..." name line)
  12226. (funcall cmd params)
  12227. (message "Updating dynamic block `%s' at line %d...done" name line)
  12228. (goto-char pos))))
  12229. (defun org-beginning-of-dblock ()
  12230. "Find the beginning of the dynamic block at point.
  12231. Error if there is no scuh block at point."
  12232. (let ((pos (point))
  12233. beg)
  12234. (end-of-line 1)
  12235. (if (and (re-search-backward org-dblock-start-re nil t)
  12236. (setq beg (match-beginning 0))
  12237. (re-search-forward org-dblock-end-re nil t)
  12238. (> (match-end 0) pos))
  12239. (goto-char beg)
  12240. (goto-char pos)
  12241. (error "Not in a dynamic block"))))
  12242. (defun org-update-all-dblocks ()
  12243. "Update all dynamic blocks in the buffer.
  12244. This function can be used in a hook."
  12245. (when (org-mode-p)
  12246. (org-map-dblocks 'org-update-dblock)))
  12247. ;;;; Completion
  12248. (defconst org-additional-option-like-keywords
  12249. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12250. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12251. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12252. (defun org-complete (&optional arg)
  12253. "Perform completion on word at point.
  12254. At the beginning of a headline, this completes TODO keywords as given in
  12255. `org-todo-keywords'.
  12256. If the current word is preceded by a backslash, completes the TeX symbols
  12257. that are supported for HTML support.
  12258. If the current word is preceded by \"#+\", completes special words for
  12259. setting file options.
  12260. In the line after \"#+STARTUP:, complete valid keywords.\"
  12261. At all other locations, this simply calls the value of
  12262. `org-completion-fallback-command'."
  12263. (interactive "P")
  12264. (org-without-partial-completion
  12265. (catch 'exit
  12266. (let* ((end (point))
  12267. (beg1 (save-excursion
  12268. (skip-chars-backward (org-re "[:alnum:]_@"))
  12269. (point)))
  12270. (beg (save-excursion
  12271. (skip-chars-backward "a-zA-Z0-9_:$")
  12272. (point)))
  12273. (confirm (lambda (x) (stringp (car x))))
  12274. (searchhead (equal (char-before beg) ?*))
  12275. (tag (and (equal (char-before beg1) ?:)
  12276. (equal (char-after (point-at-bol)) ?*)))
  12277. (prop (and (equal (char-before beg1) ?:)
  12278. (not (equal (char-after (point-at-bol)) ?*))))
  12279. (texp (equal (char-before beg) ?\\))
  12280. (link (equal (char-before beg) ?\[))
  12281. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12282. beg)
  12283. "#+"))
  12284. (startup (string-match "^#\\+STARTUP:.*"
  12285. (buffer-substring (point-at-bol) (point))))
  12286. (completion-ignore-case opt)
  12287. (type nil)
  12288. (tbl nil)
  12289. (table (cond
  12290. (opt
  12291. (setq type :opt)
  12292. (append
  12293. (mapcar
  12294. (lambda (x)
  12295. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12296. (cons (match-string 2 x) (match-string 1 x)))
  12297. (org-split-string (org-get-current-options) "\n"))
  12298. (mapcar 'list org-additional-option-like-keywords)))
  12299. (startup
  12300. (setq type :startup)
  12301. org-startup-options)
  12302. (link (append org-link-abbrev-alist-local
  12303. org-link-abbrev-alist))
  12304. (texp
  12305. (setq type :tex)
  12306. org-html-entities)
  12307. ((string-match "\\`\\*+[ \t]+\\'"
  12308. (buffer-substring (point-at-bol) beg))
  12309. (setq type :todo)
  12310. (mapcar 'list org-todo-keywords-1))
  12311. (searchhead
  12312. (setq type :searchhead)
  12313. (save-excursion
  12314. (goto-char (point-min))
  12315. (while (re-search-forward org-todo-line-regexp nil t)
  12316. (push (list
  12317. (org-make-org-heading-search-string
  12318. (match-string 3) t))
  12319. tbl)))
  12320. tbl)
  12321. (tag (setq type :tag beg beg1)
  12322. (or org-tag-alist (org-get-buffer-tags)))
  12323. (prop (setq type :prop beg beg1)
  12324. (mapcar 'list (org-buffer-property-keys)))
  12325. (t (progn
  12326. (call-interactively org-completion-fallback-command)
  12327. (throw 'exit nil)))))
  12328. (pattern (buffer-substring-no-properties beg end))
  12329. (completion (try-completion pattern table confirm)))
  12330. (cond ((eq completion t)
  12331. (if (not (assoc (upcase pattern) table))
  12332. (message "Already complete")
  12333. (if (equal type :opt)
  12334. (insert (substring (cdr (assoc (upcase pattern) table))
  12335. (length pattern)))
  12336. (if (memq type '(:tag :prop)) (insert ":")))))
  12337. ((null completion)
  12338. (message "Can't find completion for \"%s\"" pattern)
  12339. (ding))
  12340. ((not (string= pattern completion))
  12341. (delete-region beg end)
  12342. (if (string-match " +$" completion)
  12343. (setq completion (replace-match "" t t completion)))
  12344. (insert completion)
  12345. (if (get-buffer-window "*Completions*")
  12346. (delete-window (get-buffer-window "*Completions*")))
  12347. (if (assoc completion table)
  12348. (if (eq type :todo) (insert " ")
  12349. (if (memq type '(:tag :prop)) (insert ":"))))
  12350. (if (and (equal type :opt) (assoc completion table))
  12351. (message "%s" (substitute-command-keys
  12352. "Press \\[org-complete] again to insert example settings"))))
  12353. (t
  12354. (message "Making completion list...")
  12355. (let ((list (sort (all-completions pattern table confirm)
  12356. 'string<)))
  12357. (with-output-to-temp-buffer "*Completions*"
  12358. (condition-case nil
  12359. ;; Protection needed for XEmacs and emacs 21
  12360. (display-completion-list list pattern)
  12361. (error (display-completion-list list)))))
  12362. (message "Making completion list...%s" "done")))))))
  12363. ;;;; TODO, DEADLINE, Comments
  12364. (defun org-toggle-comment ()
  12365. "Change the COMMENT state of an entry."
  12366. (interactive)
  12367. (save-excursion
  12368. (org-back-to-heading)
  12369. (let (case-fold-search)
  12370. (if (looking-at (concat outline-regexp
  12371. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  12372. (replace-match "" t t nil 1)
  12373. (if (looking-at outline-regexp)
  12374. (progn
  12375. (goto-char (match-end 0))
  12376. (insert org-comment-string " ")))))))
  12377. (defvar org-last-todo-state-is-todo nil
  12378. "This is non-nil when the last TODO state change led to a TODO state.
  12379. If the last change removed the TODO tag or switched to DONE, then
  12380. this is nil.")
  12381. (defvar org-setting-tags nil) ; dynamically skiped
  12382. ;; FIXME: better place
  12383. (defun org-property-or-variable-value (var &optional inherit)
  12384. "Check if there is a property fixing the value of VAR.
  12385. If yes, return this value. If not, return the current value of the variable."
  12386. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12387. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12388. (read prop)
  12389. (symbol-value var))))
  12390. (defun org-parse-local-options (string var)
  12391. "Parse STRING for startup setting relevant for variable VAR."
  12392. (let ((rtn (symbol-value var))
  12393. e opts)
  12394. (save-match-data
  12395. (if (or (not string) (not (string-match "\\S-" string)))
  12396. rtn
  12397. (setq opts (delq nil (mapcar (lambda (x)
  12398. (setq e (assoc x org-startup-options))
  12399. (if (eq (nth 1 e) var) e nil))
  12400. (org-split-string string "[ \t]+"))))
  12401. (if (not opts)
  12402. rtn
  12403. (setq rtn nil)
  12404. (while (setq e (pop opts))
  12405. (if (not (nth 3 e))
  12406. (setq rtn (nth 2 e))
  12407. (if (not (listp rtn)) (setq rtn nil))
  12408. (push (nth 2 e) rtn)))
  12409. rtn)))))
  12410. (defvar org-blocker-hook nil
  12411. "Hook for functions that are allowed to block a state change.
  12412. Each function gets as its single argument a property list, see
  12413. `org-trigger-hook' for more information about this list.
  12414. If any of the functions in this hook returns nil, the state change
  12415. is blocked.")
  12416. (defvar org-trigger-hook nil
  12417. "Hook for functions that are triggered by a state change.
  12418. Each function gets as its single argument a property list with at least
  12419. the following elements:
  12420. (:type type-of-change :position pos-at-entry-start
  12421. :from old-state :to new-state)
  12422. Depending on the type, more properties may be present.
  12423. This mechanism is currently implemented for:
  12424. TODO state changes
  12425. ------------------
  12426. :type todo-state-change
  12427. :from previous state (keyword as a string), or nil
  12428. :to new state (keyword as a string), or nil")
  12429. (defun org-todo (&optional arg)
  12430. "Change the TODO state of an item.
  12431. The state of an item is given by a keyword at the start of the heading,
  12432. like
  12433. *** TODO Write paper
  12434. *** DONE Call mom
  12435. The different keywords are specified in the variable `org-todo-keywords'.
  12436. By default the available states are \"TODO\" and \"DONE\".
  12437. So for this example: when the item starts with TODO, it is changed to DONE.
  12438. When it starts with DONE, the DONE is removed. And when neither TODO nor
  12439. DONE are present, add TODO at the beginning of the heading.
  12440. With C-u prefix arg, use completion to determine the new state.
  12441. With numeric prefix arg, switch to that state.
  12442. For calling through lisp, arg is also interpreted in the following way:
  12443. 'none -> empty state
  12444. \"\"(empty string) -> switch to empty state
  12445. 'done -> switch to DONE
  12446. 'nextset -> switch to the next set of keywords
  12447. 'previousset -> switch to the previous set of keywords
  12448. \"WAITING\" -> switch to the specified keyword, but only if it
  12449. really is a member of `org-todo-keywords'."
  12450. (interactive "P")
  12451. (save-excursion
  12452. (catch 'exit
  12453. (org-back-to-heading)
  12454. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  12455. (or (looking-at (concat " +" org-todo-regexp " *"))
  12456. (looking-at " *"))
  12457. (let* ((match-data (match-data))
  12458. (startpos (point-at-bol))
  12459. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  12460. (org-log-done (org-parse-local-options logging 'org-log-done))
  12461. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  12462. (this (match-string 1))
  12463. (hl-pos (match-beginning 0))
  12464. (head (org-get-todo-sequence-head this))
  12465. (ass (assoc head org-todo-kwd-alist))
  12466. (interpret (nth 1 ass))
  12467. (done-word (nth 3 ass))
  12468. (final-done-word (nth 4 ass))
  12469. (last-state (or this ""))
  12470. (completion-ignore-case t)
  12471. (member (member this org-todo-keywords-1))
  12472. (tail (cdr member))
  12473. (state (cond
  12474. ((and org-todo-key-trigger
  12475. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  12476. (and (not arg) org-use-fast-todo-selection
  12477. (not (eq org-use-fast-todo-selection 'prefix)))))
  12478. ;; Use fast selection
  12479. (org-fast-todo-selection))
  12480. ((and (equal arg '(4))
  12481. (or (not org-use-fast-todo-selection)
  12482. (not org-todo-key-trigger)))
  12483. ;; Read a state with completion
  12484. (completing-read "State: " (mapcar (lambda(x) (list x))
  12485. org-todo-keywords-1)
  12486. nil t))
  12487. ((eq arg 'right)
  12488. (if this
  12489. (if tail (car tail) nil)
  12490. (car org-todo-keywords-1)))
  12491. ((eq arg 'left)
  12492. (if (equal member org-todo-keywords-1)
  12493. nil
  12494. (if this
  12495. (nth (- (length org-todo-keywords-1) (length tail) 2)
  12496. org-todo-keywords-1)
  12497. (org-last org-todo-keywords-1))))
  12498. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  12499. (setq arg nil))) ; hack to fall back to cycling
  12500. (arg
  12501. ;; user or caller requests a specific state
  12502. (cond
  12503. ((equal arg "") nil)
  12504. ((eq arg 'none) nil)
  12505. ((eq arg 'done) (or done-word (car org-done-keywords)))
  12506. ((eq arg 'nextset)
  12507. (or (car (cdr (member head org-todo-heads)))
  12508. (car org-todo-heads)))
  12509. ((eq arg 'previousset)
  12510. (let ((org-todo-heads (reverse org-todo-heads)))
  12511. (or (car (cdr (member head org-todo-heads)))
  12512. (car org-todo-heads))))
  12513. ((car (member arg org-todo-keywords-1)))
  12514. ((nth (1- (prefix-numeric-value arg))
  12515. org-todo-keywords-1))))
  12516. ((null member) (or head (car org-todo-keywords-1)))
  12517. ((equal this final-done-word) nil) ;; -> make empty
  12518. ((null tail) nil) ;; -> first entry
  12519. ((eq interpret 'sequence)
  12520. (car tail))
  12521. ((memq interpret '(type priority))
  12522. (if (eq this-command last-command)
  12523. (car tail)
  12524. (if (> (length tail) 0)
  12525. (or done-word (car org-done-keywords))
  12526. nil)))
  12527. (t nil)))
  12528. (next (if state (concat " " state " ") " "))
  12529. (change-plist (list :type 'todo-state-change :from this :to state
  12530. :position startpos))
  12531. dostates)
  12532. (when org-blocker-hook
  12533. (unless (save-excursion
  12534. (save-match-data
  12535. (run-hook-with-args-until-failure
  12536. 'org-blocker-hook change-plist)))
  12537. (if (interactive-p)
  12538. (error "TODO state change from %s to %s blocked" this state)
  12539. ;; fail silently
  12540. (message "TODO state change from %s to %s blocked" this state)
  12541. (throw 'exit nil))))
  12542. (store-match-data match-data)
  12543. (replace-match next t t)
  12544. (unless (pos-visible-in-window-p hl-pos)
  12545. (message "TODO state changed to %s" (org-trim next)))
  12546. (unless head
  12547. (setq head (org-get-todo-sequence-head state)
  12548. ass (assoc head org-todo-kwd-alist)
  12549. interpret (nth 1 ass)
  12550. done-word (nth 3 ass)
  12551. final-done-word (nth 4 ass)))
  12552. (when (memq arg '(nextset previousset))
  12553. (message "Keyword-Set %d/%d: %s"
  12554. (- (length org-todo-sets) -1
  12555. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  12556. (length org-todo-sets)
  12557. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  12558. (setq org-last-todo-state-is-todo
  12559. (not (member state org-done-keywords)))
  12560. (when (and org-log-done (not (memq arg '(nextset previousset))))
  12561. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  12562. (or (not org-todo-log-states)
  12563. (member state org-todo-log-states))))
  12564. (cond
  12565. ((and state (member state org-not-done-keywords)
  12566. (not (member this org-not-done-keywords)))
  12567. ;; This is now a todo state and was not one before
  12568. ;; Remove any CLOSED timestamp, and possibly log the state change
  12569. (org-add-planning-info nil nil 'closed)
  12570. (and dostates (org-add-log-maybe 'state state 'findpos)))
  12571. ((and state dostates)
  12572. ;; This is a non-nil state, and we need to log it
  12573. (org-add-log-maybe 'state state 'findpos))
  12574. ((and (member state org-done-keywords)
  12575. (not (member this org-done-keywords)))
  12576. ;; It is now done, and it was not done before
  12577. (org-add-planning-info 'closed (org-current-time))
  12578. (org-add-log-maybe 'done state 'findpos))))
  12579. ;; Fixup tag positioning
  12580. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  12581. (run-hooks 'org-after-todo-state-change-hook)
  12582. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  12583. (if (and arg (not (member state org-done-keywords)))
  12584. (setq head (org-get-todo-sequence-head state)))
  12585. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  12586. ;; Fixup cursor location if close to the keyword
  12587. (if (and (outline-on-heading-p)
  12588. (not (bolp))
  12589. (save-excursion (beginning-of-line 1)
  12590. (looking-at org-todo-line-regexp))
  12591. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  12592. (progn
  12593. (goto-char (or (match-end 2) (match-end 1)))
  12594. (just-one-space)))
  12595. (when org-trigger-hook
  12596. (save-excursion
  12597. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  12598. (defun org-get-todo-sequence-head (kwd)
  12599. "Return the head of the TODO sequence to which KWD belongs.
  12600. If KWD is not set, check if there is a text property remembering the
  12601. right sequence."
  12602. (let (p)
  12603. (cond
  12604. ((not kwd)
  12605. (or (get-text-property (point-at-bol) 'org-todo-head)
  12606. (progn
  12607. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  12608. nil (point-at-eol)))
  12609. (get-text-property p 'org-todo-head))))
  12610. ((not (member kwd org-todo-keywords-1))
  12611. (car org-todo-keywords-1))
  12612. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  12613. (defun org-fast-todo-selection ()
  12614. "Fast TODO keyword selection with single keys.
  12615. Returns the new TODO keyword, or nil if no state change should occur."
  12616. (let* ((fulltable org-todo-key-alist)
  12617. (done-keywords org-done-keywords) ;; needed for the faces.
  12618. (maxlen (apply 'max (mapcar
  12619. (lambda (x)
  12620. (if (stringp (car x)) (string-width (car x)) 0))
  12621. fulltable)))
  12622. (expert nil)
  12623. (fwidth (+ maxlen 3 1 3))
  12624. (ncol (/ (- (window-width) 4) fwidth))
  12625. tg cnt e c tbl
  12626. groups ingroup)
  12627. (save-window-excursion
  12628. (if expert
  12629. (set-buffer (get-buffer-create " *Org todo*"))
  12630. ; (delete-other-windows)
  12631. ; (split-window-vertically)
  12632. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  12633. (erase-buffer)
  12634. (org-set-local 'org-done-keywords done-keywords)
  12635. (setq tbl fulltable cnt 0)
  12636. (while (setq e (pop tbl))
  12637. (cond
  12638. ((equal e '(:startgroup))
  12639. (push '() groups) (setq ingroup t)
  12640. (when (not (= cnt 0))
  12641. (setq cnt 0)
  12642. (insert "\n"))
  12643. (insert "{ "))
  12644. ((equal e '(:endgroup))
  12645. (setq ingroup nil cnt 0)
  12646. (insert "}\n"))
  12647. (t
  12648. (setq tg (car e) c (cdr e))
  12649. (if ingroup (push tg (car groups)))
  12650. (setq tg (org-add-props tg nil 'face
  12651. (org-get-todo-face tg)))
  12652. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12653. (insert "[" c "] " tg (make-string
  12654. (- fwidth 4 (length tg)) ?\ ))
  12655. (when (= (setq cnt (1+ cnt)) ncol)
  12656. (insert "\n")
  12657. (if ingroup (insert " "))
  12658. (setq cnt 0)))))
  12659. (insert "\n")
  12660. (goto-char (point-min))
  12661. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  12662. (fit-window-to-buffer))
  12663. (message "[a-z..]:Set [SPC]:clear")
  12664. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12665. (cond
  12666. ((or (= c ?\C-g)
  12667. (and (= c ?q) (not (rassoc c fulltable))))
  12668. (setq quit-flag t))
  12669. ((= c ?\ ) nil)
  12670. ((setq e (rassoc c fulltable) tg (car e))
  12671. tg)
  12672. (t (setq quit-flag t))))))
  12673. (defun org-get-repeat ()
  12674. "Check if tere is a deadline/schedule with repeater in this entry."
  12675. (save-match-data
  12676. (save-excursion
  12677. (org-back-to-heading t)
  12678. (if (re-search-forward
  12679. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  12680. (match-string 1)))))
  12681. (defvar org-last-changed-timestamp)
  12682. (defvar org-log-post-message)
  12683. (defun org-auto-repeat-maybe ()
  12684. "Check if the current headline contains a repeated deadline/schedule.
  12685. If yes, set TODO state back to what it was and change the base date
  12686. of repeating deadline/scheduled time stamps to new date.
  12687. This function should be run in the `org-after-todo-state-change-hook'."
  12688. ;; last-state is dynamically scoped into this function
  12689. (let* ((repeat (org-get-repeat))
  12690. (aa (assoc last-state org-todo-kwd-alist))
  12691. (interpret (nth 1 aa))
  12692. (head (nth 2 aa))
  12693. (done-word (nth 3 aa))
  12694. (whata '(("d" . day) ("m" . month) ("y" . year)))
  12695. (msg "Entry repeats: ")
  12696. (org-log-done)
  12697. re type n what ts)
  12698. (when repeat
  12699. (org-todo (if (eq interpret 'type) last-state head))
  12700. (when (and org-log-repeat
  12701. (not (memq 'org-add-log-note
  12702. (default-value 'post-command-hook))))
  12703. ;; Make sure a note is taken
  12704. (let ((org-log-done '(done)))
  12705. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  12706. 'findpos)))
  12707. (org-back-to-heading t)
  12708. (org-add-planning-info nil nil 'closed)
  12709. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  12710. org-deadline-time-regexp "\\)"))
  12711. (while (re-search-forward
  12712. re (save-excursion (outline-next-heading) (point)) t)
  12713. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  12714. ts (match-string (if (match-end 2) 2 4)))
  12715. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  12716. (setq n (string-to-number (match-string 1 ts))
  12717. what (match-string 2 ts))
  12718. (if (equal what "w") (setq n (* n 7) what "d"))
  12719. (org-timestamp-change n (cdr (assoc what whata))))
  12720. (setq msg (concat msg type org-last-changed-timestamp " ")))
  12721. (setq org-log-post-message msg)
  12722. (message msg))))
  12723. (defun org-show-todo-tree (arg)
  12724. "Make a compact tree which shows all headlines marked with TODO.
  12725. The tree will show the lines where the regexp matches, and all higher
  12726. headlines above the match.
  12727. With \\[universal-argument] prefix, also show the DONE entries.
  12728. With a numeric prefix N, construct a sparse tree for the Nth element
  12729. of `org-todo-keywords-1'."
  12730. (interactive "P")
  12731. (let ((case-fold-search nil)
  12732. (kwd-re
  12733. (cond ((null arg) org-not-done-regexp)
  12734. ((equal arg '(4))
  12735. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  12736. (mapcar 'list org-todo-keywords-1))))
  12737. (concat "\\("
  12738. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12739. "\\)\\>")))
  12740. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12741. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12742. org-todo-keywords-1)))
  12743. (t (error "Invalid prefix argument: %s" arg)))))
  12744. (message "%d TODO entries found"
  12745. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  12746. (defun org-deadline (&optional remove)
  12747. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12748. With argument REMOVE, remove any deadline from the item."
  12749. (interactive "P")
  12750. (if remove
  12751. (progn
  12752. (org-add-planning-info nil nil 'deadline)
  12753. (message "Item no longer has a deadline."))
  12754. (org-add-planning-info 'deadline nil 'closed)))
  12755. (defun org-schedule (&optional remove)
  12756. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12757. With argument REMOVE, remove any scheduling date from the item."
  12758. (interactive "P")
  12759. (if remove
  12760. (progn
  12761. (org-add-planning-info nil nil 'scheduled)
  12762. (message "Item is no longer scheduled."))
  12763. (org-add-planning-info 'scheduled nil 'closed)))
  12764. (defun org-add-planning-info (what &optional time &rest remove)
  12765. "Insert new timestamp with keyword in the line directly after the headline.
  12766. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  12767. If non is given, the user is prompted for a date.
  12768. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12769. be removed."
  12770. (interactive)
  12771. (let (org-time-was-given org-end-time-was-given)
  12772. (when what (setq time (or time (org-read-date nil 'to-time))))
  12773. (when (and org-insert-labeled-timestamps-at-point
  12774. (member what '(scheduled deadline)))
  12775. (insert
  12776. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12777. (org-insert-time-stamp time org-time-was-given
  12778. nil nil nil (list org-end-time-was-given))
  12779. (setq what nil))
  12780. (save-excursion
  12781. (save-restriction
  12782. (let (col list elt ts buffer-invisibility-spec)
  12783. (org-back-to-heading t)
  12784. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  12785. (goto-char (match-end 1))
  12786. (setq col (current-column))
  12787. (goto-char (match-end 0))
  12788. (if (eobp) (insert "\n") (forward-char 1))
  12789. (if (and (not (looking-at outline-regexp))
  12790. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12791. "[^\r\n]*"))
  12792. (not (equal (match-string 1) org-clock-string)))
  12793. (narrow-to-region (match-beginning 0) (match-end 0))
  12794. (insert-before-markers "\n")
  12795. (backward-char 1)
  12796. (narrow-to-region (point) (point))
  12797. (indent-to-column col))
  12798. ;; Check if we have to remove something.
  12799. (setq list (cons what remove))
  12800. (while list
  12801. (setq elt (pop list))
  12802. (goto-char (point-min))
  12803. (when (or (and (eq elt 'scheduled)
  12804. (re-search-forward org-scheduled-time-regexp nil t))
  12805. (and (eq elt 'deadline)
  12806. (re-search-forward org-deadline-time-regexp nil t))
  12807. (and (eq elt 'closed)
  12808. (re-search-forward org-closed-time-regexp nil t)))
  12809. (replace-match "")
  12810. (if (looking-at "--+<[^>]+>") (replace-match ""))
  12811. (if (looking-at " +") (replace-match ""))))
  12812. (goto-char (point-max))
  12813. (when what
  12814. (insert
  12815. (if (not (equal (char-before) ?\ )) " " "")
  12816. (cond ((eq what 'scheduled) org-scheduled-string)
  12817. ((eq what 'deadline) org-deadline-string)
  12818. ((eq what 'closed) org-closed-string))
  12819. " ")
  12820. (setq ts (org-insert-time-stamp
  12821. time
  12822. (or org-time-was-given
  12823. (and (eq what 'closed) org-log-done-with-time))
  12824. (eq what 'closed)
  12825. nil nil (list org-end-time-was-given)))
  12826. (end-of-line 1))
  12827. (goto-char (point-min))
  12828. (widen)
  12829. (if (looking-at "[ \t]+\r?\n")
  12830. (replace-match ""))
  12831. ts)))))
  12832. (defvar org-log-note-marker (make-marker))
  12833. (defvar org-log-note-purpose nil)
  12834. (defvar org-log-note-state nil)
  12835. (defvar org-log-note-window-configuration nil)
  12836. (defvar org-log-note-return-to (make-marker))
  12837. (defvar org-log-post-message nil
  12838. "Message to be displayed after a log note has been stored.
  12839. The auto-repeater uses this.")
  12840. (defun org-add-log-maybe (&optional purpose state findpos)
  12841. "Set up the post command hook to take a note."
  12842. (save-excursion
  12843. (when (and (listp org-log-done)
  12844. (memq purpose org-log-done))
  12845. (when findpos
  12846. (org-back-to-heading t)
  12847. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  12848. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12849. "[^\r\n]*\\)?"))
  12850. (goto-char (match-end 0))
  12851. (unless org-log-states-order-reversed
  12852. (and (= (char-after) ?\n) (forward-char 1))
  12853. (org-skip-over-state-notes)
  12854. (skip-chars-backward " \t\n\r")))
  12855. (move-marker org-log-note-marker (point))
  12856. (setq org-log-note-purpose purpose)
  12857. (setq org-log-note-state state)
  12858. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  12859. (defun org-skip-over-state-notes ()
  12860. "Skip past the list of State notes in an entry."
  12861. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12862. (while (looking-at "[ \t]*- State")
  12863. (condition-case nil
  12864. (org-next-item)
  12865. (error (org-end-of-item)))))
  12866. (defun org-add-log-note (&optional purpose)
  12867. "Pop up a window for taking a note, and add this note later at point."
  12868. (remove-hook 'post-command-hook 'org-add-log-note)
  12869. (setq org-log-note-window-configuration (current-window-configuration))
  12870. (delete-other-windows)
  12871. (move-marker org-log-note-return-to (point))
  12872. (switch-to-buffer (marker-buffer org-log-note-marker))
  12873. (goto-char org-log-note-marker)
  12874. (org-switch-to-buffer-other-window "*Org Note*")
  12875. (erase-buffer)
  12876. (let ((org-inhibit-startup t)) (org-mode))
  12877. (insert (format "# Insert note for %s.
  12878. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12879. (cond
  12880. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12881. ((eq org-log-note-purpose 'done) "closed todo item")
  12882. ((eq org-log-note-purpose 'state)
  12883. (format "state change to \"%s\"" org-log-note-state))
  12884. (t (error "This should not happen")))))
  12885. (org-set-local 'org-finish-function 'org-store-log-note))
  12886. (defun org-store-log-note ()
  12887. "Finish taking a log note, and insert it to where it belongs."
  12888. (let ((txt (buffer-string))
  12889. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12890. lines ind)
  12891. (kill-buffer (current-buffer))
  12892. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  12893. (setq txt (replace-match "" t t txt)))
  12894. (if (string-match "\\s-+\\'" txt)
  12895. (setq txt (replace-match "" t t txt)))
  12896. (setq lines (org-split-string txt "\n"))
  12897. (when (and note (string-match "\\S-" note))
  12898. (setq note
  12899. (org-replace-escapes
  12900. note
  12901. (list (cons "%u" (user-login-name))
  12902. (cons "%U" user-full-name)
  12903. (cons "%t" (format-time-string
  12904. (org-time-stamp-format 'long 'inactive)
  12905. (current-time)))
  12906. (cons "%s" (if org-log-note-state
  12907. (concat "\"" org-log-note-state "\"")
  12908. "")))))
  12909. (if lines (setq note (concat note " \\\\")))
  12910. (push note lines))
  12911. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  12912. (when lines
  12913. (save-excursion
  12914. (set-buffer (marker-buffer org-log-note-marker))
  12915. (save-excursion
  12916. (goto-char org-log-note-marker)
  12917. (move-marker org-log-note-marker nil)
  12918. (end-of-line 1)
  12919. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12920. (indent-relative nil)
  12921. (insert "- " (pop lines))
  12922. (org-indent-line-function)
  12923. (beginning-of-line 1)
  12924. (looking-at "[ \t]*")
  12925. (setq ind (concat (match-string 0) " "))
  12926. (end-of-line 1)
  12927. (while lines (insert "\n" ind (pop lines)))))))
  12928. (set-window-configuration org-log-note-window-configuration)
  12929. (with-current-buffer (marker-buffer org-log-note-return-to)
  12930. (goto-char org-log-note-return-to))
  12931. (move-marker org-log-note-return-to nil)
  12932. (and org-log-post-message (message org-log-post-message)))
  12933. ;; FIXME: what else would be useful?
  12934. ;; - priority
  12935. ;; - date
  12936. (defun org-sparse-tree (&optional arg)
  12937. "Create a sparse tree, prompt for the details.
  12938. This command can create sparse trees. You first need to select the type
  12939. of match used to create the tree:
  12940. t Show entries with a specific TODO keyword.
  12941. T Show entries selected by a tags match.
  12942. p Enter a property name and its value (both with completion on existing
  12943. names/values) and show entries with that property.
  12944. r Show entries matching a regular expression"
  12945. (interactive "P")
  12946. (let (ans kwd value)
  12947. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines")
  12948. (setq ans (read-char-exclusive))
  12949. (cond
  12950. ((equal ans ?d)
  12951. (call-interactively 'org-check-deadlines))
  12952. ((equal ans ?t)
  12953. (org-show-todo-tree '(4)))
  12954. ((equal ans ?T)
  12955. (call-interactively 'org-tags-sparse-tree))
  12956. ((member ans '(?p ?P))
  12957. (setq kwd (completing-read "Property: "
  12958. (mapcar 'list (org-buffer-property-keys))))
  12959. (setq value (completing-read "Value: "
  12960. (mapcar 'list (org-property-values kwd))))
  12961. (unless (string-match "\\`{.*}\\'" value)
  12962. (setq value (concat "\"" value "\"")))
  12963. (org-tags-sparse-tree arg (concat kwd "=" value)))
  12964. ((member ans '(?r ?R ?/))
  12965. (call-interactively 'org-occur))
  12966. (t (error "No such sparse tree command \"%c\"" ans)))))
  12967. (defvar org-occur-highlights nil)
  12968. (make-variable-buffer-local 'org-occur-highlights)
  12969. (defun org-occur (regexp &optional keep-previous callback)
  12970. "Make a compact tree which shows all matches of REGEXP.
  12971. The tree will show the lines where the regexp matches, and all higher
  12972. headlines above the match. It will also show the heading after the match,
  12973. to make sure editing the matching entry is easy.
  12974. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12975. call to `org-occur' will be kept, to allow stacking of calls to this
  12976. command.
  12977. If CALLBACK is non-nil, it is a function which is called to confirm
  12978. that the match should indeed be shown."
  12979. (interactive "sRegexp: \nP")
  12980. (or keep-previous (org-remove-occur-highlights nil nil t))
  12981. (let ((cnt 0))
  12982. (save-excursion
  12983. (goto-char (point-min))
  12984. (if (or (not keep-previous) ; do not want to keep
  12985. (not org-occur-highlights)) ; no previous matches
  12986. ;; hide everything
  12987. (org-overview))
  12988. (while (re-search-forward regexp nil t)
  12989. (when (or (not callback)
  12990. (save-match-data (funcall callback)))
  12991. (setq cnt (1+ cnt))
  12992. (when org-highlight-sparse-tree-matches
  12993. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12994. (org-show-context 'occur-tree))))
  12995. (when org-remove-highlights-with-change
  12996. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12997. nil 'local))
  12998. (unless org-sparse-tree-open-archived-trees
  12999. (org-hide-archived-subtrees (point-min) (point-max)))
  13000. (run-hooks 'org-occur-hook)
  13001. (if (interactive-p)
  13002. (message "%d match(es) for regexp %s" cnt regexp))
  13003. cnt))
  13004. (defun org-show-context (&optional key)
  13005. "Make sure point and context and visible.
  13006. How much context is shown depends upon the variables
  13007. `org-show-hierarchy-above', `org-show-following-heading'. and
  13008. `org-show-siblings'."
  13009. (let ((heading-p (org-on-heading-p t))
  13010. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13011. (following-p (org-get-alist-option org-show-following-heading key))
  13012. (entry-p (org-get-alist-option org-show-entry-below key))
  13013. (siblings-p (org-get-alist-option org-show-siblings key)))
  13014. (catch 'exit
  13015. ;; Show heading or entry text
  13016. (if (and heading-p (not entry-p))
  13017. (org-flag-heading nil) ; only show the heading
  13018. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13019. (org-show-hidden-entry))) ; show entire entry
  13020. (when following-p
  13021. ;; Show next sibling, or heading below text
  13022. (save-excursion
  13023. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13024. (org-flag-heading nil))))
  13025. (when siblings-p (org-show-siblings))
  13026. (when hierarchy-p
  13027. ;; show all higher headings, possibly with siblings
  13028. (save-excursion
  13029. (while (and (condition-case nil
  13030. (progn (org-up-heading-all 1) t)
  13031. (error nil))
  13032. (not (bobp)))
  13033. (org-flag-heading nil)
  13034. (when siblings-p (org-show-siblings))))))))
  13035. (defun org-reveal (&optional siblings)
  13036. "Show current entry, hierarchy above it, and the following headline.
  13037. This can be used to show a consistent set of context around locations
  13038. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13039. not t for the search context.
  13040. With optional argument SIBLINGS, on each level of the hierarchy all
  13041. siblings are shown. This repairs the tree structure to what it would
  13042. look like when opened with hierarchical calls to `org-cycle'."
  13043. (interactive "P")
  13044. (let ((org-show-hierarchy-above t)
  13045. (org-show-following-heading t)
  13046. (org-show-siblings (if siblings t org-show-siblings)))
  13047. (org-show-context nil)))
  13048. (defun org-highlight-new-match (beg end)
  13049. "Highlight from BEG to END and mark the highlight is an occur headline."
  13050. (let ((ov (org-make-overlay beg end)))
  13051. (org-overlay-put ov 'face 'secondary-selection)
  13052. (push ov org-occur-highlights)))
  13053. (defun org-remove-occur-highlights (&optional beg end noremove)
  13054. "Remove the occur highlights from the buffer.
  13055. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13056. from the `before-change-functions' in the current buffer."
  13057. (interactive)
  13058. (unless org-inhibit-highlight-removal
  13059. (mapc 'org-delete-overlay org-occur-highlights)
  13060. (setq org-occur-highlights nil)
  13061. (unless noremove
  13062. (remove-hook 'before-change-functions
  13063. 'org-remove-occur-highlights 'local))))
  13064. ;;;; Priorities
  13065. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13066. "Regular expression matching the priority indicator.")
  13067. (defvar org-remove-priority-next-time nil)
  13068. (defun org-priority-up ()
  13069. "Increase the priority of the current item."
  13070. (interactive)
  13071. (org-priority 'up))
  13072. (defun org-priority-down ()
  13073. "Decrease the priority of the current item."
  13074. (interactive)
  13075. (org-priority 'down))
  13076. (defun org-priority (&optional action)
  13077. "Change the priority of an item by ARG.
  13078. ACTION can be `set', `up', `down', or a character."
  13079. (interactive)
  13080. (setq action (or action 'set))
  13081. (let (current new news have remove)
  13082. (save-excursion
  13083. (org-back-to-heading)
  13084. (if (looking-at org-priority-regexp)
  13085. (setq current (string-to-char (match-string 2))
  13086. have t)
  13087. (setq current org-default-priority))
  13088. (cond
  13089. ((or (eq action 'set) (integerp action))
  13090. (if (integerp action)
  13091. (setq new action)
  13092. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13093. (setq new (read-char-exclusive)))
  13094. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13095. (= (upcase org-lowest-priority) org-lowest-priority))
  13096. (setq new (upcase new)))
  13097. (cond ((equal new ?\ ) (setq remove t))
  13098. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13099. (error "Priority must be between `%c' and `%c'"
  13100. org-highest-priority org-lowest-priority))))
  13101. ((eq action 'up)
  13102. (if (and (not have) (eq last-command this-command))
  13103. (setq new org-lowest-priority)
  13104. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13105. org-default-priority (1- current)))))
  13106. ((eq action 'down)
  13107. (if (and (not have) (eq last-command this-command))
  13108. (setq new org-highest-priority)
  13109. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13110. org-default-priority (1+ current)))))
  13111. (t (error "Invalid action")))
  13112. (if (or (< (upcase new) org-highest-priority)
  13113. (> (upcase new) org-lowest-priority))
  13114. (setq remove t))
  13115. (setq news (format "%c" new))
  13116. (if have
  13117. (if remove
  13118. (replace-match "" t t nil 1)
  13119. (replace-match news t t nil 2))
  13120. (if remove
  13121. (error "No priority cookie found in line")
  13122. (looking-at org-todo-line-regexp)
  13123. (if (match-end 2)
  13124. (progn
  13125. (goto-char (match-end 2))
  13126. (insert " [#" news "]"))
  13127. (goto-char (match-beginning 3))
  13128. (insert "[#" news "] ")))))
  13129. (org-preserve-lc (org-set-tags nil 'align))
  13130. (if remove
  13131. (message "Priority removed")
  13132. (message "Priority of current item set to %s" news))))
  13133. (defun org-get-priority (s)
  13134. "Find priority cookie and return priority."
  13135. (save-match-data
  13136. (if (not (string-match org-priority-regexp s))
  13137. (* 1000 (- org-lowest-priority org-default-priority))
  13138. (* 1000 (- org-lowest-priority
  13139. (string-to-char (match-string 2 s)))))))
  13140. ;;;; Tags
  13141. (defun org-scan-tags (action matcher &optional todo-only)
  13142. "Scan headline tags with inheritance and produce output ACTION.
  13143. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13144. evaluated, testing if a given set of tags qualifies a headline for
  13145. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13146. are included in the output."
  13147. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13148. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13149. (org-re
  13150. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13151. (props (list 'face nil
  13152. 'done-face 'org-done
  13153. 'undone-face nil
  13154. 'mouse-face 'highlight
  13155. 'org-not-done-regexp org-not-done-regexp
  13156. 'org-todo-regexp org-todo-regexp
  13157. 'keymap org-agenda-keymap
  13158. 'help-echo
  13159. (format "mouse-2 or RET jump to org file %s"
  13160. (abbreviate-file-name
  13161. (or (buffer-file-name (buffer-base-buffer))
  13162. (buffer-name (buffer-base-buffer)))))))
  13163. (case-fold-search nil)
  13164. lspos
  13165. tags tags-list tags-alist (llast 0) rtn level category i txt
  13166. todo marker entry priority)
  13167. (save-excursion
  13168. (goto-char (point-min))
  13169. (when (eq action 'sparse-tree)
  13170. (org-overview)
  13171. (org-remove-occur-highlights))
  13172. (while (re-search-forward re nil t)
  13173. (catch :skip
  13174. (setq todo (if (match-end 1) (match-string 2))
  13175. tags (if (match-end 4) (match-string 4)))
  13176. (goto-char (setq lspos (1+ (match-beginning 0))))
  13177. (setq level (org-reduced-level (funcall outline-level))
  13178. category (org-get-category))
  13179. (setq i llast llast level)
  13180. ;; remove tag lists from same and sublevels
  13181. (while (>= i level)
  13182. (when (setq entry (assoc i tags-alist))
  13183. (setq tags-alist (delete entry tags-alist)))
  13184. (setq i (1- i)))
  13185. ;; add the nex tags
  13186. (when tags
  13187. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13188. tags-alist
  13189. (cons (cons level tags) tags-alist)))
  13190. ;; compile tags for current headline
  13191. (setq tags-list
  13192. (if org-use-tag-inheritance
  13193. (apply 'append (mapcar 'cdr tags-alist))
  13194. tags))
  13195. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13196. (eval matcher)
  13197. (or (not org-agenda-skip-archived-trees)
  13198. (not (member org-archive-tag tags-list))))
  13199. (and (eq action 'agenda) (org-agenda-skip))
  13200. ;; list this headline
  13201. (if (eq action 'sparse-tree)
  13202. (progn
  13203. (and org-highlight-sparse-tree-matches
  13204. (org-get-heading) (match-end 0)
  13205. (org-highlight-new-match
  13206. (match-beginning 0) (match-beginning 1)))
  13207. (org-show-context 'tags-tree))
  13208. (setq txt (org-format-agenda-item
  13209. ""
  13210. (concat
  13211. (if org-tags-match-list-sublevels
  13212. (make-string (1- level) ?.) "")
  13213. (org-get-heading))
  13214. category tags-list)
  13215. priority (org-get-priority txt))
  13216. (goto-char lspos)
  13217. (setq marker (org-agenda-new-marker))
  13218. (org-add-props txt props
  13219. 'org-marker marker 'org-hd-marker marker 'org-category category
  13220. 'priority priority 'type "tagsmatch")
  13221. (push txt rtn))
  13222. ;; if we are to skip sublevels, jump to end of subtree
  13223. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13224. (when (and (eq action 'sparse-tree)
  13225. (not org-sparse-tree-open-archived-trees))
  13226. (org-hide-archived-subtrees (point-min) (point-max)))
  13227. (nreverse rtn)))
  13228. (defvar todo-only) ;; dynamically scoped
  13229. (defun org-tags-sparse-tree (&optional todo-only match)
  13230. "Create a sparse tree according to tags string MATCH.
  13231. MATCH can contain positive and negative selection of tags, like
  13232. \"+WORK+URGENT-WITHBOSS\".
  13233. If optional argument TODO_ONLY is non-nil, only select lines that are
  13234. also TODO lines."
  13235. (interactive "P")
  13236. (org-prepare-agenda-buffers (list (current-buffer)))
  13237. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13238. (defvar org-cached-props nil)
  13239. (defun org-cached-entry-get (pom property)
  13240. (if (or (eq t org-use-property-inheritance)
  13241. (member property org-use-property-inheritance))
  13242. ;; Caching is not possible, check it directly
  13243. (org-entry-get pom property 'inherit)
  13244. ;; Get all properties, so that we can do complicated checks easily
  13245. (cdr (assoc property (or org-cached-props
  13246. (setq org-cached-props
  13247. (org-entry-properties pom)))))))
  13248. (defun org-global-tags-completion-table (&optional files)
  13249. "Return the list of all tags in all agenda buffer/files."
  13250. (save-excursion
  13251. (org-uniquify
  13252. (apply 'append
  13253. (mapcar
  13254. (lambda (file)
  13255. (set-buffer (find-file-noselect file))
  13256. (org-get-buffer-tags))
  13257. (if (and files (car files))
  13258. files
  13259. (org-agenda-files)))))))
  13260. (defun org-make-tags-matcher (match)
  13261. "Create the TAGS//TODO matcher form for the selection string MATCH."
  13262. ;; todo-only is scoped dynamically into this function, and the function
  13263. ;; may change it it the matcher asksk for it.
  13264. (unless match
  13265. ;; Get a new match request, with completion
  13266. (let ((org-last-tags-completion-table
  13267. (org-global-tags-completion-table)))
  13268. (setq match (completing-read
  13269. "Match: " 'org-tags-completion-function nil nil nil
  13270. 'org-tags-history))))
  13271. ;; Parse the string and create a lisp form
  13272. (let ((match0 match)
  13273. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  13274. minus tag mm
  13275. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13276. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  13277. (if (string-match "/+" match)
  13278. ;; match contains also a todo-matching request
  13279. (progn
  13280. (setq tagsmatch (substring match 0 (match-beginning 0))
  13281. todomatch (substring match (match-end 0)))
  13282. (if (string-match "^!" todomatch)
  13283. (setq todo-only t todomatch (substring todomatch 1)))
  13284. (if (string-match "^\\s-*$" todomatch)
  13285. (setq todomatch nil)))
  13286. ;; only matching tags
  13287. (setq tagsmatch match todomatch nil))
  13288. ;; Make the tags matcher
  13289. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13290. (setq tagsmatcher t)
  13291. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13292. (while (setq term (pop orterms))
  13293. (while (and (equal (substring term -1) "\\") orterms)
  13294. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13295. (while (string-match re term)
  13296. (setq minus (and (match-end 1)
  13297. (equal (match-string 1 term) "-"))
  13298. tag (match-string 2 term)
  13299. re-p (equal (string-to-char tag) ?{)
  13300. level-p (match-end 3)
  13301. prop-p (match-end 4)
  13302. mm (cond
  13303. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13304. (level-p `(= level ,(string-to-number
  13305. (match-string 3 term))))
  13306. (prop-p
  13307. (setq pn (match-string 4 term)
  13308. pv (match-string 5 term)
  13309. cat-p (equal pn "CATEGORY")
  13310. re-p (equal (string-to-char pv) ?{)
  13311. pv (substring pv 1 -1))
  13312. (if (equal pn "CATEGORY")
  13313. (setq gv '(get-text-property (point) 'org-category))
  13314. (setq gv `(org-cached-entry-get nil ,pn)))
  13315. (if re-p
  13316. `(string-match ,pv (or ,gv ""))
  13317. `(equal ,pv ,gv)))
  13318. (t `(member ,(downcase tag) tags-list)))
  13319. mm (if minus (list 'not mm) mm)
  13320. term (substring term (match-end 0)))
  13321. (push mm tagsmatcher))
  13322. (push (if (> (length tagsmatcher) 1)
  13323. (cons 'and tagsmatcher)
  13324. (car tagsmatcher))
  13325. orlist)
  13326. (setq tagsmatcher nil))
  13327. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13328. (setq tagsmatcher
  13329. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13330. ;; Make the todo matcher
  13331. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13332. (setq todomatcher t)
  13333. (setq orterms (org-split-string todomatch "|") orlist nil)
  13334. (while (setq term (pop orterms))
  13335. (while (string-match re term)
  13336. (setq minus (and (match-end 1)
  13337. (equal (match-string 1 term) "-"))
  13338. kwd (match-string 2 term)
  13339. re-p (equal (string-to-char kwd) ?{)
  13340. term (substring term (match-end 0))
  13341. mm (if re-p
  13342. `(string-match ,(substring kwd 1 -1) todo)
  13343. (list 'equal 'todo kwd))
  13344. mm (if minus (list 'not mm) mm))
  13345. (push mm todomatcher))
  13346. (push (if (> (length todomatcher) 1)
  13347. (cons 'and todomatcher)
  13348. (car todomatcher))
  13349. orlist)
  13350. (setq todomatcher nil))
  13351. (setq todomatcher (if (> (length orlist) 1)
  13352. (cons 'or orlist) (car orlist))))
  13353. ;; Return the string and lisp forms of the matcher
  13354. (setq matcher (if todomatcher
  13355. (list 'and tagsmatcher todomatcher)
  13356. tagsmatcher))
  13357. (cons match0 matcher)))
  13358. (defun org-match-any-p (re list)
  13359. "Does re match any element of list?"
  13360. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13361. (delq nil list))
  13362. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  13363. (defvar org-tags-overlay (org-make-overlay 1 1))
  13364. (org-detach-overlay org-tags-overlay)
  13365. (defun org-align-tags-here (to-col)
  13366. ;; Assumes that this is a headline
  13367. (let ((pos (point)) (col (current-column)) tags)
  13368. (beginning-of-line 1)
  13369. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13370. (< pos (match-beginning 2)))
  13371. (progn
  13372. (setq tags (match-string 2))
  13373. (goto-char (match-beginning 1))
  13374. (insert " ")
  13375. (delete-region (point) (1+ (match-end 0)))
  13376. (backward-char 1)
  13377. (move-to-column
  13378. (max (1+ (current-column))
  13379. (1+ col)
  13380. (if (> to-col 0)
  13381. to-col
  13382. (- (abs to-col) (length tags))))
  13383. t)
  13384. (insert tags)
  13385. (move-to-column (min (current-column) col) t))
  13386. (goto-char pos))))
  13387. (defun org-set-tags (&optional arg just-align)
  13388. "Set the tags for the current headline.
  13389. With prefix ARG, realign all tags in headings in the current buffer."
  13390. (interactive "P")
  13391. (let* ((re (concat "^" outline-regexp))
  13392. (current (org-get-tags-string))
  13393. (col (current-column))
  13394. (org-setting-tags t)
  13395. table current-tags inherited-tags ; computed below when needed
  13396. tags p0 c0 c1 rpl)
  13397. (if arg
  13398. (save-excursion
  13399. (goto-char (point-min))
  13400. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13401. (while (re-search-forward re nil t)
  13402. (org-set-tags nil t)
  13403. (end-of-line 1)))
  13404. (message "All tags realigned to column %d" org-tags-column))
  13405. (if just-align
  13406. (setq tags current)
  13407. ;; Get a new set of tags from the user
  13408. (save-excursion
  13409. (setq table (or org-tag-alist (org-get-buffer-tags))
  13410. org-last-tags-completion-table table
  13411. current-tags (org-split-string current ":")
  13412. inherited-tags (nreverse
  13413. (nthcdr (length current-tags)
  13414. (nreverse (org-get-tags-at))))
  13415. tags
  13416. (if (or (eq t org-use-fast-tag-selection)
  13417. (and org-use-fast-tag-selection
  13418. (delq nil (mapcar 'cdr table))))
  13419. (org-fast-tag-selection
  13420. current-tags inherited-tags table
  13421. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  13422. (let ((org-add-colon-after-tag-completion t))
  13423. (org-trim
  13424. (org-without-partial-completion
  13425. (completing-read "Tags: " 'org-tags-completion-function
  13426. nil nil current 'org-tags-history)))))))
  13427. (while (string-match "[-+&]+" tags)
  13428. ;; No boolean logic, just a list
  13429. (setq tags (replace-match ":" t t tags))))
  13430. (if (string-match "\\`[\t ]*\\'" tags)
  13431. (setq tags "")
  13432. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13433. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13434. ;; Insert new tags at the correct column
  13435. (beginning-of-line 1)
  13436. (cond
  13437. ((and (equal current "") (equal tags "")))
  13438. ((re-search-forward
  13439. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13440. (point-at-eol) t)
  13441. (if (equal tags "")
  13442. (setq rpl "")
  13443. (goto-char (match-beginning 0))
  13444. (setq c0 (current-column) p0 (point)
  13445. c1 (max (1+ c0) (if (> org-tags-column 0)
  13446. org-tags-column
  13447. (- (- org-tags-column) (length tags))))
  13448. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13449. (replace-match rpl t t)
  13450. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  13451. tags)
  13452. (t (error "Tags alignment failed")))
  13453. (move-to-column col)
  13454. (unless just-align
  13455. (run-hooks 'org-after-tags-change-hook)))))
  13456. (defun org-change-tag-in-region (beg end tag off)
  13457. "Add or remove TAG for each entry in the region.
  13458. This works in the agenda, and also in an org-mode buffer."
  13459. (interactive
  13460. (list (region-beginning) (region-end)
  13461. (let ((org-last-tags-completion-table
  13462. (if (org-mode-p)
  13463. (org-get-buffer-tags)
  13464. (org-global-tags-completion-table))))
  13465. (completing-read
  13466. "Tag: " 'org-tags-completion-function nil nil nil
  13467. 'org-tags-history))
  13468. (progn
  13469. (message "[s]et or [r]emove? ")
  13470. (equal (read-char-exclusive) ?r))))
  13471. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13472. (let ((agendap (equal major-mode 'org-agenda-mode))
  13473. l1 l2 m buf pos newhead (cnt 0))
  13474. (goto-char end)
  13475. (setq l2 (1- (org-current-line)))
  13476. (goto-char beg)
  13477. (setq l1 (org-current-line))
  13478. (loop for l from l1 to l2 do
  13479. (goto-line l)
  13480. (setq m (get-text-property (point) 'org-hd-marker))
  13481. (when (or (and (org-mode-p) (org-on-heading-p))
  13482. (and agendap m))
  13483. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13484. pos (if agendap m (point)))
  13485. (with-current-buffer buf
  13486. (save-excursion
  13487. (save-restriction
  13488. (goto-char pos)
  13489. (setq cnt (1+ cnt))
  13490. (org-toggle-tag tag (if off 'off 'on))
  13491. (setq newhead (org-get-heading)))))
  13492. (and agendap (org-agenda-change-all-lines newhead m))))
  13493. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13494. (defun org-tags-completion-function (string predicate &optional flag)
  13495. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13496. (confirm (lambda (x) (stringp (car x)))))
  13497. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  13498. (setq s1 (match-string 1 string)
  13499. s2 (match-string 2 string))
  13500. (setq s1 "" s2 string))
  13501. (cond
  13502. ((eq flag nil)
  13503. ;; try completion
  13504. (setq rtn (try-completion s2 ctable confirm))
  13505. (if (stringp rtn)
  13506. (setq rtn
  13507. (concat s1 s2 (substring rtn (length s2))
  13508. (if (and org-add-colon-after-tag-completion
  13509. (assoc rtn ctable))
  13510. ":" ""))))
  13511. rtn)
  13512. ((eq flag t)
  13513. ;; all-completions
  13514. (all-completions s2 ctable confirm)
  13515. )
  13516. ((eq flag 'lambda)
  13517. ;; exact match?
  13518. (assoc s2 ctable)))
  13519. ))
  13520. (defun org-fast-tag-insert (kwd tags face &optional end)
  13521. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  13522. (insert (format "%-12s" (concat kwd ":"))
  13523. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13524. (or end "")))
  13525. (defun org-fast-tag-show-exit (flag)
  13526. (save-excursion
  13527. (goto-line 3)
  13528. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13529. (replace-match ""))
  13530. (when flag
  13531. (end-of-line 1)
  13532. (move-to-column (- (window-width) 19) t)
  13533. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13534. (defun org-set-current-tags-overlay (current prefix)
  13535. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13536. (if (featurep 'xemacs)
  13537. (org-overlay-display org-tags-overlay (concat prefix s)
  13538. 'secondary-selection)
  13539. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13540. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13541. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13542. "Fast tag selection with single keys.
  13543. CURRENT is the current list of tags in the headline, INHERITED is the
  13544. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13545. possibly with grouping information. TODO-TABLE is a similar table with
  13546. TODO keywords, should these have keys assigned to them.
  13547. If the keys are nil, a-z are automatically assigned.
  13548. Returns the new tags string, or nil to not change the current settings."
  13549. (let* ((fulltable (append table todo-table))
  13550. (maxlen (apply 'max (mapcar
  13551. (lambda (x)
  13552. (if (stringp (car x)) (string-width (car x)) 0))
  13553. fulltable)))
  13554. (buf (current-buffer))
  13555. (expert (eq org-fast-tag-selection-single-key 'expert))
  13556. (buffer-tags nil)
  13557. (fwidth (+ maxlen 3 1 3))
  13558. (ncol (/ (- (window-width) 4) fwidth))
  13559. (i-face 'org-done)
  13560. (c-face 'org-todo)
  13561. tg cnt e c char c1 c2 ntable tbl rtn
  13562. ov-start ov-end ov-prefix
  13563. (exit-after-next org-fast-tag-selection-single-key)
  13564. (done-keywords org-done-keywords)
  13565. groups ingroup)
  13566. (save-excursion
  13567. (beginning-of-line 1)
  13568. (if (looking-at
  13569. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13570. (setq ov-start (match-beginning 1)
  13571. ov-end (match-end 1)
  13572. ov-prefix "")
  13573. (setq ov-start (1- (point-at-eol))
  13574. ov-end (1+ ov-start))
  13575. (skip-chars-forward "^\n\r")
  13576. (setq ov-prefix
  13577. (concat
  13578. (buffer-substring (1- (point)) (point))
  13579. (if (> (current-column) org-tags-column)
  13580. " "
  13581. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13582. (org-move-overlay org-tags-overlay ov-start ov-end)
  13583. (save-window-excursion
  13584. (if expert
  13585. (set-buffer (get-buffer-create " *Org tags*"))
  13586. (delete-other-windows)
  13587. (split-window-vertically)
  13588. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13589. (erase-buffer)
  13590. (org-set-local 'org-done-keywords done-keywords)
  13591. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13592. (org-fast-tag-insert "Current" current c-face "\n\n")
  13593. (org-fast-tag-show-exit exit-after-next)
  13594. (org-set-current-tags-overlay current ov-prefix)
  13595. (setq tbl fulltable char ?a cnt 0)
  13596. (while (setq e (pop tbl))
  13597. (cond
  13598. ((equal e '(:startgroup))
  13599. (push '() groups) (setq ingroup t)
  13600. (when (not (= cnt 0))
  13601. (setq cnt 0)
  13602. (insert "\n"))
  13603. (insert "{ "))
  13604. ((equal e '(:endgroup))
  13605. (setq ingroup nil cnt 0)
  13606. (insert "}\n"))
  13607. (t
  13608. (setq tg (car e) c2 nil)
  13609. (if (cdr e)
  13610. (setq c (cdr e))
  13611. ;; automatically assign a character.
  13612. (setq c1 (string-to-char
  13613. (downcase (substring
  13614. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13615. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13616. (while (or (rassoc char ntable) (rassoc char table))
  13617. (setq char (1+ char)))
  13618. (setq c2 c1))
  13619. (setq c (or c2 char)))
  13620. (if ingroup (push tg (car groups)))
  13621. (setq tg (org-add-props tg nil 'face
  13622. (cond
  13623. ((not (assoc tg table))
  13624. (org-get-todo-face tg))
  13625. ((member tg current) c-face)
  13626. ((member tg inherited) i-face)
  13627. (t nil))))
  13628. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13629. (insert "[" c "] " tg (make-string
  13630. (- fwidth 4 (length tg)) ?\ ))
  13631. (push (cons tg c) ntable)
  13632. (when (= (setq cnt (1+ cnt)) ncol)
  13633. (insert "\n")
  13634. (if ingroup (insert " "))
  13635. (setq cnt 0)))))
  13636. (setq ntable (nreverse ntable))
  13637. (insert "\n")
  13638. (goto-char (point-min))
  13639. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13640. (fit-window-to-buffer))
  13641. (setq rtn
  13642. (catch 'exit
  13643. (while t
  13644. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  13645. (if groups " [!] no groups" " [!]groups")
  13646. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13647. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13648. (cond
  13649. ((= c ?\r) (throw 'exit t))
  13650. ((= c ?!)
  13651. (setq groups (not groups))
  13652. (goto-char (point-min))
  13653. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13654. ((= c ?\C-c)
  13655. (if (not expert)
  13656. (org-fast-tag-show-exit
  13657. (setq exit-after-next (not exit-after-next)))
  13658. (setq expert nil)
  13659. (delete-other-windows)
  13660. (split-window-vertically)
  13661. (org-switch-to-buffer-other-window " *Org tags*")
  13662. (and (fboundp 'fit-window-to-buffer)
  13663. (fit-window-to-buffer))))
  13664. ((or (= c ?\C-g)
  13665. (and (= c ?q) (not (rassoc c ntable))))
  13666. (org-detach-overlay org-tags-overlay)
  13667. (setq quit-flag t))
  13668. ((= c ?\ )
  13669. (setq current nil)
  13670. (if exit-after-next (setq exit-after-next 'now)))
  13671. ((= c ?\t)
  13672. (condition-case nil
  13673. (setq tg (completing-read
  13674. "Tag: "
  13675. (or buffer-tags
  13676. (with-current-buffer buf
  13677. (org-get-buffer-tags)))))
  13678. (quit (setq tg "")))
  13679. (when (string-match "\\S-" tg)
  13680. (add-to-list 'buffer-tags (list tg))
  13681. (if (member tg current)
  13682. (setq current (delete tg current))
  13683. (push tg current)))
  13684. (if exit-after-next (setq exit-after-next 'now)))
  13685. ((setq e (rassoc c todo-table) tg (car e))
  13686. (with-current-buffer buf
  13687. (save-excursion (org-todo tg)))
  13688. (if exit-after-next (setq exit-after-next 'now)))
  13689. ((setq e (rassoc c ntable) tg (car e))
  13690. (if (member tg current)
  13691. (setq current (delete tg current))
  13692. (loop for g in groups do
  13693. (if (member tg g)
  13694. (mapc (lambda (x)
  13695. (setq current (delete x current)))
  13696. g)))
  13697. (push tg current))
  13698. (if exit-after-next (setq exit-after-next 'now))))
  13699. ;; Create a sorted list
  13700. (setq current
  13701. (sort current
  13702. (lambda (a b)
  13703. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13704. (if (eq exit-after-next 'now) (throw 'exit t))
  13705. (goto-char (point-min))
  13706. (beginning-of-line 2)
  13707. (delete-region (point) (point-at-eol))
  13708. (org-fast-tag-insert "Current" current c-face)
  13709. (org-set-current-tags-overlay current ov-prefix)
  13710. (while (re-search-forward
  13711. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  13712. (setq tg (match-string 1))
  13713. (add-text-properties
  13714. (match-beginning 1) (match-end 1)
  13715. (list 'face
  13716. (cond
  13717. ((member tg current) c-face)
  13718. ((member tg inherited) i-face)
  13719. (t (get-text-property (match-beginning 1) 'face))))))
  13720. (goto-char (point-min)))))
  13721. (org-detach-overlay org-tags-overlay)
  13722. (if rtn
  13723. (mapconcat 'identity current ":")
  13724. nil))))
  13725. (defun org-get-tags-string ()
  13726. "Get the TAGS string in the current headline."
  13727. (unless (org-on-heading-p t)
  13728. (error "Not on a heading"))
  13729. (save-excursion
  13730. (beginning-of-line 1)
  13731. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13732. (org-match-string-no-properties 1)
  13733. "")))
  13734. (defun org-get-tags ()
  13735. "Get the list of tags specified in the current headline."
  13736. (org-split-string (org-get-tags-string) ":"))
  13737. (defun org-get-buffer-tags ()
  13738. "Get a table of all tags used in the buffer, for completion."
  13739. (let (tags)
  13740. (save-excursion
  13741. (goto-char (point-min))
  13742. (while (re-search-forward
  13743. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  13744. (when (equal (char-after (point-at-bol 0)) ?*)
  13745. (mapc (lambda (x) (add-to-list 'tags x))
  13746. (org-split-string (org-match-string-no-properties 1) ":")))))
  13747. (mapcar 'list tags)))
  13748. ;;;; Properties
  13749. ;;; Setting and retrieving properties
  13750. (defconst org-special-properties
  13751. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY")
  13752. "The special properties valid in Org-mode.
  13753. These are properties that are not defined in the property drawer,
  13754. but in some other way.")
  13755. (defconst org-default-properties
  13756. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  13757. "LOCATION" "LOGGING" "COLUMNS")
  13758. "Some properties that are used by Org-mode for various purposes.
  13759. Being in this list makes sure that they are offered for completion.")
  13760. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13761. "Regular expression matching the first line of a property drawer.")
  13762. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13763. "Regular expression matching the first line of a property drawer.")
  13764. (defun org-property-action ()
  13765. "Do an action on properties."
  13766. (interactive)
  13767. (let (c)
  13768. (org-at-property-p)
  13769. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13770. (setq c (read-char-exclusive))
  13771. (cond
  13772. ((equal c ?s)
  13773. (call-interactively 'org-set-property))
  13774. ((equal c ?d)
  13775. (call-interactively 'org-delete-property))
  13776. ((equal c ?D)
  13777. (call-interactively 'org-delete-property-globally))
  13778. ((equal c ?c)
  13779. (call-interactively 'org-compute-property-at-point))
  13780. (t (error "No such property action %c" c)))))
  13781. (defun org-at-property-p ()
  13782. "Is the cursor in a property line?"
  13783. ;; FIXME: Does not check if we are actually in the drawer.
  13784. ;; FIXME: also returns true on any drawers.....
  13785. ;; This is used by C-c C-c for property action.
  13786. (save-excursion
  13787. (beginning-of-line 1)
  13788. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  13789. (defmacro org-with-point-at (pom &rest body)
  13790. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  13791. (declare (indent 1) (debug t))
  13792. `(save-excursion
  13793. (if (markerp pom) (set-buffer (marker-buffer pom)))
  13794. (save-excursion
  13795. (goto-char (or pom (point)))
  13796. ,@body)))
  13797. (defun org-get-property-block (&optional beg end force)
  13798. "Return the (beg . end) range of the body of the property drawer.
  13799. BEG and END can be beginning and end of subtree, if not given
  13800. they will be found.
  13801. If the drawer does not exist and FORCE is non-nil, create the drawer."
  13802. (catch 'exit
  13803. (save-excursion
  13804. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  13805. (end (or end (progn (outline-next-heading) (point)))))
  13806. (goto-char beg)
  13807. (if (re-search-forward org-property-start-re end t)
  13808. (setq beg (1+ (match-end 0)))
  13809. (if force
  13810. (save-excursion
  13811. (org-insert-property-drawer)
  13812. (setq end (progn (outline-next-heading) (point))))
  13813. (throw 'exit nil))
  13814. (goto-char beg)
  13815. (if (re-search-forward org-property-start-re end t)
  13816. (setq beg (1+ (match-end 0)))))
  13817. (if (re-search-forward org-property-end-re end t)
  13818. (setq end (match-beginning 0))
  13819. (or force (throw 'exit nil))
  13820. (goto-char beg)
  13821. (setq end beg)
  13822. (org-indent-line-function)
  13823. (insert ":END:\n"))
  13824. (cons beg end)))))
  13825. (defun org-entry-properties (&optional pom which)
  13826. "Get all properties of the entry at point-or-marker POM.
  13827. This includes the TODO keyword, the tags, time strings for deadline,
  13828. scheduled, and clocking, and any additional properties defined in the
  13829. entry. The return value is an alist, keys may occur multiple times
  13830. if the property key was used several times.
  13831. POM may also be nil, in which case the current entry is used.
  13832. If WHICH is nil or `all', get all properties. If WHICH is
  13833. `special' or `standard', only get that subclass."
  13834. (setq which (or which 'all))
  13835. (org-with-point-at pom
  13836. (let ((clockstr (substring org-clock-string 0 -1))
  13837. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  13838. beg end range props sum-props key value)
  13839. (save-excursion
  13840. (when (condition-case nil (org-back-to-heading t) (error nil))
  13841. (setq beg (point))
  13842. (setq sum-props (get-text-property (point) 'org-summaries))
  13843. (outline-next-heading)
  13844. (setq end (point))
  13845. (when (memq which '(all special))
  13846. ;; Get the special properties, like TODO and tags
  13847. (goto-char beg)
  13848. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  13849. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13850. (when (looking-at org-priority-regexp)
  13851. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13852. (when (and (setq value (org-get-tags-string))
  13853. (string-match "\\S-" value))
  13854. (push (cons "TAGS" value) props))
  13855. (when (setq value (org-get-tags-at))
  13856. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  13857. props))
  13858. (while (re-search-forward org-keyword-time-regexp end t)
  13859. (setq key (substring (org-match-string-no-properties 1) 0 -1))
  13860. (unless (member key excluded) (push key excluded))
  13861. (push (cons key
  13862. (if (equal key clockstr)
  13863. (org-no-properties
  13864. (org-trim
  13865. (buffer-substring
  13866. (match-beginning 2) (point-at-eol))))
  13867. (org-match-string-no-properties 2)))
  13868. props)))
  13869. (when (memq which '(all standard))
  13870. ;; Get the standard properties, like :PORP: ...
  13871. (setq range (org-get-property-block beg end))
  13872. (when range
  13873. (goto-char (car range))
  13874. (while (re-search-forward
  13875. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13876. (cdr range) t)
  13877. (setq key (org-match-string-no-properties 1)
  13878. value (org-trim (or (org-match-string-no-properties 2) "")))
  13879. (unless (member key excluded)
  13880. (push (cons key (or value "")) props)))))
  13881. (append sum-props (nreverse props)))))))
  13882. (defun org-entry-get (pom property &optional inherit)
  13883. "Get value of PROPERTY for entry at point-or-marker POM.
  13884. If INHERIT is non-nil and the entry does not have the property,
  13885. then also check higher levels of the hierarchy.
  13886. If the property is present but empty, the return value is the empty string.
  13887. If the property is not present at all, nil is returned."
  13888. (org-with-point-at pom
  13889. (if inherit
  13890. (org-entry-get-with-inheritance property)
  13891. (if (member property org-special-properties)
  13892. ;; We need a special property. Use brute force, get all properties.
  13893. (cdr (assoc property (org-entry-properties nil 'special)))
  13894. (let ((range (org-get-property-block)))
  13895. (if (and range
  13896. (goto-char (car range))
  13897. (re-search-forward
  13898. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  13899. (cdr range) t))
  13900. ;; Found the property, return it.
  13901. (if (match-end 1)
  13902. (org-match-string-no-properties 1)
  13903. "")))))))
  13904. (defun org-entry-delete (pom property)
  13905. "Delete the property PROPERTY from entry at point-or-marker POM."
  13906. (org-with-point-at pom
  13907. (if (member property org-special-properties)
  13908. nil ; cannot delete these properties.
  13909. (let ((range (org-get-property-block)))
  13910. (if (and range
  13911. (goto-char (car range))
  13912. (re-search-forward
  13913. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  13914. (cdr range) t))
  13915. (progn
  13916. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13917. t)
  13918. nil)))))
  13919. ;; Multi-values properties are properties that contain multiple values
  13920. ;; These values are assumed to be single words, separated by whitespace.
  13921. (defun org-entry-add-to-multivalued-property (pom property value)
  13922. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13923. (let* ((old (org-entry-get pom property))
  13924. (values (and old (org-split-string old "[ \t]"))))
  13925. (unless (member value values)
  13926. (setq values (cons value values))
  13927. (org-entry-put pom property
  13928. (mapconcat 'identity values " ")))))
  13929. (defun org-entry-remove-from-multivalued-property (pom property value)
  13930. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13931. (let* ((old (org-entry-get pom property))
  13932. (values (and old (org-split-string old "[ \t]"))))
  13933. (when (member value values)
  13934. (setq values (delete value values))
  13935. (org-entry-put pom property
  13936. (mapconcat 'identity values " ")))))
  13937. (defun org-entry-member-in-multivalued-property (pom property value)
  13938. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13939. (let* ((old (org-entry-get pom property))
  13940. (values (and old (org-split-string old "[ \t]"))))
  13941. (member value values)))
  13942. (defvar org-entry-property-inherited-from (make-marker))
  13943. (defun org-entry-get-with-inheritance (property)
  13944. "Get entry property, and search higher levels if not present."
  13945. (let (tmp)
  13946. (save-excursion
  13947. (save-restriction
  13948. (widen)
  13949. (catch 'ex
  13950. (while t
  13951. (when (setq tmp (org-entry-get nil property))
  13952. (org-back-to-heading t)
  13953. (move-marker org-entry-property-inherited-from (point))
  13954. (throw 'ex tmp))
  13955. (or (org-up-heading-safe) (throw 'ex nil)))))
  13956. (or tmp (cdr (assoc property org-local-properties))
  13957. (cdr (assoc property org-global-properties))))))
  13958. (defun org-entry-put (pom property value)
  13959. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13960. (org-with-point-at pom
  13961. (org-back-to-heading t)
  13962. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13963. range)
  13964. (cond
  13965. ((equal property "TODO")
  13966. (when (and (stringp value) (string-match "\\S-" value)
  13967. (not (member value org-todo-keywords-1)))
  13968. (error "\"%s\" is not a valid TODO state" value))
  13969. (if (or (not value)
  13970. (not (string-match "\\S-" value)))
  13971. (setq value 'none))
  13972. (org-todo value)
  13973. (org-set-tags nil 'align))
  13974. ((equal property "PRIORITY")
  13975. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13976. (string-to-char value) ?\ ))
  13977. (org-set-tags nil 'align))
  13978. ((equal property "SCHEDULED")
  13979. (if (re-search-forward org-scheduled-time-regexp end t)
  13980. (cond
  13981. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13982. ((eq value 'later) (org-timestamp-change 1 'day))
  13983. (t (call-interactively 'org-schedule)))
  13984. (call-interactively 'org-schedule)))
  13985. ((equal property "DEADLINE")
  13986. (if (re-search-forward org-deadline-time-regexp end t)
  13987. (cond
  13988. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13989. ((eq value 'later) (org-timestamp-change 1 'day))
  13990. (t (call-interactively 'org-deadline)))
  13991. (call-interactively 'org-deadline)))
  13992. ((member property org-special-properties)
  13993. (error "The %s property can not yet be set with `org-entry-put'"
  13994. property))
  13995. (t ; a non-special property
  13996. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13997. (setq range (org-get-property-block beg end 'force))
  13998. (goto-char (car range))
  13999. (if (re-search-forward
  14000. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14001. (progn
  14002. (delete-region (match-beginning 1) (match-end 1))
  14003. (goto-char (match-beginning 1)))
  14004. (goto-char (cdr range))
  14005. (insert "\n")
  14006. (backward-char 1)
  14007. (org-indent-line-function)
  14008. (insert ":" property ":"))
  14009. (and value (insert " " value))
  14010. (org-indent-line-function)))))))
  14011. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  14012. "Get all property keys in the current buffer.
  14013. With INCLUDE-SPECIALS, also list the special properties that relect things
  14014. like tags and TODO state.
  14015. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14016. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  14017. (let (rtn range)
  14018. (save-excursion
  14019. (save-restriction
  14020. (widen)
  14021. (goto-char (point-min))
  14022. (while (re-search-forward org-property-start-re nil t)
  14023. (setq range (org-get-property-block))
  14024. (goto-char (car range))
  14025. (while (re-search-forward
  14026. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  14027. (cdr range) t)
  14028. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14029. (outline-next-heading))))
  14030. (when include-specials
  14031. (setq rtn (append org-special-properties rtn)))
  14032. (when include-defaults
  14033. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14034. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14035. (defun org-property-values (key)
  14036. "Return a list of all values of property KEY."
  14037. (save-excursion
  14038. (save-restriction
  14039. (widen)
  14040. (goto-char (point-min))
  14041. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14042. values)
  14043. (while (re-search-forward re nil t)
  14044. (add-to-list 'values (org-trim (match-string 1))))
  14045. (delete "" values)))))
  14046. (defun org-insert-property-drawer ()
  14047. "Insert a property drawer into the current entry."
  14048. (interactive)
  14049. (org-back-to-heading t)
  14050. (looking-at outline-regexp)
  14051. (let ((indent (- (match-end 0)(match-beginning 0)))
  14052. (beg (point))
  14053. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14054. end hiddenp)
  14055. (outline-next-heading)
  14056. (setq end (point))
  14057. (goto-char beg)
  14058. (while (re-search-forward re end t))
  14059. (setq hiddenp (org-invisible-p))
  14060. (end-of-line 1)
  14061. (and (equal (char-after) ?\n) (forward-char 1))
  14062. (org-skip-over-state-notes)
  14063. (skip-chars-backward " \t\n\r")
  14064. (if (eq (char-before) ?*) (forward-char 1))
  14065. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14066. (beginning-of-line 0)
  14067. (indent-to-column indent)
  14068. (beginning-of-line 2)
  14069. (indent-to-column indent)
  14070. (beginning-of-line 0)
  14071. (if hiddenp
  14072. (save-excursion
  14073. (org-back-to-heading t)
  14074. (hide-entry))
  14075. (org-flag-drawer t))))
  14076. (defun org-set-property (property value)
  14077. "In the current entry, set PROPERTY to VALUE.
  14078. When called interactively, this will prompt for a property name, offering
  14079. completion on existing and default properties. And then it will prompt
  14080. for a value, offering competion either on allowed values (via an inherited
  14081. xxx_ALL property) or on existing values in other instances of this property
  14082. in the current file."
  14083. (interactive
  14084. (let* ((prop (completing-read
  14085. "Property: " (mapcar 'list (org-buffer-property-keys nil t))))
  14086. (cur (org-entry-get nil prop))
  14087. (allowed (org-property-get-allowed-values nil prop 'table))
  14088. (existing (mapcar 'list (org-property-values prop)))
  14089. (val (if allowed
  14090. (completing-read "Value: " allowed nil 'req-match)
  14091. (completing-read
  14092. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14093. (concat "[" cur "]") "")
  14094. ": ")
  14095. existing nil nil "" nil cur))))
  14096. (list prop (if (equal val "") cur val))))
  14097. (unless (equal (org-entry-get nil property) value)
  14098. (org-entry-put nil property value)))
  14099. (defun org-delete-property (property)
  14100. "In the current entry, delete PROPERTY."
  14101. (interactive
  14102. (let* ((prop (completing-read
  14103. "Property: " (org-entry-properties nil 'standard))))
  14104. (list prop)))
  14105. (message (concat "Property " property
  14106. (if (org-entry-delete nil property)
  14107. " deleted"
  14108. " was not present in the entry"))))
  14109. (defun org-delete-property-globally (property)
  14110. "Remove PROPERTY globally, from all entries."
  14111. (interactive
  14112. (let* ((prop (completing-read
  14113. "Globally remove property: "
  14114. (mapcar 'list (org-buffer-property-keys)))))
  14115. (list prop)))
  14116. (save-excursion
  14117. (save-restriction
  14118. (widen)
  14119. (goto-char (point-min))
  14120. (let ((cnt 0))
  14121. (while (re-search-forward
  14122. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14123. nil t)
  14124. (setq cnt (1+ cnt))
  14125. (replace-match ""))
  14126. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14127. (defvar org-columns-current-fmt-compiled) ; defined below
  14128. (defun org-compute-property-at-point ()
  14129. "Compute the property at point.
  14130. This looks for an enclosing column format, extracts the operator and
  14131. then applies it to the proerty in the column format's scope."
  14132. (interactive)
  14133. (unless (org-at-property-p)
  14134. (error "Not at a property"))
  14135. (let ((prop (org-match-string-no-properties 2)))
  14136. (org-columns-get-format-and-top-level)
  14137. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14138. (error "No operator defined for property %s" prop))
  14139. (org-columns-compute prop)))
  14140. (defun org-property-get-allowed-values (pom property &optional table)
  14141. "Get allowed values for the property PROPERTY.
  14142. When TABLE is non-nil, return an alist that can directly be used for
  14143. completion."
  14144. (let (vals)
  14145. (cond
  14146. ((equal property "TODO")
  14147. (setq vals (org-with-point-at pom
  14148. (append org-todo-keywords-1 '("")))))
  14149. ((equal property "PRIORITY")
  14150. (let ((n org-lowest-priority))
  14151. (while (>= n org-highest-priority)
  14152. (push (char-to-string n) vals)
  14153. (setq n (1- n)))))
  14154. ((member property org-special-properties))
  14155. (t
  14156. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14157. (when (and vals (string-match "\\S-" vals))
  14158. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14159. (setq vals (mapcar (lambda (x)
  14160. (cond ((stringp x) x)
  14161. ((numberp x) (number-to-string x))
  14162. ((symbolp x) (symbol-name x))
  14163. (t "???")))
  14164. vals)))))
  14165. (if table (mapcar 'list vals) vals)))
  14166. (defun org-property-previous-allowed-value (&optional previous)
  14167. "Switch to the next allowed value for this property."
  14168. (interactive)
  14169. (org-property-next-allowed-value t))
  14170. (defun org-property-next-allowed-value (&optional previous)
  14171. "Switch to the next allowed value for this property."
  14172. (interactive)
  14173. (unless (org-at-property-p)
  14174. (error "Not at a property"))
  14175. (let* ((key (match-string 2))
  14176. (value (match-string 3))
  14177. (allowed (or (org-property-get-allowed-values (point) key)
  14178. (and (member value '("[ ]" "[-]" "[X]"))
  14179. '("[ ]" "[X]"))))
  14180. nval)
  14181. (unless allowed
  14182. (error "Allowed values for this property have not been defined"))
  14183. (if previous (setq allowed (reverse allowed)))
  14184. (if (member value allowed)
  14185. (setq nval (car (cdr (member value allowed)))))
  14186. (setq nval (or nval (car allowed)))
  14187. (if (equal nval value)
  14188. (error "Only one allowed value for this property"))
  14189. (org-at-property-p)
  14190. (replace-match (concat " :" key ": " nval) t t)
  14191. (org-indent-line-function)
  14192. (beginning-of-line 1)
  14193. (skip-chars-forward " \t")))
  14194. (defun org-find-entry-with-id (ident)
  14195. "Locate the entry that contains the ID property with exact value IDENT.
  14196. IDENT can be a string, a symbol or a number, this function will search for
  14197. the string representation of it.
  14198. Return the position where this entry starts, or nil if there is no such entry."
  14199. (let ((id (cond
  14200. ((stringp ident) ident)
  14201. ((symbol-name ident) (symbol-name ident))
  14202. ((numberp ident) (number-to-string ident))
  14203. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14204. (case-fold-search nil))
  14205. (save-excursion
  14206. (save-restriction
  14207. (goto-char (point-min))
  14208. (when (re-search-forward
  14209. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14210. nil t)
  14211. (org-back-to-heading)
  14212. (point))))))
  14213. ;;; Column View
  14214. (defvar org-columns-overlays nil
  14215. "Holds the list of current column overlays.")
  14216. (defvar org-columns-current-fmt nil
  14217. "Local variable, holds the currently active column format.")
  14218. (defvar org-columns-current-fmt-compiled nil
  14219. "Local variable, holds the currently active column format.
  14220. This is the compiled version of the format.")
  14221. (defvar org-columns-current-widths nil
  14222. "Loval variable, holds the currently widths of fields.")
  14223. (defvar org-columns-current-maxwidths nil
  14224. "Loval variable, holds the currently active maximum column widths.")
  14225. (defvar org-columns-begin-marker (make-marker)
  14226. "Points to the position where last a column creation command was called.")
  14227. (defvar org-columns-top-level-marker (make-marker)
  14228. "Points to the position where current columns region starts.")
  14229. (defvar org-columns-map (make-sparse-keymap)
  14230. "The keymap valid in column display.")
  14231. (defun org-columns-content ()
  14232. "Switch to contents view while in columns view."
  14233. (interactive)
  14234. (org-overview)
  14235. (org-content))
  14236. (org-defkey org-columns-map "c" 'org-columns-content)
  14237. (org-defkey org-columns-map "o" 'org-overview)
  14238. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  14239. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  14240. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  14241. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  14242. (org-defkey org-columns-map "v" 'org-columns-show-value)
  14243. (org-defkey org-columns-map "q" 'org-columns-quit)
  14244. (org-defkey org-columns-map "r" 'org-columns-redo)
  14245. (org-defkey org-columns-map [left] 'backward-char)
  14246. (org-defkey org-columns-map "\M-b" 'backward-char)
  14247. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  14248. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  14249. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  14250. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  14251. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  14252. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  14253. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  14254. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  14255. (org-defkey org-columns-map "<" 'org-columns-narrow)
  14256. (org-defkey org-columns-map ">" 'org-columns-widen)
  14257. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  14258. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  14259. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  14260. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  14261. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  14262. '("Column"
  14263. ["Edit property" org-columns-edit-value t]
  14264. ["Next allowed value" org-columns-next-allowed-value t]
  14265. ["Previous allowed value" org-columns-previous-allowed-value t]
  14266. ["Show full value" org-columns-show-value t]
  14267. ["Edit allowed values" org-columns-edit-allowed t]
  14268. "--"
  14269. ["Edit column attributes" org-columns-edit-attributes t]
  14270. ["Increase column width" org-columns-widen t]
  14271. ["Decrease column width" org-columns-narrow t]
  14272. "--"
  14273. ["Move column right" org-columns-move-right t]
  14274. ["Move column left" org-columns-move-left t]
  14275. ["Add column" org-columns-new t]
  14276. ["Delete column" org-columns-delete t]
  14277. "--"
  14278. ["CONTENTS" org-columns-content t]
  14279. ["OVERVIEW" org-overview t]
  14280. ["Refresh columns display" org-columns-redo t]
  14281. "--"
  14282. ["Open link" org-columns-open-link t]
  14283. "--"
  14284. ["Quit" org-columns-quit t]))
  14285. (defun org-columns-new-overlay (beg end &optional string face)
  14286. "Create a new column overlay and add it to the list."
  14287. (let ((ov (org-make-overlay beg end)))
  14288. (org-overlay-put ov 'face (or face 'secondary-selection))
  14289. (org-overlay-display ov string face)
  14290. (push ov org-columns-overlays)
  14291. ov))
  14292. (defun org-columns-display-here (&optional props)
  14293. "Overlay the current line with column display."
  14294. (interactive)
  14295. (let* ((fmt org-columns-current-fmt-compiled)
  14296. (beg (point-at-bol))
  14297. (level-face (save-excursion
  14298. (beginning-of-line 1)
  14299. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  14300. (org-get-level-face 2))))
  14301. (color (list :foreground
  14302. (face-attribute (or level-face 'default) :foreground)))
  14303. props pom property ass width f string ov column val modval)
  14304. ;; Check if the entry is in another buffer.
  14305. (unless props
  14306. (if (eq major-mode 'org-agenda-mode)
  14307. (setq pom (or (get-text-property (point) 'org-hd-marker)
  14308. (get-text-property (point) 'org-marker))
  14309. props (if pom (org-entry-properties pom) nil))
  14310. (setq props (org-entry-properties nil))))
  14311. ;; Walk the format
  14312. (while (setq column (pop fmt))
  14313. (setq property (car column)
  14314. ass (if (equal property "ITEM")
  14315. (cons "ITEM"
  14316. (save-match-data
  14317. (org-no-properties
  14318. (org-remove-tabs
  14319. (buffer-substring-no-properties
  14320. (point-at-bol) (point-at-eol))))))
  14321. (assoc property props))
  14322. width (or (cdr (assoc property org-columns-current-maxwidths))
  14323. (nth 2 column)
  14324. (length property))
  14325. f (format "%%-%d.%ds | " width width)
  14326. val (or (cdr ass) "")
  14327. modval (if (equal property "ITEM")
  14328. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  14329. string (format f (or modval val)))
  14330. ;; Create the overlay
  14331. (org-unmodified
  14332. (setq ov (org-columns-new-overlay
  14333. beg (setq beg (1+ beg)) string
  14334. (list color 'org-column)))
  14335. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  14336. (org-overlay-put ov 'keymap org-columns-map)
  14337. (org-overlay-put ov 'org-columns-key property)
  14338. (org-overlay-put ov 'org-columns-value (cdr ass))
  14339. (org-overlay-put ov 'org-columns-value-modified modval)
  14340. (org-overlay-put ov 'org-columns-pom pom)
  14341. (org-overlay-put ov 'org-columns-format f))
  14342. (if (or (not (char-after beg))
  14343. (equal (char-after beg) ?\n))
  14344. (let ((inhibit-read-only t))
  14345. (save-excursion
  14346. (goto-char beg)
  14347. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  14348. ;; Make the rest of the line disappear.
  14349. (org-unmodified
  14350. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  14351. (org-overlay-put ov 'invisible t)
  14352. (org-overlay-put ov 'keymap org-columns-map)
  14353. (org-overlay-put ov 'intangible t)
  14354. (push ov org-columns-overlays)
  14355. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  14356. (org-overlay-put ov 'keymap org-columns-map)
  14357. (push ov org-columns-overlays)
  14358. (let ((inhibit-read-only t))
  14359. (put-text-property (max (point-min) (1- (point-at-bol)))
  14360. (min (point-max) (1+ (point-at-eol)))
  14361. 'read-only "Type `e' to edit property")))))
  14362. (defvar org-previous-header-line-format nil
  14363. "The header line format before column view was turned on.")
  14364. (defvar org-columns-inhibit-recalculation nil
  14365. "Inhibit recomputing of columns on column view startup.")
  14366. (defvar header-line-format)
  14367. (defun org-columns-display-here-title ()
  14368. "Overlay the newline before the current line with the table title."
  14369. (interactive)
  14370. (let ((fmt org-columns-current-fmt-compiled)
  14371. string (title "")
  14372. property width f column str widths)
  14373. (while (setq column (pop fmt))
  14374. (setq property (car column)
  14375. str (or (nth 1 column) property)
  14376. width (or (cdr (assoc property org-columns-current-maxwidths))
  14377. (nth 2 column)
  14378. (length str))
  14379. widths (push width widths)
  14380. f (format "%%-%d.%ds | " width width)
  14381. string (format f str)
  14382. title (concat title string)))
  14383. (setq title (concat
  14384. (org-add-props " " nil 'display '(space :align-to 0))
  14385. (org-add-props title nil 'face '(:weight bold :underline t))))
  14386. (org-set-local 'org-previous-header-line-format header-line-format)
  14387. (org-set-local 'org-columns-current-widths (nreverse widths))
  14388. (setq header-line-format title)))
  14389. (defun org-columns-remove-overlays ()
  14390. "Remove all currently active column overlays."
  14391. (interactive)
  14392. (when (marker-buffer org-columns-begin-marker)
  14393. (with-current-buffer (marker-buffer org-columns-begin-marker)
  14394. (when (local-variable-p 'org-previous-header-line-format)
  14395. (setq header-line-format org-previous-header-line-format)
  14396. (kill-local-variable 'org-previous-header-line-format))
  14397. (move-marker org-columns-begin-marker nil)
  14398. (move-marker org-columns-top-level-marker nil)
  14399. (org-unmodified
  14400. (mapc 'org-delete-overlay org-columns-overlays)
  14401. (setq org-columns-overlays nil)
  14402. (let ((inhibit-read-only t))
  14403. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  14404. (defun org-columns-cleanup-item (item fmt)
  14405. "Remove from ITEM what is a column in the format FMT."
  14406. (if (not org-complex-heading-regexp)
  14407. item
  14408. (when (string-match org-complex-heading-regexp item)
  14409. (concat
  14410. (org-add-props (concat (match-string 1 item) " ") nil
  14411. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  14412. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  14413. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  14414. " " (match-string 4 item)
  14415. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  14416. (defun org-columns-show-value ()
  14417. "Show the full value of the property."
  14418. (interactive)
  14419. (let ((value (get-char-property (point) 'org-columns-value)))
  14420. (message "Value is: %s" (or value ""))))
  14421. (defun org-columns-quit ()
  14422. "Remove the column overlays and in this way exit column editing."
  14423. (interactive)
  14424. (org-unmodified
  14425. (org-columns-remove-overlays)
  14426. (let ((inhibit-read-only t))
  14427. (remove-text-properties (point-min) (point-max) '(read-only t))))
  14428. (when (eq major-mode 'org-agenda-mode)
  14429. (message
  14430. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  14431. (defun org-columns-check-computed ()
  14432. "Check if this column value is computed.
  14433. If yes, throw an error indicating that changing it does not make sense."
  14434. (let ((val (get-char-property (point) 'org-columns-value)))
  14435. (when (and (stringp val)
  14436. (get-char-property 0 'org-computed val))
  14437. (error "This value is computed from the entry's children"))))
  14438. (defun org-columns-todo (&optional arg)
  14439. "Change the TODO state during column view."
  14440. (interactive "P")
  14441. (org-columns-edit-value "TODO"))
  14442. (defun org-columns-set-tags-or-toggle (&optional arg)
  14443. "Toggle checkbox at point, or set tags for current headline."
  14444. (interactive "P")
  14445. (if (string-match "\\`\\[[ xX-]\\]\\'"
  14446. (get-char-property (point) 'org-columns-value))
  14447. (org-columns-next-allowed-value)
  14448. (org-columns-edit-value "TAGS")))
  14449. (defun org-columns-edit-value (&optional key)
  14450. "Edit the value of the property at point in column view.
  14451. Where possible, use the standard interface for changing this line."
  14452. (interactive)
  14453. (org-columns-check-computed)
  14454. (let* ((external-key key)
  14455. (col (current-column))
  14456. (key (or key (get-char-property (point) 'org-columns-key)))
  14457. (value (get-char-property (point) 'org-columns-value))
  14458. (bol (point-at-bol)) (eol (point-at-eol))
  14459. (pom (or (get-text-property bol 'org-hd-marker)
  14460. (point))) ; keep despite of compiler waring
  14461. (line-overlays
  14462. (delq nil (mapcar (lambda (x)
  14463. (and (eq (overlay-buffer x) (current-buffer))
  14464. (>= (overlay-start x) bol)
  14465. (<= (overlay-start x) eol)
  14466. x))
  14467. org-columns-overlays)))
  14468. nval eval allowed)
  14469. (cond
  14470. ((equal key "ITEM")
  14471. (setq eval '(org-with-point-at pom
  14472. (org-edit-headline))))
  14473. ((equal key "TODO")
  14474. (setq eval '(org-with-point-at pom
  14475. (let ((current-prefix-arg
  14476. (if external-key current-prefix-arg '(4))))
  14477. (call-interactively 'org-todo)))))
  14478. ((equal key "PRIORITY")
  14479. (setq eval '(org-with-point-at pom
  14480. (call-interactively 'org-priority))))
  14481. ((equal key "TAGS")
  14482. (setq eval '(org-with-point-at pom
  14483. (let ((org-fast-tag-selection-single-key
  14484. (if (eq org-fast-tag-selection-single-key 'expert)
  14485. t org-fast-tag-selection-single-key)))
  14486. (call-interactively 'org-set-tags)))))
  14487. ((equal key "DEADLINE")
  14488. (setq eval '(org-with-point-at pom
  14489. (call-interactively 'org-deadline))))
  14490. ((equal key "SCHEDULED")
  14491. (setq eval '(org-with-point-at pom
  14492. (call-interactively 'org-schedule))))
  14493. (t
  14494. (setq allowed (org-property-get-allowed-values pom key 'table))
  14495. (if allowed
  14496. (setq nval (completing-read "Value: " allowed nil t))
  14497. (setq nval (read-string "Edit: " value)))
  14498. (setq nval (org-trim nval))
  14499. (when (not (equal nval value))
  14500. (setq eval '(org-entry-put pom key nval)))))
  14501. (when eval
  14502. (let ((inhibit-read-only t))
  14503. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  14504. (unwind-protect
  14505. (progn
  14506. (setq org-columns-overlays
  14507. (org-delete-all line-overlays org-columns-overlays))
  14508. (mapc 'org-delete-overlay line-overlays)
  14509. (org-columns-eval eval))
  14510. (org-columns-display-here))))
  14511. (move-to-column col)
  14512. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  14513. (org-columns-update key))))
  14514. (defun org-edit-headline () ; FIXME: this is not columns specific
  14515. "Edit the current headline, the part without TODO keyword, TAGS."
  14516. (org-back-to-heading)
  14517. (when (looking-at org-todo-line-regexp)
  14518. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  14519. (txt (match-string 3))
  14520. (post "")
  14521. txt2)
  14522. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  14523. (setq post (match-string 0 txt)
  14524. txt (substring txt 0 (match-beginning 0))))
  14525. (setq txt2 (read-string "Edit: " txt))
  14526. (when (not (equal txt txt2))
  14527. (beginning-of-line 1)
  14528. (insert pre txt2 post)
  14529. (delete-region (point) (point-at-eol))
  14530. (org-set-tags nil t)))))
  14531. (defun org-columns-edit-allowed ()
  14532. "Edit the list of allowed values for the current property."
  14533. (interactive)
  14534. (let* ((key (get-char-property (point) 'org-columns-key))
  14535. (key1 (concat key "_ALL"))
  14536. (allowed (org-entry-get (point) key1 t))
  14537. nval)
  14538. ;; FIXME: Cover editing TODO, TAGS etc inbiffer settings.????
  14539. (setq nval (read-string "Allowed: " allowed))
  14540. (org-entry-put
  14541. (cond ((marker-position org-entry-property-inherited-from)
  14542. org-entry-property-inherited-from)
  14543. ((marker-position org-columns-top-level-marker)
  14544. org-columns-top-level-marker))
  14545. key1 nval)))
  14546. (defmacro org-no-warnings (&rest body)
  14547. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  14548. (defun org-columns-eval (form)
  14549. (let (hidep)
  14550. (save-excursion
  14551. (beginning-of-line 1)
  14552. ;; `next-line' is needed here, because it skips invisible line.
  14553. ;; FIXME: RMS says this should be wrapped into `with-no-warnings'
  14554. ;; but I don't know how to do this and keep the code XEmacs compatible.
  14555. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  14556. (setq hidep (org-on-heading-p 1)))
  14557. (eval form)
  14558. (and hidep (hide-entry))))
  14559. (defun org-columns-previous-allowed-value ()
  14560. "Switch to the previous allowed value for this column."
  14561. (interactive)
  14562. (org-columns-next-allowed-value t))
  14563. (defun org-columns-next-allowed-value (&optional previous)
  14564. "Switch to the next allowed value for this column."
  14565. (interactive)
  14566. (org-columns-check-computed)
  14567. (let* ((col (current-column))
  14568. (key (get-char-property (point) 'org-columns-key))
  14569. (value (get-char-property (point) 'org-columns-value))
  14570. (bol (point-at-bol)) (eol (point-at-eol))
  14571. (pom (or (get-text-property bol 'org-hd-marker)
  14572. (point))) ; keep despite of compiler waring
  14573. (line-overlays
  14574. (delq nil (mapcar (lambda (x)
  14575. (and (eq (overlay-buffer x) (current-buffer))
  14576. (>= (overlay-start x) bol)
  14577. (<= (overlay-start x) eol)
  14578. x))
  14579. org-columns-overlays)))
  14580. (allowed (or (org-property-get-allowed-values pom key)
  14581. (and (equal
  14582. (nth 4 (assoc key org-columns-current-fmt-compiled))
  14583. 'checkbox) '("[ ]" "[X]"))))
  14584. nval)
  14585. (when (equal key "ITEM")
  14586. (error "Cannot edit item headline from here"))
  14587. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  14588. (error "Allowed values for this property have not been defined"))
  14589. (if (member key '("SCHEDULED" "DEADLINE"))
  14590. (setq nval (if previous 'earlier 'later))
  14591. (if previous (setq allowed (reverse allowed)))
  14592. (if (member value allowed)
  14593. (setq nval (car (cdr (member value allowed)))))
  14594. (setq nval (or nval (car allowed)))
  14595. (if (equal nval value)
  14596. (error "Only one allowed value for this property")))
  14597. (let ((inhibit-read-only t))
  14598. (remove-text-properties (1- bol) eol '(read-only t))
  14599. (unwind-protect
  14600. (progn
  14601. (setq org-columns-overlays
  14602. (org-delete-all line-overlays org-columns-overlays))
  14603. (mapc 'org-delete-overlay line-overlays)
  14604. (org-columns-eval '(org-entry-put pom key nval)))
  14605. (org-columns-display-here)))
  14606. (move-to-column col)
  14607. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  14608. (org-columns-update key))))
  14609. (defun org-verify-version (task)
  14610. (cond
  14611. ((eq task 'columns)
  14612. (if (or (featurep 'xemacs)
  14613. (< emacs-major-version 22))
  14614. (error "Emacs 22 is required for the columns feature")))))
  14615. (defun org-columns-open-link (&optional arg)
  14616. (interactive "P")
  14617. (let ((key (get-char-property (point) 'org-columns-key))
  14618. (value (get-char-property (point) 'org-columns-value)))
  14619. (org-open-link-from-string arg)))
  14620. (defun org-open-link-from-string (s &optional arg)
  14621. "Open a link in the string S, as if it was in Org-mode."
  14622. (interactive)
  14623. (with-temp-buffer
  14624. (let ((org-inhibit-startup t))
  14625. (org-mode)
  14626. (insert s)
  14627. (goto-char (point-min))
  14628. (org-open-at-point arg))))
  14629. (defun org-columns-get-format-and-top-level ()
  14630. (let (fmt)
  14631. (when (condition-case nil (org-back-to-heading) (error nil))
  14632. (move-marker org-entry-property-inherited-from nil)
  14633. (setq fmt (org-entry-get nil "COLUMNS" t)))
  14634. (setq fmt (or fmt org-columns-default-format))
  14635. (org-set-local 'org-columns-current-fmt fmt)
  14636. (org-columns-compile-format fmt)
  14637. (if (marker-position org-entry-property-inherited-from)
  14638. (move-marker org-columns-top-level-marker
  14639. org-entry-property-inherited-from)
  14640. (move-marker org-columns-top-level-marker (point)))
  14641. fmt))
  14642. (defun org-columns ()
  14643. "Turn on column view on an org-mode file."
  14644. (interactive)
  14645. (org-verify-version 'columns)
  14646. (org-columns-remove-overlays)
  14647. (move-marker org-columns-begin-marker (point))
  14648. (let (beg end fmt cache maxwidths)
  14649. (setq fmt (org-columns-get-format-and-top-level))
  14650. (save-excursion
  14651. (goto-char org-columns-top-level-marker)
  14652. (setq beg (point))
  14653. (unless org-columns-inhibit-recalculation
  14654. (org-columns-compute-all))
  14655. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  14656. (point-max)))
  14657. (goto-char beg)
  14658. ;; Get and cache the properties
  14659. (while (re-search-forward (concat "^" outline-regexp) end t)
  14660. (push (cons (org-current-line) (org-entry-properties)) cache))
  14661. (when cache
  14662. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  14663. (org-set-local 'org-columns-current-maxwidths maxwidths)
  14664. (org-columns-display-here-title)
  14665. (mapc (lambda (x)
  14666. (goto-line (car x))
  14667. (org-columns-display-here (cdr x)))
  14668. cache)))))
  14669. (defun org-columns-new (&optional prop title width op fmt)
  14670. "Insert a new column, to the leeft o the current column."
  14671. (interactive)
  14672. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  14673. cell)
  14674. (setq prop (completing-read
  14675. "Property: " (mapcar 'list (org-buffer-property-keys t))
  14676. nil nil prop))
  14677. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  14678. (setq width (read-string "Column width: " (if width (number-to-string width))))
  14679. (if (string-match "\\S-" width)
  14680. (setq width (string-to-number width))
  14681. (setq width nil))
  14682. (setq fmt (completing-read "Summary [none]: "
  14683. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox"))
  14684. nil t))
  14685. (if (string-match "\\S-" fmt)
  14686. (setq fmt (intern fmt))
  14687. (setq fmt nil))
  14688. (if (eq fmt 'none) (setq fmt nil))
  14689. (if editp
  14690. (progn
  14691. (setcar editp prop)
  14692. (setcdr editp (list title width nil fmt)))
  14693. (setq cell (nthcdr (1- (current-column))
  14694. org-columns-current-fmt-compiled))
  14695. (setcdr cell (cons (list prop title width nil fmt)
  14696. (cdr cell))))
  14697. (org-columns-store-format)
  14698. (org-columns-redo)))
  14699. (defun org-columns-delete ()
  14700. "Delete the column at point from columns view."
  14701. (interactive)
  14702. (let* ((n (current-column))
  14703. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  14704. (when (y-or-n-p
  14705. (format "Are you sure you want to remove column \"%s\"? " title))
  14706. (setq org-columns-current-fmt-compiled
  14707. (delq (nth n org-columns-current-fmt-compiled)
  14708. org-columns-current-fmt-compiled))
  14709. (org-columns-store-format)
  14710. (org-columns-redo)
  14711. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  14712. (backward-char 1)))))
  14713. (defun org-columns-edit-attributes ()
  14714. "Edit the attributes of the current column."
  14715. (interactive)
  14716. (let* ((n (current-column))
  14717. (info (nth n org-columns-current-fmt-compiled)))
  14718. (apply 'org-columns-new info)))
  14719. (defun org-columns-widen (arg)
  14720. "Make the column wider by ARG characters."
  14721. (interactive "p")
  14722. (let* ((n (current-column))
  14723. (entry (nth n org-columns-current-fmt-compiled))
  14724. (width (or (nth 2 entry)
  14725. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  14726. (setq width (max 1 (+ width arg)))
  14727. (setcar (nthcdr 2 entry) width)
  14728. (org-columns-store-format)
  14729. (org-columns-redo)))
  14730. (defun org-columns-narrow (arg)
  14731. "Make the column nrrower by ARG characters."
  14732. (interactive "p")
  14733. (org-columns-widen (- arg)))
  14734. (defun org-columns-move-right ()
  14735. "Swap this column with the one to the right."
  14736. (interactive)
  14737. (let* ((n (current-column))
  14738. (cell (nthcdr n org-columns-current-fmt-compiled))
  14739. e)
  14740. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  14741. (error "Cannot shift this column further to the right"))
  14742. (setq e (car cell))
  14743. (setcar cell (car (cdr cell)))
  14744. (setcdr cell (cons e (cdr (cdr cell))))
  14745. (org-columns-store-format)
  14746. (org-columns-redo)
  14747. (forward-char 1)))
  14748. (defun org-columns-move-left ()
  14749. "Swap this column with the one to the left."
  14750. (interactive)
  14751. (let* ((n (current-column)))
  14752. (when (= n 0)
  14753. (error "Cannot shift this column further to the left"))
  14754. (backward-char 1)
  14755. (org-columns-move-right)
  14756. (backward-char 1)))
  14757. (defun org-columns-store-format ()
  14758. "Store the text version of the current columns format in appropriate place.
  14759. This is either in the COLUMNS property of the node starting the current column
  14760. display, or in the #+COLUMNS line of the current buffer."
  14761. (let (fmt (cnt 0))
  14762. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  14763. (org-set-local 'org-columns-current-fmt fmt)
  14764. (if (marker-position org-columns-top-level-marker)
  14765. (save-excursion
  14766. (goto-char org-columns-top-level-marker)
  14767. (if (and (org-at-heading-p)
  14768. (org-entry-get nil "COLUMNS"))
  14769. (org-entry-put nil "COLUMNS" fmt)
  14770. (goto-char (point-min))
  14771. ;; Overwrite all #+COLUMNS lines....
  14772. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  14773. (setq cnt (1+ cnt))
  14774. (replace-match (concat "#+COLUMNS: " fmt) t t))
  14775. (unless (> cnt 0)
  14776. (goto-char (point-min))
  14777. (or (org-on-heading-p t) (outline-next-heading))
  14778. (let ((inhibit-read-only t))
  14779. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  14780. (org-set-local 'org-columns-default-format fmt))))))
  14781. (defvar org-overriding-columns-format nil
  14782. "When set, overrides any other definition.")
  14783. (defvar org-agenda-view-columns-initially nil
  14784. "When set, switch to columns view immediately after creating the agenda.")
  14785. (defun org-agenda-columns ()
  14786. "Turn on column view in the agenda."
  14787. (interactive)
  14788. (org-verify-version 'columns)
  14789. (org-columns-remove-overlays)
  14790. (move-marker org-columns-begin-marker (point))
  14791. (let (fmt cache maxwidths m)
  14792. (cond
  14793. ((and (local-variable-p 'org-overriding-columns-format)
  14794. org-overriding-columns-format)
  14795. (setq fmt org-overriding-columns-format))
  14796. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  14797. (setq fmt (org-entry-get m "COLUMNS" t)))
  14798. ((and (boundp 'org-columns-current-fmt)
  14799. (local-variable-p 'org-columns-current-fmt)
  14800. org-columns-current-fmt)
  14801. (setq fmt org-columns-current-fmt))
  14802. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  14803. (setq m (get-text-property m 'org-hd-marker))
  14804. (setq fmt (org-entry-get m "COLUMNS" t))))
  14805. (setq fmt (or fmt org-columns-default-format))
  14806. (org-set-local 'org-columns-current-fmt fmt)
  14807. (org-columns-compile-format fmt)
  14808. (save-excursion
  14809. ;; Get and cache the properties
  14810. (goto-char (point-min))
  14811. (while (not (eobp))
  14812. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  14813. (get-text-property (point) 'org-marker)))
  14814. (push (cons (org-current-line) (org-entry-properties m)) cache))
  14815. (beginning-of-line 2))
  14816. (when cache
  14817. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  14818. (org-set-local 'org-columns-current-maxwidths maxwidths)
  14819. (org-columns-display-here-title)
  14820. (mapc (lambda (x)
  14821. (goto-line (car x))
  14822. (org-columns-display-here (cdr x)))
  14823. cache)))))
  14824. (defun org-columns-get-autowidth-alist (s cache)
  14825. "Derive the maximum column widths from the format and the cache."
  14826. (let ((start 0) rtn)
  14827. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  14828. (push (cons (match-string 1 s) 1) rtn)
  14829. (setq start (match-end 0)))
  14830. (mapc (lambda (x)
  14831. (setcdr x (apply 'max
  14832. (mapcar
  14833. (lambda (y)
  14834. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  14835. cache))))
  14836. rtn)
  14837. rtn))
  14838. (defun org-columns-compute-all ()
  14839. "Compute all columns that have operators defined."
  14840. (org-unmodified
  14841. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  14842. (let ((columns org-columns-current-fmt-compiled) col)
  14843. (while (setq col (pop columns))
  14844. (when (nth 3 col)
  14845. (save-excursion
  14846. (org-columns-compute (car col)))))))
  14847. (defun org-columns-update (property)
  14848. "Recompute PROPERTY, and update the columns display for it."
  14849. (org-columns-compute property)
  14850. (let (fmt val pos)
  14851. (save-excursion
  14852. (mapc (lambda (ov)
  14853. (when (equal (org-overlay-get ov 'org-columns-key) property)
  14854. (setq pos (org-overlay-start ov))
  14855. (goto-char pos)
  14856. (when (setq val (cdr (assoc property
  14857. (get-text-property
  14858. (point-at-bol) 'org-summaries))))
  14859. (setq fmt (org-overlay-get ov 'org-columns-format))
  14860. (org-overlay-put ov 'org-columns-value val)
  14861. (org-overlay-put ov 'display (format fmt val)))))
  14862. org-columns-overlays))))
  14863. (defun org-columns-compute (property)
  14864. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  14865. (interactive)
  14866. (let* ((re (concat "^" outline-regexp))
  14867. (lmax 30) ; Does anyone use deeper levels???
  14868. (lsum (make-vector lmax 0))
  14869. (lflag (make-vector lmax nil))
  14870. (level 0)
  14871. (ass (assoc property org-columns-current-fmt-compiled))
  14872. (format (nth 4 ass))
  14873. (printf (nth 5 ass))
  14874. (beg org-columns-top-level-marker)
  14875. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  14876. (save-excursion
  14877. ;; Find the region to compute
  14878. (goto-char beg)
  14879. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  14880. (goto-char end)
  14881. ;; Walk the tree from the back and do the computations
  14882. (while (re-search-backward re beg t)
  14883. (setq sumpos (match-beginning 0)
  14884. last-level level
  14885. level (org-outline-level)
  14886. val (org-entry-get nil property)
  14887. valflag (and val (string-match "\\S-" val)))
  14888. (cond
  14889. ((< level last-level)
  14890. ;; put the sum of lower levels here as a property
  14891. (setq sum (aref lsum last-level) ; current sum
  14892. flag (aref lflag last-level) ; any valid entries from children?
  14893. str (org-column-number-to-string sum format printf)
  14894. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  14895. useval (if flag str1 (if valflag val ""))
  14896. sum-alist (get-text-property sumpos 'org-summaries))
  14897. (if (assoc property sum-alist)
  14898. (setcdr (assoc property sum-alist) useval)
  14899. (push (cons property useval) sum-alist)
  14900. (org-unmodified
  14901. (add-text-properties sumpos (1+ sumpos)
  14902. (list 'org-summaries sum-alist))))
  14903. (when val
  14904. (org-entry-put nil property (if flag str val)))
  14905. ;; add current to current level accumulator
  14906. (when (or flag valflag)
  14907. ;; FIXME: is this ok?????????
  14908. (aset lsum level (+ (aref lsum level)
  14909. (if flag sum (org-column-string-to-number
  14910. (if flag str val) format))))
  14911. (aset lflag level t))
  14912. ;; clear accumulators for deeper levels
  14913. (loop for l from (1+ level) to (1- lmax) do
  14914. (aset lsum l 0)
  14915. (aset lflag l nil)))
  14916. ((>= level last-level)
  14917. ;; add what we have here to the accumulator for this level
  14918. (aset lsum level (+ (aref lsum level)
  14919. (org-column-string-to-number (or val "0") format)))
  14920. (and valflag (aset lflag level t)))
  14921. (t (error "This should not happen")))))))
  14922. (defun org-columns-redo ()
  14923. "Construct the column display again."
  14924. (interactive)
  14925. (message "Recomputing columns...")
  14926. (save-excursion
  14927. (if (marker-position org-columns-begin-marker)
  14928. (goto-char org-columns-begin-marker))
  14929. (org-columns-remove-overlays)
  14930. (if (org-mode-p)
  14931. (call-interactively 'org-columns)
  14932. (call-interactively 'org-agenda-columns)))
  14933. (message "Recomputing columns...done"))
  14934. (defun org-columns-not-in-agenda ()
  14935. (if (eq major-mode 'org-agenda-mode)
  14936. (error "This command is only allowed in Org-mode buffers")))
  14937. (defun org-string-to-number (s)
  14938. "Convert string to number, and interpret hh:mm:ss."
  14939. (if (not (string-match ":" s))
  14940. (string-to-number s)
  14941. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  14942. (while l
  14943. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  14944. sum)))
  14945. (defun org-column-number-to-string (n fmt printf)
  14946. "Convert a computed column number to a string value, according to FMT."
  14947. (cond
  14948. ((eq fmt 'add_times)
  14949. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  14950. (format "%d:%02d" h m)))
  14951. ((eq fmt 'checkbox)
  14952. (cond ((= n (floor n)) "[X]")
  14953. ((> n 1.) "[-]")
  14954. (t "[ ]")))
  14955. (printf (format printf n))
  14956. ((eq fmt 'currency)
  14957. (format "%.2f" n))
  14958. (t (number-to-string n))))
  14959. (defun org-column-string-to-number (s fmt)
  14960. "Convert a column value to a number that can be used for column computing."
  14961. (cond
  14962. ((string-match ":" s)
  14963. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  14964. (while l
  14965. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  14966. sum))
  14967. ((eq fmt 'checkbox)
  14968. (if (equal s "[X]") 1. 0.000001))
  14969. (t (string-to-number s))))
  14970. (defun org-columns-uncompile-format (cfmt)
  14971. "Turn the compiled columns format back into a string representation."
  14972. (let ((rtn "") e s prop title op width fmt printf)
  14973. (while (setq e (pop cfmt))
  14974. (setq prop (car e)
  14975. title (nth 1 e)
  14976. width (nth 2 e)
  14977. op (nth 3 e)
  14978. fmt (nth 4 e)
  14979. printf (nth 5 e))
  14980. (cond
  14981. ((eq fmt 'add_times) (setq op ":"))
  14982. ((eq fmt 'checkbox) (setq op "X"))
  14983. ((eq fmt 'add_numbers) (setq op "+"))
  14984. ((eq fmt 'currency) (setq op "$")))
  14985. (if (and op printf) (setq op (concat op ";" printf)))
  14986. (if (equal title prop) (setq title nil))
  14987. (setq s (concat "%" (if width (number-to-string width))
  14988. prop
  14989. (if title (concat "(" title ")"))
  14990. (if op (concat "{" op "}"))))
  14991. (setq rtn (concat rtn " " s)))
  14992. (org-trim rtn)))
  14993. (defun org-columns-compile-format (fmt)
  14994. "Turn a column format string into an alist of specifications.
  14995. The alist has one entry for each column in the format. The elements of
  14996. that list are:
  14997. property the property
  14998. title the title field for the columns
  14999. width the column width in characters, can be nil for automatic
  15000. operator the operator if any
  15001. format the output format for computed results, derived from operator
  15002. printf a printf format for computed values"
  15003. (let ((start 0) width prop title op f printf)
  15004. (setq org-columns-current-fmt-compiled nil)
  15005. (while (string-match
  15006. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15007. fmt start)
  15008. (setq start (match-end 0)
  15009. width (match-string 1 fmt)
  15010. prop (match-string 2 fmt)
  15011. title (or (match-string 3 fmt) prop)
  15012. op (match-string 4 fmt)
  15013. f nil
  15014. printf nil)
  15015. (if width (setq width (string-to-number width)))
  15016. (when (and op (string-match ";" op))
  15017. (setq printf (substring op (match-end 0))
  15018. op (substring op 0 (match-beginning 0))))
  15019. (cond
  15020. ((equal op "+") (setq f 'add_numbers))
  15021. ((equal op "$") (setq f 'currency))
  15022. ((equal op ":") (setq f 'add_times))
  15023. ((equal op "X") (setq f 'checkbox)))
  15024. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15025. (setq org-columns-current-fmt-compiled
  15026. (nreverse org-columns-current-fmt-compiled))))
  15027. ;;; Dynamic block for Column view
  15028. (defun org-columns-capture-view ()
  15029. "Get the column view of the current buffer and return it as a list.
  15030. The list will contains the title row and all other rows. Each row is
  15031. a list of fields."
  15032. (save-excursion
  15033. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15034. (n (length title)) row tbl)
  15035. (goto-char (point-min))
  15036. (while (re-search-forward "^\\*+ " nil t)
  15037. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15038. (setq row nil)
  15039. (loop for i from 0 to (1- n) do
  15040. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15041. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15042. "")
  15043. row))
  15044. (setq row (nreverse row))
  15045. (push row tbl)))
  15046. (append (list title 'hline) (nreverse tbl)))))
  15047. (defun org-dblock-write:columnview (params)
  15048. "Write the column view table.
  15049. PARAMS is a property list of parameters:
  15050. :width enforce same column widths with <N> specifiers.
  15051. :id the :ID: property of the entry where the columns view
  15052. should be built, as a string. When `local', call locally.
  15053. When `global' call column view with the cursor at the beginning
  15054. of the buffer (usually this means that the whole buffer switches
  15055. to column view).
  15056. :hlines When t, insert a hline before each item. When a number, insert
  15057. a hline before each level <= that number.
  15058. :vlines When t, make each column a colgroup to enforce vertical lines."
  15059. (let ((pos (move-marker (make-marker) (point)))
  15060. (hlines (plist-get params :hlines))
  15061. (vlines (plist-get params :vlines))
  15062. tbl id idpos nfields tmp)
  15063. (save-excursion
  15064. (save-restriction
  15065. (when (setq id (plist-get params :id))
  15066. (cond ((not id) nil)
  15067. ((eq id 'global) (goto-char (point-min)))
  15068. ((eq id 'local) nil)
  15069. ((setq idpos (org-find-entry-with-id id))
  15070. (goto-char idpos))
  15071. (t (error "Cannot find entry with :ID: %s" id))))
  15072. (org-columns)
  15073. (setq tbl (org-columns-capture-view))
  15074. (setq nfields (length (car tbl)))
  15075. (org-columns-quit)))
  15076. (goto-char pos)
  15077. (move-marker pos nil)
  15078. (when tbl
  15079. (when (plist-get params :hlines)
  15080. (setq tmp nil)
  15081. (while tbl
  15082. (if (eq (car tbl) 'hline)
  15083. (push (pop tbl) tmp)
  15084. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15085. (if (and (not (eq (car tmp) 'hline))
  15086. (or (eq hlines t)
  15087. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15088. (push 'hline tmp)))
  15089. (push (pop tbl) tmp)))
  15090. (setq tbl (nreverse tmp)))
  15091. (when vlines
  15092. (setq tbl (mapcar (lambda (x)
  15093. (if (eq 'hline x) x (cons "" x)))
  15094. tbl))
  15095. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15096. (setq pos (point))
  15097. (insert (org-listtable-to-string tbl))
  15098. (when (plist-get params :width)
  15099. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15100. org-columns-current-widths "|")))
  15101. (goto-char pos)
  15102. (org-table-align))))
  15103. (defun org-listtable-to-string (tbl)
  15104. "Convert a listtable TBL to a string that contains the Org-mode table.
  15105. The table still need to be alligned. The resulting string has no leading
  15106. and tailing newline characters."
  15107. (mapconcat
  15108. (lambda (x)
  15109. (cond
  15110. ((listp x)
  15111. (concat "|" (mapconcat 'identity x "|") "|"))
  15112. ((eq x 'hline) "|-|")
  15113. (t (error "Garbage in listtable: %s" x))))
  15114. tbl "\n"))
  15115. (defun org-insert-columns-dblock ()
  15116. "Create a dynamic block capturing a column view table."
  15117. (interactive)
  15118. (let ((defaults '(:name "columnview" :hlines 1))
  15119. (id (completing-read
  15120. "Capture columns (local, global, entry with :ID: property) [local]: "
  15121. (append '(("global") ("local"))
  15122. (mapcar 'list (org-property-values "ID"))))))
  15123. (if (equal id "") (setq id 'local))
  15124. (if (equal id "global") (setq id 'global))
  15125. (setq defaults (append defaults (list :id id)))
  15126. (org-create-dblock defaults)
  15127. (org-update-dblock)))
  15128. ;;;; Timestamps
  15129. (defvar org-last-changed-timestamp nil)
  15130. (defvar org-time-was-given) ; dynamically scoped parameter
  15131. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15132. (defvar org-ts-what) ; dynamically scoped parameter
  15133. (defun org-time-stamp (arg)
  15134. "Prompt for a date/time and insert a time stamp.
  15135. If the user specifies a time like HH:MM, or if this command is called
  15136. with a prefix argument, the time stamp will contain date and time.
  15137. Otherwise, only the date will be included. All parts of a date not
  15138. specified by the user will be filled in from the current date/time.
  15139. So if you press just return without typing anything, the time stamp
  15140. will represent the current date/time. If there is already a timestamp
  15141. at the cursor, it will be modified."
  15142. (interactive "P")
  15143. (let* ((ts nil)
  15144. (default-time
  15145. ;; Default time is either today, or, when entering a range,
  15146. ;; the range start.
  15147. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15148. (save-excursion
  15149. (re-search-backward
  15150. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15151. (- (point) 20) t)))
  15152. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15153. (current-time)))
  15154. (default-input (and ts (org-get-compact-tod ts)))
  15155. org-time-was-given org-end-time-was-given time)
  15156. (cond
  15157. ((and (org-at-timestamp-p)
  15158. (eq last-command 'org-time-stamp)
  15159. (eq this-command 'org-time-stamp))
  15160. (insert "--")
  15161. (setq time (let ((this-command this-command))
  15162. (org-read-date arg 'totime nil nil default-time default-input)))
  15163. (org-insert-time-stamp time (or org-time-was-given arg)))
  15164. ((org-at-timestamp-p)
  15165. (setq time (let ((this-command this-command))
  15166. (org-read-date arg 'totime nil nil default-time default-input)))
  15167. (when (org-at-timestamp-p) ; just to get the match data
  15168. (replace-match "")
  15169. (setq org-last-changed-timestamp
  15170. (org-insert-time-stamp
  15171. time (or org-time-was-given arg)
  15172. nil nil nil (list org-end-time-was-given))))
  15173. (message "Timestamp updated"))
  15174. (t
  15175. (setq time (let ((this-command this-command))
  15176. (org-read-date arg 'totime nil nil default-time default-input)))
  15177. (org-insert-time-stamp time (or org-time-was-given arg)
  15178. nil nil nil (list org-end-time-was-given))))))
  15179. ;; FIXME: can we use this for something else????
  15180. ;; like computing time differences?????
  15181. (defun org-get-compact-tod (s)
  15182. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15183. (let* ((t1 (match-string 1 s))
  15184. (h1 (string-to-number (match-string 2 s)))
  15185. (m1 (string-to-number (match-string 3 s)))
  15186. (t2 (and (match-end 4) (match-string 5 s)))
  15187. (h2 (and t2 (string-to-number (match-string 6 s))))
  15188. (m2 (and t2 (string-to-number (match-string 7 s))))
  15189. dh dm)
  15190. (if (not t2)
  15191. t1
  15192. (setq dh (- h2 h1) dm (- m2 m1))
  15193. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15194. (concat t1 "+" (number-to-string dh)
  15195. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  15196. (defun org-time-stamp-inactive (&optional arg)
  15197. "Insert an inactive time stamp.
  15198. An inactive time stamp is enclosed in square brackets instead of angle
  15199. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15200. does not link to the calendar and cannot be changed with the S-cursor keys.
  15201. So these are more for recording a certain time/date."
  15202. (interactive "P")
  15203. (let (org-time-was-given org-end-time-was-given time)
  15204. (setq time (org-read-date arg 'totime))
  15205. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  15206. nil nil (list org-end-time-was-given))))
  15207. (defvar org-date-ovl (org-make-overlay 1 1))
  15208. (org-overlay-put org-date-ovl 'face 'org-warning)
  15209. (org-detach-overlay org-date-ovl)
  15210. (defvar org-ans1) ; dynamically scoped parameter
  15211. (defvar org-ans2) ; dynamically scoped parameter
  15212. (defvar org-plain-time-of-day-regexp) ; defined below
  15213. (defvar org-read-date-overlay nil)
  15214. (defvar org-dcst nil) ; dynamically scoped
  15215. (defun org-read-date (&optional with-time to-time from-string prompt
  15216. default-time default-input)
  15217. "Read a date, possibly a time, and make things smooth for the user.
  15218. The prompt will suggest to enter an ISO date, but you can also enter anything
  15219. which will at least partially be understood by `parse-time-string'.
  15220. Unrecognized parts of the date will default to the current day, month, year,
  15221. hour and minute. If this command is called to replace a timestamp at point,
  15222. of to enter the second timestamp of a range, the default time is taken from the
  15223. existing stamp. For example,
  15224. 3-2-5 --> 2003-02-05
  15225. feb 15 --> currentyear-02-15
  15226. sep 12 9 --> 2009-09-12
  15227. 12:45 --> today 12:45
  15228. 22 sept 0:34 --> currentyear-09-22 0:34
  15229. 12 --> currentyear-currentmonth-12
  15230. Fri --> nearest Friday (today or later)
  15231. etc.
  15232. Furthermore you can specify a relative date by giving, as the *first* thing
  15233. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  15234. change in days weeks, months, years.
  15235. With a single plus or minus, the date is relative to today. With a double
  15236. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15237. +4d --> four days from today
  15238. +4 --> same as above
  15239. +2w --> two weeks from today
  15240. ++5 --> five days from default date
  15241. The function understands only English month and weekday abbreviations,
  15242. but this can be configured with the variables `parse-time-months' and
  15243. `parse-time-weekdays'.
  15244. While prompting, a calendar is popped up - you can also select the
  15245. date with the mouse (button 1). The calendar shows a period of three
  15246. months. To scroll it to other months, use the keys `>' and `<'.
  15247. If you don't like the calendar, turn it off with
  15248. \(setq org-read-date-popup-calendar nil)
  15249. With optional argument TO-TIME, the date will immediately be converted
  15250. to an internal time.
  15251. With an optional argument WITH-TIME, the prompt will suggest to also
  15252. insert a time. Note that when WITH-TIME is not set, you can still
  15253. enter a time, and this function will inform the calling routine about
  15254. this change. The calling routine may then choose to change the format
  15255. used to insert the time stamp into the buffer to include the time.
  15256. With optional argument FROM-STRING, read from this string instead from
  15257. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15258. the time/date that is used for everything that is not specified by the
  15259. user."
  15260. (require 'parse-time)
  15261. (let* ((org-time-stamp-rounding-minutes
  15262. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  15263. (org-dcst org-display-custom-times)
  15264. (ct (org-current-time))
  15265. (def (or default-time ct))
  15266. (defdecode (decode-time def))
  15267. (dummy (progn
  15268. (when (< (nth 2 defdecode) org-extend-today-until)
  15269. (setcar (nthcdr 2 defdecode) -1)
  15270. (setcar (nthcdr 1 defdecode) 59)
  15271. (setq def (apply 'encode-time defdecode)
  15272. defdecode (decode-time def)))))
  15273. (calendar-move-hook nil)
  15274. (view-diary-entries-initially nil)
  15275. (view-calendar-holidays-initially nil)
  15276. (timestr (format-time-string
  15277. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  15278. (prompt (concat (if prompt (concat prompt " ") "")
  15279. (format "Date+time [%s]: " timestr)))
  15280. ans (org-ans0 "") org-ans1 org-ans2 final)
  15281. (cond
  15282. (from-string (setq ans from-string))
  15283. (org-read-date-popup-calendar
  15284. (save-excursion
  15285. (save-window-excursion
  15286. (calendar)
  15287. (calendar-forward-day (- (time-to-days def)
  15288. (calendar-absolute-from-gregorian
  15289. (calendar-current-date))))
  15290. (org-eval-in-calendar nil t)
  15291. (let* ((old-map (current-local-map))
  15292. (map (copy-keymap calendar-mode-map))
  15293. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  15294. (org-defkey map (kbd "RET") 'org-calendar-select)
  15295. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  15296. 'org-calendar-select-mouse)
  15297. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  15298. 'org-calendar-select-mouse)
  15299. (org-defkey minibuffer-local-map [(meta shift left)]
  15300. (lambda () (interactive)
  15301. (org-eval-in-calendar '(calendar-backward-month 1))))
  15302. (org-defkey minibuffer-local-map [(meta shift right)]
  15303. (lambda () (interactive)
  15304. (org-eval-in-calendar '(calendar-forward-month 1))))
  15305. (org-defkey minibuffer-local-map [(meta shift up)]
  15306. (lambda () (interactive)
  15307. (org-eval-in-calendar '(calendar-backward-year 1))))
  15308. (org-defkey minibuffer-local-map [(meta shift down)]
  15309. (lambda () (interactive)
  15310. (org-eval-in-calendar '(calendar-forward-year 1))))
  15311. (org-defkey minibuffer-local-map [(shift up)]
  15312. (lambda () (interactive)
  15313. (org-eval-in-calendar '(calendar-backward-week 1))))
  15314. (org-defkey minibuffer-local-map [(shift down)]
  15315. (lambda () (interactive)
  15316. (org-eval-in-calendar '(calendar-forward-week 1))))
  15317. (org-defkey minibuffer-local-map [(shift left)]
  15318. (lambda () (interactive)
  15319. (org-eval-in-calendar '(calendar-backward-day 1))))
  15320. (org-defkey minibuffer-local-map [(shift right)]
  15321. (lambda () (interactive)
  15322. (org-eval-in-calendar '(calendar-forward-day 1))))
  15323. (org-defkey minibuffer-local-map ">"
  15324. (lambda () (interactive)
  15325. (org-eval-in-calendar '(scroll-calendar-left 1))))
  15326. (org-defkey minibuffer-local-map "<"
  15327. (lambda () (interactive)
  15328. (org-eval-in-calendar '(scroll-calendar-right 1))))
  15329. (unwind-protect
  15330. (progn
  15331. (use-local-map map)
  15332. (add-hook 'post-command-hook 'org-read-date-display)
  15333. (setq org-ans0 (read-string prompt default-input nil nil))
  15334. ;; org-ans0: from prompt
  15335. ;; org-ans1: from mouse click
  15336. ;; org-ans2: from calendar motion
  15337. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15338. (remove-hook 'post-command-hook 'org-read-date-display)
  15339. (use-local-map old-map)
  15340. (when org-read-date-overlay
  15341. (org-delete-overlay org-read-date-overlay)
  15342. (setq org-read-date-overlay nil)))))))
  15343. (t ; Naked prompt only
  15344. (unwind-protect
  15345. (setq ans (read-string prompt default-input nil timestr))
  15346. (when org-read-date-overlay
  15347. (org-delete-overlay org-read-date-overlay)
  15348. (setq org-read-date-overlay nil)))))
  15349. (setq final (org-read-date-analyze ans def defdecode))
  15350. (if to-time
  15351. (apply 'encode-time final)
  15352. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15353. (format "%04d-%02d-%02d %02d:%02d"
  15354. (nth 5 final) (nth 4 final) (nth 3 final)
  15355. (nth 2 final) (nth 1 final))
  15356. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15357. (defvar def)
  15358. (defvar defdecode)
  15359. (defvar with-time)
  15360. (defun org-read-date-display ()
  15361. "Display the currrent date prompt interpretation in the minibuffer."
  15362. (when org-read-date-display-live
  15363. (when org-read-date-overlay
  15364. (org-delete-overlay org-read-date-overlay))
  15365. (let ((p (point)))
  15366. (end-of-line 1)
  15367. (while (not (equal (buffer-substring
  15368. (max (point-min) (- (point) 4)) (point))
  15369. " "))
  15370. (insert " "))
  15371. (goto-char p))
  15372. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15373. " " (or org-ans1 org-ans2)))
  15374. (org-end-time-was-given nil)
  15375. (f (org-read-date-analyze ans def defdecode))
  15376. (fmts (if org-dcst
  15377. org-time-stamp-custom-formats
  15378. org-time-stamp-formats))
  15379. (fmt (if (or with-time
  15380. (and (boundp 'org-time-was-given) org-time-was-given))
  15381. (cdr fmts)
  15382. (car fmts)))
  15383. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  15384. (when (and org-end-time-was-given
  15385. (string-match org-plain-time-of-day-regexp txt))
  15386. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15387. org-end-time-was-given
  15388. (substring txt (match-end 0)))))
  15389. (setq org-read-date-overlay
  15390. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15391. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  15392. (defun org-read-date-analyze (ans def defdecode)
  15393. "Analyze the combined answer of the date prompt."
  15394. ;; FIXME: cleanup and comment
  15395. (let (delta deltan deltaw deltadef year month day
  15396. hour minute second wday pm h2 m2 tl wday1)
  15397. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  15398. (setq ans (replace-match "" t t ans)
  15399. deltan (car delta)
  15400. deltaw (nth 1 delta)
  15401. deltadef (nth 2 delta)))
  15402. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  15403. (when (string-match
  15404. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15405. (setq year (if (match-end 2)
  15406. (string-to-number (match-string 2 ans))
  15407. (string-to-number (format-time-string "%Y")))
  15408. month (string-to-number (match-string 3 ans))
  15409. day (string-to-number (match-string 4 ans)))
  15410. (if (< year 100) (setq year (+ 2000 year)))
  15411. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15412. t nil ans)))
  15413. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15414. ;; If there is a time with am/pm, and *no* time without it, we convert
  15415. ;; so that matching will be successful.
  15416. (loop for i from 1 to 2 do ; twice, for end time as well
  15417. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15418. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15419. (setq hour (string-to-number (match-string 1 ans))
  15420. minute (if (match-end 3)
  15421. (string-to-number (match-string 3 ans))
  15422. 0)
  15423. pm (equal ?p
  15424. (string-to-char (downcase (match-string 4 ans)))))
  15425. (if (and (= hour 12) (not pm))
  15426. (setq hour 0)
  15427. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15428. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15429. t t ans))))
  15430. ;; Check if a time range is given as a duration
  15431. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15432. (setq hour (string-to-number (match-string 1 ans))
  15433. h2 (+ hour (string-to-number (match-string 3 ans)))
  15434. minute (string-to-number (match-string 2 ans))
  15435. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  15436. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15437. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  15438. ;; Check if there is a time range
  15439. (when (boundp 'org-end-time-was-given)
  15440. (setq org-time-was-given nil)
  15441. (when (and (string-match org-plain-time-of-day-regexp ans)
  15442. (match-end 8))
  15443. (setq org-end-time-was-given (match-string 8 ans))
  15444. (setq ans (concat (substring ans 0 (match-beginning 7))
  15445. (substring ans (match-end 7))))))
  15446. (setq tl (parse-time-string ans)
  15447. day (or (nth 3 tl) (nth 3 defdecode))
  15448. month (or (nth 4 tl)
  15449. (if (and org-read-date-prefer-future
  15450. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  15451. (1+ (nth 4 defdecode))
  15452. (nth 4 defdecode)))
  15453. year (or (nth 5 tl)
  15454. (if (and org-read-date-prefer-future
  15455. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  15456. (1+ (nth 5 defdecode))
  15457. (nth 5 defdecode)))
  15458. hour (or (nth 2 tl) (nth 2 defdecode))
  15459. minute (or (nth 1 tl) (nth 1 defdecode))
  15460. second (or (nth 0 tl) 0)
  15461. wday (nth 6 tl))
  15462. (when deltan
  15463. (unless deltadef
  15464. (let ((now (decode-time (current-time))))
  15465. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15466. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15467. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15468. ((equal deltaw "m") (setq month (+ month deltan)))
  15469. ((equal deltaw "y") (setq year (+ year deltan)))))
  15470. (when (and wday (not (nth 3 tl)))
  15471. ;; Weekday was given, but no day, so pick that day in the week
  15472. ;; on or after the derived date.
  15473. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15474. (unless (equal wday wday1)
  15475. (setq day (+ day (% (- wday wday1 -7) 7)))))
  15476. (if (and (boundp 'org-time-was-given)
  15477. (nth 2 tl))
  15478. (setq org-time-was-given t))
  15479. (if (< year 100) (setq year (+ 2000 year)))
  15480. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  15481. (list second minute hour day month year)))
  15482. (defvar parse-time-weekdays)
  15483. (defun org-read-date-get-relative (s today default)
  15484. "Check string S for special relative date string.
  15485. TODAY and DEFAULT are internal times, for today and for a default.
  15486. Return shift list (N what def-flag)
  15487. WHAT is \"d\", \"w\", \"m\", or \"y\" for day. week, month, year.
  15488. N is the number if WHATs to shift
  15489. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15490. the DEFAULT date rather than TODAY."
  15491. (when (string-match
  15492. (concat
  15493. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  15494. "\\([0-9]+\\)?"
  15495. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15496. "\\([ \t]\\|$\\)") s)
  15497. (let* ((dir (if (match-end 1)
  15498. (string-to-char (substring (match-string 1 s) -1))
  15499. ?+))
  15500. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15501. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15502. (what (if (match-end 3) (match-string 3 s) "d"))
  15503. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15504. (date (if rel default today))
  15505. (wday (nth 6 (decode-time date)))
  15506. delta)
  15507. (if wday1
  15508. (progn
  15509. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15510. (if (= dir ?-) (setq delta (- delta 7)))
  15511. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15512. (list delta "d" rel))
  15513. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15514. (defun org-eval-in-calendar (form &optional keepdate)
  15515. "Eval FORM in the calendar window and return to current window.
  15516. Also, store the cursor date in variable org-ans2."
  15517. (let ((sw (selected-window)))
  15518. (select-window (get-buffer-window "*Calendar*"))
  15519. (eval form)
  15520. (when (and (not keepdate) (calendar-cursor-to-date))
  15521. (let* ((date (calendar-cursor-to-date))
  15522. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15523. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15524. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15525. (select-window sw)))
  15526. ; ;; Update the prompt to show new default date
  15527. ; (save-excursion
  15528. ; (goto-char (point-min))
  15529. ; (when (and org-ans2
  15530. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  15531. ; (get-text-property (match-end 0) 'field))
  15532. ; (let ((inhibit-read-only t))
  15533. ; (replace-match (concat "[" org-ans2 "]") t t)
  15534. ; (add-text-properties (point-min) (1+ (match-end 0))
  15535. ; (text-properties-at (1+ (point-min)))))))))
  15536. (defun org-calendar-select ()
  15537. "Return to `org-read-date' with the date currently selected.
  15538. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15539. (interactive)
  15540. (when (calendar-cursor-to-date)
  15541. (let* ((date (calendar-cursor-to-date))
  15542. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15543. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15544. (if (active-minibuffer-window) (exit-minibuffer))))
  15545. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15546. "Insert a date stamp for the date given by the internal TIME.
  15547. WITH-HM means, use the stamp format that includes the time of the day.
  15548. INACTIVE means use square brackets instead of angular ones, so that the
  15549. stamp will not contribute to the agenda.
  15550. PRE and POST are optional strings to be inserted before and after the
  15551. stamp.
  15552. The command returns the inserted time stamp."
  15553. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15554. stamp)
  15555. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15556. (insert-before-markers (or pre ""))
  15557. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15558. (when (listp extra)
  15559. (setq extra (car extra))
  15560. (if (and (stringp extra)
  15561. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15562. (setq extra (format "-%02d:%02d"
  15563. (string-to-number (match-string 1 extra))
  15564. (string-to-number (match-string 2 extra))))
  15565. (setq extra nil)))
  15566. (when extra
  15567. (backward-char 1)
  15568. (insert-before-markers extra)
  15569. (forward-char 1))
  15570. (insert-before-markers (or post ""))
  15571. stamp))
  15572. (defun org-toggle-time-stamp-overlays ()
  15573. "Toggle the use of custom time stamp formats."
  15574. (interactive)
  15575. (setq org-display-custom-times (not org-display-custom-times))
  15576. (unless org-display-custom-times
  15577. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15578. (while (setq p (next-single-property-change p 'display))
  15579. (if (and (get-text-property p 'display)
  15580. (eq (get-text-property p 'face) 'org-date))
  15581. (remove-text-properties
  15582. p (setq p (next-single-property-change p 'display))
  15583. '(display t))))
  15584. (set-buffer-modified-p bmp)))
  15585. (if (featurep 'xemacs)
  15586. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15587. (org-restart-font-lock)
  15588. (setq org-table-may-need-update t)
  15589. (if org-display-custom-times
  15590. (message "Time stamps are overlayed with custom format")
  15591. (message "Time stamp overlays removed")))
  15592. (defun org-display-custom-time (beg end)
  15593. "Overlay modified time stamp format over timestamp between BED and END."
  15594. (let* ((ts (buffer-substring beg end))
  15595. t1 w1 with-hm tf time str w2 (off 0))
  15596. (save-match-data
  15597. (setq t1 (org-parse-time-string ts t))
  15598. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  15599. (setq off (- (match-end 0) (match-beginning 0)))))
  15600. (setq end (- end off))
  15601. (setq w1 (- end beg)
  15602. with-hm (and (nth 1 t1) (nth 2 t1))
  15603. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15604. time (org-fix-decoded-time t1)
  15605. str (org-add-props
  15606. (format-time-string
  15607. (substring tf 1 -1) (apply 'encode-time time))
  15608. nil 'mouse-face 'highlight)
  15609. w2 (length str))
  15610. (if (not (= w2 w1))
  15611. (add-text-properties (1+ beg) (+ 2 beg)
  15612. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15613. (if (featurep 'xemacs)
  15614. (progn
  15615. (put-text-property beg end 'invisible t)
  15616. (put-text-property beg end 'end-glyph (make-glyph str)))
  15617. (put-text-property beg end 'display str))))
  15618. (defun org-translate-time (string)
  15619. "Translate all timestamps in STRING to custom format.
  15620. But do this only if the variable `org-display-custom-times' is set."
  15621. (when org-display-custom-times
  15622. (save-match-data
  15623. (let* ((start 0)
  15624. (re org-ts-regexp-both)
  15625. t1 with-hm inactive tf time str beg end)
  15626. (while (setq start (string-match re string start))
  15627. (setq beg (match-beginning 0)
  15628. end (match-end 0)
  15629. t1 (save-match-data
  15630. (org-parse-time-string (substring string beg end) t))
  15631. with-hm (and (nth 1 t1) (nth 2 t1))
  15632. inactive (equal (substring string beg (1+ beg)) "[")
  15633. tf (funcall (if with-hm 'cdr 'car)
  15634. org-time-stamp-custom-formats)
  15635. time (org-fix-decoded-time t1)
  15636. str (format-time-string
  15637. (concat
  15638. (if inactive "[" "<") (substring tf 1 -1)
  15639. (if inactive "]" ">"))
  15640. (apply 'encode-time time))
  15641. string (replace-match str t t string)
  15642. start (+ start (length str)))))))
  15643. string)
  15644. (defun org-fix-decoded-time (time)
  15645. "Set 0 instead of nil for the first 6 elements of time.
  15646. Don't touch the rest."
  15647. (let ((n 0))
  15648. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15649. (defun org-days-to-time (timestamp-string)
  15650. "Difference between TIMESTAMP-STRING and now in days."
  15651. (- (time-to-days (org-time-string-to-time timestamp-string))
  15652. (time-to-days (current-time))))
  15653. (defun org-deadline-close (timestamp-string &optional ndays)
  15654. "Is the time in TIMESTAMP-STRING close to the current date?"
  15655. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15656. (and (< (org-days-to-time timestamp-string) ndays)
  15657. (not (org-entry-is-done-p))))
  15658. (defun org-get-wdays (ts)
  15659. "Get the deadline lead time appropriate for timestring TS."
  15660. (cond
  15661. ((<= org-deadline-warning-days 0)
  15662. ;; 0 or negative, enforce this value no matter what
  15663. (- org-deadline-warning-days))
  15664. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  15665. ;; lead time is specified.
  15666. (floor (* (string-to-number (match-string 1 ts))
  15667. (cdr (assoc (match-string 2 ts)
  15668. '(("d" . 1) ("w" . 7)
  15669. ("m" . 30.4) ("y" . 365.25)))))))
  15670. ;; go for the default.
  15671. (t org-deadline-warning-days)))
  15672. (defun org-calendar-select-mouse (ev)
  15673. "Return to `org-read-date' with the date currently selected.
  15674. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15675. (interactive "e")
  15676. (mouse-set-point ev)
  15677. (when (calendar-cursor-to-date)
  15678. (let* ((date (calendar-cursor-to-date))
  15679. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15680. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15681. (if (active-minibuffer-window) (exit-minibuffer))))
  15682. (defun org-check-deadlines (ndays)
  15683. "Check if there are any deadlines due or past due.
  15684. A deadline is considered due if it happens within `org-deadline-warning-days'
  15685. days from today's date. If the deadline appears in an entry marked DONE,
  15686. it is not shown. The prefix arg NDAYS can be used to test that many
  15687. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15688. (interactive "P")
  15689. (let* ((org-warn-days
  15690. (cond
  15691. ((equal ndays '(4)) 100000)
  15692. (ndays (prefix-numeric-value ndays))
  15693. (t (abs org-deadline-warning-days))))
  15694. (case-fold-search nil)
  15695. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15696. (callback
  15697. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15698. (message "%d deadlines past-due or due within %d days"
  15699. (org-occur regexp nil callback)
  15700. org-warn-days)))
  15701. (defun org-evaluate-time-range (&optional to-buffer)
  15702. "Evaluate a time range by computing the difference between start and end.
  15703. Normally the result is just printed in the echo area, but with prefix arg
  15704. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15705. If the time range is actually in a table, the result is inserted into the
  15706. next column.
  15707. For time difference computation, a year is assumed to be exactly 365
  15708. days in order to avoid rounding problems."
  15709. (interactive "P")
  15710. (or
  15711. (org-clock-update-time-maybe)
  15712. (save-excursion
  15713. (unless (org-at-date-range-p t)
  15714. (goto-char (point-at-bol))
  15715. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15716. (if (not (org-at-date-range-p t))
  15717. (error "Not at a time-stamp range, and none found in current line")))
  15718. (let* ((ts1 (match-string 1))
  15719. (ts2 (match-string 2))
  15720. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15721. (match-end (match-end 0))
  15722. (time1 (org-time-string-to-time ts1))
  15723. (time2 (org-time-string-to-time ts2))
  15724. (t1 (time-to-seconds time1))
  15725. (t2 (time-to-seconds time2))
  15726. (diff (abs (- t2 t1)))
  15727. (negative (< (- t2 t1) 0))
  15728. ;; (ys (floor (* 365 24 60 60)))
  15729. (ds (* 24 60 60))
  15730. (hs (* 60 60))
  15731. (fy "%dy %dd %02d:%02d")
  15732. (fy1 "%dy %dd")
  15733. (fd "%dd %02d:%02d")
  15734. (fd1 "%dd")
  15735. (fh "%02d:%02d")
  15736. y d h m align)
  15737. (if havetime
  15738. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15739. y 0
  15740. d (floor (/ diff ds)) diff (mod diff ds)
  15741. h (floor (/ diff hs)) diff (mod diff hs)
  15742. m (floor (/ diff 60)))
  15743. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15744. y 0
  15745. d (floor (+ (/ diff ds) 0.5))
  15746. h 0 m 0))
  15747. (if (not to-buffer)
  15748. (message (org-make-tdiff-string y d h m))
  15749. (if (org-at-table-p)
  15750. (progn
  15751. (goto-char match-end)
  15752. (setq align t)
  15753. (and (looking-at " *|") (goto-char (match-end 0))))
  15754. (goto-char match-end))
  15755. (if (looking-at
  15756. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15757. (replace-match ""))
  15758. (if negative (insert " -"))
  15759. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15760. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15761. (insert " " (format fh h m))))
  15762. (if align (org-table-align))
  15763. (message "Time difference inserted")))))
  15764. (defun org-make-tdiff-string (y d h m)
  15765. (let ((fmt "")
  15766. (l nil))
  15767. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15768. l (push y l)))
  15769. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15770. l (push d l)))
  15771. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15772. l (push h l)))
  15773. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15774. l (push m l)))
  15775. (apply 'format fmt (nreverse l))))
  15776. (defun org-time-string-to-time (s)
  15777. (apply 'encode-time (org-parse-time-string s)))
  15778. (defun org-time-string-to-absolute (s &optional daynr)
  15779. "Convert a time stamp to an absolute day number.
  15780. If there is a specifyer for a cyclic time stamp, get the closest date to
  15781. DAYNR."
  15782. (cond
  15783. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15784. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15785. daynr
  15786. (+ daynr 1000)))
  15787. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  15788. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15789. (time-to-days (current-time))) (match-string 0 s)))
  15790. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  15791. (defun org-time-from-absolute (d)
  15792. "Return the time corresponding to date D.
  15793. D may be an absolute day number, or a calendar-type list (month day year)."
  15794. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15795. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15796. (defun org-calendar-holiday ()
  15797. "List of holidays, for Diary display in Org-mode."
  15798. (require 'holidays)
  15799. (let ((hl (funcall
  15800. (if (fboundp 'calendar-check-holidays)
  15801. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15802. (if hl (mapconcat 'identity hl "; "))))
  15803. (defun org-diary-sexp-entry (sexp entry date)
  15804. "Process a SEXP diary ENTRY for DATE."
  15805. (require 'diary-lib)
  15806. (let ((result (if calendar-debug-sexp
  15807. (let ((stack-trace-on-error t))
  15808. (eval (car (read-from-string sexp))))
  15809. (condition-case nil
  15810. (eval (car (read-from-string sexp)))
  15811. (error
  15812. (beep)
  15813. (message "Bad sexp at line %d in %s: %s"
  15814. (org-current-line)
  15815. (buffer-file-name) sexp)
  15816. (sleep-for 2))))))
  15817. (cond ((stringp result) result)
  15818. ((and (consp result)
  15819. (stringp (cdr result))) (cdr result))
  15820. (result entry)
  15821. (t nil))))
  15822. (defun org-diary-to-ical-string (frombuf)
  15823. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15824. This uses the icalendar.el library."
  15825. (let* ((tmpdir (if (featurep 'xemacs)
  15826. (temp-directory)
  15827. temporary-file-directory))
  15828. (tmpfile (make-temp-name
  15829. (expand-file-name "orgics" tmpdir)))
  15830. buf rtn b e)
  15831. (save-excursion
  15832. (set-buffer frombuf)
  15833. (icalendar-export-region (point-min) (point-max) tmpfile)
  15834. (setq buf (find-buffer-visiting tmpfile))
  15835. (set-buffer buf)
  15836. (goto-char (point-min))
  15837. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15838. (setq b (match-beginning 0)))
  15839. (goto-char (point-max))
  15840. (if (re-search-backward "^END:VEVENT" nil t)
  15841. (setq e (match-end 0)))
  15842. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15843. (kill-buffer buf)
  15844. (kill-buffer frombuf)
  15845. (delete-file tmpfile)
  15846. rtn))
  15847. (defun org-closest-date (start current change)
  15848. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  15849. ;; Make the proper lists from the dates
  15850. (catch 'exit
  15851. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  15852. dn dw sday cday n1 n2
  15853. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15854. (setq start (org-date-to-gregorian start)
  15855. current (org-date-to-gregorian
  15856. (if org-agenda-repeating-timestamp-show-all
  15857. current
  15858. (time-to-days (current-time))))
  15859. sday (calendar-absolute-from-gregorian start)
  15860. cday (calendar-absolute-from-gregorian current))
  15861. (if (<= cday sday) (throw 'exit sday))
  15862. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  15863. (setq dn (string-to-number (match-string 1 change))
  15864. dw (cdr (assoc (match-string 2 change) a1)))
  15865. (error "Invalid change specifyer: %s" change))
  15866. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15867. (cond
  15868. ((eq dw 'day)
  15869. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15870. n2 (+ n1 dn)))
  15871. ((eq dw 'year)
  15872. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15873. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15874. (setq date1 (list m d y1)
  15875. n1 (calendar-absolute-from-gregorian date1)
  15876. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15877. n2 (calendar-absolute-from-gregorian date2)))
  15878. ((eq dw 'month)
  15879. ;; approx number of month between the tow dates
  15880. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15881. ;; How often does dn fit in there?
  15882. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15883. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15884. m (+ m nm)
  15885. ny (floor (/ m 12))
  15886. y (+ y ny)
  15887. m (- m (* ny 12)))
  15888. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15889. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15890. (setq m2 (+ m dn) y2 y)
  15891. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15892. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15893. (while (< n2 cday)
  15894. (setq n1 n2 m m2 y y2)
  15895. (setq m2 (+ m dn) y2 y)
  15896. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15897. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15898. (if org-agenda-repeating-timestamp-show-all
  15899. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  15900. (if (= cday n1) n1 n2)))))
  15901. (defun org-date-to-gregorian (date)
  15902. "Turn any specification of DATE into a gregorian date for the calendar."
  15903. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15904. ((and (listp date) (= (length date) 3)) date)
  15905. ((stringp date)
  15906. (setq date (org-parse-time-string date))
  15907. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15908. ((listp date)
  15909. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15910. (defun org-parse-time-string (s &optional nodefault)
  15911. "Parse the standard Org-mode time string.
  15912. This should be a lot faster than the normal `parse-time-string'.
  15913. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15914. hour and minute fields will be nil if not given."
  15915. (if (string-match org-ts-regexp0 s)
  15916. (list 0
  15917. (if (or (match-beginning 8) (not nodefault))
  15918. (string-to-number (or (match-string 8 s) "0")))
  15919. (if (or (match-beginning 7) (not nodefault))
  15920. (string-to-number (or (match-string 7 s) "0")))
  15921. (string-to-number (match-string 4 s))
  15922. (string-to-number (match-string 3 s))
  15923. (string-to-number (match-string 2 s))
  15924. nil nil nil)
  15925. (make-list 9 0)))
  15926. (defun org-timestamp-up (&optional arg)
  15927. "Increase the date item at the cursor by one.
  15928. If the cursor is on the year, change the year. If it is on the month or
  15929. the day, change that.
  15930. With prefix ARG, change by that many units."
  15931. (interactive "p")
  15932. (org-timestamp-change (prefix-numeric-value arg)))
  15933. (defun org-timestamp-down (&optional arg)
  15934. "Decrease the date item at the cursor by one.
  15935. If the cursor is on the year, change the year. If it is on the month or
  15936. the day, change that.
  15937. With prefix ARG, change by that many units."
  15938. (interactive "p")
  15939. (org-timestamp-change (- (prefix-numeric-value arg))))
  15940. (defun org-timestamp-up-day (&optional arg)
  15941. "Increase the date in the time stamp by one day.
  15942. With prefix ARG, change that many days."
  15943. (interactive "p")
  15944. (if (and (not (org-at-timestamp-p t))
  15945. (org-on-heading-p))
  15946. (org-todo 'up)
  15947. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  15948. (defun org-timestamp-down-day (&optional arg)
  15949. "Decrease the date in the time stamp by one day.
  15950. With prefix ARG, change that many days."
  15951. (interactive "p")
  15952. (if (and (not (org-at-timestamp-p t))
  15953. (org-on-heading-p))
  15954. (org-todo 'down)
  15955. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  15956. (defsubst org-pos-in-match-range (pos n)
  15957. (and (match-beginning n)
  15958. (<= (match-beginning n) pos)
  15959. (>= (match-end n) pos)))
  15960. (defun org-at-timestamp-p (&optional inactive-ok)
  15961. "Determine if the cursor is in or at a timestamp."
  15962. (interactive)
  15963. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15964. (pos (point))
  15965. (ans (or (looking-at tsr)
  15966. (save-excursion
  15967. (skip-chars-backward "^[<\n\r\t")
  15968. (if (> (point) (point-min)) (backward-char 1))
  15969. (and (looking-at tsr)
  15970. (> (- (match-end 0) pos) -1))))))
  15971. (and ans
  15972. (boundp 'org-ts-what)
  15973. (setq org-ts-what
  15974. (cond
  15975. ((= pos (match-beginning 0)) 'bracket)
  15976. ((= pos (1- (match-end 0))) 'bracket)
  15977. ((org-pos-in-match-range pos 2) 'year)
  15978. ((org-pos-in-match-range pos 3) 'month)
  15979. ((org-pos-in-match-range pos 7) 'hour)
  15980. ((org-pos-in-match-range pos 8) 'minute)
  15981. ((or (org-pos-in-match-range pos 4)
  15982. (org-pos-in-match-range pos 5)) 'day)
  15983. ((and (> pos (or (match-end 8) (match-end 5)))
  15984. (< pos (match-end 0)))
  15985. (- pos (or (match-end 8) (match-end 5))))
  15986. (t 'day))))
  15987. ans))
  15988. (defun org-toggle-timestamp-type ()
  15989. ""
  15990. (interactive)
  15991. (when (org-at-timestamp-p t)
  15992. (save-excursion
  15993. (goto-char (match-beginning 0))
  15994. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  15995. (goto-char (1- (match-end 0)))
  15996. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  15997. (message "Timestamp is now %sactive"
  15998. (if (equal (char-before) ?>) "in" ""))))
  15999. (defun org-timestamp-change (n &optional what)
  16000. "Change the date in the time stamp at point.
  16001. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16002. `year', `minute', `second'. If WHAT is not given, the cursor position
  16003. in the timestamp determines what will be changed."
  16004. (let ((pos (point))
  16005. with-hm inactive
  16006. org-ts-what
  16007. extra
  16008. ts time time0)
  16009. (if (not (org-at-timestamp-p t))
  16010. (error "Not at a timestamp"))
  16011. (if (and (not what) (eq org-ts-what 'bracket))
  16012. (org-toggle-timestamp-type)
  16013. (if (and (not what) (not (eq org-ts-what 'day))
  16014. org-display-custom-times
  16015. (get-text-property (point) 'display)
  16016. (not (get-text-property (1- (point)) 'display)))
  16017. (setq org-ts-what 'day))
  16018. (setq org-ts-what (or what org-ts-what)
  16019. inactive (= (char-after (match-beginning 0)) ?\[)
  16020. ts (match-string 0))
  16021. (replace-match "")
  16022. (if (string-match
  16023. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16024. ts)
  16025. (setq extra (match-string 1 ts)))
  16026. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16027. (setq with-hm t))
  16028. (setq time0 (org-parse-time-string ts))
  16029. (setq time
  16030. (encode-time (or (car time0) 0)
  16031. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16032. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16033. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16034. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16035. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16036. (nthcdr 6 time0)))
  16037. (when (integerp org-ts-what)
  16038. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16039. (if (eq what 'calendar)
  16040. (let ((cal-date (org-get-date-from-calendar)))
  16041. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16042. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16043. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16044. (setcar time0 (or (car time0) 0))
  16045. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16046. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16047. (setq time (apply 'encode-time time0))))
  16048. (setq org-last-changed-timestamp
  16049. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16050. (org-clock-update-time-maybe)
  16051. (goto-char pos)
  16052. ;; Try to recenter the calendar window, if any
  16053. (if (and org-calendar-follow-timestamp-change
  16054. (get-buffer-window "*Calendar*" t)
  16055. (memq org-ts-what '(day month year)))
  16056. (org-recenter-calendar (time-to-days time))))))
  16057. ;; FIXME: does not yet work for lead times
  16058. (defun org-modify-ts-extra (s pos n)
  16059. "Change the different parts of the lead-time and repeat fields in timestamp."
  16060. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16061. ng h m new)
  16062. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16063. (cond
  16064. ((or (org-pos-in-match-range pos 2)
  16065. (org-pos-in-match-range pos 3))
  16066. (setq m (string-to-number (match-string 3 s))
  16067. h (string-to-number (match-string 2 s)))
  16068. (if (org-pos-in-match-range pos 2)
  16069. (setq h (+ h n))
  16070. (setq m (+ m n)))
  16071. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16072. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16073. (setq h (min 24 (max 0 h)))
  16074. (setq ng 1 new (format "-%02d:%02d" h m)))
  16075. ((org-pos-in-match-range pos 6)
  16076. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16077. ((org-pos-in-match-range pos 5)
  16078. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16079. (when ng
  16080. (setq s (concat
  16081. (substring s 0 (match-beginning ng))
  16082. new
  16083. (substring s (match-end ng))))))
  16084. s))
  16085. (defun org-recenter-calendar (date)
  16086. "If the calendar is visible, recenter it to DATE."
  16087. (let* ((win (selected-window))
  16088. (cwin (get-buffer-window "*Calendar*" t))
  16089. (calendar-move-hook nil))
  16090. (when cwin
  16091. (select-window cwin)
  16092. (calendar-goto-date (if (listp date) date
  16093. (calendar-gregorian-from-absolute date)))
  16094. (select-window win))))
  16095. (defun org-goto-calendar (&optional arg)
  16096. "Go to the Emacs calendar at the current date.
  16097. If there is a time stamp in the current line, go to that date.
  16098. A prefix ARG can be used to force the current date."
  16099. (interactive "P")
  16100. (let ((tsr org-ts-regexp) diff
  16101. (calendar-move-hook nil)
  16102. (view-calendar-holidays-initially nil)
  16103. (view-diary-entries-initially nil))
  16104. (if (or (org-at-timestamp-p)
  16105. (save-excursion
  16106. (beginning-of-line 1)
  16107. (looking-at (concat ".*" tsr))))
  16108. (let ((d1 (time-to-days (current-time)))
  16109. (d2 (time-to-days
  16110. (org-time-string-to-time (match-string 1)))))
  16111. (setq diff (- d2 d1))))
  16112. (calendar)
  16113. (calendar-goto-today)
  16114. (if (and diff (not arg)) (calendar-forward-day diff))))
  16115. (defun org-get-date-from-calendar ()
  16116. "Return a list (month day year) of date at point in calendar."
  16117. (with-current-buffer "*Calendar*"
  16118. (save-match-data
  16119. (calendar-cursor-to-date))))
  16120. (defun org-date-from-calendar ()
  16121. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16122. If there is already a time stamp at the cursor position, update it."
  16123. (interactive)
  16124. (if (org-at-timestamp-p t)
  16125. (org-timestamp-change 0 'calendar)
  16126. (let ((cal-date (org-get-date-from-calendar)))
  16127. (org-insert-time-stamp
  16128. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16129. ;; Make appt aware of appointments from the agenda
  16130. ;;;###autoload
  16131. (defun org-agenda-to-appt (&optional filter)
  16132. "Activate appointments found in `org-agenda-files'.
  16133. When prefixed, prompt for a regular expression and use it as a
  16134. filter: only add entries if they match this regular expression.
  16135. FILTER can be a string. In this case, use this string as a
  16136. regular expression to filter results.
  16137. FILTER can also be an alist, with the car of each cell being
  16138. either 'headline or 'category. For example:
  16139. '((headline \"IMPORTANT\")
  16140. (category \"Work\"))
  16141. will only add headlines containing IMPORTANT or headlines
  16142. belonging to the category \"Work\"."
  16143. (interactive "P")
  16144. (require 'calendar)
  16145. (if (equal filter '(4))
  16146. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16147. (let* ((cnt 0) ; count added events
  16148. (today (org-date-to-gregorian
  16149. (time-to-days (current-time))))
  16150. (files (org-agenda-files)) entries file)
  16151. ;; Get all entries which may contain an appt
  16152. (while (setq file (pop files))
  16153. (setq entries
  16154. (append entries
  16155. (org-agenda-get-day-entries
  16156. file today
  16157. :timestamp :scheduled :deadline))))
  16158. (setq entries (delq nil entries))
  16159. ;; Map thru entries and find if they pass thru the filter
  16160. (mapc
  16161. (lambda(x)
  16162. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  16163. (cat (get-text-property 1 'org-category x))
  16164. (tod (get-text-property 1 'time-of-day x))
  16165. (ok (or (null filter)
  16166. (and (stringp filter) (string-match filter evt))
  16167. (and (listp filter)
  16168. (or (string-match
  16169. (cadr (assoc 'category filter)) cat)
  16170. (string-match
  16171. (cadr (assoc 'headline filter)) evt))))))
  16172. ;; FIXME Shall we remove text-properties for the appt text?
  16173. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  16174. (when (and ok tod)
  16175. (setq tod (number-to-string tod)
  16176. tod (when (string-match
  16177. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  16178. (concat (match-string 1 tod) ":"
  16179. (match-string 2 tod))))
  16180. (appt-add tod evt)
  16181. (setq cnt (1+ cnt))))) entries)
  16182. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  16183. ;;; The clock for measuring work time.
  16184. (defvar org-mode-line-string "")
  16185. (put 'org-mode-line-string 'risky-local-variable t)
  16186. (defvar org-mode-line-timer nil)
  16187. (defvar org-clock-heading "")
  16188. (defvar org-clock-start-time "")
  16189. (defun org-update-mode-line ()
  16190. (let* ((delta (- (time-to-seconds (current-time))
  16191. (time-to-seconds org-clock-start-time)))
  16192. (h (floor delta 3600))
  16193. (m (floor (- delta (* 3600 h)) 60)))
  16194. (setq org-mode-line-string
  16195. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  16196. 'help-echo "Org-mode clock is running"))
  16197. (force-mode-line-update)))
  16198. (defvar org-clock-marker (make-marker)
  16199. "Marker recording the last clock-in.")
  16200. (defvar org-clock-mode-line-entry nil
  16201. "Information for the modeline about the running clock.")
  16202. (defun org-clock-in ()
  16203. "Start the clock on the current item.
  16204. If necessary, clock-out of the currently active clock."
  16205. (interactive)
  16206. (org-clock-out t)
  16207. (let (ts)
  16208. (save-excursion
  16209. (org-back-to-heading t)
  16210. (if (looking-at org-todo-line-regexp)
  16211. (setq org-clock-heading (match-string 3))
  16212. (setq org-clock-heading "???"))
  16213. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  16214. (org-clock-find-position)
  16215. (insert "\n") (backward-char 1)
  16216. (indent-relative)
  16217. (insert org-clock-string " ")
  16218. (setq org-clock-start-time (current-time))
  16219. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16220. (move-marker org-clock-marker (point) (buffer-base-buffer))
  16221. (or global-mode-string (setq global-mode-string '("")))
  16222. (or (memq 'org-mode-line-string global-mode-string)
  16223. (setq global-mode-string
  16224. (append global-mode-string '(org-mode-line-string))))
  16225. (org-update-mode-line)
  16226. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  16227. (message "Clock started at %s" ts))))
  16228. (defun org-clock-find-position ()
  16229. "Find the location where the next clock line should be inserted."
  16230. (org-back-to-heading t)
  16231. (catch 'exit
  16232. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  16233. (re (concat "^[ \t]*" org-clock-string))
  16234. (cnt 0)
  16235. first last)
  16236. (goto-char beg)
  16237. (when (eobp) (newline) (setq end (max (point) end)))
  16238. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  16239. ;; we seem to have a CLOCK drawer, so go there.
  16240. (beginning-of-line 2)
  16241. (throw 'exit t))
  16242. ;; Lets count the CLOCK lines
  16243. (goto-char beg)
  16244. (while (re-search-forward re end t)
  16245. (setq first (or first (match-beginning 0))
  16246. last (match-beginning 0)
  16247. cnt (1+ cnt)))
  16248. (when (and (integerp org-clock-into-drawer)
  16249. (>= (1+ cnt) org-clock-into-drawer))
  16250. ;; Wrap current entries into a new drawer
  16251. (goto-char last)
  16252. (beginning-of-line 2)
  16253. (if (org-at-item-p) (org-end-of-item))
  16254. (insert ":END:\n")
  16255. (beginning-of-line 0)
  16256. (org-indent-line-function)
  16257. (goto-char first)
  16258. (insert ":CLOCK:\n")
  16259. (beginning-of-line 0)
  16260. (org-indent-line-function)
  16261. (org-flag-drawer t)
  16262. (beginning-of-line 2)
  16263. (throw 'exit nil))
  16264. (goto-char beg)
  16265. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16266. (not (equal (match-string 1) org-clock-string)))
  16267. ;; Planning info, skip to after it
  16268. (beginning-of-line 2)
  16269. (or (bolp) (newline)))
  16270. (when (eq t org-clock-into-drawer)
  16271. (insert ":CLOCK:\n:END:\n")
  16272. (beginning-of-line -1)
  16273. (org-indent-line-function)
  16274. (org-flag-drawer t)
  16275. (beginning-of-line 2)
  16276. (org-indent-line-function)))))
  16277. (defun org-clock-out (&optional fail-quietly)
  16278. "Stop the currently running clock.
  16279. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  16280. (interactive)
  16281. (catch 'exit
  16282. (if (not (marker-buffer org-clock-marker))
  16283. (if fail-quietly (throw 'exit t) (error "No active clock")))
  16284. (let (ts te s h m)
  16285. (save-excursion
  16286. (set-buffer (marker-buffer org-clock-marker))
  16287. (goto-char org-clock-marker)
  16288. (beginning-of-line 1)
  16289. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16290. (equal (match-string 1) org-clock-string))
  16291. (setq ts (match-string 2))
  16292. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  16293. (goto-char (match-end 0))
  16294. (delete-region (point) (point-at-eol))
  16295. (insert "--")
  16296. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16297. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  16298. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  16299. h (floor (/ s 3600))
  16300. s (- s (* 3600 h))
  16301. m (floor (/ s 60))
  16302. s (- s (* 60 s)))
  16303. (insert " => " (format "%2d:%02d" h m))
  16304. (move-marker org-clock-marker nil)
  16305. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  16306. (org-log-done (org-parse-local-options logging 'org-log-done))
  16307. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  16308. (org-add-log-maybe 'clock-out))
  16309. (when org-mode-line-timer
  16310. (cancel-timer org-mode-line-timer)
  16311. (setq org-mode-line-timer nil))
  16312. (setq global-mode-string
  16313. (delq 'org-mode-line-string global-mode-string))
  16314. (force-mode-line-update)
  16315. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  16316. (defun org-clock-cancel ()
  16317. "Cancel the running clock be removing the start timestamp."
  16318. (interactive)
  16319. (if (not (marker-buffer org-clock-marker))
  16320. (error "No active clock"))
  16321. (save-excursion
  16322. (set-buffer (marker-buffer org-clock-marker))
  16323. (goto-char org-clock-marker)
  16324. (delete-region (1- (point-at-bol)) (point-at-eol)))
  16325. (message "Clock canceled"))
  16326. (defun org-clock-goto (&optional delete-windows)
  16327. "Go to the currently clocked-in entry."
  16328. (interactive "P")
  16329. (if (not (marker-buffer org-clock-marker))
  16330. (error "No active clock"))
  16331. (switch-to-buffer-other-window
  16332. (marker-buffer org-clock-marker))
  16333. (if delete-windows (delete-other-windows))
  16334. (goto-char org-clock-marker)
  16335. (org-show-entry)
  16336. (org-back-to-heading)
  16337. (recenter))
  16338. (defvar org-clock-file-total-minutes nil
  16339. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  16340. (make-variable-buffer-local 'org-clock-file-total-minutes)
  16341. (defun org-clock-sum (&optional tstart tend)
  16342. "Sum the times for each subtree.
  16343. Puts the resulting times in minutes as a text property on each headline."
  16344. (interactive)
  16345. (let* ((bmp (buffer-modified-p))
  16346. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  16347. org-clock-string
  16348. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  16349. (lmax 30)
  16350. (ltimes (make-vector lmax 0))
  16351. (t1 0)
  16352. (level 0)
  16353. ts te dt
  16354. time)
  16355. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  16356. (save-excursion
  16357. (goto-char (point-max))
  16358. (while (re-search-backward re nil t)
  16359. (cond
  16360. ((match-end 2)
  16361. ;; Two time stamps
  16362. (setq ts (match-string 2)
  16363. te (match-string 3)
  16364. ts (time-to-seconds
  16365. (apply 'encode-time (org-parse-time-string ts)))
  16366. te (time-to-seconds
  16367. (apply 'encode-time (org-parse-time-string te)))
  16368. ts (if tstart (max ts tstart) ts)
  16369. te (if tend (min te tend) te)
  16370. dt (- te ts)
  16371. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  16372. ((match-end 4)
  16373. ;; A naket time
  16374. (setq t1 (+ t1 (string-to-number (match-string 5))
  16375. (* 60 (string-to-number (match-string 4))))))
  16376. (t ;; A headline
  16377. (setq level (- (match-end 1) (match-beginning 1)))
  16378. (when (or (> t1 0) (> (aref ltimes level) 0))
  16379. (loop for l from 0 to level do
  16380. (aset ltimes l (+ (aref ltimes l) t1)))
  16381. (setq t1 0 time (aref ltimes level))
  16382. (loop for l from level to (1- lmax) do
  16383. (aset ltimes l 0))
  16384. (goto-char (match-beginning 0))
  16385. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  16386. (setq org-clock-file-total-minutes (aref ltimes 0)))
  16387. (set-buffer-modified-p bmp)))
  16388. (defun org-clock-display (&optional total-only)
  16389. "Show subtree times in the entire buffer.
  16390. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  16391. in the echo area."
  16392. (interactive)
  16393. (org-remove-clock-overlays)
  16394. (let (time h m p)
  16395. (org-clock-sum)
  16396. (unless total-only
  16397. (save-excursion
  16398. (goto-char (point-min))
  16399. (while (or (and (equal (setq p (point)) (point-min))
  16400. (get-text-property p :org-clock-minutes))
  16401. (setq p (next-single-property-change
  16402. (point) :org-clock-minutes)))
  16403. (goto-char p)
  16404. (when (setq time (get-text-property p :org-clock-minutes))
  16405. (org-put-clock-overlay time (funcall outline-level))))
  16406. (setq h (/ org-clock-file-total-minutes 60)
  16407. m (- org-clock-file-total-minutes (* 60 h)))
  16408. ;; Arrange to remove the overlays upon next change.
  16409. (when org-remove-highlights-with-change
  16410. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  16411. nil 'local))))
  16412. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  16413. (defvar org-clock-overlays nil)
  16414. (make-variable-buffer-local 'org-clock-overlays)
  16415. (defun org-put-clock-overlay (time &optional level)
  16416. "Put an overlays on the current line, displaying TIME.
  16417. If LEVEL is given, prefix time with a corresponding number of stars.
  16418. This creates a new overlay and stores it in `org-clock-overlays', so that it
  16419. will be easy to remove."
  16420. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  16421. (l (if level (org-get-legal-level level 0) 0))
  16422. (off 0)
  16423. ov tx)
  16424. (move-to-column c)
  16425. (unless (eolp) (skip-chars-backward "^ \t"))
  16426. (skip-chars-backward " \t")
  16427. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  16428. tx (concat (buffer-substring (1- (point)) (point))
  16429. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  16430. (org-add-props (format "%s %2d:%02d%s"
  16431. (make-string l ?*) h m
  16432. (make-string (- 10 l) ?\ ))
  16433. '(face secondary-selection))
  16434. ""))
  16435. (if (not (featurep 'xemacs))
  16436. (org-overlay-put ov 'display tx)
  16437. (org-overlay-put ov 'invisible t)
  16438. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  16439. (push ov org-clock-overlays)))
  16440. (defun org-remove-clock-overlays (&optional beg end noremove)
  16441. "Remove the occur highlights from the buffer.
  16442. BEG and END are ignored. If NOREMOVE is nil, remove this function
  16443. from the `before-change-functions' in the current buffer."
  16444. (interactive)
  16445. (unless org-inhibit-highlight-removal
  16446. (mapc 'org-delete-overlay org-clock-overlays)
  16447. (setq org-clock-overlays nil)
  16448. (unless noremove
  16449. (remove-hook 'before-change-functions
  16450. 'org-remove-clock-overlays 'local))))
  16451. (defun org-clock-out-if-current ()
  16452. "Clock out if the current entry contains the running clock.
  16453. This is used to stop the clock after a TODO entry is marked DONE,
  16454. and is only done if the variable `org-clock-out-when-done' is not nil."
  16455. (when (and org-clock-out-when-done
  16456. (member state org-done-keywords)
  16457. (equal (marker-buffer org-clock-marker) (current-buffer))
  16458. (< (point) org-clock-marker)
  16459. (> (save-excursion (outline-next-heading) (point))
  16460. org-clock-marker))
  16461. ;; Clock out, but don't accept a logging message for this.
  16462. (let ((org-log-done (if (and (listp org-log-done)
  16463. (member 'clock-out org-log-done))
  16464. '(done)
  16465. org-log-done)))
  16466. (org-clock-out))))
  16467. (add-hook 'org-after-todo-state-change-hook
  16468. 'org-clock-out-if-current)
  16469. (defun org-check-running-clock ()
  16470. "Check if the current buffer contains the running clock.
  16471. If yes, offer to stop it and to save the buffer with the changes."
  16472. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  16473. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  16474. (buffer-name))))
  16475. (org-clock-out)
  16476. (when (y-or-n-p "Save changed buffer?")
  16477. (save-buffer))))
  16478. (defun org-clock-report (&optional arg)
  16479. "Create a table containing a report about clocked time.
  16480. If the cursor is inside an existing clocktable block, then the table
  16481. will be updated. If not, a new clocktable will be inserted.
  16482. When called with a prefix argument, move to the first clock table in the
  16483. buffer and update it."
  16484. (interactive "P")
  16485. (org-remove-clock-overlays)
  16486. (when arg (org-find-dblock "clocktable"))
  16487. (if (org-in-clocktable-p)
  16488. (goto-char (org-in-clocktable-p))
  16489. (org-create-dblock (list :name "clocktable"
  16490. :maxlevel 2 :scope 'file)))
  16491. (org-update-dblock))
  16492. (defun org-in-clocktable-p ()
  16493. "Check if the cursor is in a clocktable."
  16494. (let ((pos (point)) start)
  16495. (save-excursion
  16496. (end-of-line 1)
  16497. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  16498. (setq start (match-beginning 0))
  16499. (re-search-forward "^#\\+END:.*" nil t)
  16500. (>= (match-end 0) pos)
  16501. start))))
  16502. (defun org-clock-update-time-maybe ()
  16503. "If this is a CLOCK line, update it and return t.
  16504. Otherwise, return nil."
  16505. (interactive)
  16506. (save-excursion
  16507. (beginning-of-line 1)
  16508. (skip-chars-forward " \t")
  16509. (when (looking-at org-clock-string)
  16510. (let ((re (concat "[ \t]*" org-clock-string
  16511. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  16512. "\\([ \t]*=>.*\\)?"))
  16513. ts te h m s)
  16514. (if (not (looking-at re))
  16515. nil
  16516. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  16517. (end-of-line 1)
  16518. (setq ts (match-string 1)
  16519. te (match-string 2))
  16520. (setq s (- (time-to-seconds
  16521. (apply 'encode-time (org-parse-time-string te)))
  16522. (time-to-seconds
  16523. (apply 'encode-time (org-parse-time-string ts))))
  16524. h (floor (/ s 3600))
  16525. s (- s (* 3600 h))
  16526. m (floor (/ s 60))
  16527. s (- s (* 60 s)))
  16528. (insert " => " (format "%2d:%02d" h m))
  16529. t)))))
  16530. (defun org-clock-special-range (key &optional time as-strings)
  16531. "Return two times bordering a special time range.
  16532. Key is a symbol specifying the range and can be one of `today', `yesterday',
  16533. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  16534. A week starts Monday 0:00 and ends Sunday 24:00.
  16535. The range is determined relative to TIME. TIME defaults to the current time.
  16536. The return value is a cons cell with two internal times like the ones
  16537. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  16538. the returned times will be formatted strings."
  16539. (let* ((tm (decode-time (or time (current-time))))
  16540. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  16541. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  16542. (dow (nth 6 tm))
  16543. s1 m1 h1 d1 month1 y1 diff ts te fm)
  16544. (cond
  16545. ((eq key 'today)
  16546. (setq h 0 m 0 h1 24 m1 0))
  16547. ((eq key 'yesterday)
  16548. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  16549. ((eq key 'thisweek)
  16550. (setq diff (if (= dow 0) 6 (1- dow))
  16551. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  16552. ((eq key 'lastweek)
  16553. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  16554. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  16555. ((eq key 'thismonth)
  16556. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  16557. ((eq key 'lastmonth)
  16558. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  16559. ((eq key 'thisyear)
  16560. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  16561. ((eq key 'lastyear)
  16562. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  16563. (t (error "No such time block %s" key)))
  16564. (setq ts (encode-time s m h d month y)
  16565. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  16566. (or d1 d) (or month1 month) (or y1 y)))
  16567. (setq fm (cdr org-time-stamp-formats))
  16568. (if as-strings
  16569. (cons (format-time-string fm ts) (format-time-string fm te))
  16570. (cons ts te))))
  16571. (defun org-dblock-write:clocktable (params)
  16572. "Write the standard clocktable."
  16573. (let ((hlchars '((1 . "*") (2 . "/")))
  16574. (emph nil)
  16575. (ins (make-marker))
  16576. (total-time nil)
  16577. ipos time h m p level hlc hdl maxlevel
  16578. ts te cc block beg end pos scope tbl tostring multifile)
  16579. (setq scope (plist-get params :scope)
  16580. tostring (plist-get params :tostring)
  16581. multifile (plist-get params :multifile)
  16582. maxlevel (or (plist-get params :maxlevel) 3)
  16583. emph (plist-get params :emphasize)
  16584. ts (plist-get params :tstart)
  16585. te (plist-get params :tend)
  16586. block (plist-get params :block))
  16587. (when block
  16588. (setq cc (org-clock-special-range block nil t)
  16589. ts (car cc) te (cdr cc)))
  16590. (if ts (setq ts (time-to-seconds
  16591. (apply 'encode-time (org-parse-time-string ts)))))
  16592. (if te (setq te (time-to-seconds
  16593. (apply 'encode-time (org-parse-time-string te)))))
  16594. (move-marker ins (point))
  16595. (setq ipos (point))
  16596. ;; Get the right scope
  16597. (setq pos (point))
  16598. (save-restriction
  16599. (cond
  16600. ((not scope))
  16601. ((eq scope 'file) (widen))
  16602. ((eq scope 'subtree) (org-narrow-to-subtree))
  16603. ((eq scope 'tree)
  16604. (while (org-up-heading-safe))
  16605. (org-narrow-to-subtree))
  16606. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  16607. (symbol-name scope)))
  16608. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  16609. (catch 'exit
  16610. (while (org-up-heading-safe)
  16611. (looking-at outline-regexp)
  16612. (if (<= (org-reduced-level (funcall outline-level)) level)
  16613. (throw 'exit nil))))
  16614. (org-narrow-to-subtree))
  16615. ((or (listp scope) (eq scope 'agenda))
  16616. (let* ((files (if (listp scope) scope (org-agenda-files)))
  16617. (scope 'agenda)
  16618. (p1 (copy-sequence params))
  16619. file)
  16620. (plist-put p1 :tostring t)
  16621. (plist-put p1 :multifile t)
  16622. (plist-put p1 :scope 'file)
  16623. (org-prepare-agenda-buffers files)
  16624. (while (setq file (pop files))
  16625. (with-current-buffer (find-buffer-visiting file)
  16626. (push (org-clocktable-add-file
  16627. file (org-dblock-write:clocktable p1)) tbl)
  16628. (setq total-time (+ (or total-time 0)
  16629. org-clock-file-total-minutes)))))))
  16630. (goto-char pos)
  16631. (unless (eq scope 'agenda)
  16632. (org-clock-sum ts te)
  16633. (goto-char (point-min))
  16634. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  16635. (goto-char p)
  16636. (when (setq time (get-text-property p :org-clock-minutes))
  16637. (save-excursion
  16638. (beginning-of-line 1)
  16639. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  16640. (setq level (org-reduced-level
  16641. (- (match-end 1) (match-beginning 1))))
  16642. (<= level maxlevel))
  16643. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  16644. hdl (match-string 2)
  16645. h (/ time 60)
  16646. m (- time (* 60 h)))
  16647. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  16648. (push (concat
  16649. "| " (int-to-string level) "|" hlc hdl hlc " |"
  16650. (make-string (1- level) ?|)
  16651. hlc (format "%d:%02d" h m) hlc
  16652. " |") tbl))))))
  16653. (setq tbl (nreverse tbl))
  16654. (if tostring
  16655. (if tbl (mapconcat 'identity tbl "\n") nil)
  16656. (goto-char ins)
  16657. (insert-before-markers
  16658. "Clock summary at ["
  16659. (substring
  16660. (format-time-string (cdr org-time-stamp-formats))
  16661. 1 -1)
  16662. "]."
  16663. (if block
  16664. (format " Considered range is /%s/." block)
  16665. "")
  16666. "\n\n"
  16667. (if (eq scope 'agenda) "|File" "")
  16668. "|L|Headline|Time|\n")
  16669. (setq total-time (or total-time org-clock-file-total-minutes)
  16670. h (/ total-time 60)
  16671. m (- total-time (* 60 h)))
  16672. (insert-before-markers
  16673. "|-\n|"
  16674. (if (eq scope 'agenda) "|" "")
  16675. "|"
  16676. "*Total time*| "
  16677. (format "*%d:%02d*" h m)
  16678. "|\n|-\n")
  16679. (setq tbl (delq nil tbl))
  16680. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  16681. (equal (substring (car tbl) 0 2) "|-"))
  16682. (pop tbl))
  16683. (insert-before-markers (mapconcat
  16684. 'identity (delq nil tbl)
  16685. (if (eq scope 'agenda) "\n|-\n" "\n")))
  16686. (backward-delete-char 1)
  16687. (goto-char ipos)
  16688. (skip-chars-forward "^|")
  16689. (org-table-align)))))
  16690. (defun org-clocktable-add-file (file table)
  16691. (if table
  16692. (let ((lines (org-split-string table "\n"))
  16693. (ff (file-name-nondirectory file)))
  16694. (mapconcat 'identity
  16695. (mapcar (lambda (x)
  16696. (if (string-match org-table-dataline-regexp x)
  16697. (concat "|" ff x)
  16698. x))
  16699. lines)
  16700. "\n"))))
  16701. ;; FIXME: I don't think anybody uses this, ask David
  16702. (defun org-collect-clock-time-entries ()
  16703. "Return an internal list with clocking information.
  16704. This list has one entry for each CLOCK interval.
  16705. FIXME: describe the elements."
  16706. (interactive)
  16707. (let ((re (concat "^[ \t]*" org-clock-string
  16708. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  16709. rtn beg end next cont level title total closedp leafp
  16710. clockpos titlepos h m donep)
  16711. (save-excursion
  16712. (org-clock-sum)
  16713. (goto-char (point-min))
  16714. (while (re-search-forward re nil t)
  16715. (setq clockpos (match-beginning 0)
  16716. beg (match-string 1) end (match-string 2)
  16717. cont (match-end 0))
  16718. (setq beg (apply 'encode-time (org-parse-time-string beg))
  16719. end (apply 'encode-time (org-parse-time-string end)))
  16720. (org-back-to-heading t)
  16721. (setq donep (org-entry-is-done-p))
  16722. (setq titlepos (point)
  16723. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  16724. h (/ total 60) m (- total (* 60 h))
  16725. total (cons h m))
  16726. (looking-at "\\(\\*+\\) +\\(.*\\)")
  16727. (setq level (- (match-end 1) (match-beginning 1))
  16728. title (org-match-string-no-properties 2))
  16729. (save-excursion (outline-next-heading) (setq next (point)))
  16730. (setq closedp (re-search-forward org-closed-time-regexp next t))
  16731. (goto-char next)
  16732. (setq leafp (and (looking-at "^\\*+ ")
  16733. (<= (- (match-end 0) (point)) level)))
  16734. (push (list beg end clockpos closedp donep
  16735. total title titlepos level leafp)
  16736. rtn)
  16737. (goto-char cont)))
  16738. (nreverse rtn)))
  16739. ;;;; Agenda, and Diary Integration
  16740. ;;; Define the Org-agenda-mode
  16741. (defvar org-agenda-mode-map (make-sparse-keymap)
  16742. "Keymap for `org-agenda-mode'.")
  16743. (defvar org-agenda-menu) ; defined later in this file.
  16744. (defvar org-agenda-follow-mode nil)
  16745. (defvar org-agenda-show-log nil)
  16746. (defvar org-agenda-redo-command nil)
  16747. (defvar org-agenda-mode-hook nil)
  16748. (defvar org-agenda-type nil)
  16749. (defvar org-agenda-force-single-file nil)
  16750. (defun org-agenda-mode ()
  16751. "Mode for time-sorted view on action items in Org-mode files.
  16752. The following commands are available:
  16753. \\{org-agenda-mode-map}"
  16754. (interactive)
  16755. (kill-all-local-variables)
  16756. (setq org-agenda-undo-list nil
  16757. org-agenda-pending-undo-list nil)
  16758. (setq major-mode 'org-agenda-mode)
  16759. ;; Keep global-font-lock-mode from turning on font-lock-mode
  16760. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  16761. (setq mode-name "Org-Agenda")
  16762. (use-local-map org-agenda-mode-map)
  16763. (easy-menu-add org-agenda-menu)
  16764. (if org-startup-truncated (setq truncate-lines t))
  16765. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  16766. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  16767. ;; Make sure properties are removed when copying text
  16768. (when (boundp 'buffer-substring-filters)
  16769. (org-set-local 'buffer-substring-filters
  16770. (cons (lambda (x)
  16771. (set-text-properties 0 (length x) nil x) x)
  16772. buffer-substring-filters)))
  16773. (unless org-agenda-keep-modes
  16774. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  16775. org-agenda-show-log nil))
  16776. (easy-menu-change
  16777. '("Agenda") "Agenda Files"
  16778. (append
  16779. (list
  16780. (vector
  16781. (if (get 'org-agenda-files 'org-restrict)
  16782. "Restricted to single file"
  16783. "Edit File List")
  16784. '(org-edit-agenda-file-list)
  16785. (not (get 'org-agenda-files 'org-restrict)))
  16786. "--")
  16787. (mapcar 'org-file-menu-entry (org-agenda-files))))
  16788. (org-agenda-set-mode-name)
  16789. (apply
  16790. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  16791. (list 'org-agenda-mode-hook)))
  16792. (substitute-key-definition 'undo 'org-agenda-undo
  16793. org-agenda-mode-map global-map)
  16794. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  16795. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  16796. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  16797. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  16798. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  16799. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  16800. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  16801. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  16802. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  16803. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  16804. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  16805. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  16806. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  16807. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  16808. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  16809. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  16810. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  16811. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  16812. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  16813. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  16814. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  16815. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  16816. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  16817. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  16818. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  16819. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  16820. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  16821. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  16822. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  16823. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  16824. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  16825. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  16826. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  16827. (while l (org-defkey org-agenda-mode-map
  16828. (int-to-string (pop l)) 'digit-argument)))
  16829. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  16830. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  16831. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  16832. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  16833. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  16834. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  16835. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  16836. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  16837. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  16838. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  16839. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  16840. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  16841. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  16842. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  16843. (org-defkey org-agenda-mode-map "n" 'next-line)
  16844. (org-defkey org-agenda-mode-map "p" 'previous-line)
  16845. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  16846. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  16847. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  16848. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  16849. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  16850. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  16851. (eval-after-load "calendar"
  16852. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  16853. 'org-calendar-goto-agenda))
  16854. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  16855. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  16856. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  16857. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  16858. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  16859. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  16860. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  16861. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  16862. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  16863. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  16864. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  16865. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16866. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  16867. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  16868. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  16869. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  16870. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  16871. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  16872. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  16873. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  16874. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  16875. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  16876. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  16877. "Local keymap for agenda entries from Org-mode.")
  16878. (org-defkey org-agenda-keymap
  16879. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  16880. (org-defkey org-agenda-keymap
  16881. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  16882. (when org-agenda-mouse-1-follows-link
  16883. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  16884. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  16885. '("Agenda"
  16886. ("Agenda Files")
  16887. "--"
  16888. ["Show" org-agenda-show t]
  16889. ["Go To (other window)" org-agenda-goto t]
  16890. ["Go To (this window)" org-agenda-switch-to t]
  16891. ["Follow Mode" org-agenda-follow-mode
  16892. :style toggle :selected org-agenda-follow-mode :active t]
  16893. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  16894. "--"
  16895. ["Cycle TODO" org-agenda-todo t]
  16896. ["Archive subtree" org-agenda-archive t]
  16897. ["Delete subtree" org-agenda-kill t]
  16898. "--"
  16899. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  16900. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  16901. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  16902. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  16903. "--"
  16904. ("Tags and Properties"
  16905. ["Show all Tags" org-agenda-show-tags t]
  16906. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  16907. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  16908. "--"
  16909. ["Column View" org-columns t])
  16910. ("Date/Schedule"
  16911. ["Schedule" org-agenda-schedule t]
  16912. ["Set Deadline" org-agenda-deadline t]
  16913. "--"
  16914. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  16915. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  16916. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  16917. ("Clock"
  16918. ["Clock in" org-agenda-clock-in t]
  16919. ["Clock out" org-agenda-clock-out t]
  16920. ["Clock cancel" org-agenda-clock-cancel t]
  16921. ["Goto running clock" org-clock-goto t])
  16922. ("Priority"
  16923. ["Set Priority" org-agenda-priority t]
  16924. ["Increase Priority" org-agenda-priority-up t]
  16925. ["Decrease Priority" org-agenda-priority-down t]
  16926. ["Show Priority" org-agenda-show-priority t])
  16927. ("Calendar/Diary"
  16928. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  16929. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  16930. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  16931. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  16932. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  16933. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  16934. "--"
  16935. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  16936. "--"
  16937. ("View"
  16938. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  16939. :style radio :selected (equal org-agenda-ndays 1)]
  16940. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  16941. :style radio :selected (equal org-agenda-ndays 7)]
  16942. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  16943. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  16944. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  16945. :style radio :selected (member org-agenda-ndays '(365 366))]
  16946. "--"
  16947. ["Show Logbook entries" org-agenda-log-mode
  16948. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  16949. ["Include Diary" org-agenda-toggle-diary
  16950. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  16951. ["Use Time Grid" org-agenda-toggle-time-grid
  16952. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  16953. ["Write view to file" org-write-agenda t]
  16954. ["Rebuild buffer" org-agenda-redo t]
  16955. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  16956. "--"
  16957. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  16958. "--"
  16959. ["Quit" org-agenda-quit t]
  16960. ["Exit and Release Buffers" org-agenda-exit t]
  16961. ))
  16962. ;;; Agenda undo
  16963. (defvar org-agenda-allow-remote-undo t
  16964. "Non-nil means, allow remote undo from the agenda buffer.")
  16965. (defvar org-agenda-undo-list nil
  16966. "List of undoable operations in the agenda since last refresh.")
  16967. (defvar org-agenda-undo-has-started-in nil
  16968. "Buffers that have already seen `undo-start' in the current undo sequence.")
  16969. (defvar org-agenda-pending-undo-list nil
  16970. "In a series of undo commands, this is the list of remaning undo items.")
  16971. (defmacro org-if-unprotected (&rest body)
  16972. "Execute BODY if there is no `org-protected' text property at point."
  16973. (declare (debug t))
  16974. `(unless (get-text-property (point) 'org-protected)
  16975. ,@body))
  16976. (defmacro org-with-remote-undo (_buffer &rest _body)
  16977. "Execute BODY while recording undo information in two buffers."
  16978. (declare (indent 1) (debug t))
  16979. `(let ((_cline (org-current-line))
  16980. (_cmd this-command)
  16981. (_buf1 (current-buffer))
  16982. (_buf2 ,_buffer)
  16983. (_undo1 buffer-undo-list)
  16984. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  16985. _c1 _c2)
  16986. ,@_body
  16987. (when org-agenda-allow-remote-undo
  16988. (setq _c1 (org-verify-change-for-undo
  16989. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  16990. _c2 (org-verify-change-for-undo
  16991. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  16992. (when (or _c1 _c2)
  16993. ;; make sure there are undo boundaries
  16994. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  16995. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  16996. ;; remember which buffer to undo
  16997. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  16998. org-agenda-undo-list)))))
  16999. (defun org-agenda-undo ()
  17000. "Undo a remote editing step in the agenda.
  17001. This undoes changes both in the agenda buffer and in the remote buffer
  17002. that have been changed along."
  17003. (interactive)
  17004. (or org-agenda-allow-remote-undo
  17005. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17006. (if (not (eq this-command last-command))
  17007. (setq org-agenda-undo-has-started-in nil
  17008. org-agenda-pending-undo-list org-agenda-undo-list))
  17009. (if (not org-agenda-pending-undo-list)
  17010. (error "No further undo information"))
  17011. (let* ((entry (pop org-agenda-pending-undo-list))
  17012. buf line cmd rembuf)
  17013. (setq cmd (pop entry) line (pop entry))
  17014. (setq rembuf (nth 2 entry))
  17015. (org-with-remote-undo rembuf
  17016. (while (bufferp (setq buf (pop entry)))
  17017. (if (pop entry)
  17018. (with-current-buffer buf
  17019. (let ((last-undo-buffer buf)
  17020. (inhibit-read-only t))
  17021. (unless (memq buf org-agenda-undo-has-started-in)
  17022. (push buf org-agenda-undo-has-started-in)
  17023. (make-local-variable 'pending-undo-list)
  17024. (undo-start))
  17025. (while (and pending-undo-list
  17026. (listp pending-undo-list)
  17027. (not (car pending-undo-list)))
  17028. (pop pending-undo-list))
  17029. (undo-more 1))))))
  17030. (goto-line line)
  17031. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17032. (defun org-verify-change-for-undo (l1 l2)
  17033. "Verify that a real change occurred between the undo lists L1 and L2."
  17034. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17035. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17036. (not (eq l1 l2)))
  17037. ;;; Agenda dispatch
  17038. (defvar org-agenda-restrict nil)
  17039. (defvar org-agenda-restrict-begin (make-marker))
  17040. (defvar org-agenda-restrict-end (make-marker))
  17041. (defvar org-agenda-last-dispatch-buffer nil)
  17042. ;;;###autoload
  17043. (defun org-agenda (arg &optional keys restriction)
  17044. "Dispatch agenda commands to collect entries to the agenda buffer.
  17045. Prompts for a command to execute. Any prefix arg will be passed
  17046. on to the selected command. The default selections are:
  17047. a Call `org-agenda-list' to display the agenda for current day or week.
  17048. t Call `org-todo-list' to display the global todo list.
  17049. T Call `org-todo-list' to display the global todo list, select only
  17050. entries with a specific TODO keyword (the user gets a prompt).
  17051. m Call `org-tags-view' to display headlines with tags matching
  17052. a condition (the user is prompted for the condition).
  17053. M Like `m', but select only TODO entries, no ordinary headlines.
  17054. L Create a timeline for the current buffer.
  17055. e Export views to associated files.
  17056. More commands can be added by configuring the variable
  17057. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17058. searches can be pre-defined in this way.
  17059. If the current buffer is in Org-mode and visiting a file, you can also
  17060. first press `<' once to indicate that the agenda should be temporarily
  17061. \(until the next use of \\[org-agenda]) restricted to the current file.
  17062. Pressing `<' twice means to restrict to the current subtree or region
  17063. \(if active)."
  17064. (interactive "P")
  17065. (catch 'exit
  17066. (let* ((prefix-descriptions nil)
  17067. (org-agenda-custom-commands
  17068. ;; normalize different versions
  17069. (delq nil
  17070. (mapcar
  17071. (lambda (x)
  17072. (cond ((stringp (cdr x))
  17073. (push x prefix-descriptions)
  17074. nil)
  17075. ((stringp (nth 1 x)) x)
  17076. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17077. (t (cons (car x) (cons "" (cdr x))))))
  17078. org-agenda-custom-commands)))
  17079. (buf (current-buffer))
  17080. (bfn (buffer-file-name (buffer-base-buffer)))
  17081. entry key type match lprops ans)
  17082. ;; Turn off restriction
  17083. (put 'org-agenda-files 'org-restrict nil)
  17084. (setq org-agenda-restrict nil)
  17085. (move-marker org-agenda-restrict-begin nil)
  17086. (move-marker org-agenda-restrict-end nil)
  17087. ;; Delete old local properties
  17088. (put 'org-agenda-redo-command 'org-lprops nil)
  17089. ;; Remember where this call originated
  17090. (setq org-agenda-last-dispatch-buffer (current-buffer))
  17091. (unless keys
  17092. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  17093. keys (car ans)
  17094. restriction (cdr ans)))
  17095. ;; Estabish the restriction, if any
  17096. (when restriction
  17097. (put 'org-agenda-files 'org-restrict (list bfn))
  17098. (cond
  17099. ((eq restriction 'region)
  17100. (setq org-agenda-restrict t)
  17101. (move-marker org-agenda-restrict-begin (region-beginning))
  17102. (move-marker org-agenda-restrict-end (region-end)))
  17103. ((eq restriction 'subtree)
  17104. (save-excursion
  17105. (setq org-agenda-restrict t)
  17106. (org-back-to-heading t)
  17107. (move-marker org-agenda-restrict-begin (point))
  17108. (move-marker org-agenda-restrict-end
  17109. (progn (org-end-of-subtree t)))))))
  17110. (require 'calendar) ; FIXME: can we avoid this for some commands?
  17111. ;; For example the todo list should not need it (but does...)
  17112. (cond
  17113. ((setq entry (assoc keys org-agenda-custom-commands))
  17114. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  17115. (progn
  17116. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  17117. (put 'org-agenda-redo-command 'org-lprops lprops)
  17118. (cond
  17119. ((eq type 'agenda)
  17120. (org-let lprops '(org-agenda-list current-prefix-arg)))
  17121. ((eq type 'alltodo)
  17122. (org-let lprops '(org-todo-list current-prefix-arg)))
  17123. ((eq type 'stuck)
  17124. (org-let lprops '(org-agenda-list-stuck-projects
  17125. current-prefix-arg)))
  17126. ((eq type 'tags)
  17127. (org-let lprops '(org-tags-view current-prefix-arg match)))
  17128. ((eq type 'tags-todo)
  17129. (org-let lprops '(org-tags-view '(4) match)))
  17130. ((eq type 'todo)
  17131. (org-let lprops '(org-todo-list match)))
  17132. ((eq type 'tags-tree)
  17133. (org-check-for-org-mode)
  17134. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  17135. ((eq type 'todo-tree)
  17136. (org-check-for-org-mode)
  17137. (org-let lprops
  17138. '(org-occur (concat "^" outline-regexp "[ \t]*"
  17139. (regexp-quote match) "\\>"))))
  17140. ((eq type 'occur-tree)
  17141. (org-check-for-org-mode)
  17142. (org-let lprops '(org-occur match)))
  17143. ((functionp type)
  17144. (org-let lprops '(funcall type match)))
  17145. ((fboundp type)
  17146. (org-let lprops '(funcall type match)))
  17147. (t (error "Invalid custom agenda command type %s" type))))
  17148. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  17149. ((equal keys "C") (customize-variable 'org-agenda-custom-commands))
  17150. ((equal keys "a") (call-interactively 'org-agenda-list))
  17151. ((equal keys "t") (call-interactively 'org-todo-list))
  17152. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  17153. ((equal keys "m") (call-interactively 'org-tags-view))
  17154. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  17155. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  17156. ((equal keys "L")
  17157. (unless (org-mode-p)
  17158. (error "This is not an Org-mode file"))
  17159. (unless restriction
  17160. (put 'org-agenda-files 'org-restrict (list bfn))
  17161. (org-call-with-arg 'org-timeline arg)))
  17162. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  17163. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  17164. ((equal keys "!") (customize-variable 'org-stuck-projects))
  17165. (t (error "Invalid agenda key"))))))
  17166. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  17167. "The user interface for selecting an agenda command."
  17168. (catch 'exit
  17169. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  17170. (restrict-ok (and bfn (org-mode-p)))
  17171. (region-p (org-region-active-p))
  17172. (custom org-agenda-custom-commands)
  17173. (selstring "")
  17174. restriction second-time
  17175. c entry key type match prefixes rmheader header-end custom1 desc)
  17176. (save-window-excursion
  17177. (delete-other-windows)
  17178. (org-switch-to-buffer-other-window " *Agenda Commands*")
  17179. (erase-buffer)
  17180. (insert (eval-when-compile
  17181. (let ((header
  17182. "Press key for an agenda command: < Buffer,subtree/region restriction
  17183. -------------------------------- C Configure custom agenda commands
  17184. a Agenda for current week or day e Export agenda views
  17185. t List of all TODO entries T Entries with special TODO kwd
  17186. m Match a TAGS query M Like m, but only TODO entries
  17187. L Timeline for current buffer # List stuck projects (!=configure)
  17188. / Multi-occur
  17189. ")
  17190. (start 0))
  17191. (while (string-match
  17192. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  17193. header start)
  17194. (setq start (match-end 0))
  17195. (add-text-properties (match-beginning 2) (match-end 2)
  17196. '(face bold) header))
  17197. header)))
  17198. (setq header-end (move-marker (make-marker) (point)))
  17199. (while t
  17200. (setq custom1 custom)
  17201. (when (eq rmheader t)
  17202. (goto-line 1)
  17203. (re-search-forward ":" nil t)
  17204. (delete-region (match-end 0) (point-at-eol))
  17205. (forward-char 1)
  17206. (looking-at "-+")
  17207. (delete-region (match-end 0) (point-at-eol))
  17208. (move-marker header-end (match-end 0)))
  17209. (goto-char header-end)
  17210. (delete-region (point) (point-max))
  17211. (while (setq entry (pop custom1))
  17212. (setq key (car entry) desc (nth 1 entry)
  17213. type (nth 2 entry) match (nth 3 entry))
  17214. (if (> (length key) 1)
  17215. (add-to-list 'prefixes (string-to-char key))
  17216. (insert
  17217. (format
  17218. "\n%-4s%-14s: %s"
  17219. (org-add-props (copy-sequence key)
  17220. '(face bold))
  17221. (cond
  17222. ((string-match "\\S-" desc) desc)
  17223. ((eq type 'agenda) "Agenda for current week or day")
  17224. ((eq type 'alltodo) "List of all TODO entries")
  17225. ((eq type 'stuck) "List of stuck projects")
  17226. ((eq type 'todo) "TODO keyword")
  17227. ((eq type 'tags) "Tags query")
  17228. ((eq type 'tags-todo) "Tags (TODO)")
  17229. ((eq type 'tags-tree) "Tags tree")
  17230. ((eq type 'todo-tree) "TODO kwd tree")
  17231. ((eq type 'occur-tree) "Occur tree")
  17232. ((functionp type) (if (symbolp type)
  17233. (symbol-name type)
  17234. "Lambda expression"))
  17235. (t "???"))
  17236. (cond
  17237. ((stringp match)
  17238. (org-add-props match nil 'face 'org-warning))
  17239. (match
  17240. (format "set of %d commands" (length match)))
  17241. (t ""))))))
  17242. (when prefixes
  17243. (mapc (lambda (x)
  17244. (insert
  17245. (format "\n%s %s"
  17246. (org-add-props (char-to-string x)
  17247. nil 'face 'bold)
  17248. (or (cdr (assoc (concat selstring (char-to-string x))
  17249. prefix-descriptions))
  17250. "Prefix key"))))
  17251. prefixes))
  17252. (goto-char (point-min))
  17253. (when (fboundp 'fit-window-to-buffer)
  17254. (if second-time
  17255. (if (not (pos-visible-in-window-p (point-max)))
  17256. (fit-window-to-buffer))
  17257. (setq second-time t)
  17258. (fit-window-to-buffer)))
  17259. (message "Press key for agenda command%s:"
  17260. (if restrict-ok
  17261. (if restriction
  17262. (format " (restricted to %s)" restriction)
  17263. " (unrestricted)")
  17264. ""))
  17265. (setq c (read-char-exclusive))
  17266. (message "")
  17267. (cond
  17268. ((assoc (char-to-string c) custom)
  17269. (setq selstring (concat selstring (char-to-string c)))
  17270. (throw 'exit (cons selstring restriction)))
  17271. ((memq c prefixes)
  17272. (setq selstring (concat selstring (char-to-string c))
  17273. prefixes nil
  17274. rmheader (or rmheader t)
  17275. custom (delq nil (mapcar
  17276. (lambda (x)
  17277. (if (or (= (length (car x)) 1)
  17278. (/= (string-to-char (car x)) c))
  17279. nil
  17280. (cons (substring (car x) 1) (cdr x))))
  17281. custom))))
  17282. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  17283. (message "Restriction is only possible in Org-mode buffers")
  17284. (ding) (sit-for 1))
  17285. ((eq c ?1)
  17286. (setq restriction 'buffer))
  17287. ((eq c ?0)
  17288. (setq restriction (if region-p 'region 'subtree)))
  17289. ((eq c ?<)
  17290. (setq restriction
  17291. (cond
  17292. ((eq restriction 'buffer)
  17293. (if region-p 'region 'subtree))
  17294. ((memq restriction '(subtree region))
  17295. nil)
  17296. (t 'buffer))))
  17297. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?/)))
  17298. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  17299. ((equal c ?q) (error "Abort"))
  17300. (t (error "Invalid key %c" c))))))))
  17301. (defun org-run-agenda-series (name series)
  17302. (org-prepare-agenda name)
  17303. (let* ((org-agenda-multi t)
  17304. (redo (list 'org-run-agenda-series name (list 'quote series)))
  17305. (cmds (car series))
  17306. (gprops (nth 1 series))
  17307. match ;; The byte compiler incorrectly complains about this. Keep it!
  17308. cmd type lprops)
  17309. (while (setq cmd (pop cmds))
  17310. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  17311. (cond
  17312. ((eq type 'agenda)
  17313. (org-let2 gprops lprops
  17314. '(call-interactively 'org-agenda-list)))
  17315. ((eq type 'alltodo)
  17316. (org-let2 gprops lprops
  17317. '(call-interactively 'org-todo-list)))
  17318. ((eq type 'stuck)
  17319. (org-let2 gprops lprops
  17320. '(call-interactively 'org-agenda-list-stuck-projects)))
  17321. ((eq type 'tags)
  17322. (org-let2 gprops lprops
  17323. '(org-tags-view current-prefix-arg match)))
  17324. ((eq type 'tags-todo)
  17325. (org-let2 gprops lprops
  17326. '(org-tags-view '(4) match)))
  17327. ((eq type 'todo)
  17328. (org-let2 gprops lprops
  17329. '(org-todo-list match)))
  17330. ((fboundp type)
  17331. (org-let2 gprops lprops
  17332. '(funcall type match)))
  17333. (t (error "Invalid type in command series"))))
  17334. (widen)
  17335. (setq org-agenda-redo-command redo)
  17336. (goto-char (point-min)))
  17337. (org-finalize-agenda))
  17338. ;;;###autoload
  17339. (defmacro org-batch-agenda (cmd-key &rest parameters)
  17340. "Run an agenda command in batch mode and send the result to STDOUT.
  17341. If CMD-KEY is a string of length 1, it is used as a key in
  17342. `org-agenda-custom-commands' and triggers this command. If it is a
  17343. longer string is is used as a tags/todo match string.
  17344. Paramters are alternating variable names and values that will be bound
  17345. before running the agenda command."
  17346. (let (pars)
  17347. (while parameters
  17348. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17349. (if (> (length cmd-key) 2)
  17350. (eval (list 'let (nreverse pars)
  17351. (list 'org-tags-view nil cmd-key)))
  17352. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17353. (set-buffer org-agenda-buffer-name)
  17354. (princ (org-encode-for-stdout (buffer-string)))))
  17355. (defun org-encode-for-stdout (string)
  17356. (if (fboundp 'encode-coding-string)
  17357. (encode-coding-string string buffer-file-coding-system)
  17358. string))
  17359. (defvar org-agenda-info nil)
  17360. ;;;###autoload
  17361. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  17362. "Run an agenda command in batch mode and send the result to STDOUT.
  17363. If CMD-KEY is a string of length 1, it is used as a key in
  17364. `org-agenda-custom-commands' and triggers this command. If it is a
  17365. longer string is is used as a tags/todo match string.
  17366. Paramters are alternating variable names and values that will be bound
  17367. before running the agenda command.
  17368. The output gives a line for each selected agenda item. Each
  17369. item is a list of comma-separated values, like this:
  17370. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  17371. category The category of the item
  17372. head The headline, without TODO kwd, TAGS and PRIORITY
  17373. type The type of the agenda entry, can be
  17374. todo selected in TODO match
  17375. tagsmatch selected in tags match
  17376. diary imported from diary
  17377. deadline a deadline on given date
  17378. scheduled scheduled on given date
  17379. timestamp entry has timestamp on given date
  17380. closed entry was closed on given date
  17381. upcoming-deadline warning about deadline
  17382. past-scheduled forwarded scheduled item
  17383. block entry has date block including g. date
  17384. todo The todo keyword, if any
  17385. tags All tags including inherited ones, separated by colons
  17386. date The relevant date, like 2007-2-14
  17387. time The time, like 15:00-16:50
  17388. extra Sting with extra planning info
  17389. priority-l The priority letter if any was given
  17390. priority-n The computed numerical priority
  17391. agenda-day The day in the agenda where this is listed"
  17392. (let (pars)
  17393. (while parameters
  17394. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17395. (push (list 'org-agenda-remove-tags t) pars)
  17396. (if (> (length cmd-key) 2)
  17397. (eval (list 'let (nreverse pars)
  17398. (list 'org-tags-view nil cmd-key)))
  17399. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17400. (set-buffer org-agenda-buffer-name)
  17401. (let* ((lines (org-split-string (buffer-string) "\n"))
  17402. line)
  17403. (while (setq line (pop lines))
  17404. (catch 'next
  17405. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  17406. (setq org-agenda-info
  17407. (org-fix-agenda-info (text-properties-at 0 line)))
  17408. (princ
  17409. (org-encode-for-stdout
  17410. (mapconcat 'org-agenda-export-csv-mapper
  17411. '(org-category txt type todo tags date time-of-day extra
  17412. priority-letter priority agenda-day)
  17413. ",")))
  17414. (princ "\n"))))))
  17415. (defun org-fix-agenda-info (props)
  17416. "Make sure all properties on an agenda item have a canonical form,
  17417. so the the export commands caneasily use it."
  17418. (let (tmp re)
  17419. (when (setq tmp (plist-get props 'tags))
  17420. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  17421. (when (setq tmp (plist-get props 'date))
  17422. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  17423. (let ((calendar-date-display-form '(year "-" month "-" day)))
  17424. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  17425. (setq tmp (calendar-date-string tmp)))
  17426. (setq props (plist-put props 'date tmp)))
  17427. (when (setq tmp (plist-get props 'day))
  17428. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  17429. (let ((calendar-date-display-form '(year "-" month "-" day)))
  17430. (setq tmp (calendar-date-string tmp)))
  17431. (setq props (plist-put props 'day tmp))
  17432. (setq props (plist-put props 'agenda-day tmp)))
  17433. (when (setq tmp (plist-get props 'txt))
  17434. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  17435. (plist-put props 'priority-letter (match-string 1 tmp))
  17436. (setq tmp (replace-match "" t t tmp)))
  17437. (when (and (setq re (plist-get props 'org-todo-regexp))
  17438. (setq re (concat "\\`\\.*" re " ?"))
  17439. (string-match re tmp))
  17440. (plist-put props 'todo (match-string 1 tmp))
  17441. (setq tmp (replace-match "" t t tmp)))
  17442. (plist-put props 'txt tmp)))
  17443. props)
  17444. (defun org-agenda-export-csv-mapper (prop)
  17445. (let ((res (plist-get org-agenda-info prop)))
  17446. (setq res
  17447. (cond
  17448. ((not res) "")
  17449. ((stringp res) res)
  17450. (t (prin1-to-string res))))
  17451. (while (string-match "," res)
  17452. (setq res (replace-match ";" t t res)))
  17453. (org-trim res)))
  17454. ;;;###autoload
  17455. (defun org-store-agenda-views (&rest parameters)
  17456. (interactive)
  17457. (eval (list 'org-batch-store-agenda-views)))
  17458. ;; FIXME, why is this a macro?????
  17459. ;;;###autoload
  17460. (defmacro org-batch-store-agenda-views (&rest parameters)
  17461. "Run all custom agenda commands that have a file argument."
  17462. (let ((cmds org-agenda-custom-commands)
  17463. (pop-up-frames nil)
  17464. (dir default-directory)
  17465. pars cmd thiscmdkey files opts)
  17466. (while parameters
  17467. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17468. (setq pars (reverse pars))
  17469. (save-window-excursion
  17470. (while cmds
  17471. (setq cmd (pop cmds)
  17472. thiscmdkey (car cmd)
  17473. opts (nth 3 cmd)
  17474. files (nth 4 cmd))
  17475. (if (stringp files) (setq files (list files)))
  17476. (when files
  17477. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  17478. (list 'org-agenda nil thiscmdkey)))
  17479. (set-buffer org-agenda-buffer-name)
  17480. (while files
  17481. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  17482. (list 'org-write-agenda
  17483. (expand-file-name (pop files) dir) t))))
  17484. (and (get-buffer org-agenda-buffer-name)
  17485. (kill-buffer org-agenda-buffer-name)))))))
  17486. (defun org-write-agenda (file &optional nosettings)
  17487. "Write the current buffer (an agenda view) as a file.
  17488. Depending on the extension of the file name, plain text (.txt),
  17489. HTML (.html or .htm) or Postscript (.ps) is produced.
  17490. If NOSETTINGS is given, do not scope the settings of
  17491. `org-agenda-exporter-settings' into the export commands. This is used when
  17492. the settings have already been scoped and we do not wish to overrule other,
  17493. higher priority settings."
  17494. (interactive "FWrite agenda to file: ")
  17495. (if (not (file-writable-p file))
  17496. (error "Cannot write agenda to file %s" file))
  17497. (cond
  17498. ((string-match "\\.html?\\'" file) (require 'htmlize))
  17499. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  17500. (org-let (if nosettings nil org-agenda-exporter-settings)
  17501. '(save-excursion
  17502. (save-window-excursion
  17503. (cond
  17504. ((string-match "\\.html?\\'" file)
  17505. (set-buffer (htmlize-buffer (current-buffer)))
  17506. (when (and org-agenda-export-html-style
  17507. (string-match "<style>" org-agenda-export-html-style))
  17508. ;; replace <style> section with org-agenda-export-html-style
  17509. (goto-char (point-min))
  17510. (kill-region (- (search-forward "<style") 6)
  17511. (search-forward "</style>"))
  17512. (insert org-agenda-export-html-style))
  17513. (write-file file)
  17514. (kill-buffer (current-buffer))
  17515. (message "HTML written to %s" file))
  17516. ((string-match "\\.ps\\'" file)
  17517. (ps-print-buffer-with-faces file)
  17518. (message "Postscript written to %s" file))
  17519. (t
  17520. (let ((bs (buffer-string)))
  17521. (find-file file)
  17522. (insert bs)
  17523. (save-buffer 0)
  17524. (kill-buffer (current-buffer))
  17525. (message "Plain text written to %s" file))))))
  17526. (set-buffer org-agenda-buffer-name)))
  17527. (defmacro org-no-read-only (&rest body)
  17528. "Inhibit read-only for BODY."
  17529. `(let ((inhibit-read-only t)) ,@body))
  17530. (defun org-check-for-org-mode ()
  17531. "Make sure current buffer is in org-mode. Error if not."
  17532. (or (org-mode-p)
  17533. (error "Cannot execute org-mode agenda command on buffer in %s."
  17534. major-mode)))
  17535. (defun org-fit-agenda-window ()
  17536. "Fit the window to the buffer size."
  17537. (and (memq org-agenda-window-setup '(reorganize-frame))
  17538. (fboundp 'fit-window-to-buffer)
  17539. (fit-window-to-buffer
  17540. nil
  17541. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  17542. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  17543. ;;; Agenda file list
  17544. (defun org-agenda-files (&optional unrestricted)
  17545. "Get the list of agenda files.
  17546. Optional UNRESTRICTED means return the full list even if a restriction
  17547. is currently in place."
  17548. (let ((files
  17549. (cond
  17550. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  17551. ((stringp org-agenda-files) (org-read-agenda-file-list))
  17552. ((listp org-agenda-files) org-agenda-files)
  17553. (t (error "Invalid value of `org-agenda-files'")))))
  17554. (setq files (apply 'append
  17555. (mapcar (lambda (f)
  17556. (if (file-directory-p f)
  17557. (directory-files f t "\\.org\\'")
  17558. (list f)))
  17559. files)))
  17560. (if org-agenda-skip-unavailable-files
  17561. (delq nil
  17562. (mapcar (function
  17563. (lambda (file)
  17564. (and (file-readable-p file) file)))
  17565. files))
  17566. files))) ; `org-check-agenda-file' will remove them from the list
  17567. (defun org-edit-agenda-file-list ()
  17568. "Edit the list of agenda files.
  17569. Depending on setup, this either uses customize to edit the variable
  17570. `org-agenda-files', or it visits the file that is holding the list. In the
  17571. latter case, the buffer is set up in a way that saving it automatically kills
  17572. the buffer and restores the previous window configuration."
  17573. (interactive)
  17574. (if (stringp org-agenda-files)
  17575. (let ((cw (current-window-configuration)))
  17576. (find-file org-agenda-files)
  17577. (org-set-local 'org-window-configuration cw)
  17578. (org-add-hook 'after-save-hook
  17579. (lambda ()
  17580. (set-window-configuration
  17581. (prog1 org-window-configuration
  17582. (kill-buffer (current-buffer))))
  17583. (org-install-agenda-files-menu)
  17584. (message "New agenda file list installed"))
  17585. nil 'local)
  17586. (message (substitute-command-keys
  17587. "Edit list and finish with \\[save-buffer]")))
  17588. (customize-variable 'org-agenda-files)))
  17589. (defun org-store-new-agenda-file-list (list)
  17590. "Set new value for the agenda file list and save it correcly."
  17591. (if (stringp org-agenda-files)
  17592. (let ((f org-agenda-files) b)
  17593. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  17594. (with-temp-file f
  17595. (insert (mapconcat 'identity list "\n") "\n")))
  17596. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  17597. (setq org-agenda-files list)
  17598. (customize-save-variable 'org-agenda-files org-agenda-files))))
  17599. (defun org-read-agenda-file-list ()
  17600. "Read the list of agenda files from a file."
  17601. (when (stringp org-agenda-files)
  17602. (with-temp-buffer
  17603. (insert-file-contents org-agenda-files)
  17604. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  17605. ;;;###autoload
  17606. (defun org-cycle-agenda-files ()
  17607. "Cycle through the files in `org-agenda-files'.
  17608. If the current buffer visits an agenda file, find the next one in the list.
  17609. If the current buffer does not, find the first agenda file."
  17610. (interactive)
  17611. (let* ((fs (org-agenda-files t))
  17612. (files (append fs (list (car fs))))
  17613. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  17614. file)
  17615. (unless files (error "No agenda files"))
  17616. (catch 'exit
  17617. (while (setq file (pop files))
  17618. (if (equal (file-truename file) tcf)
  17619. (when (car files)
  17620. (find-file (car files))
  17621. (throw 'exit t))))
  17622. (find-file (car fs)))
  17623. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  17624. (defun org-agenda-file-to-front (&optional to-end)
  17625. "Move/add the current file to the top of the agenda file list.
  17626. If the file is not present in the list, it is added to the front. If it is
  17627. present, it is moved there. With optional argument TO-END, add/move to the
  17628. end of the list."
  17629. (interactive "P")
  17630. (let ((org-agenda-skip-unavailable-files nil)
  17631. (file-alist (mapcar (lambda (x)
  17632. (cons (file-truename x) x))
  17633. (org-agenda-files t)))
  17634. (ctf (file-truename buffer-file-name))
  17635. x had)
  17636. (setq x (assoc ctf file-alist) had x)
  17637. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17638. (if to-end
  17639. (setq file-alist (append (delq x file-alist) (list x)))
  17640. (setq file-alist (cons x (delq x file-alist))))
  17641. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17642. (org-install-agenda-files-menu)
  17643. (message "File %s to %s of agenda file list"
  17644. (if had "moved" "added") (if to-end "end" "front"))))
  17645. (defun org-remove-file (&optional file)
  17646. "Remove current file from the list of files in variable `org-agenda-files'.
  17647. These are the files which are being checked for agenda entries.
  17648. Optional argument FILE means, use this file instead of the current."
  17649. (interactive)
  17650. (let* ((org-agenda-skip-unavailable-files nil)
  17651. (file (or file buffer-file-name))
  17652. (true-file (file-truename file))
  17653. (afile (abbreviate-file-name file))
  17654. (files (delq nil (mapcar
  17655. (lambda (x)
  17656. (if (equal true-file
  17657. (file-truename x))
  17658. nil x))
  17659. (org-agenda-files t)))))
  17660. (if (not (= (length files) (length (org-agenda-files t))))
  17661. (progn
  17662. (org-store-new-agenda-file-list files)
  17663. (org-install-agenda-files-menu)
  17664. (message "Removed file: %s" afile))
  17665. (message "File was not in list: %s" afile))))
  17666. (defun org-file-menu-entry (file)
  17667. (vector file (list 'find-file file) t))
  17668. (defun org-check-agenda-file (file)
  17669. "Make sure FILE exists. If not, ask user what to do."
  17670. (when (not (file-exists-p file))
  17671. (message "non-existent file %s. [R]emove from list or [A]bort?"
  17672. (abbreviate-file-name file))
  17673. (let ((r (downcase (read-char-exclusive))))
  17674. (cond
  17675. ((equal r ?r)
  17676. (org-remove-file file)
  17677. (throw 'nextfile t))
  17678. (t (error "Abort"))))))
  17679. ;;; Agenda prepare and finalize
  17680. (defvar org-agenda-multi nil) ; dynammically scoped
  17681. (defvar org-agenda-buffer-name "*Org Agenda*")
  17682. (defvar org-pre-agenda-window-conf nil)
  17683. (defvar org-agenda-name nil)
  17684. (defun org-prepare-agenda (&optional name)
  17685. (setq org-todo-keywords-for-agenda nil)
  17686. (setq org-done-keywords-for-agenda nil)
  17687. (if org-agenda-multi
  17688. (progn
  17689. (setq buffer-read-only nil)
  17690. (goto-char (point-max))
  17691. (unless (or (bobp) org-agenda-compact-blocks)
  17692. (insert "\n" (make-string (window-width) ?=) "\n"))
  17693. (narrow-to-region (point) (point-max)))
  17694. (org-agenda-maybe-reset-markers 'force)
  17695. (org-prepare-agenda-buffers (org-agenda-files))
  17696. (setq org-todo-keywords-for-agenda
  17697. (org-uniquify org-todo-keywords-for-agenda))
  17698. (setq org-done-keywords-for-agenda
  17699. (org-uniquify org-done-keywords-for-agenda))
  17700. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  17701. (awin (get-buffer-window abuf)))
  17702. (cond
  17703. ((equal (current-buffer) abuf) nil)
  17704. (awin (select-window awin))
  17705. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  17706. ((equal org-agenda-window-setup 'current-window)
  17707. (switch-to-buffer abuf))
  17708. ((equal org-agenda-window-setup 'other-window)
  17709. (org-switch-to-buffer-other-window abuf))
  17710. ((equal org-agenda-window-setup 'other-frame)
  17711. (switch-to-buffer-other-frame abuf))
  17712. ((equal org-agenda-window-setup 'reorganize-frame)
  17713. (delete-other-windows)
  17714. (org-switch-to-buffer-other-window abuf))))
  17715. (setq buffer-read-only nil)
  17716. (erase-buffer)
  17717. (org-agenda-mode)
  17718. (and name (not org-agenda-name)
  17719. (org-set-local 'org-agenda-name name)))
  17720. (setq buffer-read-only nil))
  17721. (defun org-finalize-agenda ()
  17722. "Finishing touch for the agenda buffer, called just before displaying it."
  17723. (unless org-agenda-multi
  17724. (save-excursion
  17725. (let ((inhibit-read-only t))
  17726. (goto-char (point-min))
  17727. (while (org-activate-bracket-links (point-max))
  17728. (add-text-properties (match-beginning 0) (match-end 0)
  17729. '(face org-link)))
  17730. (org-agenda-align-tags)
  17731. (unless org-agenda-with-colors
  17732. (remove-text-properties (point-min) (point-max) '(face nil))))
  17733. (if (and (boundp 'org-overriding-columns-format)
  17734. org-overriding-columns-format)
  17735. (org-set-local 'org-overriding-columns-format
  17736. org-overriding-columns-format))
  17737. (if (and (boundp 'org-agenda-view-columns-initially)
  17738. org-agenda-view-columns-initially)
  17739. (org-agenda-columns))
  17740. (when org-agenda-fontify-priorities
  17741. (org-fontify-priorities))
  17742. (run-hooks 'org-finalize-agenda-hook))))
  17743. (defun org-fontify-priorities ()
  17744. "Make highest priority lines bold, and lowest italic."
  17745. (interactive)
  17746. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  17747. (org-delete-overlay o)))
  17748. (org-overlays-in (point-min) (point-max)))
  17749. (save-excursion
  17750. (let ((inhibit-read-only t)
  17751. b e p ov h l)
  17752. (goto-char (point-min))
  17753. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  17754. (setq h (or (get-char-property (point) 'org-highest-priority)
  17755. org-highest-priority)
  17756. l (or (get-char-property (point) 'org-lowest-priority)
  17757. org-lowest-priority)
  17758. p (string-to-char (match-string 1))
  17759. b (match-beginning 0) e (point-at-eol)
  17760. ov (org-make-overlay b e))
  17761. (org-overlay-put
  17762. ov 'face
  17763. (cond ((listp org-agenda-fontify-priorities)
  17764. (cdr (assoc p org-agenda-fontify-priorities)))
  17765. ((equal p l) 'italic)
  17766. ((equal p h) 'bold)))
  17767. (org-overlay-put ov 'org-type 'org-priority)))))
  17768. (defun org-prepare-agenda-buffers (files)
  17769. "Create buffers for all agenda files, protect archived trees and comments."
  17770. (interactive)
  17771. (let ((pa '(:org-archived t))
  17772. (pc '(:org-comment t))
  17773. (pall '(:org-archived t :org-comment t))
  17774. (inhibit-read-only t)
  17775. (rea (concat ":" org-archive-tag ":"))
  17776. bmp file re)
  17777. (save-excursion
  17778. (save-restriction
  17779. (while (setq file (pop files))
  17780. (if (bufferp file)
  17781. (set-buffer file)
  17782. (org-check-agenda-file file)
  17783. (set-buffer (org-get-agenda-file-buffer file)))
  17784. (widen)
  17785. (setq bmp (buffer-modified-p))
  17786. (org-refresh-category-properties)
  17787. (setq org-todo-keywords-for-agenda
  17788. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17789. (setq org-done-keywords-for-agenda
  17790. (append org-done-keywords-for-agenda org-done-keywords))
  17791. (save-excursion
  17792. (remove-text-properties (point-min) (point-max) pall)
  17793. (when org-agenda-skip-archived-trees
  17794. (goto-char (point-min))
  17795. (while (re-search-forward rea nil t)
  17796. (if (org-on-heading-p t)
  17797. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17798. (goto-char (point-min))
  17799. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  17800. (while (re-search-forward re nil t)
  17801. (add-text-properties
  17802. (match-beginning 0) (org-end-of-subtree t) pc)))
  17803. (set-buffer-modified-p bmp))))))
  17804. (defvar org-agenda-skip-function nil
  17805. "Function to be called at each match during agenda construction.
  17806. If this function returns nil, the current match should not be skipped.
  17807. Otherwise, the function must return a position from where the search
  17808. should be continued.
  17809. This may also be a Lisp form, it will be evaluated.
  17810. Never set this variable using `setq' or so, because then it will apply
  17811. to all future agenda commands. Instead, bind it with `let' to scope
  17812. it dynamically into the agenda-constructing command. A good way to set
  17813. it is through options in org-agenda-custom-commands.")
  17814. (defun org-agenda-skip ()
  17815. "Throw to `:skip' in places that should be skipped.
  17816. Also moves point to the end of the skipped region, so that search can
  17817. continue from there."
  17818. (let ((p (point-at-bol)) to fp)
  17819. (and org-agenda-skip-archived-trees
  17820. (get-text-property p :org-archived)
  17821. (org-end-of-subtree t)
  17822. (throw :skip t))
  17823. (and (get-text-property p :org-comment)
  17824. (org-end-of-subtree t)
  17825. (throw :skip t))
  17826. (if (equal (char-after p) ?#) (throw :skip t))
  17827. (when (and (or (setq fp (functionp org-agenda-skip-function))
  17828. (consp org-agenda-skip-function))
  17829. (setq to (save-excursion
  17830. (save-match-data
  17831. (if fp
  17832. (funcall org-agenda-skip-function)
  17833. (eval org-agenda-skip-function))))))
  17834. (goto-char to)
  17835. (throw :skip t))))
  17836. (defvar org-agenda-markers nil
  17837. "List of all currently active markers created by `org-agenda'.")
  17838. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  17839. "Creation time of the last agenda marker.")
  17840. (defun org-agenda-new-marker (&optional pos)
  17841. "Return a new agenda marker.
  17842. Org-mode keeps a list of these markers and resets them when they are
  17843. no longer in use."
  17844. (let ((m (copy-marker (or pos (point)))))
  17845. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  17846. (push m org-agenda-markers)
  17847. m))
  17848. (defun org-agenda-maybe-reset-markers (&optional force)
  17849. "Reset markers created by `org-agenda'. But only if they are old enough."
  17850. (if (or (and force (not org-agenda-multi))
  17851. (> (- (time-to-seconds (current-time))
  17852. org-agenda-last-marker-time)
  17853. 5))
  17854. (while org-agenda-markers
  17855. (move-marker (pop org-agenda-markers) nil))))
  17856. (defvar org-agenda-new-buffers nil
  17857. "Buffers created to visit agenda files.")
  17858. (defun org-get-agenda-file-buffer (file)
  17859. "Get a buffer visiting FILE. If the buffer needs to be created, add
  17860. it to the list of buffers which might be released later."
  17861. (let ((buf (org-find-base-buffer-visiting file)))
  17862. (if buf
  17863. buf ; just return it
  17864. ;; Make a new buffer and remember it
  17865. (setq buf (find-file-noselect file))
  17866. (if buf (push buf org-agenda-new-buffers))
  17867. buf)))
  17868. (defun org-release-buffers (blist)
  17869. "Release all buffers in list, asking the user for confirmation when needed.
  17870. When a buffer is unmodified, it is just killed. When modified, it is saved
  17871. \(if the user agrees) and then killed."
  17872. (let (buf file)
  17873. (while (setq buf (pop blist))
  17874. (setq file (buffer-file-name buf))
  17875. (when (and (buffer-modified-p buf)
  17876. file
  17877. (y-or-n-p (format "Save file %s? " file)))
  17878. (with-current-buffer buf (save-buffer)))
  17879. (kill-buffer buf))))
  17880. (defun org-get-category (&optional pos)
  17881. "Get the category applying to position POS."
  17882. (get-text-property (or pos (point)) 'org-category))
  17883. ;;; Agenda timeline
  17884. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  17885. (defun org-timeline (&optional include-all)
  17886. "Show a time-sorted view of the entries in the current org file.
  17887. Only entries with a time stamp of today or later will be listed. With
  17888. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  17889. under the current date.
  17890. If the buffer contains an active region, only check the region for
  17891. dates."
  17892. (interactive "P")
  17893. (require 'calendar)
  17894. (org-compile-prefix-format 'timeline)
  17895. (org-set-sorting-strategy 'timeline)
  17896. (let* ((dopast t)
  17897. (dotodo include-all)
  17898. (doclosed org-agenda-show-log)
  17899. (entry buffer-file-name)
  17900. (date (calendar-current-date))
  17901. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  17902. (end (if (org-region-active-p) (region-end) (point-max)))
  17903. (day-numbers (org-get-all-dates beg end 'no-ranges
  17904. t doclosed ; always include today
  17905. org-timeline-show-empty-dates))
  17906. (org-deadline-warning-days 0)
  17907. (org-agenda-only-exact-dates t)
  17908. (today (time-to-days (current-time)))
  17909. (past t)
  17910. args
  17911. s e rtn d emptyp)
  17912. (setq org-agenda-redo-command
  17913. (list 'progn
  17914. (list 'org-switch-to-buffer-other-window (current-buffer))
  17915. (list 'org-timeline (list 'quote include-all))))
  17916. (if (not dopast)
  17917. ;; Remove past dates from the list of dates.
  17918. (setq day-numbers (delq nil (mapcar (lambda(x)
  17919. (if (>= x today) x nil))
  17920. day-numbers))))
  17921. (org-prepare-agenda (concat "Timeline "
  17922. (file-name-nondirectory buffer-file-name)))
  17923. (if doclosed (push :closed args))
  17924. (push :timestamp args)
  17925. (push :deadline args)
  17926. (push :scheduled args)
  17927. (push :sexp args)
  17928. (if dotodo (push :todo args))
  17929. (while (setq d (pop day-numbers))
  17930. (if (and (listp d) (eq (car d) :omitted))
  17931. (progn
  17932. (setq s (point))
  17933. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  17934. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  17935. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  17936. (if (and (>= d today)
  17937. dopast
  17938. past)
  17939. (progn
  17940. (setq past nil)
  17941. (insert (make-string 79 ?-) "\n")))
  17942. (setq date (calendar-gregorian-from-absolute d))
  17943. (setq s (point))
  17944. (setq rtn (and (not emptyp)
  17945. (apply 'org-agenda-get-day-entries entry
  17946. date args)))
  17947. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  17948. (progn
  17949. (insert
  17950. (if (stringp org-agenda-format-date)
  17951. (format-time-string org-agenda-format-date
  17952. (org-time-from-absolute date))
  17953. (funcall org-agenda-format-date date))
  17954. "\n")
  17955. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  17956. (put-text-property s (1- (point)) 'org-date-line t)
  17957. (if (equal d today)
  17958. (put-text-property s (1- (point)) 'org-today t))
  17959. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  17960. (put-text-property s (1- (point)) 'day d)))))
  17961. (goto-char (point-min))
  17962. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  17963. (point-min)))
  17964. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  17965. (org-finalize-agenda)
  17966. (setq buffer-read-only t)))
  17967. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  17968. "Return a list of all relevant day numbers from BEG to END buffer positions.
  17969. If NO-RANGES is non-nil, include only the start and end dates of a range,
  17970. not every single day in the range. If FORCE-TODAY is non-nil, make
  17971. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  17972. inactive time stamps (those in square brackets) are included.
  17973. When EMPTY is non-nil, also include days without any entries."
  17974. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  17975. dates dates1 date day day1 day2 ts1 ts2)
  17976. (if force-today
  17977. (setq dates (list (time-to-days (current-time)))))
  17978. (save-excursion
  17979. (goto-char beg)
  17980. (while (re-search-forward re end t)
  17981. (setq day (time-to-days (org-time-string-to-time
  17982. (substring (match-string 1) 0 10))))
  17983. (or (memq day dates) (push day dates)))
  17984. (unless no-ranges
  17985. (goto-char beg)
  17986. (while (re-search-forward org-tr-regexp end t)
  17987. (setq ts1 (substring (match-string 1) 0 10)
  17988. ts2 (substring (match-string 2) 0 10)
  17989. day1 (time-to-days (org-time-string-to-time ts1))
  17990. day2 (time-to-days (org-time-string-to-time ts2)))
  17991. (while (< (setq day1 (1+ day1)) day2)
  17992. (or (memq day1 dates) (push day1 dates)))))
  17993. (setq dates (sort dates '<))
  17994. (when empty
  17995. (while (setq day (pop dates))
  17996. (setq day2 (car dates))
  17997. (push day dates1)
  17998. (when (and day2 empty)
  17999. (if (or (eq empty t)
  18000. (and (numberp empty) (<= (- day2 day) empty)))
  18001. (while (< (setq day (1+ day)) day2)
  18002. (push (list day) dates1))
  18003. (push (cons :omitted (- day2 day)) dates1))))
  18004. (setq dates (nreverse dates1)))
  18005. dates)))
  18006. ;;; Agenda Daily/Weekly
  18007. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18008. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18009. (defvar org-agenda-last-arguments nil
  18010. "The arguments of the previous call to org-agenda")
  18011. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18012. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18013. (defvar org-include-all-loc nil) ; local variable
  18014. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18015. ;;;###autoload
  18016. (defun org-agenda-list (&optional include-all start-day ndays)
  18017. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18018. The view will be for the current day or week, but from the overview buffer
  18019. you will be able to go to other days/weeks.
  18020. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18021. all unfinished TODO items will also be shown, before the agenda.
  18022. This feature is considered obsolete, please use the TODO list or a block
  18023. agenda instead.
  18024. With a numeric prefix argument in an interactive call, the agenda will
  18025. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18026. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18027. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18028. given in `org-agenda-start-on-weekday'."
  18029. (interactive "P")
  18030. (if (and (integerp include-all) (> include-all 0))
  18031. (setq ndays include-all include-all nil))
  18032. (setq ndays (or ndays org-agenda-ndays)
  18033. start-day (or start-day org-agenda-start-day))
  18034. (if org-agenda-overriding-arguments
  18035. (setq include-all (car org-agenda-overriding-arguments)
  18036. start-day (nth 1 org-agenda-overriding-arguments)
  18037. ndays (nth 2 org-agenda-overriding-arguments)))
  18038. (if (stringp start-day)
  18039. ;; Convert to an absolute day number
  18040. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18041. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18042. (org-compile-prefix-format 'agenda)
  18043. (org-set-sorting-strategy 'agenda)
  18044. (require 'calendar)
  18045. (let* ((org-agenda-start-on-weekday
  18046. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18047. org-agenda-start-on-weekday nil))
  18048. (thefiles (org-agenda-files))
  18049. (files thefiles)
  18050. (today (time-to-days
  18051. (time-subtract (current-time)
  18052. (list 0 (* 3600 org-extend-today-until) 0))))
  18053. (sd (or start-day today))
  18054. (start (if (or (null org-agenda-start-on-weekday)
  18055. (< org-agenda-ndays 7))
  18056. sd
  18057. (let* ((nt (calendar-day-of-week
  18058. (calendar-gregorian-from-absolute sd)))
  18059. (n1 org-agenda-start-on-weekday)
  18060. (d (- nt n1)))
  18061. (- sd (+ (if (< d 0) 7 0) d)))))
  18062. (day-numbers (list start))
  18063. (day-cnt 0)
  18064. (inhibit-redisplay (not debug-on-error))
  18065. s e rtn rtnall file date d start-pos end-pos todayp nd)
  18066. (setq org-agenda-redo-command
  18067. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  18068. ;; Make the list of days
  18069. (setq ndays (or ndays org-agenda-ndays)
  18070. nd ndays)
  18071. (while (> ndays 1)
  18072. (push (1+ (car day-numbers)) day-numbers)
  18073. (setq ndays (1- ndays)))
  18074. (setq day-numbers (nreverse day-numbers))
  18075. (org-prepare-agenda "Day/Week")
  18076. (org-set-local 'org-starting-day (car day-numbers))
  18077. (org-set-local 'org-include-all-loc include-all)
  18078. (org-set-local 'org-agenda-span
  18079. (org-agenda-ndays-to-span nd))
  18080. (when (and (or include-all org-agenda-include-all-todo)
  18081. (member today day-numbers))
  18082. (setq files thefiles
  18083. rtnall nil)
  18084. (while (setq file (pop files))
  18085. (catch 'nextfile
  18086. (org-check-agenda-file file)
  18087. (setq date (calendar-gregorian-from-absolute today)
  18088. rtn (org-agenda-get-day-entries
  18089. file date :todo))
  18090. (setq rtnall (append rtnall rtn))))
  18091. (when rtnall
  18092. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  18093. (add-text-properties (point-min) (1- (point))
  18094. (list 'face 'org-agenda-structure))
  18095. (insert (org-finalize-agenda-entries rtnall) "\n")))
  18096. (unless org-agenda-compact-blocks
  18097. (setq s (point))
  18098. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  18099. "-agenda:\n")
  18100. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  18101. 'org-date-line t)))
  18102. (while (setq d (pop day-numbers))
  18103. (setq date (calendar-gregorian-from-absolute d)
  18104. s (point))
  18105. (if (or (setq todayp (= d today))
  18106. (and (not start-pos) (= d sd)))
  18107. (setq start-pos (point))
  18108. (if (and start-pos (not end-pos))
  18109. (setq end-pos (point))))
  18110. (setq files thefiles
  18111. rtnall nil)
  18112. (while (setq file (pop files))
  18113. (catch 'nextfile
  18114. (org-check-agenda-file file)
  18115. (if org-agenda-show-log
  18116. (setq rtn (org-agenda-get-day-entries
  18117. file date
  18118. :deadline :scheduled :timestamp :sexp :closed))
  18119. (setq rtn (org-agenda-get-day-entries
  18120. file date
  18121. :deadline :scheduled :sexp :timestamp)))
  18122. (setq rtnall (append rtnall rtn))))
  18123. (if org-agenda-include-diary
  18124. (progn
  18125. (require 'diary-lib)
  18126. (setq rtn (org-get-entries-from-diary date))
  18127. (setq rtnall (append rtnall rtn))))
  18128. (if (or rtnall org-agenda-show-all-dates)
  18129. (progn
  18130. (setq day-cnt (1+ day-cnt))
  18131. (insert
  18132. (if (stringp org-agenda-format-date)
  18133. (format-time-string org-agenda-format-date
  18134. (org-time-from-absolute date))
  18135. (funcall org-agenda-format-date date))
  18136. "\n")
  18137. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18138. (put-text-property s (1- (point)) 'org-date-line t)
  18139. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  18140. (if todayp (put-text-property s (1- (point)) 'org-today t))
  18141. (if rtnall (insert
  18142. (org-finalize-agenda-entries
  18143. (org-agenda-add-time-grid-maybe
  18144. rtnall nd todayp))
  18145. "\n"))
  18146. (put-text-property s (1- (point)) 'day d)
  18147. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  18148. (goto-char (point-min))
  18149. (org-fit-agenda-window)
  18150. (unless (and (pos-visible-in-window-p (point-min))
  18151. (pos-visible-in-window-p (point-max)))
  18152. (goto-char (1- (point-max)))
  18153. (recenter -1)
  18154. (if (not (pos-visible-in-window-p (or start-pos 1)))
  18155. (progn
  18156. (goto-char (or start-pos 1))
  18157. (recenter 1))))
  18158. (goto-char (or start-pos 1))
  18159. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  18160. (org-finalize-agenda)
  18161. (setq buffer-read-only t)
  18162. (message "")))
  18163. (defun org-agenda-ndays-to-span (n)
  18164. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  18165. ;;; Agenda TODO list
  18166. (defvar org-select-this-todo-keyword nil)
  18167. (defvar org-last-arg nil)
  18168. ;;;###autoload
  18169. (defun org-todo-list (arg)
  18170. "Show all TODO entries from all agenda file in a single list.
  18171. The prefix arg can be used to select a specific TODO keyword and limit
  18172. the list to these. When using \\[universal-argument], you will be prompted
  18173. for a keyword. A numeric prefix directly selects the Nth keyword in
  18174. `org-todo-keywords-1'."
  18175. (interactive "P")
  18176. (require 'calendar)
  18177. (org-compile-prefix-format 'todo)
  18178. (org-set-sorting-strategy 'todo)
  18179. (org-prepare-agenda "TODO")
  18180. (let* ((today (time-to-days (current-time)))
  18181. (date (calendar-gregorian-from-absolute today))
  18182. (kwds org-todo-keywords-for-agenda)
  18183. (completion-ignore-case t)
  18184. (org-select-this-todo-keyword
  18185. (if (stringp arg) arg
  18186. (and arg (integerp arg) (> arg 0)
  18187. (nth (1- arg) kwds))))
  18188. rtn rtnall files file pos)
  18189. (when (equal arg '(4))
  18190. (setq org-select-this-todo-keyword
  18191. (completing-read "Keyword (or KWD1|K2D2|...): "
  18192. (mapcar 'list kwds) nil nil)))
  18193. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  18194. (org-set-local 'org-last-arg arg)
  18195. (setq org-agenda-redo-command
  18196. '(org-todo-list (or current-prefix-arg org-last-arg)))
  18197. (setq files (org-agenda-files)
  18198. rtnall nil)
  18199. (while (setq file (pop files))
  18200. (catch 'nextfile
  18201. (org-check-agenda-file file)
  18202. (setq rtn (org-agenda-get-day-entries file date :todo))
  18203. (setq rtnall (append rtnall rtn))))
  18204. (if org-agenda-overriding-header
  18205. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18206. nil 'face 'org-agenda-structure) "\n")
  18207. (insert "Global list of TODO items of type: ")
  18208. (add-text-properties (point-min) (1- (point))
  18209. (list 'face 'org-agenda-structure))
  18210. (setq pos (point))
  18211. (insert (or org-select-this-todo-keyword "ALL") "\n")
  18212. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  18213. (setq pos (point))
  18214. (unless org-agenda-multi
  18215. (insert "Available with `N r': (0)ALL")
  18216. (let ((n 0) s)
  18217. (mapc (lambda (x)
  18218. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  18219. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  18220. (insert "\n "))
  18221. (insert " " s))
  18222. kwds))
  18223. (insert "\n"))
  18224. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  18225. (when rtnall
  18226. (insert (org-finalize-agenda-entries rtnall) "\n"))
  18227. (goto-char (point-min))
  18228. (org-fit-agenda-window)
  18229. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  18230. (org-finalize-agenda)
  18231. (setq buffer-read-only t)))
  18232. ;;; Agenda tags match
  18233. ;;;###autoload
  18234. (defun org-tags-view (&optional todo-only match)
  18235. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  18236. The prefix arg TODO-ONLY limits the search to TODO entries."
  18237. (interactive "P")
  18238. (org-compile-prefix-format 'tags)
  18239. (org-set-sorting-strategy 'tags)
  18240. (let* ((org-tags-match-list-sublevels
  18241. (if todo-only t org-tags-match-list-sublevels))
  18242. (completion-ignore-case t)
  18243. rtn rtnall files file pos matcher
  18244. buffer)
  18245. (setq matcher (org-make-tags-matcher match)
  18246. match (car matcher) matcher (cdr matcher))
  18247. (org-prepare-agenda (concat "TAGS " match))
  18248. (setq org-agenda-redo-command
  18249. (list 'org-tags-view (list 'quote todo-only)
  18250. (list 'if 'current-prefix-arg nil match)))
  18251. (setq files (org-agenda-files)
  18252. rtnall nil)
  18253. (while (setq file (pop files))
  18254. (catch 'nextfile
  18255. (org-check-agenda-file file)
  18256. (setq buffer (if (file-exists-p file)
  18257. (org-get-agenda-file-buffer file)
  18258. (error "No such file %s" file)))
  18259. (if (not buffer)
  18260. ;; If file does not exist, merror message to agenda
  18261. (setq rtn (list
  18262. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  18263. rtnall (append rtnall rtn))
  18264. (with-current-buffer buffer
  18265. (unless (org-mode-p)
  18266. (error "Agenda file %s is not in `org-mode'" file))
  18267. (save-excursion
  18268. (save-restriction
  18269. (if org-agenda-restrict
  18270. (narrow-to-region org-agenda-restrict-begin
  18271. org-agenda-restrict-end)
  18272. (widen))
  18273. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  18274. (setq rtnall (append rtnall rtn))))))))
  18275. (if org-agenda-overriding-header
  18276. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18277. nil 'face 'org-agenda-structure) "\n")
  18278. (insert "Headlines with TAGS match: ")
  18279. (add-text-properties (point-min) (1- (point))
  18280. (list 'face 'org-agenda-structure))
  18281. (setq pos (point))
  18282. (insert match "\n")
  18283. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  18284. (setq pos (point))
  18285. (unless org-agenda-multi
  18286. (insert "Press `C-u r' to search again with new search string\n"))
  18287. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  18288. (when rtnall
  18289. (insert (org-finalize-agenda-entries rtnall) "\n"))
  18290. (goto-char (point-min))
  18291. (org-fit-agenda-window)
  18292. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  18293. (org-finalize-agenda)
  18294. (setq buffer-read-only t)))
  18295. ;;; Agenda Finding stuck projects
  18296. (defvar org-agenda-skip-regexp nil
  18297. "Regular expression used in skipping subtrees for the agenda.
  18298. This is basically a temporary global variable that can be set and then
  18299. used by user-defined selections using `org-agenda-skip-function'.")
  18300. (defvar org-agenda-overriding-header nil
  18301. "When this is set during todo and tags searches, will replace header.")
  18302. (defun org-agenda-skip-subtree-when-regexp-matches ()
  18303. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  18304. If yes, it returns the end position of this tree, causing agenda commands
  18305. to skip this subtree. This is a function that can be put into
  18306. `org-agenda-skip-function' for the duration of a command."
  18307. (let ((end (save-excursion (org-end-of-subtree t)))
  18308. skip)
  18309. (save-excursion
  18310. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  18311. (and skip end)))
  18312. (defun org-agenda-skip-entry-if (&rest conditions)
  18313. "Skip entry if any of CONDITIONS is true.
  18314. See `org-agenda-skip-if for details."
  18315. (org-agenda-skip-if nil conditions))
  18316. (defun org-agenda-skip-subtree-if (&rest conditions)
  18317. "Skip entry if any of CONDITIONS is true.
  18318. See `org-agenda-skip-if for details."
  18319. (org-agenda-skip-if t conditions))
  18320. (defun org-agenda-skip-if (subtree conditions)
  18321. "Checks current entity for CONDITIONS.
  18322. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  18323. the entry, i.e. the text before the next heading is checked.
  18324. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  18325. from different tests. Valid conditions are:
  18326. scheduled Check if there is a scheduled cookie
  18327. notscheduled Check if there is no scheduled cookie
  18328. deadline Check if there is a deadline
  18329. notdeadline Check if there is no deadline
  18330. regexp Check if regexp matches
  18331. notregexp Check if regexp does not match.
  18332. The regexp is taken from the conditions list, it must com right after the
  18333. `regexp' of `notregexp' element.
  18334. If any of these conditions is met, this function returns the end point of
  18335. the entity, causing the search to continue from there. This is a function
  18336. that can be put into `org-agenda-skip-function' for the duration of a command."
  18337. (let (beg end m)
  18338. (org-back-to-heading t)
  18339. (setq beg (point)
  18340. end (if subtree
  18341. (progn (org-end-of-subtree t) (point))
  18342. (progn (outline-next-heading) (1- (point)))))
  18343. (goto-char beg)
  18344. (and
  18345. (or
  18346. (and (memq 'scheduled conditions)
  18347. (re-search-forward org-scheduled-time-regexp end t))
  18348. (and (memq 'notscheduled conditions)
  18349. (not (re-search-forward org-scheduled-time-regexp end t)))
  18350. (and (memq 'deadline conditions)
  18351. (re-search-forward org-deadline-time-regexp end t))
  18352. (and (memq 'notdeadline conditions)
  18353. (not (re-search-forward org-deadline-time-regexp end t)))
  18354. (and (setq m (memq 'regexp conditions))
  18355. (stringp (nth 1 m))
  18356. (re-search-forward (nth 1 m) end t))
  18357. (and (setq m (memq 'notregexp conditions))
  18358. (stringp (nth 1 m))
  18359. (not (re-search-forward (nth 1 m) end t))))
  18360. end)))
  18361. ;;;###autoload
  18362. (defun org-agenda-list-stuck-projects (&rest ignore)
  18363. "Create agenda view for projects that are stuck.
  18364. Stuck projects are project that have no next actions. For the definitions
  18365. of what a project is and how to check if it stuck, customize the variable
  18366. `org-stuck-projects'.
  18367. MATCH is being ignored."
  18368. (interactive)
  18369. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  18370. ;; FIXME: we could have used org-agenda-skip-if here.
  18371. (org-agenda-overriding-header "List of stuck projects: ")
  18372. (matcher (nth 0 org-stuck-projects))
  18373. (todo (nth 1 org-stuck-projects))
  18374. (todo-wds (if (member "*" todo)
  18375. (progn
  18376. (org-prepare-agenda-buffers (org-agenda-files))
  18377. (org-delete-all
  18378. org-done-keywords-for-agenda
  18379. (copy-sequence org-todo-keywords-for-agenda)))
  18380. todo))
  18381. (todo-re (concat "^\\*+[ \t]+\\("
  18382. (mapconcat 'identity todo-wds "\\|")
  18383. "\\)\\>"))
  18384. (tags (nth 2 org-stuck-projects))
  18385. (tags-re (if (member "*" tags)
  18386. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  18387. (concat "^\\*+ .*:\\("
  18388. (mapconcat 'identity tags "\\|")
  18389. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  18390. (gen-re (nth 3 org-stuck-projects))
  18391. (re-list
  18392. (delq nil
  18393. (list
  18394. (if todo todo-re)
  18395. (if tags tags-re)
  18396. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  18397. gen-re)))))
  18398. (setq org-agenda-skip-regexp
  18399. (if re-list
  18400. (mapconcat 'identity re-list "\\|")
  18401. (error "No information how to identify unstuck projects")))
  18402. (org-tags-view nil matcher)
  18403. (with-current-buffer org-agenda-buffer-name
  18404. (setq org-agenda-redo-command
  18405. '(org-agenda-list-stuck-projects
  18406. (or current-prefix-arg org-last-arg))))))
  18407. ;;; Diary integration
  18408. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  18409. (defun org-get-entries-from-diary (date)
  18410. "Get the (Emacs Calendar) diary entries for DATE."
  18411. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  18412. (diary-display-hook '(fancy-diary-display))
  18413. (pop-up-frames nil)
  18414. (list-diary-entries-hook
  18415. (cons 'org-diary-default-entry list-diary-entries-hook))
  18416. (diary-file-name-prefix-function nil) ; turn this feature off
  18417. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  18418. entries
  18419. (org-disable-agenda-to-diary t))
  18420. (save-excursion
  18421. (save-window-excursion
  18422. (funcall (if (fboundp 'diary-list-entries)
  18423. 'diary-list-entries 'list-diary-entries)
  18424. date 1)))
  18425. (if (not (get-buffer fancy-diary-buffer))
  18426. (setq entries nil)
  18427. (with-current-buffer fancy-diary-buffer
  18428. (setq buffer-read-only nil)
  18429. (if (zerop (buffer-size))
  18430. ;; No entries
  18431. (setq entries nil)
  18432. ;; Omit the date and other unnecessary stuff
  18433. (org-agenda-cleanup-fancy-diary)
  18434. ;; Add prefix to each line and extend the text properties
  18435. (if (zerop (buffer-size))
  18436. (setq entries nil)
  18437. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  18438. (set-buffer-modified-p nil)
  18439. (kill-buffer fancy-diary-buffer)))
  18440. (when entries
  18441. (setq entries (org-split-string entries "\n"))
  18442. (setq entries
  18443. (mapcar
  18444. (lambda (x)
  18445. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  18446. ;; Extend the text properties to the beginning of the line
  18447. (org-add-props x (text-properties-at (1- (length x)) x)
  18448. 'type "diary" 'date date))
  18449. entries)))))
  18450. (defun org-agenda-cleanup-fancy-diary ()
  18451. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  18452. This gets rid of the date, the underline under the date, and
  18453. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  18454. date. It also removes lines that contain only whitespace."
  18455. (goto-char (point-min))
  18456. (if (looking-at ".*?:[ \t]*")
  18457. (progn
  18458. (replace-match "")
  18459. (re-search-forward "\n=+$" nil t)
  18460. (replace-match "")
  18461. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  18462. (re-search-forward "\n=+$" nil t)
  18463. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  18464. (goto-char (point-min))
  18465. (while (re-search-forward "^ +\n" nil t)
  18466. (replace-match ""))
  18467. (goto-char (point-min))
  18468. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  18469. (replace-match "")))
  18470. ;; Make sure entries from the diary have the right text properties.
  18471. (eval-after-load "diary-lib"
  18472. '(if (boundp 'diary-modify-entry-list-string-function)
  18473. ;; We can rely on the hook, nothing to do
  18474. nil
  18475. ;; Hook not avaiable, must use advice to make this work
  18476. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  18477. "Make the position visible."
  18478. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  18479. (stringp string)
  18480. buffer-file-name)
  18481. (setq string (org-modify-diary-entry-string string))))))
  18482. (defun org-modify-diary-entry-string (string)
  18483. "Add text properties to string, allowing org-mode to act on it."
  18484. (org-add-props string nil
  18485. 'mouse-face 'highlight
  18486. 'keymap org-agenda-keymap
  18487. 'help-echo (if buffer-file-name
  18488. (format "mouse-2 or RET jump to diary file %s"
  18489. (abbreviate-file-name buffer-file-name))
  18490. "")
  18491. 'org-agenda-diary-link t
  18492. 'org-marker (org-agenda-new-marker (point-at-bol))))
  18493. (defun org-diary-default-entry ()
  18494. "Add a dummy entry to the diary.
  18495. Needed to avoid empty dates which mess up holiday display."
  18496. ;; Catch the error if dealing with the new add-to-diary-alist
  18497. (when org-disable-agenda-to-diary
  18498. (condition-case nil
  18499. (add-to-diary-list original-date "Org-mode dummy" "")
  18500. (error
  18501. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  18502. ;;;###autoload
  18503. (defun org-diary (&rest args)
  18504. "Return diary information from org-files.
  18505. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  18506. It accesses org files and extracts information from those files to be
  18507. listed in the diary. The function accepts arguments specifying what
  18508. items should be listed. The following arguments are allowed:
  18509. :timestamp List the headlines of items containing a date stamp or
  18510. date range matching the selected date. Deadlines will
  18511. also be listed, on the expiration day.
  18512. :sexp List entries resulting from diary-like sexps.
  18513. :deadline List any deadlines past due, or due within
  18514. `org-deadline-warning-days'. The listing occurs only
  18515. in the diary for *today*, not at any other date. If
  18516. an entry is marked DONE, it is no longer listed.
  18517. :scheduled List all items which are scheduled for the given date.
  18518. The diary for *today* also contains items which were
  18519. scheduled earlier and are not yet marked DONE.
  18520. :todo List all TODO items from the org-file. This may be a
  18521. long list - so this is not turned on by default.
  18522. Like deadlines, these entries only show up in the
  18523. diary for *today*, not at any other date.
  18524. The call in the diary file should look like this:
  18525. &%%(org-diary) ~/path/to/some/orgfile.org
  18526. Use a separate line for each org file to check. Or, if you omit the file name,
  18527. all files listed in `org-agenda-files' will be checked automatically:
  18528. &%%(org-diary)
  18529. If you don't give any arguments (as in the example above), the default
  18530. arguments (:deadline :scheduled :timestamp :sexp) are used.
  18531. So the example above may also be written as
  18532. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  18533. The function expects the lisp variables `entry' and `date' to be provided
  18534. by the caller, because this is how the calendar works. Don't use this
  18535. function from a program - use `org-agenda-get-day-entries' instead."
  18536. (org-agenda-maybe-reset-markers)
  18537. (org-compile-prefix-format 'agenda)
  18538. (org-set-sorting-strategy 'agenda)
  18539. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  18540. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  18541. (list entry)
  18542. (org-agenda-files t)))
  18543. file rtn results)
  18544. (org-prepare-agenda-buffers files)
  18545. ;; If this is called during org-agenda, don't return any entries to
  18546. ;; the calendar. Org Agenda will list these entries itself.
  18547. (if org-disable-agenda-to-diary (setq files nil))
  18548. (while (setq file (pop files))
  18549. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  18550. (setq results (append results rtn)))
  18551. (if results
  18552. (concat (org-finalize-agenda-entries results) "\n"))))
  18553. ;;; Agenda entry finders
  18554. (defun org-agenda-get-day-entries (file date &rest args)
  18555. "Does the work for `org-diary' and `org-agenda'.
  18556. FILE is the path to a file to be checked for entries. DATE is date like
  18557. the one returned by `calendar-current-date'. ARGS are symbols indicating
  18558. which kind of entries should be extracted. For details about these, see
  18559. the documentation of `org-diary'."
  18560. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  18561. (let* ((org-startup-folded nil)
  18562. (org-startup-align-all-tables nil)
  18563. (buffer (if (file-exists-p file)
  18564. (org-get-agenda-file-buffer file)
  18565. (error "No such file %s" file)))
  18566. arg results rtn)
  18567. (if (not buffer)
  18568. ;; If file does not exist, make sure an error message ends up in diary
  18569. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  18570. (with-current-buffer buffer
  18571. (unless (org-mode-p)
  18572. (error "Agenda file %s is not in `org-mode'" file))
  18573. (let ((case-fold-search nil))
  18574. (save-excursion
  18575. (save-restriction
  18576. (if org-agenda-restrict
  18577. (narrow-to-region org-agenda-restrict-begin
  18578. org-agenda-restrict-end)
  18579. (widen))
  18580. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  18581. (while (setq arg (pop args))
  18582. (cond
  18583. ((and (eq arg :todo)
  18584. (equal date (calendar-current-date)))
  18585. (setq rtn (org-agenda-get-todos))
  18586. (setq results (append results rtn)))
  18587. ((eq arg :timestamp)
  18588. (setq rtn (org-agenda-get-blocks))
  18589. (setq results (append results rtn))
  18590. (setq rtn (org-agenda-get-timestamps))
  18591. (setq results (append results rtn)))
  18592. ((eq arg :sexp)
  18593. (setq rtn (org-agenda-get-sexps))
  18594. (setq results (append results rtn)))
  18595. ((eq arg :scheduled)
  18596. (setq rtn (org-agenda-get-scheduled))
  18597. (setq results (append results rtn)))
  18598. ((eq arg :closed)
  18599. (setq rtn (org-agenda-get-closed))
  18600. (setq results (append results rtn)))
  18601. ((eq arg :deadline)
  18602. (setq rtn (org-agenda-get-deadlines))
  18603. (setq results (append results rtn))))))))
  18604. results))))
  18605. ;; FIXME: this works only if the cursor is *not* at the
  18606. ;; beginning of the entry
  18607. ;(defun org-entry-is-done-p ()
  18608. ; "Is the current entry marked DONE?"
  18609. ; (save-excursion
  18610. ; (and (re-search-backward "[\r\n]\\*+ " nil t)
  18611. ; (looking-at org-nl-done-regexp))))
  18612. (defun org-entry-is-todo-p ()
  18613. (member (org-get-todo-state) org-not-done-keywords))
  18614. (defun org-entry-is-done-p ()
  18615. (member (org-get-todo-state) org-done-keywords))
  18616. (defun org-get-todo-state ()
  18617. (save-excursion
  18618. (org-back-to-heading t)
  18619. (and (looking-at org-todo-line-regexp)
  18620. (match-end 2)
  18621. (match-string 2))))
  18622. (defun org-at-date-range-p (&optional inactive-ok)
  18623. "Is the cursor inside a date range?"
  18624. (interactive)
  18625. (save-excursion
  18626. (catch 'exit
  18627. (let ((pos (point)))
  18628. (skip-chars-backward "^[<\r\n")
  18629. (skip-chars-backward "<[")
  18630. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  18631. (>= (match-end 0) pos)
  18632. (throw 'exit t))
  18633. (skip-chars-backward "^<[\r\n")
  18634. (skip-chars-backward "<[")
  18635. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  18636. (>= (match-end 0) pos)
  18637. (throw 'exit t)))
  18638. nil)))
  18639. (defun org-agenda-get-todos ()
  18640. "Return the TODO information for agenda display."
  18641. (let* ((props (list 'face nil
  18642. 'done-face 'org-done
  18643. 'org-not-done-regexp org-not-done-regexp
  18644. 'org-todo-regexp org-todo-regexp
  18645. 'mouse-face 'highlight
  18646. 'keymap org-agenda-keymap
  18647. 'help-echo
  18648. (format "mouse-2 or RET jump to org file %s"
  18649. (abbreviate-file-name buffer-file-name))))
  18650. ;; FIXME: get rid of the \n at some point but watch out
  18651. (regexp (concat "^\\*+[ \t]+\\("
  18652. (if org-select-this-todo-keyword
  18653. (if (equal org-select-this-todo-keyword "*")
  18654. org-todo-regexp
  18655. (concat "\\<\\("
  18656. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  18657. "\\)\\>"))
  18658. org-not-done-regexp)
  18659. "[^\n\r]*\\)"))
  18660. marker priority category tags
  18661. ee txt beg end)
  18662. (goto-char (point-min))
  18663. (while (re-search-forward regexp nil t)
  18664. (catch :skip
  18665. (save-match-data
  18666. (beginning-of-line)
  18667. (setq beg (point) end (progn (outline-next-heading) (point)))
  18668. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  18669. (re-search-forward org-ts-regexp end t))
  18670. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  18671. (re-search-forward org-scheduled-time-regexp end t))
  18672. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  18673. (re-search-forward org-deadline-time-regexp end t)
  18674. (org-deadline-close (match-string 1))))
  18675. (goto-char (1+ beg))
  18676. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  18677. (throw :skip nil)))
  18678. (goto-char beg)
  18679. (org-agenda-skip)
  18680. (goto-char (match-beginning 1))
  18681. (setq marker (org-agenda-new-marker (match-beginning 0))
  18682. category (org-get-category)
  18683. tags (org-get-tags-at (point))
  18684. txt (org-format-agenda-item "" (match-string 1) category tags)
  18685. priority (1+ (org-get-priority txt)))
  18686. (org-add-props txt props
  18687. 'org-marker marker 'org-hd-marker marker
  18688. 'priority priority 'org-category category
  18689. 'type "todo")
  18690. (push txt ee)
  18691. (if org-agenda-todo-list-sublevels
  18692. (goto-char (match-end 1))
  18693. (org-end-of-subtree 'invisible))))
  18694. (nreverse ee)))
  18695. (defconst org-agenda-no-heading-message
  18696. "No heading for this item in buffer or region.")
  18697. (defun org-agenda-get-timestamps ()
  18698. "Return the date stamp information for agenda display."
  18699. (let* ((props (list 'face nil
  18700. 'org-not-done-regexp org-not-done-regexp
  18701. 'org-todo-regexp org-todo-regexp
  18702. 'mouse-face 'highlight
  18703. 'keymap org-agenda-keymap
  18704. 'help-echo
  18705. (format "mouse-2 or RET jump to org file %s"
  18706. (abbreviate-file-name buffer-file-name))))
  18707. (d1 (calendar-absolute-from-gregorian date))
  18708. (remove-re
  18709. (concat
  18710. (regexp-quote
  18711. (format-time-string
  18712. "<%Y-%m-%d"
  18713. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18714. ".*?>"))
  18715. (regexp
  18716. (concat
  18717. (regexp-quote
  18718. (substring
  18719. (format-time-string
  18720. (car org-time-stamp-formats)
  18721. (apply 'encode-time ; DATE bound by calendar
  18722. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  18723. 0 11))
  18724. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  18725. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  18726. marker hdmarker deadlinep scheduledp donep tmp priority category
  18727. ee txt timestr tags b0 b3 e3 head)
  18728. (goto-char (point-min))
  18729. (while (re-search-forward regexp nil t)
  18730. (setq b0 (match-beginning 0)
  18731. b3 (match-beginning 3) e3 (match-end 3))
  18732. (catch :skip
  18733. (and (org-at-date-range-p) (throw :skip nil))
  18734. (org-agenda-skip)
  18735. (if (and (match-end 1)
  18736. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  18737. (throw :skip nil))
  18738. (if (and e3
  18739. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  18740. (throw :skip nil))
  18741. (setq marker (org-agenda-new-marker b0)
  18742. category (org-get-category b0)
  18743. tmp (buffer-substring (max (point-min)
  18744. (- b0 org-ds-keyword-length))
  18745. b0)
  18746. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  18747. deadlinep (string-match org-deadline-regexp tmp)
  18748. scheduledp (string-match org-scheduled-regexp tmp)
  18749. donep (org-entry-is-done-p))
  18750. (if (or scheduledp deadlinep) (throw :skip t))
  18751. (if (string-match ">" timestr)
  18752. ;; substring should only run to end of time stamp
  18753. (setq timestr (substring timestr 0 (match-end 0))))
  18754. (save-excursion
  18755. (if (re-search-backward "^\\*+ " nil t)
  18756. (progn
  18757. (goto-char (match-beginning 0))
  18758. (setq hdmarker (org-agenda-new-marker)
  18759. tags (org-get-tags-at))
  18760. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  18761. (setq head (match-string 1))
  18762. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  18763. (setq txt (org-format-agenda-item
  18764. nil head category tags timestr nil
  18765. remove-re)))
  18766. (setq txt org-agenda-no-heading-message))
  18767. (setq priority (org-get-priority txt))
  18768. (org-add-props txt props
  18769. 'org-marker marker 'org-hd-marker hdmarker)
  18770. (org-add-props txt nil 'priority priority
  18771. 'org-category category 'date date
  18772. 'type "timestamp")
  18773. (push txt ee))
  18774. (outline-next-heading)))
  18775. (nreverse ee)))
  18776. (defun org-agenda-get-sexps ()
  18777. "Return the sexp information for agenda display."
  18778. (require 'diary-lib)
  18779. (let* ((props (list 'face nil
  18780. 'mouse-face 'highlight
  18781. 'keymap org-agenda-keymap
  18782. 'help-echo
  18783. (format "mouse-2 or RET jump to org file %s"
  18784. (abbreviate-file-name buffer-file-name))))
  18785. (regexp "^&?%%(")
  18786. marker category ee txt tags entry result beg b sexp sexp-entry)
  18787. (goto-char (point-min))
  18788. (while (re-search-forward regexp nil t)
  18789. (catch :skip
  18790. (org-agenda-skip)
  18791. (setq beg (match-beginning 0))
  18792. (goto-char (1- (match-end 0)))
  18793. (setq b (point))
  18794. (forward-sexp 1)
  18795. (setq sexp (buffer-substring b (point)))
  18796. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  18797. (org-trim (match-string 1))
  18798. ""))
  18799. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  18800. (when result
  18801. (setq marker (org-agenda-new-marker beg)
  18802. category (org-get-category beg))
  18803. (if (string-match "\\S-" result)
  18804. (setq txt result)
  18805. (setq txt "SEXP entry returned empty string"))
  18806. (setq txt (org-format-agenda-item
  18807. "" txt category tags 'time))
  18808. (org-add-props txt props 'org-marker marker)
  18809. (org-add-props txt nil
  18810. 'org-category category 'date date
  18811. 'type "sexp")
  18812. (push txt ee))))
  18813. (nreverse ee)))
  18814. (defun org-agenda-get-closed ()
  18815. "Return the logged TODO entries for agenda display."
  18816. (let* ((props (list 'mouse-face 'highlight
  18817. 'org-not-done-regexp org-not-done-regexp
  18818. 'org-todo-regexp org-todo-regexp
  18819. 'keymap org-agenda-keymap
  18820. 'help-echo
  18821. (format "mouse-2 or RET jump to org file %s"
  18822. (abbreviate-file-name buffer-file-name))))
  18823. (regexp (concat
  18824. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  18825. (regexp-quote
  18826. (substring
  18827. (format-time-string
  18828. (car org-time-stamp-formats)
  18829. (apply 'encode-time ; DATE bound by calendar
  18830. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  18831. 1 11))))
  18832. marker hdmarker priority category tags closedp
  18833. ee txt timestr)
  18834. (goto-char (point-min))
  18835. (while (re-search-forward regexp nil t)
  18836. (catch :skip
  18837. (org-agenda-skip)
  18838. (setq marker (org-agenda-new-marker (match-beginning 0))
  18839. closedp (equal (match-string 1) org-closed-string)
  18840. category (org-get-category (match-beginning 0))
  18841. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  18842. ;; donep (org-entry-is-done-p)
  18843. )
  18844. (if (string-match "\\]" timestr)
  18845. ;; substring should only run to end of time stamp
  18846. (setq timestr (substring timestr 0 (match-end 0))))
  18847. (save-excursion
  18848. (if (re-search-backward "^\\*+ " nil t)
  18849. (progn
  18850. (goto-char (match-beginning 0))
  18851. (setq hdmarker (org-agenda-new-marker)
  18852. tags (org-get-tags-at))
  18853. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  18854. (setq txt (org-format-agenda-item
  18855. (if closedp "Closed: " "Clocked: ")
  18856. (match-string 1) category tags timestr)))
  18857. (setq txt org-agenda-no-heading-message))
  18858. (setq priority 100000)
  18859. (org-add-props txt props
  18860. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  18861. 'priority priority 'org-category category
  18862. 'type "closed" 'date date
  18863. 'undone-face 'org-warning 'done-face 'org-done)
  18864. (push txt ee))
  18865. (outline-next-heading)))
  18866. (nreverse ee)))
  18867. (defun org-agenda-get-deadlines ()
  18868. "Return the deadline information for agenda display."
  18869. (let* ((props (list 'mouse-face 'highlight
  18870. 'org-not-done-regexp org-not-done-regexp
  18871. 'org-todo-regexp org-todo-regexp
  18872. 'keymap org-agenda-keymap
  18873. 'help-echo
  18874. (format "mouse-2 or RET jump to org file %s"
  18875. (abbreviate-file-name buffer-file-name))))
  18876. (regexp org-deadline-time-regexp)
  18877. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  18878. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  18879. d2 diff dfrac wdays pos pos1 category tags
  18880. ee txt head face s upcomingp donep timestr)
  18881. (goto-char (point-min))
  18882. (while (re-search-forward regexp nil t)
  18883. (catch :skip
  18884. (org-agenda-skip)
  18885. (setq s (match-string 1)
  18886. pos (1- (match-beginning 1))
  18887. d2 (org-time-string-to-absolute (match-string 1) d1)
  18888. diff (- d2 d1)
  18889. wdays (org-get-wdays s)
  18890. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  18891. upcomingp (and todayp (> diff 0)))
  18892. ;; When to show a deadline in the calendar:
  18893. ;; If the expiration is within wdays warning time.
  18894. ;; Past-due deadlines are only shown on the current date
  18895. (if (or (and (<= diff wdays)
  18896. (and todayp (not org-agenda-only-exact-dates)))
  18897. (= diff 0))
  18898. (save-excursion
  18899. (setq category (org-get-category))
  18900. (if (re-search-backward "^\\*+[ \t]+" nil t)
  18901. (progn
  18902. (goto-char (match-end 0))
  18903. (setq pos1 (match-beginning 0))
  18904. (setq tags (org-get-tags-at pos1))
  18905. (setq head (buffer-substring-no-properties
  18906. (point)
  18907. (progn (skip-chars-forward "^\r\n")
  18908. (point))))
  18909. (setq donep (string-match org-looking-at-done-regexp head))
  18910. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  18911. (setq timestr
  18912. (concat (substring s (match-beginning 1)) " "))
  18913. (setq timestr 'time))
  18914. (if (and donep
  18915. (or org-agenda-skip-deadline-if-done
  18916. (not (= diff 0))))
  18917. (setq txt nil)
  18918. (setq txt (org-format-agenda-item
  18919. (if (= diff 0)
  18920. (car org-agenda-deadline-leaders)
  18921. (format (nth 1 org-agenda-deadline-leaders)
  18922. diff))
  18923. head category tags timestr))))
  18924. (setq txt org-agenda-no-heading-message))
  18925. (when txt
  18926. (setq face (org-agenda-deadline-face dfrac))
  18927. (org-add-props txt props
  18928. 'org-marker (org-agenda-new-marker pos)
  18929. 'org-hd-marker (org-agenda-new-marker pos1)
  18930. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  18931. (org-get-priority txt))
  18932. 'org-category category
  18933. 'type (if upcomingp "upcoming-deadline" "deadline")
  18934. 'date (if upcomingp date d2)
  18935. 'face (if donep 'org-done face)
  18936. 'undone-face face 'done-face 'org-done)
  18937. (push txt ee))))))
  18938. (nreverse ee)))
  18939. (defun org-agenda-deadline-face (fraction)
  18940. "Return the face to displaying a deadline item.
  18941. FRACTION is what fraction of the head-warning time has passed."
  18942. (let ((faces org-agenda-deadline-faces) f)
  18943. (catch 'exit
  18944. (while (setq f (pop faces))
  18945. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  18946. (defun org-agenda-get-scheduled ()
  18947. "Return the scheduled information for agenda display."
  18948. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  18949. 'org-todo-regexp org-todo-regexp
  18950. 'done-face 'org-done
  18951. 'mouse-face 'highlight
  18952. 'keymap org-agenda-keymap
  18953. 'help-echo
  18954. (format "mouse-2 or RET jump to org file %s"
  18955. (abbreviate-file-name buffer-file-name))))
  18956. (regexp org-scheduled-time-regexp)
  18957. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  18958. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  18959. d2 diff pos pos1 category tags
  18960. ee txt head pastschedp donep face timestr s)
  18961. (goto-char (point-min))
  18962. (while (re-search-forward regexp nil t)
  18963. (catch :skip
  18964. (org-agenda-skip)
  18965. (setq s (match-string 1)
  18966. pos (1- (match-beginning 1))
  18967. d2 (org-time-string-to-absolute (match-string 1) d1)
  18968. diff (- d2 d1))
  18969. (setq pastschedp (and todayp (< diff 0)))
  18970. ;; When to show a scheduled item in the calendar:
  18971. ;; If it is on or past the date.
  18972. (if (or (and (< diff 0)
  18973. (and todayp (not org-agenda-only-exact-dates)))
  18974. (= diff 0))
  18975. (save-excursion
  18976. (setq category (org-get-category))
  18977. (if (re-search-backward "^\\*+[ \t]+" nil t)
  18978. (progn
  18979. (goto-char (match-end 0))
  18980. (setq pos1 (match-beginning 0))
  18981. (setq tags (org-get-tags-at))
  18982. (setq head (buffer-substring-no-properties
  18983. (point)
  18984. (progn (skip-chars-forward "^\r\n") (point))))
  18985. (setq donep (string-match org-looking-at-done-regexp head))
  18986. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  18987. (setq timestr
  18988. (concat (substring s (match-beginning 1)) " "))
  18989. (setq timestr 'time))
  18990. (if (and donep
  18991. (or org-agenda-skip-scheduled-if-done
  18992. (not (= diff 0))))
  18993. (setq txt nil)
  18994. (setq txt (org-format-agenda-item
  18995. (if (= diff 0)
  18996. (car org-agenda-scheduled-leaders)
  18997. (format (nth 1 org-agenda-scheduled-leaders)
  18998. (- 1 diff)))
  18999. head category tags timestr))))
  19000. (setq txt org-agenda-no-heading-message))
  19001. (when txt
  19002. (setq face (if pastschedp
  19003. 'org-scheduled-previously
  19004. 'org-scheduled-today))
  19005. (org-add-props txt props
  19006. 'undone-face face
  19007. 'face (if donep 'org-done face)
  19008. 'org-marker (org-agenda-new-marker pos)
  19009. 'org-hd-marker (org-agenda-new-marker pos1)
  19010. 'type (if pastschedp "past-scheduled" "scheduled")
  19011. 'date (if pastschedp d2 date)
  19012. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19013. 'org-category category)
  19014. (push txt ee))))))
  19015. (nreverse ee)))
  19016. (defun org-agenda-get-blocks ()
  19017. "Return the date-range information for agenda display."
  19018. (let* ((props (list 'face nil
  19019. 'org-not-done-regexp org-not-done-regexp
  19020. 'org-todo-regexp org-todo-regexp
  19021. 'mouse-face 'highlight
  19022. 'keymap org-agenda-keymap
  19023. 'help-echo
  19024. (format "mouse-2 or RET jump to org file %s"
  19025. (abbreviate-file-name buffer-file-name))))
  19026. (regexp org-tr-regexp)
  19027. (d0 (calendar-absolute-from-gregorian date))
  19028. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19029. donep head)
  19030. (goto-char (point-min))
  19031. (while (re-search-forward regexp nil t)
  19032. (catch :skip
  19033. (org-agenda-skip)
  19034. (setq pos (point))
  19035. (setq timestr (match-string 0)
  19036. s1 (match-string 1)
  19037. s2 (match-string 2)
  19038. d1 (time-to-days (org-time-string-to-time s1))
  19039. d2 (time-to-days (org-time-string-to-time s2)))
  19040. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19041. ;; Only allow days between the limits, because the normal
  19042. ;; date stamps will catch the limits.
  19043. (save-excursion
  19044. (setq marker (org-agenda-new-marker (point)))
  19045. (setq category (org-get-category))
  19046. (if (re-search-backward "^\\*+ " nil t)
  19047. (progn
  19048. (goto-char (match-beginning 0))
  19049. (setq hdmarker (org-agenda-new-marker (point)))
  19050. (setq tags (org-get-tags-at))
  19051. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19052. (setq head (match-string 1))
  19053. (and org-agenda-skip-timestamp-if-done
  19054. (org-entry-is-done-p)
  19055. (throw :skip t))
  19056. (setq txt (org-format-agenda-item
  19057. (format (if (= d1 d2) "" "(%d/%d): ")
  19058. (1+ (- d0 d1)) (1+ (- d2 d1)))
  19059. head category tags
  19060. (if (= d0 d1) timestr))))
  19061. (setq txt org-agenda-no-heading-message))
  19062. (org-add-props txt props
  19063. 'org-marker marker 'org-hd-marker hdmarker
  19064. 'type "block" 'date date
  19065. 'priority (org-get-priority txt) 'org-category category)
  19066. (push txt ee)))
  19067. (goto-char pos)))
  19068. ;; Sort the entries by expiration date.
  19069. (nreverse ee)))
  19070. ;;; Agenda presentation and sorting
  19071. (defconst org-plain-time-of-day-regexp
  19072. (concat
  19073. "\\(\\<[012]?[0-9]"
  19074. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19075. "\\(--?"
  19076. "\\(\\<[012]?[0-9]"
  19077. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19078. "\\)?")
  19079. "Regular expression to match a plain time or time range.
  19080. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19081. groups carry important information:
  19082. 0 the full match
  19083. 1 the first time, range or not
  19084. 8 the second time, if it is a range.")
  19085. (defconst org-plain-time-extension-regexp
  19086. (concat
  19087. "\\(\\<[012]?[0-9]"
  19088. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19089. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  19090. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  19091. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19092. groups carry important information:
  19093. 0 the full match
  19094. 7 hours of duration
  19095. 9 minutes of duration")
  19096. (defconst org-stamp-time-of-day-regexp
  19097. (concat
  19098. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  19099. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  19100. "\\(--?"
  19101. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  19102. "Regular expression to match a timestamp time or time range.
  19103. After a match, the following groups carry important information:
  19104. 0 the full match
  19105. 1 date plus weekday, for backreferencing to make sure both times on same day
  19106. 2 the first time, range or not
  19107. 4 the second time, if it is a range.")
  19108. (defvar org-prefix-has-time nil
  19109. "A flag, set by `org-compile-prefix-format'.
  19110. The flag is set if the currently compiled format contains a `%t'.")
  19111. (defvar org-prefix-has-tag nil
  19112. "A flag, set by `org-compile-prefix-format'.
  19113. The flag is set if the currently compiled format contains a `%T'.")
  19114. (defun org-format-agenda-item (extra txt &optional category tags dotime
  19115. noprefix remove-re)
  19116. "Format TXT to be inserted into the agenda buffer.
  19117. In particular, it adds the prefix and corresponding text properties. EXTRA
  19118. must be a string and replaces the `%s' specifier in the prefix format.
  19119. CATEGORY (string, symbol or nil) may be used to overrule the default
  19120. category taken from local variable or file name. It will replace the `%c'
  19121. specifier in the format. DOTIME, when non-nil, indicates that a
  19122. time-of-day should be extracted from TXT for sorting of this entry, and for
  19123. the `%t' specifier in the format. When DOTIME is a string, this string is
  19124. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  19125. only the correctly processes TXT should be returned - this is used by
  19126. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  19127. Any match of REMOVE-RE will be removed from TXT."
  19128. (save-match-data
  19129. ;; Diary entries sometimes have extra whitespace at the beginning
  19130. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  19131. (let* ((category (or category
  19132. org-category
  19133. (if buffer-file-name
  19134. (file-name-sans-extension
  19135. (file-name-nondirectory buffer-file-name))
  19136. "")))
  19137. (tag (if tags (nth (1- (length tags)) tags) ""))
  19138. time ; time and tag are needed for the eval of the prefix format
  19139. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  19140. (time-of-day (and dotime (org-get-time-of-day ts)))
  19141. stamp plain s0 s1 s2 rtn srp)
  19142. (when (and dotime time-of-day org-prefix-has-time)
  19143. ;; Extract starting and ending time and move them to prefix
  19144. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  19145. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  19146. (setq s0 (match-string 0 ts)
  19147. srp (and stamp (match-end 3))
  19148. s1 (match-string (if plain 1 2) ts)
  19149. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  19150. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  19151. ;; them, we might want to remove them there to avoid duplication.
  19152. ;; The user can turn this off with a variable.
  19153. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  19154. (string-match (concat (regexp-quote s0) " *") txt)
  19155. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  19156. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  19157. (= (match-beginning 0) 0)
  19158. t))
  19159. (setq txt (replace-match "" nil nil txt))))
  19160. ;; Normalize the time(s) to 24 hour
  19161. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  19162. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  19163. (when (and s1 (not s2) org-agenda-default-appointment-duration
  19164. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  19165. (let ((m (+ (string-to-number (match-string 2 s1))
  19166. (* 60 (string-to-number (match-string 1 s1)))
  19167. org-agenda-default-appointment-duration))
  19168. h)
  19169. (setq h (/ m 60) m (- m (* h 60)))
  19170. (setq s2 (format "%02d:%02d" h m))))
  19171. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  19172. txt)
  19173. ;; Tags are in the string
  19174. (if (or (eq org-agenda-remove-tags t)
  19175. (and org-agenda-remove-tags
  19176. org-prefix-has-tag))
  19177. (setq txt (replace-match "" t t txt))
  19178. (setq txt (replace-match
  19179. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  19180. (match-string 2 txt))
  19181. t t txt))))
  19182. (when remove-re
  19183. (while (string-match remove-re txt)
  19184. (setq txt (replace-match "" t t txt))))
  19185. ;; Create the final string
  19186. (if noprefix
  19187. (setq rtn txt)
  19188. ;; Prepare the variables needed in the eval of the compiled format
  19189. (setq time (cond (s2 (concat s1 "-" s2))
  19190. (s1 (concat s1 "......"))
  19191. (t ""))
  19192. extra (or extra "")
  19193. category (if (symbolp category) (symbol-name category) category))
  19194. ;; Evaluate the compiled format
  19195. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  19196. ;; And finally add the text properties
  19197. (org-add-props rtn nil
  19198. 'org-category (downcase category) 'tags tags
  19199. 'org-highest-priority org-highest-priority
  19200. 'org-lowest-priority org-lowest-priority
  19201. 'prefix-length (- (length rtn) (length txt))
  19202. 'time-of-day time-of-day
  19203. 'txt txt
  19204. 'time time
  19205. 'extra extra
  19206. 'dotime dotime))))
  19207. (defvar org-agenda-sorting-strategy) ;; FIXME: can be removed?
  19208. (defvar org-agenda-sorting-strategy-selected nil)
  19209. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  19210. (catch 'exit
  19211. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  19212. ((and todayp (member 'today (car org-agenda-time-grid))))
  19213. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  19214. ((member 'weekly (car org-agenda-time-grid)))
  19215. (t (throw 'exit list)))
  19216. (let* ((have (delq nil (mapcar
  19217. (lambda (x) (get-text-property 1 'time-of-day x))
  19218. list)))
  19219. (string (nth 1 org-agenda-time-grid))
  19220. (gridtimes (nth 2 org-agenda-time-grid))
  19221. (req (car org-agenda-time-grid))
  19222. (remove (member 'remove-match req))
  19223. new time)
  19224. (if (and (member 'require-timed req) (not have))
  19225. ;; don't show empty grid
  19226. (throw 'exit list))
  19227. (while (setq time (pop gridtimes))
  19228. (unless (and remove (member time have))
  19229. (setq time (int-to-string time))
  19230. (push (org-format-agenda-item
  19231. nil string "" nil
  19232. (concat (substring time 0 -2) ":" (substring time -2)))
  19233. new)
  19234. (put-text-property
  19235. 1 (length (car new)) 'face 'org-time-grid (car new))))
  19236. (if (member 'time-up org-agenda-sorting-strategy-selected)
  19237. (append new list)
  19238. (append list new)))))
  19239. (defun org-compile-prefix-format (key)
  19240. "Compile the prefix format into a Lisp form that can be evaluated.
  19241. The resulting form is returned and stored in the variable
  19242. `org-prefix-format-compiled'."
  19243. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  19244. (let ((s (cond
  19245. ((stringp org-agenda-prefix-format)
  19246. org-agenda-prefix-format)
  19247. ((assq key org-agenda-prefix-format)
  19248. (cdr (assq key org-agenda-prefix-format)))
  19249. (t " %-12:c%?-12t% s")))
  19250. (start 0)
  19251. varform vars var e c f opt)
  19252. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  19253. s start)
  19254. (setq var (cdr (assoc (match-string 4 s)
  19255. '(("c" . category) ("t" . time) ("s" . extra)
  19256. ("T" . tag))))
  19257. c (or (match-string 3 s) "")
  19258. opt (match-beginning 1)
  19259. start (1+ (match-beginning 0)))
  19260. (if (equal var 'time) (setq org-prefix-has-time t))
  19261. (if (equal var 'tag) (setq org-prefix-has-tag t))
  19262. (setq f (concat "%" (match-string 2 s) "s"))
  19263. (if opt
  19264. (setq varform
  19265. `(if (equal "" ,var)
  19266. ""
  19267. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  19268. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  19269. (setq s (replace-match "%s" t nil s))
  19270. (push varform vars))
  19271. (setq vars (nreverse vars))
  19272. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  19273. (defun org-set-sorting-strategy (key)
  19274. (if (symbolp (car org-agenda-sorting-strategy))
  19275. ;; the old format
  19276. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  19277. (setq org-agenda-sorting-strategy-selected
  19278. (or (cdr (assq key org-agenda-sorting-strategy))
  19279. (cdr (assq 'agenda org-agenda-sorting-strategy))
  19280. '(time-up category-keep priority-down)))))
  19281. (defun org-get-time-of-day (s &optional string mod24)
  19282. "Check string S for a time of day.
  19283. If found, return it as a military time number between 0 and 2400.
  19284. If not found, return nil.
  19285. The optional STRING argument forces conversion into a 5 character wide string
  19286. HH:MM."
  19287. (save-match-data
  19288. (when
  19289. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  19290. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  19291. (let* ((h (string-to-number (match-string 1 s)))
  19292. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  19293. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  19294. (am-p (equal ampm "am"))
  19295. (h1 (cond ((not ampm) h)
  19296. ((= h 12) (if am-p 0 12))
  19297. (t (+ h (if am-p 0 12)))))
  19298. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  19299. (mod h1 24) h1))
  19300. (t0 (+ (* 100 h2) m))
  19301. (t1 (concat (if (>= h1 24) "+" " ")
  19302. (if (< t0 100) "0" "")
  19303. (if (< t0 10) "0" "")
  19304. (int-to-string t0))))
  19305. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  19306. (defun org-finalize-agenda-entries (list &optional nosort)
  19307. "Sort and concatenate the agenda items."
  19308. (setq list (mapcar 'org-agenda-highlight-todo list))
  19309. (if nosort
  19310. list
  19311. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  19312. (defun org-agenda-highlight-todo (x)
  19313. (let (re pl)
  19314. (if (eq x 'line)
  19315. (save-excursion
  19316. (beginning-of-line 1)
  19317. (setq re (get-text-property (point) 'org-todo-regexp))
  19318. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  19319. (and (looking-at (concat "[ \t]*\\.*" re))
  19320. (add-text-properties (match-beginning 0) (match-end 0)
  19321. (list 'face (org-get-todo-face 0)))))
  19322. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  19323. pl (get-text-property 0 'prefix-length x))
  19324. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  19325. (add-text-properties
  19326. (or (match-end 1) (match-end 0)) (match-end 0)
  19327. (list 'face (org-get-todo-face (match-string 2 x)))
  19328. x))
  19329. x)))
  19330. (defsubst org-cmp-priority (a b)
  19331. "Compare the priorities of string A and B."
  19332. (let ((pa (or (get-text-property 1 'priority a) 0))
  19333. (pb (or (get-text-property 1 'priority b) 0)))
  19334. (cond ((> pa pb) +1)
  19335. ((< pa pb) -1)
  19336. (t nil))))
  19337. (defsubst org-cmp-category (a b)
  19338. "Compare the string values of categories of strings A and B."
  19339. (let ((ca (or (get-text-property 1 'org-category a) ""))
  19340. (cb (or (get-text-property 1 'org-category b) "")))
  19341. (cond ((string-lessp ca cb) -1)
  19342. ((string-lessp cb ca) +1)
  19343. (t nil))))
  19344. (defsubst org-cmp-tag (a b)
  19345. "Compare the string values of categories of strings A and B."
  19346. (let ((ta (car (last (get-text-property 1 'tags a))))
  19347. (tb (car (last (get-text-property 1 'tags b)))))
  19348. (cond ((not ta) +1)
  19349. ((not tb) -1)
  19350. ((string-lessp ta tb) -1)
  19351. ((string-lessp tb ta) +1)
  19352. (t nil))))
  19353. (defsubst org-cmp-time (a b)
  19354. "Compare the time-of-day values of strings A and B."
  19355. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  19356. (ta (or (get-text-property 1 'time-of-day a) def))
  19357. (tb (or (get-text-property 1 'time-of-day b) def)))
  19358. (cond ((< ta tb) -1)
  19359. ((< tb ta) +1)
  19360. (t nil))))
  19361. (defun org-entries-lessp (a b)
  19362. "Predicate for sorting agenda entries."
  19363. ;; The following variables will be used when the form is evaluated.
  19364. ;; So even though the compiler complains, keep them.
  19365. (let* ((time-up (org-cmp-time a b))
  19366. (time-down (if time-up (- time-up) nil))
  19367. (priority-up (org-cmp-priority a b))
  19368. (priority-down (if priority-up (- priority-up) nil))
  19369. (category-up (org-cmp-category a b))
  19370. (category-down (if category-up (- category-up) nil))
  19371. (category-keep (if category-up +1 nil))
  19372. (tag-up (org-cmp-tag a b))
  19373. (tag-down (if tag-up (- tag-up) nil)))
  19374. (cdr (assoc
  19375. (eval (cons 'or org-agenda-sorting-strategy-selected))
  19376. '((-1 . t) (1 . nil) (nil . nil))))))
  19377. ;;; Agenda commands
  19378. (defun org-agenda-check-type (error &rest types)
  19379. "Check if agenda buffer is of allowed type.
  19380. If ERROR is non-nil, throw an error, otherwise just return nil."
  19381. (if (memq org-agenda-type types)
  19382. t
  19383. (if error
  19384. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  19385. nil)))
  19386. (defun org-agenda-quit ()
  19387. "Exit agenda by removing the window or the buffer."
  19388. (interactive)
  19389. (let ((buf (current-buffer)))
  19390. (if (not (one-window-p)) (delete-window))
  19391. (kill-buffer buf)
  19392. (org-agenda-maybe-reset-markers 'force)
  19393. (org-columns-remove-overlays))
  19394. ;; Maybe restore the pre-agenda window configuration.
  19395. (and org-agenda-restore-windows-after-quit
  19396. (not (eq org-agenda-window-setup 'other-frame))
  19397. org-pre-agenda-window-conf
  19398. (set-window-configuration org-pre-agenda-window-conf)))
  19399. (defun org-agenda-exit ()
  19400. "Exit agenda by removing the window or the buffer.
  19401. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  19402. Org-mode buffers visited directly by the user will not be touched."
  19403. (interactive)
  19404. (org-release-buffers org-agenda-new-buffers)
  19405. (setq org-agenda-new-buffers nil)
  19406. (org-agenda-quit))
  19407. (defun org-agenda-execute (arg)
  19408. "Execute another agenda command, keeping same window.\\<global-map>
  19409. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  19410. (interactive "P")
  19411. (let ((org-agenda-window-setup 'current-window))
  19412. (org-agenda arg)))
  19413. (defun org-save-all-org-buffers ()
  19414. "Save all Org-mode buffers without user confirmation."
  19415. (interactive)
  19416. (message "Saving all Org-mode buffers...")
  19417. (save-some-buffers t 'org-mode-p)
  19418. (message "Saving all Org-mode buffers... done"))
  19419. (defun org-agenda-redo ()
  19420. "Rebuild Agenda.
  19421. When this is the global TODO list, a prefix argument will be interpreted."
  19422. (interactive)
  19423. (let* ((org-agenda-keep-modes t)
  19424. (line (org-current-line))
  19425. (window-line (- line (org-current-line (window-start))))
  19426. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  19427. (message "Rebuilding agenda buffer...")
  19428. (org-let lprops '(eval org-agenda-redo-command))
  19429. (setq org-agenda-undo-list nil
  19430. org-agenda-pending-undo-list nil)
  19431. (message "Rebuilding agenda buffer...done")
  19432. (goto-line line)
  19433. (recenter window-line)))
  19434. (defun org-agenda-goto-date (date)
  19435. "Jump to DATE in agenda."
  19436. (interactive (list (org-read-date)))
  19437. (org-agenda-list nil date))
  19438. (defun org-agenda-goto-today ()
  19439. "Go to today."
  19440. (interactive)
  19441. (org-agenda-check-type t 'timeline 'agenda)
  19442. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  19443. (cond
  19444. (tdpos (goto-char tdpos))
  19445. ((eq org-agenda-type 'agenda)
  19446. (let* ((sd (time-to-days
  19447. (time-subtract (current-time)
  19448. (list 0 (* 3600 org-extend-today-until) 0))))
  19449. (comp (org-agenda-compute-time-span sd org-agenda-span))
  19450. (org-agenda-overriding-arguments org-agenda-last-arguments))
  19451. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  19452. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  19453. (org-agenda-redo)
  19454. (org-agenda-find-same-or-today-or-agenda)))
  19455. (t (error "Cannot find today")))))
  19456. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  19457. (goto-char
  19458. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  19459. (text-property-any (point-min) (point-max) 'org-today t)
  19460. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  19461. (point-min))))
  19462. (defun org-agenda-later (arg)
  19463. "Go forward in time by thee current span.
  19464. With prefix ARG, go forward that many times the current span."
  19465. (interactive "p")
  19466. (org-agenda-check-type t 'agenda)
  19467. (let* ((span org-agenda-span)
  19468. (sd org-starting-day)
  19469. (greg (calendar-gregorian-from-absolute sd))
  19470. (cnt (get-text-property (point) 'org-day-cnt))
  19471. greg2 nd)
  19472. (cond
  19473. ((eq span 'day)
  19474. (setq sd (+ arg sd) nd 1))
  19475. ((eq span 'week)
  19476. (setq sd (+ (* 7 arg) sd) nd 7))
  19477. ((eq span 'month)
  19478. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  19479. sd (calendar-absolute-from-gregorian greg2))
  19480. (setcar greg2 (1+ (car greg2)))
  19481. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  19482. ((eq span 'year)
  19483. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  19484. sd (calendar-absolute-from-gregorian greg2))
  19485. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  19486. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  19487. (let ((org-agenda-overriding-arguments
  19488. (list (car org-agenda-last-arguments) sd nd t)))
  19489. (org-agenda-redo)
  19490. (org-agenda-find-same-or-today-or-agenda cnt))))
  19491. (defun org-agenda-earlier (arg)
  19492. "Go backward in time by the current span.
  19493. With prefix ARG, go backward that many times the current span."
  19494. (interactive "p")
  19495. (org-agenda-later (- arg)))
  19496. (defun org-agenda-day-view ()
  19497. "Switch to daily view for agenda."
  19498. (interactive)
  19499. (setq org-agenda-ndays 1)
  19500. (org-agenda-change-time-span 'day))
  19501. (defun org-agenda-week-view ()
  19502. "Switch to daily view for agenda."
  19503. (interactive)
  19504. (setq org-agenda-ndays 7)
  19505. (org-agenda-change-time-span 'week))
  19506. (defun org-agenda-month-view ()
  19507. "Switch to daily view for agenda."
  19508. (interactive)
  19509. (org-agenda-change-time-span 'month))
  19510. (defun org-agenda-year-view ()
  19511. "Switch to daily view for agenda."
  19512. (interactive)
  19513. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  19514. (org-agenda-change-time-span 'year)
  19515. (error "Abort")))
  19516. (defun org-agenda-change-time-span (span)
  19517. "Change the agenda view to SPAN.
  19518. SPAN may be `day', `week', `month', `year'."
  19519. (org-agenda-check-type t 'agenda)
  19520. (if (equal org-agenda-span span)
  19521. (error "Viewing span is already \"%s\"" span))
  19522. (let* ((sd (or (get-text-property (point) 'day)
  19523. org-starting-day))
  19524. (computed (org-agenda-compute-time-span sd span))
  19525. (org-agenda-overriding-arguments
  19526. (list (car org-agenda-last-arguments)
  19527. (car computed) (cdr computed) t)))
  19528. (org-agenda-redo)
  19529. (org-agenda-find-same-or-today-or-agenda))
  19530. (org-agenda-set-mode-name)
  19531. (message "Switched to %s view" span))
  19532. (defun org-agenda-compute-time-span (sd span)
  19533. "Compute starting date and number of days for agenda.
  19534. SPAN may be `day', `week', `month', `year'. The return value
  19535. is a cons cell with the starting date and the number of days,
  19536. so that the date SD will be in that range."
  19537. (let* ((greg (calendar-gregorian-from-absolute sd))
  19538. nd)
  19539. (cond
  19540. ((eq span 'day)
  19541. (setq nd 1))
  19542. ((eq span 'week)
  19543. (let* ((nt (calendar-day-of-week
  19544. (calendar-gregorian-from-absolute sd)))
  19545. (d (if org-agenda-start-on-weekday
  19546. (- nt org-agenda-start-on-weekday)
  19547. 0)))
  19548. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  19549. (setq nd 7)))
  19550. ((eq span 'month)
  19551. (setq sd (calendar-absolute-from-gregorian
  19552. (list (car greg) 1 (nth 2 greg)))
  19553. nd (- (calendar-absolute-from-gregorian
  19554. (list (1+ (car greg)) 1 (nth 2 greg)))
  19555. sd)))
  19556. ((eq span 'year)
  19557. (setq sd (calendar-absolute-from-gregorian
  19558. (list 1 1 (nth 2 greg)))
  19559. nd (- (calendar-absolute-from-gregorian
  19560. (list 1 1 (1+ (nth 2 greg))))
  19561. sd))))
  19562. (cons sd nd)))
  19563. ;; FIXME: does not work if user makes date format that starts with a blank
  19564. (defun org-agenda-next-date-line (&optional arg)
  19565. "Jump to the next line indicating a date in agenda buffer."
  19566. (interactive "p")
  19567. (org-agenda-check-type t 'agenda 'timeline)
  19568. (beginning-of-line 1)
  19569. (if (looking-at "^\\S-") (forward-char 1))
  19570. (if (not (re-search-forward "^\\S-" nil t arg))
  19571. (progn
  19572. (backward-char 1)
  19573. (error "No next date after this line in this buffer")))
  19574. (goto-char (match-beginning 0)))
  19575. (defun org-agenda-previous-date-line (&optional arg)
  19576. "Jump to the previous line indicating a date in agenda buffer."
  19577. (interactive "p")
  19578. (org-agenda-check-type t 'agenda 'timeline)
  19579. (beginning-of-line 1)
  19580. (if (not (re-search-backward "^\\S-" nil t arg))
  19581. (error "No previous date before this line in this buffer")))
  19582. ;; Initialize the highlight
  19583. (defvar org-hl (org-make-overlay 1 1))
  19584. (org-overlay-put org-hl 'face 'highlight)
  19585. (defun org-highlight (begin end &optional buffer)
  19586. "Highlight a region with overlay."
  19587. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  19588. org-hl begin end (or buffer (current-buffer))))
  19589. (defun org-unhighlight ()
  19590. "Detach overlay INDEX."
  19591. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  19592. ;; FIXME this is currently not used.
  19593. (defun org-highlight-until-next-command (beg end &optional buffer)
  19594. (org-highlight beg end buffer)
  19595. (add-hook 'pre-command-hook 'org-unhighlight-once))
  19596. (defun org-unhighlight-once ()
  19597. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  19598. (org-unhighlight))
  19599. (defun org-agenda-follow-mode ()
  19600. "Toggle follow mode in an agenda buffer."
  19601. (interactive)
  19602. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  19603. (org-agenda-set-mode-name)
  19604. (message "Follow mode is %s"
  19605. (if org-agenda-follow-mode "on" "off")))
  19606. (defun org-agenda-log-mode ()
  19607. "Toggle log mode in an agenda buffer."
  19608. (interactive)
  19609. (org-agenda-check-type t 'agenda 'timeline)
  19610. (setq org-agenda-show-log (not org-agenda-show-log))
  19611. (org-agenda-set-mode-name)
  19612. (org-agenda-redo)
  19613. (message "Log mode is %s"
  19614. (if org-agenda-show-log "on" "off")))
  19615. (defun org-agenda-toggle-diary ()
  19616. "Toggle diary inclusion in an agenda buffer."
  19617. (interactive)
  19618. (org-agenda-check-type t 'agenda)
  19619. (setq org-agenda-include-diary (not org-agenda-include-diary))
  19620. (org-agenda-redo)
  19621. (org-agenda-set-mode-name)
  19622. (message "Diary inclusion turned %s"
  19623. (if org-agenda-include-diary "on" "off")))
  19624. (defun org-agenda-toggle-time-grid ()
  19625. "Toggle time grid in an agenda buffer."
  19626. (interactive)
  19627. (org-agenda-check-type t 'agenda)
  19628. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  19629. (org-agenda-redo)
  19630. (org-agenda-set-mode-name)
  19631. (message "Time-grid turned %s"
  19632. (if org-agenda-use-time-grid "on" "off")))
  19633. (defun org-agenda-set-mode-name ()
  19634. "Set the mode name to indicate all the small mode settings."
  19635. (setq mode-name
  19636. (concat "Org-Agenda"
  19637. (if (equal org-agenda-ndays 1) " Day" "")
  19638. (if (equal org-agenda-ndays 7) " Week" "")
  19639. (if org-agenda-follow-mode " Follow" "")
  19640. (if org-agenda-include-diary " Diary" "")
  19641. (if org-agenda-use-time-grid " Grid" "")
  19642. (if org-agenda-show-log " Log" "")))
  19643. (force-mode-line-update))
  19644. (defun org-agenda-post-command-hook ()
  19645. (and (eolp) (not (bolp)) (backward-char 1))
  19646. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  19647. (if (and org-agenda-follow-mode
  19648. (get-text-property (point) 'org-marker))
  19649. (org-agenda-show)))
  19650. (defun org-agenda-show-priority ()
  19651. "Show the priority of the current item.
  19652. This priority is composed of the main priority given with the [#A] cookies,
  19653. and by additional input from the age of a schedules or deadline entry."
  19654. (interactive)
  19655. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  19656. (message "Priority is %d" (if pri pri -1000))))
  19657. (defun org-agenda-show-tags ()
  19658. "Show the tags applicable to the current item."
  19659. (interactive)
  19660. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  19661. (if tags
  19662. (message "Tags are :%s:"
  19663. (org-no-properties (mapconcat 'identity tags ":")))
  19664. (message "No tags associated with this line"))))
  19665. (defun org-agenda-goto (&optional highlight)
  19666. "Go to the Org-mode file which contains the item at point."
  19667. (interactive)
  19668. (let* ((marker (or (get-text-property (point) 'org-marker)
  19669. (org-agenda-error)))
  19670. (buffer (marker-buffer marker))
  19671. (pos (marker-position marker)))
  19672. (switch-to-buffer-other-window buffer)
  19673. (widen)
  19674. (goto-char pos)
  19675. (when (org-mode-p)
  19676. (org-show-context 'agenda)
  19677. (save-excursion
  19678. (and (outline-next-heading)
  19679. (org-flag-heading nil)))) ; show the next heading
  19680. (run-hooks 'org-agenda-after-show-hook)
  19681. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  19682. (defvar org-agenda-after-show-hook nil
  19683. "Normal hook run after an item has been shown from the agenda.
  19684. Point is in the buffer where the item originated.")
  19685. (defun org-agenda-kill ()
  19686. "Kill the entry or subtree belonging to the current agenda entry."
  19687. (interactive)
  19688. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  19689. (let* ((marker (or (get-text-property (point) 'org-marker)
  19690. (org-agenda-error)))
  19691. (buffer (marker-buffer marker))
  19692. (pos (marker-position marker))
  19693. (type (get-text-property (point) 'type))
  19694. dbeg dend (n 0) conf)
  19695. (org-with-remote-undo buffer
  19696. (with-current-buffer buffer
  19697. (save-excursion
  19698. (goto-char pos)
  19699. (if (and (org-mode-p) (not (member type '("sexp"))))
  19700. (setq dbeg (progn (org-back-to-heading t) (point))
  19701. dend (org-end-of-subtree t t))
  19702. (setq dbeg (point-at-bol)
  19703. dend (min (point-max) (1+ (point-at-eol)))))
  19704. (goto-char dbeg)
  19705. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  19706. (setq conf (or (eq t org-agenda-confirm-kill)
  19707. (and (numberp org-agenda-confirm-kill)
  19708. (> n org-agenda-confirm-kill))))
  19709. (and conf
  19710. (not (y-or-n-p
  19711. (format "Delete entry with %d lines in buffer \"%s\"? "
  19712. n (buffer-name buffer))))
  19713. (error "Abort"))
  19714. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  19715. (with-current-buffer buffer (delete-region dbeg dend))
  19716. (message "Agenda item and source killed"))))
  19717. (defun org-agenda-archive ()
  19718. "Kill the entry or subtree belonging to the current agenda entry."
  19719. (interactive)
  19720. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  19721. (let* ((marker (or (get-text-property (point) 'org-marker)
  19722. (org-agenda-error)))
  19723. (buffer (marker-buffer marker))
  19724. (pos (marker-position marker)))
  19725. (org-with-remote-undo buffer
  19726. (with-current-buffer buffer
  19727. (if (org-mode-p)
  19728. (save-excursion
  19729. (goto-char pos)
  19730. (org-remove-subtree-entries-from-agenda)
  19731. (org-back-to-heading t)
  19732. (org-archive-subtree))
  19733. (error "Archiving works only in Org-mode files"))))))
  19734. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  19735. "Remove all lines in the agenda that correspond to a given subtree.
  19736. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  19737. If this information is not given, the function uses the tree at point."
  19738. (let ((buf (or buf (current-buffer))) m p)
  19739. (save-excursion
  19740. (unless (and beg end)
  19741. (org-back-to-heading t)
  19742. (setq beg (point))
  19743. (org-end-of-subtree t)
  19744. (setq end (point)))
  19745. (set-buffer (get-buffer org-agenda-buffer-name))
  19746. (save-excursion
  19747. (goto-char (point-max))
  19748. (beginning-of-line 1)
  19749. (while (not (bobp))
  19750. (when (and (setq m (get-text-property (point) 'org-marker))
  19751. (equal buf (marker-buffer m))
  19752. (setq p (marker-position m))
  19753. (>= p beg)
  19754. (<= p end))
  19755. (let ((inhibit-read-only t))
  19756. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  19757. (beginning-of-line 0))))))
  19758. (defun org-agenda-open-link ()
  19759. "Follow the link in the current line, if any."
  19760. (interactive)
  19761. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  19762. (save-excursion
  19763. (save-restriction
  19764. (narrow-to-region (point-at-bol) (point-at-eol))
  19765. (org-open-at-point))))
  19766. (defun org-agenda-copy-local-variable (var)
  19767. "Get a variable from a referenced buffer and install it here."
  19768. (let ((m (get-text-property (point) 'org-marker)))
  19769. (when (and m (buffer-live-p (marker-buffer m)))
  19770. (org-set-local var (with-current-buffer (marker-buffer m)
  19771. (symbol-value var))))))
  19772. (defun org-agenda-switch-to (&optional delete-other-windows)
  19773. "Go to the Org-mode file which contains the item at point."
  19774. (interactive)
  19775. (let* ((marker (or (get-text-property (point) 'org-marker)
  19776. (org-agenda-error)))
  19777. (buffer (marker-buffer marker))
  19778. (pos (marker-position marker)))
  19779. (switch-to-buffer buffer)
  19780. (and delete-other-windows (delete-other-windows))
  19781. (widen)
  19782. (goto-char pos)
  19783. (when (org-mode-p)
  19784. (org-show-context 'agenda)
  19785. (save-excursion
  19786. (and (outline-next-heading)
  19787. (org-flag-heading nil)))))) ; show the next heading
  19788. (defun org-agenda-goto-mouse (ev)
  19789. "Go to the Org-mode file which contains the item at the mouse click."
  19790. (interactive "e")
  19791. (mouse-set-point ev)
  19792. (org-agenda-goto))
  19793. (defun org-agenda-show ()
  19794. "Display the Org-mode file which contains the item at point."
  19795. (interactive)
  19796. (let ((win (selected-window)))
  19797. (org-agenda-goto t)
  19798. (select-window win)))
  19799. (defun org-agenda-recenter (arg)
  19800. "Display the Org-mode file which contains the item at point and recenter."
  19801. (interactive "P")
  19802. (let ((win (selected-window)))
  19803. (org-agenda-goto t)
  19804. (recenter arg)
  19805. (select-window win)))
  19806. (defun org-agenda-show-mouse (ev)
  19807. "Display the Org-mode file which contains the item at the mouse click."
  19808. (interactive "e")
  19809. (mouse-set-point ev)
  19810. (org-agenda-show))
  19811. (defun org-agenda-check-no-diary ()
  19812. "Check if the entry is a diary link and abort if yes."
  19813. (if (get-text-property (point) 'org-agenda-diary-link)
  19814. (org-agenda-error)))
  19815. (defun org-agenda-error ()
  19816. (error "Command not allowed in this line"))
  19817. (defun org-agenda-tree-to-indirect-buffer ()
  19818. "Show the subtree corresponding to the current entry in an indirect buffer.
  19819. This calls the command `org-tree-to-indirect-buffer' from the original
  19820. Org-mode buffer.
  19821. With numerical prefix arg ARG, go up to this level and then take that tree.
  19822. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  19823. dedicated frame)."
  19824. (interactive)
  19825. (org-agenda-check-no-diary)
  19826. (let* ((marker (or (get-text-property (point) 'org-marker)
  19827. (org-agenda-error)))
  19828. (buffer (marker-buffer marker))
  19829. (pos (marker-position marker)))
  19830. (with-current-buffer buffer
  19831. (save-excursion
  19832. (goto-char pos)
  19833. (call-interactively 'org-tree-to-indirect-buffer)))))
  19834. (defvar org-last-heading-marker (make-marker)
  19835. "Marker pointing to the headline that last changed its TODO state
  19836. by a remote command from the agenda.")
  19837. (defun org-agenda-todo-nextset ()
  19838. "Switch TODO entry to next sequence."
  19839. (interactive)
  19840. (org-agenda-todo 'nextset))
  19841. (defun org-agenda-todo-previousset ()
  19842. "Switch TODO entry to previous sequence."
  19843. (interactive)
  19844. (org-agenda-todo 'previousset))
  19845. (defun org-agenda-todo (&optional arg)
  19846. "Cycle TODO state of line at point, also in Org-mode file.
  19847. This changes the line at point, all other lines in the agenda referring to
  19848. the same tree node, and the headline of the tree node in the Org-mode file."
  19849. (interactive "P")
  19850. (org-agenda-check-no-diary)
  19851. (let* ((col (current-column))
  19852. (marker (or (get-text-property (point) 'org-marker)
  19853. (org-agenda-error)))
  19854. (buffer (marker-buffer marker))
  19855. (pos (marker-position marker))
  19856. (hdmarker (get-text-property (point) 'org-hd-marker))
  19857. (inhibit-read-only t)
  19858. newhead)
  19859. (org-with-remote-undo buffer
  19860. (with-current-buffer buffer
  19861. (widen)
  19862. (goto-char pos)
  19863. (org-show-context 'agenda)
  19864. (save-excursion
  19865. (and (outline-next-heading)
  19866. (org-flag-heading nil))) ; show the next heading
  19867. (org-todo arg)
  19868. (and (bolp) (forward-char 1))
  19869. (setq newhead (org-get-heading))
  19870. (save-excursion
  19871. (org-back-to-heading)
  19872. (move-marker org-last-heading-marker (point))))
  19873. (beginning-of-line 1)
  19874. (save-excursion
  19875. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  19876. (move-to-column col))))
  19877. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  19878. "Change all lines in the agenda buffer which match HDMARKER.
  19879. The new content of the line will be NEWHEAD (as modified by
  19880. `org-format-agenda-item'). HDMARKER is checked with
  19881. `equal' against all `org-hd-marker' text properties in the file.
  19882. If FIXFACE is non-nil, the face of each item is modified acording to
  19883. the new TODO state."
  19884. (let* ((inhibit-read-only t)
  19885. props m pl undone-face done-face finish new dotime cat tags)
  19886. (save-excursion
  19887. (goto-char (point-max))
  19888. (beginning-of-line 1)
  19889. (while (not finish)
  19890. (setq finish (bobp))
  19891. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  19892. (equal m hdmarker))
  19893. (setq props (text-properties-at (point))
  19894. dotime (get-text-property (point) 'dotime)
  19895. cat (get-text-property (point) 'org-category)
  19896. tags (get-text-property (point) 'tags)
  19897. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  19898. pl (get-text-property (point) 'prefix-length)
  19899. undone-face (get-text-property (point) 'undone-face)
  19900. done-face (get-text-property (point) 'done-face))
  19901. (move-to-column pl)
  19902. (cond
  19903. ((equal new "")
  19904. (beginning-of-line 1)
  19905. (and (looking-at ".*\n?") (replace-match "")))
  19906. ((looking-at ".*")
  19907. (replace-match new t t)
  19908. (beginning-of-line 1)
  19909. (add-text-properties (point-at-bol) (point-at-eol) props)
  19910. (when fixface
  19911. (add-text-properties
  19912. (point-at-bol) (point-at-eol)
  19913. (list 'face
  19914. (if org-last-todo-state-is-todo
  19915. undone-face done-face))))
  19916. (org-agenda-highlight-todo 'line)
  19917. (beginning-of-line 1))
  19918. (t (error "Line update did not work"))))
  19919. (beginning-of-line 0)))
  19920. (org-finalize-agenda)))
  19921. (defun org-agenda-align-tags (&optional line)
  19922. "Align all tags in agenda items to `org-agenda-tags-column'."
  19923. (let ((inhibit-read-only t) l c)
  19924. (save-excursion
  19925. (goto-char (if line (point-at-bol) (point-min)))
  19926. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  19927. (if line (point-at-eol) nil) t)
  19928. (add-text-properties
  19929. (match-beginning 2) (match-end 2)
  19930. (list 'face (list 'org-tag (get-text-property
  19931. (match-beginning 2) 'face))))
  19932. (setq l (- (match-end 2) (match-beginning 2))
  19933. c (if (< org-agenda-tags-column 0)
  19934. (- (abs org-agenda-tags-column) l)
  19935. org-agenda-tags-column))
  19936. (delete-region (match-beginning 1) (match-end 1))
  19937. (goto-char (match-beginning 1))
  19938. (insert (org-add-props
  19939. (make-string (max 1 (- c (current-column))) ?\ )
  19940. (text-properties-at (point))))))))
  19941. (defun org-agenda-priority-up ()
  19942. "Increase the priority of line at point, also in Org-mode file."
  19943. (interactive)
  19944. (org-agenda-priority 'up))
  19945. (defun org-agenda-priority-down ()
  19946. "Decrease the priority of line at point, also in Org-mode file."
  19947. (interactive)
  19948. (org-agenda-priority 'down))
  19949. (defun org-agenda-priority (&optional force-direction)
  19950. "Set the priority of line at point, also in Org-mode file.
  19951. This changes the line at point, all other lines in the agenda referring to
  19952. the same tree node, and the headline of the tree node in the Org-mode file."
  19953. (interactive)
  19954. (org-agenda-check-no-diary)
  19955. (let* ((marker (or (get-text-property (point) 'org-marker)
  19956. (org-agenda-error)))
  19957. (hdmarker (get-text-property (point) 'org-hd-marker))
  19958. (buffer (marker-buffer hdmarker))
  19959. (pos (marker-position hdmarker))
  19960. (inhibit-read-only t)
  19961. newhead)
  19962. (org-with-remote-undo buffer
  19963. (with-current-buffer buffer
  19964. (widen)
  19965. (goto-char pos)
  19966. (org-show-context 'agenda)
  19967. (save-excursion
  19968. (and (outline-next-heading)
  19969. (org-flag-heading nil))) ; show the next heading
  19970. (funcall 'org-priority force-direction)
  19971. (end-of-line 1)
  19972. (setq newhead (org-get-heading)))
  19973. (org-agenda-change-all-lines newhead hdmarker)
  19974. (beginning-of-line 1))))
  19975. (defun org-get-tags-at (&optional pos)
  19976. "Get a list of all headline tags applicable at POS.
  19977. POS defaults to point. If tags are inherited, the list contains
  19978. the targets in the same sequence as the headlines appear, i.e.
  19979. the tags of the current headline come last."
  19980. (interactive)
  19981. (let (tags lastpos)
  19982. (save-excursion
  19983. (save-restriction
  19984. (widen)
  19985. (goto-char (or pos (point)))
  19986. (save-match-data
  19987. (org-back-to-heading t)
  19988. (condition-case nil
  19989. (while (not (equal lastpos (point)))
  19990. (setq lastpos (point))
  19991. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  19992. (setq tags (append (org-split-string
  19993. (org-match-string-no-properties 1) ":")
  19994. tags)))
  19995. (or org-use-tag-inheritance (error ""))
  19996. (org-up-heading-all 1))
  19997. (error nil))))
  19998. tags)))
  19999. ;; FIXME: should fix the tags property of the agenda line.
  20000. (defun org-agenda-set-tags ()
  20001. "Set tags for the current headline."
  20002. (interactive)
  20003. (org-agenda-check-no-diary)
  20004. (if (and (org-region-active-p) (interactive-p))
  20005. (call-interactively 'org-change-tag-in-region)
  20006. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20007. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20008. (org-agenda-error)))
  20009. (buffer (marker-buffer hdmarker))
  20010. (pos (marker-position hdmarker))
  20011. (inhibit-read-only t)
  20012. newhead)
  20013. (org-with-remote-undo buffer
  20014. (with-current-buffer buffer
  20015. (widen)
  20016. (goto-char pos)
  20017. (save-excursion
  20018. (org-show-context 'agenda))
  20019. (save-excursion
  20020. (and (outline-next-heading)
  20021. (org-flag-heading nil))) ; show the next heading
  20022. (goto-char pos)
  20023. (call-interactively 'org-set-tags)
  20024. (end-of-line 1)
  20025. (setq newhead (org-get-heading)))
  20026. (org-agenda-change-all-lines newhead hdmarker)
  20027. (beginning-of-line 1)))))
  20028. (defun org-agenda-toggle-archive-tag ()
  20029. "Toggle the archive tag for the current entry."
  20030. (interactive)
  20031. (org-agenda-check-no-diary)
  20032. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20033. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20034. (org-agenda-error)))
  20035. (buffer (marker-buffer hdmarker))
  20036. (pos (marker-position hdmarker))
  20037. (inhibit-read-only t)
  20038. newhead)
  20039. (org-with-remote-undo buffer
  20040. (with-current-buffer buffer
  20041. (widen)
  20042. (goto-char pos)
  20043. (org-show-context 'agenda)
  20044. (save-excursion
  20045. (and (outline-next-heading)
  20046. (org-flag-heading nil))) ; show the next heading
  20047. (call-interactively 'org-toggle-archive-tag)
  20048. (end-of-line 1)
  20049. (setq newhead (org-get-heading)))
  20050. (org-agenda-change-all-lines newhead hdmarker)
  20051. (beginning-of-line 1))))
  20052. (defun org-agenda-date-later (arg &optional what)
  20053. "Change the date of this item to one day later."
  20054. (interactive "p")
  20055. (org-agenda-check-type t 'agenda 'timeline)
  20056. (org-agenda-check-no-diary)
  20057. (let* ((marker (or (get-text-property (point) 'org-marker)
  20058. (org-agenda-error)))
  20059. (buffer (marker-buffer marker))
  20060. (pos (marker-position marker)))
  20061. (org-with-remote-undo buffer
  20062. (with-current-buffer buffer
  20063. (widen)
  20064. (goto-char pos)
  20065. (if (not (org-at-timestamp-p))
  20066. (error "Cannot find time stamp"))
  20067. (org-timestamp-change arg (or what 'day)))
  20068. (org-agenda-show-new-time marker org-last-changed-timestamp))
  20069. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  20070. (defun org-agenda-date-earlier (arg &optional what)
  20071. "Change the date of this item to one day earlier."
  20072. (interactive "p")
  20073. (org-agenda-date-later (- arg) what))
  20074. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  20075. "Show new date stamp via text properties."
  20076. ;; We use text properties to make this undoable
  20077. (let ((inhibit-read-only t))
  20078. (setq stamp (concat " " prefix " => " stamp))
  20079. (save-excursion
  20080. (goto-char (point-max))
  20081. (while (not (bobp))
  20082. (when (equal marker (get-text-property (point) 'org-marker))
  20083. (move-to-column (- (window-width) (length stamp)) t)
  20084. (if (featurep 'xemacs)
  20085. ;; Use `duplicable' property to trigger undo recording
  20086. (let ((ex (make-extent nil nil))
  20087. (gl (make-glyph stamp)))
  20088. (set-glyph-face gl 'secondary-selection)
  20089. (set-extent-properties
  20090. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  20091. (insert-extent ex (1- (point)) (point-at-eol)))
  20092. (add-text-properties
  20093. (1- (point)) (point-at-eol)
  20094. (list 'display (org-add-props stamp nil
  20095. 'face 'secondary-selection))))
  20096. (beginning-of-line 1))
  20097. (beginning-of-line 0)))))
  20098. (defun org-agenda-date-prompt (arg)
  20099. "Change the date of this item. Date is prompted for, with default today.
  20100. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  20101. be used to request time specification in the time stamp."
  20102. (interactive "P")
  20103. (org-agenda-check-type t 'agenda 'timeline)
  20104. (org-agenda-check-no-diary)
  20105. (let* ((marker (or (get-text-property (point) 'org-marker)
  20106. (org-agenda-error)))
  20107. (buffer (marker-buffer marker))
  20108. (pos (marker-position marker)))
  20109. (org-with-remote-undo buffer
  20110. (with-current-buffer buffer
  20111. (widen)
  20112. (goto-char pos)
  20113. (if (not (org-at-timestamp-p))
  20114. (error "Cannot find time stamp"))
  20115. (org-time-stamp arg)
  20116. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  20117. (defun org-agenda-schedule (arg)
  20118. "Schedule the item at point."
  20119. (interactive "P")
  20120. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20121. (org-agenda-check-no-diary)
  20122. (let* ((marker (or (get-text-property (point) 'org-marker)
  20123. (org-agenda-error)))
  20124. (buffer (marker-buffer marker))
  20125. (pos (marker-position marker))
  20126. (org-insert-labeled-timestamps-at-point nil)
  20127. ts)
  20128. (org-with-remote-undo buffer
  20129. (with-current-buffer buffer
  20130. (widen)
  20131. (goto-char pos)
  20132. (setq ts (org-schedule arg)))
  20133. (org-agenda-show-new-time marker ts "S"))
  20134. (message "Item scheduled for %s" ts)))
  20135. (defun org-agenda-deadline (arg)
  20136. "Schedule the item at point."
  20137. (interactive "P")
  20138. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20139. (org-agenda-check-no-diary)
  20140. (let* ((marker (or (get-text-property (point) 'org-marker)
  20141. (org-agenda-error)))
  20142. (buffer (marker-buffer marker))
  20143. (pos (marker-position marker))
  20144. (org-insert-labeled-timestamps-at-point nil)
  20145. ts)
  20146. (org-with-remote-undo buffer
  20147. (with-current-buffer buffer
  20148. (widen)
  20149. (goto-char pos)
  20150. (setq ts (org-deadline arg)))
  20151. (org-agenda-show-new-time marker ts "S"))
  20152. (message "Deadline for this item set to %s" ts)))
  20153. (defun org-get-heading (&optional no-tags)
  20154. "Return the heading of the current entry, without the stars."
  20155. (save-excursion
  20156. (org-back-to-heading t)
  20157. (if (looking-at
  20158. (if no-tags
  20159. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  20160. "\\*+[ \t]+\\([^\r\n]*\\)"))
  20161. (match-string 1) "")))
  20162. (defun org-agenda-clock-in (&optional arg)
  20163. "Start the clock on the currently selected item."
  20164. (interactive "P")
  20165. (org-agenda-check-no-diary)
  20166. (let* ((marker (or (get-text-property (point) 'org-marker)
  20167. (org-agenda-error)))
  20168. (pos (marker-position marker)))
  20169. (org-with-remote-undo (marker-buffer marker)
  20170. (with-current-buffer (marker-buffer marker)
  20171. (widen)
  20172. (goto-char pos)
  20173. (org-clock-in)))))
  20174. (defun org-agenda-clock-out (&optional arg)
  20175. "Stop the currently running clock."
  20176. (interactive "P")
  20177. (unless (marker-buffer org-clock-marker)
  20178. (error "No running clock"))
  20179. (org-with-remote-undo (marker-buffer org-clock-marker)
  20180. (org-clock-out)))
  20181. (defun org-agenda-clock-cancel (&optional arg)
  20182. "Cancel the currently running clock."
  20183. (interactive "P")
  20184. (unless (marker-buffer org-clock-marker)
  20185. (error "No running clock"))
  20186. (org-with-remote-undo (marker-buffer org-clock-marker)
  20187. (org-clock-cancel)))
  20188. (defun org-agenda-diary-entry ()
  20189. "Make a diary entry, like the `i' command from the calendar.
  20190. All the standard commands work: block, weekly etc."
  20191. (interactive)
  20192. (org-agenda-check-type t 'agenda 'timeline)
  20193. (require 'diary-lib)
  20194. (let* ((char (progn
  20195. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  20196. (read-char-exclusive)))
  20197. (cmd (cdr (assoc char
  20198. '((?d . insert-diary-entry)
  20199. (?w . insert-weekly-diary-entry)
  20200. (?m . insert-monthly-diary-entry)
  20201. (?y . insert-yearly-diary-entry)
  20202. (?a . insert-anniversary-diary-entry)
  20203. (?b . insert-block-diary-entry)
  20204. (?c . insert-cyclic-diary-entry)))))
  20205. (oldf (symbol-function 'calendar-cursor-to-date))
  20206. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  20207. (point (point))
  20208. (mark (or (mark t) (point))))
  20209. (unless cmd
  20210. (error "No command associated with <%c>" char))
  20211. (unless (and (get-text-property point 'day)
  20212. (or (not (equal ?b char))
  20213. (get-text-property mark 'day)))
  20214. (error "Don't know which date to use for diary entry"))
  20215. ;; We implement this by hacking the `calendar-cursor-to-date' function
  20216. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  20217. (let ((calendar-mark-ring
  20218. (list (calendar-gregorian-from-absolute
  20219. (or (get-text-property mark 'day)
  20220. (get-text-property point 'day))))))
  20221. (unwind-protect
  20222. (progn
  20223. (fset 'calendar-cursor-to-date
  20224. (lambda (&optional error)
  20225. (calendar-gregorian-from-absolute
  20226. (get-text-property point 'day))))
  20227. (call-interactively cmd))
  20228. (fset 'calendar-cursor-to-date oldf)))))
  20229. (defun org-agenda-execute-calendar-command (cmd)
  20230. "Execute a calendar command from the agenda, with the date associated to
  20231. the cursor position."
  20232. (org-agenda-check-type t 'agenda 'timeline)
  20233. (require 'diary-lib)
  20234. (unless (get-text-property (point) 'day)
  20235. (error "Don't know which date to use for calendar command"))
  20236. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  20237. (point (point))
  20238. (date (calendar-gregorian-from-absolute
  20239. (get-text-property point 'day)))
  20240. ;; the following 3 vars are needed in the calendar
  20241. (displayed-day (extract-calendar-day date))
  20242. (displayed-month (extract-calendar-month date))
  20243. (displayed-year (extract-calendar-year date)))
  20244. (unwind-protect
  20245. (progn
  20246. (fset 'calendar-cursor-to-date
  20247. (lambda (&optional error)
  20248. (calendar-gregorian-from-absolute
  20249. (get-text-property point 'day))))
  20250. (call-interactively cmd))
  20251. (fset 'calendar-cursor-to-date oldf))))
  20252. (defun org-agenda-phases-of-moon ()
  20253. "Display the phases of the moon for the 3 months around the cursor date."
  20254. (interactive)
  20255. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  20256. (defun org-agenda-holidays ()
  20257. "Display the holidays for the 3 months around the cursor date."
  20258. (interactive)
  20259. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  20260. (defun org-agenda-sunrise-sunset (arg)
  20261. "Display sunrise and sunset for the cursor date.
  20262. Latitude and longitude can be specified with the variables
  20263. `calendar-latitude' and `calendar-longitude'. When called with prefix
  20264. argument, latitude and longitude will be prompted for."
  20265. (interactive "P")
  20266. (let ((calendar-longitude (if arg nil calendar-longitude))
  20267. (calendar-latitude (if arg nil calendar-latitude))
  20268. (calendar-location-name
  20269. (if arg "the given coordinates" calendar-location-name)))
  20270. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  20271. (defun org-agenda-goto-calendar ()
  20272. "Open the Emacs calendar with the date at the cursor."
  20273. (interactive)
  20274. (org-agenda-check-type t 'agenda 'timeline)
  20275. (let* ((day (or (get-text-property (point) 'day)
  20276. (error "Don't know which date to open in calendar")))
  20277. (date (calendar-gregorian-from-absolute day))
  20278. (calendar-move-hook nil)
  20279. (view-calendar-holidays-initially nil)
  20280. (view-diary-entries-initially nil))
  20281. (calendar)
  20282. (calendar-goto-date date)))
  20283. (defun org-calendar-goto-agenda ()
  20284. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  20285. This is a command that has to be installed in `calendar-mode-map'."
  20286. (interactive)
  20287. (org-agenda-list nil (calendar-absolute-from-gregorian
  20288. (calendar-cursor-to-date))
  20289. nil))
  20290. (defun org-agenda-convert-date ()
  20291. (interactive)
  20292. (org-agenda-check-type t 'agenda 'timeline)
  20293. (let ((day (get-text-property (point) 'day))
  20294. date s)
  20295. (unless day
  20296. (error "Don't know which date to convert"))
  20297. (setq date (calendar-gregorian-from-absolute day))
  20298. (setq s (concat
  20299. "Gregorian: " (calendar-date-string date) "\n"
  20300. "ISO: " (calendar-iso-date-string date) "\n"
  20301. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  20302. "Julian: " (calendar-julian-date-string date) "\n"
  20303. "Astron. JD: " (calendar-astro-date-string date)
  20304. " (Julian date number at noon UTC)\n"
  20305. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  20306. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  20307. "French: " (calendar-french-date-string date) "\n"
  20308. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  20309. "Mayan: " (calendar-mayan-date-string date) "\n"
  20310. "Coptic: " (calendar-coptic-date-string date) "\n"
  20311. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  20312. "Persian: " (calendar-persian-date-string date) "\n"
  20313. "Chinese: " (calendar-chinese-date-string date) "\n"))
  20314. (with-output-to-temp-buffer "*Dates*"
  20315. (princ s))
  20316. (if (fboundp 'fit-window-to-buffer)
  20317. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  20318. ;;;; Embedded LaTeX
  20319. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  20320. "Keymap for the minor `org-cdlatex-mode'.")
  20321. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  20322. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  20323. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  20324. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  20325. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  20326. (defvar org-cdlatex-texmathp-advice-is-done nil
  20327. "Flag remembering if we have applied the advice to texmathp already.")
  20328. (define-minor-mode org-cdlatex-mode
  20329. "Toggle the minor `org-cdlatex-mode'.
  20330. This mode supports entering LaTeX environment and math in LaTeX fragments
  20331. in Org-mode.
  20332. \\{org-cdlatex-mode-map}"
  20333. nil " OCDL" nil
  20334. (when org-cdlatex-mode (require 'cdlatex))
  20335. (unless org-cdlatex-texmathp-advice-is-done
  20336. (setq org-cdlatex-texmathp-advice-is-done t)
  20337. (defadvice texmathp (around org-math-always-on activate)
  20338. "Always return t in org-mode buffers.
  20339. This is because we want to insert math symbols without dollars even outside
  20340. the LaTeX math segments. If Orgmode thinks that point is actually inside
  20341. en embedded LaTeX fragement, let texmathp do its job.
  20342. \\[org-cdlatex-mode-map]"
  20343. (interactive)
  20344. (let (p)
  20345. (cond
  20346. ((not (org-mode-p)) ad-do-it)
  20347. ((eq this-command 'cdlatex-math-symbol)
  20348. (setq ad-return-value t
  20349. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  20350. (t
  20351. (let ((p (org-inside-LaTeX-fragment-p)))
  20352. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  20353. (setq ad-return-value t
  20354. texmathp-why '("Org-mode embedded math" . 0))
  20355. (if p ad-do-it)))))))))
  20356. (defun turn-on-org-cdlatex ()
  20357. "Unconditionally turn on `org-cdlatex-mode'."
  20358. (org-cdlatex-mode 1))
  20359. (defun org-inside-LaTeX-fragment-p ()
  20360. "Test if point is inside a LaTeX fragment.
  20361. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  20362. sequence appearing also before point.
  20363. Even though the matchers for math are configurable, this function assumes
  20364. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  20365. delimiters are skipped when they have been removed by customization.
  20366. The return value is nil, or a cons cell with the delimiter and
  20367. and the position of this delimiter.
  20368. This function does a reasonably good job, but can locally be fooled by
  20369. for example currency specifications. For example it will assume being in
  20370. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  20371. fragments that are properly closed, but during editing, we have to live
  20372. with the uncertainty caused by missing closing delimiters. This function
  20373. looks only before point, not after."
  20374. (catch 'exit
  20375. (let ((pos (point))
  20376. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  20377. (lim (progn
  20378. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  20379. (point)))
  20380. dd-on str (start 0) m re)
  20381. (goto-char pos)
  20382. (when dodollar
  20383. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  20384. re (nth 1 (assoc "$" org-latex-regexps)))
  20385. (while (string-match re str start)
  20386. (cond
  20387. ((= (match-end 0) (length str))
  20388. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  20389. ((= (match-end 0) (- (length str) 5))
  20390. (throw 'exit nil))
  20391. (t (setq start (match-end 0))))))
  20392. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  20393. (goto-char pos)
  20394. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  20395. (and (match-beginning 2) (throw 'exit nil))
  20396. ;; count $$
  20397. (while (re-search-backward "\\$\\$" lim t)
  20398. (setq dd-on (not dd-on)))
  20399. (goto-char pos)
  20400. (if dd-on (cons "$$" m))))))
  20401. (defun org-try-cdlatex-tab ()
  20402. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  20403. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  20404. - inside a LaTeX fragment, or
  20405. - after the first word in a line, where an abbreviation expansion could
  20406. insert a LaTeX environment."
  20407. (when org-cdlatex-mode
  20408. (cond
  20409. ((save-excursion
  20410. (skip-chars-backward "a-zA-Z0-9*")
  20411. (skip-chars-backward " \t")
  20412. (bolp))
  20413. (cdlatex-tab) t)
  20414. ((org-inside-LaTeX-fragment-p)
  20415. (cdlatex-tab) t)
  20416. (t nil))))
  20417. (defun org-cdlatex-underscore-caret (&optional arg)
  20418. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  20419. Revert to the normal definition outside of these fragments."
  20420. (interactive "P")
  20421. (if (org-inside-LaTeX-fragment-p)
  20422. (call-interactively 'cdlatex-sub-superscript)
  20423. (let (org-cdlatex-mode)
  20424. (call-interactively (key-binding (vector last-input-event))))))
  20425. (defun org-cdlatex-math-modify (&optional arg)
  20426. "Execute `cdlatex-math-modify' in LaTeX fragments.
  20427. Revert to the normal definition outside of these fragments."
  20428. (interactive "P")
  20429. (if (org-inside-LaTeX-fragment-p)
  20430. (call-interactively 'cdlatex-math-modify)
  20431. (let (org-cdlatex-mode)
  20432. (call-interactively (key-binding (vector last-input-event))))))
  20433. (defvar org-latex-fragment-image-overlays nil
  20434. "List of overlays carrying the images of latex fragments.")
  20435. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  20436. (defun org-remove-latex-fragment-image-overlays ()
  20437. "Remove all overlays with LaTeX fragment images in current buffer."
  20438. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  20439. (setq org-latex-fragment-image-overlays nil))
  20440. (defun org-preview-latex-fragment (&optional subtree)
  20441. "Preview the LaTeX fragment at point, or all locally or globally.
  20442. If the cursor is in a LaTeX fragment, create the image and overlay
  20443. it over the source code. If there is no fragment at point, display
  20444. all fragments in the current text, from one headline to the next. With
  20445. prefix SUBTREE, display all fragments in the current subtree. With a
  20446. double prefix `C-u C-u', or when the cursor is before the first headline,
  20447. display all fragments in the buffer.
  20448. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  20449. (interactive "P")
  20450. (org-remove-latex-fragment-image-overlays)
  20451. (save-excursion
  20452. (save-restriction
  20453. (let (beg end at msg)
  20454. (cond
  20455. ((or (equal subtree '(16))
  20456. (not (save-excursion
  20457. (re-search-backward (concat "^" outline-regexp) nil t))))
  20458. (setq beg (point-min) end (point-max)
  20459. msg "Creating images for buffer...%s"))
  20460. ((equal subtree '(4))
  20461. (org-back-to-heading)
  20462. (setq beg (point) end (org-end-of-subtree t)
  20463. msg "Creating images for subtree...%s"))
  20464. (t
  20465. (if (setq at (org-inside-LaTeX-fragment-p))
  20466. (goto-char (max (point-min) (- (cdr at) 2)))
  20467. (org-back-to-heading))
  20468. (setq beg (point) end (progn (outline-next-heading) (point))
  20469. msg (if at "Creating image...%s"
  20470. "Creating images for entry...%s"))))
  20471. (message msg "")
  20472. (narrow-to-region beg end)
  20473. (goto-char beg)
  20474. (org-format-latex
  20475. (concat "ltxpng/" (file-name-sans-extension
  20476. (file-name-nondirectory
  20477. buffer-file-name)))
  20478. default-directory 'overlays msg at 'forbuffer)
  20479. (message msg "done. Use `C-c C-c' to remove images.")))))
  20480. (defvar org-latex-regexps
  20481. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  20482. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  20483. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  20484. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  20485. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  20486. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  20487. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  20488. "Regular expressions for matching embedded LaTeX.")
  20489. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  20490. "Replace LaTeX fragments with links to an image, and produce images."
  20491. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  20492. (let* ((prefixnodir (file-name-nondirectory prefix))
  20493. (absprefix (expand-file-name prefix dir))
  20494. (todir (file-name-directory absprefix))
  20495. (opt org-format-latex-options)
  20496. (matchers (plist-get opt :matchers))
  20497. (re-list org-latex-regexps)
  20498. (cnt 0) txt link beg end re e checkdir
  20499. m n block linkfile movefile ov)
  20500. ;; Check if there are old images files with this prefix, and remove them
  20501. (when (file-directory-p todir)
  20502. (mapc 'delete-file
  20503. (directory-files
  20504. todir 'full
  20505. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  20506. ;; Check the different regular expressions
  20507. (while (setq e (pop re-list))
  20508. (setq m (car e) re (nth 1 e) n (nth 2 e)
  20509. block (if (nth 3 e) "\n\n" ""))
  20510. (when (member m matchers)
  20511. (goto-char (point-min))
  20512. (while (re-search-forward re nil t)
  20513. (when (or (not at) (equal (cdr at) (match-beginning n)))
  20514. (setq txt (match-string n)
  20515. beg (match-beginning n) end (match-end n)
  20516. cnt (1+ cnt)
  20517. linkfile (format "%s_%04d.png" prefix cnt)
  20518. movefile (format "%s_%04d.png" absprefix cnt)
  20519. link (concat block "[[file:" linkfile "]]" block))
  20520. (if msg (message msg cnt))
  20521. (goto-char beg)
  20522. (unless checkdir ; make sure the directory exists
  20523. (setq checkdir t)
  20524. (or (file-directory-p todir) (make-directory todir)))
  20525. (org-create-formula-image
  20526. txt movefile opt forbuffer)
  20527. (if overlays
  20528. (progn
  20529. (setq ov (org-make-overlay beg end))
  20530. (if (featurep 'xemacs)
  20531. (progn
  20532. (org-overlay-put ov 'invisible t)
  20533. (org-overlay-put
  20534. ov 'end-glyph
  20535. (make-glyph (vector 'png :file movefile))))
  20536. (org-overlay-put
  20537. ov 'display
  20538. (list 'image :type 'png :file movefile :ascent 'center)))
  20539. (push ov org-latex-fragment-image-overlays)
  20540. (goto-char end))
  20541. (delete-region beg end)
  20542. (insert link))))))))
  20543. ;; This function borrows from Ganesh Swami's latex2png.el
  20544. (defun org-create-formula-image (string tofile options buffer)
  20545. (let* ((tmpdir (if (featurep 'xemacs)
  20546. (temp-directory)
  20547. temporary-file-directory))
  20548. (texfilebase (make-temp-name
  20549. (expand-file-name "orgtex" tmpdir)))
  20550. (texfile (concat texfilebase ".tex"))
  20551. (dvifile (concat texfilebase ".dvi"))
  20552. (pngfile (concat texfilebase ".png"))
  20553. (fnh (face-attribute 'default :height nil))
  20554. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  20555. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  20556. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  20557. "Black"))
  20558. (bg (or (plist-get options (if buffer :background :html-background))
  20559. "Transparent")))
  20560. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  20561. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  20562. (with-temp-file texfile
  20563. (insert org-format-latex-header
  20564. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  20565. (let ((dir default-directory))
  20566. (condition-case nil
  20567. (progn
  20568. (cd tmpdir)
  20569. (call-process "latex" nil nil nil texfile))
  20570. (error nil))
  20571. (cd dir))
  20572. (if (not (file-exists-p dvifile))
  20573. (progn (message "Failed to create dvi file from %s" texfile) nil)
  20574. (call-process "dvipng" nil nil nil
  20575. "-E" "-fg" fg "-bg" bg
  20576. "-D" dpi
  20577. ;;"-x" scale "-y" scale
  20578. "-T" "tight"
  20579. "-o" pngfile
  20580. dvifile)
  20581. (if (not (file-exists-p pngfile))
  20582. (progn (message "Failed to create png file from %s" texfile) nil)
  20583. ;; Use the requested file name and clean up
  20584. (copy-file pngfile tofile 'replace)
  20585. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  20586. (delete-file (concat texfilebase e)))
  20587. pngfile))))
  20588. (defun org-dvipng-color (attr)
  20589. "Return an rgb color specification for dvipng."
  20590. (apply 'format "rgb %s %s %s"
  20591. (mapcar 'org-normalize-color
  20592. (color-values (face-attribute 'default attr nil)))))
  20593. (defun org-normalize-color (value)
  20594. "Return string to be used as color value for an RGB component."
  20595. (format "%g" (/ value 65535.0)))
  20596. ;;;; Exporting
  20597. ;;; Variables, constants, and parameter plists
  20598. (defconst org-level-max 20)
  20599. (defvar org-export-html-preamble nil
  20600. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  20601. (defvar org-export-html-postamble nil
  20602. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  20603. (defvar org-export-html-auto-preamble t
  20604. "Should default preamble be inserted? Set by publishing functions.")
  20605. (defvar org-export-html-auto-postamble t
  20606. "Should default postamble be inserted? Set by publishing functions.")
  20607. (defvar org-current-export-file nil) ; dynamically scoped parameter
  20608. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  20609. (defconst org-export-plist-vars
  20610. '((:language . org-export-default-language)
  20611. (:customtime . org-display-custom-times)
  20612. (:headline-levels . org-export-headline-levels)
  20613. (:section-numbers . org-export-with-section-numbers)
  20614. (:table-of-contents . org-export-with-toc)
  20615. (:preserve-breaks . org-export-preserve-breaks)
  20616. (:archived-trees . org-export-with-archived-trees)
  20617. (:emphasize . org-export-with-emphasize)
  20618. (:sub-superscript . org-export-with-sub-superscripts)
  20619. (:special-strings . org-export-with-special-strings)
  20620. (:footnotes . org-export-with-footnotes)
  20621. (:drawers . org-export-with-drawers)
  20622. (:tags . org-export-with-tags)
  20623. (:TeX-macros . org-export-with-TeX-macros)
  20624. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  20625. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  20626. (:fixed-width . org-export-with-fixed-width)
  20627. (:timestamps . org-export-with-timestamps)
  20628. (:author-info . org-export-author-info)
  20629. (:time-stamp-file . org-export-time-stamp-file)
  20630. (:tables . org-export-with-tables)
  20631. (:table-auto-headline . org-export-highlight-first-table-line)
  20632. (:style . org-export-html-style)
  20633. (:agenda-style . org-agenda-export-html-style) ;; FIXME: Does this work????
  20634. (:convert-org-links . org-export-html-link-org-files-as-html)
  20635. (:inline-images . org-export-html-inline-images)
  20636. (:html-extension . org-export-html-extension)
  20637. (:html-table-tag . org-export-html-table-tag)
  20638. (:expand-quoted-html . org-export-html-expand)
  20639. (:timestamp . org-export-html-with-timestamp)
  20640. (:publishing-directory . org-export-publishing-directory)
  20641. (:preamble . org-export-html-preamble)
  20642. (:postamble . org-export-html-postamble)
  20643. (:auto-preamble . org-export-html-auto-preamble)
  20644. (:auto-postamble . org-export-html-auto-postamble)
  20645. (:author . user-full-name)
  20646. (:email . user-mail-address)))
  20647. (defun org-default-export-plist ()
  20648. "Return the property list with default settings for the export variables."
  20649. (let ((l org-export-plist-vars) rtn e)
  20650. (while (setq e (pop l))
  20651. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  20652. rtn))
  20653. (defun org-infile-export-plist ()
  20654. "Return the property list with file-local settings for export."
  20655. (save-excursion
  20656. (goto-char 0)
  20657. (let ((re (org-make-options-regexp
  20658. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  20659. p key val text options)
  20660. (while (re-search-forward re nil t)
  20661. (setq key (org-match-string-no-properties 1)
  20662. val (org-match-string-no-properties 2))
  20663. (cond
  20664. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  20665. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  20666. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  20667. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  20668. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  20669. ((string-equal key "TEXT")
  20670. (setq text (if text (concat text "\n" val) val)))
  20671. ((string-equal key "OPTIONS") (setq options val))))
  20672. (setq p (plist-put p :text text))
  20673. (when options
  20674. (let ((op '(("H" . :headline-levels)
  20675. ("num" . :section-numbers)
  20676. ("toc" . :table-of-contents)
  20677. ("\\n" . :preserve-breaks)
  20678. ("@" . :expand-quoted-html)
  20679. (":" . :fixed-width)
  20680. ("|" . :tables)
  20681. ("^" . :sub-superscript)
  20682. ("-" . :special-strings)
  20683. ("f" . :footnotes)
  20684. ("d" . :drawers)
  20685. ("tags" . :tags)
  20686. ("*" . :emphasize)
  20687. ("TeX" . :TeX-macros)
  20688. ("LaTeX" . :LaTeX-fragments)
  20689. ("skip" . :skip-before-1st-heading)
  20690. ("author" . :author-info)
  20691. ("timestamp" . :time-stamp-file)))
  20692. o)
  20693. (while (setq o (pop op))
  20694. (if (string-match (concat (regexp-quote (car o))
  20695. ":\\([^ \t\n\r;,.]*\\)")
  20696. options)
  20697. (setq p (plist-put p (cdr o)
  20698. (car (read-from-string
  20699. (match-string 1 options)))))))))
  20700. p)))
  20701. (defun org-export-directory (type plist)
  20702. (let* ((val (plist-get plist :publishing-directory))
  20703. (dir (if (listp val)
  20704. (or (cdr (assoc type val)) ".")
  20705. val)))
  20706. dir))
  20707. (defun org-skip-comments (lines)
  20708. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  20709. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  20710. (re2 "^\\(\\*+\\)[ \t\n\r]")
  20711. (case-fold-search nil)
  20712. rtn line level)
  20713. (while (setq line (pop lines))
  20714. (cond
  20715. ((and (string-match re1 line)
  20716. (setq level (- (match-end 1) (match-beginning 1))))
  20717. ;; Beginning of a COMMENT subtree. Skip it.
  20718. (while (and (setq line (pop lines))
  20719. (or (not (string-match re2 line))
  20720. (> (- (match-end 1) (match-beginning 1)) level))))
  20721. (setq lines (cons line lines)))
  20722. ((string-match "^#" line)
  20723. ;; an ordinary comment line
  20724. )
  20725. ((and org-export-table-remove-special-lines
  20726. (string-match "^[ \t]*|" line)
  20727. (or (string-match "^[ \t]*| *[!_^] *|" line)
  20728. (and (string-match "| *<[0-9]+> *|" line)
  20729. (not (string-match "| *[^ <|]" line)))))
  20730. ;; a special table line that should be removed
  20731. )
  20732. (t (setq rtn (cons line rtn)))))
  20733. (nreverse rtn)))
  20734. (defun org-export (&optional arg)
  20735. (interactive)
  20736. (let ((help "[t] insert the export option template
  20737. \[v] limit export to visible part of outline tree
  20738. \[a] export as ASCII
  20739. \[h] export as HTML
  20740. \[H] export as HTML to temporary buffer
  20741. \[R] export region as HTML
  20742. \[b] export as HTML and browse immediately
  20743. \[x] export as XOXO
  20744. \[l] export as LaTeX
  20745. \[L] export as LaTeX to temporary buffer
  20746. \[i] export current file as iCalendar file
  20747. \[I] export all agenda files as iCalendar files
  20748. \[c] export agenda files into combined iCalendar file
  20749. \[F] publish current file
  20750. \[P] publish current project
  20751. \[X] publish... (project will be prompted for)
  20752. \[A] publish all projects")
  20753. (cmds
  20754. '((?t . org-insert-export-options-template)
  20755. (?v . org-export-visible)
  20756. (?a . org-export-as-ascii)
  20757. (?h . org-export-as-html)
  20758. (?b . org-export-as-html-and-open)
  20759. (?H . org-export-as-html-to-buffer)
  20760. (?R . org-export-region-as-html)
  20761. (?x . org-export-as-xoxo)
  20762. (?l . org-export-as-latex)
  20763. (?L . org-export-as-latex-to-buffer)
  20764. (?i . org-export-icalendar-this-file)
  20765. (?I . org-export-icalendar-all-agenda-files)
  20766. (?c . org-export-icalendar-combine-agenda-files)
  20767. (?F . org-publish-current-file)
  20768. (?P . org-publish-current-project)
  20769. (?X . org-publish)
  20770. (?A . org-publish-all)))
  20771. r1 r2 ass)
  20772. (save-window-excursion
  20773. (delete-other-windows)
  20774. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  20775. (princ help))
  20776. (message "Select command: ")
  20777. (setq r1 (read-char-exclusive)))
  20778. (setq r2 (if (< r1 27) (+ r1 96) r1))
  20779. (if (setq ass (assq r2 cmds))
  20780. (call-interactively (cdr ass))
  20781. (error "No command associated with key %c" r1))))
  20782. (defconst org-html-entities
  20783. '(("nbsp")
  20784. ("iexcl")
  20785. ("cent")
  20786. ("pound")
  20787. ("curren")
  20788. ("yen")
  20789. ("brvbar")
  20790. ("vert" . "&#124;")
  20791. ("sect")
  20792. ("uml")
  20793. ("copy")
  20794. ("ordf")
  20795. ("laquo")
  20796. ("not")
  20797. ("shy")
  20798. ("reg")
  20799. ("macr")
  20800. ("deg")
  20801. ("plusmn")
  20802. ("sup2")
  20803. ("sup3")
  20804. ("acute")
  20805. ("micro")
  20806. ("para")
  20807. ("middot")
  20808. ("odot"."o")
  20809. ("star"."*")
  20810. ("cedil")
  20811. ("sup1")
  20812. ("ordm")
  20813. ("raquo")
  20814. ("frac14")
  20815. ("frac12")
  20816. ("frac34")
  20817. ("iquest")
  20818. ("Agrave")
  20819. ("Aacute")
  20820. ("Acirc")
  20821. ("Atilde")
  20822. ("Auml")
  20823. ("Aring") ("AA"."&Aring;")
  20824. ("AElig")
  20825. ("Ccedil")
  20826. ("Egrave")
  20827. ("Eacute")
  20828. ("Ecirc")
  20829. ("Euml")
  20830. ("Igrave")
  20831. ("Iacute")
  20832. ("Icirc")
  20833. ("Iuml")
  20834. ("ETH")
  20835. ("Ntilde")
  20836. ("Ograve")
  20837. ("Oacute")
  20838. ("Ocirc")
  20839. ("Otilde")
  20840. ("Ouml")
  20841. ("times")
  20842. ("Oslash")
  20843. ("Ugrave")
  20844. ("Uacute")
  20845. ("Ucirc")
  20846. ("Uuml")
  20847. ("Yacute")
  20848. ("THORN")
  20849. ("szlig")
  20850. ("agrave")
  20851. ("aacute")
  20852. ("acirc")
  20853. ("atilde")
  20854. ("auml")
  20855. ("aring")
  20856. ("aelig")
  20857. ("ccedil")
  20858. ("egrave")
  20859. ("eacute")
  20860. ("ecirc")
  20861. ("euml")
  20862. ("igrave")
  20863. ("iacute")
  20864. ("icirc")
  20865. ("iuml")
  20866. ("eth")
  20867. ("ntilde")
  20868. ("ograve")
  20869. ("oacute")
  20870. ("ocirc")
  20871. ("otilde")
  20872. ("ouml")
  20873. ("divide")
  20874. ("oslash")
  20875. ("ugrave")
  20876. ("uacute")
  20877. ("ucirc")
  20878. ("uuml")
  20879. ("yacute")
  20880. ("thorn")
  20881. ("yuml")
  20882. ("fnof")
  20883. ("Alpha")
  20884. ("Beta")
  20885. ("Gamma")
  20886. ("Delta")
  20887. ("Epsilon")
  20888. ("Zeta")
  20889. ("Eta")
  20890. ("Theta")
  20891. ("Iota")
  20892. ("Kappa")
  20893. ("Lambda")
  20894. ("Mu")
  20895. ("Nu")
  20896. ("Xi")
  20897. ("Omicron")
  20898. ("Pi")
  20899. ("Rho")
  20900. ("Sigma")
  20901. ("Tau")
  20902. ("Upsilon")
  20903. ("Phi")
  20904. ("Chi")
  20905. ("Psi")
  20906. ("Omega")
  20907. ("alpha")
  20908. ("beta")
  20909. ("gamma")
  20910. ("delta")
  20911. ("epsilon")
  20912. ("varepsilon"."&epsilon;")
  20913. ("zeta")
  20914. ("eta")
  20915. ("theta")
  20916. ("iota")
  20917. ("kappa")
  20918. ("lambda")
  20919. ("mu")
  20920. ("nu")
  20921. ("xi")
  20922. ("omicron")
  20923. ("pi")
  20924. ("rho")
  20925. ("sigmaf") ("varsigma"."&sigmaf;")
  20926. ("sigma")
  20927. ("tau")
  20928. ("upsilon")
  20929. ("phi")
  20930. ("chi")
  20931. ("psi")
  20932. ("omega")
  20933. ("thetasym") ("vartheta"."&thetasym;")
  20934. ("upsih")
  20935. ("piv")
  20936. ("bull") ("bullet"."&bull;")
  20937. ("hellip") ("dots"."&hellip;")
  20938. ("prime")
  20939. ("Prime")
  20940. ("oline")
  20941. ("frasl")
  20942. ("weierp")
  20943. ("image")
  20944. ("real")
  20945. ("trade")
  20946. ("alefsym")
  20947. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  20948. ("uarr") ("uparrow"."&uarr;")
  20949. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  20950. ("darr")("downarrow"."&darr;")
  20951. ("harr") ("leftrightarrow"."&harr;")
  20952. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  20953. ("lArr") ("Leftarrow"."&lArr;")
  20954. ("uArr") ("Uparrow"."&uArr;")
  20955. ("rArr") ("Rightarrow"."&rArr;")
  20956. ("dArr") ("Downarrow"."&dArr;")
  20957. ("hArr") ("Leftrightarrow"."&hArr;")
  20958. ("forall")
  20959. ("part") ("partial"."&part;")
  20960. ("exist") ("exists"."&exist;")
  20961. ("empty") ("emptyset"."&empty;")
  20962. ("nabla")
  20963. ("isin") ("in"."&isin;")
  20964. ("notin")
  20965. ("ni")
  20966. ("prod")
  20967. ("sum")
  20968. ("minus")
  20969. ("lowast") ("ast"."&lowast;")
  20970. ("radic")
  20971. ("prop") ("proptp"."&prop;")
  20972. ("infin") ("infty"."&infin;")
  20973. ("ang") ("angle"."&ang;")
  20974. ("and") ("wedge"."&and;")
  20975. ("or") ("vee"."&or;")
  20976. ("cap")
  20977. ("cup")
  20978. ("int")
  20979. ("there4")
  20980. ("sim")
  20981. ("cong") ("simeq"."&cong;")
  20982. ("asymp")("approx"."&asymp;")
  20983. ("ne") ("neq"."&ne;")
  20984. ("equiv")
  20985. ("le")
  20986. ("ge")
  20987. ("sub") ("subset"."&sub;")
  20988. ("sup") ("supset"."&sup;")
  20989. ("nsub")
  20990. ("sube")
  20991. ("supe")
  20992. ("oplus")
  20993. ("otimes")
  20994. ("perp")
  20995. ("sdot") ("cdot"."&sdot;")
  20996. ("lceil")
  20997. ("rceil")
  20998. ("lfloor")
  20999. ("rfloor")
  21000. ("lang")
  21001. ("rang")
  21002. ("loz") ("Diamond"."&loz;")
  21003. ("spades") ("spadesuit"."&spades;")
  21004. ("clubs") ("clubsuit"."&clubs;")
  21005. ("hearts") ("diamondsuit"."&hearts;")
  21006. ("diams") ("diamondsuit"."&diams;")
  21007. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  21008. ("quot")
  21009. ("amp")
  21010. ("lt")
  21011. ("gt")
  21012. ("OElig")
  21013. ("oelig")
  21014. ("Scaron")
  21015. ("scaron")
  21016. ("Yuml")
  21017. ("circ")
  21018. ("tilde")
  21019. ("ensp")
  21020. ("emsp")
  21021. ("thinsp")
  21022. ("zwnj")
  21023. ("zwj")
  21024. ("lrm")
  21025. ("rlm")
  21026. ("ndash")
  21027. ("mdash")
  21028. ("lsquo")
  21029. ("rsquo")
  21030. ("sbquo")
  21031. ("ldquo")
  21032. ("rdquo")
  21033. ("bdquo")
  21034. ("dagger")
  21035. ("Dagger")
  21036. ("permil")
  21037. ("lsaquo")
  21038. ("rsaquo")
  21039. ("euro")
  21040. ("arccos"."arccos")
  21041. ("arcsin"."arcsin")
  21042. ("arctan"."arctan")
  21043. ("arg"."arg")
  21044. ("cos"."cos")
  21045. ("cosh"."cosh")
  21046. ("cot"."cot")
  21047. ("coth"."coth")
  21048. ("csc"."csc")
  21049. ("deg"."deg")
  21050. ("det"."det")
  21051. ("dim"."dim")
  21052. ("exp"."exp")
  21053. ("gcd"."gcd")
  21054. ("hom"."hom")
  21055. ("inf"."inf")
  21056. ("ker"."ker")
  21057. ("lg"."lg")
  21058. ("lim"."lim")
  21059. ("liminf"."liminf")
  21060. ("limsup"."limsup")
  21061. ("ln"."ln")
  21062. ("log"."log")
  21063. ("max"."max")
  21064. ("min"."min")
  21065. ("Pr"."Pr")
  21066. ("sec"."sec")
  21067. ("sin"."sin")
  21068. ("sinh"."sinh")
  21069. ("sup"."sup")
  21070. ("tan"."tan")
  21071. ("tanh"."tanh")
  21072. )
  21073. "Entities for TeX->HTML translation.
  21074. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  21075. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  21076. In that case, \"\\ent\" will be translated to \"&other;\".
  21077. The list contains HTML entities for Latin-1, Greek and other symbols.
  21078. It is supplemented by a number of commonly used TeX macros with appropriate
  21079. translations. There is currently no way for users to extend this.")
  21080. ;;; General functions for all backends
  21081. (defun org-cleaned-string-for-export (string &rest parameters)
  21082. "Cleanup a buffer STRING so that links can be created safely."
  21083. (interactive)
  21084. (let* ((re-radio (and org-target-link-regexp
  21085. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  21086. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  21087. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  21088. (re-archive (concat ":" org-archive-tag ":"))
  21089. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  21090. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  21091. (htmlp (plist-get parameters :for-html))
  21092. (asciip (plist-get parameters :for-ascii))
  21093. (latexp (plist-get parameters :for-LaTeX))
  21094. (commentsp (plist-get parameters :comments))
  21095. (archived-trees (plist-get parameters :archived-trees))
  21096. (inhibit-read-only t)
  21097. (drawers org-drawers)
  21098. (exp-drawers (plist-get parameters :drawers))
  21099. (outline-regexp "\\*+ ")
  21100. a b xx
  21101. rtn p)
  21102. (with-current-buffer (get-buffer-create " org-mode-tmp")
  21103. (erase-buffer)
  21104. (insert string)
  21105. ;; Remove license-to-kill stuff
  21106. (while (setq p (text-property-any (point-min) (point-max)
  21107. :org-license-to-kill t))
  21108. (delete-region p (next-single-property-change p :org-license-to-kill)))
  21109. (let ((org-inhibit-startup t)) (org-mode))
  21110. (untabify (point-min) (point-max))
  21111. ;; Get the correct stuff before the first headline
  21112. (when (plist-get parameters :skip-before-1st-heading)
  21113. (goto-char (point-min))
  21114. (when (re-search-forward "^\\*+[ \t]" nil t)
  21115. (delete-region (point-min) (match-beginning 0))
  21116. (goto-char (point-min))
  21117. (insert "\n")))
  21118. (when (plist-get parameters :add-text)
  21119. (goto-char (point-min))
  21120. (insert (plist-get parameters :add-text) "\n"))
  21121. ;; Get rid of archived trees
  21122. (when (not (eq archived-trees t))
  21123. (goto-char (point-min))
  21124. (while (re-search-forward re-archive nil t)
  21125. (if (not (org-on-heading-p t))
  21126. (org-end-of-subtree t)
  21127. (beginning-of-line 1)
  21128. (setq a (if archived-trees
  21129. (1+ (point-at-eol)) (point))
  21130. b (org-end-of-subtree t))
  21131. (if (> b a) (delete-region a b)))))
  21132. ;; Get rid of drawers
  21133. (unless (eq t exp-drawers)
  21134. (goto-char (point-min))
  21135. (let ((re (concat "^[ \t]*:\\("
  21136. (mapconcat
  21137. 'identity
  21138. (org-delete-all exp-drawers
  21139. (copy-sequence drawers))
  21140. "\\|")
  21141. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  21142. (while (re-search-forward re nil t)
  21143. (replace-match ""))))
  21144. ;; Find targets in comments and move them out of comments,
  21145. ;; but mark them as targets that should be invisible
  21146. (goto-char (point-min))
  21147. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  21148. (replace-match "\\1(INVISIBLE)"))
  21149. ;; Protect backend specific stuff, throw away the others.
  21150. (let ((formatters
  21151. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  21152. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  21153. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  21154. fmt)
  21155. (goto-char (point-min))
  21156. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  21157. (goto-char (match-end 0))
  21158. (while (not (looking-at "#\\+END_EXAMPLE"))
  21159. (insert ": ")
  21160. (beginning-of-line 2)))
  21161. (goto-char (point-min))
  21162. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  21163. (add-text-properties (match-beginning 0) (match-end 0)
  21164. '(org-protected t)))
  21165. (while formatters
  21166. (setq fmt (pop formatters))
  21167. (when (car fmt)
  21168. (goto-char (point-min))
  21169. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  21170. ":[ \t]*\\(.*\\)") nil t)
  21171. (replace-match "\\1" t)
  21172. (add-text-properties
  21173. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  21174. '(org-protected t))))
  21175. (goto-char (point-min))
  21176. (while (re-search-forward
  21177. (concat "^#\\+"
  21178. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  21179. (cadddr fmt) "\\>.*\n?") nil t)
  21180. (if (car fmt)
  21181. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  21182. '(org-protected t))
  21183. (delete-region (match-beginning 0) (match-end 0))))))
  21184. ;; Protect quoted subtrees
  21185. (goto-char (point-min))
  21186. (while (re-search-forward re-quote nil t)
  21187. (goto-char (match-beginning 0))
  21188. (end-of-line 1)
  21189. (add-text-properties (point) (org-end-of-subtree t)
  21190. '(org-protected t)))
  21191. ;; Protect verbatim elements
  21192. (goto-char (point-min))
  21193. (while (re-search-forward org-verbatim-re nil t)
  21194. (add-text-properties (match-beginning 4) (match-end 4)
  21195. '(org-protected t))
  21196. (goto-char (1+ (match-end 4))))
  21197. ;; Remove subtrees that are commented
  21198. (goto-char (point-min))
  21199. (while (re-search-forward re-commented nil t)
  21200. (goto-char (match-beginning 0))
  21201. (delete-region (point) (org-end-of-subtree t)))
  21202. ;; Remove special table lines
  21203. (when org-export-table-remove-special-lines
  21204. (goto-char (point-min))
  21205. (while (re-search-forward "^[ \t]*|" nil t)
  21206. (beginning-of-line 1)
  21207. (if (or (looking-at "[ \t]*| *[!_^] *|")
  21208. (and (looking-at ".*?| *<[0-9]+> *|")
  21209. (not (looking-at ".*?| *[^ <|]"))))
  21210. (delete-region (max (point-min) (1- (point-at-bol)))
  21211. (point-at-eol))
  21212. (end-of-line 1))))
  21213. ;; Specific LaTeX stuff
  21214. (when latexp
  21215. (require 'org-export-latex nil)
  21216. (org-export-latex-cleaned-string))
  21217. (when asciip
  21218. (org-export-ascii-clean-string))
  21219. ;; Specific HTML stuff
  21220. (when htmlp
  21221. ;; Convert LaTeX fragments to images
  21222. (when (plist-get parameters :LaTeX-fragments)
  21223. (org-format-latex
  21224. (concat "ltxpng/" (file-name-sans-extension
  21225. (file-name-nondirectory
  21226. org-current-export-file)))
  21227. org-current-export-dir nil "Creating LaTeX image %s"))
  21228. (message "Exporting..."))
  21229. ;; Remove or replace comments
  21230. (goto-char (point-min))
  21231. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  21232. (if commentsp
  21233. (progn (add-text-properties
  21234. (match-beginning 0) (match-end 0) '(org-protected t))
  21235. (replace-match (format commentsp (match-string 1)) t t))
  21236. (replace-match "")))
  21237. ;; Find matches for radio targets and turn them into internal links
  21238. (goto-char (point-min))
  21239. (when re-radio
  21240. (while (re-search-forward re-radio nil t)
  21241. (org-if-unprotected
  21242. (replace-match "\\1[[\\2]]"))))
  21243. ;; Find all links that contain a newline and put them into a single line
  21244. (goto-char (point-min))
  21245. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  21246. (org-if-unprotected
  21247. (replace-match "\\1 \\3")
  21248. (goto-char (match-beginning 0))))
  21249. ;; Normalize links: Convert angle and plain links into bracket links
  21250. ;; Expand link abbreviations
  21251. (goto-char (point-min))
  21252. (while (re-search-forward re-plain-link nil t)
  21253. (goto-char (1- (match-end 0)))
  21254. (org-if-unprotected
  21255. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  21256. ":" (match-string 3) "]]")))
  21257. ;; added 'org-link face to links
  21258. (put-text-property 0 (length s) 'face 'org-link s)
  21259. (replace-match s t t))))
  21260. (goto-char (point-min))
  21261. (while (re-search-forward re-angle-link nil t)
  21262. (goto-char (1- (match-end 0)))
  21263. (org-if-unprotected
  21264. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  21265. ":" (match-string 3) "]]")))
  21266. (put-text-property 0 (length s) 'face 'org-link s)
  21267. (replace-match s t t))))
  21268. (goto-char (point-min))
  21269. (while (re-search-forward org-bracket-link-regexp nil t)
  21270. (org-if-unprotected
  21271. (let* ((s (concat "[[" (setq xx (save-match-data
  21272. (org-link-expand-abbrev (match-string 1))))
  21273. "]"
  21274. (if (match-end 3)
  21275. (match-string 2)
  21276. (concat "[" xx "]"))
  21277. "]")))
  21278. (put-text-property 0 (length s) 'face 'org-link s)
  21279. (replace-match s t t))))
  21280. ;; Find multiline emphasis and put them into single line
  21281. (when (plist-get parameters :emph-multiline)
  21282. (goto-char (point-min))
  21283. (while (re-search-forward org-emph-re nil t)
  21284. (if (not (= (char-after (match-beginning 3))
  21285. (char-after (match-beginning 4))))
  21286. (org-if-unprotected
  21287. (subst-char-in-region (match-beginning 0) (match-end 0)
  21288. ?\n ?\ t)
  21289. (goto-char (1- (match-end 0))))
  21290. (goto-char (1+ (match-beginning 0))))))
  21291. (setq rtn (buffer-string)))
  21292. (kill-buffer " org-mode-tmp")
  21293. rtn))
  21294. (defun org-export-grab-title-from-buffer ()
  21295. "Get a title for the current document, from looking at the buffer."
  21296. (let ((inhibit-read-only t))
  21297. (save-excursion
  21298. (goto-char (point-min))
  21299. (let ((end (save-excursion (outline-next-heading) (point))))
  21300. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  21301. ;; Mark the line so that it will not be exported as normal text.
  21302. (org-unmodified
  21303. (add-text-properties (match-beginning 0) (match-end 0)
  21304. (list :org-license-to-kill t)))
  21305. ;; Return the title string
  21306. (org-trim (match-string 0)))))))
  21307. (defun org-export-get-title-from-subtree ()
  21308. "Return subtree title and exclude it from export."
  21309. (let (title (m (mark)))
  21310. (save-excursion
  21311. (goto-char (region-beginning))
  21312. (when (and (org-at-heading-p)
  21313. (>= (org-end-of-subtree t t) (region-end)))
  21314. ;; This is a subtree, we take the title from the first heading
  21315. (goto-char (region-beginning))
  21316. (looking-at org-todo-line-regexp)
  21317. (setq title (match-string 3))
  21318. (org-unmodified
  21319. (add-text-properties (point) (1+ (point-at-eol))
  21320. (list :org-license-to-kill t)))))
  21321. title))
  21322. (defun org-solidify-link-text (s &optional alist)
  21323. "Take link text and make a safe target out of it."
  21324. (save-match-data
  21325. (let* ((rtn
  21326. (mapconcat
  21327. 'identity
  21328. (org-split-string s "[ \t\r\n]+") "--"))
  21329. (a (assoc rtn alist)))
  21330. (or (cdr a) rtn))))
  21331. (defun org-get-min-level (lines)
  21332. "Get the minimum level in LINES."
  21333. (let ((re "^\\(\\*+\\) ") l min)
  21334. (catch 'exit
  21335. (while (setq l (pop lines))
  21336. (if (string-match re l)
  21337. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  21338. 1)))
  21339. ;; Variable holding the vector with section numbers
  21340. (defvar org-section-numbers (make-vector org-level-max 0))
  21341. (defun org-init-section-numbers ()
  21342. "Initialize the vector for the section numbers."
  21343. (let* ((level -1)
  21344. (numbers (nreverse (org-split-string "" "\\.")))
  21345. (depth (1- (length org-section-numbers)))
  21346. (i depth) number-string)
  21347. (while (>= i 0)
  21348. (if (> i level)
  21349. (aset org-section-numbers i 0)
  21350. (setq number-string (or (car numbers) "0"))
  21351. (if (string-match "\\`[A-Z]\\'" number-string)
  21352. (aset org-section-numbers i
  21353. (- (string-to-char number-string) ?A -1))
  21354. (aset org-section-numbers i (string-to-number number-string)))
  21355. (pop numbers))
  21356. (setq i (1- i)))))
  21357. (defun org-section-number (&optional level)
  21358. "Return a string with the current section number.
  21359. When LEVEL is non-nil, increase section numbers on that level."
  21360. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  21361. (when level
  21362. (when (> level -1)
  21363. (aset org-section-numbers
  21364. level (1+ (aref org-section-numbers level))))
  21365. (setq idx (1+ level))
  21366. (while (<= idx depth)
  21367. (if (not (= idx 1))
  21368. (aset org-section-numbers idx 0))
  21369. (setq idx (1+ idx))))
  21370. (setq idx 0)
  21371. (while (<= idx depth)
  21372. (setq n (aref org-section-numbers idx))
  21373. (setq string (concat string (if (not (string= string "")) "." "")
  21374. (int-to-string n)))
  21375. (setq idx (1+ idx)))
  21376. (save-match-data
  21377. (if (string-match "\\`\\([@0]\\.\\)+" string)
  21378. (setq string (replace-match "" t nil string)))
  21379. (if (string-match "\\(\\.0\\)+\\'" string)
  21380. (setq string (replace-match "" t nil string))))
  21381. string))
  21382. ;;; ASCII export
  21383. (defvar org-last-level nil) ; dynamically scoped variable
  21384. (defvar org-min-level nil) ; dynamically scoped variable
  21385. (defvar org-levels-open nil) ; dynamically scoped parameter
  21386. (defvar org-ascii-current-indentation nil) ; For communication
  21387. (defun org-export-as-ascii (arg)
  21388. "Export the outline as a pretty ASCII file.
  21389. If there is an active region, export only the region.
  21390. The prefix ARG specifies how many levels of the outline should become
  21391. underlined headlines. The default is 3."
  21392. (interactive "P")
  21393. (setq-default org-todo-line-regexp org-todo-line-regexp)
  21394. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  21395. (org-infile-export-plist)))
  21396. (region-p (org-region-active-p))
  21397. (subtree-p
  21398. (when region-p
  21399. (save-excursion
  21400. (goto-char (region-beginning))
  21401. (and (org-at-heading-p)
  21402. (>= (org-end-of-subtree t t) (region-end))))))
  21403. (custom-times org-display-custom-times)
  21404. (org-ascii-current-indentation '(0 . 0))
  21405. (level 0) line txt
  21406. (umax nil)
  21407. (umax-toc nil)
  21408. (case-fold-search nil)
  21409. (filename (concat (file-name-as-directory
  21410. (org-export-directory :ascii opt-plist))
  21411. (file-name-sans-extension
  21412. (or (and subtree-p
  21413. (org-entry-get (region-beginning)
  21414. "EXPORT_FILE_NAME" t))
  21415. (file-name-nondirectory buffer-file-name)))
  21416. ".txt"))
  21417. (filename (if (equal (file-truename filename)
  21418. (file-truename buffer-file-name))
  21419. (concat filename ".txt")
  21420. filename))
  21421. (buffer (find-file-noselect filename))
  21422. (org-levels-open (make-vector org-level-max nil))
  21423. (odd org-odd-levels-only)
  21424. (date (plist-get opt-plist :date))
  21425. (author (plist-get opt-plist :author))
  21426. (title (or (and subtree-p (org-export-get-title-from-subtree))
  21427. (plist-get opt-plist :title)
  21428. (and (not
  21429. (plist-get opt-plist :skip-before-1st-heading))
  21430. (org-export-grab-title-from-buffer))
  21431. (file-name-sans-extension
  21432. (file-name-nondirectory buffer-file-name))))
  21433. (email (plist-get opt-plist :email))
  21434. (language (plist-get opt-plist :language))
  21435. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  21436. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  21437. (todo nil)
  21438. (lang-words nil)
  21439. (region
  21440. (buffer-substring
  21441. (if (org-region-active-p) (region-beginning) (point-min))
  21442. (if (org-region-active-p) (region-end) (point-max))))
  21443. (lines (org-split-string
  21444. (org-cleaned-string-for-export
  21445. region
  21446. :for-ascii t
  21447. :skip-before-1st-heading
  21448. (plist-get opt-plist :skip-before-1st-heading)
  21449. :drawers (plist-get opt-plist :drawers)
  21450. :verbatim-multiline t
  21451. :archived-trees
  21452. (plist-get opt-plist :archived-trees)
  21453. :add-text (plist-get opt-plist :text))
  21454. "\n"))
  21455. thetoc have-headings first-heading-pos
  21456. table-open table-buffer)
  21457. (let ((inhibit-read-only t))
  21458. (org-unmodified
  21459. (remove-text-properties (point-min) (point-max)
  21460. '(:org-license-to-kill t))))
  21461. (setq org-min-level (org-get-min-level lines))
  21462. (setq org-last-level org-min-level)
  21463. (org-init-section-numbers)
  21464. (find-file-noselect filename)
  21465. (setq lang-words (or (assoc language org-export-language-setup)
  21466. (assoc "en" org-export-language-setup)))
  21467. (switch-to-buffer-other-window buffer)
  21468. (erase-buffer)
  21469. (fundamental-mode)
  21470. ;; create local variables for all options, to make sure all called
  21471. ;; functions get the correct information
  21472. (mapc (lambda (x)
  21473. (set (make-local-variable (cdr x))
  21474. (plist-get opt-plist (car x))))
  21475. org-export-plist-vars)
  21476. (org-set-local 'org-odd-levels-only odd)
  21477. (setq umax (if arg (prefix-numeric-value arg)
  21478. org-export-headline-levels))
  21479. (setq umax-toc (if (integerp org-export-with-toc)
  21480. (min org-export-with-toc umax)
  21481. umax))
  21482. ;; File header
  21483. (if title (org-insert-centered title ?=))
  21484. (insert "\n")
  21485. (if (and (or author email)
  21486. org-export-author-info)
  21487. (insert (concat (nth 1 lang-words) ": " (or author "")
  21488. (if email (concat " <" email ">") "")
  21489. "\n")))
  21490. (cond
  21491. ((and date (string-match "%" date))
  21492. (setq date (format-time-string date (current-time))))
  21493. (date)
  21494. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  21495. (if (and date org-export-time-stamp-file)
  21496. (insert (concat (nth 2 lang-words) ": " date"\n")))
  21497. (insert "\n\n")
  21498. (if org-export-with-toc
  21499. (progn
  21500. (push (concat (nth 3 lang-words) "\n") thetoc)
  21501. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  21502. (mapc '(lambda (line)
  21503. (if (string-match org-todo-line-regexp
  21504. line)
  21505. ;; This is a headline
  21506. (progn
  21507. (setq have-headings t)
  21508. (setq level (- (match-end 1) (match-beginning 1))
  21509. level (org-tr-level level)
  21510. txt (match-string 3 line)
  21511. todo
  21512. (or (and org-export-mark-todo-in-toc
  21513. (match-beginning 2)
  21514. (not (member (match-string 2 line)
  21515. org-done-keywords)))
  21516. ; TODO, not DONE
  21517. (and org-export-mark-todo-in-toc
  21518. (= level umax-toc)
  21519. (org-search-todo-below
  21520. line lines level))))
  21521. (setq txt (org-html-expand-for-ascii txt))
  21522. (while (string-match org-bracket-link-regexp txt)
  21523. (setq txt
  21524. (replace-match
  21525. (match-string (if (match-end 2) 3 1) txt)
  21526. t t txt)))
  21527. (if (and (memq org-export-with-tags '(not-in-toc nil))
  21528. (string-match
  21529. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  21530. txt))
  21531. (setq txt (replace-match "" t t txt)))
  21532. (if (string-match quote-re0 txt)
  21533. (setq txt (replace-match "" t t txt)))
  21534. (if org-export-with-section-numbers
  21535. (setq txt (concat (org-section-number level)
  21536. " " txt)))
  21537. (if (<= level umax-toc)
  21538. (progn
  21539. (push
  21540. (concat
  21541. (make-string
  21542. (* (max 0 (- level org-min-level)) 4) ?\ )
  21543. (format (if todo "%s (*)\n" "%s\n") txt))
  21544. thetoc)
  21545. (setq org-last-level level))
  21546. ))))
  21547. lines)
  21548. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  21549. (org-init-section-numbers)
  21550. (while (setq line (pop lines))
  21551. ;; Remove the quoted HTML tags.
  21552. (setq line (org-html-expand-for-ascii line))
  21553. ;; Remove targets
  21554. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  21555. (setq line (replace-match "" t t line)))
  21556. ;; Replace internal links
  21557. (while (string-match org-bracket-link-regexp line)
  21558. (setq line (replace-match
  21559. (if (match-end 3) "[\\3]" "[\\1]")
  21560. t nil line)))
  21561. (when custom-times
  21562. (setq line (org-translate-time line)))
  21563. (cond
  21564. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  21565. ;; a Headline
  21566. (setq first-heading-pos (or first-heading-pos (point)))
  21567. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  21568. txt (match-string 2 line))
  21569. (org-ascii-level-start level txt umax lines))
  21570. ((and org-export-with-tables
  21571. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  21572. (if (not table-open)
  21573. ;; New table starts
  21574. (setq table-open t table-buffer nil))
  21575. ;; Accumulate lines
  21576. (setq table-buffer (cons line table-buffer))
  21577. (when (or (not lines)
  21578. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  21579. (car lines))))
  21580. (setq table-open nil
  21581. table-buffer (nreverse table-buffer))
  21582. (insert (mapconcat
  21583. (lambda (x)
  21584. (org-fix-indentation x org-ascii-current-indentation))
  21585. (org-format-table-ascii table-buffer)
  21586. "\n") "\n")))
  21587. (t
  21588. (setq line (org-fix-indentation line org-ascii-current-indentation))
  21589. (if (and org-export-with-fixed-width
  21590. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  21591. (setq line (replace-match "\\1" nil nil line)))
  21592. (insert line "\n"))))
  21593. (normal-mode)
  21594. ;; insert the table of contents
  21595. (when thetoc
  21596. (goto-char (point-min))
  21597. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  21598. (progn
  21599. (goto-char (match-beginning 0))
  21600. (replace-match ""))
  21601. (goto-char first-heading-pos))
  21602. (mapc 'insert thetoc)
  21603. (or (looking-at "[ \t]*\n[ \t]*\n")
  21604. (insert "\n\n")))
  21605. ;; Convert whitespace place holders
  21606. (goto-char (point-min))
  21607. (let (beg end)
  21608. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  21609. (setq end (next-single-property-change beg 'org-whitespace))
  21610. (goto-char beg)
  21611. (delete-region beg end)
  21612. (insert (make-string (- end beg) ?\ ))))
  21613. (save-buffer)
  21614. ;; remove display and invisible chars
  21615. (let (beg end)
  21616. (goto-char (point-min))
  21617. (while (setq beg (next-single-property-change (point) 'display))
  21618. (setq end (next-single-property-change beg 'display))
  21619. (delete-region beg end)
  21620. (goto-char beg)
  21621. (insert "=>"))
  21622. (goto-char (point-min))
  21623. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  21624. (setq end (next-single-property-change beg 'org-cwidth))
  21625. (delete-region beg end)
  21626. (goto-char beg)))
  21627. (goto-char (point-min))))
  21628. (defun org-export-ascii-clean-string ()
  21629. "Do extra work for ASCII export"
  21630. (goto-char (point-min))
  21631. (while (re-search-forward org-verbatim-re nil t)
  21632. (goto-char (match-end 2))
  21633. (backward-delete-char 1) (insert "'")
  21634. (goto-char (match-beginning 2))
  21635. (delete-char 1) (insert "`")
  21636. (goto-char (match-end 2))))
  21637. (defun org-search-todo-below (line lines level)
  21638. "Search the subtree below LINE for any TODO entries."
  21639. (let ((rest (cdr (memq line lines)))
  21640. (re org-todo-line-regexp)
  21641. line lv todo)
  21642. (catch 'exit
  21643. (while (setq line (pop rest))
  21644. (if (string-match re line)
  21645. (progn
  21646. (setq lv (- (match-end 1) (match-beginning 1))
  21647. todo (and (match-beginning 2)
  21648. (not (member (match-string 2 line)
  21649. org-done-keywords))))
  21650. ; TODO, not DONE
  21651. (if (<= lv level) (throw 'exit nil))
  21652. (if todo (throw 'exit t))))))))
  21653. (defun org-html-expand-for-ascii (line)
  21654. "Handle quoted HTML for ASCII export."
  21655. (if org-export-html-expand
  21656. (while (string-match "@<[^<>\n]*>" line)
  21657. ;; We just remove the tags for now.
  21658. (setq line (replace-match "" nil nil line))))
  21659. line)
  21660. (defun org-insert-centered (s &optional underline)
  21661. "Insert the string S centered and underline it with character UNDERLINE."
  21662. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  21663. (insert (make-string ind ?\ ) s "\n")
  21664. (if underline
  21665. (insert (make-string ind ?\ )
  21666. (make-string (string-width s) underline)
  21667. "\n"))))
  21668. (defun org-ascii-level-start (level title umax &optional lines)
  21669. "Insert a new level in ASCII export."
  21670. (let (char (n (- level umax 1)) (ind 0))
  21671. (if (> level umax)
  21672. (progn
  21673. (insert (make-string (* 2 n) ?\ )
  21674. (char-to-string (nth (% n (length org-export-ascii-bullets))
  21675. org-export-ascii-bullets))
  21676. " " title "\n")
  21677. ;; find the indentation of the next non-empty line
  21678. (catch 'stop
  21679. (while lines
  21680. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  21681. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  21682. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  21683. (pop lines)))
  21684. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  21685. (if (or (not (equal (char-before) ?\n))
  21686. (not (equal (char-before (1- (point))) ?\n)))
  21687. (insert "\n"))
  21688. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  21689. (unless org-export-with-tags
  21690. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  21691. (setq title (replace-match "" t t title))))
  21692. (if org-export-with-section-numbers
  21693. (setq title (concat (org-section-number level) " " title)))
  21694. (insert title "\n" (make-string (string-width title) char) "\n")
  21695. (setq org-ascii-current-indentation '(0 . 0)))))
  21696. (defun org-export-visible (type arg)
  21697. "Create a copy of the visible part of the current buffer, and export it.
  21698. The copy is created in a temporary buffer and removed after use.
  21699. TYPE is the final key (as a string) that also select the export command in
  21700. the `C-c C-e' export dispatcher.
  21701. As a special case, if the you type SPC at the prompt, the temporary
  21702. org-mode file will not be removed but presented to you so that you can
  21703. continue to use it. The prefix arg ARG is passed through to the exporting
  21704. command."
  21705. (interactive
  21706. (list (progn
  21707. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  21708. (read-char-exclusive))
  21709. current-prefix-arg))
  21710. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  21711. (error "Invalid export key"))
  21712. (let* ((binding (cdr (assoc type
  21713. '((?a . org-export-as-ascii)
  21714. (?\C-a . org-export-as-ascii)
  21715. (?b . org-export-as-html-and-open)
  21716. (?\C-b . org-export-as-html-and-open)
  21717. (?h . org-export-as-html)
  21718. (?H . org-export-as-html-to-buffer)
  21719. (?R . org-export-region-as-html)
  21720. (?x . org-export-as-xoxo)))))
  21721. (keepp (equal type ?\ ))
  21722. (file buffer-file-name)
  21723. (buffer (get-buffer-create "*Org Export Visible*"))
  21724. s e)
  21725. ;; Need to hack the drawers here.
  21726. (save-excursion
  21727. (goto-char (point-min))
  21728. (while (re-search-forward org-drawer-regexp nil t)
  21729. (goto-char (match-beginning 1))
  21730. (or (org-invisible-p) (org-flag-drawer nil))))
  21731. (with-current-buffer buffer (erase-buffer))
  21732. (save-excursion
  21733. (setq s (goto-char (point-min)))
  21734. (while (not (= (point) (point-max)))
  21735. (goto-char (org-find-invisible))
  21736. (append-to-buffer buffer s (point))
  21737. (setq s (goto-char (org-find-visible))))
  21738. (org-cycle-hide-drawers 'all)
  21739. (goto-char (point-min))
  21740. (unless keepp
  21741. ;; Copy all comment lines to the end, to make sure #+ settings are
  21742. ;; still available for the second export step. Kind of a hack, but
  21743. ;; does do the trick.
  21744. (if (looking-at "#[^\r\n]*")
  21745. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  21746. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  21747. (append-to-buffer buffer (1+ (match-beginning 0))
  21748. (min (point-max) (1+ (match-end 0))))))
  21749. (set-buffer buffer)
  21750. (let ((buffer-file-name file)
  21751. (org-inhibit-startup t))
  21752. (org-mode)
  21753. (show-all)
  21754. (unless keepp (funcall binding arg))))
  21755. (if (not keepp)
  21756. (kill-buffer buffer)
  21757. (switch-to-buffer-other-window buffer)
  21758. (goto-char (point-min)))))
  21759. (defun org-find-visible ()
  21760. (let ((s (point)))
  21761. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  21762. (get-char-property s 'invisible)))
  21763. s))
  21764. (defun org-find-invisible ()
  21765. (let ((s (point)))
  21766. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  21767. (not (get-char-property s 'invisible))))
  21768. s))
  21769. ;;; HTML export
  21770. (defun org-get-current-options ()
  21771. "Return a string with current options as keyword options.
  21772. Does include HTML export options as well as TODO and CATEGORY stuff."
  21773. (format
  21774. "#+TITLE: %s
  21775. #+AUTHOR: %s
  21776. #+EMAIL: %s
  21777. #+LANGUAGE: %s
  21778. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  21779. #+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
  21780. #+CATEGORY: %s
  21781. #+SEQ_TODO: %s
  21782. #+TYP_TODO: %s
  21783. #+PRIORITIES: %c %c %c
  21784. #+DRAWERS: %s
  21785. #+STARTUP: %s %s %s %s %s
  21786. #+TAGS: %s
  21787. #+ARCHIVE: %s
  21788. #+LINK: %s
  21789. "
  21790. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  21791. org-export-headline-levels
  21792. org-export-with-section-numbers
  21793. org-export-with-toc
  21794. org-export-preserve-breaks
  21795. org-export-html-expand
  21796. org-export-with-fixed-width
  21797. org-export-with-tables
  21798. org-export-with-sub-superscripts
  21799. org-export-with-special-strings
  21800. org-export-with-footnotes
  21801. org-export-with-emphasize
  21802. org-export-with-TeX-macros
  21803. org-export-with-LaTeX-fragments
  21804. org-export-skip-text-before-1st-heading
  21805. org-export-with-drawers
  21806. org-export-with-tags
  21807. (file-name-nondirectory buffer-file-name)
  21808. "TODO FEEDBACK VERIFY DONE"
  21809. "Me Jason Marie DONE"
  21810. org-highest-priority org-lowest-priority org-default-priority
  21811. (mapconcat 'identity org-drawers " ")
  21812. (cdr (assoc org-startup-folded
  21813. '((nil . "showall") (t . "overview") (content . "content"))))
  21814. (if org-odd-levels-only "odd" "oddeven")
  21815. (if org-hide-leading-stars "hidestars" "showstars")
  21816. (if org-startup-align-all-tables "align" "noalign")
  21817. (cond ((eq t org-log-done) "logdone")
  21818. ((not org-log-done) "nologging")
  21819. ((listp org-log-done)
  21820. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  21821. org-log-done " ")))
  21822. (or (mapconcat (lambda (x)
  21823. (cond
  21824. ((equal '(:startgroup) x) "{")
  21825. ((equal '(:endgroup) x) "}")
  21826. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  21827. (t (car x))))
  21828. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  21829. org-archive-location
  21830. "org file:~/org/%s.org"
  21831. ))
  21832. (defun org-insert-export-options-template ()
  21833. "Insert into the buffer a template with information for exporting."
  21834. (interactive)
  21835. (if (not (bolp)) (newline))
  21836. (let ((s (org-get-current-options)))
  21837. (and (string-match "#\\+CATEGORY" s)
  21838. (setq s (substring s 0 (match-beginning 0))))
  21839. (insert s)))
  21840. (defun org-toggle-fixed-width-section (arg)
  21841. "Toggle the fixed-width export.
  21842. If there is no active region, the QUOTE keyword at the current headline is
  21843. inserted or removed. When present, it causes the text between this headline
  21844. and the next to be exported as fixed-width text, and unmodified.
  21845. If there is an active region, this command adds or removes a colon as the
  21846. first character of this line. If the first character of a line is a colon,
  21847. this line is also exported in fixed-width font."
  21848. (interactive "P")
  21849. (let* ((cc 0)
  21850. (regionp (org-region-active-p))
  21851. (beg (if regionp (region-beginning) (point)))
  21852. (end (if regionp (region-end)))
  21853. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  21854. (case-fold-search nil)
  21855. (re "[ \t]*\\(:\\)")
  21856. off)
  21857. (if regionp
  21858. (save-excursion
  21859. (goto-char beg)
  21860. (setq cc (current-column))
  21861. (beginning-of-line 1)
  21862. (setq off (looking-at re))
  21863. (while (> nlines 0)
  21864. (setq nlines (1- nlines))
  21865. (beginning-of-line 1)
  21866. (cond
  21867. (arg
  21868. (move-to-column cc t)
  21869. (insert ":\n")
  21870. (forward-line -1))
  21871. ((and off (looking-at re))
  21872. (replace-match "" t t nil 1))
  21873. ((not off) (move-to-column cc t) (insert ":")))
  21874. (forward-line 1)))
  21875. (save-excursion
  21876. (org-back-to-heading)
  21877. (if (looking-at (concat outline-regexp
  21878. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  21879. (replace-match "" t t nil 1)
  21880. (if (looking-at outline-regexp)
  21881. (progn
  21882. (goto-char (match-end 0))
  21883. (insert org-quote-string " "))))))))
  21884. (defun org-export-as-html-and-open (arg)
  21885. "Export the outline as HTML and immediately open it with a browser.
  21886. If there is an active region, export only the region.
  21887. The prefix ARG specifies how many levels of the outline should become
  21888. headlines. The default is 3. Lower levels will become bulleted lists."
  21889. (interactive "P")
  21890. (org-export-as-html arg 'hidden)
  21891. (org-open-file buffer-file-name))
  21892. (defun org-export-as-html-batch ()
  21893. "Call `org-export-as-html', may be used in batch processing as
  21894. emacs --batch
  21895. --load=$HOME/lib/emacs/org.el
  21896. --eval \"(setq org-export-headline-levels 2)\"
  21897. --visit=MyFile --funcall org-export-as-html-batch"
  21898. (org-export-as-html org-export-headline-levels 'hidden))
  21899. (defun org-export-as-html-to-buffer (arg)
  21900. "Call `org-exort-as-html` with output to a temporary buffer.
  21901. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  21902. (interactive "P")
  21903. (org-export-as-html arg nil nil "*Org HTML Export*")
  21904. (switch-to-buffer-other-window "*Org HTML Export*"))
  21905. (defun org-replace-region-by-html (beg end)
  21906. "Assume the current region has org-mode syntax, and convert it to HTML.
  21907. This can be used in any buffer. For example, you could write an
  21908. itemized list in org-mode syntax in an HTML buffer and then use this
  21909. command to convert it."
  21910. (interactive "r")
  21911. (let (reg html buf pop-up-frames)
  21912. (save-window-excursion
  21913. (if (org-mode-p)
  21914. (setq html (org-export-region-as-html
  21915. beg end t 'string))
  21916. (setq reg (buffer-substring beg end)
  21917. buf (get-buffer-create "*Org tmp*"))
  21918. (with-current-buffer buf
  21919. (erase-buffer)
  21920. (insert reg)
  21921. (org-mode)
  21922. (setq html (org-export-region-as-html
  21923. (point-min) (point-max) t 'string)))
  21924. (kill-buffer buf)))
  21925. (delete-region beg end)
  21926. (insert html)))
  21927. (defun org-export-region-as-html (beg end &optional body-only buffer)
  21928. "Convert region from BEG to END in org-mode buffer to HTML.
  21929. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  21930. contents, and only produce the region of converted text, useful for
  21931. cut-and-paste operations.
  21932. If BUFFER is a buffer or a string, use/create that buffer as a target
  21933. of the converted HTML. If BUFFER is the symbol `string', return the
  21934. produced HTML as a string and leave not buffer behind. For example,
  21935. a Lisp program could call this function in the following way:
  21936. (setq html (org-export-region-as-html beg end t 'string))
  21937. When called interactively, the output buffer is selected, and shown
  21938. in a window. A non-interactive call will only retunr the buffer."
  21939. (interactive "r\nP")
  21940. (when (interactive-p)
  21941. (setq buffer "*Org HTML Export*"))
  21942. (let ((transient-mark-mode t) (zmacs-regions t)
  21943. rtn)
  21944. (goto-char end)
  21945. (set-mark (point)) ;; to activate the region
  21946. (goto-char beg)
  21947. (setq rtn (org-export-as-html
  21948. nil nil nil
  21949. buffer body-only))
  21950. (if (fboundp 'deactivate-mark) (deactivate-mark))
  21951. (if (and (interactive-p) (bufferp rtn))
  21952. (switch-to-buffer-other-window rtn)
  21953. rtn)))
  21954. (defvar html-table-tag nil) ; dynamically scoped into this.
  21955. (defun org-export-as-html (arg &optional hidden ext-plist
  21956. to-buffer body-only)
  21957. "Export the outline as a pretty HTML file.
  21958. If there is an active region, export only the region. The prefix
  21959. ARG specifies how many levels of the outline should become
  21960. headlines. The default is 3. Lower levels will become bulleted
  21961. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  21962. EXT-PLIST is a property list with external parameters overriding
  21963. org-mode's default settings, but still inferior to file-local
  21964. settings. When TO-BUFFER is non-nil, create a buffer with that
  21965. name and export to that buffer. If TO-BUFFER is the symbol `string',
  21966. don't leave any buffer behind but just return the resulting HTML as
  21967. a string. When BODY-ONLY is set, don't produce the file header and footer,
  21968. simply return the content of <body>...</body>, without even
  21969. the body tags themselves."
  21970. (interactive "P")
  21971. ;; Make sure we have a file name when we need it.
  21972. (when (and (not (or to-buffer body-only))
  21973. (not buffer-file-name))
  21974. (if (buffer-base-buffer)
  21975. (org-set-local 'buffer-file-name
  21976. (with-current-buffer (buffer-base-buffer)
  21977. buffer-file-name))
  21978. (error "Need a file name to be able to export.")))
  21979. (message "Exporting...")
  21980. (setq-default org-todo-line-regexp org-todo-line-regexp)
  21981. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  21982. (setq-default org-done-keywords org-done-keywords)
  21983. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  21984. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  21985. ext-plist
  21986. (org-infile-export-plist)))
  21987. (style (plist-get opt-plist :style))
  21988. (link-validate (plist-get opt-plist :link-validation-function))
  21989. valid thetoc have-headings first-heading-pos
  21990. (odd org-odd-levels-only)
  21991. (region-p (org-region-active-p))
  21992. (subtree-p
  21993. (when region-p
  21994. (save-excursion
  21995. (goto-char (region-beginning))
  21996. (and (org-at-heading-p)
  21997. (>= (org-end-of-subtree t t) (region-end))))))
  21998. ;; The following two are dynamically scoped into other
  21999. ;; routines below.
  22000. (org-current-export-dir (org-export-directory :html opt-plist))
  22001. (org-current-export-file buffer-file-name)
  22002. (level 0) (line "") (origline "") txt todo
  22003. (umax nil)
  22004. (umax-toc nil)
  22005. (filename (if to-buffer nil
  22006. (concat (file-name-as-directory
  22007. (org-export-directory :html opt-plist))
  22008. (file-name-sans-extension
  22009. (or (and subtree-p
  22010. (org-entry-get (region-beginning)
  22011. "EXPORT_FILE_NAME" t))
  22012. (file-name-nondirectory buffer-file-name)))
  22013. "." org-export-html-extension)))
  22014. (current-dir (if buffer-file-name
  22015. (file-name-directory buffer-file-name)
  22016. default-directory))
  22017. (buffer (if to-buffer
  22018. (cond
  22019. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  22020. (t (get-buffer-create to-buffer)))
  22021. (find-file-noselect filename)))
  22022. (org-levels-open (make-vector org-level-max nil))
  22023. (date (plist-get opt-plist :date))
  22024. (author (plist-get opt-plist :author))
  22025. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22026. (plist-get opt-plist :title)
  22027. (and (not
  22028. (plist-get opt-plist :skip-before-1st-heading))
  22029. (org-export-grab-title-from-buffer))
  22030. (and buffer-file-name
  22031. (file-name-sans-extension
  22032. (file-name-nondirectory buffer-file-name)))
  22033. "UNTITLED"))
  22034. (html-table-tag (plist-get opt-plist :html-table-tag))
  22035. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22036. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  22037. (inquote nil)
  22038. (infixed nil)
  22039. (in-local-list nil)
  22040. (local-list-num nil)
  22041. (local-list-indent nil)
  22042. (llt org-plain-list-ordered-item-terminator)
  22043. (email (plist-get opt-plist :email))
  22044. (language (plist-get opt-plist :language))
  22045. (lang-words nil)
  22046. (target-alist nil) tg
  22047. (head-count 0) cnt
  22048. (start 0)
  22049. (coding-system (and (boundp 'buffer-file-coding-system)
  22050. buffer-file-coding-system))
  22051. (coding-system-for-write (or org-export-html-coding-system
  22052. coding-system))
  22053. (save-buffer-coding-system (or org-export-html-coding-system
  22054. coding-system))
  22055. (charset (and coding-system-for-write
  22056. (fboundp 'coding-system-get)
  22057. (coding-system-get coding-system-for-write
  22058. 'mime-charset)))
  22059. (region
  22060. (buffer-substring
  22061. (if region-p (region-beginning) (point-min))
  22062. (if region-p (region-end) (point-max))))
  22063. (lines
  22064. (org-split-string
  22065. (org-cleaned-string-for-export
  22066. region
  22067. :emph-multiline t
  22068. :for-html t
  22069. :skip-before-1st-heading
  22070. (plist-get opt-plist :skip-before-1st-heading)
  22071. :drawers (plist-get opt-plist :drawers)
  22072. :archived-trees
  22073. (plist-get opt-plist :archived-trees)
  22074. :add-text
  22075. (plist-get opt-plist :text)
  22076. :LaTeX-fragments
  22077. (plist-get opt-plist :LaTeX-fragments))
  22078. "[\r\n]"))
  22079. table-open type
  22080. table-buffer table-orig-buffer
  22081. ind start-is-num starter didclose
  22082. rpl path desc descp desc1 desc2 link
  22083. )
  22084. (let ((inhibit-read-only t))
  22085. (org-unmodified
  22086. (remove-text-properties (point-min) (point-max)
  22087. '(:org-license-to-kill t))))
  22088. (message "Exporting...")
  22089. (setq org-min-level (org-get-min-level lines))
  22090. (setq org-last-level org-min-level)
  22091. (org-init-section-numbers)
  22092. (cond
  22093. ((and date (string-match "%" date))
  22094. (setq date (format-time-string date (current-time))))
  22095. (date)
  22096. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22097. ;; Get the language-dependent settings
  22098. (setq lang-words (or (assoc language org-export-language-setup)
  22099. (assoc "en" org-export-language-setup)))
  22100. ;; Switch to the output buffer
  22101. (set-buffer buffer)
  22102. (erase-buffer)
  22103. (fundamental-mode)
  22104. (and (fboundp 'set-buffer-file-coding-system)
  22105. (set-buffer-file-coding-system coding-system-for-write))
  22106. (let ((case-fold-search nil)
  22107. (org-odd-levels-only odd))
  22108. ;; create local variables for all options, to make sure all called
  22109. ;; functions get the correct information
  22110. (mapc (lambda (x)
  22111. (set (make-local-variable (cdr x))
  22112. (plist-get opt-plist (car x))))
  22113. org-export-plist-vars)
  22114. (setq umax (if arg (prefix-numeric-value arg)
  22115. org-export-headline-levels))
  22116. (setq umax-toc (if (integerp org-export-with-toc)
  22117. (min org-export-with-toc umax)
  22118. umax))
  22119. (unless body-only
  22120. ;; File header
  22121. (insert (format
  22122. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  22123. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  22124. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  22125. lang=\"%s\" xml:lang=\"%s\">
  22126. <head>
  22127. <title>%s</title>
  22128. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  22129. <meta name=\"generator\" content=\"Org-mode\"/>
  22130. <meta name=\"generated\" content=\"%s\"/>
  22131. <meta name=\"author\" content=\"%s\"/>
  22132. %s
  22133. </head><body>
  22134. "
  22135. language language (org-html-expand title)
  22136. (or charset "iso-8859-1") date author style))
  22137. (insert (or (plist-get opt-plist :preamble) ""))
  22138. (when (plist-get opt-plist :auto-preamble)
  22139. (if title (insert (format org-export-html-title-format
  22140. (org-html-expand title))))))
  22141. (if (and org-export-with-toc (not body-only))
  22142. (progn
  22143. (push (format "<h%d>%s</h%d>\n"
  22144. org-export-html-toplevel-hlevel
  22145. (nth 3 lang-words)
  22146. org-export-html-toplevel-hlevel)
  22147. thetoc)
  22148. (push "<ul>\n<li>" thetoc)
  22149. (setq lines
  22150. (mapcar '(lambda (line)
  22151. (if (string-match org-todo-line-regexp line)
  22152. ;; This is a headline
  22153. (progn
  22154. (setq have-headings t)
  22155. (setq level (- (match-end 1) (match-beginning 1))
  22156. level (org-tr-level level)
  22157. txt (save-match-data
  22158. (org-html-expand
  22159. (org-export-cleanup-toc-line
  22160. (match-string 3 line))))
  22161. todo
  22162. (or (and org-export-mark-todo-in-toc
  22163. (match-beginning 2)
  22164. (not (member (match-string 2 line)
  22165. org-done-keywords)))
  22166. ; TODO, not DONE
  22167. (and org-export-mark-todo-in-toc
  22168. (= level umax-toc)
  22169. (org-search-todo-below
  22170. line lines level))))
  22171. (if (string-match
  22172. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  22173. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  22174. (if (string-match quote-re0 txt)
  22175. (setq txt (replace-match "" t t txt)))
  22176. (if org-export-with-section-numbers
  22177. (setq txt (concat (org-section-number level)
  22178. " " txt)))
  22179. (if (<= level (max umax umax-toc))
  22180. (setq head-count (+ head-count 1)))
  22181. (if (<= level umax-toc)
  22182. (progn
  22183. (if (> level org-last-level)
  22184. (progn
  22185. (setq cnt (- level org-last-level))
  22186. (while (>= (setq cnt (1- cnt)) 0)
  22187. (push "\n<ul>\n<li>" thetoc))
  22188. (push "\n" thetoc)))
  22189. (if (< level org-last-level)
  22190. (progn
  22191. (setq cnt (- org-last-level level))
  22192. (while (>= (setq cnt (1- cnt)) 0)
  22193. (push "</li>\n</ul>" thetoc))
  22194. (push "\n" thetoc)))
  22195. ;; Check for targets
  22196. (while (string-match org-target-regexp line)
  22197. (setq tg (match-string 1 line)
  22198. line (replace-match
  22199. (concat "@<span class=\"target\">" tg "@</span> ")
  22200. t t line))
  22201. (push (cons (org-solidify-link-text tg)
  22202. (format "sec-%d" head-count))
  22203. target-alist))
  22204. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  22205. (setq txt (replace-match "" t t txt)))
  22206. (push
  22207. (format
  22208. (if todo
  22209. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  22210. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  22211. head-count txt) thetoc)
  22212. (setq org-last-level level))
  22213. )))
  22214. line)
  22215. lines))
  22216. (while (> org-last-level (1- org-min-level))
  22217. (setq org-last-level (1- org-last-level))
  22218. (push "</li>\n</ul>\n" thetoc))
  22219. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22220. (setq head-count 0)
  22221. (org-init-section-numbers)
  22222. (while (setq line (pop lines) origline line)
  22223. (catch 'nextline
  22224. ;; end of quote section?
  22225. (when (and inquote (string-match "^\\*+ " line))
  22226. (insert "</pre>\n")
  22227. (setq inquote nil))
  22228. ;; inside a quote section?
  22229. (when inquote
  22230. (insert (org-html-protect line) "\n")
  22231. (throw 'nextline nil))
  22232. ;; verbatim lines
  22233. (when (and org-export-with-fixed-width
  22234. (string-match "^[ \t]*:\\(.*\\)" line))
  22235. (when (not infixed)
  22236. (setq infixed t)
  22237. (insert "<pre>\n"))
  22238. (insert (org-html-protect (match-string 1 line)) "\n")
  22239. (when (and lines
  22240. (not (string-match "^[ \t]*\\(:.*\\)"
  22241. (car lines))))
  22242. (setq infixed nil)
  22243. (insert "</pre>\n"))
  22244. (throw 'nextline nil))
  22245. ;; Protected HTML
  22246. (when (get-text-property 0 'org-protected line)
  22247. (let (par)
  22248. (when (re-search-backward
  22249. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  22250. (setq par (match-string 1))
  22251. (replace-match "\\2\n"))
  22252. (insert line "\n")
  22253. (while (and lines
  22254. (get-text-property 0 'org-protected (car lines)))
  22255. (insert (pop lines) "\n"))
  22256. (and par (insert "<p>\n")))
  22257. (throw 'nextline nil))
  22258. ;; Horizontal line
  22259. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  22260. (insert "\n<hr/>\n")
  22261. (throw 'nextline nil))
  22262. ;; make targets to anchors
  22263. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  22264. (cond
  22265. ((match-end 2)
  22266. (setq line (replace-match
  22267. (concat "@<a name=\""
  22268. (org-solidify-link-text (match-string 1 line))
  22269. "\">\\nbsp@</a>")
  22270. t t line)))
  22271. ((and org-export-with-toc (equal (string-to-char line) ?*))
  22272. (setq line (replace-match
  22273. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  22274. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  22275. t t line)))
  22276. (t
  22277. (setq line (replace-match
  22278. (concat "@<a name=\""
  22279. (org-solidify-link-text (match-string 1 line))
  22280. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  22281. t t line)))))
  22282. (setq line (org-html-handle-time-stamps line))
  22283. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  22284. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  22285. ;; Also handle sub_superscripts and checkboxes
  22286. (setq line (org-html-expand line))
  22287. ;; Format the links
  22288. (setq start 0)
  22289. (while (string-match org-bracket-link-analytic-regexp line start)
  22290. (setq start (match-beginning 0))
  22291. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  22292. (setq path (match-string 3 line))
  22293. (setq desc1 (if (match-end 5) (match-string 5 line))
  22294. desc2 (if (match-end 2) (concat type ":" path) path)
  22295. descp (and desc1 (not (equal desc1 desc2)))
  22296. desc (or desc1 desc2))
  22297. ;; Make an image out of the description if that is so wanted
  22298. (when (and descp (org-file-image-p desc))
  22299. (save-match-data
  22300. (if (string-match "^file:" desc)
  22301. (setq desc (substring desc (match-end 0)))))
  22302. (setq desc (concat "<img src=\"" desc "\"/>")))
  22303. ;; FIXME: do we need to unescape here somewhere?
  22304. (cond
  22305. ((equal type "internal")
  22306. (setq rpl
  22307. (concat
  22308. "<a href=\"#"
  22309. (org-solidify-link-text
  22310. (save-match-data (org-link-unescape path)) target-alist)
  22311. "\">" desc "</a>")))
  22312. ((member type '("http" "https"))
  22313. ;; standard URL, just check if we need to inline an image
  22314. (if (and (or (eq t org-export-html-inline-images)
  22315. (and org-export-html-inline-images (not descp)))
  22316. (org-file-image-p path))
  22317. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  22318. (setq link (concat type ":" path))
  22319. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  22320. ((member type '("ftp" "mailto" "news"))
  22321. ;; standard URL
  22322. (setq link (concat type ":" path))
  22323. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  22324. ((string= type "file")
  22325. ;; FILE link
  22326. (let* ((filename path)
  22327. (abs-p (file-name-absolute-p filename))
  22328. thefile file-is-image-p search)
  22329. (save-match-data
  22330. (if (string-match "::\\(.*\\)" filename)
  22331. (setq search (match-string 1 filename)
  22332. filename (replace-match "" t nil filename)))
  22333. (setq valid
  22334. (if (functionp link-validate)
  22335. (funcall link-validate filename current-dir)
  22336. t))
  22337. (setq file-is-image-p (org-file-image-p filename))
  22338. (setq thefile (if abs-p (expand-file-name filename) filename))
  22339. (when (and org-export-html-link-org-files-as-html
  22340. (string-match "\\.org$" thefile))
  22341. (setq thefile (concat (substring thefile 0
  22342. (match-beginning 0))
  22343. "." org-export-html-extension))
  22344. (if (and search
  22345. ;; make sure this is can be used as target search
  22346. (not (string-match "^[0-9]*$" search))
  22347. (not (string-match "^\\*" search))
  22348. (not (string-match "^/.*/$" search)))
  22349. (setq thefile (concat thefile "#"
  22350. (org-solidify-link-text
  22351. (org-link-unescape search)))))
  22352. (when (string-match "^file:" desc)
  22353. (setq desc (replace-match "" t t desc))
  22354. (if (string-match "\\.org$" desc)
  22355. (setq desc (replace-match "" t t desc))))))
  22356. (setq rpl (if (and file-is-image-p
  22357. (or (eq t org-export-html-inline-images)
  22358. (and org-export-html-inline-images
  22359. (not descp))))
  22360. (concat "<img src=\"" thefile "\"/>")
  22361. (concat "<a href=\"" thefile "\">" desc "</a>")))
  22362. (if (not valid) (setq rpl desc))))
  22363. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  22364. (setq rpl (concat "<i>&lt;" type ":"
  22365. (save-match-data (org-link-unescape path))
  22366. "&gt;</i>"))))
  22367. (setq line (replace-match rpl t t line)
  22368. start (+ start (length rpl))))
  22369. ;; TODO items
  22370. (if (and (string-match org-todo-line-regexp line)
  22371. (match-beginning 2))
  22372. (setq line
  22373. (concat (substring line 0 (match-beginning 2))
  22374. "<span class=\""
  22375. (if (member (match-string 2 line)
  22376. org-done-keywords)
  22377. "done" "todo")
  22378. "\">" (match-string 2 line)
  22379. "</span>" (substring line (match-end 2)))))
  22380. ;; Does this contain a reference to a footnote?
  22381. (when org-export-with-footnotes
  22382. (setq start 0)
  22383. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  22384. (if (get-text-property (match-beginning 2) 'org-protected line)
  22385. (setq start (match-end 2))
  22386. (let ((n (match-string 2 line)))
  22387. (setq line
  22388. (replace-match
  22389. (format
  22390. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  22391. (match-string 1 line) n n n)
  22392. t t line))))))
  22393. (cond
  22394. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22395. ;; This is a headline
  22396. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22397. txt (match-string 2 line))
  22398. (if (string-match quote-re0 txt)
  22399. (setq txt (replace-match "" t t txt)))
  22400. (if (<= level (max umax umax-toc))
  22401. (setq head-count (+ head-count 1)))
  22402. (when in-local-list
  22403. ;; Close any local lists before inserting a new header line
  22404. (while local-list-num
  22405. (org-close-li)
  22406. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  22407. (pop local-list-num))
  22408. (setq local-list-indent nil
  22409. in-local-list nil))
  22410. (setq first-heading-pos (or first-heading-pos (point)))
  22411. (org-html-level-start level txt umax
  22412. (and org-export-with-toc (<= level umax))
  22413. head-count)
  22414. ;; QUOTES
  22415. (when (string-match quote-re line)
  22416. (insert "<pre>")
  22417. (setq inquote t)))
  22418. ((and org-export-with-tables
  22419. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22420. (if (not table-open)
  22421. ;; New table starts
  22422. (setq table-open t table-buffer nil table-orig-buffer nil))
  22423. ;; Accumulate lines
  22424. (setq table-buffer (cons line table-buffer)
  22425. table-orig-buffer (cons origline table-orig-buffer))
  22426. (when (or (not lines)
  22427. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22428. (car lines))))
  22429. (setq table-open nil
  22430. table-buffer (nreverse table-buffer)
  22431. table-orig-buffer (nreverse table-orig-buffer))
  22432. (org-close-par-maybe)
  22433. (insert (org-format-table-html table-buffer table-orig-buffer))))
  22434. (t
  22435. ;; Normal lines
  22436. (when (string-match
  22437. (cond
  22438. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  22439. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  22440. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  22441. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  22442. line)
  22443. (setq ind (org-get-string-indentation line)
  22444. start-is-num (match-beginning 4)
  22445. starter (if (match-beginning 2)
  22446. (substring (match-string 2 line) 0 -1))
  22447. line (substring line (match-beginning 5)))
  22448. (unless (string-match "[^ \t]" line)
  22449. ;; empty line. Pretend indentation is large.
  22450. (setq ind (if org-empty-line-terminates-plain-lists
  22451. 0
  22452. (1+ (or (car local-list-indent) 1)))))
  22453. (setq didclose nil)
  22454. (while (and in-local-list
  22455. (or (and (= ind (car local-list-indent))
  22456. (not starter))
  22457. (< ind (car local-list-indent))))
  22458. (setq didclose t)
  22459. (org-close-li)
  22460. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  22461. (pop local-list-num) (pop local-list-indent)
  22462. (setq in-local-list local-list-indent))
  22463. (cond
  22464. ((and starter
  22465. (or (not in-local-list)
  22466. (> ind (car local-list-indent))))
  22467. ;; Start new (level of) list
  22468. (org-close-par-maybe)
  22469. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  22470. (push start-is-num local-list-num)
  22471. (push ind local-list-indent)
  22472. (setq in-local-list t))
  22473. (starter
  22474. ;; continue current list
  22475. (org-close-li)
  22476. (insert "<li>\n"))
  22477. (didclose
  22478. ;; we did close a list, normal text follows: need <p>
  22479. (org-open-par)))
  22480. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  22481. (setq line
  22482. (replace-match
  22483. (if (equal (match-string 1 line) "X")
  22484. "<b>[X]</b>"
  22485. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  22486. t t line))))
  22487. ;; Empty lines start a new paragraph. If hand-formatted lists
  22488. ;; are not fully interpreted, lines starting with "-", "+", "*"
  22489. ;; also start a new paragraph.
  22490. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  22491. ;; Is this the start of a footnote?
  22492. (when org-export-with-footnotes
  22493. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  22494. (org-close-par-maybe)
  22495. (let ((n (match-string 1 line)))
  22496. (setq line (replace-match
  22497. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  22498. ;; Check if the line break needs to be conserved
  22499. (cond
  22500. ((string-match "\\\\\\\\[ \t]*$" line)
  22501. (setq line (replace-match "<br/>" t t line)))
  22502. (org-export-preserve-breaks
  22503. (setq line (concat line "<br/>"))))
  22504. (insert line "\n")))))
  22505. ;; Properly close all local lists and other lists
  22506. (when inquote (insert "</pre>\n"))
  22507. (when in-local-list
  22508. ;; Close any local lists before inserting a new header line
  22509. (while local-list-num
  22510. (org-close-li)
  22511. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  22512. (pop local-list-num))
  22513. (setq local-list-indent nil
  22514. in-local-list nil))
  22515. (org-html-level-start 1 nil umax
  22516. (and org-export-with-toc (<= level umax))
  22517. head-count)
  22518. (unless body-only
  22519. (when (plist-get opt-plist :auto-postamble)
  22520. (insert "<div id=\"postamble\">")
  22521. (when (and org-export-author-info author)
  22522. (insert "<p class=\"author\"> "
  22523. (nth 1 lang-words) ": " author "\n")
  22524. (when email
  22525. (if (listp (split-string email ",+ *"))
  22526. (mapc (lambda(e)
  22527. (insert "<a href=\"mailto:" e "\">&lt;"
  22528. e "&gt;</a>\n"))
  22529. (split-string email ",+ *"))
  22530. (insert "<a href=\"mailto:" email "\">&lt;"
  22531. email "&gt;</a>\n")))
  22532. (insert "</p>\n"))
  22533. (when (and date org-export-time-stamp-file)
  22534. (insert "<p class=\"date\"> "
  22535. (nth 2 lang-words) ": "
  22536. date "</p>\n"))
  22537. (insert "</div>"))
  22538. (if org-export-html-with-timestamp
  22539. (insert org-export-html-html-helper-timestamp))
  22540. (insert (or (plist-get opt-plist :postamble) ""))
  22541. (insert "</body>\n</html>\n"))
  22542. (normal-mode)
  22543. (if (eq major-mode default-major-mode) (html-mode))
  22544. ;; insert the table of contents
  22545. (goto-char (point-min))
  22546. (when thetoc
  22547. (if (or (re-search-forward
  22548. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  22549. (re-search-forward
  22550. "\\[TABLE-OF-CONTENTS\\]" nil t))
  22551. (progn
  22552. (goto-char (match-beginning 0))
  22553. (replace-match ""))
  22554. (goto-char first-heading-pos)
  22555. (when (looking-at "\\s-*</p>")
  22556. (goto-char (match-end 0))
  22557. (insert "\n")))
  22558. (insert "<div id=\"table-of-contents\">\n")
  22559. (mapc 'insert thetoc)
  22560. (insert "</div>\n"))
  22561. ;; remove empty paragraphs and lists
  22562. (goto-char (point-min))
  22563. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  22564. (replace-match ""))
  22565. (goto-char (point-min))
  22566. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  22567. (replace-match ""))
  22568. ;; Convert whitespace place holders
  22569. (goto-char (point-min))
  22570. (let (beg end n)
  22571. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22572. (setq n (get-text-property beg 'org-whitespace)
  22573. end (next-single-property-change beg 'org-whitespace))
  22574. (goto-char beg)
  22575. (delete-region beg end)
  22576. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  22577. (make-string n ?x)))))
  22578. (or to-buffer (save-buffer))
  22579. (goto-char (point-min))
  22580. (message "Exporting... done")
  22581. (if (eq to-buffer 'string)
  22582. (prog1 (buffer-substring (point-min) (point-max))
  22583. (kill-buffer (current-buffer)))
  22584. (current-buffer)))))
  22585. (defvar org-table-colgroup-info nil)
  22586. (defun org-format-table-ascii (lines)
  22587. "Format a table for ascii export."
  22588. (if (stringp lines)
  22589. (setq lines (org-split-string lines "\n")))
  22590. (if (not (string-match "^[ \t]*|" (car lines)))
  22591. ;; Table made by table.el - test for spanning
  22592. lines
  22593. ;; A normal org table
  22594. ;; Get rid of hlines at beginning and end
  22595. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  22596. (setq lines (nreverse lines))
  22597. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  22598. (setq lines (nreverse lines))
  22599. (when org-export-table-remove-special-lines
  22600. ;; Check if the table has a marking column. If yes remove the
  22601. ;; column and the special lines
  22602. (setq lines (org-table-clean-before-export lines)))
  22603. ;; Get rid of the vertical lines except for grouping
  22604. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  22605. rtn line vl1 start)
  22606. (while (setq line (pop lines))
  22607. (if (string-match org-table-hline-regexp line)
  22608. (and (string-match "|\\(.*\\)|" line)
  22609. (setq line (replace-match " \\1" t nil line)))
  22610. (setq start 0 vl1 vl)
  22611. (while (string-match "|" line start)
  22612. (setq start (match-end 0))
  22613. (or (pop vl1) (setq line (replace-match " " t t line)))))
  22614. (push line rtn))
  22615. (nreverse rtn))))
  22616. (defun org-colgroup-info-to-vline-list (info)
  22617. (let (vl new last)
  22618. (while info
  22619. (setq last new new (pop info))
  22620. (if (or (memq last '(:end :startend))
  22621. (memq new '(:start :startend)))
  22622. (push t vl)
  22623. (push nil vl)))
  22624. (setq vl (nreverse vl))
  22625. (and vl (setcar vl nil))
  22626. vl))
  22627. (defun org-format-table-html (lines olines)
  22628. "Find out which HTML converter to use and return the HTML code."
  22629. (if (stringp lines)
  22630. (setq lines (org-split-string lines "\n")))
  22631. (if (string-match "^[ \t]*|" (car lines))
  22632. ;; A normal org table
  22633. (org-format-org-table-html lines)
  22634. ;; Table made by table.el - test for spanning
  22635. (let* ((hlines (delq nil (mapcar
  22636. (lambda (x)
  22637. (if (string-match "^[ \t]*\\+-" x) x
  22638. nil))
  22639. lines)))
  22640. (first (car hlines))
  22641. (ll (and (string-match "\\S-+" first)
  22642. (match-string 0 first)))
  22643. (re (concat "^[ \t]*" (regexp-quote ll)))
  22644. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  22645. hlines))))
  22646. (if (and (not spanning)
  22647. (not org-export-prefer-native-exporter-for-tables))
  22648. ;; We can use my own converter with HTML conversions
  22649. (org-format-table-table-html lines)
  22650. ;; Need to use the code generator in table.el, with the original text.
  22651. (org-format-table-table-html-using-table-generate-source olines)))))
  22652. (defun org-format-org-table-html (lines &optional splice)
  22653. "Format a table into HTML."
  22654. ;; Get rid of hlines at beginning and end
  22655. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  22656. (setq lines (nreverse lines))
  22657. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  22658. (setq lines (nreverse lines))
  22659. (when org-export-table-remove-special-lines
  22660. ;; Check if the table has a marking column. If yes remove the
  22661. ;; column and the special lines
  22662. (setq lines (org-table-clean-before-export lines)))
  22663. (let ((head (and org-export-highlight-first-table-line
  22664. (delq nil (mapcar
  22665. (lambda (x) (string-match "^[ \t]*|-" x))
  22666. (cdr lines)))))
  22667. (nlines 0) fnum i
  22668. tbopen line fields html gr colgropen)
  22669. (if splice (setq head nil))
  22670. (unless splice (push (if head "<thead>" "<tbody>") html))
  22671. (setq tbopen t)
  22672. (while (setq line (pop lines))
  22673. (catch 'next-line
  22674. (if (string-match "^[ \t]*|-" line)
  22675. (progn
  22676. (unless splice
  22677. (push (if head "</thead>" "</tbody>") html)
  22678. (if lines (push "<tbody>" html) (setq tbopen nil)))
  22679. (setq head nil) ;; head ends here, first time around
  22680. ;; ignore this line
  22681. (throw 'next-line t)))
  22682. ;; Break the line into fields
  22683. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  22684. (unless fnum (setq fnum (make-vector (length fields) 0)))
  22685. (setq nlines (1+ nlines) i -1)
  22686. (push (concat "<tr>"
  22687. (mapconcat
  22688. (lambda (x)
  22689. (setq i (1+ i))
  22690. (if (and (< i nlines)
  22691. (string-match org-table-number-regexp x))
  22692. (incf (aref fnum i)))
  22693. (if head
  22694. (concat (car org-export-table-header-tags) x
  22695. (cdr org-export-table-header-tags))
  22696. (concat (car org-export-table-data-tags) x
  22697. (cdr org-export-table-data-tags))))
  22698. fields "")
  22699. "</tr>")
  22700. html)))
  22701. (unless splice (if tbopen (push "</tbody>" html)))
  22702. (unless splice (push "</table>\n" html))
  22703. (setq html (nreverse html))
  22704. (unless splice
  22705. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  22706. (push (mapconcat
  22707. (lambda (x)
  22708. (setq gr (pop org-table-colgroup-info))
  22709. (format "%s<col align=\"%s\"></col>%s"
  22710. (if (memq gr '(:start :startend))
  22711. (prog1
  22712. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  22713. (setq colgropen t))
  22714. "")
  22715. (if (> (/ (float x) nlines) org-table-number-fraction)
  22716. "right" "left")
  22717. (if (memq gr '(:end :startend))
  22718. (progn (setq colgropen nil) "</colgroup>")
  22719. "")))
  22720. fnum "")
  22721. html)
  22722. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  22723. (push html-table-tag html))
  22724. (concat (mapconcat 'identity html "\n") "\n")))
  22725. (defun org-table-clean-before-export (lines)
  22726. "Check if the table has a marking column.
  22727. If yes remove the column and the special lines."
  22728. (setq org-table-colgroup-info nil)
  22729. (if (memq nil
  22730. (mapcar
  22731. (lambda (x) (or (string-match "^[ \t]*|-" x)
  22732. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  22733. lines))
  22734. (progn
  22735. (setq org-table-clean-did-remove-column nil)
  22736. (delq nil
  22737. (mapcar
  22738. (lambda (x)
  22739. (cond
  22740. ((string-match "^[ \t]*| */ *|" x)
  22741. (setq org-table-colgroup-info
  22742. (mapcar (lambda (x)
  22743. (cond ((member x '("<" "&lt;")) :start)
  22744. ((member x '(">" "&gt;")) :end)
  22745. ((member x '("<>" "&lt;&gt;")) :startend)
  22746. (t nil)))
  22747. (org-split-string x "[ \t]*|[ \t]*")))
  22748. nil)
  22749. (t x)))
  22750. lines)))
  22751. (setq org-table-clean-did-remove-column t)
  22752. (delq nil
  22753. (mapcar
  22754. (lambda (x)
  22755. (cond
  22756. ((string-match "^[ \t]*| */ *|" x)
  22757. (setq org-table-colgroup-info
  22758. (mapcar (lambda (x)
  22759. (cond ((member x '("<" "&lt;")) :start)
  22760. ((member x '(">" "&gt;")) :end)
  22761. ((member x '("<>" "&lt;&gt;")) :startend)
  22762. (t nil)))
  22763. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  22764. nil)
  22765. ((string-match "^[ \t]*| *[!_^/] *|" x)
  22766. nil) ; ignore this line
  22767. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  22768. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  22769. ;; remove the first column
  22770. (replace-match "\\1|" t nil x))))
  22771. lines))))
  22772. (defun org-format-table-table-html (lines)
  22773. "Format a table generated by table.el into HTML.
  22774. This conversion does *not* use `table-generate-source' from table.el.
  22775. This has the advantage that Org-mode's HTML conversions can be used.
  22776. But it has the disadvantage, that no cell- or row-spanning is allowed."
  22777. (let (line field-buffer
  22778. (head org-export-highlight-first-table-line)
  22779. fields html empty)
  22780. (setq html (concat html-table-tag "\n"))
  22781. (while (setq line (pop lines))
  22782. (setq empty "&nbsp;")
  22783. (catch 'next-line
  22784. (if (string-match "^[ \t]*\\+-" line)
  22785. (progn
  22786. (if field-buffer
  22787. (progn
  22788. (setq
  22789. html
  22790. (concat
  22791. html
  22792. "<tr>"
  22793. (mapconcat
  22794. (lambda (x)
  22795. (if (equal x "") (setq x empty))
  22796. (if head
  22797. (concat (car org-export-table-header-tags) x
  22798. (cdr org-export-table-header-tags))
  22799. (concat (car org-export-table-data-tags) x
  22800. (cdr org-export-table-data-tags))))
  22801. field-buffer "\n")
  22802. "</tr>\n"))
  22803. (setq head nil)
  22804. (setq field-buffer nil)))
  22805. ;; Ignore this line
  22806. (throw 'next-line t)))
  22807. ;; Break the line into fields and store the fields
  22808. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  22809. (if field-buffer
  22810. (setq field-buffer (mapcar
  22811. (lambda (x)
  22812. (concat x "<br/>" (pop fields)))
  22813. field-buffer))
  22814. (setq field-buffer fields))))
  22815. (setq html (concat html "</table>\n"))
  22816. html))
  22817. (defun org-format-table-table-html-using-table-generate-source (lines)
  22818. "Format a table into html, using `table-generate-source' from table.el.
  22819. This has the advantage that cell- or row-spanning is allowed.
  22820. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  22821. (require 'table)
  22822. (with-current-buffer (get-buffer-create " org-tmp1 ")
  22823. (erase-buffer)
  22824. (insert (mapconcat 'identity lines "\n"))
  22825. (goto-char (point-min))
  22826. (if (not (re-search-forward "|[^+]" nil t))
  22827. (error "Error processing table"))
  22828. (table-recognize-table)
  22829. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  22830. (table-generate-source 'html " org-tmp2 ")
  22831. (set-buffer " org-tmp2 ")
  22832. (buffer-substring (point-min) (point-max))))
  22833. (defun org-html-handle-time-stamps (s)
  22834. "Format time stamps in string S, or remove them."
  22835. (catch 'exit
  22836. (let (r b)
  22837. (while (string-match org-maybe-keyword-time-regexp s)
  22838. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  22839. ;; never export CLOCK
  22840. (throw 'exit ""))
  22841. (or b (setq b (substring s 0 (match-beginning 0))))
  22842. (if (not org-export-with-timestamps)
  22843. (setq r (concat r (substring s 0 (match-beginning 0)))
  22844. s (substring s (match-end 0)))
  22845. (setq r (concat
  22846. r (substring s 0 (match-beginning 0))
  22847. (if (match-end 1)
  22848. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  22849. (match-string 1 s)))
  22850. (format " @<span class=\"timestamp\">%s@</span>"
  22851. (substring
  22852. (org-translate-time (match-string 3 s)) 1 -1)))
  22853. s (substring s (match-end 0)))))
  22854. ;; Line break if line started and ended with time stamp stuff
  22855. (if (not r)
  22856. s
  22857. (setq r (concat r s))
  22858. (unless (string-match "\\S-" (concat b s))
  22859. (setq r (concat r "@<br/>")))
  22860. r))))
  22861. (defun org-html-protect (s)
  22862. ;; convert & to &amp;, < to &lt; and > to &gt;
  22863. (let ((start 0))
  22864. (while (string-match "&" s start)
  22865. (setq s (replace-match "&amp;" t t s)
  22866. start (1+ (match-beginning 0))))
  22867. (while (string-match "<" s)
  22868. (setq s (replace-match "&lt;" t t s)))
  22869. (while (string-match ">" s)
  22870. (setq s (replace-match "&gt;" t t s))))
  22871. s)
  22872. (defun org-export-cleanup-toc-line (s)
  22873. "Remove tags and time staps from lines going into the toc."
  22874. (when (memq org-export-with-tags '(not-in-toc nil))
  22875. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  22876. (setq s (replace-match "" t t s))))
  22877. (when org-export-remove-timestamps-from-toc
  22878. (while (string-match org-maybe-keyword-time-regexp s)
  22879. (setq s (replace-match "" t t s))))
  22880. (while (string-match org-bracket-link-regexp s)
  22881. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  22882. t t s)))
  22883. s)
  22884. (defun org-html-expand (string)
  22885. "Prepare STRING for HTML export. Applies all active conversions.
  22886. If there are links in the string, don't modify these."
  22887. (let* ((re (concat org-bracket-link-regexp "\\|"
  22888. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  22889. m s l res)
  22890. (while (setq m (string-match re string))
  22891. (setq s (substring string 0 m)
  22892. l (match-string 0 string)
  22893. string (substring string (match-end 0)))
  22894. (push (org-html-do-expand s) res)
  22895. (push l res))
  22896. (push (org-html-do-expand string) res)
  22897. (apply 'concat (nreverse res))))
  22898. (defun org-html-do-expand (s)
  22899. "Apply all active conversions to translate special ASCII to HTML."
  22900. (setq s (org-html-protect s))
  22901. (if org-export-html-expand
  22902. (let ((start 0))
  22903. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  22904. (setq s (replace-match "<\\1>" t nil s)))))
  22905. (if org-export-with-emphasize
  22906. (setq s (org-export-html-convert-emphasize s)))
  22907. (if org-export-with-special-strings
  22908. (setq s (org-export-html-convert-special-strings s)))
  22909. (if org-export-with-sub-superscripts
  22910. (setq s (org-export-html-convert-sub-super s)))
  22911. (if org-export-with-TeX-macros
  22912. (let ((start 0) wd ass)
  22913. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  22914. (if (get-text-property (match-beginning 0) 'org-protected s)
  22915. (setq start (match-end 0))
  22916. (setq wd (match-string 1 s))
  22917. (if (setq ass (assoc wd org-html-entities))
  22918. (setq s (replace-match (or (cdr ass)
  22919. (concat "&" (car ass) ";"))
  22920. t t s))
  22921. (setq start (+ start (length wd))))))))
  22922. s)
  22923. (defun org-create-multibrace-regexp (left right n)
  22924. "Create a regular expression which will match a balanced sexp.
  22925. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  22926. as single character strings.
  22927. The regexp returned will match the entire expression including the
  22928. delimiters. It will also define a single group which contains the
  22929. match except for the outermost delimiters. The maximum depth of
  22930. stacked delimiters is N. Escaping delimiters is not possible."
  22931. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  22932. (or "\\|")
  22933. (re nothing)
  22934. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  22935. (while (> n 1)
  22936. (setq n (1- n)
  22937. re (concat re or next)
  22938. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  22939. (concat left "\\(" re "\\)" right)))
  22940. (defvar org-match-substring-regexp
  22941. (concat
  22942. "\\([^\\]\\)\\([_^]\\)\\("
  22943. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  22944. "\\|"
  22945. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  22946. "\\|"
  22947. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  22948. "The regular expression matching a sub- or superscript.")
  22949. (defvar org-match-substring-with-braces-regexp
  22950. (concat
  22951. "\\([^\\]\\)\\([_^]\\)\\("
  22952. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  22953. "\\)")
  22954. "The regular expression matching a sub- or superscript, forcing braces.")
  22955. (defconst org-export-html-special-string-regexps
  22956. '(("\\\\-" . "&shy;")
  22957. ("---\\([^-]\\)" . "&mdash;\\1")
  22958. ("--\\([^-]\\)" . "&ndash;\\1")
  22959. ("\\.\\.\\." . "&hellip;"))
  22960. "Regular expressions for special string conversion.")
  22961. (defun org-export-html-convert-special-strings (string)
  22962. "Convert special characters in STRING to HTML."
  22963. (let ((all org-export-html-special-string-regexps)
  22964. e a re rpl start)
  22965. (while (setq a (pop all))
  22966. (setq re (car a) rpl (cdr a) start 0)
  22967. (while (string-match re string start)
  22968. (if (get-text-property (match-beginning 0) 'org-protected string)
  22969. (setq start (match-end 0))
  22970. (setq string (replace-match rpl t nil string)))))
  22971. string))
  22972. (defun org-export-html-convert-sub-super (string)
  22973. "Convert sub- and superscripts in STRING to HTML."
  22974. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  22975. (while (string-match org-match-substring-regexp string s)
  22976. (cond
  22977. ((and requireb (match-end 8)) (setq s (match-end 2)))
  22978. ((get-text-property (match-beginning 2) 'org-protected string)
  22979. (setq s (match-end 2)))
  22980. (t
  22981. (setq s (match-end 1)
  22982. key (if (string= (match-string 2 string) "_") "sub" "sup")
  22983. c (or (match-string 8 string)
  22984. (match-string 6 string)
  22985. (match-string 5 string))
  22986. string (replace-match
  22987. (concat (match-string 1 string)
  22988. "<" key ">" c "</" key ">")
  22989. t t string)))))
  22990. (while (string-match "\\\\\\([_^]\\)" string)
  22991. (setq string (replace-match (match-string 1 string) t t string)))
  22992. string))
  22993. (defun org-export-html-convert-emphasize (string)
  22994. "Apply emphasis."
  22995. (let ((s 0) rpl)
  22996. (while (string-match org-emph-re string s)
  22997. (if (not (equal
  22998. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  22999. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  23000. (setq s (match-beginning 0)
  23001. rpl
  23002. (concat
  23003. (match-string 1 string)
  23004. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  23005. (match-string 4 string)
  23006. (nth 3 (assoc (match-string 3 string)
  23007. org-emphasis-alist))
  23008. (match-string 5 string))
  23009. string (replace-match rpl t t string)
  23010. s (+ s (- (length rpl) 2)))
  23011. (setq s (1+ s))))
  23012. string))
  23013. (defvar org-par-open nil)
  23014. (defun org-open-par ()
  23015. "Insert <p>, but first close previous paragraph if any."
  23016. (org-close-par-maybe)
  23017. (insert "\n<p>")
  23018. (setq org-par-open t))
  23019. (defun org-close-par-maybe ()
  23020. "Close paragraph if there is one open."
  23021. (when org-par-open
  23022. (insert "</p>")
  23023. (setq org-par-open nil)))
  23024. (defun org-close-li ()
  23025. "Close <li> if necessary."
  23026. (org-close-par-maybe)
  23027. (insert "</li>\n"))
  23028. (defvar body-only) ; dynamically scoped into this.
  23029. (defun org-html-level-start (level title umax with-toc head-count)
  23030. "Insert a new level in HTML export.
  23031. When TITLE is nil, just close all open levels."
  23032. (org-close-par-maybe)
  23033. (let ((l org-level-max))
  23034. (while (>= l level)
  23035. (if (aref org-levels-open (1- l))
  23036. (progn
  23037. (org-html-level-close l umax)
  23038. (aset org-levels-open (1- l) nil)))
  23039. (setq l (1- l)))
  23040. (when title
  23041. ;; If title is nil, this means this function is called to close
  23042. ;; all levels, so the rest is done only if title is given
  23043. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23044. (setq title (replace-match
  23045. (if org-export-with-tags
  23046. (save-match-data
  23047. (concat
  23048. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  23049. (mapconcat 'identity (org-split-string
  23050. (match-string 1 title) ":")
  23051. "&nbsp;")
  23052. "</span>"))
  23053. "")
  23054. t t title)))
  23055. (if (> level umax)
  23056. (progn
  23057. (if (aref org-levels-open (1- level))
  23058. (progn
  23059. (org-close-li)
  23060. (insert "<li>" title "<br/>\n"))
  23061. (aset org-levels-open (1- level) t)
  23062. (org-close-par-maybe)
  23063. (insert "<ul>\n<li>" title "<br/>\n")))
  23064. (aset org-levels-open (1- level) t)
  23065. (if (and org-export-with-section-numbers (not body-only))
  23066. (setq title (concat (org-section-number level) " " title)))
  23067. (setq level (+ level org-export-html-toplevel-hlevel -1))
  23068. (if with-toc
  23069. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  23070. level level head-count title level))
  23071. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  23072. (org-open-par)))))
  23073. (defun org-html-level-close (level max-outline-level)
  23074. "Terminate one level in HTML export."
  23075. (if (<= level max-outline-level)
  23076. (insert "</div>\n")
  23077. (org-close-li)
  23078. (insert "</ul>\n")))
  23079. ;;; iCalendar export
  23080. ;;;###autoload
  23081. (defun org-export-icalendar-this-file ()
  23082. "Export current file as an iCalendar file.
  23083. The iCalendar file will be located in the same directory as the Org-mode
  23084. file, but with extension `.ics'."
  23085. (interactive)
  23086. (org-export-icalendar nil buffer-file-name))
  23087. ;;;###autoload
  23088. (defun org-export-icalendar-all-agenda-files ()
  23089. "Export all files in `org-agenda-files' to iCalendar .ics files.
  23090. Each iCalendar file will be located in the same directory as the Org-mode
  23091. file, but with extension `.ics'."
  23092. (interactive)
  23093. (apply 'org-export-icalendar nil (org-agenda-files t)))
  23094. ;;;###autoload
  23095. (defun org-export-icalendar-combine-agenda-files ()
  23096. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  23097. The file is stored under the name `org-combined-agenda-icalendar-file'."
  23098. (interactive)
  23099. (apply 'org-export-icalendar t (org-agenda-files t)))
  23100. (defun org-export-icalendar (combine &rest files)
  23101. "Create iCalendar files for all elements of FILES.
  23102. If COMBINE is non-nil, combine all calendar entries into a single large
  23103. file and store it under the name `org-combined-agenda-icalendar-file'."
  23104. (save-excursion
  23105. (org-prepare-agenda-buffers files)
  23106. (let* ((dir (org-export-directory
  23107. :ical (list :publishing-directory
  23108. org-export-publishing-directory)))
  23109. file ical-file ical-buffer category started org-agenda-new-buffers)
  23110. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  23111. (when combine
  23112. (setq ical-file
  23113. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  23114. org-combined-agenda-icalendar-file
  23115. (expand-file-name org-combined-agenda-icalendar-file dir))
  23116. ical-buffer (org-get-agenda-file-buffer ical-file))
  23117. (set-buffer ical-buffer) (erase-buffer))
  23118. (while (setq file (pop files))
  23119. (catch 'nextfile
  23120. (org-check-agenda-file file)
  23121. (set-buffer (org-get-agenda-file-buffer file))
  23122. (unless combine
  23123. (setq ical-file (concat (file-name-as-directory dir)
  23124. (file-name-sans-extension
  23125. (file-name-nondirectory buffer-file-name))
  23126. ".ics"))
  23127. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  23128. (with-current-buffer ical-buffer (erase-buffer)))
  23129. (setq category (or org-category
  23130. (file-name-sans-extension
  23131. (file-name-nondirectory buffer-file-name))))
  23132. (if (symbolp category) (setq category (symbol-name category)))
  23133. (let ((standard-output ical-buffer))
  23134. (if combine
  23135. (and (not started) (setq started t)
  23136. (org-start-icalendar-file org-icalendar-combined-name))
  23137. (org-start-icalendar-file category))
  23138. (org-print-icalendar-entries combine)
  23139. (when (or (and combine (not files)) (not combine))
  23140. (org-finish-icalendar-file)
  23141. (set-buffer ical-buffer)
  23142. (save-buffer)
  23143. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  23144. (org-release-buffers org-agenda-new-buffers))))
  23145. (defvar org-after-save-iCalendar-file-hook nil
  23146. "Hook run after an iCalendar file has been saved.
  23147. The iCalendar buffer is still current when this hook is run.
  23148. A good way to use this is to tell a desktop calenndar application to re-read
  23149. the iCalendar file.")
  23150. (defun org-print-icalendar-entries (&optional combine)
  23151. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  23152. When COMBINE is non nil, add the category to each line."
  23153. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  23154. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  23155. (dts (org-ical-ts-to-string
  23156. (format-time-string (cdr org-time-stamp-formats) (current-time))
  23157. "DTSTART"))
  23158. hd ts ts2 state status (inc t) pos b sexp rrule
  23159. scheduledp deadlinep tmp pri category entry location summary desc
  23160. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  23161. (org-refresh-category-properties)
  23162. (save-excursion
  23163. (goto-char (point-min))
  23164. (while (re-search-forward re1 nil t)
  23165. (catch :skip
  23166. (org-agenda-skip)
  23167. (setq pos (match-beginning 0)
  23168. ts (match-string 0)
  23169. inc t
  23170. hd (org-get-heading)
  23171. summary (org-icalendar-cleanup-string
  23172. (org-entry-get nil "SUMMARY"))
  23173. desc (org-icalendar-cleanup-string
  23174. (or (org-entry-get nil "DESCRIPTION")
  23175. (and org-icalendar-include-body (org-get-entry)))
  23176. t org-icalendar-include-body)
  23177. location (org-icalendar-cleanup-string
  23178. (org-entry-get nil "LOCATION"))
  23179. category (org-get-category))
  23180. (if (looking-at re2)
  23181. (progn
  23182. (goto-char (match-end 0))
  23183. (setq ts2 (match-string 1) inc nil))
  23184. (setq tmp (buffer-substring (max (point-min)
  23185. (- pos org-ds-keyword-length))
  23186. pos)
  23187. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  23188. (progn
  23189. (setq inc nil)
  23190. (replace-match "\\1" t nil ts))
  23191. ts)
  23192. deadlinep (string-match org-deadline-regexp tmp)
  23193. scheduledp (string-match org-scheduled-regexp tmp)
  23194. ;; donep (org-entry-is-done-p)
  23195. ))
  23196. (if (or (string-match org-tr-regexp hd)
  23197. (string-match org-ts-regexp hd))
  23198. (setq hd (replace-match "" t t hd)))
  23199. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  23200. (setq rrule
  23201. (concat "\nRRULE:FREQ="
  23202. (cdr (assoc
  23203. (match-string 2 ts)
  23204. '(("d" . "DAILY")("w" . "WEEKLY")
  23205. ("m" . "MONTHLY")("y" . "YEARLY"))))
  23206. ";INTERVAL=" (match-string 1 ts)))
  23207. (setq rrule ""))
  23208. (setq summary (or summary hd))
  23209. (if (string-match org-bracket-link-regexp summary)
  23210. (setq summary
  23211. (replace-match (if (match-end 3)
  23212. (match-string 3 summary)
  23213. (match-string 1 summary))
  23214. t t summary)))
  23215. (if deadlinep (setq summary (concat "DL: " summary)))
  23216. (if scheduledp (setq summary (concat "S: " summary)))
  23217. (if (string-match "\\`<%%" ts)
  23218. (with-current-buffer sexp-buffer
  23219. (insert (substring ts 1 -1) " " summary "\n"))
  23220. (princ (format "BEGIN:VEVENT
  23221. %s
  23222. %s%s
  23223. SUMMARY:%s%s%s
  23224. CATEGORIES:%s
  23225. END:VEVENT\n"
  23226. (org-ical-ts-to-string ts "DTSTART")
  23227. (org-ical-ts-to-string ts2 "DTEND" inc)
  23228. rrule summary
  23229. (if (and desc (string-match "\\S-" desc))
  23230. (concat "\nDESCRIPTION: " desc) "")
  23231. (if (and location (string-match "\\S-" location))
  23232. (concat "\nLOCATION: " location) "")
  23233. category)))))
  23234. (when (and org-icalendar-include-sexps
  23235. (condition-case nil (require 'icalendar) (error nil))
  23236. (fboundp 'icalendar-export-region))
  23237. ;; Get all the literal sexps
  23238. (goto-char (point-min))
  23239. (while (re-search-forward "^&?%%(" nil t)
  23240. (catch :skip
  23241. (org-agenda-skip)
  23242. (setq b (match-beginning 0))
  23243. (goto-char (1- (match-end 0)))
  23244. (forward-sexp 1)
  23245. (end-of-line 1)
  23246. (setq sexp (buffer-substring b (point)))
  23247. (with-current-buffer sexp-buffer
  23248. (insert sexp "\n"))
  23249. (princ (org-diary-to-ical-string sexp-buffer)))))
  23250. (when org-icalendar-include-todo
  23251. (goto-char (point-min))
  23252. (while (re-search-forward org-todo-line-regexp nil t)
  23253. (catch :skip
  23254. (org-agenda-skip)
  23255. (setq state (match-string 2))
  23256. (setq status (if (member state org-done-keywords)
  23257. "COMPLETED" "NEEDS-ACTION"))
  23258. (when (and state
  23259. (or (not (member state org-done-keywords))
  23260. (eq org-icalendar-include-todo 'all))
  23261. (not (member org-archive-tag (org-get-tags-at)))
  23262. )
  23263. (setq hd (match-string 3)
  23264. summary (org-icalendar-cleanup-string
  23265. (org-entry-get nil "SUMMARY"))
  23266. desc (org-icalendar-cleanup-string
  23267. (or (org-entry-get nil "DESCRIPTION")
  23268. (and org-icalendar-include-body (org-get-entry)))
  23269. t org-icalendar-include-body)
  23270. location (org-icalendar-cleanup-string
  23271. (org-entry-get nil "LOCATION")))
  23272. (if (string-match org-bracket-link-regexp hd)
  23273. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  23274. (match-string 1 hd))
  23275. t t hd)))
  23276. (if (string-match org-priority-regexp hd)
  23277. (setq pri (string-to-char (match-string 2 hd))
  23278. hd (concat (substring hd 0 (match-beginning 1))
  23279. (substring hd (match-end 1))))
  23280. (setq pri org-default-priority))
  23281. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  23282. (- org-lowest-priority org-highest-priority))))))
  23283. (princ (format "BEGIN:VTODO
  23284. %s
  23285. SUMMARY:%s%s%s
  23286. CATEGORIES:%s
  23287. SEQUENCE:1
  23288. PRIORITY:%d
  23289. STATUS:%s
  23290. END:VTODO\n"
  23291. dts
  23292. (or summary hd)
  23293. (if (and location (string-match "\\S-" location))
  23294. (concat "\nLOCATION: " location) "")
  23295. (if (and desc (string-match "\\S-" desc))
  23296. (concat "\nDESCRIPTION: " desc) "")
  23297. category pri status)))))))))
  23298. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  23299. "Take out stuff and quote what needs to be quoted.
  23300. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  23301. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  23302. characters."
  23303. (if (not s)
  23304. nil
  23305. (when is-body
  23306. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  23307. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  23308. (while (string-match re s) (setq s (replace-match "" t t s)))
  23309. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  23310. (let ((start 0))
  23311. (while (string-match "\\([,;\\]\\)" s start)
  23312. (setq start (+ (match-beginning 0) 2)
  23313. s (replace-match "\\\\\\1" nil nil s))))
  23314. (when is-body
  23315. (while (string-match "[ \t]*\n[ \t]*" s)
  23316. (setq s (replace-match "\\n" t t s))))
  23317. (setq s (org-trim s))
  23318. (if is-body
  23319. (if maxlength
  23320. (if (and (numberp maxlength)
  23321. (> (length s) maxlength))
  23322. (setq s (substring s 0 maxlength)))))
  23323. s))
  23324. (defun org-get-entry ()
  23325. "Clean-up description string."
  23326. (save-excursion
  23327. (org-back-to-heading t)
  23328. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  23329. (defun org-start-icalendar-file (name)
  23330. "Start an iCalendar file by inserting the header."
  23331. (let ((user user-full-name)
  23332. (name (or name "unknown"))
  23333. (timezone (cadr (current-time-zone))))
  23334. (princ
  23335. (format "BEGIN:VCALENDAR
  23336. VERSION:2.0
  23337. X-WR-CALNAME:%s
  23338. PRODID:-//%s//Emacs with Org-mode//EN
  23339. X-WR-TIMEZONE:%s
  23340. CALSCALE:GREGORIAN\n" name user timezone))))
  23341. (defun org-finish-icalendar-file ()
  23342. "Finish an iCalendar file by inserting the END statement."
  23343. (princ "END:VCALENDAR\n"))
  23344. (defun org-ical-ts-to-string (s keyword &optional inc)
  23345. "Take a time string S and convert it to iCalendar format.
  23346. KEYWORD is added in front, to make a complete line like DTSTART....
  23347. When INC is non-nil, increase the hour by two (if time string contains
  23348. a time), or the day by one (if it does not contain a time)."
  23349. (let ((t1 (org-parse-time-string s 'nodefault))
  23350. t2 fmt have-time time)
  23351. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  23352. (setq t2 t1 have-time t)
  23353. (setq t2 (org-parse-time-string s)))
  23354. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  23355. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  23356. (when inc
  23357. (if have-time
  23358. (if org-agenda-default-appointment-duration
  23359. (setq mi (+ org-agenda-default-appointment-duration mi))
  23360. (setq h (+ 2 h)))
  23361. (setq d (1+ d))))
  23362. (setq time (encode-time s mi h d m y)))
  23363. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  23364. (concat keyword (format-time-string fmt time))))
  23365. ;;; XOXO export
  23366. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  23367. (with-current-buffer buffer
  23368. (apply 'insert output)))
  23369. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  23370. (defun org-export-as-xoxo (&optional buffer)
  23371. "Export the org buffer as XOXO.
  23372. The XOXO buffer is named *xoxo-<source buffer name>*"
  23373. (interactive (list (current-buffer)))
  23374. ;; A quickie abstraction
  23375. ;; Output everything as XOXO
  23376. (with-current-buffer (get-buffer buffer)
  23377. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  23378. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23379. (org-infile-export-plist)))
  23380. (filename (concat (file-name-as-directory
  23381. (org-export-directory :xoxo opt-plist))
  23382. (file-name-sans-extension
  23383. (file-name-nondirectory buffer-file-name))
  23384. ".html"))
  23385. (out (find-file-noselect filename))
  23386. (last-level 1)
  23387. (hanging-li nil))
  23388. ;; Check the output buffer is empty.
  23389. (with-current-buffer out (erase-buffer))
  23390. ;; Kick off the output
  23391. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  23392. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  23393. (let* ((hd (match-string-no-properties 1))
  23394. (level (length hd))
  23395. (text (concat
  23396. (match-string-no-properties 2)
  23397. (save-excursion
  23398. (goto-char (match-end 0))
  23399. (let ((str ""))
  23400. (catch 'loop
  23401. (while 't
  23402. (forward-line)
  23403. (if (looking-at "^[ \t]\\(.*\\)")
  23404. (setq str (concat str (match-string-no-properties 1)))
  23405. (throw 'loop str)))))))))
  23406. ;; Handle level rendering
  23407. (cond
  23408. ((> level last-level)
  23409. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  23410. ((< level last-level)
  23411. (dotimes (- (- last-level level) 1)
  23412. (if hanging-li
  23413. (org-export-as-xoxo-insert-into out "</li>\n"))
  23414. (org-export-as-xoxo-insert-into out "</ol>\n"))
  23415. (when hanging-li
  23416. (org-export-as-xoxo-insert-into out "</li>\n")
  23417. (setq hanging-li nil)))
  23418. ((equal level last-level)
  23419. (if hanging-li
  23420. (org-export-as-xoxo-insert-into out "</li>\n")))
  23421. )
  23422. (setq last-level level)
  23423. ;; And output the new li
  23424. (setq hanging-li 't)
  23425. (if (equal ?+ (elt text 0))
  23426. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  23427. (org-export-as-xoxo-insert-into out "<li>" text))))
  23428. ;; Finally finish off the ol
  23429. (dotimes (- last-level 1)
  23430. (if hanging-li
  23431. (org-export-as-xoxo-insert-into out "</li>\n"))
  23432. (org-export-as-xoxo-insert-into out "</ol>\n"))
  23433. ;; Finish the buffer off and clean it up.
  23434. (switch-to-buffer-other-window out)
  23435. (indent-region (point-min) (point-max) nil)
  23436. (save-buffer)
  23437. (goto-char (point-min))
  23438. )))
  23439. ;;;; Key bindings
  23440. ;; Make `C-c C-x' a prefix key
  23441. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  23442. ;; TAB key with modifiers
  23443. (org-defkey org-mode-map "\C-i" 'org-cycle)
  23444. (org-defkey org-mode-map [(tab)] 'org-cycle)
  23445. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  23446. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  23447. (org-defkey org-mode-map "\M-\t" 'org-complete)
  23448. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  23449. ;; The following line is necessary under Suse GNU/Linux
  23450. (unless (featurep 'xemacs)
  23451. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  23452. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  23453. (define-key org-mode-map [backtab] 'org-shifttab)
  23454. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  23455. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  23456. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  23457. ;; Cursor keys with modifiers
  23458. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  23459. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  23460. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  23461. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  23462. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  23463. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  23464. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  23465. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  23466. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  23467. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  23468. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  23469. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  23470. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  23471. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  23472. ;;; Extra keys for tty access.
  23473. ;; We only set them when really needed because otherwise the
  23474. ;; menus don't show the simple keys
  23475. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  23476. (not window-system))
  23477. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  23478. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  23479. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  23480. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  23481. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  23482. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  23483. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  23484. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  23485. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  23486. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  23487. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  23488. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  23489. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  23490. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  23491. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  23492. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  23493. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  23494. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  23495. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  23496. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  23497. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  23498. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  23499. ;; All the other keys
  23500. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  23501. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  23502. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  23503. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  23504. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  23505. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  23506. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  23507. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  23508. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  23509. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  23510. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  23511. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  23512. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  23513. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  23514. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  23515. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  23516. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  23517. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  23518. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  23519. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  23520. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  23521. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  23522. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  23523. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  23524. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  23525. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  23526. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  23527. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  23528. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  23529. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  23530. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  23531. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  23532. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  23533. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  23534. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  23535. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  23536. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  23537. (org-defkey org-mode-map "\C-c^" 'org-sort)
  23538. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  23539. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  23540. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  23541. (org-defkey org-mode-map "\C-m" 'org-return)
  23542. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  23543. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  23544. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  23545. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  23546. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  23547. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  23548. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  23549. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  23550. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  23551. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  23552. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  23553. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  23554. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  23555. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  23556. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  23557. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  23558. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  23559. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  23560. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  23561. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  23562. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  23563. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  23564. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  23565. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  23566. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  23567. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  23568. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  23569. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  23570. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  23571. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  23572. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  23573. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  23574. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  23575. (when (featurep 'xemacs)
  23576. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  23577. (defsubst org-table-p () (org-at-table-p))
  23578. (defun org-self-insert-command (N)
  23579. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  23580. If the cursor is in a table looking at whitespace, the whitespace is
  23581. overwritten, and the table is not marked as requiring realignment."
  23582. (interactive "p")
  23583. (if (and (org-table-p)
  23584. (progn
  23585. ;; check if we blank the field, and if that triggers align
  23586. (and org-table-auto-blank-field
  23587. (member last-command
  23588. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  23589. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  23590. ;; got extra space, this field does not determine column width
  23591. (let (org-table-may-need-update) (org-table-blank-field))
  23592. ;; no extra space, this field may determine column width
  23593. (org-table-blank-field)))
  23594. t)
  23595. (eq N 1)
  23596. (looking-at "[^|\n]* |"))
  23597. (let (org-table-may-need-update)
  23598. (goto-char (1- (match-end 0)))
  23599. (delete-backward-char 1)
  23600. (goto-char (match-beginning 0))
  23601. (self-insert-command N))
  23602. (setq org-table-may-need-update t)
  23603. (self-insert-command N)
  23604. (org-fix-tags-on-the-fly)))
  23605. (defun org-fix-tags-on-the-fly ()
  23606. (when (and (equal (char-after (point-at-bol)) ?*)
  23607. (org-on-heading-p))
  23608. (org-align-tags-here org-tags-column)))
  23609. (defun org-delete-backward-char (N)
  23610. "Like `delete-backward-char', insert whitespace at field end in tables.
  23611. When deleting backwards, in tables this function will insert whitespace in
  23612. front of the next \"|\" separator, to keep the table aligned. The table will
  23613. still be marked for re-alignment if the field did fill the entire column,
  23614. because, in this case the deletion might narrow the column."
  23615. (interactive "p")
  23616. (if (and (org-table-p)
  23617. (eq N 1)
  23618. (string-match "|" (buffer-substring (point-at-bol) (point)))
  23619. (looking-at ".*?|"))
  23620. (let ((pos (point))
  23621. (noalign (looking-at "[^|\n\r]* |"))
  23622. (c org-table-may-need-update))
  23623. (backward-delete-char N)
  23624. (skip-chars-forward "^|")
  23625. (insert " ")
  23626. (goto-char (1- pos))
  23627. ;; noalign: if there were two spaces at the end, this field
  23628. ;; does not determine the width of the column.
  23629. (if noalign (setq org-table-may-need-update c)))
  23630. (backward-delete-char N)
  23631. (org-fix-tags-on-the-fly)))
  23632. (defun org-delete-char (N)
  23633. "Like `delete-char', but insert whitespace at field end in tables.
  23634. When deleting characters, in tables this function will insert whitespace in
  23635. front of the next \"|\" separator, to keep the table aligned. The table will
  23636. still be marked for re-alignment if the field did fill the entire column,
  23637. because, in this case the deletion might narrow the column."
  23638. (interactive "p")
  23639. (if (and (org-table-p)
  23640. (not (bolp))
  23641. (not (= (char-after) ?|))
  23642. (eq N 1))
  23643. (if (looking-at ".*?|")
  23644. (let ((pos (point))
  23645. (noalign (looking-at "[^|\n\r]* |"))
  23646. (c org-table-may-need-update))
  23647. (replace-match (concat
  23648. (substring (match-string 0) 1 -1)
  23649. " |"))
  23650. (goto-char pos)
  23651. ;; noalign: if there were two spaces at the end, this field
  23652. ;; does not determine the width of the column.
  23653. (if noalign (setq org-table-may-need-update c)))
  23654. (delete-char N))
  23655. (delete-char N)
  23656. (org-fix-tags-on-the-fly)))
  23657. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  23658. (put 'org-self-insert-command 'delete-selection t)
  23659. (put 'orgtbl-self-insert-command 'delete-selection t)
  23660. (put 'org-delete-char 'delete-selection 'supersede)
  23661. (put 'org-delete-backward-char 'delete-selection 'supersede)
  23662. ;; Make `flyspell-mode' delay after some commands
  23663. (put 'org-self-insert-command 'flyspell-delayed t)
  23664. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  23665. (put 'org-delete-char 'flyspell-delayed t)
  23666. (put 'org-delete-backward-char 'flyspell-delayed t)
  23667. ;; Make pabbrev-mode expand after org-mode commands
  23668. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  23669. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  23670. ;; How to do this: Measure non-white length of current string
  23671. ;; If equal to column width, we should realign.
  23672. (defun org-remap (map &rest commands)
  23673. "In MAP, remap the functions given in COMMANDS.
  23674. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  23675. (let (new old)
  23676. (while commands
  23677. (setq old (pop commands) new (pop commands))
  23678. (if (fboundp 'command-remapping)
  23679. (org-defkey map (vector 'remap old) new)
  23680. (substitute-key-definition old new map global-map)))))
  23681. (when (eq org-enable-table-editor 'optimized)
  23682. ;; If the user wants maximum table support, we need to hijack
  23683. ;; some standard editing functions
  23684. (org-remap org-mode-map
  23685. 'self-insert-command 'org-self-insert-command
  23686. 'delete-char 'org-delete-char
  23687. 'delete-backward-char 'org-delete-backward-char)
  23688. (org-defkey org-mode-map "|" 'org-force-self-insert))
  23689. (defun org-shiftcursor-error ()
  23690. "Throw an error because Shift-Cursor command was applied in wrong context."
  23691. (error "This command is active in special context like tables, headlines or timestamps"))
  23692. (defun org-shifttab (&optional arg)
  23693. "Global visibility cycling or move to previous table field.
  23694. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  23695. on context.
  23696. See the individual commands for more information."
  23697. (interactive "P")
  23698. (cond
  23699. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  23700. (arg (message "Content view to level: ")
  23701. (org-content (prefix-numeric-value arg))
  23702. (setq org-cycle-global-status 'overview))
  23703. (t (call-interactively 'org-global-cycle))))
  23704. (defun org-shiftmetaleft ()
  23705. "Promote subtree or delete table column.
  23706. Calls `org-promote-subtree', `org-outdent-item',
  23707. or `org-table-delete-column', depending on context.
  23708. See the individual commands for more information."
  23709. (interactive)
  23710. (cond
  23711. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  23712. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  23713. ((org-at-item-p) (call-interactively 'org-outdent-item))
  23714. (t (org-shiftcursor-error))))
  23715. (defun org-shiftmetaright ()
  23716. "Demote subtree or insert table column.
  23717. Calls `org-demote-subtree', `org-indent-item',
  23718. or `org-table-insert-column', depending on context.
  23719. See the individual commands for more information."
  23720. (interactive)
  23721. (cond
  23722. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  23723. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  23724. ((org-at-item-p) (call-interactively 'org-indent-item))
  23725. (t (org-shiftcursor-error))))
  23726. (defun org-shiftmetaup (&optional arg)
  23727. "Move subtree up or kill table row.
  23728. Calls `org-move-subtree-up' or `org-table-kill-row' or
  23729. `org-move-item-up' depending on context. See the individual commands
  23730. for more information."
  23731. (interactive "P")
  23732. (cond
  23733. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  23734. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  23735. ((org-at-item-p) (call-interactively 'org-move-item-up))
  23736. (t (org-shiftcursor-error))))
  23737. (defun org-shiftmetadown (&optional arg)
  23738. "Move subtree down or insert table row.
  23739. Calls `org-move-subtree-down' or `org-table-insert-row' or
  23740. `org-move-item-down', depending on context. See the individual
  23741. commands for more information."
  23742. (interactive "P")
  23743. (cond
  23744. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  23745. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  23746. ((org-at-item-p) (call-interactively 'org-move-item-down))
  23747. (t (org-shiftcursor-error))))
  23748. (defun org-metaleft (&optional arg)
  23749. "Promote heading or move table column to left.
  23750. Calls `org-do-promote' or `org-table-move-column', depending on context.
  23751. With no specific context, calls the Emacs default `backward-word'.
  23752. See the individual commands for more information."
  23753. (interactive "P")
  23754. (cond
  23755. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  23756. ((or (org-on-heading-p) (org-region-active-p))
  23757. (call-interactively 'org-do-promote))
  23758. ((org-at-item-p) (call-interactively 'org-outdent-item))
  23759. (t (call-interactively 'backward-word))))
  23760. (defun org-metaright (&optional arg)
  23761. "Demote subtree or move table column to right.
  23762. Calls `org-do-demote' or `org-table-move-column', depending on context.
  23763. With no specific context, calls the Emacs default `forward-word'.
  23764. See the individual commands for more information."
  23765. (interactive "P")
  23766. (cond
  23767. ((org-at-table-p) (call-interactively 'org-table-move-column))
  23768. ((or (org-on-heading-p) (org-region-active-p))
  23769. (call-interactively 'org-do-demote))
  23770. ((org-at-item-p) (call-interactively 'org-indent-item))
  23771. (t (call-interactively 'forward-word))))
  23772. (defun org-metaup (&optional arg)
  23773. "Move subtree up or move table row up.
  23774. Calls `org-move-subtree-up' or `org-table-move-row' or
  23775. `org-move-item-up', depending on context. See the individual commands
  23776. for more information."
  23777. (interactive "P")
  23778. (cond
  23779. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  23780. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  23781. ((org-at-item-p) (call-interactively 'org-move-item-up))
  23782. (t (transpose-lines 1) (beginning-of-line -1))))
  23783. (defun org-metadown (&optional arg)
  23784. "Move subtree down or move table row down.
  23785. Calls `org-move-subtree-down' or `org-table-move-row' or
  23786. `org-move-item-down', depending on context. See the individual
  23787. commands for more information."
  23788. (interactive "P")
  23789. (cond
  23790. ((org-at-table-p) (call-interactively 'org-table-move-row))
  23791. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  23792. ((org-at-item-p) (call-interactively 'org-move-item-down))
  23793. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  23794. (defun org-shiftup (&optional arg)
  23795. "Increase item in timestamp or increase priority of current headline.
  23796. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  23797. depending on context. See the individual commands for more information."
  23798. (interactive "P")
  23799. (cond
  23800. ((org-at-timestamp-p t)
  23801. (call-interactively (if org-edit-timestamp-down-means-later
  23802. 'org-timestamp-down 'org-timestamp-up)))
  23803. ((org-on-heading-p) (call-interactively 'org-priority-up))
  23804. ((org-at-item-p) (call-interactively 'org-previous-item))
  23805. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  23806. (defun org-shiftdown (&optional arg)
  23807. "Decrease item in timestamp or decrease priority of current headline.
  23808. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  23809. depending on context. See the individual commands for more information."
  23810. (interactive "P")
  23811. (cond
  23812. ((org-at-timestamp-p t)
  23813. (call-interactively (if org-edit-timestamp-down-means-later
  23814. 'org-timestamp-up 'org-timestamp-down)))
  23815. ((org-on-heading-p) (call-interactively 'org-priority-down))
  23816. (t (call-interactively 'org-next-item))))
  23817. (defun org-shiftright ()
  23818. "Next TODO keyword or timestamp one day later, depending on context."
  23819. (interactive)
  23820. (cond
  23821. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  23822. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  23823. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  23824. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  23825. (t (org-shiftcursor-error))))
  23826. (defun org-shiftleft ()
  23827. "Previous TODO keyword or timestamp one day earlier, depending on context."
  23828. (interactive)
  23829. (cond
  23830. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  23831. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  23832. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  23833. ((org-at-property-p)
  23834. (call-interactively 'org-property-previous-allowed-value))
  23835. (t (org-shiftcursor-error))))
  23836. (defun org-shiftcontrolright ()
  23837. "Switch to next TODO set."
  23838. (interactive)
  23839. (cond
  23840. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  23841. (t (org-shiftcursor-error))))
  23842. (defun org-shiftcontrolleft ()
  23843. "Switch to previous TODO set."
  23844. (interactive)
  23845. (cond
  23846. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  23847. (t (org-shiftcursor-error))))
  23848. (defun org-ctrl-c-ret ()
  23849. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  23850. (interactive)
  23851. (cond
  23852. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  23853. (t (call-interactively 'org-insert-heading))))
  23854. (defun org-copy-special ()
  23855. "Copy region in table or copy current subtree.
  23856. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  23857. See the individual commands for more information."
  23858. (interactive)
  23859. (call-interactively
  23860. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  23861. (defun org-cut-special ()
  23862. "Cut region in table or cut current subtree.
  23863. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  23864. See the individual commands for more information."
  23865. (interactive)
  23866. (call-interactively
  23867. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  23868. (defun org-paste-special (arg)
  23869. "Paste rectangular region into table, or past subtree relative to level.
  23870. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  23871. See the individual commands for more information."
  23872. (interactive "P")
  23873. (if (org-at-table-p)
  23874. (org-table-paste-rectangle)
  23875. (org-paste-subtree arg)))
  23876. (defun org-ctrl-c-ctrl-c (&optional arg)
  23877. "Set tags in headline, or update according to changed information at point.
  23878. This command does many different things, depending on context:
  23879. - If the cursor is in a headline, prompt for tags and insert them
  23880. into the current line, aligned to `org-tags-column'. When called
  23881. with prefix arg, realign all tags in the current buffer.
  23882. - If the cursor is in one of the special #+KEYWORD lines, this
  23883. triggers scanning the buffer for these lines and updating the
  23884. information.
  23885. - If the cursor is inside a table, realign the table. This command
  23886. works even if the automatic table editor has been turned off.
  23887. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  23888. the entire table.
  23889. - If the cursor is a the beginning of a dynamic block, update it.
  23890. - If the cursor is inside a table created by the table.el package,
  23891. activate that table.
  23892. - If the current buffer is a remember buffer, close note and file it.
  23893. with a prefix argument, file it without further interaction to the default
  23894. location.
  23895. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  23896. links in this buffer.
  23897. - If the cursor is on a numbered item in a plain list, renumber the
  23898. ordered list.
  23899. - If the cursor is on a checkbox, toggle it."
  23900. (interactive "P")
  23901. (let ((org-enable-table-editor t))
  23902. (cond
  23903. ((or org-clock-overlays
  23904. org-occur-highlights
  23905. org-latex-fragment-image-overlays)
  23906. (org-remove-clock-overlays)
  23907. (org-remove-occur-highlights)
  23908. (org-remove-latex-fragment-image-overlays)
  23909. (message "Temporary highlights/overlays removed from current buffer"))
  23910. ((and (local-variable-p 'org-finish-function (current-buffer))
  23911. (fboundp org-finish-function))
  23912. (funcall org-finish-function))
  23913. ((org-at-property-p)
  23914. (call-interactively 'org-property-action))
  23915. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  23916. ((org-on-heading-p) (call-interactively 'org-set-tags))
  23917. ((org-at-table.el-p)
  23918. (require 'table)
  23919. (beginning-of-line 1)
  23920. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  23921. (call-interactively 'table-recognize-table))
  23922. ((org-at-table-p)
  23923. (org-table-maybe-eval-formula)
  23924. (if arg
  23925. (call-interactively 'org-table-recalculate)
  23926. (org-table-maybe-recalculate-line))
  23927. (call-interactively 'org-table-align))
  23928. ((org-at-item-checkbox-p)
  23929. (call-interactively 'org-toggle-checkbox))
  23930. ((org-at-item-p)
  23931. (call-interactively 'org-maybe-renumber-ordered-list))
  23932. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  23933. ;; Dynamic block
  23934. (beginning-of-line 1)
  23935. (org-update-dblock))
  23936. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  23937. (cond
  23938. ((equal (match-string 1) "TBLFM")
  23939. ;; Recalculate the table before this line
  23940. (save-excursion
  23941. (beginning-of-line 1)
  23942. (skip-chars-backward " \r\n\t")
  23943. (if (org-at-table-p)
  23944. (org-call-with-arg 'org-table-recalculate t))))
  23945. (t
  23946. (call-interactively 'org-mode-restart))))
  23947. (t (error "C-c C-c can do nothing useful at this location.")))))
  23948. (defun org-mode-restart ()
  23949. "Restart Org-mode, to scan again for special lines.
  23950. Also updates the keyword regular expressions."
  23951. (interactive)
  23952. (let ((org-inhibit-startup t)) (org-mode))
  23953. (message "Org-mode restarted to refresh keyword and special line setup"))
  23954. (defun org-kill-note-or-show-branches ()
  23955. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  23956. (interactive)
  23957. (if (not org-finish-function)
  23958. (call-interactively 'show-branches)
  23959. (let ((org-note-abort t))
  23960. (funcall org-finish-function))))
  23961. (defun org-return ()
  23962. "Goto next table row or insert a newline.
  23963. Calls `org-table-next-row' or `newline', depending on context.
  23964. See the individual commands for more information."
  23965. (interactive)
  23966. (cond
  23967. ((bobp) (newline))
  23968. ((org-at-table-p)
  23969. (org-table-justify-field-maybe)
  23970. (call-interactively 'org-table-next-row))
  23971. (t (newline))))
  23972. (defun org-ctrl-c-minus ()
  23973. "Insert separator line in table or modify bullet type in list.
  23974. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  23975. depending on context."
  23976. (interactive)
  23977. (cond
  23978. ((org-at-table-p)
  23979. (call-interactively 'org-table-insert-hline))
  23980. ((org-on-heading-p)
  23981. ;; Convert to item
  23982. (save-excursion
  23983. (beginning-of-line 1)
  23984. (if (looking-at "\\*+ ")
  23985. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  23986. ((org-in-item-p)
  23987. (call-interactively 'org-cycle-list-bullet))
  23988. (t (error "`C-c -' does have no function here."))))
  23989. (defun org-meta-return (&optional arg)
  23990. "Insert a new heading or wrap a region in a table.
  23991. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  23992. See the individual commands for more information."
  23993. (interactive "P")
  23994. (cond
  23995. ((org-at-table-p)
  23996. (call-interactively 'org-table-wrap-region))
  23997. (t (call-interactively 'org-insert-heading))))
  23998. ;;; Menu entries
  23999. ;; Define the Org-mode menus
  24000. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  24001. '("Tbl"
  24002. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  24003. ["Next Field" org-cycle (org-at-table-p)]
  24004. ["Previous Field" org-shifttab (org-at-table-p)]
  24005. ["Next Row" org-return (org-at-table-p)]
  24006. "--"
  24007. ["Blank Field" org-table-blank-field (org-at-table-p)]
  24008. ["Edit Field" org-table-edit-field (org-at-table-p)]
  24009. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  24010. "--"
  24011. ("Column"
  24012. ["Move Column Left" org-metaleft (org-at-table-p)]
  24013. ["Move Column Right" org-metaright (org-at-table-p)]
  24014. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  24015. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  24016. ("Row"
  24017. ["Move Row Up" org-metaup (org-at-table-p)]
  24018. ["Move Row Down" org-metadown (org-at-table-p)]
  24019. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  24020. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  24021. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  24022. "--"
  24023. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  24024. ("Rectangle"
  24025. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  24026. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  24027. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  24028. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  24029. "--"
  24030. ("Calculate"
  24031. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  24032. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  24033. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  24034. "--"
  24035. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  24036. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  24037. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  24038. "--"
  24039. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  24040. "--"
  24041. ["Sum Column/Rectangle" org-table-sum
  24042. (or (org-at-table-p) (org-region-active-p))]
  24043. ["Which Column?" org-table-current-column (org-at-table-p)])
  24044. ["Debug Formulas"
  24045. org-table-toggle-formula-debugger
  24046. :style toggle :selected org-table-formula-debug]
  24047. ["Show Col/Row Numbers"
  24048. org-table-toggle-coordinate-overlays
  24049. :style toggle :selected org-table-overlay-coordinates]
  24050. "--"
  24051. ["Create" org-table-create (and (not (org-at-table-p))
  24052. org-enable-table-editor)]
  24053. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  24054. ["Import from File" org-table-import (not (org-at-table-p))]
  24055. ["Export to File" org-table-export (org-at-table-p)]
  24056. "--"
  24057. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  24058. (easy-menu-define org-org-menu org-mode-map "Org menu"
  24059. '("Org"
  24060. ("Show/Hide"
  24061. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  24062. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  24063. ["Sparse Tree" org-occur t]
  24064. ["Reveal Context" org-reveal t]
  24065. ["Show All" show-all t]
  24066. "--"
  24067. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  24068. "--"
  24069. ["New Heading" org-insert-heading t]
  24070. ("Navigate Headings"
  24071. ["Up" outline-up-heading t]
  24072. ["Next" outline-next-visible-heading t]
  24073. ["Previous" outline-previous-visible-heading t]
  24074. ["Next Same Level" outline-forward-same-level t]
  24075. ["Previous Same Level" outline-backward-same-level t]
  24076. "--"
  24077. ["Jump" org-goto t])
  24078. ("Edit Structure"
  24079. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  24080. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  24081. "--"
  24082. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  24083. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  24084. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  24085. "--"
  24086. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  24087. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  24088. ["Demote Heading" org-metaright (not (org-at-table-p))]
  24089. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  24090. "--"
  24091. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  24092. "--"
  24093. ["Convert to odd levels" org-convert-to-odd-levels t]
  24094. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  24095. ("Editing"
  24096. ["Emphasis..." org-emphasize t])
  24097. ("Archive"
  24098. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  24099. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  24100. ; :active t :keys "C-u C-c C-x C-a"]
  24101. ["Sparse trees open ARCHIVE trees"
  24102. (setq org-sparse-tree-open-archived-trees
  24103. (not org-sparse-tree-open-archived-trees))
  24104. :style toggle :selected org-sparse-tree-open-archived-trees]
  24105. ["Cycling opens ARCHIVE trees"
  24106. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  24107. :style toggle :selected org-cycle-open-archived-trees]
  24108. ["Agenda includes ARCHIVE trees"
  24109. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  24110. :style toggle :selected (not org-agenda-skip-archived-trees)]
  24111. "--"
  24112. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  24113. ; ["Check and Move Children" (org-archive-subtree '(4))
  24114. ; :active t :keys "C-u C-c C-x C-s"]
  24115. )
  24116. "--"
  24117. ("TODO Lists"
  24118. ["TODO/DONE/-" org-todo t]
  24119. ("Select keyword"
  24120. ["Next keyword" org-shiftright (org-on-heading-p)]
  24121. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  24122. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  24123. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  24124. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  24125. ["Show TODO Tree" org-show-todo-tree t]
  24126. ["Global TODO list" org-todo-list t]
  24127. "--"
  24128. ["Set Priority" org-priority t]
  24129. ["Priority Up" org-shiftup t]
  24130. ["Priority Down" org-shiftdown t])
  24131. ("TAGS and Properties"
  24132. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  24133. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  24134. "--"
  24135. ["Set property" 'org-set-property t]
  24136. ["Column view of properties" org-columns t]
  24137. ["Insert Column View DBlock" org-insert-columns-dblock t])
  24138. ("Dates and Scheduling"
  24139. ["Timestamp" org-time-stamp t]
  24140. ["Timestamp (inactive)" org-time-stamp-inactive t]
  24141. ("Change Date"
  24142. ["1 Day Later" org-shiftright t]
  24143. ["1 Day Earlier" org-shiftleft t]
  24144. ["1 ... Later" org-shiftup t]
  24145. ["1 ... Earlier" org-shiftdown t])
  24146. ["Compute Time Range" org-evaluate-time-range t]
  24147. ["Schedule Item" org-schedule t]
  24148. ["Deadline" org-deadline t]
  24149. "--"
  24150. ["Custom time format" org-toggle-time-stamp-overlays
  24151. :style radio :selected org-display-custom-times]
  24152. "--"
  24153. ["Goto Calendar" org-goto-calendar t]
  24154. ["Date from Calendar" org-date-from-calendar t])
  24155. ("Logging work"
  24156. ["Clock in" org-clock-in t]
  24157. ["Clock out" org-clock-out t]
  24158. ["Clock cancel" org-clock-cancel t]
  24159. ["Goto running clock" org-clock-goto t]
  24160. ["Display times" org-clock-display t]
  24161. ["Create clock table" org-clock-report t]
  24162. "--"
  24163. ["Record DONE time"
  24164. (progn (setq org-log-done (not org-log-done))
  24165. (message "Switching to %s will %s record a timestamp"
  24166. (car org-done-keywords)
  24167. (if org-log-done "automatically" "not")))
  24168. :style toggle :selected org-log-done])
  24169. "--"
  24170. ["Agenda Command..." org-agenda t]
  24171. ("File List for Agenda")
  24172. ("Special views current file"
  24173. ["TODO Tree" org-show-todo-tree t]
  24174. ["Check Deadlines" org-check-deadlines t]
  24175. ["Timeline" org-timeline t]
  24176. ["Tags Tree" org-tags-sparse-tree t])
  24177. "--"
  24178. ("Hyperlinks"
  24179. ["Store Link (Global)" org-store-link t]
  24180. ["Insert Link" org-insert-link t]
  24181. ["Follow Link" org-open-at-point t]
  24182. "--"
  24183. ["Next link" org-next-link t]
  24184. ["Previous link" org-previous-link t]
  24185. "--"
  24186. ["Descriptive Links"
  24187. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  24188. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  24189. ["Literal Links"
  24190. (progn
  24191. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  24192. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  24193. "--"
  24194. ["Export/Publish..." org-export t]
  24195. ("LaTeX"
  24196. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  24197. :selected org-cdlatex-mode]
  24198. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  24199. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  24200. ["Modify math symbol" org-cdlatex-math-modify
  24201. (org-inside-LaTeX-fragment-p)]
  24202. ["Export LaTeX fragments as images"
  24203. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  24204. :style toggle :selected org-export-with-LaTeX-fragments])
  24205. "--"
  24206. ("Documentation"
  24207. ["Show Version" org-version t]
  24208. ["Info Documentation" org-info t])
  24209. ("Customize"
  24210. ["Browse Org Group" org-customize t]
  24211. "--"
  24212. ["Expand This Menu" org-create-customize-menu
  24213. (fboundp 'customize-menu-create)])
  24214. "--"
  24215. ["Refresh setup" org-mode-restart t]
  24216. ))
  24217. (defun org-info (&optional node)
  24218. "Read documentation for Org-mode in the info system.
  24219. With optional NODE, go directly to that node."
  24220. (interactive)
  24221. (require 'info)
  24222. (Info-goto-node (format "(org)%s" (or node ""))))
  24223. (defun org-install-agenda-files-menu ()
  24224. (let ((bl (buffer-list)))
  24225. (save-excursion
  24226. (while bl
  24227. (set-buffer (pop bl))
  24228. (if (org-mode-p) (setq bl nil)))
  24229. (when (org-mode-p)
  24230. (easy-menu-change
  24231. '("Org") "File List for Agenda"
  24232. (append
  24233. (list
  24234. ["Edit File List" (org-edit-agenda-file-list) t]
  24235. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  24236. ["Remove Current File from List" org-remove-file t]
  24237. ["Cycle through agenda files" org-cycle-agenda-files t]
  24238. ["Occur in all agenda files" org-occur-in-agenda-files t]
  24239. "--")
  24240. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  24241. ;;;; Documentation
  24242. (defun org-customize ()
  24243. "Call the customize function with org as argument."
  24244. (interactive)
  24245. (customize-browse 'org))
  24246. (defun org-create-customize-menu ()
  24247. "Create a full customization menu for Org-mode, insert it into the menu."
  24248. (interactive)
  24249. (if (fboundp 'customize-menu-create)
  24250. (progn
  24251. (easy-menu-change
  24252. '("Org") "Customize"
  24253. `(["Browse Org group" org-customize t]
  24254. "--"
  24255. ,(customize-menu-create 'org)
  24256. ["Set" Custom-set t]
  24257. ["Save" Custom-save t]
  24258. ["Reset to Current" Custom-reset-current t]
  24259. ["Reset to Saved" Custom-reset-saved t]
  24260. ["Reset to Standard Settings" Custom-reset-standard t]))
  24261. (message "\"Org\"-menu now contains full customization menu"))
  24262. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  24263. ;;;; Miscellaneous stuff
  24264. ;;; Generally useful functions
  24265. (defun org-context ()
  24266. "Return a list of contexts of the current cursor position.
  24267. If several contexts apply, all are returned.
  24268. Each context entry is a list with a symbol naming the context, and
  24269. two positions indicating start and end of the context. Possible
  24270. contexts are:
  24271. :headline anywhere in a headline
  24272. :headline-stars on the leading stars in a headline
  24273. :todo-keyword on a TODO keyword (including DONE) in a headline
  24274. :tags on the TAGS in a headline
  24275. :priority on the priority cookie in a headline
  24276. :item on the first line of a plain list item
  24277. :item-bullet on the bullet/number of a plain list item
  24278. :checkbox on the checkbox in a plain list item
  24279. :table in an org-mode table
  24280. :table-special on a special filed in a table
  24281. :table-table in a table.el table
  24282. :link on a hyperlink
  24283. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  24284. :target on a <<target>>
  24285. :radio-target on a <<<radio-target>>>
  24286. :latex-fragment on a LaTeX fragment
  24287. :latex-preview on a LaTeX fragment with overlayed preview image
  24288. This function expects the position to be visible because it uses font-lock
  24289. faces as a help to recognize the following contexts: :table-special, :link,
  24290. and :keyword."
  24291. (let* ((f (get-text-property (point) 'face))
  24292. (faces (if (listp f) f (list f)))
  24293. (p (point)) clist o)
  24294. ;; First the large context
  24295. (cond
  24296. ((org-on-heading-p t)
  24297. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  24298. (when (progn
  24299. (beginning-of-line 1)
  24300. (looking-at org-todo-line-tags-regexp))
  24301. (push (org-point-in-group p 1 :headline-stars) clist)
  24302. (push (org-point-in-group p 2 :todo-keyword) clist)
  24303. (push (org-point-in-group p 4 :tags) clist))
  24304. (goto-char p)
  24305. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  24306. (if (looking-at "\\[#[A-Z0-9]\\]")
  24307. (push (org-point-in-group p 0 :priority) clist)))
  24308. ((org-at-item-p)
  24309. (push (org-point-in-group p 2 :item-bullet) clist)
  24310. (push (list :item (point-at-bol)
  24311. (save-excursion (org-end-of-item) (point)))
  24312. clist)
  24313. (and (org-at-item-checkbox-p)
  24314. (push (org-point-in-group p 0 :checkbox) clist)))
  24315. ((org-at-table-p)
  24316. (push (list :table (org-table-begin) (org-table-end)) clist)
  24317. (if (memq 'org-formula faces)
  24318. (push (list :table-special
  24319. (previous-single-property-change p 'face)
  24320. (next-single-property-change p 'face)) clist)))
  24321. ((org-at-table-p 'any)
  24322. (push (list :table-table) clist)))
  24323. (goto-char p)
  24324. ;; Now the small context
  24325. (cond
  24326. ((org-at-timestamp-p)
  24327. (push (org-point-in-group p 0 :timestamp) clist))
  24328. ((memq 'org-link faces)
  24329. (push (list :link
  24330. (previous-single-property-change p 'face)
  24331. (next-single-property-change p 'face)) clist))
  24332. ((memq 'org-special-keyword faces)
  24333. (push (list :keyword
  24334. (previous-single-property-change p 'face)
  24335. (next-single-property-change p 'face)) clist))
  24336. ((org-on-target-p)
  24337. (push (org-point-in-group p 0 :target) clist)
  24338. (goto-char (1- (match-beginning 0)))
  24339. (if (looking-at org-radio-target-regexp)
  24340. (push (org-point-in-group p 0 :radio-target) clist))
  24341. (goto-char p))
  24342. ((setq o (car (delq nil
  24343. (mapcar
  24344. (lambda (x)
  24345. (if (memq x org-latex-fragment-image-overlays) x))
  24346. (org-overlays-at (point))))))
  24347. (push (list :latex-fragment
  24348. (org-overlay-start o) (org-overlay-end o)) clist)
  24349. (push (list :latex-preview
  24350. (org-overlay-start o) (org-overlay-end o)) clist))
  24351. ((org-inside-LaTeX-fragment-p)
  24352. ;; FIXME: positions wrong.
  24353. (push (list :latex-fragment (point) (point)) clist)))
  24354. (setq clist (nreverse (delq nil clist)))
  24355. clist))
  24356. ;; FIXME: Compare with at-regexp-p Do we need both?
  24357. (defun org-in-regexp (re &optional nlines visually)
  24358. "Check if point is inside a match of regexp.
  24359. Normally only the current line is checked, but you can include NLINES extra
  24360. lines both before and after point into the search.
  24361. If VISUALLY is set, require that the cursor is not after the match but
  24362. really on, so that the block visually is on the match."
  24363. (catch 'exit
  24364. (let ((pos (point))
  24365. (eol (point-at-eol (+ 1 (or nlines 0))))
  24366. (inc (if visually 1 0)))
  24367. (save-excursion
  24368. (beginning-of-line (- 1 (or nlines 0)))
  24369. (while (re-search-forward re eol t)
  24370. (if (and (<= (match-beginning 0) pos)
  24371. (>= (+ inc (match-end 0)) pos))
  24372. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  24373. (defun org-at-regexp-p (regexp)
  24374. "Is point inside a match of REGEXP in the current line?"
  24375. (catch 'exit
  24376. (save-excursion
  24377. (let ((pos (point)) (end (point-at-eol)))
  24378. (beginning-of-line 1)
  24379. (while (re-search-forward regexp end t)
  24380. (if (and (<= (match-beginning 0) pos)
  24381. (>= (match-end 0) pos))
  24382. (throw 'exit t)))
  24383. nil))))
  24384. (defun org-occur-in-agenda-files (regexp &optional nlines)
  24385. "Call `multi-occur' with buffers for all agenda files."
  24386. (interactive "sOrg-files matching: \np")
  24387. (let* ((files (org-agenda-files))
  24388. (tnames (mapcar 'file-truename files))
  24389. (extra org-agenda-multi-occur-extra-files)
  24390. f)
  24391. (while (setq f (pop extra))
  24392. (unless (member (file-truename f) tnames)
  24393. (add-to-list 'files f 'append)
  24394. (add-to-list 'tnames (file-truename f) 'append)))
  24395. (multi-occur
  24396. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  24397. regexp)))
  24398. (defun org-uniquify (list)
  24399. "Remove duplicate elements from LIST."
  24400. (let (res)
  24401. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  24402. res))
  24403. (defun org-delete-all (elts list)
  24404. "Remove all elements in ELTS from LIST."
  24405. (while elts
  24406. (setq list (delete (pop elts) list)))
  24407. list)
  24408. (defun org-point-in-group (point group &optional context)
  24409. "Check if POINT is in match-group GROUP.
  24410. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  24411. match. If the match group does ot exist or point is not inside it,
  24412. return nil."
  24413. (and (match-beginning group)
  24414. (>= point (match-beginning group))
  24415. (<= point (match-end group))
  24416. (if context
  24417. (list context (match-beginning group) (match-end group))
  24418. t)))
  24419. (defun org-switch-to-buffer-other-window (&rest args)
  24420. "Switch to buffer in a second window on the current frame.
  24421. In particular, do not allow pop-up frames."
  24422. (let (pop-up-frames special-display-buffer-names special-display-regexps
  24423. special-display-function)
  24424. (apply 'switch-to-buffer-other-window args)))
  24425. (defun org-combine-plists (&rest plists)
  24426. "Create a single property list from all plists in PLISTS.
  24427. The process starts by copying the first list, and then setting properties
  24428. from the other lists. Settings in the last list are the most significant
  24429. ones and overrule settings in the other lists."
  24430. (let ((rtn (copy-sequence (pop plists)))
  24431. p v ls)
  24432. (while plists
  24433. (setq ls (pop plists))
  24434. (while ls
  24435. (setq p (pop ls) v (pop ls))
  24436. (setq rtn (plist-put rtn p v))))
  24437. rtn))
  24438. (defun org-move-line-down (arg)
  24439. "Move the current line down. With prefix argument, move it past ARG lines."
  24440. (interactive "p")
  24441. (let ((col (current-column))
  24442. beg end pos)
  24443. (beginning-of-line 1) (setq beg (point))
  24444. (beginning-of-line 2) (setq end (point))
  24445. (beginning-of-line (+ 1 arg))
  24446. (setq pos (move-marker (make-marker) (point)))
  24447. (insert (delete-and-extract-region beg end))
  24448. (goto-char pos)
  24449. (move-to-column col)))
  24450. (defun org-move-line-up (arg)
  24451. "Move the current line up. With prefix argument, move it past ARG lines."
  24452. (interactive "p")
  24453. (let ((col (current-column))
  24454. beg end pos)
  24455. (beginning-of-line 1) (setq beg (point))
  24456. (beginning-of-line 2) (setq end (point))
  24457. (beginning-of-line (- arg))
  24458. (setq pos (move-marker (make-marker) (point)))
  24459. (insert (delete-and-extract-region beg end))
  24460. (goto-char pos)
  24461. (move-to-column col)))
  24462. (defun org-replace-escapes (string table)
  24463. "Replace %-escapes in STRING with values in TABLE.
  24464. TABLE is an association list with keys like \"%a\" and string values.
  24465. The sequences in STRING may contain normal field width and padding information,
  24466. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  24467. so values can contain further %-escapes if they are define later in TABLE."
  24468. (let ((case-fold-search nil)
  24469. e re rpl)
  24470. (while (setq e (pop table))
  24471. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  24472. (while (string-match re string)
  24473. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  24474. (cdr e)))
  24475. (setq string (replace-match rpl t t string))))
  24476. string))
  24477. (defun org-sublist (list start end)
  24478. "Return a section of LIST, from START to END.
  24479. Counting starts at 1."
  24480. (let (rtn (c start))
  24481. (setq list (nthcdr (1- start) list))
  24482. (while (and list (<= c end))
  24483. (push (pop list) rtn)
  24484. (setq c (1+ c)))
  24485. (nreverse rtn)))
  24486. (defun org-find-base-buffer-visiting (file)
  24487. "Like `find-buffer-visiting' but alway return the base buffer and
  24488. not an indirect buffer"
  24489. (let ((buf (find-buffer-visiting file)))
  24490. (if buf
  24491. (or (buffer-base-buffer buf) buf)
  24492. nil)))
  24493. (defun org-image-file-name-regexp ()
  24494. "Return regexp matching the file names of images."
  24495. (if (fboundp 'image-file-name-regexp)
  24496. (image-file-name-regexp)
  24497. (let ((image-file-name-extensions
  24498. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  24499. "xbm" "xpm" "pbm" "pgm" "ppm")))
  24500. (concat "\\."
  24501. (regexp-opt (nconc (mapcar 'upcase
  24502. image-file-name-extensions)
  24503. image-file-name-extensions)
  24504. t)
  24505. "\\'"))))
  24506. (defun org-file-image-p (file)
  24507. "Return non-nil if FILE is an image."
  24508. (save-match-data
  24509. (string-match (org-image-file-name-regexp) file)))
  24510. ;;; Paragraph filling stuff.
  24511. ;; We want this to be just right, so use the full arsenal.
  24512. (defun org-indent-line-function ()
  24513. "Indent line like previous, but further if previous was headline or item."
  24514. (interactive)
  24515. (let* ((pos (point))
  24516. (itemp (org-at-item-p))
  24517. column bpos bcol tpos tcol bullet btype bullet-type)
  24518. ;; Find the previous relevant line
  24519. (beginning-of-line 1)
  24520. (cond
  24521. ((looking-at "#") (setq column 0))
  24522. ((looking-at "\\*+ ") (setq column 0))
  24523. (t
  24524. (beginning-of-line 0)
  24525. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  24526. (beginning-of-line 0))
  24527. (cond
  24528. ((looking-at "\\*+[ \t]+")
  24529. (goto-char (match-end 0))
  24530. (setq column (current-column)))
  24531. ((org-in-item-p)
  24532. (org-beginning-of-item)
  24533. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  24534. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  24535. (setq bpos (match-beginning 1) tpos (match-end 0)
  24536. bcol (progn (goto-char bpos) (current-column))
  24537. tcol (progn (goto-char tpos) (current-column))
  24538. bullet (match-string 1)
  24539. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  24540. (if (not itemp)
  24541. (setq column tcol)
  24542. (goto-char pos)
  24543. (beginning-of-line 1)
  24544. (if (looking-at "\\S-")
  24545. (progn
  24546. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  24547. (setq bullet (match-string 1)
  24548. btype (if (string-match "[0-9]" bullet) "n" bullet))
  24549. (setq column (if (equal btype bullet-type) bcol tcol)))
  24550. (setq column (org-get-indentation)))))
  24551. (t (setq column (org-get-indentation))))))
  24552. (goto-char pos)
  24553. (if (<= (current-column) (current-indentation))
  24554. (indent-line-to column)
  24555. (save-excursion (indent-line-to column)))
  24556. (setq column (current-column))
  24557. (beginning-of-line 1)
  24558. (if (looking-at
  24559. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  24560. (replace-match (concat "\\1" (format org-property-format
  24561. (match-string 2) (match-string 3)))
  24562. t nil))
  24563. (move-to-column column)))
  24564. (defun org-set-autofill-regexps ()
  24565. (interactive)
  24566. ;; In the paragraph separator we include headlines, because filling
  24567. ;; text in a line directly attached to a headline would otherwise
  24568. ;; fill the headline as well.
  24569. (org-set-local 'comment-start-skip "^#+[ \t]*")
  24570. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  24571. ;; The paragraph starter includes hand-formatted lists.
  24572. (org-set-local 'paragraph-start
  24573. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  24574. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  24575. ;; But only if the user has not turned off tables or fixed-width regions
  24576. (org-set-local
  24577. 'auto-fill-inhibit-regexp
  24578. (concat "\\*+ \\|#\\+"
  24579. "\\|[ \t]*" org-keyword-time-regexp
  24580. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  24581. (concat
  24582. "\\|[ \t]*["
  24583. (if org-enable-table-editor "|" "")
  24584. (if org-enable-fixed-width-editor ":" "")
  24585. "]"))))
  24586. ;; We use our own fill-paragraph function, to make sure that tables
  24587. ;; and fixed-width regions are not wrapped. That function will pass
  24588. ;; through to `fill-paragraph' when appropriate.
  24589. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  24590. ; Adaptive filling: To get full control, first make sure that
  24591. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  24592. (org-set-local 'adaptive-fill-regexp "\000")
  24593. (org-set-local 'adaptive-fill-function
  24594. 'org-adaptive-fill-function))
  24595. (defun org-fill-paragraph (&optional justify)
  24596. "Re-align a table, pass through to fill-paragraph if no table."
  24597. (let ((table-p (org-at-table-p))
  24598. (table.el-p (org-at-table.el-p)))
  24599. (cond ((and (equal (char-after (point-at-bol)) ?*)
  24600. (save-excursion (goto-char (point-at-bol))
  24601. (looking-at outline-regexp)))
  24602. t) ; skip headlines
  24603. (table.el-p t) ; skip table.el tables
  24604. (table-p (org-table-align) t) ; align org-mode tables
  24605. (t nil)))) ; call paragraph-fill
  24606. ;; For reference, this is the default value of adaptive-fill-regexp
  24607. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  24608. (defun org-adaptive-fill-function ()
  24609. "Return a fill prefix for org-mode files.
  24610. In particular, this makes sure hanging paragraphs for hand-formatted lists
  24611. work correctly."
  24612. (cond ((looking-at "#[ \t]+")
  24613. (match-string 0))
  24614. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  24615. (save-excursion
  24616. (goto-char (match-end 0))
  24617. (make-string (current-column) ?\ )))
  24618. (t nil)))
  24619. ;;;; Functions extending outline functionality
  24620. (defun org-beginning-of-line (&optional arg)
  24621. "Go to the beginning of the current line. If that is invisible, continue
  24622. to a visible line beginning. This makes the function of C-a more intuitive.
  24623. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  24624. first attempt, and only move to after the tags when the cursor is already
  24625. beyond the end of the headline."
  24626. (interactive "P")
  24627. (let ((pos (point)))
  24628. (beginning-of-line 1)
  24629. (if (bobp)
  24630. nil
  24631. (backward-char 1)
  24632. (if (org-invisible-p)
  24633. (while (and (not (bobp)) (org-invisible-p))
  24634. (backward-char 1)
  24635. (beginning-of-line 1))
  24636. (forward-char 1)))
  24637. (when org-special-ctrl-a/e
  24638. (cond
  24639. ((and (looking-at org-todo-line-regexp)
  24640. (= (char-after (match-end 1)) ?\ ))
  24641. (goto-char
  24642. (if (eq org-special-ctrl-a/e t)
  24643. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  24644. ((= pos (point)) (match-beginning 3))
  24645. (t (point)))
  24646. (cond ((> pos (point)) (point))
  24647. ((not (eq last-command this-command)) (point))
  24648. (t (match-beginning 3))))))
  24649. ((org-at-item-p)
  24650. (goto-char
  24651. (if (eq org-special-ctrl-a/e t)
  24652. (cond ((> pos (match-end 4)) (match-end 4))
  24653. ((= pos (point)) (match-end 4))
  24654. (t (point)))
  24655. (cond ((> pos (point)) (point))
  24656. ((not (eq last-command this-command)) (point))
  24657. (t (match-end 4))))))))))
  24658. (defun org-end-of-line (&optional arg)
  24659. "Go to the end of the line.
  24660. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  24661. first attempt, and only move to after the tags when the cursor is already
  24662. beyond the end of the headline."
  24663. (interactive "P")
  24664. (if (or (not org-special-ctrl-a/e)
  24665. (not (org-on-heading-p)))
  24666. (end-of-line arg)
  24667. (let ((pos (point)))
  24668. (beginning-of-line 1)
  24669. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  24670. (if (eq org-special-ctrl-a/e t)
  24671. (if (or (< pos (match-beginning 1))
  24672. (= pos (match-end 0)))
  24673. (goto-char (match-beginning 1))
  24674. (goto-char (match-end 0)))
  24675. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  24676. (goto-char (match-end 0))
  24677. (goto-char (match-beginning 1))))
  24678. (end-of-line arg)))))
  24679. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  24680. (define-key org-mode-map "\C-e" 'org-end-of-line)
  24681. (defun org-invisible-p ()
  24682. "Check if point is at a character currently not visible."
  24683. ;; Early versions of noutline don't have `outline-invisible-p'.
  24684. (if (fboundp 'outline-invisible-p)
  24685. (outline-invisible-p)
  24686. (get-char-property (point) 'invisible)))
  24687. (defun org-invisible-p2 ()
  24688. "Check if point is at a character currently not visible."
  24689. (save-excursion
  24690. (if (and (eolp) (not (bobp))) (backward-char 1))
  24691. ;; Early versions of noutline don't have `outline-invisible-p'.
  24692. (if (fboundp 'outline-invisible-p)
  24693. (outline-invisible-p)
  24694. (get-char-property (point) 'invisible))))
  24695. (defalias 'org-back-to-heading 'outline-back-to-heading)
  24696. (defalias 'org-on-heading-p 'outline-on-heading-p)
  24697. (defalias 'org-at-heading-p 'outline-on-heading-p)
  24698. (defun org-at-heading-or-item-p ()
  24699. (or (org-on-heading-p) (org-at-item-p)))
  24700. (defun org-on-target-p ()
  24701. (or (org-in-regexp org-radio-target-regexp)
  24702. (org-in-regexp org-target-regexp)))
  24703. (defun org-up-heading-all (arg)
  24704. "Move to the heading line of which the present line is a subheading.
  24705. This function considers both visible and invisible heading lines.
  24706. With argument, move up ARG levels."
  24707. (if (fboundp 'outline-up-heading-all)
  24708. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  24709. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  24710. (defun org-up-heading-safe ()
  24711. "Move to the heading line of which the present line is a subheading.
  24712. This version will not throw an error. It will return the level of the
  24713. headline found, or nil if no higher level is found."
  24714. (let ((pos (point)) start-level level
  24715. (re (concat "^" outline-regexp)))
  24716. (catch 'exit
  24717. (outline-back-to-heading t)
  24718. (setq start-level (funcall outline-level))
  24719. (if (equal start-level 1) (throw 'exit nil))
  24720. (while (re-search-backward re nil t)
  24721. (setq level (funcall outline-level))
  24722. (if (< level start-level) (throw 'exit level)))
  24723. nil)))
  24724. (defun org-goto-sibling (&optional previous)
  24725. "Goto the next sibling, even if it is invisible.
  24726. When PREVIOUS is set, go to the previous sibling instead. Returns t
  24727. when a sibling was found. When none is found, return nil and don't
  24728. move point."
  24729. (let ((fun (if previous 're-search-backward 're-search-forward))
  24730. (pos (point))
  24731. (re (concat "^" outline-regexp))
  24732. level l)
  24733. (when (condition-case nil (org-back-to-heading t) (error nil))
  24734. (setq level (funcall outline-level))
  24735. (catch 'exit
  24736. (or previous (forward-char 1))
  24737. (while (funcall fun re nil t)
  24738. (setq l (funcall outline-level))
  24739. (when (< l level) (goto-char pos) (throw 'exit nil))
  24740. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  24741. (goto-char pos)
  24742. nil))))
  24743. (defun org-show-siblings ()
  24744. "Show all siblings of the current headline."
  24745. (save-excursion
  24746. (while (org-goto-sibling) (org-flag-heading nil)))
  24747. (save-excursion
  24748. (while (org-goto-sibling 'previous)
  24749. (org-flag-heading nil))))
  24750. (defun org-show-hidden-entry ()
  24751. "Show an entry where even the heading is hidden."
  24752. (save-excursion
  24753. (org-show-entry)))
  24754. (defun org-flag-heading (flag &optional entry)
  24755. "Flag the current heading. FLAG non-nil means make invisible.
  24756. When ENTRY is non-nil, show the entire entry."
  24757. (save-excursion
  24758. (org-back-to-heading t)
  24759. ;; Check if we should show the entire entry
  24760. (if entry
  24761. (progn
  24762. (org-show-entry)
  24763. (save-excursion
  24764. (and (outline-next-heading)
  24765. (org-flag-heading nil))))
  24766. (outline-flag-region (max (point-min) (1- (point)))
  24767. (save-excursion (outline-end-of-heading) (point))
  24768. flag))))
  24769. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  24770. ;; This is an exact copy of the original function, but it uses
  24771. ;; `org-back-to-heading', to make it work also in invisible
  24772. ;; trees. And is uses an invisible-OK argument.
  24773. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  24774. (org-back-to-heading invisible-OK)
  24775. (let ((first t)
  24776. (level (funcall outline-level)))
  24777. (while (and (not (eobp))
  24778. (or first (> (funcall outline-level) level)))
  24779. (setq first nil)
  24780. (outline-next-heading))
  24781. (unless to-heading
  24782. (if (memq (preceding-char) '(?\n ?\^M))
  24783. (progn
  24784. ;; Go to end of line before heading
  24785. (forward-char -1)
  24786. (if (memq (preceding-char) '(?\n ?\^M))
  24787. ;; leave blank line before heading
  24788. (forward-char -1))))))
  24789. (point))
  24790. (defun org-show-subtree ()
  24791. "Show everything after this heading at deeper levels."
  24792. (outline-flag-region
  24793. (point)
  24794. (save-excursion
  24795. (outline-end-of-subtree) (outline-next-heading) (point))
  24796. nil))
  24797. (defun org-show-entry ()
  24798. "Show the body directly following this heading.
  24799. Show the heading too, if it is currently invisible."
  24800. (interactive)
  24801. (save-excursion
  24802. (condition-case nil
  24803. (progn
  24804. (org-back-to-heading t)
  24805. (outline-flag-region
  24806. (max (point-min) (1- (point)))
  24807. (save-excursion
  24808. (re-search-forward
  24809. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  24810. (or (match-beginning 1) (point-max)))
  24811. nil))
  24812. (error nil))))
  24813. (defun org-make-options-regexp (kwds)
  24814. "Make a regular expression for keyword lines."
  24815. (concat
  24816. "^"
  24817. "#?[ \t]*\\+\\("
  24818. (mapconcat 'regexp-quote kwds "\\|")
  24819. "\\):[ \t]*"
  24820. "\\(.+\\)"))
  24821. ;; Make isearch reveal the necessary context
  24822. (defun org-isearch-end ()
  24823. "Reveal context after isearch exits."
  24824. (when isearch-success ; only if search was successful
  24825. (if (featurep 'xemacs)
  24826. ;; Under XEmacs, the hook is run in the correct place,
  24827. ;; we directly show the context.
  24828. (org-show-context 'isearch)
  24829. ;; In Emacs the hook runs *before* restoring the overlays.
  24830. ;; So we have to use a one-time post-command-hook to do this.
  24831. ;; (Emacs 22 has a special variable, see function `org-mode')
  24832. (unless (and (boundp 'isearch-mode-end-hook-quit)
  24833. isearch-mode-end-hook-quit)
  24834. ;; Only when the isearch was not quitted.
  24835. (org-add-hook 'post-command-hook 'org-isearch-post-command
  24836. 'append 'local)))))
  24837. (defun org-isearch-post-command ()
  24838. "Remove self from hook, and show context."
  24839. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  24840. (org-show-context 'isearch))
  24841. ;;;; Address problems with some other packages
  24842. ;; Make flyspell not check words in links, to not mess up our keymap
  24843. (defun org-mode-flyspell-verify ()
  24844. "Don't let flyspell put overlays at active buttons."
  24845. (not (get-text-property (point) 'keymap)))
  24846. ;; Make `bookmark-jump' show the jump location if it was hidden.
  24847. (eval-after-load "bookmark"
  24848. '(if (boundp 'bookmark-after-jump-hook)
  24849. ;; We can use the hook
  24850. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  24851. ;; Hook not available, use advice
  24852. (defadvice bookmark-jump (after org-make-visible activate)
  24853. "Make the position visible."
  24854. (org-bookmark-jump-unhide))))
  24855. (defun org-bookmark-jump-unhide ()
  24856. "Unhide the current position, to show the bookmark location."
  24857. (and (org-mode-p)
  24858. (or (org-invisible-p)
  24859. (save-excursion (goto-char (max (point-min) (1- (point))))
  24860. (org-invisible-p)))
  24861. (org-show-context 'bookmark-jump)))
  24862. ;; Fix a bug in htmlize where there are text properties (face nil)
  24863. (eval-after-load "htmlize"
  24864. '(progn
  24865. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  24866. "Make sure there are no nil faces"
  24867. (setq ad-return-value (delq nil ad-return-value)))))
  24868. ;; Make session.el ignore our circular variable
  24869. (eval-after-load "session"
  24870. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  24871. ;;;; Experimental code
  24872. (defun org-closed-in-range ()
  24873. "Sparse tree of items closed in a certain time range.
  24874. Still experimental, may disappear in the future."
  24875. (interactive)
  24876. ;; Get the time interval from the user.
  24877. (let* ((time1 (time-to-seconds
  24878. (org-read-date nil 'to-time nil "Starting date: ")))
  24879. (time2 (time-to-seconds
  24880. (org-read-date nil 'to-time nil "End date:")))
  24881. ;; callback function
  24882. (callback (lambda ()
  24883. (let ((time
  24884. (time-to-seconds
  24885. (apply 'encode-time
  24886. (org-parse-time-string
  24887. (match-string 1))))))
  24888. ;; check if time in interval
  24889. (and (>= time time1) (<= time time2))))))
  24890. ;; make tree, check each match with the callback
  24891. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  24892. (defun org-fill-paragraph-experimental (&optional justify)
  24893. "Re-align a table, pass through to fill-paragraph if no table."
  24894. (let ((table-p (org-at-table-p))
  24895. (table.el-p (org-at-table.el-p)))
  24896. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  24897. (table.el-p t) ; skip table.el tables
  24898. (table-p (org-table-align) t) ; align org-mode tables
  24899. ((save-excursion
  24900. (let ((pos (1+ (point-at-eol))))
  24901. (backward-paragraph 1)
  24902. (re-search-forward "\\\\\\\\[ \t]*$" pos t)))
  24903. (save-excursion
  24904. (save-restriction
  24905. (narrow-to-region (1+ (match-end 0)) (point-max))
  24906. (fill-paragraph nil)
  24907. t)))
  24908. (t nil)))) ; call paragraph-fill
  24909. ;; FIXME: this needs a much better algorithm
  24910. (defun org-assign-fast-keys (alist)
  24911. "Assign fast keys to a keyword-key alist.
  24912. Respect keys that are already there."
  24913. (let (new e k c c1 c2 (char ?a))
  24914. (while (setq e (pop alist))
  24915. (cond
  24916. ((equal e '(:startgroup)) (push e new))
  24917. ((equal e '(:endgroup)) (push e new))
  24918. (t
  24919. (setq k (car e) c2 nil)
  24920. (if (cdr e)
  24921. (setq c (cdr e))
  24922. ;; automatically assign a character.
  24923. (setq c1 (string-to-char
  24924. (downcase (substring
  24925. k (if (= (string-to-char k) ?@) 1 0)))))
  24926. (if (or (rassoc c1 new) (rassoc c1 alist))
  24927. (while (or (rassoc char new) (rassoc char alist))
  24928. (setq char (1+ char)))
  24929. (setq c2 c1))
  24930. (setq c (or c2 char)))
  24931. (push (cons k c) new))))
  24932. (nreverse new)))
  24933. (defcustom org-refile-targets '((nil . (:level . 1)))
  24934. "Targets for refiling entries with \\[org-refile].
  24935. This is list of cons cells. Each cell contains:
  24936. - a specification of the files to be considered, either a list of files,
  24937. or a symbol whose function or value fields will be used to retrieve
  24938. a file name or a list of file names. Nil means, refile to a different
  24939. heading in the current buffer.
  24940. - A specification of how to find candidate refile targets. This may be
  24941. any of
  24942. - a cons cell (:tag . \"TAG\") to identify refile targes by a tag.
  24943. This tag has to be present in all target headlines, inheritance will
  24944. not be considered.
  24945. - a cons cell (:todo . \"KEYWORD\" to identify refile targets by
  24946. todo keyword.
  24947. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  24948. headlines that are refiling targets.
  24949. - a cons cell (:level . N). Any headline of level N is considered a target."
  24950. ;; FIXME: what if there are a var and func with same name???
  24951. :group 'org
  24952. :type '(repeat
  24953. (cons
  24954. (choice :value org-agenda-files
  24955. (const :tag "All agenda files" org-agenda-files)
  24956. (const :tag "Current buffer" nil)
  24957. (function) (variable) (file))
  24958. (choice :tag "Identify target headline by"
  24959. (cons :tag "Specific tag" (const :tag) (string))
  24960. (cons :tag "TODO keyword" (const :todo) (string))
  24961. (cons :tag "Regular expression" (const :regexp) (regexp))
  24962. (cons :tag "Level number" (const :level) (integer))))))
  24963. (defvar org-refile-target-table nil
  24964. "The list of refile targets, created by `org-refile'.")
  24965. (defun org-get-refile-targets ()
  24966. "Produce a table with refile targets."
  24967. ;; FIXME: interpret the different specs for refile targets.
  24968. (put 'org-agenda-files 'org-restrict nil)
  24969. (let ((entries org-refile-targets)
  24970. org-agenda-new-files targets txt re files f desc descre)
  24971. (while (setq entry (pop entries))
  24972. (setq files (car entry) desc (cdr entry))
  24973. (cond
  24974. ((null files) (setq files (list (current-buffer))))
  24975. ((and (symbolp files) (fboundp files))
  24976. (setq files (funcall files)))
  24977. ((and (symbolp files) (boundp files))
  24978. (setq files (symbol-value files))))
  24979. (if (stringp files) (setq files (list files)))
  24980. (cond
  24981. ((eq (car desc) :tag)
  24982. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  24983. ((eq (car desc) :todo)
  24984. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  24985. ((eq (car desc) :regexp)
  24986. (setq descre (cdr desc)))
  24987. ((eq (car desc) :level)
  24988. (setq descre (concat "^\\*\\{" (number-to-string
  24989. (if org-odd-levels-only
  24990. (1- (* 2 (cdr desc)))
  24991. (cdr desc)))
  24992. "\\}[ \t]")))
  24993. (t (error "Bad refiling target description %s" desc)))
  24994. (while (setq f (pop files))
  24995. (save-excursion
  24996. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  24997. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  24998. (save-excursion
  24999. (save-restriction
  25000. (widen)
  25001. (goto-char (point-min))
  25002. (while (re-search-forward descre nil t)
  25003. (goto-char (point-at-bol))
  25004. (when (looking-at org-complex-heading-regexp)
  25005. (setq txt (match-string 4)
  25006. re (concat "^" (regexp-quote
  25007. (buffer-substring (match-beginning 1)
  25008. (match-end 4)))))
  25009. (if (match-end 5) (setq re (concat re "[ \t]+"
  25010. (regexp-quote
  25011. (match-string 5)))))
  25012. (setq re (concat re "[ \t]*$"))
  25013. (push (list txt f re (point)) targets))
  25014. (goto-char (point-at-eol))))))))
  25015. (org-release-buffers org-agenda-new-buffers)
  25016. (nreverse targets)))
  25017. (defun org-refile (&optional reversed-or-update)
  25018. "Move the entry at point to another heading.
  25019. The list of target headings is compiled using the information in
  25020. `org-refile-targets', which see. This list is created upon first use, and
  25021. you can update it by calling this command with a double prefix (`C-u C-u').
  25022. At the target location, the entry is filed as a subitem of the target heading.
  25023. Depending on `org-reverse-note-order', the new subitem will either be the
  25024. first of the last subitem. A single C-u prefix will toggle the value of this
  25025. variable for the duration of the command."
  25026. (interactive "P")
  25027. (if (equal reversed-or-update '(16))
  25028. (progn
  25029. (setq org-refile-target-table (org-get-refile-targets))
  25030. (message "Refile targets updated (%d targets)"
  25031. (length org-refile-target-table)))
  25032. (when (or (not org-refile-target-table)
  25033. (and (= (length org-refile-targets) 1)
  25034. (not (caar org-refile-targets))))
  25035. (setq org-refile-target-table (org-get-refile-targets)))
  25036. (unless org-refile-target-table
  25037. (error "No refile targets"))
  25038. (let* ((cbuf (current-buffer))
  25039. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  25040. (fname (and filename (file-truename filename)))
  25041. (tbl (mapcar
  25042. (lambda (x)
  25043. (if (not (equal fname (file-truename (nth 1 x))))
  25044. (cons (concat (car x) " (" (file-name-nondirectory
  25045. (nth 1 x)) ")")
  25046. (cdr x))
  25047. x))
  25048. org-refile-target-table))
  25049. (completion-ignore-case t)
  25050. pos it nbuf file re level reversed)
  25051. (when (setq it (completing-read "Refile to: " tbl
  25052. nil t nil 'org-refile-history))
  25053. (setq it (assoc it tbl)
  25054. file (nth 1 it)
  25055. re (nth 2 it))
  25056. (org-copy-special)
  25057. (save-excursion
  25058. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  25059. (find-file-noselect file))))
  25060. (setq reversed (org-notes-order-reversed-p))
  25061. (if (equal reversed-or-update '(16)) (setq reversed (not reversed)))
  25062. (save-excursion
  25063. (save-restriction
  25064. (widen)
  25065. (goto-char (point-min))
  25066. (unless (re-search-forward re nil t)
  25067. (error "Cannot find target location - try again with `C-u' prefix."))
  25068. (goto-char (match-beginning 0))
  25069. (looking-at outline-regexp)
  25070. (setq level (org-get-legal-level (funcall outline-level) 1))
  25071. (goto-char (or (save-excursion
  25072. (if reversed
  25073. (outline-next-heading)
  25074. (outline-get-next-sibling)))
  25075. (point-max)))
  25076. (org-paste-subtree level))))
  25077. (org-cut-special)
  25078. (message "Entry refiled to \"%s\"" (car it))))))
  25079. (defcustom org-highlight-latex-fragments-and-specials nil
  25080. "Non-nil means, fontify what is treated specially by the exporters."
  25081. :group 'org-font-lock
  25082. :type 'boolean)
  25083. (defvar org-latex-and-specials-regexp nil
  25084. "Regular expression for highlighting export special stuff.")
  25085. (defun org-compute-latex-and-specials-regexp ()
  25086. "Compute regular expression for stuff treated specially by exporters."
  25087. (if (not org-highlight-latex-fragments-and-specials)
  25088. (org-set-local 'org-latex-and-specials-regexp nil)
  25089. (let*
  25090. ((matchers (plist-get org-format-latex-options :matchers))
  25091. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  25092. org-latex-regexps)))
  25093. (options (org-combine-plists (org-default-export-plist)
  25094. (org-infile-export-plist)))
  25095. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  25096. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  25097. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  25098. (org-export-html-expand (plist-get options :expand-quoted-html))
  25099. (org-export-with-special-strings (plist-get options :special-strings))
  25100. (re-sub
  25101. (cond
  25102. ((equal org-export-with-sub-superscripts '{})
  25103. (list org-match-substring-with-braces-regexp))
  25104. (org-export-with-sub-superscripts
  25105. (list org-match-substring-regexp))
  25106. (t nil)))
  25107. (re-latex
  25108. (if org-export-with-LaTeX-fragments
  25109. (mapcar (lambda (x) (nth 1 x)) latexs)))
  25110. (re-macros
  25111. (if org-export-with-TeX-macros
  25112. (list (concat "\\\\"
  25113. (regexp-opt
  25114. (append (mapcar 'car org-html-entities)
  25115. (if (boundp 'org-latex-entities)
  25116. org-latex-entities nil))
  25117. 'words))) ; FIXME
  25118. ))
  25119. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  25120. (re-special (if org-export-with-special-strings
  25121. (mapcar (lambda (x) (car x))
  25122. org-export-html-special-string-regexps)))
  25123. (re-rest
  25124. (delq nil
  25125. (list
  25126. (if org-export-html-expand "@<[^>\n]+>")
  25127. ))))
  25128. (org-set-local
  25129. 'org-latex-and-specials-regexp
  25130. (mapconcat 'identity (append re-latex re-sub re-macros re-special re-rest) "\\|")))))
  25131. (defface org-latex-and-export-specials
  25132. (let ((font (cond ((assq :inherit custom-face-attributes)
  25133. '(:inherit underline))
  25134. (t '(:underline t)))))
  25135. `((((class grayscale) (background light))
  25136. (:foreground "DimGray" ,@font))
  25137. (((class grayscale) (background dark))
  25138. (:foreground "LightGray" ,@font))
  25139. (((class color) (background light))
  25140. (:foreground "SaddleBrown"))
  25141. (((class color) (background dark))
  25142. (:foreground "burlywood"))
  25143. (t (,@font))))
  25144. "Face used to highlight math latex and other special exporter stuff."
  25145. :group 'org-faces)
  25146. (defun org-do-latex-and-special-faces (limit)
  25147. "Run through the buffer and add overlays to links."
  25148. (when org-latex-and-specials-regexp
  25149. (let (rtn d)
  25150. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  25151. limit t))
  25152. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0)) 'face))
  25153. '(org-code org-verbatim underline)))
  25154. (progn
  25155. (setq rtn t
  25156. d (cond ((member (char-after (1+ (match-beginning 0))) '(?_ ?^)) 1)
  25157. (t 0)))
  25158. (font-lock-prepend-text-property
  25159. (+ d (match-beginning 0)) (match-end 0)
  25160. 'face 'org-latex-and-export-specials)
  25161. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  25162. '(font-lock-multiline t)))))
  25163. rtn)))
  25164. ;;;; Finish up
  25165. (provide 'org)
  25166. (run-hooks 'org-load-hook)
  25167. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  25168. ;;; org.el ends here